blob: 8e01eacca02439b0c12f4416561f6089000ea463 [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
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09002453 output->repainted = false;
2454
Daniel Stone6847b852017-03-01 11:34:08 +00002455 /* We're not ready yet; come back to make a decision later. */
2456 if (output->repaint_status != REPAINT_SCHEDULED)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002457 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002458
2459 msec_to_repaint = timespec_sub_to_msec(&output->next_repaint, now);
2460 if (msec_to_repaint > 1)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002461 return ret;
Daniel Stone05df8c12017-03-03 16:59:42 +00002462
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002463 /* If we're sleeping, drop the repaint machinery entirely; we will
2464 * explicitly repaint all outputs when we come back. */
2465 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2466 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
2467 goto err;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002468
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002469 /* We don't actually need to repaint this output; drop it from
2470 * repaint until something causes damage. */
2471 if (!output->repaint_needed)
2472 goto err;
2473
2474 /* If repaint fails, we aren't going to get weston_output_finish_frame
2475 * to trigger a new repaint, so drop it from repaint and hope
Daniel Stone6847b852017-03-01 11:34:08 +00002476 * something schedules a successful repaint later. As repainting may
2477 * take some time, re-read our clock as a courtesy to the next
2478 * output. */
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002479 ret = weston_output_repaint(output, repaint_data);
Daniel Stone6847b852017-03-01 11:34:08 +00002480 weston_compositor_read_presentation_clock(compositor, now);
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002481 if (ret != 0)
2482 goto err;
2483
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09002484 output->repainted = true;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002485 return ret;
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002486
2487err:
Pekka Paalanen0513a952014-05-21 16:17:27 +03002488 weston_output_schedule_repaint_reset(output);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002489 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002490}
2491
2492static void
2493output_repaint_timer_arm(struct weston_compositor *compositor)
2494{
2495 struct weston_output *output;
2496 bool any_should_repaint = false;
2497 struct timespec now;
Sergi Granellb4c08862017-03-18 13:01:15 +01002498 int64_t msec_to_next = INT64_MAX;
Daniel Stone6847b852017-03-01 11:34:08 +00002499
2500 weston_compositor_read_presentation_clock(compositor, &now);
2501
2502 wl_list_for_each(output, &compositor->output_list, link) {
2503 int64_t msec_to_this;
2504
2505 if (output->repaint_status != REPAINT_SCHEDULED)
2506 continue;
2507
2508 msec_to_this = timespec_sub_to_msec(&output->next_repaint,
2509 &now);
2510 if (!any_should_repaint || msec_to_this < msec_to_next)
2511 msec_to_next = msec_to_this;
2512
2513 any_should_repaint = true;
2514 }
2515
2516 if (!any_should_repaint)
2517 return;
2518
2519 /* Even if we should repaint immediately, add the minimum 1 ms delay.
2520 * This is a workaround to allow coalescing multiple output repaints
2521 * particularly from weston_output_finish_frame()
2522 * into the same call, which would not happen if we called
2523 * output_repaint_timer_handler() directly.
2524 */
2525 if (msec_to_next < 1)
2526 msec_to_next = 1;
2527
2528 wl_event_source_timer_update(compositor->repaint_timer, msec_to_next);
2529}
2530
2531static int
2532output_repaint_timer_handler(void *data)
2533{
2534 struct weston_compositor *compositor = data;
2535 struct weston_output *output;
2536 struct timespec now;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002537 void *repaint_data = NULL;
Emre Ucane479ed82018-03-20 15:29:40 +01002538 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002539
2540 weston_compositor_read_presentation_clock(compositor, &now);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002541
2542 if (compositor->backend->repaint_begin)
2543 repaint_data = compositor->backend->repaint_begin(compositor);
2544
2545 wl_list_for_each(output, &compositor->output_list, link) {
2546 ret = weston_output_maybe_repaint(output, &now, repaint_data);
2547 if (ret)
2548 break;
2549 }
2550
2551 if (ret == 0) {
Tomohito Esaki09bfcd62018-06-05 10:37:05 +09002552 if (compositor->backend->repaint_flush)
2553 compositor->backend->repaint_flush(compositor,
2554 repaint_data);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002555 } else {
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09002556 wl_list_for_each(output, &compositor->output_list, link) {
2557 if (output->repainted)
2558 weston_output_schedule_repaint_reset(output);
2559 }
2560
Tomohito Esaki09bfcd62018-06-05 10:37:05 +09002561 if (compositor->backend->repaint_cancel)
2562 compositor->backend->repaint_cancel(compositor,
2563 repaint_data);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002564 }
Daniel Stone6847b852017-03-01 11:34:08 +00002565
2566 output_repaint_timer_arm(compositor);
2567
Pekka Paalanen0513a952014-05-21 16:17:27 +03002568 return 0;
2569}
2570
Kristian Høgsbergef044142011-06-21 15:02:12 -04002571WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002572weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002573 const struct timespec *stamp,
2574 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002575{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002576 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002577 int32_t refresh_nsec;
2578 struct timespec now;
Daniel Stone6847b852017-03-01 11:34:08 +00002579 int64_t msec_rel;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002580
Pekka Paalanenb5026542014-11-12 15:09:24 +02002581
Daniel Stone05df8c12017-03-03 16:59:42 +00002582 assert(output->repaint_status == REPAINT_AWAITING_COMPLETION);
Daniel Stone3615ce12017-03-01 11:34:05 +00002583 assert(stamp || (presented_flags & WP_PRESENTATION_FEEDBACK_INVALID));
2584
Daniel Stone6847b852017-03-01 11:34:08 +00002585 weston_compositor_read_presentation_clock(compositor, &now);
2586
Daniel Stone3615ce12017-03-01 11:34:05 +00002587 /* If we haven't been supplied any timestamp at all, we don't have a
2588 * timebase to work against, so any delay just wastes time. Push a
2589 * repaint as soon as possible so we can get on with it. */
Daniel Stone6847b852017-03-01 11:34:08 +00002590 if (!stamp) {
2591 output->next_repaint = now;
Daniel Stone3615ce12017-03-01 11:34:05 +00002592 goto out;
Daniel Stone6847b852017-03-01 11:34:08 +00002593 }
Daniel Stone3615ce12017-03-01 11:34:05 +00002594
Marius Vladdf9278a2018-03-06 18:56:23 +02002595 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2596 TLP_VBLANK(stamp), TLP_END);
2597
Pekka Paalanend7894d02015-07-03 15:08:53 +03002598 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002599 weston_presentation_feedback_present_list(&output->feedback_list,
2600 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002601 output->msc,
2602 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002603
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002604 output->frame_time = *stamp;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002605
Daniel Stone6847b852017-03-01 11:34:08 +00002606 timespec_add_nsec(&output->next_repaint, stamp, refresh_nsec);
2607 timespec_add_msec(&output->next_repaint, &output->next_repaint,
2608 -compositor->repaint_msec);
2609 msec_rel = timespec_sub_to_msec(&output->next_repaint, &now);
Daniel Stone84aff5c2017-03-01 11:34:04 +00002610
2611 if (msec_rel < -1000 || msec_rel > 1000) {
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002612 static bool warned;
2613
2614 if (!warned)
2615 weston_log("Warning: computed repaint delay is "
Daniel Stone6847b852017-03-01 11:34:08 +00002616 "insane: %lld msec\n", (long long) msec_rel);
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002617 warned = true;
2618
Daniel Stone6847b852017-03-01 11:34:08 +00002619 output->next_repaint = now;
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002620 }
2621
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002622 /* Called from restart_repaint_loop and restart happens already after
2623 * the deadline given by repaint_msec? In that case we delay until
2624 * the deadline of the next frame, to give clients a more predictable
2625 * timing of the repaint cycle to lock on. */
Daniel Stoneeca5cca2017-02-28 21:53:51 +00002626 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID &&
2627 msec_rel < 0) {
2628 while (timespec_sub_to_nsec(&output->next_repaint, &now) < 0) {
2629 timespec_add_nsec(&output->next_repaint,
2630 &output->next_repaint,
2631 refresh_nsec);
2632 }
2633 }
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002634
Daniel Stone3615ce12017-03-01 11:34:05 +00002635out:
Daniel Stone05df8c12017-03-03 16:59:42 +00002636 output->repaint_status = REPAINT_SCHEDULED;
Daniel Stone6847b852017-03-01 11:34:08 +00002637 output_repaint_timer_arm(compositor);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002638}
2639
2640static void
2641idle_repaint(void *data)
2642{
2643 struct weston_output *output = data;
2644
Daniel Stone05df8c12017-03-03 16:59:42 +00002645 assert(output->repaint_status == REPAINT_BEGIN_FROM_IDLE);
2646 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03002647 output->idle_repaint_source = NULL;
Jonas Ådahle5a12252013-04-05 23:07:11 +02002648 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002649}
2650
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002651WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002652weston_layer_entry_insert(struct weston_layer_entry *list,
2653 struct weston_layer_entry *entry)
2654{
2655 wl_list_insert(&list->link, &entry->link);
2656 entry->layer = list->layer;
2657}
2658
2659WL_EXPORT void
2660weston_layer_entry_remove(struct weston_layer_entry *entry)
2661{
2662 wl_list_remove(&entry->link);
2663 wl_list_init(&entry->link);
2664 entry->layer = NULL;
2665}
2666
Quentin Glidic82681572016-12-17 13:40:51 +01002667
2668/** Initialize the weston_layer struct.
2669 *
2670 * \param compositor The compositor instance
2671 * \param layer The layer to initialize
2672 */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002673WL_EXPORT void
Quentin Glidic82681572016-12-17 13:40:51 +01002674weston_layer_init(struct weston_layer *layer,
2675 struct weston_compositor *compositor)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002676{
Quentin Glidic82681572016-12-17 13:40:51 +01002677 layer->compositor = compositor;
2678 wl_list_init(&layer->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002679 wl_list_init(&layer->view_list.link);
2680 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002681 weston_layer_set_mask_infinite(layer);
Quentin Glidic82681572016-12-17 13:40:51 +01002682}
2683
2684/** Sets the position of the layer in the layer list. The layer will be placed
2685 * below any layer with the same position value, if any.
2686 * This function is safe to call if the layer is already on the list, but the
2687 * layer may be moved below other layers at the same position, if any.
2688 *
2689 * \param layer The layer to modify
2690 * \param position The position the layer will be placed at
2691 */
2692WL_EXPORT void
2693weston_layer_set_position(struct weston_layer *layer,
2694 enum weston_layer_position position)
2695{
2696 struct weston_layer *below;
2697
2698 wl_list_remove(&layer->link);
2699
2700 /* layer_list is ordered from top to bottom, the last layer being the
2701 * background with the smallest position value */
2702
2703 layer->position = position;
2704 wl_list_for_each_reverse(below, &layer->compositor->layer_list, link) {
2705 if (below->position >= layer->position) {
2706 wl_list_insert(&below->link, &layer->link);
2707 return;
2708 }
2709 }
2710 wl_list_insert(&layer->compositor->layer_list, &layer->link);
2711}
2712
2713/** Hide a layer by taking it off the layer list.
2714 * This function is safe to call if the layer is not on the list.
2715 *
2716 * \param layer The layer to hide
2717 */
2718WL_EXPORT void
2719weston_layer_unset_position(struct weston_layer *layer)
2720{
2721 wl_list_remove(&layer->link);
2722 wl_list_init(&layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002723}
2724
2725WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002726weston_layer_set_mask(struct weston_layer *layer,
2727 int x, int y, int width, int height)
2728{
2729 struct weston_view *view;
2730
2731 layer->mask.x1 = x;
2732 layer->mask.x2 = x + width;
2733 layer->mask.y1 = y;
2734 layer->mask.y2 = y + height;
2735
2736 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2737 weston_view_geometry_dirty(view);
2738 }
2739}
2740
2741WL_EXPORT void
2742weston_layer_set_mask_infinite(struct weston_layer *layer)
2743{
2744 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2745 UINT32_MAX, UINT32_MAX);
2746}
2747
2748WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002749weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002750{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002751 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002752 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002753
Bryce Harrington08976ac2016-08-30 12:05:16 -07002754 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2755 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002756 return;
2757
Pekka Paalanenb5026542014-11-12 15:09:24 +02002758 if (!output->repaint_needed)
2759 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2760
Kristian Høgsbergef044142011-06-21 15:02:12 -04002761 loop = wl_display_get_event_loop(compositor->wl_display);
Daniel Stone09a97e22017-03-01 11:34:06 +00002762 output->repaint_needed = true;
Daniel Stone05df8c12017-03-03 16:59:42 +00002763
2764 /* If we already have a repaint scheduled for our idle handler,
2765 * no need to set it again. If the repaint has been called but
2766 * not finished, then weston_output_finish_frame() will notice
2767 * that a repaint is needed and schedule one. */
2768 if (output->repaint_status != REPAINT_NOT_SCHEDULED)
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002769 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002770
Daniel Stone05df8c12017-03-03 16:59:42 +00002771 output->repaint_status = REPAINT_BEGIN_FROM_IDLE;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03002772 assert(!output->idle_repaint_source);
2773 output->idle_repaint_source = wl_event_loop_add_idle(loop, idle_repaint,
2774 output);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002775 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002776}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002777
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002778WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002779weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2780{
2781 struct weston_output *output;
2782
2783 wl_list_for_each(output, &compositor->output_list, link)
2784 weston_output_schedule_repaint(output);
2785}
2786
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002787static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002788surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002789{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002790 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002791}
2792
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002793static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002794surface_attach(struct wl_client *client,
2795 struct wl_resource *resource,
2796 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2797{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002798 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002799 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002800
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002801 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002802 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002803 if (buffer == NULL) {
2804 wl_client_post_no_memory(client);
2805 return;
2806 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002807 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002808
Pekka Paalanende685b82012-12-04 15:58:12 +02002809 /* Attach, attach, without commit in between does not send
2810 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002811 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002812
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002813 surface->pending.sx = sx;
2814 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002815 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002816}
2817
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002818static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002819surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002820 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002821 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002822{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002823 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002824
Derek Foreman57e92ed2015-11-17 14:11:35 -06002825 if (width <= 0 || height <= 0)
2826 return;
2827
Derek Foreman152254b2015-11-26 14:17:48 -06002828 pixman_region32_union_rect(&surface->pending.damage_surface,
2829 &surface->pending.damage_surface,
2830 x, y, width, height);
2831}
2832
2833static void
2834surface_damage_buffer(struct wl_client *client,
2835 struct wl_resource *resource,
2836 int32_t x, int32_t y, int32_t width, int32_t height)
2837{
2838 struct weston_surface *surface = wl_resource_get_user_data(resource);
2839
2840 if (width <= 0 || height <= 0)
2841 return;
2842
2843 pixman_region32_union_rect(&surface->pending.damage_buffer,
2844 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002845 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002846}
2847
Kristian Høgsberg33418202011-08-16 23:01:28 -04002848static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002849destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002850{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002851 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002852
2853 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002854 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002855}
2856
2857static void
2858surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002859 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002860{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002861 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002862 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002863
2864 cb = malloc(sizeof *cb);
2865 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002866 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002867 return;
2868 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002869
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002870 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2871 callback);
2872 if (cb->resource == NULL) {
2873 free(cb);
2874 wl_resource_post_no_memory(resource);
2875 return;
2876 }
2877
Jason Ekstranda85118c2013-06-27 20:17:02 -05002878 wl_resource_set_implementation(cb->resource, NULL, cb,
2879 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002880
Pekka Paalanenbc106382012-10-10 12:49:31 +03002881 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002882}
2883
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002884static void
2885surface_set_opaque_region(struct wl_client *client,
2886 struct wl_resource *resource,
2887 struct wl_resource *region_resource)
2888{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002889 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002890 struct weston_region *region;
2891
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002892 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002893 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002894 pixman_region32_copy(&surface->pending.opaque,
2895 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002896 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002897 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002898 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002899}
2900
2901static void
2902surface_set_input_region(struct wl_client *client,
2903 struct wl_resource *resource,
2904 struct wl_resource *region_resource)
2905{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002906 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002907 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002908
2909 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002910 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002911 pixman_region32_copy(&surface->pending.input,
2912 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002913 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002914 pixman_region32_fini(&surface->pending.input);
2915 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002916 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002917}
2918
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002919/* Cause damage to this sub-surface and all its children.
2920 *
2921 * This is useful when there are state changes that need an implicit
2922 * damage, e.g. a z-order change.
2923 */
2924static void
2925weston_surface_damage_subsurfaces(struct weston_subsurface *sub)
2926{
2927 struct weston_subsurface *child;
2928
2929 weston_surface_damage(sub->surface);
2930 sub->reordered = false;
2931
2932 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link)
2933 if (child != sub)
2934 weston_surface_damage_subsurfaces(child);
2935}
2936
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002937static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002938weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002939{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002940 struct weston_subsurface *sub;
2941
2942 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2943 parent_link_pending) {
2944 wl_list_remove(&sub->parent_link);
2945 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002946
2947 if (sub->reordered)
2948 weston_surface_damage_subsurfaces(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002949 }
2950}
2951
2952static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03002953weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05002954 struct weston_matrix *matrix)
2955{
Pekka Paalanen04baea52016-04-26 15:50:58 +03002956 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05002957 double src_width, src_height, dest_width, dest_height;
2958
2959 weston_matrix_init(matrix);
2960
2961 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
2962 src_width = surface->width_from_buffer;
2963 src_height = surface->height_from_buffer;
2964 } else {
2965 src_width = wl_fixed_to_double(vp->buffer.src_width);
2966 src_height = wl_fixed_to_double(vp->buffer.src_height);
2967 }
2968
2969 if (vp->surface.width == -1) {
2970 dest_width = src_width;
2971 dest_height = src_height;
2972 } else {
2973 dest_width = vp->surface.width;
2974 dest_height = vp->surface.height;
2975 }
2976
2977 if (src_width != dest_width || src_height != dest_height)
2978 weston_matrix_scale(matrix,
2979 src_width / dest_width,
2980 src_height / dest_height, 1);
2981
2982 if (vp->buffer.src_width != wl_fixed_from_int(-1))
2983 weston_matrix_translate(matrix,
2984 wl_fixed_to_double(vp->buffer.src_x),
2985 wl_fixed_to_double(vp->buffer.src_y),
2986 0);
2987
2988 switch (vp->buffer.transform) {
2989 case WL_OUTPUT_TRANSFORM_FLIPPED:
2990 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2991 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2992 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2993 weston_matrix_scale(matrix, -1, 1, 1);
2994 weston_matrix_translate(matrix,
2995 surface->width_from_buffer, 0, 0);
2996 break;
2997 }
2998
2999 switch (vp->buffer.transform) {
3000 default:
3001 case WL_OUTPUT_TRANSFORM_NORMAL:
3002 case WL_OUTPUT_TRANSFORM_FLIPPED:
3003 break;
3004 case WL_OUTPUT_TRANSFORM_90:
3005 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3006 weston_matrix_rotate_xy(matrix, 0, 1);
3007 weston_matrix_translate(matrix,
3008 surface->height_from_buffer, 0, 0);
3009 break;
3010 case WL_OUTPUT_TRANSFORM_180:
3011 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3012 weston_matrix_rotate_xy(matrix, -1, 0);
3013 weston_matrix_translate(matrix,
3014 surface->width_from_buffer,
3015 surface->height_from_buffer, 0);
3016 break;
3017 case WL_OUTPUT_TRANSFORM_270:
3018 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3019 weston_matrix_rotate_xy(matrix, 0, -1);
3020 weston_matrix_translate(matrix,
3021 0, surface->width_from_buffer, 0);
3022 break;
3023 }
3024
3025 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
3026}
3027
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003028/**
3029 * Compute a + b > c while being safe to overflows.
3030 */
3031static bool
3032fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
3033{
3034 return (int64_t)a + (int64_t)b > (int64_t)c;
3035}
3036
3037static bool
3038weston_surface_is_pending_viewport_source_valid(
3039 const struct weston_surface *surface)
3040{
3041 const struct weston_surface_state *pend = &surface->pending;
3042 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
3043 int width_from_buffer = 0;
3044 int height_from_buffer = 0;
3045 wl_fixed_t w;
3046 wl_fixed_t h;
3047
3048 /* If viewport source rect is not set, it is always ok. */
3049 if (vp->buffer.src_width == wl_fixed_from_int(-1))
3050 return true;
3051
3052 if (pend->newly_attached) {
3053 if (pend->buffer) {
3054 convert_size_by_transform_scale(&width_from_buffer,
3055 &height_from_buffer,
3056 pend->buffer->width,
3057 pend->buffer->height,
3058 vp->buffer.transform,
3059 vp->buffer.scale);
3060 } else {
3061 /* No buffer: viewport is irrelevant. */
3062 return true;
3063 }
3064 } else {
3065 width_from_buffer = surface->width_from_buffer;
3066 height_from_buffer = surface->height_from_buffer;
3067 }
3068
3069 assert((width_from_buffer == 0) == (height_from_buffer == 0));
3070 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
3071
3072 /* No buffer: viewport is irrelevant. */
3073 if (width_from_buffer == 0 || height_from_buffer == 0)
3074 return true;
3075
3076 /* overflow checks for wl_fixed_from_int() */
3077 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
3078 return false;
3079 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
3080 return false;
3081
3082 w = wl_fixed_from_int(width_from_buffer);
3083 h = wl_fixed_from_int(height_from_buffer);
3084
3085 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
3086 return false;
3087 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
3088 return false;
3089
3090 return true;
3091}
3092
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003093static bool
3094fixed_is_integer(wl_fixed_t v)
3095{
3096 return (v & 0xff) == 0;
3097}
3098
3099static bool
3100weston_surface_is_pending_viewport_dst_size_int(
3101 const struct weston_surface *surface)
3102{
3103 const struct weston_buffer_viewport *vp =
3104 &surface->pending.buffer_viewport;
3105
3106 if (vp->surface.width != -1) {
3107 assert(vp->surface.width > 0 && vp->surface.height > 0);
3108 return true;
3109 }
3110
3111 return fixed_is_integer(vp->buffer.src_width) &&
3112 fixed_is_integer(vp->buffer.src_height);
3113}
3114
Derek Foreman152254b2015-11-26 14:17:48 -06003115/* Translate pending damage in buffer co-ordinates to surface
3116 * co-ordinates and union it with a pixman_region32_t.
3117 * This should only be called after the buffer is attached.
3118 */
3119static void
3120apply_damage_buffer(pixman_region32_t *dest,
3121 struct weston_surface *surface,
3122 struct weston_surface_state *state)
3123{
3124 struct weston_buffer *buffer = surface->buffer_ref.buffer;
3125
3126 /* wl_surface.damage_buffer needs to be clipped to the buffer,
3127 * translated into surface co-ordinates and unioned with
3128 * any other surface damage.
3129 * None of this makes sense if there is no buffer though.
3130 */
3131 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
3132 pixman_region32_t buffer_damage;
3133
3134 pixman_region32_intersect_rect(&state->damage_buffer,
3135 &state->damage_buffer,
3136 0, 0, buffer->width,
3137 buffer->height);
3138 pixman_region32_init(&buffer_damage);
3139 weston_matrix_transform_region(&buffer_damage,
3140 &surface->buffer_to_surface_matrix,
3141 &state->damage_buffer);
3142 pixman_region32_union(dest, dest, &buffer_damage);
3143 pixman_region32_fini(&buffer_damage);
3144 }
3145 /* We should clear this on commit even if there was no buffer */
3146 pixman_region32_clear(&state->damage_buffer);
3147}
3148
Jason Ekstrand1e059042014-10-16 10:55:19 -05003149static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05003150weston_surface_commit_state(struct weston_surface *surface,
3151 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003152{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003153 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003154 pixman_region32_t opaque;
3155
Alexander Larsson4ea95522013-05-22 14:41:37 +02003156 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02003157 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03003158 /* wp_viewport.set_source */
3159 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003160 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003161
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003162 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003163 if (state->newly_attached)
3164 weston_surface_attach(surface, state->buffer);
3165 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01003166
Jason Ekstrand1e059042014-10-16 10:55:19 -05003167 weston_surface_build_buffer_matrix(surface,
3168 &surface->surface_to_buffer_matrix);
3169 weston_matrix_invert(&surface->buffer_to_surface_matrix,
3170 &surface->surface_to_buffer_matrix);
3171
Jason Ekstrand7b982072014-05-20 14:33:03 -05003172 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003173 weston_surface_update_size(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003174 if (surface->committed)
3175 surface->committed(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003176 }
Giulio Camuffo184df502013-02-21 11:29:21 +01003177
Jason Ekstrand7b982072014-05-20 14:33:03 -05003178 state->sx = 0;
3179 state->sy = 0;
3180 state->newly_attached = 0;
3181 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03003182
Derek Foreman152254b2015-11-26 14:17:48 -06003183 /* wl_surface.damage and wl_surface.damage_buffer */
Pekka Paalanenb5026542014-11-12 15:09:24 +02003184 if (weston_timeline_enabled_ &&
Derek Foreman152254b2015-11-26 14:17:48 -06003185 (pixman_region32_not_empty(&state->damage_surface) ||
3186 pixman_region32_not_empty(&state->damage_buffer)))
Pekka Paalanenb5026542014-11-12 15:09:24 +02003187 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06003188
Pekka Paalanen8e159182012-10-10 12:49:25 +03003189 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06003190 &state->damage_surface);
3191
3192 apply_damage_buffer(&surface->damage, surface, state);
3193
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05003194 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07003195 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06003196 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003197
3198 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003199 pixman_region32_init(&opaque);
3200 pixman_region32_intersect_rect(&opaque, &state->opaque,
3201 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003202
3203 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
3204 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003205 wl_list_for_each(view, &surface->views, surface_link)
3206 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003207 }
3208
3209 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003210
3211 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003212 pixman_region32_intersect_rect(&surface->input, &state->input,
3213 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003214
Pekka Paalanenbc106382012-10-10 12:49:31 +03003215 /* wl_surface.frame */
3216 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05003217 &state->frame_callback_list);
3218 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003219
3220 /* XXX:
3221 * What should happen with a feedback request, if there
3222 * is no wl_buffer attached for this commit?
3223 */
3224
3225 /* presentation.feedback */
3226 wl_list_insert_list(&surface->feedback_list,
3227 &state->feedback_list);
3228 wl_list_init(&state->feedback_list);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003229
3230 wl_signal_emit(&surface->commit_signal, surface);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003231}
3232
3233static void
3234weston_surface_commit(struct weston_surface *surface)
3235{
3236 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003237
Pekka Paalanene67858b2013-04-25 13:57:42 +03003238 weston_surface_commit_subsurface_order(surface);
3239
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003240 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003241}
3242
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003243static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003244weston_subsurface_commit(struct weston_subsurface *sub);
3245
3246static void
3247weston_subsurface_parent_commit(struct weston_subsurface *sub,
3248 int parent_is_synchronized);
3249
3250static void
3251surface_commit(struct wl_client *client, struct wl_resource *resource)
3252{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003253 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003254 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
3255
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003256 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
3257 assert(surface->viewport_resource);
3258
3259 wl_resource_post_error(surface->viewport_resource,
3260 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
3261 "wl_surface@%d has viewport source outside buffer",
3262 wl_resource_get_id(resource));
3263 return;
3264 }
3265
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003266 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
3267 assert(surface->viewport_resource);
3268
3269 wl_resource_post_error(surface->viewport_resource,
3270 WP_VIEWPORT_ERROR_BAD_SIZE,
3271 "wl_surface@%d viewport dst size not integer",
3272 wl_resource_get_id(resource));
3273 return;
3274 }
3275
Pekka Paalanene67858b2013-04-25 13:57:42 +03003276 if (sub) {
3277 weston_subsurface_commit(sub);
3278 return;
3279 }
3280
3281 weston_surface_commit(surface);
3282
3283 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
3284 if (sub->surface != surface)
3285 weston_subsurface_parent_commit(sub, 0);
3286 }
3287}
3288
3289static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003290surface_set_buffer_transform(struct wl_client *client,
3291 struct wl_resource *resource, int transform)
3292{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003293 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003294
Jonny Lamba55f1392014-05-30 12:07:15 +02003295 /* if wl_output.transform grows more members this will need to be updated. */
3296 if (transform < 0 ||
3297 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
3298 wl_resource_post_error(resource,
3299 WL_SURFACE_ERROR_INVALID_TRANSFORM,
3300 "buffer transform must be a valid transform "
3301 "('%d' specified)", transform);
3302 return;
3303 }
3304
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003305 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003306 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003307}
3308
Alexander Larsson4ea95522013-05-22 14:41:37 +02003309static void
3310surface_set_buffer_scale(struct wl_client *client,
3311 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003312 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003313{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003314 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003315
Jonny Lamba55f1392014-05-30 12:07:15 +02003316 if (scale < 1) {
3317 wl_resource_post_error(resource,
3318 WL_SURFACE_ERROR_INVALID_SCALE,
3319 "buffer scale must be at least one "
3320 "('%d' specified)", scale);
3321 return;
3322 }
3323
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003324 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003325 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003326}
3327
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003328static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003329 surface_destroy,
3330 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04003331 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003332 surface_frame,
3333 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003334 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003335 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02003336 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06003337 surface_set_buffer_scale,
3338 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003339};
3340
3341static void
3342compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003343 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003344{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04003345 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003346 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003347
Kristian Høgsberg18c93002012-01-27 11:58:31 -05003348 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003349 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003350 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003351 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003352 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003353
Jason Ekstranda85118c2013-06-27 20:17:02 -05003354 surface->resource =
3355 wl_resource_create(client, &wl_surface_interface,
3356 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003357 if (surface->resource == NULL) {
3358 weston_surface_destroy(surface);
3359 wl_resource_post_no_memory(resource);
3360 return;
3361 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003362 wl_resource_set_implementation(surface->resource, &surface_interface,
3363 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003364
3365 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003366}
3367
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003368static void
3369destroy_region(struct wl_resource *resource)
3370{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003371 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003372
3373 pixman_region32_fini(&region->region);
3374 free(region);
3375}
3376
3377static void
3378region_destroy(struct wl_client *client, struct wl_resource *resource)
3379{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003380 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003381}
3382
3383static void
3384region_add(struct wl_client *client, struct wl_resource *resource,
3385 int32_t x, int32_t y, int32_t width, int32_t height)
3386{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003387 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003388
3389 pixman_region32_union_rect(&region->region, &region->region,
3390 x, y, width, height);
3391}
3392
3393static void
3394region_subtract(struct wl_client *client, struct wl_resource *resource,
3395 int32_t x, int32_t y, int32_t width, int32_t height)
3396{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003397 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003398 pixman_region32_t rect;
3399
3400 pixman_region32_init_rect(&rect, x, y, width, height);
3401 pixman_region32_subtract(&region->region, &region->region, &rect);
3402 pixman_region32_fini(&rect);
3403}
3404
3405static const struct wl_region_interface region_interface = {
3406 region_destroy,
3407 region_add,
3408 region_subtract
3409};
3410
3411static void
3412compositor_create_region(struct wl_client *client,
3413 struct wl_resource *resource, uint32_t id)
3414{
3415 struct weston_region *region;
3416
3417 region = malloc(sizeof *region);
3418 if (region == NULL) {
3419 wl_resource_post_no_memory(resource);
3420 return;
3421 }
3422
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003423 pixman_region32_init(&region->region);
3424
Jason Ekstranda85118c2013-06-27 20:17:02 -05003425 region->resource =
3426 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003427 if (region->resource == NULL) {
3428 free(region);
3429 wl_resource_post_no_memory(resource);
3430 return;
3431 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003432 wl_resource_set_implementation(region->resource, &region_interface,
3433 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003434}
3435
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003436static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003437 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003438 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003439};
3440
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003441static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003442weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3443{
3444 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003445
Jason Ekstrand7b982072014-05-20 14:33:03 -05003446 weston_surface_commit_state(surface, &sub->cached);
3447 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003448
3449 weston_surface_commit_subsurface_order(surface);
3450
3451 weston_surface_schedule_repaint(surface);
3452
Jason Ekstrand7b982072014-05-20 14:33:03 -05003453 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003454}
3455
3456static void
3457weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3458{
3459 struct weston_surface *surface = sub->surface;
3460
3461 /*
3462 * If this commit would cause the surface to move by the
3463 * attach(dx, dy) parameters, the old damage region must be
3464 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05003465 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003466 */
Derek Foreman152254b2015-11-26 14:17:48 -06003467 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003468 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06003469 pixman_region32_union(&sub->cached.damage_surface,
3470 &sub->cached.damage_surface,
3471 &surface->pending.damage_surface);
3472 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003473
3474 if (surface->pending.newly_attached) {
3475 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003476 weston_surface_state_set_buffer(&sub->cached,
3477 surface->pending.buffer);
3478 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003479 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003480 weston_presentation_feedback_discard_list(
3481 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003482 }
3483 sub->cached.sx += surface->pending.sx;
3484 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003485
Derek Foreman152254b2015-11-26 14:17:48 -06003486 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
3487
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003488 sub->cached.buffer_viewport.changed |=
3489 surface->pending.buffer_viewport.changed;
3490 sub->cached.buffer_viewport.buffer =
3491 surface->pending.buffer_viewport.buffer;
3492 sub->cached.buffer_viewport.surface =
3493 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003494
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003495 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003496
3497 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3498
3499 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3500
3501 wl_list_insert_list(&sub->cached.frame_callback_list,
3502 &surface->pending.frame_callback_list);
3503 wl_list_init(&surface->pending.frame_callback_list);
3504
Pekka Paalanen133e4392014-09-23 22:08:46 -04003505 wl_list_insert_list(&sub->cached.feedback_list,
3506 &surface->pending.feedback_list);
3507 wl_list_init(&surface->pending.feedback_list);
3508
Jason Ekstrand7b982072014-05-20 14:33:03 -05003509 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003510}
3511
Derek Foreman280e7dd2014-10-03 13:13:42 -05003512static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003513weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3514{
3515 while (sub) {
3516 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003517 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003518
3519 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003520 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003521
3522 sub = weston_surface_to_subsurface(sub->parent);
3523 }
3524
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003525 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003526}
3527
3528static void
3529weston_subsurface_commit(struct weston_subsurface *sub)
3530{
3531 struct weston_surface *surface = sub->surface;
3532 struct weston_subsurface *tmp;
3533
3534 /* Recursive check for effectively synchronized. */
3535 if (weston_subsurface_is_synchronized(sub)) {
3536 weston_subsurface_commit_to_cache(sub);
3537 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003538 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003539 /* flush accumulated state from cache */
3540 weston_subsurface_commit_to_cache(sub);
3541 weston_subsurface_commit_from_cache(sub);
3542 } else {
3543 weston_surface_commit(surface);
3544 }
3545
3546 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3547 if (tmp->surface != surface)
3548 weston_subsurface_parent_commit(tmp, 0);
3549 }
3550 }
3551}
3552
3553static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003554weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003555{
3556 struct weston_surface *surface = sub->surface;
3557 struct weston_subsurface *tmp;
3558
Pekka Paalanene67858b2013-04-25 13:57:42 +03003559 /* From now on, commit_from_cache the whole sub-tree, regardless of
3560 * the synchronized mode of each child. This sub-surface or some
3561 * of its ancestors were synchronized, so we are synchronized
3562 * all the way down.
3563 */
3564
Jason Ekstrand7b982072014-05-20 14:33:03 -05003565 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003566 weston_subsurface_commit_from_cache(sub);
3567
3568 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3569 if (tmp->surface != surface)
3570 weston_subsurface_parent_commit(tmp, 1);
3571 }
3572}
3573
3574static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003575weston_subsurface_parent_commit(struct weston_subsurface *sub,
3576 int parent_is_synchronized)
3577{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003578 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003579 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003580 wl_list_for_each(view, &sub->surface->views, surface_link)
3581 weston_view_set_position(view,
3582 sub->position.x,
3583 sub->position.y);
3584
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003585 sub->position.set = 0;
3586 }
3587
3588 if (parent_is_synchronized || sub->synchronized)
3589 weston_subsurface_synchronized_commit(sub);
3590}
3591
Pekka Paalanen8274d902014-08-06 19:36:51 +03003592static int
3593subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3594{
3595 return snprintf(buf, len, "sub-surface");
3596}
3597
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003598static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003599subsurface_committed(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003600{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003601 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003602
Jason Ekstranda7af7042013-10-12 22:38:11 -05003603 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003604 weston_view_set_position(view,
3605 view->geometry.x + dx,
3606 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003607
3608 /* No need to check parent mappedness, because if parent is not
3609 * mapped, parent is not in a visible layer, so this sub-surface
3610 * will not be drawn either.
3611 */
Armin Krezovićf8486c32016-06-30 06:04:28 +02003612
Pekka Paalanene67858b2013-04-25 13:57:42 +03003613 if (!weston_surface_is_mapped(surface)) {
Armin Krezovićf8486c32016-06-30 06:04:28 +02003614 surface->is_mapped = true;
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003615
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003616 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003617 * because that would call it also for the parent surface,
3618 * which might not be mapped yet. That would lead to
3619 * inconsistent state, where the window could never be
3620 * mapped.
3621 *
Armin Krezovićf8486c32016-06-30 06:04:28 +02003622 * Instead just force the is_mapped flag on, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003623 * weston_surface_is_mapped() return true, so that when the
3624 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003625 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003626 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03003627 }
3628}
3629
3630static struct weston_subsurface *
3631weston_surface_to_subsurface(struct weston_surface *surface)
3632{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003633 if (surface->committed == subsurface_committed)
3634 return surface->committed_private;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003635
3636 return NULL;
3637}
3638
Pekka Paalanen01388e22013-04-25 13:57:44 +03003639WL_EXPORT struct weston_surface *
3640weston_surface_get_main_surface(struct weston_surface *surface)
3641{
3642 struct weston_subsurface *sub;
3643
3644 while (surface && (sub = weston_surface_to_subsurface(surface)))
3645 surface = sub->parent;
3646
3647 return surface;
3648}
3649
Pekka Paalanen50b67472014-10-01 15:02:41 +03003650WL_EXPORT int
3651weston_surface_set_role(struct weston_surface *surface,
3652 const char *role_name,
3653 struct wl_resource *error_resource,
3654 uint32_t error_code)
3655{
3656 assert(role_name);
3657
3658 if (surface->role_name == NULL ||
3659 surface->role_name == role_name ||
3660 strcmp(surface->role_name, role_name) == 0) {
3661 surface->role_name = role_name;
3662
3663 return 0;
3664 }
3665
3666 wl_resource_post_error(error_resource, error_code,
3667 "Cannot assign role %s to wl_surface@%d,"
3668 " already has role %s\n",
3669 role_name,
3670 wl_resource_get_id(surface->resource),
3671 surface->role_name);
3672 return -1;
3673}
3674
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02003675WL_EXPORT const char *
3676weston_surface_get_role(struct weston_surface *surface)
3677{
3678 return surface->role_name;
3679}
3680
Pekka Paalanen8274d902014-08-06 19:36:51 +03003681WL_EXPORT void
3682weston_surface_set_label_func(struct weston_surface *surface,
3683 int (*desc)(struct weston_surface *,
3684 char *, size_t))
3685{
3686 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02003687 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003688}
3689
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003690/** Get the size of surface contents
3691 *
3692 * \param surface The surface to query.
3693 * \param width Returns the width of raw contents.
3694 * \param height Returns the height of raw contents.
3695 *
3696 * Retrieves the raw surface content size in pixels for the given surface.
3697 * This is the whole content size in buffer pixels. If the surface
3698 * has no content or the renderer does not implement this feature,
3699 * zeroes are returned.
3700 *
3701 * This function is used to determine the buffer size needed for
3702 * a weston_surface_copy_content() call.
3703 */
3704WL_EXPORT void
3705weston_surface_get_content_size(struct weston_surface *surface,
3706 int *width, int *height)
3707{
3708 struct weston_renderer *rer = surface->compositor->renderer;
3709
3710 if (!rer->surface_get_content_size) {
3711 *width = 0;
3712 *height = 0;
3713 return;
3714 }
3715
3716 rer->surface_get_content_size(surface, width, height);
3717}
3718
Quentin Glidic248dd102016-08-12 10:41:34 +02003719/** Get the bounding box of a surface and its subsurfaces
3720 *
3721 * \param surface The surface to query.
3722 * \return The bounding box relative to the surface origin.
3723 *
3724 */
3725WL_EXPORT struct weston_geometry
3726weston_surface_get_bounding_box(struct weston_surface *surface)
3727{
3728 pixman_region32_t region;
3729 pixman_box32_t *box;
3730 struct weston_subsurface *subsurface;
3731
3732 pixman_region32_init_rect(&region,
3733 0, 0,
3734 surface->width, surface->height);
3735
3736 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link)
3737 pixman_region32_union_rect(&region, &region,
3738 subsurface->position.x,
3739 subsurface->position.y,
3740 subsurface->surface->width,
3741 subsurface->surface->height);
3742
3743 box = pixman_region32_extents(&region);
3744 struct weston_geometry geometry = {
3745 .x = box->x1,
3746 .y = box->y1,
3747 .width = box->x2 - box->x1,
3748 .height = box->y2 - box->y1,
3749 };
3750
3751 pixman_region32_fini(&region);
3752
3753 return geometry;
3754}
3755
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003756/** Copy surface contents to system memory.
3757 *
3758 * \param surface The surface to copy from.
3759 * \param target Pointer to the target memory buffer.
3760 * \param size Size of the target buffer in bytes.
3761 * \param src_x X location on contents to copy from.
3762 * \param src_y Y location on contents to copy from.
3763 * \param width Width in pixels of the area to copy.
3764 * \param height Height in pixels of the area to copy.
3765 * \return 0 for success, -1 for failure.
3766 *
3767 * Surface contents are maintained by the renderer. They can be in a
3768 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
3769 * else.
3770 *
3771 * Surface contents are copied into memory pointed to by target,
3772 * which has size bytes of space available. The target memory
3773 * may be larger than needed, but being smaller returns an error.
3774 * The extra bytes in target may or may not be written; their content is
3775 * unspecified. Size must be large enough to hold the image.
3776 *
3777 * The image in the target memory will be arranged in rows from
3778 * top to bottom, and pixels on a row from left to right. The pixel
3779 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
3780 * width * 4.
3781 *
3782 * Parameters src_x and src_y define the upper-left corner in buffer
3783 * coordinates (pixels) to copy from. Parameters width and height
3784 * define the size of the area to copy in pixels.
3785 *
3786 * The rectangle defined by src_x, src_y, width, height must fit in
3787 * the surface contents. Otherwise an error is returned.
3788 *
3789 * Use surface_get_data_size to determine the content size; the
3790 * needed target buffer size and rectangle limits.
3791 *
3792 * CURRENT IMPLEMENTATION RESTRICTIONS:
3793 * - the machine must be little-endian due to Pixman formats.
3794 *
3795 * NOTE: Pixman formats are premultiplied.
3796 */
3797WL_EXPORT int
3798weston_surface_copy_content(struct weston_surface *surface,
3799 void *target, size_t size,
3800 int src_x, int src_y,
3801 int width, int height)
3802{
3803 struct weston_renderer *rer = surface->compositor->renderer;
3804 int cw, ch;
3805 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
3806
3807 if (!rer->surface_copy_content)
3808 return -1;
3809
3810 weston_surface_get_content_size(surface, &cw, &ch);
3811
3812 if (src_x < 0 || src_y < 0)
3813 return -1;
3814
3815 if (width <= 0 || height <= 0)
3816 return -1;
3817
3818 if (src_x + width > cw || src_y + height > ch)
3819 return -1;
3820
3821 if (width * bytespp * height > size)
3822 return -1;
3823
3824 return rer->surface_copy_content(surface, target, size,
3825 src_x, src_y, width, height);
3826}
3827
Pekka Paalanene67858b2013-04-25 13:57:42 +03003828static void
3829subsurface_set_position(struct wl_client *client,
3830 struct wl_resource *resource, int32_t x, int32_t y)
3831{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003832 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003833
3834 if (!sub)
3835 return;
3836
3837 sub->position.x = x;
3838 sub->position.y = y;
3839 sub->position.set = 1;
3840}
3841
3842static struct weston_subsurface *
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003843subsurface_find_sibling(struct weston_subsurface *sub,
3844 struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003845{
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003846 struct weston_surface *parent = sub->parent;
3847 struct weston_subsurface *sibling;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003848
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003849 wl_list_for_each(sibling, &parent->subsurface_list, parent_link) {
3850 if (sibling->surface == surface && sibling != sub)
3851 return sibling;
3852 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003853
3854 return NULL;
3855}
3856
3857static struct weston_subsurface *
3858subsurface_sibling_check(struct weston_subsurface *sub,
3859 struct weston_surface *surface,
3860 const char *request)
3861{
3862 struct weston_subsurface *sibling;
3863
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003864 sibling = subsurface_find_sibling(sub, surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003865 if (!sibling) {
3866 wl_resource_post_error(sub->resource,
3867 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3868 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003869 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003870 return NULL;
3871 }
3872
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003873 assert(sibling->parent == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003874
3875 return sibling;
3876}
3877
3878static void
3879subsurface_place_above(struct wl_client *client,
3880 struct wl_resource *resource,
3881 struct wl_resource *sibling_resource)
3882{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003883 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003884 struct weston_surface *surface =
3885 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003886 struct weston_subsurface *sibling;
3887
3888 if (!sub)
3889 return;
3890
3891 sibling = subsurface_sibling_check(sub, surface, "place_above");
3892 if (!sibling)
3893 return;
3894
3895 wl_list_remove(&sub->parent_link_pending);
3896 wl_list_insert(sibling->parent_link_pending.prev,
3897 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003898
3899 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003900}
3901
3902static void
3903subsurface_place_below(struct wl_client *client,
3904 struct wl_resource *resource,
3905 struct wl_resource *sibling_resource)
3906{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003907 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003908 struct weston_surface *surface =
3909 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003910 struct weston_subsurface *sibling;
3911
3912 if (!sub)
3913 return;
3914
3915 sibling = subsurface_sibling_check(sub, surface, "place_below");
3916 if (!sibling)
3917 return;
3918
3919 wl_list_remove(&sub->parent_link_pending);
3920 wl_list_insert(&sibling->parent_link_pending,
3921 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003922
3923 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003924}
3925
3926static void
3927subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
3928{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003929 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003930
3931 if (sub)
3932 sub->synchronized = 1;
3933}
3934
3935static void
3936subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
3937{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003938 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003939
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003940 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003941 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003942
3943 /* If sub became effectively desynchronized, flush. */
3944 if (!weston_subsurface_is_synchronized(sub))
3945 weston_subsurface_synchronized_commit(sub);
3946 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003947}
3948
3949static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003950weston_subsurface_unlink_parent(struct weston_subsurface *sub)
3951{
3952 wl_list_remove(&sub->parent_link);
3953 wl_list_remove(&sub->parent_link_pending);
3954 wl_list_remove(&sub->parent_destroy_listener.link);
3955 sub->parent = NULL;
3956}
3957
3958static void
3959weston_subsurface_destroy(struct weston_subsurface *sub);
3960
3961static void
3962subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
3963{
3964 struct weston_subsurface *sub =
3965 container_of(listener, struct weston_subsurface,
3966 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003967 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003968
3969 /* The protocol object (wl_resource) is left inert. */
3970 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003971 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003972
3973 weston_subsurface_destroy(sub);
3974}
3975
3976static void
3977subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3978{
3979 struct weston_subsurface *sub =
3980 container_of(listener, struct weston_subsurface,
3981 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003982 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003983 assert(sub->surface != sub->parent);
3984
3985 if (weston_surface_is_mapped(sub->surface))
3986 weston_surface_unmap(sub->surface);
3987
3988 weston_subsurface_unlink_parent(sub);
3989}
3990
3991static void
3992subsurface_resource_destroy(struct wl_resource *resource)
3993{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003994 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003995
3996 if (sub)
3997 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003998}
3999
4000static void
4001subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
4002{
4003 wl_resource_destroy(resource);
4004}
4005
4006static void
4007weston_subsurface_link_parent(struct weston_subsurface *sub,
4008 struct weston_surface *parent)
4009{
4010 sub->parent = parent;
4011 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004012 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004013 &sub->parent_destroy_listener);
4014
4015 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4016 wl_list_insert(&parent->subsurface_list_pending,
4017 &sub->parent_link_pending);
4018}
4019
4020static void
4021weston_subsurface_link_surface(struct weston_subsurface *sub,
4022 struct weston_surface *surface)
4023{
4024 sub->surface = surface;
4025 sub->surface_destroy_listener.notify =
4026 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004027 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004028 &sub->surface_destroy_listener);
4029}
4030
4031static void
4032weston_subsurface_destroy(struct weston_subsurface *sub)
4033{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004034 struct weston_view *view, *next;
4035
Pekka Paalanene67858b2013-04-25 13:57:42 +03004036 assert(sub->surface);
4037
4038 if (sub->resource) {
4039 assert(weston_surface_to_subsurface(sub->surface) == sub);
4040 assert(sub->parent_destroy_listener.notify ==
4041 subsurface_handle_parent_destroy);
4042
George Kiagiadakised04d382014-06-13 18:10:26 +02004043 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
4044 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004045 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02004046 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05004047
Pekka Paalanene67858b2013-04-25 13:57:42 +03004048 if (sub->parent)
4049 weston_subsurface_unlink_parent(sub);
4050
Jason Ekstrand7b982072014-05-20 14:33:03 -05004051 weston_surface_state_fini(&sub->cached);
4052 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004053
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004054 sub->surface->committed = NULL;
4055 sub->surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004056 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004057 } else {
4058 /* the dummy weston_subsurface for the parent itself */
4059 assert(sub->parent_destroy_listener.notify == NULL);
4060 wl_list_remove(&sub->parent_link);
4061 wl_list_remove(&sub->parent_link_pending);
4062 }
4063
4064 wl_list_remove(&sub->surface_destroy_listener.link);
4065 free(sub);
4066}
4067
4068static const struct wl_subsurface_interface subsurface_implementation = {
4069 subsurface_destroy,
4070 subsurface_set_position,
4071 subsurface_place_above,
4072 subsurface_place_below,
4073 subsurface_set_sync,
4074 subsurface_set_desync
4075};
4076
4077static struct weston_subsurface *
4078weston_subsurface_create(uint32_t id, struct weston_surface *surface,
4079 struct weston_surface *parent)
4080{
4081 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004082 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004083
Bryce Harringtonde16d892014-11-20 22:21:57 -08004084 sub = zalloc(sizeof *sub);
4085 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004086 return NULL;
4087
Jason Ekstranda7af7042013-10-12 22:38:11 -05004088 wl_list_init(&sub->unused_views);
4089
Jason Ekstranda85118c2013-06-27 20:17:02 -05004090 sub->resource =
4091 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004092 if (!sub->resource) {
4093 free(sub);
4094 return NULL;
4095 }
4096
Jason Ekstranda85118c2013-06-27 20:17:02 -05004097 wl_resource_set_implementation(sub->resource,
4098 &subsurface_implementation,
4099 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004100 weston_subsurface_link_surface(sub, surface);
4101 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05004102 weston_surface_state_init(&sub->cached);
4103 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004104 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004105
4106 return sub;
4107}
4108
4109/* Create a dummy subsurface for having the parent itself in its
4110 * sub-surface lists. Makes stacking order manipulation easy.
4111 */
4112static struct weston_subsurface *
4113weston_subsurface_create_for_parent(struct weston_surface *parent)
4114{
4115 struct weston_subsurface *sub;
4116
Bryce Harringtonde16d892014-11-20 22:21:57 -08004117 sub = zalloc(sizeof *sub);
4118 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004119 return NULL;
4120
4121 weston_subsurface_link_surface(sub, parent);
4122 sub->parent = parent;
4123 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4124 wl_list_insert(&parent->subsurface_list_pending,
4125 &sub->parent_link_pending);
4126
4127 return sub;
4128}
4129
4130static void
4131subcompositor_get_subsurface(struct wl_client *client,
4132 struct wl_resource *resource,
4133 uint32_t id,
4134 struct wl_resource *surface_resource,
4135 struct wl_resource *parent_resource)
4136{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004137 struct weston_surface *surface =
4138 wl_resource_get_user_data(surface_resource);
4139 struct weston_surface *parent =
4140 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004141 struct weston_subsurface *sub;
4142 static const char where[] = "get_subsurface: wl_subsurface@";
4143
4144 if (surface == parent) {
4145 wl_resource_post_error(resource,
4146 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4147 "%s%d: wl_surface@%d cannot be its own parent",
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
4152 if (weston_surface_to_subsurface(surface)) {
4153 wl_resource_post_error(resource,
4154 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4155 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004156 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004157 return;
4158 }
4159
Pekka Paalanen50b67472014-10-01 15:02:41 +03004160 if (weston_surface_set_role(surface, "wl_subsurface", resource,
4161 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004162 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004163
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004164 if (weston_surface_get_main_surface(parent) == surface) {
4165 wl_resource_post_error(resource,
4166 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4167 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004168 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004169 return;
4170 }
4171
Pekka Paalanene67858b2013-04-25 13:57:42 +03004172 /* make sure the parent is in its own list */
4173 if (wl_list_empty(&parent->subsurface_list)) {
4174 if (!weston_subsurface_create_for_parent(parent)) {
4175 wl_resource_post_no_memory(resource);
4176 return;
4177 }
4178 }
4179
4180 sub = weston_subsurface_create(id, surface, parent);
4181 if (!sub) {
4182 wl_resource_post_no_memory(resource);
4183 return;
4184 }
4185
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004186 surface->committed = subsurface_committed;
4187 surface->committed_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004188 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004189}
4190
4191static void
4192subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
4193{
4194 wl_resource_destroy(resource);
4195}
4196
4197static const struct wl_subcompositor_interface subcompositor_interface = {
4198 subcompositor_destroy,
4199 subcompositor_get_subsurface
4200};
4201
4202static void
4203bind_subcompositor(struct wl_client *client,
4204 void *data, uint32_t version, uint32_t id)
4205{
4206 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004207 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004208
Jason Ekstranda85118c2013-06-27 20:17:02 -05004209 resource =
4210 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004211 if (resource == NULL) {
4212 wl_client_post_no_memory(client);
4213 return;
4214 }
4215 wl_resource_set_implementation(resource, &subcompositor_interface,
4216 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004217}
4218
Bryce Harrington0795ece2016-08-30 12:04:26 -07004219/** Set a DPMS mode on all of the compositor's outputs
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004220 *
4221 * \param compositor The compositor instance
4222 * \param state The DPMS state the outputs will be set to
4223 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004224static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004225weston_compositor_dpms(struct weston_compositor *compositor,
4226 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004227{
4228 struct weston_output *output;
4229
Bryce Harrington08976ac2016-08-30 12:05:16 -07004230 wl_list_for_each(output, &compositor->output_list, link)
4231 if (output->set_dpms)
4232 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004233}
4234
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004235/** Restores the compositor to active status
4236 *
4237 * \param compositor The compositor instance
4238 *
4239 * If the compositor was in a sleeping mode, all outputs are powered
4240 * back on via DPMS. Otherwise if the compositor was inactive
4241 * (idle/locked, offscreen, or sleeping) then the compositor's wake
4242 * signal will fire.
4243 *
4244 * Restarts the idle timer.
4245 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004246WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004247weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004248{
Neil Roberts8b62e202013-09-30 13:14:47 +01004249 uint32_t old_state = compositor->state;
4250
4251 /* The state needs to be changed before emitting the wake
4252 * signal because that may try to schedule a repaint which
4253 * will not work if the compositor is still sleeping */
4254 compositor->state = WESTON_COMPOSITOR_ACTIVE;
4255
4256 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004257 case WESTON_COMPOSITOR_SLEEPING:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004258 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004259 case WESTON_COMPOSITOR_OFFSCREEN:
Daniel Stone893b9362016-11-08 15:47:09 +00004260 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004261 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004262 /* fall through */
4263 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004264 wl_event_source_timer_update(compositor->idle_source,
4265 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004266 }
4267}
4268
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004269/** Turns off rendering and frame events for the compositor.
4270 *
4271 * \param compositor The compositor instance
4272 *
4273 * This is used for example to prevent further rendering while the
4274 * compositor is shutting down.
4275 *
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004276 * Stops the idle timer.
4277 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004278WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004279weston_compositor_offscreen(struct weston_compositor *compositor)
4280{
4281 switch (compositor->state) {
4282 case WESTON_COMPOSITOR_OFFSCREEN:
4283 return;
4284 case WESTON_COMPOSITOR_SLEEPING:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004285 default:
4286 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4287 wl_event_source_timer_update(compositor->idle_source, 0);
4288 }
4289}
4290
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004291/** Powers down all attached output devices
4292 *
4293 * \param compositor The compositor instance
4294 *
4295 * Causes rendering to the outputs to cease, and no frame events to be
4296 * sent. Only powers down the outputs if the compositor is not already
4297 * in sleep mode.
4298 *
4299 * Stops the idle timer.
4300 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004301WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004302weston_compositor_sleep(struct weston_compositor *compositor)
4303{
4304 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
4305 return;
4306
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004307 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004308 compositor->state = WESTON_COMPOSITOR_SLEEPING;
4309 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
4310}
4311
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004312/** Sets compositor to idle mode
4313 *
4314 * \param data The compositor instance
4315 *
4316 * This is called when the idle timer fires. Once the compositor is in
4317 * idle mode it requires a wake action (e.g. via
4318 * weston_compositor_wake()) to restore it. The compositor's
4319 * idle_signal will be triggered when the idle event occurs.
4320 *
4321 * Idleness can be inhibited by setting the compositor's idle_inhibit
4322 * property.
4323 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004324static int
4325idle_handler(void *data)
4326{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004327 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004328
4329 if (compositor->idle_inhibit)
4330 return 1;
4331
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004332 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004333 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004334
4335 return 1;
4336}
4337
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004338WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08004339weston_plane_init(struct weston_plane *plane,
4340 struct weston_compositor *ec,
4341 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004342{
4343 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004344 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004345 plane->x = x;
4346 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08004347 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004348
4349 /* Init the link so that the call to wl_list_remove() when releasing
4350 * the plane without ever stacking doesn't lead to a crash */
4351 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004352}
4353
4354WL_EXPORT void
4355weston_plane_release(struct weston_plane *plane)
4356{
Xiong Zhang97116532013-10-23 13:58:31 +08004357 struct weston_view *view;
4358
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004359 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004360 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004361
Xiong Zhang97116532013-10-23 13:58:31 +08004362 wl_list_for_each(view, &plane->compositor->view_list, link) {
4363 if (view->plane == plane)
4364 view->plane = NULL;
4365 }
4366
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004367 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004368}
4369
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004370WL_EXPORT void
4371weston_compositor_stack_plane(struct weston_compositor *ec,
4372 struct weston_plane *plane,
4373 struct weston_plane *above)
4374{
4375 if (above)
4376 wl_list_insert(above->link.prev, &plane->link);
4377 else
4378 wl_list_insert(&ec->plane_list, &plane->link);
4379}
4380
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004381static void
4382output_release(struct wl_client *client, struct wl_resource *resource)
4383{
4384 wl_resource_destroy(resource);
4385}
4386
4387static const struct wl_output_interface output_interface = {
4388 output_release,
4389};
4390
4391
Casey Dahlin9074db52012-04-19 22:50:09 -04004392static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004393{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004394 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004395}
4396
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004397static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004398bind_output(struct wl_client *client,
4399 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05004400{
Pekka Paalanen05347622017-03-27 12:24:34 +03004401 struct weston_head *head = data;
4402 struct weston_output *output = head->output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004403 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004404 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004405
Jason Ekstranda85118c2013-06-27 20:17:02 -05004406 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004407 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004408 if (resource == NULL) {
4409 wl_client_post_no_memory(client);
4410 return;
4411 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004412
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03004413 wl_list_insert(&head->resource_list, wl_resource_get_link(resource));
Pekka Paalanen055c1132017-03-27 16:31:25 +03004414 wl_resource_set_implementation(resource, &output_interface, head,
Pekka Paalanen05347622017-03-27 12:24:34 +03004415 unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04004416
Pekka Paalanen05347622017-03-27 12:24:34 +03004417 assert(output);
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004418 wl_output_send_geometry(resource,
4419 output->x,
4420 output->y,
Pekka Paalanen01f60212017-03-24 15:39:24 +02004421 head->mm_width,
4422 head->mm_height,
4423 head->subpixel,
4424 head->make, head->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04004425 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004426 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004427 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02004428 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004429
4430 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004431 wl_output_send_mode(resource,
4432 mode->flags,
4433 mode->width,
4434 mode->height,
4435 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004436 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02004437
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004438 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004439 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004440}
4441
Pekka Paalanendcac3512017-12-08 14:13:34 +02004442static void
4443weston_head_add_global(struct weston_head *head)
4444{
4445 head->global = wl_global_create(head->compositor->wl_display,
4446 &wl_output_interface, 3,
4447 head, bind_output);
4448}
4449
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02004450/** Remove the global wl_output protocol object
4451 *
4452 * \param head The head whose global to remove.
4453 *
4454 * Also orphans the wl_resources for this head (wl_output).
4455 */
4456static void
4457weston_head_remove_global(struct weston_head *head)
4458{
4459 struct wl_resource *resource, *tmp;
4460
4461 if (head->global)
4462 wl_global_destroy(head->global);
4463 head->global = NULL;
4464
4465 wl_resource_for_each_safe(resource, tmp, &head->resource_list) {
4466 unbind_resource(resource);
4467 wl_resource_set_destructor(resource, NULL);
4468 wl_resource_set_user_data(resource, NULL);
4469 }
4470}
4471
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004472/** Get the backing object of wl_output
4473 *
4474 * \param resource A wl_output protocol object.
4475 * \return The backing object (user data) of a wl_resource representing a
4476 * wl_output protocol object.
4477 */
Pekka Paalanen055c1132017-03-27 16:31:25 +03004478WL_EXPORT struct weston_head *
4479weston_head_from_resource(struct wl_resource *resource)
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004480{
4481 assert(wl_resource_instance_of(resource, &wl_output_interface,
4482 &output_interface));
4483
4484 return wl_resource_get_user_data(resource);
4485}
4486
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004487/** Initialize a pre-allocated weston_head
4488 *
4489 * \param head The head to initialize.
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004490 * \param name The head name, e.g. the connector name or equivalent.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004491 *
4492 * The head will be safe to attach, detach and release.
4493 *
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004494 * The name is used in logs, and can be used by compositors as a configuration
4495 * identifier.
4496 *
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004497 * \memberof weston_head
4498 * \internal
4499 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004500WL_EXPORT void
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004501weston_head_init(struct weston_head *head, const char *name)
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004502{
4503 /* Add some (in)sane defaults which can be used
4504 * for checking if an output was properly configured
4505 */
4506 memset(head, 0, sizeof *head);
4507
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004508 wl_list_init(&head->compositor_link);
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03004509 wl_signal_init(&head->destroy_signal);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004510 wl_list_init(&head->output_link);
4511 wl_list_init(&head->resource_list);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004512 head->name = strdup(name);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004513}
4514
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004515/** Send output heads changed signal
4516 *
4517 * \param output The output that changed.
4518 *
4519 * Notify that the enabled output gained and/or lost heads, or that the
4520 * associated heads may have changed their connection status. This does not
4521 * include cases where the output becomes enabled or disabled. The registered
4522 * callbacks are called after the change has successfully happened.
4523 *
4524 * If connection status change causes the compositor to attach or detach a head
4525 * to an enabled output, the registered callbacks may be called multiple times.
4526 */
4527static void
4528weston_output_emit_heads_changed(struct weston_output *output)
4529{
4530 wl_signal_emit(&output->compositor->output_heads_changed_signal,
4531 output);
4532}
4533
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004534/** Idle task for emitting heads_changed_signal */
4535static void
4536weston_compositor_call_heads_changed(void *data)
4537{
4538 struct weston_compositor *compositor = data;
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004539 struct weston_head *head;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004540
4541 compositor->heads_changed_source = NULL;
4542
4543 wl_signal_emit(&compositor->heads_changed_signal, compositor);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004544
4545 wl_list_for_each(head, &compositor->head_list, compositor_link) {
4546 if (head->output && head->output->enabled)
4547 weston_output_emit_heads_changed(head->output);
4548 }
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004549}
4550
4551/** Schedule a call on idle to heads_changed callback
4552 *
4553 * \param compositor The Compositor.
4554 *
4555 * \memberof weston_compositor
4556 * \internal
4557 */
4558static void
4559weston_compositor_schedule_heads_changed(struct weston_compositor *compositor)
4560{
4561 struct wl_event_loop *loop;
4562
4563 if (compositor->heads_changed_source)
4564 return;
4565
4566 loop = wl_display_get_event_loop(compositor->wl_display);
4567 compositor->heads_changed_source = wl_event_loop_add_idle(loop,
4568 weston_compositor_call_heads_changed, compositor);
4569}
4570
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004571/** Register a new head
4572 *
4573 * \param compositor The compositor.
4574 * \param head The head to register, must not be already registered.
4575 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004576 * This signals the core that a new head has become available, leading to
4577 * heads_changed hook being called later.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004578 *
4579 * \memberof weston_compositor
4580 * \internal
4581 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004582WL_EXPORT void
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004583weston_compositor_add_head(struct weston_compositor *compositor,
4584 struct weston_head *head)
4585{
4586 assert(wl_list_empty(&head->compositor_link));
4587 assert(head->name);
4588
4589 wl_list_insert(compositor->head_list.prev, &head->compositor_link);
4590 head->compositor = compositor;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004591 weston_compositor_schedule_heads_changed(compositor);
4592}
4593
4594/** Adds a listener to be called when heads change
4595 *
4596 * \param compositor The compositor.
4597 * \param listener The listener to add.
4598 *
4599 * The listener notify function argument is the \var compositor.
4600 *
4601 * The listener function will be called after heads are added or their
4602 * connection status has changed. Several changes may be accumulated into a
4603 * single call. The user is expected to iterate over the existing heads and
4604 * check their statuses to find out what changed.
4605 *
4606 * \sa weston_compositor_iterate_heads, weston_head_is_connected,
4607 * weston_head_is_enabled
4608 * \memberof weston_compositor
4609 */
4610WL_EXPORT void
4611weston_compositor_add_heads_changed_listener(struct weston_compositor *compositor,
4612 struct wl_listener *listener)
4613{
4614 wl_signal_add(&compositor->heads_changed_signal, listener);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004615}
4616
4617/** Iterate over available heads
4618 *
4619 * \param compositor The compositor.
4620 * \param item The iterator, or NULL for start.
4621 * \return The next available head in the list.
4622 *
4623 * Returns all available heads, regardless of being connected or enabled.
4624 *
4625 * You can iterate over all heads as follows:
4626 * \code
4627 * struct weston_head *head = NULL;
4628 *
4629 * while ((head = weston_compositor_iterate_heads(compositor, head))) {
4630 * ...
4631 * }
4632 * \endcode
4633 *
4634 * If you cause \c iter to be removed from the list, you cannot use it to
4635 * continue iterating. Removing any other item is safe.
4636 *
4637 * \memberof weston_compositor
4638 */
4639WL_EXPORT struct weston_head *
4640weston_compositor_iterate_heads(struct weston_compositor *compositor,
4641 struct weston_head *iter)
4642{
4643 struct wl_list *list = &compositor->head_list;
4644 struct wl_list *node;
4645
4646 assert(compositor);
4647 assert(!iter || iter->compositor == compositor);
4648
4649 if (iter)
4650 node = iter->compositor_link.next;
4651 else
4652 node = list->next;
4653
4654 assert(node);
4655 assert(!iter || node != &iter->compositor_link);
4656
4657 if (node == list)
4658 return NULL;
4659
4660 return container_of(node, struct weston_head, compositor_link);
4661}
4662
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004663/** Iterate over attached heads
4664 *
4665 * \param output The output whose heads to iterate.
4666 * \param item The iterator, or NULL for start.
4667 * \return The next attached head in the list.
4668 *
4669 * Returns all heads currently attached to the output.
4670 *
4671 * You can iterate over all heads as follows:
4672 * \code
4673 * struct weston_head *head = NULL;
4674 *
4675 * while ((head = weston_output_iterate_heads(output, head))) {
4676 * ...
4677 * }
4678 * \endcode
4679 *
4680 * If you cause \c iter to be removed from the list, you cannot use it to
4681 * continue iterating. Removing any other item is safe.
4682 *
4683 * \memberof weston_compositor
4684 */
4685WL_EXPORT struct weston_head *
4686weston_output_iterate_heads(struct weston_output *output,
4687 struct weston_head *iter)
4688{
4689 struct wl_list *list = &output->head_list;
4690 struct wl_list *node;
4691
4692 assert(output);
4693 assert(!iter || iter->output == output);
4694
4695 if (iter)
4696 node = iter->output_link.next;
4697 else
4698 node = list->next;
4699
4700 assert(node);
4701 assert(!iter || node != &iter->output_link);
4702
4703 if (node == list)
4704 return NULL;
4705
4706 return container_of(node, struct weston_head, output_link);
4707}
4708
Pekka Paalanendcac3512017-12-08 14:13:34 +02004709/** Attach a head to an output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004710 *
4711 * \param output The output to attach to.
4712 * \param head The head that is not yet attached.
4713 * \return 0 on success, -1 on failure.
4714 *
4715 * Attaches the given head to the output. All heads of an output are clones
4716 * and share the resolution and timings.
4717 *
4718 * Cloning heads this way uses less resources than creating an output for
4719 * each head, but is not always possible due to environment, driver and hardware
4720 * limitations.
4721 *
4722 * On failure, the head remains unattached. Success of this function does not
4723 * guarantee the output configuration is actually valid. The final checks are
Pekka Paalanendcac3512017-12-08 14:13:34 +02004724 * made on weston_output_enable() unless the output was already enabled.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004725 *
4726 * \memberof weston_output
4727 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004728WL_EXPORT int
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004729weston_output_attach_head(struct weston_output *output,
4730 struct weston_head *head)
4731{
Pekka Paalanendcac3512017-12-08 14:13:34 +02004732 char *head_names;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004733
4734 if (!wl_list_empty(&head->output_link))
4735 return -1;
4736
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004737 if (output->attach_head) {
4738 if (output->attach_head(output, head) < 0)
4739 return -1;
4740 } else if (!wl_list_empty(&output->head_list)) {
4741 /* No support for clones in the legacy path. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004742 return -1;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004743 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004744
4745 head->output = output;
4746 wl_list_insert(output->head_list.prev, &head->output_link);
4747
Pekka Paalanendcac3512017-12-08 14:13:34 +02004748 if (output->enabled) {
4749 weston_head_add_global(head);
4750
4751 head_names = weston_output_create_heads_string(output);
4752 weston_log("Output '%s' updated to have head(s) %s\n",
4753 output->name, head_names);
4754 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004755
4756 weston_output_emit_heads_changed(output);
Pekka Paalanendcac3512017-12-08 14:13:34 +02004757 }
4758
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004759 return 0;
4760}
4761
4762/** Detach a head from its output
4763 *
4764 * \param head The head to detach.
4765 *
4766 * It is safe to detach a non-attached head.
4767 *
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004768 * If the head is attached to an enabled output and the output will be left
4769 * with no heads, the output will be disabled.
4770 *
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004771 * \memberof weston_head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004772 * \sa weston_output_disable
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004773 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004774WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004775weston_head_detach(struct weston_head *head)
4776{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004777 struct weston_output *output = head->output;
Pekka Paalanena0106992017-12-08 16:11:17 +02004778 char *head_names;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004779
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004780 wl_list_remove(&head->output_link);
4781 wl_list_init(&head->output_link);
4782 head->output = NULL;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004783
4784 if (!output)
4785 return;
4786
4787 if (output->detach_head)
4788 output->detach_head(output, head);
4789
4790 if (output->enabled) {
4791 weston_head_remove_global(head);
4792
Pekka Paalanena0106992017-12-08 16:11:17 +02004793 if (wl_list_empty(&output->head_list)) {
4794 weston_log("Output '%s' no heads left, disabling.\n",
4795 output->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004796 weston_output_disable(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02004797 } else {
4798 head_names = weston_output_create_heads_string(output);
4799 weston_log("Output '%s' updated to have head(s) %s\n",
4800 output->name, head_names);
4801 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004802
4803 weston_output_emit_heads_changed(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02004804 }
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004805 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004806}
4807
4808/** Destroy a head
4809 *
4810 * \param head The head to be released.
4811 *
4812 * Destroys the head. The caller is responsible for freeing the memory pointed
4813 * to by \c head.
4814 *
4815 * \memberof weston_head
4816 * \internal
4817 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004818WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004819weston_head_release(struct weston_head *head)
4820{
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03004821 wl_signal_emit(&head->destroy_signal, head);
4822
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004823 weston_head_detach(head);
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03004824
4825 free(head->make);
4826 free(head->model);
4827 free(head->serial_number);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004828 free(head->name);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004829
4830 wl_list_remove(&head->compositor_link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004831}
4832
Pekka Paalanene19970f2017-08-28 14:11:02 +03004833static void
4834weston_head_set_device_changed(struct weston_head *head)
4835{
4836 head->device_changed = true;
4837
4838 if (head->compositor)
4839 weston_compositor_schedule_heads_changed(head->compositor);
4840}
4841
4842/** String equal comparison with NULLs being equal */
4843static bool
4844str_null_eq(const char *a, const char *b)
4845{
4846 if (!a && !b)
4847 return true;
4848
4849 if (!!a != !!b)
4850 return false;
4851
4852 return strcmp(a, b) == 0;
4853}
4854
Pekka Paalanen01f60212017-03-24 15:39:24 +02004855/** Store monitor make, model and serial number
4856 *
4857 * \param head The head to modify.
4858 * \param make The monitor make. If EDID is available, the PNP ID. Otherwise
4859 * any string, or NULL for none.
4860 * \param model The monitor model or name, or a made-up string, or NULL for
4861 * none.
4862 * \param serialno The monitor serial number, a made-up string, or NULL for
4863 * none.
4864 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03004865 * This may set the device_changed flag.
4866 *
Pekka Paalanen01f60212017-03-24 15:39:24 +02004867 * \memberof weston_head
4868 * \internal
4869 */
4870WL_EXPORT void
4871weston_head_set_monitor_strings(struct weston_head *head,
4872 const char *make,
4873 const char *model,
4874 const char *serialno)
4875{
Pekka Paalanene19970f2017-08-28 14:11:02 +03004876 if (str_null_eq(head->make, make) &&
4877 str_null_eq(head->model, model) &&
4878 str_null_eq(head->serial_number, serialno))
4879 return;
4880
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03004881 free(head->make);
4882 free(head->model);
4883 free(head->serial_number);
4884
4885 head->make = make ? strdup(make) : NULL;
4886 head->model = model ? strdup(model) : NULL;
4887 head->serial_number = serialno ? strdup(serialno) : NULL;
Pekka Paalanene19970f2017-08-28 14:11:02 +03004888
4889 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02004890}
4891
4892/** Store physical image size
4893 *
4894 * \param head The head to modify.
4895 * \param mm_width Image area width in millimeters.
4896 * \param mm_height Image area height in millimeters.
4897 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03004898 * This may set the device_changed flag.
4899 *
Pekka Paalanen01f60212017-03-24 15:39:24 +02004900 * \memberof weston_head
4901 * \internal
4902 */
4903WL_EXPORT void
4904weston_head_set_physical_size(struct weston_head *head,
4905 int32_t mm_width, int32_t mm_height)
4906{
Pekka Paalanene19970f2017-08-28 14:11:02 +03004907 if (head->mm_width == mm_width &&
4908 head->mm_height == mm_height)
4909 return;
4910
Pekka Paalanen01f60212017-03-24 15:39:24 +02004911 head->mm_width = mm_width;
4912 head->mm_height = mm_height;
Pekka Paalanene19970f2017-08-28 14:11:02 +03004913
4914 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02004915}
4916
4917/** Store monitor sub-pixel layout
4918 *
4919 * \param head The head to modify.
4920 * \param sp Sub-pixel layout. The possible values are:
4921 * - WL_OUTPUT_SUBPIXEL_UNKNOWN,
4922 * - WL_OUTPUT_SUBPIXEL_NONE,
4923 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB,
4924 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR,
4925 * - WL_OUTPUT_SUBPIXEL_VERTICAL_RGB,
4926 * - WL_OUTPUT_SUBPIXEL_VERTICAL_BGR
4927 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03004928 * This may set the device_changed flag.
4929 *
Pekka Paalanen01f60212017-03-24 15:39:24 +02004930 * \memberof weston_head
4931 * \internal
4932 */
4933WL_EXPORT void
4934weston_head_set_subpixel(struct weston_head *head,
4935 enum wl_output_subpixel sp)
4936{
Pekka Paalanene19970f2017-08-28 14:11:02 +03004937 if (head->subpixel == sp)
4938 return;
4939
Pekka Paalanen01f60212017-03-24 15:39:24 +02004940 head->subpixel = sp;
Pekka Paalanene19970f2017-08-28 14:11:02 +03004941
4942 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02004943}
4944
4945/** Mark the monitor as internal
4946 *
4947 * This is used for embedded screens, like laptop panels.
4948 *
4949 * \param head The head to mark as internal.
4950 *
4951 * By default a head is external. The type is often inferred from the physical
4952 * connector type.
4953 *
4954 * \memberof weston_head
4955 * \internal
4956 */
4957WL_EXPORT void
4958weston_head_set_internal(struct weston_head *head)
4959{
4960 head->connection_internal = true;
4961}
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004962
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03004963/** Store connector status
4964 *
4965 * \param head The head to modify.
4966 * \param connected Whether the head is connected.
4967 *
4968 * Connectors are created as disconnected. This function can be used to
4969 * set the connector status.
4970 *
4971 * The status should be set to true when a physical connector is connected to
4972 * a video sink device like a monitor and to false when the connector is
4973 * disconnected. For nested backends, the connection status should reflect the
4974 * connection to the parent display server.
4975 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004976 * When the connection status changes, it schedules a call to the heads_changed
Pekka Paalanene19970f2017-08-28 14:11:02 +03004977 * hook and sets the device_changed flag.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004978 *
4979 * \sa weston_compositor_set_heads_changed_cb
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03004980 * \memberof weston_head
4981 * \internal
4982 */
4983WL_EXPORT void
4984weston_head_set_connection_status(struct weston_head *head, bool connected)
4985{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004986 if (head->connected == connected)
4987 return;
4988
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03004989 head->connected = connected;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004990
Pekka Paalanene19970f2017-08-28 14:11:02 +03004991 weston_head_set_device_changed(head);
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03004992}
4993
4994/** Is the head currently connected?
4995 *
4996 * \param head The head to query.
4997 * \return Connection status.
4998 *
4999 * Returns true if the head is physically connected to a monitor, or in
5000 * case of a nested backend returns true when there is a connection to the
5001 * parent display server.
5002 *
5003 * This is independent from the head being enabled.
5004 *
5005 * \sa weston_head_is_enabled
5006 * \memberof weston_head
5007 */
5008WL_EXPORT bool
5009weston_head_is_connected(struct weston_head *head)
5010{
5011 return head->connected;
5012}
5013
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005014/** Is the head currently enabled?
5015 *
5016 * \param head The head to query.
5017 * \return Video status.
5018 *
5019 * Returns true if the head is currently transmitting a video stream.
5020 *
5021 * This is independent of the head being connected.
5022 *
5023 * \sa weston_head_is_connected
5024 * \memberof weston_head
5025 */
5026WL_EXPORT bool
5027weston_head_is_enabled(struct weston_head *head)
5028{
5029 if (!head->output)
5030 return false;
5031
5032 return head->output->enabled;
5033}
5034
Pekka Paalanene19970f2017-08-28 14:11:02 +03005035/** Has the device information changed?
5036 *
5037 * \param head The head to query.
5038 * \return True if the device information has changed since last reset.
5039 *
5040 * The information about the connected display device, e.g. a monitor, may
5041 * change without being disconnected in between. Changing information
5042 * causes a call to the heads_changed hook.
5043 *
5044 * The information includes make, model, serial number, physical size,
5045 * and sub-pixel type. The connection status is also included.
5046 *
5047 * \sa weston_head_reset_device_changed, weston_compositor_set_heads_changed_cb
5048 * \memberof weston_head
5049 */
5050WL_EXPORT bool
5051weston_head_is_device_changed(struct weston_head *head)
5052{
5053 return head->device_changed;
5054}
5055
5056/** Acknowledge device information change
5057 *
5058 * \param head The head to acknowledge.
5059 *
5060 * Clears the device changed flag on this head. When a compositor has processed
5061 * device information, it should call this to be able to notice further
5062 * changes.
5063 *
5064 * \sa weston_head_is_device_changed
5065 * \memberof weston_head
5066 */
5067WL_EXPORT void
5068weston_head_reset_device_changed(struct weston_head *head)
5069{
5070 head->device_changed = false;
5071}
5072
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005073/** Get the name of a head
5074 *
5075 * \param head The head to query.
5076 * \return The head's name, not NULL.
5077 *
5078 * The name depends on the backend. The DRM backend uses connector names,
5079 * other backends may use hardcoded names or user-given names.
5080 */
5081WL_EXPORT const char *
5082weston_head_get_name(struct weston_head *head)
5083{
5084 return head->name;
5085}
5086
5087/** Get the output the head is attached to
5088 *
5089 * \param head The head to query.
5090 * \return The output the head is attached to, or NULL if detached.
5091 */
5092WL_EXPORT struct weston_output *
5093weston_head_get_output(struct weston_head *head)
5094{
5095 return head->output;
5096}
5097
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005098/** Add destroy callback for a head
5099 *
5100 * \param head The head to watch for.
5101 * \param listener The listener to add. The \c notify member must be set.
5102 *
5103 * Heads may get destroyed for various reasons by the backends. If a head is
5104 * attached to an output, the compositor should listen for head destruction
5105 * and reconfigure or destroy the output if necessary.
5106 *
5107 * The destroy callbacks will be called on weston_head destruction before any
5108 * automatic detaching from an associated weston_output and before any
5109 * weston_head information is lost.
5110 *
5111 * The \c data argument to the notify callback is the weston_head being
5112 * destroyed.
5113 */
5114WL_EXPORT void
5115weston_head_add_destroy_listener(struct weston_head *head,
5116 struct wl_listener *listener)
5117{
5118 wl_signal_add(&head->destroy_signal, listener);
5119}
5120
5121/** Look up destroy listener for a head
5122 *
5123 * \param head The head to query.
5124 * \param notify The notify function used used for the added destroy listener.
5125 * \return The listener, or NULL if not found.
5126 *
5127 * This looks up the previously added destroy listener struct based on the
5128 * notify function it has. The listener can be used to access user data
5129 * through \c container_of().
5130 *
5131 * \sa wl_signal_get()
5132 */
5133WL_EXPORT struct wl_listener *
5134weston_head_get_destroy_listener(struct weston_head *head,
5135 wl_notify_func_t notify)
5136{
5137 return wl_signal_get(&head->destroy_signal, notify);
5138}
5139
David Fort0de859e2016-05-27 23:22:57 +02005140/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005141static void
David Fort0de859e2016-05-27 23:22:57 +02005142weston_compositor_reflow_outputs(struct weston_compositor *compositor,
5143 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005144{
5145 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02005146 bool start_resizing = false;
5147
5148 if (!delta_width)
5149 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005150
5151 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02005152 if (output == resized_output) {
5153 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005154 continue;
5155 }
5156
David Fort0de859e2016-05-27 23:22:57 +02005157 if (start_resizing) {
5158 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005159 output->dirty = 1;
5160 }
5161 }
5162}
5163
Pekka Paalanend72bad22017-03-29 17:01:41 +03005164static void
Scott Moreauccbf29d2012-02-22 14:21:41 -07005165weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005166{
Scott Moreau850ca422012-05-21 15:21:25 -06005167 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05005168
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005169 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005170 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06005171
Scott Moreauccbf29d2012-02-22 14:21:41 -07005172 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06005173 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005174 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005175 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005176 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005177 weston_matrix_scale(&output->matrix, magnification,
5178 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005179 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005180
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005181 switch (output->transform) {
5182 case WL_OUTPUT_TRANSFORM_FLIPPED:
5183 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5184 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5185 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5186 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5187 weston_matrix_scale(&output->matrix, -1, 1, 1);
5188 break;
5189 }
5190
5191 switch (output->transform) {
5192 default:
5193 case WL_OUTPUT_TRANSFORM_NORMAL:
5194 case WL_OUTPUT_TRANSFORM_FLIPPED:
5195 break;
5196 case WL_OUTPUT_TRANSFORM_90:
5197 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5198 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
5199 weston_matrix_rotate_xy(&output->matrix, 0, 1);
5200 break;
5201 case WL_OUTPUT_TRANSFORM_180:
5202 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5203 weston_matrix_translate(&output->matrix,
5204 -output->width, -output->height, 0);
5205 weston_matrix_rotate_xy(&output->matrix, -1, 0);
5206 break;
5207 case WL_OUTPUT_TRANSFORM_270:
5208 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5209 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5210 weston_matrix_rotate_xy(&output->matrix, 0, -1);
5211 break;
5212 }
5213
5214 if (output->current_scale != 1)
5215 weston_matrix_scale(&output->matrix,
5216 output->current_scale,
5217 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01005218
Scott Moreauccbf29d2012-02-22 14:21:41 -07005219 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05005220
5221 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005222}
5223
Scott Moreau1bad5db2012-08-18 01:04:05 -06005224static void
Alexander Larsson0b135062013-05-28 16:23:36 +02005225weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06005226{
5227 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005228 output->native_scale = scale;
5229 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06005230
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005231 convert_size_by_transform_scale(&output->width, &output->height,
5232 output->current_mode->width,
5233 output->current_mode->height,
5234 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02005235}
5236
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005237static void
5238weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07005239{
5240 output->x = x;
5241 output->y = y;
5242
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005243 pixman_region32_fini(&output->previous_damage);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02005244 pixman_region32_init(&output->previous_damage);
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005245
5246 pixman_region32_fini(&output->region);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005247 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06005248 output->width,
5249 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005250}
5251
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005252WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005253weston_output_move(struct weston_output *output, int x, int y)
5254{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005255 struct weston_head *head;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005256 struct wl_resource *resource;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005257 int ver;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005258
5259 output->move_x = x - output->x;
5260 output->move_y = y - output->y;
5261
5262 if (output->move_x == 0 && output->move_y == 0)
5263 return;
5264
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005265 weston_output_init_geometry(output, x, y);
5266
5267 output->dirty = 1;
5268
5269 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005270 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005271
5272 /* Notify clients of the change for output position. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005273 wl_list_for_each(head, &output->head_list, output_link) {
5274 wl_resource_for_each(resource, &head->resource_list) {
5275 wl_output_send_geometry(resource,
5276 output->x,
5277 output->y,
5278 head->mm_width,
5279 head->mm_height,
5280 head->subpixel,
5281 head->make,
5282 head->model,
5283 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08005284
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005285 ver = wl_resource_get_version(resource);
5286 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
5287 wl_output_send_done(resource);
5288 }
Quanxian Wangb2c86362014-03-14 09:16:25 +08005289 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005290}
5291
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005292/** Signal that a pending output is taken into use.
5293 *
5294 * Removes the output from the pending list and adds it to the compositor's
5295 * list of enabled outputs. The output created signal is emitted.
Giulio Camuffob1147152015-05-06 21:41:57 +03005296 *
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005297 * The output gets an internal ID assigned, and the wl_output global is
5298 * created.
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005299 *
Giulio Camuffob1147152015-05-06 21:41:57 +03005300 * \param compositor The compositor instance.
5301 * \param output The output to be added.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005302 *
5303 * \internal
Giulio Camuffob1147152015-05-06 21:41:57 +03005304 */
Pekka Paalanenf9681b52017-03-29 16:58:48 +03005305static void
Giulio Camuffob1147152015-05-06 21:41:57 +03005306weston_compositor_add_output(struct weston_compositor *compositor,
5307 struct weston_output *output)
5308{
Armin Krezoviće5403842016-08-05 15:28:29 +02005309 struct weston_view *view, *next;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005310 struct weston_head *head;
Armin Krezoviće5403842016-08-05 15:28:29 +02005311
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005312 assert(!output->enabled);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005313
5314 /* Verify we haven't reached the limit of 32 available output IDs */
5315 assert(ffs(~compositor->output_id_pool) > 0);
5316
5317 /* Invert the output id pool and look for the lowest numbered
5318 * switch (the least significant bit). Take that bit's position
5319 * as our ID, and mark it used in the compositor's output_id_pool.
5320 */
5321 output->id = ffs(~compositor->output_id_pool) - 1;
5322 compositor->output_id_pool |= 1u << output->id;
5323
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005324 wl_list_remove(&output->link);
Giulio Camuffob1147152015-05-06 21:41:57 +03005325 wl_list_insert(compositor->output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005326 output->enabled = true;
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005327
Pekka Paalanendcac3512017-12-08 14:13:34 +02005328 wl_list_for_each(head, &output->head_list, output_link)
5329 weston_head_add_global(head);
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005330
Giulio Camuffob1147152015-05-06 21:41:57 +03005331 wl_signal_emit(&compositor->output_created_signal, output);
Armin Krezoviće5403842016-08-05 15:28:29 +02005332
5333 wl_list_for_each_safe(view, next, &compositor->view_list, link)
5334 weston_view_geometry_dirty(view);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005335}
5336
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005337/** Transform device coordinates into global coordinates
5338 *
5339 * \param device_x[in] X coordinate in device units.
5340 * \param device_y[in] Y coordinate in device units.
5341 * \param x[out] X coordinate in the global space.
5342 * \param y[out] Y coordinate in the global space.
5343 *
5344 * Transforms coordinates from the device coordinate space
5345 * (physical pixel units) to the global coordinate space (logical pixel units).
5346 * This takes into account output transform and scale.
5347 *
5348 * \memberof weston_output
5349 * \internal
5350 */
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005351WL_EXPORT void
5352weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005353 double device_x, double device_y,
5354 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005355{
Derek Foreman0f679412014-10-02 13:41:17 -05005356 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005357 device_x,
5358 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05005359 0.0,
5360 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005361
Derek Foreman67a18b92015-03-24 11:36:14 -05005362 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005363
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005364 *x = p.f[0] / p.f[3];
5365 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005366}
5367
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005368/** Removes output from compositor's list of enabled outputs
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005369 *
5370 * \param output The weston_output object that is being removed.
5371 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005372 * The following happens:
5373 *
5374 * - The output assignments of all views in the current scenegraph are
5375 * recomputed.
5376 *
5377 * - Presentation feedback is discarded.
5378 *
5379 * - Compositor is notified that outputs were changed and
5380 * applies the necessary changes to re-layout outputs.
5381 *
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005382 * - The output is put back in the pending outputs list.
5383 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005384 * - Signal is emitted to notify all users of the weston_output
5385 * object that the output is being destroyed.
5386 *
5387 * - wl_output protocol objects referencing this weston_output
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005388 * are made inert, and the wl_output global is removed.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005389 *
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005390 * - The output's internal ID is released.
5391 *
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005392 * \memberof weston_output
5393 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005394 */
5395static void
5396weston_compositor_remove_output(struct weston_output *output)
5397{
Pekka Paalanenbccda712017-03-29 16:16:04 +03005398 struct weston_compositor *compositor = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005399 struct weston_view *view;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005400 struct weston_head *head;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005401
5402 assert(output->destroying);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005403 assert(output->enabled);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005404
Pekka Paalanenbccda712017-03-29 16:16:04 +03005405 wl_list_for_each(view, &compositor->view_list, link) {
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005406 if (view->output_mask & (1u << output->id))
5407 weston_view_assign_output(view);
5408 }
5409
5410 weston_presentation_feedback_discard_list(&output->feedback_list);
5411
Pekka Paalanenbccda712017-03-29 16:16:04 +03005412 weston_compositor_reflow_outputs(compositor, output, output->width);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005413
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005414 wl_list_remove(&output->link);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005415 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005416 output->enabled = false;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005417
Pekka Paalanenbccda712017-03-29 16:16:04 +03005418 wl_signal_emit(&compositor->output_destroyed_signal, output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005419 wl_signal_emit(&output->destroy_signal, output);
5420
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02005421 wl_list_for_each(head, &output->head_list, output_link)
5422 weston_head_remove_global(head);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005423
5424 compositor->output_id_pool &= ~(1u << output->id);
5425 output->id = 0xffffffff; /* invalid */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005426}
5427
5428/** Sets the output scale for a given output.
5429 *
5430 * \param output The weston_output object that the scale is set for.
5431 * \param scale Scale factor for the given output.
5432 *
5433 * It only supports setting scale for an output that
5434 * is not enabled and it can only be ran once.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005435 *
5436 * \memberof weston_output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005437 */
5438WL_EXPORT void
5439weston_output_set_scale(struct weston_output *output,
5440 int32_t scale)
5441{
5442 /* We can only set scale on a disabled output */
5443 assert(!output->enabled);
5444
5445 /* We only want to set scale once */
5446 assert(!output->scale);
5447
5448 output->scale = scale;
5449}
5450
5451/** Sets the output transform for a given output.
5452 *
5453 * \param output The weston_output object that the transform is set for.
5454 * \param transform Transform value for the given output.
5455 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005456 * Refer to wl_output::transform section located at
5457 * https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_output
5458 * for list of values that can be passed to this function.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005459 *
5460 * \memberof weston_output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005461 */
5462WL_EXPORT void
5463weston_output_set_transform(struct weston_output *output,
5464 uint32_t transform)
5465{
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005466 struct weston_pointer_motion_event ev;
5467 struct wl_resource *resource;
5468 struct weston_seat *seat;
5469 pixman_region32_t old_region;
5470 int mid_x, mid_y;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005471 struct weston_head *head;
5472 int ver;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005473
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005474 if (!output->enabled && output->transform == UINT32_MAX) {
5475 output->transform = transform;
5476 return;
5477 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005478
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005479 weston_output_transform_scale_init(output, transform, output->scale);
5480
5481 pixman_region32_init(&old_region);
5482 pixman_region32_copy(&old_region, &output->region);
5483
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005484 weston_output_init_geometry(output, output->x, output->y);
5485
5486 output->dirty = 1;
5487
5488 /* Notify clients of the change for output transform. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005489 wl_list_for_each(head, &output->head_list, output_link) {
5490 wl_resource_for_each(resource, &head->resource_list) {
5491 wl_output_send_geometry(resource,
5492 output->x,
5493 output->y,
5494 head->mm_width,
5495 head->mm_height,
5496 head->subpixel,
5497 head->make,
5498 head->model,
5499 output->transform);
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005500
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005501 ver = wl_resource_get_version(resource);
5502 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
5503 wl_output_send_done(resource);
5504 }
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005505 }
5506
5507 /* we must ensure that pointers are inside output, otherwise they disappear */
5508 mid_x = output->x + output->width / 2;
5509 mid_y = output->y + output->height / 2;
5510
5511 ev.mask = WESTON_POINTER_MOTION_ABS;
5512 ev.x = wl_fixed_to_double(wl_fixed_from_int(mid_x));
5513 ev.y = wl_fixed_to_double(wl_fixed_from_int(mid_y));
5514
5515 wl_list_for_each(seat, &output->compositor->seat_list, link) {
5516 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5517
5518 if (pointer && pixman_region32_contains_point(&old_region,
5519 wl_fixed_to_int(pointer->x),
5520 wl_fixed_to_int(pointer->y),
5521 NULL))
5522 weston_pointer_move(pointer, &ev);
5523 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005524}
5525
5526/** Initializes a weston_output object with enough data so
5527 ** an output can be configured.
5528 *
5529 * \param output The weston_output object to initialize
5530 * \param compositor The compositor instance.
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005531 * \param name Name for the output (the string is copied).
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005532 *
5533 * Sets initial values for fields that are expected to be
5534 * configured either by compositors or backends.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005535 *
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005536 * The name is used in logs, and can be used by compositors as a configuration
5537 * identifier.
5538 *
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005539 * \memberof weston_output
5540 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005541 */
5542WL_EXPORT void
Armin Krezović40087402016-09-30 14:11:12 +02005543weston_output_init(struct weston_output *output,
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005544 struct weston_compositor *compositor,
5545 const char *name)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005546{
5547 output->compositor = compositor;
5548 output->destroying = 0;
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005549 output->name = strdup(name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005550 wl_list_init(&output->link);
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02005551 wl_signal_init(&output->user_destroy_signal);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005552 output->enabled = false;
5553
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005554 wl_list_init(&output->head_list);
5555
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005556 /* Add some (in)sane defaults which can be used
5557 * for checking if an output was properly configured
5558 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005559 output->scale = 0;
5560 /* Can't use -1 on uint32_t and 0 is valid enum value */
5561 output->transform = UINT32_MAX;
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005562
5563 pixman_region32_init(&output->previous_damage);
5564 pixman_region32_init(&output->region);
Pekka Paalanen42704142017-09-06 16:47:52 +03005565 wl_list_init(&output->mode_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005566}
5567
5568/** Adds weston_output object to pending output list.
5569 *
5570 * \param output The weston_output object to add
5571 * \param compositor The compositor instance.
5572 *
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03005573 * The opposite of this operation is built into weston_output_release().
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005574 *
5575 * \memberof weston_output
5576 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005577 */
5578WL_EXPORT void
5579weston_compositor_add_pending_output(struct weston_output *output,
5580 struct weston_compositor *compositor)
5581{
Pekka Paalanene952a012017-03-29 17:14:00 +03005582 assert(output->disable);
5583 assert(output->enable);
5584
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005585 wl_list_remove(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005586 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005587}
5588
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02005589/** Create a string with the attached heads' names.
5590 *
5591 * The string must be free()'d.
5592 */
5593static char *
5594weston_output_create_heads_string(struct weston_output *output)
5595{
5596 FILE *fp;
5597 char *str = NULL;
5598 size_t size = 0;
5599 struct weston_head *head;
5600 const char *sep = "";
5601
5602 fp = open_memstream(&str, &size);
5603 if (!fp)
5604 return NULL;
5605
5606 wl_list_for_each(head, &output->head_list, output_link) {
5607 fprintf(fp, "%s%s", sep, head->name);
5608 sep = ", ";
5609 }
5610 fclose(fp);
5611
5612 return str;
5613}
5614
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005615/** Constructs a weston_output object that can be used by the compositor.
5616 *
Pekka Paalanencc201e42017-03-30 15:11:25 +03005617 * \param output The weston_output object that needs to be enabled. Must not
Pekka Paalanenddce54d2017-08-23 16:00:21 +03005618 * be enabled already. Must have at least one head attached.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005619 *
5620 * Output coordinates are calculated and each new output is by default
5621 * assigned to the right of previous one.
5622 *
5623 * Sets up the transformation, zoom, and geometry of the output using
5624 * the properties that need to be configured by the compositor.
5625 *
5626 * Establishes a repaint timer for the output with the relevant display
5627 * object's event loop. See output_repaint_timer_handler().
5628 *
5629 * The output is assigned an ID. Weston can support up to 32 distinct
5630 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
5631 * is referred to and used to find the first available ID number, and
5632 * then this ID is marked as used in output_id_pool.
5633 *
5634 * The output is also assigned a Wayland global with the wl_output
5635 * external interface.
5636 *
5637 * Backend specific function is called to set up the output output.
5638 *
5639 * Output is added to the compositor's output list
5640 *
5641 * If the backend specific function fails, the weston_output object
5642 * is returned to a state it was before calling this function and
5643 * is added to the compositor's pending_output_list in case it needs
5644 * to be reconfigured or just so it can be destroyed at shutdown.
5645 *
5646 * 0 is returned on success, -1 on failure.
5647 */
5648WL_EXPORT int
5649weston_output_enable(struct weston_output *output)
5650{
Armin Krezović782f5df2016-09-30 14:11:11 +02005651 struct weston_compositor *c = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005652 struct weston_output *iterator;
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03005653 struct weston_head *head;
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02005654 char *head_names;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005655 int x = 0, y = 0;
5656
Pekka Paalanencc201e42017-03-30 15:11:25 +03005657 if (output->enabled) {
5658 weston_log("Error: attempt to enable an enabled output '%s'\n",
5659 output->name);
5660 return -1;
5661 }
5662
Pekka Paalanenddce54d2017-08-23 16:00:21 +03005663 if (wl_list_empty(&output->head_list)) {
5664 weston_log("Error: cannot enable output '%s' without heads.\n",
5665 output->name);
5666 return -1;
5667 }
5668
Pekka Paalanen586e1ac2017-09-14 16:17:59 +03005669 if (wl_list_empty(&output->mode_list) || !output->current_mode) {
5670 weston_log("Error: no video mode for output '%s'.\n",
5671 output->name);
5672 return -1;
5673 }
5674
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03005675 wl_list_for_each(head, &output->head_list, output_link) {
5676 assert(head->make);
5677 assert(head->model);
5678 }
5679
Armin Krezović782f5df2016-09-30 14:11:11 +02005680 iterator = container_of(c->output_list.prev,
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005681 struct weston_output, link);
5682
Armin Krezović782f5df2016-09-30 14:11:11 +02005683 if (!wl_list_empty(&c->output_list))
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005684 x = iterator->x + iterator->width;
5685
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005686 /* Make sure the scale is set up */
5687 assert(output->scale);
5688
5689 /* Make sure we have a transform set */
5690 assert(output->transform != UINT32_MAX);
5691
Armin Krezović782f5df2016-09-30 14:11:11 +02005692 output->x = x;
5693 output->y = y;
5694 output->dirty = 1;
5695 output->original_scale = output->scale;
5696
5697 weston_output_transform_scale_init(output, output->transform, output->scale);
5698 weston_output_init_zoom(output);
5699
5700 weston_output_init_geometry(output, x, y);
5701 weston_output_damage(output);
5702
5703 wl_signal_init(&output->frame_signal);
5704 wl_signal_init(&output->destroy_signal);
5705 wl_list_init(&output->animation_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02005706 wl_list_init(&output->feedback_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02005707
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005708 /* Enable the output (set up the crtc or create a
5709 * window representing the output, set up the
5710 * renderer, etc)
5711 */
5712 if (output->enable(output) < 0) {
5713 weston_log("Enabling output \"%s\" failed.\n", output->name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005714 return -1;
5715 }
5716
5717 weston_compositor_add_output(output->compositor, output);
5718
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02005719 head_names = weston_output_create_heads_string(output);
5720 weston_log("Output '%s' enabled with head(s) %s\n",
5721 output->name, head_names);
5722 free(head_names);
5723
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005724 return 0;
5725}
5726
5727/** Converts a weston_output object to a pending output state, so it
5728 ** can be configured again or destroyed.
5729 *
5730 * \param output The weston_output object that needs to be disabled.
5731 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005732 * Calls a backend specific function to disable an output, in case
5733 * such function exists.
5734 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03005735 * The backend specific disable function may choose to postpone the disabling
5736 * by returning a negative value, in which case this function returns early.
5737 * In that case the backend will guarantee the output will be disabled soon
5738 * by the backend calling this function again. One must not attempt to re-enable
5739 * the output until that happens.
5740 *
5741 * Otherwise, if the output is being used by the compositor, it is removed
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005742 * from weston's output_list (see weston_compositor_remove_output())
5743 * and is returned to a state it was before weston_output_enable()
5744 * was ran (see weston_output_enable_undo()).
5745 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03005746 * See weston_output_init() for more information on the
5747 * state output is returned to.
Pekka Paalanencc201e42017-03-30 15:11:25 +03005748 *
5749 * If the output has never been enabled yet, this function can still be
5750 * called to ensure that the output is actually turned off rather than left
5751 * in the state it was discovered in.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005752 */
5753WL_EXPORT void
5754weston_output_disable(struct weston_output *output)
5755{
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005756 /* Should we rename this? */
5757 output->destroying = 1;
5758
Pekka Paalanenc65df642017-03-29 15:45:46 +03005759 /* Disable is called unconditionally also for not-enabled outputs,
5760 * because at compositor start-up, if there is an output that is
5761 * already on but the compositor wants to turn it off, we have to
5762 * forward the turn-off to the backend so it knows to do it.
5763 * The backend cannot initially turn off everything, because it
5764 * would cause unnecessary mode-sets for all outputs the compositor
5765 * wants to be on.
5766 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005767 if (output->disable(output) < 0)
5768 return;
5769
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005770 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005771 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005772
5773 output->destroying = 0;
5774}
5775
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03005776/** Forces a synchronous call to heads_changed hook
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005777 *
5778 * \param compositor The compositor instance
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03005779 *
5780 * If there are new or changed heads, calls the heads_changed hook and
5781 * returns after the hook returns.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005782 */
5783WL_EXPORT void
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03005784weston_compositor_flush_heads_changed(struct weston_compositor *compositor)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005785{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005786 if (compositor->heads_changed_source) {
5787 wl_event_source_remove(compositor->heads_changed_source);
5788 weston_compositor_call_heads_changed(compositor);
5789 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005790}
5791
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02005792/** Add destroy callback for an output
5793 *
5794 * \param output The output to watch.
5795 * \param listener The listener to add. The \c notify member must be set.
5796 *
5797 * The listener callback will be called when user destroys an output. This
5798 * may be delayed by a backend in some cases. The main purpose of the
5799 * listener is to allow hooking up custom data to the output. The custom data
5800 * can be fetched via weston_output_get_destroy_listener() followed by
5801 * container_of().
5802 *
5803 * The \c data argument to the notify callback is the weston_output being
5804 * destroyed.
5805 *
5806 * @note This is for the final destruction of an output, not when it gets
5807 * disabled. If you want to keep track of enabled outputs, this is not it.
5808 */
5809WL_EXPORT void
5810weston_output_add_destroy_listener(struct weston_output *output,
5811 struct wl_listener *listener)
5812{
5813 wl_signal_add(&output->user_destroy_signal, listener);
5814}
5815
5816/** Look up destroy listener for an output
5817 *
5818 * \param output The output to query.
5819 * \param notify The notify function used used for the added destroy listener.
5820 * \return The listener, or NULL if not found.
5821 *
5822 * This looks up the previously added destroy listener struct based on the
5823 * notify function it has. The listener can be used to access user data
5824 * through \c container_of().
5825 *
5826 * \sa wl_signal_get() weston_output_add_destroy_listener()
5827 */
5828WL_EXPORT struct wl_listener *
5829weston_output_get_destroy_listener(struct weston_output *output,
5830 wl_notify_func_t notify)
5831{
5832 return wl_signal_get(&output->user_destroy_signal, notify);
5833}
5834
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005835/** Uninitialize an output
5836 *
5837 * Removes the output from the list of enabled outputs if necessary, but
5838 * does not call the backend's output disable function. The output will no
5839 * longer be in the list of pending outputs either.
5840 *
5841 * All fields of weston_output become uninitialized, i.e. should not be used
5842 * anymore. The caller can free the memory after this.
5843 *
5844 * \memberof weston_output
5845 * \internal
5846 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005847WL_EXPORT void
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03005848weston_output_release(struct weston_output *output)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005849{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005850 struct weston_head *head, *tmp;
5851
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005852 output->destroying = 1;
5853
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02005854 wl_signal_emit(&output->user_destroy_signal, output);
5855
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03005856 if (output->idle_repaint_source)
5857 wl_event_source_remove(output->idle_repaint_source);
5858
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005859 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005860 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005861
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005862 pixman_region32_fini(&output->region);
5863 pixman_region32_fini(&output->previous_damage);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005864 wl_list_remove(&output->link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005865
5866 wl_list_for_each_safe(head, tmp, &output->head_list, output_link)
5867 weston_head_detach(head);
5868
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005869 free(output->name);
5870}
5871
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02005872/** Find an output by its given name
5873 *
5874 * \param compositor The compositor to search in.
5875 * \param name The output name to search for.
5876 * \return An existing output with the given name, or NULL if not found.
5877 *
5878 * \memberof weston_compositor
5879 */
5880WL_EXPORT struct weston_output *
5881weston_compositor_find_output_by_name(struct weston_compositor *compositor,
5882 const char *name)
5883{
5884 struct weston_output *output;
5885
5886 wl_list_for_each(output, &compositor->output_list, link)
5887 if (strcmp(output->name, name) == 0)
5888 return output;
5889
5890 wl_list_for_each(output, &compositor->pending_output_list, link)
5891 if (strcmp(output->name, name) == 0)
5892 return output;
5893
5894 return NULL;
5895}
5896
5897/** Create a named output
5898 *
5899 * \param compositor The compositor.
5900 * \param name The name for the output.
5901 * \return A new \c weston_output, or NULL on failure.
5902 *
5903 * This creates a new weston_output that starts with no heads attached.
5904 *
5905 * An output must be configured and it must have at least one head before
5906 * it can be enabled.
5907 *
5908 * \memberof weston_compositor
5909 */
5910WL_EXPORT struct weston_output *
5911weston_compositor_create_output(struct weston_compositor *compositor,
5912 const char *name)
5913{
5914 assert(compositor->backend->create_output);
5915
5916 if (weston_compositor_find_output_by_name(compositor, name)) {
5917 weston_log("Warning: attempted to create an output with a "
5918 "duplicate name '%s'.\n", name);
5919 return NULL;
5920 }
5921
5922 return compositor->backend->create_output(compositor, name);
5923}
5924
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005925/** Create an output for an unused head
5926 *
5927 * \param compositor The compositor.
5928 * \param head The head to attach to the output.
5929 * \return A new \c weston_output, or NULL on failure.
5930 *
5931 * This creates a new weston_output that starts with the given head attached.
5932 * The output inherits the name of the head. The head must not be already
5933 * attached to another output.
5934 *
5935 * An output must be configured before it can be enabled.
5936 *
5937 * \memberof weston_compositor
5938 */
5939WL_EXPORT struct weston_output *
5940weston_compositor_create_output_with_head(struct weston_compositor *compositor,
5941 struct weston_head *head)
5942{
5943 struct weston_output *output;
5944
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02005945 output = weston_compositor_create_output(compositor, head->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005946 if (!output)
5947 return NULL;
5948
5949 if (weston_output_attach_head(output, head) < 0) {
Pekka Paalanen42c0e142017-10-27 12:07:49 +03005950 weston_output_destroy(output);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005951 return NULL;
5952 }
5953
5954 return output;
5955}
5956
5957/** Destroy an output
5958 *
5959 * \param output The output to destroy.
5960 *
5961 * The heads attached to the given output are detached and become unused again.
5962 *
5963 * It is not necessary to explicitly destroy all outputs at compositor exit.
5964 * weston_compositor_destroy() will automatically destroy any remaining
5965 * outputs.
5966 *
5967 * \memberof weston_output
5968 */
5969WL_EXPORT void
5970weston_output_destroy(struct weston_output *output)
5971{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005972 output->destroy(output);
5973}
5974
Pekka Paalanencf0a4762017-04-04 16:36:07 +03005975/** When you need a head...
5976 *
5977 * This function is a hack, used until all code has been converted to become
5978 * multi-head aware.
5979 *
5980 * \param output The weston_output whose head to get.
5981 * \return The first head in the output's list.
5982 */
5983WL_EXPORT struct weston_head *
5984weston_output_get_first_head(struct weston_output *output)
5985{
5986 if (wl_list_empty(&output->head_list))
5987 return NULL;
5988
5989 return container_of(output->head_list.next,
5990 struct weston_head, output_link);
5991}
5992
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01005993static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005994destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01005995{
Jonny Lamb74130762013-11-26 18:19:46 +01005996 struct weston_surface *surface =
5997 wl_resource_get_user_data(resource);
5998
Pekka Paalanen4826f872016-04-22 14:14:38 +03005999 if (!surface)
6000 return;
6001
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006002 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02006003 surface->pending.buffer_viewport.buffer.src_width =
6004 wl_fixed_from_int(-1);
6005 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006006 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006007}
6008
6009static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006010viewport_destroy(struct wl_client *client,
6011 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006012{
6013 wl_resource_destroy(resource);
6014}
6015
6016static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006017viewport_set_source(struct wl_client *client,
6018 struct wl_resource *resource,
6019 wl_fixed_t src_x,
6020 wl_fixed_t src_y,
6021 wl_fixed_t src_width,
6022 wl_fixed_t src_height)
6023{
6024 struct weston_surface *surface =
6025 wl_resource_get_user_data(resource);
6026
Pekka Paalanen4826f872016-04-22 14:14:38 +03006027 if (!surface) {
6028 wl_resource_post_error(resource,
6029 WP_VIEWPORT_ERROR_NO_SURFACE,
6030 "wl_surface for this viewport is no longer exists");
6031 return;
6032 }
6033
6034 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03006035 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006036
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006037 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03006038 src_height == wl_fixed_from_int(-1) &&
6039 src_x == wl_fixed_from_int(-1) &&
6040 src_y == wl_fixed_from_int(-1)) {
6041 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006042 surface->pending.buffer_viewport.buffer.src_width =
6043 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006044 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006045 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006046 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006047
Pekka Paalanen201769a2016-04-26 14:42:11 +03006048 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006049 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006050 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03006051 "wl_surface@%d viewport source "
6052 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
6053 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006054 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03006055 wl_fixed_to_double(src_height),
6056 wl_fixed_to_double(src_x),
6057 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006058 return;
6059 }
6060
6061 surface->pending.buffer_viewport.buffer.src_x = src_x;
6062 surface->pending.buffer_viewport.buffer.src_y = src_y;
6063 surface->pending.buffer_viewport.buffer.src_width = src_width;
6064 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006065 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006066}
6067
6068static void
6069viewport_set_destination(struct wl_client *client,
6070 struct wl_resource *resource,
6071 int32_t dst_width,
6072 int32_t dst_height)
6073{
6074 struct weston_surface *surface =
6075 wl_resource_get_user_data(resource);
6076
Pekka Paalanen4826f872016-04-22 14:14:38 +03006077 if (!surface) {
6078 wl_resource_post_error(resource,
6079 WP_VIEWPORT_ERROR_NO_SURFACE,
6080 "wl_surface for this viewport no longer exists");
6081 return;
6082 }
6083
6084 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006085
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006086 if (dst_width == -1 && dst_height == -1) {
6087 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006088 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006089 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006090 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006091 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006092
6093 if (dst_width <= 0 || dst_height <= 0) {
6094 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006095 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006096 "destination size must be positive (%dx%d)",
6097 dst_width, dst_height);
6098 return;
6099 }
6100
6101 surface->pending.buffer_viewport.surface.width = dst_width;
6102 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006103 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006104}
6105
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006106static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006107 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006108 viewport_set_source,
6109 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01006110};
6111
6112static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006113viewporter_destroy(struct wl_client *client,
6114 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006115{
6116 wl_resource_destroy(resource);
6117}
6118
6119static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006120viewporter_get_viewport(struct wl_client *client,
6121 struct wl_resource *viewporter,
6122 uint32_t id,
6123 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006124{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006125 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006126 struct weston_surface *surface =
6127 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006128 struct wl_resource *resource;
6129
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006130 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006131 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006132 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006133 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01006134 return;
6135 }
6136
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006137 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006138 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006139 if (resource == NULL) {
6140 wl_client_post_no_memory(client);
6141 return;
6142 }
6143
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006144 wl_resource_set_implementation(resource, &viewport_interface,
6145 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01006146
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006147 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006148}
6149
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006150static const struct wp_viewporter_interface viewporter_interface = {
6151 viewporter_destroy,
6152 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01006153};
6154
6155static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006156bind_viewporter(struct wl_client *client,
6157 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006158{
6159 struct wl_resource *resource;
6160
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006161 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006162 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006163 if (resource == NULL) {
6164 wl_client_post_no_memory(client);
6165 return;
6166 }
6167
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006168 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01006169 NULL, NULL);
6170}
6171
6172static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04006173destroy_presentation_feedback(struct wl_resource *feedback_resource)
6174{
6175 struct weston_presentation_feedback *feedback;
6176
6177 feedback = wl_resource_get_user_data(feedback_resource);
6178
6179 wl_list_remove(&feedback->link);
6180 free(feedback);
6181}
6182
6183static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006184presentation_destroy(struct wl_client *client, struct wl_resource *resource)
6185{
6186 wl_resource_destroy(resource);
6187}
6188
6189static void
6190presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04006191 struct wl_resource *presentation_resource,
6192 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006193 uint32_t callback)
6194{
Pekka Paalanen133e4392014-09-23 22:08:46 -04006195 struct weston_surface *surface;
6196 struct weston_presentation_feedback *feedback;
6197
6198 surface = wl_resource_get_user_data(surface_resource);
6199
Bryce Harringtonde16d892014-11-20 22:21:57 -08006200 feedback = zalloc(sizeof *feedback);
6201 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04006202 goto err_calloc;
6203
6204 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006205 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04006206 1, callback);
6207 if (!feedback->resource)
6208 goto err_create;
6209
6210 wl_resource_set_implementation(feedback->resource, NULL, feedback,
6211 destroy_presentation_feedback);
6212
6213 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
6214
6215 return;
6216
6217err_create:
6218 free(feedback);
6219
6220err_calloc:
6221 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006222}
6223
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006224static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006225 presentation_destroy,
6226 presentation_feedback
6227};
6228
6229static void
6230bind_presentation(struct wl_client *client,
6231 void *data, uint32_t version, uint32_t id)
6232{
6233 struct weston_compositor *compositor = data;
6234 struct wl_resource *resource;
6235
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006236 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006237 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006238 if (resource == NULL) {
6239 wl_client_post_no_memory(client);
6240 return;
6241 }
6242
6243 wl_resource_set_implementation(resource, &presentation_implementation,
6244 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006245 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006246}
6247
6248static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05006249compositor_bind(struct wl_client *client,
6250 void *data, uint32_t version, uint32_t id)
6251{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006252 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05006253 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05006254
Jason Ekstranda85118c2013-06-27 20:17:02 -05006255 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006256 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07006257 if (resource == NULL) {
6258 wl_client_post_no_memory(client);
6259 return;
6260 }
6261
6262 wl_resource_set_implementation(resource, &compositor_interface,
6263 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05006264}
6265
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006266WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006267weston_environment_get_fd(const char *env)
6268{
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07006269 char *e;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006270 int fd, flags;
6271
6272 e = getenv(env);
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07006273 if (!e || !safe_strtoint(e, &fd))
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006274 return -1;
6275
6276 flags = fcntl(fd, F_GETFD);
6277 if (flags == -1)
6278 return -1;
6279
6280 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
6281 unsetenv(env);
6282
6283 return fd;
6284}
6285
Pekka Paalanenb5026542014-11-12 15:09:24 +02006286static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02006287timeline_key_binding_handler(struct weston_keyboard *keyboard,
6288 const struct timespec *time, uint32_t key,
6289 void *data)
Pekka Paalanenb5026542014-11-12 15:09:24 +02006290{
6291 struct weston_compositor *compositor = data;
6292
6293 if (weston_timeline_enabled_)
6294 weston_timeline_close();
6295 else
6296 weston_timeline_open(compositor);
6297}
6298
Giulio Camuffo459137b2014-10-11 23:56:24 +03006299/** Create the compositor.
6300 *
6301 * This functions creates and initializes a compositor instance.
6302 *
6303 * \param display The Wayland display to be used.
6304 * \param user_data A pointer to an object that can later be retrieved
6305 * using the \ref weston_compositor_get_user_data function.
6306 * \return The compositor instance on success or NULL on failure.
6307 */
6308WL_EXPORT struct weston_compositor *
6309weston_compositor_create(struct wl_display *display, void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04006310{
Giulio Camuffo459137b2014-10-11 23:56:24 +03006311 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05006312 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07006313
Giulio Camuffo459137b2014-10-11 23:56:24 +03006314 ec = zalloc(sizeof *ec);
6315 if (!ec)
6316 return NULL;
6317
6318 ec->wl_display = display;
6319 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006320 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07006321 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006322 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006323 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006324 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006325 wl_signal_init(&ec->idle_signal);
6326 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006327 wl_signal_init(&ec->show_input_panel_signal);
6328 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02006329 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01006330 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01006331 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02006332 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006333 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02006334 wl_signal_init(&ec->output_resized_signal);
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03006335 wl_signal_init(&ec->heads_changed_signal);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02006336 wl_signal_init(&ec->output_heads_changed_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07006337 wl_signal_init(&ec->session_signal);
6338 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04006339
Casey Dahlin58ba1372012-04-19 22:50:08 -04006340 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03006341 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04006342
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02006343 ec->activate_serial = 1;
6344
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05006345 ec->touch_mode = WESTON_TOUCH_MODE_NORMAL;
6346
Derek Foreman152254b2015-11-26 14:17:48 -06006347 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006348 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006349 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05006350
Giulio Camuffo954f1832014-10-11 18:27:30 +03006351 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006352 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006353 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03006354
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006355 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006356 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006357 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006358
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006359 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006360 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006361 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006362
Jonas Ådahl30d61d82014-10-22 21:21:17 +02006363 if (weston_input_init(ec) != 0)
6364 goto fail;
6365
Jason Ekstranda7af7042013-10-12 22:38:11 -05006366 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02006367 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006368 wl_list_init(&ec->layer_list);
6369 wl_list_init(&ec->seat_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006370 wl_list_init(&ec->pending_output_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006371 wl_list_init(&ec->output_list);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03006372 wl_list_init(&ec->head_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006373 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01006374 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006375 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01006376 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006377 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006378 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006379
Pekka Paalanen827b5d22016-06-29 11:54:26 +02006380 wl_list_init(&ec->plugin_api_list);
6381
Xiong Zhang97116532013-10-23 13:58:31 +08006382 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02006383 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04006384
Giulio Camuffo459137b2014-10-11 23:56:24 +03006385 wl_data_device_manager_init(ec->wl_display);
6386
6387 wl_display_init_shm(ec->wl_display);
6388
6389 loop = wl_display_get_event_loop(ec->wl_display);
6390 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Daniel Stone6847b852017-03-01 11:34:08 +00006391 ec->repaint_timer =
6392 wl_event_loop_add_timer(loop, output_repaint_timer_handler,
6393 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03006394
Quentin Glidic82681572016-12-17 13:40:51 +01006395 weston_layer_init(&ec->fade_layer, ec);
6396 weston_layer_init(&ec->cursor_layer, ec);
6397
6398 weston_layer_set_position(&ec->fade_layer, WESTON_LAYER_POSITION_FADE);
6399 weston_layer_set_position(&ec->cursor_layer,
6400 WESTON_LAYER_POSITION_CURSOR);
Giulio Camuffo459137b2014-10-11 23:56:24 +03006401
6402 weston_compositor_add_debug_binding(ec, KEY_T,
6403 timeline_key_binding_handler, ec);
6404
Giulio Camuffo459137b2014-10-11 23:56:24 +03006405 return ec;
6406
6407fail:
6408 free(ec);
6409 return NULL;
6410}
6411
Benjamin Franzkeb8263022011-08-30 11:32:47 +02006412WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006413weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07006414{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006415 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07006416
Pekka Paalanend1591ae2012-01-02 16:06:56 +02006417 wl_event_source_remove(ec->idle_source);
6418
Matt Roper361d2ad2011-08-29 13:52:23 -07006419 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02006420 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07006421 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02006422
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006423 /* Destroy all pending outputs associated with this compositor */
6424 wl_list_for_each_safe(output, next, &ec->pending_output_list, link)
6425 output->destroy(output);
6426
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02006427 if (ec->renderer)
6428 ec->renderer->destroy(ec);
6429
Daniel Stone325fc2d2012-05-30 16:31:58 +01006430 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09006431 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006432 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01006433 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006434 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006435 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02006436
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04006437 weston_plane_release(&ec->primary_plane);
Matt Roper361d2ad2011-08-29 13:52:23 -07006438}
6439
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05006440WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04006441weston_compositor_exit_with_code(struct weston_compositor *compositor,
6442 int exit_code)
6443{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02006444 if (compositor->exit_code == EXIT_SUCCESS)
6445 compositor->exit_code = exit_code;
6446
Giulio Camuffo459137b2014-10-11 23:56:24 +03006447 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04006448}
6449
6450WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01006451weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
6452 const struct weston_pointer_grab_interface *interface)
6453{
6454 struct weston_seat *seat;
6455
6456 ec->default_pointer_grab = interface;
6457 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05006458 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
6459
6460 if (pointer)
6461 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01006462 }
6463}
6464
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04006465WL_EXPORT int
6466weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
6467 clockid_t clk_id)
6468{
6469 struct timespec ts;
6470
6471 if (clock_gettime(clk_id, &ts) < 0)
6472 return -1;
6473
6474 compositor->presentation_clock = clk_id;
6475
6476 return 0;
6477}
6478
6479/*
6480 * For choosing the software clock, when the display hardware or API
6481 * does not expose a compatible presentation timestamp.
6482 */
6483WL_EXPORT int
6484weston_compositor_set_presentation_clock_software(
6485 struct weston_compositor *compositor)
6486{
6487 /* In order of preference */
6488 static const clockid_t clocks[] = {
6489 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
6490 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
6491 CLOCK_MONOTONIC, /* no jumps, may crawl */
6492 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
6493 CLOCK_REALTIME /* may jump and crawl */
6494 };
6495 unsigned i;
6496
6497 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
6498 if (weston_compositor_set_presentation_clock(compositor,
6499 clocks[i]) == 0)
6500 return 0;
6501
6502 weston_log("Error: no suitable presentation clock available.\n");
6503
6504 return -1;
6505}
6506
Pekka Paalanen662f3842015-03-18 12:17:26 +02006507/** Read the current time from the Presentation clock
6508 *
6509 * \param compositor
6510 * \param ts[out] The current time.
6511 *
6512 * \note Reading the current time in user space is always imprecise to some
6513 * degree.
6514 *
6515 * This function is never meant to fail. If reading the clock does fail,
6516 * an error message is logged and a zero time is returned. Callers are not
6517 * supposed to detect or react to failures.
6518 */
6519WL_EXPORT void
6520weston_compositor_read_presentation_clock(
6521 const struct weston_compositor *compositor,
6522 struct timespec *ts)
6523{
6524 static bool warned;
6525 int ret;
6526
6527 ret = clock_gettime(compositor->presentation_clock, ts);
6528 if (ret < 0) {
6529 ts->tv_sec = 0;
6530 ts->tv_nsec = 0;
6531
6532 if (!warned)
6533 weston_log("Error: failure to read "
6534 "the presentation clock %#x: '%m' (%d)\n",
6535 compositor->presentation_clock, errno);
6536 warned = true;
6537 }
6538}
6539
Pekka Paalanen230f3b12014-09-29 14:18:40 -04006540/** Import dmabuf buffer into current renderer
6541 *
6542 * \param compositor
6543 * \param buffer the dmabuf buffer to import
6544 * \return true on usable buffers, false otherwise
6545 *
6546 * This function tests that the linux_dmabuf_buffer is usable
6547 * for the current renderer. Returns false on unusable buffers. Usually
6548 * usability is tested by importing the dmabufs for composition.
6549 *
6550 * This hook is also used for detecting if the renderer supports
6551 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
6552 * supported.
6553 * */
6554WL_EXPORT bool
6555weston_compositor_import_dmabuf(struct weston_compositor *compositor,
6556 struct linux_dmabuf_buffer *buffer)
6557{
6558 struct weston_renderer *renderer;
6559
6560 renderer = compositor->renderer;
6561
6562 if (renderer->import_dmabuf == NULL)
6563 return false;
6564
6565 return renderer->import_dmabuf(compositor, buffer);
6566}
6567
Giulio Camuffocdb4d292013-11-14 23:42:53 +01006568WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05006569weston_version(int *major, int *minor, int *micro)
6570{
6571 *major = WESTON_VERSION_MAJOR;
6572 *minor = WESTON_VERSION_MINOR;
6573 *micro = WESTON_VERSION_MICRO;
6574}
6575
Daniel Stonee03c1112016-11-24 20:45:45 +00006576/**
6577 * Attempts to find a module path from the module map specified in the
6578 * environment. If found, writes the full path into the path variable.
6579 *
6580 * The module map is a string in environment variable WESTON_MODULE_MAP, where
6581 * each entry is of the form "name=path" and entries are separated by
6582 * semicolons. Whitespace is significant.
6583 *
6584 * \param name The name to search for.
6585 * \param path Where the path is written to if found.
6586 * \param path_len Allocated bytes at \c path .
6587 * \returns The length of the string written to path on success, or 0 if the
6588 * module was not specified in the environment map or path_len was too small.
6589 */
6590WL_EXPORT size_t
6591weston_module_path_from_env(const char *name, char *path, size_t path_len)
6592{
6593 const char *mapping = getenv("WESTON_MODULE_MAP");
6594 const char *end;
6595 const int name_len = strlen(name);
6596
6597 if (!mapping)
6598 return 0;
6599
6600 end = mapping + strlen(mapping);
6601 while (mapping < end && *mapping) {
6602 const char *filename, *next;
6603
6604 /* early out: impossibly short string */
6605 if (end - mapping < name_len + 1)
6606 return 0;
6607
6608 filename = &mapping[name_len + 1];
6609 next = strchrnul(mapping, ';');
6610
6611 if (strncmp(mapping, name, name_len) == 0 &&
6612 mapping[name_len] == '=') {
6613 size_t file_len = next - filename; /* no trailing NUL */
6614 if (file_len >= path_len)
6615 return 0;
6616 strncpy(path, filename, file_len);
6617 path[file_len] = '\0';
6618 return file_len;
6619 }
6620
6621 mapping = next + 1;
6622 }
6623
6624 return 0;
6625}
6626
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03006627WL_EXPORT void *
6628weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006629{
6630 char path[PATH_MAX];
6631 void *module, *init;
Daniel Stonebeb97e52016-11-28 12:13:54 +00006632 size_t len;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006633
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08006634 if (name == NULL)
6635 return NULL;
6636
Derek Foreman3f86e502015-06-08 11:46:54 -05006637 if (name[0] != '/') {
Daniel Stonee03c1112016-11-24 20:45:45 +00006638 len = weston_module_path_from_env(name, path, sizeof path);
6639 if (len == 0)
Daniel Stonebeb97e52016-11-28 12:13:54 +00006640 len = snprintf(path, sizeof path, "%s/%s",
6641 LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05006642 } else {
Daniel Stonebeb97e52016-11-28 12:13:54 +00006643 len = snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05006644 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006645
Daniel Stonebeb97e52016-11-28 12:13:54 +00006646 /* snprintf returns the length of the string it would've written,
6647 * _excluding_ the NUL byte. So even being equal to the size of
6648 * our buffer is an error here. */
6649 if (len >= sizeof path)
6650 return NULL;
6651
Kristian Høgsberga6813d22012-09-12 12:21:01 -04006652 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
6653 if (module) {
6654 weston_log("Module '%s' already loaded\n", path);
6655 dlclose(module);
6656 return NULL;
6657 }
6658
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03006659 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04006660 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006661 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03006662 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006663 return NULL;
6664 }
6665
6666 init = dlsym(module, entrypoint);
6667 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03006668 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00006669 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006670 return NULL;
6671 }
6672
6673 return init;
6674}
6675
Giulio Camuffo459137b2014-10-11 23:56:24 +03006676
6677/** Destroys the compositor.
6678 *
6679 * This function cleans up the compositor state and destroys it.
6680 *
6681 * \param compositor The compositor to be destroyed.
6682 */
6683WL_EXPORT void
6684weston_compositor_destroy(struct weston_compositor *compositor)
6685{
6686 /* prevent further rendering while shutting down */
6687 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
6688
6689 wl_signal_emit(&compositor->destroy_signal, compositor);
6690
6691 weston_compositor_xkb_destroy(compositor);
6692
Giulio Camuffo2d24e642015-10-03 16:25:15 +03006693 if (compositor->backend)
6694 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02006695
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03006696 /* The backend is responsible for destroying the heads. */
6697 assert(wl_list_empty(&compositor->head_list));
6698
Pekka Paalanen827b5d22016-06-29 11:54:26 +02006699 weston_plugin_api_destroy_list(compositor);
6700
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03006701 if (compositor->heads_changed_source)
6702 wl_event_source_remove(compositor->heads_changed_source);
6703
Giulio Camuffo459137b2014-10-11 23:56:24 +03006704 free(compositor);
6705}
6706
6707/** Instruct the compositor to exit.
6708 *
6709 * This functions does not directly destroy the compositor object, it merely
6710 * command it to start the tear down process. It is not guaranteed that the
6711 * tear down will happen immediately.
6712 *
6713 * \param compositor The compositor to tear down.
6714 */
6715WL_EXPORT void
6716weston_compositor_exit(struct weston_compositor *compositor)
6717{
6718 compositor->exit(compositor);
6719}
6720
6721/** Return the user data stored in the compositor.
6722 *
6723 * This function returns the user data pointer set with user_data parameter
6724 * to the \ref weston_compositor_create function.
6725 */
6726WL_EXPORT void *
6727weston_compositor_get_user_data(struct weston_compositor *compositor)
6728{
6729 return compositor->user_data;
6730}
Pekka Paalanendd186732016-06-03 14:49:54 +03006731
Pekka Paalanen50dbf382016-06-03 15:23:46 +03006732static const char * const backend_map[] = {
6733 [WESTON_BACKEND_DRM] = "drm-backend.so",
6734 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
6735 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
6736 [WESTON_BACKEND_RDP] = "rdp-backend.so",
6737 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
6738 [WESTON_BACKEND_X11] = "x11-backend.so",
6739};
6740
Pekka Paalanendd186732016-06-03 14:49:54 +03006741/** Load a backend into a weston_compositor
6742 *
6743 * A backend must be loaded to make a weston_compositor work. A backend
6744 * provides input and output capabilities, and determines the renderer to use.
6745 *
6746 * \param compositor A compositor that has not had a backend loaded yet.
6747 * \param backend Name of the backend file.
6748 * \param config_base A pointer to a backend-specific configuration
6749 * structure's 'base' member.
6750 *
6751 * \return 0 on success, or -1 on error.
6752 */
6753WL_EXPORT int
6754weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03006755 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03006756 struct weston_backend_config *config_base)
6757{
6758 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03006759 struct weston_backend_config *config_base);
6760
Pekka Paalanend7e35112017-08-29 17:04:12 +03006761 if (compositor->backend) {
6762 weston_log("Error: attempt to load a backend when one is already loaded\n");
6763 return -1;
6764 }
6765
Quentin Glidic887c0182016-07-10 11:00:53 +02006766 if (backend >= ARRAY_LENGTH(backend_map))
Pekka Paalanen50dbf382016-06-03 15:23:46 +03006767 return -1;
6768
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01006769 backend_init = weston_load_module(backend_map[backend], "weston_backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03006770 if (!backend_init)
6771 return -1;
6772
Pekka Paalanend7e35112017-08-29 17:04:12 +03006773 if (backend_init(compositor, config_base) < 0) {
6774 compositor->backend = NULL;
6775 return -1;
6776 }
6777
6778 return 0;
Pekka Paalanendd186732016-06-03 14:49:54 +03006779}
Giulio Camuffo9c764df2016-06-29 11:54:27 +02006780
6781WL_EXPORT int
6782weston_compositor_load_xwayland(struct weston_compositor *compositor)
6783{
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01006784 int (*module_init)(struct weston_compositor *ec);
Giulio Camuffo9c764df2016-06-29 11:54:27 +02006785
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01006786 module_init = weston_load_module("xwayland.so", "weston_module_init");
Giulio Camuffo9c764df2016-06-29 11:54:27 +02006787 if (!module_init)
6788 return -1;
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01006789 if (module_init(compositor) < 0)
Giulio Camuffo9c764df2016-06-29 11:54:27 +02006790 return -1;
6791 return 0;
6792}