blob: 6a300e0dd0e1ed9a3b91f8a4f421bf5afcde9ad3 [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
Semi Malinene7a52fb2018-04-26 11:08:10 +02001025static void
1026notify_view_output_destroy(struct wl_listener *listener, void *data)
1027{
1028 struct weston_view *view =
1029 container_of(listener,
1030 struct weston_view, output_destroy_listener);
1031
1032 view->output = NULL;
1033 view->output_destroy_listener.notify = NULL;
1034}
1035
1036/** Set the primary output of the view
1037 *
1038 * \param view The view whose primary output to set
1039 * \param output The new primary output for the view
1040 *
1041 * Set \a output to be the primary output of the \a view.
1042 *
1043 * Notice that the assignment may be temporary; the primary output could be
1044 * automatically changed. Hence, one cannot rely on the value persisting.
1045 *
1046 * Passing NULL as /a output will set the primary output to NULL.
1047 */
1048WL_EXPORT void
1049weston_view_set_output(struct weston_view *view, struct weston_output *output)
1050{
1051 if (view->output_destroy_listener.notify) {
1052 wl_list_remove(&view->output_destroy_listener.link);
1053 view->output_destroy_listener.notify = NULL;
1054 }
1055 view->output = output;
1056 if (output) {
1057 view->output_destroy_listener.notify =
1058 notify_view_output_destroy;
1059 wl_signal_add(&output->destroy_signal,
1060 &view->output_destroy_listener);
1061 }
1062}
1063
Bryce Harrington3f650b82015-12-23 11:01:58 -08001064/** Recalculate which output(s) the surface has views displayed on
1065 *
1066 * \param es The surface to remap to outputs
1067 *
1068 * Finds the output that is showing the largest amount of one
1069 * of the surface's various views. This output becomes the
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001070 * surface's primary output for vsync and frame callback purposes.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001071 *
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001072 * Also notes all outputs of all of the surface's views
Bryce Harrington3f650b82015-12-23 11:01:58 -08001073 * in the output_mask for the surface.
1074 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001075static void
1076weston_surface_assign_output(struct weston_surface *es)
1077{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001078 struct weston_output *new_output;
1079 struct weston_view *view;
1080 pixman_region32_t region;
1081 uint32_t max, area, mask;
1082 pixman_box32_t *e;
1083
1084 new_output = NULL;
1085 max = 0;
1086 mask = 0;
1087 pixman_region32_init(&region);
1088 wl_list_for_each(view, &es->views, surface_link) {
1089 if (!view->output)
1090 continue;
1091
1092 pixman_region32_intersect(&region, &view->transform.boundingbox,
1093 &view->output->region);
1094
1095 e = pixman_region32_extents(&region);
1096 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1097
1098 mask |= view->output_mask;
1099
1100 if (area >= max) {
1101 new_output = view->output;
1102 max = area;
1103 }
1104 }
1105 pixman_region32_fini(&region);
1106
1107 es->output = new_output;
1108 weston_surface_update_output_mask(es, mask);
1109}
1110
Bryce Harrington3f650b82015-12-23 11:01:58 -08001111/** Recalculate which output(s) the view is displayed on
1112 *
1113 * \param ev The view to remap to outputs
1114 *
1115 * Identifies the set of outputs that the view is visible on,
1116 * noting them into the output_mask. The output that the view
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001117 * is most visible on is set as the view's primary output.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001118 *
1119 * Also does the same for the view's surface. See
1120 * weston_surface_assign_output().
1121 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001122static void
1123weston_view_assign_output(struct weston_view *ev)
1124{
1125 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001126 struct weston_output *output, *new_output;
1127 pixman_region32_t region;
1128 uint32_t max, area, mask;
1129 pixman_box32_t *e;
1130
1131 new_output = NULL;
1132 max = 0;
1133 mask = 0;
1134 pixman_region32_init(&region);
1135 wl_list_for_each(output, &ec->output_list, link) {
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03001136 if (output->destroying)
1137 continue;
1138
Jason Ekstranda7af7042013-10-12 22:38:11 -05001139 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001140 &output->region);
1141
1142 e = pixman_region32_extents(&region);
1143 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1144
1145 if (area > 0)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001146 mask |= 1u << output->id;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001147
1148 if (area >= max) {
1149 new_output = output;
1150 max = area;
1151 }
1152 }
1153 pixman_region32_fini(&region);
1154
Semi Malinene7a52fb2018-04-26 11:08:10 +02001155 weston_view_set_output(ev, new_output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001156 ev->output_mask = mask;
1157
1158 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001159}
1160
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001161static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001162weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1163 int from_x, int from_y, int *to_x, int *to_y)
1164{
1165 float x, y;
1166
1167 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1168 weston_view_from_global_float(to, x, y, &x, &y);
1169
1170 *to_x = round(x);
1171 *to_y = round(y);
1172}
1173
1174static void
1175weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1176{
1177 pixman_box32_t *a;
1178 pixman_box32_t b;
1179
1180 a = pixman_region32_extents(&from->geometry.scissor);
1181
1182 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1183 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1184
1185 pixman_region32_fini(&to->geometry.scissor);
1186 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1187}
1188
1189static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001190view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001191 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001192{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001193 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1194 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001195 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001196 { inbox->x1, inbox->y1 },
1197 { inbox->x1, inbox->y2 },
1198 { inbox->x2, inbox->y1 },
1199 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001200 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001201 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001202 int i;
1203
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001204 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001205 /* avoid rounding empty bbox to 1x1 */
1206 pixman_region32_init(bbox);
1207 return;
1208 }
1209
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001210 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001211 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001212 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001213 if (x < min_x)
1214 min_x = x;
1215 if (x > max_x)
1216 max_x = x;
1217 if (y < min_y)
1218 min_y = y;
1219 if (y > max_y)
1220 max_y = y;
1221 }
1222
Pekka Paalanen219b9822012-02-08 15:38:37 +02001223 int_x = floorf(min_x);
1224 int_y = floorf(min_y);
1225 pixman_region32_init_rect(bbox, int_x, int_y,
1226 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001227}
1228
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001229static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001230weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001231{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001232 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001233
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001234 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001235 view->geometry.x = roundf(view->geometry.x);
1236 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001237
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001238 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001239 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1240 view->transform.position.matrix.d[12] = view->geometry.x;
1241 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001242
Jason Ekstranda7af7042013-10-12 22:38:11 -05001243 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001244
Jason Ekstranda7af7042013-10-12 22:38:11 -05001245 view->transform.inverse = view->transform.position.matrix;
1246 view->transform.inverse.d[12] = -view->geometry.x;
1247 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001248
Jason Ekstranda7af7042013-10-12 22:38:11 -05001249 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001250 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001251 view->surface->width,
1252 view->surface->height);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001253 if (view->geometry.scissor_enabled)
1254 pixman_region32_intersect(&view->transform.boundingbox,
1255 &view->transform.boundingbox,
1256 &view->geometry.scissor);
1257
1258 pixman_region32_translate(&view->transform.boundingbox,
1259 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001260
Jason Ekstranda7af7042013-10-12 22:38:11 -05001261 if (view->alpha == 1.0) {
1262 pixman_region32_copy(&view->transform.opaque,
1263 &view->surface->opaque);
1264 pixman_region32_translate(&view->transform.opaque,
1265 view->geometry.x,
1266 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001267 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001268}
1269
1270static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001271weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001272{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001273 struct weston_view *parent = view->geometry.parent;
1274 struct weston_matrix *matrix = &view->transform.matrix;
1275 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001276 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001277 pixman_region32_t surfregion;
1278 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001279
Jason Ekstranda7af7042013-10-12 22:38:11 -05001280 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001281
1282 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001283 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1284 view->transform.position.matrix.d[12] = view->geometry.x;
1285 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001286
1287 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001288 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001289 weston_matrix_multiply(matrix, &tform->matrix);
1290
Pekka Paalanen483243f2013-03-08 14:56:50 +02001291 if (parent)
1292 weston_matrix_multiply(matrix, &parent->transform.matrix);
1293
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001294 if (weston_matrix_invert(inverse, matrix) < 0) {
1295 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001296 weston_log("error: weston_view %p"
1297 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001298 return -1;
1299 }
1300
Giulio Camuffo148c1992016-09-04 18:50:46 +03001301 if (view->alpha == 1.0 &&
1302 matrix->type == WESTON_MATRIX_TRANSFORM_TRANSLATE) {
1303 pixman_region32_copy(&view->transform.opaque,
1304 &view->surface->opaque);
1305 pixman_region32_translate(&view->transform.opaque,
1306 matrix->d[12],
1307 matrix->d[13]);
1308 }
1309
Pekka Paalanen380adf52015-02-16 14:39:11 +02001310 pixman_region32_init_rect(&surfregion, 0, 0,
1311 view->surface->width, view->surface->height);
1312 if (view->geometry.scissor_enabled)
1313 pixman_region32_intersect(&surfregion, &surfregion,
1314 &view->geometry.scissor);
1315 surfbox = pixman_region32_extents(&surfregion);
1316
1317 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1318 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001319
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001320 return 0;
1321}
1322
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001323static struct weston_layer *
1324get_view_layer(struct weston_view *view)
1325{
1326 if (view->parent_view)
1327 return get_view_layer(view->parent_view);
1328 return view->layer_link.layer;
1329}
1330
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001331WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001332weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001333{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001334 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001335 struct weston_layer *layer;
1336 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001337
Jason Ekstranda7af7042013-10-12 22:38:11 -05001338 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001339 return;
1340
Pekka Paalanen483243f2013-03-08 14:56:50 +02001341 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001342 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001343
Jason Ekstranda7af7042013-10-12 22:38:11 -05001344 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001345
Jason Ekstranda7af7042013-10-12 22:38:11 -05001346 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001347
Jason Ekstranda7af7042013-10-12 22:38:11 -05001348 pixman_region32_fini(&view->transform.boundingbox);
1349 pixman_region32_fini(&view->transform.opaque);
1350 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001351
Pekka Paalanencd403622012-01-25 13:37:39 +02001352 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001353 if (view->geometry.transformation_list.next ==
1354 &view->transform.position.link &&
1355 view->geometry.transformation_list.prev ==
1356 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001357 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001358 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001359 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001360 if (weston_view_update_transform_enable(view) < 0)
1361 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001362 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001363
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001364 layer = get_view_layer(view);
1365 if (layer) {
1366 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001367 pixman_region32_intersect(&view->transform.boundingbox,
1368 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001369 pixman_region32_intersect(&view->transform.opaque,
1370 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001371 pixman_region32_fini(&mask);
1372 }
1373
Pekka Paalanen380adf52015-02-16 14:39:11 +02001374 if (parent) {
1375 if (parent->geometry.scissor_enabled) {
1376 view->geometry.scissor_enabled = true;
1377 weston_view_transfer_scissor(parent, view);
1378 } else {
1379 view->geometry.scissor_enabled = false;
1380 }
1381 }
1382
Jason Ekstranda7af7042013-10-12 22:38:11 -05001383 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001384
Jason Ekstranda7af7042013-10-12 22:38:11 -05001385 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001386
Jason Ekstranda7af7042013-10-12 22:38:11 -05001387 wl_signal_emit(&view->surface->compositor->transform_signal,
1388 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001389}
1390
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001391WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001392weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001393{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001394 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001395
1396 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001397 * The invariant: if view->geometry.dirty, then all views
1398 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001399 * Corollary: if not parent->geometry.dirty, then all ancestors
1400 * are not dirty.
1401 */
1402
Jason Ekstranda7af7042013-10-12 22:38:11 -05001403 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001404 return;
1405
Jason Ekstranda7af7042013-10-12 22:38:11 -05001406 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001407
Jason Ekstranda7af7042013-10-12 22:38:11 -05001408 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001409 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001410 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001411}
1412
1413WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001414weston_view_to_global_fixed(struct weston_view *view,
1415 wl_fixed_t vx, wl_fixed_t vy,
1416 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001417{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001418 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001419
Jason Ekstranda7af7042013-10-12 22:38:11 -05001420 weston_view_to_global_float(view,
1421 wl_fixed_to_double(vx),
1422 wl_fixed_to_double(vy),
1423 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001424 *x = wl_fixed_from_double(xf);
1425 *y = wl_fixed_from_double(yf);
1426}
1427
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001428WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001429weston_view_from_global_float(struct weston_view *view,
1430 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001431{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001432 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001433 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1434
Jason Ekstranda7af7042013-10-12 22:38:11 -05001435 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001436
1437 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001438 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001439 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001440 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001441 *vx = 0;
1442 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001443 return;
1444 }
1445
Jason Ekstranda7af7042013-10-12 22:38:11 -05001446 *vx = v.f[0] / v.f[3];
1447 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001448 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001449 *vx = x - view->geometry.x;
1450 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001451 }
1452}
1453
1454WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001455weston_view_from_global_fixed(struct weston_view *view,
1456 wl_fixed_t x, wl_fixed_t y,
1457 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001458{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001459 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001460
Jason Ekstranda7af7042013-10-12 22:38:11 -05001461 weston_view_from_global_float(view,
1462 wl_fixed_to_double(x),
1463 wl_fixed_to_double(y),
1464 &vxf, &vyf);
1465 *vx = wl_fixed_from_double(vxf);
1466 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001467}
1468
1469WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001470weston_view_from_global(struct weston_view *view,
1471 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001472{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001473 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001474
Jason Ekstranda7af7042013-10-12 22:38:11 -05001475 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1476 *vx = floorf(vxf);
1477 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001478}
1479
Bryce Harrington3f650b82015-12-23 11:01:58 -08001480/**
1481 * \param surface The surface to be repainted
1482 *
1483 * Marks the output(s) that the surface is shown on as needing to be
1484 * repainted. See weston_output_schedule_repaint().
1485 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001486WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001487weston_surface_schedule_repaint(struct weston_surface *surface)
1488{
1489 struct weston_output *output;
1490
1491 wl_list_for_each(output, &surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001492 if (surface->output_mask & (1u << output->id))
Kristian Høgsberg98238702012-08-03 16:29:12 -04001493 weston_output_schedule_repaint(output);
1494}
1495
Bryce Harrington3f650b82015-12-23 11:01:58 -08001496/**
1497 * \param view The view to be repainted
1498 *
1499 * Marks the output(s) that the view is shown on as needing to be
1500 * repainted. See weston_output_schedule_repaint().
1501 */
Kristian Høgsberg98238702012-08-03 16:29:12 -04001502WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001503weston_view_schedule_repaint(struct weston_view *view)
1504{
1505 struct weston_output *output;
1506
1507 wl_list_for_each(output, &view->surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001508 if (view->output_mask & (1u << output->id))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001509 weston_output_schedule_repaint(output);
1510}
1511
Pekka Paalanene508ce62015-02-19 13:59:55 +02001512/**
1513 * XXX: This function does it the wrong way.
1514 * surface->damage is the damage from the client, and causes
1515 * surface_flush_damage() to copy pixels. No window management action can
1516 * cause damage to the client-provided content, warranting re-upload!
1517 *
1518 * Instead of surface->damage, this function should record the damage
1519 * with all the views for this surface to avoid extraneous texture
1520 * uploads.
1521 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001522WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001523weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001524{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001525 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001526 0, 0, surface->width,
1527 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001528
Kristian Høgsberg98238702012-08-03 16:29:12 -04001529 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001530}
1531
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001532WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001533weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001534{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001535 if (view->geometry.x == x && view->geometry.y == y)
1536 return;
1537
Jason Ekstranda7af7042013-10-12 22:38:11 -05001538 view->geometry.x = x;
1539 view->geometry.y = y;
1540 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001541}
1542
Pekka Paalanen483243f2013-03-08 14:56:50 +02001543static void
1544transform_parent_handle_parent_destroy(struct wl_listener *listener,
1545 void *data)
1546{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001547 struct weston_view *view =
1548 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001549 geometry.parent_destroy_listener);
1550
Jason Ekstranda7af7042013-10-12 22:38:11 -05001551 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001552}
1553
1554WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001555weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001556 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001557{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001558 if (view->geometry.parent) {
1559 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1560 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001561
1562 if (!parent)
1563 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001564 }
1565
Jason Ekstranda7af7042013-10-12 22:38:11 -05001566 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001567
Jason Ekstranda7af7042013-10-12 22:38:11 -05001568 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001569 transform_parent_handle_parent_destroy;
1570 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001571 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001572 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001573 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001574 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001575 }
1576
Jason Ekstranda7af7042013-10-12 22:38:11 -05001577 weston_view_geometry_dirty(view);
1578}
1579
Pekka Paalanen380adf52015-02-16 14:39:11 +02001580/** Set a clip mask rectangle on a view
1581 *
1582 * \param view The view to set the clip mask on.
1583 * \param x Top-left corner X coordinate of the clip rectangle.
1584 * \param y Top-left corner Y coordinate of the clip rectangle.
1585 * \param width Width of the clip rectangle, non-negative.
1586 * \param height Height of the clip rectangle, non-negative.
1587 *
1588 * A shell may set a clip mask rectangle on a view. Everything outside
1589 * the rectangle is cut away for input and output purposes: it is
1590 * not drawn and cannot be hit by hit-test based input like pointer
1591 * motion or touch-downs. Everything inside the rectangle will behave
1592 * normally. Clients are unaware of clipping.
1593 *
Yong Bakos4c72e292016-04-28 11:59:10 -05001594 * The rectangle is set in surface-local coordinates. Setting a clip
Pekka Paalanen380adf52015-02-16 14:39:11 +02001595 * mask rectangle does not affect the view position, the view is positioned
1596 * as it would be without a clip. The clip also does not change
1597 * weston_surface::width,height.
1598 *
1599 * The clip mask rectangle is part of transformation inheritance
1600 * (weston_view_set_transform_parent()). A clip set in the root of the
1601 * transformation inheritance tree will affect all views in the tree.
1602 * A clip can be set only on the root view. Attempting to set a clip
1603 * on view that has a transformation parent will fail. Assigning a parent
1604 * to a view that has a clip set will cause the clip to be forgotten.
1605 *
1606 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1607 * on the additional transformations in the child views. These transformations
1608 * may not rotate the coordinate axes, i.e., only translation and scaling
1609 * are allowed. Violating this restriction causes the clipping to malfunction.
1610 * Furthermore, using scaling may cause rounding errors in child clipping.
1611 *
1612 * The clip mask rectangle is not automatically adjusted based on
1613 * wl_surface.attach dx and dy arguments.
1614 *
1615 * A clip mask rectangle can be set only if the compositor capability
1616 * WESTON_CAP_VIEW_CLIP_MASK is present.
1617 *
1618 * This function sets the clip mask rectangle and schedules a repaint for
1619 * the view.
1620 */
1621WL_EXPORT void
1622weston_view_set_mask(struct weston_view *view,
1623 int x, int y, int width, int height)
1624{
1625 struct weston_compositor *compositor = view->surface->compositor;
1626
1627 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1628 weston_log("%s not allowed without capability!\n", __func__);
1629 return;
1630 }
1631
1632 if (view->geometry.parent) {
1633 weston_log("view %p has a parent, clip forbidden!\n", view);
1634 return;
1635 }
1636
1637 if (width < 0 || height < 0) {
1638 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1639 x, y, width, height);
1640 return;
1641 }
1642
1643 pixman_region32_fini(&view->geometry.scissor);
1644 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1645 view->geometry.scissor_enabled = true;
1646 weston_view_geometry_dirty(view);
1647 weston_view_schedule_repaint(view);
1648}
1649
1650/** Remove the clip mask from a view
1651 *
1652 * \param view The view to remove the clip mask from.
1653 *
1654 * Removed the clip mask rectangle and schedules a repaint.
1655 *
1656 * \sa weston_view_set_mask
1657 */
1658WL_EXPORT void
1659weston_view_set_mask_infinite(struct weston_view *view)
1660{
1661 view->geometry.scissor_enabled = false;
1662 weston_view_geometry_dirty(view);
1663 weston_view_schedule_repaint(view);
1664}
1665
Armin Krezović0da12b82016-06-30 06:04:33 +02001666/* Check if view should be displayed
1667 *
1668 * The indicator is set manually when assigning
1669 * a view to a surface.
1670 *
1671 * This needs reworking. See the thread starting at:
1672 *
1673 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1674 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001675WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001676weston_view_is_mapped(struct weston_view *view)
1677{
Armin Krezović0da12b82016-06-30 06:04:33 +02001678 return view->is_mapped;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001679}
1680
Armin Krezović0da12b82016-06-30 06:04:33 +02001681/* Check if a surface has a view assigned to it
1682 *
1683 * The indicator is set manually when mapping
1684 * a surface and creating a view for it.
1685 *
1686 * This needs to go. See the thread starting at:
1687 *
1688 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1689 *
1690 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001691WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001692weston_surface_is_mapped(struct weston_surface *surface)
1693{
Armin Krezović0da12b82016-06-30 06:04:33 +02001694 return surface->is_mapped;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001695}
1696
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001697static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001698surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001699{
1700 struct weston_view *view;
1701
1702 if (surface->width == width && surface->height == height)
1703 return;
1704
1705 surface->width = width;
1706 surface->height = height;
1707
1708 wl_list_for_each(view, &surface->views, surface_link)
1709 weston_view_geometry_dirty(view);
1710}
1711
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001712WL_EXPORT void
1713weston_surface_set_size(struct weston_surface *surface,
1714 int32_t width, int32_t height)
1715{
1716 assert(!surface->resource);
1717 surface_set_size(surface, width, height);
1718}
1719
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001720static int
1721fixed_round_up_to_int(wl_fixed_t f)
1722{
1723 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1724}
1725
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001726static void
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001727convert_size_by_transform_scale(int32_t *width_out, int32_t *height_out,
1728 int32_t width, int32_t height,
1729 uint32_t transform,
1730 int32_t scale)
1731{
1732 assert(scale > 0);
1733
1734 switch (transform) {
1735 case WL_OUTPUT_TRANSFORM_NORMAL:
1736 case WL_OUTPUT_TRANSFORM_180:
1737 case WL_OUTPUT_TRANSFORM_FLIPPED:
1738 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1739 *width_out = width / scale;
1740 *height_out = height / scale;
1741 break;
1742 case WL_OUTPUT_TRANSFORM_90:
1743 case WL_OUTPUT_TRANSFORM_270:
1744 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1745 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1746 *width_out = height / scale;
1747 *height_out = width / scale;
1748 break;
1749 default:
1750 assert(0 && "invalid transform");
1751 }
1752}
1753
1754static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001755weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001756{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001757 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001758
1759 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001760 surface->width_from_buffer = 0;
1761 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001762 return;
1763 }
1764
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001765 convert_size_by_transform_scale(&surface->width_from_buffer,
1766 &surface->height_from_buffer,
1767 surface->buffer_ref.buffer->width,
1768 surface->buffer_ref.buffer->height,
1769 vp->buffer.transform,
1770 vp->buffer.scale);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001771}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001772
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001773static void
1774weston_surface_update_size(struct weston_surface *surface)
1775{
1776 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1777 int32_t width, height;
1778
1779 width = surface->width_from_buffer;
1780 height = surface->height_from_buffer;
1781
1782 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001783 surface_set_size(surface,
1784 vp->surface.width, vp->surface.height);
1785 return;
1786 }
1787
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001788 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001789 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1790 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1791
1792 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001793 return;
1794 }
1795
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001796 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001797}
1798
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02001799WL_EXPORT void
1800weston_compositor_get_time(struct timespec *time)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001801{
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02001802 clock_gettime(CLOCK_REALTIME, time);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001803}
1804
Jason Ekstranda7af7042013-10-12 22:38:11 -05001805WL_EXPORT struct weston_view *
1806weston_compositor_pick_view(struct weston_compositor *compositor,
1807 wl_fixed_t x, wl_fixed_t y,
1808 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001809{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001810 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001811 wl_fixed_t view_x, view_y;
1812 int view_ix, view_iy;
1813 int ix = wl_fixed_to_int(x);
1814 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001815
Jason Ekstranda7af7042013-10-12 22:38:11 -05001816 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001817 if (!pixman_region32_contains_point(
1818 &view->transform.boundingbox, ix, iy, NULL))
1819 continue;
1820
1821 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
1822 view_ix = wl_fixed_to_int(view_x);
1823 view_iy = wl_fixed_to_int(view_y);
1824
1825 if (!pixman_region32_contains_point(&view->surface->input,
1826 view_ix, view_iy, NULL))
1827 continue;
1828
Pekka Paalanen380adf52015-02-16 14:39:11 +02001829 if (view->geometry.scissor_enabled &&
1830 !pixman_region32_contains_point(&view->geometry.scissor,
1831 view_ix, view_iy, NULL))
1832 continue;
1833
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001834 *vx = view_x;
1835 *vy = view_y;
1836 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001837 }
1838
Derek Foremanf9318d12015-05-11 15:40:11 -05001839 *vx = wl_fixed_from_int(-1000000);
1840 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001841 return NULL;
1842}
1843
1844static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001845weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001846{
Daniel Stone37816df2012-05-16 18:45:18 +01001847 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001848
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001849 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001850 return;
1851
Daniel Stone37816df2012-05-16 18:45:18 +01001852 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001853 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001854}
1855
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001856WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001857weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001858{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001859 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001860
Jason Ekstranda7af7042013-10-12 22:38:11 -05001861 if (!weston_view_is_mapped(view))
1862 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001863
Jason Ekstranda7af7042013-10-12 22:38:11 -05001864 weston_view_damage_below(view);
Semi Malinene7a52fb2018-04-26 11:08:10 +02001865 weston_view_set_output(view, NULL);
Xiong Zhang97116532013-10-23 13:58:31 +08001866 view->plane = NULL;
Armin Krezovićf8486c32016-06-30 06:04:28 +02001867 view->is_mapped = false;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001868 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001869 wl_list_remove(&view->link);
1870 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001871 view->output_mask = 0;
1872 weston_surface_assign_output(view->surface);
1873
1874 if (weston_surface_is_mapped(view->surface))
1875 return;
1876
1877 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001878 struct weston_touch *touch = weston_seat_get_touch(seat);
1879 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1880 struct weston_keyboard *keyboard =
1881 weston_seat_get_keyboard(seat);
1882
1883 if (keyboard && keyboard->focus == view->surface)
1884 weston_keyboard_set_focus(keyboard, NULL);
1885 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05001886 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05001887 if (touch && touch->focus == view)
1888 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001889 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001890}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001891
Jason Ekstranda7af7042013-10-12 22:38:11 -05001892WL_EXPORT void
1893weston_surface_unmap(struct weston_surface *surface)
1894{
1895 struct weston_view *view;
1896
Armin Krezovićf8486c32016-06-30 06:04:28 +02001897 surface->is_mapped = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001898 wl_list_for_each(view, &surface->views, surface_link)
1899 weston_view_unmap(view);
1900 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001901}
1902
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001903static void
1904weston_surface_reset_pending_buffer(struct weston_surface *surface)
1905{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001906 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001907 surface->pending.sx = 0;
1908 surface->pending.sy = 0;
1909 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001910 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001911}
1912
Jason Ekstranda7af7042013-10-12 22:38:11 -05001913WL_EXPORT void
1914weston_view_destroy(struct weston_view *view)
1915{
1916 wl_signal_emit(&view->destroy_signal, view);
1917
1918 assert(wl_list_empty(&view->geometry.child_list));
1919
1920 if (weston_view_is_mapped(view)) {
1921 weston_view_unmap(view);
1922 weston_compositor_build_view_list(view->surface->compositor);
1923 }
1924
1925 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001926 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001927
1928 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001929 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001930 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001931 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001932
1933 weston_view_set_transform_parent(view, NULL);
1934
Jason Ekstranda7af7042013-10-12 22:38:11 -05001935 wl_list_remove(&view->surface_link);
1936
1937 free(view);
1938}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001939
1940WL_EXPORT void
1941weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001942{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001943 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001944 struct weston_view *ev, *nv;
Jonas Ådahld3414f22016-07-22 17:56:31 +08001945 struct weston_pointer_constraint *constraint, *next_constraint;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001946
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001947 if (--surface->ref_count > 0)
1948 return;
1949
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001950 assert(surface->resource == NULL);
1951
Pekka Paalanenca790762015-04-17 14:23:38 +03001952 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001953
Pekka Paalanene67858b2013-04-25 13:57:42 +03001954 assert(wl_list_empty(&surface->subsurface_list_pending));
1955 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001956
Jason Ekstranda7af7042013-10-12 22:38:11 -05001957 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1958 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001959
Jason Ekstrand7b982072014-05-20 14:33:03 -05001960 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001961
Pekka Paalanende685b82012-12-04 15:58:12 +02001962 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001963
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001964 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001965 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001966 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001967
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001968 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001969 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001970
Pekka Paalanen133e4392014-09-23 22:08:46 -04001971 weston_presentation_feedback_discard_list(&surface->feedback_list);
1972
Jonas Ådahld3414f22016-07-22 17:56:31 +08001973 wl_list_for_each_safe(constraint, next_constraint,
1974 &surface->pointer_constraints,
1975 link)
1976 weston_pointer_constraint_destroy(constraint);
1977
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001978 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001979}
1980
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001981static void
1982destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001983{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001984 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001985
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001986 assert(surface);
1987
Giulio Camuffo0d379742013-11-15 22:06:15 +01001988 /* Set the resource to NULL, since we don't want to leave a
1989 * dangling pointer if the surface was refcounted and survives
1990 * the weston_surface_destroy() call. */
1991 surface->resource = NULL;
Pekka Paalanen4826f872016-04-22 14:14:38 +03001992
1993 if (surface->viewport_resource)
1994 wl_resource_set_user_data(surface->viewport_resource, NULL);
1995
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001996 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001997}
1998
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001999static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002000weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
2001{
2002 struct weston_buffer *buffer =
2003 container_of(listener, struct weston_buffer, destroy_listener);
2004
2005 wl_signal_emit(&buffer->destroy_signal, buffer);
2006 free(buffer);
2007}
2008
Giulio Camuffoe058cd12013-12-12 14:14:29 +01002009WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002010weston_buffer_from_resource(struct wl_resource *resource)
2011{
2012 struct weston_buffer *buffer;
2013 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002014
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002015 listener = wl_resource_get_destroy_listener(resource,
2016 weston_buffer_destroy_handler);
2017
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002018 if (listener)
2019 return container_of(listener, struct weston_buffer,
2020 destroy_listener);
2021
2022 buffer = zalloc(sizeof *buffer);
2023 if (buffer == NULL)
2024 return NULL;
2025
2026 buffer->resource = resource;
2027 wl_signal_init(&buffer->destroy_signal);
2028 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04002029 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002030 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002031
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002032 return buffer;
2033}
2034
2035static void
Pekka Paalanende685b82012-12-04 15:58:12 +02002036weston_buffer_reference_handle_destroy(struct wl_listener *listener,
2037 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002038{
Pekka Paalanende685b82012-12-04 15:58:12 +02002039 struct weston_buffer_reference *ref =
2040 container_of(listener, struct weston_buffer_reference,
2041 destroy_listener);
2042
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002043 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02002044 ref->buffer = NULL;
2045}
2046
2047WL_EXPORT void
2048weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002049 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002050{
2051 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05002052 ref->buffer->busy_count--;
2053 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002054 assert(wl_resource_get_client(ref->buffer->resource));
Matt Hoosier3052bc72017-09-26 08:09:40 -05002055 wl_buffer_send_release(ref->buffer->resource);
Kristian Høgsberg20347802013-03-04 12:07:46 -05002056 }
Pekka Paalanende685b82012-12-04 15:58:12 +02002057 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002058 }
2059
Pekka Paalanende685b82012-12-04 15:58:12 +02002060 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002061 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002062 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02002063 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02002064 }
2065
Pekka Paalanende685b82012-12-04 15:58:12 +02002066 ref->buffer = buffer;
2067 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
2068}
2069
2070static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002071weston_surface_attach(struct weston_surface *surface,
2072 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002073{
2074 weston_buffer_reference(&surface->buffer_ref, buffer);
2075
Pekka Paalanena6421c42012-12-04 15:58:10 +02002076 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002077 if (weston_surface_is_mapped(surface))
2078 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002079 }
2080
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002081 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02002082
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002083 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002084 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002085}
2086
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002087WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002088weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002089{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002090 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002091
2092 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002093 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002094}
2095
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05002096WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002097weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002098{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002099 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002100
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002101 pixman_region32_union(&compositor->primary_plane.damage,
2102 &compositor->primary_plane.damage,
2103 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002104 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002105}
2106
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04002107static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002108surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002109{
Pekka Paalanende685b82012-12-04 15:58:12 +02002110 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002111 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002112 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002113
Pekka Paalanenb5026542014-11-12 15:09:24 +02002114 if (weston_timeline_enabled_ &&
2115 pixman_region32_not_empty(&surface->damage))
2116 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
2117 TLP_OUTPUT(surface->output), TLP_END);
2118
Jason Ekstrandef540082014-06-26 10:37:36 -07002119 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002120}
2121
2122static void
2123view_accumulate_damage(struct weston_view *view,
2124 pixman_region32_t *opaque)
2125{
2126 pixman_region32_t damage;
2127
2128 pixman_region32_init(&damage);
2129 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002130 pixman_box32_t *extents;
2131
Jason Ekstranda7af7042013-10-12 22:38:11 -05002132 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002133 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002134 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002135 pixman_region32_copy(&damage, &view->surface->damage);
2136 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002137 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002138 }
2139
Pekka Paalanen380adf52015-02-16 14:39:11 +02002140 pixman_region32_intersect(&damage, &damage,
2141 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002142 pixman_region32_subtract(&damage, &damage, opaque);
2143 pixman_region32_union(&view->plane->damage,
2144 &view->plane->damage, &damage);
2145 pixman_region32_fini(&damage);
2146 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002147 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002148}
2149
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002150static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002151compositor_accumulate_damage(struct weston_compositor *ec)
2152{
2153 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002154 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002155 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002156
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002157 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002158
2159 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002160 pixman_region32_copy(&plane->clip, &clip);
2161
2162 pixman_region32_init(&opaque);
2163
Jason Ekstranda7af7042013-10-12 22:38:11 -05002164 wl_list_for_each(ev, &ec->view_list, link) {
2165 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002166 continue;
2167
Jason Ekstranda7af7042013-10-12 22:38:11 -05002168 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002169 }
2170
2171 pixman_region32_union(&clip, &clip, &opaque);
2172 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002173 }
2174
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002175 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002176
Jason Ekstranda7af7042013-10-12 22:38:11 -05002177 wl_list_for_each(ev, &ec->view_list, link)
Derek Foreman060cf112015-11-18 16:32:26 -06002178 ev->surface->touched = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002179
2180 wl_list_for_each(ev, &ec->view_list, link) {
2181 if (ev->surface->touched)
2182 continue;
Derek Foreman060cf112015-11-18 16:32:26 -06002183 ev->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002184
2185 surface_flush_damage(ev->surface);
2186
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002187 /* Both the renderer and the backend have seen the buffer
2188 * by now. If renderer needs the buffer, it has its own
2189 * reference set. If the backend wants to keep the buffer
2190 * around for migrating the surface into a non-primary plane
2191 * later, keep_buffer is true. Otherwise, drop the core
2192 * reference now, and allow early buffer release. This enables
2193 * clients to use single-buffering.
2194 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002195 if (!ev->surface->keep_buffer)
2196 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002197 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002198}
2199
2200static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002201surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002202{
2203 struct weston_subsurface *sub;
2204
Pekka Paalanene67858b2013-04-25 13:57:42 +03002205 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002206 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002207 continue;
2208
Jason Ekstranda7af7042013-10-12 22:38:11 -05002209 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2210 wl_list_init(&sub->surface->views);
2211
2212 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002213 }
2214}
2215
2216static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002217surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002218{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002219 struct weston_subsurface *sub;
2220 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002221
Jason Ekstranda7af7042013-10-12 22:38:11 -05002222 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2223 if (sub->surface == surface)
2224 continue;
2225
George Kiagiadakised04d382014-06-13 18:10:26 +02002226 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2227 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002228 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002229 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002230
2231 surface_free_unused_subsurface_views(sub->surface);
2232 }
2233}
2234
2235static void
2236view_list_add_subsurface_view(struct weston_compositor *compositor,
2237 struct weston_subsurface *sub,
2238 struct weston_view *parent)
2239{
2240 struct weston_subsurface *child;
2241 struct weston_view *view = NULL, *iv;
2242
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002243 if (!weston_surface_is_mapped(sub->surface))
2244 return;
2245
Jason Ekstranda7af7042013-10-12 22:38:11 -05002246 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2247 if (iv->geometry.parent == parent) {
2248 view = iv;
2249 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002250 }
2251 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002252
2253 if (view) {
2254 /* Put it back in the surface's list of views */
2255 wl_list_remove(&view->surface_link);
2256 wl_list_insert(&sub->surface->views, &view->surface_link);
2257 } else {
2258 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002259 weston_view_set_position(view,
2260 sub->position.x,
2261 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002262 weston_view_set_transform_parent(view, parent);
2263 }
2264
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002265 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002266 weston_view_update_transform(view);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002267 view->is_mapped = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002268
Pekka Paalanenb188e912013-11-19 14:03:35 +02002269 if (wl_list_empty(&sub->surface->subsurface_list)) {
2270 wl_list_insert(compositor->view_list.prev, &view->link);
2271 return;
2272 }
2273
2274 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2275 if (child->surface == sub->surface)
2276 wl_list_insert(compositor->view_list.prev, &view->link);
2277 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002278 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002279 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002280}
2281
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002282/* This recursively adds the sub-surfaces for a view, relying on the
2283 * sub-surface order. Thus, if a client restacks the sub-surfaces, that
2284 * change first happens to the sub-surface list, and then automatically
2285 * propagates here. See weston_surface_damage_subsurfaces() for how the
2286 * sub-surfaces receive damage when the client changes the state.
2287 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002288static void
2289view_list_add(struct weston_compositor *compositor,
2290 struct weston_view *view)
2291{
2292 struct weston_subsurface *sub;
2293
2294 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002295
Pekka Paalanenb188e912013-11-19 14:03:35 +02002296 if (wl_list_empty(&view->surface->subsurface_list)) {
2297 wl_list_insert(compositor->view_list.prev, &view->link);
2298 return;
2299 }
2300
2301 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2302 if (sub->surface == view->surface)
2303 wl_list_insert(compositor->view_list.prev, &view->link);
2304 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002305 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002306 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002307}
2308
2309static void
2310weston_compositor_build_view_list(struct weston_compositor *compositor)
2311{
2312 struct weston_view *view;
2313 struct weston_layer *layer;
2314
2315 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002316 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002317 surface_stash_subsurface_views(view->surface);
2318
2319 wl_list_init(&compositor->view_list);
2320 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002321 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002322 view_list_add(compositor, view);
2323 }
2324 }
2325
2326 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002327 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002328 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002329}
2330
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002331static void
2332weston_output_take_feedback_list(struct weston_output *output,
2333 struct weston_surface *surface)
2334{
2335 struct weston_view *view;
2336 struct weston_presentation_feedback *feedback;
2337 uint32_t flags = 0xffffffff;
2338
2339 if (wl_list_empty(&surface->feedback_list))
2340 return;
2341
2342 /* All views must have the flag for the flag to survive. */
2343 wl_list_for_each(view, &surface->views, surface_link) {
2344 /* ignore views that are not on this output at all */
2345 if (view->output_mask & (1u << output->id))
2346 flags &= view->psf_flags;
2347 }
2348
2349 wl_list_for_each(feedback, &surface->feedback_list, link)
2350 feedback->psf_flags = flags;
2351
2352 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2353 wl_list_init(&surface->feedback_list);
2354}
2355
David Herrmann1edf44c2013-10-22 17:11:26 +02002356static int
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002357weston_output_repaint(struct weston_output *output, void *repaint_data)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002358{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002359 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002360 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002361 struct weston_animation *animation, *next;
2362 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002363 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002364 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002365 int r;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002366 uint32_t frame_time_msec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002367
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002368 if (output->destroying)
2369 return 0;
2370
Pekka Paalanenb5026542014-11-12 15:09:24 +02002371 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2372
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002373 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002374 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002375
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002376 if (output->assign_planes && !output->disable_planes) {
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002377 output->assign_planes(output, repaint_data);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002378 } else {
2379 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002380 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002381 ev->psf_flags = 0;
2382 }
2383 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002384
Pekka Paalanene67858b2013-04-25 13:57:42 +03002385 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002386 wl_list_for_each(ev, &ec->view_list, link) {
2387 /* Note: This operation is safe to do multiple times on the
2388 * same surface.
2389 */
2390 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002391 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002392 &ev->surface->frame_callback_list);
2393 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002394
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002395 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002396 }
2397 }
2398
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002399 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002400
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002401 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002402 pixman_region32_intersect(&output_damage,
2403 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002404 pixman_region32_subtract(&output_damage,
2405 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002406
Scott Moreauccbf29d2012-02-22 14:21:41 -07002407 if (output->dirty)
2408 weston_output_update_matrix(output);
2409
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002410 r = output->repaint(output, &output_damage, repaint_data);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002411
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002412 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002413
Daniel Stone09a97e22017-03-01 11:34:06 +00002414 output->repaint_needed = false;
Daniel Stone05df8c12017-03-03 16:59:42 +00002415 if (r == 0)
2416 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002417
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002418 weston_compositor_repick(ec);
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002419
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002420 frame_time_msec = timespec_to_msec(&output->frame_time);
2421
Jonas Ådahldb773762012-06-13 00:01:21 +02002422 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002423 wl_callback_send_done(cb->resource, frame_time_msec);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002424 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002425 }
2426
Scott Moreaud64cf212012-06-08 19:40:54 -06002427 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002428 animation->frame_counter++;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002429 animation->frame(animation, output, &output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002430 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002431
Pekka Paalanenb5026542014-11-12 15:09:24 +02002432 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2433
David Herrmann1edf44c2013-10-22 17:11:26 +02002434 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002435}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002436
Pekka Paalanen82919792014-05-21 13:51:49 +03002437static void
2438weston_output_schedule_repaint_reset(struct weston_output *output)
2439{
Daniel Stone05df8c12017-03-03 16:59:42 +00002440 output->repaint_status = REPAINT_NOT_SCHEDULED;
Pekka Paalanen82919792014-05-21 13:51:49 +03002441 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
Pekka Paalanen82919792014-05-21 13:51:49 +03002442}
2443
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002444static int
2445weston_output_maybe_repaint(struct weston_output *output, struct timespec *now,
2446 void *repaint_data)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002447{
Pekka Paalanen0513a952014-05-21 16:17:27 +03002448 struct weston_compositor *compositor = output->compositor;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002449 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002450 int64_t msec_to_repaint;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002451
Daniel Stone6847b852017-03-01 11:34:08 +00002452 /* We're not ready yet; come back to make a decision later. */
2453 if (output->repaint_status != REPAINT_SCHEDULED)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002454 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002455
2456 msec_to_repaint = timespec_sub_to_msec(&output->next_repaint, now);
2457 if (msec_to_repaint > 1)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002458 return ret;
Daniel Stone05df8c12017-03-03 16:59:42 +00002459
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002460 /* If we're sleeping, drop the repaint machinery entirely; we will
2461 * explicitly repaint all outputs when we come back. */
2462 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2463 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
2464 goto err;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002465
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002466 /* We don't actually need to repaint this output; drop it from
2467 * repaint until something causes damage. */
2468 if (!output->repaint_needed)
2469 goto err;
2470
2471 /* If repaint fails, we aren't going to get weston_output_finish_frame
2472 * to trigger a new repaint, so drop it from repaint and hope
Daniel Stone6847b852017-03-01 11:34:08 +00002473 * something schedules a successful repaint later. As repainting may
2474 * take some time, re-read our clock as a courtesy to the next
2475 * output. */
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002476 ret = weston_output_repaint(output, repaint_data);
Daniel Stone6847b852017-03-01 11:34:08 +00002477 weston_compositor_read_presentation_clock(compositor, now);
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002478 if (ret != 0)
2479 goto err;
2480
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002481 return ret;
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002482
2483err:
Pekka Paalanen0513a952014-05-21 16:17:27 +03002484 weston_output_schedule_repaint_reset(output);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002485 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002486}
2487
2488static void
2489output_repaint_timer_arm(struct weston_compositor *compositor)
2490{
2491 struct weston_output *output;
2492 bool any_should_repaint = false;
2493 struct timespec now;
Sergi Granellb4c08862017-03-18 13:01:15 +01002494 int64_t msec_to_next = INT64_MAX;
Daniel Stone6847b852017-03-01 11:34:08 +00002495
2496 weston_compositor_read_presentation_clock(compositor, &now);
2497
2498 wl_list_for_each(output, &compositor->output_list, link) {
2499 int64_t msec_to_this;
2500
2501 if (output->repaint_status != REPAINT_SCHEDULED)
2502 continue;
2503
2504 msec_to_this = timespec_sub_to_msec(&output->next_repaint,
2505 &now);
2506 if (!any_should_repaint || msec_to_this < msec_to_next)
2507 msec_to_next = msec_to_this;
2508
2509 any_should_repaint = true;
2510 }
2511
2512 if (!any_should_repaint)
2513 return;
2514
2515 /* Even if we should repaint immediately, add the minimum 1 ms delay.
2516 * This is a workaround to allow coalescing multiple output repaints
2517 * particularly from weston_output_finish_frame()
2518 * into the same call, which would not happen if we called
2519 * output_repaint_timer_handler() directly.
2520 */
2521 if (msec_to_next < 1)
2522 msec_to_next = 1;
2523
2524 wl_event_source_timer_update(compositor->repaint_timer, msec_to_next);
2525}
2526
2527static int
2528output_repaint_timer_handler(void *data)
2529{
2530 struct weston_compositor *compositor = data;
2531 struct weston_output *output;
2532 struct timespec now;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002533 void *repaint_data = NULL;
Emre Ucane479ed82018-03-20 15:29:40 +01002534 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002535
2536 weston_compositor_read_presentation_clock(compositor, &now);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002537
2538 if (compositor->backend->repaint_begin)
2539 repaint_data = compositor->backend->repaint_begin(compositor);
2540
2541 wl_list_for_each(output, &compositor->output_list, link) {
2542 ret = weston_output_maybe_repaint(output, &now, repaint_data);
2543 if (ret)
2544 break;
2545 }
2546
2547 if (ret == 0) {
2548 if (compositor->backend->repaint_flush)
2549 compositor->backend->repaint_flush(compositor,
2550 repaint_data);
2551 } else {
2552 if (compositor->backend->repaint_cancel)
2553 compositor->backend->repaint_cancel(compositor,
2554 repaint_data);
2555 }
Daniel Stone6847b852017-03-01 11:34:08 +00002556
2557 output_repaint_timer_arm(compositor);
2558
Pekka Paalanen0513a952014-05-21 16:17:27 +03002559 return 0;
2560}
2561
Kristian Høgsbergef044142011-06-21 15:02:12 -04002562WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002563weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002564 const struct timespec *stamp,
2565 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002566{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002567 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002568 int32_t refresh_nsec;
2569 struct timespec now;
Daniel Stone6847b852017-03-01 11:34:08 +00002570 int64_t msec_rel;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002571
Pekka Paalanenb5026542014-11-12 15:09:24 +02002572
Daniel Stone05df8c12017-03-03 16:59:42 +00002573 assert(output->repaint_status == REPAINT_AWAITING_COMPLETION);
Daniel Stone3615ce12017-03-01 11:34:05 +00002574 assert(stamp || (presented_flags & WP_PRESENTATION_FEEDBACK_INVALID));
2575
Daniel Stone6847b852017-03-01 11:34:08 +00002576 weston_compositor_read_presentation_clock(compositor, &now);
2577
Daniel Stone3615ce12017-03-01 11:34:05 +00002578 /* If we haven't been supplied any timestamp at all, we don't have a
2579 * timebase to work against, so any delay just wastes time. Push a
2580 * repaint as soon as possible so we can get on with it. */
Daniel Stone6847b852017-03-01 11:34:08 +00002581 if (!stamp) {
2582 output->next_repaint = now;
Daniel Stone3615ce12017-03-01 11:34:05 +00002583 goto out;
Daniel Stone6847b852017-03-01 11:34:08 +00002584 }
Daniel Stone3615ce12017-03-01 11:34:05 +00002585
Marius Vladdf9278a2018-03-06 18:56:23 +02002586 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2587 TLP_VBLANK(stamp), TLP_END);
2588
Pekka Paalanend7894d02015-07-03 15:08:53 +03002589 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002590 weston_presentation_feedback_present_list(&output->feedback_list,
2591 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002592 output->msc,
2593 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002594
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002595 output->frame_time = *stamp;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002596
Daniel Stone6847b852017-03-01 11:34:08 +00002597 timespec_add_nsec(&output->next_repaint, stamp, refresh_nsec);
2598 timespec_add_msec(&output->next_repaint, &output->next_repaint,
2599 -compositor->repaint_msec);
2600 msec_rel = timespec_sub_to_msec(&output->next_repaint, &now);
Daniel Stone84aff5c2017-03-01 11:34:04 +00002601
2602 if (msec_rel < -1000 || msec_rel > 1000) {
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002603 static bool warned;
2604
2605 if (!warned)
2606 weston_log("Warning: computed repaint delay is "
Daniel Stone6847b852017-03-01 11:34:08 +00002607 "insane: %lld msec\n", (long long) msec_rel);
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002608 warned = true;
2609
Daniel Stone6847b852017-03-01 11:34:08 +00002610 output->next_repaint = now;
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002611 }
2612
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002613 /* Called from restart_repaint_loop and restart happens already after
2614 * the deadline given by repaint_msec? In that case we delay until
2615 * the deadline of the next frame, to give clients a more predictable
2616 * timing of the repaint cycle to lock on. */
Daniel Stoneeca5cca2017-02-28 21:53:51 +00002617 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID &&
2618 msec_rel < 0) {
2619 while (timespec_sub_to_nsec(&output->next_repaint, &now) < 0) {
2620 timespec_add_nsec(&output->next_repaint,
2621 &output->next_repaint,
2622 refresh_nsec);
2623 }
2624 }
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002625
Daniel Stone3615ce12017-03-01 11:34:05 +00002626out:
Daniel Stone05df8c12017-03-03 16:59:42 +00002627 output->repaint_status = REPAINT_SCHEDULED;
Daniel Stone6847b852017-03-01 11:34:08 +00002628 output_repaint_timer_arm(compositor);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002629}
2630
2631static void
2632idle_repaint(void *data)
2633{
2634 struct weston_output *output = data;
2635
Daniel Stone05df8c12017-03-03 16:59:42 +00002636 assert(output->repaint_status == REPAINT_BEGIN_FROM_IDLE);
2637 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03002638 output->idle_repaint_source = NULL;
Jonas Ådahle5a12252013-04-05 23:07:11 +02002639 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002640}
2641
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002642WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002643weston_layer_entry_insert(struct weston_layer_entry *list,
2644 struct weston_layer_entry *entry)
2645{
2646 wl_list_insert(&list->link, &entry->link);
2647 entry->layer = list->layer;
2648}
2649
2650WL_EXPORT void
2651weston_layer_entry_remove(struct weston_layer_entry *entry)
2652{
2653 wl_list_remove(&entry->link);
2654 wl_list_init(&entry->link);
2655 entry->layer = NULL;
2656}
2657
Quentin Glidic82681572016-12-17 13:40:51 +01002658
2659/** Initialize the weston_layer struct.
2660 *
2661 * \param compositor The compositor instance
2662 * \param layer The layer to initialize
2663 */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002664WL_EXPORT void
Quentin Glidic82681572016-12-17 13:40:51 +01002665weston_layer_init(struct weston_layer *layer,
2666 struct weston_compositor *compositor)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002667{
Quentin Glidic82681572016-12-17 13:40:51 +01002668 layer->compositor = compositor;
2669 wl_list_init(&layer->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002670 wl_list_init(&layer->view_list.link);
2671 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002672 weston_layer_set_mask_infinite(layer);
Quentin Glidic82681572016-12-17 13:40:51 +01002673}
2674
2675/** Sets the position of the layer in the layer list. The layer will be placed
2676 * below any layer with the same position value, if any.
2677 * This function is safe to call if the layer is already on the list, but the
2678 * layer may be moved below other layers at the same position, if any.
2679 *
2680 * \param layer The layer to modify
2681 * \param position The position the layer will be placed at
2682 */
2683WL_EXPORT void
2684weston_layer_set_position(struct weston_layer *layer,
2685 enum weston_layer_position position)
2686{
2687 struct weston_layer *below;
2688
2689 wl_list_remove(&layer->link);
2690
2691 /* layer_list is ordered from top to bottom, the last layer being the
2692 * background with the smallest position value */
2693
2694 layer->position = position;
2695 wl_list_for_each_reverse(below, &layer->compositor->layer_list, link) {
2696 if (below->position >= layer->position) {
2697 wl_list_insert(&below->link, &layer->link);
2698 return;
2699 }
2700 }
2701 wl_list_insert(&layer->compositor->layer_list, &layer->link);
2702}
2703
2704/** Hide a layer by taking it off the layer list.
2705 * This function is safe to call if the layer is not on the list.
2706 *
2707 * \param layer The layer to hide
2708 */
2709WL_EXPORT void
2710weston_layer_unset_position(struct weston_layer *layer)
2711{
2712 wl_list_remove(&layer->link);
2713 wl_list_init(&layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002714}
2715
2716WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002717weston_layer_set_mask(struct weston_layer *layer,
2718 int x, int y, int width, int height)
2719{
2720 struct weston_view *view;
2721
2722 layer->mask.x1 = x;
2723 layer->mask.x2 = x + width;
2724 layer->mask.y1 = y;
2725 layer->mask.y2 = y + height;
2726
2727 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2728 weston_view_geometry_dirty(view);
2729 }
2730}
2731
2732WL_EXPORT void
2733weston_layer_set_mask_infinite(struct weston_layer *layer)
2734{
2735 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2736 UINT32_MAX, UINT32_MAX);
2737}
2738
2739WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002740weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002741{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002742 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002743 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002744
Bryce Harrington08976ac2016-08-30 12:05:16 -07002745 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2746 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002747 return;
2748
Pekka Paalanenb5026542014-11-12 15:09:24 +02002749 if (!output->repaint_needed)
2750 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2751
Kristian Høgsbergef044142011-06-21 15:02:12 -04002752 loop = wl_display_get_event_loop(compositor->wl_display);
Daniel Stone09a97e22017-03-01 11:34:06 +00002753 output->repaint_needed = true;
Daniel Stone05df8c12017-03-03 16:59:42 +00002754
2755 /* If we already have a repaint scheduled for our idle handler,
2756 * no need to set it again. If the repaint has been called but
2757 * not finished, then weston_output_finish_frame() will notice
2758 * that a repaint is needed and schedule one. */
2759 if (output->repaint_status != REPAINT_NOT_SCHEDULED)
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002760 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002761
Daniel Stone05df8c12017-03-03 16:59:42 +00002762 output->repaint_status = REPAINT_BEGIN_FROM_IDLE;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03002763 assert(!output->idle_repaint_source);
2764 output->idle_repaint_source = wl_event_loop_add_idle(loop, idle_repaint,
2765 output);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002766 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002767}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002768
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002769WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002770weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2771{
2772 struct weston_output *output;
2773
2774 wl_list_for_each(output, &compositor->output_list, link)
2775 weston_output_schedule_repaint(output);
2776}
2777
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002778static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002779surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002780{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002781 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002782}
2783
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002784static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002785surface_attach(struct wl_client *client,
2786 struct wl_resource *resource,
2787 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2788{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002789 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002790 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002791
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002792 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002793 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002794 if (buffer == NULL) {
2795 wl_client_post_no_memory(client);
2796 return;
2797 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002798 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002799
Pekka Paalanende685b82012-12-04 15:58:12 +02002800 /* Attach, attach, without commit in between does not send
2801 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002802 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002803
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002804 surface->pending.sx = sx;
2805 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002806 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002807}
2808
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002809static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002810surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002811 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002812 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002813{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002814 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002815
Derek Foreman57e92ed2015-11-17 14:11:35 -06002816 if (width <= 0 || height <= 0)
2817 return;
2818
Derek Foreman152254b2015-11-26 14:17:48 -06002819 pixman_region32_union_rect(&surface->pending.damage_surface,
2820 &surface->pending.damage_surface,
2821 x, y, width, height);
2822}
2823
2824static void
2825surface_damage_buffer(struct wl_client *client,
2826 struct wl_resource *resource,
2827 int32_t x, int32_t y, int32_t width, int32_t height)
2828{
2829 struct weston_surface *surface = wl_resource_get_user_data(resource);
2830
2831 if (width <= 0 || height <= 0)
2832 return;
2833
2834 pixman_region32_union_rect(&surface->pending.damage_buffer,
2835 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002836 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002837}
2838
Kristian Høgsberg33418202011-08-16 23:01:28 -04002839static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002840destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002841{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002842 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002843
2844 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002845 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002846}
2847
2848static void
2849surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002850 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002851{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002852 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002853 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002854
2855 cb = malloc(sizeof *cb);
2856 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002857 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002858 return;
2859 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002860
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002861 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2862 callback);
2863 if (cb->resource == NULL) {
2864 free(cb);
2865 wl_resource_post_no_memory(resource);
2866 return;
2867 }
2868
Jason Ekstranda85118c2013-06-27 20:17:02 -05002869 wl_resource_set_implementation(cb->resource, NULL, cb,
2870 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002871
Pekka Paalanenbc106382012-10-10 12:49:31 +03002872 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002873}
2874
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002875static void
2876surface_set_opaque_region(struct wl_client *client,
2877 struct wl_resource *resource,
2878 struct wl_resource *region_resource)
2879{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002880 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002881 struct weston_region *region;
2882
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002883 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002884 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002885 pixman_region32_copy(&surface->pending.opaque,
2886 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002887 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002888 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002889 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002890}
2891
2892static void
2893surface_set_input_region(struct wl_client *client,
2894 struct wl_resource *resource,
2895 struct wl_resource *region_resource)
2896{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002897 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002898 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002899
2900 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002901 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002902 pixman_region32_copy(&surface->pending.input,
2903 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002904 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002905 pixman_region32_fini(&surface->pending.input);
2906 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002907 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002908}
2909
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002910/* Cause damage to this sub-surface and all its children.
2911 *
2912 * This is useful when there are state changes that need an implicit
2913 * damage, e.g. a z-order change.
2914 */
2915static void
2916weston_surface_damage_subsurfaces(struct weston_subsurface *sub)
2917{
2918 struct weston_subsurface *child;
2919
2920 weston_surface_damage(sub->surface);
2921 sub->reordered = false;
2922
2923 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link)
2924 if (child != sub)
2925 weston_surface_damage_subsurfaces(child);
2926}
2927
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002928static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002929weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002930{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002931 struct weston_subsurface *sub;
2932
2933 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2934 parent_link_pending) {
2935 wl_list_remove(&sub->parent_link);
2936 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002937
2938 if (sub->reordered)
2939 weston_surface_damage_subsurfaces(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002940 }
2941}
2942
2943static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03002944weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05002945 struct weston_matrix *matrix)
2946{
Pekka Paalanen04baea52016-04-26 15:50:58 +03002947 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05002948 double src_width, src_height, dest_width, dest_height;
2949
2950 weston_matrix_init(matrix);
2951
2952 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
2953 src_width = surface->width_from_buffer;
2954 src_height = surface->height_from_buffer;
2955 } else {
2956 src_width = wl_fixed_to_double(vp->buffer.src_width);
2957 src_height = wl_fixed_to_double(vp->buffer.src_height);
2958 }
2959
2960 if (vp->surface.width == -1) {
2961 dest_width = src_width;
2962 dest_height = src_height;
2963 } else {
2964 dest_width = vp->surface.width;
2965 dest_height = vp->surface.height;
2966 }
2967
2968 if (src_width != dest_width || src_height != dest_height)
2969 weston_matrix_scale(matrix,
2970 src_width / dest_width,
2971 src_height / dest_height, 1);
2972
2973 if (vp->buffer.src_width != wl_fixed_from_int(-1))
2974 weston_matrix_translate(matrix,
2975 wl_fixed_to_double(vp->buffer.src_x),
2976 wl_fixed_to_double(vp->buffer.src_y),
2977 0);
2978
2979 switch (vp->buffer.transform) {
2980 case WL_OUTPUT_TRANSFORM_FLIPPED:
2981 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2982 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2983 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2984 weston_matrix_scale(matrix, -1, 1, 1);
2985 weston_matrix_translate(matrix,
2986 surface->width_from_buffer, 0, 0);
2987 break;
2988 }
2989
2990 switch (vp->buffer.transform) {
2991 default:
2992 case WL_OUTPUT_TRANSFORM_NORMAL:
2993 case WL_OUTPUT_TRANSFORM_FLIPPED:
2994 break;
2995 case WL_OUTPUT_TRANSFORM_90:
2996 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2997 weston_matrix_rotate_xy(matrix, 0, 1);
2998 weston_matrix_translate(matrix,
2999 surface->height_from_buffer, 0, 0);
3000 break;
3001 case WL_OUTPUT_TRANSFORM_180:
3002 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3003 weston_matrix_rotate_xy(matrix, -1, 0);
3004 weston_matrix_translate(matrix,
3005 surface->width_from_buffer,
3006 surface->height_from_buffer, 0);
3007 break;
3008 case WL_OUTPUT_TRANSFORM_270:
3009 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3010 weston_matrix_rotate_xy(matrix, 0, -1);
3011 weston_matrix_translate(matrix,
3012 0, surface->width_from_buffer, 0);
3013 break;
3014 }
3015
3016 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
3017}
3018
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003019/**
3020 * Compute a + b > c while being safe to overflows.
3021 */
3022static bool
3023fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
3024{
3025 return (int64_t)a + (int64_t)b > (int64_t)c;
3026}
3027
3028static bool
3029weston_surface_is_pending_viewport_source_valid(
3030 const struct weston_surface *surface)
3031{
3032 const struct weston_surface_state *pend = &surface->pending;
3033 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
3034 int width_from_buffer = 0;
3035 int height_from_buffer = 0;
3036 wl_fixed_t w;
3037 wl_fixed_t h;
3038
3039 /* If viewport source rect is not set, it is always ok. */
3040 if (vp->buffer.src_width == wl_fixed_from_int(-1))
3041 return true;
3042
3043 if (pend->newly_attached) {
3044 if (pend->buffer) {
3045 convert_size_by_transform_scale(&width_from_buffer,
3046 &height_from_buffer,
3047 pend->buffer->width,
3048 pend->buffer->height,
3049 vp->buffer.transform,
3050 vp->buffer.scale);
3051 } else {
3052 /* No buffer: viewport is irrelevant. */
3053 return true;
3054 }
3055 } else {
3056 width_from_buffer = surface->width_from_buffer;
3057 height_from_buffer = surface->height_from_buffer;
3058 }
3059
3060 assert((width_from_buffer == 0) == (height_from_buffer == 0));
3061 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
3062
3063 /* No buffer: viewport is irrelevant. */
3064 if (width_from_buffer == 0 || height_from_buffer == 0)
3065 return true;
3066
3067 /* overflow checks for wl_fixed_from_int() */
3068 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
3069 return false;
3070 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
3071 return false;
3072
3073 w = wl_fixed_from_int(width_from_buffer);
3074 h = wl_fixed_from_int(height_from_buffer);
3075
3076 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
3077 return false;
3078 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
3079 return false;
3080
3081 return true;
3082}
3083
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003084static bool
3085fixed_is_integer(wl_fixed_t v)
3086{
3087 return (v & 0xff) == 0;
3088}
3089
3090static bool
3091weston_surface_is_pending_viewport_dst_size_int(
3092 const struct weston_surface *surface)
3093{
3094 const struct weston_buffer_viewport *vp =
3095 &surface->pending.buffer_viewport;
3096
3097 if (vp->surface.width != -1) {
3098 assert(vp->surface.width > 0 && vp->surface.height > 0);
3099 return true;
3100 }
3101
3102 return fixed_is_integer(vp->buffer.src_width) &&
3103 fixed_is_integer(vp->buffer.src_height);
3104}
3105
Derek Foreman152254b2015-11-26 14:17:48 -06003106/* Translate pending damage in buffer co-ordinates to surface
3107 * co-ordinates and union it with a pixman_region32_t.
3108 * This should only be called after the buffer is attached.
3109 */
3110static void
3111apply_damage_buffer(pixman_region32_t *dest,
3112 struct weston_surface *surface,
3113 struct weston_surface_state *state)
3114{
3115 struct weston_buffer *buffer = surface->buffer_ref.buffer;
3116
3117 /* wl_surface.damage_buffer needs to be clipped to the buffer,
3118 * translated into surface co-ordinates and unioned with
3119 * any other surface damage.
3120 * None of this makes sense if there is no buffer though.
3121 */
3122 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
3123 pixman_region32_t buffer_damage;
3124
3125 pixman_region32_intersect_rect(&state->damage_buffer,
3126 &state->damage_buffer,
3127 0, 0, buffer->width,
3128 buffer->height);
3129 pixman_region32_init(&buffer_damage);
3130 weston_matrix_transform_region(&buffer_damage,
3131 &surface->buffer_to_surface_matrix,
3132 &state->damage_buffer);
3133 pixman_region32_union(dest, dest, &buffer_damage);
3134 pixman_region32_fini(&buffer_damage);
3135 }
3136 /* We should clear this on commit even if there was no buffer */
3137 pixman_region32_clear(&state->damage_buffer);
3138}
3139
Jason Ekstrand1e059042014-10-16 10:55:19 -05003140static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05003141weston_surface_commit_state(struct weston_surface *surface,
3142 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003143{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003144 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003145 pixman_region32_t opaque;
3146
Alexander Larsson4ea95522013-05-22 14:41:37 +02003147 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02003148 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03003149 /* wp_viewport.set_source */
3150 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003151 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003152
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003153 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003154 if (state->newly_attached)
3155 weston_surface_attach(surface, state->buffer);
3156 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01003157
Jason Ekstrand1e059042014-10-16 10:55:19 -05003158 weston_surface_build_buffer_matrix(surface,
3159 &surface->surface_to_buffer_matrix);
3160 weston_matrix_invert(&surface->buffer_to_surface_matrix,
3161 &surface->surface_to_buffer_matrix);
3162
Jason Ekstrand7b982072014-05-20 14:33:03 -05003163 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003164 weston_surface_update_size(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003165 if (surface->committed)
3166 surface->committed(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003167 }
Giulio Camuffo184df502013-02-21 11:29:21 +01003168
Jason Ekstrand7b982072014-05-20 14:33:03 -05003169 state->sx = 0;
3170 state->sy = 0;
3171 state->newly_attached = 0;
3172 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03003173
Derek Foreman152254b2015-11-26 14:17:48 -06003174 /* wl_surface.damage and wl_surface.damage_buffer */
Pekka Paalanenb5026542014-11-12 15:09:24 +02003175 if (weston_timeline_enabled_ &&
Derek Foreman152254b2015-11-26 14:17:48 -06003176 (pixman_region32_not_empty(&state->damage_surface) ||
3177 pixman_region32_not_empty(&state->damage_buffer)))
Pekka Paalanenb5026542014-11-12 15:09:24 +02003178 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06003179
Pekka Paalanen8e159182012-10-10 12:49:25 +03003180 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06003181 &state->damage_surface);
3182
3183 apply_damage_buffer(&surface->damage, surface, state);
3184
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05003185 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07003186 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06003187 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003188
3189 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003190 pixman_region32_init(&opaque);
3191 pixman_region32_intersect_rect(&opaque, &state->opaque,
3192 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003193
3194 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
3195 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003196 wl_list_for_each(view, &surface->views, surface_link)
3197 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003198 }
3199
3200 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003201
3202 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003203 pixman_region32_intersect_rect(&surface->input, &state->input,
3204 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003205
Pekka Paalanenbc106382012-10-10 12:49:31 +03003206 /* wl_surface.frame */
3207 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05003208 &state->frame_callback_list);
3209 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003210
3211 /* XXX:
3212 * What should happen with a feedback request, if there
3213 * is no wl_buffer attached for this commit?
3214 */
3215
3216 /* presentation.feedback */
3217 wl_list_insert_list(&surface->feedback_list,
3218 &state->feedback_list);
3219 wl_list_init(&state->feedback_list);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003220
3221 wl_signal_emit(&surface->commit_signal, surface);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003222}
3223
3224static void
3225weston_surface_commit(struct weston_surface *surface)
3226{
3227 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003228
Pekka Paalanene67858b2013-04-25 13:57:42 +03003229 weston_surface_commit_subsurface_order(surface);
3230
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003231 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003232}
3233
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003234static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003235weston_subsurface_commit(struct weston_subsurface *sub);
3236
3237static void
3238weston_subsurface_parent_commit(struct weston_subsurface *sub,
3239 int parent_is_synchronized);
3240
3241static void
3242surface_commit(struct wl_client *client, struct wl_resource *resource)
3243{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003244 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003245 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
3246
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003247 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
3248 assert(surface->viewport_resource);
3249
3250 wl_resource_post_error(surface->viewport_resource,
3251 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
3252 "wl_surface@%d has viewport source outside buffer",
3253 wl_resource_get_id(resource));
3254 return;
3255 }
3256
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003257 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
3258 assert(surface->viewport_resource);
3259
3260 wl_resource_post_error(surface->viewport_resource,
3261 WP_VIEWPORT_ERROR_BAD_SIZE,
3262 "wl_surface@%d viewport dst size not integer",
3263 wl_resource_get_id(resource));
3264 return;
3265 }
3266
Pekka Paalanene67858b2013-04-25 13:57:42 +03003267 if (sub) {
3268 weston_subsurface_commit(sub);
3269 return;
3270 }
3271
3272 weston_surface_commit(surface);
3273
3274 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
3275 if (sub->surface != surface)
3276 weston_subsurface_parent_commit(sub, 0);
3277 }
3278}
3279
3280static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003281surface_set_buffer_transform(struct wl_client *client,
3282 struct wl_resource *resource, int transform)
3283{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003284 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003285
Jonny Lamba55f1392014-05-30 12:07:15 +02003286 /* if wl_output.transform grows more members this will need to be updated. */
3287 if (transform < 0 ||
3288 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
3289 wl_resource_post_error(resource,
3290 WL_SURFACE_ERROR_INVALID_TRANSFORM,
3291 "buffer transform must be a valid transform "
3292 "('%d' specified)", transform);
3293 return;
3294 }
3295
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003296 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003297 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003298}
3299
Alexander Larsson4ea95522013-05-22 14:41:37 +02003300static void
3301surface_set_buffer_scale(struct wl_client *client,
3302 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003303 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003304{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003305 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003306
Jonny Lamba55f1392014-05-30 12:07:15 +02003307 if (scale < 1) {
3308 wl_resource_post_error(resource,
3309 WL_SURFACE_ERROR_INVALID_SCALE,
3310 "buffer scale must be at least one "
3311 "('%d' specified)", scale);
3312 return;
3313 }
3314
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003315 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003316 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003317}
3318
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003319static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003320 surface_destroy,
3321 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04003322 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003323 surface_frame,
3324 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003325 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003326 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02003327 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06003328 surface_set_buffer_scale,
3329 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003330};
3331
3332static void
3333compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003334 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003335{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04003336 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003337 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003338
Kristian Høgsberg18c93002012-01-27 11:58:31 -05003339 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003340 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003341 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003342 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003343 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003344
Jason Ekstranda85118c2013-06-27 20:17:02 -05003345 surface->resource =
3346 wl_resource_create(client, &wl_surface_interface,
3347 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003348 if (surface->resource == NULL) {
3349 weston_surface_destroy(surface);
3350 wl_resource_post_no_memory(resource);
3351 return;
3352 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003353 wl_resource_set_implementation(surface->resource, &surface_interface,
3354 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003355
3356 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003357}
3358
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003359static void
3360destroy_region(struct wl_resource *resource)
3361{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003362 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003363
3364 pixman_region32_fini(&region->region);
3365 free(region);
3366}
3367
3368static void
3369region_destroy(struct wl_client *client, struct wl_resource *resource)
3370{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003371 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003372}
3373
3374static void
3375region_add(struct wl_client *client, struct wl_resource *resource,
3376 int32_t x, int32_t y, int32_t width, int32_t height)
3377{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003378 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003379
3380 pixman_region32_union_rect(&region->region, &region->region,
3381 x, y, width, height);
3382}
3383
3384static void
3385region_subtract(struct wl_client *client, struct wl_resource *resource,
3386 int32_t x, int32_t y, int32_t width, int32_t height)
3387{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003388 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003389 pixman_region32_t rect;
3390
3391 pixman_region32_init_rect(&rect, x, y, width, height);
3392 pixman_region32_subtract(&region->region, &region->region, &rect);
3393 pixman_region32_fini(&rect);
3394}
3395
3396static const struct wl_region_interface region_interface = {
3397 region_destroy,
3398 region_add,
3399 region_subtract
3400};
3401
3402static void
3403compositor_create_region(struct wl_client *client,
3404 struct wl_resource *resource, uint32_t id)
3405{
3406 struct weston_region *region;
3407
3408 region = malloc(sizeof *region);
3409 if (region == NULL) {
3410 wl_resource_post_no_memory(resource);
3411 return;
3412 }
3413
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003414 pixman_region32_init(&region->region);
3415
Jason Ekstranda85118c2013-06-27 20:17:02 -05003416 region->resource =
3417 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003418 if (region->resource == NULL) {
3419 free(region);
3420 wl_resource_post_no_memory(resource);
3421 return;
3422 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003423 wl_resource_set_implementation(region->resource, &region_interface,
3424 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003425}
3426
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003427static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003428 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003429 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003430};
3431
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003432static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003433weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3434{
3435 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003436
Jason Ekstrand7b982072014-05-20 14:33:03 -05003437 weston_surface_commit_state(surface, &sub->cached);
3438 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003439
3440 weston_surface_commit_subsurface_order(surface);
3441
3442 weston_surface_schedule_repaint(surface);
3443
Jason Ekstrand7b982072014-05-20 14:33:03 -05003444 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003445}
3446
3447static void
3448weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3449{
3450 struct weston_surface *surface = sub->surface;
3451
3452 /*
3453 * If this commit would cause the surface to move by the
3454 * attach(dx, dy) parameters, the old damage region must be
3455 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05003456 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003457 */
Derek Foreman152254b2015-11-26 14:17:48 -06003458 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003459 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06003460 pixman_region32_union(&sub->cached.damage_surface,
3461 &sub->cached.damage_surface,
3462 &surface->pending.damage_surface);
3463 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003464
3465 if (surface->pending.newly_attached) {
3466 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003467 weston_surface_state_set_buffer(&sub->cached,
3468 surface->pending.buffer);
3469 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003470 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003471 weston_presentation_feedback_discard_list(
3472 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003473 }
3474 sub->cached.sx += surface->pending.sx;
3475 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003476
Derek Foreman152254b2015-11-26 14:17:48 -06003477 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
3478
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003479 sub->cached.buffer_viewport.changed |=
3480 surface->pending.buffer_viewport.changed;
3481 sub->cached.buffer_viewport.buffer =
3482 surface->pending.buffer_viewport.buffer;
3483 sub->cached.buffer_viewport.surface =
3484 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003485
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003486 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003487
3488 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3489
3490 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3491
3492 wl_list_insert_list(&sub->cached.frame_callback_list,
3493 &surface->pending.frame_callback_list);
3494 wl_list_init(&surface->pending.frame_callback_list);
3495
Pekka Paalanen133e4392014-09-23 22:08:46 -04003496 wl_list_insert_list(&sub->cached.feedback_list,
3497 &surface->pending.feedback_list);
3498 wl_list_init(&surface->pending.feedback_list);
3499
Jason Ekstrand7b982072014-05-20 14:33:03 -05003500 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003501}
3502
Derek Foreman280e7dd2014-10-03 13:13:42 -05003503static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003504weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3505{
3506 while (sub) {
3507 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003508 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003509
3510 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003511 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003512
3513 sub = weston_surface_to_subsurface(sub->parent);
3514 }
3515
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003516 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003517}
3518
3519static void
3520weston_subsurface_commit(struct weston_subsurface *sub)
3521{
3522 struct weston_surface *surface = sub->surface;
3523 struct weston_subsurface *tmp;
3524
3525 /* Recursive check for effectively synchronized. */
3526 if (weston_subsurface_is_synchronized(sub)) {
3527 weston_subsurface_commit_to_cache(sub);
3528 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003529 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003530 /* flush accumulated state from cache */
3531 weston_subsurface_commit_to_cache(sub);
3532 weston_subsurface_commit_from_cache(sub);
3533 } else {
3534 weston_surface_commit(surface);
3535 }
3536
3537 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3538 if (tmp->surface != surface)
3539 weston_subsurface_parent_commit(tmp, 0);
3540 }
3541 }
3542}
3543
3544static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003545weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003546{
3547 struct weston_surface *surface = sub->surface;
3548 struct weston_subsurface *tmp;
3549
Pekka Paalanene67858b2013-04-25 13:57:42 +03003550 /* From now on, commit_from_cache the whole sub-tree, regardless of
3551 * the synchronized mode of each child. This sub-surface or some
3552 * of its ancestors were synchronized, so we are synchronized
3553 * all the way down.
3554 */
3555
Jason Ekstrand7b982072014-05-20 14:33:03 -05003556 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003557 weston_subsurface_commit_from_cache(sub);
3558
3559 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3560 if (tmp->surface != surface)
3561 weston_subsurface_parent_commit(tmp, 1);
3562 }
3563}
3564
3565static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003566weston_subsurface_parent_commit(struct weston_subsurface *sub,
3567 int parent_is_synchronized)
3568{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003569 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003570 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003571 wl_list_for_each(view, &sub->surface->views, surface_link)
3572 weston_view_set_position(view,
3573 sub->position.x,
3574 sub->position.y);
3575
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003576 sub->position.set = 0;
3577 }
3578
3579 if (parent_is_synchronized || sub->synchronized)
3580 weston_subsurface_synchronized_commit(sub);
3581}
3582
Pekka Paalanen8274d902014-08-06 19:36:51 +03003583static int
3584subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3585{
3586 return snprintf(buf, len, "sub-surface");
3587}
3588
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003589static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003590subsurface_committed(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003591{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003592 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003593
Jason Ekstranda7af7042013-10-12 22:38:11 -05003594 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003595 weston_view_set_position(view,
3596 view->geometry.x + dx,
3597 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003598
3599 /* No need to check parent mappedness, because if parent is not
3600 * mapped, parent is not in a visible layer, so this sub-surface
3601 * will not be drawn either.
3602 */
Armin Krezovićf8486c32016-06-30 06:04:28 +02003603
Pekka Paalanene67858b2013-04-25 13:57:42 +03003604 if (!weston_surface_is_mapped(surface)) {
Armin Krezovićf8486c32016-06-30 06:04:28 +02003605 surface->is_mapped = true;
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003606
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003607 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003608 * because that would call it also for the parent surface,
3609 * which might not be mapped yet. That would lead to
3610 * inconsistent state, where the window could never be
3611 * mapped.
3612 *
Armin Krezovićf8486c32016-06-30 06:04:28 +02003613 * Instead just force the is_mapped flag on, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003614 * weston_surface_is_mapped() return true, so that when the
3615 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003616 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003617 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03003618 }
3619}
3620
3621static struct weston_subsurface *
3622weston_surface_to_subsurface(struct weston_surface *surface)
3623{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003624 if (surface->committed == subsurface_committed)
3625 return surface->committed_private;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003626
3627 return NULL;
3628}
3629
Pekka Paalanen01388e22013-04-25 13:57:44 +03003630WL_EXPORT struct weston_surface *
3631weston_surface_get_main_surface(struct weston_surface *surface)
3632{
3633 struct weston_subsurface *sub;
3634
3635 while (surface && (sub = weston_surface_to_subsurface(surface)))
3636 surface = sub->parent;
3637
3638 return surface;
3639}
3640
Pekka Paalanen50b67472014-10-01 15:02:41 +03003641WL_EXPORT int
3642weston_surface_set_role(struct weston_surface *surface,
3643 const char *role_name,
3644 struct wl_resource *error_resource,
3645 uint32_t error_code)
3646{
3647 assert(role_name);
3648
3649 if (surface->role_name == NULL ||
3650 surface->role_name == role_name ||
3651 strcmp(surface->role_name, role_name) == 0) {
3652 surface->role_name = role_name;
3653
3654 return 0;
3655 }
3656
3657 wl_resource_post_error(error_resource, error_code,
3658 "Cannot assign role %s to wl_surface@%d,"
3659 " already has role %s\n",
3660 role_name,
3661 wl_resource_get_id(surface->resource),
3662 surface->role_name);
3663 return -1;
3664}
3665
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02003666WL_EXPORT const char *
3667weston_surface_get_role(struct weston_surface *surface)
3668{
3669 return surface->role_name;
3670}
3671
Pekka Paalanen8274d902014-08-06 19:36:51 +03003672WL_EXPORT void
3673weston_surface_set_label_func(struct weston_surface *surface,
3674 int (*desc)(struct weston_surface *,
3675 char *, size_t))
3676{
3677 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02003678 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003679}
3680
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003681/** Get the size of surface contents
3682 *
3683 * \param surface The surface to query.
3684 * \param width Returns the width of raw contents.
3685 * \param height Returns the height of raw contents.
3686 *
3687 * Retrieves the raw surface content size in pixels for the given surface.
3688 * This is the whole content size in buffer pixels. If the surface
3689 * has no content or the renderer does not implement this feature,
3690 * zeroes are returned.
3691 *
3692 * This function is used to determine the buffer size needed for
3693 * a weston_surface_copy_content() call.
3694 */
3695WL_EXPORT void
3696weston_surface_get_content_size(struct weston_surface *surface,
3697 int *width, int *height)
3698{
3699 struct weston_renderer *rer = surface->compositor->renderer;
3700
3701 if (!rer->surface_get_content_size) {
3702 *width = 0;
3703 *height = 0;
3704 return;
3705 }
3706
3707 rer->surface_get_content_size(surface, width, height);
3708}
3709
Quentin Glidic248dd102016-08-12 10:41:34 +02003710/** Get the bounding box of a surface and its subsurfaces
3711 *
3712 * \param surface The surface to query.
3713 * \return The bounding box relative to the surface origin.
3714 *
3715 */
3716WL_EXPORT struct weston_geometry
3717weston_surface_get_bounding_box(struct weston_surface *surface)
3718{
3719 pixman_region32_t region;
3720 pixman_box32_t *box;
3721 struct weston_subsurface *subsurface;
3722
3723 pixman_region32_init_rect(&region,
3724 0, 0,
3725 surface->width, surface->height);
3726
3727 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link)
3728 pixman_region32_union_rect(&region, &region,
3729 subsurface->position.x,
3730 subsurface->position.y,
3731 subsurface->surface->width,
3732 subsurface->surface->height);
3733
3734 box = pixman_region32_extents(&region);
3735 struct weston_geometry geometry = {
3736 .x = box->x1,
3737 .y = box->y1,
3738 .width = box->x2 - box->x1,
3739 .height = box->y2 - box->y1,
3740 };
3741
3742 pixman_region32_fini(&region);
3743
3744 return geometry;
3745}
3746
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003747/** Copy surface contents to system memory.
3748 *
3749 * \param surface The surface to copy from.
3750 * \param target Pointer to the target memory buffer.
3751 * \param size Size of the target buffer in bytes.
3752 * \param src_x X location on contents to copy from.
3753 * \param src_y Y location on contents to copy from.
3754 * \param width Width in pixels of the area to copy.
3755 * \param height Height in pixels of the area to copy.
3756 * \return 0 for success, -1 for failure.
3757 *
3758 * Surface contents are maintained by the renderer. They can be in a
3759 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
3760 * else.
3761 *
3762 * Surface contents are copied into memory pointed to by target,
3763 * which has size bytes of space available. The target memory
3764 * may be larger than needed, but being smaller returns an error.
3765 * The extra bytes in target may or may not be written; their content is
3766 * unspecified. Size must be large enough to hold the image.
3767 *
3768 * The image in the target memory will be arranged in rows from
3769 * top to bottom, and pixels on a row from left to right. The pixel
3770 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
3771 * width * 4.
3772 *
3773 * Parameters src_x and src_y define the upper-left corner in buffer
3774 * coordinates (pixels) to copy from. Parameters width and height
3775 * define the size of the area to copy in pixels.
3776 *
3777 * The rectangle defined by src_x, src_y, width, height must fit in
3778 * the surface contents. Otherwise an error is returned.
3779 *
3780 * Use surface_get_data_size to determine the content size; the
3781 * needed target buffer size and rectangle limits.
3782 *
3783 * CURRENT IMPLEMENTATION RESTRICTIONS:
3784 * - the machine must be little-endian due to Pixman formats.
3785 *
3786 * NOTE: Pixman formats are premultiplied.
3787 */
3788WL_EXPORT int
3789weston_surface_copy_content(struct weston_surface *surface,
3790 void *target, size_t size,
3791 int src_x, int src_y,
3792 int width, int height)
3793{
3794 struct weston_renderer *rer = surface->compositor->renderer;
3795 int cw, ch;
3796 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
3797
3798 if (!rer->surface_copy_content)
3799 return -1;
3800
3801 weston_surface_get_content_size(surface, &cw, &ch);
3802
3803 if (src_x < 0 || src_y < 0)
3804 return -1;
3805
3806 if (width <= 0 || height <= 0)
3807 return -1;
3808
3809 if (src_x + width > cw || src_y + height > ch)
3810 return -1;
3811
3812 if (width * bytespp * height > size)
3813 return -1;
3814
3815 return rer->surface_copy_content(surface, target, size,
3816 src_x, src_y, width, height);
3817}
3818
Pekka Paalanene67858b2013-04-25 13:57:42 +03003819static void
3820subsurface_set_position(struct wl_client *client,
3821 struct wl_resource *resource, int32_t x, int32_t y)
3822{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003823 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003824
3825 if (!sub)
3826 return;
3827
3828 sub->position.x = x;
3829 sub->position.y = y;
3830 sub->position.set = 1;
3831}
3832
3833static struct weston_subsurface *
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003834subsurface_find_sibling(struct weston_subsurface *sub,
3835 struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003836{
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003837 struct weston_surface *parent = sub->parent;
3838 struct weston_subsurface *sibling;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003839
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003840 wl_list_for_each(sibling, &parent->subsurface_list, parent_link) {
3841 if (sibling->surface == surface && sibling != sub)
3842 return sibling;
3843 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003844
3845 return NULL;
3846}
3847
3848static struct weston_subsurface *
3849subsurface_sibling_check(struct weston_subsurface *sub,
3850 struct weston_surface *surface,
3851 const char *request)
3852{
3853 struct weston_subsurface *sibling;
3854
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003855 sibling = subsurface_find_sibling(sub, surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003856 if (!sibling) {
3857 wl_resource_post_error(sub->resource,
3858 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3859 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003860 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003861 return NULL;
3862 }
3863
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003864 assert(sibling->parent == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003865
3866 return sibling;
3867}
3868
3869static void
3870subsurface_place_above(struct wl_client *client,
3871 struct wl_resource *resource,
3872 struct wl_resource *sibling_resource)
3873{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003874 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003875 struct weston_surface *surface =
3876 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003877 struct weston_subsurface *sibling;
3878
3879 if (!sub)
3880 return;
3881
3882 sibling = subsurface_sibling_check(sub, surface, "place_above");
3883 if (!sibling)
3884 return;
3885
3886 wl_list_remove(&sub->parent_link_pending);
3887 wl_list_insert(sibling->parent_link_pending.prev,
3888 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003889
3890 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003891}
3892
3893static void
3894subsurface_place_below(struct wl_client *client,
3895 struct wl_resource *resource,
3896 struct wl_resource *sibling_resource)
3897{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003898 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003899 struct weston_surface *surface =
3900 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003901 struct weston_subsurface *sibling;
3902
3903 if (!sub)
3904 return;
3905
3906 sibling = subsurface_sibling_check(sub, surface, "place_below");
3907 if (!sibling)
3908 return;
3909
3910 wl_list_remove(&sub->parent_link_pending);
3911 wl_list_insert(&sibling->parent_link_pending,
3912 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003913
3914 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003915}
3916
3917static void
3918subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
3919{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003920 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003921
3922 if (sub)
3923 sub->synchronized = 1;
3924}
3925
3926static void
3927subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
3928{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003929 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003930
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003931 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003932 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003933
3934 /* If sub became effectively desynchronized, flush. */
3935 if (!weston_subsurface_is_synchronized(sub))
3936 weston_subsurface_synchronized_commit(sub);
3937 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003938}
3939
3940static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003941weston_subsurface_unlink_parent(struct weston_subsurface *sub)
3942{
3943 wl_list_remove(&sub->parent_link);
3944 wl_list_remove(&sub->parent_link_pending);
3945 wl_list_remove(&sub->parent_destroy_listener.link);
3946 sub->parent = NULL;
3947}
3948
3949static void
3950weston_subsurface_destroy(struct weston_subsurface *sub);
3951
3952static void
3953subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
3954{
3955 struct weston_subsurface *sub =
3956 container_of(listener, struct weston_subsurface,
3957 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003958 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003959
3960 /* The protocol object (wl_resource) is left inert. */
3961 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003962 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003963
3964 weston_subsurface_destroy(sub);
3965}
3966
3967static void
3968subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3969{
3970 struct weston_subsurface *sub =
3971 container_of(listener, struct weston_subsurface,
3972 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003973 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003974 assert(sub->surface != sub->parent);
3975
3976 if (weston_surface_is_mapped(sub->surface))
3977 weston_surface_unmap(sub->surface);
3978
3979 weston_subsurface_unlink_parent(sub);
3980}
3981
3982static void
3983subsurface_resource_destroy(struct wl_resource *resource)
3984{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003985 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003986
3987 if (sub)
3988 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003989}
3990
3991static void
3992subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
3993{
3994 wl_resource_destroy(resource);
3995}
3996
3997static void
3998weston_subsurface_link_parent(struct weston_subsurface *sub,
3999 struct weston_surface *parent)
4000{
4001 sub->parent = parent;
4002 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004003 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004004 &sub->parent_destroy_listener);
4005
4006 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4007 wl_list_insert(&parent->subsurface_list_pending,
4008 &sub->parent_link_pending);
4009}
4010
4011static void
4012weston_subsurface_link_surface(struct weston_subsurface *sub,
4013 struct weston_surface *surface)
4014{
4015 sub->surface = surface;
4016 sub->surface_destroy_listener.notify =
4017 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004018 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004019 &sub->surface_destroy_listener);
4020}
4021
4022static void
4023weston_subsurface_destroy(struct weston_subsurface *sub)
4024{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004025 struct weston_view *view, *next;
4026
Pekka Paalanene67858b2013-04-25 13:57:42 +03004027 assert(sub->surface);
4028
4029 if (sub->resource) {
4030 assert(weston_surface_to_subsurface(sub->surface) == sub);
4031 assert(sub->parent_destroy_listener.notify ==
4032 subsurface_handle_parent_destroy);
4033
George Kiagiadakised04d382014-06-13 18:10:26 +02004034 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
4035 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004036 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02004037 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05004038
Pekka Paalanene67858b2013-04-25 13:57:42 +03004039 if (sub->parent)
4040 weston_subsurface_unlink_parent(sub);
4041
Jason Ekstrand7b982072014-05-20 14:33:03 -05004042 weston_surface_state_fini(&sub->cached);
4043 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004044
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004045 sub->surface->committed = NULL;
4046 sub->surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004047 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004048 } else {
4049 /* the dummy weston_subsurface for the parent itself */
4050 assert(sub->parent_destroy_listener.notify == NULL);
4051 wl_list_remove(&sub->parent_link);
4052 wl_list_remove(&sub->parent_link_pending);
4053 }
4054
4055 wl_list_remove(&sub->surface_destroy_listener.link);
4056 free(sub);
4057}
4058
4059static const struct wl_subsurface_interface subsurface_implementation = {
4060 subsurface_destroy,
4061 subsurface_set_position,
4062 subsurface_place_above,
4063 subsurface_place_below,
4064 subsurface_set_sync,
4065 subsurface_set_desync
4066};
4067
4068static struct weston_subsurface *
4069weston_subsurface_create(uint32_t id, struct weston_surface *surface,
4070 struct weston_surface *parent)
4071{
4072 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004073 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004074
Bryce Harringtonde16d892014-11-20 22:21:57 -08004075 sub = zalloc(sizeof *sub);
4076 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004077 return NULL;
4078
Jason Ekstranda7af7042013-10-12 22:38:11 -05004079 wl_list_init(&sub->unused_views);
4080
Jason Ekstranda85118c2013-06-27 20:17:02 -05004081 sub->resource =
4082 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004083 if (!sub->resource) {
4084 free(sub);
4085 return NULL;
4086 }
4087
Jason Ekstranda85118c2013-06-27 20:17:02 -05004088 wl_resource_set_implementation(sub->resource,
4089 &subsurface_implementation,
4090 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004091 weston_subsurface_link_surface(sub, surface);
4092 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05004093 weston_surface_state_init(&sub->cached);
4094 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004095 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004096
4097 return sub;
4098}
4099
4100/* Create a dummy subsurface for having the parent itself in its
4101 * sub-surface lists. Makes stacking order manipulation easy.
4102 */
4103static struct weston_subsurface *
4104weston_subsurface_create_for_parent(struct weston_surface *parent)
4105{
4106 struct weston_subsurface *sub;
4107
Bryce Harringtonde16d892014-11-20 22:21:57 -08004108 sub = zalloc(sizeof *sub);
4109 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004110 return NULL;
4111
4112 weston_subsurface_link_surface(sub, parent);
4113 sub->parent = parent;
4114 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4115 wl_list_insert(&parent->subsurface_list_pending,
4116 &sub->parent_link_pending);
4117
4118 return sub;
4119}
4120
4121static void
4122subcompositor_get_subsurface(struct wl_client *client,
4123 struct wl_resource *resource,
4124 uint32_t id,
4125 struct wl_resource *surface_resource,
4126 struct wl_resource *parent_resource)
4127{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004128 struct weston_surface *surface =
4129 wl_resource_get_user_data(surface_resource);
4130 struct weston_surface *parent =
4131 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004132 struct weston_subsurface *sub;
4133 static const char where[] = "get_subsurface: wl_subsurface@";
4134
4135 if (surface == parent) {
4136 wl_resource_post_error(resource,
4137 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4138 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004139 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004140 return;
4141 }
4142
4143 if (weston_surface_to_subsurface(surface)) {
4144 wl_resource_post_error(resource,
4145 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4146 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004147 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004148 return;
4149 }
4150
Pekka Paalanen50b67472014-10-01 15:02:41 +03004151 if (weston_surface_set_role(surface, "wl_subsurface", resource,
4152 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004153 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004154
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004155 if (weston_surface_get_main_surface(parent) == surface) {
4156 wl_resource_post_error(resource,
4157 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4158 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004159 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004160 return;
4161 }
4162
Pekka Paalanene67858b2013-04-25 13:57:42 +03004163 /* make sure the parent is in its own list */
4164 if (wl_list_empty(&parent->subsurface_list)) {
4165 if (!weston_subsurface_create_for_parent(parent)) {
4166 wl_resource_post_no_memory(resource);
4167 return;
4168 }
4169 }
4170
4171 sub = weston_subsurface_create(id, surface, parent);
4172 if (!sub) {
4173 wl_resource_post_no_memory(resource);
4174 return;
4175 }
4176
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004177 surface->committed = subsurface_committed;
4178 surface->committed_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004179 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004180}
4181
4182static void
4183subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
4184{
4185 wl_resource_destroy(resource);
4186}
4187
4188static const struct wl_subcompositor_interface subcompositor_interface = {
4189 subcompositor_destroy,
4190 subcompositor_get_subsurface
4191};
4192
4193static void
4194bind_subcompositor(struct wl_client *client,
4195 void *data, uint32_t version, uint32_t id)
4196{
4197 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004198 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004199
Jason Ekstranda85118c2013-06-27 20:17:02 -05004200 resource =
4201 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004202 if (resource == NULL) {
4203 wl_client_post_no_memory(client);
4204 return;
4205 }
4206 wl_resource_set_implementation(resource, &subcompositor_interface,
4207 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004208}
4209
Bryce Harrington0795ece2016-08-30 12:04:26 -07004210/** Set a DPMS mode on all of the compositor's outputs
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004211 *
4212 * \param compositor The compositor instance
4213 * \param state The DPMS state the outputs will be set to
4214 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004215static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004216weston_compositor_dpms(struct weston_compositor *compositor,
4217 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004218{
4219 struct weston_output *output;
4220
Bryce Harrington08976ac2016-08-30 12:05:16 -07004221 wl_list_for_each(output, &compositor->output_list, link)
4222 if (output->set_dpms)
4223 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004224}
4225
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004226/** Restores the compositor to active status
4227 *
4228 * \param compositor The compositor instance
4229 *
4230 * If the compositor was in a sleeping mode, all outputs are powered
4231 * back on via DPMS. Otherwise if the compositor was inactive
4232 * (idle/locked, offscreen, or sleeping) then the compositor's wake
4233 * signal will fire.
4234 *
4235 * Restarts the idle timer.
4236 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004237WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004238weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004239{
Neil Roberts8b62e202013-09-30 13:14:47 +01004240 uint32_t old_state = compositor->state;
4241
4242 /* The state needs to be changed before emitting the wake
4243 * signal because that may try to schedule a repaint which
4244 * will not work if the compositor is still sleeping */
4245 compositor->state = WESTON_COMPOSITOR_ACTIVE;
4246
4247 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004248 case WESTON_COMPOSITOR_SLEEPING:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004249 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004250 case WESTON_COMPOSITOR_OFFSCREEN:
Daniel Stone893b9362016-11-08 15:47:09 +00004251 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004252 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004253 /* fall through */
4254 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004255 wl_event_source_timer_update(compositor->idle_source,
4256 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004257 }
4258}
4259
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004260/** Turns off rendering and frame events for the compositor.
4261 *
4262 * \param compositor The compositor instance
4263 *
4264 * This is used for example to prevent further rendering while the
4265 * compositor is shutting down.
4266 *
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004267 * Stops the idle timer.
4268 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004269WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004270weston_compositor_offscreen(struct weston_compositor *compositor)
4271{
4272 switch (compositor->state) {
4273 case WESTON_COMPOSITOR_OFFSCREEN:
4274 return;
4275 case WESTON_COMPOSITOR_SLEEPING:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004276 default:
4277 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4278 wl_event_source_timer_update(compositor->idle_source, 0);
4279 }
4280}
4281
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004282/** Powers down all attached output devices
4283 *
4284 * \param compositor The compositor instance
4285 *
4286 * Causes rendering to the outputs to cease, and no frame events to be
4287 * sent. Only powers down the outputs if the compositor is not already
4288 * in sleep mode.
4289 *
4290 * Stops the idle timer.
4291 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004292WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004293weston_compositor_sleep(struct weston_compositor *compositor)
4294{
4295 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
4296 return;
4297
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004298 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004299 compositor->state = WESTON_COMPOSITOR_SLEEPING;
4300 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
4301}
4302
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004303/** Sets compositor to idle mode
4304 *
4305 * \param data The compositor instance
4306 *
4307 * This is called when the idle timer fires. Once the compositor is in
4308 * idle mode it requires a wake action (e.g. via
4309 * weston_compositor_wake()) to restore it. The compositor's
4310 * idle_signal will be triggered when the idle event occurs.
4311 *
4312 * Idleness can be inhibited by setting the compositor's idle_inhibit
4313 * property.
4314 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004315static int
4316idle_handler(void *data)
4317{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004318 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004319
4320 if (compositor->idle_inhibit)
4321 return 1;
4322
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004323 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004324 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004325
4326 return 1;
4327}
4328
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004329WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08004330weston_plane_init(struct weston_plane *plane,
4331 struct weston_compositor *ec,
4332 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004333{
4334 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004335 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004336 plane->x = x;
4337 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08004338 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004339
4340 /* Init the link so that the call to wl_list_remove() when releasing
4341 * the plane without ever stacking doesn't lead to a crash */
4342 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004343}
4344
4345WL_EXPORT void
4346weston_plane_release(struct weston_plane *plane)
4347{
Xiong Zhang97116532013-10-23 13:58:31 +08004348 struct weston_view *view;
4349
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004350 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004351 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004352
Xiong Zhang97116532013-10-23 13:58:31 +08004353 wl_list_for_each(view, &plane->compositor->view_list, link) {
4354 if (view->plane == plane)
4355 view->plane = NULL;
4356 }
4357
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004358 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004359}
4360
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004361WL_EXPORT void
4362weston_compositor_stack_plane(struct weston_compositor *ec,
4363 struct weston_plane *plane,
4364 struct weston_plane *above)
4365{
4366 if (above)
4367 wl_list_insert(above->link.prev, &plane->link);
4368 else
4369 wl_list_insert(&ec->plane_list, &plane->link);
4370}
4371
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004372static void
4373output_release(struct wl_client *client, struct wl_resource *resource)
4374{
4375 wl_resource_destroy(resource);
4376}
4377
4378static const struct wl_output_interface output_interface = {
4379 output_release,
4380};
4381
4382
Casey Dahlin9074db52012-04-19 22:50:09 -04004383static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004384{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004385 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004386}
4387
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004388static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004389bind_output(struct wl_client *client,
4390 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05004391{
Pekka Paalanen05347622017-03-27 12:24:34 +03004392 struct weston_head *head = data;
4393 struct weston_output *output = head->output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004394 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004395 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004396
Jason Ekstranda85118c2013-06-27 20:17:02 -05004397 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004398 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004399 if (resource == NULL) {
4400 wl_client_post_no_memory(client);
4401 return;
4402 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004403
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03004404 wl_list_insert(&head->resource_list, wl_resource_get_link(resource));
Pekka Paalanen055c1132017-03-27 16:31:25 +03004405 wl_resource_set_implementation(resource, &output_interface, head,
Pekka Paalanen05347622017-03-27 12:24:34 +03004406 unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04004407
Pekka Paalanen05347622017-03-27 12:24:34 +03004408 assert(output);
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004409 wl_output_send_geometry(resource,
4410 output->x,
4411 output->y,
Pekka Paalanen01f60212017-03-24 15:39:24 +02004412 head->mm_width,
4413 head->mm_height,
4414 head->subpixel,
4415 head->make, head->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04004416 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004417 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004418 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02004419 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004420
4421 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004422 wl_output_send_mode(resource,
4423 mode->flags,
4424 mode->width,
4425 mode->height,
4426 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004427 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02004428
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004429 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004430 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004431}
4432
Pekka Paalanendcac3512017-12-08 14:13:34 +02004433static void
4434weston_head_add_global(struct weston_head *head)
4435{
4436 head->global = wl_global_create(head->compositor->wl_display,
4437 &wl_output_interface, 3,
4438 head, bind_output);
4439}
4440
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02004441/** Remove the global wl_output protocol object
4442 *
4443 * \param head The head whose global to remove.
4444 *
4445 * Also orphans the wl_resources for this head (wl_output).
4446 */
4447static void
4448weston_head_remove_global(struct weston_head *head)
4449{
4450 struct wl_resource *resource, *tmp;
4451
4452 if (head->global)
4453 wl_global_destroy(head->global);
4454 head->global = NULL;
4455
4456 wl_resource_for_each_safe(resource, tmp, &head->resource_list) {
4457 unbind_resource(resource);
4458 wl_resource_set_destructor(resource, NULL);
4459 wl_resource_set_user_data(resource, NULL);
4460 }
4461}
4462
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004463/** Get the backing object of wl_output
4464 *
4465 * \param resource A wl_output protocol object.
4466 * \return The backing object (user data) of a wl_resource representing a
4467 * wl_output protocol object.
4468 */
Pekka Paalanen055c1132017-03-27 16:31:25 +03004469WL_EXPORT struct weston_head *
4470weston_head_from_resource(struct wl_resource *resource)
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004471{
4472 assert(wl_resource_instance_of(resource, &wl_output_interface,
4473 &output_interface));
4474
4475 return wl_resource_get_user_data(resource);
4476}
4477
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004478/** Initialize a pre-allocated weston_head
4479 *
4480 * \param head The head to initialize.
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004481 * \param name The head name, e.g. the connector name or equivalent.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004482 *
4483 * The head will be safe to attach, detach and release.
4484 *
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004485 * The name is used in logs, and can be used by compositors as a configuration
4486 * identifier.
4487 *
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004488 * \memberof weston_head
4489 * \internal
4490 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004491WL_EXPORT void
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004492weston_head_init(struct weston_head *head, const char *name)
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004493{
4494 /* Add some (in)sane defaults which can be used
4495 * for checking if an output was properly configured
4496 */
4497 memset(head, 0, sizeof *head);
4498
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004499 wl_list_init(&head->compositor_link);
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03004500 wl_signal_init(&head->destroy_signal);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004501 wl_list_init(&head->output_link);
4502 wl_list_init(&head->resource_list);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004503 head->name = strdup(name);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004504}
4505
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004506/** Send output heads changed signal
4507 *
4508 * \param output The output that changed.
4509 *
4510 * Notify that the enabled output gained and/or lost heads, or that the
4511 * associated heads may have changed their connection status. This does not
4512 * include cases where the output becomes enabled or disabled. The registered
4513 * callbacks are called after the change has successfully happened.
4514 *
4515 * If connection status change causes the compositor to attach or detach a head
4516 * to an enabled output, the registered callbacks may be called multiple times.
4517 */
4518static void
4519weston_output_emit_heads_changed(struct weston_output *output)
4520{
4521 wl_signal_emit(&output->compositor->output_heads_changed_signal,
4522 output);
4523}
4524
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004525/** Idle task for emitting heads_changed_signal */
4526static void
4527weston_compositor_call_heads_changed(void *data)
4528{
4529 struct weston_compositor *compositor = data;
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004530 struct weston_head *head;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004531
4532 compositor->heads_changed_source = NULL;
4533
4534 wl_signal_emit(&compositor->heads_changed_signal, compositor);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004535
4536 wl_list_for_each(head, &compositor->head_list, compositor_link) {
4537 if (head->output && head->output->enabled)
4538 weston_output_emit_heads_changed(head->output);
4539 }
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004540}
4541
4542/** Schedule a call on idle to heads_changed callback
4543 *
4544 * \param compositor The Compositor.
4545 *
4546 * \memberof weston_compositor
4547 * \internal
4548 */
4549static void
4550weston_compositor_schedule_heads_changed(struct weston_compositor *compositor)
4551{
4552 struct wl_event_loop *loop;
4553
4554 if (compositor->heads_changed_source)
4555 return;
4556
4557 loop = wl_display_get_event_loop(compositor->wl_display);
4558 compositor->heads_changed_source = wl_event_loop_add_idle(loop,
4559 weston_compositor_call_heads_changed, compositor);
4560}
4561
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004562/** Register a new head
4563 *
4564 * \param compositor The compositor.
4565 * \param head The head to register, must not be already registered.
4566 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004567 * This signals the core that a new head has become available, leading to
4568 * heads_changed hook being called later.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004569 *
4570 * \memberof weston_compositor
4571 * \internal
4572 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004573WL_EXPORT void
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004574weston_compositor_add_head(struct weston_compositor *compositor,
4575 struct weston_head *head)
4576{
4577 assert(wl_list_empty(&head->compositor_link));
4578 assert(head->name);
4579
4580 wl_list_insert(compositor->head_list.prev, &head->compositor_link);
4581 head->compositor = compositor;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004582 weston_compositor_schedule_heads_changed(compositor);
4583}
4584
4585/** Adds a listener to be called when heads change
4586 *
4587 * \param compositor The compositor.
4588 * \param listener The listener to add.
4589 *
4590 * The listener notify function argument is the \var compositor.
4591 *
4592 * The listener function will be called after heads are added or their
4593 * connection status has changed. Several changes may be accumulated into a
4594 * single call. The user is expected to iterate over the existing heads and
4595 * check their statuses to find out what changed.
4596 *
4597 * \sa weston_compositor_iterate_heads, weston_head_is_connected,
4598 * weston_head_is_enabled
4599 * \memberof weston_compositor
4600 */
4601WL_EXPORT void
4602weston_compositor_add_heads_changed_listener(struct weston_compositor *compositor,
4603 struct wl_listener *listener)
4604{
4605 wl_signal_add(&compositor->heads_changed_signal, listener);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004606}
4607
4608/** Iterate over available heads
4609 *
4610 * \param compositor The compositor.
4611 * \param item The iterator, or NULL for start.
4612 * \return The next available head in the list.
4613 *
4614 * Returns all available heads, regardless of being connected or enabled.
4615 *
4616 * You can iterate over all heads as follows:
4617 * \code
4618 * struct weston_head *head = NULL;
4619 *
4620 * while ((head = weston_compositor_iterate_heads(compositor, head))) {
4621 * ...
4622 * }
4623 * \endcode
4624 *
4625 * If you cause \c iter to be removed from the list, you cannot use it to
4626 * continue iterating. Removing any other item is safe.
4627 *
4628 * \memberof weston_compositor
4629 */
4630WL_EXPORT struct weston_head *
4631weston_compositor_iterate_heads(struct weston_compositor *compositor,
4632 struct weston_head *iter)
4633{
4634 struct wl_list *list = &compositor->head_list;
4635 struct wl_list *node;
4636
4637 assert(compositor);
4638 assert(!iter || iter->compositor == compositor);
4639
4640 if (iter)
4641 node = iter->compositor_link.next;
4642 else
4643 node = list->next;
4644
4645 assert(node);
4646 assert(!iter || node != &iter->compositor_link);
4647
4648 if (node == list)
4649 return NULL;
4650
4651 return container_of(node, struct weston_head, compositor_link);
4652}
4653
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004654/** Iterate over attached heads
4655 *
4656 * \param output The output whose heads to iterate.
4657 * \param item The iterator, or NULL for start.
4658 * \return The next attached head in the list.
4659 *
4660 * Returns all heads currently attached to the output.
4661 *
4662 * You can iterate over all heads as follows:
4663 * \code
4664 * struct weston_head *head = NULL;
4665 *
4666 * while ((head = weston_output_iterate_heads(output, head))) {
4667 * ...
4668 * }
4669 * \endcode
4670 *
4671 * If you cause \c iter to be removed from the list, you cannot use it to
4672 * continue iterating. Removing any other item is safe.
4673 *
4674 * \memberof weston_compositor
4675 */
4676WL_EXPORT struct weston_head *
4677weston_output_iterate_heads(struct weston_output *output,
4678 struct weston_head *iter)
4679{
4680 struct wl_list *list = &output->head_list;
4681 struct wl_list *node;
4682
4683 assert(output);
4684 assert(!iter || iter->output == output);
4685
4686 if (iter)
4687 node = iter->output_link.next;
4688 else
4689 node = list->next;
4690
4691 assert(node);
4692 assert(!iter || node != &iter->output_link);
4693
4694 if (node == list)
4695 return NULL;
4696
4697 return container_of(node, struct weston_head, output_link);
4698}
4699
Pekka Paalanendcac3512017-12-08 14:13:34 +02004700/** Attach a head to an output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004701 *
4702 * \param output The output to attach to.
4703 * \param head The head that is not yet attached.
4704 * \return 0 on success, -1 on failure.
4705 *
4706 * Attaches the given head to the output. All heads of an output are clones
4707 * and share the resolution and timings.
4708 *
4709 * Cloning heads this way uses less resources than creating an output for
4710 * each head, but is not always possible due to environment, driver and hardware
4711 * limitations.
4712 *
4713 * On failure, the head remains unattached. Success of this function does not
4714 * guarantee the output configuration is actually valid. The final checks are
Pekka Paalanendcac3512017-12-08 14:13:34 +02004715 * made on weston_output_enable() unless the output was already enabled.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004716 *
4717 * \memberof weston_output
4718 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004719WL_EXPORT int
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004720weston_output_attach_head(struct weston_output *output,
4721 struct weston_head *head)
4722{
Pekka Paalanendcac3512017-12-08 14:13:34 +02004723 char *head_names;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004724
4725 if (!wl_list_empty(&head->output_link))
4726 return -1;
4727
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004728 if (output->attach_head) {
4729 if (output->attach_head(output, head) < 0)
4730 return -1;
4731 } else if (!wl_list_empty(&output->head_list)) {
4732 /* No support for clones in the legacy path. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004733 return -1;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004734 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004735
4736 head->output = output;
4737 wl_list_insert(output->head_list.prev, &head->output_link);
4738
Pekka Paalanendcac3512017-12-08 14:13:34 +02004739 if (output->enabled) {
4740 weston_head_add_global(head);
4741
4742 head_names = weston_output_create_heads_string(output);
4743 weston_log("Output '%s' updated to have head(s) %s\n",
4744 output->name, head_names);
4745 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004746
4747 weston_output_emit_heads_changed(output);
Pekka Paalanendcac3512017-12-08 14:13:34 +02004748 }
4749
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004750 return 0;
4751}
4752
4753/** Detach a head from its output
4754 *
4755 * \param head The head to detach.
4756 *
4757 * It is safe to detach a non-attached head.
4758 *
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004759 * If the head is attached to an enabled output and the output will be left
4760 * with no heads, the output will be disabled.
4761 *
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004762 * \memberof weston_head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004763 * \sa weston_output_disable
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004764 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004765WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004766weston_head_detach(struct weston_head *head)
4767{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004768 struct weston_output *output = head->output;
Pekka Paalanena0106992017-12-08 16:11:17 +02004769 char *head_names;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004770
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004771 wl_list_remove(&head->output_link);
4772 wl_list_init(&head->output_link);
4773 head->output = NULL;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004774
4775 if (!output)
4776 return;
4777
4778 if (output->detach_head)
4779 output->detach_head(output, head);
4780
4781 if (output->enabled) {
4782 weston_head_remove_global(head);
4783
Pekka Paalanena0106992017-12-08 16:11:17 +02004784 if (wl_list_empty(&output->head_list)) {
4785 weston_log("Output '%s' no heads left, disabling.\n",
4786 output->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004787 weston_output_disable(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02004788 } else {
4789 head_names = weston_output_create_heads_string(output);
4790 weston_log("Output '%s' updated to have head(s) %s\n",
4791 output->name, head_names);
4792 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004793
4794 weston_output_emit_heads_changed(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02004795 }
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004796 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004797}
4798
4799/** Destroy a head
4800 *
4801 * \param head The head to be released.
4802 *
4803 * Destroys the head. The caller is responsible for freeing the memory pointed
4804 * to by \c head.
4805 *
4806 * \memberof weston_head
4807 * \internal
4808 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004809WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004810weston_head_release(struct weston_head *head)
4811{
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03004812 wl_signal_emit(&head->destroy_signal, head);
4813
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004814 weston_head_detach(head);
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03004815
4816 free(head->make);
4817 free(head->model);
4818 free(head->serial_number);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004819 free(head->name);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004820
4821 wl_list_remove(&head->compositor_link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004822}
4823
Pekka Paalanene19970f2017-08-28 14:11:02 +03004824static void
4825weston_head_set_device_changed(struct weston_head *head)
4826{
4827 head->device_changed = true;
4828
4829 if (head->compositor)
4830 weston_compositor_schedule_heads_changed(head->compositor);
4831}
4832
4833/** String equal comparison with NULLs being equal */
4834static bool
4835str_null_eq(const char *a, const char *b)
4836{
4837 if (!a && !b)
4838 return true;
4839
4840 if (!!a != !!b)
4841 return false;
4842
4843 return strcmp(a, b) == 0;
4844}
4845
Pekka Paalanen01f60212017-03-24 15:39:24 +02004846/** Store monitor make, model and serial number
4847 *
4848 * \param head The head to modify.
4849 * \param make The monitor make. If EDID is available, the PNP ID. Otherwise
4850 * any string, or NULL for none.
4851 * \param model The monitor model or name, or a made-up string, or NULL for
4852 * none.
4853 * \param serialno The monitor serial number, a made-up string, or NULL for
4854 * none.
4855 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03004856 * This may set the device_changed flag.
4857 *
Pekka Paalanen01f60212017-03-24 15:39:24 +02004858 * \memberof weston_head
4859 * \internal
4860 */
4861WL_EXPORT void
4862weston_head_set_monitor_strings(struct weston_head *head,
4863 const char *make,
4864 const char *model,
4865 const char *serialno)
4866{
Pekka Paalanene19970f2017-08-28 14:11:02 +03004867 if (str_null_eq(head->make, make) &&
4868 str_null_eq(head->model, model) &&
4869 str_null_eq(head->serial_number, serialno))
4870 return;
4871
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03004872 free(head->make);
4873 free(head->model);
4874 free(head->serial_number);
4875
4876 head->make = make ? strdup(make) : NULL;
4877 head->model = model ? strdup(model) : NULL;
4878 head->serial_number = serialno ? strdup(serialno) : NULL;
Pekka Paalanene19970f2017-08-28 14:11:02 +03004879
4880 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02004881}
4882
4883/** Store physical image size
4884 *
4885 * \param head The head to modify.
4886 * \param mm_width Image area width in millimeters.
4887 * \param mm_height Image area height in millimeters.
4888 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03004889 * This may set the device_changed flag.
4890 *
Pekka Paalanen01f60212017-03-24 15:39:24 +02004891 * \memberof weston_head
4892 * \internal
4893 */
4894WL_EXPORT void
4895weston_head_set_physical_size(struct weston_head *head,
4896 int32_t mm_width, int32_t mm_height)
4897{
Pekka Paalanene19970f2017-08-28 14:11:02 +03004898 if (head->mm_width == mm_width &&
4899 head->mm_height == mm_height)
4900 return;
4901
Pekka Paalanen01f60212017-03-24 15:39:24 +02004902 head->mm_width = mm_width;
4903 head->mm_height = mm_height;
Pekka Paalanene19970f2017-08-28 14:11:02 +03004904
4905 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02004906}
4907
4908/** Store monitor sub-pixel layout
4909 *
4910 * \param head The head to modify.
4911 * \param sp Sub-pixel layout. The possible values are:
4912 * - WL_OUTPUT_SUBPIXEL_UNKNOWN,
4913 * - WL_OUTPUT_SUBPIXEL_NONE,
4914 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB,
4915 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR,
4916 * - WL_OUTPUT_SUBPIXEL_VERTICAL_RGB,
4917 * - WL_OUTPUT_SUBPIXEL_VERTICAL_BGR
4918 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03004919 * This may set the device_changed flag.
4920 *
Pekka Paalanen01f60212017-03-24 15:39:24 +02004921 * \memberof weston_head
4922 * \internal
4923 */
4924WL_EXPORT void
4925weston_head_set_subpixel(struct weston_head *head,
4926 enum wl_output_subpixel sp)
4927{
Pekka Paalanene19970f2017-08-28 14:11:02 +03004928 if (head->subpixel == sp)
4929 return;
4930
Pekka Paalanen01f60212017-03-24 15:39:24 +02004931 head->subpixel = sp;
Pekka Paalanene19970f2017-08-28 14:11:02 +03004932
4933 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02004934}
4935
4936/** Mark the monitor as internal
4937 *
4938 * This is used for embedded screens, like laptop panels.
4939 *
4940 * \param head The head to mark as internal.
4941 *
4942 * By default a head is external. The type is often inferred from the physical
4943 * connector type.
4944 *
4945 * \memberof weston_head
4946 * \internal
4947 */
4948WL_EXPORT void
4949weston_head_set_internal(struct weston_head *head)
4950{
4951 head->connection_internal = true;
4952}
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004953
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03004954/** Store connector status
4955 *
4956 * \param head The head to modify.
4957 * \param connected Whether the head is connected.
4958 *
4959 * Connectors are created as disconnected. This function can be used to
4960 * set the connector status.
4961 *
4962 * The status should be set to true when a physical connector is connected to
4963 * a video sink device like a monitor and to false when the connector is
4964 * disconnected. For nested backends, the connection status should reflect the
4965 * connection to the parent display server.
4966 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004967 * When the connection status changes, it schedules a call to the heads_changed
Pekka Paalanene19970f2017-08-28 14:11:02 +03004968 * hook and sets the device_changed flag.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004969 *
4970 * \sa weston_compositor_set_heads_changed_cb
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03004971 * \memberof weston_head
4972 * \internal
4973 */
4974WL_EXPORT void
4975weston_head_set_connection_status(struct weston_head *head, bool connected)
4976{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004977 if (head->connected == connected)
4978 return;
4979
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03004980 head->connected = connected;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004981
Pekka Paalanene19970f2017-08-28 14:11:02 +03004982 weston_head_set_device_changed(head);
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03004983}
4984
4985/** Is the head currently connected?
4986 *
4987 * \param head The head to query.
4988 * \return Connection status.
4989 *
4990 * Returns true if the head is physically connected to a monitor, or in
4991 * case of a nested backend returns true when there is a connection to the
4992 * parent display server.
4993 *
4994 * This is independent from the head being enabled.
4995 *
4996 * \sa weston_head_is_enabled
4997 * \memberof weston_head
4998 */
4999WL_EXPORT bool
5000weston_head_is_connected(struct weston_head *head)
5001{
5002 return head->connected;
5003}
5004
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005005/** Is the head currently enabled?
5006 *
5007 * \param head The head to query.
5008 * \return Video status.
5009 *
5010 * Returns true if the head is currently transmitting a video stream.
5011 *
5012 * This is independent of the head being connected.
5013 *
5014 * \sa weston_head_is_connected
5015 * \memberof weston_head
5016 */
5017WL_EXPORT bool
5018weston_head_is_enabled(struct weston_head *head)
5019{
5020 if (!head->output)
5021 return false;
5022
5023 return head->output->enabled;
5024}
5025
Pekka Paalanene19970f2017-08-28 14:11:02 +03005026/** Has the device information changed?
5027 *
5028 * \param head The head to query.
5029 * \return True if the device information has changed since last reset.
5030 *
5031 * The information about the connected display device, e.g. a monitor, may
5032 * change without being disconnected in between. Changing information
5033 * causes a call to the heads_changed hook.
5034 *
5035 * The information includes make, model, serial number, physical size,
5036 * and sub-pixel type. The connection status is also included.
5037 *
5038 * \sa weston_head_reset_device_changed, weston_compositor_set_heads_changed_cb
5039 * \memberof weston_head
5040 */
5041WL_EXPORT bool
5042weston_head_is_device_changed(struct weston_head *head)
5043{
5044 return head->device_changed;
5045}
5046
5047/** Acknowledge device information change
5048 *
5049 * \param head The head to acknowledge.
5050 *
5051 * Clears the device changed flag on this head. When a compositor has processed
5052 * device information, it should call this to be able to notice further
5053 * changes.
5054 *
5055 * \sa weston_head_is_device_changed
5056 * \memberof weston_head
5057 */
5058WL_EXPORT void
5059weston_head_reset_device_changed(struct weston_head *head)
5060{
5061 head->device_changed = false;
5062}
5063
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005064/** Get the name of a head
5065 *
5066 * \param head The head to query.
5067 * \return The head's name, not NULL.
5068 *
5069 * The name depends on the backend. The DRM backend uses connector names,
5070 * other backends may use hardcoded names or user-given names.
5071 */
5072WL_EXPORT const char *
5073weston_head_get_name(struct weston_head *head)
5074{
5075 return head->name;
5076}
5077
5078/** Get the output the head is attached to
5079 *
5080 * \param head The head to query.
5081 * \return The output the head is attached to, or NULL if detached.
5082 */
5083WL_EXPORT struct weston_output *
5084weston_head_get_output(struct weston_head *head)
5085{
5086 return head->output;
5087}
5088
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005089/** Add destroy callback for a head
5090 *
5091 * \param head The head to watch for.
5092 * \param listener The listener to add. The \c notify member must be set.
5093 *
5094 * Heads may get destroyed for various reasons by the backends. If a head is
5095 * attached to an output, the compositor should listen for head destruction
5096 * and reconfigure or destroy the output if necessary.
5097 *
5098 * The destroy callbacks will be called on weston_head destruction before any
5099 * automatic detaching from an associated weston_output and before any
5100 * weston_head information is lost.
5101 *
5102 * The \c data argument to the notify callback is the weston_head being
5103 * destroyed.
5104 */
5105WL_EXPORT void
5106weston_head_add_destroy_listener(struct weston_head *head,
5107 struct wl_listener *listener)
5108{
5109 wl_signal_add(&head->destroy_signal, listener);
5110}
5111
5112/** Look up destroy listener for a head
5113 *
5114 * \param head The head to query.
5115 * \param notify The notify function used used for the added destroy listener.
5116 * \return The listener, or NULL if not found.
5117 *
5118 * This looks up the previously added destroy listener struct based on the
5119 * notify function it has. The listener can be used to access user data
5120 * through \c container_of().
5121 *
5122 * \sa wl_signal_get()
5123 */
5124WL_EXPORT struct wl_listener *
5125weston_head_get_destroy_listener(struct weston_head *head,
5126 wl_notify_func_t notify)
5127{
5128 return wl_signal_get(&head->destroy_signal, notify);
5129}
5130
David Fort0de859e2016-05-27 23:22:57 +02005131/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005132static void
David Fort0de859e2016-05-27 23:22:57 +02005133weston_compositor_reflow_outputs(struct weston_compositor *compositor,
5134 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005135{
5136 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02005137 bool start_resizing = false;
5138
5139 if (!delta_width)
5140 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005141
5142 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02005143 if (output == resized_output) {
5144 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005145 continue;
5146 }
5147
David Fort0de859e2016-05-27 23:22:57 +02005148 if (start_resizing) {
5149 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005150 output->dirty = 1;
5151 }
5152 }
5153}
5154
Pekka Paalanend72bad22017-03-29 17:01:41 +03005155static void
Scott Moreauccbf29d2012-02-22 14:21:41 -07005156weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005157{
Scott Moreau850ca422012-05-21 15:21:25 -06005158 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05005159
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005160 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005161 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06005162
Scott Moreauccbf29d2012-02-22 14:21:41 -07005163 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06005164 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005165 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005166 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005167 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005168 weston_matrix_scale(&output->matrix, magnification,
5169 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005170 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005171
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005172 switch (output->transform) {
5173 case WL_OUTPUT_TRANSFORM_FLIPPED:
5174 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5175 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5176 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5177 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5178 weston_matrix_scale(&output->matrix, -1, 1, 1);
5179 break;
5180 }
5181
5182 switch (output->transform) {
5183 default:
5184 case WL_OUTPUT_TRANSFORM_NORMAL:
5185 case WL_OUTPUT_TRANSFORM_FLIPPED:
5186 break;
5187 case WL_OUTPUT_TRANSFORM_90:
5188 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5189 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
5190 weston_matrix_rotate_xy(&output->matrix, 0, 1);
5191 break;
5192 case WL_OUTPUT_TRANSFORM_180:
5193 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5194 weston_matrix_translate(&output->matrix,
5195 -output->width, -output->height, 0);
5196 weston_matrix_rotate_xy(&output->matrix, -1, 0);
5197 break;
5198 case WL_OUTPUT_TRANSFORM_270:
5199 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5200 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5201 weston_matrix_rotate_xy(&output->matrix, 0, -1);
5202 break;
5203 }
5204
5205 if (output->current_scale != 1)
5206 weston_matrix_scale(&output->matrix,
5207 output->current_scale,
5208 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01005209
Scott Moreauccbf29d2012-02-22 14:21:41 -07005210 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05005211
5212 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005213}
5214
Scott Moreau1bad5db2012-08-18 01:04:05 -06005215static void
Alexander Larsson0b135062013-05-28 16:23:36 +02005216weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06005217{
5218 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005219 output->native_scale = scale;
5220 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06005221
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005222 convert_size_by_transform_scale(&output->width, &output->height,
5223 output->current_mode->width,
5224 output->current_mode->height,
5225 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02005226}
5227
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005228static void
5229weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07005230{
5231 output->x = x;
5232 output->y = y;
5233
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005234 pixman_region32_fini(&output->previous_damage);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02005235 pixman_region32_init(&output->previous_damage);
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005236
5237 pixman_region32_fini(&output->region);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005238 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06005239 output->width,
5240 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005241}
5242
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005243WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005244weston_output_move(struct weston_output *output, int x, int y)
5245{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005246 struct weston_head *head;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005247 struct wl_resource *resource;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005248 int ver;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005249
5250 output->move_x = x - output->x;
5251 output->move_y = y - output->y;
5252
5253 if (output->move_x == 0 && output->move_y == 0)
5254 return;
5255
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005256 weston_output_init_geometry(output, x, y);
5257
5258 output->dirty = 1;
5259
5260 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005261 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005262
5263 /* Notify clients of the change for output position. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005264 wl_list_for_each(head, &output->head_list, output_link) {
5265 wl_resource_for_each(resource, &head->resource_list) {
5266 wl_output_send_geometry(resource,
5267 output->x,
5268 output->y,
5269 head->mm_width,
5270 head->mm_height,
5271 head->subpixel,
5272 head->make,
5273 head->model,
5274 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08005275
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005276 ver = wl_resource_get_version(resource);
5277 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
5278 wl_output_send_done(resource);
5279 }
Quanxian Wangb2c86362014-03-14 09:16:25 +08005280 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005281}
5282
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005283/** Signal that a pending output is taken into use.
5284 *
5285 * Removes the output from the pending list and adds it to the compositor's
5286 * list of enabled outputs. The output created signal is emitted.
Giulio Camuffob1147152015-05-06 21:41:57 +03005287 *
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005288 * The output gets an internal ID assigned, and the wl_output global is
5289 * created.
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005290 *
Giulio Camuffob1147152015-05-06 21:41:57 +03005291 * \param compositor The compositor instance.
5292 * \param output The output to be added.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005293 *
5294 * \internal
Giulio Camuffob1147152015-05-06 21:41:57 +03005295 */
Pekka Paalanenf9681b52017-03-29 16:58:48 +03005296static void
Giulio Camuffob1147152015-05-06 21:41:57 +03005297weston_compositor_add_output(struct weston_compositor *compositor,
5298 struct weston_output *output)
5299{
Armin Krezoviće5403842016-08-05 15:28:29 +02005300 struct weston_view *view, *next;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005301 struct weston_head *head;
Armin Krezoviće5403842016-08-05 15:28:29 +02005302
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005303 assert(!output->enabled);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005304
5305 /* Verify we haven't reached the limit of 32 available output IDs */
5306 assert(ffs(~compositor->output_id_pool) > 0);
5307
5308 /* Invert the output id pool and look for the lowest numbered
5309 * switch (the least significant bit). Take that bit's position
5310 * as our ID, and mark it used in the compositor's output_id_pool.
5311 */
5312 output->id = ffs(~compositor->output_id_pool) - 1;
5313 compositor->output_id_pool |= 1u << output->id;
5314
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005315 wl_list_remove(&output->link);
Giulio Camuffob1147152015-05-06 21:41:57 +03005316 wl_list_insert(compositor->output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005317 output->enabled = true;
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005318
Pekka Paalanendcac3512017-12-08 14:13:34 +02005319 wl_list_for_each(head, &output->head_list, output_link)
5320 weston_head_add_global(head);
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005321
Giulio Camuffob1147152015-05-06 21:41:57 +03005322 wl_signal_emit(&compositor->output_created_signal, output);
Armin Krezoviće5403842016-08-05 15:28:29 +02005323
5324 wl_list_for_each_safe(view, next, &compositor->view_list, link)
5325 weston_view_geometry_dirty(view);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005326}
5327
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005328/** Transform device coordinates into global coordinates
5329 *
5330 * \param device_x[in] X coordinate in device units.
5331 * \param device_y[in] Y coordinate in device units.
5332 * \param x[out] X coordinate in the global space.
5333 * \param y[out] Y coordinate in the global space.
5334 *
5335 * Transforms coordinates from the device coordinate space
5336 * (physical pixel units) to the global coordinate space (logical pixel units).
5337 * This takes into account output transform and scale.
5338 *
5339 * \memberof weston_output
5340 * \internal
5341 */
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005342WL_EXPORT void
5343weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005344 double device_x, double device_y,
5345 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005346{
Derek Foreman0f679412014-10-02 13:41:17 -05005347 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005348 device_x,
5349 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05005350 0.0,
5351 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005352
Derek Foreman67a18b92015-03-24 11:36:14 -05005353 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005354
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005355 *x = p.f[0] / p.f[3];
5356 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005357}
5358
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005359/** Removes output from compositor's list of enabled outputs
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005360 *
5361 * \param output The weston_output object that is being removed.
5362 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005363 * The following happens:
5364 *
5365 * - The output assignments of all views in the current scenegraph are
5366 * recomputed.
5367 *
5368 * - Presentation feedback is discarded.
5369 *
5370 * - Compositor is notified that outputs were changed and
5371 * applies the necessary changes to re-layout outputs.
5372 *
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005373 * - The output is put back in the pending outputs list.
5374 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005375 * - Signal is emitted to notify all users of the weston_output
5376 * object that the output is being destroyed.
5377 *
5378 * - wl_output protocol objects referencing this weston_output
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005379 * are made inert, and the wl_output global is removed.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005380 *
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005381 * - The output's internal ID is released.
5382 *
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005383 * \memberof weston_output
5384 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005385 */
5386static void
5387weston_compositor_remove_output(struct weston_output *output)
5388{
Pekka Paalanenbccda712017-03-29 16:16:04 +03005389 struct weston_compositor *compositor = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005390 struct weston_view *view;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005391 struct weston_head *head;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005392
5393 assert(output->destroying);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005394 assert(output->enabled);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005395
Pekka Paalanenbccda712017-03-29 16:16:04 +03005396 wl_list_for_each(view, &compositor->view_list, link) {
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005397 if (view->output_mask & (1u << output->id))
5398 weston_view_assign_output(view);
5399 }
5400
5401 weston_presentation_feedback_discard_list(&output->feedback_list);
5402
Pekka Paalanenbccda712017-03-29 16:16:04 +03005403 weston_compositor_reflow_outputs(compositor, output, output->width);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005404
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005405 wl_list_remove(&output->link);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005406 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005407 output->enabled = false;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005408
Pekka Paalanenbccda712017-03-29 16:16:04 +03005409 wl_signal_emit(&compositor->output_destroyed_signal, output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005410 wl_signal_emit(&output->destroy_signal, output);
5411
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02005412 wl_list_for_each(head, &output->head_list, output_link)
5413 weston_head_remove_global(head);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005414
5415 compositor->output_id_pool &= ~(1u << output->id);
5416 output->id = 0xffffffff; /* invalid */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005417}
5418
5419/** Sets the output scale for a given output.
5420 *
5421 * \param output The weston_output object that the scale is set for.
5422 * \param scale Scale factor for the given output.
5423 *
5424 * It only supports setting scale for an output that
5425 * is not enabled and it can only be ran once.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005426 *
5427 * \memberof weston_output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005428 */
5429WL_EXPORT void
5430weston_output_set_scale(struct weston_output *output,
5431 int32_t scale)
5432{
5433 /* We can only set scale on a disabled output */
5434 assert(!output->enabled);
5435
5436 /* We only want to set scale once */
5437 assert(!output->scale);
5438
5439 output->scale = scale;
5440}
5441
5442/** Sets the output transform for a given output.
5443 *
5444 * \param output The weston_output object that the transform is set for.
5445 * \param transform Transform value for the given output.
5446 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005447 * Refer to wl_output::transform section located at
5448 * https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_output
5449 * for list of values that can be passed to this function.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005450 *
5451 * \memberof weston_output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005452 */
5453WL_EXPORT void
5454weston_output_set_transform(struct weston_output *output,
5455 uint32_t transform)
5456{
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005457 struct weston_pointer_motion_event ev;
5458 struct wl_resource *resource;
5459 struct weston_seat *seat;
5460 pixman_region32_t old_region;
5461 int mid_x, mid_y;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005462 struct weston_head *head;
5463 int ver;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005464
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005465 if (!output->enabled && output->transform == UINT32_MAX) {
5466 output->transform = transform;
5467 return;
5468 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005469
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005470 weston_output_transform_scale_init(output, transform, output->scale);
5471
5472 pixman_region32_init(&old_region);
5473 pixman_region32_copy(&old_region, &output->region);
5474
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005475 weston_output_init_geometry(output, output->x, output->y);
5476
5477 output->dirty = 1;
5478
5479 /* Notify clients of the change for output transform. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005480 wl_list_for_each(head, &output->head_list, output_link) {
5481 wl_resource_for_each(resource, &head->resource_list) {
5482 wl_output_send_geometry(resource,
5483 output->x,
5484 output->y,
5485 head->mm_width,
5486 head->mm_height,
5487 head->subpixel,
5488 head->make,
5489 head->model,
5490 output->transform);
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005491
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005492 ver = wl_resource_get_version(resource);
5493 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
5494 wl_output_send_done(resource);
5495 }
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005496 }
5497
5498 /* we must ensure that pointers are inside output, otherwise they disappear */
5499 mid_x = output->x + output->width / 2;
5500 mid_y = output->y + output->height / 2;
5501
5502 ev.mask = WESTON_POINTER_MOTION_ABS;
5503 ev.x = wl_fixed_to_double(wl_fixed_from_int(mid_x));
5504 ev.y = wl_fixed_to_double(wl_fixed_from_int(mid_y));
5505
5506 wl_list_for_each(seat, &output->compositor->seat_list, link) {
5507 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5508
5509 if (pointer && pixman_region32_contains_point(&old_region,
5510 wl_fixed_to_int(pointer->x),
5511 wl_fixed_to_int(pointer->y),
5512 NULL))
5513 weston_pointer_move(pointer, &ev);
5514 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005515}
5516
5517/** Initializes a weston_output object with enough data so
5518 ** an output can be configured.
5519 *
5520 * \param output The weston_output object to initialize
5521 * \param compositor The compositor instance.
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005522 * \param name Name for the output (the string is copied).
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005523 *
5524 * Sets initial values for fields that are expected to be
5525 * configured either by compositors or backends.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005526 *
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005527 * The name is used in logs, and can be used by compositors as a configuration
5528 * identifier.
5529 *
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005530 * \memberof weston_output
5531 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005532 */
5533WL_EXPORT void
Armin Krezović40087402016-09-30 14:11:12 +02005534weston_output_init(struct weston_output *output,
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005535 struct weston_compositor *compositor,
5536 const char *name)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005537{
5538 output->compositor = compositor;
5539 output->destroying = 0;
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005540 output->name = strdup(name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005541 wl_list_init(&output->link);
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02005542 wl_signal_init(&output->user_destroy_signal);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005543 output->enabled = false;
5544
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005545 wl_list_init(&output->head_list);
5546
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005547 /* Add some (in)sane defaults which can be used
5548 * for checking if an output was properly configured
5549 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005550 output->scale = 0;
5551 /* Can't use -1 on uint32_t and 0 is valid enum value */
5552 output->transform = UINT32_MAX;
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005553
5554 pixman_region32_init(&output->previous_damage);
5555 pixman_region32_init(&output->region);
Pekka Paalanen42704142017-09-06 16:47:52 +03005556 wl_list_init(&output->mode_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005557}
5558
5559/** Adds weston_output object to pending output list.
5560 *
5561 * \param output The weston_output object to add
5562 * \param compositor The compositor instance.
5563 *
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03005564 * The opposite of this operation is built into weston_output_release().
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005565 *
5566 * \memberof weston_output
5567 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005568 */
5569WL_EXPORT void
5570weston_compositor_add_pending_output(struct weston_output *output,
5571 struct weston_compositor *compositor)
5572{
Pekka Paalanene952a012017-03-29 17:14:00 +03005573 assert(output->disable);
5574 assert(output->enable);
5575
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005576 wl_list_remove(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005577 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005578}
5579
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02005580/** Create a string with the attached heads' names.
5581 *
5582 * The string must be free()'d.
5583 */
5584static char *
5585weston_output_create_heads_string(struct weston_output *output)
5586{
5587 FILE *fp;
5588 char *str = NULL;
5589 size_t size = 0;
5590 struct weston_head *head;
5591 const char *sep = "";
5592
5593 fp = open_memstream(&str, &size);
5594 if (!fp)
5595 return NULL;
5596
5597 wl_list_for_each(head, &output->head_list, output_link) {
5598 fprintf(fp, "%s%s", sep, head->name);
5599 sep = ", ";
5600 }
5601 fclose(fp);
5602
5603 return str;
5604}
5605
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005606/** Constructs a weston_output object that can be used by the compositor.
5607 *
Pekka Paalanencc201e42017-03-30 15:11:25 +03005608 * \param output The weston_output object that needs to be enabled. Must not
Pekka Paalanenddce54d2017-08-23 16:00:21 +03005609 * be enabled already. Must have at least one head attached.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005610 *
5611 * Output coordinates are calculated and each new output is by default
5612 * assigned to the right of previous one.
5613 *
5614 * Sets up the transformation, zoom, and geometry of the output using
5615 * the properties that need to be configured by the compositor.
5616 *
5617 * Establishes a repaint timer for the output with the relevant display
5618 * object's event loop. See output_repaint_timer_handler().
5619 *
5620 * The output is assigned an ID. Weston can support up to 32 distinct
5621 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
5622 * is referred to and used to find the first available ID number, and
5623 * then this ID is marked as used in output_id_pool.
5624 *
5625 * The output is also assigned a Wayland global with the wl_output
5626 * external interface.
5627 *
5628 * Backend specific function is called to set up the output output.
5629 *
5630 * Output is added to the compositor's output list
5631 *
5632 * If the backend specific function fails, the weston_output object
5633 * is returned to a state it was before calling this function and
5634 * is added to the compositor's pending_output_list in case it needs
5635 * to be reconfigured or just so it can be destroyed at shutdown.
5636 *
5637 * 0 is returned on success, -1 on failure.
5638 */
5639WL_EXPORT int
5640weston_output_enable(struct weston_output *output)
5641{
Armin Krezović782f5df2016-09-30 14:11:11 +02005642 struct weston_compositor *c = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005643 struct weston_output *iterator;
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03005644 struct weston_head *head;
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02005645 char *head_names;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005646 int x = 0, y = 0;
5647
Pekka Paalanencc201e42017-03-30 15:11:25 +03005648 if (output->enabled) {
5649 weston_log("Error: attempt to enable an enabled output '%s'\n",
5650 output->name);
5651 return -1;
5652 }
5653
Pekka Paalanenddce54d2017-08-23 16:00:21 +03005654 if (wl_list_empty(&output->head_list)) {
5655 weston_log("Error: cannot enable output '%s' without heads.\n",
5656 output->name);
5657 return -1;
5658 }
5659
Pekka Paalanen586e1ac2017-09-14 16:17:59 +03005660 if (wl_list_empty(&output->mode_list) || !output->current_mode) {
5661 weston_log("Error: no video mode for output '%s'.\n",
5662 output->name);
5663 return -1;
5664 }
5665
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03005666 wl_list_for_each(head, &output->head_list, output_link) {
5667 assert(head->make);
5668 assert(head->model);
5669 }
5670
Armin Krezović782f5df2016-09-30 14:11:11 +02005671 iterator = container_of(c->output_list.prev,
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005672 struct weston_output, link);
5673
Armin Krezović782f5df2016-09-30 14:11:11 +02005674 if (!wl_list_empty(&c->output_list))
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005675 x = iterator->x + iterator->width;
5676
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005677 /* Make sure the scale is set up */
5678 assert(output->scale);
5679
5680 /* Make sure we have a transform set */
5681 assert(output->transform != UINT32_MAX);
5682
Armin Krezović782f5df2016-09-30 14:11:11 +02005683 output->x = x;
5684 output->y = y;
5685 output->dirty = 1;
5686 output->original_scale = output->scale;
5687
5688 weston_output_transform_scale_init(output, output->transform, output->scale);
5689 weston_output_init_zoom(output);
5690
5691 weston_output_init_geometry(output, x, y);
5692 weston_output_damage(output);
5693
5694 wl_signal_init(&output->frame_signal);
5695 wl_signal_init(&output->destroy_signal);
5696 wl_list_init(&output->animation_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02005697 wl_list_init(&output->feedback_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02005698
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005699 /* Enable the output (set up the crtc or create a
5700 * window representing the output, set up the
5701 * renderer, etc)
5702 */
5703 if (output->enable(output) < 0) {
5704 weston_log("Enabling output \"%s\" failed.\n", output->name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005705 return -1;
5706 }
5707
5708 weston_compositor_add_output(output->compositor, output);
5709
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02005710 head_names = weston_output_create_heads_string(output);
5711 weston_log("Output '%s' enabled with head(s) %s\n",
5712 output->name, head_names);
5713 free(head_names);
5714
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005715 return 0;
5716}
5717
5718/** Converts a weston_output object to a pending output state, so it
5719 ** can be configured again or destroyed.
5720 *
5721 * \param output The weston_output object that needs to be disabled.
5722 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005723 * Calls a backend specific function to disable an output, in case
5724 * such function exists.
5725 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03005726 * The backend specific disable function may choose to postpone the disabling
5727 * by returning a negative value, in which case this function returns early.
5728 * In that case the backend will guarantee the output will be disabled soon
5729 * by the backend calling this function again. One must not attempt to re-enable
5730 * the output until that happens.
5731 *
5732 * Otherwise, if the output is being used by the compositor, it is removed
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005733 * from weston's output_list (see weston_compositor_remove_output())
5734 * and is returned to a state it was before weston_output_enable()
5735 * was ran (see weston_output_enable_undo()).
5736 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03005737 * See weston_output_init() for more information on the
5738 * state output is returned to.
Pekka Paalanencc201e42017-03-30 15:11:25 +03005739 *
5740 * If the output has never been enabled yet, this function can still be
5741 * called to ensure that the output is actually turned off rather than left
5742 * in the state it was discovered in.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005743 */
5744WL_EXPORT void
5745weston_output_disable(struct weston_output *output)
5746{
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005747 /* Should we rename this? */
5748 output->destroying = 1;
5749
Pekka Paalanenc65df642017-03-29 15:45:46 +03005750 /* Disable is called unconditionally also for not-enabled outputs,
5751 * because at compositor start-up, if there is an output that is
5752 * already on but the compositor wants to turn it off, we have to
5753 * forward the turn-off to the backend so it knows to do it.
5754 * The backend cannot initially turn off everything, because it
5755 * would cause unnecessary mode-sets for all outputs the compositor
5756 * wants to be on.
5757 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005758 if (output->disable(output) < 0)
5759 return;
5760
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005761 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005762 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005763
5764 output->destroying = 0;
5765}
5766
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03005767/** Forces a synchronous call to heads_changed hook
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005768 *
5769 * \param compositor The compositor instance
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03005770 *
5771 * If there are new or changed heads, calls the heads_changed hook and
5772 * returns after the hook returns.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005773 */
5774WL_EXPORT void
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03005775weston_compositor_flush_heads_changed(struct weston_compositor *compositor)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005776{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005777 if (compositor->heads_changed_source) {
5778 wl_event_source_remove(compositor->heads_changed_source);
5779 weston_compositor_call_heads_changed(compositor);
5780 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005781}
5782
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02005783/** Add destroy callback for an output
5784 *
5785 * \param output The output to watch.
5786 * \param listener The listener to add. The \c notify member must be set.
5787 *
5788 * The listener callback will be called when user destroys an output. This
5789 * may be delayed by a backend in some cases. The main purpose of the
5790 * listener is to allow hooking up custom data to the output. The custom data
5791 * can be fetched via weston_output_get_destroy_listener() followed by
5792 * container_of().
5793 *
5794 * The \c data argument to the notify callback is the weston_output being
5795 * destroyed.
5796 *
5797 * @note This is for the final destruction of an output, not when it gets
5798 * disabled. If you want to keep track of enabled outputs, this is not it.
5799 */
5800WL_EXPORT void
5801weston_output_add_destroy_listener(struct weston_output *output,
5802 struct wl_listener *listener)
5803{
5804 wl_signal_add(&output->user_destroy_signal, listener);
5805}
5806
5807/** Look up destroy listener for an output
5808 *
5809 * \param output The output to query.
5810 * \param notify The notify function used used for the added destroy listener.
5811 * \return The listener, or NULL if not found.
5812 *
5813 * This looks up the previously added destroy listener struct based on the
5814 * notify function it has. The listener can be used to access user data
5815 * through \c container_of().
5816 *
5817 * \sa wl_signal_get() weston_output_add_destroy_listener()
5818 */
5819WL_EXPORT struct wl_listener *
5820weston_output_get_destroy_listener(struct weston_output *output,
5821 wl_notify_func_t notify)
5822{
5823 return wl_signal_get(&output->user_destroy_signal, notify);
5824}
5825
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005826/** Uninitialize an output
5827 *
5828 * Removes the output from the list of enabled outputs if necessary, but
5829 * does not call the backend's output disable function. The output will no
5830 * longer be in the list of pending outputs either.
5831 *
5832 * All fields of weston_output become uninitialized, i.e. should not be used
5833 * anymore. The caller can free the memory after this.
5834 *
5835 * \memberof weston_output
5836 * \internal
5837 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005838WL_EXPORT void
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03005839weston_output_release(struct weston_output *output)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005840{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005841 struct weston_head *head, *tmp;
5842
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005843 output->destroying = 1;
5844
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02005845 wl_signal_emit(&output->user_destroy_signal, output);
5846
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03005847 if (output->idle_repaint_source)
5848 wl_event_source_remove(output->idle_repaint_source);
5849
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005850 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005851 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005852
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005853 pixman_region32_fini(&output->region);
5854 pixman_region32_fini(&output->previous_damage);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005855 wl_list_remove(&output->link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005856
5857 wl_list_for_each_safe(head, tmp, &output->head_list, output_link)
5858 weston_head_detach(head);
5859
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005860 free(output->name);
5861}
5862
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02005863/** Find an output by its given name
5864 *
5865 * \param compositor The compositor to search in.
5866 * \param name The output name to search for.
5867 * \return An existing output with the given name, or NULL if not found.
5868 *
5869 * \memberof weston_compositor
5870 */
5871WL_EXPORT struct weston_output *
5872weston_compositor_find_output_by_name(struct weston_compositor *compositor,
5873 const char *name)
5874{
5875 struct weston_output *output;
5876
5877 wl_list_for_each(output, &compositor->output_list, link)
5878 if (strcmp(output->name, name) == 0)
5879 return output;
5880
5881 wl_list_for_each(output, &compositor->pending_output_list, link)
5882 if (strcmp(output->name, name) == 0)
5883 return output;
5884
5885 return NULL;
5886}
5887
5888/** Create a named output
5889 *
5890 * \param compositor The compositor.
5891 * \param name The name for the output.
5892 * \return A new \c weston_output, or NULL on failure.
5893 *
5894 * This creates a new weston_output that starts with no heads attached.
5895 *
5896 * An output must be configured and it must have at least one head before
5897 * it can be enabled.
5898 *
5899 * \memberof weston_compositor
5900 */
5901WL_EXPORT struct weston_output *
5902weston_compositor_create_output(struct weston_compositor *compositor,
5903 const char *name)
5904{
5905 assert(compositor->backend->create_output);
5906
5907 if (weston_compositor_find_output_by_name(compositor, name)) {
5908 weston_log("Warning: attempted to create an output with a "
5909 "duplicate name '%s'.\n", name);
5910 return NULL;
5911 }
5912
5913 return compositor->backend->create_output(compositor, name);
5914}
5915
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005916/** Create an output for an unused head
5917 *
5918 * \param compositor The compositor.
5919 * \param head The head to attach to the output.
5920 * \return A new \c weston_output, or NULL on failure.
5921 *
5922 * This creates a new weston_output that starts with the given head attached.
5923 * The output inherits the name of the head. The head must not be already
5924 * attached to another output.
5925 *
5926 * An output must be configured before it can be enabled.
5927 *
5928 * \memberof weston_compositor
5929 */
5930WL_EXPORT struct weston_output *
5931weston_compositor_create_output_with_head(struct weston_compositor *compositor,
5932 struct weston_head *head)
5933{
5934 struct weston_output *output;
5935
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02005936 output = weston_compositor_create_output(compositor, head->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005937 if (!output)
5938 return NULL;
5939
5940 if (weston_output_attach_head(output, head) < 0) {
Pekka Paalanen42c0e142017-10-27 12:07:49 +03005941 weston_output_destroy(output);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005942 return NULL;
5943 }
5944
5945 return output;
5946}
5947
5948/** Destroy an output
5949 *
5950 * \param output The output to destroy.
5951 *
5952 * The heads attached to the given output are detached and become unused again.
5953 *
5954 * It is not necessary to explicitly destroy all outputs at compositor exit.
5955 * weston_compositor_destroy() will automatically destroy any remaining
5956 * outputs.
5957 *
5958 * \memberof weston_output
5959 */
5960WL_EXPORT void
5961weston_output_destroy(struct weston_output *output)
5962{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005963 output->destroy(output);
5964}
5965
Pekka Paalanencf0a4762017-04-04 16:36:07 +03005966/** When you need a head...
5967 *
5968 * This function is a hack, used until all code has been converted to become
5969 * multi-head aware.
5970 *
5971 * \param output The weston_output whose head to get.
5972 * \return The first head in the output's list.
5973 */
5974WL_EXPORT struct weston_head *
5975weston_output_get_first_head(struct weston_output *output)
5976{
5977 if (wl_list_empty(&output->head_list))
5978 return NULL;
5979
5980 return container_of(output->head_list.next,
5981 struct weston_head, output_link);
5982}
5983
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01005984static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005985destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01005986{
Jonny Lamb74130762013-11-26 18:19:46 +01005987 struct weston_surface *surface =
5988 wl_resource_get_user_data(resource);
5989
Pekka Paalanen4826f872016-04-22 14:14:38 +03005990 if (!surface)
5991 return;
5992
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005993 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02005994 surface->pending.buffer_viewport.buffer.src_width =
5995 wl_fixed_from_int(-1);
5996 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02005997 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01005998}
5999
6000static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006001viewport_destroy(struct wl_client *client,
6002 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006003{
6004 wl_resource_destroy(resource);
6005}
6006
6007static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006008viewport_set_source(struct wl_client *client,
6009 struct wl_resource *resource,
6010 wl_fixed_t src_x,
6011 wl_fixed_t src_y,
6012 wl_fixed_t src_width,
6013 wl_fixed_t src_height)
6014{
6015 struct weston_surface *surface =
6016 wl_resource_get_user_data(resource);
6017
Pekka Paalanen4826f872016-04-22 14:14:38 +03006018 if (!surface) {
6019 wl_resource_post_error(resource,
6020 WP_VIEWPORT_ERROR_NO_SURFACE,
6021 "wl_surface for this viewport is no longer exists");
6022 return;
6023 }
6024
6025 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03006026 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006027
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006028 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03006029 src_height == wl_fixed_from_int(-1) &&
6030 src_x == wl_fixed_from_int(-1) &&
6031 src_y == wl_fixed_from_int(-1)) {
6032 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006033 surface->pending.buffer_viewport.buffer.src_width =
6034 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006035 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006036 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006037 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006038
Pekka Paalanen201769a2016-04-26 14:42:11 +03006039 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006040 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006041 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03006042 "wl_surface@%d viewport source "
6043 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
6044 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006045 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03006046 wl_fixed_to_double(src_height),
6047 wl_fixed_to_double(src_x),
6048 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006049 return;
6050 }
6051
6052 surface->pending.buffer_viewport.buffer.src_x = src_x;
6053 surface->pending.buffer_viewport.buffer.src_y = src_y;
6054 surface->pending.buffer_viewport.buffer.src_width = src_width;
6055 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006056 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006057}
6058
6059static void
6060viewport_set_destination(struct wl_client *client,
6061 struct wl_resource *resource,
6062 int32_t dst_width,
6063 int32_t dst_height)
6064{
6065 struct weston_surface *surface =
6066 wl_resource_get_user_data(resource);
6067
Pekka Paalanen4826f872016-04-22 14:14:38 +03006068 if (!surface) {
6069 wl_resource_post_error(resource,
6070 WP_VIEWPORT_ERROR_NO_SURFACE,
6071 "wl_surface for this viewport no longer exists");
6072 return;
6073 }
6074
6075 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006076
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006077 if (dst_width == -1 && dst_height == -1) {
6078 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006079 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006080 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006081 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006082 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006083
6084 if (dst_width <= 0 || dst_height <= 0) {
6085 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006086 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006087 "destination size must be positive (%dx%d)",
6088 dst_width, dst_height);
6089 return;
6090 }
6091
6092 surface->pending.buffer_viewport.surface.width = dst_width;
6093 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006094 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006095}
6096
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006097static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006098 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006099 viewport_set_source,
6100 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01006101};
6102
6103static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006104viewporter_destroy(struct wl_client *client,
6105 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006106{
6107 wl_resource_destroy(resource);
6108}
6109
6110static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006111viewporter_get_viewport(struct wl_client *client,
6112 struct wl_resource *viewporter,
6113 uint32_t id,
6114 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006115{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006116 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006117 struct weston_surface *surface =
6118 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006119 struct wl_resource *resource;
6120
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006121 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006122 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006123 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006124 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01006125 return;
6126 }
6127
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006128 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006129 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006130 if (resource == NULL) {
6131 wl_client_post_no_memory(client);
6132 return;
6133 }
6134
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006135 wl_resource_set_implementation(resource, &viewport_interface,
6136 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01006137
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006138 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006139}
6140
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006141static const struct wp_viewporter_interface viewporter_interface = {
6142 viewporter_destroy,
6143 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01006144};
6145
6146static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006147bind_viewporter(struct wl_client *client,
6148 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006149{
6150 struct wl_resource *resource;
6151
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006152 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006153 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006154 if (resource == NULL) {
6155 wl_client_post_no_memory(client);
6156 return;
6157 }
6158
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006159 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01006160 NULL, NULL);
6161}
6162
6163static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04006164destroy_presentation_feedback(struct wl_resource *feedback_resource)
6165{
6166 struct weston_presentation_feedback *feedback;
6167
6168 feedback = wl_resource_get_user_data(feedback_resource);
6169
6170 wl_list_remove(&feedback->link);
6171 free(feedback);
6172}
6173
6174static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006175presentation_destroy(struct wl_client *client, struct wl_resource *resource)
6176{
6177 wl_resource_destroy(resource);
6178}
6179
6180static void
6181presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04006182 struct wl_resource *presentation_resource,
6183 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006184 uint32_t callback)
6185{
Pekka Paalanen133e4392014-09-23 22:08:46 -04006186 struct weston_surface *surface;
6187 struct weston_presentation_feedback *feedback;
6188
6189 surface = wl_resource_get_user_data(surface_resource);
6190
Bryce Harringtonde16d892014-11-20 22:21:57 -08006191 feedback = zalloc(sizeof *feedback);
6192 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04006193 goto err_calloc;
6194
6195 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006196 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04006197 1, callback);
6198 if (!feedback->resource)
6199 goto err_create;
6200
6201 wl_resource_set_implementation(feedback->resource, NULL, feedback,
6202 destroy_presentation_feedback);
6203
6204 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
6205
6206 return;
6207
6208err_create:
6209 free(feedback);
6210
6211err_calloc:
6212 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006213}
6214
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006215static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006216 presentation_destroy,
6217 presentation_feedback
6218};
6219
6220static void
6221bind_presentation(struct wl_client *client,
6222 void *data, uint32_t version, uint32_t id)
6223{
6224 struct weston_compositor *compositor = data;
6225 struct wl_resource *resource;
6226
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006227 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006228 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006229 if (resource == NULL) {
6230 wl_client_post_no_memory(client);
6231 return;
6232 }
6233
6234 wl_resource_set_implementation(resource, &presentation_implementation,
6235 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006236 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006237}
6238
6239static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05006240compositor_bind(struct wl_client *client,
6241 void *data, uint32_t version, uint32_t id)
6242{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006243 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05006244 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05006245
Jason Ekstranda85118c2013-06-27 20:17:02 -05006246 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006247 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07006248 if (resource == NULL) {
6249 wl_client_post_no_memory(client);
6250 return;
6251 }
6252
6253 wl_resource_set_implementation(resource, &compositor_interface,
6254 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05006255}
6256
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006257WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006258weston_environment_get_fd(const char *env)
6259{
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07006260 char *e;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006261 int fd, flags;
6262
6263 e = getenv(env);
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07006264 if (!e || !safe_strtoint(e, &fd))
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006265 return -1;
6266
6267 flags = fcntl(fd, F_GETFD);
6268 if (flags == -1)
6269 return -1;
6270
6271 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
6272 unsetenv(env);
6273
6274 return fd;
6275}
6276
Pekka Paalanenb5026542014-11-12 15:09:24 +02006277static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02006278timeline_key_binding_handler(struct weston_keyboard *keyboard,
6279 const struct timespec *time, uint32_t key,
6280 void *data)
Pekka Paalanenb5026542014-11-12 15:09:24 +02006281{
6282 struct weston_compositor *compositor = data;
6283
6284 if (weston_timeline_enabled_)
6285 weston_timeline_close();
6286 else
6287 weston_timeline_open(compositor);
6288}
6289
Giulio Camuffo459137b2014-10-11 23:56:24 +03006290/** Create the compositor.
6291 *
6292 * This functions creates and initializes a compositor instance.
6293 *
6294 * \param display The Wayland display to be used.
6295 * \param user_data A pointer to an object that can later be retrieved
6296 * using the \ref weston_compositor_get_user_data function.
6297 * \return The compositor instance on success or NULL on failure.
6298 */
6299WL_EXPORT struct weston_compositor *
6300weston_compositor_create(struct wl_display *display, void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04006301{
Giulio Camuffo459137b2014-10-11 23:56:24 +03006302 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05006303 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07006304
Giulio Camuffo459137b2014-10-11 23:56:24 +03006305 ec = zalloc(sizeof *ec);
6306 if (!ec)
6307 return NULL;
6308
6309 ec->wl_display = display;
6310 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006311 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07006312 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006313 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006314 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006315 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006316 wl_signal_init(&ec->idle_signal);
6317 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006318 wl_signal_init(&ec->show_input_panel_signal);
6319 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02006320 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01006321 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01006322 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02006323 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006324 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02006325 wl_signal_init(&ec->output_resized_signal);
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03006326 wl_signal_init(&ec->heads_changed_signal);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02006327 wl_signal_init(&ec->output_heads_changed_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07006328 wl_signal_init(&ec->session_signal);
6329 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04006330
Casey Dahlin58ba1372012-04-19 22:50:08 -04006331 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03006332 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04006333
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02006334 ec->activate_serial = 1;
6335
Derek Foreman152254b2015-11-26 14:17:48 -06006336 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006337 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006338 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05006339
Giulio Camuffo954f1832014-10-11 18:27:30 +03006340 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006341 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006342 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03006343
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006344 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006345 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006346 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006347
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006348 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006349 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006350 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006351
Jonas Ådahl30d61d82014-10-22 21:21:17 +02006352 if (weston_input_init(ec) != 0)
6353 goto fail;
6354
Jason Ekstranda7af7042013-10-12 22:38:11 -05006355 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02006356 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006357 wl_list_init(&ec->layer_list);
6358 wl_list_init(&ec->seat_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006359 wl_list_init(&ec->pending_output_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006360 wl_list_init(&ec->output_list);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03006361 wl_list_init(&ec->head_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006362 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01006363 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006364 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01006365 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006366 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006367 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006368
Pekka Paalanen827b5d22016-06-29 11:54:26 +02006369 wl_list_init(&ec->plugin_api_list);
6370
Xiong Zhang97116532013-10-23 13:58:31 +08006371 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02006372 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04006373
Giulio Camuffo459137b2014-10-11 23:56:24 +03006374 wl_data_device_manager_init(ec->wl_display);
6375
6376 wl_display_init_shm(ec->wl_display);
6377
6378 loop = wl_display_get_event_loop(ec->wl_display);
6379 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Daniel Stone6847b852017-03-01 11:34:08 +00006380 ec->repaint_timer =
6381 wl_event_loop_add_timer(loop, output_repaint_timer_handler,
6382 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03006383
Quentin Glidic82681572016-12-17 13:40:51 +01006384 weston_layer_init(&ec->fade_layer, ec);
6385 weston_layer_init(&ec->cursor_layer, ec);
6386
6387 weston_layer_set_position(&ec->fade_layer, WESTON_LAYER_POSITION_FADE);
6388 weston_layer_set_position(&ec->cursor_layer,
6389 WESTON_LAYER_POSITION_CURSOR);
Giulio Camuffo459137b2014-10-11 23:56:24 +03006390
6391 weston_compositor_add_debug_binding(ec, KEY_T,
6392 timeline_key_binding_handler, ec);
6393
Giulio Camuffo459137b2014-10-11 23:56:24 +03006394 return ec;
6395
6396fail:
6397 free(ec);
6398 return NULL;
6399}
6400
Benjamin Franzkeb8263022011-08-30 11:32:47 +02006401WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006402weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07006403{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006404 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07006405
Pekka Paalanend1591ae2012-01-02 16:06:56 +02006406 wl_event_source_remove(ec->idle_source);
6407
Matt Roper361d2ad2011-08-29 13:52:23 -07006408 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02006409 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07006410 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02006411
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006412 /* Destroy all pending outputs associated with this compositor */
6413 wl_list_for_each_safe(output, next, &ec->pending_output_list, link)
6414 output->destroy(output);
6415
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02006416 if (ec->renderer)
6417 ec->renderer->destroy(ec);
6418
Daniel Stone325fc2d2012-05-30 16:31:58 +01006419 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09006420 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006421 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01006422 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006423 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006424 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02006425
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04006426 weston_plane_release(&ec->primary_plane);
Matt Roper361d2ad2011-08-29 13:52:23 -07006427}
6428
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05006429WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04006430weston_compositor_exit_with_code(struct weston_compositor *compositor,
6431 int exit_code)
6432{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02006433 if (compositor->exit_code == EXIT_SUCCESS)
6434 compositor->exit_code = exit_code;
6435
Giulio Camuffo459137b2014-10-11 23:56:24 +03006436 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04006437}
6438
6439WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01006440weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
6441 const struct weston_pointer_grab_interface *interface)
6442{
6443 struct weston_seat *seat;
6444
6445 ec->default_pointer_grab = interface;
6446 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05006447 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
6448
6449 if (pointer)
6450 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01006451 }
6452}
6453
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04006454WL_EXPORT int
6455weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
6456 clockid_t clk_id)
6457{
6458 struct timespec ts;
6459
6460 if (clock_gettime(clk_id, &ts) < 0)
6461 return -1;
6462
6463 compositor->presentation_clock = clk_id;
6464
6465 return 0;
6466}
6467
6468/*
6469 * For choosing the software clock, when the display hardware or API
6470 * does not expose a compatible presentation timestamp.
6471 */
6472WL_EXPORT int
6473weston_compositor_set_presentation_clock_software(
6474 struct weston_compositor *compositor)
6475{
6476 /* In order of preference */
6477 static const clockid_t clocks[] = {
6478 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
6479 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
6480 CLOCK_MONOTONIC, /* no jumps, may crawl */
6481 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
6482 CLOCK_REALTIME /* may jump and crawl */
6483 };
6484 unsigned i;
6485
6486 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
6487 if (weston_compositor_set_presentation_clock(compositor,
6488 clocks[i]) == 0)
6489 return 0;
6490
6491 weston_log("Error: no suitable presentation clock available.\n");
6492
6493 return -1;
6494}
6495
Pekka Paalanen662f3842015-03-18 12:17:26 +02006496/** Read the current time from the Presentation clock
6497 *
6498 * \param compositor
6499 * \param ts[out] The current time.
6500 *
6501 * \note Reading the current time in user space is always imprecise to some
6502 * degree.
6503 *
6504 * This function is never meant to fail. If reading the clock does fail,
6505 * an error message is logged and a zero time is returned. Callers are not
6506 * supposed to detect or react to failures.
6507 */
6508WL_EXPORT void
6509weston_compositor_read_presentation_clock(
6510 const struct weston_compositor *compositor,
6511 struct timespec *ts)
6512{
6513 static bool warned;
6514 int ret;
6515
6516 ret = clock_gettime(compositor->presentation_clock, ts);
6517 if (ret < 0) {
6518 ts->tv_sec = 0;
6519 ts->tv_nsec = 0;
6520
6521 if (!warned)
6522 weston_log("Error: failure to read "
6523 "the presentation clock %#x: '%m' (%d)\n",
6524 compositor->presentation_clock, errno);
6525 warned = true;
6526 }
6527}
6528
Pekka Paalanen230f3b12014-09-29 14:18:40 -04006529/** Import dmabuf buffer into current renderer
6530 *
6531 * \param compositor
6532 * \param buffer the dmabuf buffer to import
6533 * \return true on usable buffers, false otherwise
6534 *
6535 * This function tests that the linux_dmabuf_buffer is usable
6536 * for the current renderer. Returns false on unusable buffers. Usually
6537 * usability is tested by importing the dmabufs for composition.
6538 *
6539 * This hook is also used for detecting if the renderer supports
6540 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
6541 * supported.
6542 * */
6543WL_EXPORT bool
6544weston_compositor_import_dmabuf(struct weston_compositor *compositor,
6545 struct linux_dmabuf_buffer *buffer)
6546{
6547 struct weston_renderer *renderer;
6548
6549 renderer = compositor->renderer;
6550
6551 if (renderer->import_dmabuf == NULL)
6552 return false;
6553
6554 return renderer->import_dmabuf(compositor, buffer);
6555}
6556
Giulio Camuffocdb4d292013-11-14 23:42:53 +01006557WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05006558weston_version(int *major, int *minor, int *micro)
6559{
6560 *major = WESTON_VERSION_MAJOR;
6561 *minor = WESTON_VERSION_MINOR;
6562 *micro = WESTON_VERSION_MICRO;
6563}
6564
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03006565WL_EXPORT void *
6566weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006567{
Derek Foreman3f86e502015-06-08 11:46:54 -05006568 const char *builddir = getenv("WESTON_BUILD_DIR");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006569 char path[PATH_MAX];
6570 void *module, *init;
Daniel Stonebeb97e52016-11-28 12:13:54 +00006571 size_t len;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006572
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08006573 if (name == NULL)
6574 return NULL;
6575
Derek Foreman3f86e502015-06-08 11:46:54 -05006576 if (name[0] != '/') {
6577 if (builddir)
Daniel Stonebeb97e52016-11-28 12:13:54 +00006578 len = snprintf(path, sizeof path, "%s/.libs/%s",
6579 builddir, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05006580 else
Daniel Stonebeb97e52016-11-28 12:13:54 +00006581 len = snprintf(path, sizeof path, "%s/%s",
6582 LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05006583 } else {
Daniel Stonebeb97e52016-11-28 12:13:54 +00006584 len = snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05006585 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006586
Daniel Stonebeb97e52016-11-28 12:13:54 +00006587 /* snprintf returns the length of the string it would've written,
6588 * _excluding_ the NUL byte. So even being equal to the size of
6589 * our buffer is an error here. */
6590 if (len >= sizeof path)
6591 return NULL;
6592
Kristian Høgsberga6813d22012-09-12 12:21:01 -04006593 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
6594 if (module) {
6595 weston_log("Module '%s' already loaded\n", path);
6596 dlclose(module);
6597 return NULL;
6598 }
6599
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03006600 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04006601 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006602 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03006603 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006604 return NULL;
6605 }
6606
6607 init = dlsym(module, entrypoint);
6608 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03006609 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00006610 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006611 return NULL;
6612 }
6613
6614 return init;
6615}
6616
Giulio Camuffo459137b2014-10-11 23:56:24 +03006617
6618/** Destroys the compositor.
6619 *
6620 * This function cleans up the compositor state and destroys it.
6621 *
6622 * \param compositor The compositor to be destroyed.
6623 */
6624WL_EXPORT void
6625weston_compositor_destroy(struct weston_compositor *compositor)
6626{
6627 /* prevent further rendering while shutting down */
6628 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
6629
6630 wl_signal_emit(&compositor->destroy_signal, compositor);
6631
6632 weston_compositor_xkb_destroy(compositor);
6633
Giulio Camuffo2d24e642015-10-03 16:25:15 +03006634 if (compositor->backend)
6635 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02006636
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03006637 /* The backend is responsible for destroying the heads. */
6638 assert(wl_list_empty(&compositor->head_list));
6639
Pekka Paalanen827b5d22016-06-29 11:54:26 +02006640 weston_plugin_api_destroy_list(compositor);
6641
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03006642 if (compositor->heads_changed_source)
6643 wl_event_source_remove(compositor->heads_changed_source);
6644
Giulio Camuffo459137b2014-10-11 23:56:24 +03006645 free(compositor);
6646}
6647
6648/** Instruct the compositor to exit.
6649 *
6650 * This functions does not directly destroy the compositor object, it merely
6651 * command it to start the tear down process. It is not guaranteed that the
6652 * tear down will happen immediately.
6653 *
6654 * \param compositor The compositor to tear down.
6655 */
6656WL_EXPORT void
6657weston_compositor_exit(struct weston_compositor *compositor)
6658{
6659 compositor->exit(compositor);
6660}
6661
6662/** Return the user data stored in the compositor.
6663 *
6664 * This function returns the user data pointer set with user_data parameter
6665 * to the \ref weston_compositor_create function.
6666 */
6667WL_EXPORT void *
6668weston_compositor_get_user_data(struct weston_compositor *compositor)
6669{
6670 return compositor->user_data;
6671}
Pekka Paalanendd186732016-06-03 14:49:54 +03006672
Pekka Paalanen50dbf382016-06-03 15:23:46 +03006673static const char * const backend_map[] = {
6674 [WESTON_BACKEND_DRM] = "drm-backend.so",
6675 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
6676 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
6677 [WESTON_BACKEND_RDP] = "rdp-backend.so",
6678 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
6679 [WESTON_BACKEND_X11] = "x11-backend.so",
6680};
6681
Pekka Paalanendd186732016-06-03 14:49:54 +03006682/** Load a backend into a weston_compositor
6683 *
6684 * A backend must be loaded to make a weston_compositor work. A backend
6685 * provides input and output capabilities, and determines the renderer to use.
6686 *
6687 * \param compositor A compositor that has not had a backend loaded yet.
6688 * \param backend Name of the backend file.
6689 * \param config_base A pointer to a backend-specific configuration
6690 * structure's 'base' member.
6691 *
6692 * \return 0 on success, or -1 on error.
6693 */
6694WL_EXPORT int
6695weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03006696 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03006697 struct weston_backend_config *config_base)
6698{
6699 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03006700 struct weston_backend_config *config_base);
6701
Pekka Paalanend7e35112017-08-29 17:04:12 +03006702 if (compositor->backend) {
6703 weston_log("Error: attempt to load a backend when one is already loaded\n");
6704 return -1;
6705 }
6706
Quentin Glidic887c0182016-07-10 11:00:53 +02006707 if (backend >= ARRAY_LENGTH(backend_map))
Pekka Paalanen50dbf382016-06-03 15:23:46 +03006708 return -1;
6709
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01006710 backend_init = weston_load_module(backend_map[backend], "weston_backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03006711 if (!backend_init)
6712 return -1;
6713
Pekka Paalanend7e35112017-08-29 17:04:12 +03006714 if (backend_init(compositor, config_base) < 0) {
6715 compositor->backend = NULL;
6716 return -1;
6717 }
6718
6719 return 0;
Pekka Paalanendd186732016-06-03 14:49:54 +03006720}
Giulio Camuffo9c764df2016-06-29 11:54:27 +02006721
6722WL_EXPORT int
6723weston_compositor_load_xwayland(struct weston_compositor *compositor)
6724{
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01006725 int (*module_init)(struct weston_compositor *ec);
Giulio Camuffo9c764df2016-06-29 11:54:27 +02006726
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01006727 module_init = weston_load_module("xwayland.so", "weston_module_init");
Giulio Camuffo9c764df2016-06-29 11:54:27 +02006728 if (!module_init)
6729 return -1;
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01006730 if (module_init(compositor) < 0)
Giulio Camuffo9c764df2016-06-29 11:54:27 +02006731 return -1;
6732 return 0;
6733}