blob: 0f1f01fb59819deff13e1518f914def5aeb0fdba [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>
Alexandros Frantzisacff29b2018-10-19 12:14:11 +030054#include <inttypes.h>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050055
Pekka Paalanenb5026542014-11-12 15:09:24 +020056#include "timeline.h"
57
Kristian Høgsberg82863022010-06-04 21:52:02 -040058#include "compositor.h"
Daniel Stonece62cb32018-07-20 09:46:24 +010059#include "weston-debug.h"
60#include "linux-dmabuf.h"
Pekka Paalanene95ad5c2016-04-15 14:47:08 +030061#include "viewporter-server-protocol.h"
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020062#include "presentation-time-server-protocol.h"
Alexandros Frantzisacff29b2018-10-19 12:14:11 +030063#include "linux-explicit-synchronization-unstable-v1-server-protocol.h"
Alexandros Frantzis67629672018-10-19 12:14:11 +030064#include "linux-explicit-synchronization.h"
Alexandros Frantzisacff29b2018-10-19 12:14:11 +030065#include "shared/fd-util.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070066#include "shared/helpers.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070067#include "shared/os-compatibility.h"
Bryce Harrington25a2bdd2016-08-03 17:40:52 -070068#include "shared/string-helpers.h"
Pekka Paalanenaa21f622015-07-03 15:44:50 +030069#include "shared/timespec-util.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040070#include "git-version.h"
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -050071#include "version.h"
Pekka Paalanen827b5d22016-06-29 11:54:26 +020072#include "plugin-registry.h"
Marius Vlad00a6e012018-11-20 17:52:31 +020073#include "pixel-formats.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050074
Pekka Paalanen0513a952014-05-21 16:17:27 +030075#define DEFAULT_REPAINT_WINDOW 7 /* milliseconds */
76
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020077static void
Pekka Paalanend72bad22017-03-29 17:01:41 +030078weston_output_update_matrix(struct weston_output *output);
79
80static void
Alexander Larsson0b135062013-05-28 16:23:36 +020081weston_output_transform_scale_init(struct weston_output *output,
82 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020083
Rob Bradford27b17932013-06-26 18:08:46 +010084static void
Jason Ekstranda7af7042013-10-12 22:38:11 -050085weston_compositor_build_view_list(struct weston_compositor *compositor);
Rob Bradford27b17932013-06-26 18:08:46 +010086
Pekka Paalanendcac3512017-12-08 14:13:34 +020087static char *
88weston_output_create_heads_string(struct weston_output *output);
89
Pekka Paalanen6528c032017-03-28 15:27:10 +030090/** Send wl_output events for mode and scale changes
91 *
92 * \param head Send on all resources bound to this head.
93 * \param mode_changed If true, send the current mode.
94 * \param scale_changed If true, send the current scale.
95 */
96static void
97weston_mode_switch_send_events(struct weston_head *head,
98 bool mode_changed, bool scale_changed)
99{
100 struct weston_output *output = head->output;
101 struct wl_resource *resource;
102 int version;
103
104 wl_resource_for_each(resource, &head->resource_list) {
105 if (mode_changed) {
106 wl_output_send_mode(resource,
107 output->current_mode->flags,
108 output->current_mode->width,
109 output->current_mode->height,
110 output->current_mode->refresh);
111 }
112
113 version = wl_resource_get_version(resource);
114 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION && scale_changed)
115 wl_output_send_scale(resource, output->current_scale);
116
117 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
118 wl_output_send_done(resource);
119 }
120}
121
122static void
123weston_mode_switch_finish(struct weston_output *output,
124 int mode_changed, int scale_changed)
Alex Wu2dda6042012-04-17 17:20:47 +0800125{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200126 struct weston_seat *seat;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300127 struct weston_head *head;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200128 pixman_region32_t old_output_region;
Alexander Larsson355748e2013-05-28 16:23:38 +0200129
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200130 pixman_region32_init(&old_output_region);
131 pixman_region32_copy(&old_output_region, &output->region);
132
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200133 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +0200134 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200135
136 pixman_region32_init(&output->previous_damage);
137 pixman_region32_init_rect(&output->region, output->x, output->y,
138 output->width, output->height);
139
140 weston_output_update_matrix(output);
141
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200142 /* If a pointer falls outside the outputs new geometry, move it to its
143 * lower-right corner */
144 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500145 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200146 int32_t x, y;
147
148 if (!pointer)
149 continue;
150
151 x = wl_fixed_to_int(pointer->x);
152 y = wl_fixed_to_int(pointer->y);
153
154 if (!pixman_region32_contains_point(&old_output_region,
155 x, y, NULL) ||
156 pixman_region32_contains_point(&output->region,
157 x, y, NULL))
158 continue;
159
160 if (x >= output->x + output->width)
161 x = output->x + output->width - 1;
162 if (y >= output->y + output->height)
163 y = output->y + output->height - 1;
164
165 pointer->x = wl_fixed_from_int(x);
166 pointer->y = wl_fixed_from_int(y);
167 }
168
169 pixman_region32_fini(&old_output_region);
170
Derek Foremandd4cd332014-11-10 10:29:59 -0600171 if (!mode_changed && !scale_changed)
172 return;
173
Hardening57388e42013-09-18 23:56:36 +0200174 /* notify clients of the changes */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300175 wl_list_for_each(head, &output->head_list, output_link)
176 weston_mode_switch_send_events(head,
177 mode_changed, scale_changed);
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600178}
179
David Fort0de859e2016-05-27 23:22:57 +0200180static void
181weston_compositor_reflow_outputs(struct weston_compositor *compositor,
182 struct weston_output *resized_output, int delta_width);
183
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600184WL_EXPORT int
185weston_output_mode_set_native(struct weston_output *output,
186 struct weston_mode *mode,
187 int32_t scale)
188{
189 int ret;
190 int mode_changed = 0, scale_changed = 0;
David Fort0de859e2016-05-27 23:22:57 +0200191 int32_t old_width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600192
193 if (!output->switch_mode)
194 return -1;
195
196 if (!output->original_mode) {
197 mode_changed = 1;
198 ret = output->switch_mode(output, mode);
199 if (ret < 0)
200 return ret;
201 if (output->current_scale != scale) {
202 scale_changed = 1;
203 output->current_scale = scale;
Hardening57388e42013-09-18 23:56:36 +0200204 }
205 }
206
David Fort0de859e2016-05-27 23:22:57 +0200207 old_width = output->width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600208 output->native_mode = mode;
209 output->native_scale = scale;
210
211 weston_mode_switch_finish(output, mode_changed, scale_changed);
212
David Fort0de859e2016-05-27 23:22:57 +0200213 if (mode_changed || scale_changed) {
214 weston_compositor_reflow_outputs(output->compositor, output, output->width - old_width);
215
216 wl_signal_emit(&output->compositor->output_resized_signal, output);
217 }
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600218 return 0;
219}
220
221WL_EXPORT int
222weston_output_mode_switch_to_native(struct weston_output *output)
223{
224 int ret;
225 int mode_changed = 0, scale_changed = 0;
226
227 if (!output->switch_mode)
228 return -1;
229
230 if (!output->original_mode) {
231 weston_log("already in the native mode\n");
232 return -1;
233 }
234 /* the non fullscreen clients haven't seen a mode set since we
235 * switched into a temporary, so we need to notify them if the
236 * mode at that time is different from the native mode now.
237 */
238 mode_changed = (output->original_mode != output->native_mode);
239 scale_changed = (output->original_scale != output->native_scale);
240
241 ret = output->switch_mode(output, output->native_mode);
242 if (ret < 0)
243 return ret;
244
245 output->current_scale = output->native_scale;
246
247 output->original_mode = NULL;
248 output->original_scale = 0;
249
250 weston_mode_switch_finish(output, mode_changed, scale_changed);
251
252 return 0;
253}
254
255WL_EXPORT int
256weston_output_mode_switch_to_temporary(struct weston_output *output,
257 struct weston_mode *mode,
258 int32_t scale)
259{
260 int ret;
261
262 if (!output->switch_mode)
263 return -1;
264
265 /* original_mode is the last mode non full screen clients have seen,
266 * so we shouldn't change it if we already have one set.
267 */
268 if (!output->original_mode) {
269 output->original_mode = output->native_mode;
270 output->original_scale = output->native_scale;
271 }
272 ret = output->switch_mode(output, mode);
273 if (ret < 0)
274 return ret;
275
276 output->current_scale = scale;
277
278 weston_mode_switch_finish(output, 0, 0);
279
280 return 0;
Alex Wu2dda6042012-04-17 17:20:47 +0800281}
282
Benjamin Franzke06286262011-05-06 19:12:33 +0200283static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300284region_init_infinite(pixman_region32_t *region)
285{
286 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
287 UINT32_MAX, UINT32_MAX);
288}
289
Pekka Paalanene67858b2013-04-25 13:57:42 +0300290static struct weston_subsurface *
291weston_surface_to_subsurface(struct weston_surface *surface);
292
Jason Ekstranda7af7042013-10-12 22:38:11 -0500293WL_EXPORT struct weston_view *
294weston_view_create(struct weston_surface *surface)
295{
296 struct weston_view *view;
297
Bryce Harringtonde16d892014-11-20 22:21:57 -0800298 view = zalloc(sizeof *view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500299 if (view == NULL)
300 return NULL;
301
302 view->surface = surface;
Daniel Stonefb4869d2016-12-09 16:27:54 +0000303 view->plane = &surface->compositor->primary_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500304
Jason Ekstranda7af7042013-10-12 22:38:11 -0500305 /* Assign to surface */
306 wl_list_insert(&surface->views, &view->surface_link);
307
308 wl_signal_init(&view->destroy_signal);
309 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300310 wl_list_init(&view->layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500311
Jason Ekstranda7af7042013-10-12 22:38:11 -0500312 pixman_region32_init(&view->clip);
313
314 view->alpha = 1.0;
315 pixman_region32_init(&view->transform.opaque);
316
317 wl_list_init(&view->geometry.transformation_list);
318 wl_list_insert(&view->geometry.transformation_list,
319 &view->transform.position.link);
320 weston_matrix_init(&view->transform.position.matrix);
321 wl_list_init(&view->geometry.child_list);
Pekka Paalanen380adf52015-02-16 14:39:11 +0200322 pixman_region32_init(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500323 pixman_region32_init(&view->transform.boundingbox);
324 view->transform.dirty = 1;
325
Jason Ekstranda7af7042013-10-12 22:38:11 -0500326 return view;
327}
328
Jason Ekstrand108865d2014-06-26 10:04:49 -0700329struct weston_frame_callback {
330 struct wl_resource *resource;
331 struct wl_list link;
332};
333
Pekka Paalanen133e4392014-09-23 22:08:46 -0400334struct weston_presentation_feedback {
335 struct wl_resource *resource;
336
337 /* XXX: could use just wl_resource_get_link() instead */
338 struct wl_list link;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200339
340 /* The per-surface feedback flags */
341 uint32_t psf_flags;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400342};
343
344static void
345weston_presentation_feedback_discard(
346 struct weston_presentation_feedback *feedback)
347{
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200348 wp_presentation_feedback_send_discarded(feedback->resource);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400349 wl_resource_destroy(feedback->resource);
350}
351
352static void
353weston_presentation_feedback_discard_list(struct wl_list *list)
354{
355 struct weston_presentation_feedback *feedback, *tmp;
356
357 wl_list_for_each_safe(feedback, tmp, list, link)
358 weston_presentation_feedback_discard(feedback);
359}
360
361static void
362weston_presentation_feedback_present(
363 struct weston_presentation_feedback *feedback,
364 struct weston_output *output,
365 uint32_t refresh_nsec,
366 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200367 uint64_t seq,
368 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400369{
370 struct wl_client *client = wl_resource_get_client(feedback->resource);
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300371 struct weston_head *head;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400372 struct wl_resource *o;
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200373 uint32_t tv_sec_hi;
374 uint32_t tv_sec_lo;
375 uint32_t tv_nsec;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300376 bool done = false;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400377
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300378 wl_list_for_each(head, &output->head_list, output_link) {
379 wl_resource_for_each(o, &head->resource_list) {
380 if (wl_resource_get_client(o) != client)
381 continue;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400382
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300383 wp_presentation_feedback_send_sync_output(feedback->resource, o);
384 done = true;
385 }
386
387 /* For clone mode, send it for just one wl_output global,
388 * they are all equivalent anyway.
389 */
390 if (done)
391 break;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400392 }
393
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200394 timespec_to_proto(ts, &tv_sec_hi, &tv_sec_lo, &tv_nsec);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200395 wp_presentation_feedback_send_presented(feedback->resource,
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200396 tv_sec_hi, tv_sec_lo, tv_nsec,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200397 refresh_nsec,
398 seq >> 32, seq & 0xffffffff,
399 flags | feedback->psf_flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400400 wl_resource_destroy(feedback->resource);
401}
402
403static void
404weston_presentation_feedback_present_list(struct wl_list *list,
405 struct weston_output *output,
406 uint32_t refresh_nsec,
407 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200408 uint64_t seq,
409 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400410{
411 struct weston_presentation_feedback *feedback, *tmp;
412
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200413 assert(!(flags & WP_PRESENTATION_FEEDBACK_INVALID) ||
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200414 wl_list_empty(list));
415
Pekka Paalanen133e4392014-09-23 22:08:46 -0400416 wl_list_for_each_safe(feedback, tmp, list, link)
417 weston_presentation_feedback_present(feedback, output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200418 refresh_nsec, ts, seq,
419 flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400420}
421
Jason Ekstrand7b982072014-05-20 14:33:03 -0500422static void
423surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
424{
425 struct weston_surface_state *state =
426 container_of(listener, struct weston_surface_state,
427 buffer_destroy_listener);
428
429 state->buffer = NULL;
430}
431
432static void
433weston_surface_state_init(struct weston_surface_state *state)
434{
435 state->newly_attached = 0;
436 state->buffer = NULL;
437 state->buffer_destroy_listener.notify =
438 surface_state_handle_buffer_destroy;
439 state->sx = 0;
440 state->sy = 0;
441
Derek Foreman152254b2015-11-26 14:17:48 -0600442 pixman_region32_init(&state->damage_surface);
443 pixman_region32_init(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500444 pixman_region32_init(&state->opaque);
445 region_init_infinite(&state->input);
446
447 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400448 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500449
450 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
451 state->buffer_viewport.buffer.scale = 1;
452 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
453 state->buffer_viewport.surface.width = -1;
454 state->buffer_viewport.changed = 0;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300455
456 state->acquire_fence_fd = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500457}
458
459static void
460weston_surface_state_fini(struct weston_surface_state *state)
461{
462 struct weston_frame_callback *cb, *next;
463
464 wl_list_for_each_safe(cb, next,
465 &state->frame_callback_list, link)
466 wl_resource_destroy(cb->resource);
467
Pekka Paalanen133e4392014-09-23 22:08:46 -0400468 weston_presentation_feedback_discard_list(&state->feedback_list);
469
Jason Ekstrand7b982072014-05-20 14:33:03 -0500470 pixman_region32_fini(&state->input);
471 pixman_region32_fini(&state->opaque);
Derek Foreman152254b2015-11-26 14:17:48 -0600472 pixman_region32_fini(&state->damage_surface);
473 pixman_region32_fini(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500474
475 if (state->buffer)
476 wl_list_remove(&state->buffer_destroy_listener.link);
477 state->buffer = NULL;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300478
479 fd_clear(&state->acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +0300480 weston_buffer_release_reference(&state->buffer_release_ref, NULL);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500481}
482
483static void
484weston_surface_state_set_buffer(struct weston_surface_state *state,
485 struct weston_buffer *buffer)
486{
487 if (state->buffer == buffer)
488 return;
489
490 if (state->buffer)
491 wl_list_remove(&state->buffer_destroy_listener.link);
492 state->buffer = buffer;
493 if (state->buffer)
494 wl_signal_add(&state->buffer->destroy_signal,
495 &state->buffer_destroy_listener);
496}
497
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500498WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500499weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500500{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500501 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400502
Bryce Harringtonde16d892014-11-20 22:21:57 -0800503 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400504 if (surface == NULL)
505 return NULL;
506
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500507 wl_signal_init(&surface->destroy_signal);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +0800508 wl_signal_init(&surface->commit_signal);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500509
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500510 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200511 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400512
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200513 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
514 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200515 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
516 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500517
518 weston_surface_state_init(&surface->pending);
519
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400520 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500521 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300522 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400523
Jason Ekstranda7af7042013-10-12 22:38:11 -0500524 wl_list_init(&surface->views);
525
526 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400527 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500528
Pekka Paalanene67858b2013-04-25 13:57:42 +0300529 wl_list_init(&surface->subsurface_list);
530 wl_list_init(&surface->subsurface_list_pending);
531
Jason Ekstrand1e059042014-10-16 10:55:19 -0500532 weston_matrix_init(&surface->buffer_to_surface_matrix);
533 weston_matrix_init(&surface->surface_to_buffer_matrix);
534
Jonas Ådahld3414f22016-07-22 17:56:31 +0800535 wl_list_init(&surface->pointer_constraints);
536
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300537 surface->acquire_fence_fd = -1;
538
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400539 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500540}
541
Alex Wu8811bf92012-02-28 18:07:54 +0800542WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500543weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200544 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500545{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100546 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Philipp Zabel195dade2018-09-03 19:44:59 +0200547 surface->is_opaque = !(alpha < 1.0);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500548}
549
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400550WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500551weston_view_to_global_float(struct weston_view *view,
552 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200553{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500554 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200555 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
556
Jason Ekstranda7af7042013-10-12 22:38:11 -0500557 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200558
559 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200560 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700561 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200562 v.f[3]);
563 *x = 0;
564 *y = 0;
565 return;
566 }
567
568 *x = v.f[0] / v.f[3];
569 *y = v.f[1] / v.f[3];
570 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500571 *x = sx + view->geometry.x;
572 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200573 }
574}
575
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500576WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200577weston_transformed_coord(int width, int height,
578 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200579 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200580 float sx, float sy, float *bx, float *by)
581{
582 switch (transform) {
583 case WL_OUTPUT_TRANSFORM_NORMAL:
584 default:
585 *bx = sx;
586 *by = sy;
587 break;
588 case WL_OUTPUT_TRANSFORM_FLIPPED:
589 *bx = width - sx;
590 *by = sy;
591 break;
592 case WL_OUTPUT_TRANSFORM_90:
593 *bx = height - sy;
594 *by = sx;
595 break;
596 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
597 *bx = height - sy;
598 *by = width - sx;
599 break;
600 case WL_OUTPUT_TRANSFORM_180:
601 *bx = width - sx;
602 *by = height - sy;
603 break;
604 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
605 *bx = sx;
606 *by = height - sy;
607 break;
608 case WL_OUTPUT_TRANSFORM_270:
609 *bx = sy;
610 *by = width - sx;
611 break;
612 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
613 *bx = sy;
614 *by = sx;
615 break;
616 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200617
618 *bx *= scale;
619 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200620}
621
622WL_EXPORT pixman_box32_t
623weston_transformed_rect(int width, int height,
624 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200625 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200626 pixman_box32_t rect)
627{
628 float x1, x2, y1, y2;
629
630 pixman_box32_t ret;
631
Alexander Larsson4ea95522013-05-22 14:41:37 +0200632 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200633 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200634 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200635 rect.x2, rect.y2, &x2, &y2);
636
637 if (x1 <= x2) {
638 ret.x1 = x1;
639 ret.x2 = x2;
640 } else {
641 ret.x1 = x2;
642 ret.x2 = x1;
643 }
644
645 if (y1 <= y2) {
646 ret.y1 = y1;
647 ret.y2 = y2;
648 } else {
649 ret.y1 = y2;
650 ret.y2 = y1;
651 }
652
653 return ret;
654}
655
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600656/** Transform a region by a matrix, restricted to axis-aligned transformations
657 *
658 * Warning: This function does not work for projective, affine, or matrices
659 * that encode arbitrary rotations. Only 90-degree step rotations are
660 * supported.
661 */
662WL_EXPORT void
663weston_matrix_transform_region(pixman_region32_t *dest,
664 struct weston_matrix *matrix,
665 pixman_region32_t *src)
666{
667 pixman_box32_t *src_rects, *dest_rects;
668 int nrects, i;
669
670 src_rects = pixman_region32_rectangles(src, &nrects);
671 dest_rects = malloc(nrects * sizeof(*dest_rects));
672 if (!dest_rects)
673 return;
674
675 for (i = 0; i < nrects; i++) {
676 struct weston_vector vec1 = {{
677 src_rects[i].x1, src_rects[i].y1, 0, 1
678 }};
679 weston_matrix_transform(matrix, &vec1);
680 vec1.f[0] /= vec1.f[3];
681 vec1.f[1] /= vec1.f[3];
682
683 struct weston_vector vec2 = {{
684 src_rects[i].x2, src_rects[i].y2, 0, 1
685 }};
686 weston_matrix_transform(matrix, &vec2);
687 vec2.f[0] /= vec2.f[3];
688 vec2.f[1] /= vec2.f[3];
689
690 if (vec1.f[0] < vec2.f[0]) {
691 dest_rects[i].x1 = floor(vec1.f[0]);
692 dest_rects[i].x2 = ceil(vec2.f[0]);
693 } else {
694 dest_rects[i].x1 = floor(vec2.f[0]);
695 dest_rects[i].x2 = ceil(vec1.f[0]);
696 }
697
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600698 if (vec1.f[1] < vec2.f[1]) {
699 dest_rects[i].y1 = floor(vec1.f[1]);
700 dest_rects[i].y2 = ceil(vec2.f[1]);
701 } else {
702 dest_rects[i].y1 = floor(vec2.f[1]);
703 dest_rects[i].y2 = ceil(vec1.f[1]);
704 }
705 }
706
707 pixman_region32_clear(dest);
708 pixman_region32_init_rects(dest, dest_rects, nrects);
709 free(dest_rects);
710}
711
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200712WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500713weston_transformed_region(int width, int height,
714 enum wl_output_transform transform,
715 int32_t scale,
716 pixman_region32_t *src, pixman_region32_t *dest)
717{
718 pixman_box32_t *src_rects, *dest_rects;
719 int nrects, i;
720
721 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
722 if (src != dest)
723 pixman_region32_copy(dest, src);
724 return;
725 }
726
727 src_rects = pixman_region32_rectangles(src, &nrects);
728 dest_rects = malloc(nrects * sizeof(*dest_rects));
729 if (!dest_rects)
730 return;
731
732 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
733 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
734 } else {
735 for (i = 0; i < nrects; i++) {
736 switch (transform) {
737 default:
738 case WL_OUTPUT_TRANSFORM_NORMAL:
739 dest_rects[i].x1 = src_rects[i].x1;
740 dest_rects[i].y1 = src_rects[i].y1;
741 dest_rects[i].x2 = src_rects[i].x2;
742 dest_rects[i].y2 = src_rects[i].y2;
743 break;
744 case WL_OUTPUT_TRANSFORM_90:
745 dest_rects[i].x1 = height - src_rects[i].y2;
746 dest_rects[i].y1 = src_rects[i].x1;
747 dest_rects[i].x2 = height - src_rects[i].y1;
748 dest_rects[i].y2 = src_rects[i].x2;
749 break;
750 case WL_OUTPUT_TRANSFORM_180:
751 dest_rects[i].x1 = width - src_rects[i].x2;
752 dest_rects[i].y1 = height - src_rects[i].y2;
753 dest_rects[i].x2 = width - src_rects[i].x1;
754 dest_rects[i].y2 = height - src_rects[i].y1;
755 break;
756 case WL_OUTPUT_TRANSFORM_270:
757 dest_rects[i].x1 = src_rects[i].y1;
758 dest_rects[i].y1 = width - src_rects[i].x2;
759 dest_rects[i].x2 = src_rects[i].y2;
760 dest_rects[i].y2 = width - src_rects[i].x1;
761 break;
762 case WL_OUTPUT_TRANSFORM_FLIPPED:
763 dest_rects[i].x1 = width - src_rects[i].x2;
764 dest_rects[i].y1 = src_rects[i].y1;
765 dest_rects[i].x2 = width - src_rects[i].x1;
766 dest_rects[i].y2 = src_rects[i].y2;
767 break;
768 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
769 dest_rects[i].x1 = height - src_rects[i].y2;
770 dest_rects[i].y1 = width - src_rects[i].x2;
771 dest_rects[i].x2 = height - src_rects[i].y1;
772 dest_rects[i].y2 = width - src_rects[i].x1;
773 break;
774 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
775 dest_rects[i].x1 = src_rects[i].x1;
776 dest_rects[i].y1 = height - src_rects[i].y2;
777 dest_rects[i].x2 = src_rects[i].x2;
778 dest_rects[i].y2 = height - src_rects[i].y1;
779 break;
780 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
781 dest_rects[i].x1 = src_rects[i].y1;
782 dest_rects[i].y1 = src_rects[i].x1;
783 dest_rects[i].x2 = src_rects[i].y2;
784 dest_rects[i].y2 = src_rects[i].x2;
785 break;
786 }
787 }
788 }
789
790 if (scale != 1) {
791 for (i = 0; i < nrects; i++) {
792 dest_rects[i].x1 *= scale;
793 dest_rects[i].x2 *= scale;
794 dest_rects[i].y1 *= scale;
795 dest_rects[i].y2 *= scale;
796 }
797 }
798
799 pixman_region32_clear(dest);
800 pixman_region32_init_rects(dest, dest_rects, nrects);
801 free(dest_rects);
802}
803
Jonny Lamb74130762013-11-26 18:19:46 +0100804static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300805viewport_surface_to_buffer(struct weston_surface *surface,
806 float sx, float sy, float *bx, float *by)
Jonny Lamb74130762013-11-26 18:19:46 +0100807{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200808 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200809 double src_width, src_height;
810 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200811
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200812 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
813 if (vp->surface.width == -1) {
814 *bx = sx;
815 *by = sy;
816 return;
817 }
Jonny Lamb74130762013-11-26 18:19:46 +0100818
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200819 src_x = 0.0;
820 src_y = 0.0;
821 src_width = surface->width_from_buffer;
822 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100823 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200824 src_x = wl_fixed_to_double(vp->buffer.src_x);
825 src_y = wl_fixed_to_double(vp->buffer.src_y);
826 src_width = wl_fixed_to_double(vp->buffer.src_width);
827 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100828 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200829
830 *bx = sx * src_width / surface->width + src_x;
831 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100832}
833
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500834WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200835weston_surface_to_buffer_float(struct weston_surface *surface,
836 float sx, float sy, float *bx, float *by)
837{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200838 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
839
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300840 /* first transform coordinates if the viewport is set */
841 viewport_surface_to_buffer(surface, sx, sy, bx, by);
Jonny Lamb74130762013-11-26 18:19:46 +0100842
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500843 weston_transformed_coord(surface->width_from_buffer,
844 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200845 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100846 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200847}
848
Derek Foreman99739672015-12-03 16:38:11 -0600849/** Transform a rectangle from surface coordinates to buffer coordinates
850 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300851 * \param surface The surface to fetch wp_viewport and buffer transformation
Derek Foreman99739672015-12-03 16:38:11 -0600852 * from.
853 * \param rect The rectangle to transform.
854 * \return The transformed rectangle.
855 *
856 * Viewport and buffer transformations can only do translation, scaling,
857 * and rotations in 90-degree steps. Therefore the only loss in the
858 * conversion is coordinate rounding.
859 *
860 * However, some coordinate rounding takes place as an intermediate
861 * step before the buffer scale factor is applied, so the rectangle
862 * boundary may not be exactly as expected.
863 *
864 * This is OK for damage tracking since a little extra coverage is
865 * not a problem.
866 */
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200867WL_EXPORT pixman_box32_t
868weston_surface_to_buffer_rect(struct weston_surface *surface,
869 pixman_box32_t rect)
870{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200871 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100872 float xf, yf;
873
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300874 /* first transform box coordinates if the viewport is set */
875 viewport_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100876 rect.x1 = floorf(xf);
877 rect.y1 = floorf(yf);
878
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300879 viewport_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
Derek Foremane2e15ac2015-12-01 13:00:43 -0600880 rect.x2 = ceilf(xf);
881 rect.y2 = ceilf(yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100882
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500883 return weston_transformed_rect(surface->width_from_buffer,
884 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200885 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200886 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200887}
888
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200889/** Transform a region from surface coordinates to buffer coordinates
890 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300891 * \param surface The surface to fetch wp_viewport and buffer transformation
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200892 * from.
893 * \param surface_region[in] The region in surface coordinates.
894 * \param buffer_region[out] The region converted to buffer coordinates.
895 *
896 * Buffer_region must be init'd, but will be completely overwritten.
897 *
898 * Viewport and buffer transformations can only do translation, scaling,
899 * and rotations in 90-degree steps. Therefore the only loss in the
Derek Foreman99739672015-12-03 16:38:11 -0600900 * conversion is from the coordinate rounding that takes place in
901 * \ref weston_surface_to_buffer_rect.
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200902 */
903WL_EXPORT void
904weston_surface_to_buffer_region(struct weston_surface *surface,
905 pixman_region32_t *surface_region,
906 pixman_region32_t *buffer_region)
907{
908 pixman_box32_t *src_rects, *dest_rects;
909 int nrects, i;
910
911 src_rects = pixman_region32_rectangles(surface_region, &nrects);
912 dest_rects = malloc(nrects * sizeof(*dest_rects));
913 if (!dest_rects)
914 return;
915
916 for (i = 0; i < nrects; i++) {
917 dest_rects[i] = weston_surface_to_buffer_rect(surface,
918 src_rects[i]);
919 }
920
921 pixman_region32_fini(buffer_region);
922 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
923 free(dest_rects);
924}
925
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200926WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500927weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400928 struct weston_plane *plane)
929{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500930 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400931 return;
932
Jason Ekstranda7af7042013-10-12 22:38:11 -0500933 weston_view_damage_below(view);
934 view->plane = plane;
935 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400936}
937
Pekka Paalanen51723d52015-02-17 13:10:01 +0200938/** Inflict damage on the plane where the view is visible.
939 *
940 * \param view The view that causes the damage.
941 *
942 * If the view is currently on a plane (including the primary plane),
943 * take the view's boundingbox, subtract all the opaque views that cover it,
944 * and add the remaining region as damage to the plane. This corresponds
945 * to the damage inflicted to the plane if this view disappeared.
946 *
947 * A repaint is scheduled for this view.
948 *
949 * The region of all opaque views covering this view is stored in
950 * weston_view::clip and updated by view_accumulate_damage() during
951 * weston_output_repaint(). Specifically, that region matches the
952 * scenegraph as it was last painted.
953 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400954WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500955weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200956{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500957 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200958
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500959 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +0200960 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500961 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800962 if (view->plane)
963 pixman_region32_union(&view->plane->damage,
964 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500965 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800966 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200967}
968
Pekka Paalanen01e00682017-03-24 16:21:06 +0200969/** Send wl_surface.enter/leave events
970 *
971 * \param surface The surface.
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300972 * \param head A head of the entered/left output.
Pekka Paalanen01e00682017-03-24 16:21:06 +0200973 * \param enter True if entered.
974 * \param left True if left.
975 *
976 * Send the enter/leave events for all protocol objects bound to the given
977 * output by the client owning the surface.
978 */
979static void
980weston_surface_send_enter_leave(struct weston_surface *surface,
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300981 struct weston_head *head,
Pekka Paalanen01e00682017-03-24 16:21:06 +0200982 bool enter,
983 bool leave)
984{
985 struct wl_resource *wloutput;
986 struct wl_client *client;
987
988 assert(enter != leave);
989
990 client = wl_resource_get_client(surface->resource);
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300991 wl_resource_for_each(wloutput, &head->resource_list) {
Pekka Paalanen01e00682017-03-24 16:21:06 +0200992 if (wl_resource_get_client(wloutput) != client)
993 continue;
994
995 if (enter)
996 wl_surface_send_enter(surface->resource, wloutput);
997 if (leave)
998 wl_surface_send_leave(surface->resource, wloutput);
999 }
1000}
1001
Bryce Harrington3f650b82015-12-23 11:01:58 -08001002/**
1003 * \param es The surface
1004 * \param mask The new set of outputs for the surface
1005 *
1006 * Sets the surface's set of outputs to the ones specified by
1007 * the new output mask provided. Identifies the outputs that
1008 * have changed, the posts enter and leave events for these
1009 * outputs as appropriate.
1010 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001011static void
1012weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
1013{
1014 uint32_t different = es->output_mask ^ mask;
1015 uint32_t entered = mask & different;
1016 uint32_t left = es->output_mask & different;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001017 uint32_t output_bit;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001018 struct weston_output *output;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001019 struct weston_head *head;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001020
1021 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001022 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001023 return;
1024 if (different == 0)
1025 return;
1026
1027 wl_list_for_each(output, &es->compositor->output_list, link) {
Pekka Paalanen01e00682017-03-24 16:21:06 +02001028 output_bit = 1u << output->id;
1029 if (!(output_bit & different))
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001030 continue;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001031
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001032 wl_list_for_each(head, &output->head_list, output_link) {
1033 weston_surface_send_enter_leave(es, head,
1034 output_bit & entered,
1035 output_bit & left);
1036 }
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001037 }
1038}
1039
Semi Malinene7a52fb2018-04-26 11:08:10 +02001040static void
1041notify_view_output_destroy(struct wl_listener *listener, void *data)
1042{
1043 struct weston_view *view =
1044 container_of(listener,
1045 struct weston_view, output_destroy_listener);
1046
1047 view->output = NULL;
1048 view->output_destroy_listener.notify = NULL;
1049}
1050
1051/** Set the primary output of the view
1052 *
1053 * \param view The view whose primary output to set
1054 * \param output The new primary output for the view
1055 *
1056 * Set \a output to be the primary output of the \a view.
1057 *
1058 * Notice that the assignment may be temporary; the primary output could be
1059 * automatically changed. Hence, one cannot rely on the value persisting.
1060 *
1061 * Passing NULL as /a output will set the primary output to NULL.
1062 */
1063WL_EXPORT void
1064weston_view_set_output(struct weston_view *view, struct weston_output *output)
1065{
1066 if (view->output_destroy_listener.notify) {
1067 wl_list_remove(&view->output_destroy_listener.link);
1068 view->output_destroy_listener.notify = NULL;
1069 }
1070 view->output = output;
1071 if (output) {
1072 view->output_destroy_listener.notify =
1073 notify_view_output_destroy;
1074 wl_signal_add(&output->destroy_signal,
1075 &view->output_destroy_listener);
1076 }
1077}
1078
Bryce Harrington3f650b82015-12-23 11:01:58 -08001079/** Recalculate which output(s) the surface has views displayed on
1080 *
1081 * \param es The surface to remap to outputs
1082 *
1083 * Finds the output that is showing the largest amount of one
1084 * of the surface's various views. This output becomes the
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001085 * surface's primary output for vsync and frame callback purposes.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001086 *
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001087 * Also notes all outputs of all of the surface's views
Bryce Harrington3f650b82015-12-23 11:01:58 -08001088 * in the output_mask for the surface.
1089 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001090static void
1091weston_surface_assign_output(struct weston_surface *es)
1092{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001093 struct weston_output *new_output;
1094 struct weston_view *view;
1095 pixman_region32_t region;
1096 uint32_t max, area, mask;
1097 pixman_box32_t *e;
1098
1099 new_output = NULL;
1100 max = 0;
1101 mask = 0;
1102 pixman_region32_init(&region);
1103 wl_list_for_each(view, &es->views, surface_link) {
1104 if (!view->output)
1105 continue;
1106
1107 pixman_region32_intersect(&region, &view->transform.boundingbox,
1108 &view->output->region);
1109
1110 e = pixman_region32_extents(&region);
1111 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1112
1113 mask |= view->output_mask;
1114
1115 if (area >= max) {
1116 new_output = view->output;
1117 max = area;
1118 }
1119 }
1120 pixman_region32_fini(&region);
1121
1122 es->output = new_output;
1123 weston_surface_update_output_mask(es, mask);
1124}
1125
Bryce Harrington3f650b82015-12-23 11:01:58 -08001126/** Recalculate which output(s) the view is displayed on
1127 *
1128 * \param ev The view to remap to outputs
1129 *
1130 * Identifies the set of outputs that the view is visible on,
1131 * noting them into the output_mask. The output that the view
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001132 * is most visible on is set as the view's primary output.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001133 *
1134 * Also does the same for the view's surface. See
1135 * weston_surface_assign_output().
1136 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001137static void
1138weston_view_assign_output(struct weston_view *ev)
1139{
1140 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001141 struct weston_output *output, *new_output;
1142 pixman_region32_t region;
1143 uint32_t max, area, mask;
1144 pixman_box32_t *e;
1145
1146 new_output = NULL;
1147 max = 0;
1148 mask = 0;
1149 pixman_region32_init(&region);
1150 wl_list_for_each(output, &ec->output_list, link) {
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03001151 if (output->destroying)
1152 continue;
1153
Jason Ekstranda7af7042013-10-12 22:38:11 -05001154 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001155 &output->region);
1156
1157 e = pixman_region32_extents(&region);
1158 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1159
1160 if (area > 0)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001161 mask |= 1u << output->id;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001162
1163 if (area >= max) {
1164 new_output = output;
1165 max = area;
1166 }
1167 }
1168 pixman_region32_fini(&region);
1169
Semi Malinene7a52fb2018-04-26 11:08:10 +02001170 weston_view_set_output(ev, new_output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001171 ev->output_mask = mask;
1172
1173 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001174}
1175
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001176static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001177weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1178 int from_x, int from_y, int *to_x, int *to_y)
1179{
1180 float x, y;
1181
1182 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1183 weston_view_from_global_float(to, x, y, &x, &y);
1184
1185 *to_x = round(x);
1186 *to_y = round(y);
1187}
1188
1189static void
1190weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1191{
1192 pixman_box32_t *a;
1193 pixman_box32_t b;
1194
1195 a = pixman_region32_extents(&from->geometry.scissor);
1196
1197 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1198 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1199
1200 pixman_region32_fini(&to->geometry.scissor);
1201 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1202}
1203
1204static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001205view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001206 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001207{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001208 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1209 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001210 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001211 { inbox->x1, inbox->y1 },
1212 { inbox->x1, inbox->y2 },
1213 { inbox->x2, inbox->y1 },
1214 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001215 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001216 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001217 int i;
1218
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001219 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001220 /* avoid rounding empty bbox to 1x1 */
1221 pixman_region32_init(bbox);
1222 return;
1223 }
1224
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001225 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001226 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001227 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001228 if (x < min_x)
1229 min_x = x;
1230 if (x > max_x)
1231 max_x = x;
1232 if (y < min_y)
1233 min_y = y;
1234 if (y > max_y)
1235 max_y = y;
1236 }
1237
Pekka Paalanen219b9822012-02-08 15:38:37 +02001238 int_x = floorf(min_x);
1239 int_y = floorf(min_y);
1240 pixman_region32_init_rect(bbox, int_x, int_y,
1241 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001242}
1243
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001244static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001245weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001246{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001247 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001248
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001249 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001250 view->geometry.x = roundf(view->geometry.x);
1251 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001252
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001253 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001254 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1255 view->transform.position.matrix.d[12] = view->geometry.x;
1256 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001257
Jason Ekstranda7af7042013-10-12 22:38:11 -05001258 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001259
Jason Ekstranda7af7042013-10-12 22:38:11 -05001260 view->transform.inverse = view->transform.position.matrix;
1261 view->transform.inverse.d[12] = -view->geometry.x;
1262 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001263
Jason Ekstranda7af7042013-10-12 22:38:11 -05001264 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001265 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001266 view->surface->width,
1267 view->surface->height);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001268 if (view->geometry.scissor_enabled)
1269 pixman_region32_intersect(&view->transform.boundingbox,
1270 &view->transform.boundingbox,
1271 &view->geometry.scissor);
1272
1273 pixman_region32_translate(&view->transform.boundingbox,
1274 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001275
Jason Ekstranda7af7042013-10-12 22:38:11 -05001276 if (view->alpha == 1.0) {
1277 pixman_region32_copy(&view->transform.opaque,
1278 &view->surface->opaque);
1279 pixman_region32_translate(&view->transform.opaque,
1280 view->geometry.x,
1281 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001282 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001283}
1284
1285static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001286weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001287{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001288 struct weston_view *parent = view->geometry.parent;
1289 struct weston_matrix *matrix = &view->transform.matrix;
1290 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001291 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001292 pixman_region32_t surfregion;
1293 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001294
Jason Ekstranda7af7042013-10-12 22:38:11 -05001295 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001296
1297 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001298 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1299 view->transform.position.matrix.d[12] = view->geometry.x;
1300 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001301
1302 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001303 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001304 weston_matrix_multiply(matrix, &tform->matrix);
1305
Pekka Paalanen483243f2013-03-08 14:56:50 +02001306 if (parent)
1307 weston_matrix_multiply(matrix, &parent->transform.matrix);
1308
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001309 if (weston_matrix_invert(inverse, matrix) < 0) {
1310 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001311 weston_log("error: weston_view %p"
1312 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001313 return -1;
1314 }
1315
Giulio Camuffo148c1992016-09-04 18:50:46 +03001316 if (view->alpha == 1.0 &&
1317 matrix->type == WESTON_MATRIX_TRANSFORM_TRANSLATE) {
1318 pixman_region32_copy(&view->transform.opaque,
1319 &view->surface->opaque);
1320 pixman_region32_translate(&view->transform.opaque,
1321 matrix->d[12],
1322 matrix->d[13]);
1323 }
1324
Pekka Paalanen380adf52015-02-16 14:39:11 +02001325 pixman_region32_init_rect(&surfregion, 0, 0,
1326 view->surface->width, view->surface->height);
1327 if (view->geometry.scissor_enabled)
1328 pixman_region32_intersect(&surfregion, &surfregion,
1329 &view->geometry.scissor);
1330 surfbox = pixman_region32_extents(&surfregion);
1331
1332 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1333 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001334
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001335 return 0;
1336}
1337
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001338static struct weston_layer *
1339get_view_layer(struct weston_view *view)
1340{
1341 if (view->parent_view)
1342 return get_view_layer(view->parent_view);
1343 return view->layer_link.layer;
1344}
1345
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001346WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001347weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001348{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001349 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001350 struct weston_layer *layer;
1351 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001352
Jason Ekstranda7af7042013-10-12 22:38:11 -05001353 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001354 return;
1355
Pekka Paalanen483243f2013-03-08 14:56:50 +02001356 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001357 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001358
Jason Ekstranda7af7042013-10-12 22:38:11 -05001359 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001360
Jason Ekstranda7af7042013-10-12 22:38:11 -05001361 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001362
Jason Ekstranda7af7042013-10-12 22:38:11 -05001363 pixman_region32_fini(&view->transform.boundingbox);
1364 pixman_region32_fini(&view->transform.opaque);
1365 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001366
Pekka Paalanencd403622012-01-25 13:37:39 +02001367 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001368 if (view->geometry.transformation_list.next ==
1369 &view->transform.position.link &&
1370 view->geometry.transformation_list.prev ==
1371 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001372 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001373 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001374 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001375 if (weston_view_update_transform_enable(view) < 0)
1376 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001377 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001378
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001379 layer = get_view_layer(view);
1380 if (layer) {
1381 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001382 pixman_region32_intersect(&view->transform.boundingbox,
1383 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001384 pixman_region32_intersect(&view->transform.opaque,
1385 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001386 pixman_region32_fini(&mask);
1387 }
1388
Pekka Paalanen380adf52015-02-16 14:39:11 +02001389 if (parent) {
1390 if (parent->geometry.scissor_enabled) {
1391 view->geometry.scissor_enabled = true;
1392 weston_view_transfer_scissor(parent, view);
1393 } else {
1394 view->geometry.scissor_enabled = false;
1395 }
1396 }
1397
Jason Ekstranda7af7042013-10-12 22:38:11 -05001398 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001399
Jason Ekstranda7af7042013-10-12 22:38:11 -05001400 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001401
Jason Ekstranda7af7042013-10-12 22:38:11 -05001402 wl_signal_emit(&view->surface->compositor->transform_signal,
1403 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001404}
1405
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001406WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001407weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001408{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001409 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001410
1411 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001412 * The invariant: if view->geometry.dirty, then all views
1413 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001414 * Corollary: if not parent->geometry.dirty, then all ancestors
1415 * are not dirty.
1416 */
1417
Jason Ekstranda7af7042013-10-12 22:38:11 -05001418 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001419 return;
1420
Jason Ekstranda7af7042013-10-12 22:38:11 -05001421 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001422
Jason Ekstranda7af7042013-10-12 22:38:11 -05001423 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001424 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001425 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001426}
1427
1428WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001429weston_view_to_global_fixed(struct weston_view *view,
1430 wl_fixed_t vx, wl_fixed_t vy,
1431 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001432{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001433 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001434
Jason Ekstranda7af7042013-10-12 22:38:11 -05001435 weston_view_to_global_float(view,
1436 wl_fixed_to_double(vx),
1437 wl_fixed_to_double(vy),
1438 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001439 *x = wl_fixed_from_double(xf);
1440 *y = wl_fixed_from_double(yf);
1441}
1442
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001443WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001444weston_view_from_global_float(struct weston_view *view,
1445 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001446{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001447 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001448 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1449
Jason Ekstranda7af7042013-10-12 22:38:11 -05001450 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001451
1452 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001453 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001454 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001455 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001456 *vx = 0;
1457 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001458 return;
1459 }
1460
Jason Ekstranda7af7042013-10-12 22:38:11 -05001461 *vx = v.f[0] / v.f[3];
1462 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001463 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001464 *vx = x - view->geometry.x;
1465 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001466 }
1467}
1468
1469WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001470weston_view_from_global_fixed(struct weston_view *view,
1471 wl_fixed_t x, wl_fixed_t y,
1472 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001473{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001474 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001475
Jason Ekstranda7af7042013-10-12 22:38:11 -05001476 weston_view_from_global_float(view,
1477 wl_fixed_to_double(x),
1478 wl_fixed_to_double(y),
1479 &vxf, &vyf);
1480 *vx = wl_fixed_from_double(vxf);
1481 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001482}
1483
1484WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001485weston_view_from_global(struct weston_view *view,
1486 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001487{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001488 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001489
Jason Ekstranda7af7042013-10-12 22:38:11 -05001490 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1491 *vx = floorf(vxf);
1492 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001493}
1494
Bryce Harrington3f650b82015-12-23 11:01:58 -08001495/**
1496 * \param surface The surface to be repainted
1497 *
1498 * Marks the output(s) that the surface is shown on as needing to be
1499 * repainted. See weston_output_schedule_repaint().
1500 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001501WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001502weston_surface_schedule_repaint(struct weston_surface *surface)
1503{
1504 struct weston_output *output;
1505
1506 wl_list_for_each(output, &surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001507 if (surface->output_mask & (1u << output->id))
Kristian Høgsberg98238702012-08-03 16:29:12 -04001508 weston_output_schedule_repaint(output);
1509}
1510
Bryce Harrington3f650b82015-12-23 11:01:58 -08001511/**
1512 * \param view The view to be repainted
1513 *
1514 * Marks the output(s) that the view is shown on as needing to be
1515 * repainted. See weston_output_schedule_repaint().
1516 */
Kristian Høgsberg98238702012-08-03 16:29:12 -04001517WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001518weston_view_schedule_repaint(struct weston_view *view)
1519{
1520 struct weston_output *output;
1521
1522 wl_list_for_each(output, &view->surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001523 if (view->output_mask & (1u << output->id))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001524 weston_output_schedule_repaint(output);
1525}
1526
Pekka Paalanene508ce62015-02-19 13:59:55 +02001527/**
1528 * XXX: This function does it the wrong way.
1529 * surface->damage is the damage from the client, and causes
1530 * surface_flush_damage() to copy pixels. No window management action can
1531 * cause damage to the client-provided content, warranting re-upload!
1532 *
1533 * Instead of surface->damage, this function should record the damage
1534 * with all the views for this surface to avoid extraneous texture
1535 * uploads.
1536 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001537WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001538weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001539{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001540 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001541 0, 0, surface->width,
1542 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001543
Kristian Høgsberg98238702012-08-03 16:29:12 -04001544 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001545}
1546
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001547WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001548weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001549{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001550 if (view->geometry.x == x && view->geometry.y == y)
1551 return;
1552
Jason Ekstranda7af7042013-10-12 22:38:11 -05001553 view->geometry.x = x;
1554 view->geometry.y = y;
1555 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001556}
1557
Pekka Paalanen483243f2013-03-08 14:56:50 +02001558static void
1559transform_parent_handle_parent_destroy(struct wl_listener *listener,
1560 void *data)
1561{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001562 struct weston_view *view =
1563 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001564 geometry.parent_destroy_listener);
1565
Jason Ekstranda7af7042013-10-12 22:38:11 -05001566 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001567}
1568
1569WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001570weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001571 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001572{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001573 if (view->geometry.parent) {
1574 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1575 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001576
1577 if (!parent)
1578 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001579 }
1580
Jason Ekstranda7af7042013-10-12 22:38:11 -05001581 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001582
Jason Ekstranda7af7042013-10-12 22:38:11 -05001583 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001584 transform_parent_handle_parent_destroy;
1585 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001586 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001587 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001588 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001589 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001590 }
1591
Jason Ekstranda7af7042013-10-12 22:38:11 -05001592 weston_view_geometry_dirty(view);
1593}
1594
Pekka Paalanen380adf52015-02-16 14:39:11 +02001595/** Set a clip mask rectangle on a view
1596 *
1597 * \param view The view to set the clip mask on.
1598 * \param x Top-left corner X coordinate of the clip rectangle.
1599 * \param y Top-left corner Y coordinate of the clip rectangle.
1600 * \param width Width of the clip rectangle, non-negative.
1601 * \param height Height of the clip rectangle, non-negative.
1602 *
1603 * A shell may set a clip mask rectangle on a view. Everything outside
1604 * the rectangle is cut away for input and output purposes: it is
1605 * not drawn and cannot be hit by hit-test based input like pointer
1606 * motion or touch-downs. Everything inside the rectangle will behave
1607 * normally. Clients are unaware of clipping.
1608 *
Yong Bakos4c72e292016-04-28 11:59:10 -05001609 * The rectangle is set in surface-local coordinates. Setting a clip
Pekka Paalanen380adf52015-02-16 14:39:11 +02001610 * mask rectangle does not affect the view position, the view is positioned
1611 * as it would be without a clip. The clip also does not change
1612 * weston_surface::width,height.
1613 *
1614 * The clip mask rectangle is part of transformation inheritance
1615 * (weston_view_set_transform_parent()). A clip set in the root of the
1616 * transformation inheritance tree will affect all views in the tree.
1617 * A clip can be set only on the root view. Attempting to set a clip
1618 * on view that has a transformation parent will fail. Assigning a parent
1619 * to a view that has a clip set will cause the clip to be forgotten.
1620 *
1621 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1622 * on the additional transformations in the child views. These transformations
1623 * may not rotate the coordinate axes, i.e., only translation and scaling
1624 * are allowed. Violating this restriction causes the clipping to malfunction.
1625 * Furthermore, using scaling may cause rounding errors in child clipping.
1626 *
1627 * The clip mask rectangle is not automatically adjusted based on
1628 * wl_surface.attach dx and dy arguments.
1629 *
1630 * A clip mask rectangle can be set only if the compositor capability
1631 * WESTON_CAP_VIEW_CLIP_MASK is present.
1632 *
1633 * This function sets the clip mask rectangle and schedules a repaint for
1634 * the view.
1635 */
1636WL_EXPORT void
1637weston_view_set_mask(struct weston_view *view,
1638 int x, int y, int width, int height)
1639{
1640 struct weston_compositor *compositor = view->surface->compositor;
1641
1642 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1643 weston_log("%s not allowed without capability!\n", __func__);
1644 return;
1645 }
1646
1647 if (view->geometry.parent) {
1648 weston_log("view %p has a parent, clip forbidden!\n", view);
1649 return;
1650 }
1651
1652 if (width < 0 || height < 0) {
1653 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1654 x, y, width, height);
1655 return;
1656 }
1657
1658 pixman_region32_fini(&view->geometry.scissor);
1659 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1660 view->geometry.scissor_enabled = true;
1661 weston_view_geometry_dirty(view);
1662 weston_view_schedule_repaint(view);
1663}
1664
1665/** Remove the clip mask from a view
1666 *
1667 * \param view The view to remove the clip mask from.
1668 *
1669 * Removed the clip mask rectangle and schedules a repaint.
1670 *
1671 * \sa weston_view_set_mask
1672 */
1673WL_EXPORT void
1674weston_view_set_mask_infinite(struct weston_view *view)
1675{
1676 view->geometry.scissor_enabled = false;
1677 weston_view_geometry_dirty(view);
1678 weston_view_schedule_repaint(view);
1679}
1680
Armin Krezović0da12b82016-06-30 06:04:33 +02001681/* Check if view should be displayed
1682 *
1683 * The indicator is set manually when assigning
1684 * a view to a surface.
1685 *
1686 * This needs reworking. See the thread starting at:
1687 *
1688 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1689 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001690WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001691weston_view_is_mapped(struct weston_view *view)
1692{
Armin Krezović0da12b82016-06-30 06:04:33 +02001693 return view->is_mapped;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001694}
1695
Philipp Zabel70decd52018-09-03 20:11:15 +02001696/* Check if view is opaque in specified region
1697 *
1698 * \param view The view to check for opacity.
1699 * \param region The region to check for opacity, in view coordinates.
1700 *
1701 * Returns true if the view is opaque in the specified region, because view
1702 * alpha is 1.0 and either the opaque region set by the client contains the
1703 * specified region, or the buffer pixel format or solid color is opaque.
1704 */
1705WL_EXPORT bool
1706weston_view_is_opaque(struct weston_view *ev, pixman_region32_t *region)
1707{
1708 pixman_region32_t r;
1709 bool ret = false;
1710
1711 if (ev->alpha < 1.0)
1712 return false;
1713
1714 if (ev->surface->is_opaque)
1715 return true;
1716
1717 if (ev->transform.dirty) {
1718 weston_log("%s: transform dirty", __func__);
1719 return false;
1720 }
1721
1722 pixman_region32_init(&r);
1723 pixman_region32_subtract(&r, region, &ev->transform.opaque);
1724
1725 if (!pixman_region32_not_empty(&r))
1726 ret = true;
1727
1728 pixman_region32_fini(&r);
1729
1730 return ret;
1731}
1732
Armin Krezović0da12b82016-06-30 06:04:33 +02001733/* Check if a surface has a view assigned to it
1734 *
1735 * The indicator is set manually when mapping
1736 * a surface and creating a view for it.
1737 *
1738 * This needs to go. See the thread starting at:
1739 *
1740 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1741 *
1742 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001743WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001744weston_surface_is_mapped(struct weston_surface *surface)
1745{
Armin Krezović0da12b82016-06-30 06:04:33 +02001746 return surface->is_mapped;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001747}
1748
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001749static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001750surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001751{
1752 struct weston_view *view;
1753
1754 if (surface->width == width && surface->height == height)
1755 return;
1756
1757 surface->width = width;
1758 surface->height = height;
1759
1760 wl_list_for_each(view, &surface->views, surface_link)
1761 weston_view_geometry_dirty(view);
1762}
1763
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001764WL_EXPORT void
1765weston_surface_set_size(struct weston_surface *surface,
1766 int32_t width, int32_t height)
1767{
1768 assert(!surface->resource);
1769 surface_set_size(surface, width, height);
1770}
1771
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001772static int
1773fixed_round_up_to_int(wl_fixed_t f)
1774{
1775 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1776}
1777
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001778static void
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001779convert_size_by_transform_scale(int32_t *width_out, int32_t *height_out,
1780 int32_t width, int32_t height,
1781 uint32_t transform,
1782 int32_t scale)
1783{
1784 assert(scale > 0);
1785
1786 switch (transform) {
1787 case WL_OUTPUT_TRANSFORM_NORMAL:
1788 case WL_OUTPUT_TRANSFORM_180:
1789 case WL_OUTPUT_TRANSFORM_FLIPPED:
1790 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1791 *width_out = width / scale;
1792 *height_out = height / scale;
1793 break;
1794 case WL_OUTPUT_TRANSFORM_90:
1795 case WL_OUTPUT_TRANSFORM_270:
1796 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1797 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1798 *width_out = height / scale;
1799 *height_out = width / scale;
1800 break;
1801 default:
1802 assert(0 && "invalid transform");
1803 }
1804}
1805
1806static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001807weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001808{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001809 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001810
1811 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001812 surface->width_from_buffer = 0;
1813 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001814 return;
1815 }
1816
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001817 convert_size_by_transform_scale(&surface->width_from_buffer,
1818 &surface->height_from_buffer,
1819 surface->buffer_ref.buffer->width,
1820 surface->buffer_ref.buffer->height,
1821 vp->buffer.transform,
1822 vp->buffer.scale);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001823}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001824
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001825static void
1826weston_surface_update_size(struct weston_surface *surface)
1827{
1828 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1829 int32_t width, height;
1830
1831 width = surface->width_from_buffer;
1832 height = surface->height_from_buffer;
1833
1834 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001835 surface_set_size(surface,
1836 vp->surface.width, vp->surface.height);
1837 return;
1838 }
1839
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001840 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001841 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1842 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1843
1844 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001845 return;
1846 }
1847
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001848 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001849}
1850
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02001851WL_EXPORT void
1852weston_compositor_get_time(struct timespec *time)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001853{
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02001854 clock_gettime(CLOCK_REALTIME, time);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001855}
1856
Jason Ekstranda7af7042013-10-12 22:38:11 -05001857WL_EXPORT struct weston_view *
1858weston_compositor_pick_view(struct weston_compositor *compositor,
1859 wl_fixed_t x, wl_fixed_t y,
1860 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001861{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001862 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001863 wl_fixed_t view_x, view_y;
1864 int view_ix, view_iy;
1865 int ix = wl_fixed_to_int(x);
1866 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001867
Jason Ekstranda7af7042013-10-12 22:38:11 -05001868 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001869 if (!pixman_region32_contains_point(
1870 &view->transform.boundingbox, ix, iy, NULL))
1871 continue;
1872
1873 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
1874 view_ix = wl_fixed_to_int(view_x);
1875 view_iy = wl_fixed_to_int(view_y);
1876
1877 if (!pixman_region32_contains_point(&view->surface->input,
1878 view_ix, view_iy, NULL))
1879 continue;
1880
Pekka Paalanen380adf52015-02-16 14:39:11 +02001881 if (view->geometry.scissor_enabled &&
1882 !pixman_region32_contains_point(&view->geometry.scissor,
1883 view_ix, view_iy, NULL))
1884 continue;
1885
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001886 *vx = view_x;
1887 *vy = view_y;
1888 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001889 }
1890
Derek Foremanf9318d12015-05-11 15:40:11 -05001891 *vx = wl_fixed_from_int(-1000000);
1892 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001893 return NULL;
1894}
1895
1896static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001897weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001898{
Daniel Stone37816df2012-05-16 18:45:18 +01001899 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001900
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001901 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001902 return;
1903
Daniel Stone37816df2012-05-16 18:45:18 +01001904 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001905 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001906}
1907
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001908WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001909weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001910{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001911 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001912
Jason Ekstranda7af7042013-10-12 22:38:11 -05001913 if (!weston_view_is_mapped(view))
1914 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001915
Jason Ekstranda7af7042013-10-12 22:38:11 -05001916 weston_view_damage_below(view);
Semi Malinene7a52fb2018-04-26 11:08:10 +02001917 weston_view_set_output(view, NULL);
Xiong Zhang97116532013-10-23 13:58:31 +08001918 view->plane = NULL;
Armin Krezovićf8486c32016-06-30 06:04:28 +02001919 view->is_mapped = false;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001920 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001921 wl_list_remove(&view->link);
1922 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001923 view->output_mask = 0;
1924 weston_surface_assign_output(view->surface);
1925
1926 if (weston_surface_is_mapped(view->surface))
1927 return;
1928
1929 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001930 struct weston_touch *touch = weston_seat_get_touch(seat);
1931 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1932 struct weston_keyboard *keyboard =
1933 weston_seat_get_keyboard(seat);
1934
1935 if (keyboard && keyboard->focus == view->surface)
1936 weston_keyboard_set_focus(keyboard, NULL);
1937 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05001938 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05001939 if (touch && touch->focus == view)
1940 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001941 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001942}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001943
Jason Ekstranda7af7042013-10-12 22:38:11 -05001944WL_EXPORT void
1945weston_surface_unmap(struct weston_surface *surface)
1946{
1947 struct weston_view *view;
1948
Armin Krezovićf8486c32016-06-30 06:04:28 +02001949 surface->is_mapped = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001950 wl_list_for_each(view, &surface->views, surface_link)
1951 weston_view_unmap(view);
1952 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001953}
1954
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001955static void
1956weston_surface_reset_pending_buffer(struct weston_surface *surface)
1957{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001958 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001959 surface->pending.sx = 0;
1960 surface->pending.sy = 0;
1961 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001962 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001963}
1964
Jason Ekstranda7af7042013-10-12 22:38:11 -05001965WL_EXPORT void
1966weston_view_destroy(struct weston_view *view)
1967{
1968 wl_signal_emit(&view->destroy_signal, view);
1969
1970 assert(wl_list_empty(&view->geometry.child_list));
1971
1972 if (weston_view_is_mapped(view)) {
1973 weston_view_unmap(view);
1974 weston_compositor_build_view_list(view->surface->compositor);
1975 }
1976
1977 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001978 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001979
1980 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001981 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001982 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001983 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001984
1985 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen944fae82018-05-22 13:15:58 +03001986 weston_view_set_output(view, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001987
Jason Ekstranda7af7042013-10-12 22:38:11 -05001988 wl_list_remove(&view->surface_link);
1989
1990 free(view);
1991}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001992
1993WL_EXPORT void
1994weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001995{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001996 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001997 struct weston_view *ev, *nv;
Jonas Ådahld3414f22016-07-22 17:56:31 +08001998 struct weston_pointer_constraint *constraint, *next_constraint;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001999
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02002000 if (--surface->ref_count > 0)
2001 return;
2002
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03002003 assert(surface->resource == NULL);
2004
Pekka Paalanenca790762015-04-17 14:23:38 +03002005 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02002006
Pekka Paalanene67858b2013-04-25 13:57:42 +03002007 assert(wl_list_empty(&surface->subsurface_list_pending));
2008 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02002009
Jason Ekstranda7af7042013-10-12 22:38:11 -05002010 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
2011 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002012
Jason Ekstrand7b982072014-05-20 14:33:03 -05002013 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002014
Pekka Paalanende685b82012-12-04 15:58:12 +02002015 weston_buffer_reference(&surface->buffer_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002016 weston_buffer_release_reference(&surface->buffer_release_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04002017
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02002018 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002019 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002020 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02002021
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002022 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002023 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002024
Pekka Paalanen133e4392014-09-23 22:08:46 -04002025 weston_presentation_feedback_discard_list(&surface->feedback_list);
2026
Jonas Ådahld3414f22016-07-22 17:56:31 +08002027 wl_list_for_each_safe(constraint, next_constraint,
2028 &surface->pointer_constraints,
2029 link)
2030 weston_pointer_constraint_destroy(constraint);
2031
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03002032 fd_clear(&surface->acquire_fence_fd);
2033
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04002034 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05002035}
2036
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002037static void
2038destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08002039{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002040 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08002041
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03002042 assert(surface);
2043
Giulio Camuffo0d379742013-11-15 22:06:15 +01002044 /* Set the resource to NULL, since we don't want to leave a
2045 * dangling pointer if the surface was refcounted and survives
2046 * the weston_surface_destroy() call. */
2047 surface->resource = NULL;
Pekka Paalanen4826f872016-04-22 14:14:38 +03002048
2049 if (surface->viewport_resource)
2050 wl_resource_set_user_data(surface->viewport_resource, NULL);
2051
Alexandros Frantzis27d7c392018-10-19 12:14:11 +03002052 if (surface->synchronization_resource) {
2053 wl_resource_set_user_data(surface->synchronization_resource,
2054 NULL);
2055 }
2056
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002057 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08002058}
2059
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002060static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002061weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
2062{
2063 struct weston_buffer *buffer =
2064 container_of(listener, struct weston_buffer, destroy_listener);
2065
2066 wl_signal_emit(&buffer->destroy_signal, buffer);
2067 free(buffer);
2068}
2069
Giulio Camuffoe058cd12013-12-12 14:14:29 +01002070WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002071weston_buffer_from_resource(struct wl_resource *resource)
2072{
2073 struct weston_buffer *buffer;
2074 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002075
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002076 listener = wl_resource_get_destroy_listener(resource,
2077 weston_buffer_destroy_handler);
2078
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002079 if (listener)
2080 return container_of(listener, struct weston_buffer,
2081 destroy_listener);
2082
2083 buffer = zalloc(sizeof *buffer);
2084 if (buffer == NULL)
2085 return NULL;
2086
2087 buffer->resource = resource;
2088 wl_signal_init(&buffer->destroy_signal);
2089 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04002090 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002091 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002092
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002093 return buffer;
2094}
2095
2096static void
Pekka Paalanende685b82012-12-04 15:58:12 +02002097weston_buffer_reference_handle_destroy(struct wl_listener *listener,
2098 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002099{
Pekka Paalanende685b82012-12-04 15:58:12 +02002100 struct weston_buffer_reference *ref =
2101 container_of(listener, struct weston_buffer_reference,
2102 destroy_listener);
2103
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002104 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02002105 ref->buffer = NULL;
2106}
2107
2108WL_EXPORT void
2109weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002110 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002111{
2112 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05002113 ref->buffer->busy_count--;
2114 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002115 assert(wl_resource_get_client(ref->buffer->resource));
Matt Hoosier3052bc72017-09-26 08:09:40 -05002116 wl_buffer_send_release(ref->buffer->resource);
Kristian Høgsberg20347802013-03-04 12:07:46 -05002117 }
Pekka Paalanende685b82012-12-04 15:58:12 +02002118 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002119 }
2120
Pekka Paalanende685b82012-12-04 15:58:12 +02002121 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002122 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002123 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02002124 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02002125 }
2126
Pekka Paalanende685b82012-12-04 15:58:12 +02002127 ref->buffer = buffer;
2128 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
2129}
2130
2131static void
Alexandros Frantzis67629672018-10-19 12:14:11 +03002132weston_buffer_release_reference_handle_destroy(struct wl_listener *listener,
2133 void *data)
2134{
2135 struct weston_buffer_release_reference *ref =
2136 container_of(listener, struct weston_buffer_release_reference,
2137 destroy_listener);
2138
2139 assert((struct wl_resource *)data == ref->buffer_release->resource);
2140 ref->buffer_release = NULL;
2141}
2142
2143static void
2144weston_buffer_release_destroy(struct weston_buffer_release *buffer_release)
2145{
2146 struct wl_resource *resource = buffer_release->resource;
2147 int release_fence_fd = buffer_release->fence_fd;
2148
2149 if (release_fence_fd >= 0) {
2150 zwp_linux_buffer_release_v1_send_fenced_release(
2151 resource, release_fence_fd);
2152 } else {
2153 zwp_linux_buffer_release_v1_send_immediate_release(
2154 resource);
2155 }
2156
2157 wl_resource_destroy(resource);
2158}
2159
2160WL_EXPORT void
2161weston_buffer_release_reference(struct weston_buffer_release_reference *ref,
2162 struct weston_buffer_release *buffer_release)
2163{
2164 if (buffer_release == ref->buffer_release)
2165 return;
2166
2167 if (ref->buffer_release) {
2168 ref->buffer_release->ref_count--;
2169 wl_list_remove(&ref->destroy_listener.link);
2170 if (ref->buffer_release->ref_count == 0)
2171 weston_buffer_release_destroy(ref->buffer_release);
2172 }
2173
2174 if (buffer_release) {
2175 buffer_release->ref_count++;
2176 wl_resource_add_destroy_listener(buffer_release->resource,
2177 &ref->destroy_listener);
2178 }
2179
2180 ref->buffer_release = buffer_release;
2181 ref->destroy_listener.notify =
2182 weston_buffer_release_reference_handle_destroy;
2183}
2184
2185WL_EXPORT void
2186weston_buffer_release_move(struct weston_buffer_release_reference *dest,
2187 struct weston_buffer_release_reference *src)
2188{
2189 weston_buffer_release_reference(dest, src->buffer_release);
2190 weston_buffer_release_reference(src, NULL);
2191}
2192
2193static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002194weston_surface_attach(struct weston_surface *surface,
2195 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002196{
2197 weston_buffer_reference(&surface->buffer_ref, buffer);
2198
Pekka Paalanena6421c42012-12-04 15:58:10 +02002199 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002200 if (weston_surface_is_mapped(surface))
2201 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002202 }
2203
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002204 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02002205
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002206 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002207 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002208}
2209
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002210WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002211weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002212{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002213 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002214
2215 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002216 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002217}
2218
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05002219WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002220weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002221{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002222 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002223
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002224 pixman_region32_union(&compositor->primary_plane.damage,
2225 &compositor->primary_plane.damage,
2226 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002227 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002228}
2229
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04002230static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002231surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002232{
Pekka Paalanende685b82012-12-04 15:58:12 +02002233 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002234 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002235 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002236
Pekka Paalanenb5026542014-11-12 15:09:24 +02002237 if (weston_timeline_enabled_ &&
2238 pixman_region32_not_empty(&surface->damage))
2239 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
2240 TLP_OUTPUT(surface->output), TLP_END);
2241
Jason Ekstrandef540082014-06-26 10:37:36 -07002242 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002243}
2244
2245static void
2246view_accumulate_damage(struct weston_view *view,
2247 pixman_region32_t *opaque)
2248{
2249 pixman_region32_t damage;
2250
2251 pixman_region32_init(&damage);
2252 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002253 pixman_box32_t *extents;
2254
Jason Ekstranda7af7042013-10-12 22:38:11 -05002255 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002256 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002257 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002258 pixman_region32_copy(&damage, &view->surface->damage);
2259 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002260 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002261 }
2262
Pekka Paalanen380adf52015-02-16 14:39:11 +02002263 pixman_region32_intersect(&damage, &damage,
2264 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002265 pixman_region32_subtract(&damage, &damage, opaque);
2266 pixman_region32_union(&view->plane->damage,
2267 &view->plane->damage, &damage);
2268 pixman_region32_fini(&damage);
2269 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002270 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002271}
2272
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002273static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002274compositor_accumulate_damage(struct weston_compositor *ec)
2275{
2276 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002277 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002278 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002279
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002280 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002281
2282 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002283 pixman_region32_copy(&plane->clip, &clip);
2284
2285 pixman_region32_init(&opaque);
2286
Jason Ekstranda7af7042013-10-12 22:38:11 -05002287 wl_list_for_each(ev, &ec->view_list, link) {
2288 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002289 continue;
2290
Jason Ekstranda7af7042013-10-12 22:38:11 -05002291 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002292 }
2293
2294 pixman_region32_union(&clip, &clip, &opaque);
2295 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002296 }
2297
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002298 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002299
Jason Ekstranda7af7042013-10-12 22:38:11 -05002300 wl_list_for_each(ev, &ec->view_list, link)
Derek Foreman060cf112015-11-18 16:32:26 -06002301 ev->surface->touched = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002302
2303 wl_list_for_each(ev, &ec->view_list, link) {
2304 if (ev->surface->touched)
2305 continue;
Derek Foreman060cf112015-11-18 16:32:26 -06002306 ev->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002307
2308 surface_flush_damage(ev->surface);
2309
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002310 /* Both the renderer and the backend have seen the buffer
2311 * by now. If renderer needs the buffer, it has its own
2312 * reference set. If the backend wants to keep the buffer
2313 * around for migrating the surface into a non-primary plane
2314 * later, keep_buffer is true. Otherwise, drop the core
2315 * reference now, and allow early buffer release. This enables
2316 * clients to use single-buffering.
2317 */
Alexandros Frantzis67629672018-10-19 12:14:11 +03002318 if (!ev->surface->keep_buffer) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002319 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002320 weston_buffer_release_reference(
2321 &ev->surface->buffer_release_ref, NULL);
2322 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002323 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002324}
2325
2326static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002327surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002328{
2329 struct weston_subsurface *sub;
2330
Pekka Paalanene67858b2013-04-25 13:57:42 +03002331 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002332 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002333 continue;
2334
Jason Ekstranda7af7042013-10-12 22:38:11 -05002335 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2336 wl_list_init(&sub->surface->views);
2337
2338 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002339 }
2340}
2341
2342static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002343surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002344{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002345 struct weston_subsurface *sub;
2346 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002347
Jason Ekstranda7af7042013-10-12 22:38:11 -05002348 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2349 if (sub->surface == surface)
2350 continue;
2351
George Kiagiadakised04d382014-06-13 18:10:26 +02002352 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2353 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002354 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002355 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002356
2357 surface_free_unused_subsurface_views(sub->surface);
2358 }
2359}
2360
2361static void
2362view_list_add_subsurface_view(struct weston_compositor *compositor,
2363 struct weston_subsurface *sub,
2364 struct weston_view *parent)
2365{
2366 struct weston_subsurface *child;
2367 struct weston_view *view = NULL, *iv;
2368
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002369 if (!weston_surface_is_mapped(sub->surface))
2370 return;
2371
Jason Ekstranda7af7042013-10-12 22:38:11 -05002372 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2373 if (iv->geometry.parent == parent) {
2374 view = iv;
2375 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002376 }
2377 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002378
2379 if (view) {
2380 /* Put it back in the surface's list of views */
2381 wl_list_remove(&view->surface_link);
2382 wl_list_insert(&sub->surface->views, &view->surface_link);
2383 } else {
2384 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002385 weston_view_set_position(view,
2386 sub->position.x,
2387 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002388 weston_view_set_transform_parent(view, parent);
2389 }
2390
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002391 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002392 weston_view_update_transform(view);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002393 view->is_mapped = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002394
Pekka Paalanenb188e912013-11-19 14:03:35 +02002395 if (wl_list_empty(&sub->surface->subsurface_list)) {
2396 wl_list_insert(compositor->view_list.prev, &view->link);
2397 return;
2398 }
2399
2400 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2401 if (child->surface == sub->surface)
2402 wl_list_insert(compositor->view_list.prev, &view->link);
2403 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002404 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002405 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002406}
2407
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002408/* This recursively adds the sub-surfaces for a view, relying on the
2409 * sub-surface order. Thus, if a client restacks the sub-surfaces, that
2410 * change first happens to the sub-surface list, and then automatically
2411 * propagates here. See weston_surface_damage_subsurfaces() for how the
2412 * sub-surfaces receive damage when the client changes the state.
2413 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002414static void
2415view_list_add(struct weston_compositor *compositor,
2416 struct weston_view *view)
2417{
2418 struct weston_subsurface *sub;
2419
2420 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002421
Pekka Paalanenb188e912013-11-19 14:03:35 +02002422 if (wl_list_empty(&view->surface->subsurface_list)) {
2423 wl_list_insert(compositor->view_list.prev, &view->link);
2424 return;
2425 }
2426
2427 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2428 if (sub->surface == view->surface)
2429 wl_list_insert(compositor->view_list.prev, &view->link);
2430 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002431 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002432 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002433}
2434
2435static void
2436weston_compositor_build_view_list(struct weston_compositor *compositor)
2437{
2438 struct weston_view *view;
2439 struct weston_layer *layer;
2440
2441 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002442 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002443 surface_stash_subsurface_views(view->surface);
2444
2445 wl_list_init(&compositor->view_list);
2446 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002447 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002448 view_list_add(compositor, view);
2449 }
2450 }
2451
2452 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002453 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002454 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002455}
2456
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002457static void
2458weston_output_take_feedback_list(struct weston_output *output,
2459 struct weston_surface *surface)
2460{
2461 struct weston_view *view;
2462 struct weston_presentation_feedback *feedback;
2463 uint32_t flags = 0xffffffff;
2464
2465 if (wl_list_empty(&surface->feedback_list))
2466 return;
2467
2468 /* All views must have the flag for the flag to survive. */
2469 wl_list_for_each(view, &surface->views, surface_link) {
2470 /* ignore views that are not on this output at all */
2471 if (view->output_mask & (1u << output->id))
2472 flags &= view->psf_flags;
2473 }
2474
2475 wl_list_for_each(feedback, &surface->feedback_list, link)
2476 feedback->psf_flags = flags;
2477
2478 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2479 wl_list_init(&surface->feedback_list);
2480}
2481
David Herrmann1edf44c2013-10-22 17:11:26 +02002482static int
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002483weston_output_repaint(struct weston_output *output, void *repaint_data)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002484{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002485 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002486 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002487 struct weston_animation *animation, *next;
2488 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002489 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002490 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002491 int r;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002492 uint32_t frame_time_msec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002493
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002494 if (output->destroying)
2495 return 0;
2496
Pekka Paalanenb5026542014-11-12 15:09:24 +02002497 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2498
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002499 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002500 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002501
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002502 if (output->assign_planes && !output->disable_planes) {
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002503 output->assign_planes(output, repaint_data);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002504 } else {
2505 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002506 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002507 ev->psf_flags = 0;
2508 }
2509 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002510
Pekka Paalanene67858b2013-04-25 13:57:42 +03002511 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002512 wl_list_for_each(ev, &ec->view_list, link) {
2513 /* Note: This operation is safe to do multiple times on the
2514 * same surface.
2515 */
2516 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002517 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002518 &ev->surface->frame_callback_list);
2519 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002520
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002521 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002522 }
2523 }
2524
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002525 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002526
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002527 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002528 pixman_region32_intersect(&output_damage,
2529 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002530 pixman_region32_subtract(&output_damage,
2531 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002532
Scott Moreauccbf29d2012-02-22 14:21:41 -07002533 if (output->dirty)
2534 weston_output_update_matrix(output);
2535
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002536 r = output->repaint(output, &output_damage, repaint_data);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002537
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002538 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002539
Daniel Stone09a97e22017-03-01 11:34:06 +00002540 output->repaint_needed = false;
Daniel Stone05df8c12017-03-03 16:59:42 +00002541 if (r == 0)
2542 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002543
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002544 weston_compositor_repick(ec);
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002545
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002546 frame_time_msec = timespec_to_msec(&output->frame_time);
2547
Jonas Ådahldb773762012-06-13 00:01:21 +02002548 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002549 wl_callback_send_done(cb->resource, frame_time_msec);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002550 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002551 }
2552
Scott Moreaud64cf212012-06-08 19:40:54 -06002553 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002554 animation->frame_counter++;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002555 animation->frame(animation, output, &output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002556 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002557
Pekka Paalanenb5026542014-11-12 15:09:24 +02002558 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2559
David Herrmann1edf44c2013-10-22 17:11:26 +02002560 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002561}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002562
Pekka Paalanen82919792014-05-21 13:51:49 +03002563static void
2564weston_output_schedule_repaint_reset(struct weston_output *output)
2565{
Daniel Stone05df8c12017-03-03 16:59:42 +00002566 output->repaint_status = REPAINT_NOT_SCHEDULED;
Pekka Paalanen82919792014-05-21 13:51:49 +03002567 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
Pekka Paalanen82919792014-05-21 13:51:49 +03002568}
2569
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002570static int
2571weston_output_maybe_repaint(struct weston_output *output, struct timespec *now,
2572 void *repaint_data)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002573{
Pekka Paalanen0513a952014-05-21 16:17:27 +03002574 struct weston_compositor *compositor = output->compositor;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002575 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002576 int64_t msec_to_repaint;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002577
Daniel Stone6847b852017-03-01 11:34:08 +00002578 /* We're not ready yet; come back to make a decision later. */
2579 if (output->repaint_status != REPAINT_SCHEDULED)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002580 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002581
2582 msec_to_repaint = timespec_sub_to_msec(&output->next_repaint, now);
2583 if (msec_to_repaint > 1)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002584 return ret;
Daniel Stone05df8c12017-03-03 16:59:42 +00002585
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002586 /* If we're sleeping, drop the repaint machinery entirely; we will
2587 * explicitly repaint all outputs when we come back. */
2588 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2589 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
2590 goto err;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002591
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002592 /* We don't actually need to repaint this output; drop it from
2593 * repaint until something causes damage. */
2594 if (!output->repaint_needed)
2595 goto err;
2596
2597 /* If repaint fails, we aren't going to get weston_output_finish_frame
2598 * to trigger a new repaint, so drop it from repaint and hope
Daniel Stone6847b852017-03-01 11:34:08 +00002599 * something schedules a successful repaint later. As repainting may
2600 * take some time, re-read our clock as a courtesy to the next
2601 * output. */
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002602 ret = weston_output_repaint(output, repaint_data);
Daniel Stone6847b852017-03-01 11:34:08 +00002603 weston_compositor_read_presentation_clock(compositor, now);
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002604 if (ret != 0)
2605 goto err;
2606
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09002607 output->repainted = true;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002608 return ret;
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002609
2610err:
Pekka Paalanen0513a952014-05-21 16:17:27 +03002611 weston_output_schedule_repaint_reset(output);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002612 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002613}
2614
2615static void
2616output_repaint_timer_arm(struct weston_compositor *compositor)
2617{
2618 struct weston_output *output;
2619 bool any_should_repaint = false;
2620 struct timespec now;
Sergi Granellb4c08862017-03-18 13:01:15 +01002621 int64_t msec_to_next = INT64_MAX;
Daniel Stone6847b852017-03-01 11:34:08 +00002622
2623 weston_compositor_read_presentation_clock(compositor, &now);
2624
2625 wl_list_for_each(output, &compositor->output_list, link) {
2626 int64_t msec_to_this;
2627
2628 if (output->repaint_status != REPAINT_SCHEDULED)
2629 continue;
2630
2631 msec_to_this = timespec_sub_to_msec(&output->next_repaint,
2632 &now);
2633 if (!any_should_repaint || msec_to_this < msec_to_next)
2634 msec_to_next = msec_to_this;
2635
2636 any_should_repaint = true;
2637 }
2638
2639 if (!any_should_repaint)
2640 return;
2641
2642 /* Even if we should repaint immediately, add the minimum 1 ms delay.
2643 * This is a workaround to allow coalescing multiple output repaints
2644 * particularly from weston_output_finish_frame()
2645 * into the same call, which would not happen if we called
2646 * output_repaint_timer_handler() directly.
2647 */
2648 if (msec_to_next < 1)
2649 msec_to_next = 1;
2650
2651 wl_event_source_timer_update(compositor->repaint_timer, msec_to_next);
2652}
2653
2654static int
2655output_repaint_timer_handler(void *data)
2656{
2657 struct weston_compositor *compositor = data;
2658 struct weston_output *output;
2659 struct timespec now;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002660 void *repaint_data = NULL;
Emre Ucane479ed82018-03-20 15:29:40 +01002661 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002662
2663 weston_compositor_read_presentation_clock(compositor, &now);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002664
2665 if (compositor->backend->repaint_begin)
2666 repaint_data = compositor->backend->repaint_begin(compositor);
2667
2668 wl_list_for_each(output, &compositor->output_list, link) {
2669 ret = weston_output_maybe_repaint(output, &now, repaint_data);
2670 if (ret)
2671 break;
2672 }
2673
2674 if (ret == 0) {
Tomohito Esaki09bfcd62018-06-05 10:37:05 +09002675 if (compositor->backend->repaint_flush)
2676 compositor->backend->repaint_flush(compositor,
2677 repaint_data);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002678 } else {
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09002679 wl_list_for_each(output, &compositor->output_list, link) {
2680 if (output->repainted)
2681 weston_output_schedule_repaint_reset(output);
2682 }
2683
Tomohito Esaki09bfcd62018-06-05 10:37:05 +09002684 if (compositor->backend->repaint_cancel)
2685 compositor->backend->repaint_cancel(compositor,
2686 repaint_data);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002687 }
Daniel Stone6847b852017-03-01 11:34:08 +00002688
Tomohito Esakiddaf95c2018-07-10 11:47:15 +09002689 wl_list_for_each(output, &compositor->output_list, link)
2690 output->repainted = false;
2691
Daniel Stone6847b852017-03-01 11:34:08 +00002692 output_repaint_timer_arm(compositor);
2693
Pekka Paalanen0513a952014-05-21 16:17:27 +03002694 return 0;
2695}
2696
Kristian Høgsbergef044142011-06-21 15:02:12 -04002697WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002698weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002699 const struct timespec *stamp,
2700 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002701{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002702 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002703 int32_t refresh_nsec;
2704 struct timespec now;
Daniel Stone6847b852017-03-01 11:34:08 +00002705 int64_t msec_rel;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002706
Pekka Paalanenb5026542014-11-12 15:09:24 +02002707
Daniel Stone05df8c12017-03-03 16:59:42 +00002708 assert(output->repaint_status == REPAINT_AWAITING_COMPLETION);
Daniel Stone3615ce12017-03-01 11:34:05 +00002709 assert(stamp || (presented_flags & WP_PRESENTATION_FEEDBACK_INVALID));
2710
Daniel Stone6847b852017-03-01 11:34:08 +00002711 weston_compositor_read_presentation_clock(compositor, &now);
2712
Daniel Stone3615ce12017-03-01 11:34:05 +00002713 /* If we haven't been supplied any timestamp at all, we don't have a
2714 * timebase to work against, so any delay just wastes time. Push a
2715 * repaint as soon as possible so we can get on with it. */
Daniel Stone6847b852017-03-01 11:34:08 +00002716 if (!stamp) {
2717 output->next_repaint = now;
Daniel Stone3615ce12017-03-01 11:34:05 +00002718 goto out;
Daniel Stone6847b852017-03-01 11:34:08 +00002719 }
Daniel Stone3615ce12017-03-01 11:34:05 +00002720
Marius Vladdf9278a2018-03-06 18:56:23 +02002721 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2722 TLP_VBLANK(stamp), TLP_END);
2723
Pekka Paalanend7894d02015-07-03 15:08:53 +03002724 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002725 weston_presentation_feedback_present_list(&output->feedback_list,
2726 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002727 output->msc,
2728 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002729
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002730 output->frame_time = *stamp;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002731
Daniel Stone6847b852017-03-01 11:34:08 +00002732 timespec_add_nsec(&output->next_repaint, stamp, refresh_nsec);
2733 timespec_add_msec(&output->next_repaint, &output->next_repaint,
2734 -compositor->repaint_msec);
2735 msec_rel = timespec_sub_to_msec(&output->next_repaint, &now);
Daniel Stone84aff5c2017-03-01 11:34:04 +00002736
2737 if (msec_rel < -1000 || msec_rel > 1000) {
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002738 static bool warned;
2739
2740 if (!warned)
2741 weston_log("Warning: computed repaint delay is "
Daniel Stone6847b852017-03-01 11:34:08 +00002742 "insane: %lld msec\n", (long long) msec_rel);
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002743 warned = true;
2744
Daniel Stone6847b852017-03-01 11:34:08 +00002745 output->next_repaint = now;
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002746 }
2747
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002748 /* Called from restart_repaint_loop and restart happens already after
2749 * the deadline given by repaint_msec? In that case we delay until
2750 * the deadline of the next frame, to give clients a more predictable
2751 * timing of the repaint cycle to lock on. */
Daniel Stoneeca5cca2017-02-28 21:53:51 +00002752 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID &&
2753 msec_rel < 0) {
2754 while (timespec_sub_to_nsec(&output->next_repaint, &now) < 0) {
2755 timespec_add_nsec(&output->next_repaint,
2756 &output->next_repaint,
2757 refresh_nsec);
2758 }
2759 }
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002760
Daniel Stone3615ce12017-03-01 11:34:05 +00002761out:
Daniel Stone05df8c12017-03-03 16:59:42 +00002762 output->repaint_status = REPAINT_SCHEDULED;
Daniel Stone6847b852017-03-01 11:34:08 +00002763 output_repaint_timer_arm(compositor);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002764}
2765
2766static void
2767idle_repaint(void *data)
2768{
2769 struct weston_output *output = data;
2770
Daniel Stone05df8c12017-03-03 16:59:42 +00002771 assert(output->repaint_status == REPAINT_BEGIN_FROM_IDLE);
2772 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03002773 output->idle_repaint_source = NULL;
Jonas Ådahle5a12252013-04-05 23:07:11 +02002774 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002775}
2776
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002777WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002778weston_layer_entry_insert(struct weston_layer_entry *list,
2779 struct weston_layer_entry *entry)
2780{
2781 wl_list_insert(&list->link, &entry->link);
2782 entry->layer = list->layer;
2783}
2784
2785WL_EXPORT void
2786weston_layer_entry_remove(struct weston_layer_entry *entry)
2787{
2788 wl_list_remove(&entry->link);
2789 wl_list_init(&entry->link);
2790 entry->layer = NULL;
2791}
2792
Quentin Glidic82681572016-12-17 13:40:51 +01002793
2794/** Initialize the weston_layer struct.
2795 *
2796 * \param compositor The compositor instance
2797 * \param layer The layer to initialize
2798 */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002799WL_EXPORT void
Quentin Glidic82681572016-12-17 13:40:51 +01002800weston_layer_init(struct weston_layer *layer,
2801 struct weston_compositor *compositor)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002802{
Quentin Glidic82681572016-12-17 13:40:51 +01002803 layer->compositor = compositor;
2804 wl_list_init(&layer->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002805 wl_list_init(&layer->view_list.link);
2806 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002807 weston_layer_set_mask_infinite(layer);
Quentin Glidic82681572016-12-17 13:40:51 +01002808}
2809
2810/** Sets the position of the layer in the layer list. The layer will be placed
2811 * below any layer with the same position value, if any.
2812 * This function is safe to call if the layer is already on the list, but the
2813 * layer may be moved below other layers at the same position, if any.
2814 *
2815 * \param layer The layer to modify
2816 * \param position The position the layer will be placed at
2817 */
2818WL_EXPORT void
2819weston_layer_set_position(struct weston_layer *layer,
2820 enum weston_layer_position position)
2821{
2822 struct weston_layer *below;
2823
2824 wl_list_remove(&layer->link);
2825
2826 /* layer_list is ordered from top to bottom, the last layer being the
2827 * background with the smallest position value */
2828
2829 layer->position = position;
2830 wl_list_for_each_reverse(below, &layer->compositor->layer_list, link) {
2831 if (below->position >= layer->position) {
2832 wl_list_insert(&below->link, &layer->link);
2833 return;
2834 }
2835 }
2836 wl_list_insert(&layer->compositor->layer_list, &layer->link);
2837}
2838
2839/** Hide a layer by taking it off the layer list.
2840 * This function is safe to call if the layer is not on the list.
2841 *
2842 * \param layer The layer to hide
2843 */
2844WL_EXPORT void
2845weston_layer_unset_position(struct weston_layer *layer)
2846{
2847 wl_list_remove(&layer->link);
2848 wl_list_init(&layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002849}
2850
2851WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002852weston_layer_set_mask(struct weston_layer *layer,
2853 int x, int y, int width, int height)
2854{
2855 struct weston_view *view;
2856
2857 layer->mask.x1 = x;
2858 layer->mask.x2 = x + width;
2859 layer->mask.y1 = y;
2860 layer->mask.y2 = y + height;
2861
2862 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2863 weston_view_geometry_dirty(view);
2864 }
2865}
2866
2867WL_EXPORT void
2868weston_layer_set_mask_infinite(struct weston_layer *layer)
2869{
2870 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2871 UINT32_MAX, UINT32_MAX);
2872}
2873
Daniel Stone3b775632018-07-20 08:38:25 +01002874WL_EXPORT bool
2875weston_layer_mask_is_infinite(struct weston_layer *layer)
2876{
2877 return layer->mask.x1 == INT32_MIN &&
2878 layer->mask.y1 == INT32_MIN &&
2879 layer->mask.x2 == INT32_MIN + UINT32_MAX &&
2880 layer->mask.y2 == INT32_MIN + UINT32_MAX;
2881}
2882
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002883WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002884weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002885{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002886 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002887 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002888
Bryce Harrington08976ac2016-08-30 12:05:16 -07002889 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2890 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002891 return;
2892
Pekka Paalanenb5026542014-11-12 15:09:24 +02002893 if (!output->repaint_needed)
2894 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2895
Kristian Høgsbergef044142011-06-21 15:02:12 -04002896 loop = wl_display_get_event_loop(compositor->wl_display);
Daniel Stone09a97e22017-03-01 11:34:06 +00002897 output->repaint_needed = true;
Daniel Stone05df8c12017-03-03 16:59:42 +00002898
2899 /* If we already have a repaint scheduled for our idle handler,
2900 * no need to set it again. If the repaint has been called but
2901 * not finished, then weston_output_finish_frame() will notice
2902 * that a repaint is needed and schedule one. */
2903 if (output->repaint_status != REPAINT_NOT_SCHEDULED)
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002904 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002905
Daniel Stone05df8c12017-03-03 16:59:42 +00002906 output->repaint_status = REPAINT_BEGIN_FROM_IDLE;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03002907 assert(!output->idle_repaint_source);
2908 output->idle_repaint_source = wl_event_loop_add_idle(loop, idle_repaint,
2909 output);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002910 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002911}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002912
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002913WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002914weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2915{
2916 struct weston_output *output;
2917
2918 wl_list_for_each(output, &compositor->output_list, link)
2919 weston_output_schedule_repaint(output);
2920}
2921
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002922static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002923surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002924{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002925 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002926}
2927
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002928static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002929surface_attach(struct wl_client *client,
2930 struct wl_resource *resource,
2931 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2932{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002933 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002934 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002935
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002936 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002937 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002938 if (buffer == NULL) {
2939 wl_client_post_no_memory(client);
2940 return;
2941 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002942 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002943
Pekka Paalanende685b82012-12-04 15:58:12 +02002944 /* Attach, attach, without commit in between does not send
2945 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002946 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002947
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002948 surface->pending.sx = sx;
2949 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002950 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002951}
2952
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002953static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002954surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002955 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002956 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002957{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002958 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002959
Derek Foreman57e92ed2015-11-17 14:11:35 -06002960 if (width <= 0 || height <= 0)
2961 return;
2962
Derek Foreman152254b2015-11-26 14:17:48 -06002963 pixman_region32_union_rect(&surface->pending.damage_surface,
2964 &surface->pending.damage_surface,
2965 x, y, width, height);
2966}
2967
2968static void
2969surface_damage_buffer(struct wl_client *client,
2970 struct wl_resource *resource,
2971 int32_t x, int32_t y, int32_t width, int32_t height)
2972{
2973 struct weston_surface *surface = wl_resource_get_user_data(resource);
2974
2975 if (width <= 0 || height <= 0)
2976 return;
2977
2978 pixman_region32_union_rect(&surface->pending.damage_buffer,
2979 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002980 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002981}
2982
Kristian Høgsberg33418202011-08-16 23:01:28 -04002983static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002984destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002985{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002986 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002987
2988 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002989 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002990}
2991
2992static void
2993surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002994 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002995{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002996 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002997 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002998
2999 cb = malloc(sizeof *cb);
3000 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003001 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003002 return;
3003 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03003004
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003005 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
3006 callback);
3007 if (cb->resource == NULL) {
3008 free(cb);
3009 wl_resource_post_no_memory(resource);
3010 return;
3011 }
3012
Jason Ekstranda85118c2013-06-27 20:17:02 -05003013 wl_resource_set_implementation(cb->resource, NULL, cb,
3014 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003015
Pekka Paalanenbc106382012-10-10 12:49:31 +03003016 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003017}
3018
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003019static void
3020surface_set_opaque_region(struct wl_client *client,
3021 struct wl_resource *resource,
3022 struct wl_resource *region_resource)
3023{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003024 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003025 struct weston_region *region;
3026
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003027 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003028 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003029 pixman_region32_copy(&surface->pending.opaque,
3030 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003031 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07003032 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003033 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003034}
3035
3036static void
3037surface_set_input_region(struct wl_client *client,
3038 struct wl_resource *resource,
3039 struct wl_resource *region_resource)
3040{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003041 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003042 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003043
3044 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003045 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003046 pixman_region32_copy(&surface->pending.input,
3047 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003048 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003049 pixman_region32_fini(&surface->pending.input);
3050 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003051 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003052}
3053
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003054/* Cause damage to this sub-surface and all its children.
3055 *
3056 * This is useful when there are state changes that need an implicit
3057 * damage, e.g. a z-order change.
3058 */
3059static void
3060weston_surface_damage_subsurfaces(struct weston_subsurface *sub)
3061{
3062 struct weston_subsurface *child;
3063
3064 weston_surface_damage(sub->surface);
3065 sub->reordered = false;
3066
3067 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link)
3068 if (child != sub)
3069 weston_surface_damage_subsurfaces(child);
3070}
3071
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003072static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003073weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003074{
Pekka Paalanene67858b2013-04-25 13:57:42 +03003075 struct weston_subsurface *sub;
3076
3077 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
3078 parent_link_pending) {
3079 wl_list_remove(&sub->parent_link);
3080 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003081
3082 if (sub->reordered)
3083 weston_surface_damage_subsurfaces(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003084 }
3085}
3086
3087static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03003088weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05003089 struct weston_matrix *matrix)
3090{
Pekka Paalanen04baea52016-04-26 15:50:58 +03003091 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05003092 double src_width, src_height, dest_width, dest_height;
3093
3094 weston_matrix_init(matrix);
3095
3096 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
3097 src_width = surface->width_from_buffer;
3098 src_height = surface->height_from_buffer;
3099 } else {
3100 src_width = wl_fixed_to_double(vp->buffer.src_width);
3101 src_height = wl_fixed_to_double(vp->buffer.src_height);
3102 }
3103
3104 if (vp->surface.width == -1) {
3105 dest_width = src_width;
3106 dest_height = src_height;
3107 } else {
3108 dest_width = vp->surface.width;
3109 dest_height = vp->surface.height;
3110 }
3111
3112 if (src_width != dest_width || src_height != dest_height)
3113 weston_matrix_scale(matrix,
3114 src_width / dest_width,
3115 src_height / dest_height, 1);
3116
3117 if (vp->buffer.src_width != wl_fixed_from_int(-1))
3118 weston_matrix_translate(matrix,
3119 wl_fixed_to_double(vp->buffer.src_x),
3120 wl_fixed_to_double(vp->buffer.src_y),
3121 0);
3122
3123 switch (vp->buffer.transform) {
3124 case WL_OUTPUT_TRANSFORM_FLIPPED:
3125 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3126 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3127 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3128 weston_matrix_scale(matrix, -1, 1, 1);
3129 weston_matrix_translate(matrix,
3130 surface->width_from_buffer, 0, 0);
3131 break;
3132 }
3133
3134 switch (vp->buffer.transform) {
3135 default:
3136 case WL_OUTPUT_TRANSFORM_NORMAL:
3137 case WL_OUTPUT_TRANSFORM_FLIPPED:
3138 break;
3139 case WL_OUTPUT_TRANSFORM_90:
3140 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3141 weston_matrix_rotate_xy(matrix, 0, 1);
3142 weston_matrix_translate(matrix,
3143 surface->height_from_buffer, 0, 0);
3144 break;
3145 case WL_OUTPUT_TRANSFORM_180:
3146 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3147 weston_matrix_rotate_xy(matrix, -1, 0);
3148 weston_matrix_translate(matrix,
3149 surface->width_from_buffer,
3150 surface->height_from_buffer, 0);
3151 break;
3152 case WL_OUTPUT_TRANSFORM_270:
3153 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3154 weston_matrix_rotate_xy(matrix, 0, -1);
3155 weston_matrix_translate(matrix,
3156 0, surface->width_from_buffer, 0);
3157 break;
3158 }
3159
3160 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
3161}
3162
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003163/**
3164 * Compute a + b > c while being safe to overflows.
3165 */
3166static bool
3167fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
3168{
3169 return (int64_t)a + (int64_t)b > (int64_t)c;
3170}
3171
3172static bool
3173weston_surface_is_pending_viewport_source_valid(
3174 const struct weston_surface *surface)
3175{
3176 const struct weston_surface_state *pend = &surface->pending;
3177 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
3178 int width_from_buffer = 0;
3179 int height_from_buffer = 0;
3180 wl_fixed_t w;
3181 wl_fixed_t h;
3182
3183 /* If viewport source rect is not set, it is always ok. */
3184 if (vp->buffer.src_width == wl_fixed_from_int(-1))
3185 return true;
3186
3187 if (pend->newly_attached) {
3188 if (pend->buffer) {
3189 convert_size_by_transform_scale(&width_from_buffer,
3190 &height_from_buffer,
3191 pend->buffer->width,
3192 pend->buffer->height,
3193 vp->buffer.transform,
3194 vp->buffer.scale);
3195 } else {
3196 /* No buffer: viewport is irrelevant. */
3197 return true;
3198 }
3199 } else {
3200 width_from_buffer = surface->width_from_buffer;
3201 height_from_buffer = surface->height_from_buffer;
3202 }
3203
3204 assert((width_from_buffer == 0) == (height_from_buffer == 0));
3205 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
3206
3207 /* No buffer: viewport is irrelevant. */
3208 if (width_from_buffer == 0 || height_from_buffer == 0)
3209 return true;
3210
3211 /* overflow checks for wl_fixed_from_int() */
3212 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
3213 return false;
3214 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
3215 return false;
3216
3217 w = wl_fixed_from_int(width_from_buffer);
3218 h = wl_fixed_from_int(height_from_buffer);
3219
3220 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
3221 return false;
3222 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
3223 return false;
3224
3225 return true;
3226}
3227
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003228static bool
3229fixed_is_integer(wl_fixed_t v)
3230{
3231 return (v & 0xff) == 0;
3232}
3233
3234static bool
3235weston_surface_is_pending_viewport_dst_size_int(
3236 const struct weston_surface *surface)
3237{
3238 const struct weston_buffer_viewport *vp =
3239 &surface->pending.buffer_viewport;
3240
3241 if (vp->surface.width != -1) {
3242 assert(vp->surface.width > 0 && vp->surface.height > 0);
3243 return true;
3244 }
3245
3246 return fixed_is_integer(vp->buffer.src_width) &&
3247 fixed_is_integer(vp->buffer.src_height);
3248}
3249
Derek Foreman152254b2015-11-26 14:17:48 -06003250/* Translate pending damage in buffer co-ordinates to surface
3251 * co-ordinates and union it with a pixman_region32_t.
3252 * This should only be called after the buffer is attached.
3253 */
3254static void
3255apply_damage_buffer(pixman_region32_t *dest,
3256 struct weston_surface *surface,
3257 struct weston_surface_state *state)
3258{
3259 struct weston_buffer *buffer = surface->buffer_ref.buffer;
3260
3261 /* wl_surface.damage_buffer needs to be clipped to the buffer,
3262 * translated into surface co-ordinates and unioned with
3263 * any other surface damage.
3264 * None of this makes sense if there is no buffer though.
3265 */
3266 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
3267 pixman_region32_t buffer_damage;
3268
3269 pixman_region32_intersect_rect(&state->damage_buffer,
3270 &state->damage_buffer,
3271 0, 0, buffer->width,
3272 buffer->height);
3273 pixman_region32_init(&buffer_damage);
3274 weston_matrix_transform_region(&buffer_damage,
3275 &surface->buffer_to_surface_matrix,
3276 &state->damage_buffer);
3277 pixman_region32_union(dest, dest, &buffer_damage);
3278 pixman_region32_fini(&buffer_damage);
3279 }
3280 /* We should clear this on commit even if there was no buffer */
3281 pixman_region32_clear(&state->damage_buffer);
3282}
3283
Jason Ekstrand1e059042014-10-16 10:55:19 -05003284static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05003285weston_surface_commit_state(struct weston_surface *surface,
3286 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003287{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003288 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003289 pixman_region32_t opaque;
3290
Alexander Larsson4ea95522013-05-22 14:41:37 +02003291 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02003292 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03003293 /* wp_viewport.set_source */
3294 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003295 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003296
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003297 /* wl_surface.attach */
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003298 if (state->newly_attached) {
3299 /* zwp_surface_synchronization_v1.set_acquire_fence */
3300 fd_move(&surface->acquire_fence_fd,
3301 &state->acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003302 /* zwp_surface_synchronization_v1.get_release */
3303 weston_buffer_release_move(&surface->buffer_release_ref,
3304 &state->buffer_release_ref);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003305
Jason Ekstrand7b982072014-05-20 14:33:03 -05003306 weston_surface_attach(surface, state->buffer);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003307 }
Jason Ekstrand7b982072014-05-20 14:33:03 -05003308 weston_surface_state_set_buffer(state, NULL);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003309 assert(state->acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003310 assert(state->buffer_release_ref.buffer_release == NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01003311
Jason Ekstrand1e059042014-10-16 10:55:19 -05003312 weston_surface_build_buffer_matrix(surface,
3313 &surface->surface_to_buffer_matrix);
3314 weston_matrix_invert(&surface->buffer_to_surface_matrix,
3315 &surface->surface_to_buffer_matrix);
3316
Jason Ekstrand7b982072014-05-20 14:33:03 -05003317 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003318 weston_surface_update_size(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003319 if (surface->committed)
3320 surface->committed(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003321 }
Giulio Camuffo184df502013-02-21 11:29:21 +01003322
Jason Ekstrand7b982072014-05-20 14:33:03 -05003323 state->sx = 0;
3324 state->sy = 0;
3325 state->newly_attached = 0;
3326 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03003327
Derek Foreman152254b2015-11-26 14:17:48 -06003328 /* wl_surface.damage and wl_surface.damage_buffer */
Pekka Paalanenb5026542014-11-12 15:09:24 +02003329 if (weston_timeline_enabled_ &&
Derek Foreman152254b2015-11-26 14:17:48 -06003330 (pixman_region32_not_empty(&state->damage_surface) ||
3331 pixman_region32_not_empty(&state->damage_buffer)))
Pekka Paalanenb5026542014-11-12 15:09:24 +02003332 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06003333
Pekka Paalanen8e159182012-10-10 12:49:25 +03003334 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06003335 &state->damage_surface);
3336
3337 apply_damage_buffer(&surface->damage, surface, state);
3338
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05003339 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07003340 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06003341 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003342
3343 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003344 pixman_region32_init(&opaque);
3345 pixman_region32_intersect_rect(&opaque, &state->opaque,
3346 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003347
3348 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
3349 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003350 wl_list_for_each(view, &surface->views, surface_link)
3351 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003352 }
3353
3354 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003355
3356 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003357 pixman_region32_intersect_rect(&surface->input, &state->input,
3358 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003359
Pekka Paalanenbc106382012-10-10 12:49:31 +03003360 /* wl_surface.frame */
3361 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05003362 &state->frame_callback_list);
3363 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003364
3365 /* XXX:
3366 * What should happen with a feedback request, if there
3367 * is no wl_buffer attached for this commit?
3368 */
3369
3370 /* presentation.feedback */
3371 wl_list_insert_list(&surface->feedback_list,
3372 &state->feedback_list);
3373 wl_list_init(&state->feedback_list);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003374
3375 wl_signal_emit(&surface->commit_signal, surface);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003376}
3377
3378static void
3379weston_surface_commit(struct weston_surface *surface)
3380{
3381 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003382
Pekka Paalanene67858b2013-04-25 13:57:42 +03003383 weston_surface_commit_subsurface_order(surface);
3384
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003385 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003386}
3387
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003388static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003389weston_subsurface_commit(struct weston_subsurface *sub);
3390
3391static void
3392weston_subsurface_parent_commit(struct weston_subsurface *sub,
3393 int parent_is_synchronized);
3394
3395static void
3396surface_commit(struct wl_client *client, struct wl_resource *resource)
3397{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003398 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003399 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
3400
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003401 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
3402 assert(surface->viewport_resource);
3403
3404 wl_resource_post_error(surface->viewport_resource,
3405 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
3406 "wl_surface@%d has viewport source outside buffer",
3407 wl_resource_get_id(resource));
3408 return;
3409 }
3410
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003411 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
3412 assert(surface->viewport_resource);
3413
3414 wl_resource_post_error(surface->viewport_resource,
3415 WP_VIEWPORT_ERROR_BAD_SIZE,
3416 "wl_surface@%d viewport dst size not integer",
3417 wl_resource_get_id(resource));
3418 return;
3419 }
3420
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003421 if (surface->pending.acquire_fence_fd >= 0) {
3422 assert(surface->synchronization_resource);
3423
3424 if (!surface->pending.buffer) {
3425 fd_clear(&surface->pending.acquire_fence_fd);
3426 wl_resource_post_error(surface->synchronization_resource,
3427 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3428 "wl_surface@%"PRIu32" no buffer for synchronization",
3429 wl_resource_get_id(resource));
3430 return;
3431 }
3432
3433 /* We support fences for both wp_linux_dmabuf and opaque EGL
3434 * buffers, as mandated by minor version 2 of the
3435 * zwp_linux_explicit_synchronization_v1 protocol. Since
3436 * renderers that support fences currently only support these
3437 * two buffer types plus SHM buffers, we can just check for the
3438 * SHM buffer case here.
3439 */
3440 if (wl_shm_buffer_get(surface->pending.buffer->resource)) {
3441 fd_clear(&surface->pending.acquire_fence_fd);
3442 wl_resource_post_error(surface->synchronization_resource,
3443 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_UNSUPPORTED_BUFFER,
3444 "wl_surface@%"PRIu32" unsupported buffer for synchronization",
3445 wl_resource_get_id(resource));
3446 return;
3447 }
3448 }
3449
Alexandros Frantzis67629672018-10-19 12:14:11 +03003450 if (surface->pending.buffer_release_ref.buffer_release &&
3451 !surface->pending.buffer) {
3452 assert(surface->synchronization_resource);
3453
3454 wl_resource_post_error(surface->synchronization_resource,
3455 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3456 "wl_surface@%"PRIu32" no buffer for synchronization",
3457 wl_resource_get_id(resource));
3458 return;
3459 }
3460
Pekka Paalanene67858b2013-04-25 13:57:42 +03003461 if (sub) {
3462 weston_subsurface_commit(sub);
3463 return;
3464 }
3465
3466 weston_surface_commit(surface);
3467
3468 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
3469 if (sub->surface != surface)
3470 weston_subsurface_parent_commit(sub, 0);
3471 }
3472}
3473
3474static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003475surface_set_buffer_transform(struct wl_client *client,
3476 struct wl_resource *resource, int transform)
3477{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003478 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003479
Jonny Lamba55f1392014-05-30 12:07:15 +02003480 /* if wl_output.transform grows more members this will need to be updated. */
3481 if (transform < 0 ||
3482 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
3483 wl_resource_post_error(resource,
3484 WL_SURFACE_ERROR_INVALID_TRANSFORM,
3485 "buffer transform must be a valid transform "
3486 "('%d' specified)", transform);
3487 return;
3488 }
3489
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003490 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003491 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003492}
3493
Alexander Larsson4ea95522013-05-22 14:41:37 +02003494static void
3495surface_set_buffer_scale(struct wl_client *client,
3496 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003497 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003498{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003499 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003500
Jonny Lamba55f1392014-05-30 12:07:15 +02003501 if (scale < 1) {
3502 wl_resource_post_error(resource,
3503 WL_SURFACE_ERROR_INVALID_SCALE,
3504 "buffer scale must be at least one "
3505 "('%d' specified)", scale);
3506 return;
3507 }
3508
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003509 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003510 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003511}
3512
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003513static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003514 surface_destroy,
3515 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04003516 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003517 surface_frame,
3518 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003519 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003520 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02003521 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06003522 surface_set_buffer_scale,
3523 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003524};
3525
3526static void
3527compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003528 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003529{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04003530 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003531 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003532
Kristian Høgsberg18c93002012-01-27 11:58:31 -05003533 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003534 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003535 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003536 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003537 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003538
Jason Ekstranda85118c2013-06-27 20:17:02 -05003539 surface->resource =
3540 wl_resource_create(client, &wl_surface_interface,
3541 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003542 if (surface->resource == NULL) {
3543 weston_surface_destroy(surface);
3544 wl_resource_post_no_memory(resource);
3545 return;
3546 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003547 wl_resource_set_implementation(surface->resource, &surface_interface,
3548 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003549
3550 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003551}
3552
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003553static void
3554destroy_region(struct wl_resource *resource)
3555{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003556 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003557
3558 pixman_region32_fini(&region->region);
3559 free(region);
3560}
3561
3562static void
3563region_destroy(struct wl_client *client, struct wl_resource *resource)
3564{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003565 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003566}
3567
3568static void
3569region_add(struct wl_client *client, struct wl_resource *resource,
3570 int32_t x, int32_t y, int32_t width, int32_t height)
3571{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003572 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003573
3574 pixman_region32_union_rect(&region->region, &region->region,
3575 x, y, width, height);
3576}
3577
3578static void
3579region_subtract(struct wl_client *client, struct wl_resource *resource,
3580 int32_t x, int32_t y, int32_t width, int32_t height)
3581{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003582 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003583 pixman_region32_t rect;
3584
3585 pixman_region32_init_rect(&rect, x, y, width, height);
3586 pixman_region32_subtract(&region->region, &region->region, &rect);
3587 pixman_region32_fini(&rect);
3588}
3589
3590static const struct wl_region_interface region_interface = {
3591 region_destroy,
3592 region_add,
3593 region_subtract
3594};
3595
3596static void
3597compositor_create_region(struct wl_client *client,
3598 struct wl_resource *resource, uint32_t id)
3599{
3600 struct weston_region *region;
3601
3602 region = malloc(sizeof *region);
3603 if (region == NULL) {
3604 wl_resource_post_no_memory(resource);
3605 return;
3606 }
3607
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003608 pixman_region32_init(&region->region);
3609
Jason Ekstranda85118c2013-06-27 20:17:02 -05003610 region->resource =
3611 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003612 if (region->resource == NULL) {
3613 free(region);
3614 wl_resource_post_no_memory(resource);
3615 return;
3616 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003617 wl_resource_set_implementation(region->resource, &region_interface,
3618 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003619}
3620
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003621static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003622 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003623 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003624};
3625
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003626static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003627weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3628{
3629 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003630
Jason Ekstrand7b982072014-05-20 14:33:03 -05003631 weston_surface_commit_state(surface, &sub->cached);
3632 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003633
3634 weston_surface_commit_subsurface_order(surface);
3635
3636 weston_surface_schedule_repaint(surface);
3637
Jason Ekstrand7b982072014-05-20 14:33:03 -05003638 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003639}
3640
3641static void
3642weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3643{
3644 struct weston_surface *surface = sub->surface;
3645
3646 /*
3647 * If this commit would cause the surface to move by the
3648 * attach(dx, dy) parameters, the old damage region must be
3649 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05003650 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003651 */
Derek Foreman152254b2015-11-26 14:17:48 -06003652 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003653 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06003654 pixman_region32_union(&sub->cached.damage_surface,
3655 &sub->cached.damage_surface,
3656 &surface->pending.damage_surface);
3657 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003658
3659 if (surface->pending.newly_attached) {
3660 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003661 weston_surface_state_set_buffer(&sub->cached,
3662 surface->pending.buffer);
3663 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003664 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003665 weston_presentation_feedback_discard_list(
3666 &sub->cached.feedback_list);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003667 /* zwp_surface_synchronization_v1.set_acquire_fence */
3668 fd_move(&sub->cached.acquire_fence_fd,
3669 &surface->pending.acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003670 /* zwp_surface_synchronization_v1.get_release */
3671 weston_buffer_release_move(&sub->cached.buffer_release_ref,
3672 &surface->pending.buffer_release_ref);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003673 }
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003674 assert(surface->pending.acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003675 assert(surface->pending.buffer_release_ref.buffer_release == NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003676 sub->cached.sx += surface->pending.sx;
3677 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003678
Derek Foreman152254b2015-11-26 14:17:48 -06003679 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
3680
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003681 sub->cached.buffer_viewport.changed |=
3682 surface->pending.buffer_viewport.changed;
3683 sub->cached.buffer_viewport.buffer =
3684 surface->pending.buffer_viewport.buffer;
3685 sub->cached.buffer_viewport.surface =
3686 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003687
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003688 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003689
3690 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3691
3692 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3693
3694 wl_list_insert_list(&sub->cached.frame_callback_list,
3695 &surface->pending.frame_callback_list);
3696 wl_list_init(&surface->pending.frame_callback_list);
3697
Pekka Paalanen133e4392014-09-23 22:08:46 -04003698 wl_list_insert_list(&sub->cached.feedback_list,
3699 &surface->pending.feedback_list);
3700 wl_list_init(&surface->pending.feedback_list);
3701
Jason Ekstrand7b982072014-05-20 14:33:03 -05003702 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003703}
3704
Derek Foreman280e7dd2014-10-03 13:13:42 -05003705static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003706weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3707{
3708 while (sub) {
3709 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003710 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003711
3712 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003713 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003714
3715 sub = weston_surface_to_subsurface(sub->parent);
3716 }
3717
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003718 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003719}
3720
3721static void
3722weston_subsurface_commit(struct weston_subsurface *sub)
3723{
3724 struct weston_surface *surface = sub->surface;
3725 struct weston_subsurface *tmp;
3726
3727 /* Recursive check for effectively synchronized. */
3728 if (weston_subsurface_is_synchronized(sub)) {
3729 weston_subsurface_commit_to_cache(sub);
3730 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003731 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003732 /* flush accumulated state from cache */
3733 weston_subsurface_commit_to_cache(sub);
3734 weston_subsurface_commit_from_cache(sub);
3735 } else {
3736 weston_surface_commit(surface);
3737 }
3738
3739 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3740 if (tmp->surface != surface)
3741 weston_subsurface_parent_commit(tmp, 0);
3742 }
3743 }
3744}
3745
3746static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003747weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003748{
3749 struct weston_surface *surface = sub->surface;
3750 struct weston_subsurface *tmp;
3751
Pekka Paalanene67858b2013-04-25 13:57:42 +03003752 /* From now on, commit_from_cache the whole sub-tree, regardless of
3753 * the synchronized mode of each child. This sub-surface or some
3754 * of its ancestors were synchronized, so we are synchronized
3755 * all the way down.
3756 */
3757
Jason Ekstrand7b982072014-05-20 14:33:03 -05003758 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003759 weston_subsurface_commit_from_cache(sub);
3760
3761 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3762 if (tmp->surface != surface)
3763 weston_subsurface_parent_commit(tmp, 1);
3764 }
3765}
3766
3767static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003768weston_subsurface_parent_commit(struct weston_subsurface *sub,
3769 int parent_is_synchronized)
3770{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003771 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003772 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003773 wl_list_for_each(view, &sub->surface->views, surface_link)
3774 weston_view_set_position(view,
3775 sub->position.x,
3776 sub->position.y);
3777
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003778 sub->position.set = 0;
3779 }
3780
3781 if (parent_is_synchronized || sub->synchronized)
3782 weston_subsurface_synchronized_commit(sub);
3783}
3784
Pekka Paalanen8274d902014-08-06 19:36:51 +03003785static int
3786subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3787{
3788 return snprintf(buf, len, "sub-surface");
3789}
3790
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003791static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003792subsurface_committed(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003793{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003794 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003795
Jason Ekstranda7af7042013-10-12 22:38:11 -05003796 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003797 weston_view_set_position(view,
3798 view->geometry.x + dx,
3799 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003800
3801 /* No need to check parent mappedness, because if parent is not
3802 * mapped, parent is not in a visible layer, so this sub-surface
3803 * will not be drawn either.
3804 */
Armin Krezovićf8486c32016-06-30 06:04:28 +02003805
Pekka Paalanene67858b2013-04-25 13:57:42 +03003806 if (!weston_surface_is_mapped(surface)) {
Armin Krezovićf8486c32016-06-30 06:04:28 +02003807 surface->is_mapped = true;
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003808
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003809 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003810 * because that would call it also for the parent surface,
3811 * which might not be mapped yet. That would lead to
3812 * inconsistent state, where the window could never be
3813 * mapped.
3814 *
Armin Krezovićf8486c32016-06-30 06:04:28 +02003815 * Instead just force the is_mapped flag on, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003816 * weston_surface_is_mapped() return true, so that when the
3817 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003818 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003819 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03003820 }
3821}
3822
3823static struct weston_subsurface *
3824weston_surface_to_subsurface(struct weston_surface *surface)
3825{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003826 if (surface->committed == subsurface_committed)
3827 return surface->committed_private;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003828
3829 return NULL;
3830}
3831
Pekka Paalanen01388e22013-04-25 13:57:44 +03003832WL_EXPORT struct weston_surface *
3833weston_surface_get_main_surface(struct weston_surface *surface)
3834{
3835 struct weston_subsurface *sub;
3836
3837 while (surface && (sub = weston_surface_to_subsurface(surface)))
3838 surface = sub->parent;
3839
3840 return surface;
3841}
3842
Pekka Paalanen50b67472014-10-01 15:02:41 +03003843WL_EXPORT int
3844weston_surface_set_role(struct weston_surface *surface,
3845 const char *role_name,
3846 struct wl_resource *error_resource,
3847 uint32_t error_code)
3848{
3849 assert(role_name);
3850
3851 if (surface->role_name == NULL ||
3852 surface->role_name == role_name ||
3853 strcmp(surface->role_name, role_name) == 0) {
3854 surface->role_name = role_name;
3855
3856 return 0;
3857 }
3858
3859 wl_resource_post_error(error_resource, error_code,
3860 "Cannot assign role %s to wl_surface@%d,"
3861 " already has role %s\n",
3862 role_name,
3863 wl_resource_get_id(surface->resource),
3864 surface->role_name);
3865 return -1;
3866}
3867
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02003868WL_EXPORT const char *
3869weston_surface_get_role(struct weston_surface *surface)
3870{
3871 return surface->role_name;
3872}
3873
Pekka Paalanen8274d902014-08-06 19:36:51 +03003874WL_EXPORT void
3875weston_surface_set_label_func(struct weston_surface *surface,
3876 int (*desc)(struct weston_surface *,
3877 char *, size_t))
3878{
3879 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02003880 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003881}
3882
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003883/** Get the size of surface contents
3884 *
3885 * \param surface The surface to query.
3886 * \param width Returns the width of raw contents.
3887 * \param height Returns the height of raw contents.
3888 *
3889 * Retrieves the raw surface content size in pixels for the given surface.
3890 * This is the whole content size in buffer pixels. If the surface
3891 * has no content or the renderer does not implement this feature,
3892 * zeroes are returned.
3893 *
3894 * This function is used to determine the buffer size needed for
3895 * a weston_surface_copy_content() call.
3896 */
3897WL_EXPORT void
3898weston_surface_get_content_size(struct weston_surface *surface,
3899 int *width, int *height)
3900{
3901 struct weston_renderer *rer = surface->compositor->renderer;
3902
3903 if (!rer->surface_get_content_size) {
3904 *width = 0;
3905 *height = 0;
3906 return;
3907 }
3908
3909 rer->surface_get_content_size(surface, width, height);
3910}
3911
Quentin Glidic248dd102016-08-12 10:41:34 +02003912/** Get the bounding box of a surface and its subsurfaces
3913 *
3914 * \param surface The surface to query.
3915 * \return The bounding box relative to the surface origin.
3916 *
3917 */
3918WL_EXPORT struct weston_geometry
3919weston_surface_get_bounding_box(struct weston_surface *surface)
3920{
3921 pixman_region32_t region;
3922 pixman_box32_t *box;
3923 struct weston_subsurface *subsurface;
3924
3925 pixman_region32_init_rect(&region,
3926 0, 0,
3927 surface->width, surface->height);
3928
3929 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link)
3930 pixman_region32_union_rect(&region, &region,
3931 subsurface->position.x,
3932 subsurface->position.y,
3933 subsurface->surface->width,
3934 subsurface->surface->height);
3935
3936 box = pixman_region32_extents(&region);
3937 struct weston_geometry geometry = {
3938 .x = box->x1,
3939 .y = box->y1,
3940 .width = box->x2 - box->x1,
3941 .height = box->y2 - box->y1,
3942 };
3943
3944 pixman_region32_fini(&region);
3945
3946 return geometry;
3947}
3948
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003949/** Copy surface contents to system memory.
3950 *
3951 * \param surface The surface to copy from.
3952 * \param target Pointer to the target memory buffer.
3953 * \param size Size of the target buffer in bytes.
3954 * \param src_x X location on contents to copy from.
3955 * \param src_y Y location on contents to copy from.
3956 * \param width Width in pixels of the area to copy.
3957 * \param height Height in pixels of the area to copy.
3958 * \return 0 for success, -1 for failure.
3959 *
3960 * Surface contents are maintained by the renderer. They can be in a
3961 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
3962 * else.
3963 *
3964 * Surface contents are copied into memory pointed to by target,
3965 * which has size bytes of space available. The target memory
3966 * may be larger than needed, but being smaller returns an error.
3967 * The extra bytes in target may or may not be written; their content is
3968 * unspecified. Size must be large enough to hold the image.
3969 *
3970 * The image in the target memory will be arranged in rows from
3971 * top to bottom, and pixels on a row from left to right. The pixel
3972 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
3973 * width * 4.
3974 *
3975 * Parameters src_x and src_y define the upper-left corner in buffer
3976 * coordinates (pixels) to copy from. Parameters width and height
3977 * define the size of the area to copy in pixels.
3978 *
3979 * The rectangle defined by src_x, src_y, width, height must fit in
3980 * the surface contents. Otherwise an error is returned.
3981 *
Changwoo Chof97d2502017-08-05 00:30:47 +09003982 * Use weston_surface_get_content_size to determine the content size; the
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003983 * needed target buffer size and rectangle limits.
3984 *
3985 * CURRENT IMPLEMENTATION RESTRICTIONS:
3986 * - the machine must be little-endian due to Pixman formats.
3987 *
3988 * NOTE: Pixman formats are premultiplied.
3989 */
3990WL_EXPORT int
3991weston_surface_copy_content(struct weston_surface *surface,
3992 void *target, size_t size,
3993 int src_x, int src_y,
3994 int width, int height)
3995{
3996 struct weston_renderer *rer = surface->compositor->renderer;
3997 int cw, ch;
3998 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
3999
4000 if (!rer->surface_copy_content)
4001 return -1;
4002
4003 weston_surface_get_content_size(surface, &cw, &ch);
4004
4005 if (src_x < 0 || src_y < 0)
4006 return -1;
4007
4008 if (width <= 0 || height <= 0)
4009 return -1;
4010
4011 if (src_x + width > cw || src_y + height > ch)
4012 return -1;
4013
4014 if (width * bytespp * height > size)
4015 return -1;
4016
4017 return rer->surface_copy_content(surface, target, size,
4018 src_x, src_y, width, height);
4019}
4020
Pekka Paalanene67858b2013-04-25 13:57:42 +03004021static void
4022subsurface_set_position(struct wl_client *client,
4023 struct wl_resource *resource, int32_t x, int32_t y)
4024{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004025 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004026
4027 if (!sub)
4028 return;
4029
4030 sub->position.x = x;
4031 sub->position.y = y;
4032 sub->position.set = 1;
4033}
4034
4035static struct weston_subsurface *
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004036subsurface_find_sibling(struct weston_subsurface *sub,
4037 struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004038{
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004039 struct weston_surface *parent = sub->parent;
4040 struct weston_subsurface *sibling;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004041
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004042 wl_list_for_each(sibling, &parent->subsurface_list, parent_link) {
4043 if (sibling->surface == surface && sibling != sub)
4044 return sibling;
4045 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004046
4047 return NULL;
4048}
4049
4050static struct weston_subsurface *
4051subsurface_sibling_check(struct weston_subsurface *sub,
4052 struct weston_surface *surface,
4053 const char *request)
4054{
4055 struct weston_subsurface *sibling;
4056
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004057 sibling = subsurface_find_sibling(sub, surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004058 if (!sibling) {
4059 wl_resource_post_error(sub->resource,
4060 WL_SUBSURFACE_ERROR_BAD_SURFACE,
4061 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004062 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004063 return NULL;
4064 }
4065
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004066 assert(sibling->parent == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004067
4068 return sibling;
4069}
4070
4071static void
4072subsurface_place_above(struct wl_client *client,
4073 struct wl_resource *resource,
4074 struct wl_resource *sibling_resource)
4075{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004076 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004077 struct weston_surface *surface =
4078 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004079 struct weston_subsurface *sibling;
4080
4081 if (!sub)
4082 return;
4083
4084 sibling = subsurface_sibling_check(sub, surface, "place_above");
4085 if (!sibling)
4086 return;
4087
4088 wl_list_remove(&sub->parent_link_pending);
4089 wl_list_insert(sibling->parent_link_pending.prev,
4090 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004091
4092 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004093}
4094
4095static void
4096subsurface_place_below(struct wl_client *client,
4097 struct wl_resource *resource,
4098 struct wl_resource *sibling_resource)
4099{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004100 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004101 struct weston_surface *surface =
4102 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004103 struct weston_subsurface *sibling;
4104
4105 if (!sub)
4106 return;
4107
4108 sibling = subsurface_sibling_check(sub, surface, "place_below");
4109 if (!sibling)
4110 return;
4111
4112 wl_list_remove(&sub->parent_link_pending);
4113 wl_list_insert(&sibling->parent_link_pending,
4114 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004115
4116 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004117}
4118
4119static void
4120subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
4121{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004122 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004123
4124 if (sub)
4125 sub->synchronized = 1;
4126}
4127
4128static void
4129subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
4130{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004131 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004132
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004133 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03004134 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004135
4136 /* If sub became effectively desynchronized, flush. */
4137 if (!weston_subsurface_is_synchronized(sub))
4138 weston_subsurface_synchronized_commit(sub);
4139 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004140}
4141
4142static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03004143weston_subsurface_unlink_parent(struct weston_subsurface *sub)
4144{
4145 wl_list_remove(&sub->parent_link);
4146 wl_list_remove(&sub->parent_link_pending);
4147 wl_list_remove(&sub->parent_destroy_listener.link);
4148 sub->parent = NULL;
4149}
4150
4151static void
4152weston_subsurface_destroy(struct weston_subsurface *sub);
4153
4154static void
4155subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
4156{
4157 struct weston_subsurface *sub =
4158 container_of(listener, struct weston_subsurface,
4159 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004160 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004161
4162 /* The protocol object (wl_resource) is left inert. */
4163 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004164 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004165
4166 weston_subsurface_destroy(sub);
4167}
4168
4169static void
4170subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
4171{
4172 struct weston_subsurface *sub =
4173 container_of(listener, struct weston_subsurface,
4174 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004175 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004176 assert(sub->surface != sub->parent);
4177
4178 if (weston_surface_is_mapped(sub->surface))
4179 weston_surface_unmap(sub->surface);
4180
4181 weston_subsurface_unlink_parent(sub);
4182}
4183
4184static void
4185subsurface_resource_destroy(struct wl_resource *resource)
4186{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004187 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004188
4189 if (sub)
4190 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004191}
4192
4193static void
4194subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
4195{
4196 wl_resource_destroy(resource);
4197}
4198
4199static void
4200weston_subsurface_link_parent(struct weston_subsurface *sub,
4201 struct weston_surface *parent)
4202{
4203 sub->parent = parent;
4204 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004205 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004206 &sub->parent_destroy_listener);
4207
4208 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4209 wl_list_insert(&parent->subsurface_list_pending,
4210 &sub->parent_link_pending);
4211}
4212
4213static void
4214weston_subsurface_link_surface(struct weston_subsurface *sub,
4215 struct weston_surface *surface)
4216{
4217 sub->surface = surface;
4218 sub->surface_destroy_listener.notify =
4219 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004220 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004221 &sub->surface_destroy_listener);
4222}
4223
4224static void
4225weston_subsurface_destroy(struct weston_subsurface *sub)
4226{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004227 struct weston_view *view, *next;
4228
Pekka Paalanene67858b2013-04-25 13:57:42 +03004229 assert(sub->surface);
4230
4231 if (sub->resource) {
4232 assert(weston_surface_to_subsurface(sub->surface) == sub);
4233 assert(sub->parent_destroy_listener.notify ==
4234 subsurface_handle_parent_destroy);
4235
George Kiagiadakised04d382014-06-13 18:10:26 +02004236 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
4237 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004238 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02004239 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05004240
Pekka Paalanene67858b2013-04-25 13:57:42 +03004241 if (sub->parent)
4242 weston_subsurface_unlink_parent(sub);
4243
Jason Ekstrand7b982072014-05-20 14:33:03 -05004244 weston_surface_state_fini(&sub->cached);
4245 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004246
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004247 sub->surface->committed = NULL;
4248 sub->surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004249 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004250 } else {
4251 /* the dummy weston_subsurface for the parent itself */
4252 assert(sub->parent_destroy_listener.notify == NULL);
4253 wl_list_remove(&sub->parent_link);
4254 wl_list_remove(&sub->parent_link_pending);
4255 }
4256
4257 wl_list_remove(&sub->surface_destroy_listener.link);
4258 free(sub);
4259}
4260
4261static const struct wl_subsurface_interface subsurface_implementation = {
4262 subsurface_destroy,
4263 subsurface_set_position,
4264 subsurface_place_above,
4265 subsurface_place_below,
4266 subsurface_set_sync,
4267 subsurface_set_desync
4268};
4269
4270static struct weston_subsurface *
4271weston_subsurface_create(uint32_t id, struct weston_surface *surface,
4272 struct weston_surface *parent)
4273{
4274 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004275 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004276
Bryce Harringtonde16d892014-11-20 22:21:57 -08004277 sub = zalloc(sizeof *sub);
4278 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004279 return NULL;
4280
Jason Ekstranda7af7042013-10-12 22:38:11 -05004281 wl_list_init(&sub->unused_views);
4282
Jason Ekstranda85118c2013-06-27 20:17:02 -05004283 sub->resource =
4284 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004285 if (!sub->resource) {
4286 free(sub);
4287 return NULL;
4288 }
4289
Jason Ekstranda85118c2013-06-27 20:17:02 -05004290 wl_resource_set_implementation(sub->resource,
4291 &subsurface_implementation,
4292 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004293 weston_subsurface_link_surface(sub, surface);
4294 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05004295 weston_surface_state_init(&sub->cached);
4296 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004297 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004298
4299 return sub;
4300}
4301
4302/* Create a dummy subsurface for having the parent itself in its
4303 * sub-surface lists. Makes stacking order manipulation easy.
4304 */
4305static struct weston_subsurface *
4306weston_subsurface_create_for_parent(struct weston_surface *parent)
4307{
4308 struct weston_subsurface *sub;
4309
Bryce Harringtonde16d892014-11-20 22:21:57 -08004310 sub = zalloc(sizeof *sub);
4311 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004312 return NULL;
4313
4314 weston_subsurface_link_surface(sub, parent);
4315 sub->parent = parent;
4316 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4317 wl_list_insert(&parent->subsurface_list_pending,
4318 &sub->parent_link_pending);
4319
4320 return sub;
4321}
4322
4323static void
4324subcompositor_get_subsurface(struct wl_client *client,
4325 struct wl_resource *resource,
4326 uint32_t id,
4327 struct wl_resource *surface_resource,
4328 struct wl_resource *parent_resource)
4329{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004330 struct weston_surface *surface =
4331 wl_resource_get_user_data(surface_resource);
4332 struct weston_surface *parent =
4333 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004334 struct weston_subsurface *sub;
4335 static const char where[] = "get_subsurface: wl_subsurface@";
4336
4337 if (surface == parent) {
4338 wl_resource_post_error(resource,
4339 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4340 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004341 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004342 return;
4343 }
4344
4345 if (weston_surface_to_subsurface(surface)) {
4346 wl_resource_post_error(resource,
4347 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4348 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004349 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004350 return;
4351 }
4352
Pekka Paalanen50b67472014-10-01 15:02:41 +03004353 if (weston_surface_set_role(surface, "wl_subsurface", resource,
4354 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004355 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004356
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004357 if (weston_surface_get_main_surface(parent) == surface) {
4358 wl_resource_post_error(resource,
4359 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4360 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004361 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004362 return;
4363 }
4364
Pekka Paalanene67858b2013-04-25 13:57:42 +03004365 /* make sure the parent is in its own list */
4366 if (wl_list_empty(&parent->subsurface_list)) {
4367 if (!weston_subsurface_create_for_parent(parent)) {
4368 wl_resource_post_no_memory(resource);
4369 return;
4370 }
4371 }
4372
4373 sub = weston_subsurface_create(id, surface, parent);
4374 if (!sub) {
4375 wl_resource_post_no_memory(resource);
4376 return;
4377 }
4378
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004379 surface->committed = subsurface_committed;
4380 surface->committed_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004381 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004382}
4383
4384static void
4385subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
4386{
4387 wl_resource_destroy(resource);
4388}
4389
4390static const struct wl_subcompositor_interface subcompositor_interface = {
4391 subcompositor_destroy,
4392 subcompositor_get_subsurface
4393};
4394
4395static void
4396bind_subcompositor(struct wl_client *client,
4397 void *data, uint32_t version, uint32_t id)
4398{
4399 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004400 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004401
Jason Ekstranda85118c2013-06-27 20:17:02 -05004402 resource =
4403 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004404 if (resource == NULL) {
4405 wl_client_post_no_memory(client);
4406 return;
4407 }
4408 wl_resource_set_implementation(resource, &subcompositor_interface,
4409 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004410}
4411
Bryce Harrington0795ece2016-08-30 12:04:26 -07004412/** Set a DPMS mode on all of the compositor's outputs
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004413 *
4414 * \param compositor The compositor instance
4415 * \param state The DPMS state the outputs will be set to
4416 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004417static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004418weston_compositor_dpms(struct weston_compositor *compositor,
4419 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004420{
4421 struct weston_output *output;
4422
Bryce Harrington08976ac2016-08-30 12:05:16 -07004423 wl_list_for_each(output, &compositor->output_list, link)
4424 if (output->set_dpms)
4425 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004426}
4427
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004428/** Restores the compositor to active status
4429 *
4430 * \param compositor The compositor instance
4431 *
4432 * If the compositor was in a sleeping mode, all outputs are powered
4433 * back on via DPMS. Otherwise if the compositor was inactive
4434 * (idle/locked, offscreen, or sleeping) then the compositor's wake
4435 * signal will fire.
4436 *
4437 * Restarts the idle timer.
4438 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004439WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004440weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004441{
Neil Roberts8b62e202013-09-30 13:14:47 +01004442 uint32_t old_state = compositor->state;
4443
4444 /* The state needs to be changed before emitting the wake
4445 * signal because that may try to schedule a repaint which
4446 * will not work if the compositor is still sleeping */
4447 compositor->state = WESTON_COMPOSITOR_ACTIVE;
4448
4449 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004450 case WESTON_COMPOSITOR_SLEEPING:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004451 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004452 case WESTON_COMPOSITOR_OFFSCREEN:
Daniel Stone893b9362016-11-08 15:47:09 +00004453 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004454 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004455 /* fall through */
4456 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004457 wl_event_source_timer_update(compositor->idle_source,
4458 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004459 }
4460}
4461
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004462/** Turns off rendering and frame events for the compositor.
4463 *
4464 * \param compositor The compositor instance
4465 *
4466 * This is used for example to prevent further rendering while the
4467 * compositor is shutting down.
4468 *
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004469 * Stops the idle timer.
4470 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004471WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004472weston_compositor_offscreen(struct weston_compositor *compositor)
4473{
4474 switch (compositor->state) {
4475 case WESTON_COMPOSITOR_OFFSCREEN:
4476 return;
4477 case WESTON_COMPOSITOR_SLEEPING:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004478 default:
4479 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4480 wl_event_source_timer_update(compositor->idle_source, 0);
4481 }
4482}
4483
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004484/** Powers down all attached output devices
4485 *
4486 * \param compositor The compositor instance
4487 *
4488 * Causes rendering to the outputs to cease, and no frame events to be
4489 * sent. Only powers down the outputs if the compositor is not already
4490 * in sleep mode.
4491 *
4492 * Stops the idle timer.
4493 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004494WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004495weston_compositor_sleep(struct weston_compositor *compositor)
4496{
4497 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
4498 return;
4499
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004500 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004501 compositor->state = WESTON_COMPOSITOR_SLEEPING;
4502 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
4503}
4504
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004505/** Sets compositor to idle mode
4506 *
4507 * \param data The compositor instance
4508 *
4509 * This is called when the idle timer fires. Once the compositor is in
4510 * idle mode it requires a wake action (e.g. via
4511 * weston_compositor_wake()) to restore it. The compositor's
4512 * idle_signal will be triggered when the idle event occurs.
4513 *
4514 * Idleness can be inhibited by setting the compositor's idle_inhibit
4515 * property.
4516 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004517static int
4518idle_handler(void *data)
4519{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004520 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004521
4522 if (compositor->idle_inhibit)
4523 return 1;
4524
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004525 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004526 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004527
4528 return 1;
4529}
4530
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004531WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08004532weston_plane_init(struct weston_plane *plane,
4533 struct weston_compositor *ec,
4534 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004535{
4536 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004537 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004538 plane->x = x;
4539 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08004540 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004541
4542 /* Init the link so that the call to wl_list_remove() when releasing
4543 * the plane without ever stacking doesn't lead to a crash */
4544 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004545}
4546
4547WL_EXPORT void
4548weston_plane_release(struct weston_plane *plane)
4549{
Xiong Zhang97116532013-10-23 13:58:31 +08004550 struct weston_view *view;
4551
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004552 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004553 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004554
Xiong Zhang97116532013-10-23 13:58:31 +08004555 wl_list_for_each(view, &plane->compositor->view_list, link) {
4556 if (view->plane == plane)
4557 view->plane = NULL;
4558 }
4559
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004560 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004561}
4562
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004563WL_EXPORT void
4564weston_compositor_stack_plane(struct weston_compositor *ec,
4565 struct weston_plane *plane,
4566 struct weston_plane *above)
4567{
4568 if (above)
4569 wl_list_insert(above->link.prev, &plane->link);
4570 else
4571 wl_list_insert(&ec->plane_list, &plane->link);
4572}
4573
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004574static void
4575output_release(struct wl_client *client, struct wl_resource *resource)
4576{
4577 wl_resource_destroy(resource);
4578}
4579
4580static const struct wl_output_interface output_interface = {
4581 output_release,
4582};
4583
4584
Casey Dahlin9074db52012-04-19 22:50:09 -04004585static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004586{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004587 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004588}
4589
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004590static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004591bind_output(struct wl_client *client,
4592 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05004593{
Pekka Paalanen05347622017-03-27 12:24:34 +03004594 struct weston_head *head = data;
4595 struct weston_output *output = head->output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004596 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004597 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004598
Jason Ekstranda85118c2013-06-27 20:17:02 -05004599 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004600 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004601 if (resource == NULL) {
4602 wl_client_post_no_memory(client);
4603 return;
4604 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004605
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03004606 wl_list_insert(&head->resource_list, wl_resource_get_link(resource));
Pekka Paalanen055c1132017-03-27 16:31:25 +03004607 wl_resource_set_implementation(resource, &output_interface, head,
Pekka Paalanen05347622017-03-27 12:24:34 +03004608 unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04004609
Pekka Paalanen05347622017-03-27 12:24:34 +03004610 assert(output);
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004611 wl_output_send_geometry(resource,
4612 output->x,
4613 output->y,
Pekka Paalanen01f60212017-03-24 15:39:24 +02004614 head->mm_width,
4615 head->mm_height,
4616 head->subpixel,
4617 head->make, head->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04004618 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004619 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004620 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02004621 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004622
4623 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004624 wl_output_send_mode(resource,
4625 mode->flags,
4626 mode->width,
4627 mode->height,
4628 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004629 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02004630
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004631 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004632 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004633}
4634
Pekka Paalanendcac3512017-12-08 14:13:34 +02004635static void
4636weston_head_add_global(struct weston_head *head)
4637{
4638 head->global = wl_global_create(head->compositor->wl_display,
4639 &wl_output_interface, 3,
4640 head, bind_output);
4641}
4642
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02004643/** Remove the global wl_output protocol object
4644 *
4645 * \param head The head whose global to remove.
4646 *
4647 * Also orphans the wl_resources for this head (wl_output).
4648 */
4649static void
4650weston_head_remove_global(struct weston_head *head)
4651{
4652 struct wl_resource *resource, *tmp;
4653
4654 if (head->global)
4655 wl_global_destroy(head->global);
4656 head->global = NULL;
4657
4658 wl_resource_for_each_safe(resource, tmp, &head->resource_list) {
4659 unbind_resource(resource);
4660 wl_resource_set_destructor(resource, NULL);
4661 wl_resource_set_user_data(resource, NULL);
4662 }
4663}
4664
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004665/** Get the backing object of wl_output
4666 *
4667 * \param resource A wl_output protocol object.
4668 * \return The backing object (user data) of a wl_resource representing a
4669 * wl_output protocol object.
4670 */
Pekka Paalanen055c1132017-03-27 16:31:25 +03004671WL_EXPORT struct weston_head *
4672weston_head_from_resource(struct wl_resource *resource)
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004673{
4674 assert(wl_resource_instance_of(resource, &wl_output_interface,
4675 &output_interface));
4676
4677 return wl_resource_get_user_data(resource);
4678}
4679
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004680/** Initialize a pre-allocated weston_head
4681 *
4682 * \param head The head to initialize.
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004683 * \param name The head name, e.g. the connector name or equivalent.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004684 *
4685 * The head will be safe to attach, detach and release.
4686 *
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004687 * The name is used in logs, and can be used by compositors as a configuration
4688 * identifier.
4689 *
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004690 * \memberof weston_head
4691 * \internal
4692 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004693WL_EXPORT void
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004694weston_head_init(struct weston_head *head, const char *name)
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004695{
4696 /* Add some (in)sane defaults which can be used
4697 * for checking if an output was properly configured
4698 */
4699 memset(head, 0, sizeof *head);
4700
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004701 wl_list_init(&head->compositor_link);
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03004702 wl_signal_init(&head->destroy_signal);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004703 wl_list_init(&head->output_link);
4704 wl_list_init(&head->resource_list);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004705 head->name = strdup(name);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004706}
4707
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004708/** Send output heads changed signal
4709 *
4710 * \param output The output that changed.
4711 *
4712 * Notify that the enabled output gained and/or lost heads, or that the
4713 * associated heads may have changed their connection status. This does not
4714 * include cases where the output becomes enabled or disabled. The registered
4715 * callbacks are called after the change has successfully happened.
4716 *
4717 * If connection status change causes the compositor to attach or detach a head
4718 * to an enabled output, the registered callbacks may be called multiple times.
4719 */
4720static void
4721weston_output_emit_heads_changed(struct weston_output *output)
4722{
4723 wl_signal_emit(&output->compositor->output_heads_changed_signal,
4724 output);
4725}
4726
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004727/** Idle task for emitting heads_changed_signal */
4728static void
4729weston_compositor_call_heads_changed(void *data)
4730{
4731 struct weston_compositor *compositor = data;
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004732 struct weston_head *head;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004733
4734 compositor->heads_changed_source = NULL;
4735
4736 wl_signal_emit(&compositor->heads_changed_signal, compositor);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004737
4738 wl_list_for_each(head, &compositor->head_list, compositor_link) {
4739 if (head->output && head->output->enabled)
4740 weston_output_emit_heads_changed(head->output);
4741 }
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004742}
4743
4744/** Schedule a call on idle to heads_changed callback
4745 *
4746 * \param compositor The Compositor.
4747 *
4748 * \memberof weston_compositor
4749 * \internal
4750 */
4751static void
4752weston_compositor_schedule_heads_changed(struct weston_compositor *compositor)
4753{
4754 struct wl_event_loop *loop;
4755
4756 if (compositor->heads_changed_source)
4757 return;
4758
4759 loop = wl_display_get_event_loop(compositor->wl_display);
4760 compositor->heads_changed_source = wl_event_loop_add_idle(loop,
4761 weston_compositor_call_heads_changed, compositor);
4762}
4763
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004764/** Register a new head
4765 *
4766 * \param compositor The compositor.
4767 * \param head The head to register, must not be already registered.
4768 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004769 * This signals the core that a new head has become available, leading to
4770 * heads_changed hook being called later.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004771 *
4772 * \memberof weston_compositor
4773 * \internal
4774 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004775WL_EXPORT void
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004776weston_compositor_add_head(struct weston_compositor *compositor,
4777 struct weston_head *head)
4778{
4779 assert(wl_list_empty(&head->compositor_link));
4780 assert(head->name);
4781
4782 wl_list_insert(compositor->head_list.prev, &head->compositor_link);
4783 head->compositor = compositor;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004784 weston_compositor_schedule_heads_changed(compositor);
4785}
4786
4787/** Adds a listener to be called when heads change
4788 *
4789 * \param compositor The compositor.
4790 * \param listener The listener to add.
4791 *
4792 * The listener notify function argument is the \var compositor.
4793 *
4794 * The listener function will be called after heads are added or their
4795 * connection status has changed. Several changes may be accumulated into a
4796 * single call. The user is expected to iterate over the existing heads and
4797 * check their statuses to find out what changed.
4798 *
4799 * \sa weston_compositor_iterate_heads, weston_head_is_connected,
4800 * weston_head_is_enabled
4801 * \memberof weston_compositor
4802 */
4803WL_EXPORT void
4804weston_compositor_add_heads_changed_listener(struct weston_compositor *compositor,
4805 struct wl_listener *listener)
4806{
4807 wl_signal_add(&compositor->heads_changed_signal, listener);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004808}
4809
4810/** Iterate over available heads
4811 *
4812 * \param compositor The compositor.
4813 * \param item The iterator, or NULL for start.
4814 * \return The next available head in the list.
4815 *
4816 * Returns all available heads, regardless of being connected or enabled.
4817 *
4818 * You can iterate over all heads as follows:
4819 * \code
4820 * struct weston_head *head = NULL;
4821 *
4822 * while ((head = weston_compositor_iterate_heads(compositor, head))) {
4823 * ...
4824 * }
4825 * \endcode
4826 *
4827 * If you cause \c iter to be removed from the list, you cannot use it to
4828 * continue iterating. Removing any other item is safe.
4829 *
4830 * \memberof weston_compositor
4831 */
4832WL_EXPORT struct weston_head *
4833weston_compositor_iterate_heads(struct weston_compositor *compositor,
4834 struct weston_head *iter)
4835{
4836 struct wl_list *list = &compositor->head_list;
4837 struct wl_list *node;
4838
4839 assert(compositor);
4840 assert(!iter || iter->compositor == compositor);
4841
4842 if (iter)
4843 node = iter->compositor_link.next;
4844 else
4845 node = list->next;
4846
4847 assert(node);
4848 assert(!iter || node != &iter->compositor_link);
4849
4850 if (node == list)
4851 return NULL;
4852
4853 return container_of(node, struct weston_head, compositor_link);
4854}
4855
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004856/** Iterate over attached heads
4857 *
4858 * \param output The output whose heads to iterate.
4859 * \param item The iterator, or NULL for start.
4860 * \return The next attached head in the list.
4861 *
4862 * Returns all heads currently attached to the output.
4863 *
4864 * You can iterate over all heads as follows:
4865 * \code
4866 * struct weston_head *head = NULL;
4867 *
4868 * while ((head = weston_output_iterate_heads(output, head))) {
4869 * ...
4870 * }
4871 * \endcode
4872 *
4873 * If you cause \c iter to be removed from the list, you cannot use it to
4874 * continue iterating. Removing any other item is safe.
4875 *
4876 * \memberof weston_compositor
4877 */
4878WL_EXPORT struct weston_head *
4879weston_output_iterate_heads(struct weston_output *output,
4880 struct weston_head *iter)
4881{
4882 struct wl_list *list = &output->head_list;
4883 struct wl_list *node;
4884
4885 assert(output);
4886 assert(!iter || iter->output == output);
4887
4888 if (iter)
4889 node = iter->output_link.next;
4890 else
4891 node = list->next;
4892
4893 assert(node);
4894 assert(!iter || node != &iter->output_link);
4895
4896 if (node == list)
4897 return NULL;
4898
4899 return container_of(node, struct weston_head, output_link);
4900}
4901
Pekka Paalanendcac3512017-12-08 14:13:34 +02004902/** Attach a head to an output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004903 *
4904 * \param output The output to attach to.
4905 * \param head The head that is not yet attached.
4906 * \return 0 on success, -1 on failure.
4907 *
4908 * Attaches the given head to the output. All heads of an output are clones
4909 * and share the resolution and timings.
4910 *
4911 * Cloning heads this way uses less resources than creating an output for
4912 * each head, but is not always possible due to environment, driver and hardware
4913 * limitations.
4914 *
4915 * On failure, the head remains unattached. Success of this function does not
4916 * guarantee the output configuration is actually valid. The final checks are
Pekka Paalanendcac3512017-12-08 14:13:34 +02004917 * made on weston_output_enable() unless the output was already enabled.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004918 *
4919 * \memberof weston_output
4920 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004921WL_EXPORT int
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004922weston_output_attach_head(struct weston_output *output,
4923 struct weston_head *head)
4924{
Pekka Paalanendcac3512017-12-08 14:13:34 +02004925 char *head_names;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004926
4927 if (!wl_list_empty(&head->output_link))
4928 return -1;
4929
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004930 if (output->attach_head) {
4931 if (output->attach_head(output, head) < 0)
4932 return -1;
4933 } else if (!wl_list_empty(&output->head_list)) {
4934 /* No support for clones in the legacy path. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004935 return -1;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004936 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004937
4938 head->output = output;
4939 wl_list_insert(output->head_list.prev, &head->output_link);
4940
Pekka Paalanendcac3512017-12-08 14:13:34 +02004941 if (output->enabled) {
4942 weston_head_add_global(head);
4943
4944 head_names = weston_output_create_heads_string(output);
4945 weston_log("Output '%s' updated to have head(s) %s\n",
4946 output->name, head_names);
4947 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004948
4949 weston_output_emit_heads_changed(output);
Pekka Paalanendcac3512017-12-08 14:13:34 +02004950 }
4951
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004952 return 0;
4953}
4954
4955/** Detach a head from its output
4956 *
4957 * \param head The head to detach.
4958 *
4959 * It is safe to detach a non-attached head.
4960 *
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004961 * If the head is attached to an enabled output and the output will be left
4962 * with no heads, the output will be disabled.
4963 *
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004964 * \memberof weston_head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004965 * \sa weston_output_disable
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004966 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004967WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004968weston_head_detach(struct weston_head *head)
4969{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004970 struct weston_output *output = head->output;
Pekka Paalanena0106992017-12-08 16:11:17 +02004971 char *head_names;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004972
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004973 wl_list_remove(&head->output_link);
4974 wl_list_init(&head->output_link);
4975 head->output = NULL;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004976
4977 if (!output)
4978 return;
4979
4980 if (output->detach_head)
4981 output->detach_head(output, head);
4982
4983 if (output->enabled) {
4984 weston_head_remove_global(head);
4985
Pekka Paalanena0106992017-12-08 16:11:17 +02004986 if (wl_list_empty(&output->head_list)) {
4987 weston_log("Output '%s' no heads left, disabling.\n",
4988 output->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004989 weston_output_disable(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02004990 } else {
4991 head_names = weston_output_create_heads_string(output);
4992 weston_log("Output '%s' updated to have head(s) %s\n",
4993 output->name, head_names);
4994 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004995
4996 weston_output_emit_heads_changed(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02004997 }
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004998 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004999}
5000
5001/** Destroy a head
5002 *
5003 * \param head The head to be released.
5004 *
5005 * Destroys the head. The caller is responsible for freeing the memory pointed
5006 * to by \c head.
5007 *
5008 * \memberof weston_head
5009 * \internal
5010 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005011WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005012weston_head_release(struct weston_head *head)
5013{
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005014 wl_signal_emit(&head->destroy_signal, head);
5015
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005016 weston_head_detach(head);
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005017
5018 free(head->make);
5019 free(head->model);
5020 free(head->serial_number);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005021 free(head->name);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005022
5023 wl_list_remove(&head->compositor_link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005024}
5025
Pekka Paalanene19970f2017-08-28 14:11:02 +03005026static void
5027weston_head_set_device_changed(struct weston_head *head)
5028{
5029 head->device_changed = true;
5030
5031 if (head->compositor)
5032 weston_compositor_schedule_heads_changed(head->compositor);
5033}
5034
5035/** String equal comparison with NULLs being equal */
5036static bool
5037str_null_eq(const char *a, const char *b)
5038{
5039 if (!a && !b)
5040 return true;
5041
5042 if (!!a != !!b)
5043 return false;
5044
5045 return strcmp(a, b) == 0;
5046}
5047
Pekka Paalanen01f60212017-03-24 15:39:24 +02005048/** Store monitor make, model and serial number
5049 *
5050 * \param head The head to modify.
5051 * \param make The monitor make. If EDID is available, the PNP ID. Otherwise
5052 * any string, or NULL for none.
5053 * \param model The monitor model or name, or a made-up string, or NULL for
5054 * none.
5055 * \param serialno The monitor serial number, a made-up string, or NULL for
5056 * none.
5057 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005058 * This may set the device_changed flag.
5059 *
Pekka Paalanen01f60212017-03-24 15:39:24 +02005060 * \memberof weston_head
5061 * \internal
5062 */
5063WL_EXPORT void
5064weston_head_set_monitor_strings(struct weston_head *head,
5065 const char *make,
5066 const char *model,
5067 const char *serialno)
5068{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005069 if (str_null_eq(head->make, make) &&
5070 str_null_eq(head->model, model) &&
5071 str_null_eq(head->serial_number, serialno))
5072 return;
5073
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005074 free(head->make);
5075 free(head->model);
5076 free(head->serial_number);
5077
5078 head->make = make ? strdup(make) : NULL;
5079 head->model = model ? strdup(model) : NULL;
5080 head->serial_number = serialno ? strdup(serialno) : NULL;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005081
5082 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005083}
5084
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005085/** Store display non-desktop status
5086 *
5087 * \param head The head to modify.
5088 * \param non_desktop Whether the head connects to a non-desktop display.
5089 *
5090 * \memberof weston_head
5091 * \internal
5092 */
5093WL_EXPORT void
5094weston_head_set_non_desktop(struct weston_head *head, bool non_desktop)
5095{
5096 if (head->non_desktop == non_desktop)
5097 return;
5098
5099 head->non_desktop = non_desktop;
5100
5101 weston_head_set_device_changed(head);
5102}
5103
Pekka Paalanen01f60212017-03-24 15:39:24 +02005104/** Store physical image size
5105 *
5106 * \param head The head to modify.
5107 * \param mm_width Image area width in millimeters.
5108 * \param mm_height Image area height in millimeters.
5109 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005110 * This may set the device_changed flag.
5111 *
Pekka Paalanen01f60212017-03-24 15:39:24 +02005112 * \memberof weston_head
5113 * \internal
5114 */
5115WL_EXPORT void
5116weston_head_set_physical_size(struct weston_head *head,
5117 int32_t mm_width, int32_t mm_height)
5118{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005119 if (head->mm_width == mm_width &&
5120 head->mm_height == mm_height)
5121 return;
5122
Pekka Paalanen01f60212017-03-24 15:39:24 +02005123 head->mm_width = mm_width;
5124 head->mm_height = mm_height;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005125
5126 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005127}
5128
5129/** Store monitor sub-pixel layout
5130 *
5131 * \param head The head to modify.
5132 * \param sp Sub-pixel layout. The possible values are:
5133 * - WL_OUTPUT_SUBPIXEL_UNKNOWN,
5134 * - WL_OUTPUT_SUBPIXEL_NONE,
5135 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB,
5136 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR,
5137 * - WL_OUTPUT_SUBPIXEL_VERTICAL_RGB,
5138 * - WL_OUTPUT_SUBPIXEL_VERTICAL_BGR
5139 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005140 * This may set the device_changed flag.
5141 *
Pekka Paalanen01f60212017-03-24 15:39:24 +02005142 * \memberof weston_head
5143 * \internal
5144 */
5145WL_EXPORT void
5146weston_head_set_subpixel(struct weston_head *head,
5147 enum wl_output_subpixel sp)
5148{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005149 if (head->subpixel == sp)
5150 return;
5151
Pekka Paalanen01f60212017-03-24 15:39:24 +02005152 head->subpixel = sp;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005153
5154 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005155}
5156
5157/** Mark the monitor as internal
5158 *
5159 * This is used for embedded screens, like laptop panels.
5160 *
5161 * \param head The head to mark as internal.
5162 *
5163 * By default a head is external. The type is often inferred from the physical
5164 * connector type.
5165 *
5166 * \memberof weston_head
5167 * \internal
5168 */
5169WL_EXPORT void
5170weston_head_set_internal(struct weston_head *head)
5171{
5172 head->connection_internal = true;
5173}
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005174
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005175/** Store connector status
5176 *
5177 * \param head The head to modify.
5178 * \param connected Whether the head is connected.
5179 *
5180 * Connectors are created as disconnected. This function can be used to
5181 * set the connector status.
5182 *
5183 * The status should be set to true when a physical connector is connected to
5184 * a video sink device like a monitor and to false when the connector is
5185 * disconnected. For nested backends, the connection status should reflect the
5186 * connection to the parent display server.
5187 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005188 * When the connection status changes, it schedules a call to the heads_changed
Pekka Paalanene19970f2017-08-28 14:11:02 +03005189 * hook and sets the device_changed flag.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005190 *
5191 * \sa weston_compositor_set_heads_changed_cb
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005192 * \memberof weston_head
5193 * \internal
5194 */
5195WL_EXPORT void
5196weston_head_set_connection_status(struct weston_head *head, bool connected)
5197{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005198 if (head->connected == connected)
5199 return;
5200
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005201 head->connected = connected;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005202
Pekka Paalanene19970f2017-08-28 14:11:02 +03005203 weston_head_set_device_changed(head);
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005204}
5205
5206/** Is the head currently connected?
5207 *
5208 * \param head The head to query.
5209 * \return Connection status.
5210 *
5211 * Returns true if the head is physically connected to a monitor, or in
5212 * case of a nested backend returns true when there is a connection to the
5213 * parent display server.
5214 *
5215 * This is independent from the head being enabled.
5216 *
5217 * \sa weston_head_is_enabled
5218 * \memberof weston_head
5219 */
5220WL_EXPORT bool
5221weston_head_is_connected(struct weston_head *head)
5222{
5223 return head->connected;
5224}
5225
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005226/** Is the head currently enabled?
5227 *
5228 * \param head The head to query.
5229 * \return Video status.
5230 *
5231 * Returns true if the head is currently transmitting a video stream.
5232 *
5233 * This is independent of the head being connected.
5234 *
5235 * \sa weston_head_is_connected
5236 * \memberof weston_head
5237 */
5238WL_EXPORT bool
5239weston_head_is_enabled(struct weston_head *head)
5240{
5241 if (!head->output)
5242 return false;
5243
5244 return head->output->enabled;
5245}
5246
Pekka Paalanene19970f2017-08-28 14:11:02 +03005247/** Has the device information changed?
5248 *
5249 * \param head The head to query.
5250 * \return True if the device information has changed since last reset.
5251 *
5252 * The information about the connected display device, e.g. a monitor, may
5253 * change without being disconnected in between. Changing information
5254 * causes a call to the heads_changed hook.
5255 *
5256 * The information includes make, model, serial number, physical size,
5257 * and sub-pixel type. The connection status is also included.
5258 *
5259 * \sa weston_head_reset_device_changed, weston_compositor_set_heads_changed_cb
5260 * \memberof weston_head
5261 */
5262WL_EXPORT bool
5263weston_head_is_device_changed(struct weston_head *head)
5264{
5265 return head->device_changed;
5266}
5267
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005268/** Does the head represent a non-desktop display?
5269 *
5270 * \param head The head to query.
5271 * \return True if the device is a non-desktop display.
5272 *
5273 * Non-desktop heads are not attached to outputs by default.
5274 * This stops weston from extending the desktop onto head mounted displays.
5275 *
5276 * \memberof weston_head
5277 */
5278WL_EXPORT bool
5279weston_head_is_non_desktop(struct weston_head *head)
5280{
5281 return head->non_desktop;
5282}
5283
Pekka Paalanene19970f2017-08-28 14:11:02 +03005284/** Acknowledge device information change
5285 *
5286 * \param head The head to acknowledge.
5287 *
5288 * Clears the device changed flag on this head. When a compositor has processed
5289 * device information, it should call this to be able to notice further
5290 * changes.
5291 *
5292 * \sa weston_head_is_device_changed
5293 * \memberof weston_head
5294 */
5295WL_EXPORT void
5296weston_head_reset_device_changed(struct weston_head *head)
5297{
5298 head->device_changed = false;
5299}
5300
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005301/** Get the name of a head
5302 *
5303 * \param head The head to query.
5304 * \return The head's name, not NULL.
5305 *
5306 * The name depends on the backend. The DRM backend uses connector names,
5307 * other backends may use hardcoded names or user-given names.
5308 */
5309WL_EXPORT const char *
5310weston_head_get_name(struct weston_head *head)
5311{
5312 return head->name;
5313}
5314
5315/** Get the output the head is attached to
5316 *
5317 * \param head The head to query.
5318 * \return The output the head is attached to, or NULL if detached.
5319 */
5320WL_EXPORT struct weston_output *
5321weston_head_get_output(struct weston_head *head)
5322{
5323 return head->output;
5324}
5325
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005326/** Add destroy callback for a head
5327 *
5328 * \param head The head to watch for.
5329 * \param listener The listener to add. The \c notify member must be set.
5330 *
5331 * Heads may get destroyed for various reasons by the backends. If a head is
5332 * attached to an output, the compositor should listen for head destruction
5333 * and reconfigure or destroy the output if necessary.
5334 *
5335 * The destroy callbacks will be called on weston_head destruction before any
5336 * automatic detaching from an associated weston_output and before any
5337 * weston_head information is lost.
5338 *
5339 * The \c data argument to the notify callback is the weston_head being
5340 * destroyed.
5341 */
5342WL_EXPORT void
5343weston_head_add_destroy_listener(struct weston_head *head,
5344 struct wl_listener *listener)
5345{
5346 wl_signal_add(&head->destroy_signal, listener);
5347}
5348
5349/** Look up destroy listener for a head
5350 *
5351 * \param head The head to query.
5352 * \param notify The notify function used used for the added destroy listener.
5353 * \return The listener, or NULL if not found.
5354 *
5355 * This looks up the previously added destroy listener struct based on the
5356 * notify function it has. The listener can be used to access user data
5357 * through \c container_of().
5358 *
5359 * \sa wl_signal_get()
5360 */
5361WL_EXPORT struct wl_listener *
5362weston_head_get_destroy_listener(struct weston_head *head,
5363 wl_notify_func_t notify)
5364{
5365 return wl_signal_get(&head->destroy_signal, notify);
5366}
5367
David Fort0de859e2016-05-27 23:22:57 +02005368/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005369static void
David Fort0de859e2016-05-27 23:22:57 +02005370weston_compositor_reflow_outputs(struct weston_compositor *compositor,
5371 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005372{
5373 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02005374 bool start_resizing = false;
5375
5376 if (!delta_width)
5377 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005378
5379 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02005380 if (output == resized_output) {
5381 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005382 continue;
5383 }
5384
David Fort0de859e2016-05-27 23:22:57 +02005385 if (start_resizing) {
5386 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005387 output->dirty = 1;
5388 }
5389 }
5390}
5391
Pekka Paalanend72bad22017-03-29 17:01:41 +03005392static void
Scott Moreauccbf29d2012-02-22 14:21:41 -07005393weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005394{
Scott Moreau850ca422012-05-21 15:21:25 -06005395 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05005396
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005397 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005398 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06005399
Scott Moreauccbf29d2012-02-22 14:21:41 -07005400 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06005401 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005402 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005403 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005404 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005405 weston_matrix_scale(&output->matrix, magnification,
5406 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005407 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005408
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005409 switch (output->transform) {
5410 case WL_OUTPUT_TRANSFORM_FLIPPED:
5411 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5412 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5413 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5414 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5415 weston_matrix_scale(&output->matrix, -1, 1, 1);
5416 break;
5417 }
5418
5419 switch (output->transform) {
5420 default:
5421 case WL_OUTPUT_TRANSFORM_NORMAL:
5422 case WL_OUTPUT_TRANSFORM_FLIPPED:
5423 break;
5424 case WL_OUTPUT_TRANSFORM_90:
5425 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5426 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
5427 weston_matrix_rotate_xy(&output->matrix, 0, 1);
5428 break;
5429 case WL_OUTPUT_TRANSFORM_180:
5430 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5431 weston_matrix_translate(&output->matrix,
5432 -output->width, -output->height, 0);
5433 weston_matrix_rotate_xy(&output->matrix, -1, 0);
5434 break;
5435 case WL_OUTPUT_TRANSFORM_270:
5436 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5437 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5438 weston_matrix_rotate_xy(&output->matrix, 0, -1);
5439 break;
5440 }
5441
5442 if (output->current_scale != 1)
5443 weston_matrix_scale(&output->matrix,
5444 output->current_scale,
5445 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01005446
Scott Moreauccbf29d2012-02-22 14:21:41 -07005447 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05005448
5449 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005450}
5451
Scott Moreau1bad5db2012-08-18 01:04:05 -06005452static void
Alexander Larsson0b135062013-05-28 16:23:36 +02005453weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06005454{
5455 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005456 output->native_scale = scale;
5457 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06005458
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005459 convert_size_by_transform_scale(&output->width, &output->height,
5460 output->current_mode->width,
5461 output->current_mode->height,
5462 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02005463}
5464
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005465static void
5466weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07005467{
5468 output->x = x;
5469 output->y = y;
5470
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005471 pixman_region32_fini(&output->previous_damage);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02005472 pixman_region32_init(&output->previous_damage);
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005473
5474 pixman_region32_fini(&output->region);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005475 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06005476 output->width,
5477 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005478}
5479
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005480WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005481weston_output_move(struct weston_output *output, int x, int y)
5482{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005483 struct weston_head *head;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005484 struct wl_resource *resource;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005485 int ver;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005486
5487 output->move_x = x - output->x;
5488 output->move_y = y - output->y;
5489
5490 if (output->move_x == 0 && output->move_y == 0)
5491 return;
5492
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005493 weston_output_init_geometry(output, x, y);
5494
5495 output->dirty = 1;
5496
5497 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005498 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005499
5500 /* Notify clients of the change for output position. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005501 wl_list_for_each(head, &output->head_list, output_link) {
5502 wl_resource_for_each(resource, &head->resource_list) {
5503 wl_output_send_geometry(resource,
5504 output->x,
5505 output->y,
5506 head->mm_width,
5507 head->mm_height,
5508 head->subpixel,
5509 head->make,
5510 head->model,
5511 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08005512
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005513 ver = wl_resource_get_version(resource);
5514 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
5515 wl_output_send_done(resource);
5516 }
Quanxian Wangb2c86362014-03-14 09:16:25 +08005517 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005518}
5519
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005520/** Signal that a pending output is taken into use.
5521 *
5522 * Removes the output from the pending list and adds it to the compositor's
5523 * list of enabled outputs. The output created signal is emitted.
Giulio Camuffob1147152015-05-06 21:41:57 +03005524 *
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005525 * The output gets an internal ID assigned, and the wl_output global is
5526 * created.
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005527 *
Giulio Camuffob1147152015-05-06 21:41:57 +03005528 * \param compositor The compositor instance.
5529 * \param output The output to be added.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005530 *
5531 * \internal
Giulio Camuffob1147152015-05-06 21:41:57 +03005532 */
Pekka Paalanenf9681b52017-03-29 16:58:48 +03005533static void
Giulio Camuffob1147152015-05-06 21:41:57 +03005534weston_compositor_add_output(struct weston_compositor *compositor,
5535 struct weston_output *output)
5536{
Armin Krezoviće5403842016-08-05 15:28:29 +02005537 struct weston_view *view, *next;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005538 struct weston_head *head;
Armin Krezoviće5403842016-08-05 15:28:29 +02005539
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005540 assert(!output->enabled);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005541
5542 /* Verify we haven't reached the limit of 32 available output IDs */
5543 assert(ffs(~compositor->output_id_pool) > 0);
5544
5545 /* Invert the output id pool and look for the lowest numbered
5546 * switch (the least significant bit). Take that bit's position
5547 * as our ID, and mark it used in the compositor's output_id_pool.
5548 */
5549 output->id = ffs(~compositor->output_id_pool) - 1;
5550 compositor->output_id_pool |= 1u << output->id;
5551
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005552 wl_list_remove(&output->link);
Giulio Camuffob1147152015-05-06 21:41:57 +03005553 wl_list_insert(compositor->output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005554 output->enabled = true;
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005555
Pekka Paalanendcac3512017-12-08 14:13:34 +02005556 wl_list_for_each(head, &output->head_list, output_link)
5557 weston_head_add_global(head);
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005558
Giulio Camuffob1147152015-05-06 21:41:57 +03005559 wl_signal_emit(&compositor->output_created_signal, output);
Armin Krezoviće5403842016-08-05 15:28:29 +02005560
5561 wl_list_for_each_safe(view, next, &compositor->view_list, link)
5562 weston_view_geometry_dirty(view);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005563}
5564
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005565/** Transform device coordinates into global coordinates
5566 *
5567 * \param device_x[in] X coordinate in device units.
5568 * \param device_y[in] Y coordinate in device units.
5569 * \param x[out] X coordinate in the global space.
5570 * \param y[out] Y coordinate in the global space.
5571 *
5572 * Transforms coordinates from the device coordinate space
5573 * (physical pixel units) to the global coordinate space (logical pixel units).
5574 * This takes into account output transform and scale.
5575 *
5576 * \memberof weston_output
5577 * \internal
5578 */
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005579WL_EXPORT void
5580weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005581 double device_x, double device_y,
5582 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005583{
Derek Foreman0f679412014-10-02 13:41:17 -05005584 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005585 device_x,
5586 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05005587 0.0,
5588 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005589
Derek Foreman67a18b92015-03-24 11:36:14 -05005590 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005591
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005592 *x = p.f[0] / p.f[3];
5593 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005594}
5595
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005596/** Removes output from compositor's list of enabled outputs
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005597 *
5598 * \param output The weston_output object that is being removed.
5599 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005600 * The following happens:
5601 *
5602 * - The output assignments of all views in the current scenegraph are
5603 * recomputed.
5604 *
5605 * - Presentation feedback is discarded.
5606 *
5607 * - Compositor is notified that outputs were changed and
5608 * applies the necessary changes to re-layout outputs.
5609 *
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005610 * - The output is put back in the pending outputs list.
5611 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005612 * - Signal is emitted to notify all users of the weston_output
5613 * object that the output is being destroyed.
5614 *
5615 * - wl_output protocol objects referencing this weston_output
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005616 * are made inert, and the wl_output global is removed.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005617 *
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005618 * - The output's internal ID is released.
5619 *
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005620 * \memberof weston_output
5621 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005622 */
5623static void
5624weston_compositor_remove_output(struct weston_output *output)
5625{
Pekka Paalanenbccda712017-03-29 16:16:04 +03005626 struct weston_compositor *compositor = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005627 struct weston_view *view;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005628 struct weston_head *head;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005629
5630 assert(output->destroying);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005631 assert(output->enabled);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005632
Pekka Paalanenbccda712017-03-29 16:16:04 +03005633 wl_list_for_each(view, &compositor->view_list, link) {
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005634 if (view->output_mask & (1u << output->id))
5635 weston_view_assign_output(view);
5636 }
5637
5638 weston_presentation_feedback_discard_list(&output->feedback_list);
5639
Pekka Paalanen9711fd92018-06-21 14:26:18 +03005640 weston_compositor_reflow_outputs(compositor, output, -output->width);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005641
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005642 wl_list_remove(&output->link);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005643 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005644 output->enabled = false;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005645
Pekka Paalanenbccda712017-03-29 16:16:04 +03005646 wl_signal_emit(&compositor->output_destroyed_signal, output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005647 wl_signal_emit(&output->destroy_signal, output);
5648
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02005649 wl_list_for_each(head, &output->head_list, output_link)
5650 weston_head_remove_global(head);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005651
5652 compositor->output_id_pool &= ~(1u << output->id);
5653 output->id = 0xffffffff; /* invalid */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005654}
5655
5656/** Sets the output scale for a given output.
5657 *
5658 * \param output The weston_output object that the scale is set for.
5659 * \param scale Scale factor for the given output.
5660 *
5661 * It only supports setting scale for an output that
5662 * is not enabled and it can only be ran once.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005663 *
5664 * \memberof weston_output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005665 */
5666WL_EXPORT void
5667weston_output_set_scale(struct weston_output *output,
5668 int32_t scale)
5669{
5670 /* We can only set scale on a disabled output */
5671 assert(!output->enabled);
5672
5673 /* We only want to set scale once */
5674 assert(!output->scale);
5675
5676 output->scale = scale;
5677}
5678
5679/** Sets the output transform for a given output.
5680 *
5681 * \param output The weston_output object that the transform is set for.
5682 * \param transform Transform value for the given output.
5683 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005684 * Refer to wl_output::transform section located at
5685 * https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_output
5686 * for list of values that can be passed to this function.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005687 *
5688 * \memberof weston_output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005689 */
5690WL_EXPORT void
5691weston_output_set_transform(struct weston_output *output,
5692 uint32_t transform)
5693{
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005694 struct weston_pointer_motion_event ev;
5695 struct wl_resource *resource;
5696 struct weston_seat *seat;
5697 pixman_region32_t old_region;
5698 int mid_x, mid_y;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005699 struct weston_head *head;
5700 int ver;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005701
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005702 if (!output->enabled && output->transform == UINT32_MAX) {
5703 output->transform = transform;
5704 return;
5705 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005706
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005707 weston_output_transform_scale_init(output, transform, output->scale);
5708
5709 pixman_region32_init(&old_region);
5710 pixman_region32_copy(&old_region, &output->region);
5711
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005712 weston_output_init_geometry(output, output->x, output->y);
5713
5714 output->dirty = 1;
5715
5716 /* Notify clients of the change for output transform. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005717 wl_list_for_each(head, &output->head_list, output_link) {
5718 wl_resource_for_each(resource, &head->resource_list) {
5719 wl_output_send_geometry(resource,
5720 output->x,
5721 output->y,
5722 head->mm_width,
5723 head->mm_height,
5724 head->subpixel,
5725 head->make,
5726 head->model,
5727 output->transform);
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005728
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005729 ver = wl_resource_get_version(resource);
5730 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
5731 wl_output_send_done(resource);
5732 }
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005733 }
5734
5735 /* we must ensure that pointers are inside output, otherwise they disappear */
5736 mid_x = output->x + output->width / 2;
5737 mid_y = output->y + output->height / 2;
5738
5739 ev.mask = WESTON_POINTER_MOTION_ABS;
5740 ev.x = wl_fixed_to_double(wl_fixed_from_int(mid_x));
5741 ev.y = wl_fixed_to_double(wl_fixed_from_int(mid_y));
5742
5743 wl_list_for_each(seat, &output->compositor->seat_list, link) {
5744 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5745
5746 if (pointer && pixman_region32_contains_point(&old_region,
5747 wl_fixed_to_int(pointer->x),
5748 wl_fixed_to_int(pointer->y),
5749 NULL))
5750 weston_pointer_move(pointer, &ev);
5751 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005752}
5753
5754/** Initializes a weston_output object with enough data so
5755 ** an output can be configured.
5756 *
5757 * \param output The weston_output object to initialize
5758 * \param compositor The compositor instance.
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005759 * \param name Name for the output (the string is copied).
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005760 *
5761 * Sets initial values for fields that are expected to be
5762 * configured either by compositors or backends.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005763 *
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005764 * The name is used in logs, and can be used by compositors as a configuration
5765 * identifier.
5766 *
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005767 * \memberof weston_output
5768 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005769 */
5770WL_EXPORT void
Armin Krezović40087402016-09-30 14:11:12 +02005771weston_output_init(struct weston_output *output,
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005772 struct weston_compositor *compositor,
5773 const char *name)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005774{
5775 output->compositor = compositor;
5776 output->destroying = 0;
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03005777 output->name = strdup(name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005778 wl_list_init(&output->link);
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02005779 wl_signal_init(&output->user_destroy_signal);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005780 output->enabled = false;
5781
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005782 wl_list_init(&output->head_list);
5783
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005784 /* Add some (in)sane defaults which can be used
5785 * for checking if an output was properly configured
5786 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005787 output->scale = 0;
5788 /* Can't use -1 on uint32_t and 0 is valid enum value */
5789 output->transform = UINT32_MAX;
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005790
5791 pixman_region32_init(&output->previous_damage);
5792 pixman_region32_init(&output->region);
Pekka Paalanen42704142017-09-06 16:47:52 +03005793 wl_list_init(&output->mode_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005794}
5795
5796/** Adds weston_output object to pending output list.
5797 *
5798 * \param output The weston_output object to add
5799 * \param compositor The compositor instance.
5800 *
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03005801 * The opposite of this operation is built into weston_output_release().
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005802 *
5803 * \memberof weston_output
5804 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005805 */
5806WL_EXPORT void
5807weston_compositor_add_pending_output(struct weston_output *output,
5808 struct weston_compositor *compositor)
5809{
Pekka Paalanene952a012017-03-29 17:14:00 +03005810 assert(output->disable);
5811 assert(output->enable);
5812
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005813 wl_list_remove(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005814 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005815}
5816
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02005817/** Create a string with the attached heads' names.
5818 *
5819 * The string must be free()'d.
5820 */
5821static char *
5822weston_output_create_heads_string(struct weston_output *output)
5823{
5824 FILE *fp;
5825 char *str = NULL;
5826 size_t size = 0;
5827 struct weston_head *head;
5828 const char *sep = "";
5829
5830 fp = open_memstream(&str, &size);
5831 if (!fp)
5832 return NULL;
5833
5834 wl_list_for_each(head, &output->head_list, output_link) {
5835 fprintf(fp, "%s%s", sep, head->name);
5836 sep = ", ";
5837 }
5838 fclose(fp);
5839
5840 return str;
5841}
5842
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005843/** Constructs a weston_output object that can be used by the compositor.
5844 *
Pekka Paalanencc201e42017-03-30 15:11:25 +03005845 * \param output The weston_output object that needs to be enabled. Must not
Pekka Paalanenddce54d2017-08-23 16:00:21 +03005846 * be enabled already. Must have at least one head attached.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005847 *
5848 * Output coordinates are calculated and each new output is by default
5849 * assigned to the right of previous one.
5850 *
5851 * Sets up the transformation, zoom, and geometry of the output using
5852 * the properties that need to be configured by the compositor.
5853 *
5854 * Establishes a repaint timer for the output with the relevant display
5855 * object's event loop. See output_repaint_timer_handler().
5856 *
5857 * The output is assigned an ID. Weston can support up to 32 distinct
5858 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
5859 * is referred to and used to find the first available ID number, and
5860 * then this ID is marked as used in output_id_pool.
5861 *
5862 * The output is also assigned a Wayland global with the wl_output
5863 * external interface.
5864 *
5865 * Backend specific function is called to set up the output output.
5866 *
5867 * Output is added to the compositor's output list
5868 *
5869 * If the backend specific function fails, the weston_output object
5870 * is returned to a state it was before calling this function and
5871 * is added to the compositor's pending_output_list in case it needs
5872 * to be reconfigured or just so it can be destroyed at shutdown.
5873 *
5874 * 0 is returned on success, -1 on failure.
5875 */
5876WL_EXPORT int
5877weston_output_enable(struct weston_output *output)
5878{
Armin Krezović782f5df2016-09-30 14:11:11 +02005879 struct weston_compositor *c = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005880 struct weston_output *iterator;
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03005881 struct weston_head *head;
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02005882 char *head_names;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005883 int x = 0, y = 0;
5884
Pekka Paalanencc201e42017-03-30 15:11:25 +03005885 if (output->enabled) {
5886 weston_log("Error: attempt to enable an enabled output '%s'\n",
5887 output->name);
5888 return -1;
5889 }
5890
Pekka Paalanenddce54d2017-08-23 16:00:21 +03005891 if (wl_list_empty(&output->head_list)) {
5892 weston_log("Error: cannot enable output '%s' without heads.\n",
5893 output->name);
5894 return -1;
5895 }
5896
Pekka Paalanen586e1ac2017-09-14 16:17:59 +03005897 if (wl_list_empty(&output->mode_list) || !output->current_mode) {
5898 weston_log("Error: no video mode for output '%s'.\n",
5899 output->name);
5900 return -1;
5901 }
5902
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03005903 wl_list_for_each(head, &output->head_list, output_link) {
5904 assert(head->make);
5905 assert(head->model);
5906 }
5907
Armin Krezović782f5df2016-09-30 14:11:11 +02005908 iterator = container_of(c->output_list.prev,
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005909 struct weston_output, link);
5910
Armin Krezović782f5df2016-09-30 14:11:11 +02005911 if (!wl_list_empty(&c->output_list))
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005912 x = iterator->x + iterator->width;
5913
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005914 /* Make sure the scale is set up */
5915 assert(output->scale);
5916
5917 /* Make sure we have a transform set */
5918 assert(output->transform != UINT32_MAX);
5919
Armin Krezović782f5df2016-09-30 14:11:11 +02005920 output->x = x;
5921 output->y = y;
5922 output->dirty = 1;
5923 output->original_scale = output->scale;
5924
5925 weston_output_transform_scale_init(output, output->transform, output->scale);
5926 weston_output_init_zoom(output);
5927
5928 weston_output_init_geometry(output, x, y);
5929 weston_output_damage(output);
5930
5931 wl_signal_init(&output->frame_signal);
5932 wl_signal_init(&output->destroy_signal);
5933 wl_list_init(&output->animation_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02005934 wl_list_init(&output->feedback_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02005935
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005936 /* Enable the output (set up the crtc or create a
5937 * window representing the output, set up the
5938 * renderer, etc)
5939 */
5940 if (output->enable(output) < 0) {
5941 weston_log("Enabling output \"%s\" failed.\n", output->name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005942 return -1;
5943 }
5944
5945 weston_compositor_add_output(output->compositor, output);
5946
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02005947 head_names = weston_output_create_heads_string(output);
5948 weston_log("Output '%s' enabled with head(s) %s\n",
5949 output->name, head_names);
5950 free(head_names);
5951
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005952 return 0;
5953}
5954
5955/** Converts a weston_output object to a pending output state, so it
5956 ** can be configured again or destroyed.
5957 *
5958 * \param output The weston_output object that needs to be disabled.
5959 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005960 * Calls a backend specific function to disable an output, in case
5961 * such function exists.
5962 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03005963 * The backend specific disable function may choose to postpone the disabling
5964 * by returning a negative value, in which case this function returns early.
5965 * In that case the backend will guarantee the output will be disabled soon
5966 * by the backend calling this function again. One must not attempt to re-enable
5967 * the output until that happens.
5968 *
5969 * Otherwise, if the output is being used by the compositor, it is removed
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005970 * from weston's output_list (see weston_compositor_remove_output())
5971 * and is returned to a state it was before weston_output_enable()
5972 * was ran (see weston_output_enable_undo()).
5973 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03005974 * See weston_output_init() for more information on the
5975 * state output is returned to.
Pekka Paalanencc201e42017-03-30 15:11:25 +03005976 *
5977 * If the output has never been enabled yet, this function can still be
5978 * called to ensure that the output is actually turned off rather than left
5979 * in the state it was discovered in.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005980 */
5981WL_EXPORT void
5982weston_output_disable(struct weston_output *output)
5983{
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005984 /* Should we rename this? */
5985 output->destroying = 1;
5986
Pekka Paalanenc65df642017-03-29 15:45:46 +03005987 /* Disable is called unconditionally also for not-enabled outputs,
5988 * because at compositor start-up, if there is an output that is
5989 * already on but the compositor wants to turn it off, we have to
5990 * forward the turn-off to the backend so it knows to do it.
5991 * The backend cannot initially turn off everything, because it
5992 * would cause unnecessary mode-sets for all outputs the compositor
5993 * wants to be on.
5994 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005995 if (output->disable(output) < 0)
5996 return;
5997
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005998 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005999 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006000
6001 output->destroying = 0;
6002}
6003
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006004/** Forces a synchronous call to heads_changed hook
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006005 *
6006 * \param compositor The compositor instance
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006007 *
6008 * If there are new or changed heads, calls the heads_changed hook and
6009 * returns after the hook returns.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006010 */
6011WL_EXPORT void
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006012weston_compositor_flush_heads_changed(struct weston_compositor *compositor)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006013{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03006014 if (compositor->heads_changed_source) {
6015 wl_event_source_remove(compositor->heads_changed_source);
6016 weston_compositor_call_heads_changed(compositor);
6017 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006018}
6019
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006020/** Add destroy callback for an output
6021 *
6022 * \param output The output to watch.
6023 * \param listener The listener to add. The \c notify member must be set.
6024 *
6025 * The listener callback will be called when user destroys an output. This
6026 * may be delayed by a backend in some cases. The main purpose of the
6027 * listener is to allow hooking up custom data to the output. The custom data
6028 * can be fetched via weston_output_get_destroy_listener() followed by
6029 * container_of().
6030 *
6031 * The \c data argument to the notify callback is the weston_output being
6032 * destroyed.
6033 *
6034 * @note This is for the final destruction of an output, not when it gets
6035 * disabled. If you want to keep track of enabled outputs, this is not it.
6036 */
6037WL_EXPORT void
6038weston_output_add_destroy_listener(struct weston_output *output,
6039 struct wl_listener *listener)
6040{
6041 wl_signal_add(&output->user_destroy_signal, listener);
6042}
6043
6044/** Look up destroy listener for an output
6045 *
6046 * \param output The output to query.
6047 * \param notify The notify function used used for the added destroy listener.
6048 * \return The listener, or NULL if not found.
6049 *
6050 * This looks up the previously added destroy listener struct based on the
6051 * notify function it has. The listener can be used to access user data
6052 * through \c container_of().
6053 *
6054 * \sa wl_signal_get() weston_output_add_destroy_listener()
6055 */
6056WL_EXPORT struct wl_listener *
6057weston_output_get_destroy_listener(struct weston_output *output,
6058 wl_notify_func_t notify)
6059{
6060 return wl_signal_get(&output->user_destroy_signal, notify);
6061}
6062
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006063/** Uninitialize an output
6064 *
6065 * Removes the output from the list of enabled outputs if necessary, but
6066 * does not call the backend's output disable function. The output will no
6067 * longer be in the list of pending outputs either.
6068 *
6069 * All fields of weston_output become uninitialized, i.e. should not be used
6070 * anymore. The caller can free the memory after this.
6071 *
6072 * \memberof weston_output
6073 * \internal
6074 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006075WL_EXPORT void
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03006076weston_output_release(struct weston_output *output)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006077{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006078 struct weston_head *head, *tmp;
6079
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006080 output->destroying = 1;
6081
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006082 wl_signal_emit(&output->user_destroy_signal, output);
6083
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03006084 if (output->idle_repaint_source)
6085 wl_event_source_remove(output->idle_repaint_source);
6086
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006087 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006088 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006089
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006090 pixman_region32_fini(&output->region);
6091 pixman_region32_fini(&output->previous_damage);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006092 wl_list_remove(&output->link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006093
6094 wl_list_for_each_safe(head, tmp, &output->head_list, output_link)
6095 weston_head_detach(head);
6096
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006097 free(output->name);
6098}
6099
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006100/** Find an output by its given name
6101 *
6102 * \param compositor The compositor to search in.
6103 * \param name The output name to search for.
6104 * \return An existing output with the given name, or NULL if not found.
6105 *
6106 * \memberof weston_compositor
6107 */
6108WL_EXPORT struct weston_output *
6109weston_compositor_find_output_by_name(struct weston_compositor *compositor,
6110 const char *name)
6111{
6112 struct weston_output *output;
6113
6114 wl_list_for_each(output, &compositor->output_list, link)
6115 if (strcmp(output->name, name) == 0)
6116 return output;
6117
6118 wl_list_for_each(output, &compositor->pending_output_list, link)
6119 if (strcmp(output->name, name) == 0)
6120 return output;
6121
6122 return NULL;
6123}
6124
6125/** Create a named output
6126 *
6127 * \param compositor The compositor.
6128 * \param name The name for the output.
6129 * \return A new \c weston_output, or NULL on failure.
6130 *
6131 * This creates a new weston_output that starts with no heads attached.
6132 *
6133 * An output must be configured and it must have at least one head before
6134 * it can be enabled.
6135 *
6136 * \memberof weston_compositor
6137 */
6138WL_EXPORT struct weston_output *
6139weston_compositor_create_output(struct weston_compositor *compositor,
6140 const char *name)
6141{
6142 assert(compositor->backend->create_output);
6143
6144 if (weston_compositor_find_output_by_name(compositor, name)) {
6145 weston_log("Warning: attempted to create an output with a "
6146 "duplicate name '%s'.\n", name);
6147 return NULL;
6148 }
6149
6150 return compositor->backend->create_output(compositor, name);
6151}
6152
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006153/** Create an output for an unused head
6154 *
6155 * \param compositor The compositor.
6156 * \param head The head to attach to the output.
6157 * \return A new \c weston_output, or NULL on failure.
6158 *
6159 * This creates a new weston_output that starts with the given head attached.
6160 * The output inherits the name of the head. The head must not be already
6161 * attached to another output.
6162 *
6163 * An output must be configured before it can be enabled.
6164 *
6165 * \memberof weston_compositor
6166 */
6167WL_EXPORT struct weston_output *
6168weston_compositor_create_output_with_head(struct weston_compositor *compositor,
6169 struct weston_head *head)
6170{
6171 struct weston_output *output;
6172
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006173 output = weston_compositor_create_output(compositor, head->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006174 if (!output)
6175 return NULL;
6176
6177 if (weston_output_attach_head(output, head) < 0) {
Pekka Paalanen42c0e142017-10-27 12:07:49 +03006178 weston_output_destroy(output);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006179 return NULL;
6180 }
6181
6182 return output;
6183}
6184
6185/** Destroy an output
6186 *
6187 * \param output The output to destroy.
6188 *
6189 * The heads attached to the given output are detached and become unused again.
6190 *
6191 * It is not necessary to explicitly destroy all outputs at compositor exit.
6192 * weston_compositor_destroy() will automatically destroy any remaining
6193 * outputs.
6194 *
6195 * \memberof weston_output
6196 */
6197WL_EXPORT void
6198weston_output_destroy(struct weston_output *output)
6199{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006200 output->destroy(output);
6201}
6202
Pekka Paalanencf0a4762017-04-04 16:36:07 +03006203/** When you need a head...
6204 *
6205 * This function is a hack, used until all code has been converted to become
6206 * multi-head aware.
6207 *
6208 * \param output The weston_output whose head to get.
6209 * \return The first head in the output's list.
6210 */
6211WL_EXPORT struct weston_head *
6212weston_output_get_first_head(struct weston_output *output)
6213{
6214 if (wl_list_empty(&output->head_list))
6215 return NULL;
6216
6217 return container_of(output->head_list.next,
6218 struct weston_head, output_link);
6219}
6220
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01006221static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006222destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006223{
Jonny Lamb74130762013-11-26 18:19:46 +01006224 struct weston_surface *surface =
6225 wl_resource_get_user_data(resource);
6226
Pekka Paalanen4826f872016-04-22 14:14:38 +03006227 if (!surface)
6228 return;
6229
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006230 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02006231 surface->pending.buffer_viewport.buffer.src_width =
6232 wl_fixed_from_int(-1);
6233 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006234 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006235}
6236
6237static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006238viewport_destroy(struct wl_client *client,
6239 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006240{
6241 wl_resource_destroy(resource);
6242}
6243
6244static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006245viewport_set_source(struct wl_client *client,
6246 struct wl_resource *resource,
6247 wl_fixed_t src_x,
6248 wl_fixed_t src_y,
6249 wl_fixed_t src_width,
6250 wl_fixed_t src_height)
6251{
6252 struct weston_surface *surface =
6253 wl_resource_get_user_data(resource);
6254
Pekka Paalanen4826f872016-04-22 14:14:38 +03006255 if (!surface) {
6256 wl_resource_post_error(resource,
6257 WP_VIEWPORT_ERROR_NO_SURFACE,
6258 "wl_surface for this viewport is no longer exists");
6259 return;
6260 }
6261
6262 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03006263 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006264
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006265 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03006266 src_height == wl_fixed_from_int(-1) &&
6267 src_x == wl_fixed_from_int(-1) &&
6268 src_y == wl_fixed_from_int(-1)) {
6269 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006270 surface->pending.buffer_viewport.buffer.src_width =
6271 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006272 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006273 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006274 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006275
Pekka Paalanen201769a2016-04-26 14:42:11 +03006276 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006277 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006278 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03006279 "wl_surface@%d viewport source "
6280 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
6281 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006282 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03006283 wl_fixed_to_double(src_height),
6284 wl_fixed_to_double(src_x),
6285 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006286 return;
6287 }
6288
6289 surface->pending.buffer_viewport.buffer.src_x = src_x;
6290 surface->pending.buffer_viewport.buffer.src_y = src_y;
6291 surface->pending.buffer_viewport.buffer.src_width = src_width;
6292 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006293 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006294}
6295
6296static void
6297viewport_set_destination(struct wl_client *client,
6298 struct wl_resource *resource,
6299 int32_t dst_width,
6300 int32_t dst_height)
6301{
6302 struct weston_surface *surface =
6303 wl_resource_get_user_data(resource);
6304
Pekka Paalanen4826f872016-04-22 14:14:38 +03006305 if (!surface) {
6306 wl_resource_post_error(resource,
6307 WP_VIEWPORT_ERROR_NO_SURFACE,
6308 "wl_surface for this viewport no longer exists");
6309 return;
6310 }
6311
6312 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006313
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006314 if (dst_width == -1 && dst_height == -1) {
6315 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006316 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006317 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006318 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006319 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006320
6321 if (dst_width <= 0 || dst_height <= 0) {
6322 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006323 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006324 "destination size must be positive (%dx%d)",
6325 dst_width, dst_height);
6326 return;
6327 }
6328
6329 surface->pending.buffer_viewport.surface.width = dst_width;
6330 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006331 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006332}
6333
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006334static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006335 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006336 viewport_set_source,
6337 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01006338};
6339
6340static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006341viewporter_destroy(struct wl_client *client,
6342 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006343{
6344 wl_resource_destroy(resource);
6345}
6346
6347static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006348viewporter_get_viewport(struct wl_client *client,
6349 struct wl_resource *viewporter,
6350 uint32_t id,
6351 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006352{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006353 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006354 struct weston_surface *surface =
6355 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006356 struct wl_resource *resource;
6357
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006358 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006359 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006360 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006361 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01006362 return;
6363 }
6364
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006365 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006366 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006367 if (resource == NULL) {
6368 wl_client_post_no_memory(client);
6369 return;
6370 }
6371
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006372 wl_resource_set_implementation(resource, &viewport_interface,
6373 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01006374
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006375 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006376}
6377
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006378static const struct wp_viewporter_interface viewporter_interface = {
6379 viewporter_destroy,
6380 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01006381};
6382
6383static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006384bind_viewporter(struct wl_client *client,
6385 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006386{
6387 struct wl_resource *resource;
6388
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006389 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006390 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006391 if (resource == NULL) {
6392 wl_client_post_no_memory(client);
6393 return;
6394 }
6395
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006396 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01006397 NULL, NULL);
6398}
6399
6400static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04006401destroy_presentation_feedback(struct wl_resource *feedback_resource)
6402{
6403 struct weston_presentation_feedback *feedback;
6404
6405 feedback = wl_resource_get_user_data(feedback_resource);
6406
6407 wl_list_remove(&feedback->link);
6408 free(feedback);
6409}
6410
6411static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006412presentation_destroy(struct wl_client *client, struct wl_resource *resource)
6413{
6414 wl_resource_destroy(resource);
6415}
6416
6417static void
6418presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04006419 struct wl_resource *presentation_resource,
6420 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006421 uint32_t callback)
6422{
Pekka Paalanen133e4392014-09-23 22:08:46 -04006423 struct weston_surface *surface;
6424 struct weston_presentation_feedback *feedback;
6425
6426 surface = wl_resource_get_user_data(surface_resource);
6427
Bryce Harringtonde16d892014-11-20 22:21:57 -08006428 feedback = zalloc(sizeof *feedback);
6429 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04006430 goto err_calloc;
6431
6432 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006433 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04006434 1, callback);
6435 if (!feedback->resource)
6436 goto err_create;
6437
6438 wl_resource_set_implementation(feedback->resource, NULL, feedback,
6439 destroy_presentation_feedback);
6440
6441 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
6442
6443 return;
6444
6445err_create:
6446 free(feedback);
6447
6448err_calloc:
6449 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006450}
6451
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006452static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006453 presentation_destroy,
6454 presentation_feedback
6455};
6456
6457static void
6458bind_presentation(struct wl_client *client,
6459 void *data, uint32_t version, uint32_t id)
6460{
6461 struct weston_compositor *compositor = data;
6462 struct wl_resource *resource;
6463
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006464 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006465 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006466 if (resource == NULL) {
6467 wl_client_post_no_memory(client);
6468 return;
6469 }
6470
6471 wl_resource_set_implementation(resource, &presentation_implementation,
6472 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006473 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006474}
6475
6476static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05006477compositor_bind(struct wl_client *client,
6478 void *data, uint32_t version, uint32_t id)
6479{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006480 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05006481 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05006482
Jason Ekstranda85118c2013-06-27 20:17:02 -05006483 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006484 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07006485 if (resource == NULL) {
6486 wl_client_post_no_memory(client);
6487 return;
6488 }
6489
6490 wl_resource_set_implementation(resource, &compositor_interface,
6491 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05006492}
6493
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006494WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006495weston_environment_get_fd(const char *env)
6496{
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07006497 char *e;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006498 int fd, flags;
6499
6500 e = getenv(env);
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07006501 if (!e || !safe_strtoint(e, &fd))
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006502 return -1;
6503
6504 flags = fcntl(fd, F_GETFD);
6505 if (flags == -1)
6506 return -1;
6507
6508 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
6509 unsetenv(env);
6510
6511 return fd;
6512}
6513
Pekka Paalanenb5026542014-11-12 15:09:24 +02006514static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02006515timeline_key_binding_handler(struct weston_keyboard *keyboard,
6516 const struct timespec *time, uint32_t key,
6517 void *data)
Pekka Paalanenb5026542014-11-12 15:09:24 +02006518{
6519 struct weston_compositor *compositor = data;
6520
6521 if (weston_timeline_enabled_)
6522 weston_timeline_close();
6523 else
6524 weston_timeline_open(compositor);
6525}
6526
Daniel Stonece62cb32018-07-20 09:46:24 +01006527static const char *
6528output_repaint_status_text(struct weston_output *output)
6529{
6530 switch (output->repaint_status) {
6531 case REPAINT_NOT_SCHEDULED:
6532 return "no repaint";
6533 case REPAINT_BEGIN_FROM_IDLE:
6534 return "start_repaint_loop scheduled";
6535 case REPAINT_SCHEDULED:
6536 return "repaint scheduled";
6537 case REPAINT_AWAITING_COMPLETION:
6538 return "awaiting completion";
6539 }
6540
6541 assert(!"output_repaint_status_text missing enum");
6542 return NULL;
6543}
6544
6545static void
6546debug_scene_view_print_buffer(FILE *fp, struct weston_view *view)
6547{
6548 struct weston_buffer *buffer = view->surface->buffer_ref.buffer;
6549 struct wl_shm_buffer *shm;
6550 struct linux_dmabuf_buffer *dmabuf;
Marius Vlad00a6e012018-11-20 17:52:31 +02006551 const struct pixel_format_info *pixel_info = NULL;
Daniel Stonece62cb32018-07-20 09:46:24 +01006552
6553 if (!buffer) {
6554 fprintf(fp, "\t\t[buffer not available]\n");
6555 return;
6556 }
6557
6558 shm = wl_shm_buffer_get(buffer->resource);
6559 if (shm) {
Marius Vlad00a6e012018-11-20 17:52:31 +02006560 uint32_t _format = wl_shm_buffer_get_format(shm);
6561 pixel_info = pixel_format_get_info_shm(_format);
Daniel Stonece62cb32018-07-20 09:46:24 +01006562 fprintf(fp, "\t\tSHM buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02006563 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
6564 (unsigned long) _format,
6565 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Daniel Stonece62cb32018-07-20 09:46:24 +01006566 return;
6567 }
6568
6569 dmabuf = linux_dmabuf_buffer_get(buffer->resource);
6570 if (dmabuf) {
Marius Vlad00a6e012018-11-20 17:52:31 +02006571 pixel_info = pixel_format_get_info(dmabuf->attributes.format);
Daniel Stonece62cb32018-07-20 09:46:24 +01006572 fprintf(fp, "\t\tdmabuf buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02006573 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
6574 (unsigned long) dmabuf->attributes.format,
6575 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Daniel Stonece62cb32018-07-20 09:46:24 +01006576 fprintf(fp, "\t\t\tmodifier: 0x%llx\n",
6577 (unsigned long long) dmabuf->attributes.modifier[0]);
6578 return;
6579 }
6580
6581 fprintf(fp, "\t\tEGL buffer");
6582}
6583
6584static void
6585debug_scene_view_print(FILE *fp, struct weston_view *view, int view_idx)
6586{
6587 struct weston_compositor *ec = view->surface->compositor;
6588 struct weston_output *output;
6589 char desc[512];
6590 pixman_box32_t *box;
6591 uint32_t surface_id = 0;
6592 pid_t pid = 0;
6593
6594 if (view->surface->resource) {
6595 struct wl_resource *resource = view->surface->resource;
6596 wl_client_get_credentials(wl_resource_get_client(resource),
6597 &pid, NULL, NULL);
6598 surface_id = wl_resource_get_id(view->surface->resource);
6599 }
6600
6601 if (!view->surface->get_label ||
6602 view->surface->get_label(view->surface, desc, sizeof(desc)) < 0) {
6603 strcpy(desc, "[no description available]");
6604 }
6605 fprintf(fp, "\tView %d (role %s, PID %d, surface ID %u, %s, %p):\n",
6606 view_idx, view->surface->role_name, pid, surface_id,
6607 desc, view);
6608
6609 box = pixman_region32_extents(&view->transform.boundingbox);
6610 fprintf(fp, "\t\tposition: (%d, %d) -> (%d, %d)\n",
6611 box->x1, box->y1, box->x2, box->y2);
6612 box = pixman_region32_extents(&view->transform.opaque);
6613
6614 if (pixman_region32_equal(&view->transform.opaque,
6615 &view->transform.boundingbox)) {
6616 fprintf(fp, "\t\t[fully opaque]\n");
6617 } else if (!pixman_region32_not_empty(&view->transform.opaque)) {
6618 fprintf(fp, "\t\t[not opaque]\n");
6619 } else {
6620 fprintf(fp, "\t\t[opaque: (%d, %d) -> (%d, %d)]\n",
6621 box->x1, box->y1, box->x2, box->y2);
6622 }
6623
6624 if (view->alpha < 1.0)
6625 fprintf(fp, "\t\talpha: %f\n", view->alpha);
6626
6627 if (view->output_mask != 0) {
6628 bool first_output = true;
6629 fprintf(fp, "\t\toutputs: ");
6630 wl_list_for_each(output, &ec->output_list, link) {
6631 if (!(view->output_mask & (1 << output->id)))
6632 continue;
6633 fprintf(fp, "%s%d (%s)%s",
6634 (first_output) ? "" : ", ",
6635 output->id, output->name,
6636 (view->output == output) ? " (primary)" : "");
6637 first_output = false;
6638 }
6639 } else {
6640 fprintf(fp, "\t\t[no outputs]");
6641 }
6642
6643 fprintf(fp, "\n");
6644
6645 debug_scene_view_print_buffer(fp, view);
6646}
6647
6648/**
6649 * Output information on how libweston is currently composing the scene
6650 * graph.
6651 */
6652WL_EXPORT char *
6653weston_compositor_print_scene_graph(struct weston_compositor *ec)
6654{
6655 struct weston_output *output;
6656 struct weston_layer *layer;
6657 struct timespec now;
6658 int layer_idx = 0;
6659 FILE *fp;
6660 char *ret;
6661 size_t len;
6662 int err;
6663
6664 fp = open_memstream(&ret, &len);
6665 assert(fp);
6666
6667 weston_compositor_read_presentation_clock(ec, &now);
6668 fprintf(fp, "Weston scene graph at %ld.%09ld:\n\n",
6669 now.tv_sec, now.tv_nsec);
6670
6671 wl_list_for_each(output, &ec->output_list, link) {
6672 struct weston_head *head;
6673 int head_idx = 0;
6674
6675 fprintf(fp, "Output %d (%s):\n", output->id, output->name);
6676 assert(output->enabled);
6677
6678 fprintf(fp, "\tposition: (%d, %d) -> (%d, %d)\n",
6679 output->x, output->y,
6680 output->x + output->width,
6681 output->y + output->height);
6682 fprintf(fp, "\tmode: %dx%d@%.3fHz\n",
6683 output->current_mode->width,
6684 output->current_mode->height,
6685 output->current_mode->refresh / 1000.0);
6686 fprintf(fp, "\tscale: %d\n", output->scale);
6687
6688 fprintf(fp, "\trepaint status: %s\n",
6689 output_repaint_status_text(output));
6690 if (output->repaint_status == REPAINT_SCHEDULED)
6691 fprintf(fp, "\tnext repaint: %ld.%09ld\n",
6692 output->next_repaint.tv_sec,
6693 output->next_repaint.tv_nsec);
6694
6695 wl_list_for_each(head, &output->head_list, output_link) {
6696 fprintf(fp, "\tHead %d (%s): %sconnected\n",
6697 head_idx++, head->name,
6698 (head->connected) ? "" : "not ");
6699 }
6700 }
6701
6702 fprintf(fp, "\n");
6703
6704 wl_list_for_each(layer, &ec->layer_list, link) {
6705 struct weston_view *view;
6706 int view_idx = 0;
6707
6708 fprintf(fp, "Layer %d (pos 0x%lx):\n", layer_idx++,
6709 (unsigned long) layer->position);
6710
6711 if (!weston_layer_mask_is_infinite(layer)) {
6712 fprintf(fp, "\t[mask: (%d, %d) -> (%d,%d)]\n\n",
6713 layer->mask.x1, layer->mask.y1,
6714 layer->mask.x2, layer->mask.y2);
6715 }
6716
6717 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
6718 debug_scene_view_print(fp, view, view_idx++);
6719
6720 if (wl_list_empty(&layer->view_list.link))
6721 fprintf(fp, "\t[no views]\n");
6722
6723 fprintf(fp, "\n");
6724 }
6725
6726 err = fclose(fp);
6727 assert(err == 0);
6728
6729 return ret;
6730}
6731
6732/**
6733 * Called when the 'scene-graph' debug scope is bound by a client. This
6734 * one-shot weston-debug scope prints the current scene graph when bound,
6735 * and then terminates the stream.
6736 */
6737static void
6738debug_scene_graph_cb(struct weston_debug_stream *stream, void *data)
6739{
6740 struct weston_compositor *ec = data;
6741 char *str = weston_compositor_print_scene_graph(ec);
6742
6743 weston_debug_stream_printf(stream, "%s", str);
6744 free(str);
6745 weston_debug_stream_complete(stream);
6746}
6747
Giulio Camuffo459137b2014-10-11 23:56:24 +03006748/** Create the compositor.
6749 *
6750 * This functions creates and initializes a compositor instance.
6751 *
6752 * \param display The Wayland display to be used.
6753 * \param user_data A pointer to an object that can later be retrieved
6754 * using the \ref weston_compositor_get_user_data function.
6755 * \return The compositor instance on success or NULL on failure.
6756 */
6757WL_EXPORT struct weston_compositor *
6758weston_compositor_create(struct wl_display *display, void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04006759{
Giulio Camuffo459137b2014-10-11 23:56:24 +03006760 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05006761 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07006762
Giulio Camuffo459137b2014-10-11 23:56:24 +03006763 ec = zalloc(sizeof *ec);
6764 if (!ec)
6765 return NULL;
6766
6767 ec->wl_display = display;
6768 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006769 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07006770 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006771 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006772 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03006773 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006774 wl_signal_init(&ec->idle_signal);
6775 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02006776 wl_signal_init(&ec->show_input_panel_signal);
6777 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02006778 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01006779 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01006780 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02006781 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006782 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02006783 wl_signal_init(&ec->output_resized_signal);
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03006784 wl_signal_init(&ec->heads_changed_signal);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02006785 wl_signal_init(&ec->output_heads_changed_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07006786 wl_signal_init(&ec->session_signal);
6787 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04006788
Casey Dahlin58ba1372012-04-19 22:50:08 -04006789 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03006790 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04006791
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02006792 ec->activate_serial = 1;
6793
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05006794 ec->touch_mode = WESTON_TOUCH_MODE_NORMAL;
6795
Derek Foreman152254b2015-11-26 14:17:48 -06006796 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006797 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006798 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05006799
Giulio Camuffo954f1832014-10-11 18:27:30 +03006800 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006801 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006802 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03006803
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006804 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006805 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006806 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006807
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006808 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006809 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03006810 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006811
Pekka Paalanena5630ea2017-10-12 13:13:42 +02006812 if (weston_debug_compositor_create(ec) < 0)
6813 goto fail;
6814
Jonas Ådahl30d61d82014-10-22 21:21:17 +02006815 if (weston_input_init(ec) != 0)
6816 goto fail;
6817
Jason Ekstranda7af7042013-10-12 22:38:11 -05006818 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02006819 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006820 wl_list_init(&ec->layer_list);
6821 wl_list_init(&ec->seat_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006822 wl_list_init(&ec->pending_output_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006823 wl_list_init(&ec->output_list);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03006824 wl_list_init(&ec->head_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006825 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01006826 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006827 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01006828 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006829 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006830 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01006831
Pekka Paalanen827b5d22016-06-29 11:54:26 +02006832 wl_list_init(&ec->plugin_api_list);
6833
Xiong Zhang97116532013-10-23 13:58:31 +08006834 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02006835 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04006836
Giulio Camuffo459137b2014-10-11 23:56:24 +03006837 wl_data_device_manager_init(ec->wl_display);
6838
6839 wl_display_init_shm(ec->wl_display);
6840
6841 loop = wl_display_get_event_loop(ec->wl_display);
6842 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Daniel Stone6847b852017-03-01 11:34:08 +00006843 ec->repaint_timer =
6844 wl_event_loop_add_timer(loop, output_repaint_timer_handler,
6845 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03006846
Quentin Glidic82681572016-12-17 13:40:51 +01006847 weston_layer_init(&ec->fade_layer, ec);
6848 weston_layer_init(&ec->cursor_layer, ec);
6849
6850 weston_layer_set_position(&ec->fade_layer, WESTON_LAYER_POSITION_FADE);
6851 weston_layer_set_position(&ec->cursor_layer,
6852 WESTON_LAYER_POSITION_CURSOR);
Giulio Camuffo459137b2014-10-11 23:56:24 +03006853
6854 weston_compositor_add_debug_binding(ec, KEY_T,
6855 timeline_key_binding_handler, ec);
6856
Daniel Stonece62cb32018-07-20 09:46:24 +01006857 ec->debug_scene =
6858 weston_compositor_add_debug_scope(ec, "scene-graph",
6859 "Scene graph details\n",
6860 debug_scene_graph_cb,
6861 ec);
6862
Giulio Camuffo459137b2014-10-11 23:56:24 +03006863 return ec;
6864
6865fail:
6866 free(ec);
6867 return NULL;
6868}
6869
Benjamin Franzkeb8263022011-08-30 11:32:47 +02006870WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006871weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07006872{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006873 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07006874
Pekka Paalanend1591ae2012-01-02 16:06:56 +02006875 wl_event_source_remove(ec->idle_source);
6876
Matt Roper361d2ad2011-08-29 13:52:23 -07006877 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02006878 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07006879 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02006880
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006881 /* Destroy all pending outputs associated with this compositor */
6882 wl_list_for_each_safe(output, next, &ec->pending_output_list, link)
6883 output->destroy(output);
6884
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02006885 if (ec->renderer)
6886 ec->renderer->destroy(ec);
6887
Daniel Stone325fc2d2012-05-30 16:31:58 +01006888 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09006889 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006890 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01006891 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006892 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006893 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02006894
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04006895 weston_plane_release(&ec->primary_plane);
Matt Roper361d2ad2011-08-29 13:52:23 -07006896}
6897
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05006898WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04006899weston_compositor_exit_with_code(struct weston_compositor *compositor,
6900 int exit_code)
6901{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02006902 if (compositor->exit_code == EXIT_SUCCESS)
6903 compositor->exit_code = exit_code;
6904
Giulio Camuffo459137b2014-10-11 23:56:24 +03006905 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04006906}
6907
6908WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01006909weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
6910 const struct weston_pointer_grab_interface *interface)
6911{
6912 struct weston_seat *seat;
6913
6914 ec->default_pointer_grab = interface;
6915 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05006916 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
6917
6918 if (pointer)
6919 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01006920 }
6921}
6922
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04006923WL_EXPORT int
6924weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
6925 clockid_t clk_id)
6926{
6927 struct timespec ts;
6928
6929 if (clock_gettime(clk_id, &ts) < 0)
6930 return -1;
6931
6932 compositor->presentation_clock = clk_id;
6933
6934 return 0;
6935}
6936
6937/*
6938 * For choosing the software clock, when the display hardware or API
6939 * does not expose a compatible presentation timestamp.
6940 */
6941WL_EXPORT int
6942weston_compositor_set_presentation_clock_software(
6943 struct weston_compositor *compositor)
6944{
6945 /* In order of preference */
6946 static const clockid_t clocks[] = {
6947 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
6948 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
6949 CLOCK_MONOTONIC, /* no jumps, may crawl */
6950 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
6951 CLOCK_REALTIME /* may jump and crawl */
6952 };
6953 unsigned i;
6954
6955 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
6956 if (weston_compositor_set_presentation_clock(compositor,
6957 clocks[i]) == 0)
6958 return 0;
6959
6960 weston_log("Error: no suitable presentation clock available.\n");
6961
6962 return -1;
6963}
6964
Pekka Paalanen662f3842015-03-18 12:17:26 +02006965/** Read the current time from the Presentation clock
6966 *
6967 * \param compositor
6968 * \param ts[out] The current time.
6969 *
6970 * \note Reading the current time in user space is always imprecise to some
6971 * degree.
6972 *
6973 * This function is never meant to fail. If reading the clock does fail,
6974 * an error message is logged and a zero time is returned. Callers are not
6975 * supposed to detect or react to failures.
6976 */
6977WL_EXPORT void
6978weston_compositor_read_presentation_clock(
6979 const struct weston_compositor *compositor,
6980 struct timespec *ts)
6981{
6982 static bool warned;
6983 int ret;
6984
6985 ret = clock_gettime(compositor->presentation_clock, ts);
6986 if (ret < 0) {
6987 ts->tv_sec = 0;
6988 ts->tv_nsec = 0;
6989
6990 if (!warned)
6991 weston_log("Error: failure to read "
6992 "the presentation clock %#x: '%m' (%d)\n",
6993 compositor->presentation_clock, errno);
6994 warned = true;
6995 }
6996}
6997
Pekka Paalanen230f3b12014-09-29 14:18:40 -04006998/** Import dmabuf buffer into current renderer
6999 *
7000 * \param compositor
7001 * \param buffer the dmabuf buffer to import
7002 * \return true on usable buffers, false otherwise
7003 *
7004 * This function tests that the linux_dmabuf_buffer is usable
7005 * for the current renderer. Returns false on unusable buffers. Usually
7006 * usability is tested by importing the dmabufs for composition.
7007 *
7008 * This hook is also used for detecting if the renderer supports
7009 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
7010 * supported.
7011 * */
7012WL_EXPORT bool
7013weston_compositor_import_dmabuf(struct weston_compositor *compositor,
7014 struct linux_dmabuf_buffer *buffer)
7015{
7016 struct weston_renderer *renderer;
7017
7018 renderer = compositor->renderer;
7019
7020 if (renderer->import_dmabuf == NULL)
7021 return false;
7022
7023 return renderer->import_dmabuf(compositor, buffer);
7024}
7025
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007026WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05007027weston_version(int *major, int *minor, int *micro)
7028{
7029 *major = WESTON_VERSION_MAJOR;
7030 *minor = WESTON_VERSION_MINOR;
7031 *micro = WESTON_VERSION_MICRO;
7032}
7033
Daniel Stonee03c1112016-11-24 20:45:45 +00007034/**
7035 * Attempts to find a module path from the module map specified in the
7036 * environment. If found, writes the full path into the path variable.
7037 *
7038 * The module map is a string in environment variable WESTON_MODULE_MAP, where
7039 * each entry is of the form "name=path" and entries are separated by
7040 * semicolons. Whitespace is significant.
7041 *
7042 * \param name The name to search for.
7043 * \param path Where the path is written to if found.
7044 * \param path_len Allocated bytes at \c path .
7045 * \returns The length of the string written to path on success, or 0 if the
7046 * module was not specified in the environment map or path_len was too small.
7047 */
7048WL_EXPORT size_t
7049weston_module_path_from_env(const char *name, char *path, size_t path_len)
7050{
7051 const char *mapping = getenv("WESTON_MODULE_MAP");
7052 const char *end;
7053 const int name_len = strlen(name);
7054
7055 if (!mapping)
7056 return 0;
7057
7058 end = mapping + strlen(mapping);
7059 while (mapping < end && *mapping) {
7060 const char *filename, *next;
7061
7062 /* early out: impossibly short string */
7063 if (end - mapping < name_len + 1)
7064 return 0;
7065
7066 filename = &mapping[name_len + 1];
7067 next = strchrnul(mapping, ';');
7068
7069 if (strncmp(mapping, name, name_len) == 0 &&
7070 mapping[name_len] == '=') {
7071 size_t file_len = next - filename; /* no trailing NUL */
7072 if (file_len >= path_len)
7073 return 0;
7074 strncpy(path, filename, file_len);
7075 path[file_len] = '\0';
7076 return file_len;
7077 }
7078
7079 mapping = next + 1;
7080 }
7081
7082 return 0;
7083}
7084
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03007085WL_EXPORT void *
7086weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007087{
7088 char path[PATH_MAX];
7089 void *module, *init;
Daniel Stonebeb97e52016-11-28 12:13:54 +00007090 size_t len;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007091
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08007092 if (name == NULL)
7093 return NULL;
7094
Derek Foreman3f86e502015-06-08 11:46:54 -05007095 if (name[0] != '/') {
Daniel Stonee03c1112016-11-24 20:45:45 +00007096 len = weston_module_path_from_env(name, path, sizeof path);
7097 if (len == 0)
Daniel Stonebeb97e52016-11-28 12:13:54 +00007098 len = snprintf(path, sizeof path, "%s/%s",
7099 LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05007100 } else {
Daniel Stonebeb97e52016-11-28 12:13:54 +00007101 len = snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05007102 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007103
Daniel Stonebeb97e52016-11-28 12:13:54 +00007104 /* snprintf returns the length of the string it would've written,
7105 * _excluding_ the NUL byte. So even being equal to the size of
7106 * our buffer is an error here. */
7107 if (len >= sizeof path)
7108 return NULL;
7109
Kristian Høgsberga6813d22012-09-12 12:21:01 -04007110 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
7111 if (module) {
7112 weston_log("Module '%s' already loaded\n", path);
7113 dlclose(module);
7114 return NULL;
7115 }
7116
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03007117 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04007118 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007119 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03007120 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007121 return NULL;
7122 }
7123
7124 init = dlsym(module, entrypoint);
7125 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03007126 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00007127 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007128 return NULL;
7129 }
7130
7131 return init;
7132}
7133
Giulio Camuffo459137b2014-10-11 23:56:24 +03007134
7135/** Destroys the compositor.
7136 *
7137 * This function cleans up the compositor state and destroys it.
7138 *
7139 * \param compositor The compositor to be destroyed.
7140 */
7141WL_EXPORT void
7142weston_compositor_destroy(struct weston_compositor *compositor)
7143{
7144 /* prevent further rendering while shutting down */
7145 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
7146
7147 wl_signal_emit(&compositor->destroy_signal, compositor);
7148
7149 weston_compositor_xkb_destroy(compositor);
7150
Giulio Camuffo2d24e642015-10-03 16:25:15 +03007151 if (compositor->backend)
7152 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007153
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03007154 /* The backend is responsible for destroying the heads. */
7155 assert(wl_list_empty(&compositor->head_list));
7156
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007157 weston_plugin_api_destroy_list(compositor);
7158
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03007159 if (compositor->heads_changed_source)
7160 wl_event_source_remove(compositor->heads_changed_source);
7161
Daniel Stonece62cb32018-07-20 09:46:24 +01007162 weston_debug_scope_destroy(compositor->debug_scene);
7163 compositor->debug_scene = NULL;
Pekka Paalanena5630ea2017-10-12 13:13:42 +02007164 weston_debug_compositor_destroy(compositor);
7165
Giulio Camuffo459137b2014-10-11 23:56:24 +03007166 free(compositor);
7167}
7168
7169/** Instruct the compositor to exit.
7170 *
7171 * This functions does not directly destroy the compositor object, it merely
7172 * command it to start the tear down process. It is not guaranteed that the
7173 * tear down will happen immediately.
7174 *
7175 * \param compositor The compositor to tear down.
7176 */
7177WL_EXPORT void
7178weston_compositor_exit(struct weston_compositor *compositor)
7179{
7180 compositor->exit(compositor);
7181}
7182
7183/** Return the user data stored in the compositor.
7184 *
7185 * This function returns the user data pointer set with user_data parameter
7186 * to the \ref weston_compositor_create function.
7187 */
7188WL_EXPORT void *
7189weston_compositor_get_user_data(struct weston_compositor *compositor)
7190{
7191 return compositor->user_data;
7192}
Pekka Paalanendd186732016-06-03 14:49:54 +03007193
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007194static const char * const backend_map[] = {
7195 [WESTON_BACKEND_DRM] = "drm-backend.so",
7196 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
7197 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
7198 [WESTON_BACKEND_RDP] = "rdp-backend.so",
7199 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
7200 [WESTON_BACKEND_X11] = "x11-backend.so",
7201};
7202
Pekka Paalanendd186732016-06-03 14:49:54 +03007203/** Load a backend into a weston_compositor
7204 *
7205 * A backend must be loaded to make a weston_compositor work. A backend
7206 * provides input and output capabilities, and determines the renderer to use.
7207 *
7208 * \param compositor A compositor that has not had a backend loaded yet.
7209 * \param backend Name of the backend file.
7210 * \param config_base A pointer to a backend-specific configuration
7211 * structure's 'base' member.
7212 *
7213 * \return 0 on success, or -1 on error.
7214 */
7215WL_EXPORT int
7216weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007217 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03007218 struct weston_backend_config *config_base)
7219{
7220 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03007221 struct weston_backend_config *config_base);
7222
Pekka Paalanend7e35112017-08-29 17:04:12 +03007223 if (compositor->backend) {
7224 weston_log("Error: attempt to load a backend when one is already loaded\n");
7225 return -1;
7226 }
7227
Quentin Glidic887c0182016-07-10 11:00:53 +02007228 if (backend >= ARRAY_LENGTH(backend_map))
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007229 return -1;
7230
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01007231 backend_init = weston_load_module(backend_map[backend], "weston_backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03007232 if (!backend_init)
7233 return -1;
7234
Pekka Paalanend7e35112017-08-29 17:04:12 +03007235 if (backend_init(compositor, config_base) < 0) {
7236 compositor->backend = NULL;
7237 return -1;
7238 }
7239
7240 return 0;
Pekka Paalanendd186732016-06-03 14:49:54 +03007241}
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007242
7243WL_EXPORT int
7244weston_compositor_load_xwayland(struct weston_compositor *compositor)
7245{
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007246 int (*module_init)(struct weston_compositor *ec);
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007247
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007248 module_init = weston_load_module("xwayland.so", "weston_module_init");
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007249 if (!module_init)
7250 return -1;
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007251 if (module_init(compositor) < 0)
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007252 return -1;
7253 return 0;
7254}
Vasilis Liaskovitis486b4632018-10-10 16:14:55 +02007255
7256/** Resolve an internal compositor error by disconnecting the client.
7257 *
7258 * This function is used in cases when the wl_buffer turns out
7259 * unusable and there is no fallback path.
7260 *
7261 * It is possible the fault is caused by a compositor bug, the underlying
7262 * graphics stack bug or normal behaviour, or perhaps a client mistake.
7263 * In any case, the options are to either composite garbage or nothing,
7264 * or disconnect the client. This is a helper function for the latter.
7265 *
7266 * The error is sent as an INVALID_OBJECT error on the client's wl_display.
7267 *
7268 * \param buffer The weston buffer that is unusable.
7269 * \param msg A custom error message attached to the protocol error.
7270 */
7271WL_EXPORT void
7272weston_buffer_send_server_error(struct weston_buffer *buffer,
7273 const char *msg)
7274{
7275 struct wl_client *client;
7276 struct wl_resource *display_resource;
7277 uint32_t id;
7278
7279 assert(buffer->resource);
7280 id = wl_resource_get_id(buffer->resource);
7281 client = wl_resource_get_client(buffer->resource);
7282 display_resource = wl_client_get_object(client, 1);
7283
7284 assert(display_resource);
7285 wl_resource_post_error(display_resource,
7286 WL_DISPLAY_ERROR_INVALID_OBJECT,
7287 "server error with "
7288 "wl_buffer@%u: %s", id, msg);
7289}