blob: 747c55fa053fc9c551f11329ab39963b8eddf2e5 [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 Paalanen925788f2018-04-19 14:20:01 +03004 * Copyright © 2012-2018 Collabora, Ltd.
5 * Copyright © 2017, 2018 General Electric Company
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05006 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -07007 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050014 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -070015 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial
17 * portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
23 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
24 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050027 */
28
Kristian Høgsberga9410222011-01-14 17:22:35 -050029#include "config.h"
30
Daniel Stoneb7452fe2012-06-01 12:14:06 +010031#include <fcntl.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040032#include <stdio.h>
33#include <string.h>
34#include <stdlib.h>
35#include <stdint.h>
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +010036#include <limits.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050037#include <stdarg.h>
Benjamin Franzke6f5fc692011-06-21 19:34:19 +020038#include <assert.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040039#include <sys/ioctl.h>
Daniel Stoneb7452fe2012-06-01 12:14:06 +010040#include <sys/mman.h>
Kristian Høgsberg27da5382011-06-21 17:32:25 -040041#include <sys/wait.h>
Pekka Paalanen409ef0a2011-12-02 15:30:21 +020042#include <sys/socket.h>
Martin Minarikf12c2872012-06-11 00:57:39 +020043#include <sys/utsname.h>
Martin Minarik37032f82012-06-18 20:15:18 +020044#include <sys/stat.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040045#include <unistd.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050046#include <math.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040047#include <linux/input.h>
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040048#include <dlfcn.h>
Kristian Høgsberg85449032011-05-02 12:11:07 -040049#include <signal.h>
Kristian Høgsberg0690da62012-01-16 11:53:54 -050050#include <setjmp.h>
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040051#include <sys/time.h>
52#include <time.h>
Pekka Paalanen23ade622014-08-27 13:31:26 +030053#include <errno.h>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050054
Pekka Paalanenb5026542014-11-12 15:09:24 +020055#include "timeline.h"
56
Kristian Høgsberg82863022010-06-04 21:52:02 -040057#include "compositor.h"
Pekka Paalanene95ad5c2016-04-15 14:47:08 +030058#include "viewporter-server-protocol.h"
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020059#include "presentation-time-server-protocol.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070060#include "shared/helpers.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070061#include "shared/os-compatibility.h"
Bryce Harrington25a2bdd2016-08-03 17:40:52 -070062#include "shared/string-helpers.h"
Pekka Paalanenaa21f622015-07-03 15:44:50 +030063#include "shared/timespec-util.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040064#include "git-version.h"
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -050065#include "version.h"
Pekka Paalanen827b5d22016-06-29 11:54:26 +020066#include "plugin-registry.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050067
Pekka Paalanen0513a952014-05-21 16:17:27 +030068#define DEFAULT_REPAINT_WINDOW 7 /* milliseconds */
69
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020070static void
Pekka Paalanend72bad22017-03-29 17:01:41 +030071weston_output_update_matrix(struct weston_output *output);
72
73static void
Alexander Larsson0b135062013-05-28 16:23:36 +020074weston_output_transform_scale_init(struct weston_output *output,
75 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020076
Rob Bradford27b17932013-06-26 18:08:46 +010077static void
Jason Ekstranda7af7042013-10-12 22:38:11 -050078weston_compositor_build_view_list(struct weston_compositor *compositor);
Rob Bradford27b17932013-06-26 18:08:46 +010079
Pekka Paalanendcac3512017-12-08 14:13:34 +020080static char *
81weston_output_create_heads_string(struct weston_output *output);
82
Pekka Paalanen6528c032017-03-28 15:27:10 +030083/** Send wl_output events for mode and scale changes
84 *
85 * \param head Send on all resources bound to this head.
86 * \param mode_changed If true, send the current mode.
87 * \param scale_changed If true, send the current scale.
88 */
89static void
90weston_mode_switch_send_events(struct weston_head *head,
91 bool mode_changed, bool scale_changed)
92{
93 struct weston_output *output = head->output;
94 struct wl_resource *resource;
95 int version;
96
97 wl_resource_for_each(resource, &head->resource_list) {
98 if (mode_changed) {
99 wl_output_send_mode(resource,
100 output->current_mode->flags,
101 output->current_mode->width,
102 output->current_mode->height,
103 output->current_mode->refresh);
104 }
105
106 version = wl_resource_get_version(resource);
107 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION && scale_changed)
108 wl_output_send_scale(resource, output->current_scale);
109
110 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
111 wl_output_send_done(resource);
112 }
113}
114
115static void
116weston_mode_switch_finish(struct weston_output *output,
117 int mode_changed, int scale_changed)
Alex Wu2dda6042012-04-17 17:20:47 +0800118{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200119 struct weston_seat *seat;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300120 struct weston_head *head;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200121 pixman_region32_t old_output_region;
Alexander Larsson355748e2013-05-28 16:23:38 +0200122
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200123 pixman_region32_init(&old_output_region);
124 pixman_region32_copy(&old_output_region, &output->region);
125
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200126 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +0200127 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200128
129 pixman_region32_init(&output->previous_damage);
130 pixman_region32_init_rect(&output->region, output->x, output->y,
131 output->width, output->height);
132
133 weston_output_update_matrix(output);
134
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200135 /* If a pointer falls outside the outputs new geometry, move it to its
136 * lower-right corner */
137 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500138 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200139 int32_t x, y;
140
141 if (!pointer)
142 continue;
143
144 x = wl_fixed_to_int(pointer->x);
145 y = wl_fixed_to_int(pointer->y);
146
147 if (!pixman_region32_contains_point(&old_output_region,
148 x, y, NULL) ||
149 pixman_region32_contains_point(&output->region,
150 x, y, NULL))
151 continue;
152
153 if (x >= output->x + output->width)
154 x = output->x + output->width - 1;
155 if (y >= output->y + output->height)
156 y = output->y + output->height - 1;
157
158 pointer->x = wl_fixed_from_int(x);
159 pointer->y = wl_fixed_from_int(y);
160 }
161
162 pixman_region32_fini(&old_output_region);
163
Derek Foremandd4cd332014-11-10 10:29:59 -0600164 if (!mode_changed && !scale_changed)
165 return;
166
Hardening57388e42013-09-18 23:56:36 +0200167 /* notify clients of the changes */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300168 wl_list_for_each(head, &output->head_list, output_link)
169 weston_mode_switch_send_events(head,
170 mode_changed, scale_changed);
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600171}
172
David Fort0de859e2016-05-27 23:22:57 +0200173static void
174weston_compositor_reflow_outputs(struct weston_compositor *compositor,
175 struct weston_output *resized_output, int delta_width);
176
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600177WL_EXPORT int
178weston_output_mode_set_native(struct weston_output *output,
179 struct weston_mode *mode,
180 int32_t scale)
181{
182 int ret;
183 int mode_changed = 0, scale_changed = 0;
David Fort0de859e2016-05-27 23:22:57 +0200184 int32_t old_width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600185
186 if (!output->switch_mode)
187 return -1;
188
189 if (!output->original_mode) {
190 mode_changed = 1;
191 ret = output->switch_mode(output, mode);
192 if (ret < 0)
193 return ret;
194 if (output->current_scale != scale) {
195 scale_changed = 1;
196 output->current_scale = scale;
Hardening57388e42013-09-18 23:56:36 +0200197 }
198 }
199
David Fort0de859e2016-05-27 23:22:57 +0200200 old_width = output->width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600201 output->native_mode = mode;
202 output->native_scale = scale;
203
204 weston_mode_switch_finish(output, mode_changed, scale_changed);
205
David Fort0de859e2016-05-27 23:22:57 +0200206 if (mode_changed || scale_changed) {
207 weston_compositor_reflow_outputs(output->compositor, output, output->width - old_width);
208
209 wl_signal_emit(&output->compositor->output_resized_signal, output);
210 }
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600211 return 0;
212}
213
214WL_EXPORT int
215weston_output_mode_switch_to_native(struct weston_output *output)
216{
217 int ret;
218 int mode_changed = 0, scale_changed = 0;
219
220 if (!output->switch_mode)
221 return -1;
222
223 if (!output->original_mode) {
224 weston_log("already in the native mode\n");
225 return -1;
226 }
227 /* the non fullscreen clients haven't seen a mode set since we
228 * switched into a temporary, so we need to notify them if the
229 * mode at that time is different from the native mode now.
230 */
231 mode_changed = (output->original_mode != output->native_mode);
232 scale_changed = (output->original_scale != output->native_scale);
233
234 ret = output->switch_mode(output, output->native_mode);
235 if (ret < 0)
236 return ret;
237
238 output->current_scale = output->native_scale;
239
240 output->original_mode = NULL;
241 output->original_scale = 0;
242
243 weston_mode_switch_finish(output, mode_changed, scale_changed);
244
245 return 0;
246}
247
248WL_EXPORT int
249weston_output_mode_switch_to_temporary(struct weston_output *output,
250 struct weston_mode *mode,
251 int32_t scale)
252{
253 int ret;
254
255 if (!output->switch_mode)
256 return -1;
257
258 /* original_mode is the last mode non full screen clients have seen,
259 * so we shouldn't change it if we already have one set.
260 */
261 if (!output->original_mode) {
262 output->original_mode = output->native_mode;
263 output->original_scale = output->native_scale;
264 }
265 ret = output->switch_mode(output, mode);
266 if (ret < 0)
267 return ret;
268
269 output->current_scale = scale;
270
271 weston_mode_switch_finish(output, 0, 0);
272
273 return 0;
Alex Wu2dda6042012-04-17 17:20:47 +0800274}
275
Benjamin Franzke06286262011-05-06 19:12:33 +0200276static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300277region_init_infinite(pixman_region32_t *region)
278{
279 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
280 UINT32_MAX, UINT32_MAX);
281}
282
Pekka Paalanene67858b2013-04-25 13:57:42 +0300283static struct weston_subsurface *
284weston_surface_to_subsurface(struct weston_surface *surface);
285
Jason Ekstranda7af7042013-10-12 22:38:11 -0500286WL_EXPORT struct weston_view *
287weston_view_create(struct weston_surface *surface)
288{
289 struct weston_view *view;
290
Bryce Harringtonde16d892014-11-20 22:21:57 -0800291 view = zalloc(sizeof *view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500292 if (view == NULL)
293 return NULL;
294
295 view->surface = surface;
Daniel Stonefb4869d2016-12-09 16:27:54 +0000296 view->plane = &surface->compositor->primary_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500297
Jason Ekstranda7af7042013-10-12 22:38:11 -0500298 /* Assign to surface */
299 wl_list_insert(&surface->views, &view->surface_link);
300
301 wl_signal_init(&view->destroy_signal);
302 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300303 wl_list_init(&view->layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500304
Jason Ekstranda7af7042013-10-12 22:38:11 -0500305 pixman_region32_init(&view->clip);
306
307 view->alpha = 1.0;
308 pixman_region32_init(&view->transform.opaque);
309
310 wl_list_init(&view->geometry.transformation_list);
311 wl_list_insert(&view->geometry.transformation_list,
312 &view->transform.position.link);
313 weston_matrix_init(&view->transform.position.matrix);
314 wl_list_init(&view->geometry.child_list);
Pekka Paalanen380adf52015-02-16 14:39:11 +0200315 pixman_region32_init(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500316 pixman_region32_init(&view->transform.boundingbox);
317 view->transform.dirty = 1;
318
Jason Ekstranda7af7042013-10-12 22:38:11 -0500319 return view;
320}
321
Jason Ekstrand108865d2014-06-26 10:04:49 -0700322struct weston_frame_callback {
323 struct wl_resource *resource;
324 struct wl_list link;
325};
326
Pekka Paalanen133e4392014-09-23 22:08:46 -0400327struct weston_presentation_feedback {
328 struct wl_resource *resource;
329
330 /* XXX: could use just wl_resource_get_link() instead */
331 struct wl_list link;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200332
333 /* The per-surface feedback flags */
334 uint32_t psf_flags;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400335};
336
337static void
338weston_presentation_feedback_discard(
339 struct weston_presentation_feedback *feedback)
340{
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200341 wp_presentation_feedback_send_discarded(feedback->resource);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400342 wl_resource_destroy(feedback->resource);
343}
344
345static void
346weston_presentation_feedback_discard_list(struct wl_list *list)
347{
348 struct weston_presentation_feedback *feedback, *tmp;
349
350 wl_list_for_each_safe(feedback, tmp, list, link)
351 weston_presentation_feedback_discard(feedback);
352}
353
354static void
355weston_presentation_feedback_present(
356 struct weston_presentation_feedback *feedback,
357 struct weston_output *output,
358 uint32_t refresh_nsec,
359 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200360 uint64_t seq,
361 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400362{
363 struct wl_client *client = wl_resource_get_client(feedback->resource);
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300364 struct weston_head *head;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400365 struct wl_resource *o;
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200366 uint32_t tv_sec_hi;
367 uint32_t tv_sec_lo;
368 uint32_t tv_nsec;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300369 bool done = false;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400370
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300371 wl_list_for_each(head, &output->head_list, output_link) {
372 wl_resource_for_each(o, &head->resource_list) {
373 if (wl_resource_get_client(o) != client)
374 continue;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400375
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300376 wp_presentation_feedback_send_sync_output(feedback->resource, o);
377 done = true;
378 }
379
380 /* For clone mode, send it for just one wl_output global,
381 * they are all equivalent anyway.
382 */
383 if (done)
384 break;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400385 }
386
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200387 timespec_to_proto(ts, &tv_sec_hi, &tv_sec_lo, &tv_nsec);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200388 wp_presentation_feedback_send_presented(feedback->resource,
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200389 tv_sec_hi, tv_sec_lo, tv_nsec,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200390 refresh_nsec,
391 seq >> 32, seq & 0xffffffff,
392 flags | feedback->psf_flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400393 wl_resource_destroy(feedback->resource);
394}
395
396static void
397weston_presentation_feedback_present_list(struct wl_list *list,
398 struct weston_output *output,
399 uint32_t refresh_nsec,
400 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200401 uint64_t seq,
402 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400403{
404 struct weston_presentation_feedback *feedback, *tmp;
405
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200406 assert(!(flags & WP_PRESENTATION_FEEDBACK_INVALID) ||
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200407 wl_list_empty(list));
408
Pekka Paalanen133e4392014-09-23 22:08:46 -0400409 wl_list_for_each_safe(feedback, tmp, list, link)
410 weston_presentation_feedback_present(feedback, output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200411 refresh_nsec, ts, seq,
412 flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400413}
414
Jason Ekstrand7b982072014-05-20 14:33:03 -0500415static void
416surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
417{
418 struct weston_surface_state *state =
419 container_of(listener, struct weston_surface_state,
420 buffer_destroy_listener);
421
422 state->buffer = NULL;
423}
424
425static void
426weston_surface_state_init(struct weston_surface_state *state)
427{
428 state->newly_attached = 0;
429 state->buffer = NULL;
430 state->buffer_destroy_listener.notify =
431 surface_state_handle_buffer_destroy;
432 state->sx = 0;
433 state->sy = 0;
434
Derek Foreman152254b2015-11-26 14:17:48 -0600435 pixman_region32_init(&state->damage_surface);
436 pixman_region32_init(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500437 pixman_region32_init(&state->opaque);
438 region_init_infinite(&state->input);
439
440 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400441 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500442
443 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
444 state->buffer_viewport.buffer.scale = 1;
445 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
446 state->buffer_viewport.surface.width = -1;
447 state->buffer_viewport.changed = 0;
448}
449
450static void
451weston_surface_state_fini(struct weston_surface_state *state)
452{
453 struct weston_frame_callback *cb, *next;
454
455 wl_list_for_each_safe(cb, next,
456 &state->frame_callback_list, link)
457 wl_resource_destroy(cb->resource);
458
Pekka Paalanen133e4392014-09-23 22:08:46 -0400459 weston_presentation_feedback_discard_list(&state->feedback_list);
460
Jason Ekstrand7b982072014-05-20 14:33:03 -0500461 pixman_region32_fini(&state->input);
462 pixman_region32_fini(&state->opaque);
Derek Foreman152254b2015-11-26 14:17:48 -0600463 pixman_region32_fini(&state->damage_surface);
464 pixman_region32_fini(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500465
466 if (state->buffer)
467 wl_list_remove(&state->buffer_destroy_listener.link);
468 state->buffer = NULL;
469}
470
471static void
472weston_surface_state_set_buffer(struct weston_surface_state *state,
473 struct weston_buffer *buffer)
474{
475 if (state->buffer == buffer)
476 return;
477
478 if (state->buffer)
479 wl_list_remove(&state->buffer_destroy_listener.link);
480 state->buffer = buffer;
481 if (state->buffer)
482 wl_signal_add(&state->buffer->destroy_signal,
483 &state->buffer_destroy_listener);
484}
485
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500486WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500487weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500488{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500489 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400490
Bryce Harringtonde16d892014-11-20 22:21:57 -0800491 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400492 if (surface == NULL)
493 return NULL;
494
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500495 wl_signal_init(&surface->destroy_signal);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +0800496 wl_signal_init(&surface->commit_signal);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500497
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500498 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200499 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400500
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200501 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
502 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200503 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
504 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500505
506 weston_surface_state_init(&surface->pending);
507
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400508 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500509 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300510 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400511
Jason Ekstranda7af7042013-10-12 22:38:11 -0500512 wl_list_init(&surface->views);
513
514 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400515 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500516
Pekka Paalanene67858b2013-04-25 13:57:42 +0300517 wl_list_init(&surface->subsurface_list);
518 wl_list_init(&surface->subsurface_list_pending);
519
Jason Ekstrand1e059042014-10-16 10:55:19 -0500520 weston_matrix_init(&surface->buffer_to_surface_matrix);
521 weston_matrix_init(&surface->surface_to_buffer_matrix);
522
Jonas Ådahld3414f22016-07-22 17:56:31 +0800523 wl_list_init(&surface->pointer_constraints);
524
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400525 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500526}
527
Alex Wu8811bf92012-02-28 18:07:54 +0800528WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500529weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200530 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500531{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100532 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500533}
534
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400535WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500536weston_view_to_global_float(struct weston_view *view,
537 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200538{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500539 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200540 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
541
Jason Ekstranda7af7042013-10-12 22:38:11 -0500542 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200543
544 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200545 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700546 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200547 v.f[3]);
548 *x = 0;
549 *y = 0;
550 return;
551 }
552
553 *x = v.f[0] / v.f[3];
554 *y = v.f[1] / v.f[3];
555 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500556 *x = sx + view->geometry.x;
557 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200558 }
559}
560
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500561WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200562weston_transformed_coord(int width, int height,
563 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200564 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200565 float sx, float sy, float *bx, float *by)
566{
567 switch (transform) {
568 case WL_OUTPUT_TRANSFORM_NORMAL:
569 default:
570 *bx = sx;
571 *by = sy;
572 break;
573 case WL_OUTPUT_TRANSFORM_FLIPPED:
574 *bx = width - sx;
575 *by = sy;
576 break;
577 case WL_OUTPUT_TRANSFORM_90:
578 *bx = height - sy;
579 *by = sx;
580 break;
581 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
582 *bx = height - sy;
583 *by = width - sx;
584 break;
585 case WL_OUTPUT_TRANSFORM_180:
586 *bx = width - sx;
587 *by = height - sy;
588 break;
589 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
590 *bx = sx;
591 *by = height - sy;
592 break;
593 case WL_OUTPUT_TRANSFORM_270:
594 *bx = sy;
595 *by = width - sx;
596 break;
597 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
598 *bx = sy;
599 *by = sx;
600 break;
601 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200602
603 *bx *= scale;
604 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200605}
606
607WL_EXPORT pixman_box32_t
608weston_transformed_rect(int width, int height,
609 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200610 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200611 pixman_box32_t rect)
612{
613 float x1, x2, y1, y2;
614
615 pixman_box32_t ret;
616
Alexander Larsson4ea95522013-05-22 14:41:37 +0200617 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200618 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200619 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200620 rect.x2, rect.y2, &x2, &y2);
621
622 if (x1 <= x2) {
623 ret.x1 = x1;
624 ret.x2 = x2;
625 } else {
626 ret.x1 = x2;
627 ret.x2 = x1;
628 }
629
630 if (y1 <= y2) {
631 ret.y1 = y1;
632 ret.y2 = y2;
633 } else {
634 ret.y1 = y2;
635 ret.y2 = y1;
636 }
637
638 return ret;
639}
640
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600641/** Transform a region by a matrix, restricted to axis-aligned transformations
642 *
643 * Warning: This function does not work for projective, affine, or matrices
644 * that encode arbitrary rotations. Only 90-degree step rotations are
645 * supported.
646 */
647WL_EXPORT void
648weston_matrix_transform_region(pixman_region32_t *dest,
649 struct weston_matrix *matrix,
650 pixman_region32_t *src)
651{
652 pixman_box32_t *src_rects, *dest_rects;
653 int nrects, i;
654
655 src_rects = pixman_region32_rectangles(src, &nrects);
656 dest_rects = malloc(nrects * sizeof(*dest_rects));
657 if (!dest_rects)
658 return;
659
660 for (i = 0; i < nrects; i++) {
661 struct weston_vector vec1 = {{
662 src_rects[i].x1, src_rects[i].y1, 0, 1
663 }};
664 weston_matrix_transform(matrix, &vec1);
665 vec1.f[0] /= vec1.f[3];
666 vec1.f[1] /= vec1.f[3];
667
668 struct weston_vector vec2 = {{
669 src_rects[i].x2, src_rects[i].y2, 0, 1
670 }};
671 weston_matrix_transform(matrix, &vec2);
672 vec2.f[0] /= vec2.f[3];
673 vec2.f[1] /= vec2.f[3];
674
675 if (vec1.f[0] < vec2.f[0]) {
676 dest_rects[i].x1 = floor(vec1.f[0]);
677 dest_rects[i].x2 = ceil(vec2.f[0]);
678 } else {
679 dest_rects[i].x1 = floor(vec2.f[0]);
680 dest_rects[i].x2 = ceil(vec1.f[0]);
681 }
682
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600683 if (vec1.f[1] < vec2.f[1]) {
684 dest_rects[i].y1 = floor(vec1.f[1]);
685 dest_rects[i].y2 = ceil(vec2.f[1]);
686 } else {
687 dest_rects[i].y1 = floor(vec2.f[1]);
688 dest_rects[i].y2 = ceil(vec1.f[1]);
689 }
690 }
691
692 pixman_region32_clear(dest);
693 pixman_region32_init_rects(dest, dest_rects, nrects);
694 free(dest_rects);
695}
696
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200697WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500698weston_transformed_region(int width, int height,
699 enum wl_output_transform transform,
700 int32_t scale,
701 pixman_region32_t *src, pixman_region32_t *dest)
702{
703 pixman_box32_t *src_rects, *dest_rects;
704 int nrects, i;
705
706 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
707 if (src != dest)
708 pixman_region32_copy(dest, src);
709 return;
710 }
711
712 src_rects = pixman_region32_rectangles(src, &nrects);
713 dest_rects = malloc(nrects * sizeof(*dest_rects));
714 if (!dest_rects)
715 return;
716
717 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
718 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
719 } else {
720 for (i = 0; i < nrects; i++) {
721 switch (transform) {
722 default:
723 case WL_OUTPUT_TRANSFORM_NORMAL:
724 dest_rects[i].x1 = src_rects[i].x1;
725 dest_rects[i].y1 = src_rects[i].y1;
726 dest_rects[i].x2 = src_rects[i].x2;
727 dest_rects[i].y2 = src_rects[i].y2;
728 break;
729 case WL_OUTPUT_TRANSFORM_90:
730 dest_rects[i].x1 = height - src_rects[i].y2;
731 dest_rects[i].y1 = src_rects[i].x1;
732 dest_rects[i].x2 = height - src_rects[i].y1;
733 dest_rects[i].y2 = src_rects[i].x2;
734 break;
735 case WL_OUTPUT_TRANSFORM_180:
736 dest_rects[i].x1 = width - src_rects[i].x2;
737 dest_rects[i].y1 = height - src_rects[i].y2;
738 dest_rects[i].x2 = width - src_rects[i].x1;
739 dest_rects[i].y2 = height - src_rects[i].y1;
740 break;
741 case WL_OUTPUT_TRANSFORM_270:
742 dest_rects[i].x1 = src_rects[i].y1;
743 dest_rects[i].y1 = width - src_rects[i].x2;
744 dest_rects[i].x2 = src_rects[i].y2;
745 dest_rects[i].y2 = width - src_rects[i].x1;
746 break;
747 case WL_OUTPUT_TRANSFORM_FLIPPED:
748 dest_rects[i].x1 = width - src_rects[i].x2;
749 dest_rects[i].y1 = src_rects[i].y1;
750 dest_rects[i].x2 = width - src_rects[i].x1;
751 dest_rects[i].y2 = src_rects[i].y2;
752 break;
753 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
754 dest_rects[i].x1 = height - src_rects[i].y2;
755 dest_rects[i].y1 = width - src_rects[i].x2;
756 dest_rects[i].x2 = height - src_rects[i].y1;
757 dest_rects[i].y2 = width - src_rects[i].x1;
758 break;
759 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
760 dest_rects[i].x1 = src_rects[i].x1;
761 dest_rects[i].y1 = height - src_rects[i].y2;
762 dest_rects[i].x2 = src_rects[i].x2;
763 dest_rects[i].y2 = height - src_rects[i].y1;
764 break;
765 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
766 dest_rects[i].x1 = src_rects[i].y1;
767 dest_rects[i].y1 = src_rects[i].x1;
768 dest_rects[i].x2 = src_rects[i].y2;
769 dest_rects[i].y2 = src_rects[i].x2;
770 break;
771 }
772 }
773 }
774
775 if (scale != 1) {
776 for (i = 0; i < nrects; i++) {
777 dest_rects[i].x1 *= scale;
778 dest_rects[i].x2 *= scale;
779 dest_rects[i].y1 *= scale;
780 dest_rects[i].y2 *= scale;
781 }
782 }
783
784 pixman_region32_clear(dest);
785 pixman_region32_init_rects(dest, dest_rects, nrects);
786 free(dest_rects);
787}
788
Jonny Lamb74130762013-11-26 18:19:46 +0100789static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300790viewport_surface_to_buffer(struct weston_surface *surface,
791 float sx, float sy, float *bx, float *by)
Jonny Lamb74130762013-11-26 18:19:46 +0100792{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200793 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200794 double src_width, src_height;
795 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200796
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200797 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
798 if (vp->surface.width == -1) {
799 *bx = sx;
800 *by = sy;
801 return;
802 }
Jonny Lamb74130762013-11-26 18:19:46 +0100803
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200804 src_x = 0.0;
805 src_y = 0.0;
806 src_width = surface->width_from_buffer;
807 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100808 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200809 src_x = wl_fixed_to_double(vp->buffer.src_x);
810 src_y = wl_fixed_to_double(vp->buffer.src_y);
811 src_width = wl_fixed_to_double(vp->buffer.src_width);
812 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100813 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200814
815 *bx = sx * src_width / surface->width + src_x;
816 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100817}
818
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500819WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200820weston_surface_to_buffer_float(struct weston_surface *surface,
821 float sx, float sy, float *bx, float *by)
822{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200823 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
824
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300825 /* first transform coordinates if the viewport is set */
826 viewport_surface_to_buffer(surface, sx, sy, bx, by);
Jonny Lamb74130762013-11-26 18:19:46 +0100827
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500828 weston_transformed_coord(surface->width_from_buffer,
829 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200830 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100831 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200832}
833
Derek Foreman99739672015-12-03 16:38:11 -0600834/** Transform a rectangle from surface coordinates to buffer coordinates
835 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300836 * \param surface The surface to fetch wp_viewport and buffer transformation
Derek Foreman99739672015-12-03 16:38:11 -0600837 * from.
838 * \param rect The rectangle to transform.
839 * \return The transformed rectangle.
840 *
841 * Viewport and buffer transformations can only do translation, scaling,
842 * and rotations in 90-degree steps. Therefore the only loss in the
843 * conversion is coordinate rounding.
844 *
845 * However, some coordinate rounding takes place as an intermediate
846 * step before the buffer scale factor is applied, so the rectangle
847 * boundary may not be exactly as expected.
848 *
849 * This is OK for damage tracking since a little extra coverage is
850 * not a problem.
851 */
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200852WL_EXPORT pixman_box32_t
853weston_surface_to_buffer_rect(struct weston_surface *surface,
854 pixman_box32_t rect)
855{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200856 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100857 float xf, yf;
858
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300859 /* first transform box coordinates if the viewport is set */
860 viewport_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100861 rect.x1 = floorf(xf);
862 rect.y1 = floorf(yf);
863
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300864 viewport_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
Derek Foremane2e15ac2015-12-01 13:00:43 -0600865 rect.x2 = ceilf(xf);
866 rect.y2 = ceilf(yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100867
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500868 return weston_transformed_rect(surface->width_from_buffer,
869 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200870 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200871 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200872}
873
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200874/** Transform a region from surface coordinates to buffer coordinates
875 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300876 * \param surface The surface to fetch wp_viewport and buffer transformation
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200877 * from.
878 * \param surface_region[in] The region in surface coordinates.
879 * \param buffer_region[out] The region converted to buffer coordinates.
880 *
881 * Buffer_region must be init'd, but will be completely overwritten.
882 *
883 * Viewport and buffer transformations can only do translation, scaling,
884 * and rotations in 90-degree steps. Therefore the only loss in the
Derek Foreman99739672015-12-03 16:38:11 -0600885 * conversion is from the coordinate rounding that takes place in
886 * \ref weston_surface_to_buffer_rect.
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200887 */
888WL_EXPORT void
889weston_surface_to_buffer_region(struct weston_surface *surface,
890 pixman_region32_t *surface_region,
891 pixman_region32_t *buffer_region)
892{
893 pixman_box32_t *src_rects, *dest_rects;
894 int nrects, i;
895
896 src_rects = pixman_region32_rectangles(surface_region, &nrects);
897 dest_rects = malloc(nrects * sizeof(*dest_rects));
898 if (!dest_rects)
899 return;
900
901 for (i = 0; i < nrects; i++) {
902 dest_rects[i] = weston_surface_to_buffer_rect(surface,
903 src_rects[i]);
904 }
905
906 pixman_region32_fini(buffer_region);
907 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
908 free(dest_rects);
909}
910
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200911WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500912weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400913 struct weston_plane *plane)
914{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500915 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400916 return;
917
Jason Ekstranda7af7042013-10-12 22:38:11 -0500918 weston_view_damage_below(view);
919 view->plane = plane;
920 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400921}
922
Pekka Paalanen51723d52015-02-17 13:10:01 +0200923/** Inflict damage on the plane where the view is visible.
924 *
925 * \param view The view that causes the damage.
926 *
927 * If the view is currently on a plane (including the primary plane),
928 * take the view's boundingbox, subtract all the opaque views that cover it,
929 * and add the remaining region as damage to the plane. This corresponds
930 * to the damage inflicted to the plane if this view disappeared.
931 *
932 * A repaint is scheduled for this view.
933 *
934 * The region of all opaque views covering this view is stored in
935 * weston_view::clip and updated by view_accumulate_damage() during
936 * weston_output_repaint(). Specifically, that region matches the
937 * scenegraph as it was last painted.
938 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400939WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500940weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200941{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500942 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200943
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500944 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +0200945 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500946 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800947 if (view->plane)
948 pixman_region32_union(&view->plane->damage,
949 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500950 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800951 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200952}
953
Pekka Paalanen01e00682017-03-24 16:21:06 +0200954/** Send wl_surface.enter/leave events
955 *
956 * \param surface The surface.
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300957 * \param head A head of the entered/left output.
Pekka Paalanen01e00682017-03-24 16:21:06 +0200958 * \param enter True if entered.
959 * \param left True if left.
960 *
961 * Send the enter/leave events for all protocol objects bound to the given
962 * output by the client owning the surface.
963 */
964static void
965weston_surface_send_enter_leave(struct weston_surface *surface,
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300966 struct weston_head *head,
Pekka Paalanen01e00682017-03-24 16:21:06 +0200967 bool enter,
968 bool leave)
969{
970 struct wl_resource *wloutput;
971 struct wl_client *client;
972
973 assert(enter != leave);
974
975 client = wl_resource_get_client(surface->resource);
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300976 wl_resource_for_each(wloutput, &head->resource_list) {
Pekka Paalanen01e00682017-03-24 16:21:06 +0200977 if (wl_resource_get_client(wloutput) != client)
978 continue;
979
980 if (enter)
981 wl_surface_send_enter(surface->resource, wloutput);
982 if (leave)
983 wl_surface_send_leave(surface->resource, wloutput);
984 }
985}
986
Bryce Harrington3f650b82015-12-23 11:01:58 -0800987/**
988 * \param es The surface
989 * \param mask The new set of outputs for the surface
990 *
991 * Sets the surface's set of outputs to the ones specified by
992 * the new output mask provided. Identifies the outputs that
993 * have changed, the posts enter and leave events for these
994 * outputs as appropriate.
995 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400996static void
997weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
998{
999 uint32_t different = es->output_mask ^ mask;
1000 uint32_t entered = mask & different;
1001 uint32_t left = es->output_mask & different;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001002 uint32_t output_bit;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001003 struct weston_output *output;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001004 struct weston_head *head;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001005
1006 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001007 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001008 return;
1009 if (different == 0)
1010 return;
1011
1012 wl_list_for_each(output, &es->compositor->output_list, link) {
Pekka Paalanen01e00682017-03-24 16:21:06 +02001013 output_bit = 1u << output->id;
1014 if (!(output_bit & different))
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001015 continue;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001016
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001017 wl_list_for_each(head, &output->head_list, output_link) {
1018 weston_surface_send_enter_leave(es, head,
1019 output_bit & entered,
1020 output_bit & left);
1021 }
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001022 }
1023}
1024
Bryce Harrington3f650b82015-12-23 11:01:58 -08001025/** Recalculate which output(s) the surface has views displayed on
1026 *
1027 * \param es The surface to remap to outputs
1028 *
1029 * Finds the output that is showing the largest amount of one
1030 * of the surface's various views. This output becomes the
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001031 * surface's primary output for vsync and frame callback purposes.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001032 *
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001033 * Also notes all outputs of all of the surface's views
Bryce Harrington3f650b82015-12-23 11:01:58 -08001034 * in the output_mask for the surface.
1035 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001036static void
1037weston_surface_assign_output(struct weston_surface *es)
1038{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001039 struct weston_output *new_output;
1040 struct weston_view *view;
1041 pixman_region32_t region;
1042 uint32_t max, area, mask;
1043 pixman_box32_t *e;
1044
1045 new_output = NULL;
1046 max = 0;
1047 mask = 0;
1048 pixman_region32_init(&region);
1049 wl_list_for_each(view, &es->views, surface_link) {
1050 if (!view->output)
1051 continue;
1052
1053 pixman_region32_intersect(&region, &view->transform.boundingbox,
1054 &view->output->region);
1055
1056 e = pixman_region32_extents(&region);
1057 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1058
1059 mask |= view->output_mask;
1060
1061 if (area >= max) {
1062 new_output = view->output;
1063 max = area;
1064 }
1065 }
1066 pixman_region32_fini(&region);
1067
1068 es->output = new_output;
1069 weston_surface_update_output_mask(es, mask);
1070}
1071
Bryce Harrington3f650b82015-12-23 11:01:58 -08001072/** Recalculate which output(s) the view is displayed on
1073 *
1074 * \param ev The view to remap to outputs
1075 *
1076 * Identifies the set of outputs that the view is visible on,
1077 * noting them into the output_mask. The output that the view
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001078 * is most visible on is set as the view's primary output.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001079 *
1080 * Also does the same for the view's surface. See
1081 * weston_surface_assign_output().
1082 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001083static void
1084weston_view_assign_output(struct weston_view *ev)
1085{
1086 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001087 struct weston_output *output, *new_output;
1088 pixman_region32_t region;
1089 uint32_t max, area, mask;
1090 pixman_box32_t *e;
1091
1092 new_output = NULL;
1093 max = 0;
1094 mask = 0;
1095 pixman_region32_init(&region);
1096 wl_list_for_each(output, &ec->output_list, link) {
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03001097 if (output->destroying)
1098 continue;
1099
Jason Ekstranda7af7042013-10-12 22:38:11 -05001100 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001101 &output->region);
1102
1103 e = pixman_region32_extents(&region);
1104 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1105
1106 if (area > 0)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001107 mask |= 1u << output->id;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001108
1109 if (area >= max) {
1110 new_output = output;
1111 max = area;
1112 }
1113 }
1114 pixman_region32_fini(&region);
1115
Jason Ekstranda7af7042013-10-12 22:38:11 -05001116 ev->output = new_output;
1117 ev->output_mask = mask;
1118
1119 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001120}
1121
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001122static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001123weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1124 int from_x, int from_y, int *to_x, int *to_y)
1125{
1126 float x, y;
1127
1128 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1129 weston_view_from_global_float(to, x, y, &x, &y);
1130
1131 *to_x = round(x);
1132 *to_y = round(y);
1133}
1134
1135static void
1136weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1137{
1138 pixman_box32_t *a;
1139 pixman_box32_t b;
1140
1141 a = pixman_region32_extents(&from->geometry.scissor);
1142
1143 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1144 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1145
1146 pixman_region32_fini(&to->geometry.scissor);
1147 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1148}
1149
1150static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001151view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001152 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001153{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001154 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1155 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001156 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001157 { inbox->x1, inbox->y1 },
1158 { inbox->x1, inbox->y2 },
1159 { inbox->x2, inbox->y1 },
1160 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001161 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001162 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001163 int i;
1164
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001165 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001166 /* avoid rounding empty bbox to 1x1 */
1167 pixman_region32_init(bbox);
1168 return;
1169 }
1170
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001171 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001172 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001173 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001174 if (x < min_x)
1175 min_x = x;
1176 if (x > max_x)
1177 max_x = x;
1178 if (y < min_y)
1179 min_y = y;
1180 if (y > max_y)
1181 max_y = y;
1182 }
1183
Pekka Paalanen219b9822012-02-08 15:38:37 +02001184 int_x = floorf(min_x);
1185 int_y = floorf(min_y);
1186 pixman_region32_init_rect(bbox, int_x, int_y,
1187 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001188}
1189
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001190static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001191weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001192{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001193 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001194
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001195 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001196 view->geometry.x = roundf(view->geometry.x);
1197 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001198
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001199 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001200 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1201 view->transform.position.matrix.d[12] = view->geometry.x;
1202 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001203
Jason Ekstranda7af7042013-10-12 22:38:11 -05001204 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001205
Jason Ekstranda7af7042013-10-12 22:38:11 -05001206 view->transform.inverse = view->transform.position.matrix;
1207 view->transform.inverse.d[12] = -view->geometry.x;
1208 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001209
Jason Ekstranda7af7042013-10-12 22:38:11 -05001210 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001211 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001212 view->surface->width,
1213 view->surface->height);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001214 if (view->geometry.scissor_enabled)
1215 pixman_region32_intersect(&view->transform.boundingbox,
1216 &view->transform.boundingbox,
1217 &view->geometry.scissor);
1218
1219 pixman_region32_translate(&view->transform.boundingbox,
1220 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001221
Jason Ekstranda7af7042013-10-12 22:38:11 -05001222 if (view->alpha == 1.0) {
1223 pixman_region32_copy(&view->transform.opaque,
1224 &view->surface->opaque);
1225 pixman_region32_translate(&view->transform.opaque,
1226 view->geometry.x,
1227 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001228 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001229}
1230
1231static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001232weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001233{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001234 struct weston_view *parent = view->geometry.parent;
1235 struct weston_matrix *matrix = &view->transform.matrix;
1236 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001237 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001238 pixman_region32_t surfregion;
1239 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001240
Jason Ekstranda7af7042013-10-12 22:38:11 -05001241 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001242
1243 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001244 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1245 view->transform.position.matrix.d[12] = view->geometry.x;
1246 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001247
1248 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001249 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001250 weston_matrix_multiply(matrix, &tform->matrix);
1251
Pekka Paalanen483243f2013-03-08 14:56:50 +02001252 if (parent)
1253 weston_matrix_multiply(matrix, &parent->transform.matrix);
1254
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001255 if (weston_matrix_invert(inverse, matrix) < 0) {
1256 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001257 weston_log("error: weston_view %p"
1258 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001259 return -1;
1260 }
1261
Giulio Camuffo148c1992016-09-04 18:50:46 +03001262 if (view->alpha == 1.0 &&
1263 matrix->type == WESTON_MATRIX_TRANSFORM_TRANSLATE) {
1264 pixman_region32_copy(&view->transform.opaque,
1265 &view->surface->opaque);
1266 pixman_region32_translate(&view->transform.opaque,
1267 matrix->d[12],
1268 matrix->d[13]);
1269 }
1270
Pekka Paalanen380adf52015-02-16 14:39:11 +02001271 pixman_region32_init_rect(&surfregion, 0, 0,
1272 view->surface->width, view->surface->height);
1273 if (view->geometry.scissor_enabled)
1274 pixman_region32_intersect(&surfregion, &surfregion,
1275 &view->geometry.scissor);
1276 surfbox = pixman_region32_extents(&surfregion);
1277
1278 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1279 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001280
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001281 return 0;
1282}
1283
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001284static struct weston_layer *
1285get_view_layer(struct weston_view *view)
1286{
1287 if (view->parent_view)
1288 return get_view_layer(view->parent_view);
1289 return view->layer_link.layer;
1290}
1291
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001292WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001293weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001294{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001295 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001296 struct weston_layer *layer;
1297 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001298
Jason Ekstranda7af7042013-10-12 22:38:11 -05001299 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001300 return;
1301
Pekka Paalanen483243f2013-03-08 14:56:50 +02001302 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001303 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001304
Jason Ekstranda7af7042013-10-12 22:38:11 -05001305 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001306
Jason Ekstranda7af7042013-10-12 22:38:11 -05001307 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001308
Jason Ekstranda7af7042013-10-12 22:38:11 -05001309 pixman_region32_fini(&view->transform.boundingbox);
1310 pixman_region32_fini(&view->transform.opaque);
1311 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001312
Pekka Paalanencd403622012-01-25 13:37:39 +02001313 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001314 if (view->geometry.transformation_list.next ==
1315 &view->transform.position.link &&
1316 view->geometry.transformation_list.prev ==
1317 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001318 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001319 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001320 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001321 if (weston_view_update_transform_enable(view) < 0)
1322 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001323 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001324
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001325 layer = get_view_layer(view);
1326 if (layer) {
1327 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001328 pixman_region32_intersect(&view->transform.boundingbox,
1329 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001330 pixman_region32_intersect(&view->transform.opaque,
1331 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001332 pixman_region32_fini(&mask);
1333 }
1334
Pekka Paalanen380adf52015-02-16 14:39:11 +02001335 if (parent) {
1336 if (parent->geometry.scissor_enabled) {
1337 view->geometry.scissor_enabled = true;
1338 weston_view_transfer_scissor(parent, view);
1339 } else {
1340 view->geometry.scissor_enabled = false;
1341 }
1342 }
1343
Jason Ekstranda7af7042013-10-12 22:38:11 -05001344 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001345
Jason Ekstranda7af7042013-10-12 22:38:11 -05001346 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001347
Jason Ekstranda7af7042013-10-12 22:38:11 -05001348 wl_signal_emit(&view->surface->compositor->transform_signal,
1349 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001350}
1351
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001352WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001353weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001354{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001355 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001356
1357 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001358 * The invariant: if view->geometry.dirty, then all views
1359 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001360 * Corollary: if not parent->geometry.dirty, then all ancestors
1361 * are not dirty.
1362 */
1363
Jason Ekstranda7af7042013-10-12 22:38:11 -05001364 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001365 return;
1366
Jason Ekstranda7af7042013-10-12 22:38:11 -05001367 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001368
Jason Ekstranda7af7042013-10-12 22:38:11 -05001369 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001370 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001371 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001372}
1373
1374WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001375weston_view_to_global_fixed(struct weston_view *view,
1376 wl_fixed_t vx, wl_fixed_t vy,
1377 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001378{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001379 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001380
Jason Ekstranda7af7042013-10-12 22:38:11 -05001381 weston_view_to_global_float(view,
1382 wl_fixed_to_double(vx),
1383 wl_fixed_to_double(vy),
1384 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001385 *x = wl_fixed_from_double(xf);
1386 *y = wl_fixed_from_double(yf);
1387}
1388
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001389WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001390weston_view_from_global_float(struct weston_view *view,
1391 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001392{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001393 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001394 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1395
Jason Ekstranda7af7042013-10-12 22:38:11 -05001396 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001397
1398 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001399 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001400 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001401 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001402 *vx = 0;
1403 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001404 return;
1405 }
1406
Jason Ekstranda7af7042013-10-12 22:38:11 -05001407 *vx = v.f[0] / v.f[3];
1408 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001409 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001410 *vx = x - view->geometry.x;
1411 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001412 }
1413}
1414
1415WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001416weston_view_from_global_fixed(struct weston_view *view,
1417 wl_fixed_t x, wl_fixed_t y,
1418 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001419{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001420 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001421
Jason Ekstranda7af7042013-10-12 22:38:11 -05001422 weston_view_from_global_float(view,
1423 wl_fixed_to_double(x),
1424 wl_fixed_to_double(y),
1425 &vxf, &vyf);
1426 *vx = wl_fixed_from_double(vxf);
1427 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001428}
1429
1430WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001431weston_view_from_global(struct weston_view *view,
1432 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001433{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001434 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001435
Jason Ekstranda7af7042013-10-12 22:38:11 -05001436 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1437 *vx = floorf(vxf);
1438 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001439}
1440
Bryce Harrington3f650b82015-12-23 11:01:58 -08001441/**
1442 * \param surface The surface to be repainted
1443 *
1444 * Marks the output(s) that the surface is shown on as needing to be
1445 * repainted. See weston_output_schedule_repaint().
1446 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001447WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001448weston_surface_schedule_repaint(struct weston_surface *surface)
1449{
1450 struct weston_output *output;
1451
1452 wl_list_for_each(output, &surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001453 if (surface->output_mask & (1u << output->id))
Kristian Høgsberg98238702012-08-03 16:29:12 -04001454 weston_output_schedule_repaint(output);
1455}
1456
Bryce Harrington3f650b82015-12-23 11:01:58 -08001457/**
1458 * \param view The view to be repainted
1459 *
1460 * Marks the output(s) that the view is shown on as needing to be
1461 * repainted. See weston_output_schedule_repaint().
1462 */
Kristian Høgsberg98238702012-08-03 16:29:12 -04001463WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001464weston_view_schedule_repaint(struct weston_view *view)
1465{
1466 struct weston_output *output;
1467
1468 wl_list_for_each(output, &view->surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001469 if (view->output_mask & (1u << output->id))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001470 weston_output_schedule_repaint(output);
1471}
1472
Pekka Paalanene508ce62015-02-19 13:59:55 +02001473/**
1474 * XXX: This function does it the wrong way.
1475 * surface->damage is the damage from the client, and causes
1476 * surface_flush_damage() to copy pixels. No window management action can
1477 * cause damage to the client-provided content, warranting re-upload!
1478 *
1479 * Instead of surface->damage, this function should record the damage
1480 * with all the views for this surface to avoid extraneous texture
1481 * uploads.
1482 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001483WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001484weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001485{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001486 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001487 0, 0, surface->width,
1488 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001489
Kristian Høgsberg98238702012-08-03 16:29:12 -04001490 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001491}
1492
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001493WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001494weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001495{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001496 if (view->geometry.x == x && view->geometry.y == y)
1497 return;
1498
Jason Ekstranda7af7042013-10-12 22:38:11 -05001499 view->geometry.x = x;
1500 view->geometry.y = y;
1501 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001502}
1503
Pekka Paalanen483243f2013-03-08 14:56:50 +02001504static void
1505transform_parent_handle_parent_destroy(struct wl_listener *listener,
1506 void *data)
1507{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001508 struct weston_view *view =
1509 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001510 geometry.parent_destroy_listener);
1511
Jason Ekstranda7af7042013-10-12 22:38:11 -05001512 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001513}
1514
1515WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001516weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001517 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001518{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001519 if (view->geometry.parent) {
1520 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1521 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001522
1523 if (!parent)
1524 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001525 }
1526
Jason Ekstranda7af7042013-10-12 22:38:11 -05001527 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001528
Jason Ekstranda7af7042013-10-12 22:38:11 -05001529 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001530 transform_parent_handle_parent_destroy;
1531 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001532 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001533 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001534 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001535 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001536 }
1537
Jason Ekstranda7af7042013-10-12 22:38:11 -05001538 weston_view_geometry_dirty(view);
1539}
1540
Pekka Paalanen380adf52015-02-16 14:39:11 +02001541/** Set a clip mask rectangle on a view
1542 *
1543 * \param view The view to set the clip mask on.
1544 * \param x Top-left corner X coordinate of the clip rectangle.
1545 * \param y Top-left corner Y coordinate of the clip rectangle.
1546 * \param width Width of the clip rectangle, non-negative.
1547 * \param height Height of the clip rectangle, non-negative.
1548 *
1549 * A shell may set a clip mask rectangle on a view. Everything outside
1550 * the rectangle is cut away for input and output purposes: it is
1551 * not drawn and cannot be hit by hit-test based input like pointer
1552 * motion or touch-downs. Everything inside the rectangle will behave
1553 * normally. Clients are unaware of clipping.
1554 *
Yong Bakos4c72e292016-04-28 11:59:10 -05001555 * The rectangle is set in surface-local coordinates. Setting a clip
Pekka Paalanen380adf52015-02-16 14:39:11 +02001556 * mask rectangle does not affect the view position, the view is positioned
1557 * as it would be without a clip. The clip also does not change
1558 * weston_surface::width,height.
1559 *
1560 * The clip mask rectangle is part of transformation inheritance
1561 * (weston_view_set_transform_parent()). A clip set in the root of the
1562 * transformation inheritance tree will affect all views in the tree.
1563 * A clip can be set only on the root view. Attempting to set a clip
1564 * on view that has a transformation parent will fail. Assigning a parent
1565 * to a view that has a clip set will cause the clip to be forgotten.
1566 *
1567 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1568 * on the additional transformations in the child views. These transformations
1569 * may not rotate the coordinate axes, i.e., only translation and scaling
1570 * are allowed. Violating this restriction causes the clipping to malfunction.
1571 * Furthermore, using scaling may cause rounding errors in child clipping.
1572 *
1573 * The clip mask rectangle is not automatically adjusted based on
1574 * wl_surface.attach dx and dy arguments.
1575 *
1576 * A clip mask rectangle can be set only if the compositor capability
1577 * WESTON_CAP_VIEW_CLIP_MASK is present.
1578 *
1579 * This function sets the clip mask rectangle and schedules a repaint for
1580 * the view.
1581 */
1582WL_EXPORT void
1583weston_view_set_mask(struct weston_view *view,
1584 int x, int y, int width, int height)
1585{
1586 struct weston_compositor *compositor = view->surface->compositor;
1587
1588 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1589 weston_log("%s not allowed without capability!\n", __func__);
1590 return;
1591 }
1592
1593 if (view->geometry.parent) {
1594 weston_log("view %p has a parent, clip forbidden!\n", view);
1595 return;
1596 }
1597
1598 if (width < 0 || height < 0) {
1599 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1600 x, y, width, height);
1601 return;
1602 }
1603
1604 pixman_region32_fini(&view->geometry.scissor);
1605 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1606 view->geometry.scissor_enabled = true;
1607 weston_view_geometry_dirty(view);
1608 weston_view_schedule_repaint(view);
1609}
1610
1611/** Remove the clip mask from a view
1612 *
1613 * \param view The view to remove the clip mask from.
1614 *
1615 * Removed the clip mask rectangle and schedules a repaint.
1616 *
1617 * \sa weston_view_set_mask
1618 */
1619WL_EXPORT void
1620weston_view_set_mask_infinite(struct weston_view *view)
1621{
1622 view->geometry.scissor_enabled = false;
1623 weston_view_geometry_dirty(view);
1624 weston_view_schedule_repaint(view);
1625}
1626
Armin Krezović0da12b82016-06-30 06:04:33 +02001627/* Check if view should be displayed
1628 *
1629 * The indicator is set manually when assigning
1630 * a view to a surface.
1631 *
1632 * This needs reworking. See the thread starting at:
1633 *
1634 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1635 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001636WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001637weston_view_is_mapped(struct weston_view *view)
1638{
Armin Krezović0da12b82016-06-30 06:04:33 +02001639 return view->is_mapped;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001640}
1641
Armin Krezović0da12b82016-06-30 06:04:33 +02001642/* Check if a surface has a view assigned to it
1643 *
1644 * The indicator is set manually when mapping
1645 * a surface and creating a view for it.
1646 *
1647 * This needs to go. See the thread starting at:
1648 *
1649 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1650 *
1651 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001652WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001653weston_surface_is_mapped(struct weston_surface *surface)
1654{
Armin Krezović0da12b82016-06-30 06:04:33 +02001655 return surface->is_mapped;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001656}
1657
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001658static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001659surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001660{
1661 struct weston_view *view;
1662
1663 if (surface->width == width && surface->height == height)
1664 return;
1665
1666 surface->width = width;
1667 surface->height = height;
1668
1669 wl_list_for_each(view, &surface->views, surface_link)
1670 weston_view_geometry_dirty(view);
1671}
1672
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001673WL_EXPORT void
1674weston_surface_set_size(struct weston_surface *surface,
1675 int32_t width, int32_t height)
1676{
1677 assert(!surface->resource);
1678 surface_set_size(surface, width, height);
1679}
1680
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001681static int
1682fixed_round_up_to_int(wl_fixed_t f)
1683{
1684 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1685}
1686
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001687static void
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001688convert_size_by_transform_scale(int32_t *width_out, int32_t *height_out,
1689 int32_t width, int32_t height,
1690 uint32_t transform,
1691 int32_t scale)
1692{
1693 assert(scale > 0);
1694
1695 switch (transform) {
1696 case WL_OUTPUT_TRANSFORM_NORMAL:
1697 case WL_OUTPUT_TRANSFORM_180:
1698 case WL_OUTPUT_TRANSFORM_FLIPPED:
1699 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1700 *width_out = width / scale;
1701 *height_out = height / scale;
1702 break;
1703 case WL_OUTPUT_TRANSFORM_90:
1704 case WL_OUTPUT_TRANSFORM_270:
1705 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1706 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1707 *width_out = height / scale;
1708 *height_out = width / scale;
1709 break;
1710 default:
1711 assert(0 && "invalid transform");
1712 }
1713}
1714
1715static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001716weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001717{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001718 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001719
1720 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001721 surface->width_from_buffer = 0;
1722 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001723 return;
1724 }
1725
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001726 convert_size_by_transform_scale(&surface->width_from_buffer,
1727 &surface->height_from_buffer,
1728 surface->buffer_ref.buffer->width,
1729 surface->buffer_ref.buffer->height,
1730 vp->buffer.transform,
1731 vp->buffer.scale);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001732}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001733
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001734static void
1735weston_surface_update_size(struct weston_surface *surface)
1736{
1737 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1738 int32_t width, height;
1739
1740 width = surface->width_from_buffer;
1741 height = surface->height_from_buffer;
1742
1743 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001744 surface_set_size(surface,
1745 vp->surface.width, vp->surface.height);
1746 return;
1747 }
1748
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001749 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001750 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1751 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1752
1753 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001754 return;
1755 }
1756
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001757 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001758}
1759
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02001760WL_EXPORT void
1761weston_compositor_get_time(struct timespec *time)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001762{
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02001763 clock_gettime(CLOCK_REALTIME, time);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001764}
1765
Jason Ekstranda7af7042013-10-12 22:38:11 -05001766WL_EXPORT struct weston_view *
1767weston_compositor_pick_view(struct weston_compositor *compositor,
1768 wl_fixed_t x, wl_fixed_t y,
1769 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001770{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001771 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001772 wl_fixed_t view_x, view_y;
1773 int view_ix, view_iy;
1774 int ix = wl_fixed_to_int(x);
1775 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001776
Jason Ekstranda7af7042013-10-12 22:38:11 -05001777 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001778 if (!pixman_region32_contains_point(
1779 &view->transform.boundingbox, ix, iy, NULL))
1780 continue;
1781
1782 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
1783 view_ix = wl_fixed_to_int(view_x);
1784 view_iy = wl_fixed_to_int(view_y);
1785
1786 if (!pixman_region32_contains_point(&view->surface->input,
1787 view_ix, view_iy, NULL))
1788 continue;
1789
Pekka Paalanen380adf52015-02-16 14:39:11 +02001790 if (view->geometry.scissor_enabled &&
1791 !pixman_region32_contains_point(&view->geometry.scissor,
1792 view_ix, view_iy, NULL))
1793 continue;
1794
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001795 *vx = view_x;
1796 *vy = view_y;
1797 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001798 }
1799
Derek Foremanf9318d12015-05-11 15:40:11 -05001800 *vx = wl_fixed_from_int(-1000000);
1801 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001802 return NULL;
1803}
1804
1805static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001806weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001807{
Daniel Stone37816df2012-05-16 18:45:18 +01001808 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001809
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001810 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001811 return;
1812
Daniel Stone37816df2012-05-16 18:45:18 +01001813 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001814 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001815}
1816
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001817WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001818weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001819{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001820 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001821
Jason Ekstranda7af7042013-10-12 22:38:11 -05001822 if (!weston_view_is_mapped(view))
1823 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001824
Jason Ekstranda7af7042013-10-12 22:38:11 -05001825 weston_view_damage_below(view);
1826 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001827 view->plane = NULL;
Armin Krezovićf8486c32016-06-30 06:04:28 +02001828 view->is_mapped = false;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001829 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001830 wl_list_remove(&view->link);
1831 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001832 view->output_mask = 0;
1833 weston_surface_assign_output(view->surface);
1834
1835 if (weston_surface_is_mapped(view->surface))
1836 return;
1837
1838 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001839 struct weston_touch *touch = weston_seat_get_touch(seat);
1840 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1841 struct weston_keyboard *keyboard =
1842 weston_seat_get_keyboard(seat);
1843
1844 if (keyboard && keyboard->focus == view->surface)
1845 weston_keyboard_set_focus(keyboard, NULL);
1846 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05001847 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05001848 if (touch && touch->focus == view)
1849 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001850 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001851}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001852
Jason Ekstranda7af7042013-10-12 22:38:11 -05001853WL_EXPORT void
1854weston_surface_unmap(struct weston_surface *surface)
1855{
1856 struct weston_view *view;
1857
Armin Krezovićf8486c32016-06-30 06:04:28 +02001858 surface->is_mapped = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001859 wl_list_for_each(view, &surface->views, surface_link)
1860 weston_view_unmap(view);
1861 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001862}
1863
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001864static void
1865weston_surface_reset_pending_buffer(struct weston_surface *surface)
1866{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001867 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001868 surface->pending.sx = 0;
1869 surface->pending.sy = 0;
1870 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001871 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001872}
1873
Jason Ekstranda7af7042013-10-12 22:38:11 -05001874WL_EXPORT void
1875weston_view_destroy(struct weston_view *view)
1876{
1877 wl_signal_emit(&view->destroy_signal, view);
1878
1879 assert(wl_list_empty(&view->geometry.child_list));
1880
1881 if (weston_view_is_mapped(view)) {
1882 weston_view_unmap(view);
1883 weston_compositor_build_view_list(view->surface->compositor);
1884 }
1885
1886 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001887 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001888
1889 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001890 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001891 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001892 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001893
1894 weston_view_set_transform_parent(view, NULL);
1895
Jason Ekstranda7af7042013-10-12 22:38:11 -05001896 wl_list_remove(&view->surface_link);
1897
1898 free(view);
1899}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001900
1901WL_EXPORT void
1902weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001903{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001904 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001905 struct weston_view *ev, *nv;
Jonas Ådahld3414f22016-07-22 17:56:31 +08001906 struct weston_pointer_constraint *constraint, *next_constraint;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001907
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001908 if (--surface->ref_count > 0)
1909 return;
1910
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001911 assert(surface->resource == NULL);
1912
Pekka Paalanenca790762015-04-17 14:23:38 +03001913 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001914
Pekka Paalanene67858b2013-04-25 13:57:42 +03001915 assert(wl_list_empty(&surface->subsurface_list_pending));
1916 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001917
Jason Ekstranda7af7042013-10-12 22:38:11 -05001918 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1919 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001920
Jason Ekstrand7b982072014-05-20 14:33:03 -05001921 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001922
Pekka Paalanende685b82012-12-04 15:58:12 +02001923 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001924
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001925 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001926 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001927 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001928
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001929 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001930 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001931
Pekka Paalanen133e4392014-09-23 22:08:46 -04001932 weston_presentation_feedback_discard_list(&surface->feedback_list);
1933
Jonas Ådahld3414f22016-07-22 17:56:31 +08001934 wl_list_for_each_safe(constraint, next_constraint,
1935 &surface->pointer_constraints,
1936 link)
1937 weston_pointer_constraint_destroy(constraint);
1938
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001939 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001940}
1941
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001942static void
1943destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001944{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001945 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001946
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001947 assert(surface);
1948
Giulio Camuffo0d379742013-11-15 22:06:15 +01001949 /* Set the resource to NULL, since we don't want to leave a
1950 * dangling pointer if the surface was refcounted and survives
1951 * the weston_surface_destroy() call. */
1952 surface->resource = NULL;
Pekka Paalanen4826f872016-04-22 14:14:38 +03001953
1954 if (surface->viewport_resource)
1955 wl_resource_set_user_data(surface->viewport_resource, NULL);
1956
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001957 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001958}
1959
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001960static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001961weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1962{
1963 struct weston_buffer *buffer =
1964 container_of(listener, struct weston_buffer, destroy_listener);
1965
1966 wl_signal_emit(&buffer->destroy_signal, buffer);
1967 free(buffer);
1968}
1969
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001970WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001971weston_buffer_from_resource(struct wl_resource *resource)
1972{
1973 struct weston_buffer *buffer;
1974 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001975
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001976 listener = wl_resource_get_destroy_listener(resource,
1977 weston_buffer_destroy_handler);
1978
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001979 if (listener)
1980 return container_of(listener, struct weston_buffer,
1981 destroy_listener);
1982
1983 buffer = zalloc(sizeof *buffer);
1984 if (buffer == NULL)
1985 return NULL;
1986
1987 buffer->resource = resource;
1988 wl_signal_init(&buffer->destroy_signal);
1989 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001990 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001991 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001992
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001993 return buffer;
1994}
1995
1996static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001997weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1998 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001999{
Pekka Paalanende685b82012-12-04 15:58:12 +02002000 struct weston_buffer_reference *ref =
2001 container_of(listener, struct weston_buffer_reference,
2002 destroy_listener);
2003
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002004 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02002005 ref->buffer = NULL;
2006}
2007
2008WL_EXPORT void
2009weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002010 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002011{
2012 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05002013 ref->buffer->busy_count--;
2014 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002015 assert(wl_resource_get_client(ref->buffer->resource));
Matt Hoosier3052bc72017-09-26 08:09:40 -05002016 wl_buffer_send_release(ref->buffer->resource);
Kristian Høgsberg20347802013-03-04 12:07:46 -05002017 }
Pekka Paalanende685b82012-12-04 15:58:12 +02002018 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002019 }
2020
Pekka Paalanende685b82012-12-04 15:58:12 +02002021 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002022 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002023 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02002024 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02002025 }
2026
Pekka Paalanende685b82012-12-04 15:58:12 +02002027 ref->buffer = buffer;
2028 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
2029}
2030
2031static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002032weston_surface_attach(struct weston_surface *surface,
2033 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002034{
2035 weston_buffer_reference(&surface->buffer_ref, buffer);
2036
Pekka Paalanena6421c42012-12-04 15:58:10 +02002037 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002038 if (weston_surface_is_mapped(surface))
2039 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002040 }
2041
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002042 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02002043
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002044 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002045 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002046}
2047
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002048WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002049weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002050{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002051 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002052
2053 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002054 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002055}
2056
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05002057WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002058weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002059{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002060 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002061
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002062 pixman_region32_union(&compositor->primary_plane.damage,
2063 &compositor->primary_plane.damage,
2064 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002065 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002066}
2067
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04002068static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002069surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002070{
Pekka Paalanende685b82012-12-04 15:58:12 +02002071 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002072 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002073 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002074
Pekka Paalanenb5026542014-11-12 15:09:24 +02002075 if (weston_timeline_enabled_ &&
2076 pixman_region32_not_empty(&surface->damage))
2077 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
2078 TLP_OUTPUT(surface->output), TLP_END);
2079
Jason Ekstrandef540082014-06-26 10:37:36 -07002080 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002081}
2082
2083static void
2084view_accumulate_damage(struct weston_view *view,
2085 pixman_region32_t *opaque)
2086{
2087 pixman_region32_t damage;
2088
2089 pixman_region32_init(&damage);
2090 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002091 pixman_box32_t *extents;
2092
Jason Ekstranda7af7042013-10-12 22:38:11 -05002093 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002094 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002095 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002096 pixman_region32_copy(&damage, &view->surface->damage);
2097 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002098 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002099 }
2100
Pekka Paalanen380adf52015-02-16 14:39:11 +02002101 pixman_region32_intersect(&damage, &damage,
2102 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002103 pixman_region32_subtract(&damage, &damage, opaque);
2104 pixman_region32_union(&view->plane->damage,
2105 &view->plane->damage, &damage);
2106 pixman_region32_fini(&damage);
2107 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002108 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002109}
2110
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002111static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002112compositor_accumulate_damage(struct weston_compositor *ec)
2113{
2114 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002115 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002116 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002117
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002118 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002119
2120 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002121 pixman_region32_copy(&plane->clip, &clip);
2122
2123 pixman_region32_init(&opaque);
2124
Jason Ekstranda7af7042013-10-12 22:38:11 -05002125 wl_list_for_each(ev, &ec->view_list, link) {
2126 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002127 continue;
2128
Jason Ekstranda7af7042013-10-12 22:38:11 -05002129 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002130 }
2131
2132 pixman_region32_union(&clip, &clip, &opaque);
2133 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002134 }
2135
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002136 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002137
Jason Ekstranda7af7042013-10-12 22:38:11 -05002138 wl_list_for_each(ev, &ec->view_list, link)
Derek Foreman060cf112015-11-18 16:32:26 -06002139 ev->surface->touched = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002140
2141 wl_list_for_each(ev, &ec->view_list, link) {
2142 if (ev->surface->touched)
2143 continue;
Derek Foreman060cf112015-11-18 16:32:26 -06002144 ev->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002145
2146 surface_flush_damage(ev->surface);
2147
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002148 /* Both the renderer and the backend have seen the buffer
2149 * by now. If renderer needs the buffer, it has its own
2150 * reference set. If the backend wants to keep the buffer
2151 * around for migrating the surface into a non-primary plane
2152 * later, keep_buffer is true. Otherwise, drop the core
2153 * reference now, and allow early buffer release. This enables
2154 * clients to use single-buffering.
2155 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002156 if (!ev->surface->keep_buffer)
2157 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002158 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002159}
2160
2161static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002162surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002163{
2164 struct weston_subsurface *sub;
2165
Pekka Paalanene67858b2013-04-25 13:57:42 +03002166 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002167 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002168 continue;
2169
Jason Ekstranda7af7042013-10-12 22:38:11 -05002170 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2171 wl_list_init(&sub->surface->views);
2172
2173 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002174 }
2175}
2176
2177static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002178surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002179{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002180 struct weston_subsurface *sub;
2181 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002182
Jason Ekstranda7af7042013-10-12 22:38:11 -05002183 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2184 if (sub->surface == surface)
2185 continue;
2186
George Kiagiadakised04d382014-06-13 18:10:26 +02002187 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2188 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002189 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002190 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002191
2192 surface_free_unused_subsurface_views(sub->surface);
2193 }
2194}
2195
2196static void
2197view_list_add_subsurface_view(struct weston_compositor *compositor,
2198 struct weston_subsurface *sub,
2199 struct weston_view *parent)
2200{
2201 struct weston_subsurface *child;
2202 struct weston_view *view = NULL, *iv;
2203
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002204 if (!weston_surface_is_mapped(sub->surface))
2205 return;
2206
Jason Ekstranda7af7042013-10-12 22:38:11 -05002207 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2208 if (iv->geometry.parent == parent) {
2209 view = iv;
2210 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002211 }
2212 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002213
2214 if (view) {
2215 /* Put it back in the surface's list of views */
2216 wl_list_remove(&view->surface_link);
2217 wl_list_insert(&sub->surface->views, &view->surface_link);
2218 } else {
2219 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002220 weston_view_set_position(view,
2221 sub->position.x,
2222 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002223 weston_view_set_transform_parent(view, parent);
2224 }
2225
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002226 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002227 weston_view_update_transform(view);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002228 view->is_mapped = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002229
Pekka Paalanenb188e912013-11-19 14:03:35 +02002230 if (wl_list_empty(&sub->surface->subsurface_list)) {
2231 wl_list_insert(compositor->view_list.prev, &view->link);
2232 return;
2233 }
2234
2235 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2236 if (child->surface == sub->surface)
2237 wl_list_insert(compositor->view_list.prev, &view->link);
2238 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002239 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002240 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002241}
2242
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002243/* This recursively adds the sub-surfaces for a view, relying on the
2244 * sub-surface order. Thus, if a client restacks the sub-surfaces, that
2245 * change first happens to the sub-surface list, and then automatically
2246 * propagates here. See weston_surface_damage_subsurfaces() for how the
2247 * sub-surfaces receive damage when the client changes the state.
2248 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002249static void
2250view_list_add(struct weston_compositor *compositor,
2251 struct weston_view *view)
2252{
2253 struct weston_subsurface *sub;
2254
2255 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002256
Pekka Paalanenb188e912013-11-19 14:03:35 +02002257 if (wl_list_empty(&view->surface->subsurface_list)) {
2258 wl_list_insert(compositor->view_list.prev, &view->link);
2259 return;
2260 }
2261
2262 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2263 if (sub->surface == view->surface)
2264 wl_list_insert(compositor->view_list.prev, &view->link);
2265 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002266 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002267 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002268}
2269
2270static void
2271weston_compositor_build_view_list(struct weston_compositor *compositor)
2272{
2273 struct weston_view *view;
2274 struct weston_layer *layer;
2275
2276 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002277 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002278 surface_stash_subsurface_views(view->surface);
2279
2280 wl_list_init(&compositor->view_list);
2281 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002282 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002283 view_list_add(compositor, view);
2284 }
2285 }
2286
2287 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002288 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002289 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002290}
2291
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002292static void
2293weston_output_take_feedback_list(struct weston_output *output,
2294 struct weston_surface *surface)
2295{
2296 struct weston_view *view;
2297 struct weston_presentation_feedback *feedback;
2298 uint32_t flags = 0xffffffff;
2299
2300 if (wl_list_empty(&surface->feedback_list))
2301 return;
2302
2303 /* All views must have the flag for the flag to survive. */
2304 wl_list_for_each(view, &surface->views, surface_link) {
2305 /* ignore views that are not on this output at all */
2306 if (view->output_mask & (1u << output->id))
2307 flags &= view->psf_flags;
2308 }
2309
2310 wl_list_for_each(feedback, &surface->feedback_list, link)
2311 feedback->psf_flags = flags;
2312
2313 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2314 wl_list_init(&surface->feedback_list);
2315}
2316
David Herrmann1edf44c2013-10-22 17:11:26 +02002317static int
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002318weston_output_repaint(struct weston_output *output, void *repaint_data)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002319{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002320 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002321 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002322 struct weston_animation *animation, *next;
2323 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002324 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002325 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002326 int r;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002327 uint32_t frame_time_msec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002328
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002329 if (output->destroying)
2330 return 0;
2331
Pekka Paalanenb5026542014-11-12 15:09:24 +02002332 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2333
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002334 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002335 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002336
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002337 if (output->assign_planes && !output->disable_planes) {
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002338 output->assign_planes(output, repaint_data);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002339 } else {
2340 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002341 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002342 ev->psf_flags = 0;
2343 }
2344 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002345
Pekka Paalanene67858b2013-04-25 13:57:42 +03002346 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002347 wl_list_for_each(ev, &ec->view_list, link) {
2348 /* Note: This operation is safe to do multiple times on the
2349 * same surface.
2350 */
2351 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002352 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002353 &ev->surface->frame_callback_list);
2354 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002355
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002356 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002357 }
2358 }
2359
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002360 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002361
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002362 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002363 pixman_region32_intersect(&output_damage,
2364 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002365 pixman_region32_subtract(&output_damage,
2366 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002367
Scott Moreauccbf29d2012-02-22 14:21:41 -07002368 if (output->dirty)
2369 weston_output_update_matrix(output);
2370
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002371 r = output->repaint(output, &output_damage, repaint_data);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002372
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002373 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002374
Daniel Stone09a97e22017-03-01 11:34:06 +00002375 output->repaint_needed = false;
Daniel Stone05df8c12017-03-03 16:59:42 +00002376 if (r == 0)
2377 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002378
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002379 weston_compositor_repick(ec);
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002380
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002381 frame_time_msec = timespec_to_msec(&output->frame_time);
2382
Jonas Ådahldb773762012-06-13 00:01:21 +02002383 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002384 wl_callback_send_done(cb->resource, frame_time_msec);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002385 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002386 }
2387
Scott Moreaud64cf212012-06-08 19:40:54 -06002388 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002389 animation->frame_counter++;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002390 animation->frame(animation, output, &output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002391 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002392
Pekka Paalanenb5026542014-11-12 15:09:24 +02002393 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2394
David Herrmann1edf44c2013-10-22 17:11:26 +02002395 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002396}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002397
Pekka Paalanen82919792014-05-21 13:51:49 +03002398static void
2399weston_output_schedule_repaint_reset(struct weston_output *output)
2400{
Daniel Stone05df8c12017-03-03 16:59:42 +00002401 output->repaint_status = REPAINT_NOT_SCHEDULED;
Pekka Paalanen82919792014-05-21 13:51:49 +03002402 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
Pekka Paalanen82919792014-05-21 13:51:49 +03002403}
2404
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002405static int
2406weston_output_maybe_repaint(struct weston_output *output, struct timespec *now,
2407 void *repaint_data)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002408{
Pekka Paalanen0513a952014-05-21 16:17:27 +03002409 struct weston_compositor *compositor = output->compositor;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002410 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002411 int64_t msec_to_repaint;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002412
Daniel Stone6847b852017-03-01 11:34:08 +00002413 /* We're not ready yet; come back to make a decision later. */
2414 if (output->repaint_status != REPAINT_SCHEDULED)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002415 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002416
2417 msec_to_repaint = timespec_sub_to_msec(&output->next_repaint, now);
2418 if (msec_to_repaint > 1)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002419 return ret;
Daniel Stone05df8c12017-03-03 16:59:42 +00002420
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002421 /* If we're sleeping, drop the repaint machinery entirely; we will
2422 * explicitly repaint all outputs when we come back. */
2423 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2424 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
2425 goto err;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002426
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002427 /* We don't actually need to repaint this output; drop it from
2428 * repaint until something causes damage. */
2429 if (!output->repaint_needed)
2430 goto err;
2431
2432 /* If repaint fails, we aren't going to get weston_output_finish_frame
2433 * to trigger a new repaint, so drop it from repaint and hope
Daniel Stone6847b852017-03-01 11:34:08 +00002434 * something schedules a successful repaint later. As repainting may
2435 * take some time, re-read our clock as a courtesy to the next
2436 * output. */
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002437 ret = weston_output_repaint(output, repaint_data);
Daniel Stone6847b852017-03-01 11:34:08 +00002438 weston_compositor_read_presentation_clock(compositor, now);
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002439 if (ret != 0)
2440 goto err;
2441
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002442 return ret;
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002443
2444err:
Pekka Paalanen0513a952014-05-21 16:17:27 +03002445 weston_output_schedule_repaint_reset(output);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002446 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002447}
2448
2449static void
2450output_repaint_timer_arm(struct weston_compositor *compositor)
2451{
2452 struct weston_output *output;
2453 bool any_should_repaint = false;
2454 struct timespec now;
Sergi Granellb4c08862017-03-18 13:01:15 +01002455 int64_t msec_to_next = INT64_MAX;
Daniel Stone6847b852017-03-01 11:34:08 +00002456
2457 weston_compositor_read_presentation_clock(compositor, &now);
2458
2459 wl_list_for_each(output, &compositor->output_list, link) {
2460 int64_t msec_to_this;
2461
2462 if (output->repaint_status != REPAINT_SCHEDULED)
2463 continue;
2464
2465 msec_to_this = timespec_sub_to_msec(&output->next_repaint,
2466 &now);
2467 if (!any_should_repaint || msec_to_this < msec_to_next)
2468 msec_to_next = msec_to_this;
2469
2470 any_should_repaint = true;
2471 }
2472
2473 if (!any_should_repaint)
2474 return;
2475
2476 /* Even if we should repaint immediately, add the minimum 1 ms delay.
2477 * This is a workaround to allow coalescing multiple output repaints
2478 * particularly from weston_output_finish_frame()
2479 * into the same call, which would not happen if we called
2480 * output_repaint_timer_handler() directly.
2481 */
2482 if (msec_to_next < 1)
2483 msec_to_next = 1;
2484
2485 wl_event_source_timer_update(compositor->repaint_timer, msec_to_next);
2486}
2487
2488static int
2489output_repaint_timer_handler(void *data)
2490{
2491 struct weston_compositor *compositor = data;
2492 struct weston_output *output;
2493 struct timespec now;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002494 void *repaint_data = NULL;
Emre Ucane479ed82018-03-20 15:29:40 +01002495 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002496
2497 weston_compositor_read_presentation_clock(compositor, &now);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002498
2499 if (compositor->backend->repaint_begin)
2500 repaint_data = compositor->backend->repaint_begin(compositor);
2501
2502 wl_list_for_each(output, &compositor->output_list, link) {
2503 ret = weston_output_maybe_repaint(output, &now, repaint_data);
2504 if (ret)
2505 break;
2506 }
2507
2508 if (ret == 0) {
2509 if (compositor->backend->repaint_flush)
2510 compositor->backend->repaint_flush(compositor,
2511 repaint_data);
2512 } else {
2513 if (compositor->backend->repaint_cancel)
2514 compositor->backend->repaint_cancel(compositor,
2515 repaint_data);
2516 }
Daniel Stone6847b852017-03-01 11:34:08 +00002517
2518 output_repaint_timer_arm(compositor);
2519
Pekka Paalanen0513a952014-05-21 16:17:27 +03002520 return 0;
2521}
2522
Kristian Høgsbergef044142011-06-21 15:02:12 -04002523WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002524weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002525 const struct timespec *stamp,
2526 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002527{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002528 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002529 int32_t refresh_nsec;
2530 struct timespec now;
Daniel Stone6847b852017-03-01 11:34:08 +00002531 int64_t msec_rel;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002532
Pekka Paalanenb5026542014-11-12 15:09:24 +02002533
Daniel Stone05df8c12017-03-03 16:59:42 +00002534 assert(output->repaint_status == REPAINT_AWAITING_COMPLETION);
Daniel Stone3615ce12017-03-01 11:34:05 +00002535 assert(stamp || (presented_flags & WP_PRESENTATION_FEEDBACK_INVALID));
2536
Daniel Stone6847b852017-03-01 11:34:08 +00002537 weston_compositor_read_presentation_clock(compositor, &now);
2538
Daniel Stone3615ce12017-03-01 11:34:05 +00002539 /* If we haven't been supplied any timestamp at all, we don't have a
2540 * timebase to work against, so any delay just wastes time. Push a
2541 * repaint as soon as possible so we can get on with it. */
Daniel Stone6847b852017-03-01 11:34:08 +00002542 if (!stamp) {
2543 output->next_repaint = now;
Daniel Stone3615ce12017-03-01 11:34:05 +00002544 goto out;
Daniel Stone6847b852017-03-01 11:34:08 +00002545 }
Daniel Stone3615ce12017-03-01 11:34:05 +00002546
Marius Vladdf9278a2018-03-06 18:56:23 +02002547 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2548 TLP_VBLANK(stamp), TLP_END);
2549
Pekka Paalanend7894d02015-07-03 15:08:53 +03002550 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002551 weston_presentation_feedback_present_list(&output->feedback_list,
2552 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002553 output->msc,
2554 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002555
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002556 output->frame_time = *stamp;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002557
Daniel Stone6847b852017-03-01 11:34:08 +00002558 timespec_add_nsec(&output->next_repaint, stamp, refresh_nsec);
2559 timespec_add_msec(&output->next_repaint, &output->next_repaint,
2560 -compositor->repaint_msec);
2561 msec_rel = timespec_sub_to_msec(&output->next_repaint, &now);
Daniel Stone84aff5c2017-03-01 11:34:04 +00002562
2563 if (msec_rel < -1000 || msec_rel > 1000) {
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002564 static bool warned;
2565
2566 if (!warned)
2567 weston_log("Warning: computed repaint delay is "
Daniel Stone6847b852017-03-01 11:34:08 +00002568 "insane: %lld msec\n", (long long) msec_rel);
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002569 warned = true;
2570
Daniel Stone6847b852017-03-01 11:34:08 +00002571 output->next_repaint = now;
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002572 }
2573
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002574 /* Called from restart_repaint_loop and restart happens already after
2575 * the deadline given by repaint_msec? In that case we delay until
2576 * the deadline of the next frame, to give clients a more predictable
2577 * timing of the repaint cycle to lock on. */
Daniel Stoneeca5cca2017-02-28 21:53:51 +00002578 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID &&
2579 msec_rel < 0) {
2580 while (timespec_sub_to_nsec(&output->next_repaint, &now) < 0) {
2581 timespec_add_nsec(&output->next_repaint,
2582 &output->next_repaint,
2583 refresh_nsec);
2584 }
2585 }
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002586
Daniel Stone3615ce12017-03-01 11:34:05 +00002587out:
Daniel Stone05df8c12017-03-03 16:59:42 +00002588 output->repaint_status = REPAINT_SCHEDULED;
Daniel Stone6847b852017-03-01 11:34:08 +00002589 output_repaint_timer_arm(compositor);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002590}
2591
2592static void
2593idle_repaint(void *data)
2594{
2595 struct weston_output *output = data;
2596
Daniel Stone05df8c12017-03-03 16:59:42 +00002597 assert(output->repaint_status == REPAINT_BEGIN_FROM_IDLE);
2598 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03002599 output->idle_repaint_source = NULL;
Jonas Ådahle5a12252013-04-05 23:07:11 +02002600 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002601}
2602
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002603WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002604weston_layer_entry_insert(struct weston_layer_entry *list,
2605 struct weston_layer_entry *entry)
2606{
2607 wl_list_insert(&list->link, &entry->link);
2608 entry->layer = list->layer;
2609}
2610
2611WL_EXPORT void
2612weston_layer_entry_remove(struct weston_layer_entry *entry)
2613{
2614 wl_list_remove(&entry->link);
2615 wl_list_init(&entry->link);
2616 entry->layer = NULL;
2617}
2618
Quentin Glidic82681572016-12-17 13:40:51 +01002619
2620/** Initialize the weston_layer struct.
2621 *
2622 * \param compositor The compositor instance
2623 * \param layer The layer to initialize
2624 */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002625WL_EXPORT void
Quentin Glidic82681572016-12-17 13:40:51 +01002626weston_layer_init(struct weston_layer *layer,
2627 struct weston_compositor *compositor)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002628{
Quentin Glidic82681572016-12-17 13:40:51 +01002629 layer->compositor = compositor;
2630 wl_list_init(&layer->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002631 wl_list_init(&layer->view_list.link);
2632 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002633 weston_layer_set_mask_infinite(layer);
Quentin Glidic82681572016-12-17 13:40:51 +01002634}
2635
2636/** Sets the position of the layer in the layer list. The layer will be placed
2637 * below any layer with the same position value, if any.
2638 * This function is safe to call if the layer is already on the list, but the
2639 * layer may be moved below other layers at the same position, if any.
2640 *
2641 * \param layer The layer to modify
2642 * \param position The position the layer will be placed at
2643 */
2644WL_EXPORT void
2645weston_layer_set_position(struct weston_layer *layer,
2646 enum weston_layer_position position)
2647{
2648 struct weston_layer *below;
2649
2650 wl_list_remove(&layer->link);
2651
2652 /* layer_list is ordered from top to bottom, the last layer being the
2653 * background with the smallest position value */
2654
2655 layer->position = position;
2656 wl_list_for_each_reverse(below, &layer->compositor->layer_list, link) {
2657 if (below->position >= layer->position) {
2658 wl_list_insert(&below->link, &layer->link);
2659 return;
2660 }
2661 }
2662 wl_list_insert(&layer->compositor->layer_list, &layer->link);
2663}
2664
2665/** Hide a layer by taking it off the layer list.
2666 * This function is safe to call if the layer is not on the list.
2667 *
2668 * \param layer The layer to hide
2669 */
2670WL_EXPORT void
2671weston_layer_unset_position(struct weston_layer *layer)
2672{
2673 wl_list_remove(&layer->link);
2674 wl_list_init(&layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002675}
2676
2677WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002678weston_layer_set_mask(struct weston_layer *layer,
2679 int x, int y, int width, int height)
2680{
2681 struct weston_view *view;
2682
2683 layer->mask.x1 = x;
2684 layer->mask.x2 = x + width;
2685 layer->mask.y1 = y;
2686 layer->mask.y2 = y + height;
2687
2688 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2689 weston_view_geometry_dirty(view);
2690 }
2691}
2692
2693WL_EXPORT void
2694weston_layer_set_mask_infinite(struct weston_layer *layer)
2695{
2696 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2697 UINT32_MAX, UINT32_MAX);
2698}
2699
2700WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002701weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002702{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002703 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002704 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002705
Bryce Harrington08976ac2016-08-30 12:05:16 -07002706 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2707 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002708 return;
2709
Pekka Paalanenb5026542014-11-12 15:09:24 +02002710 if (!output->repaint_needed)
2711 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2712
Kristian Høgsbergef044142011-06-21 15:02:12 -04002713 loop = wl_display_get_event_loop(compositor->wl_display);
Daniel Stone09a97e22017-03-01 11:34:06 +00002714 output->repaint_needed = true;
Daniel Stone05df8c12017-03-03 16:59:42 +00002715
2716 /* If we already have a repaint scheduled for our idle handler,
2717 * no need to set it again. If the repaint has been called but
2718 * not finished, then weston_output_finish_frame() will notice
2719 * that a repaint is needed and schedule one. */
2720 if (output->repaint_status != REPAINT_NOT_SCHEDULED)
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002721 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002722
Daniel Stone05df8c12017-03-03 16:59:42 +00002723 output->repaint_status = REPAINT_BEGIN_FROM_IDLE;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03002724 assert(!output->idle_repaint_source);
2725 output->idle_repaint_source = wl_event_loop_add_idle(loop, idle_repaint,
2726 output);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002727 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002728}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002729
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002730WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002731weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2732{
2733 struct weston_output *output;
2734
2735 wl_list_for_each(output, &compositor->output_list, link)
2736 weston_output_schedule_repaint(output);
2737}
2738
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002739static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002740surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002741{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002742 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002743}
2744
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002745static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002746surface_attach(struct wl_client *client,
2747 struct wl_resource *resource,
2748 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2749{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002750 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002751 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002752
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002753 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002754 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002755 if (buffer == NULL) {
2756 wl_client_post_no_memory(client);
2757 return;
2758 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002759 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002760
Pekka Paalanende685b82012-12-04 15:58:12 +02002761 /* Attach, attach, without commit in between does not send
2762 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002763 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002764
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002765 surface->pending.sx = sx;
2766 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002767 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002768}
2769
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002770static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002771surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002772 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002773 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002774{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002775 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002776
Derek Foreman57e92ed2015-11-17 14:11:35 -06002777 if (width <= 0 || height <= 0)
2778 return;
2779
Derek Foreman152254b2015-11-26 14:17:48 -06002780 pixman_region32_union_rect(&surface->pending.damage_surface,
2781 &surface->pending.damage_surface,
2782 x, y, width, height);
2783}
2784
2785static void
2786surface_damage_buffer(struct wl_client *client,
2787 struct wl_resource *resource,
2788 int32_t x, int32_t y, int32_t width, int32_t height)
2789{
2790 struct weston_surface *surface = wl_resource_get_user_data(resource);
2791
2792 if (width <= 0 || height <= 0)
2793 return;
2794
2795 pixman_region32_union_rect(&surface->pending.damage_buffer,
2796 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002797 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002798}
2799
Kristian Høgsberg33418202011-08-16 23:01:28 -04002800static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002801destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002802{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002803 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002804
2805 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002806 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002807}
2808
2809static void
2810surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002811 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002812{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002813 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002814 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002815
2816 cb = malloc(sizeof *cb);
2817 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002818 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002819 return;
2820 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002821
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002822 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2823 callback);
2824 if (cb->resource == NULL) {
2825 free(cb);
2826 wl_resource_post_no_memory(resource);
2827 return;
2828 }
2829
Jason Ekstranda85118c2013-06-27 20:17:02 -05002830 wl_resource_set_implementation(cb->resource, NULL, cb,
2831 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002832
Pekka Paalanenbc106382012-10-10 12:49:31 +03002833 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002834}
2835
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002836static void
2837surface_set_opaque_region(struct wl_client *client,
2838 struct wl_resource *resource,
2839 struct wl_resource *region_resource)
2840{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002841 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002842 struct weston_region *region;
2843
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002844 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002845 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002846 pixman_region32_copy(&surface->pending.opaque,
2847 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002848 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002849 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002850 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002851}
2852
2853static void
2854surface_set_input_region(struct wl_client *client,
2855 struct wl_resource *resource,
2856 struct wl_resource *region_resource)
2857{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002858 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002859 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002860
2861 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002862 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002863 pixman_region32_copy(&surface->pending.input,
2864 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002865 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002866 pixman_region32_fini(&surface->pending.input);
2867 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002868 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002869}
2870
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002871/* Cause damage to this sub-surface and all its children.
2872 *
2873 * This is useful when there are state changes that need an implicit
2874 * damage, e.g. a z-order change.
2875 */
2876static void
2877weston_surface_damage_subsurfaces(struct weston_subsurface *sub)
2878{
2879 struct weston_subsurface *child;
2880
2881 weston_surface_damage(sub->surface);
2882 sub->reordered = false;
2883
2884 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link)
2885 if (child != sub)
2886 weston_surface_damage_subsurfaces(child);
2887}
2888
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002889static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002890weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002891{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002892 struct weston_subsurface *sub;
2893
2894 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2895 parent_link_pending) {
2896 wl_list_remove(&sub->parent_link);
2897 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002898
2899 if (sub->reordered)
2900 weston_surface_damage_subsurfaces(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002901 }
2902}
2903
2904static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03002905weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05002906 struct weston_matrix *matrix)
2907{
Pekka Paalanen04baea52016-04-26 15:50:58 +03002908 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05002909 double src_width, src_height, dest_width, dest_height;
2910
2911 weston_matrix_init(matrix);
2912
2913 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
2914 src_width = surface->width_from_buffer;
2915 src_height = surface->height_from_buffer;
2916 } else {
2917 src_width = wl_fixed_to_double(vp->buffer.src_width);
2918 src_height = wl_fixed_to_double(vp->buffer.src_height);
2919 }
2920
2921 if (vp->surface.width == -1) {
2922 dest_width = src_width;
2923 dest_height = src_height;
2924 } else {
2925 dest_width = vp->surface.width;
2926 dest_height = vp->surface.height;
2927 }
2928
2929 if (src_width != dest_width || src_height != dest_height)
2930 weston_matrix_scale(matrix,
2931 src_width / dest_width,
2932 src_height / dest_height, 1);
2933
2934 if (vp->buffer.src_width != wl_fixed_from_int(-1))
2935 weston_matrix_translate(matrix,
2936 wl_fixed_to_double(vp->buffer.src_x),
2937 wl_fixed_to_double(vp->buffer.src_y),
2938 0);
2939
2940 switch (vp->buffer.transform) {
2941 case WL_OUTPUT_TRANSFORM_FLIPPED:
2942 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2943 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2944 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2945 weston_matrix_scale(matrix, -1, 1, 1);
2946 weston_matrix_translate(matrix,
2947 surface->width_from_buffer, 0, 0);
2948 break;
2949 }
2950
2951 switch (vp->buffer.transform) {
2952 default:
2953 case WL_OUTPUT_TRANSFORM_NORMAL:
2954 case WL_OUTPUT_TRANSFORM_FLIPPED:
2955 break;
2956 case WL_OUTPUT_TRANSFORM_90:
2957 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2958 weston_matrix_rotate_xy(matrix, 0, 1);
2959 weston_matrix_translate(matrix,
2960 surface->height_from_buffer, 0, 0);
2961 break;
2962 case WL_OUTPUT_TRANSFORM_180:
2963 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2964 weston_matrix_rotate_xy(matrix, -1, 0);
2965 weston_matrix_translate(matrix,
2966 surface->width_from_buffer,
2967 surface->height_from_buffer, 0);
2968 break;
2969 case WL_OUTPUT_TRANSFORM_270:
2970 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2971 weston_matrix_rotate_xy(matrix, 0, -1);
2972 weston_matrix_translate(matrix,
2973 0, surface->width_from_buffer, 0);
2974 break;
2975 }
2976
2977 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
2978}
2979
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03002980/**
2981 * Compute a + b > c while being safe to overflows.
2982 */
2983static bool
2984fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
2985{
2986 return (int64_t)a + (int64_t)b > (int64_t)c;
2987}
2988
2989static bool
2990weston_surface_is_pending_viewport_source_valid(
2991 const struct weston_surface *surface)
2992{
2993 const struct weston_surface_state *pend = &surface->pending;
2994 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
2995 int width_from_buffer = 0;
2996 int height_from_buffer = 0;
2997 wl_fixed_t w;
2998 wl_fixed_t h;
2999
3000 /* If viewport source rect is not set, it is always ok. */
3001 if (vp->buffer.src_width == wl_fixed_from_int(-1))
3002 return true;
3003
3004 if (pend->newly_attached) {
3005 if (pend->buffer) {
3006 convert_size_by_transform_scale(&width_from_buffer,
3007 &height_from_buffer,
3008 pend->buffer->width,
3009 pend->buffer->height,
3010 vp->buffer.transform,
3011 vp->buffer.scale);
3012 } else {
3013 /* No buffer: viewport is irrelevant. */
3014 return true;
3015 }
3016 } else {
3017 width_from_buffer = surface->width_from_buffer;
3018 height_from_buffer = surface->height_from_buffer;
3019 }
3020
3021 assert((width_from_buffer == 0) == (height_from_buffer == 0));
3022 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
3023
3024 /* No buffer: viewport is irrelevant. */
3025 if (width_from_buffer == 0 || height_from_buffer == 0)
3026 return true;
3027
3028 /* overflow checks for wl_fixed_from_int() */
3029 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
3030 return false;
3031 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
3032 return false;
3033
3034 w = wl_fixed_from_int(width_from_buffer);
3035 h = wl_fixed_from_int(height_from_buffer);
3036
3037 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
3038 return false;
3039 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
3040 return false;
3041
3042 return true;
3043}
3044
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003045static bool
3046fixed_is_integer(wl_fixed_t v)
3047{
3048 return (v & 0xff) == 0;
3049}
3050
3051static bool
3052weston_surface_is_pending_viewport_dst_size_int(
3053 const struct weston_surface *surface)
3054{
3055 const struct weston_buffer_viewport *vp =
3056 &surface->pending.buffer_viewport;
3057
3058 if (vp->surface.width != -1) {
3059 assert(vp->surface.width > 0 && vp->surface.height > 0);
3060 return true;
3061 }
3062
3063 return fixed_is_integer(vp->buffer.src_width) &&
3064 fixed_is_integer(vp->buffer.src_height);
3065}
3066
Derek Foreman152254b2015-11-26 14:17:48 -06003067/* Translate pending damage in buffer co-ordinates to surface
3068 * co-ordinates and union it with a pixman_region32_t.
3069 * This should only be called after the buffer is attached.
3070 */
3071static void
3072apply_damage_buffer(pixman_region32_t *dest,
3073 struct weston_surface *surface,
3074 struct weston_surface_state *state)
3075{
3076 struct weston_buffer *buffer = surface->buffer_ref.buffer;
3077
3078 /* wl_surface.damage_buffer needs to be clipped to the buffer,
3079 * translated into surface co-ordinates and unioned with
3080 * any other surface damage.
3081 * None of this makes sense if there is no buffer though.
3082 */
3083 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
3084 pixman_region32_t buffer_damage;
3085
3086 pixman_region32_intersect_rect(&state->damage_buffer,
3087 &state->damage_buffer,
3088 0, 0, buffer->width,
3089 buffer->height);
3090 pixman_region32_init(&buffer_damage);
3091 weston_matrix_transform_region(&buffer_damage,
3092 &surface->buffer_to_surface_matrix,
3093 &state->damage_buffer);
3094 pixman_region32_union(dest, dest, &buffer_damage);
3095 pixman_region32_fini(&buffer_damage);
3096 }
3097 /* We should clear this on commit even if there was no buffer */
3098 pixman_region32_clear(&state->damage_buffer);
3099}
3100
Jason Ekstrand1e059042014-10-16 10:55:19 -05003101static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05003102weston_surface_commit_state(struct weston_surface *surface,
3103 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003104{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003105 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003106 pixman_region32_t opaque;
3107
Alexander Larsson4ea95522013-05-22 14:41:37 +02003108 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02003109 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03003110 /* wp_viewport.set_source */
3111 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003112 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003113
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003114 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003115 if (state->newly_attached)
3116 weston_surface_attach(surface, state->buffer);
3117 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01003118
Jason Ekstrand1e059042014-10-16 10:55:19 -05003119 weston_surface_build_buffer_matrix(surface,
3120 &surface->surface_to_buffer_matrix);
3121 weston_matrix_invert(&surface->buffer_to_surface_matrix,
3122 &surface->surface_to_buffer_matrix);
3123
Jason Ekstrand7b982072014-05-20 14:33:03 -05003124 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003125 weston_surface_update_size(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003126 if (surface->committed)
3127 surface->committed(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003128 }
Giulio Camuffo184df502013-02-21 11:29:21 +01003129
Jason Ekstrand7b982072014-05-20 14:33:03 -05003130 state->sx = 0;
3131 state->sy = 0;
3132 state->newly_attached = 0;
3133 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03003134
Derek Foreman152254b2015-11-26 14:17:48 -06003135 /* wl_surface.damage and wl_surface.damage_buffer */
Pekka Paalanenb5026542014-11-12 15:09:24 +02003136 if (weston_timeline_enabled_ &&
Derek Foreman152254b2015-11-26 14:17:48 -06003137 (pixman_region32_not_empty(&state->damage_surface) ||
3138 pixman_region32_not_empty(&state->damage_buffer)))
Pekka Paalanenb5026542014-11-12 15:09:24 +02003139 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06003140
Pekka Paalanen8e159182012-10-10 12:49:25 +03003141 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06003142 &state->damage_surface);
3143
3144 apply_damage_buffer(&surface->damage, surface, state);
3145
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05003146 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07003147 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06003148 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003149
3150 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003151 pixman_region32_init(&opaque);
3152 pixman_region32_intersect_rect(&opaque, &state->opaque,
3153 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003154
3155 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
3156 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003157 wl_list_for_each(view, &surface->views, surface_link)
3158 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003159 }
3160
3161 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003162
3163 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003164 pixman_region32_intersect_rect(&surface->input, &state->input,
3165 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003166
Pekka Paalanenbc106382012-10-10 12:49:31 +03003167 /* wl_surface.frame */
3168 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05003169 &state->frame_callback_list);
3170 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003171
3172 /* XXX:
3173 * What should happen with a feedback request, if there
3174 * is no wl_buffer attached for this commit?
3175 */
3176
3177 /* presentation.feedback */
3178 wl_list_insert_list(&surface->feedback_list,
3179 &state->feedback_list);
3180 wl_list_init(&state->feedback_list);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003181
3182 wl_signal_emit(&surface->commit_signal, surface);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003183}
3184
3185static void
3186weston_surface_commit(struct weston_surface *surface)
3187{
3188 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003189
Pekka Paalanene67858b2013-04-25 13:57:42 +03003190 weston_surface_commit_subsurface_order(surface);
3191
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003192 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003193}
3194
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003195static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003196weston_subsurface_commit(struct weston_subsurface *sub);
3197
3198static void
3199weston_subsurface_parent_commit(struct weston_subsurface *sub,
3200 int parent_is_synchronized);
3201
3202static void
3203surface_commit(struct wl_client *client, struct wl_resource *resource)
3204{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003205 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003206 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
3207
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003208 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
3209 assert(surface->viewport_resource);
3210
3211 wl_resource_post_error(surface->viewport_resource,
3212 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
3213 "wl_surface@%d has viewport source outside buffer",
3214 wl_resource_get_id(resource));
3215 return;
3216 }
3217
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003218 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
3219 assert(surface->viewport_resource);
3220
3221 wl_resource_post_error(surface->viewport_resource,
3222 WP_VIEWPORT_ERROR_BAD_SIZE,
3223 "wl_surface@%d viewport dst size not integer",
3224 wl_resource_get_id(resource));
3225 return;
3226 }
3227
Pekka Paalanene67858b2013-04-25 13:57:42 +03003228 if (sub) {
3229 weston_subsurface_commit(sub);
3230 return;
3231 }
3232
3233 weston_surface_commit(surface);
3234
3235 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
3236 if (sub->surface != surface)
3237 weston_subsurface_parent_commit(sub, 0);
3238 }
3239}
3240
3241static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003242surface_set_buffer_transform(struct wl_client *client,
3243 struct wl_resource *resource, int transform)
3244{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003245 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003246
Jonny Lamba55f1392014-05-30 12:07:15 +02003247 /* if wl_output.transform grows more members this will need to be updated. */
3248 if (transform < 0 ||
3249 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
3250 wl_resource_post_error(resource,
3251 WL_SURFACE_ERROR_INVALID_TRANSFORM,
3252 "buffer transform must be a valid transform "
3253 "('%d' specified)", transform);
3254 return;
3255 }
3256
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003257 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003258 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003259}
3260
Alexander Larsson4ea95522013-05-22 14:41:37 +02003261static void
3262surface_set_buffer_scale(struct wl_client *client,
3263 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003264 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003265{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003266 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003267
Jonny Lamba55f1392014-05-30 12:07:15 +02003268 if (scale < 1) {
3269 wl_resource_post_error(resource,
3270 WL_SURFACE_ERROR_INVALID_SCALE,
3271 "buffer scale must be at least one "
3272 "('%d' specified)", scale);
3273 return;
3274 }
3275
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003276 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003277 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003278}
3279
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003280static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003281 surface_destroy,
3282 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04003283 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003284 surface_frame,
3285 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003286 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003287 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02003288 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06003289 surface_set_buffer_scale,
3290 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003291};
3292
3293static void
3294compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003295 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003296{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04003297 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003298 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003299
Kristian Høgsberg18c93002012-01-27 11:58:31 -05003300 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003301 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003302 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003303 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003304 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003305
Jason Ekstranda85118c2013-06-27 20:17:02 -05003306 surface->resource =
3307 wl_resource_create(client, &wl_surface_interface,
3308 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003309 if (surface->resource == NULL) {
3310 weston_surface_destroy(surface);
3311 wl_resource_post_no_memory(resource);
3312 return;
3313 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003314 wl_resource_set_implementation(surface->resource, &surface_interface,
3315 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003316
3317 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003318}
3319
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003320static void
3321destroy_region(struct wl_resource *resource)
3322{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003323 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003324
3325 pixman_region32_fini(&region->region);
3326 free(region);
3327}
3328
3329static void
3330region_destroy(struct wl_client *client, struct wl_resource *resource)
3331{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003332 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003333}
3334
3335static void
3336region_add(struct wl_client *client, struct wl_resource *resource,
3337 int32_t x, int32_t y, int32_t width, int32_t height)
3338{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003339 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003340
3341 pixman_region32_union_rect(&region->region, &region->region,
3342 x, y, width, height);
3343}
3344
3345static void
3346region_subtract(struct wl_client *client, struct wl_resource *resource,
3347 int32_t x, int32_t y, int32_t width, int32_t height)
3348{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003349 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003350 pixman_region32_t rect;
3351
3352 pixman_region32_init_rect(&rect, x, y, width, height);
3353 pixman_region32_subtract(&region->region, &region->region, &rect);
3354 pixman_region32_fini(&rect);
3355}
3356
3357static const struct wl_region_interface region_interface = {
3358 region_destroy,
3359 region_add,
3360 region_subtract
3361};
3362
3363static void
3364compositor_create_region(struct wl_client *client,
3365 struct wl_resource *resource, uint32_t id)
3366{
3367 struct weston_region *region;
3368
3369 region = malloc(sizeof *region);
3370 if (region == NULL) {
3371 wl_resource_post_no_memory(resource);
3372 return;
3373 }
3374
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003375 pixman_region32_init(&region->region);
3376
Jason Ekstranda85118c2013-06-27 20:17:02 -05003377 region->resource =
3378 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003379 if (region->resource == NULL) {
3380 free(region);
3381 wl_resource_post_no_memory(resource);
3382 return;
3383 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003384 wl_resource_set_implementation(region->resource, &region_interface,
3385 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003386}
3387
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003388static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003389 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003390 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003391};
3392
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003393static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003394weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3395{
3396 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003397
Jason Ekstrand7b982072014-05-20 14:33:03 -05003398 weston_surface_commit_state(surface, &sub->cached);
3399 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003400
3401 weston_surface_commit_subsurface_order(surface);
3402
3403 weston_surface_schedule_repaint(surface);
3404
Jason Ekstrand7b982072014-05-20 14:33:03 -05003405 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003406}
3407
3408static void
3409weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3410{
3411 struct weston_surface *surface = sub->surface;
3412
3413 /*
3414 * If this commit would cause the surface to move by the
3415 * attach(dx, dy) parameters, the old damage region must be
3416 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05003417 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003418 */
Derek Foreman152254b2015-11-26 14:17:48 -06003419 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003420 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06003421 pixman_region32_union(&sub->cached.damage_surface,
3422 &sub->cached.damage_surface,
3423 &surface->pending.damage_surface);
3424 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003425
3426 if (surface->pending.newly_attached) {
3427 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003428 weston_surface_state_set_buffer(&sub->cached,
3429 surface->pending.buffer);
3430 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003431 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003432 weston_presentation_feedback_discard_list(
3433 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003434 }
3435 sub->cached.sx += surface->pending.sx;
3436 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003437
Derek Foreman152254b2015-11-26 14:17:48 -06003438 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
3439
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003440 sub->cached.buffer_viewport.changed |=
3441 surface->pending.buffer_viewport.changed;
3442 sub->cached.buffer_viewport.buffer =
3443 surface->pending.buffer_viewport.buffer;
3444 sub->cached.buffer_viewport.surface =
3445 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003446
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003447 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003448
3449 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3450
3451 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3452
3453 wl_list_insert_list(&sub->cached.frame_callback_list,
3454 &surface->pending.frame_callback_list);
3455 wl_list_init(&surface->pending.frame_callback_list);
3456
Pekka Paalanen133e4392014-09-23 22:08:46 -04003457 wl_list_insert_list(&sub->cached.feedback_list,
3458 &surface->pending.feedback_list);
3459 wl_list_init(&surface->pending.feedback_list);
3460
Jason Ekstrand7b982072014-05-20 14:33:03 -05003461 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003462}
3463
Derek Foreman280e7dd2014-10-03 13:13:42 -05003464static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003465weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3466{
3467 while (sub) {
3468 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003469 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003470
3471 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003472 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003473
3474 sub = weston_surface_to_subsurface(sub->parent);
3475 }
3476
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003477 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003478}
3479
3480static void
3481weston_subsurface_commit(struct weston_subsurface *sub)
3482{
3483 struct weston_surface *surface = sub->surface;
3484 struct weston_subsurface *tmp;
3485
3486 /* Recursive check for effectively synchronized. */
3487 if (weston_subsurface_is_synchronized(sub)) {
3488 weston_subsurface_commit_to_cache(sub);
3489 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003490 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003491 /* flush accumulated state from cache */
3492 weston_subsurface_commit_to_cache(sub);
3493 weston_subsurface_commit_from_cache(sub);
3494 } else {
3495 weston_surface_commit(surface);
3496 }
3497
3498 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3499 if (tmp->surface != surface)
3500 weston_subsurface_parent_commit(tmp, 0);
3501 }
3502 }
3503}
3504
3505static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003506weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003507{
3508 struct weston_surface *surface = sub->surface;
3509 struct weston_subsurface *tmp;
3510
Pekka Paalanene67858b2013-04-25 13:57:42 +03003511 /* From now on, commit_from_cache the whole sub-tree, regardless of
3512 * the synchronized mode of each child. This sub-surface or some
3513 * of its ancestors were synchronized, so we are synchronized
3514 * all the way down.
3515 */
3516
Jason Ekstrand7b982072014-05-20 14:33:03 -05003517 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003518 weston_subsurface_commit_from_cache(sub);
3519
3520 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3521 if (tmp->surface != surface)
3522 weston_subsurface_parent_commit(tmp, 1);
3523 }
3524}
3525
3526static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003527weston_subsurface_parent_commit(struct weston_subsurface *sub,
3528 int parent_is_synchronized)
3529{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003530 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003531 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003532 wl_list_for_each(view, &sub->surface->views, surface_link)
3533 weston_view_set_position(view,
3534 sub->position.x,
3535 sub->position.y);
3536
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003537 sub->position.set = 0;
3538 }
3539
3540 if (parent_is_synchronized || sub->synchronized)
3541 weston_subsurface_synchronized_commit(sub);
3542}
3543
Pekka Paalanen8274d902014-08-06 19:36:51 +03003544static int
3545subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3546{
3547 return snprintf(buf, len, "sub-surface");
3548}
3549
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003550static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003551subsurface_committed(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003552{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003553 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003554
Jason Ekstranda7af7042013-10-12 22:38:11 -05003555 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003556 weston_view_set_position(view,
3557 view->geometry.x + dx,
3558 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003559
3560 /* No need to check parent mappedness, because if parent is not
3561 * mapped, parent is not in a visible layer, so this sub-surface
3562 * will not be drawn either.
3563 */
Armin Krezovićf8486c32016-06-30 06:04:28 +02003564
Pekka Paalanene67858b2013-04-25 13:57:42 +03003565 if (!weston_surface_is_mapped(surface)) {
Armin Krezovićf8486c32016-06-30 06:04:28 +02003566 surface->is_mapped = true;
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003567
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003568 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003569 * because that would call it also for the parent surface,
3570 * which might not be mapped yet. That would lead to
3571 * inconsistent state, where the window could never be
3572 * mapped.
3573 *
Armin Krezovićf8486c32016-06-30 06:04:28 +02003574 * Instead just force the is_mapped flag on, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003575 * weston_surface_is_mapped() return true, so that when the
3576 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003577 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003578 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03003579 }
3580}
3581
3582static struct weston_subsurface *
3583weston_surface_to_subsurface(struct weston_surface *surface)
3584{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003585 if (surface->committed == subsurface_committed)
3586 return surface->committed_private;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003587
3588 return NULL;
3589}
3590
Pekka Paalanen01388e22013-04-25 13:57:44 +03003591WL_EXPORT struct weston_surface *
3592weston_surface_get_main_surface(struct weston_surface *surface)
3593{
3594 struct weston_subsurface *sub;
3595
3596 while (surface && (sub = weston_surface_to_subsurface(surface)))
3597 surface = sub->parent;
3598
3599 return surface;
3600}
3601
Pekka Paalanen50b67472014-10-01 15:02:41 +03003602WL_EXPORT int
3603weston_surface_set_role(struct weston_surface *surface,
3604 const char *role_name,
3605 struct wl_resource *error_resource,
3606 uint32_t error_code)
3607{
3608 assert(role_name);
3609
3610 if (surface->role_name == NULL ||
3611 surface->role_name == role_name ||
3612 strcmp(surface->role_name, role_name) == 0) {
3613 surface->role_name = role_name;
3614
3615 return 0;
3616 }
3617
3618 wl_resource_post_error(error_resource, error_code,
3619 "Cannot assign role %s to wl_surface@%d,"
3620 " already has role %s\n",
3621 role_name,
3622 wl_resource_get_id(surface->resource),
3623 surface->role_name);
3624 return -1;
3625}
3626
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02003627WL_EXPORT const char *
3628weston_surface_get_role(struct weston_surface *surface)
3629{
3630 return surface->role_name;
3631}
3632
Pekka Paalanen8274d902014-08-06 19:36:51 +03003633WL_EXPORT void
3634weston_surface_set_label_func(struct weston_surface *surface,
3635 int (*desc)(struct weston_surface *,
3636 char *, size_t))
3637{
3638 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02003639 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003640}
3641
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003642/** Get the size of surface contents
3643 *
3644 * \param surface The surface to query.
3645 * \param width Returns the width of raw contents.
3646 * \param height Returns the height of raw contents.
3647 *
3648 * Retrieves the raw surface content size in pixels for the given surface.
3649 * This is the whole content size in buffer pixels. If the surface
3650 * has no content or the renderer does not implement this feature,
3651 * zeroes are returned.
3652 *
3653 * This function is used to determine the buffer size needed for
3654 * a weston_surface_copy_content() call.
3655 */
3656WL_EXPORT void
3657weston_surface_get_content_size(struct weston_surface *surface,
3658 int *width, int *height)
3659{
3660 struct weston_renderer *rer = surface->compositor->renderer;
3661
3662 if (!rer->surface_get_content_size) {
3663 *width = 0;
3664 *height = 0;
3665 return;
3666 }
3667
3668 rer->surface_get_content_size(surface, width, height);
3669}
3670
Quentin Glidic248dd102016-08-12 10:41:34 +02003671/** Get the bounding box of a surface and its subsurfaces
3672 *
3673 * \param surface The surface to query.
3674 * \return The bounding box relative to the surface origin.
3675 *
3676 */
3677WL_EXPORT struct weston_geometry
3678weston_surface_get_bounding_box(struct weston_surface *surface)
3679{
3680 pixman_region32_t region;
3681 pixman_box32_t *box;
3682 struct weston_subsurface *subsurface;
3683
3684 pixman_region32_init_rect(&region,
3685 0, 0,
3686 surface->width, surface->height);
3687
3688 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link)
3689 pixman_region32_union_rect(&region, &region,
3690 subsurface->position.x,
3691 subsurface->position.y,
3692 subsurface->surface->width,
3693 subsurface->surface->height);
3694
3695 box = pixman_region32_extents(&region);
3696 struct weston_geometry geometry = {
3697 .x = box->x1,
3698 .y = box->y1,
3699 .width = box->x2 - box->x1,
3700 .height = box->y2 - box->y1,
3701 };
3702
3703 pixman_region32_fini(&region);
3704
3705 return geometry;
3706}
3707
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003708/** Copy surface contents to system memory.
3709 *
3710 * \param surface The surface to copy from.
3711 * \param target Pointer to the target memory buffer.
3712 * \param size Size of the target buffer in bytes.
3713 * \param src_x X location on contents to copy from.
3714 * \param src_y Y location on contents to copy from.
3715 * \param width Width in pixels of the area to copy.
3716 * \param height Height in pixels of the area to copy.
3717 * \return 0 for success, -1 for failure.
3718 *
3719 * Surface contents are maintained by the renderer. They can be in a
3720 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
3721 * else.
3722 *
3723 * Surface contents are copied into memory pointed to by target,
3724 * which has size bytes of space available. The target memory
3725 * may be larger than needed, but being smaller returns an error.
3726 * The extra bytes in target may or may not be written; their content is
3727 * unspecified. Size must be large enough to hold the image.
3728 *
3729 * The image in the target memory will be arranged in rows from
3730 * top to bottom, and pixels on a row from left to right. The pixel
3731 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
3732 * width * 4.
3733 *
3734 * Parameters src_x and src_y define the upper-left corner in buffer
3735 * coordinates (pixels) to copy from. Parameters width and height
3736 * define the size of the area to copy in pixels.
3737 *
3738 * The rectangle defined by src_x, src_y, width, height must fit in
3739 * the surface contents. Otherwise an error is returned.
3740 *
3741 * Use surface_get_data_size to determine the content size; the
3742 * needed target buffer size and rectangle limits.
3743 *
3744 * CURRENT IMPLEMENTATION RESTRICTIONS:
3745 * - the machine must be little-endian due to Pixman formats.
3746 *
3747 * NOTE: Pixman formats are premultiplied.
3748 */
3749WL_EXPORT int
3750weston_surface_copy_content(struct weston_surface *surface,
3751 void *target, size_t size,
3752 int src_x, int src_y,
3753 int width, int height)
3754{
3755 struct weston_renderer *rer = surface->compositor->renderer;
3756 int cw, ch;
3757 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
3758
3759 if (!rer->surface_copy_content)
3760 return -1;
3761
3762 weston_surface_get_content_size(surface, &cw, &ch);
3763
3764 if (src_x < 0 || src_y < 0)
3765 return -1;
3766
3767 if (width <= 0 || height <= 0)
3768 return -1;
3769
3770 if (src_x + width > cw || src_y + height > ch)
3771 return -1;
3772
3773 if (width * bytespp * height > size)
3774 return -1;
3775
3776 return rer->surface_copy_content(surface, target, size,
3777 src_x, src_y, width, height);
3778}
3779
Pekka Paalanene67858b2013-04-25 13:57:42 +03003780static void
3781subsurface_set_position(struct wl_client *client,
3782 struct wl_resource *resource, int32_t x, int32_t y)
3783{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003784 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003785
3786 if (!sub)
3787 return;
3788
3789 sub->position.x = x;
3790 sub->position.y = y;
3791 sub->position.set = 1;
3792}
3793
3794static struct weston_subsurface *
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003795subsurface_find_sibling(struct weston_subsurface *sub,
3796 struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003797{
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003798 struct weston_surface *parent = sub->parent;
3799 struct weston_subsurface *sibling;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003800
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003801 wl_list_for_each(sibling, &parent->subsurface_list, parent_link) {
3802 if (sibling->surface == surface && sibling != sub)
3803 return sibling;
3804 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003805
3806 return NULL;
3807}
3808
3809static struct weston_subsurface *
3810subsurface_sibling_check(struct weston_subsurface *sub,
3811 struct weston_surface *surface,
3812 const char *request)
3813{
3814 struct weston_subsurface *sibling;
3815
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003816 sibling = subsurface_find_sibling(sub, surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003817 if (!sibling) {
3818 wl_resource_post_error(sub->resource,
3819 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3820 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003821 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003822 return NULL;
3823 }
3824
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003825 assert(sibling->parent == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003826
3827 return sibling;
3828}
3829
3830static void
3831subsurface_place_above(struct wl_client *client,
3832 struct wl_resource *resource,
3833 struct wl_resource *sibling_resource)
3834{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003835 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003836 struct weston_surface *surface =
3837 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003838 struct weston_subsurface *sibling;
3839
3840 if (!sub)
3841 return;
3842
3843 sibling = subsurface_sibling_check(sub, surface, "place_above");
3844 if (!sibling)
3845 return;
3846
3847 wl_list_remove(&sub->parent_link_pending);
3848 wl_list_insert(sibling->parent_link_pending.prev,
3849 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003850
3851 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003852}
3853
3854static void
3855subsurface_place_below(struct wl_client *client,
3856 struct wl_resource *resource,
3857 struct wl_resource *sibling_resource)
3858{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003859 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003860 struct weston_surface *surface =
3861 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003862 struct weston_subsurface *sibling;
3863
3864 if (!sub)
3865 return;
3866
3867 sibling = subsurface_sibling_check(sub, surface, "place_below");
3868 if (!sibling)
3869 return;
3870
3871 wl_list_remove(&sub->parent_link_pending);
3872 wl_list_insert(&sibling->parent_link_pending,
3873 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003874
3875 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003876}
3877
3878static void
3879subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
3880{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003881 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003882
3883 if (sub)
3884 sub->synchronized = 1;
3885}
3886
3887static void
3888subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
3889{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003890 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003891
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003892 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003893 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003894
3895 /* If sub became effectively desynchronized, flush. */
3896 if (!weston_subsurface_is_synchronized(sub))
3897 weston_subsurface_synchronized_commit(sub);
3898 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003899}
3900
3901static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003902weston_subsurface_unlink_parent(struct weston_subsurface *sub)
3903{
3904 wl_list_remove(&sub->parent_link);
3905 wl_list_remove(&sub->parent_link_pending);
3906 wl_list_remove(&sub->parent_destroy_listener.link);
3907 sub->parent = NULL;
3908}
3909
3910static void
3911weston_subsurface_destroy(struct weston_subsurface *sub);
3912
3913static void
3914subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
3915{
3916 struct weston_subsurface *sub =
3917 container_of(listener, struct weston_subsurface,
3918 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003919 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003920
3921 /* The protocol object (wl_resource) is left inert. */
3922 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003923 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003924
3925 weston_subsurface_destroy(sub);
3926}
3927
3928static void
3929subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3930{
3931 struct weston_subsurface *sub =
3932 container_of(listener, struct weston_subsurface,
3933 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003934 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003935 assert(sub->surface != sub->parent);
3936
3937 if (weston_surface_is_mapped(sub->surface))
3938 weston_surface_unmap(sub->surface);
3939
3940 weston_subsurface_unlink_parent(sub);
3941}
3942
3943static void
3944subsurface_resource_destroy(struct wl_resource *resource)
3945{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003946 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003947
3948 if (sub)
3949 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003950}
3951
3952static void
3953subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
3954{
3955 wl_resource_destroy(resource);
3956}
3957
3958static void
3959weston_subsurface_link_parent(struct weston_subsurface *sub,
3960 struct weston_surface *parent)
3961{
3962 sub->parent = parent;
3963 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003964 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003965 &sub->parent_destroy_listener);
3966
3967 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3968 wl_list_insert(&parent->subsurface_list_pending,
3969 &sub->parent_link_pending);
3970}
3971
3972static void
3973weston_subsurface_link_surface(struct weston_subsurface *sub,
3974 struct weston_surface *surface)
3975{
3976 sub->surface = surface;
3977 sub->surface_destroy_listener.notify =
3978 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003979 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003980 &sub->surface_destroy_listener);
3981}
3982
3983static void
3984weston_subsurface_destroy(struct weston_subsurface *sub)
3985{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003986 struct weston_view *view, *next;
3987
Pekka Paalanene67858b2013-04-25 13:57:42 +03003988 assert(sub->surface);
3989
3990 if (sub->resource) {
3991 assert(weston_surface_to_subsurface(sub->surface) == sub);
3992 assert(sub->parent_destroy_listener.notify ==
3993 subsurface_handle_parent_destroy);
3994
George Kiagiadakised04d382014-06-13 18:10:26 +02003995 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3996 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003997 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02003998 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05003999
Pekka Paalanene67858b2013-04-25 13:57:42 +03004000 if (sub->parent)
4001 weston_subsurface_unlink_parent(sub);
4002
Jason Ekstrand7b982072014-05-20 14:33:03 -05004003 weston_surface_state_fini(&sub->cached);
4004 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004005
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004006 sub->surface->committed = NULL;
4007 sub->surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004008 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004009 } else {
4010 /* the dummy weston_subsurface for the parent itself */
4011 assert(sub->parent_destroy_listener.notify == NULL);
4012 wl_list_remove(&sub->parent_link);
4013 wl_list_remove(&sub->parent_link_pending);
4014 }
4015
4016 wl_list_remove(&sub->surface_destroy_listener.link);
4017 free(sub);
4018}
4019
4020static const struct wl_subsurface_interface subsurface_implementation = {
4021 subsurface_destroy,
4022 subsurface_set_position,
4023 subsurface_place_above,
4024 subsurface_place_below,
4025 subsurface_set_sync,
4026 subsurface_set_desync
4027};
4028
4029static struct weston_subsurface *
4030weston_subsurface_create(uint32_t id, struct weston_surface *surface,
4031 struct weston_surface *parent)
4032{
4033 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004034 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004035
Bryce Harringtonde16d892014-11-20 22:21:57 -08004036 sub = zalloc(sizeof *sub);
4037 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004038 return NULL;
4039
Jason Ekstranda7af7042013-10-12 22:38:11 -05004040 wl_list_init(&sub->unused_views);
4041
Jason Ekstranda85118c2013-06-27 20:17:02 -05004042 sub->resource =
4043 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004044 if (!sub->resource) {
4045 free(sub);
4046 return NULL;
4047 }
4048
Jason Ekstranda85118c2013-06-27 20:17:02 -05004049 wl_resource_set_implementation(sub->resource,
4050 &subsurface_implementation,
4051 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004052 weston_subsurface_link_surface(sub, surface);
4053 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05004054 weston_surface_state_init(&sub->cached);
4055 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004056 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004057
4058 return sub;
4059}
4060
4061/* Create a dummy subsurface for having the parent itself in its
4062 * sub-surface lists. Makes stacking order manipulation easy.
4063 */
4064static struct weston_subsurface *
4065weston_subsurface_create_for_parent(struct weston_surface *parent)
4066{
4067 struct weston_subsurface *sub;
4068
Bryce Harringtonde16d892014-11-20 22:21:57 -08004069 sub = zalloc(sizeof *sub);
4070 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004071 return NULL;
4072
4073 weston_subsurface_link_surface(sub, parent);
4074 sub->parent = parent;
4075 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4076 wl_list_insert(&parent->subsurface_list_pending,
4077 &sub->parent_link_pending);
4078
4079 return sub;
4080}
4081
4082static void
4083subcompositor_get_subsurface(struct wl_client *client,
4084 struct wl_resource *resource,
4085 uint32_t id,
4086 struct wl_resource *surface_resource,
4087 struct wl_resource *parent_resource)
4088{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004089 struct weston_surface *surface =
4090 wl_resource_get_user_data(surface_resource);
4091 struct weston_surface *parent =
4092 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004093 struct weston_subsurface *sub;
4094 static const char where[] = "get_subsurface: wl_subsurface@";
4095
4096 if (surface == parent) {
4097 wl_resource_post_error(resource,
4098 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4099 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004100 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004101 return;
4102 }
4103
4104 if (weston_surface_to_subsurface(surface)) {
4105 wl_resource_post_error(resource,
4106 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4107 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004108 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004109 return;
4110 }
4111
Pekka Paalanen50b67472014-10-01 15:02:41 +03004112 if (weston_surface_set_role(surface, "wl_subsurface", resource,
4113 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004114 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004115
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004116 if (weston_surface_get_main_surface(parent) == surface) {
4117 wl_resource_post_error(resource,
4118 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4119 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004120 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004121 return;
4122 }
4123
Pekka Paalanene67858b2013-04-25 13:57:42 +03004124 /* make sure the parent is in its own list */
4125 if (wl_list_empty(&parent->subsurface_list)) {
4126 if (!weston_subsurface_create_for_parent(parent)) {
4127 wl_resource_post_no_memory(resource);
4128 return;
4129 }
4130 }
4131
4132 sub = weston_subsurface_create(id, surface, parent);
4133 if (!sub) {
4134 wl_resource_post_no_memory(resource);
4135 return;
4136 }
4137
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004138 surface->committed = subsurface_committed;
4139 surface->committed_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004140 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004141}
4142
4143static void
4144subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
4145{
4146 wl_resource_destroy(resource);
4147}
4148
4149static const struct wl_subcompositor_interface subcompositor_interface = {
4150 subcompositor_destroy,
4151 subcompositor_get_subsurface
4152};
4153
4154static void
4155bind_subcompositor(struct wl_client *client,
4156 void *data, uint32_t version, uint32_t id)
4157{
4158 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004159 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004160
Jason Ekstranda85118c2013-06-27 20:17:02 -05004161 resource =
4162 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004163 if (resource == NULL) {
4164 wl_client_post_no_memory(client);
4165 return;
4166 }
4167 wl_resource_set_implementation(resource, &subcompositor_interface,
4168 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004169}
4170
Bryce Harrington0795ece2016-08-30 12:04:26 -07004171/** Set a DPMS mode on all of the compositor's outputs
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004172 *
4173 * \param compositor The compositor instance
4174 * \param state The DPMS state the outputs will be set to
4175 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004176static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004177weston_compositor_dpms(struct weston_compositor *compositor,
4178 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004179{
4180 struct weston_output *output;
4181
Bryce Harrington08976ac2016-08-30 12:05:16 -07004182 wl_list_for_each(output, &compositor->output_list, link)
4183 if (output->set_dpms)
4184 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004185}
4186
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004187/** Restores the compositor to active status
4188 *
4189 * \param compositor The compositor instance
4190 *
4191 * If the compositor was in a sleeping mode, all outputs are powered
4192 * back on via DPMS. Otherwise if the compositor was inactive
4193 * (idle/locked, offscreen, or sleeping) then the compositor's wake
4194 * signal will fire.
4195 *
4196 * Restarts the idle timer.
4197 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004198WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004199weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004200{
Neil Roberts8b62e202013-09-30 13:14:47 +01004201 uint32_t old_state = compositor->state;
4202
4203 /* The state needs to be changed before emitting the wake
4204 * signal because that may try to schedule a repaint which
4205 * will not work if the compositor is still sleeping */
4206 compositor->state = WESTON_COMPOSITOR_ACTIVE;
4207
4208 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004209 case WESTON_COMPOSITOR_SLEEPING:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004210 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004211 case WESTON_COMPOSITOR_OFFSCREEN:
Daniel Stone893b9362016-11-08 15:47:09 +00004212 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004213 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004214 /* fall through */
4215 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004216 wl_event_source_timer_update(compositor->idle_source,
4217 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004218 }
4219}
4220
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004221/** Turns off rendering and frame events for the compositor.
4222 *
4223 * \param compositor The compositor instance
4224 *
4225 * This is used for example to prevent further rendering while the
4226 * compositor is shutting down.
4227 *
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004228 * Stops the idle timer.
4229 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004230WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004231weston_compositor_offscreen(struct weston_compositor *compositor)
4232{
4233 switch (compositor->state) {
4234 case WESTON_COMPOSITOR_OFFSCREEN:
4235 return;
4236 case WESTON_COMPOSITOR_SLEEPING:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004237 default:
4238 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4239 wl_event_source_timer_update(compositor->idle_source, 0);
4240 }
4241}
4242
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004243/** Powers down all attached output devices
4244 *
4245 * \param compositor The compositor instance
4246 *
4247 * Causes rendering to the outputs to cease, and no frame events to be
4248 * sent. Only powers down the outputs if the compositor is not already
4249 * in sleep mode.
4250 *
4251 * Stops the idle timer.
4252 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004253WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004254weston_compositor_sleep(struct weston_compositor *compositor)
4255{
4256 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
4257 return;
4258
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004259 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004260 compositor->state = WESTON_COMPOSITOR_SLEEPING;
4261 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
4262}
4263
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004264/** Sets compositor to idle mode
4265 *
4266 * \param data The compositor instance
4267 *
4268 * This is called when the idle timer fires. Once the compositor is in
4269 * idle mode it requires a wake action (e.g. via
4270 * weston_compositor_wake()) to restore it. The compositor's
4271 * idle_signal will be triggered when the idle event occurs.
4272 *
4273 * Idleness can be inhibited by setting the compositor's idle_inhibit
4274 * property.
4275 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004276static int
4277idle_handler(void *data)
4278{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004279 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004280
4281 if (compositor->idle_inhibit)
4282 return 1;
4283
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004284 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004285 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004286
4287 return 1;
4288}
4289
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004290WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08004291weston_plane_init(struct weston_plane *plane,
4292 struct weston_compositor *ec,
4293 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004294{
4295 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004296 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004297 plane->x = x;
4298 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08004299 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004300
4301 /* Init the link so that the call to wl_list_remove() when releasing
4302 * the plane without ever stacking doesn't lead to a crash */
4303 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004304}
4305
4306WL_EXPORT void
4307weston_plane_release(struct weston_plane *plane)
4308{
Xiong Zhang97116532013-10-23 13:58:31 +08004309 struct weston_view *view;
4310
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004311 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004312 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004313
Xiong Zhang97116532013-10-23 13:58:31 +08004314 wl_list_for_each(view, &plane->compositor->view_list, link) {
4315 if (view->plane == plane)
4316 view->plane = NULL;
4317 }
4318
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004319 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004320}
4321
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004322WL_EXPORT void
4323weston_compositor_stack_plane(struct weston_compositor *ec,
4324 struct weston_plane *plane,
4325 struct weston_plane *above)
4326{
4327 if (above)
4328 wl_list_insert(above->link.prev, &plane->link);
4329 else
4330 wl_list_insert(&ec->plane_list, &plane->link);
4331}
4332
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004333static void
4334output_release(struct wl_client *client, struct wl_resource *resource)
4335{
4336 wl_resource_destroy(resource);
4337}
4338
4339static const struct wl_output_interface output_interface = {
4340 output_release,
4341};
4342
4343
Casey Dahlin9074db52012-04-19 22:50:09 -04004344static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004345{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004346 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004347}
4348
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004349static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004350bind_output(struct wl_client *client,
4351 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05004352{
Pekka Paalanen05347622017-03-27 12:24:34 +03004353 struct weston_head *head = data;
4354 struct weston_output *output = head->output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004355 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004356 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004357
Jason Ekstranda85118c2013-06-27 20:17:02 -05004358 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004359 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004360 if (resource == NULL) {
4361 wl_client_post_no_memory(client);
4362 return;
4363 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004364
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03004365 wl_list_insert(&head->resource_list, wl_resource_get_link(resource));
Pekka Paalanen055c1132017-03-27 16:31:25 +03004366 wl_resource_set_implementation(resource, &output_interface, head,
Pekka Paalanen05347622017-03-27 12:24:34 +03004367 unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04004368
Pekka Paalanen05347622017-03-27 12:24:34 +03004369 assert(output);
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004370 wl_output_send_geometry(resource,
4371 output->x,
4372 output->y,
Pekka Paalanen01f60212017-03-24 15:39:24 +02004373 head->mm_width,
4374 head->mm_height,
4375 head->subpixel,
4376 head->make, head->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04004377 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004378 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004379 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02004380 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004381
4382 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004383 wl_output_send_mode(resource,
4384 mode->flags,
4385 mode->width,
4386 mode->height,
4387 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004388 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02004389
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004390 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004391 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004392}
4393
Pekka Paalanendcac3512017-12-08 14:13:34 +02004394static void
4395weston_head_add_global(struct weston_head *head)
4396{
4397 head->global = wl_global_create(head->compositor->wl_display,
4398 &wl_output_interface, 3,
4399 head, bind_output);
4400}
4401
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02004402/** Remove the global wl_output protocol object
4403 *
4404 * \param head The head whose global to remove.
4405 *
4406 * Also orphans the wl_resources for this head (wl_output).
4407 */
4408static void
4409weston_head_remove_global(struct weston_head *head)
4410{
4411 struct wl_resource *resource, *tmp;
4412
4413 if (head->global)
4414 wl_global_destroy(head->global);
4415 head->global = NULL;
4416
4417 wl_resource_for_each_safe(resource, tmp, &head->resource_list) {
4418 unbind_resource(resource);
4419 wl_resource_set_destructor(resource, NULL);
4420 wl_resource_set_user_data(resource, NULL);
4421 }
4422}
4423
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004424/** Get the backing object of wl_output
4425 *
4426 * \param resource A wl_output protocol object.
4427 * \return The backing object (user data) of a wl_resource representing a
4428 * wl_output protocol object.
4429 */
Pekka Paalanen055c1132017-03-27 16:31:25 +03004430WL_EXPORT struct weston_head *
4431weston_head_from_resource(struct wl_resource *resource)
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004432{
4433 assert(wl_resource_instance_of(resource, &wl_output_interface,
4434 &output_interface));
4435
4436 return wl_resource_get_user_data(resource);
4437}
4438
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004439/** Initialize a pre-allocated weston_head
4440 *
4441 * \param head The head to initialize.
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004442 * \param name The head name, e.g. the connector name or equivalent.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004443 *
4444 * The head will be safe to attach, detach and release.
4445 *
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004446 * The name is used in logs, and can be used by compositors as a configuration
4447 * identifier.
4448 *
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004449 * \memberof weston_head
4450 * \internal
4451 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004452WL_EXPORT void
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004453weston_head_init(struct weston_head *head, const char *name)
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004454{
4455 /* Add some (in)sane defaults which can be used
4456 * for checking if an output was properly configured
4457 */
4458 memset(head, 0, sizeof *head);
4459
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004460 wl_list_init(&head->compositor_link);
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03004461 wl_signal_init(&head->destroy_signal);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004462 wl_list_init(&head->output_link);
4463 wl_list_init(&head->resource_list);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004464 head->name = strdup(name);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004465}
4466
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004467/** Send output heads changed signal
4468 *
4469 * \param output The output that changed.
4470 *
4471 * Notify that the enabled output gained and/or lost heads, or that the
4472 * associated heads may have changed their connection status. This does not
4473 * include cases where the output becomes enabled or disabled. The registered
4474 * callbacks are called after the change has successfully happened.
4475 *
4476 * If connection status change causes the compositor to attach or detach a head
4477 * to an enabled output, the registered callbacks may be called multiple times.
4478 */
4479static void
4480weston_output_emit_heads_changed(struct weston_output *output)
4481{
4482 wl_signal_emit(&output->compositor->output_heads_changed_signal,
4483 output);
4484}
4485
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004486/** Idle task for emitting heads_changed_signal */
4487static void
4488weston_compositor_call_heads_changed(void *data)
4489{
4490 struct weston_compositor *compositor = data;
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004491 struct weston_head *head;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004492
4493 compositor->heads_changed_source = NULL;
4494
4495 wl_signal_emit(&compositor->heads_changed_signal, compositor);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004496
4497 wl_list_for_each(head, &compositor->head_list, compositor_link) {
4498 if (head->output && head->output->enabled)
4499 weston_output_emit_heads_changed(head->output);
4500 }
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004501}
4502
4503/** Schedule a call on idle to heads_changed callback
4504 *
4505 * \param compositor The Compositor.
4506 *
4507 * \memberof weston_compositor
4508 * \internal
4509 */
4510static void
4511weston_compositor_schedule_heads_changed(struct weston_compositor *compositor)
4512{
4513 struct wl_event_loop *loop;
4514
4515 if (compositor->heads_changed_source)
4516 return;
4517
4518 loop = wl_display_get_event_loop(compositor->wl_display);
4519 compositor->heads_changed_source = wl_event_loop_add_idle(loop,
4520 weston_compositor_call_heads_changed, compositor);
4521}
4522
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004523/** Register a new head
4524 *
4525 * \param compositor The compositor.
4526 * \param head The head to register, must not be already registered.
4527 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004528 * This signals the core that a new head has become available, leading to
4529 * heads_changed hook being called later.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004530 *
4531 * \memberof weston_compositor
4532 * \internal
4533 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004534WL_EXPORT void
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004535weston_compositor_add_head(struct weston_compositor *compositor,
4536 struct weston_head *head)
4537{
4538 assert(wl_list_empty(&head->compositor_link));
4539 assert(head->name);
4540
4541 wl_list_insert(compositor->head_list.prev, &head->compositor_link);
4542 head->compositor = compositor;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004543 weston_compositor_schedule_heads_changed(compositor);
4544}
4545
4546/** Adds a listener to be called when heads change
4547 *
4548 * \param compositor The compositor.
4549 * \param listener The listener to add.
4550 *
4551 * The listener notify function argument is the \var compositor.
4552 *
4553 * The listener function will be called after heads are added or their
4554 * connection status has changed. Several changes may be accumulated into a
4555 * single call. The user is expected to iterate over the existing heads and
4556 * check their statuses to find out what changed.
4557 *
4558 * \sa weston_compositor_iterate_heads, weston_head_is_connected,
4559 * weston_head_is_enabled
4560 * \memberof weston_compositor
4561 */
4562WL_EXPORT void
4563weston_compositor_add_heads_changed_listener(struct weston_compositor *compositor,
4564 struct wl_listener *listener)
4565{
4566 wl_signal_add(&compositor->heads_changed_signal, listener);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004567}
4568
4569/** Iterate over available heads
4570 *
4571 * \param compositor The compositor.
4572 * \param item The iterator, or NULL for start.
4573 * \return The next available head in the list.
4574 *
4575 * Returns all available heads, regardless of being connected or enabled.
4576 *
4577 * You can iterate over all heads as follows:
4578 * \code
4579 * struct weston_head *head = NULL;
4580 *
4581 * while ((head = weston_compositor_iterate_heads(compositor, head))) {
4582 * ...
4583 * }
4584 * \endcode
4585 *
4586 * If you cause \c iter to be removed from the list, you cannot use it to
4587 * continue iterating. Removing any other item is safe.
4588 *
4589 * \memberof weston_compositor
4590 */
4591WL_EXPORT struct weston_head *
4592weston_compositor_iterate_heads(struct weston_compositor *compositor,
4593 struct weston_head *iter)
4594{
4595 struct wl_list *list = &compositor->head_list;
4596 struct wl_list *node;
4597
4598 assert(compositor);
4599 assert(!iter || iter->compositor == compositor);
4600
4601 if (iter)
4602 node = iter->compositor_link.next;
4603 else
4604 node = list->next;
4605
4606 assert(node);
4607 assert(!iter || node != &iter->compositor_link);
4608
4609 if (node == list)
4610 return NULL;
4611
4612 return container_of(node, struct weston_head, compositor_link);
4613}
4614
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004615/** Iterate over attached heads
4616 *
4617 * \param output The output whose heads to iterate.
4618 * \param item The iterator, or NULL for start.
4619 * \return The next attached head in the list.
4620 *
4621 * Returns all heads currently attached to the output.
4622 *
4623 * You can iterate over all heads as follows:
4624 * \code
4625 * struct weston_head *head = NULL;
4626 *
4627 * while ((head = weston_output_iterate_heads(output, head))) {
4628 * ...
4629 * }
4630 * \endcode
4631 *
4632 * If you cause \c iter to be removed from the list, you cannot use it to
4633 * continue iterating. Removing any other item is safe.
4634 *
4635 * \memberof weston_compositor
4636 */
4637WL_EXPORT struct weston_head *
4638weston_output_iterate_heads(struct weston_output *output,
4639 struct weston_head *iter)
4640{
4641 struct wl_list *list = &output->head_list;
4642 struct wl_list *node;
4643
4644 assert(output);
4645 assert(!iter || iter->output == output);
4646
4647 if (iter)
4648 node = iter->output_link.next;
4649 else
4650 node = list->next;
4651
4652 assert(node);
4653 assert(!iter || node != &iter->output_link);
4654
4655 if (node == list)
4656 return NULL;
4657
4658 return container_of(node, struct weston_head, output_link);
4659}
4660
Pekka Paalanendcac3512017-12-08 14:13:34 +02004661/** Attach a head to an output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004662 *
4663 * \param output The output to attach to.
4664 * \param head The head that is not yet attached.
4665 * \return 0 on success, -1 on failure.
4666 *
4667 * Attaches the given head to the output. All heads of an output are clones
4668 * and share the resolution and timings.
4669 *
4670 * Cloning heads this way uses less resources than creating an output for
4671 * each head, but is not always possible due to environment, driver and hardware
4672 * limitations.
4673 *
4674 * On failure, the head remains unattached. Success of this function does not
4675 * guarantee the output configuration is actually valid. The final checks are
Pekka Paalanendcac3512017-12-08 14:13:34 +02004676 * made on weston_output_enable() unless the output was already enabled.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004677 *
4678 * \memberof weston_output
4679 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004680WL_EXPORT int
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004681weston_output_attach_head(struct weston_output *output,
4682 struct weston_head *head)
4683{
Pekka Paalanendcac3512017-12-08 14:13:34 +02004684 char *head_names;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004685
4686 if (!wl_list_empty(&head->output_link))
4687 return -1;
4688
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004689 if (output->attach_head) {
4690 if (output->attach_head(output, head) < 0)
4691 return -1;
4692 } else if (!wl_list_empty(&output->head_list)) {
4693 /* No support for clones in the legacy path. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004694 return -1;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004695 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004696
4697 head->output = output;
4698 wl_list_insert(output->head_list.prev, &head->output_link);
4699
Pekka Paalanendcac3512017-12-08 14:13:34 +02004700 if (output->enabled) {
4701 weston_head_add_global(head);
4702
4703 head_names = weston_output_create_heads_string(output);
4704 weston_log("Output '%s' updated to have head(s) %s\n",
4705 output->name, head_names);
4706 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004707
4708 weston_output_emit_heads_changed(output);
Pekka Paalanendcac3512017-12-08 14:13:34 +02004709 }
4710
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004711 return 0;
4712}
4713
4714/** Detach a head from its output
4715 *
4716 * \param head The head to detach.
4717 *
4718 * It is safe to detach a non-attached head.
4719 *
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004720 * If the head is attached to an enabled output and the output will be left
4721 * with no heads, the output will be disabled.
4722 *
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004723 * \memberof weston_head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004724 * \sa weston_output_disable
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004725 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004726WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004727weston_head_detach(struct weston_head *head)
4728{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004729 struct weston_output *output = head->output;
Pekka Paalanena0106992017-12-08 16:11:17 +02004730 char *head_names;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004731
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004732 wl_list_remove(&head->output_link);
4733 wl_list_init(&head->output_link);
4734 head->output = NULL;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004735
4736 if (!output)
4737 return;
4738
4739 if (output->detach_head)
4740 output->detach_head(output, head);
4741
4742 if (output->enabled) {
4743 weston_head_remove_global(head);
4744
Pekka Paalanena0106992017-12-08 16:11:17 +02004745 if (wl_list_empty(&output->head_list)) {
4746 weston_log("Output '%s' no heads left, disabling.\n",
4747 output->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004748 weston_output_disable(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02004749 } else {
4750 head_names = weston_output_create_heads_string(output);
4751 weston_log("Output '%s' updated to have head(s) %s\n",
4752 output->name, head_names);
4753 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004754
4755 weston_output_emit_heads_changed(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02004756 }
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004757 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004758}
4759
4760/** Destroy a head
4761 *
4762 * \param head The head to be released.
4763 *
4764 * Destroys the head. The caller is responsible for freeing the memory pointed
4765 * to by \c head.
4766 *
4767 * \memberof weston_head
4768 * \internal
4769 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004770WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004771weston_head_release(struct weston_head *head)
4772{
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03004773 wl_signal_emit(&head->destroy_signal, head);
4774
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004775 weston_head_detach(head);
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03004776
4777 free(head->make);
4778 free(head->model);
4779 free(head->serial_number);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004780 free(head->name);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004781
4782 wl_list_remove(&head->compositor_link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004783}
4784
Pekka Paalanene19970f2017-08-28 14:11:02 +03004785static void
4786weston_head_set_device_changed(struct weston_head *head)
4787{
4788 head->device_changed = true;
4789
4790 if (head->compositor)
4791 weston_compositor_schedule_heads_changed(head->compositor);
4792}
4793
4794/** String equal comparison with NULLs being equal */
4795static bool
4796str_null_eq(const char *a, const char *b)
4797{
4798 if (!a && !b)
4799 return true;
4800
4801 if (!!a != !!b)
4802 return false;
4803
4804 return strcmp(a, b) == 0;
4805}
4806
Pekka Paalanen01f60212017-03-24 15:39:24 +02004807/** Store monitor make, model and serial number
4808 *
4809 * \param head The head to modify.
4810 * \param make The monitor make. If EDID is available, the PNP ID. Otherwise
4811 * any string, or NULL for none.
4812 * \param model The monitor model or name, or a made-up string, or NULL for
4813 * none.
4814 * \param serialno The monitor serial number, a made-up string, or NULL for
4815 * none.
4816 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03004817 * This may set the device_changed flag.
4818 *
Pekka Paalanen01f60212017-03-24 15:39:24 +02004819 * \memberof weston_head
4820 * \internal
4821 */
4822WL_EXPORT void
4823weston_head_set_monitor_strings(struct weston_head *head,
4824 const char *make,
4825 const char *model,
4826 const char *serialno)
4827{
Pekka Paalanene19970f2017-08-28 14:11:02 +03004828 if (str_null_eq(head->make, make) &&
4829 str_null_eq(head->model, model) &&
4830 str_null_eq(head->serial_number, serialno))
4831 return;
4832
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03004833 free(head->make);
4834 free(head->model);
4835 free(head->serial_number);
4836
4837 head->make = make ? strdup(make) : NULL;
4838 head->model = model ? strdup(model) : NULL;
4839 head->serial_number = serialno ? strdup(serialno) : NULL;
Pekka Paalanene19970f2017-08-28 14:11:02 +03004840
4841 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02004842}
4843
4844/** Store physical image size
4845 *
4846 * \param head The head to modify.
4847 * \param mm_width Image area width in millimeters.
4848 * \param mm_height Image area height in millimeters.
4849 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03004850 * This may set the device_changed flag.
4851 *
Pekka Paalanen01f60212017-03-24 15:39:24 +02004852 * \memberof weston_head
4853 * \internal
4854 */
4855WL_EXPORT void
4856weston_head_set_physical_size(struct weston_head *head,
4857 int32_t mm_width, int32_t mm_height)
4858{
Pekka Paalanene19970f2017-08-28 14:11:02 +03004859 if (head->mm_width == mm_width &&
4860 head->mm_height == mm_height)
4861 return;
4862
Pekka Paalanen01f60212017-03-24 15:39:24 +02004863 head->mm_width = mm_width;
4864 head->mm_height = mm_height;
Pekka Paalanene19970f2017-08-28 14:11:02 +03004865
4866 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02004867}
4868
4869/** Store monitor sub-pixel layout
4870 *
4871 * \param head The head to modify.
4872 * \param sp Sub-pixel layout. The possible values are:
4873 * - WL_OUTPUT_SUBPIXEL_UNKNOWN,
4874 * - WL_OUTPUT_SUBPIXEL_NONE,
4875 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB,
4876 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR,
4877 * - WL_OUTPUT_SUBPIXEL_VERTICAL_RGB,
4878 * - WL_OUTPUT_SUBPIXEL_VERTICAL_BGR
4879 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03004880 * This may set the device_changed flag.
4881 *
Pekka Paalanen01f60212017-03-24 15:39:24 +02004882 * \memberof weston_head
4883 * \internal
4884 */
4885WL_EXPORT void
4886weston_head_set_subpixel(struct weston_head *head,
4887 enum wl_output_subpixel sp)
4888{
Pekka Paalanene19970f2017-08-28 14:11:02 +03004889 if (head->subpixel == sp)
4890 return;
4891
Pekka Paalanen01f60212017-03-24 15:39:24 +02004892 head->subpixel = sp;
Pekka Paalanene19970f2017-08-28 14:11:02 +03004893
4894 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02004895}
4896
4897/** Mark the monitor as internal
4898 *
4899 * This is used for embedded screens, like laptop panels.
4900 *
4901 * \param head The head to mark as internal.
4902 *
4903 * By default a head is external. The type is often inferred from the physical
4904 * connector type.
4905 *
4906 * \memberof weston_head
4907 * \internal
4908 */
4909WL_EXPORT void
4910weston_head_set_internal(struct weston_head *head)
4911{
4912 head->connection_internal = true;
4913}
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004914
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03004915/** Store connector status
4916 *
4917 * \param head The head to modify.
4918 * \param connected Whether the head is connected.
4919 *
4920 * Connectors are created as disconnected. This function can be used to
4921 * set the connector status.
4922 *
4923 * The status should be set to true when a physical connector is connected to
4924 * a video sink device like a monitor and to false when the connector is
4925 * disconnected. For nested backends, the connection status should reflect the
4926 * connection to the parent display server.
4927 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004928 * When the connection status changes, it schedules a call to the heads_changed
Pekka Paalanene19970f2017-08-28 14:11:02 +03004929 * hook and sets the device_changed flag.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004930 *
4931 * \sa weston_compositor_set_heads_changed_cb
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03004932 * \memberof weston_head
4933 * \internal
4934 */
4935WL_EXPORT void
4936weston_head_set_connection_status(struct weston_head *head, bool connected)
4937{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004938 if (head->connected == connected)
4939 return;
4940
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03004941 head->connected = connected;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004942
Pekka Paalanene19970f2017-08-28 14:11:02 +03004943 weston_head_set_device_changed(head);
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03004944}
4945
4946/** Is the head currently connected?
4947 *
4948 * \param head The head to query.
4949 * \return Connection status.
4950 *
4951 * Returns true if the head is physically connected to a monitor, or in
4952 * case of a nested backend returns true when there is a connection to the
4953 * parent display server.
4954 *
4955 * This is independent from the head being enabled.
4956 *
4957 * \sa weston_head_is_enabled
4958 * \memberof weston_head
4959 */
4960WL_EXPORT bool
4961weston_head_is_connected(struct weston_head *head)
4962{
4963 return head->connected;
4964}
4965
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02004966/** Is the head currently enabled?
4967 *
4968 * \param head The head to query.
4969 * \return Video status.
4970 *
4971 * Returns true if the head is currently transmitting a video stream.
4972 *
4973 * This is independent of the head being connected.
4974 *
4975 * \sa weston_head_is_connected
4976 * \memberof weston_head
4977 */
4978WL_EXPORT bool
4979weston_head_is_enabled(struct weston_head *head)
4980{
4981 if (!head->output)
4982 return false;
4983
4984 return head->output->enabled;
4985}
4986
Pekka Paalanene19970f2017-08-28 14:11:02 +03004987/** Has the device information changed?
4988 *
4989 * \param head The head to query.
4990 * \return True if the device information has changed since last reset.
4991 *
4992 * The information about the connected display device, e.g. a monitor, may
4993 * change without being disconnected in between. Changing information
4994 * causes a call to the heads_changed hook.
4995 *
4996 * The information includes make, model, serial number, physical size,
4997 * and sub-pixel type. The connection status is also included.
4998 *
4999 * \sa weston_head_reset_device_changed, weston_compositor_set_heads_changed_cb
5000 * \memberof weston_head
5001 */
5002WL_EXPORT bool
5003weston_head_is_device_changed(struct weston_head *head)
5004{
5005 return head->device_changed;
5006}
5007
5008/** Acknowledge device information change
5009 *
5010 * \param head The head to acknowledge.
5011 *
5012 * Clears the device changed flag on this head. When a compositor has processed
5013 * device information, it should call this to be able to notice further
5014 * changes.
5015 *
5016 * \sa weston_head_is_device_changed
5017 * \memberof weston_head
5018 */
5019WL_EXPORT void
5020weston_head_reset_device_changed(struct weston_head *head)
5021{
5022 head->device_changed = false;
5023}
5024
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005025/** Get the name of a head
5026 *
5027 * \param head The head to query.
5028 * \return The head's name, not NULL.
5029 *
5030 * The name depends on the backend. The DRM backend uses connector names,
5031 * other backends may use hardcoded names or user-given names.
5032 */
5033WL_EXPORT const char *
5034weston_head_get_name(struct weston_head *head)
5035{
5036 return head->name;
5037}
5038
5039/** Get the output the head is attached to
5040 *
5041 * \param head The head to query.
5042 * \return The output the head is attached to, or NULL if detached.
5043 */
5044WL_EXPORT struct weston_output *
5045weston_head_get_output(struct weston_head *head)
5046{
5047 return head->output;
5048}
5049
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005050/** Add destroy callback for a head
5051 *
5052 * \param head The head to watch for.
5053 * \param listener The listener to add. The \c notify member must be set.
5054 *
5055 * Heads may get destroyed for various reasons by the backends. If a head is
5056 * attached to an output, the compositor should listen for head destruction
5057 * and reconfigure or destroy the output if necessary.
5058 *
5059 * The destroy callbacks will be called on weston_head destruction before any
5060 * automatic detaching from an associated weston_output and before any
5061 * weston_head information is lost.
5062 *
5063 * The \c data argument to the notify callback is the weston_head being
5064 * destroyed.
5065 */
5066WL_EXPORT void
5067weston_head_add_destroy_listener(struct weston_head *head,
5068 struct wl_listener *listener)
5069{
5070 wl_signal_add(&head->destroy_signal, listener);
5071}
5072
5073/** Look up destroy listener for a head
5074 *
5075 * \param head The head to query.
5076 * \param notify The notify function used used for the added destroy listener.
5077 * \return The listener, or NULL if not found.
5078 *
5079 * This looks up the previously added destroy listener struct based on the
5080 * notify function it has. The listener can be used to access user data
5081 * through \c container_of().
5082 *
5083 * \sa wl_signal_get()
5084 */
5085WL_EXPORT struct wl_listener *
5086weston_head_get_destroy_listener(struct weston_head *head,
5087 wl_notify_func_t notify)
5088{
5089 return wl_signal_get(&head->destroy_signal, notify);
5090}
5091
David Fort0de859e2016-05-27 23:22:57 +02005092/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005093static void
David Fort0de859e2016-05-27 23:22:57 +02005094weston_compositor_reflow_outputs(struct weston_compositor *compositor,
5095 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005096{
5097 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02005098 bool start_resizing = false;
5099
5100 if (!delta_width)
5101 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005102
5103 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02005104 if (output == resized_output) {
5105 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005106 continue;
5107 }
5108
David Fort0de859e2016-05-27 23:22:57 +02005109 if (start_resizing) {
5110 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005111 output->dirty = 1;
5112 }
5113 }
5114}
5115
Pekka Paalanend72bad22017-03-29 17:01:41 +03005116static void
Scott Moreauccbf29d2012-02-22 14:21:41 -07005117weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005118{
Scott Moreau850ca422012-05-21 15:21:25 -06005119 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05005120
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005121 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005122 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06005123
Scott Moreauccbf29d2012-02-22 14:21:41 -07005124 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06005125 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005126 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005127 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005128 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005129 weston_matrix_scale(&output->matrix, magnification,
5130 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005131 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005132
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005133 switch (output->transform) {
5134 case WL_OUTPUT_TRANSFORM_FLIPPED:
5135 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5136 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5137 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5138 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5139 weston_matrix_scale(&output->matrix, -1, 1, 1);
5140 break;
5141 }
5142
5143 switch (output->transform) {
5144 default:
5145 case WL_OUTPUT_TRANSFORM_NORMAL:
5146 case WL_OUTPUT_TRANSFORM_FLIPPED:
5147 break;
5148 case WL_OUTPUT_TRANSFORM_90:
5149 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5150 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
5151 weston_matrix_rotate_xy(&output->matrix, 0, 1);
5152 break;
5153 case WL_OUTPUT_TRANSFORM_180:
5154 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5155 weston_matrix_translate(&output->matrix,
5156 -output->width, -output->height, 0);
5157 weston_matrix_rotate_xy(&output->matrix, -1, 0);
5158 break;
5159 case WL_OUTPUT_TRANSFORM_270:
5160 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5161 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5162 weston_matrix_rotate_xy(&output->matrix, 0, -1);
5163 break;
5164 }
5165
5166 if (output->current_scale != 1)
5167 weston_matrix_scale(&output->matrix,
5168 output->current_scale,
5169 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01005170
Scott Moreauccbf29d2012-02-22 14:21:41 -07005171 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05005172
5173 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005174}
5175
Scott Moreau1bad5db2012-08-18 01:04:05 -06005176static void
Alexander Larsson0b135062013-05-28 16:23:36 +02005177weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06005178{
5179 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005180 output->native_scale = scale;
5181 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06005182
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005183 convert_size_by_transform_scale(&output->width, &output->height,
5184 output->current_mode->width,
5185 output->current_mode->height,
5186 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02005187}
5188
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005189static void
5190weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07005191{
5192 output->x = x;
5193 output->y = y;
5194
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005195 pixman_region32_fini(&output->previous_damage);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02005196 pixman_region32_init(&output->previous_damage);
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005197
5198 pixman_region32_fini(&output->region);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005199 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06005200 output->width,
5201 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005202}
5203
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005204WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005205weston_output_move(struct weston_output *output, int x, int y)
5206{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005207 struct weston_head *head;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005208 struct wl_resource *resource;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005209 int ver;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005210
5211 output->move_x = x - output->x;
5212 output->move_y = y - output->y;
5213
5214 if (output->move_x == 0 && output->move_y == 0)
5215 return;
5216
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005217 weston_output_init_geometry(output, x, y);
5218
5219 output->dirty = 1;
5220
5221 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005222 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005223
5224 /* Notify clients of the change for output position. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005225 wl_list_for_each(head, &output->head_list, output_link) {
5226 wl_resource_for_each(resource, &head->resource_list) {
5227 wl_output_send_geometry(resource,
5228 output->x,
5229 output->y,
5230 head->mm_width,
5231 head->mm_height,
5232 head->subpixel,
5233 head->make,
5234 head->model,
5235 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08005236
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005237 ver = wl_resource_get_version(resource);
5238 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
5239 wl_output_send_done(resource);
5240 }
Quanxian Wangb2c86362014-03-14 09:16:25 +08005241 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005242}
5243
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005244/** Signal that a pending output is taken into use.
5245 *
5246 * Removes the output from the pending list and adds it to the compositor's
5247 * list of enabled outputs. The output created signal is emitted.
Giulio Camuffob1147152015-05-06 21:41:57 +03005248 *
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005249 * The output gets an internal ID assigned, and the wl_output global is
5250 * created.
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005251 *
Giulio Camuffob1147152015-05-06 21:41:57 +03005252 * \param compositor The compositor instance.
5253 * \param output The output to be added.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005254 *
5255 * \internal
Giulio Camuffob1147152015-05-06 21:41:57 +03005256 */
Pekka Paalanenf9681b52017-03-29 16:58:48 +03005257static void
Giulio Camuffob1147152015-05-06 21:41:57 +03005258weston_compositor_add_output(struct weston_compositor *compositor,
5259 struct weston_output *output)
5260{
Armin Krezoviće5403842016-08-05 15:28:29 +02005261 struct weston_view *view, *next;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005262 struct weston_head *head;
Armin Krezoviće5403842016-08-05 15:28:29 +02005263
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005264 assert(!output->enabled);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005265
5266 /* Verify we haven't reached the limit of 32 available output IDs */
5267 assert(ffs(~compositor->output_id_pool) > 0);
5268
5269 /* Invert the output id pool and look for the lowest numbered
5270 * switch (the least significant bit). Take that bit's position
5271 * as our ID, and mark it used in the compositor's output_id_pool.
5272 */
5273 output->id = ffs(~compositor->output_id_pool) - 1;
5274 compositor->output_id_pool |= 1u << output->id;
5275
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005276 wl_list_remove(&output->link);
Giulio Camuffob1147152015-05-06 21:41:57 +03005277 wl_list_insert(compositor->output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005278 output->enabled = true;
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005279
Pekka Paalanendcac3512017-12-08 14:13:34 +02005280 wl_list_for_each(head, &output->head_list, output_link)
5281 weston_head_add_global(head);
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005282
Giulio Camuffob1147152015-05-06 21:41:57 +03005283 wl_signal_emit(&compositor->output_created_signal, output);
Armin Krezoviće5403842016-08-05 15:28:29 +02005284
5285 wl_list_for_each_safe(view, next, &compositor->view_list, link)
5286 weston_view_geometry_dirty(view);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005287}
5288
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005289/** Transform device coordinates into global coordinates
5290 *
5291 * \param device_x[in] X coordinate in device units.
5292 * \param device_y[in] Y coordinate in device units.
5293 * \param x[out] X coordinate in the global space.
5294 * \param y[out] Y coordinate in the global space.
5295 *
5296 * Transforms coordinates from the device coordinate space
5297 * (physical pixel units) to the global coordinate space (logical pixel units).
5298 * This takes into account output transform and scale.
5299 *
5300 * \memberof weston_output
5301 * \internal
5302 */
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005303WL_EXPORT void
5304weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005305 double device_x, double device_y,
5306 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005307{
Derek Foreman0f679412014-10-02 13:41:17 -05005308 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005309 device_x,
5310 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05005311 0.0,
5312 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005313
Derek Foreman67a18b92015-03-24 11:36:14 -05005314 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005315
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005316 *x = p.f[0] / p.f[3];
5317 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005318}
5319
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005320/** Removes output from compositor's list of enabled outputs
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005321 *
5322 * \param output The weston_output object that is being removed.
5323 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005324 * The following happens:
5325 *
5326 * - The output assignments of all views in the current scenegraph are
5327 * recomputed.
5328 *
5329 * - Presentation feedback is discarded.
5330 *
5331 * - Compositor is notified that outputs were changed and
5332 * applies the necessary changes to re-layout outputs.
5333 *
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005334 * - The output is put back in the pending outputs list.
5335 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005336 * - Signal is emitted to notify all users of the weston_output
5337 * object that the output is being destroyed.
5338 *
5339 * - wl_output protocol objects referencing this weston_output
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005340 * are made inert, and the wl_output global is removed.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005341 *
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005342 * - The output's internal ID is released.
5343 *
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005344 * \memberof weston_output
5345 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005346 */
5347static void
5348weston_compositor_remove_output(struct weston_output *output)
5349{
Pekka Paalanenbccda712017-03-29 16:16:04 +03005350 struct weston_compositor *compositor = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005351 struct weston_view *view;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005352 struct weston_head *head;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005353
5354 assert(output->destroying);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005355 assert(output->enabled);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005356
Pekka Paalanenbccda712017-03-29 16:16:04 +03005357 wl_list_for_each(view, &compositor->view_list, link) {
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005358 if (view->output_mask & (1u << output->id))
5359 weston_view_assign_output(view);
5360 }
5361
5362 weston_presentation_feedback_discard_list(&output->feedback_list);
5363
Pekka Paalanenbccda712017-03-29 16:16:04 +03005364 weston_compositor_reflow_outputs(compositor, output, output->width);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005365
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005366 wl_list_remove(&output->link);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005367 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005368 output->enabled = false;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005369
Pekka Paalanenbccda712017-03-29 16:16:04 +03005370 wl_signal_emit(&compositor->output_destroyed_signal, output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005371 wl_signal_emit(&output->destroy_signal, output);
5372
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02005373 wl_list_for_each(head, &output->head_list, output_link)
5374 weston_head_remove_global(head);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005375
5376 compositor->output_id_pool &= ~(1u << output->id);
5377 output->id = 0xffffffff; /* invalid */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005378}
5379
5380/** Sets the output scale for a given output.
5381 *
5382 * \param output The weston_output object that the scale is set for.
5383 * \param scale Scale factor for the given output.
5384 *
5385 * It only supports setting scale for an output that
5386 * is not enabled and it can only be ran once.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005387 *
5388 * \memberof weston_output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005389 */
5390WL_EXPORT void
5391weston_output_set_scale(struct weston_output *output,
5392 int32_t scale)
5393{
5394 /* We can only set scale on a disabled output */
5395 assert(!output->enabled);
5396
5397 /* We only want to set scale once */
5398 assert(!output->scale);
5399
5400 output->scale = scale;
5401}
5402
5403/** Sets the output transform for a given output.
5404 *
5405 * \param output The weston_output object that the transform is set for.
5406 * \param transform Transform value for the given output.
5407 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005408 * Refer to wl_output::transform section located at
5409 * https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_output
5410 * for list of values that can be passed to this function.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005411 *
5412 * \memberof weston_output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005413 */
5414WL_EXPORT void
5415weston_output_set_transform(struct weston_output *output,
5416 uint32_t transform)
5417{
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005418 struct weston_pointer_motion_event ev;
5419 struct wl_resource *resource;
5420 struct weston_seat *seat;
5421 pixman_region32_t old_region;
5422 int mid_x, mid_y;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005423 struct weston_head *head;
5424 int ver;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005425
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005426 if (!output->enabled && output->transform == UINT32_MAX) {
5427 output->transform = transform;
5428 return;
5429 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005430
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005431 weston_output_transform_scale_init(output, transform, output->scale);
5432
5433 pixman_region32_init(&old_region);
5434 pixman_region32_copy(&old_region, &output->region);
5435
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005436 weston_output_init_geometry(output, output->x, output->y);
5437
5438 output->dirty = 1;
5439
5440 /* Notify clients of the change for output transform. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005441 wl_list_for_each(head, &output->head_list, output_link) {
5442 wl_resource_for_each(resource, &head->resource_list) {
5443 wl_output_send_geometry(resource,
5444 output->x,
5445 output->y,
5446 head->mm_width,
5447 head->mm_height,
5448 head->subpixel,
5449 head->make,
5450 head->model,
5451 output->transform);
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005452
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005453 ver = wl_resource_get_version(resource);
5454 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
5455 wl_output_send_done(resource);
5456 }
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005457 }
5458
5459 /* we must ensure that pointers are inside output, otherwise they disappear */
5460 mid_x = output->x + output->width / 2;
5461 mid_y = output->y + output->height / 2;
5462
5463 ev.mask = WESTON_POINTER_MOTION_ABS;
5464 ev.x = wl_fixed_to_double(wl_fixed_from_int(mid_x));
5465 ev.y = wl_fixed_to_double(wl_fixed_from_int(mid_y));
5466
5467 wl_list_for_each(seat, &output->compositor->seat_list, link) {
5468 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5469
5470 if (pointer && pixman_region32_contains_point(&old_region,
5471 wl_fixed_to_int(pointer->x),
5472 wl_fixed_to_int(pointer->y),
5473 NULL))
5474 weston_pointer_move(pointer, &ev);
5475 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005476}
5477
5478/** Initializes a weston_output object with enough data so
5479 ** an output can be configured.
5480 *
5481 * \param output The weston_output object to initialize
5482 * \param compositor The compositor instance.
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005483 * \param name Name for the output (the string is copied).
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005484 *
5485 * Sets initial values for fields that are expected to be
5486 * configured either by compositors or backends.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005487 *
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005488 * The name is used in logs, and can be used by compositors as a configuration
5489 * identifier.
5490 *
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005491 * \memberof weston_output
5492 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005493 */
5494WL_EXPORT void
Armin Krezović40087402016-09-30 14:11:12 +02005495weston_output_init(struct weston_output *output,
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005496 struct weston_compositor *compositor,
5497 const char *name)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005498{
5499 output->compositor = compositor;
5500 output->destroying = 0;
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005501 output->name = strdup(name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005502 wl_list_init(&output->link);
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02005503 wl_signal_init(&output->user_destroy_signal);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005504 output->enabled = false;
5505
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005506 wl_list_init(&output->head_list);
5507
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005508 /* Add some (in)sane defaults which can be used
5509 * for checking if an output was properly configured
5510 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005511 output->scale = 0;
5512 /* Can't use -1 on uint32_t and 0 is valid enum value */
5513 output->transform = UINT32_MAX;
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005514
5515 pixman_region32_init(&output->previous_damage);
5516 pixman_region32_init(&output->region);
Pekka Paalanen42704142017-09-06 16:47:52 +03005517 wl_list_init(&output->mode_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005518}
5519
5520/** Adds weston_output object to pending output list.
5521 *
5522 * \param output The weston_output object to add
5523 * \param compositor The compositor instance.
5524 *
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03005525 * The opposite of this operation is built into weston_output_release().
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005526 *
5527 * \memberof weston_output
5528 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005529 */
5530WL_EXPORT void
5531weston_compositor_add_pending_output(struct weston_output *output,
5532 struct weston_compositor *compositor)
5533{
Pekka Paalanene952a012017-03-29 17:14:00 +03005534 assert(output->disable);
5535 assert(output->enable);
5536
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005537 wl_list_remove(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005538 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005539}
5540
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02005541/** Create a string with the attached heads' names.
5542 *
5543 * The string must be free()'d.
5544 */
5545static char *
5546weston_output_create_heads_string(struct weston_output *output)
5547{
5548 FILE *fp;
5549 char *str = NULL;
5550 size_t size = 0;
5551 struct weston_head *head;
5552 const char *sep = "";
5553
5554 fp = open_memstream(&str, &size);
5555 if (!fp)
5556 return NULL;
5557
5558 wl_list_for_each(head, &output->head_list, output_link) {
5559 fprintf(fp, "%s%s", sep, head->name);
5560 sep = ", ";
5561 }
5562 fclose(fp);
5563
5564 return str;
5565}
5566
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005567/** Constructs a weston_output object that can be used by the compositor.
5568 *
Pekka Paalanencc201e42017-03-30 15:11:25 +03005569 * \param output The weston_output object that needs to be enabled. Must not
Pekka Paalanenddce54d2017-08-23 16:00:21 +03005570 * be enabled already. Must have at least one head attached.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005571 *
5572 * Output coordinates are calculated and each new output is by default
5573 * assigned to the right of previous one.
5574 *
5575 * Sets up the transformation, zoom, and geometry of the output using
5576 * the properties that need to be configured by the compositor.
5577 *
5578 * Establishes a repaint timer for the output with the relevant display
5579 * object's event loop. See output_repaint_timer_handler().
5580 *
5581 * The output is assigned an ID. Weston can support up to 32 distinct
5582 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
5583 * is referred to and used to find the first available ID number, and
5584 * then this ID is marked as used in output_id_pool.
5585 *
5586 * The output is also assigned a Wayland global with the wl_output
5587 * external interface.
5588 *
5589 * Backend specific function is called to set up the output output.
5590 *
5591 * Output is added to the compositor's output list
5592 *
5593 * If the backend specific function fails, the weston_output object
5594 * is returned to a state it was before calling this function and
5595 * is added to the compositor's pending_output_list in case it needs
5596 * to be reconfigured or just so it can be destroyed at shutdown.
5597 *
5598 * 0 is returned on success, -1 on failure.
5599 */
5600WL_EXPORT int
5601weston_output_enable(struct weston_output *output)
5602{
Armin Krezović782f5df2016-09-30 14:11:11 +02005603 struct weston_compositor *c = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005604 struct weston_output *iterator;
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03005605 struct weston_head *head;
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02005606 char *head_names;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005607 int x = 0, y = 0;
5608
Pekka Paalanencc201e42017-03-30 15:11:25 +03005609 if (output->enabled) {
5610 weston_log("Error: attempt to enable an enabled output '%s'\n",
5611 output->name);
5612 return -1;
5613 }
5614
Pekka Paalanenddce54d2017-08-23 16:00:21 +03005615 if (wl_list_empty(&output->head_list)) {
5616 weston_log("Error: cannot enable output '%s' without heads.\n",
5617 output->name);
5618 return -1;
5619 }
5620
Pekka Paalanen586e1ac2017-09-14 16:17:59 +03005621 if (wl_list_empty(&output->mode_list) || !output->current_mode) {
5622 weston_log("Error: no video mode for output '%s'.\n",
5623 output->name);
5624 return -1;
5625 }
5626
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03005627 wl_list_for_each(head, &output->head_list, output_link) {
5628 assert(head->make);
5629 assert(head->model);
5630 }
5631
Armin Krezović782f5df2016-09-30 14:11:11 +02005632 iterator = container_of(c->output_list.prev,
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005633 struct weston_output, link);
5634
Armin Krezović782f5df2016-09-30 14:11:11 +02005635 if (!wl_list_empty(&c->output_list))
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005636 x = iterator->x + iterator->width;
5637
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005638 /* Make sure the scale is set up */
5639 assert(output->scale);
5640
5641 /* Make sure we have a transform set */
5642 assert(output->transform != UINT32_MAX);
5643
Armin Krezović782f5df2016-09-30 14:11:11 +02005644 output->x = x;
5645 output->y = y;
5646 output->dirty = 1;
5647 output->original_scale = output->scale;
5648
5649 weston_output_transform_scale_init(output, output->transform, output->scale);
5650 weston_output_init_zoom(output);
5651
5652 weston_output_init_geometry(output, x, y);
5653 weston_output_damage(output);
5654
5655 wl_signal_init(&output->frame_signal);
5656 wl_signal_init(&output->destroy_signal);
5657 wl_list_init(&output->animation_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02005658 wl_list_init(&output->feedback_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02005659
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005660 /* Enable the output (set up the crtc or create a
5661 * window representing the output, set up the
5662 * renderer, etc)
5663 */
5664 if (output->enable(output) < 0) {
5665 weston_log("Enabling output \"%s\" failed.\n", output->name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005666 return -1;
5667 }
5668
5669 weston_compositor_add_output(output->compositor, output);
5670
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02005671 head_names = weston_output_create_heads_string(output);
5672 weston_log("Output '%s' enabled with head(s) %s\n",
5673 output->name, head_names);
5674 free(head_names);
5675
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005676 return 0;
5677}
5678
5679/** Converts a weston_output object to a pending output state, so it
5680 ** can be configured again or destroyed.
5681 *
5682 * \param output The weston_output object that needs to be disabled.
5683 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005684 * Calls a backend specific function to disable an output, in case
5685 * such function exists.
5686 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03005687 * The backend specific disable function may choose to postpone the disabling
5688 * by returning a negative value, in which case this function returns early.
5689 * In that case the backend will guarantee the output will be disabled soon
5690 * by the backend calling this function again. One must not attempt to re-enable
5691 * the output until that happens.
5692 *
5693 * Otherwise, if the output is being used by the compositor, it is removed
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005694 * from weston's output_list (see weston_compositor_remove_output())
5695 * and is returned to a state it was before weston_output_enable()
5696 * was ran (see weston_output_enable_undo()).
5697 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03005698 * See weston_output_init() for more information on the
5699 * state output is returned to.
Pekka Paalanencc201e42017-03-30 15:11:25 +03005700 *
5701 * If the output has never been enabled yet, this function can still be
5702 * called to ensure that the output is actually turned off rather than left
5703 * in the state it was discovered in.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005704 */
5705WL_EXPORT void
5706weston_output_disable(struct weston_output *output)
5707{
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005708 /* Should we rename this? */
5709 output->destroying = 1;
5710
Pekka Paalanenc65df642017-03-29 15:45:46 +03005711 /* Disable is called unconditionally also for not-enabled outputs,
5712 * because at compositor start-up, if there is an output that is
5713 * already on but the compositor wants to turn it off, we have to
5714 * forward the turn-off to the backend so it knows to do it.
5715 * The backend cannot initially turn off everything, because it
5716 * would cause unnecessary mode-sets for all outputs the compositor
5717 * wants to be on.
5718 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005719 if (output->disable(output) < 0)
5720 return;
5721
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005722 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005723 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005724
5725 output->destroying = 0;
5726}
5727
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03005728/** Forces a synchronous call to heads_changed hook
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005729 *
5730 * \param compositor The compositor instance
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03005731 *
5732 * If there are new or changed heads, calls the heads_changed hook and
5733 * returns after the hook returns.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005734 */
5735WL_EXPORT void
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03005736weston_compositor_flush_heads_changed(struct weston_compositor *compositor)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005737{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005738 if (compositor->heads_changed_source) {
5739 wl_event_source_remove(compositor->heads_changed_source);
5740 weston_compositor_call_heads_changed(compositor);
5741 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005742}
5743
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02005744/** Add destroy callback for an output
5745 *
5746 * \param output The output to watch.
5747 * \param listener The listener to add. The \c notify member must be set.
5748 *
5749 * The listener callback will be called when user destroys an output. This
5750 * may be delayed by a backend in some cases. The main purpose of the
5751 * listener is to allow hooking up custom data to the output. The custom data
5752 * can be fetched via weston_output_get_destroy_listener() followed by
5753 * container_of().
5754 *
5755 * The \c data argument to the notify callback is the weston_output being
5756 * destroyed.
5757 *
5758 * @note This is for the final destruction of an output, not when it gets
5759 * disabled. If you want to keep track of enabled outputs, this is not it.
5760 */
5761WL_EXPORT void
5762weston_output_add_destroy_listener(struct weston_output *output,
5763 struct wl_listener *listener)
5764{
5765 wl_signal_add(&output->user_destroy_signal, listener);
5766}
5767
5768/** Look up destroy listener for an output
5769 *
5770 * \param output The output to query.
5771 * \param notify The notify function used used for the added destroy listener.
5772 * \return The listener, or NULL if not found.
5773 *
5774 * This looks up the previously added destroy listener struct based on the
5775 * notify function it has. The listener can be used to access user data
5776 * through \c container_of().
5777 *
5778 * \sa wl_signal_get() weston_output_add_destroy_listener()
5779 */
5780WL_EXPORT struct wl_listener *
5781weston_output_get_destroy_listener(struct weston_output *output,
5782 wl_notify_func_t notify)
5783{
5784 return wl_signal_get(&output->user_destroy_signal, notify);
5785}
5786
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005787/** Uninitialize an output
5788 *
5789 * Removes the output from the list of enabled outputs if necessary, but
5790 * does not call the backend's output disable function. The output will no
5791 * longer be in the list of pending outputs either.
5792 *
5793 * All fields of weston_output become uninitialized, i.e. should not be used
5794 * anymore. The caller can free the memory after this.
5795 *
5796 * \memberof weston_output
5797 * \internal
5798 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005799WL_EXPORT void
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03005800weston_output_release(struct weston_output *output)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005801{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005802 struct weston_head *head, *tmp;
5803
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005804 output->destroying = 1;
5805
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02005806 wl_signal_emit(&output->user_destroy_signal, output);
5807
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03005808 if (output->idle_repaint_source)
5809 wl_event_source_remove(output->idle_repaint_source);
5810
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005811 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005812 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005813
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005814 pixman_region32_fini(&output->region);
5815 pixman_region32_fini(&output->previous_damage);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005816 wl_list_remove(&output->link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005817
5818 wl_list_for_each_safe(head, tmp, &output->head_list, output_link)
5819 weston_head_detach(head);
5820
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005821 free(output->name);
5822}
5823
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02005824/** Find an output by its given name
5825 *
5826 * \param compositor The compositor to search in.
5827 * \param name The output name to search for.
5828 * \return An existing output with the given name, or NULL if not found.
5829 *
5830 * \memberof weston_compositor
5831 */
5832WL_EXPORT struct weston_output *
5833weston_compositor_find_output_by_name(struct weston_compositor *compositor,
5834 const char *name)
5835{
5836 struct weston_output *output;
5837
5838 wl_list_for_each(output, &compositor->output_list, link)
5839 if (strcmp(output->name, name) == 0)
5840 return output;
5841
5842 wl_list_for_each(output, &compositor->pending_output_list, link)
5843 if (strcmp(output->name, name) == 0)
5844 return output;
5845
5846 return NULL;
5847}
5848
5849/** Create a named output
5850 *
5851 * \param compositor The compositor.
5852 * \param name The name for the output.
5853 * \return A new \c weston_output, or NULL on failure.
5854 *
5855 * This creates a new weston_output that starts with no heads attached.
5856 *
5857 * An output must be configured and it must have at least one head before
5858 * it can be enabled.
5859 *
5860 * \memberof weston_compositor
5861 */
5862WL_EXPORT struct weston_output *
5863weston_compositor_create_output(struct weston_compositor *compositor,
5864 const char *name)
5865{
5866 assert(compositor->backend->create_output);
5867
5868 if (weston_compositor_find_output_by_name(compositor, name)) {
5869 weston_log("Warning: attempted to create an output with a "
5870 "duplicate name '%s'.\n", name);
5871 return NULL;
5872 }
5873
5874 return compositor->backend->create_output(compositor, name);
5875}
5876
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005877/** Create an output for an unused head
5878 *
5879 * \param compositor The compositor.
5880 * \param head The head to attach to the output.
5881 * \return A new \c weston_output, or NULL on failure.
5882 *
5883 * This creates a new weston_output that starts with the given head attached.
5884 * The output inherits the name of the head. The head must not be already
5885 * attached to another output.
5886 *
5887 * An output must be configured before it can be enabled.
5888 *
5889 * \memberof weston_compositor
5890 */
5891WL_EXPORT struct weston_output *
5892weston_compositor_create_output_with_head(struct weston_compositor *compositor,
5893 struct weston_head *head)
5894{
5895 struct weston_output *output;
5896
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02005897 output = weston_compositor_create_output(compositor, head->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005898 if (!output)
5899 return NULL;
5900
5901 if (weston_output_attach_head(output, head) < 0) {
Pekka Paalanen42c0e142017-10-27 12:07:49 +03005902 weston_output_destroy(output);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005903 return NULL;
5904 }
5905
5906 return output;
5907}
5908
5909/** Destroy an output
5910 *
5911 * \param output The output to destroy.
5912 *
5913 * The heads attached to the given output are detached and become unused again.
5914 *
5915 * It is not necessary to explicitly destroy all outputs at compositor exit.
5916 * weston_compositor_destroy() will automatically destroy any remaining
5917 * outputs.
5918 *
5919 * \memberof weston_output
5920 */
5921WL_EXPORT void
5922weston_output_destroy(struct weston_output *output)
5923{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005924 output->destroy(output);
5925}
5926
Pekka Paalanencf0a4762017-04-04 16:36:07 +03005927/** When you need a head...
5928 *
5929 * This function is a hack, used until all code has been converted to become
5930 * multi-head aware.
5931 *
5932 * \param output The weston_output whose head to get.
5933 * \return The first head in the output's list.
5934 */
5935WL_EXPORT struct weston_head *
5936weston_output_get_first_head(struct weston_output *output)
5937{
5938 if (wl_list_empty(&output->head_list))
5939 return NULL;
5940
5941 return container_of(output->head_list.next,
5942 struct weston_head, output_link);
5943}
5944
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01005945static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005946destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01005947{
Jonny Lamb74130762013-11-26 18:19:46 +01005948 struct weston_surface *surface =
5949 wl_resource_get_user_data(resource);
5950
Pekka Paalanen4826f872016-04-22 14:14:38 +03005951 if (!surface)
5952 return;
5953
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005954 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02005955 surface->pending.buffer_viewport.buffer.src_width =
5956 wl_fixed_from_int(-1);
5957 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02005958 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01005959}
5960
5961static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005962viewport_destroy(struct wl_client *client,
5963 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01005964{
5965 wl_resource_destroy(resource);
5966}
5967
5968static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005969viewport_set_source(struct wl_client *client,
5970 struct wl_resource *resource,
5971 wl_fixed_t src_x,
5972 wl_fixed_t src_y,
5973 wl_fixed_t src_width,
5974 wl_fixed_t src_height)
5975{
5976 struct weston_surface *surface =
5977 wl_resource_get_user_data(resource);
5978
Pekka Paalanen4826f872016-04-22 14:14:38 +03005979 if (!surface) {
5980 wl_resource_post_error(resource,
5981 WP_VIEWPORT_ERROR_NO_SURFACE,
5982 "wl_surface for this viewport is no longer exists");
5983 return;
5984 }
5985
5986 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03005987 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005988
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005989 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03005990 src_height == wl_fixed_from_int(-1) &&
5991 src_x == wl_fixed_from_int(-1) &&
5992 src_y == wl_fixed_from_int(-1)) {
5993 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005994 surface->pending.buffer_viewport.buffer.src_width =
5995 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02005996 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005997 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005998 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03005999
Pekka Paalanen201769a2016-04-26 14:42:11 +03006000 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006001 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006002 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03006003 "wl_surface@%d viewport source "
6004 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
6005 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006006 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03006007 wl_fixed_to_double(src_height),
6008 wl_fixed_to_double(src_x),
6009 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006010 return;
6011 }
6012
6013 surface->pending.buffer_viewport.buffer.src_x = src_x;
6014 surface->pending.buffer_viewport.buffer.src_y = src_y;
6015 surface->pending.buffer_viewport.buffer.src_width = src_width;
6016 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006017 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006018}
6019
6020static void
6021viewport_set_destination(struct wl_client *client,
6022 struct wl_resource *resource,
6023 int32_t dst_width,
6024 int32_t dst_height)
6025{
6026 struct weston_surface *surface =
6027 wl_resource_get_user_data(resource);
6028
Pekka Paalanen4826f872016-04-22 14:14:38 +03006029 if (!surface) {
6030 wl_resource_post_error(resource,
6031 WP_VIEWPORT_ERROR_NO_SURFACE,
6032 "wl_surface for this viewport no longer exists");
6033 return;
6034 }
6035
6036 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006037
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006038 if (dst_width == -1 && dst_height == -1) {
6039 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006040 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006041 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006042 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006043 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006044
6045 if (dst_width <= 0 || dst_height <= 0) {
6046 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006047 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006048 "destination size must be positive (%dx%d)",
6049 dst_width, dst_height);
6050 return;
6051 }
6052
6053 surface->pending.buffer_viewport.surface.width = dst_width;
6054 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006055 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006056}
6057
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006058static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006059 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006060 viewport_set_source,
6061 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01006062};
6063
6064static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006065viewporter_destroy(struct wl_client *client,
6066 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006067{
6068 wl_resource_destroy(resource);
6069}
6070
6071static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006072viewporter_get_viewport(struct wl_client *client,
6073 struct wl_resource *viewporter,
6074 uint32_t id,
6075 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006076{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006077 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006078 struct weston_surface *surface =
6079 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006080 struct wl_resource *resource;
6081
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006082 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006083 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006084 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006085 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01006086 return;
6087 }
6088
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006089 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006090 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006091 if (resource == NULL) {
6092 wl_client_post_no_memory(client);
6093 return;
6094 }
6095
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006096 wl_resource_set_implementation(resource, &viewport_interface,
6097 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01006098
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006099 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006100}
6101
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006102static const struct wp_viewporter_interface viewporter_interface = {
6103 viewporter_destroy,
6104 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01006105};
6106
6107static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006108bind_viewporter(struct wl_client *client,
6109 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006110{
6111 struct wl_resource *resource;
6112
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006113 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006114 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006115 if (resource == NULL) {
6116 wl_client_post_no_memory(client);
6117 return;
6118 }
6119
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006120 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01006121 NULL, NULL);
6122}
6123
6124static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04006125destroy_presentation_feedback(struct wl_resource *feedback_resource)
6126{
6127 struct weston_presentation_feedback *feedback;
6128
6129 feedback = wl_resource_get_user_data(feedback_resource);
6130
6131 wl_list_remove(&feedback->link);
6132 free(feedback);
6133}
6134
6135static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006136presentation_destroy(struct wl_client *client, struct wl_resource *resource)
6137{
6138 wl_resource_destroy(resource);
6139}
6140
6141static void
6142presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04006143 struct wl_resource *presentation_resource,
6144 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006145 uint32_t callback)
6146{
Pekka Paalanen133e4392014-09-23 22:08:46 -04006147 struct weston_surface *surface;
6148 struct weston_presentation_feedback *feedback;
6149
6150 surface = wl_resource_get_user_data(surface_resource);
6151
Bryce Harringtonde16d892014-11-20 22:21:57 -08006152 feedback = zalloc(sizeof *feedback);
6153 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04006154 goto err_calloc;
6155
6156 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006157 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04006158 1, callback);
6159 if (!feedback->resource)
6160 goto err_create;
6161
6162 wl_resource_set_implementation(feedback->resource, NULL, feedback,
6163 destroy_presentation_feedback);
6164
6165 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
6166
6167 return;
6168
6169err_create:
6170 free(feedback);
6171
6172err_calloc:
6173 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006174}
6175
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006176static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006177 presentation_destroy,
6178 presentation_feedback
6179};
6180
6181static void
6182bind_presentation(struct wl_client *client,
6183 void *data, uint32_t version, uint32_t id)
6184{
6185 struct weston_compositor *compositor = data;
6186 struct wl_resource *resource;
6187
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006188 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006189 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006190 if (resource == NULL) {
6191 wl_client_post_no_memory(client);
6192 return;
6193 }
6194
6195 wl_resource_set_implementation(resource, &presentation_implementation,
6196 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006197 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006198}
6199
6200static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05006201compositor_bind(struct wl_client *client,
6202 void *data, uint32_t version, uint32_t id)
6203{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006204 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05006205 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05006206
Jason Ekstranda85118c2013-06-27 20:17:02 -05006207 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006208 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07006209 if (resource == NULL) {
6210 wl_client_post_no_memory(client);
6211 return;
6212 }
6213
6214 wl_resource_set_implementation(resource, &compositor_interface,
6215 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05006216}
6217
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006218WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006219weston_environment_get_fd(const char *env)
6220{
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07006221 char *e;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006222 int fd, flags;
6223
6224 e = getenv(env);
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07006225 if (!e || !safe_strtoint(e, &fd))
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006226 return -1;
6227
6228 flags = fcntl(fd, F_GETFD);
6229 if (flags == -1)
6230 return -1;
6231
6232 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
6233 unsetenv(env);
6234
6235 return fd;
6236}
6237
Pekka Paalanenb5026542014-11-12 15:09:24 +02006238static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02006239timeline_key_binding_handler(struct weston_keyboard *keyboard,
6240 const struct timespec *time, uint32_t key,
6241 void *data)
Pekka Paalanenb5026542014-11-12 15:09:24 +02006242{
6243 struct weston_compositor *compositor = data;
6244
6245 if (weston_timeline_enabled_)
6246 weston_timeline_close();
6247 else
6248 weston_timeline_open(compositor);
6249}
6250
Giulio Camuffo459137b2014-10-11 23:56:24 +03006251/** Create the compositor.
6252 *
6253 * This functions creates and initializes a compositor instance.
6254 *
6255 * \param display The Wayland display to be used.
6256 * \param user_data A pointer to an object that can later be retrieved
6257 * using the \ref weston_compositor_get_user_data function.
6258 * \return The compositor instance on success or NULL on failure.
6259 */
6260WL_EXPORT struct weston_compositor *
6261weston_compositor_create(struct wl_display *display, void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04006262{
Giulio Camuffo459137b2014-10-11 23:56:24 +03006263 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05006264 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07006265
Giulio Camuffo459137b2014-10-11 23:56:24 +03006266 ec = zalloc(sizeof *ec);
6267 if (!ec)
6268 return NULL;
6269
6270 ec->wl_display = display;
6271 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006272 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07006273 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006274 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006275 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006276 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006277 wl_signal_init(&ec->idle_signal);
6278 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006279 wl_signal_init(&ec->show_input_panel_signal);
6280 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02006281 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01006282 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01006283 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02006284 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006285 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02006286 wl_signal_init(&ec->output_resized_signal);
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03006287 wl_signal_init(&ec->heads_changed_signal);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02006288 wl_signal_init(&ec->output_heads_changed_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07006289 wl_signal_init(&ec->session_signal);
6290 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04006291
Casey Dahlin58ba1372012-04-19 22:50:08 -04006292 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03006293 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04006294
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02006295 ec->activate_serial = 1;
6296
Derek Foreman152254b2015-11-26 14:17:48 -06006297 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006298 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006299 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05006300
Giulio Camuffo954f1832014-10-11 18:27:30 +03006301 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006302 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006303 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03006304
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006305 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006306 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006307 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006308
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006309 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006310 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006311 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006312
Jonas Ådahl30d61d82014-10-22 21:21:17 +02006313 if (weston_input_init(ec) != 0)
6314 goto fail;
6315
Jason Ekstranda7af7042013-10-12 22:38:11 -05006316 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02006317 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006318 wl_list_init(&ec->layer_list);
6319 wl_list_init(&ec->seat_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006320 wl_list_init(&ec->pending_output_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006321 wl_list_init(&ec->output_list);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03006322 wl_list_init(&ec->head_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006323 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01006324 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006325 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01006326 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006327 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006328 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006329
Pekka Paalanen827b5d22016-06-29 11:54:26 +02006330 wl_list_init(&ec->plugin_api_list);
6331
Xiong Zhang97116532013-10-23 13:58:31 +08006332 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02006333 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04006334
Giulio Camuffo459137b2014-10-11 23:56:24 +03006335 wl_data_device_manager_init(ec->wl_display);
6336
6337 wl_display_init_shm(ec->wl_display);
6338
6339 loop = wl_display_get_event_loop(ec->wl_display);
6340 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Daniel Stone6847b852017-03-01 11:34:08 +00006341 ec->repaint_timer =
6342 wl_event_loop_add_timer(loop, output_repaint_timer_handler,
6343 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03006344
Quentin Glidic82681572016-12-17 13:40:51 +01006345 weston_layer_init(&ec->fade_layer, ec);
6346 weston_layer_init(&ec->cursor_layer, ec);
6347
6348 weston_layer_set_position(&ec->fade_layer, WESTON_LAYER_POSITION_FADE);
6349 weston_layer_set_position(&ec->cursor_layer,
6350 WESTON_LAYER_POSITION_CURSOR);
Giulio Camuffo459137b2014-10-11 23:56:24 +03006351
6352 weston_compositor_add_debug_binding(ec, KEY_T,
6353 timeline_key_binding_handler, ec);
6354
Giulio Camuffo459137b2014-10-11 23:56:24 +03006355 return ec;
6356
6357fail:
6358 free(ec);
6359 return NULL;
6360}
6361
Benjamin Franzkeb8263022011-08-30 11:32:47 +02006362WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006363weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07006364{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006365 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07006366
Pekka Paalanend1591ae2012-01-02 16:06:56 +02006367 wl_event_source_remove(ec->idle_source);
6368
Matt Roper361d2ad2011-08-29 13:52:23 -07006369 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02006370 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07006371 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02006372
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006373 /* Destroy all pending outputs associated with this compositor */
6374 wl_list_for_each_safe(output, next, &ec->pending_output_list, link)
6375 output->destroy(output);
6376
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02006377 if (ec->renderer)
6378 ec->renderer->destroy(ec);
6379
Daniel Stone325fc2d2012-05-30 16:31:58 +01006380 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09006381 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006382 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01006383 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006384 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006385 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02006386
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04006387 weston_plane_release(&ec->primary_plane);
Matt Roper361d2ad2011-08-29 13:52:23 -07006388}
6389
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05006390WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04006391weston_compositor_exit_with_code(struct weston_compositor *compositor,
6392 int exit_code)
6393{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02006394 if (compositor->exit_code == EXIT_SUCCESS)
6395 compositor->exit_code = exit_code;
6396
Giulio Camuffo459137b2014-10-11 23:56:24 +03006397 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04006398}
6399
6400WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01006401weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
6402 const struct weston_pointer_grab_interface *interface)
6403{
6404 struct weston_seat *seat;
6405
6406 ec->default_pointer_grab = interface;
6407 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05006408 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
6409
6410 if (pointer)
6411 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01006412 }
6413}
6414
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04006415WL_EXPORT int
6416weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
6417 clockid_t clk_id)
6418{
6419 struct timespec ts;
6420
6421 if (clock_gettime(clk_id, &ts) < 0)
6422 return -1;
6423
6424 compositor->presentation_clock = clk_id;
6425
6426 return 0;
6427}
6428
6429/*
6430 * For choosing the software clock, when the display hardware or API
6431 * does not expose a compatible presentation timestamp.
6432 */
6433WL_EXPORT int
6434weston_compositor_set_presentation_clock_software(
6435 struct weston_compositor *compositor)
6436{
6437 /* In order of preference */
6438 static const clockid_t clocks[] = {
6439 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
6440 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
6441 CLOCK_MONOTONIC, /* no jumps, may crawl */
6442 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
6443 CLOCK_REALTIME /* may jump and crawl */
6444 };
6445 unsigned i;
6446
6447 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
6448 if (weston_compositor_set_presentation_clock(compositor,
6449 clocks[i]) == 0)
6450 return 0;
6451
6452 weston_log("Error: no suitable presentation clock available.\n");
6453
6454 return -1;
6455}
6456
Pekka Paalanen662f3842015-03-18 12:17:26 +02006457/** Read the current time from the Presentation clock
6458 *
6459 * \param compositor
6460 * \param ts[out] The current time.
6461 *
6462 * \note Reading the current time in user space is always imprecise to some
6463 * degree.
6464 *
6465 * This function is never meant to fail. If reading the clock does fail,
6466 * an error message is logged and a zero time is returned. Callers are not
6467 * supposed to detect or react to failures.
6468 */
6469WL_EXPORT void
6470weston_compositor_read_presentation_clock(
6471 const struct weston_compositor *compositor,
6472 struct timespec *ts)
6473{
6474 static bool warned;
6475 int ret;
6476
6477 ret = clock_gettime(compositor->presentation_clock, ts);
6478 if (ret < 0) {
6479 ts->tv_sec = 0;
6480 ts->tv_nsec = 0;
6481
6482 if (!warned)
6483 weston_log("Error: failure to read "
6484 "the presentation clock %#x: '%m' (%d)\n",
6485 compositor->presentation_clock, errno);
6486 warned = true;
6487 }
6488}
6489
Pekka Paalanen230f3b12014-09-29 14:18:40 -04006490/** Import dmabuf buffer into current renderer
6491 *
6492 * \param compositor
6493 * \param buffer the dmabuf buffer to import
6494 * \return true on usable buffers, false otherwise
6495 *
6496 * This function tests that the linux_dmabuf_buffer is usable
6497 * for the current renderer. Returns false on unusable buffers. Usually
6498 * usability is tested by importing the dmabufs for composition.
6499 *
6500 * This hook is also used for detecting if the renderer supports
6501 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
6502 * supported.
6503 * */
6504WL_EXPORT bool
6505weston_compositor_import_dmabuf(struct weston_compositor *compositor,
6506 struct linux_dmabuf_buffer *buffer)
6507{
6508 struct weston_renderer *renderer;
6509
6510 renderer = compositor->renderer;
6511
6512 if (renderer->import_dmabuf == NULL)
6513 return false;
6514
6515 return renderer->import_dmabuf(compositor, buffer);
6516}
6517
Giulio Camuffocdb4d292013-11-14 23:42:53 +01006518WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05006519weston_version(int *major, int *minor, int *micro)
6520{
6521 *major = WESTON_VERSION_MAJOR;
6522 *minor = WESTON_VERSION_MINOR;
6523 *micro = WESTON_VERSION_MICRO;
6524}
6525
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03006526WL_EXPORT void *
6527weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006528{
Derek Foreman3f86e502015-06-08 11:46:54 -05006529 const char *builddir = getenv("WESTON_BUILD_DIR");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006530 char path[PATH_MAX];
6531 void *module, *init;
Daniel Stonebeb97e52016-11-28 12:13:54 +00006532 size_t len;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006533
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08006534 if (name == NULL)
6535 return NULL;
6536
Derek Foreman3f86e502015-06-08 11:46:54 -05006537 if (name[0] != '/') {
6538 if (builddir)
Daniel Stonebeb97e52016-11-28 12:13:54 +00006539 len = snprintf(path, sizeof path, "%s/.libs/%s",
6540 builddir, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05006541 else
Daniel Stonebeb97e52016-11-28 12:13:54 +00006542 len = snprintf(path, sizeof path, "%s/%s",
6543 LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05006544 } else {
Daniel Stonebeb97e52016-11-28 12:13:54 +00006545 len = snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05006546 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006547
Daniel Stonebeb97e52016-11-28 12:13:54 +00006548 /* snprintf returns the length of the string it would've written,
6549 * _excluding_ the NUL byte. So even being equal to the size of
6550 * our buffer is an error here. */
6551 if (len >= sizeof path)
6552 return NULL;
6553
Kristian Høgsberga6813d22012-09-12 12:21:01 -04006554 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
6555 if (module) {
6556 weston_log("Module '%s' already loaded\n", path);
6557 dlclose(module);
6558 return NULL;
6559 }
6560
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03006561 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04006562 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006563 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03006564 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006565 return NULL;
6566 }
6567
6568 init = dlsym(module, entrypoint);
6569 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03006570 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00006571 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006572 return NULL;
6573 }
6574
6575 return init;
6576}
6577
Giulio Camuffo459137b2014-10-11 23:56:24 +03006578
6579/** Destroys the compositor.
6580 *
6581 * This function cleans up the compositor state and destroys it.
6582 *
6583 * \param compositor The compositor to be destroyed.
6584 */
6585WL_EXPORT void
6586weston_compositor_destroy(struct weston_compositor *compositor)
6587{
6588 /* prevent further rendering while shutting down */
6589 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
6590
6591 wl_signal_emit(&compositor->destroy_signal, compositor);
6592
6593 weston_compositor_xkb_destroy(compositor);
6594
Giulio Camuffo2d24e642015-10-03 16:25:15 +03006595 if (compositor->backend)
6596 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02006597
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03006598 /* The backend is responsible for destroying the heads. */
6599 assert(wl_list_empty(&compositor->head_list));
6600
Pekka Paalanen827b5d22016-06-29 11:54:26 +02006601 weston_plugin_api_destroy_list(compositor);
6602
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03006603 if (compositor->heads_changed_source)
6604 wl_event_source_remove(compositor->heads_changed_source);
6605
Giulio Camuffo459137b2014-10-11 23:56:24 +03006606 free(compositor);
6607}
6608
6609/** Instruct the compositor to exit.
6610 *
6611 * This functions does not directly destroy the compositor object, it merely
6612 * command it to start the tear down process. It is not guaranteed that the
6613 * tear down will happen immediately.
6614 *
6615 * \param compositor The compositor to tear down.
6616 */
6617WL_EXPORT void
6618weston_compositor_exit(struct weston_compositor *compositor)
6619{
6620 compositor->exit(compositor);
6621}
6622
6623/** Return the user data stored in the compositor.
6624 *
6625 * This function returns the user data pointer set with user_data parameter
6626 * to the \ref weston_compositor_create function.
6627 */
6628WL_EXPORT void *
6629weston_compositor_get_user_data(struct weston_compositor *compositor)
6630{
6631 return compositor->user_data;
6632}
Pekka Paalanendd186732016-06-03 14:49:54 +03006633
Pekka Paalanen50dbf382016-06-03 15:23:46 +03006634static const char * const backend_map[] = {
6635 [WESTON_BACKEND_DRM] = "drm-backend.so",
6636 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
6637 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
6638 [WESTON_BACKEND_RDP] = "rdp-backend.so",
6639 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
6640 [WESTON_BACKEND_X11] = "x11-backend.so",
6641};
6642
Pekka Paalanendd186732016-06-03 14:49:54 +03006643/** Load a backend into a weston_compositor
6644 *
6645 * A backend must be loaded to make a weston_compositor work. A backend
6646 * provides input and output capabilities, and determines the renderer to use.
6647 *
6648 * \param compositor A compositor that has not had a backend loaded yet.
6649 * \param backend Name of the backend file.
6650 * \param config_base A pointer to a backend-specific configuration
6651 * structure's 'base' member.
6652 *
6653 * \return 0 on success, or -1 on error.
6654 */
6655WL_EXPORT int
6656weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03006657 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03006658 struct weston_backend_config *config_base)
6659{
6660 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03006661 struct weston_backend_config *config_base);
6662
Pekka Paalanend7e35112017-08-29 17:04:12 +03006663 if (compositor->backend) {
6664 weston_log("Error: attempt to load a backend when one is already loaded\n");
6665 return -1;
6666 }
6667
Quentin Glidic887c0182016-07-10 11:00:53 +02006668 if (backend >= ARRAY_LENGTH(backend_map))
Pekka Paalanen50dbf382016-06-03 15:23:46 +03006669 return -1;
6670
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01006671 backend_init = weston_load_module(backend_map[backend], "weston_backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03006672 if (!backend_init)
6673 return -1;
6674
Pekka Paalanend7e35112017-08-29 17:04:12 +03006675 if (backend_init(compositor, config_base) < 0) {
6676 compositor->backend = NULL;
6677 return -1;
6678 }
6679
6680 return 0;
Pekka Paalanendd186732016-06-03 14:49:54 +03006681}
Giulio Camuffo9c764df2016-06-29 11:54:27 +02006682
6683WL_EXPORT int
6684weston_compositor_load_xwayland(struct weston_compositor *compositor)
6685{
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01006686 int (*module_init)(struct weston_compositor *ec);
Giulio Camuffo9c764df2016-06-29 11:54:27 +02006687
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01006688 module_init = weston_load_module("xwayland.so", "weston_module_init");
Giulio Camuffo9c764df2016-06-29 11:54:27 +02006689 if (!module_init)
6690 return -1;
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01006691 if (module_init(compositor) < 0)
Giulio Camuffo9c764df2016-06-29 11:54:27 +02006692 return -1;
6693 return 0;
6694}