blob: 016165505fb00fe733fc6c9bed4e1c23eec4eb24 [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);
Pekka Paalanen944fae82018-05-22 13:15:58 +03001934 weston_view_set_output(view, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001935
Jason Ekstranda7af7042013-10-12 22:38:11 -05001936 wl_list_remove(&view->surface_link);
1937
1938 free(view);
1939}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001940
1941WL_EXPORT void
1942weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001943{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001944 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001945 struct weston_view *ev, *nv;
Jonas Ådahld3414f22016-07-22 17:56:31 +08001946 struct weston_pointer_constraint *constraint, *next_constraint;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001947
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001948 if (--surface->ref_count > 0)
1949 return;
1950
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001951 assert(surface->resource == NULL);
1952
Pekka Paalanenca790762015-04-17 14:23:38 +03001953 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001954
Pekka Paalanene67858b2013-04-25 13:57:42 +03001955 assert(wl_list_empty(&surface->subsurface_list_pending));
1956 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001957
Jason Ekstranda7af7042013-10-12 22:38:11 -05001958 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1959 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001960
Jason Ekstrand7b982072014-05-20 14:33:03 -05001961 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001962
Pekka Paalanende685b82012-12-04 15:58:12 +02001963 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001964
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001965 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001966 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001967 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001968
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001969 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001970 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001971
Pekka Paalanen133e4392014-09-23 22:08:46 -04001972 weston_presentation_feedback_discard_list(&surface->feedback_list);
1973
Jonas Ådahld3414f22016-07-22 17:56:31 +08001974 wl_list_for_each_safe(constraint, next_constraint,
1975 &surface->pointer_constraints,
1976 link)
1977 weston_pointer_constraint_destroy(constraint);
1978
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001979 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001980}
1981
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001982static void
1983destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001984{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001985 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001986
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001987 assert(surface);
1988
Giulio Camuffo0d379742013-11-15 22:06:15 +01001989 /* Set the resource to NULL, since we don't want to leave a
1990 * dangling pointer if the surface was refcounted and survives
1991 * the weston_surface_destroy() call. */
1992 surface->resource = NULL;
Pekka Paalanen4826f872016-04-22 14:14:38 +03001993
1994 if (surface->viewport_resource)
1995 wl_resource_set_user_data(surface->viewport_resource, NULL);
1996
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001997 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001998}
1999
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002000static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002001weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
2002{
2003 struct weston_buffer *buffer =
2004 container_of(listener, struct weston_buffer, destroy_listener);
2005
2006 wl_signal_emit(&buffer->destroy_signal, buffer);
2007 free(buffer);
2008}
2009
Giulio Camuffoe058cd12013-12-12 14:14:29 +01002010WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002011weston_buffer_from_resource(struct wl_resource *resource)
2012{
2013 struct weston_buffer *buffer;
2014 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002015
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002016 listener = wl_resource_get_destroy_listener(resource,
2017 weston_buffer_destroy_handler);
2018
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002019 if (listener)
2020 return container_of(listener, struct weston_buffer,
2021 destroy_listener);
2022
2023 buffer = zalloc(sizeof *buffer);
2024 if (buffer == NULL)
2025 return NULL;
2026
2027 buffer->resource = resource;
2028 wl_signal_init(&buffer->destroy_signal);
2029 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04002030 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002031 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002032
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002033 return buffer;
2034}
2035
2036static void
Pekka Paalanende685b82012-12-04 15:58:12 +02002037weston_buffer_reference_handle_destroy(struct wl_listener *listener,
2038 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002039{
Pekka Paalanende685b82012-12-04 15:58:12 +02002040 struct weston_buffer_reference *ref =
2041 container_of(listener, struct weston_buffer_reference,
2042 destroy_listener);
2043
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002044 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02002045 ref->buffer = NULL;
2046}
2047
2048WL_EXPORT void
2049weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002050 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002051{
2052 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05002053 ref->buffer->busy_count--;
2054 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002055 assert(wl_resource_get_client(ref->buffer->resource));
Matt Hoosier3052bc72017-09-26 08:09:40 -05002056 wl_buffer_send_release(ref->buffer->resource);
Kristian Høgsberg20347802013-03-04 12:07:46 -05002057 }
Pekka Paalanende685b82012-12-04 15:58:12 +02002058 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002059 }
2060
Pekka Paalanende685b82012-12-04 15:58:12 +02002061 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002062 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002063 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02002064 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02002065 }
2066
Pekka Paalanende685b82012-12-04 15:58:12 +02002067 ref->buffer = buffer;
2068 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
2069}
2070
2071static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002072weston_surface_attach(struct weston_surface *surface,
2073 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002074{
2075 weston_buffer_reference(&surface->buffer_ref, buffer);
2076
Pekka Paalanena6421c42012-12-04 15:58:10 +02002077 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002078 if (weston_surface_is_mapped(surface))
2079 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002080 }
2081
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002082 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02002083
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002084 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002085 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002086}
2087
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002088WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002089weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002090{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002091 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002092
2093 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002094 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002095}
2096
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05002097WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002098weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002099{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002100 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002101
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002102 pixman_region32_union(&compositor->primary_plane.damage,
2103 &compositor->primary_plane.damage,
2104 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002105 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002106}
2107
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04002108static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002109surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002110{
Pekka Paalanende685b82012-12-04 15:58:12 +02002111 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002112 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002113 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002114
Pekka Paalanenb5026542014-11-12 15:09:24 +02002115 if (weston_timeline_enabled_ &&
2116 pixman_region32_not_empty(&surface->damage))
2117 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
2118 TLP_OUTPUT(surface->output), TLP_END);
2119
Jason Ekstrandef540082014-06-26 10:37:36 -07002120 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002121}
2122
2123static void
2124view_accumulate_damage(struct weston_view *view,
2125 pixman_region32_t *opaque)
2126{
2127 pixman_region32_t damage;
2128
2129 pixman_region32_init(&damage);
2130 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002131 pixman_box32_t *extents;
2132
Jason Ekstranda7af7042013-10-12 22:38:11 -05002133 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002134 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002135 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002136 pixman_region32_copy(&damage, &view->surface->damage);
2137 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002138 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002139 }
2140
Pekka Paalanen380adf52015-02-16 14:39:11 +02002141 pixman_region32_intersect(&damage, &damage,
2142 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002143 pixman_region32_subtract(&damage, &damage, opaque);
2144 pixman_region32_union(&view->plane->damage,
2145 &view->plane->damage, &damage);
2146 pixman_region32_fini(&damage);
2147 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002148 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002149}
2150
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002151static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002152compositor_accumulate_damage(struct weston_compositor *ec)
2153{
2154 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002155 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002156 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002157
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002158 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002159
2160 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002161 pixman_region32_copy(&plane->clip, &clip);
2162
2163 pixman_region32_init(&opaque);
2164
Jason Ekstranda7af7042013-10-12 22:38:11 -05002165 wl_list_for_each(ev, &ec->view_list, link) {
2166 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002167 continue;
2168
Jason Ekstranda7af7042013-10-12 22:38:11 -05002169 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002170 }
2171
2172 pixman_region32_union(&clip, &clip, &opaque);
2173 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002174 }
2175
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002176 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002177
Jason Ekstranda7af7042013-10-12 22:38:11 -05002178 wl_list_for_each(ev, &ec->view_list, link)
Derek Foreman060cf112015-11-18 16:32:26 -06002179 ev->surface->touched = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002180
2181 wl_list_for_each(ev, &ec->view_list, link) {
2182 if (ev->surface->touched)
2183 continue;
Derek Foreman060cf112015-11-18 16:32:26 -06002184 ev->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002185
2186 surface_flush_damage(ev->surface);
2187
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002188 /* Both the renderer and the backend have seen the buffer
2189 * by now. If renderer needs the buffer, it has its own
2190 * reference set. If the backend wants to keep the buffer
2191 * around for migrating the surface into a non-primary plane
2192 * later, keep_buffer is true. Otherwise, drop the core
2193 * reference now, and allow early buffer release. This enables
2194 * clients to use single-buffering.
2195 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002196 if (!ev->surface->keep_buffer)
2197 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002198 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002199}
2200
2201static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002202surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002203{
2204 struct weston_subsurface *sub;
2205
Pekka Paalanene67858b2013-04-25 13:57:42 +03002206 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002207 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002208 continue;
2209
Jason Ekstranda7af7042013-10-12 22:38:11 -05002210 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2211 wl_list_init(&sub->surface->views);
2212
2213 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002214 }
2215}
2216
2217static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002218surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002219{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002220 struct weston_subsurface *sub;
2221 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002222
Jason Ekstranda7af7042013-10-12 22:38:11 -05002223 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2224 if (sub->surface == surface)
2225 continue;
2226
George Kiagiadakised04d382014-06-13 18:10:26 +02002227 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2228 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002229 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002230 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002231
2232 surface_free_unused_subsurface_views(sub->surface);
2233 }
2234}
2235
2236static void
2237view_list_add_subsurface_view(struct weston_compositor *compositor,
2238 struct weston_subsurface *sub,
2239 struct weston_view *parent)
2240{
2241 struct weston_subsurface *child;
2242 struct weston_view *view = NULL, *iv;
2243
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002244 if (!weston_surface_is_mapped(sub->surface))
2245 return;
2246
Jason Ekstranda7af7042013-10-12 22:38:11 -05002247 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2248 if (iv->geometry.parent == parent) {
2249 view = iv;
2250 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002251 }
2252 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002253
2254 if (view) {
2255 /* Put it back in the surface's list of views */
2256 wl_list_remove(&view->surface_link);
2257 wl_list_insert(&sub->surface->views, &view->surface_link);
2258 } else {
2259 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002260 weston_view_set_position(view,
2261 sub->position.x,
2262 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002263 weston_view_set_transform_parent(view, parent);
2264 }
2265
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002266 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002267 weston_view_update_transform(view);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002268 view->is_mapped = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002269
Pekka Paalanenb188e912013-11-19 14:03:35 +02002270 if (wl_list_empty(&sub->surface->subsurface_list)) {
2271 wl_list_insert(compositor->view_list.prev, &view->link);
2272 return;
2273 }
2274
2275 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2276 if (child->surface == sub->surface)
2277 wl_list_insert(compositor->view_list.prev, &view->link);
2278 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002279 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002280 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002281}
2282
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002283/* This recursively adds the sub-surfaces for a view, relying on the
2284 * sub-surface order. Thus, if a client restacks the sub-surfaces, that
2285 * change first happens to the sub-surface list, and then automatically
2286 * propagates here. See weston_surface_damage_subsurfaces() for how the
2287 * sub-surfaces receive damage when the client changes the state.
2288 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002289static void
2290view_list_add(struct weston_compositor *compositor,
2291 struct weston_view *view)
2292{
2293 struct weston_subsurface *sub;
2294
2295 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002296
Pekka Paalanenb188e912013-11-19 14:03:35 +02002297 if (wl_list_empty(&view->surface->subsurface_list)) {
2298 wl_list_insert(compositor->view_list.prev, &view->link);
2299 return;
2300 }
2301
2302 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2303 if (sub->surface == view->surface)
2304 wl_list_insert(compositor->view_list.prev, &view->link);
2305 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002306 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002307 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002308}
2309
2310static void
2311weston_compositor_build_view_list(struct weston_compositor *compositor)
2312{
2313 struct weston_view *view;
2314 struct weston_layer *layer;
2315
2316 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002317 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002318 surface_stash_subsurface_views(view->surface);
2319
2320 wl_list_init(&compositor->view_list);
2321 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002322 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002323 view_list_add(compositor, view);
2324 }
2325 }
2326
2327 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002328 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002329 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002330}
2331
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002332static void
2333weston_output_take_feedback_list(struct weston_output *output,
2334 struct weston_surface *surface)
2335{
2336 struct weston_view *view;
2337 struct weston_presentation_feedback *feedback;
2338 uint32_t flags = 0xffffffff;
2339
2340 if (wl_list_empty(&surface->feedback_list))
2341 return;
2342
2343 /* All views must have the flag for the flag to survive. */
2344 wl_list_for_each(view, &surface->views, surface_link) {
2345 /* ignore views that are not on this output at all */
2346 if (view->output_mask & (1u << output->id))
2347 flags &= view->psf_flags;
2348 }
2349
2350 wl_list_for_each(feedback, &surface->feedback_list, link)
2351 feedback->psf_flags = flags;
2352
2353 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2354 wl_list_init(&surface->feedback_list);
2355}
2356
David Herrmann1edf44c2013-10-22 17:11:26 +02002357static int
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002358weston_output_repaint(struct weston_output *output, void *repaint_data)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002359{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002360 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002361 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002362 struct weston_animation *animation, *next;
2363 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002364 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002365 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002366 int r;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002367 uint32_t frame_time_msec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002368
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002369 if (output->destroying)
2370 return 0;
2371
Pekka Paalanenb5026542014-11-12 15:09:24 +02002372 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2373
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002374 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002375 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002376
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002377 if (output->assign_planes && !output->disable_planes) {
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002378 output->assign_planes(output, repaint_data);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002379 } else {
2380 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002381 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002382 ev->psf_flags = 0;
2383 }
2384 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002385
Pekka Paalanene67858b2013-04-25 13:57:42 +03002386 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002387 wl_list_for_each(ev, &ec->view_list, link) {
2388 /* Note: This operation is safe to do multiple times on the
2389 * same surface.
2390 */
2391 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002392 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002393 &ev->surface->frame_callback_list);
2394 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002395
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002396 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002397 }
2398 }
2399
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002400 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002401
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002402 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002403 pixman_region32_intersect(&output_damage,
2404 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002405 pixman_region32_subtract(&output_damage,
2406 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002407
Scott Moreauccbf29d2012-02-22 14:21:41 -07002408 if (output->dirty)
2409 weston_output_update_matrix(output);
2410
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002411 r = output->repaint(output, &output_damage, repaint_data);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002412
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002413 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002414
Daniel Stone09a97e22017-03-01 11:34:06 +00002415 output->repaint_needed = false;
Daniel Stone05df8c12017-03-03 16:59:42 +00002416 if (r == 0)
2417 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002418
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002419 weston_compositor_repick(ec);
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002420
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002421 frame_time_msec = timespec_to_msec(&output->frame_time);
2422
Jonas Ådahldb773762012-06-13 00:01:21 +02002423 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002424 wl_callback_send_done(cb->resource, frame_time_msec);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002425 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002426 }
2427
Scott Moreaud64cf212012-06-08 19:40:54 -06002428 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002429 animation->frame_counter++;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002430 animation->frame(animation, output, &output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002431 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002432
Pekka Paalanenb5026542014-11-12 15:09:24 +02002433 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2434
David Herrmann1edf44c2013-10-22 17:11:26 +02002435 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002436}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002437
Pekka Paalanen82919792014-05-21 13:51:49 +03002438static void
2439weston_output_schedule_repaint_reset(struct weston_output *output)
2440{
Daniel Stone05df8c12017-03-03 16:59:42 +00002441 output->repaint_status = REPAINT_NOT_SCHEDULED;
Pekka Paalanen82919792014-05-21 13:51:49 +03002442 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
Pekka Paalanen82919792014-05-21 13:51:49 +03002443}
2444
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002445static int
2446weston_output_maybe_repaint(struct weston_output *output, struct timespec *now,
2447 void *repaint_data)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002448{
Pekka Paalanen0513a952014-05-21 16:17:27 +03002449 struct weston_compositor *compositor = output->compositor;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002450 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002451 int64_t msec_to_repaint;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002452
Daniel Stone6847b852017-03-01 11:34:08 +00002453 /* We're not ready yet; come back to make a decision later. */
2454 if (output->repaint_status != REPAINT_SCHEDULED)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002455 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002456
2457 msec_to_repaint = timespec_sub_to_msec(&output->next_repaint, now);
2458 if (msec_to_repaint > 1)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002459 return ret;
Daniel Stone05df8c12017-03-03 16:59:42 +00002460
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002461 /* If we're sleeping, drop the repaint machinery entirely; we will
2462 * explicitly repaint all outputs when we come back. */
2463 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2464 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
2465 goto err;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002466
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002467 /* We don't actually need to repaint this output; drop it from
2468 * repaint until something causes damage. */
2469 if (!output->repaint_needed)
2470 goto err;
2471
2472 /* If repaint fails, we aren't going to get weston_output_finish_frame
2473 * to trigger a new repaint, so drop it from repaint and hope
Daniel Stone6847b852017-03-01 11:34:08 +00002474 * something schedules a successful repaint later. As repainting may
2475 * take some time, re-read our clock as a courtesy to the next
2476 * output. */
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002477 ret = weston_output_repaint(output, repaint_data);
Daniel Stone6847b852017-03-01 11:34:08 +00002478 weston_compositor_read_presentation_clock(compositor, now);
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002479 if (ret != 0)
2480 goto err;
2481
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09002482 output->repainted = true;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002483 return ret;
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002484
2485err:
Pekka Paalanen0513a952014-05-21 16:17:27 +03002486 weston_output_schedule_repaint_reset(output);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002487 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002488}
2489
2490static void
2491output_repaint_timer_arm(struct weston_compositor *compositor)
2492{
2493 struct weston_output *output;
2494 bool any_should_repaint = false;
2495 struct timespec now;
Sergi Granellb4c08862017-03-18 13:01:15 +01002496 int64_t msec_to_next = INT64_MAX;
Daniel Stone6847b852017-03-01 11:34:08 +00002497
2498 weston_compositor_read_presentation_clock(compositor, &now);
2499
2500 wl_list_for_each(output, &compositor->output_list, link) {
2501 int64_t msec_to_this;
2502
2503 if (output->repaint_status != REPAINT_SCHEDULED)
2504 continue;
2505
2506 msec_to_this = timespec_sub_to_msec(&output->next_repaint,
2507 &now);
2508 if (!any_should_repaint || msec_to_this < msec_to_next)
2509 msec_to_next = msec_to_this;
2510
2511 any_should_repaint = true;
2512 }
2513
2514 if (!any_should_repaint)
2515 return;
2516
2517 /* Even if we should repaint immediately, add the minimum 1 ms delay.
2518 * This is a workaround to allow coalescing multiple output repaints
2519 * particularly from weston_output_finish_frame()
2520 * into the same call, which would not happen if we called
2521 * output_repaint_timer_handler() directly.
2522 */
2523 if (msec_to_next < 1)
2524 msec_to_next = 1;
2525
2526 wl_event_source_timer_update(compositor->repaint_timer, msec_to_next);
2527}
2528
2529static int
2530output_repaint_timer_handler(void *data)
2531{
2532 struct weston_compositor *compositor = data;
2533 struct weston_output *output;
2534 struct timespec now;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002535 void *repaint_data = NULL;
Emre Ucane479ed82018-03-20 15:29:40 +01002536 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002537
2538 weston_compositor_read_presentation_clock(compositor, &now);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002539
2540 if (compositor->backend->repaint_begin)
2541 repaint_data = compositor->backend->repaint_begin(compositor);
2542
2543 wl_list_for_each(output, &compositor->output_list, link) {
2544 ret = weston_output_maybe_repaint(output, &now, repaint_data);
2545 if (ret)
2546 break;
2547 }
2548
2549 if (ret == 0) {
Tomohito Esaki09bfcd62018-06-05 10:37:05 +09002550 if (compositor->backend->repaint_flush)
2551 compositor->backend->repaint_flush(compositor,
2552 repaint_data);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002553 } else {
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09002554 wl_list_for_each(output, &compositor->output_list, link) {
2555 if (output->repainted)
2556 weston_output_schedule_repaint_reset(output);
2557 }
2558
Tomohito Esaki09bfcd62018-06-05 10:37:05 +09002559 if (compositor->backend->repaint_cancel)
2560 compositor->backend->repaint_cancel(compositor,
2561 repaint_data);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002562 }
Daniel Stone6847b852017-03-01 11:34:08 +00002563
Tomohito Esakiddaf95c2018-07-10 11:47:15 +09002564 wl_list_for_each(output, &compositor->output_list, link)
2565 output->repainted = false;
2566
Daniel Stone6847b852017-03-01 11:34:08 +00002567 output_repaint_timer_arm(compositor);
2568
Pekka Paalanen0513a952014-05-21 16:17:27 +03002569 return 0;
2570}
2571
Kristian Høgsbergef044142011-06-21 15:02:12 -04002572WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002573weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002574 const struct timespec *stamp,
2575 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002576{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002577 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002578 int32_t refresh_nsec;
2579 struct timespec now;
Daniel Stone6847b852017-03-01 11:34:08 +00002580 int64_t msec_rel;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002581
Pekka Paalanenb5026542014-11-12 15:09:24 +02002582
Daniel Stone05df8c12017-03-03 16:59:42 +00002583 assert(output->repaint_status == REPAINT_AWAITING_COMPLETION);
Daniel Stone3615ce12017-03-01 11:34:05 +00002584 assert(stamp || (presented_flags & WP_PRESENTATION_FEEDBACK_INVALID));
2585
Daniel Stone6847b852017-03-01 11:34:08 +00002586 weston_compositor_read_presentation_clock(compositor, &now);
2587
Daniel Stone3615ce12017-03-01 11:34:05 +00002588 /* If we haven't been supplied any timestamp at all, we don't have a
2589 * timebase to work against, so any delay just wastes time. Push a
2590 * repaint as soon as possible so we can get on with it. */
Daniel Stone6847b852017-03-01 11:34:08 +00002591 if (!stamp) {
2592 output->next_repaint = now;
Daniel Stone3615ce12017-03-01 11:34:05 +00002593 goto out;
Daniel Stone6847b852017-03-01 11:34:08 +00002594 }
Daniel Stone3615ce12017-03-01 11:34:05 +00002595
Marius Vladdf9278a2018-03-06 18:56:23 +02002596 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2597 TLP_VBLANK(stamp), TLP_END);
2598
Pekka Paalanend7894d02015-07-03 15:08:53 +03002599 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002600 weston_presentation_feedback_present_list(&output->feedback_list,
2601 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002602 output->msc,
2603 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002604
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002605 output->frame_time = *stamp;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002606
Daniel Stone6847b852017-03-01 11:34:08 +00002607 timespec_add_nsec(&output->next_repaint, stamp, refresh_nsec);
2608 timespec_add_msec(&output->next_repaint, &output->next_repaint,
2609 -compositor->repaint_msec);
2610 msec_rel = timespec_sub_to_msec(&output->next_repaint, &now);
Daniel Stone84aff5c2017-03-01 11:34:04 +00002611
2612 if (msec_rel < -1000 || msec_rel > 1000) {
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002613 static bool warned;
2614
2615 if (!warned)
2616 weston_log("Warning: computed repaint delay is "
Daniel Stone6847b852017-03-01 11:34:08 +00002617 "insane: %lld msec\n", (long long) msec_rel);
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002618 warned = true;
2619
Daniel Stone6847b852017-03-01 11:34:08 +00002620 output->next_repaint = now;
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002621 }
2622
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002623 /* Called from restart_repaint_loop and restart happens already after
2624 * the deadline given by repaint_msec? In that case we delay until
2625 * the deadline of the next frame, to give clients a more predictable
2626 * timing of the repaint cycle to lock on. */
Daniel Stoneeca5cca2017-02-28 21:53:51 +00002627 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID &&
2628 msec_rel < 0) {
2629 while (timespec_sub_to_nsec(&output->next_repaint, &now) < 0) {
2630 timespec_add_nsec(&output->next_repaint,
2631 &output->next_repaint,
2632 refresh_nsec);
2633 }
2634 }
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002635
Daniel Stone3615ce12017-03-01 11:34:05 +00002636out:
Daniel Stone05df8c12017-03-03 16:59:42 +00002637 output->repaint_status = REPAINT_SCHEDULED;
Daniel Stone6847b852017-03-01 11:34:08 +00002638 output_repaint_timer_arm(compositor);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002639}
2640
2641static void
2642idle_repaint(void *data)
2643{
2644 struct weston_output *output = data;
2645
Daniel Stone05df8c12017-03-03 16:59:42 +00002646 assert(output->repaint_status == REPAINT_BEGIN_FROM_IDLE);
2647 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03002648 output->idle_repaint_source = NULL;
Jonas Ådahle5a12252013-04-05 23:07:11 +02002649 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002650}
2651
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002652WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002653weston_layer_entry_insert(struct weston_layer_entry *list,
2654 struct weston_layer_entry *entry)
2655{
2656 wl_list_insert(&list->link, &entry->link);
2657 entry->layer = list->layer;
2658}
2659
2660WL_EXPORT void
2661weston_layer_entry_remove(struct weston_layer_entry *entry)
2662{
2663 wl_list_remove(&entry->link);
2664 wl_list_init(&entry->link);
2665 entry->layer = NULL;
2666}
2667
Quentin Glidic82681572016-12-17 13:40:51 +01002668
2669/** Initialize the weston_layer struct.
2670 *
2671 * \param compositor The compositor instance
2672 * \param layer The layer to initialize
2673 */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002674WL_EXPORT void
Quentin Glidic82681572016-12-17 13:40:51 +01002675weston_layer_init(struct weston_layer *layer,
2676 struct weston_compositor *compositor)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002677{
Quentin Glidic82681572016-12-17 13:40:51 +01002678 layer->compositor = compositor;
2679 wl_list_init(&layer->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002680 wl_list_init(&layer->view_list.link);
2681 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002682 weston_layer_set_mask_infinite(layer);
Quentin Glidic82681572016-12-17 13:40:51 +01002683}
2684
2685/** Sets the position of the layer in the layer list. The layer will be placed
2686 * below any layer with the same position value, if any.
2687 * This function is safe to call if the layer is already on the list, but the
2688 * layer may be moved below other layers at the same position, if any.
2689 *
2690 * \param layer The layer to modify
2691 * \param position The position the layer will be placed at
2692 */
2693WL_EXPORT void
2694weston_layer_set_position(struct weston_layer *layer,
2695 enum weston_layer_position position)
2696{
2697 struct weston_layer *below;
2698
2699 wl_list_remove(&layer->link);
2700
2701 /* layer_list is ordered from top to bottom, the last layer being the
2702 * background with the smallest position value */
2703
2704 layer->position = position;
2705 wl_list_for_each_reverse(below, &layer->compositor->layer_list, link) {
2706 if (below->position >= layer->position) {
2707 wl_list_insert(&below->link, &layer->link);
2708 return;
2709 }
2710 }
2711 wl_list_insert(&layer->compositor->layer_list, &layer->link);
2712}
2713
2714/** Hide a layer by taking it off the layer list.
2715 * This function is safe to call if the layer is not on the list.
2716 *
2717 * \param layer The layer to hide
2718 */
2719WL_EXPORT void
2720weston_layer_unset_position(struct weston_layer *layer)
2721{
2722 wl_list_remove(&layer->link);
2723 wl_list_init(&layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002724}
2725
2726WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002727weston_layer_set_mask(struct weston_layer *layer,
2728 int x, int y, int width, int height)
2729{
2730 struct weston_view *view;
2731
2732 layer->mask.x1 = x;
2733 layer->mask.x2 = x + width;
2734 layer->mask.y1 = y;
2735 layer->mask.y2 = y + height;
2736
2737 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2738 weston_view_geometry_dirty(view);
2739 }
2740}
2741
2742WL_EXPORT void
2743weston_layer_set_mask_infinite(struct weston_layer *layer)
2744{
2745 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2746 UINT32_MAX, UINT32_MAX);
2747}
2748
2749WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002750weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002751{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002752 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002753 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002754
Bryce Harrington08976ac2016-08-30 12:05:16 -07002755 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2756 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002757 return;
2758
Pekka Paalanenb5026542014-11-12 15:09:24 +02002759 if (!output->repaint_needed)
2760 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2761
Kristian Høgsbergef044142011-06-21 15:02:12 -04002762 loop = wl_display_get_event_loop(compositor->wl_display);
Daniel Stone09a97e22017-03-01 11:34:06 +00002763 output->repaint_needed = true;
Daniel Stone05df8c12017-03-03 16:59:42 +00002764
2765 /* If we already have a repaint scheduled for our idle handler,
2766 * no need to set it again. If the repaint has been called but
2767 * not finished, then weston_output_finish_frame() will notice
2768 * that a repaint is needed and schedule one. */
2769 if (output->repaint_status != REPAINT_NOT_SCHEDULED)
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002770 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002771
Daniel Stone05df8c12017-03-03 16:59:42 +00002772 output->repaint_status = REPAINT_BEGIN_FROM_IDLE;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03002773 assert(!output->idle_repaint_source);
2774 output->idle_repaint_source = wl_event_loop_add_idle(loop, idle_repaint,
2775 output);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002776 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002777}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002778
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002779WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002780weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2781{
2782 struct weston_output *output;
2783
2784 wl_list_for_each(output, &compositor->output_list, link)
2785 weston_output_schedule_repaint(output);
2786}
2787
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002788static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002789surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002790{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002791 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002792}
2793
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002794static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002795surface_attach(struct wl_client *client,
2796 struct wl_resource *resource,
2797 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2798{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002799 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002800 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002801
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002802 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002803 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002804 if (buffer == NULL) {
2805 wl_client_post_no_memory(client);
2806 return;
2807 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002808 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002809
Pekka Paalanende685b82012-12-04 15:58:12 +02002810 /* Attach, attach, without commit in between does not send
2811 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002812 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002813
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002814 surface->pending.sx = sx;
2815 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002816 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002817}
2818
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002819static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002820surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002821 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002822 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002823{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002824 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002825
Derek Foreman57e92ed2015-11-17 14:11:35 -06002826 if (width <= 0 || height <= 0)
2827 return;
2828
Derek Foreman152254b2015-11-26 14:17:48 -06002829 pixman_region32_union_rect(&surface->pending.damage_surface,
2830 &surface->pending.damage_surface,
2831 x, y, width, height);
2832}
2833
2834static void
2835surface_damage_buffer(struct wl_client *client,
2836 struct wl_resource *resource,
2837 int32_t x, int32_t y, int32_t width, int32_t height)
2838{
2839 struct weston_surface *surface = wl_resource_get_user_data(resource);
2840
2841 if (width <= 0 || height <= 0)
2842 return;
2843
2844 pixman_region32_union_rect(&surface->pending.damage_buffer,
2845 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002846 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002847}
2848
Kristian Høgsberg33418202011-08-16 23:01:28 -04002849static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002850destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002851{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002852 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002853
2854 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002855 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002856}
2857
2858static void
2859surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002860 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002861{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002862 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002863 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002864
2865 cb = malloc(sizeof *cb);
2866 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002867 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002868 return;
2869 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002870
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002871 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2872 callback);
2873 if (cb->resource == NULL) {
2874 free(cb);
2875 wl_resource_post_no_memory(resource);
2876 return;
2877 }
2878
Jason Ekstranda85118c2013-06-27 20:17:02 -05002879 wl_resource_set_implementation(cb->resource, NULL, cb,
2880 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002881
Pekka Paalanenbc106382012-10-10 12:49:31 +03002882 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002883}
2884
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002885static void
2886surface_set_opaque_region(struct wl_client *client,
2887 struct wl_resource *resource,
2888 struct wl_resource *region_resource)
2889{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002890 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002891 struct weston_region *region;
2892
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002893 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002894 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002895 pixman_region32_copy(&surface->pending.opaque,
2896 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002897 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002898 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002899 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002900}
2901
2902static void
2903surface_set_input_region(struct wl_client *client,
2904 struct wl_resource *resource,
2905 struct wl_resource *region_resource)
2906{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002907 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002908 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002909
2910 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002911 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002912 pixman_region32_copy(&surface->pending.input,
2913 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002914 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002915 pixman_region32_fini(&surface->pending.input);
2916 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002917 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002918}
2919
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002920/* Cause damage to this sub-surface and all its children.
2921 *
2922 * This is useful when there are state changes that need an implicit
2923 * damage, e.g. a z-order change.
2924 */
2925static void
2926weston_surface_damage_subsurfaces(struct weston_subsurface *sub)
2927{
2928 struct weston_subsurface *child;
2929
2930 weston_surface_damage(sub->surface);
2931 sub->reordered = false;
2932
2933 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link)
2934 if (child != sub)
2935 weston_surface_damage_subsurfaces(child);
2936}
2937
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002938static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002939weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002940{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002941 struct weston_subsurface *sub;
2942
2943 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2944 parent_link_pending) {
2945 wl_list_remove(&sub->parent_link);
2946 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002947
2948 if (sub->reordered)
2949 weston_surface_damage_subsurfaces(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002950 }
2951}
2952
2953static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03002954weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05002955 struct weston_matrix *matrix)
2956{
Pekka Paalanen04baea52016-04-26 15:50:58 +03002957 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05002958 double src_width, src_height, dest_width, dest_height;
2959
2960 weston_matrix_init(matrix);
2961
2962 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
2963 src_width = surface->width_from_buffer;
2964 src_height = surface->height_from_buffer;
2965 } else {
2966 src_width = wl_fixed_to_double(vp->buffer.src_width);
2967 src_height = wl_fixed_to_double(vp->buffer.src_height);
2968 }
2969
2970 if (vp->surface.width == -1) {
2971 dest_width = src_width;
2972 dest_height = src_height;
2973 } else {
2974 dest_width = vp->surface.width;
2975 dest_height = vp->surface.height;
2976 }
2977
2978 if (src_width != dest_width || src_height != dest_height)
2979 weston_matrix_scale(matrix,
2980 src_width / dest_width,
2981 src_height / dest_height, 1);
2982
2983 if (vp->buffer.src_width != wl_fixed_from_int(-1))
2984 weston_matrix_translate(matrix,
2985 wl_fixed_to_double(vp->buffer.src_x),
2986 wl_fixed_to_double(vp->buffer.src_y),
2987 0);
2988
2989 switch (vp->buffer.transform) {
2990 case WL_OUTPUT_TRANSFORM_FLIPPED:
2991 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2992 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2993 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2994 weston_matrix_scale(matrix, -1, 1, 1);
2995 weston_matrix_translate(matrix,
2996 surface->width_from_buffer, 0, 0);
2997 break;
2998 }
2999
3000 switch (vp->buffer.transform) {
3001 default:
3002 case WL_OUTPUT_TRANSFORM_NORMAL:
3003 case WL_OUTPUT_TRANSFORM_FLIPPED:
3004 break;
3005 case WL_OUTPUT_TRANSFORM_90:
3006 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3007 weston_matrix_rotate_xy(matrix, 0, 1);
3008 weston_matrix_translate(matrix,
3009 surface->height_from_buffer, 0, 0);
3010 break;
3011 case WL_OUTPUT_TRANSFORM_180:
3012 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3013 weston_matrix_rotate_xy(matrix, -1, 0);
3014 weston_matrix_translate(matrix,
3015 surface->width_from_buffer,
3016 surface->height_from_buffer, 0);
3017 break;
3018 case WL_OUTPUT_TRANSFORM_270:
3019 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3020 weston_matrix_rotate_xy(matrix, 0, -1);
3021 weston_matrix_translate(matrix,
3022 0, surface->width_from_buffer, 0);
3023 break;
3024 }
3025
3026 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
3027}
3028
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003029/**
3030 * Compute a + b > c while being safe to overflows.
3031 */
3032static bool
3033fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
3034{
3035 return (int64_t)a + (int64_t)b > (int64_t)c;
3036}
3037
3038static bool
3039weston_surface_is_pending_viewport_source_valid(
3040 const struct weston_surface *surface)
3041{
3042 const struct weston_surface_state *pend = &surface->pending;
3043 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
3044 int width_from_buffer = 0;
3045 int height_from_buffer = 0;
3046 wl_fixed_t w;
3047 wl_fixed_t h;
3048
3049 /* If viewport source rect is not set, it is always ok. */
3050 if (vp->buffer.src_width == wl_fixed_from_int(-1))
3051 return true;
3052
3053 if (pend->newly_attached) {
3054 if (pend->buffer) {
3055 convert_size_by_transform_scale(&width_from_buffer,
3056 &height_from_buffer,
3057 pend->buffer->width,
3058 pend->buffer->height,
3059 vp->buffer.transform,
3060 vp->buffer.scale);
3061 } else {
3062 /* No buffer: viewport is irrelevant. */
3063 return true;
3064 }
3065 } else {
3066 width_from_buffer = surface->width_from_buffer;
3067 height_from_buffer = surface->height_from_buffer;
3068 }
3069
3070 assert((width_from_buffer == 0) == (height_from_buffer == 0));
3071 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
3072
3073 /* No buffer: viewport is irrelevant. */
3074 if (width_from_buffer == 0 || height_from_buffer == 0)
3075 return true;
3076
3077 /* overflow checks for wl_fixed_from_int() */
3078 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
3079 return false;
3080 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
3081 return false;
3082
3083 w = wl_fixed_from_int(width_from_buffer);
3084 h = wl_fixed_from_int(height_from_buffer);
3085
3086 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
3087 return false;
3088 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
3089 return false;
3090
3091 return true;
3092}
3093
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003094static bool
3095fixed_is_integer(wl_fixed_t v)
3096{
3097 return (v & 0xff) == 0;
3098}
3099
3100static bool
3101weston_surface_is_pending_viewport_dst_size_int(
3102 const struct weston_surface *surface)
3103{
3104 const struct weston_buffer_viewport *vp =
3105 &surface->pending.buffer_viewport;
3106
3107 if (vp->surface.width != -1) {
3108 assert(vp->surface.width > 0 && vp->surface.height > 0);
3109 return true;
3110 }
3111
3112 return fixed_is_integer(vp->buffer.src_width) &&
3113 fixed_is_integer(vp->buffer.src_height);
3114}
3115
Derek Foreman152254b2015-11-26 14:17:48 -06003116/* Translate pending damage in buffer co-ordinates to surface
3117 * co-ordinates and union it with a pixman_region32_t.
3118 * This should only be called after the buffer is attached.
3119 */
3120static void
3121apply_damage_buffer(pixman_region32_t *dest,
3122 struct weston_surface *surface,
3123 struct weston_surface_state *state)
3124{
3125 struct weston_buffer *buffer = surface->buffer_ref.buffer;
3126
3127 /* wl_surface.damage_buffer needs to be clipped to the buffer,
3128 * translated into surface co-ordinates and unioned with
3129 * any other surface damage.
3130 * None of this makes sense if there is no buffer though.
3131 */
3132 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
3133 pixman_region32_t buffer_damage;
3134
3135 pixman_region32_intersect_rect(&state->damage_buffer,
3136 &state->damage_buffer,
3137 0, 0, buffer->width,
3138 buffer->height);
3139 pixman_region32_init(&buffer_damage);
3140 weston_matrix_transform_region(&buffer_damage,
3141 &surface->buffer_to_surface_matrix,
3142 &state->damage_buffer);
3143 pixman_region32_union(dest, dest, &buffer_damage);
3144 pixman_region32_fini(&buffer_damage);
3145 }
3146 /* We should clear this on commit even if there was no buffer */
3147 pixman_region32_clear(&state->damage_buffer);
3148}
3149
Jason Ekstrand1e059042014-10-16 10:55:19 -05003150static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05003151weston_surface_commit_state(struct weston_surface *surface,
3152 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003153{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003154 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003155 pixman_region32_t opaque;
3156
Alexander Larsson4ea95522013-05-22 14:41:37 +02003157 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02003158 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03003159 /* wp_viewport.set_source */
3160 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003161 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003162
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003163 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003164 if (state->newly_attached)
3165 weston_surface_attach(surface, state->buffer);
3166 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01003167
Jason Ekstrand1e059042014-10-16 10:55:19 -05003168 weston_surface_build_buffer_matrix(surface,
3169 &surface->surface_to_buffer_matrix);
3170 weston_matrix_invert(&surface->buffer_to_surface_matrix,
3171 &surface->surface_to_buffer_matrix);
3172
Jason Ekstrand7b982072014-05-20 14:33:03 -05003173 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003174 weston_surface_update_size(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003175 if (surface->committed)
3176 surface->committed(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003177 }
Giulio Camuffo184df502013-02-21 11:29:21 +01003178
Jason Ekstrand7b982072014-05-20 14:33:03 -05003179 state->sx = 0;
3180 state->sy = 0;
3181 state->newly_attached = 0;
3182 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03003183
Derek Foreman152254b2015-11-26 14:17:48 -06003184 /* wl_surface.damage and wl_surface.damage_buffer */
Pekka Paalanenb5026542014-11-12 15:09:24 +02003185 if (weston_timeline_enabled_ &&
Derek Foreman152254b2015-11-26 14:17:48 -06003186 (pixman_region32_not_empty(&state->damage_surface) ||
3187 pixman_region32_not_empty(&state->damage_buffer)))
Pekka Paalanenb5026542014-11-12 15:09:24 +02003188 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06003189
Pekka Paalanen8e159182012-10-10 12:49:25 +03003190 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06003191 &state->damage_surface);
3192
3193 apply_damage_buffer(&surface->damage, surface, state);
3194
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05003195 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07003196 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06003197 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003198
3199 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003200 pixman_region32_init(&opaque);
3201 pixman_region32_intersect_rect(&opaque, &state->opaque,
3202 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003203
3204 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
3205 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003206 wl_list_for_each(view, &surface->views, surface_link)
3207 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003208 }
3209
3210 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003211
3212 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003213 pixman_region32_intersect_rect(&surface->input, &state->input,
3214 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003215
Pekka Paalanenbc106382012-10-10 12:49:31 +03003216 /* wl_surface.frame */
3217 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05003218 &state->frame_callback_list);
3219 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003220
3221 /* XXX:
3222 * What should happen with a feedback request, if there
3223 * is no wl_buffer attached for this commit?
3224 */
3225
3226 /* presentation.feedback */
3227 wl_list_insert_list(&surface->feedback_list,
3228 &state->feedback_list);
3229 wl_list_init(&state->feedback_list);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003230
3231 wl_signal_emit(&surface->commit_signal, surface);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003232}
3233
3234static void
3235weston_surface_commit(struct weston_surface *surface)
3236{
3237 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003238
Pekka Paalanene67858b2013-04-25 13:57:42 +03003239 weston_surface_commit_subsurface_order(surface);
3240
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003241 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003242}
3243
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003244static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003245weston_subsurface_commit(struct weston_subsurface *sub);
3246
3247static void
3248weston_subsurface_parent_commit(struct weston_subsurface *sub,
3249 int parent_is_synchronized);
3250
3251static void
3252surface_commit(struct wl_client *client, struct wl_resource *resource)
3253{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003254 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003255 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
3256
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003257 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
3258 assert(surface->viewport_resource);
3259
3260 wl_resource_post_error(surface->viewport_resource,
3261 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
3262 "wl_surface@%d has viewport source outside buffer",
3263 wl_resource_get_id(resource));
3264 return;
3265 }
3266
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003267 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
3268 assert(surface->viewport_resource);
3269
3270 wl_resource_post_error(surface->viewport_resource,
3271 WP_VIEWPORT_ERROR_BAD_SIZE,
3272 "wl_surface@%d viewport dst size not integer",
3273 wl_resource_get_id(resource));
3274 return;
3275 }
3276
Pekka Paalanene67858b2013-04-25 13:57:42 +03003277 if (sub) {
3278 weston_subsurface_commit(sub);
3279 return;
3280 }
3281
3282 weston_surface_commit(surface);
3283
3284 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
3285 if (sub->surface != surface)
3286 weston_subsurface_parent_commit(sub, 0);
3287 }
3288}
3289
3290static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003291surface_set_buffer_transform(struct wl_client *client,
3292 struct wl_resource *resource, int transform)
3293{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003294 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003295
Jonny Lamba55f1392014-05-30 12:07:15 +02003296 /* if wl_output.transform grows more members this will need to be updated. */
3297 if (transform < 0 ||
3298 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
3299 wl_resource_post_error(resource,
3300 WL_SURFACE_ERROR_INVALID_TRANSFORM,
3301 "buffer transform must be a valid transform "
3302 "('%d' specified)", transform);
3303 return;
3304 }
3305
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003306 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003307 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003308}
3309
Alexander Larsson4ea95522013-05-22 14:41:37 +02003310static void
3311surface_set_buffer_scale(struct wl_client *client,
3312 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003313 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003314{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003315 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003316
Jonny Lamba55f1392014-05-30 12:07:15 +02003317 if (scale < 1) {
3318 wl_resource_post_error(resource,
3319 WL_SURFACE_ERROR_INVALID_SCALE,
3320 "buffer scale must be at least one "
3321 "('%d' specified)", scale);
3322 return;
3323 }
3324
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003325 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003326 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003327}
3328
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003329static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003330 surface_destroy,
3331 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04003332 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003333 surface_frame,
3334 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003335 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003336 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02003337 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06003338 surface_set_buffer_scale,
3339 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003340};
3341
3342static void
3343compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003344 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003345{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04003346 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003347 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003348
Kristian Høgsberg18c93002012-01-27 11:58:31 -05003349 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003350 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003351 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003352 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003353 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003354
Jason Ekstranda85118c2013-06-27 20:17:02 -05003355 surface->resource =
3356 wl_resource_create(client, &wl_surface_interface,
3357 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003358 if (surface->resource == NULL) {
3359 weston_surface_destroy(surface);
3360 wl_resource_post_no_memory(resource);
3361 return;
3362 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003363 wl_resource_set_implementation(surface->resource, &surface_interface,
3364 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003365
3366 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003367}
3368
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003369static void
3370destroy_region(struct wl_resource *resource)
3371{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003372 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003373
3374 pixman_region32_fini(&region->region);
3375 free(region);
3376}
3377
3378static void
3379region_destroy(struct wl_client *client, struct wl_resource *resource)
3380{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003381 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003382}
3383
3384static void
3385region_add(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
3390 pixman_region32_union_rect(&region->region, &region->region,
3391 x, y, width, height);
3392}
3393
3394static void
3395region_subtract(struct wl_client *client, struct wl_resource *resource,
3396 int32_t x, int32_t y, int32_t width, int32_t height)
3397{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003398 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003399 pixman_region32_t rect;
3400
3401 pixman_region32_init_rect(&rect, x, y, width, height);
3402 pixman_region32_subtract(&region->region, &region->region, &rect);
3403 pixman_region32_fini(&rect);
3404}
3405
3406static const struct wl_region_interface region_interface = {
3407 region_destroy,
3408 region_add,
3409 region_subtract
3410};
3411
3412static void
3413compositor_create_region(struct wl_client *client,
3414 struct wl_resource *resource, uint32_t id)
3415{
3416 struct weston_region *region;
3417
3418 region = malloc(sizeof *region);
3419 if (region == NULL) {
3420 wl_resource_post_no_memory(resource);
3421 return;
3422 }
3423
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003424 pixman_region32_init(&region->region);
3425
Jason Ekstranda85118c2013-06-27 20:17:02 -05003426 region->resource =
3427 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003428 if (region->resource == NULL) {
3429 free(region);
3430 wl_resource_post_no_memory(resource);
3431 return;
3432 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003433 wl_resource_set_implementation(region->resource, &region_interface,
3434 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003435}
3436
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003437static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003438 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003439 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003440};
3441
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003442static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003443weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3444{
3445 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003446
Jason Ekstrand7b982072014-05-20 14:33:03 -05003447 weston_surface_commit_state(surface, &sub->cached);
3448 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003449
3450 weston_surface_commit_subsurface_order(surface);
3451
3452 weston_surface_schedule_repaint(surface);
3453
Jason Ekstrand7b982072014-05-20 14:33:03 -05003454 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003455}
3456
3457static void
3458weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3459{
3460 struct weston_surface *surface = sub->surface;
3461
3462 /*
3463 * If this commit would cause the surface to move by the
3464 * attach(dx, dy) parameters, the old damage region must be
3465 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05003466 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003467 */
Derek Foreman152254b2015-11-26 14:17:48 -06003468 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003469 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06003470 pixman_region32_union(&sub->cached.damage_surface,
3471 &sub->cached.damage_surface,
3472 &surface->pending.damage_surface);
3473 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003474
3475 if (surface->pending.newly_attached) {
3476 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003477 weston_surface_state_set_buffer(&sub->cached,
3478 surface->pending.buffer);
3479 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003480 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003481 weston_presentation_feedback_discard_list(
3482 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003483 }
3484 sub->cached.sx += surface->pending.sx;
3485 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003486
Derek Foreman152254b2015-11-26 14:17:48 -06003487 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
3488
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003489 sub->cached.buffer_viewport.changed |=
3490 surface->pending.buffer_viewport.changed;
3491 sub->cached.buffer_viewport.buffer =
3492 surface->pending.buffer_viewport.buffer;
3493 sub->cached.buffer_viewport.surface =
3494 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003495
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003496 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003497
3498 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3499
3500 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3501
3502 wl_list_insert_list(&sub->cached.frame_callback_list,
3503 &surface->pending.frame_callback_list);
3504 wl_list_init(&surface->pending.frame_callback_list);
3505
Pekka Paalanen133e4392014-09-23 22:08:46 -04003506 wl_list_insert_list(&sub->cached.feedback_list,
3507 &surface->pending.feedback_list);
3508 wl_list_init(&surface->pending.feedback_list);
3509
Jason Ekstrand7b982072014-05-20 14:33:03 -05003510 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003511}
3512
Derek Foreman280e7dd2014-10-03 13:13:42 -05003513static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003514weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3515{
3516 while (sub) {
3517 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003518 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003519
3520 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003521 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003522
3523 sub = weston_surface_to_subsurface(sub->parent);
3524 }
3525
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003526 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003527}
3528
3529static void
3530weston_subsurface_commit(struct weston_subsurface *sub)
3531{
3532 struct weston_surface *surface = sub->surface;
3533 struct weston_subsurface *tmp;
3534
3535 /* Recursive check for effectively synchronized. */
3536 if (weston_subsurface_is_synchronized(sub)) {
3537 weston_subsurface_commit_to_cache(sub);
3538 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003539 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003540 /* flush accumulated state from cache */
3541 weston_subsurface_commit_to_cache(sub);
3542 weston_subsurface_commit_from_cache(sub);
3543 } else {
3544 weston_surface_commit(surface);
3545 }
3546
3547 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3548 if (tmp->surface != surface)
3549 weston_subsurface_parent_commit(tmp, 0);
3550 }
3551 }
3552}
3553
3554static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003555weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003556{
3557 struct weston_surface *surface = sub->surface;
3558 struct weston_subsurface *tmp;
3559
Pekka Paalanene67858b2013-04-25 13:57:42 +03003560 /* From now on, commit_from_cache the whole sub-tree, regardless of
3561 * the synchronized mode of each child. This sub-surface or some
3562 * of its ancestors were synchronized, so we are synchronized
3563 * all the way down.
3564 */
3565
Jason Ekstrand7b982072014-05-20 14:33:03 -05003566 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003567 weston_subsurface_commit_from_cache(sub);
3568
3569 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3570 if (tmp->surface != surface)
3571 weston_subsurface_parent_commit(tmp, 1);
3572 }
3573}
3574
3575static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003576weston_subsurface_parent_commit(struct weston_subsurface *sub,
3577 int parent_is_synchronized)
3578{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003579 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003580 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003581 wl_list_for_each(view, &sub->surface->views, surface_link)
3582 weston_view_set_position(view,
3583 sub->position.x,
3584 sub->position.y);
3585
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003586 sub->position.set = 0;
3587 }
3588
3589 if (parent_is_synchronized || sub->synchronized)
3590 weston_subsurface_synchronized_commit(sub);
3591}
3592
Pekka Paalanen8274d902014-08-06 19:36:51 +03003593static int
3594subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3595{
3596 return snprintf(buf, len, "sub-surface");
3597}
3598
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003599static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003600subsurface_committed(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003601{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003602 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003603
Jason Ekstranda7af7042013-10-12 22:38:11 -05003604 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003605 weston_view_set_position(view,
3606 view->geometry.x + dx,
3607 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003608
3609 /* No need to check parent mappedness, because if parent is not
3610 * mapped, parent is not in a visible layer, so this sub-surface
3611 * will not be drawn either.
3612 */
Armin Krezovićf8486c32016-06-30 06:04:28 +02003613
Pekka Paalanene67858b2013-04-25 13:57:42 +03003614 if (!weston_surface_is_mapped(surface)) {
Armin Krezovićf8486c32016-06-30 06:04:28 +02003615 surface->is_mapped = true;
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003616
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003617 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003618 * because that would call it also for the parent surface,
3619 * which might not be mapped yet. That would lead to
3620 * inconsistent state, where the window could never be
3621 * mapped.
3622 *
Armin Krezovićf8486c32016-06-30 06:04:28 +02003623 * Instead just force the is_mapped flag on, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003624 * weston_surface_is_mapped() return true, so that when the
3625 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003626 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003627 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03003628 }
3629}
3630
3631static struct weston_subsurface *
3632weston_surface_to_subsurface(struct weston_surface *surface)
3633{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003634 if (surface->committed == subsurface_committed)
3635 return surface->committed_private;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003636
3637 return NULL;
3638}
3639
Pekka Paalanen01388e22013-04-25 13:57:44 +03003640WL_EXPORT struct weston_surface *
3641weston_surface_get_main_surface(struct weston_surface *surface)
3642{
3643 struct weston_subsurface *sub;
3644
3645 while (surface && (sub = weston_surface_to_subsurface(surface)))
3646 surface = sub->parent;
3647
3648 return surface;
3649}
3650
Pekka Paalanen50b67472014-10-01 15:02:41 +03003651WL_EXPORT int
3652weston_surface_set_role(struct weston_surface *surface,
3653 const char *role_name,
3654 struct wl_resource *error_resource,
3655 uint32_t error_code)
3656{
3657 assert(role_name);
3658
3659 if (surface->role_name == NULL ||
3660 surface->role_name == role_name ||
3661 strcmp(surface->role_name, role_name) == 0) {
3662 surface->role_name = role_name;
3663
3664 return 0;
3665 }
3666
3667 wl_resource_post_error(error_resource, error_code,
3668 "Cannot assign role %s to wl_surface@%d,"
3669 " already has role %s\n",
3670 role_name,
3671 wl_resource_get_id(surface->resource),
3672 surface->role_name);
3673 return -1;
3674}
3675
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02003676WL_EXPORT const char *
3677weston_surface_get_role(struct weston_surface *surface)
3678{
3679 return surface->role_name;
3680}
3681
Pekka Paalanen8274d902014-08-06 19:36:51 +03003682WL_EXPORT void
3683weston_surface_set_label_func(struct weston_surface *surface,
3684 int (*desc)(struct weston_surface *,
3685 char *, size_t))
3686{
3687 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02003688 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003689}
3690
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003691/** Get the size of surface contents
3692 *
3693 * \param surface The surface to query.
3694 * \param width Returns the width of raw contents.
3695 * \param height Returns the height of raw contents.
3696 *
3697 * Retrieves the raw surface content size in pixels for the given surface.
3698 * This is the whole content size in buffer pixels. If the surface
3699 * has no content or the renderer does not implement this feature,
3700 * zeroes are returned.
3701 *
3702 * This function is used to determine the buffer size needed for
3703 * a weston_surface_copy_content() call.
3704 */
3705WL_EXPORT void
3706weston_surface_get_content_size(struct weston_surface *surface,
3707 int *width, int *height)
3708{
3709 struct weston_renderer *rer = surface->compositor->renderer;
3710
3711 if (!rer->surface_get_content_size) {
3712 *width = 0;
3713 *height = 0;
3714 return;
3715 }
3716
3717 rer->surface_get_content_size(surface, width, height);
3718}
3719
Quentin Glidic248dd102016-08-12 10:41:34 +02003720/** Get the bounding box of a surface and its subsurfaces
3721 *
3722 * \param surface The surface to query.
3723 * \return The bounding box relative to the surface origin.
3724 *
3725 */
3726WL_EXPORT struct weston_geometry
3727weston_surface_get_bounding_box(struct weston_surface *surface)
3728{
3729 pixman_region32_t region;
3730 pixman_box32_t *box;
3731 struct weston_subsurface *subsurface;
3732
3733 pixman_region32_init_rect(&region,
3734 0, 0,
3735 surface->width, surface->height);
3736
3737 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link)
3738 pixman_region32_union_rect(&region, &region,
3739 subsurface->position.x,
3740 subsurface->position.y,
3741 subsurface->surface->width,
3742 subsurface->surface->height);
3743
3744 box = pixman_region32_extents(&region);
3745 struct weston_geometry geometry = {
3746 .x = box->x1,
3747 .y = box->y1,
3748 .width = box->x2 - box->x1,
3749 .height = box->y2 - box->y1,
3750 };
3751
3752 pixman_region32_fini(&region);
3753
3754 return geometry;
3755}
3756
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003757/** Copy surface contents to system memory.
3758 *
3759 * \param surface The surface to copy from.
3760 * \param target Pointer to the target memory buffer.
3761 * \param size Size of the target buffer in bytes.
3762 * \param src_x X location on contents to copy from.
3763 * \param src_y Y location on contents to copy from.
3764 * \param width Width in pixels of the area to copy.
3765 * \param height Height in pixels of the area to copy.
3766 * \return 0 for success, -1 for failure.
3767 *
3768 * Surface contents are maintained by the renderer. They can be in a
3769 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
3770 * else.
3771 *
3772 * Surface contents are copied into memory pointed to by target,
3773 * which has size bytes of space available. The target memory
3774 * may be larger than needed, but being smaller returns an error.
3775 * The extra bytes in target may or may not be written; their content is
3776 * unspecified. Size must be large enough to hold the image.
3777 *
3778 * The image in the target memory will be arranged in rows from
3779 * top to bottom, and pixels on a row from left to right. The pixel
3780 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
3781 * width * 4.
3782 *
3783 * Parameters src_x and src_y define the upper-left corner in buffer
3784 * coordinates (pixels) to copy from. Parameters width and height
3785 * define the size of the area to copy in pixels.
3786 *
3787 * The rectangle defined by src_x, src_y, width, height must fit in
3788 * the surface contents. Otherwise an error is returned.
3789 *
3790 * Use surface_get_data_size to determine the content size; the
3791 * needed target buffer size and rectangle limits.
3792 *
3793 * CURRENT IMPLEMENTATION RESTRICTIONS:
3794 * - the machine must be little-endian due to Pixman formats.
3795 *
3796 * NOTE: Pixman formats are premultiplied.
3797 */
3798WL_EXPORT int
3799weston_surface_copy_content(struct weston_surface *surface,
3800 void *target, size_t size,
3801 int src_x, int src_y,
3802 int width, int height)
3803{
3804 struct weston_renderer *rer = surface->compositor->renderer;
3805 int cw, ch;
3806 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
3807
3808 if (!rer->surface_copy_content)
3809 return -1;
3810
3811 weston_surface_get_content_size(surface, &cw, &ch);
3812
3813 if (src_x < 0 || src_y < 0)
3814 return -1;
3815
3816 if (width <= 0 || height <= 0)
3817 return -1;
3818
3819 if (src_x + width > cw || src_y + height > ch)
3820 return -1;
3821
3822 if (width * bytespp * height > size)
3823 return -1;
3824
3825 return rer->surface_copy_content(surface, target, size,
3826 src_x, src_y, width, height);
3827}
3828
Pekka Paalanene67858b2013-04-25 13:57:42 +03003829static void
3830subsurface_set_position(struct wl_client *client,
3831 struct wl_resource *resource, int32_t x, int32_t y)
3832{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003833 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003834
3835 if (!sub)
3836 return;
3837
3838 sub->position.x = x;
3839 sub->position.y = y;
3840 sub->position.set = 1;
3841}
3842
3843static struct weston_subsurface *
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003844subsurface_find_sibling(struct weston_subsurface *sub,
3845 struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003846{
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003847 struct weston_surface *parent = sub->parent;
3848 struct weston_subsurface *sibling;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003849
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003850 wl_list_for_each(sibling, &parent->subsurface_list, parent_link) {
3851 if (sibling->surface == surface && sibling != sub)
3852 return sibling;
3853 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003854
3855 return NULL;
3856}
3857
3858static struct weston_subsurface *
3859subsurface_sibling_check(struct weston_subsurface *sub,
3860 struct weston_surface *surface,
3861 const char *request)
3862{
3863 struct weston_subsurface *sibling;
3864
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003865 sibling = subsurface_find_sibling(sub, surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003866 if (!sibling) {
3867 wl_resource_post_error(sub->resource,
3868 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3869 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003870 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003871 return NULL;
3872 }
3873
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003874 assert(sibling->parent == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003875
3876 return sibling;
3877}
3878
3879static void
3880subsurface_place_above(struct wl_client *client,
3881 struct wl_resource *resource,
3882 struct wl_resource *sibling_resource)
3883{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003884 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003885 struct weston_surface *surface =
3886 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003887 struct weston_subsurface *sibling;
3888
3889 if (!sub)
3890 return;
3891
3892 sibling = subsurface_sibling_check(sub, surface, "place_above");
3893 if (!sibling)
3894 return;
3895
3896 wl_list_remove(&sub->parent_link_pending);
3897 wl_list_insert(sibling->parent_link_pending.prev,
3898 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003899
3900 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003901}
3902
3903static void
3904subsurface_place_below(struct wl_client *client,
3905 struct wl_resource *resource,
3906 struct wl_resource *sibling_resource)
3907{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003908 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003909 struct weston_surface *surface =
3910 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003911 struct weston_subsurface *sibling;
3912
3913 if (!sub)
3914 return;
3915
3916 sibling = subsurface_sibling_check(sub, surface, "place_below");
3917 if (!sibling)
3918 return;
3919
3920 wl_list_remove(&sub->parent_link_pending);
3921 wl_list_insert(&sibling->parent_link_pending,
3922 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003923
3924 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003925}
3926
3927static void
3928subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
3929{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003930 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003931
3932 if (sub)
3933 sub->synchronized = 1;
3934}
3935
3936static void
3937subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
3938{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003939 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003940
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003941 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003942 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003943
3944 /* If sub became effectively desynchronized, flush. */
3945 if (!weston_subsurface_is_synchronized(sub))
3946 weston_subsurface_synchronized_commit(sub);
3947 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003948}
3949
3950static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003951weston_subsurface_unlink_parent(struct weston_subsurface *sub)
3952{
3953 wl_list_remove(&sub->parent_link);
3954 wl_list_remove(&sub->parent_link_pending);
3955 wl_list_remove(&sub->parent_destroy_listener.link);
3956 sub->parent = NULL;
3957}
3958
3959static void
3960weston_subsurface_destroy(struct weston_subsurface *sub);
3961
3962static void
3963subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
3964{
3965 struct weston_subsurface *sub =
3966 container_of(listener, struct weston_subsurface,
3967 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003968 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003969
3970 /* The protocol object (wl_resource) is left inert. */
3971 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003972 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003973
3974 weston_subsurface_destroy(sub);
3975}
3976
3977static void
3978subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3979{
3980 struct weston_subsurface *sub =
3981 container_of(listener, struct weston_subsurface,
3982 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003983 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003984 assert(sub->surface != sub->parent);
3985
3986 if (weston_surface_is_mapped(sub->surface))
3987 weston_surface_unmap(sub->surface);
3988
3989 weston_subsurface_unlink_parent(sub);
3990}
3991
3992static void
3993subsurface_resource_destroy(struct wl_resource *resource)
3994{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003995 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003996
3997 if (sub)
3998 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003999}
4000
4001static void
4002subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
4003{
4004 wl_resource_destroy(resource);
4005}
4006
4007static void
4008weston_subsurface_link_parent(struct weston_subsurface *sub,
4009 struct weston_surface *parent)
4010{
4011 sub->parent = parent;
4012 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004013 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004014 &sub->parent_destroy_listener);
4015
4016 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4017 wl_list_insert(&parent->subsurface_list_pending,
4018 &sub->parent_link_pending);
4019}
4020
4021static void
4022weston_subsurface_link_surface(struct weston_subsurface *sub,
4023 struct weston_surface *surface)
4024{
4025 sub->surface = surface;
4026 sub->surface_destroy_listener.notify =
4027 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004028 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004029 &sub->surface_destroy_listener);
4030}
4031
4032static void
4033weston_subsurface_destroy(struct weston_subsurface *sub)
4034{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004035 struct weston_view *view, *next;
4036
Pekka Paalanene67858b2013-04-25 13:57:42 +03004037 assert(sub->surface);
4038
4039 if (sub->resource) {
4040 assert(weston_surface_to_subsurface(sub->surface) == sub);
4041 assert(sub->parent_destroy_listener.notify ==
4042 subsurface_handle_parent_destroy);
4043
George Kiagiadakised04d382014-06-13 18:10:26 +02004044 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
4045 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004046 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02004047 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05004048
Pekka Paalanene67858b2013-04-25 13:57:42 +03004049 if (sub->parent)
4050 weston_subsurface_unlink_parent(sub);
4051
Jason Ekstrand7b982072014-05-20 14:33:03 -05004052 weston_surface_state_fini(&sub->cached);
4053 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004054
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004055 sub->surface->committed = NULL;
4056 sub->surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004057 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004058 } else {
4059 /* the dummy weston_subsurface for the parent itself */
4060 assert(sub->parent_destroy_listener.notify == NULL);
4061 wl_list_remove(&sub->parent_link);
4062 wl_list_remove(&sub->parent_link_pending);
4063 }
4064
4065 wl_list_remove(&sub->surface_destroy_listener.link);
4066 free(sub);
4067}
4068
4069static const struct wl_subsurface_interface subsurface_implementation = {
4070 subsurface_destroy,
4071 subsurface_set_position,
4072 subsurface_place_above,
4073 subsurface_place_below,
4074 subsurface_set_sync,
4075 subsurface_set_desync
4076};
4077
4078static struct weston_subsurface *
4079weston_subsurface_create(uint32_t id, struct weston_surface *surface,
4080 struct weston_surface *parent)
4081{
4082 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004083 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004084
Bryce Harringtonde16d892014-11-20 22:21:57 -08004085 sub = zalloc(sizeof *sub);
4086 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004087 return NULL;
4088
Jason Ekstranda7af7042013-10-12 22:38:11 -05004089 wl_list_init(&sub->unused_views);
4090
Jason Ekstranda85118c2013-06-27 20:17:02 -05004091 sub->resource =
4092 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004093 if (!sub->resource) {
4094 free(sub);
4095 return NULL;
4096 }
4097
Jason Ekstranda85118c2013-06-27 20:17:02 -05004098 wl_resource_set_implementation(sub->resource,
4099 &subsurface_implementation,
4100 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004101 weston_subsurface_link_surface(sub, surface);
4102 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05004103 weston_surface_state_init(&sub->cached);
4104 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004105 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004106
4107 return sub;
4108}
4109
4110/* Create a dummy subsurface for having the parent itself in its
4111 * sub-surface lists. Makes stacking order manipulation easy.
4112 */
4113static struct weston_subsurface *
4114weston_subsurface_create_for_parent(struct weston_surface *parent)
4115{
4116 struct weston_subsurface *sub;
4117
Bryce Harringtonde16d892014-11-20 22:21:57 -08004118 sub = zalloc(sizeof *sub);
4119 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004120 return NULL;
4121
4122 weston_subsurface_link_surface(sub, parent);
4123 sub->parent = parent;
4124 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4125 wl_list_insert(&parent->subsurface_list_pending,
4126 &sub->parent_link_pending);
4127
4128 return sub;
4129}
4130
4131static void
4132subcompositor_get_subsurface(struct wl_client *client,
4133 struct wl_resource *resource,
4134 uint32_t id,
4135 struct wl_resource *surface_resource,
4136 struct wl_resource *parent_resource)
4137{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004138 struct weston_surface *surface =
4139 wl_resource_get_user_data(surface_resource);
4140 struct weston_surface *parent =
4141 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004142 struct weston_subsurface *sub;
4143 static const char where[] = "get_subsurface: wl_subsurface@";
4144
4145 if (surface == parent) {
4146 wl_resource_post_error(resource,
4147 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4148 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004149 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004150 return;
4151 }
4152
4153 if (weston_surface_to_subsurface(surface)) {
4154 wl_resource_post_error(resource,
4155 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4156 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004157 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004158 return;
4159 }
4160
Pekka Paalanen50b67472014-10-01 15:02:41 +03004161 if (weston_surface_set_role(surface, "wl_subsurface", resource,
4162 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004163 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004164
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004165 if (weston_surface_get_main_surface(parent) == surface) {
4166 wl_resource_post_error(resource,
4167 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4168 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004169 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004170 return;
4171 }
4172
Pekka Paalanene67858b2013-04-25 13:57:42 +03004173 /* make sure the parent is in its own list */
4174 if (wl_list_empty(&parent->subsurface_list)) {
4175 if (!weston_subsurface_create_for_parent(parent)) {
4176 wl_resource_post_no_memory(resource);
4177 return;
4178 }
4179 }
4180
4181 sub = weston_subsurface_create(id, surface, parent);
4182 if (!sub) {
4183 wl_resource_post_no_memory(resource);
4184 return;
4185 }
4186
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004187 surface->committed = subsurface_committed;
4188 surface->committed_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004189 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004190}
4191
4192static void
4193subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
4194{
4195 wl_resource_destroy(resource);
4196}
4197
4198static const struct wl_subcompositor_interface subcompositor_interface = {
4199 subcompositor_destroy,
4200 subcompositor_get_subsurface
4201};
4202
4203static void
4204bind_subcompositor(struct wl_client *client,
4205 void *data, uint32_t version, uint32_t id)
4206{
4207 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004208 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004209
Jason Ekstranda85118c2013-06-27 20:17:02 -05004210 resource =
4211 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004212 if (resource == NULL) {
4213 wl_client_post_no_memory(client);
4214 return;
4215 }
4216 wl_resource_set_implementation(resource, &subcompositor_interface,
4217 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004218}
4219
Bryce Harrington0795ece2016-08-30 12:04:26 -07004220/** Set a DPMS mode on all of the compositor's outputs
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004221 *
4222 * \param compositor The compositor instance
4223 * \param state The DPMS state the outputs will be set to
4224 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004225static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004226weston_compositor_dpms(struct weston_compositor *compositor,
4227 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004228{
4229 struct weston_output *output;
4230
Bryce Harrington08976ac2016-08-30 12:05:16 -07004231 wl_list_for_each(output, &compositor->output_list, link)
4232 if (output->set_dpms)
4233 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004234}
4235
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004236/** Restores the compositor to active status
4237 *
4238 * \param compositor The compositor instance
4239 *
4240 * If the compositor was in a sleeping mode, all outputs are powered
4241 * back on via DPMS. Otherwise if the compositor was inactive
4242 * (idle/locked, offscreen, or sleeping) then the compositor's wake
4243 * signal will fire.
4244 *
4245 * Restarts the idle timer.
4246 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004247WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004248weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004249{
Neil Roberts8b62e202013-09-30 13:14:47 +01004250 uint32_t old_state = compositor->state;
4251
4252 /* The state needs to be changed before emitting the wake
4253 * signal because that may try to schedule a repaint which
4254 * will not work if the compositor is still sleeping */
4255 compositor->state = WESTON_COMPOSITOR_ACTIVE;
4256
4257 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004258 case WESTON_COMPOSITOR_SLEEPING:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004259 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004260 case WESTON_COMPOSITOR_OFFSCREEN:
Daniel Stone893b9362016-11-08 15:47:09 +00004261 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004262 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004263 /* fall through */
4264 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004265 wl_event_source_timer_update(compositor->idle_source,
4266 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004267 }
4268}
4269
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004270/** Turns off rendering and frame events for the compositor.
4271 *
4272 * \param compositor The compositor instance
4273 *
4274 * This is used for example to prevent further rendering while the
4275 * compositor is shutting down.
4276 *
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004277 * Stops the idle timer.
4278 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004279WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004280weston_compositor_offscreen(struct weston_compositor *compositor)
4281{
4282 switch (compositor->state) {
4283 case WESTON_COMPOSITOR_OFFSCREEN:
4284 return;
4285 case WESTON_COMPOSITOR_SLEEPING:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004286 default:
4287 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4288 wl_event_source_timer_update(compositor->idle_source, 0);
4289 }
4290}
4291
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004292/** Powers down all attached output devices
4293 *
4294 * \param compositor The compositor instance
4295 *
4296 * Causes rendering to the outputs to cease, and no frame events to be
4297 * sent. Only powers down the outputs if the compositor is not already
4298 * in sleep mode.
4299 *
4300 * Stops the idle timer.
4301 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004302WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004303weston_compositor_sleep(struct weston_compositor *compositor)
4304{
4305 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
4306 return;
4307
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004308 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004309 compositor->state = WESTON_COMPOSITOR_SLEEPING;
4310 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
4311}
4312
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004313/** Sets compositor to idle mode
4314 *
4315 * \param data The compositor instance
4316 *
4317 * This is called when the idle timer fires. Once the compositor is in
4318 * idle mode it requires a wake action (e.g. via
4319 * weston_compositor_wake()) to restore it. The compositor's
4320 * idle_signal will be triggered when the idle event occurs.
4321 *
4322 * Idleness can be inhibited by setting the compositor's idle_inhibit
4323 * property.
4324 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004325static int
4326idle_handler(void *data)
4327{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004328 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004329
4330 if (compositor->idle_inhibit)
4331 return 1;
4332
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004333 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004334 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004335
4336 return 1;
4337}
4338
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004339WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08004340weston_plane_init(struct weston_plane *plane,
4341 struct weston_compositor *ec,
4342 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004343{
4344 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004345 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004346 plane->x = x;
4347 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08004348 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004349
4350 /* Init the link so that the call to wl_list_remove() when releasing
4351 * the plane without ever stacking doesn't lead to a crash */
4352 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004353}
4354
4355WL_EXPORT void
4356weston_plane_release(struct weston_plane *plane)
4357{
Xiong Zhang97116532013-10-23 13:58:31 +08004358 struct weston_view *view;
4359
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004360 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004361 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004362
Xiong Zhang97116532013-10-23 13:58:31 +08004363 wl_list_for_each(view, &plane->compositor->view_list, link) {
4364 if (view->plane == plane)
4365 view->plane = NULL;
4366 }
4367
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004368 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004369}
4370
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004371WL_EXPORT void
4372weston_compositor_stack_plane(struct weston_compositor *ec,
4373 struct weston_plane *plane,
4374 struct weston_plane *above)
4375{
4376 if (above)
4377 wl_list_insert(above->link.prev, &plane->link);
4378 else
4379 wl_list_insert(&ec->plane_list, &plane->link);
4380}
4381
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004382static void
4383output_release(struct wl_client *client, struct wl_resource *resource)
4384{
4385 wl_resource_destroy(resource);
4386}
4387
4388static const struct wl_output_interface output_interface = {
4389 output_release,
4390};
4391
4392
Casey Dahlin9074db52012-04-19 22:50:09 -04004393static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004394{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004395 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004396}
4397
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004398static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004399bind_output(struct wl_client *client,
4400 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05004401{
Pekka Paalanen05347622017-03-27 12:24:34 +03004402 struct weston_head *head = data;
4403 struct weston_output *output = head->output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004404 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004405 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004406
Jason Ekstranda85118c2013-06-27 20:17:02 -05004407 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004408 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004409 if (resource == NULL) {
4410 wl_client_post_no_memory(client);
4411 return;
4412 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004413
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03004414 wl_list_insert(&head->resource_list, wl_resource_get_link(resource));
Pekka Paalanen055c1132017-03-27 16:31:25 +03004415 wl_resource_set_implementation(resource, &output_interface, head,
Pekka Paalanen05347622017-03-27 12:24:34 +03004416 unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04004417
Pekka Paalanen05347622017-03-27 12:24:34 +03004418 assert(output);
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004419 wl_output_send_geometry(resource,
4420 output->x,
4421 output->y,
Pekka Paalanen01f60212017-03-24 15:39:24 +02004422 head->mm_width,
4423 head->mm_height,
4424 head->subpixel,
4425 head->make, head->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04004426 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004427 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004428 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02004429 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004430
4431 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004432 wl_output_send_mode(resource,
4433 mode->flags,
4434 mode->width,
4435 mode->height,
4436 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004437 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02004438
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004439 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004440 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004441}
4442
Pekka Paalanendcac3512017-12-08 14:13:34 +02004443static void
4444weston_head_add_global(struct weston_head *head)
4445{
4446 head->global = wl_global_create(head->compositor->wl_display,
4447 &wl_output_interface, 3,
4448 head, bind_output);
4449}
4450
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02004451/** Remove the global wl_output protocol object
4452 *
4453 * \param head The head whose global to remove.
4454 *
4455 * Also orphans the wl_resources for this head (wl_output).
4456 */
4457static void
4458weston_head_remove_global(struct weston_head *head)
4459{
4460 struct wl_resource *resource, *tmp;
4461
4462 if (head->global)
4463 wl_global_destroy(head->global);
4464 head->global = NULL;
4465
4466 wl_resource_for_each_safe(resource, tmp, &head->resource_list) {
4467 unbind_resource(resource);
4468 wl_resource_set_destructor(resource, NULL);
4469 wl_resource_set_user_data(resource, NULL);
4470 }
4471}
4472
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004473/** Get the backing object of wl_output
4474 *
4475 * \param resource A wl_output protocol object.
4476 * \return The backing object (user data) of a wl_resource representing a
4477 * wl_output protocol object.
4478 */
Pekka Paalanen055c1132017-03-27 16:31:25 +03004479WL_EXPORT struct weston_head *
4480weston_head_from_resource(struct wl_resource *resource)
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004481{
4482 assert(wl_resource_instance_of(resource, &wl_output_interface,
4483 &output_interface));
4484
4485 return wl_resource_get_user_data(resource);
4486}
4487
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004488/** Initialize a pre-allocated weston_head
4489 *
4490 * \param head The head to initialize.
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004491 * \param name The head name, e.g. the connector name or equivalent.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004492 *
4493 * The head will be safe to attach, detach and release.
4494 *
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004495 * The name is used in logs, and can be used by compositors as a configuration
4496 * identifier.
4497 *
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004498 * \memberof weston_head
4499 * \internal
4500 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004501WL_EXPORT void
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004502weston_head_init(struct weston_head *head, const char *name)
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004503{
4504 /* Add some (in)sane defaults which can be used
4505 * for checking if an output was properly configured
4506 */
4507 memset(head, 0, sizeof *head);
4508
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004509 wl_list_init(&head->compositor_link);
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03004510 wl_signal_init(&head->destroy_signal);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004511 wl_list_init(&head->output_link);
4512 wl_list_init(&head->resource_list);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004513 head->name = strdup(name);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004514}
4515
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004516/** Send output heads changed signal
4517 *
4518 * \param output The output that changed.
4519 *
4520 * Notify that the enabled output gained and/or lost heads, or that the
4521 * associated heads may have changed their connection status. This does not
4522 * include cases where the output becomes enabled or disabled. The registered
4523 * callbacks are called after the change has successfully happened.
4524 *
4525 * If connection status change causes the compositor to attach or detach a head
4526 * to an enabled output, the registered callbacks may be called multiple times.
4527 */
4528static void
4529weston_output_emit_heads_changed(struct weston_output *output)
4530{
4531 wl_signal_emit(&output->compositor->output_heads_changed_signal,
4532 output);
4533}
4534
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004535/** Idle task for emitting heads_changed_signal */
4536static void
4537weston_compositor_call_heads_changed(void *data)
4538{
4539 struct weston_compositor *compositor = data;
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004540 struct weston_head *head;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004541
4542 compositor->heads_changed_source = NULL;
4543
4544 wl_signal_emit(&compositor->heads_changed_signal, compositor);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004545
4546 wl_list_for_each(head, &compositor->head_list, compositor_link) {
4547 if (head->output && head->output->enabled)
4548 weston_output_emit_heads_changed(head->output);
4549 }
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004550}
4551
4552/** Schedule a call on idle to heads_changed callback
4553 *
4554 * \param compositor The Compositor.
4555 *
4556 * \memberof weston_compositor
4557 * \internal
4558 */
4559static void
4560weston_compositor_schedule_heads_changed(struct weston_compositor *compositor)
4561{
4562 struct wl_event_loop *loop;
4563
4564 if (compositor->heads_changed_source)
4565 return;
4566
4567 loop = wl_display_get_event_loop(compositor->wl_display);
4568 compositor->heads_changed_source = wl_event_loop_add_idle(loop,
4569 weston_compositor_call_heads_changed, compositor);
4570}
4571
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004572/** Register a new head
4573 *
4574 * \param compositor The compositor.
4575 * \param head The head to register, must not be already registered.
4576 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004577 * This signals the core that a new head has become available, leading to
4578 * heads_changed hook being called later.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004579 *
4580 * \memberof weston_compositor
4581 * \internal
4582 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004583WL_EXPORT void
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004584weston_compositor_add_head(struct weston_compositor *compositor,
4585 struct weston_head *head)
4586{
4587 assert(wl_list_empty(&head->compositor_link));
4588 assert(head->name);
4589
4590 wl_list_insert(compositor->head_list.prev, &head->compositor_link);
4591 head->compositor = compositor;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004592 weston_compositor_schedule_heads_changed(compositor);
4593}
4594
4595/** Adds a listener to be called when heads change
4596 *
4597 * \param compositor The compositor.
4598 * \param listener The listener to add.
4599 *
4600 * The listener notify function argument is the \var compositor.
4601 *
4602 * The listener function will be called after heads are added or their
4603 * connection status has changed. Several changes may be accumulated into a
4604 * single call. The user is expected to iterate over the existing heads and
4605 * check their statuses to find out what changed.
4606 *
4607 * \sa weston_compositor_iterate_heads, weston_head_is_connected,
4608 * weston_head_is_enabled
4609 * \memberof weston_compositor
4610 */
4611WL_EXPORT void
4612weston_compositor_add_heads_changed_listener(struct weston_compositor *compositor,
4613 struct wl_listener *listener)
4614{
4615 wl_signal_add(&compositor->heads_changed_signal, listener);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004616}
4617
4618/** Iterate over available heads
4619 *
4620 * \param compositor The compositor.
4621 * \param item The iterator, or NULL for start.
4622 * \return The next available head in the list.
4623 *
4624 * Returns all available heads, regardless of being connected or enabled.
4625 *
4626 * You can iterate over all heads as follows:
4627 * \code
4628 * struct weston_head *head = NULL;
4629 *
4630 * while ((head = weston_compositor_iterate_heads(compositor, head))) {
4631 * ...
4632 * }
4633 * \endcode
4634 *
4635 * If you cause \c iter to be removed from the list, you cannot use it to
4636 * continue iterating. Removing any other item is safe.
4637 *
4638 * \memberof weston_compositor
4639 */
4640WL_EXPORT struct weston_head *
4641weston_compositor_iterate_heads(struct weston_compositor *compositor,
4642 struct weston_head *iter)
4643{
4644 struct wl_list *list = &compositor->head_list;
4645 struct wl_list *node;
4646
4647 assert(compositor);
4648 assert(!iter || iter->compositor == compositor);
4649
4650 if (iter)
4651 node = iter->compositor_link.next;
4652 else
4653 node = list->next;
4654
4655 assert(node);
4656 assert(!iter || node != &iter->compositor_link);
4657
4658 if (node == list)
4659 return NULL;
4660
4661 return container_of(node, struct weston_head, compositor_link);
4662}
4663
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004664/** Iterate over attached heads
4665 *
4666 * \param output The output whose heads to iterate.
4667 * \param item The iterator, or NULL for start.
4668 * \return The next attached head in the list.
4669 *
4670 * Returns all heads currently attached to the output.
4671 *
4672 * You can iterate over all heads as follows:
4673 * \code
4674 * struct weston_head *head = NULL;
4675 *
4676 * while ((head = weston_output_iterate_heads(output, head))) {
4677 * ...
4678 * }
4679 * \endcode
4680 *
4681 * If you cause \c iter to be removed from the list, you cannot use it to
4682 * continue iterating. Removing any other item is safe.
4683 *
4684 * \memberof weston_compositor
4685 */
4686WL_EXPORT struct weston_head *
4687weston_output_iterate_heads(struct weston_output *output,
4688 struct weston_head *iter)
4689{
4690 struct wl_list *list = &output->head_list;
4691 struct wl_list *node;
4692
4693 assert(output);
4694 assert(!iter || iter->output == output);
4695
4696 if (iter)
4697 node = iter->output_link.next;
4698 else
4699 node = list->next;
4700
4701 assert(node);
4702 assert(!iter || node != &iter->output_link);
4703
4704 if (node == list)
4705 return NULL;
4706
4707 return container_of(node, struct weston_head, output_link);
4708}
4709
Pekka Paalanendcac3512017-12-08 14:13:34 +02004710/** Attach a head to an output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004711 *
4712 * \param output The output to attach to.
4713 * \param head The head that is not yet attached.
4714 * \return 0 on success, -1 on failure.
4715 *
4716 * Attaches the given head to the output. All heads of an output are clones
4717 * and share the resolution and timings.
4718 *
4719 * Cloning heads this way uses less resources than creating an output for
4720 * each head, but is not always possible due to environment, driver and hardware
4721 * limitations.
4722 *
4723 * On failure, the head remains unattached. Success of this function does not
4724 * guarantee the output configuration is actually valid. The final checks are
Pekka Paalanendcac3512017-12-08 14:13:34 +02004725 * made on weston_output_enable() unless the output was already enabled.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004726 *
4727 * \memberof weston_output
4728 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004729WL_EXPORT int
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004730weston_output_attach_head(struct weston_output *output,
4731 struct weston_head *head)
4732{
Pekka Paalanendcac3512017-12-08 14:13:34 +02004733 char *head_names;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004734
4735 if (!wl_list_empty(&head->output_link))
4736 return -1;
4737
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004738 if (output->attach_head) {
4739 if (output->attach_head(output, head) < 0)
4740 return -1;
4741 } else if (!wl_list_empty(&output->head_list)) {
4742 /* No support for clones in the legacy path. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004743 return -1;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004744 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004745
4746 head->output = output;
4747 wl_list_insert(output->head_list.prev, &head->output_link);
4748
Pekka Paalanendcac3512017-12-08 14:13:34 +02004749 if (output->enabled) {
4750 weston_head_add_global(head);
4751
4752 head_names = weston_output_create_heads_string(output);
4753 weston_log("Output '%s' updated to have head(s) %s\n",
4754 output->name, head_names);
4755 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004756
4757 weston_output_emit_heads_changed(output);
Pekka Paalanendcac3512017-12-08 14:13:34 +02004758 }
4759
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004760 return 0;
4761}
4762
4763/** Detach a head from its output
4764 *
4765 * \param head The head to detach.
4766 *
4767 * It is safe to detach a non-attached head.
4768 *
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004769 * If the head is attached to an enabled output and the output will be left
4770 * with no heads, the output will be disabled.
4771 *
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004772 * \memberof weston_head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004773 * \sa weston_output_disable
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004774 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004775WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004776weston_head_detach(struct weston_head *head)
4777{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004778 struct weston_output *output = head->output;
Pekka Paalanena0106992017-12-08 16:11:17 +02004779 char *head_names;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004780
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004781 wl_list_remove(&head->output_link);
4782 wl_list_init(&head->output_link);
4783 head->output = NULL;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004784
4785 if (!output)
4786 return;
4787
4788 if (output->detach_head)
4789 output->detach_head(output, head);
4790
4791 if (output->enabled) {
4792 weston_head_remove_global(head);
4793
Pekka Paalanena0106992017-12-08 16:11:17 +02004794 if (wl_list_empty(&output->head_list)) {
4795 weston_log("Output '%s' no heads left, disabling.\n",
4796 output->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004797 weston_output_disable(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02004798 } else {
4799 head_names = weston_output_create_heads_string(output);
4800 weston_log("Output '%s' updated to have head(s) %s\n",
4801 output->name, head_names);
4802 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004803
4804 weston_output_emit_heads_changed(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02004805 }
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004806 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004807}
4808
4809/** Destroy a head
4810 *
4811 * \param head The head to be released.
4812 *
4813 * Destroys the head. The caller is responsible for freeing the memory pointed
4814 * to by \c head.
4815 *
4816 * \memberof weston_head
4817 * \internal
4818 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004819WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004820weston_head_release(struct weston_head *head)
4821{
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03004822 wl_signal_emit(&head->destroy_signal, head);
4823
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004824 weston_head_detach(head);
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03004825
4826 free(head->make);
4827 free(head->model);
4828 free(head->serial_number);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004829 free(head->name);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004830
4831 wl_list_remove(&head->compositor_link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004832}
4833
Pekka Paalanene19970f2017-08-28 14:11:02 +03004834static void
4835weston_head_set_device_changed(struct weston_head *head)
4836{
4837 head->device_changed = true;
4838
4839 if (head->compositor)
4840 weston_compositor_schedule_heads_changed(head->compositor);
4841}
4842
4843/** String equal comparison with NULLs being equal */
4844static bool
4845str_null_eq(const char *a, const char *b)
4846{
4847 if (!a && !b)
4848 return true;
4849
4850 if (!!a != !!b)
4851 return false;
4852
4853 return strcmp(a, b) == 0;
4854}
4855
Pekka Paalanen01f60212017-03-24 15:39:24 +02004856/** Store monitor make, model and serial number
4857 *
4858 * \param head The head to modify.
4859 * \param make The monitor make. If EDID is available, the PNP ID. Otherwise
4860 * any string, or NULL for none.
4861 * \param model The monitor model or name, or a made-up string, or NULL for
4862 * none.
4863 * \param serialno The monitor serial number, a made-up string, or NULL for
4864 * none.
4865 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03004866 * This may set the device_changed flag.
4867 *
Pekka Paalanen01f60212017-03-24 15:39:24 +02004868 * \memberof weston_head
4869 * \internal
4870 */
4871WL_EXPORT void
4872weston_head_set_monitor_strings(struct weston_head *head,
4873 const char *make,
4874 const char *model,
4875 const char *serialno)
4876{
Pekka Paalanene19970f2017-08-28 14:11:02 +03004877 if (str_null_eq(head->make, make) &&
4878 str_null_eq(head->model, model) &&
4879 str_null_eq(head->serial_number, serialno))
4880 return;
4881
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03004882 free(head->make);
4883 free(head->model);
4884 free(head->serial_number);
4885
4886 head->make = make ? strdup(make) : NULL;
4887 head->model = model ? strdup(model) : NULL;
4888 head->serial_number = serialno ? strdup(serialno) : NULL;
Pekka Paalanene19970f2017-08-28 14:11:02 +03004889
4890 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02004891}
4892
4893/** Store physical image size
4894 *
4895 * \param head The head to modify.
4896 * \param mm_width Image area width in millimeters.
4897 * \param mm_height Image area height in millimeters.
4898 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03004899 * This may set the device_changed flag.
4900 *
Pekka Paalanen01f60212017-03-24 15:39:24 +02004901 * \memberof weston_head
4902 * \internal
4903 */
4904WL_EXPORT void
4905weston_head_set_physical_size(struct weston_head *head,
4906 int32_t mm_width, int32_t mm_height)
4907{
Pekka Paalanene19970f2017-08-28 14:11:02 +03004908 if (head->mm_width == mm_width &&
4909 head->mm_height == mm_height)
4910 return;
4911
Pekka Paalanen01f60212017-03-24 15:39:24 +02004912 head->mm_width = mm_width;
4913 head->mm_height = mm_height;
Pekka Paalanene19970f2017-08-28 14:11:02 +03004914
4915 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02004916}
4917
4918/** Store monitor sub-pixel layout
4919 *
4920 * \param head The head to modify.
4921 * \param sp Sub-pixel layout. The possible values are:
4922 * - WL_OUTPUT_SUBPIXEL_UNKNOWN,
4923 * - WL_OUTPUT_SUBPIXEL_NONE,
4924 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB,
4925 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR,
4926 * - WL_OUTPUT_SUBPIXEL_VERTICAL_RGB,
4927 * - WL_OUTPUT_SUBPIXEL_VERTICAL_BGR
4928 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03004929 * This may set the device_changed flag.
4930 *
Pekka Paalanen01f60212017-03-24 15:39:24 +02004931 * \memberof weston_head
4932 * \internal
4933 */
4934WL_EXPORT void
4935weston_head_set_subpixel(struct weston_head *head,
4936 enum wl_output_subpixel sp)
4937{
Pekka Paalanene19970f2017-08-28 14:11:02 +03004938 if (head->subpixel == sp)
4939 return;
4940
Pekka Paalanen01f60212017-03-24 15:39:24 +02004941 head->subpixel = sp;
Pekka Paalanene19970f2017-08-28 14:11:02 +03004942
4943 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02004944}
4945
4946/** Mark the monitor as internal
4947 *
4948 * This is used for embedded screens, like laptop panels.
4949 *
4950 * \param head The head to mark as internal.
4951 *
4952 * By default a head is external. The type is often inferred from the physical
4953 * connector type.
4954 *
4955 * \memberof weston_head
4956 * \internal
4957 */
4958WL_EXPORT void
4959weston_head_set_internal(struct weston_head *head)
4960{
4961 head->connection_internal = true;
4962}
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004963
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03004964/** Store connector status
4965 *
4966 * \param head The head to modify.
4967 * \param connected Whether the head is connected.
4968 *
4969 * Connectors are created as disconnected. This function can be used to
4970 * set the connector status.
4971 *
4972 * The status should be set to true when a physical connector is connected to
4973 * a video sink device like a monitor and to false when the connector is
4974 * disconnected. For nested backends, the connection status should reflect the
4975 * connection to the parent display server.
4976 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004977 * When the connection status changes, it schedules a call to the heads_changed
Pekka Paalanene19970f2017-08-28 14:11:02 +03004978 * hook and sets the device_changed flag.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004979 *
4980 * \sa weston_compositor_set_heads_changed_cb
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03004981 * \memberof weston_head
4982 * \internal
4983 */
4984WL_EXPORT void
4985weston_head_set_connection_status(struct weston_head *head, bool connected)
4986{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004987 if (head->connected == connected)
4988 return;
4989
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03004990 head->connected = connected;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004991
Pekka Paalanene19970f2017-08-28 14:11:02 +03004992 weston_head_set_device_changed(head);
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03004993}
4994
4995/** Is the head currently connected?
4996 *
4997 * \param head The head to query.
4998 * \return Connection status.
4999 *
5000 * Returns true if the head is physically connected to a monitor, or in
5001 * case of a nested backend returns true when there is a connection to the
5002 * parent display server.
5003 *
5004 * This is independent from the head being enabled.
5005 *
5006 * \sa weston_head_is_enabled
5007 * \memberof weston_head
5008 */
5009WL_EXPORT bool
5010weston_head_is_connected(struct weston_head *head)
5011{
5012 return head->connected;
5013}
5014
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005015/** Is the head currently enabled?
5016 *
5017 * \param head The head to query.
5018 * \return Video status.
5019 *
5020 * Returns true if the head is currently transmitting a video stream.
5021 *
5022 * This is independent of the head being connected.
5023 *
5024 * \sa weston_head_is_connected
5025 * \memberof weston_head
5026 */
5027WL_EXPORT bool
5028weston_head_is_enabled(struct weston_head *head)
5029{
5030 if (!head->output)
5031 return false;
5032
5033 return head->output->enabled;
5034}
5035
Pekka Paalanene19970f2017-08-28 14:11:02 +03005036/** Has the device information changed?
5037 *
5038 * \param head The head to query.
5039 * \return True if the device information has changed since last reset.
5040 *
5041 * The information about the connected display device, e.g. a monitor, may
5042 * change without being disconnected in between. Changing information
5043 * causes a call to the heads_changed hook.
5044 *
5045 * The information includes make, model, serial number, physical size,
5046 * and sub-pixel type. The connection status is also included.
5047 *
5048 * \sa weston_head_reset_device_changed, weston_compositor_set_heads_changed_cb
5049 * \memberof weston_head
5050 */
5051WL_EXPORT bool
5052weston_head_is_device_changed(struct weston_head *head)
5053{
5054 return head->device_changed;
5055}
5056
5057/** Acknowledge device information change
5058 *
5059 * \param head The head to acknowledge.
5060 *
5061 * Clears the device changed flag on this head. When a compositor has processed
5062 * device information, it should call this to be able to notice further
5063 * changes.
5064 *
5065 * \sa weston_head_is_device_changed
5066 * \memberof weston_head
5067 */
5068WL_EXPORT void
5069weston_head_reset_device_changed(struct weston_head *head)
5070{
5071 head->device_changed = false;
5072}
5073
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005074/** Get the name of a head
5075 *
5076 * \param head The head to query.
5077 * \return The head's name, not NULL.
5078 *
5079 * The name depends on the backend. The DRM backend uses connector names,
5080 * other backends may use hardcoded names or user-given names.
5081 */
5082WL_EXPORT const char *
5083weston_head_get_name(struct weston_head *head)
5084{
5085 return head->name;
5086}
5087
5088/** Get the output the head is attached to
5089 *
5090 * \param head The head to query.
5091 * \return The output the head is attached to, or NULL if detached.
5092 */
5093WL_EXPORT struct weston_output *
5094weston_head_get_output(struct weston_head *head)
5095{
5096 return head->output;
5097}
5098
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005099/** Add destroy callback for a head
5100 *
5101 * \param head The head to watch for.
5102 * \param listener The listener to add. The \c notify member must be set.
5103 *
5104 * Heads may get destroyed for various reasons by the backends. If a head is
5105 * attached to an output, the compositor should listen for head destruction
5106 * and reconfigure or destroy the output if necessary.
5107 *
5108 * The destroy callbacks will be called on weston_head destruction before any
5109 * automatic detaching from an associated weston_output and before any
5110 * weston_head information is lost.
5111 *
5112 * The \c data argument to the notify callback is the weston_head being
5113 * destroyed.
5114 */
5115WL_EXPORT void
5116weston_head_add_destroy_listener(struct weston_head *head,
5117 struct wl_listener *listener)
5118{
5119 wl_signal_add(&head->destroy_signal, listener);
5120}
5121
5122/** Look up destroy listener for a head
5123 *
5124 * \param head The head to query.
5125 * \param notify The notify function used used for the added destroy listener.
5126 * \return The listener, or NULL if not found.
5127 *
5128 * This looks up the previously added destroy listener struct based on the
5129 * notify function it has. The listener can be used to access user data
5130 * through \c container_of().
5131 *
5132 * \sa wl_signal_get()
5133 */
5134WL_EXPORT struct wl_listener *
5135weston_head_get_destroy_listener(struct weston_head *head,
5136 wl_notify_func_t notify)
5137{
5138 return wl_signal_get(&head->destroy_signal, notify);
5139}
5140
David Fort0de859e2016-05-27 23:22:57 +02005141/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005142static void
David Fort0de859e2016-05-27 23:22:57 +02005143weston_compositor_reflow_outputs(struct weston_compositor *compositor,
5144 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005145{
5146 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02005147 bool start_resizing = false;
5148
5149 if (!delta_width)
5150 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005151
5152 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02005153 if (output == resized_output) {
5154 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005155 continue;
5156 }
5157
David Fort0de859e2016-05-27 23:22:57 +02005158 if (start_resizing) {
5159 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005160 output->dirty = 1;
5161 }
5162 }
5163}
5164
Pekka Paalanend72bad22017-03-29 17:01:41 +03005165static void
Scott Moreauccbf29d2012-02-22 14:21:41 -07005166weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005167{
Scott Moreau850ca422012-05-21 15:21:25 -06005168 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05005169
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005170 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005171 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06005172
Scott Moreauccbf29d2012-02-22 14:21:41 -07005173 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06005174 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005175 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005176 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005177 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005178 weston_matrix_scale(&output->matrix, magnification,
5179 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005180 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005181
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005182 switch (output->transform) {
5183 case WL_OUTPUT_TRANSFORM_FLIPPED:
5184 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5185 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5186 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5187 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5188 weston_matrix_scale(&output->matrix, -1, 1, 1);
5189 break;
5190 }
5191
5192 switch (output->transform) {
5193 default:
5194 case WL_OUTPUT_TRANSFORM_NORMAL:
5195 case WL_OUTPUT_TRANSFORM_FLIPPED:
5196 break;
5197 case WL_OUTPUT_TRANSFORM_90:
5198 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5199 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
5200 weston_matrix_rotate_xy(&output->matrix, 0, 1);
5201 break;
5202 case WL_OUTPUT_TRANSFORM_180:
5203 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5204 weston_matrix_translate(&output->matrix,
5205 -output->width, -output->height, 0);
5206 weston_matrix_rotate_xy(&output->matrix, -1, 0);
5207 break;
5208 case WL_OUTPUT_TRANSFORM_270:
5209 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5210 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5211 weston_matrix_rotate_xy(&output->matrix, 0, -1);
5212 break;
5213 }
5214
5215 if (output->current_scale != 1)
5216 weston_matrix_scale(&output->matrix,
5217 output->current_scale,
5218 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01005219
Scott Moreauccbf29d2012-02-22 14:21:41 -07005220 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05005221
5222 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005223}
5224
Scott Moreau1bad5db2012-08-18 01:04:05 -06005225static void
Alexander Larsson0b135062013-05-28 16:23:36 +02005226weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06005227{
5228 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005229 output->native_scale = scale;
5230 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06005231
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005232 convert_size_by_transform_scale(&output->width, &output->height,
5233 output->current_mode->width,
5234 output->current_mode->height,
5235 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02005236}
5237
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005238static void
5239weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07005240{
5241 output->x = x;
5242 output->y = y;
5243
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005244 pixman_region32_fini(&output->previous_damage);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02005245 pixman_region32_init(&output->previous_damage);
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005246
5247 pixman_region32_fini(&output->region);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005248 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06005249 output->width,
5250 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005251}
5252
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005253WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005254weston_output_move(struct weston_output *output, int x, int y)
5255{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005256 struct weston_head *head;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005257 struct wl_resource *resource;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005258 int ver;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005259
5260 output->move_x = x - output->x;
5261 output->move_y = y - output->y;
5262
5263 if (output->move_x == 0 && output->move_y == 0)
5264 return;
5265
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005266 weston_output_init_geometry(output, x, y);
5267
5268 output->dirty = 1;
5269
5270 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005271 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005272
5273 /* Notify clients of the change for output position. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005274 wl_list_for_each(head, &output->head_list, output_link) {
5275 wl_resource_for_each(resource, &head->resource_list) {
5276 wl_output_send_geometry(resource,
5277 output->x,
5278 output->y,
5279 head->mm_width,
5280 head->mm_height,
5281 head->subpixel,
5282 head->make,
5283 head->model,
5284 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08005285
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005286 ver = wl_resource_get_version(resource);
5287 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
5288 wl_output_send_done(resource);
5289 }
Quanxian Wangb2c86362014-03-14 09:16:25 +08005290 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005291}
5292
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005293/** Signal that a pending output is taken into use.
5294 *
5295 * Removes the output from the pending list and adds it to the compositor's
5296 * list of enabled outputs. The output created signal is emitted.
Giulio Camuffob1147152015-05-06 21:41:57 +03005297 *
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005298 * The output gets an internal ID assigned, and the wl_output global is
5299 * created.
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005300 *
Giulio Camuffob1147152015-05-06 21:41:57 +03005301 * \param compositor The compositor instance.
5302 * \param output The output to be added.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005303 *
5304 * \internal
Giulio Camuffob1147152015-05-06 21:41:57 +03005305 */
Pekka Paalanenf9681b52017-03-29 16:58:48 +03005306static void
Giulio Camuffob1147152015-05-06 21:41:57 +03005307weston_compositor_add_output(struct weston_compositor *compositor,
5308 struct weston_output *output)
5309{
Armin Krezoviće5403842016-08-05 15:28:29 +02005310 struct weston_view *view, *next;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005311 struct weston_head *head;
Armin Krezoviće5403842016-08-05 15:28:29 +02005312
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005313 assert(!output->enabled);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005314
5315 /* Verify we haven't reached the limit of 32 available output IDs */
5316 assert(ffs(~compositor->output_id_pool) > 0);
5317
5318 /* Invert the output id pool and look for the lowest numbered
5319 * switch (the least significant bit). Take that bit's position
5320 * as our ID, and mark it used in the compositor's output_id_pool.
5321 */
5322 output->id = ffs(~compositor->output_id_pool) - 1;
5323 compositor->output_id_pool |= 1u << output->id;
5324
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005325 wl_list_remove(&output->link);
Giulio Camuffob1147152015-05-06 21:41:57 +03005326 wl_list_insert(compositor->output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005327 output->enabled = true;
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005328
Pekka Paalanendcac3512017-12-08 14:13:34 +02005329 wl_list_for_each(head, &output->head_list, output_link)
5330 weston_head_add_global(head);
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005331
Giulio Camuffob1147152015-05-06 21:41:57 +03005332 wl_signal_emit(&compositor->output_created_signal, output);
Armin Krezoviće5403842016-08-05 15:28:29 +02005333
5334 wl_list_for_each_safe(view, next, &compositor->view_list, link)
5335 weston_view_geometry_dirty(view);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005336}
5337
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005338/** Transform device coordinates into global coordinates
5339 *
5340 * \param device_x[in] X coordinate in device units.
5341 * \param device_y[in] Y coordinate in device units.
5342 * \param x[out] X coordinate in the global space.
5343 * \param y[out] Y coordinate in the global space.
5344 *
5345 * Transforms coordinates from the device coordinate space
5346 * (physical pixel units) to the global coordinate space (logical pixel units).
5347 * This takes into account output transform and scale.
5348 *
5349 * \memberof weston_output
5350 * \internal
5351 */
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005352WL_EXPORT void
5353weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005354 double device_x, double device_y,
5355 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005356{
Derek Foreman0f679412014-10-02 13:41:17 -05005357 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005358 device_x,
5359 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05005360 0.0,
5361 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005362
Derek Foreman67a18b92015-03-24 11:36:14 -05005363 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005364
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005365 *x = p.f[0] / p.f[3];
5366 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005367}
5368
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005369/** Removes output from compositor's list of enabled outputs
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005370 *
5371 * \param output The weston_output object that is being removed.
5372 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005373 * The following happens:
5374 *
5375 * - The output assignments of all views in the current scenegraph are
5376 * recomputed.
5377 *
5378 * - Presentation feedback is discarded.
5379 *
5380 * - Compositor is notified that outputs were changed and
5381 * applies the necessary changes to re-layout outputs.
5382 *
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005383 * - The output is put back in the pending outputs list.
5384 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005385 * - Signal is emitted to notify all users of the weston_output
5386 * object that the output is being destroyed.
5387 *
5388 * - wl_output protocol objects referencing this weston_output
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005389 * are made inert, and the wl_output global is removed.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005390 *
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005391 * - The output's internal ID is released.
5392 *
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005393 * \memberof weston_output
5394 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005395 */
5396static void
5397weston_compositor_remove_output(struct weston_output *output)
5398{
Pekka Paalanenbccda712017-03-29 16:16:04 +03005399 struct weston_compositor *compositor = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005400 struct weston_view *view;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005401 struct weston_head *head;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005402
5403 assert(output->destroying);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005404 assert(output->enabled);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005405
Pekka Paalanenbccda712017-03-29 16:16:04 +03005406 wl_list_for_each(view, &compositor->view_list, link) {
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005407 if (view->output_mask & (1u << output->id))
5408 weston_view_assign_output(view);
5409 }
5410
5411 weston_presentation_feedback_discard_list(&output->feedback_list);
5412
Pekka Paalanen9711fd92018-06-21 14:26:18 +03005413 weston_compositor_reflow_outputs(compositor, output, -output->width);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005414
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005415 wl_list_remove(&output->link);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005416 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005417 output->enabled = false;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005418
Pekka Paalanenbccda712017-03-29 16:16:04 +03005419 wl_signal_emit(&compositor->output_destroyed_signal, output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005420 wl_signal_emit(&output->destroy_signal, output);
5421
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02005422 wl_list_for_each(head, &output->head_list, output_link)
5423 weston_head_remove_global(head);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005424
5425 compositor->output_id_pool &= ~(1u << output->id);
5426 output->id = 0xffffffff; /* invalid */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005427}
5428
5429/** Sets the output scale for a given output.
5430 *
5431 * \param output The weston_output object that the scale is set for.
5432 * \param scale Scale factor for the given output.
5433 *
5434 * It only supports setting scale for an output that
5435 * is not enabled and it can only be ran once.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005436 *
5437 * \memberof weston_output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005438 */
5439WL_EXPORT void
5440weston_output_set_scale(struct weston_output *output,
5441 int32_t scale)
5442{
5443 /* We can only set scale on a disabled output */
5444 assert(!output->enabled);
5445
5446 /* We only want to set scale once */
5447 assert(!output->scale);
5448
5449 output->scale = scale;
5450}
5451
5452/** Sets the output transform for a given output.
5453 *
5454 * \param output The weston_output object that the transform is set for.
5455 * \param transform Transform value for the given output.
5456 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005457 * Refer to wl_output::transform section located at
5458 * https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_output
5459 * for list of values that can be passed to this function.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005460 *
5461 * \memberof weston_output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005462 */
5463WL_EXPORT void
5464weston_output_set_transform(struct weston_output *output,
5465 uint32_t transform)
5466{
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005467 struct weston_pointer_motion_event ev;
5468 struct wl_resource *resource;
5469 struct weston_seat *seat;
5470 pixman_region32_t old_region;
5471 int mid_x, mid_y;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005472 struct weston_head *head;
5473 int ver;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005474
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005475 if (!output->enabled && output->transform == UINT32_MAX) {
5476 output->transform = transform;
5477 return;
5478 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005479
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005480 weston_output_transform_scale_init(output, transform, output->scale);
5481
5482 pixman_region32_init(&old_region);
5483 pixman_region32_copy(&old_region, &output->region);
5484
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005485 weston_output_init_geometry(output, output->x, output->y);
5486
5487 output->dirty = 1;
5488
5489 /* Notify clients of the change for output transform. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005490 wl_list_for_each(head, &output->head_list, output_link) {
5491 wl_resource_for_each(resource, &head->resource_list) {
5492 wl_output_send_geometry(resource,
5493 output->x,
5494 output->y,
5495 head->mm_width,
5496 head->mm_height,
5497 head->subpixel,
5498 head->make,
5499 head->model,
5500 output->transform);
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005501
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005502 ver = wl_resource_get_version(resource);
5503 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
5504 wl_output_send_done(resource);
5505 }
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005506 }
5507
5508 /* we must ensure that pointers are inside output, otherwise they disappear */
5509 mid_x = output->x + output->width / 2;
5510 mid_y = output->y + output->height / 2;
5511
5512 ev.mask = WESTON_POINTER_MOTION_ABS;
5513 ev.x = wl_fixed_to_double(wl_fixed_from_int(mid_x));
5514 ev.y = wl_fixed_to_double(wl_fixed_from_int(mid_y));
5515
5516 wl_list_for_each(seat, &output->compositor->seat_list, link) {
5517 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5518
5519 if (pointer && pixman_region32_contains_point(&old_region,
5520 wl_fixed_to_int(pointer->x),
5521 wl_fixed_to_int(pointer->y),
5522 NULL))
5523 weston_pointer_move(pointer, &ev);
5524 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005525}
5526
5527/** Initializes a weston_output object with enough data so
5528 ** an output can be configured.
5529 *
5530 * \param output The weston_output object to initialize
5531 * \param compositor The compositor instance.
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005532 * \param name Name for the output (the string is copied).
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005533 *
5534 * Sets initial values for fields that are expected to be
5535 * configured either by compositors or backends.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005536 *
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005537 * The name is used in logs, and can be used by compositors as a configuration
5538 * identifier.
5539 *
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005540 * \memberof weston_output
5541 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005542 */
5543WL_EXPORT void
Armin Krezović40087402016-09-30 14:11:12 +02005544weston_output_init(struct weston_output *output,
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005545 struct weston_compositor *compositor,
5546 const char *name)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005547{
5548 output->compositor = compositor;
5549 output->destroying = 0;
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005550 output->name = strdup(name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005551 wl_list_init(&output->link);
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02005552 wl_signal_init(&output->user_destroy_signal);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005553 output->enabled = false;
5554
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005555 wl_list_init(&output->head_list);
5556
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005557 /* Add some (in)sane defaults which can be used
5558 * for checking if an output was properly configured
5559 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005560 output->scale = 0;
5561 /* Can't use -1 on uint32_t and 0 is valid enum value */
5562 output->transform = UINT32_MAX;
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005563
5564 pixman_region32_init(&output->previous_damage);
5565 pixman_region32_init(&output->region);
Pekka Paalanen42704142017-09-06 16:47:52 +03005566 wl_list_init(&output->mode_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005567}
5568
5569/** Adds weston_output object to pending output list.
5570 *
5571 * \param output The weston_output object to add
5572 * \param compositor The compositor instance.
5573 *
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03005574 * The opposite of this operation is built into weston_output_release().
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005575 *
5576 * \memberof weston_output
5577 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005578 */
5579WL_EXPORT void
5580weston_compositor_add_pending_output(struct weston_output *output,
5581 struct weston_compositor *compositor)
5582{
Pekka Paalanene952a012017-03-29 17:14:00 +03005583 assert(output->disable);
5584 assert(output->enable);
5585
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005586 wl_list_remove(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005587 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005588}
5589
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02005590/** Create a string with the attached heads' names.
5591 *
5592 * The string must be free()'d.
5593 */
5594static char *
5595weston_output_create_heads_string(struct weston_output *output)
5596{
5597 FILE *fp;
5598 char *str = NULL;
5599 size_t size = 0;
5600 struct weston_head *head;
5601 const char *sep = "";
5602
5603 fp = open_memstream(&str, &size);
5604 if (!fp)
5605 return NULL;
5606
5607 wl_list_for_each(head, &output->head_list, output_link) {
5608 fprintf(fp, "%s%s", sep, head->name);
5609 sep = ", ";
5610 }
5611 fclose(fp);
5612
5613 return str;
5614}
5615
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005616/** Constructs a weston_output object that can be used by the compositor.
5617 *
Pekka Paalanencc201e42017-03-30 15:11:25 +03005618 * \param output The weston_output object that needs to be enabled. Must not
Pekka Paalanenddce54d2017-08-23 16:00:21 +03005619 * be enabled already. Must have at least one head attached.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005620 *
5621 * Output coordinates are calculated and each new output is by default
5622 * assigned to the right of previous one.
5623 *
5624 * Sets up the transformation, zoom, and geometry of the output using
5625 * the properties that need to be configured by the compositor.
5626 *
5627 * Establishes a repaint timer for the output with the relevant display
5628 * object's event loop. See output_repaint_timer_handler().
5629 *
5630 * The output is assigned an ID. Weston can support up to 32 distinct
5631 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
5632 * is referred to and used to find the first available ID number, and
5633 * then this ID is marked as used in output_id_pool.
5634 *
5635 * The output is also assigned a Wayland global with the wl_output
5636 * external interface.
5637 *
5638 * Backend specific function is called to set up the output output.
5639 *
5640 * Output is added to the compositor's output list
5641 *
5642 * If the backend specific function fails, the weston_output object
5643 * is returned to a state it was before calling this function and
5644 * is added to the compositor's pending_output_list in case it needs
5645 * to be reconfigured or just so it can be destroyed at shutdown.
5646 *
5647 * 0 is returned on success, -1 on failure.
5648 */
5649WL_EXPORT int
5650weston_output_enable(struct weston_output *output)
5651{
Armin Krezović782f5df2016-09-30 14:11:11 +02005652 struct weston_compositor *c = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005653 struct weston_output *iterator;
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03005654 struct weston_head *head;
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02005655 char *head_names;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005656 int x = 0, y = 0;
5657
Pekka Paalanencc201e42017-03-30 15:11:25 +03005658 if (output->enabled) {
5659 weston_log("Error: attempt to enable an enabled output '%s'\n",
5660 output->name);
5661 return -1;
5662 }
5663
Pekka Paalanenddce54d2017-08-23 16:00:21 +03005664 if (wl_list_empty(&output->head_list)) {
5665 weston_log("Error: cannot enable output '%s' without heads.\n",
5666 output->name);
5667 return -1;
5668 }
5669
Pekka Paalanen586e1ac2017-09-14 16:17:59 +03005670 if (wl_list_empty(&output->mode_list) || !output->current_mode) {
5671 weston_log("Error: no video mode for output '%s'.\n",
5672 output->name);
5673 return -1;
5674 }
5675
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03005676 wl_list_for_each(head, &output->head_list, output_link) {
5677 assert(head->make);
5678 assert(head->model);
5679 }
5680
Armin Krezović782f5df2016-09-30 14:11:11 +02005681 iterator = container_of(c->output_list.prev,
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005682 struct weston_output, link);
5683
Armin Krezović782f5df2016-09-30 14:11:11 +02005684 if (!wl_list_empty(&c->output_list))
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005685 x = iterator->x + iterator->width;
5686
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005687 /* Make sure the scale is set up */
5688 assert(output->scale);
5689
5690 /* Make sure we have a transform set */
5691 assert(output->transform != UINT32_MAX);
5692
Armin Krezović782f5df2016-09-30 14:11:11 +02005693 output->x = x;
5694 output->y = y;
5695 output->dirty = 1;
5696 output->original_scale = output->scale;
5697
5698 weston_output_transform_scale_init(output, output->transform, output->scale);
5699 weston_output_init_zoom(output);
5700
5701 weston_output_init_geometry(output, x, y);
5702 weston_output_damage(output);
5703
5704 wl_signal_init(&output->frame_signal);
5705 wl_signal_init(&output->destroy_signal);
5706 wl_list_init(&output->animation_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02005707 wl_list_init(&output->feedback_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02005708
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005709 /* Enable the output (set up the crtc or create a
5710 * window representing the output, set up the
5711 * renderer, etc)
5712 */
5713 if (output->enable(output) < 0) {
5714 weston_log("Enabling output \"%s\" failed.\n", output->name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005715 return -1;
5716 }
5717
5718 weston_compositor_add_output(output->compositor, output);
5719
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02005720 head_names = weston_output_create_heads_string(output);
5721 weston_log("Output '%s' enabled with head(s) %s\n",
5722 output->name, head_names);
5723 free(head_names);
5724
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005725 return 0;
5726}
5727
5728/** Converts a weston_output object to a pending output state, so it
5729 ** can be configured again or destroyed.
5730 *
5731 * \param output The weston_output object that needs to be disabled.
5732 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005733 * Calls a backend specific function to disable an output, in case
5734 * such function exists.
5735 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03005736 * The backend specific disable function may choose to postpone the disabling
5737 * by returning a negative value, in which case this function returns early.
5738 * In that case the backend will guarantee the output will be disabled soon
5739 * by the backend calling this function again. One must not attempt to re-enable
5740 * the output until that happens.
5741 *
5742 * Otherwise, if the output is being used by the compositor, it is removed
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005743 * from weston's output_list (see weston_compositor_remove_output())
5744 * and is returned to a state it was before weston_output_enable()
5745 * was ran (see weston_output_enable_undo()).
5746 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03005747 * See weston_output_init() for more information on the
5748 * state output is returned to.
Pekka Paalanencc201e42017-03-30 15:11:25 +03005749 *
5750 * If the output has never been enabled yet, this function can still be
5751 * called to ensure that the output is actually turned off rather than left
5752 * in the state it was discovered in.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005753 */
5754WL_EXPORT void
5755weston_output_disable(struct weston_output *output)
5756{
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005757 /* Should we rename this? */
5758 output->destroying = 1;
5759
Pekka Paalanenc65df642017-03-29 15:45:46 +03005760 /* Disable is called unconditionally also for not-enabled outputs,
5761 * because at compositor start-up, if there is an output that is
5762 * already on but the compositor wants to turn it off, we have to
5763 * forward the turn-off to the backend so it knows to do it.
5764 * The backend cannot initially turn off everything, because it
5765 * would cause unnecessary mode-sets for all outputs the compositor
5766 * wants to be on.
5767 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005768 if (output->disable(output) < 0)
5769 return;
5770
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005771 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005772 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005773
5774 output->destroying = 0;
5775}
5776
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03005777/** Forces a synchronous call to heads_changed hook
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005778 *
5779 * \param compositor The compositor instance
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03005780 *
5781 * If there are new or changed heads, calls the heads_changed hook and
5782 * returns after the hook returns.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005783 */
5784WL_EXPORT void
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03005785weston_compositor_flush_heads_changed(struct weston_compositor *compositor)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005786{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005787 if (compositor->heads_changed_source) {
5788 wl_event_source_remove(compositor->heads_changed_source);
5789 weston_compositor_call_heads_changed(compositor);
5790 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005791}
5792
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02005793/** Add destroy callback for an output
5794 *
5795 * \param output The output to watch.
5796 * \param listener The listener to add. The \c notify member must be set.
5797 *
5798 * The listener callback will be called when user destroys an output. This
5799 * may be delayed by a backend in some cases. The main purpose of the
5800 * listener is to allow hooking up custom data to the output. The custom data
5801 * can be fetched via weston_output_get_destroy_listener() followed by
5802 * container_of().
5803 *
5804 * The \c data argument to the notify callback is the weston_output being
5805 * destroyed.
5806 *
5807 * @note This is for the final destruction of an output, not when it gets
5808 * disabled. If you want to keep track of enabled outputs, this is not it.
5809 */
5810WL_EXPORT void
5811weston_output_add_destroy_listener(struct weston_output *output,
5812 struct wl_listener *listener)
5813{
5814 wl_signal_add(&output->user_destroy_signal, listener);
5815}
5816
5817/** Look up destroy listener for an output
5818 *
5819 * \param output The output to query.
5820 * \param notify The notify function used used for the added destroy listener.
5821 * \return The listener, or NULL if not found.
5822 *
5823 * This looks up the previously added destroy listener struct based on the
5824 * notify function it has. The listener can be used to access user data
5825 * through \c container_of().
5826 *
5827 * \sa wl_signal_get() weston_output_add_destroy_listener()
5828 */
5829WL_EXPORT struct wl_listener *
5830weston_output_get_destroy_listener(struct weston_output *output,
5831 wl_notify_func_t notify)
5832{
5833 return wl_signal_get(&output->user_destroy_signal, notify);
5834}
5835
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005836/** Uninitialize an output
5837 *
5838 * Removes the output from the list of enabled outputs if necessary, but
5839 * does not call the backend's output disable function. The output will no
5840 * longer be in the list of pending outputs either.
5841 *
5842 * All fields of weston_output become uninitialized, i.e. should not be used
5843 * anymore. The caller can free the memory after this.
5844 *
5845 * \memberof weston_output
5846 * \internal
5847 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005848WL_EXPORT void
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03005849weston_output_release(struct weston_output *output)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005850{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005851 struct weston_head *head, *tmp;
5852
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005853 output->destroying = 1;
5854
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02005855 wl_signal_emit(&output->user_destroy_signal, output);
5856
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03005857 if (output->idle_repaint_source)
5858 wl_event_source_remove(output->idle_repaint_source);
5859
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005860 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005861 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005862
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005863 pixman_region32_fini(&output->region);
5864 pixman_region32_fini(&output->previous_damage);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005865 wl_list_remove(&output->link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005866
5867 wl_list_for_each_safe(head, tmp, &output->head_list, output_link)
5868 weston_head_detach(head);
5869
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005870 free(output->name);
5871}
5872
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02005873/** Find an output by its given name
5874 *
5875 * \param compositor The compositor to search in.
5876 * \param name The output name to search for.
5877 * \return An existing output with the given name, or NULL if not found.
5878 *
5879 * \memberof weston_compositor
5880 */
5881WL_EXPORT struct weston_output *
5882weston_compositor_find_output_by_name(struct weston_compositor *compositor,
5883 const char *name)
5884{
5885 struct weston_output *output;
5886
5887 wl_list_for_each(output, &compositor->output_list, link)
5888 if (strcmp(output->name, name) == 0)
5889 return output;
5890
5891 wl_list_for_each(output, &compositor->pending_output_list, link)
5892 if (strcmp(output->name, name) == 0)
5893 return output;
5894
5895 return NULL;
5896}
5897
5898/** Create a named output
5899 *
5900 * \param compositor The compositor.
5901 * \param name The name for the output.
5902 * \return A new \c weston_output, or NULL on failure.
5903 *
5904 * This creates a new weston_output that starts with no heads attached.
5905 *
5906 * An output must be configured and it must have at least one head before
5907 * it can be enabled.
5908 *
5909 * \memberof weston_compositor
5910 */
5911WL_EXPORT struct weston_output *
5912weston_compositor_create_output(struct weston_compositor *compositor,
5913 const char *name)
5914{
5915 assert(compositor->backend->create_output);
5916
5917 if (weston_compositor_find_output_by_name(compositor, name)) {
5918 weston_log("Warning: attempted to create an output with a "
5919 "duplicate name '%s'.\n", name);
5920 return NULL;
5921 }
5922
5923 return compositor->backend->create_output(compositor, name);
5924}
5925
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005926/** Create an output for an unused head
5927 *
5928 * \param compositor The compositor.
5929 * \param head The head to attach to the output.
5930 * \return A new \c weston_output, or NULL on failure.
5931 *
5932 * This creates a new weston_output that starts with the given head attached.
5933 * The output inherits the name of the head. The head must not be already
5934 * attached to another output.
5935 *
5936 * An output must be configured before it can be enabled.
5937 *
5938 * \memberof weston_compositor
5939 */
5940WL_EXPORT struct weston_output *
5941weston_compositor_create_output_with_head(struct weston_compositor *compositor,
5942 struct weston_head *head)
5943{
5944 struct weston_output *output;
5945
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02005946 output = weston_compositor_create_output(compositor, head->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005947 if (!output)
5948 return NULL;
5949
5950 if (weston_output_attach_head(output, head) < 0) {
Pekka Paalanen42c0e142017-10-27 12:07:49 +03005951 weston_output_destroy(output);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005952 return NULL;
5953 }
5954
5955 return output;
5956}
5957
5958/** Destroy an output
5959 *
5960 * \param output The output to destroy.
5961 *
5962 * The heads attached to the given output are detached and become unused again.
5963 *
5964 * It is not necessary to explicitly destroy all outputs at compositor exit.
5965 * weston_compositor_destroy() will automatically destroy any remaining
5966 * outputs.
5967 *
5968 * \memberof weston_output
5969 */
5970WL_EXPORT void
5971weston_output_destroy(struct weston_output *output)
5972{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005973 output->destroy(output);
5974}
5975
Pekka Paalanencf0a4762017-04-04 16:36:07 +03005976/** When you need a head...
5977 *
5978 * This function is a hack, used until all code has been converted to become
5979 * multi-head aware.
5980 *
5981 * \param output The weston_output whose head to get.
5982 * \return The first head in the output's list.
5983 */
5984WL_EXPORT struct weston_head *
5985weston_output_get_first_head(struct weston_output *output)
5986{
5987 if (wl_list_empty(&output->head_list))
5988 return NULL;
5989
5990 return container_of(output->head_list.next,
5991 struct weston_head, output_link);
5992}
5993
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01005994static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005995destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01005996{
Jonny Lamb74130762013-11-26 18:19:46 +01005997 struct weston_surface *surface =
5998 wl_resource_get_user_data(resource);
5999
Pekka Paalanen4826f872016-04-22 14:14:38 +03006000 if (!surface)
6001 return;
6002
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006003 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02006004 surface->pending.buffer_viewport.buffer.src_width =
6005 wl_fixed_from_int(-1);
6006 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006007 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006008}
6009
6010static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006011viewport_destroy(struct wl_client *client,
6012 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006013{
6014 wl_resource_destroy(resource);
6015}
6016
6017static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006018viewport_set_source(struct wl_client *client,
6019 struct wl_resource *resource,
6020 wl_fixed_t src_x,
6021 wl_fixed_t src_y,
6022 wl_fixed_t src_width,
6023 wl_fixed_t src_height)
6024{
6025 struct weston_surface *surface =
6026 wl_resource_get_user_data(resource);
6027
Pekka Paalanen4826f872016-04-22 14:14:38 +03006028 if (!surface) {
6029 wl_resource_post_error(resource,
6030 WP_VIEWPORT_ERROR_NO_SURFACE,
6031 "wl_surface for this viewport is no longer exists");
6032 return;
6033 }
6034
6035 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03006036 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006037
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006038 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03006039 src_height == wl_fixed_from_int(-1) &&
6040 src_x == wl_fixed_from_int(-1) &&
6041 src_y == wl_fixed_from_int(-1)) {
6042 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006043 surface->pending.buffer_viewport.buffer.src_width =
6044 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006045 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006046 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006047 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006048
Pekka Paalanen201769a2016-04-26 14:42:11 +03006049 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006050 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006051 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03006052 "wl_surface@%d viewport source "
6053 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
6054 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006055 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03006056 wl_fixed_to_double(src_height),
6057 wl_fixed_to_double(src_x),
6058 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006059 return;
6060 }
6061
6062 surface->pending.buffer_viewport.buffer.src_x = src_x;
6063 surface->pending.buffer_viewport.buffer.src_y = src_y;
6064 surface->pending.buffer_viewport.buffer.src_width = src_width;
6065 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006066 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006067}
6068
6069static void
6070viewport_set_destination(struct wl_client *client,
6071 struct wl_resource *resource,
6072 int32_t dst_width,
6073 int32_t dst_height)
6074{
6075 struct weston_surface *surface =
6076 wl_resource_get_user_data(resource);
6077
Pekka Paalanen4826f872016-04-22 14:14:38 +03006078 if (!surface) {
6079 wl_resource_post_error(resource,
6080 WP_VIEWPORT_ERROR_NO_SURFACE,
6081 "wl_surface for this viewport no longer exists");
6082 return;
6083 }
6084
6085 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006086
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006087 if (dst_width == -1 && dst_height == -1) {
6088 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006089 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006090 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006091 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006092 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006093
6094 if (dst_width <= 0 || dst_height <= 0) {
6095 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006096 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006097 "destination size must be positive (%dx%d)",
6098 dst_width, dst_height);
6099 return;
6100 }
6101
6102 surface->pending.buffer_viewport.surface.width = dst_width;
6103 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006104 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006105}
6106
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006107static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006108 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006109 viewport_set_source,
6110 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01006111};
6112
6113static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006114viewporter_destroy(struct wl_client *client,
6115 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006116{
6117 wl_resource_destroy(resource);
6118}
6119
6120static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006121viewporter_get_viewport(struct wl_client *client,
6122 struct wl_resource *viewporter,
6123 uint32_t id,
6124 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006125{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006126 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006127 struct weston_surface *surface =
6128 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006129 struct wl_resource *resource;
6130
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006131 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006132 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006133 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006134 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01006135 return;
6136 }
6137
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006138 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006139 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006140 if (resource == NULL) {
6141 wl_client_post_no_memory(client);
6142 return;
6143 }
6144
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006145 wl_resource_set_implementation(resource, &viewport_interface,
6146 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01006147
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006148 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006149}
6150
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006151static const struct wp_viewporter_interface viewporter_interface = {
6152 viewporter_destroy,
6153 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01006154};
6155
6156static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006157bind_viewporter(struct wl_client *client,
6158 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006159{
6160 struct wl_resource *resource;
6161
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006162 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006163 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006164 if (resource == NULL) {
6165 wl_client_post_no_memory(client);
6166 return;
6167 }
6168
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006169 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01006170 NULL, NULL);
6171}
6172
6173static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04006174destroy_presentation_feedback(struct wl_resource *feedback_resource)
6175{
6176 struct weston_presentation_feedback *feedback;
6177
6178 feedback = wl_resource_get_user_data(feedback_resource);
6179
6180 wl_list_remove(&feedback->link);
6181 free(feedback);
6182}
6183
6184static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006185presentation_destroy(struct wl_client *client, struct wl_resource *resource)
6186{
6187 wl_resource_destroy(resource);
6188}
6189
6190static void
6191presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04006192 struct wl_resource *presentation_resource,
6193 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006194 uint32_t callback)
6195{
Pekka Paalanen133e4392014-09-23 22:08:46 -04006196 struct weston_surface *surface;
6197 struct weston_presentation_feedback *feedback;
6198
6199 surface = wl_resource_get_user_data(surface_resource);
6200
Bryce Harringtonde16d892014-11-20 22:21:57 -08006201 feedback = zalloc(sizeof *feedback);
6202 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04006203 goto err_calloc;
6204
6205 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006206 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04006207 1, callback);
6208 if (!feedback->resource)
6209 goto err_create;
6210
6211 wl_resource_set_implementation(feedback->resource, NULL, feedback,
6212 destroy_presentation_feedback);
6213
6214 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
6215
6216 return;
6217
6218err_create:
6219 free(feedback);
6220
6221err_calloc:
6222 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006223}
6224
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006225static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006226 presentation_destroy,
6227 presentation_feedback
6228};
6229
6230static void
6231bind_presentation(struct wl_client *client,
6232 void *data, uint32_t version, uint32_t id)
6233{
6234 struct weston_compositor *compositor = data;
6235 struct wl_resource *resource;
6236
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006237 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006238 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006239 if (resource == NULL) {
6240 wl_client_post_no_memory(client);
6241 return;
6242 }
6243
6244 wl_resource_set_implementation(resource, &presentation_implementation,
6245 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006246 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006247}
6248
6249static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05006250compositor_bind(struct wl_client *client,
6251 void *data, uint32_t version, uint32_t id)
6252{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006253 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05006254 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05006255
Jason Ekstranda85118c2013-06-27 20:17:02 -05006256 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006257 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07006258 if (resource == NULL) {
6259 wl_client_post_no_memory(client);
6260 return;
6261 }
6262
6263 wl_resource_set_implementation(resource, &compositor_interface,
6264 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05006265}
6266
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006267WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006268weston_environment_get_fd(const char *env)
6269{
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07006270 char *e;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006271 int fd, flags;
6272
6273 e = getenv(env);
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07006274 if (!e || !safe_strtoint(e, &fd))
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006275 return -1;
6276
6277 flags = fcntl(fd, F_GETFD);
6278 if (flags == -1)
6279 return -1;
6280
6281 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
6282 unsetenv(env);
6283
6284 return fd;
6285}
6286
Pekka Paalanenb5026542014-11-12 15:09:24 +02006287static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02006288timeline_key_binding_handler(struct weston_keyboard *keyboard,
6289 const struct timespec *time, uint32_t key,
6290 void *data)
Pekka Paalanenb5026542014-11-12 15:09:24 +02006291{
6292 struct weston_compositor *compositor = data;
6293
6294 if (weston_timeline_enabled_)
6295 weston_timeline_close();
6296 else
6297 weston_timeline_open(compositor);
6298}
6299
Giulio Camuffo459137b2014-10-11 23:56:24 +03006300/** Create the compositor.
6301 *
6302 * This functions creates and initializes a compositor instance.
6303 *
6304 * \param display The Wayland display to be used.
6305 * \param user_data A pointer to an object that can later be retrieved
6306 * using the \ref weston_compositor_get_user_data function.
6307 * \return The compositor instance on success or NULL on failure.
6308 */
6309WL_EXPORT struct weston_compositor *
6310weston_compositor_create(struct wl_display *display, void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04006311{
Giulio Camuffo459137b2014-10-11 23:56:24 +03006312 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05006313 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07006314
Giulio Camuffo459137b2014-10-11 23:56:24 +03006315 ec = zalloc(sizeof *ec);
6316 if (!ec)
6317 return NULL;
6318
6319 ec->wl_display = display;
6320 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006321 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07006322 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006323 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006324 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006325 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006326 wl_signal_init(&ec->idle_signal);
6327 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006328 wl_signal_init(&ec->show_input_panel_signal);
6329 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02006330 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01006331 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01006332 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02006333 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006334 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02006335 wl_signal_init(&ec->output_resized_signal);
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03006336 wl_signal_init(&ec->heads_changed_signal);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02006337 wl_signal_init(&ec->output_heads_changed_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07006338 wl_signal_init(&ec->session_signal);
6339 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04006340
Casey Dahlin58ba1372012-04-19 22:50:08 -04006341 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03006342 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04006343
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02006344 ec->activate_serial = 1;
6345
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05006346 ec->touch_mode = WESTON_TOUCH_MODE_NORMAL;
6347
Derek Foreman152254b2015-11-26 14:17:48 -06006348 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006349 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006350 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05006351
Giulio Camuffo954f1832014-10-11 18:27:30 +03006352 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006353 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006354 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03006355
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006356 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006357 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006358 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006359
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006360 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006361 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006362 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006363
Pekka Paalanena5630ea2017-10-12 13:13:42 +02006364 if (weston_debug_compositor_create(ec) < 0)
6365 goto fail;
6366
Jonas Ådahl30d61d82014-10-22 21:21:17 +02006367 if (weston_input_init(ec) != 0)
6368 goto fail;
6369
Jason Ekstranda7af7042013-10-12 22:38:11 -05006370 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02006371 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006372 wl_list_init(&ec->layer_list);
6373 wl_list_init(&ec->seat_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006374 wl_list_init(&ec->pending_output_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006375 wl_list_init(&ec->output_list);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03006376 wl_list_init(&ec->head_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006377 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01006378 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006379 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01006380 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006381 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006382 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006383
Pekka Paalanen827b5d22016-06-29 11:54:26 +02006384 wl_list_init(&ec->plugin_api_list);
6385
Xiong Zhang97116532013-10-23 13:58:31 +08006386 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02006387 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04006388
Giulio Camuffo459137b2014-10-11 23:56:24 +03006389 wl_data_device_manager_init(ec->wl_display);
6390
6391 wl_display_init_shm(ec->wl_display);
6392
6393 loop = wl_display_get_event_loop(ec->wl_display);
6394 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Daniel Stone6847b852017-03-01 11:34:08 +00006395 ec->repaint_timer =
6396 wl_event_loop_add_timer(loop, output_repaint_timer_handler,
6397 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03006398
Quentin Glidic82681572016-12-17 13:40:51 +01006399 weston_layer_init(&ec->fade_layer, ec);
6400 weston_layer_init(&ec->cursor_layer, ec);
6401
6402 weston_layer_set_position(&ec->fade_layer, WESTON_LAYER_POSITION_FADE);
6403 weston_layer_set_position(&ec->cursor_layer,
6404 WESTON_LAYER_POSITION_CURSOR);
Giulio Camuffo459137b2014-10-11 23:56:24 +03006405
6406 weston_compositor_add_debug_binding(ec, KEY_T,
6407 timeline_key_binding_handler, ec);
6408
Giulio Camuffo459137b2014-10-11 23:56:24 +03006409 return ec;
6410
6411fail:
6412 free(ec);
6413 return NULL;
6414}
6415
Benjamin Franzkeb8263022011-08-30 11:32:47 +02006416WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006417weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07006418{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006419 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07006420
Pekka Paalanend1591ae2012-01-02 16:06:56 +02006421 wl_event_source_remove(ec->idle_source);
6422
Matt Roper361d2ad2011-08-29 13:52:23 -07006423 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02006424 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07006425 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02006426
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006427 /* Destroy all pending outputs associated with this compositor */
6428 wl_list_for_each_safe(output, next, &ec->pending_output_list, link)
6429 output->destroy(output);
6430
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02006431 if (ec->renderer)
6432 ec->renderer->destroy(ec);
6433
Daniel Stone325fc2d2012-05-30 16:31:58 +01006434 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09006435 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006436 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01006437 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006438 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006439 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02006440
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04006441 weston_plane_release(&ec->primary_plane);
Matt Roper361d2ad2011-08-29 13:52:23 -07006442}
6443
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05006444WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04006445weston_compositor_exit_with_code(struct weston_compositor *compositor,
6446 int exit_code)
6447{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02006448 if (compositor->exit_code == EXIT_SUCCESS)
6449 compositor->exit_code = exit_code;
6450
Giulio Camuffo459137b2014-10-11 23:56:24 +03006451 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04006452}
6453
6454WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01006455weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
6456 const struct weston_pointer_grab_interface *interface)
6457{
6458 struct weston_seat *seat;
6459
6460 ec->default_pointer_grab = interface;
6461 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05006462 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
6463
6464 if (pointer)
6465 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01006466 }
6467}
6468
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04006469WL_EXPORT int
6470weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
6471 clockid_t clk_id)
6472{
6473 struct timespec ts;
6474
6475 if (clock_gettime(clk_id, &ts) < 0)
6476 return -1;
6477
6478 compositor->presentation_clock = clk_id;
6479
6480 return 0;
6481}
6482
6483/*
6484 * For choosing the software clock, when the display hardware or API
6485 * does not expose a compatible presentation timestamp.
6486 */
6487WL_EXPORT int
6488weston_compositor_set_presentation_clock_software(
6489 struct weston_compositor *compositor)
6490{
6491 /* In order of preference */
6492 static const clockid_t clocks[] = {
6493 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
6494 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
6495 CLOCK_MONOTONIC, /* no jumps, may crawl */
6496 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
6497 CLOCK_REALTIME /* may jump and crawl */
6498 };
6499 unsigned i;
6500
6501 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
6502 if (weston_compositor_set_presentation_clock(compositor,
6503 clocks[i]) == 0)
6504 return 0;
6505
6506 weston_log("Error: no suitable presentation clock available.\n");
6507
6508 return -1;
6509}
6510
Pekka Paalanen662f3842015-03-18 12:17:26 +02006511/** Read the current time from the Presentation clock
6512 *
6513 * \param compositor
6514 * \param ts[out] The current time.
6515 *
6516 * \note Reading the current time in user space is always imprecise to some
6517 * degree.
6518 *
6519 * This function is never meant to fail. If reading the clock does fail,
6520 * an error message is logged and a zero time is returned. Callers are not
6521 * supposed to detect or react to failures.
6522 */
6523WL_EXPORT void
6524weston_compositor_read_presentation_clock(
6525 const struct weston_compositor *compositor,
6526 struct timespec *ts)
6527{
6528 static bool warned;
6529 int ret;
6530
6531 ret = clock_gettime(compositor->presentation_clock, ts);
6532 if (ret < 0) {
6533 ts->tv_sec = 0;
6534 ts->tv_nsec = 0;
6535
6536 if (!warned)
6537 weston_log("Error: failure to read "
6538 "the presentation clock %#x: '%m' (%d)\n",
6539 compositor->presentation_clock, errno);
6540 warned = true;
6541 }
6542}
6543
Pekka Paalanen230f3b12014-09-29 14:18:40 -04006544/** Import dmabuf buffer into current renderer
6545 *
6546 * \param compositor
6547 * \param buffer the dmabuf buffer to import
6548 * \return true on usable buffers, false otherwise
6549 *
6550 * This function tests that the linux_dmabuf_buffer is usable
6551 * for the current renderer. Returns false on unusable buffers. Usually
6552 * usability is tested by importing the dmabufs for composition.
6553 *
6554 * This hook is also used for detecting if the renderer supports
6555 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
6556 * supported.
6557 * */
6558WL_EXPORT bool
6559weston_compositor_import_dmabuf(struct weston_compositor *compositor,
6560 struct linux_dmabuf_buffer *buffer)
6561{
6562 struct weston_renderer *renderer;
6563
6564 renderer = compositor->renderer;
6565
6566 if (renderer->import_dmabuf == NULL)
6567 return false;
6568
6569 return renderer->import_dmabuf(compositor, buffer);
6570}
6571
Giulio Camuffocdb4d292013-11-14 23:42:53 +01006572WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05006573weston_version(int *major, int *minor, int *micro)
6574{
6575 *major = WESTON_VERSION_MAJOR;
6576 *minor = WESTON_VERSION_MINOR;
6577 *micro = WESTON_VERSION_MICRO;
6578}
6579
Daniel Stonee03c1112016-11-24 20:45:45 +00006580/**
6581 * Attempts to find a module path from the module map specified in the
6582 * environment. If found, writes the full path into the path variable.
6583 *
6584 * The module map is a string in environment variable WESTON_MODULE_MAP, where
6585 * each entry is of the form "name=path" and entries are separated by
6586 * semicolons. Whitespace is significant.
6587 *
6588 * \param name The name to search for.
6589 * \param path Where the path is written to if found.
6590 * \param path_len Allocated bytes at \c path .
6591 * \returns The length of the string written to path on success, or 0 if the
6592 * module was not specified in the environment map or path_len was too small.
6593 */
6594WL_EXPORT size_t
6595weston_module_path_from_env(const char *name, char *path, size_t path_len)
6596{
6597 const char *mapping = getenv("WESTON_MODULE_MAP");
6598 const char *end;
6599 const int name_len = strlen(name);
6600
6601 if (!mapping)
6602 return 0;
6603
6604 end = mapping + strlen(mapping);
6605 while (mapping < end && *mapping) {
6606 const char *filename, *next;
6607
6608 /* early out: impossibly short string */
6609 if (end - mapping < name_len + 1)
6610 return 0;
6611
6612 filename = &mapping[name_len + 1];
6613 next = strchrnul(mapping, ';');
6614
6615 if (strncmp(mapping, name, name_len) == 0 &&
6616 mapping[name_len] == '=') {
6617 size_t file_len = next - filename; /* no trailing NUL */
6618 if (file_len >= path_len)
6619 return 0;
6620 strncpy(path, filename, file_len);
6621 path[file_len] = '\0';
6622 return file_len;
6623 }
6624
6625 mapping = next + 1;
6626 }
6627
6628 return 0;
6629}
6630
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03006631WL_EXPORT void *
6632weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006633{
6634 char path[PATH_MAX];
6635 void *module, *init;
Daniel Stonebeb97e52016-11-28 12:13:54 +00006636 size_t len;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006637
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08006638 if (name == NULL)
6639 return NULL;
6640
Derek Foreman3f86e502015-06-08 11:46:54 -05006641 if (name[0] != '/') {
Daniel Stonee03c1112016-11-24 20:45:45 +00006642 len = weston_module_path_from_env(name, path, sizeof path);
6643 if (len == 0)
Daniel Stonebeb97e52016-11-28 12:13:54 +00006644 len = snprintf(path, sizeof path, "%s/%s",
6645 LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05006646 } else {
Daniel Stonebeb97e52016-11-28 12:13:54 +00006647 len = snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05006648 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006649
Daniel Stonebeb97e52016-11-28 12:13:54 +00006650 /* snprintf returns the length of the string it would've written,
6651 * _excluding_ the NUL byte. So even being equal to the size of
6652 * our buffer is an error here. */
6653 if (len >= sizeof path)
6654 return NULL;
6655
Kristian Høgsberga6813d22012-09-12 12:21:01 -04006656 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
6657 if (module) {
6658 weston_log("Module '%s' already loaded\n", path);
6659 dlclose(module);
6660 return NULL;
6661 }
6662
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03006663 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04006664 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006665 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03006666 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006667 return NULL;
6668 }
6669
6670 init = dlsym(module, entrypoint);
6671 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03006672 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00006673 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006674 return NULL;
6675 }
6676
6677 return init;
6678}
6679
Giulio Camuffo459137b2014-10-11 23:56:24 +03006680
6681/** Destroys the compositor.
6682 *
6683 * This function cleans up the compositor state and destroys it.
6684 *
6685 * \param compositor The compositor to be destroyed.
6686 */
6687WL_EXPORT void
6688weston_compositor_destroy(struct weston_compositor *compositor)
6689{
6690 /* prevent further rendering while shutting down */
6691 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
6692
6693 wl_signal_emit(&compositor->destroy_signal, compositor);
6694
6695 weston_compositor_xkb_destroy(compositor);
6696
Giulio Camuffo2d24e642015-10-03 16:25:15 +03006697 if (compositor->backend)
6698 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02006699
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03006700 /* The backend is responsible for destroying the heads. */
6701 assert(wl_list_empty(&compositor->head_list));
6702
Pekka Paalanen827b5d22016-06-29 11:54:26 +02006703 weston_plugin_api_destroy_list(compositor);
6704
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03006705 if (compositor->heads_changed_source)
6706 wl_event_source_remove(compositor->heads_changed_source);
6707
Pekka Paalanena5630ea2017-10-12 13:13:42 +02006708 weston_debug_compositor_destroy(compositor);
6709
Giulio Camuffo459137b2014-10-11 23:56:24 +03006710 free(compositor);
6711}
6712
6713/** Instruct the compositor to exit.
6714 *
6715 * This functions does not directly destroy the compositor object, it merely
6716 * command it to start the tear down process. It is not guaranteed that the
6717 * tear down will happen immediately.
6718 *
6719 * \param compositor The compositor to tear down.
6720 */
6721WL_EXPORT void
6722weston_compositor_exit(struct weston_compositor *compositor)
6723{
6724 compositor->exit(compositor);
6725}
6726
6727/** Return the user data stored in the compositor.
6728 *
6729 * This function returns the user data pointer set with user_data parameter
6730 * to the \ref weston_compositor_create function.
6731 */
6732WL_EXPORT void *
6733weston_compositor_get_user_data(struct weston_compositor *compositor)
6734{
6735 return compositor->user_data;
6736}
Pekka Paalanendd186732016-06-03 14:49:54 +03006737
Pekka Paalanen50dbf382016-06-03 15:23:46 +03006738static const char * const backend_map[] = {
6739 [WESTON_BACKEND_DRM] = "drm-backend.so",
6740 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
6741 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
6742 [WESTON_BACKEND_RDP] = "rdp-backend.so",
6743 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
6744 [WESTON_BACKEND_X11] = "x11-backend.so",
6745};
6746
Pekka Paalanendd186732016-06-03 14:49:54 +03006747/** Load a backend into a weston_compositor
6748 *
6749 * A backend must be loaded to make a weston_compositor work. A backend
6750 * provides input and output capabilities, and determines the renderer to use.
6751 *
6752 * \param compositor A compositor that has not had a backend loaded yet.
6753 * \param backend Name of the backend file.
6754 * \param config_base A pointer to a backend-specific configuration
6755 * structure's 'base' member.
6756 *
6757 * \return 0 on success, or -1 on error.
6758 */
6759WL_EXPORT int
6760weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03006761 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03006762 struct weston_backend_config *config_base)
6763{
6764 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03006765 struct weston_backend_config *config_base);
6766
Pekka Paalanend7e35112017-08-29 17:04:12 +03006767 if (compositor->backend) {
6768 weston_log("Error: attempt to load a backend when one is already loaded\n");
6769 return -1;
6770 }
6771
Quentin Glidic887c0182016-07-10 11:00:53 +02006772 if (backend >= ARRAY_LENGTH(backend_map))
Pekka Paalanen50dbf382016-06-03 15:23:46 +03006773 return -1;
6774
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01006775 backend_init = weston_load_module(backend_map[backend], "weston_backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03006776 if (!backend_init)
6777 return -1;
6778
Pekka Paalanend7e35112017-08-29 17:04:12 +03006779 if (backend_init(compositor, config_base) < 0) {
6780 compositor->backend = NULL;
6781 return -1;
6782 }
6783
6784 return 0;
Pekka Paalanendd186732016-06-03 14:49:54 +03006785}
Giulio Camuffo9c764df2016-06-29 11:54:27 +02006786
6787WL_EXPORT int
6788weston_compositor_load_xwayland(struct weston_compositor *compositor)
6789{
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01006790 int (*module_init)(struct weston_compositor *ec);
Giulio Camuffo9c764df2016-06-29 11:54:27 +02006791
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01006792 module_init = weston_load_module("xwayland.so", "weston_module_init");
Giulio Camuffo9c764df2016-06-29 11:54:27 +02006793 if (!module_init)
6794 return -1;
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01006795 if (module_init(compositor) < 0)
Giulio Camuffo9c764df2016-06-29 11:54:27 +02006796 return -1;
6797 return 0;
6798}