blob: d11a6552ca30fd5030d79ab32102200d06901e19 [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
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002482 return ret;
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002483
2484err:
Pekka Paalanen0513a952014-05-21 16:17:27 +03002485 weston_output_schedule_repaint_reset(output);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002486 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002487}
2488
2489static void
2490output_repaint_timer_arm(struct weston_compositor *compositor)
2491{
2492 struct weston_output *output;
2493 bool any_should_repaint = false;
2494 struct timespec now;
Sergi Granellb4c08862017-03-18 13:01:15 +01002495 int64_t msec_to_next = INT64_MAX;
Daniel Stone6847b852017-03-01 11:34:08 +00002496
2497 weston_compositor_read_presentation_clock(compositor, &now);
2498
2499 wl_list_for_each(output, &compositor->output_list, link) {
2500 int64_t msec_to_this;
2501
2502 if (output->repaint_status != REPAINT_SCHEDULED)
2503 continue;
2504
2505 msec_to_this = timespec_sub_to_msec(&output->next_repaint,
2506 &now);
2507 if (!any_should_repaint || msec_to_this < msec_to_next)
2508 msec_to_next = msec_to_this;
2509
2510 any_should_repaint = true;
2511 }
2512
2513 if (!any_should_repaint)
2514 return;
2515
2516 /* Even if we should repaint immediately, add the minimum 1 ms delay.
2517 * This is a workaround to allow coalescing multiple output repaints
2518 * particularly from weston_output_finish_frame()
2519 * into the same call, which would not happen if we called
2520 * output_repaint_timer_handler() directly.
2521 */
2522 if (msec_to_next < 1)
2523 msec_to_next = 1;
2524
2525 wl_event_source_timer_update(compositor->repaint_timer, msec_to_next);
2526}
2527
2528static int
2529output_repaint_timer_handler(void *data)
2530{
2531 struct weston_compositor *compositor = data;
2532 struct weston_output *output;
2533 struct timespec now;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002534 void *repaint_data = NULL;
Emre Ucane479ed82018-03-20 15:29:40 +01002535 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002536
2537 weston_compositor_read_presentation_clock(compositor, &now);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002538
2539 if (compositor->backend->repaint_begin)
2540 repaint_data = compositor->backend->repaint_begin(compositor);
2541
2542 wl_list_for_each(output, &compositor->output_list, link) {
2543 ret = weston_output_maybe_repaint(output, &now, repaint_data);
2544 if (ret)
2545 break;
2546 }
2547
2548 if (ret == 0) {
Tomohito Esaki09bfcd62018-06-05 10:37:05 +09002549 if (compositor->backend->repaint_flush)
2550 compositor->backend->repaint_flush(compositor,
2551 repaint_data);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002552 } else {
Tomohito Esaki09bfcd62018-06-05 10:37:05 +09002553 if (compositor->backend->repaint_cancel)
2554 compositor->backend->repaint_cancel(compositor,
2555 repaint_data);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002556 }
Daniel Stone6847b852017-03-01 11:34:08 +00002557
2558 output_repaint_timer_arm(compositor);
2559
Pekka Paalanen0513a952014-05-21 16:17:27 +03002560 return 0;
2561}
2562
Kristian Høgsbergef044142011-06-21 15:02:12 -04002563WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002564weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002565 const struct timespec *stamp,
2566 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002567{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002568 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002569 int32_t refresh_nsec;
2570 struct timespec now;
Daniel Stone6847b852017-03-01 11:34:08 +00002571 int64_t msec_rel;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002572
Pekka Paalanenb5026542014-11-12 15:09:24 +02002573
Daniel Stone05df8c12017-03-03 16:59:42 +00002574 assert(output->repaint_status == REPAINT_AWAITING_COMPLETION);
Daniel Stone3615ce12017-03-01 11:34:05 +00002575 assert(stamp || (presented_flags & WP_PRESENTATION_FEEDBACK_INVALID));
2576
Daniel Stone6847b852017-03-01 11:34:08 +00002577 weston_compositor_read_presentation_clock(compositor, &now);
2578
Daniel Stone3615ce12017-03-01 11:34:05 +00002579 /* If we haven't been supplied any timestamp at all, we don't have a
2580 * timebase to work against, so any delay just wastes time. Push a
2581 * repaint as soon as possible so we can get on with it. */
Daniel Stone6847b852017-03-01 11:34:08 +00002582 if (!stamp) {
2583 output->next_repaint = now;
Daniel Stone3615ce12017-03-01 11:34:05 +00002584 goto out;
Daniel Stone6847b852017-03-01 11:34:08 +00002585 }
Daniel Stone3615ce12017-03-01 11:34:05 +00002586
Marius Vladdf9278a2018-03-06 18:56:23 +02002587 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2588 TLP_VBLANK(stamp), TLP_END);
2589
Pekka Paalanend7894d02015-07-03 15:08:53 +03002590 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002591 weston_presentation_feedback_present_list(&output->feedback_list,
2592 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002593 output->msc,
2594 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002595
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002596 output->frame_time = *stamp;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002597
Daniel Stone6847b852017-03-01 11:34:08 +00002598 timespec_add_nsec(&output->next_repaint, stamp, refresh_nsec);
2599 timespec_add_msec(&output->next_repaint, &output->next_repaint,
2600 -compositor->repaint_msec);
2601 msec_rel = timespec_sub_to_msec(&output->next_repaint, &now);
Daniel Stone84aff5c2017-03-01 11:34:04 +00002602
2603 if (msec_rel < -1000 || msec_rel > 1000) {
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002604 static bool warned;
2605
2606 if (!warned)
2607 weston_log("Warning: computed repaint delay is "
Daniel Stone6847b852017-03-01 11:34:08 +00002608 "insane: %lld msec\n", (long long) msec_rel);
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002609 warned = true;
2610
Daniel Stone6847b852017-03-01 11:34:08 +00002611 output->next_repaint = now;
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002612 }
2613
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002614 /* Called from restart_repaint_loop and restart happens already after
2615 * the deadline given by repaint_msec? In that case we delay until
2616 * the deadline of the next frame, to give clients a more predictable
2617 * timing of the repaint cycle to lock on. */
Daniel Stoneeca5cca2017-02-28 21:53:51 +00002618 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID &&
2619 msec_rel < 0) {
2620 while (timespec_sub_to_nsec(&output->next_repaint, &now) < 0) {
2621 timespec_add_nsec(&output->next_repaint,
2622 &output->next_repaint,
2623 refresh_nsec);
2624 }
2625 }
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002626
Daniel Stone3615ce12017-03-01 11:34:05 +00002627out:
Daniel Stone05df8c12017-03-03 16:59:42 +00002628 output->repaint_status = REPAINT_SCHEDULED;
Daniel Stone6847b852017-03-01 11:34:08 +00002629 output_repaint_timer_arm(compositor);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002630}
2631
2632static void
2633idle_repaint(void *data)
2634{
2635 struct weston_output *output = data;
2636
Daniel Stone05df8c12017-03-03 16:59:42 +00002637 assert(output->repaint_status == REPAINT_BEGIN_FROM_IDLE);
2638 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03002639 output->idle_repaint_source = NULL;
Jonas Ådahle5a12252013-04-05 23:07:11 +02002640 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002641}
2642
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002643WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002644weston_layer_entry_insert(struct weston_layer_entry *list,
2645 struct weston_layer_entry *entry)
2646{
2647 wl_list_insert(&list->link, &entry->link);
2648 entry->layer = list->layer;
2649}
2650
2651WL_EXPORT void
2652weston_layer_entry_remove(struct weston_layer_entry *entry)
2653{
2654 wl_list_remove(&entry->link);
2655 wl_list_init(&entry->link);
2656 entry->layer = NULL;
2657}
2658
Quentin Glidic82681572016-12-17 13:40:51 +01002659
2660/** Initialize the weston_layer struct.
2661 *
2662 * \param compositor The compositor instance
2663 * \param layer The layer to initialize
2664 */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002665WL_EXPORT void
Quentin Glidic82681572016-12-17 13:40:51 +01002666weston_layer_init(struct weston_layer *layer,
2667 struct weston_compositor *compositor)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002668{
Quentin Glidic82681572016-12-17 13:40:51 +01002669 layer->compositor = compositor;
2670 wl_list_init(&layer->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002671 wl_list_init(&layer->view_list.link);
2672 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002673 weston_layer_set_mask_infinite(layer);
Quentin Glidic82681572016-12-17 13:40:51 +01002674}
2675
2676/** Sets the position of the layer in the layer list. The layer will be placed
2677 * below any layer with the same position value, if any.
2678 * This function is safe to call if the layer is already on the list, but the
2679 * layer may be moved below other layers at the same position, if any.
2680 *
2681 * \param layer The layer to modify
2682 * \param position The position the layer will be placed at
2683 */
2684WL_EXPORT void
2685weston_layer_set_position(struct weston_layer *layer,
2686 enum weston_layer_position position)
2687{
2688 struct weston_layer *below;
2689
2690 wl_list_remove(&layer->link);
2691
2692 /* layer_list is ordered from top to bottom, the last layer being the
2693 * background with the smallest position value */
2694
2695 layer->position = position;
2696 wl_list_for_each_reverse(below, &layer->compositor->layer_list, link) {
2697 if (below->position >= layer->position) {
2698 wl_list_insert(&below->link, &layer->link);
2699 return;
2700 }
2701 }
2702 wl_list_insert(&layer->compositor->layer_list, &layer->link);
2703}
2704
2705/** Hide a layer by taking it off the layer list.
2706 * This function is safe to call if the layer is not on the list.
2707 *
2708 * \param layer The layer to hide
2709 */
2710WL_EXPORT void
2711weston_layer_unset_position(struct weston_layer *layer)
2712{
2713 wl_list_remove(&layer->link);
2714 wl_list_init(&layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002715}
2716
2717WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002718weston_layer_set_mask(struct weston_layer *layer,
2719 int x, int y, int width, int height)
2720{
2721 struct weston_view *view;
2722
2723 layer->mask.x1 = x;
2724 layer->mask.x2 = x + width;
2725 layer->mask.y1 = y;
2726 layer->mask.y2 = y + height;
2727
2728 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2729 weston_view_geometry_dirty(view);
2730 }
2731}
2732
2733WL_EXPORT void
2734weston_layer_set_mask_infinite(struct weston_layer *layer)
2735{
2736 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2737 UINT32_MAX, UINT32_MAX);
2738}
2739
2740WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002741weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002742{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002743 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002744 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002745
Bryce Harrington08976ac2016-08-30 12:05:16 -07002746 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2747 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002748 return;
2749
Pekka Paalanenb5026542014-11-12 15:09:24 +02002750 if (!output->repaint_needed)
2751 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2752
Kristian Høgsbergef044142011-06-21 15:02:12 -04002753 loop = wl_display_get_event_loop(compositor->wl_display);
Daniel Stone09a97e22017-03-01 11:34:06 +00002754 output->repaint_needed = true;
Daniel Stone05df8c12017-03-03 16:59:42 +00002755
2756 /* If we already have a repaint scheduled for our idle handler,
2757 * no need to set it again. If the repaint has been called but
2758 * not finished, then weston_output_finish_frame() will notice
2759 * that a repaint is needed and schedule one. */
2760 if (output->repaint_status != REPAINT_NOT_SCHEDULED)
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002761 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002762
Daniel Stone05df8c12017-03-03 16:59:42 +00002763 output->repaint_status = REPAINT_BEGIN_FROM_IDLE;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03002764 assert(!output->idle_repaint_source);
2765 output->idle_repaint_source = wl_event_loop_add_idle(loop, idle_repaint,
2766 output);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002767 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002768}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002769
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002770WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002771weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2772{
2773 struct weston_output *output;
2774
2775 wl_list_for_each(output, &compositor->output_list, link)
2776 weston_output_schedule_repaint(output);
2777}
2778
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002779static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002780surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002781{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002782 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002783}
2784
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002785static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002786surface_attach(struct wl_client *client,
2787 struct wl_resource *resource,
2788 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2789{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002790 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002791 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002792
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002793 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002794 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002795 if (buffer == NULL) {
2796 wl_client_post_no_memory(client);
2797 return;
2798 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002799 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002800
Pekka Paalanende685b82012-12-04 15:58:12 +02002801 /* Attach, attach, without commit in between does not send
2802 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002803 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002804
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002805 surface->pending.sx = sx;
2806 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002807 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002808}
2809
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002810static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002811surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002812 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002813 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002814{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002815 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002816
Derek Foreman57e92ed2015-11-17 14:11:35 -06002817 if (width <= 0 || height <= 0)
2818 return;
2819
Derek Foreman152254b2015-11-26 14:17:48 -06002820 pixman_region32_union_rect(&surface->pending.damage_surface,
2821 &surface->pending.damage_surface,
2822 x, y, width, height);
2823}
2824
2825static void
2826surface_damage_buffer(struct wl_client *client,
2827 struct wl_resource *resource,
2828 int32_t x, int32_t y, int32_t width, int32_t height)
2829{
2830 struct weston_surface *surface = wl_resource_get_user_data(resource);
2831
2832 if (width <= 0 || height <= 0)
2833 return;
2834
2835 pixman_region32_union_rect(&surface->pending.damage_buffer,
2836 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002837 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002838}
2839
Kristian Høgsberg33418202011-08-16 23:01:28 -04002840static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002841destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002842{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002843 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002844
2845 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002846 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002847}
2848
2849static void
2850surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002851 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002852{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002853 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002854 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002855
2856 cb = malloc(sizeof *cb);
2857 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002858 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002859 return;
2860 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002861
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002862 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2863 callback);
2864 if (cb->resource == NULL) {
2865 free(cb);
2866 wl_resource_post_no_memory(resource);
2867 return;
2868 }
2869
Jason Ekstranda85118c2013-06-27 20:17:02 -05002870 wl_resource_set_implementation(cb->resource, NULL, cb,
2871 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002872
Pekka Paalanenbc106382012-10-10 12:49:31 +03002873 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002874}
2875
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002876static void
2877surface_set_opaque_region(struct wl_client *client,
2878 struct wl_resource *resource,
2879 struct wl_resource *region_resource)
2880{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002881 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002882 struct weston_region *region;
2883
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002884 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002885 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002886 pixman_region32_copy(&surface->pending.opaque,
2887 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002888 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002889 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002890 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002891}
2892
2893static void
2894surface_set_input_region(struct wl_client *client,
2895 struct wl_resource *resource,
2896 struct wl_resource *region_resource)
2897{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002898 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002899 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002900
2901 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002902 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002903 pixman_region32_copy(&surface->pending.input,
2904 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002905 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002906 pixman_region32_fini(&surface->pending.input);
2907 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002908 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002909}
2910
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002911/* Cause damage to this sub-surface and all its children.
2912 *
2913 * This is useful when there are state changes that need an implicit
2914 * damage, e.g. a z-order change.
2915 */
2916static void
2917weston_surface_damage_subsurfaces(struct weston_subsurface *sub)
2918{
2919 struct weston_subsurface *child;
2920
2921 weston_surface_damage(sub->surface);
2922 sub->reordered = false;
2923
2924 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link)
2925 if (child != sub)
2926 weston_surface_damage_subsurfaces(child);
2927}
2928
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002929static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002930weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002931{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002932 struct weston_subsurface *sub;
2933
2934 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2935 parent_link_pending) {
2936 wl_list_remove(&sub->parent_link);
2937 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002938
2939 if (sub->reordered)
2940 weston_surface_damage_subsurfaces(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002941 }
2942}
2943
2944static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03002945weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05002946 struct weston_matrix *matrix)
2947{
Pekka Paalanen04baea52016-04-26 15:50:58 +03002948 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05002949 double src_width, src_height, dest_width, dest_height;
2950
2951 weston_matrix_init(matrix);
2952
2953 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
2954 src_width = surface->width_from_buffer;
2955 src_height = surface->height_from_buffer;
2956 } else {
2957 src_width = wl_fixed_to_double(vp->buffer.src_width);
2958 src_height = wl_fixed_to_double(vp->buffer.src_height);
2959 }
2960
2961 if (vp->surface.width == -1) {
2962 dest_width = src_width;
2963 dest_height = src_height;
2964 } else {
2965 dest_width = vp->surface.width;
2966 dest_height = vp->surface.height;
2967 }
2968
2969 if (src_width != dest_width || src_height != dest_height)
2970 weston_matrix_scale(matrix,
2971 src_width / dest_width,
2972 src_height / dest_height, 1);
2973
2974 if (vp->buffer.src_width != wl_fixed_from_int(-1))
2975 weston_matrix_translate(matrix,
2976 wl_fixed_to_double(vp->buffer.src_x),
2977 wl_fixed_to_double(vp->buffer.src_y),
2978 0);
2979
2980 switch (vp->buffer.transform) {
2981 case WL_OUTPUT_TRANSFORM_FLIPPED:
2982 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2983 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2984 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2985 weston_matrix_scale(matrix, -1, 1, 1);
2986 weston_matrix_translate(matrix,
2987 surface->width_from_buffer, 0, 0);
2988 break;
2989 }
2990
2991 switch (vp->buffer.transform) {
2992 default:
2993 case WL_OUTPUT_TRANSFORM_NORMAL:
2994 case WL_OUTPUT_TRANSFORM_FLIPPED:
2995 break;
2996 case WL_OUTPUT_TRANSFORM_90:
2997 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2998 weston_matrix_rotate_xy(matrix, 0, 1);
2999 weston_matrix_translate(matrix,
3000 surface->height_from_buffer, 0, 0);
3001 break;
3002 case WL_OUTPUT_TRANSFORM_180:
3003 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3004 weston_matrix_rotate_xy(matrix, -1, 0);
3005 weston_matrix_translate(matrix,
3006 surface->width_from_buffer,
3007 surface->height_from_buffer, 0);
3008 break;
3009 case WL_OUTPUT_TRANSFORM_270:
3010 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3011 weston_matrix_rotate_xy(matrix, 0, -1);
3012 weston_matrix_translate(matrix,
3013 0, surface->width_from_buffer, 0);
3014 break;
3015 }
3016
3017 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
3018}
3019
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003020/**
3021 * Compute a + b > c while being safe to overflows.
3022 */
3023static bool
3024fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
3025{
3026 return (int64_t)a + (int64_t)b > (int64_t)c;
3027}
3028
3029static bool
3030weston_surface_is_pending_viewport_source_valid(
3031 const struct weston_surface *surface)
3032{
3033 const struct weston_surface_state *pend = &surface->pending;
3034 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
3035 int width_from_buffer = 0;
3036 int height_from_buffer = 0;
3037 wl_fixed_t w;
3038 wl_fixed_t h;
3039
3040 /* If viewport source rect is not set, it is always ok. */
3041 if (vp->buffer.src_width == wl_fixed_from_int(-1))
3042 return true;
3043
3044 if (pend->newly_attached) {
3045 if (pend->buffer) {
3046 convert_size_by_transform_scale(&width_from_buffer,
3047 &height_from_buffer,
3048 pend->buffer->width,
3049 pend->buffer->height,
3050 vp->buffer.transform,
3051 vp->buffer.scale);
3052 } else {
3053 /* No buffer: viewport is irrelevant. */
3054 return true;
3055 }
3056 } else {
3057 width_from_buffer = surface->width_from_buffer;
3058 height_from_buffer = surface->height_from_buffer;
3059 }
3060
3061 assert((width_from_buffer == 0) == (height_from_buffer == 0));
3062 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
3063
3064 /* No buffer: viewport is irrelevant. */
3065 if (width_from_buffer == 0 || height_from_buffer == 0)
3066 return true;
3067
3068 /* overflow checks for wl_fixed_from_int() */
3069 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
3070 return false;
3071 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
3072 return false;
3073
3074 w = wl_fixed_from_int(width_from_buffer);
3075 h = wl_fixed_from_int(height_from_buffer);
3076
3077 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
3078 return false;
3079 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
3080 return false;
3081
3082 return true;
3083}
3084
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003085static bool
3086fixed_is_integer(wl_fixed_t v)
3087{
3088 return (v & 0xff) == 0;
3089}
3090
3091static bool
3092weston_surface_is_pending_viewport_dst_size_int(
3093 const struct weston_surface *surface)
3094{
3095 const struct weston_buffer_viewport *vp =
3096 &surface->pending.buffer_viewport;
3097
3098 if (vp->surface.width != -1) {
3099 assert(vp->surface.width > 0 && vp->surface.height > 0);
3100 return true;
3101 }
3102
3103 return fixed_is_integer(vp->buffer.src_width) &&
3104 fixed_is_integer(vp->buffer.src_height);
3105}
3106
Derek Foreman152254b2015-11-26 14:17:48 -06003107/* Translate pending damage in buffer co-ordinates to surface
3108 * co-ordinates and union it with a pixman_region32_t.
3109 * This should only be called after the buffer is attached.
3110 */
3111static void
3112apply_damage_buffer(pixman_region32_t *dest,
3113 struct weston_surface *surface,
3114 struct weston_surface_state *state)
3115{
3116 struct weston_buffer *buffer = surface->buffer_ref.buffer;
3117
3118 /* wl_surface.damage_buffer needs to be clipped to the buffer,
3119 * translated into surface co-ordinates and unioned with
3120 * any other surface damage.
3121 * None of this makes sense if there is no buffer though.
3122 */
3123 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
3124 pixman_region32_t buffer_damage;
3125
3126 pixman_region32_intersect_rect(&state->damage_buffer,
3127 &state->damage_buffer,
3128 0, 0, buffer->width,
3129 buffer->height);
3130 pixman_region32_init(&buffer_damage);
3131 weston_matrix_transform_region(&buffer_damage,
3132 &surface->buffer_to_surface_matrix,
3133 &state->damage_buffer);
3134 pixman_region32_union(dest, dest, &buffer_damage);
3135 pixman_region32_fini(&buffer_damage);
3136 }
3137 /* We should clear this on commit even if there was no buffer */
3138 pixman_region32_clear(&state->damage_buffer);
3139}
3140
Jason Ekstrand1e059042014-10-16 10:55:19 -05003141static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05003142weston_surface_commit_state(struct weston_surface *surface,
3143 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003144{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003145 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003146 pixman_region32_t opaque;
3147
Alexander Larsson4ea95522013-05-22 14:41:37 +02003148 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02003149 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03003150 /* wp_viewport.set_source */
3151 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003152 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003153
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003154 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003155 if (state->newly_attached)
3156 weston_surface_attach(surface, state->buffer);
3157 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01003158
Jason Ekstrand1e059042014-10-16 10:55:19 -05003159 weston_surface_build_buffer_matrix(surface,
3160 &surface->surface_to_buffer_matrix);
3161 weston_matrix_invert(&surface->buffer_to_surface_matrix,
3162 &surface->surface_to_buffer_matrix);
3163
Jason Ekstrand7b982072014-05-20 14:33:03 -05003164 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003165 weston_surface_update_size(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003166 if (surface->committed)
3167 surface->committed(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003168 }
Giulio Camuffo184df502013-02-21 11:29:21 +01003169
Jason Ekstrand7b982072014-05-20 14:33:03 -05003170 state->sx = 0;
3171 state->sy = 0;
3172 state->newly_attached = 0;
3173 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03003174
Derek Foreman152254b2015-11-26 14:17:48 -06003175 /* wl_surface.damage and wl_surface.damage_buffer */
Pekka Paalanenb5026542014-11-12 15:09:24 +02003176 if (weston_timeline_enabled_ &&
Derek Foreman152254b2015-11-26 14:17:48 -06003177 (pixman_region32_not_empty(&state->damage_surface) ||
3178 pixman_region32_not_empty(&state->damage_buffer)))
Pekka Paalanenb5026542014-11-12 15:09:24 +02003179 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06003180
Pekka Paalanen8e159182012-10-10 12:49:25 +03003181 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06003182 &state->damage_surface);
3183
3184 apply_damage_buffer(&surface->damage, surface, state);
3185
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05003186 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07003187 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06003188 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003189
3190 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003191 pixman_region32_init(&opaque);
3192 pixman_region32_intersect_rect(&opaque, &state->opaque,
3193 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003194
3195 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
3196 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003197 wl_list_for_each(view, &surface->views, surface_link)
3198 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003199 }
3200
3201 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003202
3203 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003204 pixman_region32_intersect_rect(&surface->input, &state->input,
3205 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003206
Pekka Paalanenbc106382012-10-10 12:49:31 +03003207 /* wl_surface.frame */
3208 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05003209 &state->frame_callback_list);
3210 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003211
3212 /* XXX:
3213 * What should happen with a feedback request, if there
3214 * is no wl_buffer attached for this commit?
3215 */
3216
3217 /* presentation.feedback */
3218 wl_list_insert_list(&surface->feedback_list,
3219 &state->feedback_list);
3220 wl_list_init(&state->feedback_list);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003221
3222 wl_signal_emit(&surface->commit_signal, surface);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003223}
3224
3225static void
3226weston_surface_commit(struct weston_surface *surface)
3227{
3228 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003229
Pekka Paalanene67858b2013-04-25 13:57:42 +03003230 weston_surface_commit_subsurface_order(surface);
3231
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003232 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003233}
3234
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003235static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003236weston_subsurface_commit(struct weston_subsurface *sub);
3237
3238static void
3239weston_subsurface_parent_commit(struct weston_subsurface *sub,
3240 int parent_is_synchronized);
3241
3242static void
3243surface_commit(struct wl_client *client, struct wl_resource *resource)
3244{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003245 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003246 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
3247
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003248 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
3249 assert(surface->viewport_resource);
3250
3251 wl_resource_post_error(surface->viewport_resource,
3252 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
3253 "wl_surface@%d has viewport source outside buffer",
3254 wl_resource_get_id(resource));
3255 return;
3256 }
3257
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003258 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
3259 assert(surface->viewport_resource);
3260
3261 wl_resource_post_error(surface->viewport_resource,
3262 WP_VIEWPORT_ERROR_BAD_SIZE,
3263 "wl_surface@%d viewport dst size not integer",
3264 wl_resource_get_id(resource));
3265 return;
3266 }
3267
Pekka Paalanene67858b2013-04-25 13:57:42 +03003268 if (sub) {
3269 weston_subsurface_commit(sub);
3270 return;
3271 }
3272
3273 weston_surface_commit(surface);
3274
3275 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
3276 if (sub->surface != surface)
3277 weston_subsurface_parent_commit(sub, 0);
3278 }
3279}
3280
3281static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003282surface_set_buffer_transform(struct wl_client *client,
3283 struct wl_resource *resource, int transform)
3284{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003285 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003286
Jonny Lamba55f1392014-05-30 12:07:15 +02003287 /* if wl_output.transform grows more members this will need to be updated. */
3288 if (transform < 0 ||
3289 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
3290 wl_resource_post_error(resource,
3291 WL_SURFACE_ERROR_INVALID_TRANSFORM,
3292 "buffer transform must be a valid transform "
3293 "('%d' specified)", transform);
3294 return;
3295 }
3296
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003297 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003298 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003299}
3300
Alexander Larsson4ea95522013-05-22 14:41:37 +02003301static void
3302surface_set_buffer_scale(struct wl_client *client,
3303 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003304 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003305{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003306 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003307
Jonny Lamba55f1392014-05-30 12:07:15 +02003308 if (scale < 1) {
3309 wl_resource_post_error(resource,
3310 WL_SURFACE_ERROR_INVALID_SCALE,
3311 "buffer scale must be at least one "
3312 "('%d' specified)", scale);
3313 return;
3314 }
3315
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003316 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003317 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003318}
3319
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003320static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003321 surface_destroy,
3322 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04003323 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003324 surface_frame,
3325 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003326 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003327 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02003328 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06003329 surface_set_buffer_scale,
3330 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003331};
3332
3333static void
3334compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003335 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003336{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04003337 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003338 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003339
Kristian Høgsberg18c93002012-01-27 11:58:31 -05003340 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003341 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003342 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003343 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003344 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003345
Jason Ekstranda85118c2013-06-27 20:17:02 -05003346 surface->resource =
3347 wl_resource_create(client, &wl_surface_interface,
3348 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003349 if (surface->resource == NULL) {
3350 weston_surface_destroy(surface);
3351 wl_resource_post_no_memory(resource);
3352 return;
3353 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003354 wl_resource_set_implementation(surface->resource, &surface_interface,
3355 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003356
3357 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003358}
3359
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003360static void
3361destroy_region(struct wl_resource *resource)
3362{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003363 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003364
3365 pixman_region32_fini(&region->region);
3366 free(region);
3367}
3368
3369static void
3370region_destroy(struct wl_client *client, struct wl_resource *resource)
3371{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003372 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003373}
3374
3375static void
3376region_add(struct wl_client *client, struct wl_resource *resource,
3377 int32_t x, int32_t y, int32_t width, int32_t height)
3378{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003379 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003380
3381 pixman_region32_union_rect(&region->region, &region->region,
3382 x, y, width, height);
3383}
3384
3385static void
3386region_subtract(struct wl_client *client, struct wl_resource *resource,
3387 int32_t x, int32_t y, int32_t width, int32_t height)
3388{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003389 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003390 pixman_region32_t rect;
3391
3392 pixman_region32_init_rect(&rect, x, y, width, height);
3393 pixman_region32_subtract(&region->region, &region->region, &rect);
3394 pixman_region32_fini(&rect);
3395}
3396
3397static const struct wl_region_interface region_interface = {
3398 region_destroy,
3399 region_add,
3400 region_subtract
3401};
3402
3403static void
3404compositor_create_region(struct wl_client *client,
3405 struct wl_resource *resource, uint32_t id)
3406{
3407 struct weston_region *region;
3408
3409 region = malloc(sizeof *region);
3410 if (region == NULL) {
3411 wl_resource_post_no_memory(resource);
3412 return;
3413 }
3414
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003415 pixman_region32_init(&region->region);
3416
Jason Ekstranda85118c2013-06-27 20:17:02 -05003417 region->resource =
3418 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003419 if (region->resource == NULL) {
3420 free(region);
3421 wl_resource_post_no_memory(resource);
3422 return;
3423 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003424 wl_resource_set_implementation(region->resource, &region_interface,
3425 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003426}
3427
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003428static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003429 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003430 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003431};
3432
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003433static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003434weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3435{
3436 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003437
Jason Ekstrand7b982072014-05-20 14:33:03 -05003438 weston_surface_commit_state(surface, &sub->cached);
3439 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003440
3441 weston_surface_commit_subsurface_order(surface);
3442
3443 weston_surface_schedule_repaint(surface);
3444
Jason Ekstrand7b982072014-05-20 14:33:03 -05003445 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003446}
3447
3448static void
3449weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3450{
3451 struct weston_surface *surface = sub->surface;
3452
3453 /*
3454 * If this commit would cause the surface to move by the
3455 * attach(dx, dy) parameters, the old damage region must be
3456 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05003457 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003458 */
Derek Foreman152254b2015-11-26 14:17:48 -06003459 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003460 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06003461 pixman_region32_union(&sub->cached.damage_surface,
3462 &sub->cached.damage_surface,
3463 &surface->pending.damage_surface);
3464 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003465
3466 if (surface->pending.newly_attached) {
3467 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003468 weston_surface_state_set_buffer(&sub->cached,
3469 surface->pending.buffer);
3470 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003471 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003472 weston_presentation_feedback_discard_list(
3473 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003474 }
3475 sub->cached.sx += surface->pending.sx;
3476 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003477
Derek Foreman152254b2015-11-26 14:17:48 -06003478 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
3479
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003480 sub->cached.buffer_viewport.changed |=
3481 surface->pending.buffer_viewport.changed;
3482 sub->cached.buffer_viewport.buffer =
3483 surface->pending.buffer_viewport.buffer;
3484 sub->cached.buffer_viewport.surface =
3485 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003486
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003487 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003488
3489 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3490
3491 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3492
3493 wl_list_insert_list(&sub->cached.frame_callback_list,
3494 &surface->pending.frame_callback_list);
3495 wl_list_init(&surface->pending.frame_callback_list);
3496
Pekka Paalanen133e4392014-09-23 22:08:46 -04003497 wl_list_insert_list(&sub->cached.feedback_list,
3498 &surface->pending.feedback_list);
3499 wl_list_init(&surface->pending.feedback_list);
3500
Jason Ekstrand7b982072014-05-20 14:33:03 -05003501 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003502}
3503
Derek Foreman280e7dd2014-10-03 13:13:42 -05003504static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003505weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3506{
3507 while (sub) {
3508 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003509 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003510
3511 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003512 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003513
3514 sub = weston_surface_to_subsurface(sub->parent);
3515 }
3516
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003517 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003518}
3519
3520static void
3521weston_subsurface_commit(struct weston_subsurface *sub)
3522{
3523 struct weston_surface *surface = sub->surface;
3524 struct weston_subsurface *tmp;
3525
3526 /* Recursive check for effectively synchronized. */
3527 if (weston_subsurface_is_synchronized(sub)) {
3528 weston_subsurface_commit_to_cache(sub);
3529 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003530 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003531 /* flush accumulated state from cache */
3532 weston_subsurface_commit_to_cache(sub);
3533 weston_subsurface_commit_from_cache(sub);
3534 } else {
3535 weston_surface_commit(surface);
3536 }
3537
3538 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3539 if (tmp->surface != surface)
3540 weston_subsurface_parent_commit(tmp, 0);
3541 }
3542 }
3543}
3544
3545static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003546weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003547{
3548 struct weston_surface *surface = sub->surface;
3549 struct weston_subsurface *tmp;
3550
Pekka Paalanene67858b2013-04-25 13:57:42 +03003551 /* From now on, commit_from_cache the whole sub-tree, regardless of
3552 * the synchronized mode of each child. This sub-surface or some
3553 * of its ancestors were synchronized, so we are synchronized
3554 * all the way down.
3555 */
3556
Jason Ekstrand7b982072014-05-20 14:33:03 -05003557 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003558 weston_subsurface_commit_from_cache(sub);
3559
3560 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3561 if (tmp->surface != surface)
3562 weston_subsurface_parent_commit(tmp, 1);
3563 }
3564}
3565
3566static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003567weston_subsurface_parent_commit(struct weston_subsurface *sub,
3568 int parent_is_synchronized)
3569{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003570 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003571 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003572 wl_list_for_each(view, &sub->surface->views, surface_link)
3573 weston_view_set_position(view,
3574 sub->position.x,
3575 sub->position.y);
3576
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003577 sub->position.set = 0;
3578 }
3579
3580 if (parent_is_synchronized || sub->synchronized)
3581 weston_subsurface_synchronized_commit(sub);
3582}
3583
Pekka Paalanen8274d902014-08-06 19:36:51 +03003584static int
3585subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3586{
3587 return snprintf(buf, len, "sub-surface");
3588}
3589
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003590static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003591subsurface_committed(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003592{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003593 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003594
Jason Ekstranda7af7042013-10-12 22:38:11 -05003595 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003596 weston_view_set_position(view,
3597 view->geometry.x + dx,
3598 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003599
3600 /* No need to check parent mappedness, because if parent is not
3601 * mapped, parent is not in a visible layer, so this sub-surface
3602 * will not be drawn either.
3603 */
Armin Krezovićf8486c32016-06-30 06:04:28 +02003604
Pekka Paalanene67858b2013-04-25 13:57:42 +03003605 if (!weston_surface_is_mapped(surface)) {
Armin Krezovićf8486c32016-06-30 06:04:28 +02003606 surface->is_mapped = true;
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003607
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003608 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003609 * because that would call it also for the parent surface,
3610 * which might not be mapped yet. That would lead to
3611 * inconsistent state, where the window could never be
3612 * mapped.
3613 *
Armin Krezovićf8486c32016-06-30 06:04:28 +02003614 * Instead just force the is_mapped flag on, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003615 * weston_surface_is_mapped() return true, so that when the
3616 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003617 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003618 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03003619 }
3620}
3621
3622static struct weston_subsurface *
3623weston_surface_to_subsurface(struct weston_surface *surface)
3624{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003625 if (surface->committed == subsurface_committed)
3626 return surface->committed_private;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003627
3628 return NULL;
3629}
3630
Pekka Paalanen01388e22013-04-25 13:57:44 +03003631WL_EXPORT struct weston_surface *
3632weston_surface_get_main_surface(struct weston_surface *surface)
3633{
3634 struct weston_subsurface *sub;
3635
3636 while (surface && (sub = weston_surface_to_subsurface(surface)))
3637 surface = sub->parent;
3638
3639 return surface;
3640}
3641
Pekka Paalanen50b67472014-10-01 15:02:41 +03003642WL_EXPORT int
3643weston_surface_set_role(struct weston_surface *surface,
3644 const char *role_name,
3645 struct wl_resource *error_resource,
3646 uint32_t error_code)
3647{
3648 assert(role_name);
3649
3650 if (surface->role_name == NULL ||
3651 surface->role_name == role_name ||
3652 strcmp(surface->role_name, role_name) == 0) {
3653 surface->role_name = role_name;
3654
3655 return 0;
3656 }
3657
3658 wl_resource_post_error(error_resource, error_code,
3659 "Cannot assign role %s to wl_surface@%d,"
3660 " already has role %s\n",
3661 role_name,
3662 wl_resource_get_id(surface->resource),
3663 surface->role_name);
3664 return -1;
3665}
3666
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02003667WL_EXPORT const char *
3668weston_surface_get_role(struct weston_surface *surface)
3669{
3670 return surface->role_name;
3671}
3672
Pekka Paalanen8274d902014-08-06 19:36:51 +03003673WL_EXPORT void
3674weston_surface_set_label_func(struct weston_surface *surface,
3675 int (*desc)(struct weston_surface *,
3676 char *, size_t))
3677{
3678 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02003679 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003680}
3681
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003682/** Get the size of surface contents
3683 *
3684 * \param surface The surface to query.
3685 * \param width Returns the width of raw contents.
3686 * \param height Returns the height of raw contents.
3687 *
3688 * Retrieves the raw surface content size in pixels for the given surface.
3689 * This is the whole content size in buffer pixels. If the surface
3690 * has no content or the renderer does not implement this feature,
3691 * zeroes are returned.
3692 *
3693 * This function is used to determine the buffer size needed for
3694 * a weston_surface_copy_content() call.
3695 */
3696WL_EXPORT void
3697weston_surface_get_content_size(struct weston_surface *surface,
3698 int *width, int *height)
3699{
3700 struct weston_renderer *rer = surface->compositor->renderer;
3701
3702 if (!rer->surface_get_content_size) {
3703 *width = 0;
3704 *height = 0;
3705 return;
3706 }
3707
3708 rer->surface_get_content_size(surface, width, height);
3709}
3710
Quentin Glidic248dd102016-08-12 10:41:34 +02003711/** Get the bounding box of a surface and its subsurfaces
3712 *
3713 * \param surface The surface to query.
3714 * \return The bounding box relative to the surface origin.
3715 *
3716 */
3717WL_EXPORT struct weston_geometry
3718weston_surface_get_bounding_box(struct weston_surface *surface)
3719{
3720 pixman_region32_t region;
3721 pixman_box32_t *box;
3722 struct weston_subsurface *subsurface;
3723
3724 pixman_region32_init_rect(&region,
3725 0, 0,
3726 surface->width, surface->height);
3727
3728 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link)
3729 pixman_region32_union_rect(&region, &region,
3730 subsurface->position.x,
3731 subsurface->position.y,
3732 subsurface->surface->width,
3733 subsurface->surface->height);
3734
3735 box = pixman_region32_extents(&region);
3736 struct weston_geometry geometry = {
3737 .x = box->x1,
3738 .y = box->y1,
3739 .width = box->x2 - box->x1,
3740 .height = box->y2 - box->y1,
3741 };
3742
3743 pixman_region32_fini(&region);
3744
3745 return geometry;
3746}
3747
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003748/** Copy surface contents to system memory.
3749 *
3750 * \param surface The surface to copy from.
3751 * \param target Pointer to the target memory buffer.
3752 * \param size Size of the target buffer in bytes.
3753 * \param src_x X location on contents to copy from.
3754 * \param src_y Y location on contents to copy from.
3755 * \param width Width in pixels of the area to copy.
3756 * \param height Height in pixels of the area to copy.
3757 * \return 0 for success, -1 for failure.
3758 *
3759 * Surface contents are maintained by the renderer. They can be in a
3760 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
3761 * else.
3762 *
3763 * Surface contents are copied into memory pointed to by target,
3764 * which has size bytes of space available. The target memory
3765 * may be larger than needed, but being smaller returns an error.
3766 * The extra bytes in target may or may not be written; their content is
3767 * unspecified. Size must be large enough to hold the image.
3768 *
3769 * The image in the target memory will be arranged in rows from
3770 * top to bottom, and pixels on a row from left to right. The pixel
3771 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
3772 * width * 4.
3773 *
3774 * Parameters src_x and src_y define the upper-left corner in buffer
3775 * coordinates (pixels) to copy from. Parameters width and height
3776 * define the size of the area to copy in pixels.
3777 *
3778 * The rectangle defined by src_x, src_y, width, height must fit in
3779 * the surface contents. Otherwise an error is returned.
3780 *
3781 * Use surface_get_data_size to determine the content size; the
3782 * needed target buffer size and rectangle limits.
3783 *
3784 * CURRENT IMPLEMENTATION RESTRICTIONS:
3785 * - the machine must be little-endian due to Pixman formats.
3786 *
3787 * NOTE: Pixman formats are premultiplied.
3788 */
3789WL_EXPORT int
3790weston_surface_copy_content(struct weston_surface *surface,
3791 void *target, size_t size,
3792 int src_x, int src_y,
3793 int width, int height)
3794{
3795 struct weston_renderer *rer = surface->compositor->renderer;
3796 int cw, ch;
3797 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
3798
3799 if (!rer->surface_copy_content)
3800 return -1;
3801
3802 weston_surface_get_content_size(surface, &cw, &ch);
3803
3804 if (src_x < 0 || src_y < 0)
3805 return -1;
3806
3807 if (width <= 0 || height <= 0)
3808 return -1;
3809
3810 if (src_x + width > cw || src_y + height > ch)
3811 return -1;
3812
3813 if (width * bytespp * height > size)
3814 return -1;
3815
3816 return rer->surface_copy_content(surface, target, size,
3817 src_x, src_y, width, height);
3818}
3819
Pekka Paalanene67858b2013-04-25 13:57:42 +03003820static void
3821subsurface_set_position(struct wl_client *client,
3822 struct wl_resource *resource, int32_t x, int32_t y)
3823{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003824 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003825
3826 if (!sub)
3827 return;
3828
3829 sub->position.x = x;
3830 sub->position.y = y;
3831 sub->position.set = 1;
3832}
3833
3834static struct weston_subsurface *
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003835subsurface_find_sibling(struct weston_subsurface *sub,
3836 struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003837{
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003838 struct weston_surface *parent = sub->parent;
3839 struct weston_subsurface *sibling;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003840
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003841 wl_list_for_each(sibling, &parent->subsurface_list, parent_link) {
3842 if (sibling->surface == surface && sibling != sub)
3843 return sibling;
3844 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003845
3846 return NULL;
3847}
3848
3849static struct weston_subsurface *
3850subsurface_sibling_check(struct weston_subsurface *sub,
3851 struct weston_surface *surface,
3852 const char *request)
3853{
3854 struct weston_subsurface *sibling;
3855
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003856 sibling = subsurface_find_sibling(sub, surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003857 if (!sibling) {
3858 wl_resource_post_error(sub->resource,
3859 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3860 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003861 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003862 return NULL;
3863 }
3864
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003865 assert(sibling->parent == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003866
3867 return sibling;
3868}
3869
3870static void
3871subsurface_place_above(struct wl_client *client,
3872 struct wl_resource *resource,
3873 struct wl_resource *sibling_resource)
3874{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003875 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003876 struct weston_surface *surface =
3877 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003878 struct weston_subsurface *sibling;
3879
3880 if (!sub)
3881 return;
3882
3883 sibling = subsurface_sibling_check(sub, surface, "place_above");
3884 if (!sibling)
3885 return;
3886
3887 wl_list_remove(&sub->parent_link_pending);
3888 wl_list_insert(sibling->parent_link_pending.prev,
3889 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003890
3891 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003892}
3893
3894static void
3895subsurface_place_below(struct wl_client *client,
3896 struct wl_resource *resource,
3897 struct wl_resource *sibling_resource)
3898{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003899 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003900 struct weston_surface *surface =
3901 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003902 struct weston_subsurface *sibling;
3903
3904 if (!sub)
3905 return;
3906
3907 sibling = subsurface_sibling_check(sub, surface, "place_below");
3908 if (!sibling)
3909 return;
3910
3911 wl_list_remove(&sub->parent_link_pending);
3912 wl_list_insert(&sibling->parent_link_pending,
3913 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003914
3915 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003916}
3917
3918static void
3919subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
3920{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003921 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003922
3923 if (sub)
3924 sub->synchronized = 1;
3925}
3926
3927static void
3928subsurface_set_desync(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
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003932 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003933 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003934
3935 /* If sub became effectively desynchronized, flush. */
3936 if (!weston_subsurface_is_synchronized(sub))
3937 weston_subsurface_synchronized_commit(sub);
3938 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003939}
3940
3941static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003942weston_subsurface_unlink_parent(struct weston_subsurface *sub)
3943{
3944 wl_list_remove(&sub->parent_link);
3945 wl_list_remove(&sub->parent_link_pending);
3946 wl_list_remove(&sub->parent_destroy_listener.link);
3947 sub->parent = NULL;
3948}
3949
3950static void
3951weston_subsurface_destroy(struct weston_subsurface *sub);
3952
3953static void
3954subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
3955{
3956 struct weston_subsurface *sub =
3957 container_of(listener, struct weston_subsurface,
3958 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003959 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003960
3961 /* The protocol object (wl_resource) is left inert. */
3962 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003963 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003964
3965 weston_subsurface_destroy(sub);
3966}
3967
3968static void
3969subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3970{
3971 struct weston_subsurface *sub =
3972 container_of(listener, struct weston_subsurface,
3973 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003974 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003975 assert(sub->surface != sub->parent);
3976
3977 if (weston_surface_is_mapped(sub->surface))
3978 weston_surface_unmap(sub->surface);
3979
3980 weston_subsurface_unlink_parent(sub);
3981}
3982
3983static void
3984subsurface_resource_destroy(struct wl_resource *resource)
3985{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003986 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003987
3988 if (sub)
3989 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003990}
3991
3992static void
3993subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
3994{
3995 wl_resource_destroy(resource);
3996}
3997
3998static void
3999weston_subsurface_link_parent(struct weston_subsurface *sub,
4000 struct weston_surface *parent)
4001{
4002 sub->parent = parent;
4003 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004004 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004005 &sub->parent_destroy_listener);
4006
4007 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4008 wl_list_insert(&parent->subsurface_list_pending,
4009 &sub->parent_link_pending);
4010}
4011
4012static void
4013weston_subsurface_link_surface(struct weston_subsurface *sub,
4014 struct weston_surface *surface)
4015{
4016 sub->surface = surface;
4017 sub->surface_destroy_listener.notify =
4018 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004019 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004020 &sub->surface_destroy_listener);
4021}
4022
4023static void
4024weston_subsurface_destroy(struct weston_subsurface *sub)
4025{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004026 struct weston_view *view, *next;
4027
Pekka Paalanene67858b2013-04-25 13:57:42 +03004028 assert(sub->surface);
4029
4030 if (sub->resource) {
4031 assert(weston_surface_to_subsurface(sub->surface) == sub);
4032 assert(sub->parent_destroy_listener.notify ==
4033 subsurface_handle_parent_destroy);
4034
George Kiagiadakised04d382014-06-13 18:10:26 +02004035 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
4036 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004037 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02004038 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05004039
Pekka Paalanene67858b2013-04-25 13:57:42 +03004040 if (sub->parent)
4041 weston_subsurface_unlink_parent(sub);
4042
Jason Ekstrand7b982072014-05-20 14:33:03 -05004043 weston_surface_state_fini(&sub->cached);
4044 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004045
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004046 sub->surface->committed = NULL;
4047 sub->surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004048 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004049 } else {
4050 /* the dummy weston_subsurface for the parent itself */
4051 assert(sub->parent_destroy_listener.notify == NULL);
4052 wl_list_remove(&sub->parent_link);
4053 wl_list_remove(&sub->parent_link_pending);
4054 }
4055
4056 wl_list_remove(&sub->surface_destroy_listener.link);
4057 free(sub);
4058}
4059
4060static const struct wl_subsurface_interface subsurface_implementation = {
4061 subsurface_destroy,
4062 subsurface_set_position,
4063 subsurface_place_above,
4064 subsurface_place_below,
4065 subsurface_set_sync,
4066 subsurface_set_desync
4067};
4068
4069static struct weston_subsurface *
4070weston_subsurface_create(uint32_t id, struct weston_surface *surface,
4071 struct weston_surface *parent)
4072{
4073 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004074 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004075
Bryce Harringtonde16d892014-11-20 22:21:57 -08004076 sub = zalloc(sizeof *sub);
4077 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004078 return NULL;
4079
Jason Ekstranda7af7042013-10-12 22:38:11 -05004080 wl_list_init(&sub->unused_views);
4081
Jason Ekstranda85118c2013-06-27 20:17:02 -05004082 sub->resource =
4083 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004084 if (!sub->resource) {
4085 free(sub);
4086 return NULL;
4087 }
4088
Jason Ekstranda85118c2013-06-27 20:17:02 -05004089 wl_resource_set_implementation(sub->resource,
4090 &subsurface_implementation,
4091 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004092 weston_subsurface_link_surface(sub, surface);
4093 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05004094 weston_surface_state_init(&sub->cached);
4095 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004096 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004097
4098 return sub;
4099}
4100
4101/* Create a dummy subsurface for having the parent itself in its
4102 * sub-surface lists. Makes stacking order manipulation easy.
4103 */
4104static struct weston_subsurface *
4105weston_subsurface_create_for_parent(struct weston_surface *parent)
4106{
4107 struct weston_subsurface *sub;
4108
Bryce Harringtonde16d892014-11-20 22:21:57 -08004109 sub = zalloc(sizeof *sub);
4110 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004111 return NULL;
4112
4113 weston_subsurface_link_surface(sub, parent);
4114 sub->parent = parent;
4115 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4116 wl_list_insert(&parent->subsurface_list_pending,
4117 &sub->parent_link_pending);
4118
4119 return sub;
4120}
4121
4122static void
4123subcompositor_get_subsurface(struct wl_client *client,
4124 struct wl_resource *resource,
4125 uint32_t id,
4126 struct wl_resource *surface_resource,
4127 struct wl_resource *parent_resource)
4128{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004129 struct weston_surface *surface =
4130 wl_resource_get_user_data(surface_resource);
4131 struct weston_surface *parent =
4132 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004133 struct weston_subsurface *sub;
4134 static const char where[] = "get_subsurface: wl_subsurface@";
4135
4136 if (surface == parent) {
4137 wl_resource_post_error(resource,
4138 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4139 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004140 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004141 return;
4142 }
4143
4144 if (weston_surface_to_subsurface(surface)) {
4145 wl_resource_post_error(resource,
4146 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4147 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004148 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004149 return;
4150 }
4151
Pekka Paalanen50b67472014-10-01 15:02:41 +03004152 if (weston_surface_set_role(surface, "wl_subsurface", resource,
4153 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004154 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004155
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004156 if (weston_surface_get_main_surface(parent) == surface) {
4157 wl_resource_post_error(resource,
4158 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4159 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004160 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004161 return;
4162 }
4163
Pekka Paalanene67858b2013-04-25 13:57:42 +03004164 /* make sure the parent is in its own list */
4165 if (wl_list_empty(&parent->subsurface_list)) {
4166 if (!weston_subsurface_create_for_parent(parent)) {
4167 wl_resource_post_no_memory(resource);
4168 return;
4169 }
4170 }
4171
4172 sub = weston_subsurface_create(id, surface, parent);
4173 if (!sub) {
4174 wl_resource_post_no_memory(resource);
4175 return;
4176 }
4177
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004178 surface->committed = subsurface_committed;
4179 surface->committed_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004180 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004181}
4182
4183static void
4184subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
4185{
4186 wl_resource_destroy(resource);
4187}
4188
4189static const struct wl_subcompositor_interface subcompositor_interface = {
4190 subcompositor_destroy,
4191 subcompositor_get_subsurface
4192};
4193
4194static void
4195bind_subcompositor(struct wl_client *client,
4196 void *data, uint32_t version, uint32_t id)
4197{
4198 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004199 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004200
Jason Ekstranda85118c2013-06-27 20:17:02 -05004201 resource =
4202 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004203 if (resource == NULL) {
4204 wl_client_post_no_memory(client);
4205 return;
4206 }
4207 wl_resource_set_implementation(resource, &subcompositor_interface,
4208 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004209}
4210
Bryce Harrington0795ece2016-08-30 12:04:26 -07004211/** Set a DPMS mode on all of the compositor's outputs
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004212 *
4213 * \param compositor The compositor instance
4214 * \param state The DPMS state the outputs will be set to
4215 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004216static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004217weston_compositor_dpms(struct weston_compositor *compositor,
4218 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004219{
4220 struct weston_output *output;
4221
Bryce Harrington08976ac2016-08-30 12:05:16 -07004222 wl_list_for_each(output, &compositor->output_list, link)
4223 if (output->set_dpms)
4224 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004225}
4226
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004227/** Restores the compositor to active status
4228 *
4229 * \param compositor The compositor instance
4230 *
4231 * If the compositor was in a sleeping mode, all outputs are powered
4232 * back on via DPMS. Otherwise if the compositor was inactive
4233 * (idle/locked, offscreen, or sleeping) then the compositor's wake
4234 * signal will fire.
4235 *
4236 * Restarts the idle timer.
4237 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004238WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004239weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004240{
Neil Roberts8b62e202013-09-30 13:14:47 +01004241 uint32_t old_state = compositor->state;
4242
4243 /* The state needs to be changed before emitting the wake
4244 * signal because that may try to schedule a repaint which
4245 * will not work if the compositor is still sleeping */
4246 compositor->state = WESTON_COMPOSITOR_ACTIVE;
4247
4248 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004249 case WESTON_COMPOSITOR_SLEEPING:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004250 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004251 case WESTON_COMPOSITOR_OFFSCREEN:
Daniel Stone893b9362016-11-08 15:47:09 +00004252 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004253 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004254 /* fall through */
4255 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004256 wl_event_source_timer_update(compositor->idle_source,
4257 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004258 }
4259}
4260
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004261/** Turns off rendering and frame events for the compositor.
4262 *
4263 * \param compositor The compositor instance
4264 *
4265 * This is used for example to prevent further rendering while the
4266 * compositor is shutting down.
4267 *
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004268 * Stops the idle timer.
4269 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004270WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004271weston_compositor_offscreen(struct weston_compositor *compositor)
4272{
4273 switch (compositor->state) {
4274 case WESTON_COMPOSITOR_OFFSCREEN:
4275 return;
4276 case WESTON_COMPOSITOR_SLEEPING:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004277 default:
4278 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4279 wl_event_source_timer_update(compositor->idle_source, 0);
4280 }
4281}
4282
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004283/** Powers down all attached output devices
4284 *
4285 * \param compositor The compositor instance
4286 *
4287 * Causes rendering to the outputs to cease, and no frame events to be
4288 * sent. Only powers down the outputs if the compositor is not already
4289 * in sleep mode.
4290 *
4291 * Stops the idle timer.
4292 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004293WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004294weston_compositor_sleep(struct weston_compositor *compositor)
4295{
4296 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
4297 return;
4298
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004299 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004300 compositor->state = WESTON_COMPOSITOR_SLEEPING;
4301 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
4302}
4303
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004304/** Sets compositor to idle mode
4305 *
4306 * \param data The compositor instance
4307 *
4308 * This is called when the idle timer fires. Once the compositor is in
4309 * idle mode it requires a wake action (e.g. via
4310 * weston_compositor_wake()) to restore it. The compositor's
4311 * idle_signal will be triggered when the idle event occurs.
4312 *
4313 * Idleness can be inhibited by setting the compositor's idle_inhibit
4314 * property.
4315 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004316static int
4317idle_handler(void *data)
4318{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004319 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004320
4321 if (compositor->idle_inhibit)
4322 return 1;
4323
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004324 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004325 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004326
4327 return 1;
4328}
4329
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004330WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08004331weston_plane_init(struct weston_plane *plane,
4332 struct weston_compositor *ec,
4333 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004334{
4335 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004336 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004337 plane->x = x;
4338 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08004339 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004340
4341 /* Init the link so that the call to wl_list_remove() when releasing
4342 * the plane without ever stacking doesn't lead to a crash */
4343 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004344}
4345
4346WL_EXPORT void
4347weston_plane_release(struct weston_plane *plane)
4348{
Xiong Zhang97116532013-10-23 13:58:31 +08004349 struct weston_view *view;
4350
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004351 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004352 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004353
Xiong Zhang97116532013-10-23 13:58:31 +08004354 wl_list_for_each(view, &plane->compositor->view_list, link) {
4355 if (view->plane == plane)
4356 view->plane = NULL;
4357 }
4358
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004359 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004360}
4361
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004362WL_EXPORT void
4363weston_compositor_stack_plane(struct weston_compositor *ec,
4364 struct weston_plane *plane,
4365 struct weston_plane *above)
4366{
4367 if (above)
4368 wl_list_insert(above->link.prev, &plane->link);
4369 else
4370 wl_list_insert(&ec->plane_list, &plane->link);
4371}
4372
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004373static void
4374output_release(struct wl_client *client, struct wl_resource *resource)
4375{
4376 wl_resource_destroy(resource);
4377}
4378
4379static const struct wl_output_interface output_interface = {
4380 output_release,
4381};
4382
4383
Casey Dahlin9074db52012-04-19 22:50:09 -04004384static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004385{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004386 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004387}
4388
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004389static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004390bind_output(struct wl_client *client,
4391 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05004392{
Pekka Paalanen05347622017-03-27 12:24:34 +03004393 struct weston_head *head = data;
4394 struct weston_output *output = head->output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004395 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004396 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004397
Jason Ekstranda85118c2013-06-27 20:17:02 -05004398 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004399 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004400 if (resource == NULL) {
4401 wl_client_post_no_memory(client);
4402 return;
4403 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004404
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03004405 wl_list_insert(&head->resource_list, wl_resource_get_link(resource));
Pekka Paalanen055c1132017-03-27 16:31:25 +03004406 wl_resource_set_implementation(resource, &output_interface, head,
Pekka Paalanen05347622017-03-27 12:24:34 +03004407 unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04004408
Pekka Paalanen05347622017-03-27 12:24:34 +03004409 assert(output);
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004410 wl_output_send_geometry(resource,
4411 output->x,
4412 output->y,
Pekka Paalanen01f60212017-03-24 15:39:24 +02004413 head->mm_width,
4414 head->mm_height,
4415 head->subpixel,
4416 head->make, head->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04004417 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004418 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004419 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02004420 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004421
4422 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004423 wl_output_send_mode(resource,
4424 mode->flags,
4425 mode->width,
4426 mode->height,
4427 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004428 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02004429
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004430 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004431 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004432}
4433
Pekka Paalanendcac3512017-12-08 14:13:34 +02004434static void
4435weston_head_add_global(struct weston_head *head)
4436{
4437 head->global = wl_global_create(head->compositor->wl_display,
4438 &wl_output_interface, 3,
4439 head, bind_output);
4440}
4441
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02004442/** Remove the global wl_output protocol object
4443 *
4444 * \param head The head whose global to remove.
4445 *
4446 * Also orphans the wl_resources for this head (wl_output).
4447 */
4448static void
4449weston_head_remove_global(struct weston_head *head)
4450{
4451 struct wl_resource *resource, *tmp;
4452
4453 if (head->global)
4454 wl_global_destroy(head->global);
4455 head->global = NULL;
4456
4457 wl_resource_for_each_safe(resource, tmp, &head->resource_list) {
4458 unbind_resource(resource);
4459 wl_resource_set_destructor(resource, NULL);
4460 wl_resource_set_user_data(resource, NULL);
4461 }
4462}
4463
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004464/** Get the backing object of wl_output
4465 *
4466 * \param resource A wl_output protocol object.
4467 * \return The backing object (user data) of a wl_resource representing a
4468 * wl_output protocol object.
4469 */
Pekka Paalanen055c1132017-03-27 16:31:25 +03004470WL_EXPORT struct weston_head *
4471weston_head_from_resource(struct wl_resource *resource)
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004472{
4473 assert(wl_resource_instance_of(resource, &wl_output_interface,
4474 &output_interface));
4475
4476 return wl_resource_get_user_data(resource);
4477}
4478
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004479/** Initialize a pre-allocated weston_head
4480 *
4481 * \param head The head to initialize.
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004482 * \param name The head name, e.g. the connector name or equivalent.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004483 *
4484 * The head will be safe to attach, detach and release.
4485 *
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004486 * The name is used in logs, and can be used by compositors as a configuration
4487 * identifier.
4488 *
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004489 * \memberof weston_head
4490 * \internal
4491 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004492WL_EXPORT void
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004493weston_head_init(struct weston_head *head, const char *name)
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004494{
4495 /* Add some (in)sane defaults which can be used
4496 * for checking if an output was properly configured
4497 */
4498 memset(head, 0, sizeof *head);
4499
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004500 wl_list_init(&head->compositor_link);
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03004501 wl_signal_init(&head->destroy_signal);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004502 wl_list_init(&head->output_link);
4503 wl_list_init(&head->resource_list);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004504 head->name = strdup(name);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004505}
4506
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004507/** Send output heads changed signal
4508 *
4509 * \param output The output that changed.
4510 *
4511 * Notify that the enabled output gained and/or lost heads, or that the
4512 * associated heads may have changed their connection status. This does not
4513 * include cases where the output becomes enabled or disabled. The registered
4514 * callbacks are called after the change has successfully happened.
4515 *
4516 * If connection status change causes the compositor to attach or detach a head
4517 * to an enabled output, the registered callbacks may be called multiple times.
4518 */
4519static void
4520weston_output_emit_heads_changed(struct weston_output *output)
4521{
4522 wl_signal_emit(&output->compositor->output_heads_changed_signal,
4523 output);
4524}
4525
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004526/** Idle task for emitting heads_changed_signal */
4527static void
4528weston_compositor_call_heads_changed(void *data)
4529{
4530 struct weston_compositor *compositor = data;
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004531 struct weston_head *head;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004532
4533 compositor->heads_changed_source = NULL;
4534
4535 wl_signal_emit(&compositor->heads_changed_signal, compositor);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004536
4537 wl_list_for_each(head, &compositor->head_list, compositor_link) {
4538 if (head->output && head->output->enabled)
4539 weston_output_emit_heads_changed(head->output);
4540 }
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004541}
4542
4543/** Schedule a call on idle to heads_changed callback
4544 *
4545 * \param compositor The Compositor.
4546 *
4547 * \memberof weston_compositor
4548 * \internal
4549 */
4550static void
4551weston_compositor_schedule_heads_changed(struct weston_compositor *compositor)
4552{
4553 struct wl_event_loop *loop;
4554
4555 if (compositor->heads_changed_source)
4556 return;
4557
4558 loop = wl_display_get_event_loop(compositor->wl_display);
4559 compositor->heads_changed_source = wl_event_loop_add_idle(loop,
4560 weston_compositor_call_heads_changed, compositor);
4561}
4562
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004563/** Register a new head
4564 *
4565 * \param compositor The compositor.
4566 * \param head The head to register, must not be already registered.
4567 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004568 * This signals the core that a new head has become available, leading to
4569 * heads_changed hook being called later.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004570 *
4571 * \memberof weston_compositor
4572 * \internal
4573 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004574WL_EXPORT void
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004575weston_compositor_add_head(struct weston_compositor *compositor,
4576 struct weston_head *head)
4577{
4578 assert(wl_list_empty(&head->compositor_link));
4579 assert(head->name);
4580
4581 wl_list_insert(compositor->head_list.prev, &head->compositor_link);
4582 head->compositor = compositor;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004583 weston_compositor_schedule_heads_changed(compositor);
4584}
4585
4586/** Adds a listener to be called when heads change
4587 *
4588 * \param compositor The compositor.
4589 * \param listener The listener to add.
4590 *
4591 * The listener notify function argument is the \var compositor.
4592 *
4593 * The listener function will be called after heads are added or their
4594 * connection status has changed. Several changes may be accumulated into a
4595 * single call. The user is expected to iterate over the existing heads and
4596 * check their statuses to find out what changed.
4597 *
4598 * \sa weston_compositor_iterate_heads, weston_head_is_connected,
4599 * weston_head_is_enabled
4600 * \memberof weston_compositor
4601 */
4602WL_EXPORT void
4603weston_compositor_add_heads_changed_listener(struct weston_compositor *compositor,
4604 struct wl_listener *listener)
4605{
4606 wl_signal_add(&compositor->heads_changed_signal, listener);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004607}
4608
4609/** Iterate over available heads
4610 *
4611 * \param compositor The compositor.
4612 * \param item The iterator, or NULL for start.
4613 * \return The next available head in the list.
4614 *
4615 * Returns all available heads, regardless of being connected or enabled.
4616 *
4617 * You can iterate over all heads as follows:
4618 * \code
4619 * struct weston_head *head = NULL;
4620 *
4621 * while ((head = weston_compositor_iterate_heads(compositor, head))) {
4622 * ...
4623 * }
4624 * \endcode
4625 *
4626 * If you cause \c iter to be removed from the list, you cannot use it to
4627 * continue iterating. Removing any other item is safe.
4628 *
4629 * \memberof weston_compositor
4630 */
4631WL_EXPORT struct weston_head *
4632weston_compositor_iterate_heads(struct weston_compositor *compositor,
4633 struct weston_head *iter)
4634{
4635 struct wl_list *list = &compositor->head_list;
4636 struct wl_list *node;
4637
4638 assert(compositor);
4639 assert(!iter || iter->compositor == compositor);
4640
4641 if (iter)
4642 node = iter->compositor_link.next;
4643 else
4644 node = list->next;
4645
4646 assert(node);
4647 assert(!iter || node != &iter->compositor_link);
4648
4649 if (node == list)
4650 return NULL;
4651
4652 return container_of(node, struct weston_head, compositor_link);
4653}
4654
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004655/** Iterate over attached heads
4656 *
4657 * \param output The output whose heads to iterate.
4658 * \param item The iterator, or NULL for start.
4659 * \return The next attached head in the list.
4660 *
4661 * Returns all heads currently attached to the output.
4662 *
4663 * You can iterate over all heads as follows:
4664 * \code
4665 * struct weston_head *head = NULL;
4666 *
4667 * while ((head = weston_output_iterate_heads(output, head))) {
4668 * ...
4669 * }
4670 * \endcode
4671 *
4672 * If you cause \c iter to be removed from the list, you cannot use it to
4673 * continue iterating. Removing any other item is safe.
4674 *
4675 * \memberof weston_compositor
4676 */
4677WL_EXPORT struct weston_head *
4678weston_output_iterate_heads(struct weston_output *output,
4679 struct weston_head *iter)
4680{
4681 struct wl_list *list = &output->head_list;
4682 struct wl_list *node;
4683
4684 assert(output);
4685 assert(!iter || iter->output == output);
4686
4687 if (iter)
4688 node = iter->output_link.next;
4689 else
4690 node = list->next;
4691
4692 assert(node);
4693 assert(!iter || node != &iter->output_link);
4694
4695 if (node == list)
4696 return NULL;
4697
4698 return container_of(node, struct weston_head, output_link);
4699}
4700
Pekka Paalanendcac3512017-12-08 14:13:34 +02004701/** Attach a head to an output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004702 *
4703 * \param output The output to attach to.
4704 * \param head The head that is not yet attached.
4705 * \return 0 on success, -1 on failure.
4706 *
4707 * Attaches the given head to the output. All heads of an output are clones
4708 * and share the resolution and timings.
4709 *
4710 * Cloning heads this way uses less resources than creating an output for
4711 * each head, but is not always possible due to environment, driver and hardware
4712 * limitations.
4713 *
4714 * On failure, the head remains unattached. Success of this function does not
4715 * guarantee the output configuration is actually valid. The final checks are
Pekka Paalanendcac3512017-12-08 14:13:34 +02004716 * made on weston_output_enable() unless the output was already enabled.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004717 *
4718 * \memberof weston_output
4719 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004720WL_EXPORT int
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004721weston_output_attach_head(struct weston_output *output,
4722 struct weston_head *head)
4723{
Pekka Paalanendcac3512017-12-08 14:13:34 +02004724 char *head_names;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004725
4726 if (!wl_list_empty(&head->output_link))
4727 return -1;
4728
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004729 if (output->attach_head) {
4730 if (output->attach_head(output, head) < 0)
4731 return -1;
4732 } else if (!wl_list_empty(&output->head_list)) {
4733 /* No support for clones in the legacy path. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004734 return -1;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004735 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004736
4737 head->output = output;
4738 wl_list_insert(output->head_list.prev, &head->output_link);
4739
Pekka Paalanendcac3512017-12-08 14:13:34 +02004740 if (output->enabled) {
4741 weston_head_add_global(head);
4742
4743 head_names = weston_output_create_heads_string(output);
4744 weston_log("Output '%s' updated to have head(s) %s\n",
4745 output->name, head_names);
4746 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004747
4748 weston_output_emit_heads_changed(output);
Pekka Paalanendcac3512017-12-08 14:13:34 +02004749 }
4750
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004751 return 0;
4752}
4753
4754/** Detach a head from its output
4755 *
4756 * \param head The head to detach.
4757 *
4758 * It is safe to detach a non-attached head.
4759 *
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004760 * If the head is attached to an enabled output and the output will be left
4761 * with no heads, the output will be disabled.
4762 *
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004763 * \memberof weston_head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004764 * \sa weston_output_disable
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004765 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004766WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004767weston_head_detach(struct weston_head *head)
4768{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004769 struct weston_output *output = head->output;
Pekka Paalanena0106992017-12-08 16:11:17 +02004770 char *head_names;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004771
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004772 wl_list_remove(&head->output_link);
4773 wl_list_init(&head->output_link);
4774 head->output = NULL;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004775
4776 if (!output)
4777 return;
4778
4779 if (output->detach_head)
4780 output->detach_head(output, head);
4781
4782 if (output->enabled) {
4783 weston_head_remove_global(head);
4784
Pekka Paalanena0106992017-12-08 16:11:17 +02004785 if (wl_list_empty(&output->head_list)) {
4786 weston_log("Output '%s' no heads left, disabling.\n",
4787 output->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004788 weston_output_disable(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02004789 } else {
4790 head_names = weston_output_create_heads_string(output);
4791 weston_log("Output '%s' updated to have head(s) %s\n",
4792 output->name, head_names);
4793 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004794
4795 weston_output_emit_heads_changed(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02004796 }
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004797 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004798}
4799
4800/** Destroy a head
4801 *
4802 * \param head The head to be released.
4803 *
4804 * Destroys the head. The caller is responsible for freeing the memory pointed
4805 * to by \c head.
4806 *
4807 * \memberof weston_head
4808 * \internal
4809 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004810WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004811weston_head_release(struct weston_head *head)
4812{
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03004813 wl_signal_emit(&head->destroy_signal, head);
4814
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004815 weston_head_detach(head);
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03004816
4817 free(head->make);
4818 free(head->model);
4819 free(head->serial_number);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004820 free(head->name);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004821
4822 wl_list_remove(&head->compositor_link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004823}
4824
Pekka Paalanene19970f2017-08-28 14:11:02 +03004825static void
4826weston_head_set_device_changed(struct weston_head *head)
4827{
4828 head->device_changed = true;
4829
4830 if (head->compositor)
4831 weston_compositor_schedule_heads_changed(head->compositor);
4832}
4833
4834/** String equal comparison with NULLs being equal */
4835static bool
4836str_null_eq(const char *a, const char *b)
4837{
4838 if (!a && !b)
4839 return true;
4840
4841 if (!!a != !!b)
4842 return false;
4843
4844 return strcmp(a, b) == 0;
4845}
4846
Pekka Paalanen01f60212017-03-24 15:39:24 +02004847/** Store monitor make, model and serial number
4848 *
4849 * \param head The head to modify.
4850 * \param make The monitor make. If EDID is available, the PNP ID. Otherwise
4851 * any string, or NULL for none.
4852 * \param model The monitor model or name, or a made-up string, or NULL for
4853 * none.
4854 * \param serialno The monitor serial number, a made-up string, or NULL for
4855 * none.
4856 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03004857 * This may set the device_changed flag.
4858 *
Pekka Paalanen01f60212017-03-24 15:39:24 +02004859 * \memberof weston_head
4860 * \internal
4861 */
4862WL_EXPORT void
4863weston_head_set_monitor_strings(struct weston_head *head,
4864 const char *make,
4865 const char *model,
4866 const char *serialno)
4867{
Pekka Paalanene19970f2017-08-28 14:11:02 +03004868 if (str_null_eq(head->make, make) &&
4869 str_null_eq(head->model, model) &&
4870 str_null_eq(head->serial_number, serialno))
4871 return;
4872
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03004873 free(head->make);
4874 free(head->model);
4875 free(head->serial_number);
4876
4877 head->make = make ? strdup(make) : NULL;
4878 head->model = model ? strdup(model) : NULL;
4879 head->serial_number = serialno ? strdup(serialno) : NULL;
Pekka Paalanene19970f2017-08-28 14:11:02 +03004880
4881 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02004882}
4883
4884/** Store physical image size
4885 *
4886 * \param head The head to modify.
4887 * \param mm_width Image area width in millimeters.
4888 * \param mm_height Image area height in millimeters.
4889 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03004890 * This may set the device_changed flag.
4891 *
Pekka Paalanen01f60212017-03-24 15:39:24 +02004892 * \memberof weston_head
4893 * \internal
4894 */
4895WL_EXPORT void
4896weston_head_set_physical_size(struct weston_head *head,
4897 int32_t mm_width, int32_t mm_height)
4898{
Pekka Paalanene19970f2017-08-28 14:11:02 +03004899 if (head->mm_width == mm_width &&
4900 head->mm_height == mm_height)
4901 return;
4902
Pekka Paalanen01f60212017-03-24 15:39:24 +02004903 head->mm_width = mm_width;
4904 head->mm_height = mm_height;
Pekka Paalanene19970f2017-08-28 14:11:02 +03004905
4906 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02004907}
4908
4909/** Store monitor sub-pixel layout
4910 *
4911 * \param head The head to modify.
4912 * \param sp Sub-pixel layout. The possible values are:
4913 * - WL_OUTPUT_SUBPIXEL_UNKNOWN,
4914 * - WL_OUTPUT_SUBPIXEL_NONE,
4915 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB,
4916 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR,
4917 * - WL_OUTPUT_SUBPIXEL_VERTICAL_RGB,
4918 * - WL_OUTPUT_SUBPIXEL_VERTICAL_BGR
4919 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03004920 * This may set the device_changed flag.
4921 *
Pekka Paalanen01f60212017-03-24 15:39:24 +02004922 * \memberof weston_head
4923 * \internal
4924 */
4925WL_EXPORT void
4926weston_head_set_subpixel(struct weston_head *head,
4927 enum wl_output_subpixel sp)
4928{
Pekka Paalanene19970f2017-08-28 14:11:02 +03004929 if (head->subpixel == sp)
4930 return;
4931
Pekka Paalanen01f60212017-03-24 15:39:24 +02004932 head->subpixel = sp;
Pekka Paalanene19970f2017-08-28 14:11:02 +03004933
4934 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02004935}
4936
4937/** Mark the monitor as internal
4938 *
4939 * This is used for embedded screens, like laptop panels.
4940 *
4941 * \param head The head to mark as internal.
4942 *
4943 * By default a head is external. The type is often inferred from the physical
4944 * connector type.
4945 *
4946 * \memberof weston_head
4947 * \internal
4948 */
4949WL_EXPORT void
4950weston_head_set_internal(struct weston_head *head)
4951{
4952 head->connection_internal = true;
4953}
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004954
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03004955/** Store connector status
4956 *
4957 * \param head The head to modify.
4958 * \param connected Whether the head is connected.
4959 *
4960 * Connectors are created as disconnected. This function can be used to
4961 * set the connector status.
4962 *
4963 * The status should be set to true when a physical connector is connected to
4964 * a video sink device like a monitor and to false when the connector is
4965 * disconnected. For nested backends, the connection status should reflect the
4966 * connection to the parent display server.
4967 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004968 * When the connection status changes, it schedules a call to the heads_changed
Pekka Paalanene19970f2017-08-28 14:11:02 +03004969 * hook and sets the device_changed flag.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004970 *
4971 * \sa weston_compositor_set_heads_changed_cb
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03004972 * \memberof weston_head
4973 * \internal
4974 */
4975WL_EXPORT void
4976weston_head_set_connection_status(struct weston_head *head, bool connected)
4977{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004978 if (head->connected == connected)
4979 return;
4980
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03004981 head->connected = connected;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004982
Pekka Paalanene19970f2017-08-28 14:11:02 +03004983 weston_head_set_device_changed(head);
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03004984}
4985
4986/** Is the head currently connected?
4987 *
4988 * \param head The head to query.
4989 * \return Connection status.
4990 *
4991 * Returns true if the head is physically connected to a monitor, or in
4992 * case of a nested backend returns true when there is a connection to the
4993 * parent display server.
4994 *
4995 * This is independent from the head being enabled.
4996 *
4997 * \sa weston_head_is_enabled
4998 * \memberof weston_head
4999 */
5000WL_EXPORT bool
5001weston_head_is_connected(struct weston_head *head)
5002{
5003 return head->connected;
5004}
5005
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005006/** Is the head currently enabled?
5007 *
5008 * \param head The head to query.
5009 * \return Video status.
5010 *
5011 * Returns true if the head is currently transmitting a video stream.
5012 *
5013 * This is independent of the head being connected.
5014 *
5015 * \sa weston_head_is_connected
5016 * \memberof weston_head
5017 */
5018WL_EXPORT bool
5019weston_head_is_enabled(struct weston_head *head)
5020{
5021 if (!head->output)
5022 return false;
5023
5024 return head->output->enabled;
5025}
5026
Pekka Paalanene19970f2017-08-28 14:11:02 +03005027/** Has the device information changed?
5028 *
5029 * \param head The head to query.
5030 * \return True if the device information has changed since last reset.
5031 *
5032 * The information about the connected display device, e.g. a monitor, may
5033 * change without being disconnected in between. Changing information
5034 * causes a call to the heads_changed hook.
5035 *
5036 * The information includes make, model, serial number, physical size,
5037 * and sub-pixel type. The connection status is also included.
5038 *
5039 * \sa weston_head_reset_device_changed, weston_compositor_set_heads_changed_cb
5040 * \memberof weston_head
5041 */
5042WL_EXPORT bool
5043weston_head_is_device_changed(struct weston_head *head)
5044{
5045 return head->device_changed;
5046}
5047
5048/** Acknowledge device information change
5049 *
5050 * \param head The head to acknowledge.
5051 *
5052 * Clears the device changed flag on this head. When a compositor has processed
5053 * device information, it should call this to be able to notice further
5054 * changes.
5055 *
5056 * \sa weston_head_is_device_changed
5057 * \memberof weston_head
5058 */
5059WL_EXPORT void
5060weston_head_reset_device_changed(struct weston_head *head)
5061{
5062 head->device_changed = false;
5063}
5064
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005065/** Get the name of a head
5066 *
5067 * \param head The head to query.
5068 * \return The head's name, not NULL.
5069 *
5070 * The name depends on the backend. The DRM backend uses connector names,
5071 * other backends may use hardcoded names or user-given names.
5072 */
5073WL_EXPORT const char *
5074weston_head_get_name(struct weston_head *head)
5075{
5076 return head->name;
5077}
5078
5079/** Get the output the head is attached to
5080 *
5081 * \param head The head to query.
5082 * \return The output the head is attached to, or NULL if detached.
5083 */
5084WL_EXPORT struct weston_output *
5085weston_head_get_output(struct weston_head *head)
5086{
5087 return head->output;
5088}
5089
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005090/** Add destroy callback for a head
5091 *
5092 * \param head The head to watch for.
5093 * \param listener The listener to add. The \c notify member must be set.
5094 *
5095 * Heads may get destroyed for various reasons by the backends. If a head is
5096 * attached to an output, the compositor should listen for head destruction
5097 * and reconfigure or destroy the output if necessary.
5098 *
5099 * The destroy callbacks will be called on weston_head destruction before any
5100 * automatic detaching from an associated weston_output and before any
5101 * weston_head information is lost.
5102 *
5103 * The \c data argument to the notify callback is the weston_head being
5104 * destroyed.
5105 */
5106WL_EXPORT void
5107weston_head_add_destroy_listener(struct weston_head *head,
5108 struct wl_listener *listener)
5109{
5110 wl_signal_add(&head->destroy_signal, listener);
5111}
5112
5113/** Look up destroy listener for a head
5114 *
5115 * \param head The head to query.
5116 * \param notify The notify function used used for the added destroy listener.
5117 * \return The listener, or NULL if not found.
5118 *
5119 * This looks up the previously added destroy listener struct based on the
5120 * notify function it has. The listener can be used to access user data
5121 * through \c container_of().
5122 *
5123 * \sa wl_signal_get()
5124 */
5125WL_EXPORT struct wl_listener *
5126weston_head_get_destroy_listener(struct weston_head *head,
5127 wl_notify_func_t notify)
5128{
5129 return wl_signal_get(&head->destroy_signal, notify);
5130}
5131
David Fort0de859e2016-05-27 23:22:57 +02005132/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005133static void
David Fort0de859e2016-05-27 23:22:57 +02005134weston_compositor_reflow_outputs(struct weston_compositor *compositor,
5135 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005136{
5137 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02005138 bool start_resizing = false;
5139
5140 if (!delta_width)
5141 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005142
5143 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02005144 if (output == resized_output) {
5145 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005146 continue;
5147 }
5148
David Fort0de859e2016-05-27 23:22:57 +02005149 if (start_resizing) {
5150 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005151 output->dirty = 1;
5152 }
5153 }
5154}
5155
Pekka Paalanend72bad22017-03-29 17:01:41 +03005156static void
Scott Moreauccbf29d2012-02-22 14:21:41 -07005157weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005158{
Scott Moreau850ca422012-05-21 15:21:25 -06005159 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05005160
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005161 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005162 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06005163
Scott Moreauccbf29d2012-02-22 14:21:41 -07005164 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06005165 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005166 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005167 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005168 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005169 weston_matrix_scale(&output->matrix, magnification,
5170 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005171 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005172
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005173 switch (output->transform) {
5174 case WL_OUTPUT_TRANSFORM_FLIPPED:
5175 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5176 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5177 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5178 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5179 weston_matrix_scale(&output->matrix, -1, 1, 1);
5180 break;
5181 }
5182
5183 switch (output->transform) {
5184 default:
5185 case WL_OUTPUT_TRANSFORM_NORMAL:
5186 case WL_OUTPUT_TRANSFORM_FLIPPED:
5187 break;
5188 case WL_OUTPUT_TRANSFORM_90:
5189 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5190 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
5191 weston_matrix_rotate_xy(&output->matrix, 0, 1);
5192 break;
5193 case WL_OUTPUT_TRANSFORM_180:
5194 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5195 weston_matrix_translate(&output->matrix,
5196 -output->width, -output->height, 0);
5197 weston_matrix_rotate_xy(&output->matrix, -1, 0);
5198 break;
5199 case WL_OUTPUT_TRANSFORM_270:
5200 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5201 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5202 weston_matrix_rotate_xy(&output->matrix, 0, -1);
5203 break;
5204 }
5205
5206 if (output->current_scale != 1)
5207 weston_matrix_scale(&output->matrix,
5208 output->current_scale,
5209 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01005210
Scott Moreauccbf29d2012-02-22 14:21:41 -07005211 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05005212
5213 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005214}
5215
Scott Moreau1bad5db2012-08-18 01:04:05 -06005216static void
Alexander Larsson0b135062013-05-28 16:23:36 +02005217weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06005218{
5219 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005220 output->native_scale = scale;
5221 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06005222
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005223 convert_size_by_transform_scale(&output->width, &output->height,
5224 output->current_mode->width,
5225 output->current_mode->height,
5226 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02005227}
5228
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005229static void
5230weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07005231{
5232 output->x = x;
5233 output->y = y;
5234
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005235 pixman_region32_fini(&output->previous_damage);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02005236 pixman_region32_init(&output->previous_damage);
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005237
5238 pixman_region32_fini(&output->region);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005239 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06005240 output->width,
5241 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005242}
5243
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005244WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005245weston_output_move(struct weston_output *output, int x, int y)
5246{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005247 struct weston_head *head;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005248 struct wl_resource *resource;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005249 int ver;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005250
5251 output->move_x = x - output->x;
5252 output->move_y = y - output->y;
5253
5254 if (output->move_x == 0 && output->move_y == 0)
5255 return;
5256
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005257 weston_output_init_geometry(output, x, y);
5258
5259 output->dirty = 1;
5260
5261 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005262 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005263
5264 /* Notify clients of the change for output position. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005265 wl_list_for_each(head, &output->head_list, output_link) {
5266 wl_resource_for_each(resource, &head->resource_list) {
5267 wl_output_send_geometry(resource,
5268 output->x,
5269 output->y,
5270 head->mm_width,
5271 head->mm_height,
5272 head->subpixel,
5273 head->make,
5274 head->model,
5275 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08005276
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005277 ver = wl_resource_get_version(resource);
5278 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
5279 wl_output_send_done(resource);
5280 }
Quanxian Wangb2c86362014-03-14 09:16:25 +08005281 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005282}
5283
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005284/** Signal that a pending output is taken into use.
5285 *
5286 * Removes the output from the pending list and adds it to the compositor's
5287 * list of enabled outputs. The output created signal is emitted.
Giulio Camuffob1147152015-05-06 21:41:57 +03005288 *
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005289 * The output gets an internal ID assigned, and the wl_output global is
5290 * created.
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005291 *
Giulio Camuffob1147152015-05-06 21:41:57 +03005292 * \param compositor The compositor instance.
5293 * \param output The output to be added.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005294 *
5295 * \internal
Giulio Camuffob1147152015-05-06 21:41:57 +03005296 */
Pekka Paalanenf9681b52017-03-29 16:58:48 +03005297static void
Giulio Camuffob1147152015-05-06 21:41:57 +03005298weston_compositor_add_output(struct weston_compositor *compositor,
5299 struct weston_output *output)
5300{
Armin Krezoviće5403842016-08-05 15:28:29 +02005301 struct weston_view *view, *next;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005302 struct weston_head *head;
Armin Krezoviće5403842016-08-05 15:28:29 +02005303
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005304 assert(!output->enabled);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005305
5306 /* Verify we haven't reached the limit of 32 available output IDs */
5307 assert(ffs(~compositor->output_id_pool) > 0);
5308
5309 /* Invert the output id pool and look for the lowest numbered
5310 * switch (the least significant bit). Take that bit's position
5311 * as our ID, and mark it used in the compositor's output_id_pool.
5312 */
5313 output->id = ffs(~compositor->output_id_pool) - 1;
5314 compositor->output_id_pool |= 1u << output->id;
5315
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005316 wl_list_remove(&output->link);
Giulio Camuffob1147152015-05-06 21:41:57 +03005317 wl_list_insert(compositor->output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005318 output->enabled = true;
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005319
Pekka Paalanendcac3512017-12-08 14:13:34 +02005320 wl_list_for_each(head, &output->head_list, output_link)
5321 weston_head_add_global(head);
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005322
Giulio Camuffob1147152015-05-06 21:41:57 +03005323 wl_signal_emit(&compositor->output_created_signal, output);
Armin Krezoviće5403842016-08-05 15:28:29 +02005324
5325 wl_list_for_each_safe(view, next, &compositor->view_list, link)
5326 weston_view_geometry_dirty(view);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005327}
5328
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005329/** Transform device coordinates into global coordinates
5330 *
5331 * \param device_x[in] X coordinate in device units.
5332 * \param device_y[in] Y coordinate in device units.
5333 * \param x[out] X coordinate in the global space.
5334 * \param y[out] Y coordinate in the global space.
5335 *
5336 * Transforms coordinates from the device coordinate space
5337 * (physical pixel units) to the global coordinate space (logical pixel units).
5338 * This takes into account output transform and scale.
5339 *
5340 * \memberof weston_output
5341 * \internal
5342 */
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005343WL_EXPORT void
5344weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005345 double device_x, double device_y,
5346 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005347{
Derek Foreman0f679412014-10-02 13:41:17 -05005348 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005349 device_x,
5350 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05005351 0.0,
5352 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005353
Derek Foreman67a18b92015-03-24 11:36:14 -05005354 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005355
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005356 *x = p.f[0] / p.f[3];
5357 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005358}
5359
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005360/** Removes output from compositor's list of enabled outputs
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005361 *
5362 * \param output The weston_output object that is being removed.
5363 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005364 * The following happens:
5365 *
5366 * - The output assignments of all views in the current scenegraph are
5367 * recomputed.
5368 *
5369 * - Presentation feedback is discarded.
5370 *
5371 * - Compositor is notified that outputs were changed and
5372 * applies the necessary changes to re-layout outputs.
5373 *
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005374 * - The output is put back in the pending outputs list.
5375 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005376 * - Signal is emitted to notify all users of the weston_output
5377 * object that the output is being destroyed.
5378 *
5379 * - wl_output protocol objects referencing this weston_output
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005380 * are made inert, and the wl_output global is removed.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005381 *
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005382 * - The output's internal ID is released.
5383 *
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005384 * \memberof weston_output
5385 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005386 */
5387static void
5388weston_compositor_remove_output(struct weston_output *output)
5389{
Pekka Paalanenbccda712017-03-29 16:16:04 +03005390 struct weston_compositor *compositor = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005391 struct weston_view *view;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005392 struct weston_head *head;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005393
5394 assert(output->destroying);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005395 assert(output->enabled);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005396
Pekka Paalanenbccda712017-03-29 16:16:04 +03005397 wl_list_for_each(view, &compositor->view_list, link) {
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005398 if (view->output_mask & (1u << output->id))
5399 weston_view_assign_output(view);
5400 }
5401
5402 weston_presentation_feedback_discard_list(&output->feedback_list);
5403
Pekka Paalanenbccda712017-03-29 16:16:04 +03005404 weston_compositor_reflow_outputs(compositor, output, output->width);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005405
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005406 wl_list_remove(&output->link);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005407 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005408 output->enabled = false;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005409
Pekka Paalanenbccda712017-03-29 16:16:04 +03005410 wl_signal_emit(&compositor->output_destroyed_signal, output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005411 wl_signal_emit(&output->destroy_signal, output);
5412
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02005413 wl_list_for_each(head, &output->head_list, output_link)
5414 weston_head_remove_global(head);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005415
5416 compositor->output_id_pool &= ~(1u << output->id);
5417 output->id = 0xffffffff; /* invalid */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005418}
5419
5420/** Sets the output scale for a given output.
5421 *
5422 * \param output The weston_output object that the scale is set for.
5423 * \param scale Scale factor for the given output.
5424 *
5425 * It only supports setting scale for an output that
5426 * is not enabled and it can only be ran once.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005427 *
5428 * \memberof weston_output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005429 */
5430WL_EXPORT void
5431weston_output_set_scale(struct weston_output *output,
5432 int32_t scale)
5433{
5434 /* We can only set scale on a disabled output */
5435 assert(!output->enabled);
5436
5437 /* We only want to set scale once */
5438 assert(!output->scale);
5439
5440 output->scale = scale;
5441}
5442
5443/** Sets the output transform for a given output.
5444 *
5445 * \param output The weston_output object that the transform is set for.
5446 * \param transform Transform value for the given output.
5447 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005448 * Refer to wl_output::transform section located at
5449 * https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_output
5450 * for list of values that can be passed to this function.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005451 *
5452 * \memberof weston_output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005453 */
5454WL_EXPORT void
5455weston_output_set_transform(struct weston_output *output,
5456 uint32_t transform)
5457{
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005458 struct weston_pointer_motion_event ev;
5459 struct wl_resource *resource;
5460 struct weston_seat *seat;
5461 pixman_region32_t old_region;
5462 int mid_x, mid_y;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005463 struct weston_head *head;
5464 int ver;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005465
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005466 if (!output->enabled && output->transform == UINT32_MAX) {
5467 output->transform = transform;
5468 return;
5469 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005470
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005471 weston_output_transform_scale_init(output, transform, output->scale);
5472
5473 pixman_region32_init(&old_region);
5474 pixman_region32_copy(&old_region, &output->region);
5475
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005476 weston_output_init_geometry(output, output->x, output->y);
5477
5478 output->dirty = 1;
5479
5480 /* Notify clients of the change for output transform. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005481 wl_list_for_each(head, &output->head_list, output_link) {
5482 wl_resource_for_each(resource, &head->resource_list) {
5483 wl_output_send_geometry(resource,
5484 output->x,
5485 output->y,
5486 head->mm_width,
5487 head->mm_height,
5488 head->subpixel,
5489 head->make,
5490 head->model,
5491 output->transform);
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005492
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005493 ver = wl_resource_get_version(resource);
5494 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
5495 wl_output_send_done(resource);
5496 }
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005497 }
5498
5499 /* we must ensure that pointers are inside output, otherwise they disappear */
5500 mid_x = output->x + output->width / 2;
5501 mid_y = output->y + output->height / 2;
5502
5503 ev.mask = WESTON_POINTER_MOTION_ABS;
5504 ev.x = wl_fixed_to_double(wl_fixed_from_int(mid_x));
5505 ev.y = wl_fixed_to_double(wl_fixed_from_int(mid_y));
5506
5507 wl_list_for_each(seat, &output->compositor->seat_list, link) {
5508 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5509
5510 if (pointer && pixman_region32_contains_point(&old_region,
5511 wl_fixed_to_int(pointer->x),
5512 wl_fixed_to_int(pointer->y),
5513 NULL))
5514 weston_pointer_move(pointer, &ev);
5515 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005516}
5517
5518/** Initializes a weston_output object with enough data so
5519 ** an output can be configured.
5520 *
5521 * \param output The weston_output object to initialize
5522 * \param compositor The compositor instance.
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005523 * \param name Name for the output (the string is copied).
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005524 *
5525 * Sets initial values for fields that are expected to be
5526 * configured either by compositors or backends.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005527 *
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005528 * The name is used in logs, and can be used by compositors as a configuration
5529 * identifier.
5530 *
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005531 * \memberof weston_output
5532 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005533 */
5534WL_EXPORT void
Armin Krezović40087402016-09-30 14:11:12 +02005535weston_output_init(struct weston_output *output,
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005536 struct weston_compositor *compositor,
5537 const char *name)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005538{
5539 output->compositor = compositor;
5540 output->destroying = 0;
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005541 output->name = strdup(name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005542 wl_list_init(&output->link);
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02005543 wl_signal_init(&output->user_destroy_signal);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005544 output->enabled = false;
5545
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005546 wl_list_init(&output->head_list);
5547
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005548 /* Add some (in)sane defaults which can be used
5549 * for checking if an output was properly configured
5550 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005551 output->scale = 0;
5552 /* Can't use -1 on uint32_t and 0 is valid enum value */
5553 output->transform = UINT32_MAX;
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005554
5555 pixman_region32_init(&output->previous_damage);
5556 pixman_region32_init(&output->region);
Pekka Paalanen42704142017-09-06 16:47:52 +03005557 wl_list_init(&output->mode_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005558}
5559
5560/** Adds weston_output object to pending output list.
5561 *
5562 * \param output The weston_output object to add
5563 * \param compositor The compositor instance.
5564 *
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03005565 * The opposite of this operation is built into weston_output_release().
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005566 *
5567 * \memberof weston_output
5568 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005569 */
5570WL_EXPORT void
5571weston_compositor_add_pending_output(struct weston_output *output,
5572 struct weston_compositor *compositor)
5573{
Pekka Paalanene952a012017-03-29 17:14:00 +03005574 assert(output->disable);
5575 assert(output->enable);
5576
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005577 wl_list_remove(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005578 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005579}
5580
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02005581/** Create a string with the attached heads' names.
5582 *
5583 * The string must be free()'d.
5584 */
5585static char *
5586weston_output_create_heads_string(struct weston_output *output)
5587{
5588 FILE *fp;
5589 char *str = NULL;
5590 size_t size = 0;
5591 struct weston_head *head;
5592 const char *sep = "";
5593
5594 fp = open_memstream(&str, &size);
5595 if (!fp)
5596 return NULL;
5597
5598 wl_list_for_each(head, &output->head_list, output_link) {
5599 fprintf(fp, "%s%s", sep, head->name);
5600 sep = ", ";
5601 }
5602 fclose(fp);
5603
5604 return str;
5605}
5606
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005607/** Constructs a weston_output object that can be used by the compositor.
5608 *
Pekka Paalanencc201e42017-03-30 15:11:25 +03005609 * \param output The weston_output object that needs to be enabled. Must not
Pekka Paalanenddce54d2017-08-23 16:00:21 +03005610 * be enabled already. Must have at least one head attached.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005611 *
5612 * Output coordinates are calculated and each new output is by default
5613 * assigned to the right of previous one.
5614 *
5615 * Sets up the transformation, zoom, and geometry of the output using
5616 * the properties that need to be configured by the compositor.
5617 *
5618 * Establishes a repaint timer for the output with the relevant display
5619 * object's event loop. See output_repaint_timer_handler().
5620 *
5621 * The output is assigned an ID. Weston can support up to 32 distinct
5622 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
5623 * is referred to and used to find the first available ID number, and
5624 * then this ID is marked as used in output_id_pool.
5625 *
5626 * The output is also assigned a Wayland global with the wl_output
5627 * external interface.
5628 *
5629 * Backend specific function is called to set up the output output.
5630 *
5631 * Output is added to the compositor's output list
5632 *
5633 * If the backend specific function fails, the weston_output object
5634 * is returned to a state it was before calling this function and
5635 * is added to the compositor's pending_output_list in case it needs
5636 * to be reconfigured or just so it can be destroyed at shutdown.
5637 *
5638 * 0 is returned on success, -1 on failure.
5639 */
5640WL_EXPORT int
5641weston_output_enable(struct weston_output *output)
5642{
Armin Krezović782f5df2016-09-30 14:11:11 +02005643 struct weston_compositor *c = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005644 struct weston_output *iterator;
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03005645 struct weston_head *head;
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02005646 char *head_names;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005647 int x = 0, y = 0;
5648
Pekka Paalanencc201e42017-03-30 15:11:25 +03005649 if (output->enabled) {
5650 weston_log("Error: attempt to enable an enabled output '%s'\n",
5651 output->name);
5652 return -1;
5653 }
5654
Pekka Paalanenddce54d2017-08-23 16:00:21 +03005655 if (wl_list_empty(&output->head_list)) {
5656 weston_log("Error: cannot enable output '%s' without heads.\n",
5657 output->name);
5658 return -1;
5659 }
5660
Pekka Paalanen586e1ac2017-09-14 16:17:59 +03005661 if (wl_list_empty(&output->mode_list) || !output->current_mode) {
5662 weston_log("Error: no video mode for output '%s'.\n",
5663 output->name);
5664 return -1;
5665 }
5666
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03005667 wl_list_for_each(head, &output->head_list, output_link) {
5668 assert(head->make);
5669 assert(head->model);
5670 }
5671
Armin Krezović782f5df2016-09-30 14:11:11 +02005672 iterator = container_of(c->output_list.prev,
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005673 struct weston_output, link);
5674
Armin Krezović782f5df2016-09-30 14:11:11 +02005675 if (!wl_list_empty(&c->output_list))
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005676 x = iterator->x + iterator->width;
5677
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005678 /* Make sure the scale is set up */
5679 assert(output->scale);
5680
5681 /* Make sure we have a transform set */
5682 assert(output->transform != UINT32_MAX);
5683
Armin Krezović782f5df2016-09-30 14:11:11 +02005684 output->x = x;
5685 output->y = y;
5686 output->dirty = 1;
5687 output->original_scale = output->scale;
5688
5689 weston_output_transform_scale_init(output, output->transform, output->scale);
5690 weston_output_init_zoom(output);
5691
5692 weston_output_init_geometry(output, x, y);
5693 weston_output_damage(output);
5694
5695 wl_signal_init(&output->frame_signal);
5696 wl_signal_init(&output->destroy_signal);
5697 wl_list_init(&output->animation_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02005698 wl_list_init(&output->feedback_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02005699
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005700 /* Enable the output (set up the crtc or create a
5701 * window representing the output, set up the
5702 * renderer, etc)
5703 */
5704 if (output->enable(output) < 0) {
5705 weston_log("Enabling output \"%s\" failed.\n", output->name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005706 return -1;
5707 }
5708
5709 weston_compositor_add_output(output->compositor, output);
5710
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02005711 head_names = weston_output_create_heads_string(output);
5712 weston_log("Output '%s' enabled with head(s) %s\n",
5713 output->name, head_names);
5714 free(head_names);
5715
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005716 return 0;
5717}
5718
5719/** Converts a weston_output object to a pending output state, so it
5720 ** can be configured again or destroyed.
5721 *
5722 * \param output The weston_output object that needs to be disabled.
5723 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005724 * Calls a backend specific function to disable an output, in case
5725 * such function exists.
5726 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03005727 * The backend specific disable function may choose to postpone the disabling
5728 * by returning a negative value, in which case this function returns early.
5729 * In that case the backend will guarantee the output will be disabled soon
5730 * by the backend calling this function again. One must not attempt to re-enable
5731 * the output until that happens.
5732 *
5733 * Otherwise, if the output is being used by the compositor, it is removed
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005734 * from weston's output_list (see weston_compositor_remove_output())
5735 * and is returned to a state it was before weston_output_enable()
5736 * was ran (see weston_output_enable_undo()).
5737 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03005738 * See weston_output_init() for more information on the
5739 * state output is returned to.
Pekka Paalanencc201e42017-03-30 15:11:25 +03005740 *
5741 * If the output has never been enabled yet, this function can still be
5742 * called to ensure that the output is actually turned off rather than left
5743 * in the state it was discovered in.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005744 */
5745WL_EXPORT void
5746weston_output_disable(struct weston_output *output)
5747{
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005748 /* Should we rename this? */
5749 output->destroying = 1;
5750
Pekka Paalanenc65df642017-03-29 15:45:46 +03005751 /* Disable is called unconditionally also for not-enabled outputs,
5752 * because at compositor start-up, if there is an output that is
5753 * already on but the compositor wants to turn it off, we have to
5754 * forward the turn-off to the backend so it knows to do it.
5755 * The backend cannot initially turn off everything, because it
5756 * would cause unnecessary mode-sets for all outputs the compositor
5757 * wants to be on.
5758 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005759 if (output->disable(output) < 0)
5760 return;
5761
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005762 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005763 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005764
5765 output->destroying = 0;
5766}
5767
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03005768/** Forces a synchronous call to heads_changed hook
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005769 *
5770 * \param compositor The compositor instance
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03005771 *
5772 * If there are new or changed heads, calls the heads_changed hook and
5773 * returns after the hook returns.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005774 */
5775WL_EXPORT void
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03005776weston_compositor_flush_heads_changed(struct weston_compositor *compositor)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005777{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005778 if (compositor->heads_changed_source) {
5779 wl_event_source_remove(compositor->heads_changed_source);
5780 weston_compositor_call_heads_changed(compositor);
5781 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005782}
5783
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02005784/** Add destroy callback for an output
5785 *
5786 * \param output The output to watch.
5787 * \param listener The listener to add. The \c notify member must be set.
5788 *
5789 * The listener callback will be called when user destroys an output. This
5790 * may be delayed by a backend in some cases. The main purpose of the
5791 * listener is to allow hooking up custom data to the output. The custom data
5792 * can be fetched via weston_output_get_destroy_listener() followed by
5793 * container_of().
5794 *
5795 * The \c data argument to the notify callback is the weston_output being
5796 * destroyed.
5797 *
5798 * @note This is for the final destruction of an output, not when it gets
5799 * disabled. If you want to keep track of enabled outputs, this is not it.
5800 */
5801WL_EXPORT void
5802weston_output_add_destroy_listener(struct weston_output *output,
5803 struct wl_listener *listener)
5804{
5805 wl_signal_add(&output->user_destroy_signal, listener);
5806}
5807
5808/** Look up destroy listener for an output
5809 *
5810 * \param output The output to query.
5811 * \param notify The notify function used used for the added destroy listener.
5812 * \return The listener, or NULL if not found.
5813 *
5814 * This looks up the previously added destroy listener struct based on the
5815 * notify function it has. The listener can be used to access user data
5816 * through \c container_of().
5817 *
5818 * \sa wl_signal_get() weston_output_add_destroy_listener()
5819 */
5820WL_EXPORT struct wl_listener *
5821weston_output_get_destroy_listener(struct weston_output *output,
5822 wl_notify_func_t notify)
5823{
5824 return wl_signal_get(&output->user_destroy_signal, notify);
5825}
5826
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005827/** Uninitialize an output
5828 *
5829 * Removes the output from the list of enabled outputs if necessary, but
5830 * does not call the backend's output disable function. The output will no
5831 * longer be in the list of pending outputs either.
5832 *
5833 * All fields of weston_output become uninitialized, i.e. should not be used
5834 * anymore. The caller can free the memory after this.
5835 *
5836 * \memberof weston_output
5837 * \internal
5838 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005839WL_EXPORT void
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03005840weston_output_release(struct weston_output *output)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005841{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005842 struct weston_head *head, *tmp;
5843
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005844 output->destroying = 1;
5845
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02005846 wl_signal_emit(&output->user_destroy_signal, output);
5847
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03005848 if (output->idle_repaint_source)
5849 wl_event_source_remove(output->idle_repaint_source);
5850
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005851 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005852 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005853
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005854 pixman_region32_fini(&output->region);
5855 pixman_region32_fini(&output->previous_damage);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005856 wl_list_remove(&output->link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005857
5858 wl_list_for_each_safe(head, tmp, &output->head_list, output_link)
5859 weston_head_detach(head);
5860
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005861 free(output->name);
5862}
5863
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02005864/** Find an output by its given name
5865 *
5866 * \param compositor The compositor to search in.
5867 * \param name The output name to search for.
5868 * \return An existing output with the given name, or NULL if not found.
5869 *
5870 * \memberof weston_compositor
5871 */
5872WL_EXPORT struct weston_output *
5873weston_compositor_find_output_by_name(struct weston_compositor *compositor,
5874 const char *name)
5875{
5876 struct weston_output *output;
5877
5878 wl_list_for_each(output, &compositor->output_list, link)
5879 if (strcmp(output->name, name) == 0)
5880 return output;
5881
5882 wl_list_for_each(output, &compositor->pending_output_list, link)
5883 if (strcmp(output->name, name) == 0)
5884 return output;
5885
5886 return NULL;
5887}
5888
5889/** Create a named output
5890 *
5891 * \param compositor The compositor.
5892 * \param name The name for the output.
5893 * \return A new \c weston_output, or NULL on failure.
5894 *
5895 * This creates a new weston_output that starts with no heads attached.
5896 *
5897 * An output must be configured and it must have at least one head before
5898 * it can be enabled.
5899 *
5900 * \memberof weston_compositor
5901 */
5902WL_EXPORT struct weston_output *
5903weston_compositor_create_output(struct weston_compositor *compositor,
5904 const char *name)
5905{
5906 assert(compositor->backend->create_output);
5907
5908 if (weston_compositor_find_output_by_name(compositor, name)) {
5909 weston_log("Warning: attempted to create an output with a "
5910 "duplicate name '%s'.\n", name);
5911 return NULL;
5912 }
5913
5914 return compositor->backend->create_output(compositor, name);
5915}
5916
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005917/** Create an output for an unused head
5918 *
5919 * \param compositor The compositor.
5920 * \param head The head to attach to the output.
5921 * \return A new \c weston_output, or NULL on failure.
5922 *
5923 * This creates a new weston_output that starts with the given head attached.
5924 * The output inherits the name of the head. The head must not be already
5925 * attached to another output.
5926 *
5927 * An output must be configured before it can be enabled.
5928 *
5929 * \memberof weston_compositor
5930 */
5931WL_EXPORT struct weston_output *
5932weston_compositor_create_output_with_head(struct weston_compositor *compositor,
5933 struct weston_head *head)
5934{
5935 struct weston_output *output;
5936
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02005937 output = weston_compositor_create_output(compositor, head->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005938 if (!output)
5939 return NULL;
5940
5941 if (weston_output_attach_head(output, head) < 0) {
Pekka Paalanen42c0e142017-10-27 12:07:49 +03005942 weston_output_destroy(output);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005943 return NULL;
5944 }
5945
5946 return output;
5947}
5948
5949/** Destroy an output
5950 *
5951 * \param output The output to destroy.
5952 *
5953 * The heads attached to the given output are detached and become unused again.
5954 *
5955 * It is not necessary to explicitly destroy all outputs at compositor exit.
5956 * weston_compositor_destroy() will automatically destroy any remaining
5957 * outputs.
5958 *
5959 * \memberof weston_output
5960 */
5961WL_EXPORT void
5962weston_output_destroy(struct weston_output *output)
5963{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005964 output->destroy(output);
5965}
5966
Pekka Paalanencf0a4762017-04-04 16:36:07 +03005967/** When you need a head...
5968 *
5969 * This function is a hack, used until all code has been converted to become
5970 * multi-head aware.
5971 *
5972 * \param output The weston_output whose head to get.
5973 * \return The first head in the output's list.
5974 */
5975WL_EXPORT struct weston_head *
5976weston_output_get_first_head(struct weston_output *output)
5977{
5978 if (wl_list_empty(&output->head_list))
5979 return NULL;
5980
5981 return container_of(output->head_list.next,
5982 struct weston_head, output_link);
5983}
5984
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01005985static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005986destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01005987{
Jonny Lamb74130762013-11-26 18:19:46 +01005988 struct weston_surface *surface =
5989 wl_resource_get_user_data(resource);
5990
Pekka Paalanen4826f872016-04-22 14:14:38 +03005991 if (!surface)
5992 return;
5993
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005994 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02005995 surface->pending.buffer_viewport.buffer.src_width =
5996 wl_fixed_from_int(-1);
5997 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02005998 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01005999}
6000
6001static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006002viewport_destroy(struct wl_client *client,
6003 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006004{
6005 wl_resource_destroy(resource);
6006}
6007
6008static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006009viewport_set_source(struct wl_client *client,
6010 struct wl_resource *resource,
6011 wl_fixed_t src_x,
6012 wl_fixed_t src_y,
6013 wl_fixed_t src_width,
6014 wl_fixed_t src_height)
6015{
6016 struct weston_surface *surface =
6017 wl_resource_get_user_data(resource);
6018
Pekka Paalanen4826f872016-04-22 14:14:38 +03006019 if (!surface) {
6020 wl_resource_post_error(resource,
6021 WP_VIEWPORT_ERROR_NO_SURFACE,
6022 "wl_surface for this viewport is no longer exists");
6023 return;
6024 }
6025
6026 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03006027 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006028
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006029 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03006030 src_height == wl_fixed_from_int(-1) &&
6031 src_x == wl_fixed_from_int(-1) &&
6032 src_y == wl_fixed_from_int(-1)) {
6033 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006034 surface->pending.buffer_viewport.buffer.src_width =
6035 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006036 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006037 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006038 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006039
Pekka Paalanen201769a2016-04-26 14:42:11 +03006040 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006041 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006042 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03006043 "wl_surface@%d viewport source "
6044 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
6045 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006046 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03006047 wl_fixed_to_double(src_height),
6048 wl_fixed_to_double(src_x),
6049 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006050 return;
6051 }
6052
6053 surface->pending.buffer_viewport.buffer.src_x = src_x;
6054 surface->pending.buffer_viewport.buffer.src_y = src_y;
6055 surface->pending.buffer_viewport.buffer.src_width = src_width;
6056 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006057 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006058}
6059
6060static void
6061viewport_set_destination(struct wl_client *client,
6062 struct wl_resource *resource,
6063 int32_t dst_width,
6064 int32_t dst_height)
6065{
6066 struct weston_surface *surface =
6067 wl_resource_get_user_data(resource);
6068
Pekka Paalanen4826f872016-04-22 14:14:38 +03006069 if (!surface) {
6070 wl_resource_post_error(resource,
6071 WP_VIEWPORT_ERROR_NO_SURFACE,
6072 "wl_surface for this viewport no longer exists");
6073 return;
6074 }
6075
6076 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006077
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006078 if (dst_width == -1 && dst_height == -1) {
6079 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006080 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006081 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006082 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006083 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006084
6085 if (dst_width <= 0 || dst_height <= 0) {
6086 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006087 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006088 "destination size must be positive (%dx%d)",
6089 dst_width, dst_height);
6090 return;
6091 }
6092
6093 surface->pending.buffer_viewport.surface.width = dst_width;
6094 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006095 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006096}
6097
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006098static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006099 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006100 viewport_set_source,
6101 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01006102};
6103
6104static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006105viewporter_destroy(struct wl_client *client,
6106 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006107{
6108 wl_resource_destroy(resource);
6109}
6110
6111static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006112viewporter_get_viewport(struct wl_client *client,
6113 struct wl_resource *viewporter,
6114 uint32_t id,
6115 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006116{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006117 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006118 struct weston_surface *surface =
6119 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006120 struct wl_resource *resource;
6121
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006122 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006123 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006124 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006125 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01006126 return;
6127 }
6128
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006129 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006130 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006131 if (resource == NULL) {
6132 wl_client_post_no_memory(client);
6133 return;
6134 }
6135
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006136 wl_resource_set_implementation(resource, &viewport_interface,
6137 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01006138
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006139 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006140}
6141
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006142static const struct wp_viewporter_interface viewporter_interface = {
6143 viewporter_destroy,
6144 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01006145};
6146
6147static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006148bind_viewporter(struct wl_client *client,
6149 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006150{
6151 struct wl_resource *resource;
6152
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006153 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006154 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006155 if (resource == NULL) {
6156 wl_client_post_no_memory(client);
6157 return;
6158 }
6159
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006160 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01006161 NULL, NULL);
6162}
6163
6164static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04006165destroy_presentation_feedback(struct wl_resource *feedback_resource)
6166{
6167 struct weston_presentation_feedback *feedback;
6168
6169 feedback = wl_resource_get_user_data(feedback_resource);
6170
6171 wl_list_remove(&feedback->link);
6172 free(feedback);
6173}
6174
6175static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006176presentation_destroy(struct wl_client *client, struct wl_resource *resource)
6177{
6178 wl_resource_destroy(resource);
6179}
6180
6181static void
6182presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04006183 struct wl_resource *presentation_resource,
6184 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006185 uint32_t callback)
6186{
Pekka Paalanen133e4392014-09-23 22:08:46 -04006187 struct weston_surface *surface;
6188 struct weston_presentation_feedback *feedback;
6189
6190 surface = wl_resource_get_user_data(surface_resource);
6191
Bryce Harringtonde16d892014-11-20 22:21:57 -08006192 feedback = zalloc(sizeof *feedback);
6193 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04006194 goto err_calloc;
6195
6196 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006197 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04006198 1, callback);
6199 if (!feedback->resource)
6200 goto err_create;
6201
6202 wl_resource_set_implementation(feedback->resource, NULL, feedback,
6203 destroy_presentation_feedback);
6204
6205 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
6206
6207 return;
6208
6209err_create:
6210 free(feedback);
6211
6212err_calloc:
6213 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006214}
6215
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006216static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006217 presentation_destroy,
6218 presentation_feedback
6219};
6220
6221static void
6222bind_presentation(struct wl_client *client,
6223 void *data, uint32_t version, uint32_t id)
6224{
6225 struct weston_compositor *compositor = data;
6226 struct wl_resource *resource;
6227
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006228 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006229 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006230 if (resource == NULL) {
6231 wl_client_post_no_memory(client);
6232 return;
6233 }
6234
6235 wl_resource_set_implementation(resource, &presentation_implementation,
6236 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006237 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006238}
6239
6240static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05006241compositor_bind(struct wl_client *client,
6242 void *data, uint32_t version, uint32_t id)
6243{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006244 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05006245 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05006246
Jason Ekstranda85118c2013-06-27 20:17:02 -05006247 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006248 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07006249 if (resource == NULL) {
6250 wl_client_post_no_memory(client);
6251 return;
6252 }
6253
6254 wl_resource_set_implementation(resource, &compositor_interface,
6255 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05006256}
6257
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006258WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006259weston_environment_get_fd(const char *env)
6260{
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07006261 char *e;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006262 int fd, flags;
6263
6264 e = getenv(env);
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07006265 if (!e || !safe_strtoint(e, &fd))
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006266 return -1;
6267
6268 flags = fcntl(fd, F_GETFD);
6269 if (flags == -1)
6270 return -1;
6271
6272 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
6273 unsetenv(env);
6274
6275 return fd;
6276}
6277
Pekka Paalanenb5026542014-11-12 15:09:24 +02006278static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02006279timeline_key_binding_handler(struct weston_keyboard *keyboard,
6280 const struct timespec *time, uint32_t key,
6281 void *data)
Pekka Paalanenb5026542014-11-12 15:09:24 +02006282{
6283 struct weston_compositor *compositor = data;
6284
6285 if (weston_timeline_enabled_)
6286 weston_timeline_close();
6287 else
6288 weston_timeline_open(compositor);
6289}
6290
Giulio Camuffo459137b2014-10-11 23:56:24 +03006291/** Create the compositor.
6292 *
6293 * This functions creates and initializes a compositor instance.
6294 *
6295 * \param display The Wayland display to be used.
6296 * \param user_data A pointer to an object that can later be retrieved
6297 * using the \ref weston_compositor_get_user_data function.
6298 * \return The compositor instance on success or NULL on failure.
6299 */
6300WL_EXPORT struct weston_compositor *
6301weston_compositor_create(struct wl_display *display, void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04006302{
Giulio Camuffo459137b2014-10-11 23:56:24 +03006303 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05006304 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07006305
Giulio Camuffo459137b2014-10-11 23:56:24 +03006306 ec = zalloc(sizeof *ec);
6307 if (!ec)
6308 return NULL;
6309
6310 ec->wl_display = display;
6311 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006312 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07006313 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006314 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006315 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006316 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006317 wl_signal_init(&ec->idle_signal);
6318 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006319 wl_signal_init(&ec->show_input_panel_signal);
6320 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02006321 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01006322 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01006323 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02006324 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006325 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02006326 wl_signal_init(&ec->output_resized_signal);
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03006327 wl_signal_init(&ec->heads_changed_signal);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02006328 wl_signal_init(&ec->output_heads_changed_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07006329 wl_signal_init(&ec->session_signal);
6330 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04006331
Casey Dahlin58ba1372012-04-19 22:50:08 -04006332 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03006333 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04006334
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02006335 ec->activate_serial = 1;
6336
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05006337 ec->touch_mode = WESTON_TOUCH_MODE_NORMAL;
6338
Derek Foreman152254b2015-11-26 14:17:48 -06006339 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006340 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006341 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05006342
Giulio Camuffo954f1832014-10-11 18:27:30 +03006343 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006344 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006345 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03006346
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006347 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006348 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006349 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006350
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006351 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006352 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006353 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006354
Jonas Ådahl30d61d82014-10-22 21:21:17 +02006355 if (weston_input_init(ec) != 0)
6356 goto fail;
6357
Jason Ekstranda7af7042013-10-12 22:38:11 -05006358 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02006359 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006360 wl_list_init(&ec->layer_list);
6361 wl_list_init(&ec->seat_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006362 wl_list_init(&ec->pending_output_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006363 wl_list_init(&ec->output_list);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03006364 wl_list_init(&ec->head_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006365 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01006366 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006367 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01006368 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006369 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006370 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006371
Pekka Paalanen827b5d22016-06-29 11:54:26 +02006372 wl_list_init(&ec->plugin_api_list);
6373
Xiong Zhang97116532013-10-23 13:58:31 +08006374 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02006375 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04006376
Giulio Camuffo459137b2014-10-11 23:56:24 +03006377 wl_data_device_manager_init(ec->wl_display);
6378
6379 wl_display_init_shm(ec->wl_display);
6380
6381 loop = wl_display_get_event_loop(ec->wl_display);
6382 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Daniel Stone6847b852017-03-01 11:34:08 +00006383 ec->repaint_timer =
6384 wl_event_loop_add_timer(loop, output_repaint_timer_handler,
6385 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03006386
Quentin Glidic82681572016-12-17 13:40:51 +01006387 weston_layer_init(&ec->fade_layer, ec);
6388 weston_layer_init(&ec->cursor_layer, ec);
6389
6390 weston_layer_set_position(&ec->fade_layer, WESTON_LAYER_POSITION_FADE);
6391 weston_layer_set_position(&ec->cursor_layer,
6392 WESTON_LAYER_POSITION_CURSOR);
Giulio Camuffo459137b2014-10-11 23:56:24 +03006393
6394 weston_compositor_add_debug_binding(ec, KEY_T,
6395 timeline_key_binding_handler, ec);
6396
Giulio Camuffo459137b2014-10-11 23:56:24 +03006397 return ec;
6398
6399fail:
6400 free(ec);
6401 return NULL;
6402}
6403
Benjamin Franzkeb8263022011-08-30 11:32:47 +02006404WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006405weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07006406{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006407 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07006408
Pekka Paalanend1591ae2012-01-02 16:06:56 +02006409 wl_event_source_remove(ec->idle_source);
6410
Matt Roper361d2ad2011-08-29 13:52:23 -07006411 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02006412 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07006413 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02006414
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006415 /* Destroy all pending outputs associated with this compositor */
6416 wl_list_for_each_safe(output, next, &ec->pending_output_list, link)
6417 output->destroy(output);
6418
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02006419 if (ec->renderer)
6420 ec->renderer->destroy(ec);
6421
Daniel Stone325fc2d2012-05-30 16:31:58 +01006422 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09006423 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006424 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01006425 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006426 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006427 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02006428
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04006429 weston_plane_release(&ec->primary_plane);
Matt Roper361d2ad2011-08-29 13:52:23 -07006430}
6431
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05006432WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04006433weston_compositor_exit_with_code(struct weston_compositor *compositor,
6434 int exit_code)
6435{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02006436 if (compositor->exit_code == EXIT_SUCCESS)
6437 compositor->exit_code = exit_code;
6438
Giulio Camuffo459137b2014-10-11 23:56:24 +03006439 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04006440}
6441
6442WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01006443weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
6444 const struct weston_pointer_grab_interface *interface)
6445{
6446 struct weston_seat *seat;
6447
6448 ec->default_pointer_grab = interface;
6449 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05006450 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
6451
6452 if (pointer)
6453 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01006454 }
6455}
6456
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04006457WL_EXPORT int
6458weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
6459 clockid_t clk_id)
6460{
6461 struct timespec ts;
6462
6463 if (clock_gettime(clk_id, &ts) < 0)
6464 return -1;
6465
6466 compositor->presentation_clock = clk_id;
6467
6468 return 0;
6469}
6470
6471/*
6472 * For choosing the software clock, when the display hardware or API
6473 * does not expose a compatible presentation timestamp.
6474 */
6475WL_EXPORT int
6476weston_compositor_set_presentation_clock_software(
6477 struct weston_compositor *compositor)
6478{
6479 /* In order of preference */
6480 static const clockid_t clocks[] = {
6481 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
6482 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
6483 CLOCK_MONOTONIC, /* no jumps, may crawl */
6484 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
6485 CLOCK_REALTIME /* may jump and crawl */
6486 };
6487 unsigned i;
6488
6489 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
6490 if (weston_compositor_set_presentation_clock(compositor,
6491 clocks[i]) == 0)
6492 return 0;
6493
6494 weston_log("Error: no suitable presentation clock available.\n");
6495
6496 return -1;
6497}
6498
Pekka Paalanen662f3842015-03-18 12:17:26 +02006499/** Read the current time from the Presentation clock
6500 *
6501 * \param compositor
6502 * \param ts[out] The current time.
6503 *
6504 * \note Reading the current time in user space is always imprecise to some
6505 * degree.
6506 *
6507 * This function is never meant to fail. If reading the clock does fail,
6508 * an error message is logged and a zero time is returned. Callers are not
6509 * supposed to detect or react to failures.
6510 */
6511WL_EXPORT void
6512weston_compositor_read_presentation_clock(
6513 const struct weston_compositor *compositor,
6514 struct timespec *ts)
6515{
6516 static bool warned;
6517 int ret;
6518
6519 ret = clock_gettime(compositor->presentation_clock, ts);
6520 if (ret < 0) {
6521 ts->tv_sec = 0;
6522 ts->tv_nsec = 0;
6523
6524 if (!warned)
6525 weston_log("Error: failure to read "
6526 "the presentation clock %#x: '%m' (%d)\n",
6527 compositor->presentation_clock, errno);
6528 warned = true;
6529 }
6530}
6531
Pekka Paalanen230f3b12014-09-29 14:18:40 -04006532/** Import dmabuf buffer into current renderer
6533 *
6534 * \param compositor
6535 * \param buffer the dmabuf buffer to import
6536 * \return true on usable buffers, false otherwise
6537 *
6538 * This function tests that the linux_dmabuf_buffer is usable
6539 * for the current renderer. Returns false on unusable buffers. Usually
6540 * usability is tested by importing the dmabufs for composition.
6541 *
6542 * This hook is also used for detecting if the renderer supports
6543 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
6544 * supported.
6545 * */
6546WL_EXPORT bool
6547weston_compositor_import_dmabuf(struct weston_compositor *compositor,
6548 struct linux_dmabuf_buffer *buffer)
6549{
6550 struct weston_renderer *renderer;
6551
6552 renderer = compositor->renderer;
6553
6554 if (renderer->import_dmabuf == NULL)
6555 return false;
6556
6557 return renderer->import_dmabuf(compositor, buffer);
6558}
6559
Giulio Camuffocdb4d292013-11-14 23:42:53 +01006560WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05006561weston_version(int *major, int *minor, int *micro)
6562{
6563 *major = WESTON_VERSION_MAJOR;
6564 *minor = WESTON_VERSION_MINOR;
6565 *micro = WESTON_VERSION_MICRO;
6566}
6567
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03006568WL_EXPORT void *
6569weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006570{
Derek Foreman3f86e502015-06-08 11:46:54 -05006571 const char *builddir = getenv("WESTON_BUILD_DIR");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006572 char path[PATH_MAX];
6573 void *module, *init;
Daniel Stonebeb97e52016-11-28 12:13:54 +00006574 size_t len;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006575
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08006576 if (name == NULL)
6577 return NULL;
6578
Derek Foreman3f86e502015-06-08 11:46:54 -05006579 if (name[0] != '/') {
6580 if (builddir)
Daniel Stonebeb97e52016-11-28 12:13:54 +00006581 len = snprintf(path, sizeof path, "%s/.libs/%s",
6582 builddir, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05006583 else
Daniel Stonebeb97e52016-11-28 12:13:54 +00006584 len = snprintf(path, sizeof path, "%s/%s",
6585 LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05006586 } else {
Daniel Stonebeb97e52016-11-28 12:13:54 +00006587 len = snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05006588 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006589
Daniel Stonebeb97e52016-11-28 12:13:54 +00006590 /* snprintf returns the length of the string it would've written,
6591 * _excluding_ the NUL byte. So even being equal to the size of
6592 * our buffer is an error here. */
6593 if (len >= sizeof path)
6594 return NULL;
6595
Kristian Høgsberga6813d22012-09-12 12:21:01 -04006596 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
6597 if (module) {
6598 weston_log("Module '%s' already loaded\n", path);
6599 dlclose(module);
6600 return NULL;
6601 }
6602
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03006603 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04006604 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006605 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03006606 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006607 return NULL;
6608 }
6609
6610 init = dlsym(module, entrypoint);
6611 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03006612 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00006613 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006614 return NULL;
6615 }
6616
6617 return init;
6618}
6619
Giulio Camuffo459137b2014-10-11 23:56:24 +03006620
6621/** Destroys the compositor.
6622 *
6623 * This function cleans up the compositor state and destroys it.
6624 *
6625 * \param compositor The compositor to be destroyed.
6626 */
6627WL_EXPORT void
6628weston_compositor_destroy(struct weston_compositor *compositor)
6629{
6630 /* prevent further rendering while shutting down */
6631 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
6632
6633 wl_signal_emit(&compositor->destroy_signal, compositor);
6634
6635 weston_compositor_xkb_destroy(compositor);
6636
Giulio Camuffo2d24e642015-10-03 16:25:15 +03006637 if (compositor->backend)
6638 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02006639
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03006640 /* The backend is responsible for destroying the heads. */
6641 assert(wl_list_empty(&compositor->head_list));
6642
Pekka Paalanen827b5d22016-06-29 11:54:26 +02006643 weston_plugin_api_destroy_list(compositor);
6644
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03006645 if (compositor->heads_changed_source)
6646 wl_event_source_remove(compositor->heads_changed_source);
6647
Giulio Camuffo459137b2014-10-11 23:56:24 +03006648 free(compositor);
6649}
6650
6651/** Instruct the compositor to exit.
6652 *
6653 * This functions does not directly destroy the compositor object, it merely
6654 * command it to start the tear down process. It is not guaranteed that the
6655 * tear down will happen immediately.
6656 *
6657 * \param compositor The compositor to tear down.
6658 */
6659WL_EXPORT void
6660weston_compositor_exit(struct weston_compositor *compositor)
6661{
6662 compositor->exit(compositor);
6663}
6664
6665/** Return the user data stored in the compositor.
6666 *
6667 * This function returns the user data pointer set with user_data parameter
6668 * to the \ref weston_compositor_create function.
6669 */
6670WL_EXPORT void *
6671weston_compositor_get_user_data(struct weston_compositor *compositor)
6672{
6673 return compositor->user_data;
6674}
Pekka Paalanendd186732016-06-03 14:49:54 +03006675
Pekka Paalanen50dbf382016-06-03 15:23:46 +03006676static const char * const backend_map[] = {
6677 [WESTON_BACKEND_DRM] = "drm-backend.so",
6678 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
6679 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
6680 [WESTON_BACKEND_RDP] = "rdp-backend.so",
6681 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
6682 [WESTON_BACKEND_X11] = "x11-backend.so",
6683};
6684
Pekka Paalanendd186732016-06-03 14:49:54 +03006685/** Load a backend into a weston_compositor
6686 *
6687 * A backend must be loaded to make a weston_compositor work. A backend
6688 * provides input and output capabilities, and determines the renderer to use.
6689 *
6690 * \param compositor A compositor that has not had a backend loaded yet.
6691 * \param backend Name of the backend file.
6692 * \param config_base A pointer to a backend-specific configuration
6693 * structure's 'base' member.
6694 *
6695 * \return 0 on success, or -1 on error.
6696 */
6697WL_EXPORT int
6698weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03006699 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03006700 struct weston_backend_config *config_base)
6701{
6702 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03006703 struct weston_backend_config *config_base);
6704
Pekka Paalanend7e35112017-08-29 17:04:12 +03006705 if (compositor->backend) {
6706 weston_log("Error: attempt to load a backend when one is already loaded\n");
6707 return -1;
6708 }
6709
Quentin Glidic887c0182016-07-10 11:00:53 +02006710 if (backend >= ARRAY_LENGTH(backend_map))
Pekka Paalanen50dbf382016-06-03 15:23:46 +03006711 return -1;
6712
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01006713 backend_init = weston_load_module(backend_map[backend], "weston_backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03006714 if (!backend_init)
6715 return -1;
6716
Pekka Paalanend7e35112017-08-29 17:04:12 +03006717 if (backend_init(compositor, config_base) < 0) {
6718 compositor->backend = NULL;
6719 return -1;
6720 }
6721
6722 return 0;
Pekka Paalanendd186732016-06-03 14:49:54 +03006723}
Giulio Camuffo9c764df2016-06-29 11:54:27 +02006724
6725WL_EXPORT int
6726weston_compositor_load_xwayland(struct weston_compositor *compositor)
6727{
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01006728 int (*module_init)(struct weston_compositor *ec);
Giulio Camuffo9c764df2016-06-29 11:54:27 +02006729
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01006730 module_init = weston_load_module("xwayland.so", "weston_module_init");
Giulio Camuffo9c764df2016-06-29 11:54:27 +02006731 if (!module_init)
6732 return -1;
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01006733 if (module_init(compositor) < 0)
Giulio Camuffo9c764df2016-06-29 11:54:27 +02006734 return -1;
6735 return 0;
6736}