blob: 6e9653721a868d0c98479df648192ff85738e8fd [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
Pekka Paalanen3d5d9472019-03-28 16:28:47 +020058#include <libweston/libweston.h>
Marius Vladc901e892019-06-21 22:49:18 +030059#include <libweston/weston-log.h>
Daniel Stonece62cb32018-07-20 09:46:24 +010060#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"
Roman Gilge97391c2019-03-29 13:01:06 +010063#include "xdg-output-unstable-v1-server-protocol.h"
Alexandros Frantzisacff29b2018-10-19 12:14:11 +030064#include "linux-explicit-synchronization-unstable-v1-server-protocol.h"
Alexandros Frantzis67629672018-10-19 12:14:11 +030065#include "linux-explicit-synchronization.h"
Alexandros Frantzisacff29b2018-10-19 12:14:11 +030066#include "shared/fd-util.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070067#include "shared/helpers.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070068#include "shared/os-compatibility.h"
Bryce Harrington25a2bdd2016-08-03 17:40:52 -070069#include "shared/string-helpers.h"
Pekka Paalanenaa21f622015-07-03 15:44:50 +030070#include "shared/timespec-util.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040071#include "git-version.h"
Pekka Paalanencda14882019-04-04 15:41:02 +030072#include <libweston/version.h>
Pekka Paalanen27b377f2019-03-29 17:07:34 +020073#include <libweston/plugin-registry.h>
Marius Vlad00a6e012018-11-20 17:52:31 +020074#include "pixel-formats.h"
Marius Vlad63ef0782019-07-16 23:34:14 +030075#include "backend.h"
Marius Vlada72e3712019-07-10 13:46:39 +030076#include "libweston-internal.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050077
Marius Vlad4e036292019-07-09 00:36:20 +030078#include "weston-log-internal.h"
79
Marius Vlad78984ee2019-06-11 00:05:08 +030080/**
81 * \defgroup head Head
82 * \defgroup output Output
83 * \defgroup compositor Compositor
84 */
85
Pekka Paalanen0513a952014-05-21 16:17:27 +030086#define DEFAULT_REPAINT_WINDOW 7 /* milliseconds */
87
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020088static void
Pekka Paalanend72bad22017-03-29 17:01:41 +030089weston_output_update_matrix(struct weston_output *output);
90
91static void
Alexander Larsson0b135062013-05-28 16:23:36 +020092weston_output_transform_scale_init(struct weston_output *output,
93 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020094
Rob Bradford27b17932013-06-26 18:08:46 +010095static void
Jason Ekstranda7af7042013-10-12 22:38:11 -050096weston_compositor_build_view_list(struct weston_compositor *compositor);
Rob Bradford27b17932013-06-26 18:08:46 +010097
Pekka Paalanendcac3512017-12-08 14:13:34 +020098static char *
99weston_output_create_heads_string(struct weston_output *output);
100
Pekka Paalanen6528c032017-03-28 15:27:10 +0300101/** Send wl_output events for mode and scale changes
102 *
103 * \param head Send on all resources bound to this head.
104 * \param mode_changed If true, send the current mode.
105 * \param scale_changed If true, send the current scale.
106 */
107static void
108weston_mode_switch_send_events(struct weston_head *head,
109 bool mode_changed, bool scale_changed)
110{
111 struct weston_output *output = head->output;
112 struct wl_resource *resource;
113 int version;
114
115 wl_resource_for_each(resource, &head->resource_list) {
116 if (mode_changed) {
117 wl_output_send_mode(resource,
118 output->current_mode->flags,
119 output->current_mode->width,
120 output->current_mode->height,
121 output->current_mode->refresh);
122 }
123
124 version = wl_resource_get_version(resource);
125 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION && scale_changed)
126 wl_output_send_scale(resource, output->current_scale);
127
128 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
129 wl_output_send_done(resource);
130 }
Roman Gilge97391c2019-03-29 13:01:06 +0100131 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
132 zxdg_output_v1_send_logical_position(resource,
133 output->x,
134 output->y);
135 zxdg_output_v1_send_logical_size(resource,
136 output->width,
137 output->height);
138 zxdg_output_v1_send_done(resource);
139 }
Pekka Paalanen6528c032017-03-28 15:27:10 +0300140}
141
142static void
143weston_mode_switch_finish(struct weston_output *output,
144 int mode_changed, int scale_changed)
Alex Wu2dda6042012-04-17 17:20:47 +0800145{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200146 struct weston_seat *seat;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300147 struct weston_head *head;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200148 pixman_region32_t old_output_region;
Alexander Larsson355748e2013-05-28 16:23:38 +0200149
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200150 pixman_region32_init(&old_output_region);
151 pixman_region32_copy(&old_output_region, &output->region);
152
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200153 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +0200154 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200155
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200156 pixman_region32_init_rect(&output->region, output->x, output->y,
157 output->width, output->height);
158
159 weston_output_update_matrix(output);
160
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200161 /* If a pointer falls outside the outputs new geometry, move it to its
162 * lower-right corner */
163 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500164 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200165 int32_t x, y;
166
167 if (!pointer)
168 continue;
169
170 x = wl_fixed_to_int(pointer->x);
171 y = wl_fixed_to_int(pointer->y);
172
173 if (!pixman_region32_contains_point(&old_output_region,
174 x, y, NULL) ||
175 pixman_region32_contains_point(&output->region,
176 x, y, NULL))
177 continue;
178
179 if (x >= output->x + output->width)
180 x = output->x + output->width - 1;
181 if (y >= output->y + output->height)
182 y = output->y + output->height - 1;
183
184 pointer->x = wl_fixed_from_int(x);
185 pointer->y = wl_fixed_from_int(y);
186 }
187
188 pixman_region32_fini(&old_output_region);
189
Derek Foremandd4cd332014-11-10 10:29:59 -0600190 if (!mode_changed && !scale_changed)
191 return;
192
Hardening57388e42013-09-18 23:56:36 +0200193 /* notify clients of the changes */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300194 wl_list_for_each(head, &output->head_list, output_link)
195 weston_mode_switch_send_events(head,
196 mode_changed, scale_changed);
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600197}
198
David Fort0de859e2016-05-27 23:22:57 +0200199static void
200weston_compositor_reflow_outputs(struct weston_compositor *compositor,
201 struct weston_output *resized_output, int delta_width);
202
Marius Vlad55d87362019-06-11 01:15:35 +0300203/**
204 * \ingroup output
205 */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600206WL_EXPORT int
207weston_output_mode_set_native(struct weston_output *output,
208 struct weston_mode *mode,
209 int32_t scale)
210{
211 int ret;
212 int mode_changed = 0, scale_changed = 0;
David Fort0de859e2016-05-27 23:22:57 +0200213 int32_t old_width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600214
215 if (!output->switch_mode)
216 return -1;
217
218 if (!output->original_mode) {
219 mode_changed = 1;
220 ret = output->switch_mode(output, mode);
221 if (ret < 0)
222 return ret;
223 if (output->current_scale != scale) {
224 scale_changed = 1;
225 output->current_scale = scale;
Hardening57388e42013-09-18 23:56:36 +0200226 }
227 }
228
David Fort0de859e2016-05-27 23:22:57 +0200229 old_width = output->width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600230 output->native_mode = mode;
231 output->native_scale = scale;
232
233 weston_mode_switch_finish(output, mode_changed, scale_changed);
234
David Fort0de859e2016-05-27 23:22:57 +0200235 if (mode_changed || scale_changed) {
236 weston_compositor_reflow_outputs(output->compositor, output, output->width - old_width);
237
238 wl_signal_emit(&output->compositor->output_resized_signal, output);
239 }
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600240 return 0;
241}
242
Marius Vlad55d87362019-06-11 01:15:35 +0300243/**
244 * \ingroup output
245 */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600246WL_EXPORT int
247weston_output_mode_switch_to_native(struct weston_output *output)
248{
249 int ret;
250 int mode_changed = 0, scale_changed = 0;
251
252 if (!output->switch_mode)
253 return -1;
254
255 if (!output->original_mode) {
256 weston_log("already in the native mode\n");
257 return -1;
258 }
259 /* the non fullscreen clients haven't seen a mode set since we
260 * switched into a temporary, so we need to notify them if the
261 * mode at that time is different from the native mode now.
262 */
263 mode_changed = (output->original_mode != output->native_mode);
264 scale_changed = (output->original_scale != output->native_scale);
265
266 ret = output->switch_mode(output, output->native_mode);
267 if (ret < 0)
268 return ret;
269
270 output->current_scale = output->native_scale;
271
272 output->original_mode = NULL;
273 output->original_scale = 0;
274
275 weston_mode_switch_finish(output, mode_changed, scale_changed);
276
277 return 0;
278}
279
Marius Vlad55d87362019-06-11 01:15:35 +0300280/**
281 * \ingroup output
282 */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600283WL_EXPORT int
284weston_output_mode_switch_to_temporary(struct weston_output *output,
285 struct weston_mode *mode,
286 int32_t scale)
287{
288 int ret;
289
290 if (!output->switch_mode)
291 return -1;
292
293 /* original_mode is the last mode non full screen clients have seen,
294 * so we shouldn't change it if we already have one set.
295 */
296 if (!output->original_mode) {
297 output->original_mode = output->native_mode;
298 output->original_scale = output->native_scale;
299 }
300 ret = output->switch_mode(output, mode);
301 if (ret < 0)
302 return ret;
303
304 output->current_scale = scale;
305
306 weston_mode_switch_finish(output, 0, 0);
307
308 return 0;
Alex Wu2dda6042012-04-17 17:20:47 +0800309}
310
Benjamin Franzke06286262011-05-06 19:12:33 +0200311static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300312region_init_infinite(pixman_region32_t *region)
313{
314 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
315 UINT32_MAX, UINT32_MAX);
316}
317
Pekka Paalanene67858b2013-04-25 13:57:42 +0300318static struct weston_subsurface *
319weston_surface_to_subsurface(struct weston_surface *surface);
320
Jason Ekstranda7af7042013-10-12 22:38:11 -0500321WL_EXPORT struct weston_view *
322weston_view_create(struct weston_surface *surface)
323{
324 struct weston_view *view;
325
Bryce Harringtonde16d892014-11-20 22:21:57 -0800326 view = zalloc(sizeof *view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500327 if (view == NULL)
328 return NULL;
329
330 view->surface = surface;
Daniel Stonefb4869d2016-12-09 16:27:54 +0000331 view->plane = &surface->compositor->primary_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500332
Jason Ekstranda7af7042013-10-12 22:38:11 -0500333 /* Assign to surface */
334 wl_list_insert(&surface->views, &view->surface_link);
335
336 wl_signal_init(&view->destroy_signal);
337 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300338 wl_list_init(&view->layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500339
Jason Ekstranda7af7042013-10-12 22:38:11 -0500340 pixman_region32_init(&view->clip);
341
342 view->alpha = 1.0;
343 pixman_region32_init(&view->transform.opaque);
344
345 wl_list_init(&view->geometry.transformation_list);
346 wl_list_insert(&view->geometry.transformation_list,
347 &view->transform.position.link);
348 weston_matrix_init(&view->transform.position.matrix);
349 wl_list_init(&view->geometry.child_list);
Pekka Paalanen380adf52015-02-16 14:39:11 +0200350 pixman_region32_init(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500351 pixman_region32_init(&view->transform.boundingbox);
352 view->transform.dirty = 1;
353
Jason Ekstranda7af7042013-10-12 22:38:11 -0500354 return view;
355}
356
Jason Ekstrand108865d2014-06-26 10:04:49 -0700357struct weston_frame_callback {
358 struct wl_resource *resource;
359 struct wl_list link;
360};
361
Pekka Paalanen133e4392014-09-23 22:08:46 -0400362struct weston_presentation_feedback {
363 struct wl_resource *resource;
364
365 /* XXX: could use just wl_resource_get_link() instead */
366 struct wl_list link;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200367
368 /* The per-surface feedback flags */
369 uint32_t psf_flags;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400370};
371
372static void
373weston_presentation_feedback_discard(
374 struct weston_presentation_feedback *feedback)
375{
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200376 wp_presentation_feedback_send_discarded(feedback->resource);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400377 wl_resource_destroy(feedback->resource);
378}
379
380static void
381weston_presentation_feedback_discard_list(struct wl_list *list)
382{
383 struct weston_presentation_feedback *feedback, *tmp;
384
385 wl_list_for_each_safe(feedback, tmp, list, link)
386 weston_presentation_feedback_discard(feedback);
387}
388
389static void
390weston_presentation_feedback_present(
391 struct weston_presentation_feedback *feedback,
392 struct weston_output *output,
393 uint32_t refresh_nsec,
394 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200395 uint64_t seq,
396 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400397{
398 struct wl_client *client = wl_resource_get_client(feedback->resource);
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300399 struct weston_head *head;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400400 struct wl_resource *o;
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200401 uint32_t tv_sec_hi;
402 uint32_t tv_sec_lo;
403 uint32_t tv_nsec;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300404 bool done = false;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400405
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300406 wl_list_for_each(head, &output->head_list, output_link) {
407 wl_resource_for_each(o, &head->resource_list) {
408 if (wl_resource_get_client(o) != client)
409 continue;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400410
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300411 wp_presentation_feedback_send_sync_output(feedback->resource, o);
412 done = true;
413 }
414
415 /* For clone mode, send it for just one wl_output global,
416 * they are all equivalent anyway.
417 */
418 if (done)
419 break;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400420 }
421
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200422 timespec_to_proto(ts, &tv_sec_hi, &tv_sec_lo, &tv_nsec);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200423 wp_presentation_feedback_send_presented(feedback->resource,
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200424 tv_sec_hi, tv_sec_lo, tv_nsec,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200425 refresh_nsec,
426 seq >> 32, seq & 0xffffffff,
427 flags | feedback->psf_flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400428 wl_resource_destroy(feedback->resource);
429}
430
431static void
432weston_presentation_feedback_present_list(struct wl_list *list,
433 struct weston_output *output,
434 uint32_t refresh_nsec,
435 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200436 uint64_t seq,
437 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400438{
439 struct weston_presentation_feedback *feedback, *tmp;
440
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200441 assert(!(flags & WP_PRESENTATION_FEEDBACK_INVALID) ||
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200442 wl_list_empty(list));
443
Pekka Paalanen133e4392014-09-23 22:08:46 -0400444 wl_list_for_each_safe(feedback, tmp, list, link)
445 weston_presentation_feedback_present(feedback, output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200446 refresh_nsec, ts, seq,
447 flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400448}
449
Jason Ekstrand7b982072014-05-20 14:33:03 -0500450static void
451surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
452{
453 struct weston_surface_state *state =
454 container_of(listener, struct weston_surface_state,
455 buffer_destroy_listener);
456
457 state->buffer = NULL;
458}
459
460static void
461weston_surface_state_init(struct weston_surface_state *state)
462{
463 state->newly_attached = 0;
464 state->buffer = NULL;
465 state->buffer_destroy_listener.notify =
466 surface_state_handle_buffer_destroy;
467 state->sx = 0;
468 state->sy = 0;
469
Derek Foreman152254b2015-11-26 14:17:48 -0600470 pixman_region32_init(&state->damage_surface);
471 pixman_region32_init(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500472 pixman_region32_init(&state->opaque);
473 region_init_infinite(&state->input);
474
475 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400476 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500477
478 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
479 state->buffer_viewport.buffer.scale = 1;
480 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
481 state->buffer_viewport.surface.width = -1;
482 state->buffer_viewport.changed = 0;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300483
484 state->acquire_fence_fd = -1;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +0530485
486 state->desired_protection = WESTON_HDCP_DISABLE;
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +0530487 state->protection_mode = WESTON_SURFACE_PROTECTION_MODE_RELAXED;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500488}
489
490static void
491weston_surface_state_fini(struct weston_surface_state *state)
492{
493 struct weston_frame_callback *cb, *next;
494
495 wl_list_for_each_safe(cb, next,
496 &state->frame_callback_list, link)
497 wl_resource_destroy(cb->resource);
498
Pekka Paalanen133e4392014-09-23 22:08:46 -0400499 weston_presentation_feedback_discard_list(&state->feedback_list);
500
Jason Ekstrand7b982072014-05-20 14:33:03 -0500501 pixman_region32_fini(&state->input);
502 pixman_region32_fini(&state->opaque);
Derek Foreman152254b2015-11-26 14:17:48 -0600503 pixman_region32_fini(&state->damage_surface);
504 pixman_region32_fini(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500505
506 if (state->buffer)
507 wl_list_remove(&state->buffer_destroy_listener.link);
508 state->buffer = NULL;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300509
510 fd_clear(&state->acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +0300511 weston_buffer_release_reference(&state->buffer_release_ref, NULL);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500512}
513
514static void
515weston_surface_state_set_buffer(struct weston_surface_state *state,
516 struct weston_buffer *buffer)
517{
518 if (state->buffer == buffer)
519 return;
520
521 if (state->buffer)
522 wl_list_remove(&state->buffer_destroy_listener.link);
523 state->buffer = buffer;
524 if (state->buffer)
525 wl_signal_add(&state->buffer->destroy_signal,
526 &state->buffer_destroy_listener);
527}
528
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500529WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500530weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500531{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500532 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400533
Bryce Harringtonde16d892014-11-20 22:21:57 -0800534 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400535 if (surface == NULL)
536 return NULL;
537
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500538 wl_signal_init(&surface->destroy_signal);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +0800539 wl_signal_init(&surface->commit_signal);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500540
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500541 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200542 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400543
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200544 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
545 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200546 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
547 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500548
549 weston_surface_state_init(&surface->pending);
550
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400551 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500552 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300553 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400554
Jason Ekstranda7af7042013-10-12 22:38:11 -0500555 wl_list_init(&surface->views);
556
557 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400558 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500559
Pekka Paalanene67858b2013-04-25 13:57:42 +0300560 wl_list_init(&surface->subsurface_list);
561 wl_list_init(&surface->subsurface_list_pending);
562
Jason Ekstrand1e059042014-10-16 10:55:19 -0500563 weston_matrix_init(&surface->buffer_to_surface_matrix);
564 weston_matrix_init(&surface->surface_to_buffer_matrix);
565
Jonas Ådahld3414f22016-07-22 17:56:31 +0800566 wl_list_init(&surface->pointer_constraints);
567
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300568 surface->acquire_fence_fd = -1;
569
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +0530570 surface->desired_protection = WESTON_HDCP_DISABLE;
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +0530571 surface->current_protection = WESTON_HDCP_DISABLE;
572 surface->protection_mode = WESTON_SURFACE_PROTECTION_MODE_RELAXED;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +0530573
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400574 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500575}
576
Alex Wu8811bf92012-02-28 18:07:54 +0800577WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500578weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200579 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500580{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100581 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Philipp Zabel195dade2018-09-03 19:44:59 +0200582 surface->is_opaque = !(alpha < 1.0);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500583}
584
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400585WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500586weston_view_to_global_float(struct weston_view *view,
587 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200588{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500589 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200590 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
591
Jason Ekstranda7af7042013-10-12 22:38:11 -0500592 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200593
594 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200595 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700596 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200597 v.f[3]);
598 *x = 0;
599 *y = 0;
600 return;
601 }
602
603 *x = v.f[0] / v.f[3];
604 *y = v.f[1] / v.f[3];
605 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500606 *x = sx + view->geometry.x;
607 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200608 }
609}
610
Pekka Paalanen0df44772020-02-14 14:50:03 +0200611/** Transform a point to buffer coordinates
612 *
613 * \param width Surface width.
614 * \param height Surface height.
615 * \param transform Buffer transform.
616 * \param scale Buffer scale.
617 * \param sx Surface x coordinate of a point.
618 * \param sy Surface y coordinate of a point.
619 * \param[out] bx Buffer x coordinate of the point.
620 * \param[out] by Buffer Y coordinate of the point.
621 *
622 * Converts the given surface-local coordinates to buffer coordinates
623 * according to the given buffer transform and scale.
624 * This ignores wp_viewport.
625 *
626 * The given width and height must be the result of inverse scaled and
627 * inverse transformed buffer size.
628 */
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500629WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200630weston_transformed_coord(int width, int height,
631 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200632 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200633 float sx, float sy, float *bx, float *by)
634{
635 switch (transform) {
636 case WL_OUTPUT_TRANSFORM_NORMAL:
637 default:
638 *bx = sx;
639 *by = sy;
640 break;
641 case WL_OUTPUT_TRANSFORM_FLIPPED:
642 *bx = width - sx;
643 *by = sy;
644 break;
645 case WL_OUTPUT_TRANSFORM_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200646 *bx = sy;
647 *by = width - sx;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200648 break;
649 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200650 *bx = sy;
651 *by = sx;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200652 break;
653 case WL_OUTPUT_TRANSFORM_180:
654 *bx = width - sx;
655 *by = height - sy;
656 break;
657 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
658 *bx = sx;
659 *by = height - sy;
660 break;
661 case WL_OUTPUT_TRANSFORM_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200662 *bx = height - sy;
663 *by = sx;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200664 break;
665 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200666 *bx = height - sy;
667 *by = width - sx;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200668 break;
669 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200670
671 *bx *= scale;
672 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200673}
674
Pekka Paalanen0df44772020-02-14 14:50:03 +0200675/** Transform a rectangle to buffer coordinates
676 *
677 * \param width Surface width.
678 * \param height Surface height.
679 * \param transform Buffer transform.
680 * \param scale Buffer scale.
681 * \param rect Rectangle in surface coordinates.
682 * \return Rectangle in buffer coordinates.
683 *
684 * Converts the given surface-local rectangle to buffer coordinates
685 * according to the given buffer transform and scale. The resulting
686 * rectangle is guaranteed to be well-formed.
687 * This ignores wp_viewport.
688 *
689 * The given width and height must be the result of inverse scaled and
690 * inverse transformed buffer size.
691 */
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200692WL_EXPORT pixman_box32_t
693weston_transformed_rect(int width, int height,
694 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200695 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200696 pixman_box32_t rect)
697{
698 float x1, x2, y1, y2;
699
700 pixman_box32_t ret;
701
Alexander Larsson4ea95522013-05-22 14:41:37 +0200702 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200703 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200704 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200705 rect.x2, rect.y2, &x2, &y2);
706
707 if (x1 <= x2) {
708 ret.x1 = x1;
709 ret.x2 = x2;
710 } else {
711 ret.x1 = x2;
712 ret.x2 = x1;
713 }
714
715 if (y1 <= y2) {
716 ret.y1 = y1;
717 ret.y2 = y2;
718 } else {
719 ret.y1 = y2;
720 ret.y2 = y1;
721 }
722
723 return ret;
724}
725
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600726/** Transform a region by a matrix, restricted to axis-aligned transformations
727 *
728 * Warning: This function does not work for projective, affine, or matrices
729 * that encode arbitrary rotations. Only 90-degree step rotations are
730 * supported.
731 */
732WL_EXPORT void
733weston_matrix_transform_region(pixman_region32_t *dest,
734 struct weston_matrix *matrix,
735 pixman_region32_t *src)
736{
737 pixman_box32_t *src_rects, *dest_rects;
738 int nrects, i;
739
740 src_rects = pixman_region32_rectangles(src, &nrects);
741 dest_rects = malloc(nrects * sizeof(*dest_rects));
742 if (!dest_rects)
743 return;
744
745 for (i = 0; i < nrects; i++) {
746 struct weston_vector vec1 = {{
747 src_rects[i].x1, src_rects[i].y1, 0, 1
748 }};
749 weston_matrix_transform(matrix, &vec1);
750 vec1.f[0] /= vec1.f[3];
751 vec1.f[1] /= vec1.f[3];
752
753 struct weston_vector vec2 = {{
754 src_rects[i].x2, src_rects[i].y2, 0, 1
755 }};
756 weston_matrix_transform(matrix, &vec2);
757 vec2.f[0] /= vec2.f[3];
758 vec2.f[1] /= vec2.f[3];
759
760 if (vec1.f[0] < vec2.f[0]) {
761 dest_rects[i].x1 = floor(vec1.f[0]);
762 dest_rects[i].x2 = ceil(vec2.f[0]);
763 } else {
764 dest_rects[i].x1 = floor(vec2.f[0]);
765 dest_rects[i].x2 = ceil(vec1.f[0]);
766 }
767
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600768 if (vec1.f[1] < vec2.f[1]) {
769 dest_rects[i].y1 = floor(vec1.f[1]);
770 dest_rects[i].y2 = ceil(vec2.f[1]);
771 } else {
772 dest_rects[i].y1 = floor(vec2.f[1]);
773 dest_rects[i].y2 = ceil(vec1.f[1]);
774 }
775 }
776
777 pixman_region32_clear(dest);
778 pixman_region32_init_rects(dest, dest_rects, nrects);
779 free(dest_rects);
780}
781
Pekka Paalanen0df44772020-02-14 14:50:03 +0200782/** Transform a region to buffer coordinates
783 *
784 * \param width Surface width.
785 * \param height Surface height.
786 * \param transform Buffer transform.
787 * \param scale Buffer scale.
788 * \param[in] src Region in surface coordinates.
789 * \param[out] dest Resulting region in buffer coordinates.
790 *
791 * Converts the given surface-local region to buffer coordinates
792 * according to the given buffer transform and scale.
793 * This ignores wp_viewport.
794 *
795 * The given width and height must be the result of inverse scaled and
796 * inverse transformed buffer size.
797 *
798 * src and dest are allowed to point to the same memory for in-place conversion.
799 */
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200800WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500801weston_transformed_region(int width, int height,
802 enum wl_output_transform transform,
803 int32_t scale,
804 pixman_region32_t *src, pixman_region32_t *dest)
805{
806 pixman_box32_t *src_rects, *dest_rects;
807 int nrects, i;
808
809 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
810 if (src != dest)
811 pixman_region32_copy(dest, src);
812 return;
813 }
814
815 src_rects = pixman_region32_rectangles(src, &nrects);
816 dest_rects = malloc(nrects * sizeof(*dest_rects));
817 if (!dest_rects)
818 return;
819
820 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
821 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
822 } else {
823 for (i = 0; i < nrects; i++) {
824 switch (transform) {
825 default:
826 case WL_OUTPUT_TRANSFORM_NORMAL:
827 dest_rects[i].x1 = src_rects[i].x1;
828 dest_rects[i].y1 = src_rects[i].y1;
829 dest_rects[i].x2 = src_rects[i].x2;
830 dest_rects[i].y2 = src_rects[i].y2;
831 break;
832 case WL_OUTPUT_TRANSFORM_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200833 dest_rects[i].x1 = src_rects[i].y1;
834 dest_rects[i].y1 = width - src_rects[i].x2;
835 dest_rects[i].x2 = src_rects[i].y2;
836 dest_rects[i].y2 = width - src_rects[i].x1;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500837 break;
838 case WL_OUTPUT_TRANSFORM_180:
839 dest_rects[i].x1 = width - src_rects[i].x2;
840 dest_rects[i].y1 = height - src_rects[i].y2;
841 dest_rects[i].x2 = width - src_rects[i].x1;
842 dest_rects[i].y2 = height - src_rects[i].y1;
843 break;
844 case WL_OUTPUT_TRANSFORM_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200845 dest_rects[i].x1 = height - src_rects[i].y2;
846 dest_rects[i].y1 = src_rects[i].x1;
847 dest_rects[i].x2 = height - src_rects[i].y1;
848 dest_rects[i].y2 = src_rects[i].x2;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500849 break;
850 case WL_OUTPUT_TRANSFORM_FLIPPED:
851 dest_rects[i].x1 = width - src_rects[i].x2;
852 dest_rects[i].y1 = src_rects[i].y1;
853 dest_rects[i].x2 = width - src_rects[i].x1;
854 dest_rects[i].y2 = src_rects[i].y2;
855 break;
856 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200857 dest_rects[i].x1 = src_rects[i].y1;
858 dest_rects[i].y1 = src_rects[i].x1;
859 dest_rects[i].x2 = src_rects[i].y2;
860 dest_rects[i].y2 = src_rects[i].x2;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500861 break;
862 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
863 dest_rects[i].x1 = src_rects[i].x1;
864 dest_rects[i].y1 = height - src_rects[i].y2;
865 dest_rects[i].x2 = src_rects[i].x2;
866 dest_rects[i].y2 = height - src_rects[i].y1;
867 break;
868 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200869 dest_rects[i].x1 = height - src_rects[i].y2;
870 dest_rects[i].y1 = width - src_rects[i].x2;
871 dest_rects[i].x2 = height - src_rects[i].y1;
872 dest_rects[i].y2 = width - src_rects[i].x1;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500873 break;
874 }
875 }
876 }
877
878 if (scale != 1) {
879 for (i = 0; i < nrects; i++) {
880 dest_rects[i].x1 *= scale;
881 dest_rects[i].x2 *= scale;
882 dest_rects[i].y1 *= scale;
883 dest_rects[i].y2 *= scale;
884 }
885 }
886
887 pixman_region32_clear(dest);
888 pixman_region32_init_rects(dest, dest_rects, nrects);
889 free(dest_rects);
890}
891
Jonny Lamb74130762013-11-26 18:19:46 +0100892static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300893viewport_surface_to_buffer(struct weston_surface *surface,
894 float sx, float sy, float *bx, float *by)
Jonny Lamb74130762013-11-26 18:19:46 +0100895{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200896 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200897 double src_width, src_height;
898 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200899
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200900 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
901 if (vp->surface.width == -1) {
902 *bx = sx;
903 *by = sy;
904 return;
905 }
Jonny Lamb74130762013-11-26 18:19:46 +0100906
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200907 src_x = 0.0;
908 src_y = 0.0;
909 src_width = surface->width_from_buffer;
910 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100911 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200912 src_x = wl_fixed_to_double(vp->buffer.src_x);
913 src_y = wl_fixed_to_double(vp->buffer.src_y);
914 src_width = wl_fixed_to_double(vp->buffer.src_width);
915 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100916 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200917
918 *bx = sx * src_width / surface->width + src_x;
919 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100920}
921
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500922WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200923weston_surface_to_buffer_float(struct weston_surface *surface,
924 float sx, float sy, float *bx, float *by)
925{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200926 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
927
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300928 /* first transform coordinates if the viewport is set */
929 viewport_surface_to_buffer(surface, sx, sy, bx, by);
Jonny Lamb74130762013-11-26 18:19:46 +0100930
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500931 weston_transformed_coord(surface->width_from_buffer,
932 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200933 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100934 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200935}
936
Derek Foreman99739672015-12-03 16:38:11 -0600937/** Transform a rectangle from surface coordinates to buffer coordinates
938 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300939 * \param surface The surface to fetch wp_viewport and buffer transformation
Derek Foreman99739672015-12-03 16:38:11 -0600940 * from.
941 * \param rect The rectangle to transform.
942 * \return The transformed rectangle.
943 *
944 * Viewport and buffer transformations can only do translation, scaling,
945 * and rotations in 90-degree steps. Therefore the only loss in the
946 * conversion is coordinate rounding.
947 *
948 * However, some coordinate rounding takes place as an intermediate
949 * step before the buffer scale factor is applied, so the rectangle
950 * boundary may not be exactly as expected.
951 *
952 * This is OK for damage tracking since a little extra coverage is
953 * not a problem.
954 */
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200955WL_EXPORT pixman_box32_t
956weston_surface_to_buffer_rect(struct weston_surface *surface,
957 pixman_box32_t rect)
958{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200959 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100960 float xf, yf;
961
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300962 /* first transform box coordinates if the viewport is set */
963 viewport_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100964 rect.x1 = floorf(xf);
965 rect.y1 = floorf(yf);
966
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300967 viewport_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
Derek Foremane2e15ac2015-12-01 13:00:43 -0600968 rect.x2 = ceilf(xf);
969 rect.y2 = ceilf(yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100970
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500971 return weston_transformed_rect(surface->width_from_buffer,
972 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200973 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200974 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200975}
976
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200977/** Transform a region from surface coordinates to buffer coordinates
978 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300979 * \param surface The surface to fetch wp_viewport and buffer transformation
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200980 * from.
Marius Vlada2dace22019-06-12 16:05:44 +0300981 * \param[in] surface_region The region in surface coordinates.
982 * \param[out] buffer_region The region converted to buffer coordinates.
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200983 *
984 * Buffer_region must be init'd, but will be completely overwritten.
985 *
986 * Viewport and buffer transformations can only do translation, scaling,
987 * and rotations in 90-degree steps. Therefore the only loss in the
Derek Foreman99739672015-12-03 16:38:11 -0600988 * conversion is from the coordinate rounding that takes place in
989 * \ref weston_surface_to_buffer_rect.
Marius Vlada2dace22019-06-12 16:05:44 +0300990 *
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200991 */
992WL_EXPORT void
993weston_surface_to_buffer_region(struct weston_surface *surface,
994 pixman_region32_t *surface_region,
995 pixman_region32_t *buffer_region)
996{
997 pixman_box32_t *src_rects, *dest_rects;
998 int nrects, i;
999
1000 src_rects = pixman_region32_rectangles(surface_region, &nrects);
1001 dest_rects = malloc(nrects * sizeof(*dest_rects));
1002 if (!dest_rects)
1003 return;
1004
1005 for (i = 0; i < nrects; i++) {
1006 dest_rects[i] = weston_surface_to_buffer_rect(surface,
1007 src_rects[i]);
1008 }
1009
1010 pixman_region32_fini(buffer_region);
1011 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
1012 free(dest_rects);
1013}
1014
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +02001015WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001016weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001017 struct weston_plane *plane)
1018{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001019 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001020 return;
1021
Jason Ekstranda7af7042013-10-12 22:38:11 -05001022 weston_view_damage_below(view);
1023 view->plane = plane;
1024 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001025}
1026
Pekka Paalanen51723d52015-02-17 13:10:01 +02001027/** Inflict damage on the plane where the view is visible.
1028 *
1029 * \param view The view that causes the damage.
1030 *
1031 * If the view is currently on a plane (including the primary plane),
1032 * take the view's boundingbox, subtract all the opaque views that cover it,
1033 * and add the remaining region as damage to the plane. This corresponds
1034 * to the damage inflicted to the plane if this view disappeared.
1035 *
1036 * A repaint is scheduled for this view.
1037 *
1038 * The region of all opaque views covering this view is stored in
1039 * weston_view::clip and updated by view_accumulate_damage() during
1040 * weston_output_repaint(). Specifically, that region matches the
1041 * scenegraph as it was last painted.
1042 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001043WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001044weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001045{
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001046 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001047
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001048 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001049 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001050 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +08001051 if (view->plane)
1052 pixman_region32_union(&view->plane->damage,
1053 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001054 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -08001055 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001056}
1057
Pekka Paalanen01e00682017-03-24 16:21:06 +02001058/** Send wl_surface.enter/leave events
1059 *
1060 * \param surface The surface.
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03001061 * \param head A head of the entered/left output.
Pekka Paalanen01e00682017-03-24 16:21:06 +02001062 * \param enter True if entered.
Marius Vlada2dace22019-06-12 16:05:44 +03001063 * \param leave True if left.
Pekka Paalanen01e00682017-03-24 16:21:06 +02001064 *
1065 * Send the enter/leave events for all protocol objects bound to the given
1066 * output by the client owning the surface.
1067 */
1068static void
1069weston_surface_send_enter_leave(struct weston_surface *surface,
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03001070 struct weston_head *head,
Pekka Paalanen01e00682017-03-24 16:21:06 +02001071 bool enter,
1072 bool leave)
1073{
1074 struct wl_resource *wloutput;
1075 struct wl_client *client;
1076
1077 assert(enter != leave);
1078
1079 client = wl_resource_get_client(surface->resource);
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03001080 wl_resource_for_each(wloutput, &head->resource_list) {
Pekka Paalanen01e00682017-03-24 16:21:06 +02001081 if (wl_resource_get_client(wloutput) != client)
1082 continue;
1083
1084 if (enter)
1085 wl_surface_send_enter(surface->resource, wloutput);
1086 if (leave)
1087 wl_surface_send_leave(surface->resource, wloutput);
1088 }
1089}
1090
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301091static void
1092weston_surface_compute_protection(struct protected_surface *psurface)
1093{
1094 enum weston_hdcp_protection min_protection;
1095 bool min_protection_valid = false;
1096 struct weston_surface *surface = psurface->surface;
1097 struct weston_output *output;
1098
1099 wl_list_for_each(output, &surface->compositor->output_list, link)
1100 if (surface->output_mask & (1u << output->id)) {
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05301101 /*
1102 * If the content-protection is enabled with protection
1103 * mode as RELAXED for a surface, and if
1104 * content-recording features like: screen-shooter,
1105 * recorder, screen-sharing, etc are on, then notify the
1106 * client, that the protection is disabled.
1107 *
1108 * Note: If the protection mode is ENFORCED then there
1109 * is no need to bother the client as the renderer takes
1110 * care of censoring the visibility of the protected
1111 * content.
1112 */
1113
1114 if (output->disable_planes > 0 &&
1115 surface->protection_mode == WESTON_SURFACE_PROTECTION_MODE_RELAXED) {
1116 min_protection = WESTON_HDCP_DISABLE;
1117 min_protection_valid = true;
1118 break;
1119 }
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301120 if (!min_protection_valid) {
1121 min_protection = output->current_protection;
1122 min_protection_valid = true;
1123 }
1124 if (output->current_protection < min_protection)
1125 min_protection = output->current_protection;
1126 }
1127 if (!min_protection_valid)
1128 min_protection = WESTON_HDCP_DISABLE;
1129
1130 surface->current_protection = min_protection;
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05301131
1132 weston_protected_surface_send_event(psurface, surface->current_protection);
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301133}
1134
1135static void
1136notify_surface_protection_change(void *data)
1137{
1138 struct weston_compositor *compositor = data;
1139 struct content_protection *cp;
1140 struct protected_surface *psurface;
1141
1142 cp = compositor->content_protection;
1143 cp->surface_protection_update = NULL;
1144
1145 /* Notify the clients, whose surfaces are changed */
1146 wl_list_for_each(psurface, &cp->protected_list, link)
1147 if (psurface && psurface->surface)
1148 weston_surface_compute_protection(psurface);
1149}
1150
Bryce Harrington3f650b82015-12-23 11:01:58 -08001151/**
Ankit Nautiyalfaa5ab42019-07-08 13:23:24 +05301152 * \param compositor weston_compositor
1153 *
1154 * Schedule an idle task to notify surface about the update in protection,
1155 * if not already scheduled.
1156 */
1157static void
1158weston_schedule_surface_protection_update(struct weston_compositor *compositor)
1159{
1160 struct content_protection *cp = compositor->content_protection;
1161 struct wl_event_loop *loop;
1162
1163 if (!cp || cp->surface_protection_update)
1164 return;
1165 loop = wl_display_get_event_loop(compositor->wl_display);
1166 cp->surface_protection_update = wl_event_loop_add_idle(loop,
1167 notify_surface_protection_change,
1168 compositor);
1169}
1170
1171/**
Bryce Harrington3f650b82015-12-23 11:01:58 -08001172 * \param es The surface
1173 * \param mask The new set of outputs for the surface
1174 *
1175 * Sets the surface's set of outputs to the ones specified by
1176 * the new output mask provided. Identifies the outputs that
1177 * have changed, the posts enter and leave events for these
1178 * outputs as appropriate.
1179 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001180static void
1181weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
1182{
1183 uint32_t different = es->output_mask ^ mask;
1184 uint32_t entered = mask & different;
1185 uint32_t left = es->output_mask & different;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001186 uint32_t output_bit;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001187 struct weston_output *output;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001188 struct weston_head *head;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001189
1190 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001191 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001192 return;
1193 if (different == 0)
1194 return;
1195
1196 wl_list_for_each(output, &es->compositor->output_list, link) {
Pekka Paalanen01e00682017-03-24 16:21:06 +02001197 output_bit = 1u << output->id;
1198 if (!(output_bit & different))
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001199 continue;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001200
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001201 wl_list_for_each(head, &output->head_list, output_link) {
1202 weston_surface_send_enter_leave(es, head,
1203 output_bit & entered,
1204 output_bit & left);
1205 }
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001206 }
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301207 /*
1208 * Change in surfaces' output mask might trigger a change in its
1209 * protection.
1210 */
Ankit Nautiyalfaa5ab42019-07-08 13:23:24 +05301211 weston_schedule_surface_protection_update(es->compositor);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001212}
1213
Semi Malinene7a52fb2018-04-26 11:08:10 +02001214static void
1215notify_view_output_destroy(struct wl_listener *listener, void *data)
1216{
1217 struct weston_view *view =
1218 container_of(listener,
1219 struct weston_view, output_destroy_listener);
1220
1221 view->output = NULL;
1222 view->output_destroy_listener.notify = NULL;
1223}
1224
1225/** Set the primary output of the view
1226 *
1227 * \param view The view whose primary output to set
1228 * \param output The new primary output for the view
1229 *
1230 * Set \a output to be the primary output of the \a view.
1231 *
1232 * Notice that the assignment may be temporary; the primary output could be
1233 * automatically changed. Hence, one cannot rely on the value persisting.
1234 *
1235 * Passing NULL as /a output will set the primary output to NULL.
1236 */
1237WL_EXPORT void
1238weston_view_set_output(struct weston_view *view, struct weston_output *output)
1239{
1240 if (view->output_destroy_listener.notify) {
1241 wl_list_remove(&view->output_destroy_listener.link);
1242 view->output_destroy_listener.notify = NULL;
1243 }
1244 view->output = output;
1245 if (output) {
1246 view->output_destroy_listener.notify =
1247 notify_view_output_destroy;
1248 wl_signal_add(&output->destroy_signal,
1249 &view->output_destroy_listener);
1250 }
1251}
1252
Bryce Harrington3f650b82015-12-23 11:01:58 -08001253/** Recalculate which output(s) the surface has views displayed on
1254 *
1255 * \param es The surface to remap to outputs
1256 *
1257 * Finds the output that is showing the largest amount of one
1258 * of the surface's various views. This output becomes the
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001259 * surface's primary output for vsync and frame callback purposes.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001260 *
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001261 * Also notes all outputs of all of the surface's views
Bryce Harrington3f650b82015-12-23 11:01:58 -08001262 * in the output_mask for the surface.
1263 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001264static void
1265weston_surface_assign_output(struct weston_surface *es)
1266{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001267 struct weston_output *new_output;
1268 struct weston_view *view;
1269 pixman_region32_t region;
1270 uint32_t max, area, mask;
1271 pixman_box32_t *e;
1272
1273 new_output = NULL;
1274 max = 0;
1275 mask = 0;
1276 pixman_region32_init(&region);
1277 wl_list_for_each(view, &es->views, surface_link) {
1278 if (!view->output)
1279 continue;
1280
1281 pixman_region32_intersect(&region, &view->transform.boundingbox,
1282 &view->output->region);
1283
1284 e = pixman_region32_extents(&region);
1285 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1286
1287 mask |= view->output_mask;
1288
1289 if (area >= max) {
1290 new_output = view->output;
1291 max = area;
1292 }
1293 }
1294 pixman_region32_fini(&region);
1295
1296 es->output = new_output;
1297 weston_surface_update_output_mask(es, mask);
1298}
1299
Bryce Harrington3f650b82015-12-23 11:01:58 -08001300/** Recalculate which output(s) the view is displayed on
1301 *
1302 * \param ev The view to remap to outputs
1303 *
1304 * Identifies the set of outputs that the view is visible on,
1305 * noting them into the output_mask. The output that the view
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001306 * is most visible on is set as the view's primary output.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001307 *
1308 * Also does the same for the view's surface. See
1309 * weston_surface_assign_output().
1310 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001311static void
1312weston_view_assign_output(struct weston_view *ev)
1313{
1314 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001315 struct weston_output *output, *new_output;
1316 pixman_region32_t region;
1317 uint32_t max, area, mask;
1318 pixman_box32_t *e;
1319
1320 new_output = NULL;
1321 max = 0;
1322 mask = 0;
1323 pixman_region32_init(&region);
1324 wl_list_for_each(output, &ec->output_list, link) {
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03001325 if (output->destroying)
1326 continue;
1327
Jason Ekstranda7af7042013-10-12 22:38:11 -05001328 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001329 &output->region);
1330
1331 e = pixman_region32_extents(&region);
1332 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1333
1334 if (area > 0)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001335 mask |= 1u << output->id;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001336
1337 if (area >= max) {
1338 new_output = output;
1339 max = area;
1340 }
1341 }
1342 pixman_region32_fini(&region);
1343
Semi Malinene7a52fb2018-04-26 11:08:10 +02001344 weston_view_set_output(ev, new_output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001345 ev->output_mask = mask;
1346
1347 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001348}
1349
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001350static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001351weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1352 int from_x, int from_y, int *to_x, int *to_y)
1353{
1354 float x, y;
1355
1356 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1357 weston_view_from_global_float(to, x, y, &x, &y);
1358
1359 *to_x = round(x);
1360 *to_y = round(y);
1361}
1362
1363static void
1364weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1365{
1366 pixman_box32_t *a;
1367 pixman_box32_t b;
1368
1369 a = pixman_region32_extents(&from->geometry.scissor);
1370
1371 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1372 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1373
1374 pixman_region32_fini(&to->geometry.scissor);
1375 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1376}
1377
1378static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001379view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001380 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001381{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001382 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1383 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001384 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001385 { inbox->x1, inbox->y1 },
1386 { inbox->x1, inbox->y2 },
1387 { inbox->x2, inbox->y1 },
1388 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001389 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001390 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001391 int i;
1392
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001393 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001394 /* avoid rounding empty bbox to 1x1 */
1395 pixman_region32_init(bbox);
1396 return;
1397 }
1398
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001399 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001400 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001401 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001402 if (x < min_x)
1403 min_x = x;
1404 if (x > max_x)
1405 max_x = x;
1406 if (y < min_y)
1407 min_y = y;
1408 if (y > max_y)
1409 max_y = y;
1410 }
1411
Pekka Paalanen219b9822012-02-08 15:38:37 +02001412 int_x = floorf(min_x);
1413 int_y = floorf(min_y);
1414 pixman_region32_init_rect(bbox, int_x, int_y,
1415 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001416}
1417
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001418static void
Michael Olbrichcb04f982020-06-08 10:22:32 +02001419weston_view_update_transform_scissor(struct weston_view *view,
1420 pixman_region32_t *region)
1421{
1422 struct weston_view *parent = view->geometry.parent;
1423
1424 if (parent) {
1425 if (parent->geometry.scissor_enabled) {
1426 view->geometry.scissor_enabled = true;
1427 weston_view_transfer_scissor(parent, view);
1428 } else {
1429 view->geometry.scissor_enabled = false;
1430 }
1431 }
1432
1433 if (view->geometry.scissor_enabled)
1434 pixman_region32_intersect(region, region,
1435 &view->geometry.scissor);
1436}
1437static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001438weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001439{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001440 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001441
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001442 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001443 view->geometry.x = roundf(view->geometry.x);
1444 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001445
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001446 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001447 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1448 view->transform.position.matrix.d[12] = view->geometry.x;
1449 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001450
Jason Ekstranda7af7042013-10-12 22:38:11 -05001451 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001452
Jason Ekstranda7af7042013-10-12 22:38:11 -05001453 view->transform.inverse = view->transform.position.matrix;
1454 view->transform.inverse.d[12] = -view->geometry.x;
1455 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001456
Jason Ekstranda7af7042013-10-12 22:38:11 -05001457 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001458 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001459 view->surface->width,
1460 view->surface->height);
Michael Olbrichcb04f982020-06-08 10:22:32 +02001461
1462 weston_view_update_transform_scissor(view, &view->transform.boundingbox);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001463
1464 pixman_region32_translate(&view->transform.boundingbox,
1465 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001466
Jason Ekstranda7af7042013-10-12 22:38:11 -05001467 if (view->alpha == 1.0) {
1468 pixman_region32_copy(&view->transform.opaque,
1469 &view->surface->opaque);
Michael Olbrich0e4f0972020-08-19 11:31:03 +02001470 pixman_region32_intersect(&view->transform.opaque,
1471 &view->transform.opaque,
1472 &view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001473 pixman_region32_translate(&view->transform.opaque,
1474 view->geometry.x,
1475 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001476 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001477}
1478
1479static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001480weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001481{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001482 struct weston_view *parent = view->geometry.parent;
1483 struct weston_matrix *matrix = &view->transform.matrix;
1484 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001485 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001486 pixman_region32_t surfregion;
1487 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001488
Jason Ekstranda7af7042013-10-12 22:38:11 -05001489 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001490
1491 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001492 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1493 view->transform.position.matrix.d[12] = view->geometry.x;
1494 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001495
1496 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001497 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001498 weston_matrix_multiply(matrix, &tform->matrix);
1499
Pekka Paalanen483243f2013-03-08 14:56:50 +02001500 if (parent)
1501 weston_matrix_multiply(matrix, &parent->transform.matrix);
1502
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001503 if (weston_matrix_invert(inverse, matrix) < 0) {
1504 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001505 weston_log("error: weston_view %p"
1506 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001507 return -1;
1508 }
1509
Pekka Paalanen380adf52015-02-16 14:39:11 +02001510 pixman_region32_init_rect(&surfregion, 0, 0,
1511 view->surface->width, view->surface->height);
Michael Olbrichcb04f982020-06-08 10:22:32 +02001512
1513 weston_view_update_transform_scissor(view, &surfregion);
1514
Pekka Paalanen380adf52015-02-16 14:39:11 +02001515 surfbox = pixman_region32_extents(&surfregion);
1516
1517 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1518 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001519
Michael Olbrich0e4f0972020-08-19 11:31:03 +02001520 if (view->alpha == 1.0 &&
1521 matrix->type == WESTON_MATRIX_TRANSFORM_TRANSLATE) {
1522 pixman_region32_copy(&view->transform.opaque,
1523 &view->surface->opaque);
1524 pixman_region32_intersect(&view->transform.opaque,
1525 &view->transform.opaque,
1526 &view->geometry.scissor);
1527 pixman_region32_translate(&view->transform.opaque,
1528 matrix->d[12],
1529 matrix->d[13]);
1530 }
1531
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001532 return 0;
1533}
1534
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001535static struct weston_layer *
1536get_view_layer(struct weston_view *view)
1537{
1538 if (view->parent_view)
1539 return get_view_layer(view->parent_view);
1540 return view->layer_link.layer;
1541}
1542
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001543WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001544weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001545{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001546 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001547 struct weston_layer *layer;
1548 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001549
Jason Ekstranda7af7042013-10-12 22:38:11 -05001550 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001551 return;
1552
Pekka Paalanen483243f2013-03-08 14:56:50 +02001553 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001554 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001555
Jason Ekstranda7af7042013-10-12 22:38:11 -05001556 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001557
Jason Ekstranda7af7042013-10-12 22:38:11 -05001558 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001559
Jason Ekstranda7af7042013-10-12 22:38:11 -05001560 pixman_region32_fini(&view->transform.boundingbox);
1561 pixman_region32_fini(&view->transform.opaque);
1562 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001563
Pekka Paalanencd403622012-01-25 13:37:39 +02001564 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001565 if (view->geometry.transformation_list.next ==
1566 &view->transform.position.link &&
1567 view->geometry.transformation_list.prev ==
1568 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001569 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001570 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001571 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001572 if (weston_view_update_transform_enable(view) < 0)
1573 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001574 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001575
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001576 layer = get_view_layer(view);
1577 if (layer) {
1578 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001579 pixman_region32_intersect(&view->transform.boundingbox,
1580 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001581 pixman_region32_intersect(&view->transform.opaque,
1582 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001583 pixman_region32_fini(&mask);
1584 }
1585
Jason Ekstranda7af7042013-10-12 22:38:11 -05001586 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001587
Jason Ekstranda7af7042013-10-12 22:38:11 -05001588 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001589
Jason Ekstranda7af7042013-10-12 22:38:11 -05001590 wl_signal_emit(&view->surface->compositor->transform_signal,
1591 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001592}
1593
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001594WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001595weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001596{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001597 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001598
1599 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001600 * The invariant: if view->geometry.dirty, then all views
1601 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001602 * Corollary: if not parent->geometry.dirty, then all ancestors
1603 * are not dirty.
1604 */
1605
Jason Ekstranda7af7042013-10-12 22:38:11 -05001606 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001607 return;
1608
Jason Ekstranda7af7042013-10-12 22:38:11 -05001609 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001610
Jason Ekstranda7af7042013-10-12 22:38:11 -05001611 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001612 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001613 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001614}
1615
1616WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001617weston_view_to_global_fixed(struct weston_view *view,
1618 wl_fixed_t vx, wl_fixed_t vy,
1619 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001620{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001621 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001622
Jason Ekstranda7af7042013-10-12 22:38:11 -05001623 weston_view_to_global_float(view,
1624 wl_fixed_to_double(vx),
1625 wl_fixed_to_double(vy),
1626 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001627 *x = wl_fixed_from_double(xf);
1628 *y = wl_fixed_from_double(yf);
1629}
1630
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001631WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001632weston_view_from_global_float(struct weston_view *view,
1633 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001634{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001635 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001636 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1637
Jason Ekstranda7af7042013-10-12 22:38:11 -05001638 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001639
1640 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001641 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001642 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001643 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001644 *vx = 0;
1645 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001646 return;
1647 }
1648
Jason Ekstranda7af7042013-10-12 22:38:11 -05001649 *vx = v.f[0] / v.f[3];
1650 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001651 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001652 *vx = x - view->geometry.x;
1653 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001654 }
1655}
1656
1657WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001658weston_view_from_global_fixed(struct weston_view *view,
1659 wl_fixed_t x, wl_fixed_t y,
1660 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001661{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001662 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001663
Jason Ekstranda7af7042013-10-12 22:38:11 -05001664 weston_view_from_global_float(view,
1665 wl_fixed_to_double(x),
1666 wl_fixed_to_double(y),
1667 &vxf, &vyf);
1668 *vx = wl_fixed_from_double(vxf);
1669 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001670}
1671
1672WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001673weston_view_from_global(struct weston_view *view,
1674 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001675{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001676 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001677
Jason Ekstranda7af7042013-10-12 22:38:11 -05001678 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1679 *vx = floorf(vxf);
1680 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001681}
1682
Bryce Harrington3f650b82015-12-23 11:01:58 -08001683/**
1684 * \param surface The surface to be repainted
1685 *
1686 * Marks the output(s) that the surface is shown on as needing to be
1687 * repainted. See weston_output_schedule_repaint().
1688 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001689WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001690weston_surface_schedule_repaint(struct weston_surface *surface)
1691{
1692 struct weston_output *output;
1693
1694 wl_list_for_each(output, &surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001695 if (surface->output_mask & (1u << output->id))
Kristian Høgsberg98238702012-08-03 16:29:12 -04001696 weston_output_schedule_repaint(output);
1697}
1698
Bryce Harrington3f650b82015-12-23 11:01:58 -08001699/**
1700 * \param view The view to be repainted
1701 *
1702 * Marks the output(s) that the view is shown on as needing to be
1703 * repainted. See weston_output_schedule_repaint().
1704 */
Kristian Høgsberg98238702012-08-03 16:29:12 -04001705WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001706weston_view_schedule_repaint(struct weston_view *view)
1707{
1708 struct weston_output *output;
1709
1710 wl_list_for_each(output, &view->surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001711 if (view->output_mask & (1u << output->id))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001712 weston_output_schedule_repaint(output);
1713}
1714
Pekka Paalanene508ce62015-02-19 13:59:55 +02001715/**
1716 * XXX: This function does it the wrong way.
1717 * surface->damage is the damage from the client, and causes
1718 * surface_flush_damage() to copy pixels. No window management action can
1719 * cause damage to the client-provided content, warranting re-upload!
1720 *
1721 * Instead of surface->damage, this function should record the damage
1722 * with all the views for this surface to avoid extraneous texture
1723 * uploads.
1724 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001725WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001726weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001727{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001728 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001729 0, 0, surface->width,
1730 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001731
Kristian Høgsberg98238702012-08-03 16:29:12 -04001732 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001733}
1734
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001735WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001736weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001737{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001738 if (view->geometry.x == x && view->geometry.y == y)
1739 return;
1740
Jason Ekstranda7af7042013-10-12 22:38:11 -05001741 view->geometry.x = x;
1742 view->geometry.y = y;
1743 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001744}
1745
Pekka Paalanen483243f2013-03-08 14:56:50 +02001746static void
1747transform_parent_handle_parent_destroy(struct wl_listener *listener,
1748 void *data)
1749{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001750 struct weston_view *view =
1751 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001752 geometry.parent_destroy_listener);
1753
Jason Ekstranda7af7042013-10-12 22:38:11 -05001754 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001755}
1756
1757WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001758weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001759 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001760{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001761 if (view->geometry.parent) {
1762 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1763 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001764
1765 if (!parent)
1766 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001767 }
1768
Jason Ekstranda7af7042013-10-12 22:38:11 -05001769 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001770
Jason Ekstranda7af7042013-10-12 22:38:11 -05001771 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001772 transform_parent_handle_parent_destroy;
1773 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001774 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001775 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001776 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001777 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001778 }
1779
Jason Ekstranda7af7042013-10-12 22:38:11 -05001780 weston_view_geometry_dirty(view);
1781}
1782
Pekka Paalanen380adf52015-02-16 14:39:11 +02001783/** Set a clip mask rectangle on a view
1784 *
1785 * \param view The view to set the clip mask on.
1786 * \param x Top-left corner X coordinate of the clip rectangle.
1787 * \param y Top-left corner Y coordinate of the clip rectangle.
1788 * \param width Width of the clip rectangle, non-negative.
1789 * \param height Height of the clip rectangle, non-negative.
1790 *
1791 * A shell may set a clip mask rectangle on a view. Everything outside
1792 * the rectangle is cut away for input and output purposes: it is
1793 * not drawn and cannot be hit by hit-test based input like pointer
1794 * motion or touch-downs. Everything inside the rectangle will behave
1795 * normally. Clients are unaware of clipping.
1796 *
Yong Bakos4c72e292016-04-28 11:59:10 -05001797 * The rectangle is set in surface-local coordinates. Setting a clip
Pekka Paalanen380adf52015-02-16 14:39:11 +02001798 * mask rectangle does not affect the view position, the view is positioned
1799 * as it would be without a clip. The clip also does not change
1800 * weston_surface::width,height.
1801 *
1802 * The clip mask rectangle is part of transformation inheritance
1803 * (weston_view_set_transform_parent()). A clip set in the root of the
1804 * transformation inheritance tree will affect all views in the tree.
1805 * A clip can be set only on the root view. Attempting to set a clip
1806 * on view that has a transformation parent will fail. Assigning a parent
1807 * to a view that has a clip set will cause the clip to be forgotten.
1808 *
1809 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1810 * on the additional transformations in the child views. These transformations
1811 * may not rotate the coordinate axes, i.e., only translation and scaling
1812 * are allowed. Violating this restriction causes the clipping to malfunction.
1813 * Furthermore, using scaling may cause rounding errors in child clipping.
1814 *
1815 * The clip mask rectangle is not automatically adjusted based on
1816 * wl_surface.attach dx and dy arguments.
1817 *
1818 * A clip mask rectangle can be set only if the compositor capability
1819 * WESTON_CAP_VIEW_CLIP_MASK is present.
1820 *
1821 * This function sets the clip mask rectangle and schedules a repaint for
1822 * the view.
1823 */
1824WL_EXPORT void
1825weston_view_set_mask(struct weston_view *view,
1826 int x, int y, int width, int height)
1827{
1828 struct weston_compositor *compositor = view->surface->compositor;
1829
1830 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1831 weston_log("%s not allowed without capability!\n", __func__);
1832 return;
1833 }
1834
1835 if (view->geometry.parent) {
1836 weston_log("view %p has a parent, clip forbidden!\n", view);
1837 return;
1838 }
1839
1840 if (width < 0 || height < 0) {
1841 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1842 x, y, width, height);
1843 return;
1844 }
1845
1846 pixman_region32_fini(&view->geometry.scissor);
1847 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1848 view->geometry.scissor_enabled = true;
1849 weston_view_geometry_dirty(view);
1850 weston_view_schedule_repaint(view);
1851}
1852
1853/** Remove the clip mask from a view
1854 *
1855 * \param view The view to remove the clip mask from.
1856 *
1857 * Removed the clip mask rectangle and schedules a repaint.
1858 *
1859 * \sa weston_view_set_mask
1860 */
1861WL_EXPORT void
1862weston_view_set_mask_infinite(struct weston_view *view)
1863{
1864 view->geometry.scissor_enabled = false;
1865 weston_view_geometry_dirty(view);
1866 weston_view_schedule_repaint(view);
1867}
1868
Armin Krezović0da12b82016-06-30 06:04:33 +02001869/* Check if view should be displayed
1870 *
1871 * The indicator is set manually when assigning
1872 * a view to a surface.
1873 *
1874 * This needs reworking. See the thread starting at:
1875 *
1876 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1877 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001878WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001879weston_view_is_mapped(struct weston_view *view)
1880{
Armin Krezović0da12b82016-06-30 06:04:33 +02001881 return view->is_mapped;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001882}
1883
Philipp Zabel70decd52018-09-03 20:11:15 +02001884/* Check if view is opaque in specified region
1885 *
1886 * \param view The view to check for opacity.
1887 * \param region The region to check for opacity, in view coordinates.
1888 *
1889 * Returns true if the view is opaque in the specified region, because view
1890 * alpha is 1.0 and either the opaque region set by the client contains the
1891 * specified region, or the buffer pixel format or solid color is opaque.
1892 */
1893WL_EXPORT bool
1894weston_view_is_opaque(struct weston_view *ev, pixman_region32_t *region)
1895{
1896 pixman_region32_t r;
1897 bool ret = false;
1898
1899 if (ev->alpha < 1.0)
1900 return false;
1901
1902 if (ev->surface->is_opaque)
1903 return true;
1904
Marius Vlad67b382c2020-08-21 13:57:54 +03001905 if (ev->transform.dirty)
Philipp Zabel70decd52018-09-03 20:11:15 +02001906 return false;
Philipp Zabel70decd52018-09-03 20:11:15 +02001907
1908 pixman_region32_init(&r);
1909 pixman_region32_subtract(&r, region, &ev->transform.opaque);
1910
1911 if (!pixman_region32_not_empty(&r))
1912 ret = true;
1913
1914 pixman_region32_fini(&r);
1915
1916 return ret;
1917}
1918
Marius Vlad5f6bee42019-09-11 16:41:04 +03001919/** Check if the view has a valid buffer available
1920 *
1921 * @param ev The view to check if it has a valid buffer.
1922 *
1923 * Returns true if the view has a valid buffer or false otherwise.
1924 */
1925WL_EXPORT bool
1926weston_view_has_valid_buffer(struct weston_view *ev)
1927{
1928 return ev->surface->buffer_ref.buffer != NULL;
1929}
1930
Marius Vlad47e3d1e2019-09-11 16:53:08 +03001931/** Check if the view matches the entire output
1932 *
1933 * @param ev The view to check.
1934 * @param output The output to check against.
1935 *
1936 * Returns true if the view does indeed matches the entire output.
1937 */
1938WL_EXPORT bool
1939weston_view_matches_output_entirely(struct weston_view *ev,
1940 struct weston_output *output)
1941{
1942 pixman_box32_t *extents =
1943 pixman_region32_extents(&ev->transform.boundingbox);
1944
1945 if (extents->x1 != output->x ||
1946 extents->y1 != output->y ||
1947 extents->x2 != output->x + output->width ||
1948 extents->y2 != output->y + output->height)
1949 return false;
1950
1951 return true;
1952}
1953
Armin Krezović0da12b82016-06-30 06:04:33 +02001954/* Check if a surface has a view assigned to it
1955 *
1956 * The indicator is set manually when mapping
1957 * a surface and creating a view for it.
1958 *
1959 * This needs to go. See the thread starting at:
1960 *
1961 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1962 *
1963 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001964WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001965weston_surface_is_mapped(struct weston_surface *surface)
1966{
Armin Krezović0da12b82016-06-30 06:04:33 +02001967 return surface->is_mapped;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001968}
1969
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001970static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001971surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001972{
1973 struct weston_view *view;
1974
1975 if (surface->width == width && surface->height == height)
1976 return;
1977
1978 surface->width = width;
1979 surface->height = height;
1980
1981 wl_list_for_each(view, &surface->views, surface_link)
1982 weston_view_geometry_dirty(view);
1983}
1984
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001985WL_EXPORT void
1986weston_surface_set_size(struct weston_surface *surface,
1987 int32_t width, int32_t height)
1988{
1989 assert(!surface->resource);
1990 surface_set_size(surface, width, height);
1991}
1992
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001993static int
1994fixed_round_up_to_int(wl_fixed_t f)
1995{
1996 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1997}
1998
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001999static void
Pekka Paalanen59987fa2016-04-26 15:50:59 +03002000convert_size_by_transform_scale(int32_t *width_out, int32_t *height_out,
2001 int32_t width, int32_t height,
2002 uint32_t transform,
2003 int32_t scale)
2004{
2005 assert(scale > 0);
2006
2007 switch (transform) {
2008 case WL_OUTPUT_TRANSFORM_NORMAL:
2009 case WL_OUTPUT_TRANSFORM_180:
2010 case WL_OUTPUT_TRANSFORM_FLIPPED:
2011 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2012 *width_out = width / scale;
2013 *height_out = height / scale;
2014 break;
2015 case WL_OUTPUT_TRANSFORM_90:
2016 case WL_OUTPUT_TRANSFORM_270:
2017 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2018 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2019 *width_out = height / scale;
2020 *height_out = width / scale;
2021 break;
2022 default:
2023 assert(0 && "invalid transform");
2024 }
2025}
2026
2027static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002028weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002029{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002030 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01002031
2032 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02002033 surface->width_from_buffer = 0;
2034 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01002035 return;
2036 }
2037
Pekka Paalanen59987fa2016-04-26 15:50:59 +03002038 convert_size_by_transform_scale(&surface->width_from_buffer,
2039 &surface->height_from_buffer,
2040 surface->buffer_ref.buffer->width,
2041 surface->buffer_ref.buffer->height,
2042 vp->buffer.transform,
2043 vp->buffer.scale);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002044}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02002045
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002046static void
2047weston_surface_update_size(struct weston_surface *surface)
2048{
2049 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
2050 int32_t width, height;
2051
2052 width = surface->width_from_buffer;
2053 height = surface->height_from_buffer;
2054
2055 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02002056 surface_set_size(surface,
2057 vp->surface.width, vp->surface.height);
2058 return;
2059 }
2060
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002061 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03002062 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
2063 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
2064
2065 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02002066 return;
2067 }
2068
Jason Ekstrand5c11a332013-12-04 20:32:03 -06002069 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002070}
2071
Marius Vlad9fdda7f2019-06-11 16:08:55 +03002072/** weston_compositor_get_time
2073 * \ingroup compositor
2074 */
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02002075WL_EXPORT void
2076weston_compositor_get_time(struct timespec *time)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05002077{
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02002078 clock_gettime(CLOCK_REALTIME, time);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05002079}
2080
Marius Vlad9fdda7f2019-06-11 16:08:55 +03002081/** weston_compositor_pick_view
2082 * \ingroup compositor
2083 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002084WL_EXPORT struct weston_view *
2085weston_compositor_pick_view(struct weston_compositor *compositor,
2086 wl_fixed_t x, wl_fixed_t y,
2087 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02002088{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002089 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02002090 wl_fixed_t view_x, view_y;
2091 int view_ix, view_iy;
2092 int ix = wl_fixed_to_int(x);
2093 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02002094
Jason Ekstranda7af7042013-10-12 22:38:11 -05002095 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02002096 if (!pixman_region32_contains_point(
2097 &view->transform.boundingbox, ix, iy, NULL))
2098 continue;
2099
2100 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
2101 view_ix = wl_fixed_to_int(view_x);
2102 view_iy = wl_fixed_to_int(view_y);
2103
2104 if (!pixman_region32_contains_point(&view->surface->input,
2105 view_ix, view_iy, NULL))
2106 continue;
2107
Pekka Paalanen380adf52015-02-16 14:39:11 +02002108 if (view->geometry.scissor_enabled &&
2109 !pixman_region32_contains_point(&view->geometry.scissor,
2110 view_ix, view_iy, NULL))
2111 continue;
2112
Pekka Paalanenfc22a522015-02-18 15:08:29 +02002113 *vx = view_x;
2114 *vy = view_y;
2115 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02002116 }
2117
Derek Foremanf9318d12015-05-11 15:40:11 -05002118 *vx = wl_fixed_from_int(-1000000);
2119 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02002120 return NULL;
2121}
2122
2123static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002124weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002125{
Daniel Stone37816df2012-05-16 18:45:18 +01002126 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002127
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07002128 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002129 return;
2130
Daniel Stone37816df2012-05-16 18:45:18 +01002131 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04002132 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002133}
2134
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002135WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002136weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05002137{
Daniel Stone4dab5db2012-05-30 16:31:53 +01002138 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002139
Jason Ekstranda7af7042013-10-12 22:38:11 -05002140 if (!weston_view_is_mapped(view))
2141 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002142
Jason Ekstranda7af7042013-10-12 22:38:11 -05002143 weston_view_damage_below(view);
Semi Malinene7a52fb2018-04-26 11:08:10 +02002144 weston_view_set_output(view, NULL);
Xiong Zhang97116532013-10-23 13:58:31 +08002145 view->plane = NULL;
Armin Krezovićf8486c32016-06-30 06:04:28 +02002146 view->is_mapped = false;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002147 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002148 wl_list_remove(&view->link);
2149 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002150 view->output_mask = 0;
2151 weston_surface_assign_output(view->surface);
2152
2153 if (weston_surface_is_mapped(view->surface))
2154 return;
2155
2156 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002157 struct weston_touch *touch = weston_seat_get_touch(seat);
2158 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2159 struct weston_keyboard *keyboard =
2160 weston_seat_get_keyboard(seat);
2161
2162 if (keyboard && keyboard->focus == view->surface)
2163 weston_keyboard_set_focus(keyboard, NULL);
2164 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05002165 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05002166 if (touch && touch->focus == view)
2167 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01002168 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002169}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002170
Jason Ekstranda7af7042013-10-12 22:38:11 -05002171WL_EXPORT void
2172weston_surface_unmap(struct weston_surface *surface)
2173{
2174 struct weston_view *view;
2175
Armin Krezovićf8486c32016-06-30 06:04:28 +02002176 surface->is_mapped = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002177 wl_list_for_each(view, &surface->views, surface_link)
2178 weston_view_unmap(view);
2179 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05002180}
2181
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002182static void
2183weston_surface_reset_pending_buffer(struct weston_surface *surface)
2184{
Jason Ekstrand7b982072014-05-20 14:33:03 -05002185 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002186 surface->pending.sx = 0;
2187 surface->pending.sy = 0;
2188 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002189 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002190}
2191
Jason Ekstranda7af7042013-10-12 22:38:11 -05002192WL_EXPORT void
2193weston_view_destroy(struct weston_view *view)
2194{
2195 wl_signal_emit(&view->destroy_signal, view);
2196
2197 assert(wl_list_empty(&view->geometry.child_list));
2198
2199 if (weston_view_is_mapped(view)) {
2200 weston_view_unmap(view);
2201 weston_compositor_build_view_list(view->surface->compositor);
2202 }
2203
2204 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002205 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002206
2207 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02002208 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002209 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002210 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002211
2212 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen944fae82018-05-22 13:15:58 +03002213 weston_view_set_output(view, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002214
Jason Ekstranda7af7042013-10-12 22:38:11 -05002215 wl_list_remove(&view->surface_link);
2216
2217 free(view);
2218}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002219
2220WL_EXPORT void
2221weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05002222{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002223 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002224 struct weston_view *ev, *nv;
Jonas Ådahld3414f22016-07-22 17:56:31 +08002225 struct weston_pointer_constraint *constraint, *next_constraint;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04002226
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02002227 if (--surface->ref_count > 0)
2228 return;
2229
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03002230 assert(surface->resource == NULL);
2231
Pekka Paalanenca790762015-04-17 14:23:38 +03002232 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02002233
Pekka Paalanene67858b2013-04-25 13:57:42 +03002234 assert(wl_list_empty(&surface->subsurface_list_pending));
2235 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02002236
Jason Ekstranda7af7042013-10-12 22:38:11 -05002237 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
2238 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002239
Jason Ekstrand7b982072014-05-20 14:33:03 -05002240 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002241
Pekka Paalanende685b82012-12-04 15:58:12 +02002242 weston_buffer_reference(&surface->buffer_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002243 weston_buffer_release_reference(&surface->buffer_release_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04002244
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02002245 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002246 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002247 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02002248
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002249 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002250 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002251
Pekka Paalanen133e4392014-09-23 22:08:46 -04002252 weston_presentation_feedback_discard_list(&surface->feedback_list);
2253
Jonas Ådahld3414f22016-07-22 17:56:31 +08002254 wl_list_for_each_safe(constraint, next_constraint,
2255 &surface->pointer_constraints,
2256 link)
2257 weston_pointer_constraint_destroy(constraint);
2258
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03002259 fd_clear(&surface->acquire_fence_fd);
2260
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04002261 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05002262}
2263
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002264static void
2265destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08002266{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002267 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08002268
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03002269 assert(surface);
2270
Giulio Camuffo0d379742013-11-15 22:06:15 +01002271 /* Set the resource to NULL, since we don't want to leave a
2272 * dangling pointer if the surface was refcounted and survives
2273 * the weston_surface_destroy() call. */
2274 surface->resource = NULL;
Pekka Paalanen4826f872016-04-22 14:14:38 +03002275
2276 if (surface->viewport_resource)
2277 wl_resource_set_user_data(surface->viewport_resource, NULL);
2278
Alexandros Frantzis27d7c392018-10-19 12:14:11 +03002279 if (surface->synchronization_resource) {
2280 wl_resource_set_user_data(surface->synchronization_resource,
2281 NULL);
2282 }
2283
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002284 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08002285}
2286
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002287static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002288weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
2289{
2290 struct weston_buffer *buffer =
2291 container_of(listener, struct weston_buffer, destroy_listener);
2292
2293 wl_signal_emit(&buffer->destroy_signal, buffer);
2294 free(buffer);
2295}
2296
Giulio Camuffoe058cd12013-12-12 14:14:29 +01002297WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002298weston_buffer_from_resource(struct wl_resource *resource)
2299{
2300 struct weston_buffer *buffer;
2301 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002302
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002303 listener = wl_resource_get_destroy_listener(resource,
2304 weston_buffer_destroy_handler);
2305
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002306 if (listener)
2307 return container_of(listener, struct weston_buffer,
2308 destroy_listener);
2309
2310 buffer = zalloc(sizeof *buffer);
2311 if (buffer == NULL)
2312 return NULL;
2313
2314 buffer->resource = resource;
2315 wl_signal_init(&buffer->destroy_signal);
2316 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04002317 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002318 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002319
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002320 return buffer;
2321}
2322
2323static void
Pekka Paalanende685b82012-12-04 15:58:12 +02002324weston_buffer_reference_handle_destroy(struct wl_listener *listener,
2325 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002326{
Pekka Paalanende685b82012-12-04 15:58:12 +02002327 struct weston_buffer_reference *ref =
2328 container_of(listener, struct weston_buffer_reference,
2329 destroy_listener);
2330
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002331 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02002332 ref->buffer = NULL;
2333}
2334
2335WL_EXPORT void
2336weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002337 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002338{
2339 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05002340 ref->buffer->busy_count--;
2341 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002342 assert(wl_resource_get_client(ref->buffer->resource));
Matt Hoosier3052bc72017-09-26 08:09:40 -05002343 wl_buffer_send_release(ref->buffer->resource);
Kristian Høgsberg20347802013-03-04 12:07:46 -05002344 }
Pekka Paalanende685b82012-12-04 15:58:12 +02002345 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002346 }
2347
Pekka Paalanende685b82012-12-04 15:58:12 +02002348 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002349 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002350 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02002351 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02002352 }
2353
Pekka Paalanende685b82012-12-04 15:58:12 +02002354 ref->buffer = buffer;
2355 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
2356}
2357
2358static void
Alexandros Frantzis67629672018-10-19 12:14:11 +03002359weston_buffer_release_reference_handle_destroy(struct wl_listener *listener,
2360 void *data)
2361{
2362 struct weston_buffer_release_reference *ref =
2363 container_of(listener, struct weston_buffer_release_reference,
2364 destroy_listener);
2365
2366 assert((struct wl_resource *)data == ref->buffer_release->resource);
2367 ref->buffer_release = NULL;
2368}
2369
2370static void
2371weston_buffer_release_destroy(struct weston_buffer_release *buffer_release)
2372{
2373 struct wl_resource *resource = buffer_release->resource;
2374 int release_fence_fd = buffer_release->fence_fd;
2375
2376 if (release_fence_fd >= 0) {
2377 zwp_linux_buffer_release_v1_send_fenced_release(
2378 resource, release_fence_fd);
2379 } else {
2380 zwp_linux_buffer_release_v1_send_immediate_release(
2381 resource);
2382 }
2383
2384 wl_resource_destroy(resource);
2385}
2386
2387WL_EXPORT void
2388weston_buffer_release_reference(struct weston_buffer_release_reference *ref,
2389 struct weston_buffer_release *buffer_release)
2390{
2391 if (buffer_release == ref->buffer_release)
2392 return;
2393
2394 if (ref->buffer_release) {
2395 ref->buffer_release->ref_count--;
2396 wl_list_remove(&ref->destroy_listener.link);
2397 if (ref->buffer_release->ref_count == 0)
2398 weston_buffer_release_destroy(ref->buffer_release);
2399 }
2400
2401 if (buffer_release) {
2402 buffer_release->ref_count++;
2403 wl_resource_add_destroy_listener(buffer_release->resource,
2404 &ref->destroy_listener);
2405 }
2406
2407 ref->buffer_release = buffer_release;
2408 ref->destroy_listener.notify =
2409 weston_buffer_release_reference_handle_destroy;
2410}
2411
2412WL_EXPORT void
2413weston_buffer_release_move(struct weston_buffer_release_reference *dest,
2414 struct weston_buffer_release_reference *src)
2415{
2416 weston_buffer_release_reference(dest, src->buffer_release);
2417 weston_buffer_release_reference(src, NULL);
2418}
2419
2420static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002421weston_surface_attach(struct weston_surface *surface,
2422 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002423{
2424 weston_buffer_reference(&surface->buffer_ref, buffer);
2425
Pekka Paalanena6421c42012-12-04 15:58:10 +02002426 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002427 if (weston_surface_is_mapped(surface))
2428 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002429 }
2430
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002431 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02002432
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002433 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002434 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002435}
2436
Marius Vlad9fdda7f2019-06-11 16:08:55 +03002437/** weston_compositor_damage_all
2438 * \ingroup compositor
2439 */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002440WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002441weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002442{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002443 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002444
2445 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002446 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002447}
2448
Marius Vlad55d87362019-06-11 01:15:35 +03002449/**
2450 * \ingroup output
2451 */
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05002452WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002453weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002454{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002455 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002456
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002457 pixman_region32_union(&compositor->primary_plane.damage,
2458 &compositor->primary_plane.damage,
2459 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002460 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002461}
2462
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04002463static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002464surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002465{
Pekka Paalanende685b82012-12-04 15:58:12 +02002466 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002467 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002468 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002469
Marius Vlad2a1b7862019-09-05 13:12:18 +03002470 if (pixman_region32_not_empty(&surface->damage))
Marius Vlad3203ff62019-09-05 14:56:12 +03002471 TL_POINT(surface->compositor, "core_flush_damage", TLP_SURFACE(surface),
Pekka Paalanenb5026542014-11-12 15:09:24 +02002472 TLP_OUTPUT(surface->output), TLP_END);
2473
Jason Ekstrandef540082014-06-26 10:37:36 -07002474 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002475}
2476
2477static void
2478view_accumulate_damage(struct weston_view *view,
2479 pixman_region32_t *opaque)
2480{
2481 pixman_region32_t damage;
2482
2483 pixman_region32_init(&damage);
2484 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002485 pixman_box32_t *extents;
2486
Jason Ekstranda7af7042013-10-12 22:38:11 -05002487 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002488 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002489 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002490 pixman_region32_copy(&damage, &view->surface->damage);
2491 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002492 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002493 }
2494
Pekka Paalanen380adf52015-02-16 14:39:11 +02002495 pixman_region32_intersect(&damage, &damage,
2496 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002497 pixman_region32_subtract(&damage, &damage, opaque);
2498 pixman_region32_union(&view->plane->damage,
2499 &view->plane->damage, &damage);
2500 pixman_region32_fini(&damage);
2501 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002502 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002503}
2504
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002505static void
Michael Olbrichc5ea4952020-08-11 12:42:35 +02002506output_accumulate_damage(struct weston_output *output)
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002507{
Michael Olbrichc5ea4952020-08-11 12:42:35 +02002508 struct weston_compositor *ec = output->compositor;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002509 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002510 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002511 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002512
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002513 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002514
2515 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002516 pixman_region32_copy(&plane->clip, &clip);
2517
2518 pixman_region32_init(&opaque);
2519
Jason Ekstranda7af7042013-10-12 22:38:11 -05002520 wl_list_for_each(ev, &ec->view_list, link) {
2521 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002522 continue;
2523
Jason Ekstranda7af7042013-10-12 22:38:11 -05002524 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002525 }
2526
2527 pixman_region32_union(&clip, &clip, &opaque);
2528 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002529 }
2530
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002531 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002532
Jason Ekstranda7af7042013-10-12 22:38:11 -05002533 wl_list_for_each(ev, &ec->view_list, link)
Derek Foreman060cf112015-11-18 16:32:26 -06002534 ev->surface->touched = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002535
2536 wl_list_for_each(ev, &ec->view_list, link) {
Michael Olbrichc5ea4952020-08-11 12:42:35 +02002537 /* Ignore views not visible on the current output */
2538 if (!(ev->output_mask & (1u << output->id)))
2539 continue;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002540 if (ev->surface->touched)
2541 continue;
Derek Foreman060cf112015-11-18 16:32:26 -06002542 ev->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002543
2544 surface_flush_damage(ev->surface);
2545
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002546 /* Both the renderer and the backend have seen the buffer
2547 * by now. If renderer needs the buffer, it has its own
2548 * reference set. If the backend wants to keep the buffer
2549 * around for migrating the surface into a non-primary plane
2550 * later, keep_buffer is true. Otherwise, drop the core
2551 * reference now, and allow early buffer release. This enables
2552 * clients to use single-buffering.
2553 */
Alexandros Frantzis67629672018-10-19 12:14:11 +03002554 if (!ev->surface->keep_buffer) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002555 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002556 weston_buffer_release_reference(
2557 &ev->surface->buffer_release_ref, NULL);
2558 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002559 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002560}
2561
2562static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002563surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002564{
2565 struct weston_subsurface *sub;
2566
Pekka Paalanene67858b2013-04-25 13:57:42 +03002567 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002568 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002569 continue;
2570
Jason Ekstranda7af7042013-10-12 22:38:11 -05002571 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2572 wl_list_init(&sub->surface->views);
2573
2574 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002575 }
2576}
2577
2578static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002579surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002580{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002581 struct weston_subsurface *sub;
2582 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002583
Jason Ekstranda7af7042013-10-12 22:38:11 -05002584 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2585 if (sub->surface == surface)
2586 continue;
2587
George Kiagiadakised04d382014-06-13 18:10:26 +02002588 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2589 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002590 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002591 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002592
2593 surface_free_unused_subsurface_views(sub->surface);
2594 }
2595}
2596
2597static void
2598view_list_add_subsurface_view(struct weston_compositor *compositor,
2599 struct weston_subsurface *sub,
2600 struct weston_view *parent)
2601{
2602 struct weston_subsurface *child;
2603 struct weston_view *view = NULL, *iv;
2604
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002605 if (!weston_surface_is_mapped(sub->surface))
2606 return;
2607
Jason Ekstranda7af7042013-10-12 22:38:11 -05002608 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2609 if (iv->geometry.parent == parent) {
2610 view = iv;
2611 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002612 }
2613 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002614
2615 if (view) {
2616 /* Put it back in the surface's list of views */
2617 wl_list_remove(&view->surface_link);
2618 wl_list_insert(&sub->surface->views, &view->surface_link);
2619 } else {
2620 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002621 weston_view_set_position(view,
2622 sub->position.x,
2623 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002624 weston_view_set_transform_parent(view, parent);
2625 }
2626
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002627 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002628 weston_view_update_transform(view);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002629 view->is_mapped = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002630
Pekka Paalanenb188e912013-11-19 14:03:35 +02002631 if (wl_list_empty(&sub->surface->subsurface_list)) {
2632 wl_list_insert(compositor->view_list.prev, &view->link);
2633 return;
2634 }
2635
2636 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2637 if (child->surface == sub->surface)
2638 wl_list_insert(compositor->view_list.prev, &view->link);
2639 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002640 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002641 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002642}
2643
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002644/* This recursively adds the sub-surfaces for a view, relying on the
2645 * sub-surface order. Thus, if a client restacks the sub-surfaces, that
2646 * change first happens to the sub-surface list, and then automatically
2647 * propagates here. See weston_surface_damage_subsurfaces() for how the
2648 * sub-surfaces receive damage when the client changes the state.
2649 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002650static void
2651view_list_add(struct weston_compositor *compositor,
2652 struct weston_view *view)
2653{
2654 struct weston_subsurface *sub;
2655
2656 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002657
Pekka Paalanenb188e912013-11-19 14:03:35 +02002658 if (wl_list_empty(&view->surface->subsurface_list)) {
2659 wl_list_insert(compositor->view_list.prev, &view->link);
2660 return;
2661 }
2662
2663 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2664 if (sub->surface == view->surface)
2665 wl_list_insert(compositor->view_list.prev, &view->link);
2666 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002667 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002668 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002669}
2670
2671static void
2672weston_compositor_build_view_list(struct weston_compositor *compositor)
2673{
Loïc Yhuel267b16e2019-09-17 20:14:56 +02002674 struct weston_view *view, *tmp;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002675 struct weston_layer *layer;
2676
2677 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002678 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002679 surface_stash_subsurface_views(view->surface);
2680
Loïc Yhuel267b16e2019-09-17 20:14:56 +02002681 wl_list_for_each_safe(view, tmp, &compositor->view_list, link)
2682 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002683 wl_list_init(&compositor->view_list);
Loïc Yhuel267b16e2019-09-17 20:14:56 +02002684
Jason Ekstranda7af7042013-10-12 22:38:11 -05002685 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002686 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002687 view_list_add(compositor, view);
2688 }
2689 }
2690
2691 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002692 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002693 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002694}
2695
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002696static void
2697weston_output_take_feedback_list(struct weston_output *output,
2698 struct weston_surface *surface)
2699{
2700 struct weston_view *view;
2701 struct weston_presentation_feedback *feedback;
2702 uint32_t flags = 0xffffffff;
2703
2704 if (wl_list_empty(&surface->feedback_list))
2705 return;
2706
2707 /* All views must have the flag for the flag to survive. */
2708 wl_list_for_each(view, &surface->views, surface_link) {
2709 /* ignore views that are not on this output at all */
2710 if (view->output_mask & (1u << output->id))
2711 flags &= view->psf_flags;
2712 }
2713
2714 wl_list_for_each(feedback, &surface->feedback_list, link)
2715 feedback->psf_flags = flags;
2716
2717 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2718 wl_list_init(&surface->feedback_list);
2719}
2720
David Herrmann1edf44c2013-10-22 17:11:26 +02002721static int
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002722weston_output_repaint(struct weston_output *output, void *repaint_data)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002723{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002724 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002725 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002726 struct weston_animation *animation, *next;
2727 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002728 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002729 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002730 int r;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002731 uint32_t frame_time_msec;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05302732 enum weston_hdcp_protection highest_requested = WESTON_HDCP_DISABLE;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002733
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002734 if (output->destroying)
2735 return 0;
2736
Marius Vlad3203ff62019-09-05 14:56:12 +03002737 TL_POINT(ec, "core_repaint_begin", TLP_OUTPUT(output), TLP_END);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002738
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002739 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002740 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002741
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05302742 /* Find the highest protection desired for an output */
2743 wl_list_for_each(ev, &ec->view_list, link) {
2744 if (ev->surface->output_mask & (1u << output->id)) {
2745 /*
2746 * The desired_protection of the output should be the
2747 * maximum of the desired_protection of the surfaces,
2748 * that are displayed on that output, to avoid
2749 * reducing the protection for existing surfaces.
2750 */
2751 if (ev->surface->desired_protection > highest_requested)
2752 highest_requested =
2753 ev->surface->desired_protection;
2754 }
2755 }
2756
2757 output->desired_protection = highest_requested;
2758
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002759 if (output->assign_planes && !output->disable_planes) {
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002760 output->assign_planes(output, repaint_data);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002761 } else {
2762 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002763 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002764 ev->psf_flags = 0;
2765 }
2766 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002767
Pekka Paalanene67858b2013-04-25 13:57:42 +03002768 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002769 wl_list_for_each(ev, &ec->view_list, link) {
2770 /* Note: This operation is safe to do multiple times on the
2771 * same surface.
2772 */
2773 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002774 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002775 &ev->surface->frame_callback_list);
2776 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002777
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002778 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002779 }
2780 }
2781
Michael Olbrichc5ea4952020-08-11 12:42:35 +02002782 output_accumulate_damage(output);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002783
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002784 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002785 pixman_region32_intersect(&output_damage,
2786 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002787 pixman_region32_subtract(&output_damage,
2788 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002789
Scott Moreauccbf29d2012-02-22 14:21:41 -07002790 if (output->dirty)
2791 weston_output_update_matrix(output);
2792
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002793 r = output->repaint(output, &output_damage, repaint_data);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002794
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002795 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002796
Daniel Stone09a97e22017-03-01 11:34:06 +00002797 output->repaint_needed = false;
Daniel Stone05df8c12017-03-03 16:59:42 +00002798 if (r == 0)
2799 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002800
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002801 weston_compositor_repick(ec);
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002802
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002803 frame_time_msec = timespec_to_msec(&output->frame_time);
2804
Jonas Ådahldb773762012-06-13 00:01:21 +02002805 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002806 wl_callback_send_done(cb->resource, frame_time_msec);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002807 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002808 }
2809
Scott Moreaud64cf212012-06-08 19:40:54 -06002810 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002811 animation->frame_counter++;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002812 animation->frame(animation, output, &output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002813 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002814
Marius Vlad3203ff62019-09-05 14:56:12 +03002815 TL_POINT(ec, "core_repaint_posted", TLP_OUTPUT(output), TLP_END);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002816
David Herrmann1edf44c2013-10-22 17:11:26 +02002817 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002818}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002819
Pekka Paalanen82919792014-05-21 13:51:49 +03002820static void
2821weston_output_schedule_repaint_reset(struct weston_output *output)
2822{
Daniel Stone05df8c12017-03-03 16:59:42 +00002823 output->repaint_status = REPAINT_NOT_SCHEDULED;
Marius Vlad3203ff62019-09-05 14:56:12 +03002824 TL_POINT(output->compositor, "core_repaint_exit_loop",
2825 TLP_OUTPUT(output), TLP_END);
Pekka Paalanen82919792014-05-21 13:51:49 +03002826}
2827
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002828static int
2829weston_output_maybe_repaint(struct weston_output *output, struct timespec *now,
2830 void *repaint_data)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002831{
Pekka Paalanen0513a952014-05-21 16:17:27 +03002832 struct weston_compositor *compositor = output->compositor;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002833 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002834 int64_t msec_to_repaint;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002835
Daniel Stone6847b852017-03-01 11:34:08 +00002836 /* We're not ready yet; come back to make a decision later. */
2837 if (output->repaint_status != REPAINT_SCHEDULED)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002838 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002839
2840 msec_to_repaint = timespec_sub_to_msec(&output->next_repaint, now);
2841 if (msec_to_repaint > 1)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002842 return ret;
Daniel Stone05df8c12017-03-03 16:59:42 +00002843
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002844 /* If we're sleeping, drop the repaint machinery entirely; we will
2845 * explicitly repaint all outputs when we come back. */
2846 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2847 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
2848 goto err;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002849
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002850 /* We don't actually need to repaint this output; drop it from
2851 * repaint until something causes damage. */
2852 if (!output->repaint_needed)
2853 goto err;
2854
2855 /* If repaint fails, we aren't going to get weston_output_finish_frame
2856 * to trigger a new repaint, so drop it from repaint and hope
Daniel Stone6847b852017-03-01 11:34:08 +00002857 * something schedules a successful repaint later. As repainting may
2858 * take some time, re-read our clock as a courtesy to the next
2859 * output. */
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002860 ret = weston_output_repaint(output, repaint_data);
Daniel Stone6847b852017-03-01 11:34:08 +00002861 weston_compositor_read_presentation_clock(compositor, now);
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002862 if (ret != 0)
2863 goto err;
2864
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09002865 output->repainted = true;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002866 return ret;
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002867
2868err:
Pekka Paalanen0513a952014-05-21 16:17:27 +03002869 weston_output_schedule_repaint_reset(output);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002870 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002871}
2872
2873static void
2874output_repaint_timer_arm(struct weston_compositor *compositor)
2875{
2876 struct weston_output *output;
2877 bool any_should_repaint = false;
2878 struct timespec now;
Sergi Granellb4c08862017-03-18 13:01:15 +01002879 int64_t msec_to_next = INT64_MAX;
Daniel Stone6847b852017-03-01 11:34:08 +00002880
2881 weston_compositor_read_presentation_clock(compositor, &now);
2882
2883 wl_list_for_each(output, &compositor->output_list, link) {
2884 int64_t msec_to_this;
2885
2886 if (output->repaint_status != REPAINT_SCHEDULED)
2887 continue;
2888
2889 msec_to_this = timespec_sub_to_msec(&output->next_repaint,
2890 &now);
2891 if (!any_should_repaint || msec_to_this < msec_to_next)
2892 msec_to_next = msec_to_this;
2893
2894 any_should_repaint = true;
2895 }
2896
2897 if (!any_should_repaint)
2898 return;
2899
2900 /* Even if we should repaint immediately, add the minimum 1 ms delay.
2901 * This is a workaround to allow coalescing multiple output repaints
2902 * particularly from weston_output_finish_frame()
2903 * into the same call, which would not happen if we called
2904 * output_repaint_timer_handler() directly.
2905 */
2906 if (msec_to_next < 1)
2907 msec_to_next = 1;
2908
2909 wl_event_source_timer_update(compositor->repaint_timer, msec_to_next);
2910}
2911
2912static int
2913output_repaint_timer_handler(void *data)
2914{
2915 struct weston_compositor *compositor = data;
2916 struct weston_output *output;
2917 struct timespec now;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002918 void *repaint_data = NULL;
Emre Ucane479ed82018-03-20 15:29:40 +01002919 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002920
2921 weston_compositor_read_presentation_clock(compositor, &now);
Pekka Paalanen1ed2cad2021-02-10 12:33:03 +02002922 compositor->last_repaint_start = now;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002923
2924 if (compositor->backend->repaint_begin)
2925 repaint_data = compositor->backend->repaint_begin(compositor);
2926
2927 wl_list_for_each(output, &compositor->output_list, link) {
2928 ret = weston_output_maybe_repaint(output, &now, repaint_data);
2929 if (ret)
2930 break;
2931 }
2932
2933 if (ret == 0) {
Tomohito Esaki09bfcd62018-06-05 10:37:05 +09002934 if (compositor->backend->repaint_flush)
Antonio Borneoc90fccc2019-06-30 15:51:10 +02002935 ret = compositor->backend->repaint_flush(compositor,
2936 repaint_data);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002937 } else {
Antonio Borneoc90fccc2019-06-30 15:51:10 +02002938 if (compositor->backend->repaint_cancel)
2939 compositor->backend->repaint_cancel(compositor,
2940 repaint_data);
2941 }
2942
2943 if (ret != 0) {
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09002944 wl_list_for_each(output, &compositor->output_list, link) {
2945 if (output->repainted)
2946 weston_output_schedule_repaint_reset(output);
2947 }
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002948 }
Daniel Stone6847b852017-03-01 11:34:08 +00002949
Tomohito Esakiddaf95c2018-07-10 11:47:15 +09002950 wl_list_for_each(output, &compositor->output_list, link)
2951 output->repainted = false;
2952
Daniel Stone6847b852017-03-01 11:34:08 +00002953 output_repaint_timer_arm(compositor);
2954
Pekka Paalanen0513a952014-05-21 16:17:27 +03002955 return 0;
2956}
2957
Pekka Paalanen50aa3a72020-05-28 11:34:04 +03002958/** Convert a presentation timestamp to another clock domain
2959 *
2960 * \param compositor The compositor defines the presentation clock domain.
2961 * \param presentation_stamp The timestamp in presentation clock domain.
2962 * \param presentation_now Current time in presentation clock domain.
2963 * \param target_clock Defines the target clock domain.
2964 *
2965 * This approximation relies on presentation_stamp to be close to current time.
2966 * The further it is from current time and the bigger the speed difference
2967 * between the two clock domains, the bigger the conversion error.
2968 *
2969 * Conversion error due to system load is biased and unbounded.
2970 */
2971static struct timespec
2972convert_presentation_time_now(struct weston_compositor *compositor,
2973 const struct timespec *presentation_stamp,
2974 const struct timespec *presentation_now,
2975 clockid_t target_clock)
2976{
2977 struct timespec target_now = {};
2978 struct timespec target_stamp;
2979 int64_t delta_ns;
2980
2981 if (compositor->presentation_clock == target_clock)
2982 return *presentation_stamp;
2983
2984 clock_gettime(target_clock, &target_now);
2985 delta_ns = timespec_sub_to_nsec(presentation_stamp, presentation_now);
2986 timespec_add_nsec(&target_stamp, &target_now, delta_ns);
2987
2988 return target_stamp;
2989}
2990
Marius Vlad55d87362019-06-11 01:15:35 +03002991/**
2992 * \ingroup output
2993 */
Kristian Høgsbergef044142011-06-21 15:02:12 -04002994WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002995weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002996 const struct timespec *stamp,
2997 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002998{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002999 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03003000 int32_t refresh_nsec;
3001 struct timespec now;
Pekka Paalanen50aa3a72020-05-28 11:34:04 +03003002 struct timespec vblank_monotonic;
Daniel Stone6847b852017-03-01 11:34:08 +00003003 int64_t msec_rel;
Pekka Paalanen133e4392014-09-23 22:08:46 -04003004
Daniel Stone05df8c12017-03-03 16:59:42 +00003005 assert(output->repaint_status == REPAINT_AWAITING_COMPLETION);
Daniel Stone3615ce12017-03-01 11:34:05 +00003006 assert(stamp || (presented_flags & WP_PRESENTATION_FEEDBACK_INVALID));
3007
Daniel Stone6847b852017-03-01 11:34:08 +00003008 weston_compositor_read_presentation_clock(compositor, &now);
3009
Daniel Stone3615ce12017-03-01 11:34:05 +00003010 /* If we haven't been supplied any timestamp at all, we don't have a
3011 * timebase to work against, so any delay just wastes time. Push a
3012 * repaint as soon as possible so we can get on with it. */
Daniel Stone6847b852017-03-01 11:34:08 +00003013 if (!stamp) {
3014 output->next_repaint = now;
Daniel Stone3615ce12017-03-01 11:34:05 +00003015 goto out;
Daniel Stone6847b852017-03-01 11:34:08 +00003016 }
Daniel Stone3615ce12017-03-01 11:34:05 +00003017
Pekka Paalanen50aa3a72020-05-28 11:34:04 +03003018 vblank_monotonic = convert_presentation_time_now(compositor,
3019 stamp, &now,
3020 CLOCK_MONOTONIC);
Marius Vlad3203ff62019-09-05 14:56:12 +03003021 TL_POINT(compositor, "core_repaint_finished", TLP_OUTPUT(output),
Pekka Paalanen50aa3a72020-05-28 11:34:04 +03003022 TLP_VBLANK(&vblank_monotonic), TLP_END);
Marius Vladdf9278a2018-03-06 18:56:23 +02003023
Pekka Paalanend7894d02015-07-03 15:08:53 +03003024 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003025 weston_presentation_feedback_present_list(&output->feedback_list,
3026 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02003027 output->msc,
3028 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003029
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02003030 output->frame_time = *stamp;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003031
Daniel Stone6847b852017-03-01 11:34:08 +00003032 timespec_add_nsec(&output->next_repaint, stamp, refresh_nsec);
3033 timespec_add_msec(&output->next_repaint, &output->next_repaint,
3034 -compositor->repaint_msec);
3035 msec_rel = timespec_sub_to_msec(&output->next_repaint, &now);
Daniel Stone84aff5c2017-03-01 11:34:04 +00003036
3037 if (msec_rel < -1000 || msec_rel > 1000) {
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02003038 static bool warned;
3039
3040 if (!warned)
3041 weston_log("Warning: computed repaint delay is "
Daniel Stone6847b852017-03-01 11:34:08 +00003042 "insane: %lld msec\n", (long long) msec_rel);
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02003043 warned = true;
3044
Daniel Stone6847b852017-03-01 11:34:08 +00003045 output->next_repaint = now;
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02003046 }
3047
Mario Kleinerb7df04e2015-06-21 21:25:15 +02003048 /* Called from restart_repaint_loop and restart happens already after
3049 * the deadline given by repaint_msec? In that case we delay until
3050 * the deadline of the next frame, to give clients a more predictable
3051 * timing of the repaint cycle to lock on. */
Daniel Stoneeca5cca2017-02-28 21:53:51 +00003052 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID &&
3053 msec_rel < 0) {
3054 while (timespec_sub_to_nsec(&output->next_repaint, &now) < 0) {
3055 timespec_add_nsec(&output->next_repaint,
3056 &output->next_repaint,
3057 refresh_nsec);
3058 }
3059 }
Mario Kleinerb7df04e2015-06-21 21:25:15 +02003060
Daniel Stone3615ce12017-03-01 11:34:05 +00003061out:
Daniel Stone05df8c12017-03-03 16:59:42 +00003062 output->repaint_status = REPAINT_SCHEDULED;
Daniel Stone6847b852017-03-01 11:34:08 +00003063 output_repaint_timer_arm(compositor);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003064}
3065
3066static void
3067idle_repaint(void *data)
3068{
3069 struct weston_output *output = data;
Antonio Borneoc90fccc2019-06-30 15:51:10 +02003070 int ret;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003071
Daniel Stone05df8c12017-03-03 16:59:42 +00003072 assert(output->repaint_status == REPAINT_BEGIN_FROM_IDLE);
3073 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03003074 output->idle_repaint_source = NULL;
Antonio Borneoc90fccc2019-06-30 15:51:10 +02003075 ret = output->start_repaint_loop(output);
3076 if (ret != 0)
3077 weston_output_schedule_repaint_reset(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003078}
3079
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003080WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003081weston_layer_entry_insert(struct weston_layer_entry *list,
3082 struct weston_layer_entry *entry)
3083{
3084 wl_list_insert(&list->link, &entry->link);
3085 entry->layer = list->layer;
3086}
3087
3088WL_EXPORT void
3089weston_layer_entry_remove(struct weston_layer_entry *entry)
3090{
3091 wl_list_remove(&entry->link);
3092 wl_list_init(&entry->link);
3093 entry->layer = NULL;
3094}
3095
Quentin Glidic82681572016-12-17 13:40:51 +01003096
3097/** Initialize the weston_layer struct.
3098 *
3099 * \param compositor The compositor instance
3100 * \param layer The layer to initialize
3101 */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003102WL_EXPORT void
Quentin Glidic82681572016-12-17 13:40:51 +01003103weston_layer_init(struct weston_layer *layer,
3104 struct weston_compositor *compositor)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003105{
Quentin Glidic82681572016-12-17 13:40:51 +01003106 layer->compositor = compositor;
3107 wl_list_init(&layer->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003108 wl_list_init(&layer->view_list.link);
3109 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003110 weston_layer_set_mask_infinite(layer);
Quentin Glidic82681572016-12-17 13:40:51 +01003111}
3112
3113/** Sets the position of the layer in the layer list. The layer will be placed
3114 * below any layer with the same position value, if any.
3115 * This function is safe to call if the layer is already on the list, but the
3116 * layer may be moved below other layers at the same position, if any.
3117 *
3118 * \param layer The layer to modify
3119 * \param position The position the layer will be placed at
3120 */
3121WL_EXPORT void
3122weston_layer_set_position(struct weston_layer *layer,
3123 enum weston_layer_position position)
3124{
3125 struct weston_layer *below;
3126
3127 wl_list_remove(&layer->link);
3128
3129 /* layer_list is ordered from top to bottom, the last layer being the
3130 * background with the smallest position value */
3131
3132 layer->position = position;
3133 wl_list_for_each_reverse(below, &layer->compositor->layer_list, link) {
3134 if (below->position >= layer->position) {
3135 wl_list_insert(&below->link, &layer->link);
3136 return;
3137 }
3138 }
3139 wl_list_insert(&layer->compositor->layer_list, &layer->link);
3140}
3141
3142/** Hide a layer by taking it off the layer list.
3143 * This function is safe to call if the layer is not on the list.
3144 *
3145 * \param layer The layer to hide
3146 */
3147WL_EXPORT void
3148weston_layer_unset_position(struct weston_layer *layer)
3149{
3150 wl_list_remove(&layer->link);
3151 wl_list_init(&layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003152}
3153
3154WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003155weston_layer_set_mask(struct weston_layer *layer,
3156 int x, int y, int width, int height)
3157{
3158 struct weston_view *view;
3159
3160 layer->mask.x1 = x;
3161 layer->mask.x2 = x + width;
3162 layer->mask.y1 = y;
3163 layer->mask.y2 = y + height;
3164
3165 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
3166 weston_view_geometry_dirty(view);
3167 }
3168}
3169
3170WL_EXPORT void
3171weston_layer_set_mask_infinite(struct weston_layer *layer)
3172{
Adam Jackson3c3f3b12019-10-16 16:02:59 -04003173 struct weston_view *view;
3174
3175 layer->mask.x1 = INT32_MIN;
3176 layer->mask.x2 = INT32_MAX;
3177 layer->mask.y1 = INT32_MIN;
3178 layer->mask.y2 = INT32_MAX;
3179
3180 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
3181 weston_view_geometry_dirty(view);
3182 }
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003183}
3184
Daniel Stone3b775632018-07-20 08:38:25 +01003185WL_EXPORT bool
3186weston_layer_mask_is_infinite(struct weston_layer *layer)
3187{
3188 return layer->mask.x1 == INT32_MIN &&
3189 layer->mask.y1 == INT32_MIN &&
Adam Jackson3c3f3b12019-10-16 16:02:59 -04003190 layer->mask.x2 == INT32_MAX &&
3191 layer->mask.y2 == INT32_MAX;
Daniel Stone3b775632018-07-20 08:38:25 +01003192}
3193
Marius Vlad55d87362019-06-11 01:15:35 +03003194/**
3195 * \ingroup output
3196 */
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003197WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003198weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003199{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003200 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04003201 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01003202
Bryce Harrington08976ac2016-08-30 12:05:16 -07003203 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
3204 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003205 return;
3206
Pekka Paalanenb5026542014-11-12 15:09:24 +02003207 if (!output->repaint_needed)
Marius Vlad3203ff62019-09-05 14:56:12 +03003208 TL_POINT(compositor, "core_repaint_req", TLP_OUTPUT(output), TLP_END);
Pekka Paalanenb5026542014-11-12 15:09:24 +02003209
Kristian Høgsbergef044142011-06-21 15:02:12 -04003210 loop = wl_display_get_event_loop(compositor->wl_display);
Daniel Stone09a97e22017-03-01 11:34:06 +00003211 output->repaint_needed = true;
Daniel Stone05df8c12017-03-03 16:59:42 +00003212
3213 /* If we already have a repaint scheduled for our idle handler,
3214 * no need to set it again. If the repaint has been called but
3215 * not finished, then weston_output_finish_frame() will notice
3216 * that a repaint is needed and schedule one. */
3217 if (output->repaint_status != REPAINT_NOT_SCHEDULED)
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003218 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01003219
Daniel Stone05df8c12017-03-03 16:59:42 +00003220 output->repaint_status = REPAINT_BEGIN_FROM_IDLE;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03003221 assert(!output->idle_repaint_source);
3222 output->idle_repaint_source = wl_event_loop_add_idle(loop, idle_repaint,
3223 output);
Marius Vlad3203ff62019-09-05 14:56:12 +03003224 TL_POINT(compositor, "core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003225}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05003226
Marius Vlad9fdda7f2019-06-11 16:08:55 +03003227/** weston_compositor_schedule_repaint
3228 * \ingroup compositor
3229 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003230WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003231weston_compositor_schedule_repaint(struct weston_compositor *compositor)
3232{
3233 struct weston_output *output;
3234
3235 wl_list_for_each(output, &compositor->output_list, link)
3236 weston_output_schedule_repaint(output);
3237}
3238
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003239static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003240surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003241{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003242 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003243}
3244
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003245static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003246surface_attach(struct wl_client *client,
3247 struct wl_resource *resource,
3248 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
3249{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003250 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05003251 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003252
Kristian Høgsbergab19f932013-08-20 11:30:54 -07003253 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05003254 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07003255 if (buffer == NULL) {
3256 wl_client_post_no_memory(client);
3257 return;
3258 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07003259 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04003260
Pekka Paalanende685b82012-12-04 15:58:12 +02003261 /* Attach, attach, without commit in between does not send
3262 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003263 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003264
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003265 surface->pending.sx = sx;
3266 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01003267 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003268}
3269
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003270static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003271surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003272 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003273 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05003274{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003275 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003276
Derek Foreman57e92ed2015-11-17 14:11:35 -06003277 if (width <= 0 || height <= 0)
3278 return;
3279
Derek Foreman152254b2015-11-26 14:17:48 -06003280 pixman_region32_union_rect(&surface->pending.damage_surface,
3281 &surface->pending.damage_surface,
3282 x, y, width, height);
3283}
3284
3285static void
3286surface_damage_buffer(struct wl_client *client,
3287 struct wl_resource *resource,
3288 int32_t x, int32_t y, int32_t width, int32_t height)
3289{
3290 struct weston_surface *surface = wl_resource_get_user_data(resource);
3291
3292 if (width <= 0 || height <= 0)
3293 return;
3294
3295 pixman_region32_union_rect(&surface->pending.damage_buffer,
3296 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04003297 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003298}
3299
Kristian Høgsberg33418202011-08-16 23:01:28 -04003300static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003301destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04003302{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05003303 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003304
3305 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02003306 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003307}
3308
3309static void
3310surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003311 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04003312{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003313 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003314 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003315
3316 cb = malloc(sizeof *cb);
3317 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003318 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003319 return;
3320 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03003321
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003322 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
3323 callback);
3324 if (cb->resource == NULL) {
3325 free(cb);
3326 wl_resource_post_no_memory(resource);
3327 return;
3328 }
3329
Jason Ekstranda85118c2013-06-27 20:17:02 -05003330 wl_resource_set_implementation(cb->resource, NULL, cb,
3331 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003332
Pekka Paalanenbc106382012-10-10 12:49:31 +03003333 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003334}
3335
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003336static void
3337surface_set_opaque_region(struct wl_client *client,
3338 struct wl_resource *resource,
3339 struct wl_resource *region_resource)
3340{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003341 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003342 struct weston_region *region;
3343
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003344 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003345 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003346 pixman_region32_copy(&surface->pending.opaque,
3347 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003348 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07003349 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003350 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003351}
3352
3353static void
3354surface_set_input_region(struct wl_client *client,
3355 struct wl_resource *resource,
3356 struct wl_resource *region_resource)
3357{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003358 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003359 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003360
3361 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003362 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003363 pixman_region32_copy(&surface->pending.input,
3364 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003365 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003366 pixman_region32_fini(&surface->pending.input);
3367 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003368 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003369}
3370
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003371/* Cause damage to this sub-surface and all its children.
3372 *
3373 * This is useful when there are state changes that need an implicit
3374 * damage, e.g. a z-order change.
3375 */
3376static void
3377weston_surface_damage_subsurfaces(struct weston_subsurface *sub)
3378{
3379 struct weston_subsurface *child;
3380
3381 weston_surface_damage(sub->surface);
3382 sub->reordered = false;
3383
3384 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link)
3385 if (child != sub)
3386 weston_surface_damage_subsurfaces(child);
3387}
3388
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003389static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003390weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003391{
Pekka Paalanene67858b2013-04-25 13:57:42 +03003392 struct weston_subsurface *sub;
3393
3394 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
3395 parent_link_pending) {
3396 wl_list_remove(&sub->parent_link);
3397 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003398
3399 if (sub->reordered)
3400 weston_surface_damage_subsurfaces(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003401 }
3402}
3403
3404static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03003405weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05003406 struct weston_matrix *matrix)
3407{
Pekka Paalanen04baea52016-04-26 15:50:58 +03003408 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05003409 double src_width, src_height, dest_width, dest_height;
3410
3411 weston_matrix_init(matrix);
3412
3413 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
3414 src_width = surface->width_from_buffer;
3415 src_height = surface->height_from_buffer;
3416 } else {
3417 src_width = wl_fixed_to_double(vp->buffer.src_width);
3418 src_height = wl_fixed_to_double(vp->buffer.src_height);
3419 }
3420
3421 if (vp->surface.width == -1) {
3422 dest_width = src_width;
3423 dest_height = src_height;
3424 } else {
3425 dest_width = vp->surface.width;
3426 dest_height = vp->surface.height;
3427 }
3428
3429 if (src_width != dest_width || src_height != dest_height)
3430 weston_matrix_scale(matrix,
3431 src_width / dest_width,
3432 src_height / dest_height, 1);
3433
3434 if (vp->buffer.src_width != wl_fixed_from_int(-1))
3435 weston_matrix_translate(matrix,
3436 wl_fixed_to_double(vp->buffer.src_x),
3437 wl_fixed_to_double(vp->buffer.src_y),
3438 0);
3439
3440 switch (vp->buffer.transform) {
3441 case WL_OUTPUT_TRANSFORM_FLIPPED:
3442 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3443 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3444 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3445 weston_matrix_scale(matrix, -1, 1, 1);
3446 weston_matrix_translate(matrix,
3447 surface->width_from_buffer, 0, 0);
3448 break;
3449 }
3450
3451 switch (vp->buffer.transform) {
3452 default:
3453 case WL_OUTPUT_TRANSFORM_NORMAL:
3454 case WL_OUTPUT_TRANSFORM_FLIPPED:
3455 break;
3456 case WL_OUTPUT_TRANSFORM_90:
3457 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +02003458 weston_matrix_rotate_xy(matrix, 0, -1);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003459 weston_matrix_translate(matrix,
Pekka Paalanen8060d822020-02-06 15:27:54 +02003460 0, surface->width_from_buffer, 0);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003461 break;
3462 case WL_OUTPUT_TRANSFORM_180:
3463 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3464 weston_matrix_rotate_xy(matrix, -1, 0);
3465 weston_matrix_translate(matrix,
3466 surface->width_from_buffer,
3467 surface->height_from_buffer, 0);
3468 break;
3469 case WL_OUTPUT_TRANSFORM_270:
3470 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +02003471 weston_matrix_rotate_xy(matrix, 0, 1);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003472 weston_matrix_translate(matrix,
Pekka Paalanen8060d822020-02-06 15:27:54 +02003473 surface->height_from_buffer, 0, 0);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003474 break;
3475 }
3476
3477 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
3478}
3479
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003480/**
3481 * Compute a + b > c while being safe to overflows.
3482 */
3483static bool
3484fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
3485{
3486 return (int64_t)a + (int64_t)b > (int64_t)c;
3487}
3488
3489static bool
3490weston_surface_is_pending_viewport_source_valid(
3491 const struct weston_surface *surface)
3492{
3493 const struct weston_surface_state *pend = &surface->pending;
3494 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
3495 int width_from_buffer = 0;
3496 int height_from_buffer = 0;
3497 wl_fixed_t w;
3498 wl_fixed_t h;
3499
3500 /* If viewport source rect is not set, it is always ok. */
3501 if (vp->buffer.src_width == wl_fixed_from_int(-1))
3502 return true;
3503
3504 if (pend->newly_attached) {
3505 if (pend->buffer) {
3506 convert_size_by_transform_scale(&width_from_buffer,
3507 &height_from_buffer,
3508 pend->buffer->width,
3509 pend->buffer->height,
3510 vp->buffer.transform,
3511 vp->buffer.scale);
3512 } else {
3513 /* No buffer: viewport is irrelevant. */
3514 return true;
3515 }
3516 } else {
3517 width_from_buffer = surface->width_from_buffer;
3518 height_from_buffer = surface->height_from_buffer;
3519 }
3520
3521 assert((width_from_buffer == 0) == (height_from_buffer == 0));
3522 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
3523
3524 /* No buffer: viewport is irrelevant. */
3525 if (width_from_buffer == 0 || height_from_buffer == 0)
3526 return true;
3527
3528 /* overflow checks for wl_fixed_from_int() */
3529 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
3530 return false;
3531 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
3532 return false;
3533
3534 w = wl_fixed_from_int(width_from_buffer);
3535 h = wl_fixed_from_int(height_from_buffer);
3536
3537 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
3538 return false;
3539 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
3540 return false;
3541
3542 return true;
3543}
3544
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003545static bool
3546fixed_is_integer(wl_fixed_t v)
3547{
3548 return (v & 0xff) == 0;
3549}
3550
3551static bool
3552weston_surface_is_pending_viewport_dst_size_int(
3553 const struct weston_surface *surface)
3554{
3555 const struct weston_buffer_viewport *vp =
3556 &surface->pending.buffer_viewport;
3557
3558 if (vp->surface.width != -1) {
3559 assert(vp->surface.width > 0 && vp->surface.height > 0);
3560 return true;
3561 }
3562
3563 return fixed_is_integer(vp->buffer.src_width) &&
3564 fixed_is_integer(vp->buffer.src_height);
3565}
3566
Derek Foreman152254b2015-11-26 14:17:48 -06003567/* Translate pending damage in buffer co-ordinates to surface
3568 * co-ordinates and union it with a pixman_region32_t.
3569 * This should only be called after the buffer is attached.
3570 */
3571static void
3572apply_damage_buffer(pixman_region32_t *dest,
3573 struct weston_surface *surface,
3574 struct weston_surface_state *state)
3575{
3576 struct weston_buffer *buffer = surface->buffer_ref.buffer;
3577
3578 /* wl_surface.damage_buffer needs to be clipped to the buffer,
3579 * translated into surface co-ordinates and unioned with
3580 * any other surface damage.
3581 * None of this makes sense if there is no buffer though.
3582 */
3583 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
3584 pixman_region32_t buffer_damage;
3585
3586 pixman_region32_intersect_rect(&state->damage_buffer,
3587 &state->damage_buffer,
3588 0, 0, buffer->width,
3589 buffer->height);
3590 pixman_region32_init(&buffer_damage);
3591 weston_matrix_transform_region(&buffer_damage,
3592 &surface->buffer_to_surface_matrix,
3593 &state->damage_buffer);
3594 pixman_region32_union(dest, dest, &buffer_damage);
3595 pixman_region32_fini(&buffer_damage);
3596 }
3597 /* We should clear this on commit even if there was no buffer */
3598 pixman_region32_clear(&state->damage_buffer);
3599}
3600
Jason Ekstrand1e059042014-10-16 10:55:19 -05003601static void
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303602weston_surface_set_desired_protection(struct weston_surface *surface,
3603 enum weston_hdcp_protection protection)
3604{
3605 if (surface->desired_protection == protection)
3606 return;
3607 surface->desired_protection = protection;
3608 weston_surface_damage(surface);
3609}
3610
3611static void
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303612weston_surface_set_protection_mode(struct weston_surface *surface,
3613 enum weston_surface_protection_mode p_mode)
3614{
3615 struct content_protection *cp = surface->compositor->content_protection;
3616 struct protected_surface *psurface;
3617
3618 surface->protection_mode = p_mode;
3619 wl_list_for_each(psurface, &cp->protected_list, link) {
3620 if (!psurface || psurface->surface != surface)
3621 continue;
3622 weston_protected_surface_send_event(psurface,
3623 surface->current_protection);
3624 }
3625}
3626
3627static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05003628weston_surface_commit_state(struct weston_surface *surface,
3629 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003630{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003631 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003632 pixman_region32_t opaque;
3633
Alexander Larsson4ea95522013-05-22 14:41:37 +02003634 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02003635 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03003636 /* wp_viewport.set_source */
3637 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003638 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003639
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003640 /* wl_surface.attach */
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003641 if (state->newly_attached) {
3642 /* zwp_surface_synchronization_v1.set_acquire_fence */
3643 fd_move(&surface->acquire_fence_fd,
3644 &state->acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003645 /* zwp_surface_synchronization_v1.get_release */
3646 weston_buffer_release_move(&surface->buffer_release_ref,
3647 &state->buffer_release_ref);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003648 weston_surface_attach(surface, state->buffer);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003649 }
Jason Ekstrand7b982072014-05-20 14:33:03 -05003650 weston_surface_state_set_buffer(state, NULL);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003651 assert(state->acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003652 assert(state->buffer_release_ref.buffer_release == NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01003653
Jason Ekstrand1e059042014-10-16 10:55:19 -05003654 weston_surface_build_buffer_matrix(surface,
3655 &surface->surface_to_buffer_matrix);
3656 weston_matrix_invert(&surface->buffer_to_surface_matrix,
3657 &surface->surface_to_buffer_matrix);
3658
Jason Ekstrand7b982072014-05-20 14:33:03 -05003659 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003660 weston_surface_update_size(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003661 if (surface->committed)
3662 surface->committed(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003663 }
Giulio Camuffo184df502013-02-21 11:29:21 +01003664
Jason Ekstrand7b982072014-05-20 14:33:03 -05003665 state->sx = 0;
3666 state->sy = 0;
3667 state->newly_attached = 0;
3668 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03003669
Derek Foreman152254b2015-11-26 14:17:48 -06003670 /* wl_surface.damage and wl_surface.damage_buffer */
Marius Vlad2a1b7862019-09-05 13:12:18 +03003671 if (pixman_region32_not_empty(&state->damage_surface) ||
3672 pixman_region32_not_empty(&state->damage_buffer))
Marius Vlad3203ff62019-09-05 14:56:12 +03003673 TL_POINT(surface->compositor, "core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06003674
Pekka Paalanen8e159182012-10-10 12:49:25 +03003675 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06003676 &state->damage_surface);
3677
3678 apply_damage_buffer(&surface->damage, surface, state);
3679
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05003680 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07003681 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06003682 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003683
3684 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003685 pixman_region32_init(&opaque);
3686 pixman_region32_intersect_rect(&opaque, &state->opaque,
3687 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003688
3689 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
3690 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003691 wl_list_for_each(view, &surface->views, surface_link)
3692 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003693 }
3694
3695 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003696
3697 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003698 pixman_region32_intersect_rect(&surface->input, &state->input,
3699 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003700
Pekka Paalanenbc106382012-10-10 12:49:31 +03003701 /* wl_surface.frame */
3702 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05003703 &state->frame_callback_list);
3704 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003705
3706 /* XXX:
3707 * What should happen with a feedback request, if there
3708 * is no wl_buffer attached for this commit?
3709 */
3710
3711 /* presentation.feedback */
3712 wl_list_insert_list(&surface->feedback_list,
3713 &state->feedback_list);
3714 wl_list_init(&state->feedback_list);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003715
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303716 /* weston_protected_surface.enforced/relaxed */
3717 if (surface->protection_mode != state->protection_mode)
3718 weston_surface_set_protection_mode(surface,
3719 state->protection_mode);
3720
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303721 /* weston_protected_surface.set_type */
3722 weston_surface_set_desired_protection(surface, state->desired_protection);
3723
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003724 wl_signal_emit(&surface->commit_signal, surface);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003725}
3726
3727static void
3728weston_surface_commit(struct weston_surface *surface)
3729{
3730 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003731
Pekka Paalanene67858b2013-04-25 13:57:42 +03003732 weston_surface_commit_subsurface_order(surface);
3733
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003734 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003735}
3736
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003737static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003738weston_subsurface_commit(struct weston_subsurface *sub);
3739
3740static void
3741weston_subsurface_parent_commit(struct weston_subsurface *sub,
3742 int parent_is_synchronized);
3743
3744static void
3745surface_commit(struct wl_client *client, struct wl_resource *resource)
3746{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003747 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003748 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
3749
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003750 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
3751 assert(surface->viewport_resource);
3752
3753 wl_resource_post_error(surface->viewport_resource,
3754 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
3755 "wl_surface@%d has viewport source outside buffer",
3756 wl_resource_get_id(resource));
3757 return;
3758 }
3759
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003760 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
3761 assert(surface->viewport_resource);
3762
3763 wl_resource_post_error(surface->viewport_resource,
3764 WP_VIEWPORT_ERROR_BAD_SIZE,
3765 "wl_surface@%d viewport dst size not integer",
3766 wl_resource_get_id(resource));
3767 return;
3768 }
3769
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003770 if (surface->pending.acquire_fence_fd >= 0) {
3771 assert(surface->synchronization_resource);
3772
3773 if (!surface->pending.buffer) {
3774 fd_clear(&surface->pending.acquire_fence_fd);
3775 wl_resource_post_error(surface->synchronization_resource,
3776 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3777 "wl_surface@%"PRIu32" no buffer for synchronization",
3778 wl_resource_get_id(resource));
3779 return;
3780 }
3781
3782 /* We support fences for both wp_linux_dmabuf and opaque EGL
3783 * buffers, as mandated by minor version 2 of the
3784 * zwp_linux_explicit_synchronization_v1 protocol. Since
3785 * renderers that support fences currently only support these
3786 * two buffer types plus SHM buffers, we can just check for the
3787 * SHM buffer case here.
3788 */
3789 if (wl_shm_buffer_get(surface->pending.buffer->resource)) {
3790 fd_clear(&surface->pending.acquire_fence_fd);
3791 wl_resource_post_error(surface->synchronization_resource,
3792 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_UNSUPPORTED_BUFFER,
3793 "wl_surface@%"PRIu32" unsupported buffer for synchronization",
3794 wl_resource_get_id(resource));
3795 return;
3796 }
3797 }
3798
Alexandros Frantzis67629672018-10-19 12:14:11 +03003799 if (surface->pending.buffer_release_ref.buffer_release &&
3800 !surface->pending.buffer) {
3801 assert(surface->synchronization_resource);
3802
3803 wl_resource_post_error(surface->synchronization_resource,
3804 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3805 "wl_surface@%"PRIu32" no buffer for synchronization",
3806 wl_resource_get_id(resource));
3807 return;
3808 }
3809
Pekka Paalanene67858b2013-04-25 13:57:42 +03003810 if (sub) {
3811 weston_subsurface_commit(sub);
3812 return;
3813 }
3814
3815 weston_surface_commit(surface);
3816
3817 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
3818 if (sub->surface != surface)
3819 weston_subsurface_parent_commit(sub, 0);
3820 }
3821}
3822
3823static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003824surface_set_buffer_transform(struct wl_client *client,
3825 struct wl_resource *resource, int transform)
3826{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003827 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003828
Jonny Lamba55f1392014-05-30 12:07:15 +02003829 /* if wl_output.transform grows more members this will need to be updated. */
3830 if (transform < 0 ||
3831 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
3832 wl_resource_post_error(resource,
3833 WL_SURFACE_ERROR_INVALID_TRANSFORM,
3834 "buffer transform must be a valid transform "
3835 "('%d' specified)", transform);
3836 return;
3837 }
3838
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003839 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003840 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003841}
3842
Alexander Larsson4ea95522013-05-22 14:41:37 +02003843static void
3844surface_set_buffer_scale(struct wl_client *client,
3845 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003846 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003847{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003848 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003849
Jonny Lamba55f1392014-05-30 12:07:15 +02003850 if (scale < 1) {
3851 wl_resource_post_error(resource,
3852 WL_SURFACE_ERROR_INVALID_SCALE,
3853 "buffer scale must be at least one "
3854 "('%d' specified)", scale);
3855 return;
3856 }
3857
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003858 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003859 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003860}
3861
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003862static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003863 surface_destroy,
3864 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04003865 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003866 surface_frame,
3867 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003868 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003869 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02003870 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06003871 surface_set_buffer_scale,
3872 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003873};
3874
3875static void
3876compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003877 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003878{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04003879 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003880 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003881
Kristian Høgsberg18c93002012-01-27 11:58:31 -05003882 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003883 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003884 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003885 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003886 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003887
Jason Ekstranda85118c2013-06-27 20:17:02 -05003888 surface->resource =
3889 wl_resource_create(client, &wl_surface_interface,
3890 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003891 if (surface->resource == NULL) {
3892 weston_surface_destroy(surface);
3893 wl_resource_post_no_memory(resource);
3894 return;
3895 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003896 wl_resource_set_implementation(surface->resource, &surface_interface,
3897 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003898
3899 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003900}
3901
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003902static void
3903destroy_region(struct wl_resource *resource)
3904{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003905 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003906
3907 pixman_region32_fini(&region->region);
3908 free(region);
3909}
3910
3911static void
3912region_destroy(struct wl_client *client, struct wl_resource *resource)
3913{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003914 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003915}
3916
3917static void
3918region_add(struct wl_client *client, struct wl_resource *resource,
3919 int32_t x, int32_t y, int32_t width, int32_t height)
3920{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003921 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003922
3923 pixman_region32_union_rect(&region->region, &region->region,
3924 x, y, width, height);
3925}
3926
3927static void
3928region_subtract(struct wl_client *client, struct wl_resource *resource,
3929 int32_t x, int32_t y, int32_t width, int32_t height)
3930{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003931 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003932 pixman_region32_t rect;
3933
3934 pixman_region32_init_rect(&rect, x, y, width, height);
3935 pixman_region32_subtract(&region->region, &region->region, &rect);
3936 pixman_region32_fini(&rect);
3937}
3938
3939static const struct wl_region_interface region_interface = {
3940 region_destroy,
3941 region_add,
3942 region_subtract
3943};
3944
3945static void
3946compositor_create_region(struct wl_client *client,
3947 struct wl_resource *resource, uint32_t id)
3948{
3949 struct weston_region *region;
3950
3951 region = malloc(sizeof *region);
3952 if (region == NULL) {
3953 wl_resource_post_no_memory(resource);
3954 return;
3955 }
3956
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003957 pixman_region32_init(&region->region);
3958
Jason Ekstranda85118c2013-06-27 20:17:02 -05003959 region->resource =
3960 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003961 if (region->resource == NULL) {
3962 free(region);
3963 wl_resource_post_no_memory(resource);
3964 return;
3965 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003966 wl_resource_set_implementation(region->resource, &region_interface,
3967 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003968}
3969
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003970static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003971 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003972 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003973};
3974
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003975static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003976weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3977{
3978 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003979
Jason Ekstrand7b982072014-05-20 14:33:03 -05003980 weston_surface_commit_state(surface, &sub->cached);
3981 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003982
3983 weston_surface_commit_subsurface_order(surface);
3984
3985 weston_surface_schedule_repaint(surface);
3986
Jason Ekstrand7b982072014-05-20 14:33:03 -05003987 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003988}
3989
3990static void
3991weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3992{
3993 struct weston_surface *surface = sub->surface;
3994
3995 /*
3996 * If this commit would cause the surface to move by the
3997 * attach(dx, dy) parameters, the old damage region must be
3998 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05003999 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03004000 */
Derek Foreman152254b2015-11-26 14:17:48 -06004001 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004002 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06004003 pixman_region32_union(&sub->cached.damage_surface,
4004 &sub->cached.damage_surface,
4005 &surface->pending.damage_surface);
4006 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004007
4008 if (surface->pending.newly_attached) {
4009 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05004010 weston_surface_state_set_buffer(&sub->cached,
4011 surface->pending.buffer);
4012 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004013 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04004014 weston_presentation_feedback_discard_list(
4015 &sub->cached.feedback_list);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03004016 /* zwp_surface_synchronization_v1.set_acquire_fence */
4017 fd_move(&sub->cached.acquire_fence_fd,
4018 &surface->pending.acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03004019 /* zwp_surface_synchronization_v1.get_release */
4020 weston_buffer_release_move(&sub->cached.buffer_release_ref,
4021 &surface->pending.buffer_release_ref);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004022 }
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05304023 sub->cached.desired_protection = surface->pending.desired_protection;
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05304024 sub->cached.protection_mode = surface->pending.protection_mode;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03004025 assert(surface->pending.acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03004026 assert(surface->pending.buffer_release_ref.buffer_release == NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004027 sub->cached.sx += surface->pending.sx;
4028 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02004029
Derek Foreman152254b2015-11-26 14:17:48 -06004030 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
4031
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004032 sub->cached.buffer_viewport.changed |=
4033 surface->pending.buffer_viewport.changed;
4034 sub->cached.buffer_viewport.buffer =
4035 surface->pending.buffer_viewport.buffer;
4036 sub->cached.buffer_viewport.surface =
4037 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004038
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004039 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004040
4041 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
4042
4043 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
4044
4045 wl_list_insert_list(&sub->cached.frame_callback_list,
4046 &surface->pending.frame_callback_list);
4047 wl_list_init(&surface->pending.frame_callback_list);
4048
Pekka Paalanen133e4392014-09-23 22:08:46 -04004049 wl_list_insert_list(&sub->cached.feedback_list,
4050 &surface->pending.feedback_list);
4051 wl_list_init(&surface->pending.feedback_list);
4052
Jason Ekstrand7b982072014-05-20 14:33:03 -05004053 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004054}
4055
Derek Foreman280e7dd2014-10-03 13:13:42 -05004056static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03004057weston_subsurface_is_synchronized(struct weston_subsurface *sub)
4058{
4059 while (sub) {
4060 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05004061 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004062
4063 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05004064 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004065
4066 sub = weston_surface_to_subsurface(sub->parent);
4067 }
4068
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01004069 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004070}
4071
4072static void
4073weston_subsurface_commit(struct weston_subsurface *sub)
4074{
4075 struct weston_surface *surface = sub->surface;
4076 struct weston_subsurface *tmp;
4077
4078 /* Recursive check for effectively synchronized. */
4079 if (weston_subsurface_is_synchronized(sub)) {
4080 weston_subsurface_commit_to_cache(sub);
4081 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05004082 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03004083 /* flush accumulated state from cache */
4084 weston_subsurface_commit_to_cache(sub);
4085 weston_subsurface_commit_from_cache(sub);
4086 } else {
4087 weston_surface_commit(surface);
4088 }
4089
4090 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
4091 if (tmp->surface != surface)
4092 weston_subsurface_parent_commit(tmp, 0);
4093 }
4094 }
4095}
4096
4097static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004098weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004099{
4100 struct weston_surface *surface = sub->surface;
4101 struct weston_subsurface *tmp;
4102
Pekka Paalanene67858b2013-04-25 13:57:42 +03004103 /* From now on, commit_from_cache the whole sub-tree, regardless of
4104 * the synchronized mode of each child. This sub-surface or some
4105 * of its ancestors were synchronized, so we are synchronized
4106 * all the way down.
4107 */
4108
Jason Ekstrand7b982072014-05-20 14:33:03 -05004109 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004110 weston_subsurface_commit_from_cache(sub);
4111
4112 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
4113 if (tmp->surface != surface)
4114 weston_subsurface_parent_commit(tmp, 1);
4115 }
4116}
4117
4118static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004119weston_subsurface_parent_commit(struct weston_subsurface *sub,
4120 int parent_is_synchronized)
4121{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004122 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004123 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004124 wl_list_for_each(view, &sub->surface->views, surface_link)
4125 weston_view_set_position(view,
4126 sub->position.x,
4127 sub->position.y);
4128
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004129 sub->position.set = 0;
4130 }
4131
4132 if (parent_is_synchronized || sub->synchronized)
4133 weston_subsurface_synchronized_commit(sub);
4134}
4135
Pekka Paalanen8274d902014-08-06 19:36:51 +03004136static int
4137subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
4138{
4139 return snprintf(buf, len, "sub-surface");
4140}
4141
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004142static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004143subsurface_committed(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004144{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004145 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004146
Jason Ekstranda7af7042013-10-12 22:38:11 -05004147 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004148 weston_view_set_position(view,
4149 view->geometry.x + dx,
4150 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004151
4152 /* No need to check parent mappedness, because if parent is not
4153 * mapped, parent is not in a visible layer, so this sub-surface
4154 * will not be drawn either.
4155 */
Armin Krezovićf8486c32016-06-30 06:04:28 +02004156
Pekka Paalanene67858b2013-04-25 13:57:42 +03004157 if (!weston_surface_is_mapped(surface)) {
Armin Krezovićf8486c32016-06-30 06:04:28 +02004158 surface->is_mapped = true;
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03004159
Derek Foreman4b1a0a12014-09-10 15:37:33 -05004160 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03004161 * because that would call it also for the parent surface,
4162 * which might not be mapped yet. That would lead to
4163 * inconsistent state, where the window could never be
4164 * mapped.
4165 *
Armin Krezovićf8486c32016-06-30 06:04:28 +02004166 * Instead just force the is_mapped flag on, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03004167 * weston_surface_is_mapped() return true, so that when the
4168 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03004169 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03004170 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004171 }
4172}
4173
4174static struct weston_subsurface *
4175weston_surface_to_subsurface(struct weston_surface *surface)
4176{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004177 if (surface->committed == subsurface_committed)
4178 return surface->committed_private;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004179
4180 return NULL;
4181}
4182
Pekka Paalanen01388e22013-04-25 13:57:44 +03004183WL_EXPORT struct weston_surface *
4184weston_surface_get_main_surface(struct weston_surface *surface)
4185{
4186 struct weston_subsurface *sub;
4187
4188 while (surface && (sub = weston_surface_to_subsurface(surface)))
4189 surface = sub->parent;
4190
4191 return surface;
4192}
4193
Pekka Paalanen50b67472014-10-01 15:02:41 +03004194WL_EXPORT int
4195weston_surface_set_role(struct weston_surface *surface,
4196 const char *role_name,
4197 struct wl_resource *error_resource,
4198 uint32_t error_code)
4199{
4200 assert(role_name);
4201
4202 if (surface->role_name == NULL ||
4203 surface->role_name == role_name ||
4204 strcmp(surface->role_name, role_name) == 0) {
4205 surface->role_name = role_name;
4206
4207 return 0;
4208 }
4209
4210 wl_resource_post_error(error_resource, error_code,
4211 "Cannot assign role %s to wl_surface@%d,"
4212 " already has role %s\n",
4213 role_name,
4214 wl_resource_get_id(surface->resource),
4215 surface->role_name);
4216 return -1;
4217}
4218
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02004219WL_EXPORT const char *
4220weston_surface_get_role(struct weston_surface *surface)
4221{
4222 return surface->role_name;
4223}
4224
Pekka Paalanen8274d902014-08-06 19:36:51 +03004225WL_EXPORT void
4226weston_surface_set_label_func(struct weston_surface *surface,
4227 int (*desc)(struct weston_surface *,
4228 char *, size_t))
4229{
4230 surface->get_label = desc;
Marius Vlad5de92972019-10-15 13:25:41 +03004231 weston_timeline_refresh_subscription_objects(surface->compositor,
4232 surface);
Pekka Paalanen8274d902014-08-06 19:36:51 +03004233}
4234
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004235/** Get the size of surface contents
4236 *
4237 * \param surface The surface to query.
4238 * \param width Returns the width of raw contents.
4239 * \param height Returns the height of raw contents.
4240 *
4241 * Retrieves the raw surface content size in pixels for the given surface.
4242 * This is the whole content size in buffer pixels. If the surface
4243 * has no content or the renderer does not implement this feature,
4244 * zeroes are returned.
4245 *
4246 * This function is used to determine the buffer size needed for
4247 * a weston_surface_copy_content() call.
4248 */
4249WL_EXPORT void
4250weston_surface_get_content_size(struct weston_surface *surface,
4251 int *width, int *height)
4252{
4253 struct weston_renderer *rer = surface->compositor->renderer;
4254
4255 if (!rer->surface_get_content_size) {
4256 *width = 0;
4257 *height = 0;
4258 return;
4259 }
4260
4261 rer->surface_get_content_size(surface, width, height);
4262}
4263
Quentin Glidic248dd102016-08-12 10:41:34 +02004264/** Get the bounding box of a surface and its subsurfaces
4265 *
4266 * \param surface The surface to query.
4267 * \return The bounding box relative to the surface origin.
4268 *
4269 */
4270WL_EXPORT struct weston_geometry
4271weston_surface_get_bounding_box(struct weston_surface *surface)
4272{
4273 pixman_region32_t region;
4274 pixman_box32_t *box;
4275 struct weston_subsurface *subsurface;
4276
4277 pixman_region32_init_rect(&region,
4278 0, 0,
4279 surface->width, surface->height);
4280
4281 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link)
4282 pixman_region32_union_rect(&region, &region,
4283 subsurface->position.x,
4284 subsurface->position.y,
4285 subsurface->surface->width,
4286 subsurface->surface->height);
4287
4288 box = pixman_region32_extents(&region);
4289 struct weston_geometry geometry = {
4290 .x = box->x1,
4291 .y = box->y1,
4292 .width = box->x2 - box->x1,
4293 .height = box->y2 - box->y1,
4294 };
4295
4296 pixman_region32_fini(&region);
4297
4298 return geometry;
4299}
4300
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004301/** Copy surface contents to system memory.
4302 *
4303 * \param surface The surface to copy from.
4304 * \param target Pointer to the target memory buffer.
4305 * \param size Size of the target buffer in bytes.
4306 * \param src_x X location on contents to copy from.
4307 * \param src_y Y location on contents to copy from.
4308 * \param width Width in pixels of the area to copy.
4309 * \param height Height in pixels of the area to copy.
4310 * \return 0 for success, -1 for failure.
4311 *
4312 * Surface contents are maintained by the renderer. They can be in a
4313 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
4314 * else.
4315 *
4316 * Surface contents are copied into memory pointed to by target,
4317 * which has size bytes of space available. The target memory
4318 * may be larger than needed, but being smaller returns an error.
4319 * The extra bytes in target may or may not be written; their content is
4320 * unspecified. Size must be large enough to hold the image.
4321 *
4322 * The image in the target memory will be arranged in rows from
4323 * top to bottom, and pixels on a row from left to right. The pixel
4324 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
4325 * width * 4.
4326 *
4327 * Parameters src_x and src_y define the upper-left corner in buffer
4328 * coordinates (pixels) to copy from. Parameters width and height
4329 * define the size of the area to copy in pixels.
4330 *
4331 * The rectangle defined by src_x, src_y, width, height must fit in
4332 * the surface contents. Otherwise an error is returned.
4333 *
Changwoo Chof97d2502017-08-05 00:30:47 +09004334 * Use weston_surface_get_content_size to determine the content size; the
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004335 * needed target buffer size and rectangle limits.
4336 *
4337 * CURRENT IMPLEMENTATION RESTRICTIONS:
4338 * - the machine must be little-endian due to Pixman formats.
4339 *
4340 * NOTE: Pixman formats are premultiplied.
4341 */
4342WL_EXPORT int
4343weston_surface_copy_content(struct weston_surface *surface,
4344 void *target, size_t size,
4345 int src_x, int src_y,
4346 int width, int height)
4347{
4348 struct weston_renderer *rer = surface->compositor->renderer;
4349 int cw, ch;
4350 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
4351
4352 if (!rer->surface_copy_content)
4353 return -1;
4354
4355 weston_surface_get_content_size(surface, &cw, &ch);
4356
4357 if (src_x < 0 || src_y < 0)
4358 return -1;
4359
4360 if (width <= 0 || height <= 0)
4361 return -1;
4362
4363 if (src_x + width > cw || src_y + height > ch)
4364 return -1;
4365
4366 if (width * bytespp * height > size)
4367 return -1;
4368
4369 return rer->surface_copy_content(surface, target, size,
4370 src_x, src_y, width, height);
4371}
4372
Pekka Paalanene67858b2013-04-25 13:57:42 +03004373static void
4374subsurface_set_position(struct wl_client *client,
4375 struct wl_resource *resource, int32_t x, int32_t y)
4376{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004377 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004378
4379 if (!sub)
4380 return;
4381
4382 sub->position.x = x;
4383 sub->position.y = y;
4384 sub->position.set = 1;
4385}
4386
4387static struct weston_subsurface *
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004388subsurface_find_sibling(struct weston_subsurface *sub,
4389 struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004390{
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004391 struct weston_surface *parent = sub->parent;
4392 struct weston_subsurface *sibling;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004393
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004394 wl_list_for_each(sibling, &parent->subsurface_list, parent_link) {
4395 if (sibling->surface == surface && sibling != sub)
4396 return sibling;
4397 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004398
4399 return NULL;
4400}
4401
4402static struct weston_subsurface *
4403subsurface_sibling_check(struct weston_subsurface *sub,
4404 struct weston_surface *surface,
4405 const char *request)
4406{
4407 struct weston_subsurface *sibling;
4408
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004409 sibling = subsurface_find_sibling(sub, surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004410 if (!sibling) {
4411 wl_resource_post_error(sub->resource,
4412 WL_SUBSURFACE_ERROR_BAD_SURFACE,
4413 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004414 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004415 return NULL;
4416 }
4417
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004418 assert(sibling->parent == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004419
4420 return sibling;
4421}
4422
4423static void
4424subsurface_place_above(struct wl_client *client,
4425 struct wl_resource *resource,
4426 struct wl_resource *sibling_resource)
4427{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004428 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004429 struct weston_surface *surface =
4430 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004431 struct weston_subsurface *sibling;
4432
4433 if (!sub)
4434 return;
4435
4436 sibling = subsurface_sibling_check(sub, surface, "place_above");
4437 if (!sibling)
4438 return;
4439
4440 wl_list_remove(&sub->parent_link_pending);
4441 wl_list_insert(sibling->parent_link_pending.prev,
4442 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004443
4444 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004445}
4446
4447static void
4448subsurface_place_below(struct wl_client *client,
4449 struct wl_resource *resource,
4450 struct wl_resource *sibling_resource)
4451{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004452 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004453 struct weston_surface *surface =
4454 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004455 struct weston_subsurface *sibling;
4456
4457 if (!sub)
4458 return;
4459
4460 sibling = subsurface_sibling_check(sub, surface, "place_below");
4461 if (!sibling)
4462 return;
4463
4464 wl_list_remove(&sub->parent_link_pending);
4465 wl_list_insert(&sibling->parent_link_pending,
4466 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004467
4468 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004469}
4470
4471static void
4472subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
4473{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004474 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004475
4476 if (sub)
4477 sub->synchronized = 1;
4478}
4479
4480static void
4481subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
4482{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004483 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004484
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004485 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03004486 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004487
4488 /* If sub became effectively desynchronized, flush. */
4489 if (!weston_subsurface_is_synchronized(sub))
4490 weston_subsurface_synchronized_commit(sub);
4491 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004492}
4493
4494static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03004495weston_subsurface_unlink_parent(struct weston_subsurface *sub)
4496{
4497 wl_list_remove(&sub->parent_link);
4498 wl_list_remove(&sub->parent_link_pending);
4499 wl_list_remove(&sub->parent_destroy_listener.link);
4500 sub->parent = NULL;
4501}
4502
4503static void
4504weston_subsurface_destroy(struct weston_subsurface *sub);
4505
4506static void
4507subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
4508{
4509 struct weston_subsurface *sub =
4510 container_of(listener, struct weston_subsurface,
4511 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004512 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004513
4514 /* The protocol object (wl_resource) is left inert. */
4515 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004516 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004517
4518 weston_subsurface_destroy(sub);
4519}
4520
4521static void
4522subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
4523{
4524 struct weston_subsurface *sub =
4525 container_of(listener, struct weston_subsurface,
4526 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004527 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004528 assert(sub->surface != sub->parent);
4529
4530 if (weston_surface_is_mapped(sub->surface))
4531 weston_surface_unmap(sub->surface);
4532
4533 weston_subsurface_unlink_parent(sub);
4534}
4535
4536static void
4537subsurface_resource_destroy(struct wl_resource *resource)
4538{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004539 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004540
4541 if (sub)
4542 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004543}
4544
4545static void
4546subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
4547{
4548 wl_resource_destroy(resource);
4549}
4550
4551static void
4552weston_subsurface_link_parent(struct weston_subsurface *sub,
4553 struct weston_surface *parent)
4554{
4555 sub->parent = parent;
4556 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004557 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004558 &sub->parent_destroy_listener);
4559
4560 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4561 wl_list_insert(&parent->subsurface_list_pending,
4562 &sub->parent_link_pending);
4563}
4564
4565static void
4566weston_subsurface_link_surface(struct weston_subsurface *sub,
4567 struct weston_surface *surface)
4568{
4569 sub->surface = surface;
4570 sub->surface_destroy_listener.notify =
4571 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004572 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004573 &sub->surface_destroy_listener);
4574}
4575
4576static void
4577weston_subsurface_destroy(struct weston_subsurface *sub)
4578{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004579 struct weston_view *view, *next;
4580
Pekka Paalanene67858b2013-04-25 13:57:42 +03004581 assert(sub->surface);
4582
4583 if (sub->resource) {
4584 assert(weston_surface_to_subsurface(sub->surface) == sub);
4585 assert(sub->parent_destroy_listener.notify ==
4586 subsurface_handle_parent_destroy);
4587
George Kiagiadakised04d382014-06-13 18:10:26 +02004588 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
4589 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004590 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02004591 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05004592
Pekka Paalanene67858b2013-04-25 13:57:42 +03004593 if (sub->parent)
4594 weston_subsurface_unlink_parent(sub);
4595
Jason Ekstrand7b982072014-05-20 14:33:03 -05004596 weston_surface_state_fini(&sub->cached);
4597 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004598
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004599 sub->surface->committed = NULL;
4600 sub->surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004601 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004602 } else {
4603 /* the dummy weston_subsurface for the parent itself */
4604 assert(sub->parent_destroy_listener.notify == NULL);
4605 wl_list_remove(&sub->parent_link);
4606 wl_list_remove(&sub->parent_link_pending);
4607 }
4608
4609 wl_list_remove(&sub->surface_destroy_listener.link);
4610 free(sub);
4611}
4612
4613static const struct wl_subsurface_interface subsurface_implementation = {
4614 subsurface_destroy,
4615 subsurface_set_position,
4616 subsurface_place_above,
4617 subsurface_place_below,
4618 subsurface_set_sync,
4619 subsurface_set_desync
4620};
4621
4622static struct weston_subsurface *
4623weston_subsurface_create(uint32_t id, struct weston_surface *surface,
4624 struct weston_surface *parent)
4625{
4626 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004627 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004628
Bryce Harringtonde16d892014-11-20 22:21:57 -08004629 sub = zalloc(sizeof *sub);
4630 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004631 return NULL;
4632
Jason Ekstranda7af7042013-10-12 22:38:11 -05004633 wl_list_init(&sub->unused_views);
4634
Jason Ekstranda85118c2013-06-27 20:17:02 -05004635 sub->resource =
4636 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004637 if (!sub->resource) {
4638 free(sub);
4639 return NULL;
4640 }
4641
Jason Ekstranda85118c2013-06-27 20:17:02 -05004642 wl_resource_set_implementation(sub->resource,
4643 &subsurface_implementation,
4644 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004645 weston_subsurface_link_surface(sub, surface);
4646 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05004647 weston_surface_state_init(&sub->cached);
4648 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004649 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004650
4651 return sub;
4652}
4653
4654/* Create a dummy subsurface for having the parent itself in its
4655 * sub-surface lists. Makes stacking order manipulation easy.
4656 */
4657static struct weston_subsurface *
4658weston_subsurface_create_for_parent(struct weston_surface *parent)
4659{
4660 struct weston_subsurface *sub;
4661
Bryce Harringtonde16d892014-11-20 22:21:57 -08004662 sub = zalloc(sizeof *sub);
4663 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004664 return NULL;
4665
4666 weston_subsurface_link_surface(sub, parent);
4667 sub->parent = parent;
4668 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4669 wl_list_insert(&parent->subsurface_list_pending,
4670 &sub->parent_link_pending);
4671
4672 return sub;
4673}
4674
4675static void
4676subcompositor_get_subsurface(struct wl_client *client,
4677 struct wl_resource *resource,
4678 uint32_t id,
4679 struct wl_resource *surface_resource,
4680 struct wl_resource *parent_resource)
4681{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004682 struct weston_surface *surface =
4683 wl_resource_get_user_data(surface_resource);
4684 struct weston_surface *parent =
4685 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004686 struct weston_subsurface *sub;
4687 static const char where[] = "get_subsurface: wl_subsurface@";
4688
4689 if (surface == parent) {
4690 wl_resource_post_error(resource,
4691 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4692 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004693 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004694 return;
4695 }
4696
4697 if (weston_surface_to_subsurface(surface)) {
4698 wl_resource_post_error(resource,
4699 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4700 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004701 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004702 return;
4703 }
4704
Pekka Paalanen50b67472014-10-01 15:02:41 +03004705 if (weston_surface_set_role(surface, "wl_subsurface", resource,
4706 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004707 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004708
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004709 if (weston_surface_get_main_surface(parent) == surface) {
4710 wl_resource_post_error(resource,
4711 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4712 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004713 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004714 return;
4715 }
4716
Pekka Paalanene67858b2013-04-25 13:57:42 +03004717 /* make sure the parent is in its own list */
4718 if (wl_list_empty(&parent->subsurface_list)) {
4719 if (!weston_subsurface_create_for_parent(parent)) {
4720 wl_resource_post_no_memory(resource);
4721 return;
4722 }
4723 }
4724
4725 sub = weston_subsurface_create(id, surface, parent);
4726 if (!sub) {
4727 wl_resource_post_no_memory(resource);
4728 return;
4729 }
4730
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004731 surface->committed = subsurface_committed;
4732 surface->committed_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004733 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004734}
4735
4736static void
4737subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
4738{
4739 wl_resource_destroy(resource);
4740}
4741
4742static const struct wl_subcompositor_interface subcompositor_interface = {
4743 subcompositor_destroy,
4744 subcompositor_get_subsurface
4745};
4746
4747static void
4748bind_subcompositor(struct wl_client *client,
4749 void *data, uint32_t version, uint32_t id)
4750{
4751 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004752 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004753
Jason Ekstranda85118c2013-06-27 20:17:02 -05004754 resource =
4755 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004756 if (resource == NULL) {
4757 wl_client_post_no_memory(client);
4758 return;
4759 }
4760 wl_resource_set_implementation(resource, &subcompositor_interface,
4761 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004762}
4763
Bryce Harrington0795ece2016-08-30 12:04:26 -07004764/** Set a DPMS mode on all of the compositor's outputs
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004765 *
4766 * \param compositor The compositor instance
4767 * \param state The DPMS state the outputs will be set to
4768 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004769static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004770weston_compositor_dpms(struct weston_compositor *compositor,
4771 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004772{
4773 struct weston_output *output;
4774
Bryce Harrington08976ac2016-08-30 12:05:16 -07004775 wl_list_for_each(output, &compositor->output_list, link)
4776 if (output->set_dpms)
4777 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004778}
4779
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004780/** Restores the compositor to active status
4781 *
4782 * \param compositor The compositor instance
4783 *
4784 * If the compositor was in a sleeping mode, all outputs are powered
4785 * back on via DPMS. Otherwise if the compositor was inactive
4786 * (idle/locked, offscreen, or sleeping) then the compositor's wake
4787 * signal will fire.
4788 *
4789 * Restarts the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004790 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004791 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004792WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004793weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004794{
Neil Roberts8b62e202013-09-30 13:14:47 +01004795 uint32_t old_state = compositor->state;
4796
4797 /* The state needs to be changed before emitting the wake
4798 * signal because that may try to schedule a repaint which
4799 * will not work if the compositor is still sleeping */
4800 compositor->state = WESTON_COMPOSITOR_ACTIVE;
4801
4802 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004803 case WESTON_COMPOSITOR_SLEEPING:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004804 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004805 case WESTON_COMPOSITOR_OFFSCREEN:
Daniel Stone893b9362016-11-08 15:47:09 +00004806 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004807 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004808 /* fall through */
4809 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004810 wl_event_source_timer_update(compositor->idle_source,
4811 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004812 }
4813}
4814
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004815/** Turns off rendering and frame events for the compositor.
4816 *
4817 * \param compositor The compositor instance
4818 *
4819 * This is used for example to prevent further rendering while the
4820 * compositor is shutting down.
4821 *
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004822 * Stops the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004823 *
4824 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004825 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004826WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004827weston_compositor_offscreen(struct weston_compositor *compositor)
4828{
4829 switch (compositor->state) {
4830 case WESTON_COMPOSITOR_OFFSCREEN:
4831 return;
4832 case WESTON_COMPOSITOR_SLEEPING:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004833 default:
4834 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4835 wl_event_source_timer_update(compositor->idle_source, 0);
4836 }
4837}
4838
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004839/** Powers down all attached output devices
4840 *
4841 * \param compositor The compositor instance
4842 *
4843 * Causes rendering to the outputs to cease, and no frame events to be
4844 * sent. Only powers down the outputs if the compositor is not already
4845 * in sleep mode.
4846 *
4847 * Stops the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004848 *
4849 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004850 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004851WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004852weston_compositor_sleep(struct weston_compositor *compositor)
4853{
4854 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
4855 return;
4856
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004857 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004858 compositor->state = WESTON_COMPOSITOR_SLEEPING;
4859 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
4860}
4861
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004862/** Sets compositor to idle mode
4863 *
4864 * \param data The compositor instance
4865 *
4866 * This is called when the idle timer fires. Once the compositor is in
4867 * idle mode it requires a wake action (e.g. via
4868 * weston_compositor_wake()) to restore it. The compositor's
4869 * idle_signal will be triggered when the idle event occurs.
4870 *
4871 * Idleness can be inhibited by setting the compositor's idle_inhibit
4872 * property.
4873 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004874static int
4875idle_handler(void *data)
4876{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004877 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004878
4879 if (compositor->idle_inhibit)
4880 return 1;
4881
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004882 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004883 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004884
4885 return 1;
4886}
4887
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004888WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08004889weston_plane_init(struct weston_plane *plane,
4890 struct weston_compositor *ec,
4891 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004892{
4893 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004894 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004895 plane->x = x;
4896 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08004897 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004898
4899 /* Init the link so that the call to wl_list_remove() when releasing
4900 * the plane without ever stacking doesn't lead to a crash */
4901 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004902}
4903
4904WL_EXPORT void
4905weston_plane_release(struct weston_plane *plane)
4906{
Xiong Zhang97116532013-10-23 13:58:31 +08004907 struct weston_view *view;
4908
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004909 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004910 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004911
Xiong Zhang97116532013-10-23 13:58:31 +08004912 wl_list_for_each(view, &plane->compositor->view_list, link) {
4913 if (view->plane == plane)
4914 view->plane = NULL;
4915 }
4916
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004917 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004918}
4919
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004920/** weston_compositor_stack_plane
4921 * \ingroup compositor
4922 */
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004923WL_EXPORT void
4924weston_compositor_stack_plane(struct weston_compositor *ec,
4925 struct weston_plane *plane,
4926 struct weston_plane *above)
4927{
4928 if (above)
4929 wl_list_insert(above->link.prev, &plane->link);
4930 else
4931 wl_list_insert(&ec->plane_list, &plane->link);
4932}
4933
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004934static void
4935output_release(struct wl_client *client, struct wl_resource *resource)
4936{
4937 wl_resource_destroy(resource);
4938}
4939
4940static const struct wl_output_interface output_interface = {
4941 output_release,
4942};
4943
4944
Casey Dahlin9074db52012-04-19 22:50:09 -04004945static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004946{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004947 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004948}
4949
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004950static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004951bind_output(struct wl_client *client,
4952 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05004953{
Pekka Paalanen05347622017-03-27 12:24:34 +03004954 struct weston_head *head = data;
4955 struct weston_output *output = head->output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004956 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004957 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004958
Jason Ekstranda85118c2013-06-27 20:17:02 -05004959 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004960 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004961 if (resource == NULL) {
4962 wl_client_post_no_memory(client);
4963 return;
4964 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004965
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03004966 wl_list_insert(&head->resource_list, wl_resource_get_link(resource));
Pekka Paalanen055c1132017-03-27 16:31:25 +03004967 wl_resource_set_implementation(resource, &output_interface, head,
Pekka Paalanen05347622017-03-27 12:24:34 +03004968 unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04004969
Pekka Paalanen05347622017-03-27 12:24:34 +03004970 assert(output);
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004971 wl_output_send_geometry(resource,
4972 output->x,
4973 output->y,
Pekka Paalanen01f60212017-03-24 15:39:24 +02004974 head->mm_width,
4975 head->mm_height,
4976 head->subpixel,
4977 head->make, head->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04004978 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004979 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004980 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02004981 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004982
4983 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004984 wl_output_send_mode(resource,
4985 mode->flags,
4986 mode->width,
4987 mode->height,
4988 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004989 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02004990
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004991 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004992 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004993}
4994
Pekka Paalanendcac3512017-12-08 14:13:34 +02004995static void
4996weston_head_add_global(struct weston_head *head)
4997{
4998 head->global = wl_global_create(head->compositor->wl_display,
4999 &wl_output_interface, 3,
5000 head, bind_output);
5001}
5002
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02005003/** Remove the global wl_output protocol object
5004 *
5005 * \param head The head whose global to remove.
5006 *
5007 * Also orphans the wl_resources for this head (wl_output).
5008 */
5009static void
5010weston_head_remove_global(struct weston_head *head)
5011{
5012 struct wl_resource *resource, *tmp;
5013
5014 if (head->global)
5015 wl_global_destroy(head->global);
5016 head->global = NULL;
5017
5018 wl_resource_for_each_safe(resource, tmp, &head->resource_list) {
5019 unbind_resource(resource);
5020 wl_resource_set_destructor(resource, NULL);
5021 wl_resource_set_user_data(resource, NULL);
5022 }
Roman Gilge97391c2019-03-29 13:01:06 +01005023
5024 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
5025 /* It's sufficient to unset the destructor, then the list elements
5026 * won't be accessed.
5027 */
5028 wl_resource_set_destructor(resource, NULL);
5029 }
5030 wl_list_init(&head->xdg_output_resource_list);
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02005031}
5032
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005033/** Get the backing object of wl_output
5034 *
5035 * \param resource A wl_output protocol object.
5036 * \return The backing object (user data) of a wl_resource representing a
5037 * wl_output protocol object.
Marius Vlad78984ee2019-06-11 00:05:08 +03005038 *
5039 * \ingroup head
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005040 */
Pekka Paalanen055c1132017-03-27 16:31:25 +03005041WL_EXPORT struct weston_head *
5042weston_head_from_resource(struct wl_resource *resource)
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005043{
5044 assert(wl_resource_instance_of(resource, &wl_output_interface,
5045 &output_interface));
5046
5047 return wl_resource_get_user_data(resource);
5048}
5049
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005050/** Initialize a pre-allocated weston_head
5051 *
5052 * \param head The head to initialize.
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005053 * \param name The head name, e.g. the connector name or equivalent.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005054 *
5055 * The head will be safe to attach, detach and release.
5056 *
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005057 * The name is used in logs, and can be used by compositors as a configuration
5058 * identifier.
5059 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005060 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005061 * \internal
5062 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005063WL_EXPORT void
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005064weston_head_init(struct weston_head *head, const char *name)
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005065{
5066 /* Add some (in)sane defaults which can be used
5067 * for checking if an output was properly configured
5068 */
5069 memset(head, 0, sizeof *head);
5070
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005071 wl_list_init(&head->compositor_link);
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005072 wl_signal_init(&head->destroy_signal);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005073 wl_list_init(&head->output_link);
5074 wl_list_init(&head->resource_list);
Roman Gilge97391c2019-03-29 13:01:06 +01005075 wl_list_init(&head->xdg_output_resource_list);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005076 head->name = strdup(name);
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305077 head->current_protection = WESTON_HDCP_DISABLE;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005078}
5079
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005080/** Send output heads changed signal
5081 *
5082 * \param output The output that changed.
5083 *
5084 * Notify that the enabled output gained and/or lost heads, or that the
5085 * associated heads may have changed their connection status. This does not
5086 * include cases where the output becomes enabled or disabled. The registered
5087 * callbacks are called after the change has successfully happened.
5088 *
5089 * If connection status change causes the compositor to attach or detach a head
5090 * to an enabled output, the registered callbacks may be called multiple times.
Marius Vlad55d87362019-06-11 01:15:35 +03005091 *
5092 * \ingroup output
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005093 */
5094static void
5095weston_output_emit_heads_changed(struct weston_output *output)
5096{
5097 wl_signal_emit(&output->compositor->output_heads_changed_signal,
5098 output);
5099}
5100
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005101/** Idle task for emitting heads_changed_signal */
5102static void
5103weston_compositor_call_heads_changed(void *data)
5104{
5105 struct weston_compositor *compositor = data;
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005106 struct weston_head *head;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005107
5108 compositor->heads_changed_source = NULL;
5109
5110 wl_signal_emit(&compositor->heads_changed_signal, compositor);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005111
5112 wl_list_for_each(head, &compositor->head_list, compositor_link) {
5113 if (head->output && head->output->enabled)
5114 weston_output_emit_heads_changed(head->output);
5115 }
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005116}
5117
5118/** Schedule a call on idle to heads_changed callback
5119 *
5120 * \param compositor The Compositor.
5121 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005122 * \ingroup compositor
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005123 * \internal
5124 */
5125static void
5126weston_compositor_schedule_heads_changed(struct weston_compositor *compositor)
5127{
5128 struct wl_event_loop *loop;
5129
5130 if (compositor->heads_changed_source)
5131 return;
5132
5133 loop = wl_display_get_event_loop(compositor->wl_display);
5134 compositor->heads_changed_source = wl_event_loop_add_idle(loop,
5135 weston_compositor_call_heads_changed, compositor);
5136}
5137
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005138/** Register a new head
5139 *
5140 * \param compositor The compositor.
5141 * \param head The head to register, must not be already registered.
5142 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005143 * This signals the core that a new head has become available, leading to
5144 * heads_changed hook being called later.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005145 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005146 * \ingroup compositor
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005147 * \internal
5148 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005149WL_EXPORT void
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005150weston_compositor_add_head(struct weston_compositor *compositor,
5151 struct weston_head *head)
5152{
5153 assert(wl_list_empty(&head->compositor_link));
5154 assert(head->name);
5155
5156 wl_list_insert(compositor->head_list.prev, &head->compositor_link);
5157 head->compositor = compositor;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005158 weston_compositor_schedule_heads_changed(compositor);
5159}
5160
5161/** Adds a listener to be called when heads change
5162 *
5163 * \param compositor The compositor.
5164 * \param listener The listener to add.
5165 *
Marius Vlada2dace22019-06-12 16:05:44 +03005166 * The listener notify function argument is weston_compositor.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005167 *
5168 * The listener function will be called after heads are added or their
5169 * connection status has changed. Several changes may be accumulated into a
5170 * single call. The user is expected to iterate over the existing heads and
5171 * check their statuses to find out what changed.
5172 *
5173 * \sa weston_compositor_iterate_heads, weston_head_is_connected,
5174 * weston_head_is_enabled
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005175 * \ingroup compositor
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005176 */
5177WL_EXPORT void
5178weston_compositor_add_heads_changed_listener(struct weston_compositor *compositor,
5179 struct wl_listener *listener)
5180{
5181 wl_signal_add(&compositor->heads_changed_signal, listener);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005182}
5183
5184/** Iterate over available heads
5185 *
5186 * \param compositor The compositor.
Marius Vlada2dace22019-06-12 16:05:44 +03005187 * \param iter The iterator, or NULL for start.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005188 * \return The next available head in the list.
5189 *
5190 * Returns all available heads, regardless of being connected or enabled.
5191 *
5192 * You can iterate over all heads as follows:
5193 * \code
5194 * struct weston_head *head = NULL;
5195 *
5196 * while ((head = weston_compositor_iterate_heads(compositor, head))) {
5197 * ...
5198 * }
5199 * \endcode
5200 *
5201 * If you cause \c iter to be removed from the list, you cannot use it to
5202 * continue iterating. Removing any other item is safe.
5203 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005204 * \ingroup compositor
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005205 */
5206WL_EXPORT struct weston_head *
5207weston_compositor_iterate_heads(struct weston_compositor *compositor,
5208 struct weston_head *iter)
5209{
5210 struct wl_list *list = &compositor->head_list;
5211 struct wl_list *node;
5212
5213 assert(compositor);
5214 assert(!iter || iter->compositor == compositor);
5215
5216 if (iter)
5217 node = iter->compositor_link.next;
5218 else
5219 node = list->next;
5220
5221 assert(node);
5222 assert(!iter || node != &iter->compositor_link);
5223
5224 if (node == list)
5225 return NULL;
5226
5227 return container_of(node, struct weston_head, compositor_link);
5228}
5229
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005230/** Iterate over attached heads
5231 *
5232 * \param output The output whose heads to iterate.
Marius Vlada2dace22019-06-12 16:05:44 +03005233 * \param iter The iterator, or NULL for start.
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005234 * \return The next attached head in the list.
5235 *
5236 * Returns all heads currently attached to the output.
5237 *
5238 * You can iterate over all heads as follows:
5239 * \code
5240 * struct weston_head *head = NULL;
5241 *
5242 * while ((head = weston_output_iterate_heads(output, head))) {
5243 * ...
5244 * }
5245 * \endcode
5246 *
5247 * If you cause \c iter to be removed from the list, you cannot use it to
5248 * continue iterating. Removing any other item is safe.
5249 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05005250 * \ingroup output
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005251 */
5252WL_EXPORT struct weston_head *
5253weston_output_iterate_heads(struct weston_output *output,
5254 struct weston_head *iter)
5255{
5256 struct wl_list *list = &output->head_list;
5257 struct wl_list *node;
5258
5259 assert(output);
5260 assert(!iter || iter->output == output);
5261
5262 if (iter)
5263 node = iter->output_link.next;
5264 else
5265 node = list->next;
5266
5267 assert(node);
5268 assert(!iter || node != &iter->output_link);
5269
5270 if (node == list)
5271 return NULL;
5272
5273 return container_of(node, struct weston_head, output_link);
5274}
5275
Pekka Paalanendcac3512017-12-08 14:13:34 +02005276/** Attach a head to an output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005277 *
5278 * \param output The output to attach to.
5279 * \param head The head that is not yet attached.
5280 * \return 0 on success, -1 on failure.
5281 *
5282 * Attaches the given head to the output. All heads of an output are clones
5283 * and share the resolution and timings.
5284 *
5285 * Cloning heads this way uses less resources than creating an output for
5286 * each head, but is not always possible due to environment, driver and hardware
5287 * limitations.
5288 *
5289 * On failure, the head remains unattached. Success of this function does not
5290 * guarantee the output configuration is actually valid. The final checks are
Pekka Paalanendcac3512017-12-08 14:13:34 +02005291 * made on weston_output_enable() unless the output was already enabled.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005292 *
Marius Vlad55d87362019-06-11 01:15:35 +03005293 * \ingroup output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005294 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005295WL_EXPORT int
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005296weston_output_attach_head(struct weston_output *output,
5297 struct weston_head *head)
5298{
Pekka Paalanendcac3512017-12-08 14:13:34 +02005299 char *head_names;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005300
5301 if (!wl_list_empty(&head->output_link))
5302 return -1;
5303
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005304 if (output->attach_head) {
5305 if (output->attach_head(output, head) < 0)
5306 return -1;
5307 } else if (!wl_list_empty(&output->head_list)) {
5308 /* No support for clones in the legacy path. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005309 return -1;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005310 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005311
5312 head->output = output;
5313 wl_list_insert(output->head_list.prev, &head->output_link);
5314
Pekka Paalanendcac3512017-12-08 14:13:34 +02005315 if (output->enabled) {
5316 weston_head_add_global(head);
5317
5318 head_names = weston_output_create_heads_string(output);
5319 weston_log("Output '%s' updated to have head(s) %s\n",
5320 output->name, head_names);
5321 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005322
5323 weston_output_emit_heads_changed(output);
Pekka Paalanendcac3512017-12-08 14:13:34 +02005324 }
5325
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005326 return 0;
5327}
5328
5329/** Detach a head from its output
5330 *
5331 * \param head The head to detach.
5332 *
5333 * It is safe to detach a non-attached head.
5334 *
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005335 * If the head is attached to an enabled output and the output will be left
5336 * with no heads, the output will be disabled.
5337 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005338 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005339 * \sa weston_output_disable
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005340 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005341WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005342weston_head_detach(struct weston_head *head)
5343{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005344 struct weston_output *output = head->output;
Pekka Paalanena0106992017-12-08 16:11:17 +02005345 char *head_names;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005346
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005347 wl_list_remove(&head->output_link);
5348 wl_list_init(&head->output_link);
5349 head->output = NULL;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005350
5351 if (!output)
5352 return;
5353
5354 if (output->detach_head)
5355 output->detach_head(output, head);
5356
5357 if (output->enabled) {
5358 weston_head_remove_global(head);
5359
Pekka Paalanena0106992017-12-08 16:11:17 +02005360 if (wl_list_empty(&output->head_list)) {
5361 weston_log("Output '%s' no heads left, disabling.\n",
5362 output->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005363 weston_output_disable(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02005364 } else {
5365 head_names = weston_output_create_heads_string(output);
5366 weston_log("Output '%s' updated to have head(s) %s\n",
5367 output->name, head_names);
5368 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005369
5370 weston_output_emit_heads_changed(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02005371 }
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005372 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005373}
5374
5375/** Destroy a head
5376 *
5377 * \param head The head to be released.
5378 *
5379 * Destroys the head. The caller is responsible for freeing the memory pointed
5380 * to by \c head.
5381 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005382 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005383 * \internal
5384 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005385WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005386weston_head_release(struct weston_head *head)
5387{
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005388 wl_signal_emit(&head->destroy_signal, head);
5389
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005390 weston_head_detach(head);
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005391
5392 free(head->make);
5393 free(head->model);
5394 free(head->serial_number);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005395 free(head->name);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005396
5397 wl_list_remove(&head->compositor_link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005398}
5399
Pekka Paalanene19970f2017-08-28 14:11:02 +03005400static void
5401weston_head_set_device_changed(struct weston_head *head)
5402{
5403 head->device_changed = true;
5404
5405 if (head->compositor)
5406 weston_compositor_schedule_heads_changed(head->compositor);
5407}
5408
5409/** String equal comparison with NULLs being equal */
5410static bool
5411str_null_eq(const char *a, const char *b)
5412{
5413 if (!a && !b)
5414 return true;
5415
5416 if (!!a != !!b)
5417 return false;
5418
5419 return strcmp(a, b) == 0;
5420}
5421
Pekka Paalanen01f60212017-03-24 15:39:24 +02005422/** Store monitor make, model and serial number
5423 *
5424 * \param head The head to modify.
5425 * \param make The monitor make. If EDID is available, the PNP ID. Otherwise
5426 * any string, or NULL for none.
5427 * \param model The monitor model or name, or a made-up string, or NULL for
5428 * none.
5429 * \param serialno The monitor serial number, a made-up string, or NULL for
5430 * none.
5431 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005432 * This may set the device_changed flag.
5433 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005434 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005435 * \internal
5436 */
5437WL_EXPORT void
5438weston_head_set_monitor_strings(struct weston_head *head,
5439 const char *make,
5440 const char *model,
5441 const char *serialno)
5442{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005443 if (str_null_eq(head->make, make) &&
5444 str_null_eq(head->model, model) &&
5445 str_null_eq(head->serial_number, serialno))
5446 return;
5447
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005448 free(head->make);
5449 free(head->model);
5450 free(head->serial_number);
5451
5452 head->make = make ? strdup(make) : NULL;
5453 head->model = model ? strdup(model) : NULL;
5454 head->serial_number = serialno ? strdup(serialno) : NULL;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005455
5456 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005457}
5458
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005459/** Store display non-desktop status
5460 *
5461 * \param head The head to modify.
5462 * \param non_desktop Whether the head connects to a non-desktop display.
5463 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005464 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005465 * \internal
5466 */
5467WL_EXPORT void
5468weston_head_set_non_desktop(struct weston_head *head, bool non_desktop)
5469{
5470 if (head->non_desktop == non_desktop)
5471 return;
5472
5473 head->non_desktop = non_desktop;
5474
5475 weston_head_set_device_changed(head);
5476}
5477
Lucas Stacha69cb712019-11-25 23:29:31 +00005478/** Store display transformation
5479 *
5480 * \param head The head to modify.
5481 * \param transform The transformation to apply for this head
5482 *
5483 * This may set the device_changed flag.
5484 *
5485 * \ingroup head
5486 * \internal
5487 */
5488WL_EXPORT void
5489weston_head_set_transform(struct weston_head *head, uint32_t transform)
5490{
5491 if (head->transform == transform)
5492 return;
5493
5494 head->transform = transform;
5495
5496 weston_head_set_device_changed(head);
5497}
5498
5499
Pekka Paalanen01f60212017-03-24 15:39:24 +02005500/** Store physical image size
5501 *
5502 * \param head The head to modify.
5503 * \param mm_width Image area width in millimeters.
5504 * \param mm_height Image area height in millimeters.
5505 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005506 * This may set the device_changed flag.
5507 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005508 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005509 * \internal
5510 */
5511WL_EXPORT void
5512weston_head_set_physical_size(struct weston_head *head,
5513 int32_t mm_width, int32_t mm_height)
5514{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005515 if (head->mm_width == mm_width &&
5516 head->mm_height == mm_height)
5517 return;
5518
Pekka Paalanen01f60212017-03-24 15:39:24 +02005519 head->mm_width = mm_width;
5520 head->mm_height = mm_height;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005521
5522 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005523}
5524
5525/** Store monitor sub-pixel layout
5526 *
5527 * \param head The head to modify.
5528 * \param sp Sub-pixel layout. The possible values are:
5529 * - WL_OUTPUT_SUBPIXEL_UNKNOWN,
5530 * - WL_OUTPUT_SUBPIXEL_NONE,
5531 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB,
5532 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR,
5533 * - WL_OUTPUT_SUBPIXEL_VERTICAL_RGB,
5534 * - WL_OUTPUT_SUBPIXEL_VERTICAL_BGR
5535 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005536 * This may set the device_changed flag.
5537 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005538 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005539 * \internal
5540 */
5541WL_EXPORT void
5542weston_head_set_subpixel(struct weston_head *head,
5543 enum wl_output_subpixel sp)
5544{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005545 if (head->subpixel == sp)
5546 return;
5547
Pekka Paalanen01f60212017-03-24 15:39:24 +02005548 head->subpixel = sp;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005549
5550 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005551}
5552
5553/** Mark the monitor as internal
5554 *
5555 * This is used for embedded screens, like laptop panels.
5556 *
5557 * \param head The head to mark as internal.
5558 *
5559 * By default a head is external. The type is often inferred from the physical
5560 * connector type.
5561 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005562 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005563 * \internal
5564 */
5565WL_EXPORT void
5566weston_head_set_internal(struct weston_head *head)
5567{
5568 head->connection_internal = true;
5569}
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005570
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005571/** Store connector status
5572 *
5573 * \param head The head to modify.
5574 * \param connected Whether the head is connected.
5575 *
5576 * Connectors are created as disconnected. This function can be used to
5577 * set the connector status.
5578 *
5579 * The status should be set to true when a physical connector is connected to
5580 * a video sink device like a monitor and to false when the connector is
5581 * disconnected. For nested backends, the connection status should reflect the
5582 * connection to the parent display server.
5583 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005584 * When the connection status changes, it schedules a call to the heads_changed
Pekka Paalanene19970f2017-08-28 14:11:02 +03005585 * hook and sets the device_changed flag.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005586 *
5587 * \sa weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005588 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005589 * \internal
5590 */
5591WL_EXPORT void
5592weston_head_set_connection_status(struct weston_head *head, bool connected)
5593{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005594 if (head->connected == connected)
5595 return;
5596
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005597 head->connected = connected;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005598
Pekka Paalanene19970f2017-08-28 14:11:02 +03005599 weston_head_set_device_changed(head);
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005600}
5601
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305602static void
5603weston_output_compute_protection(struct weston_output *output)
5604{
5605 struct weston_head *head;
5606 enum weston_hdcp_protection op_protection;
5607 bool op_protection_valid = false;
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305608 struct weston_compositor *wc = output->compositor;
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305609
5610 wl_list_for_each(head, &output->head_list, output_link) {
5611 if (!op_protection_valid) {
5612 op_protection = head->current_protection;
5613 op_protection_valid = true;
5614 }
5615 if (head->current_protection < op_protection)
5616 op_protection = head->current_protection;
5617 }
5618
5619 if (!op_protection_valid)
5620 op_protection = WESTON_HDCP_DISABLE;
5621
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305622 if (output->current_protection != op_protection) {
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305623 output->current_protection = op_protection;
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305624 weston_output_damage(output);
Ankit Nautiyalfaa5ab42019-07-08 13:23:24 +05305625 weston_schedule_surface_protection_update(wc);
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305626 }
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305627}
5628
5629WL_EXPORT void
5630weston_head_set_content_protection_status(struct weston_head *head,
5631 enum weston_hdcp_protection status)
5632{
5633 head->current_protection = status;
5634 if (head->output)
5635 weston_output_compute_protection(head->output);
5636}
5637
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005638/** Is the head currently connected?
5639 *
5640 * \param head The head to query.
5641 * \return Connection status.
5642 *
5643 * Returns true if the head is physically connected to a monitor, or in
5644 * case of a nested backend returns true when there is a connection to the
5645 * parent display server.
5646 *
5647 * This is independent from the head being enabled.
5648 *
5649 * \sa weston_head_is_enabled
Marius Vlad78984ee2019-06-11 00:05:08 +03005650 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005651 */
5652WL_EXPORT bool
5653weston_head_is_connected(struct weston_head *head)
5654{
5655 return head->connected;
5656}
5657
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005658/** Is the head currently enabled?
5659 *
5660 * \param head The head to query.
5661 * \return Video status.
5662 *
5663 * Returns true if the head is currently transmitting a video stream.
5664 *
5665 * This is independent of the head being connected.
5666 *
5667 * \sa weston_head_is_connected
Marius Vlad78984ee2019-06-11 00:05:08 +03005668 * \ingroup head
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005669 */
5670WL_EXPORT bool
5671weston_head_is_enabled(struct weston_head *head)
5672{
5673 if (!head->output)
5674 return false;
5675
5676 return head->output->enabled;
5677}
5678
Pekka Paalanene19970f2017-08-28 14:11:02 +03005679/** Has the device information changed?
5680 *
5681 * \param head The head to query.
5682 * \return True if the device information has changed since last reset.
5683 *
5684 * The information about the connected display device, e.g. a monitor, may
5685 * change without being disconnected in between. Changing information
5686 * causes a call to the heads_changed hook.
5687 *
5688 * The information includes make, model, serial number, physical size,
5689 * and sub-pixel type. The connection status is also included.
5690 *
5691 * \sa weston_head_reset_device_changed, weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005692 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03005693 */
5694WL_EXPORT bool
5695weston_head_is_device_changed(struct weston_head *head)
5696{
5697 return head->device_changed;
5698}
5699
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005700/** Does the head represent a non-desktop display?
5701 *
5702 * \param head The head to query.
5703 * \return True if the device is a non-desktop display.
5704 *
5705 * Non-desktop heads are not attached to outputs by default.
5706 * This stops weston from extending the desktop onto head mounted displays.
5707 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005708 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005709 */
5710WL_EXPORT bool
5711weston_head_is_non_desktop(struct weston_head *head)
5712{
5713 return head->non_desktop;
5714}
5715
Pekka Paalanene19970f2017-08-28 14:11:02 +03005716/** Acknowledge device information change
5717 *
5718 * \param head The head to acknowledge.
5719 *
5720 * Clears the device changed flag on this head. When a compositor has processed
5721 * device information, it should call this to be able to notice further
5722 * changes.
5723 *
5724 * \sa weston_head_is_device_changed
Marius Vlad78984ee2019-06-11 00:05:08 +03005725 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03005726 */
5727WL_EXPORT void
5728weston_head_reset_device_changed(struct weston_head *head)
5729{
5730 head->device_changed = false;
5731}
5732
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005733/** Get the name of a head
5734 *
5735 * \param head The head to query.
5736 * \return The head's name, not NULL.
5737 *
5738 * The name depends on the backend. The DRM backend uses connector names,
5739 * other backends may use hardcoded names or user-given names.
Marius Vlad78984ee2019-06-11 00:05:08 +03005740 *
5741 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005742 */
5743WL_EXPORT const char *
5744weston_head_get_name(struct weston_head *head)
5745{
5746 return head->name;
5747}
5748
5749/** Get the output the head is attached to
5750 *
5751 * \param head The head to query.
5752 * \return The output the head is attached to, or NULL if detached.
Marius Vlad78984ee2019-06-11 00:05:08 +03005753 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005754 */
5755WL_EXPORT struct weston_output *
5756weston_head_get_output(struct weston_head *head)
5757{
5758 return head->output;
5759}
5760
Lucas Stacha69cb712019-11-25 23:29:31 +00005761/** Get the head's native transformation
5762 *
5763 * \param head The head to query.
5764 * \return The head's native transform, as a WL_OUTPUT_TRANSFORM_* value
5765 *
5766 * A weston_head may have a 'native' transform provided by the backend.
5767 * Examples include panels which are physically rotated, where the rotation
5768 * is recorded and described as part of the system configuration. This call
5769 * will return any known native transform for the head.
5770 *
5771 * \ingroup head
5772 */
5773WL_EXPORT uint32_t
5774weston_head_get_transform(struct weston_head *head)
5775{
5776 return head->transform;
5777}
5778
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005779/** Add destroy callback for a head
5780 *
5781 * \param head The head to watch for.
5782 * \param listener The listener to add. The \c notify member must be set.
5783 *
5784 * Heads may get destroyed for various reasons by the backends. If a head is
5785 * attached to an output, the compositor should listen for head destruction
5786 * and reconfigure or destroy the output if necessary.
5787 *
5788 * The destroy callbacks will be called on weston_head destruction before any
5789 * automatic detaching from an associated weston_output and before any
5790 * weston_head information is lost.
5791 *
5792 * The \c data argument to the notify callback is the weston_head being
5793 * destroyed.
Marius Vlad78984ee2019-06-11 00:05:08 +03005794 *
5795 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005796 */
5797WL_EXPORT void
5798weston_head_add_destroy_listener(struct weston_head *head,
5799 struct wl_listener *listener)
5800{
5801 wl_signal_add(&head->destroy_signal, listener);
5802}
5803
5804/** Look up destroy listener for a head
5805 *
5806 * \param head The head to query.
5807 * \param notify The notify function used used for the added destroy listener.
5808 * \return The listener, or NULL if not found.
5809 *
5810 * This looks up the previously added destroy listener struct based on the
5811 * notify function it has. The listener can be used to access user data
5812 * through \c container_of().
5813 *
5814 * \sa wl_signal_get()
Marius Vlad78984ee2019-06-11 00:05:08 +03005815 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005816 */
5817WL_EXPORT struct wl_listener *
5818weston_head_get_destroy_listener(struct weston_head *head,
5819 wl_notify_func_t notify)
5820{
5821 return wl_signal_get(&head->destroy_signal, notify);
5822}
5823
David Fort0de859e2016-05-27 23:22:57 +02005824/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005825static void
David Fort0de859e2016-05-27 23:22:57 +02005826weston_compositor_reflow_outputs(struct weston_compositor *compositor,
5827 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005828{
5829 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02005830 bool start_resizing = false;
5831
5832 if (!delta_width)
5833 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005834
5835 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02005836 if (output == resized_output) {
5837 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005838 continue;
5839 }
5840
David Fort0de859e2016-05-27 23:22:57 +02005841 if (start_resizing) {
5842 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005843 output->dirty = 1;
5844 }
5845 }
5846}
5847
Pekka Paalanen06920792020-12-08 15:22:36 +02005848/** Transform a region in-place from global to output coordinates
5849 *
5850 * \param output The output that defines the transformation.
5851 * \param region The region to be transformed in-place.
5852 *
5853 * This takes a region in the global coordinate system, and takes into account
5854 * output position, transform and scale, and the zoom, and converts the region
5855 * into output pixel coordinates in the framebuffer.
5856 *
5857 * Uses floating-point operations if zoom is active, which may round to expand
5858 * the region.
5859 *
5860 * \internal
5861 * \ingroup output
5862 */
5863WL_EXPORT void
5864weston_output_region_from_global(struct weston_output *output,
5865 pixman_region32_t *region)
5866{
5867 if (output->zoom.active) {
5868 weston_matrix_transform_region(region, &output->matrix, region);
5869 } else {
5870 pixman_region32_translate(region, -output->x, -output->y);
5871 weston_transformed_region(output->width, output->height,
5872 output->transform,
5873 output->current_scale,
5874 region, region);
5875 }
5876}
5877
Pekka Paalanend72bad22017-03-29 17:01:41 +03005878static void
Scott Moreauccbf29d2012-02-22 14:21:41 -07005879weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005880{
Scott Moreau850ca422012-05-21 15:21:25 -06005881 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05005882
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005883 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005884 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06005885
Scott Moreauccbf29d2012-02-22 14:21:41 -07005886 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06005887 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005888 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005889 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005890 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005891 weston_matrix_scale(&output->matrix, magnification,
5892 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005893 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005894
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005895 switch (output->transform) {
5896 case WL_OUTPUT_TRANSFORM_FLIPPED:
5897 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5898 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5899 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5900 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5901 weston_matrix_scale(&output->matrix, -1, 1, 1);
5902 break;
5903 }
5904
5905 switch (output->transform) {
5906 default:
5907 case WL_OUTPUT_TRANSFORM_NORMAL:
5908 case WL_OUTPUT_TRANSFORM_FLIPPED:
5909 break;
5910 case WL_OUTPUT_TRANSFORM_90:
5911 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +02005912 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5913 weston_matrix_rotate_xy(&output->matrix, 0, -1);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005914 break;
5915 case WL_OUTPUT_TRANSFORM_180:
5916 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5917 weston_matrix_translate(&output->matrix,
5918 -output->width, -output->height, 0);
5919 weston_matrix_rotate_xy(&output->matrix, -1, 0);
5920 break;
5921 case WL_OUTPUT_TRANSFORM_270:
5922 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +02005923 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
5924 weston_matrix_rotate_xy(&output->matrix, 0, 1);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005925 break;
5926 }
5927
5928 if (output->current_scale != 1)
5929 weston_matrix_scale(&output->matrix,
5930 output->current_scale,
5931 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01005932
Scott Moreauccbf29d2012-02-22 14:21:41 -07005933 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05005934
5935 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005936}
5937
Scott Moreau1bad5db2012-08-18 01:04:05 -06005938static void
Alexander Larsson0b135062013-05-28 16:23:36 +02005939weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06005940{
5941 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005942 output->native_scale = scale;
5943 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06005944
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005945 convert_size_by_transform_scale(&output->width, &output->height,
5946 output->current_mode->width,
5947 output->current_mode->height,
5948 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02005949}
5950
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005951static void
5952weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07005953{
5954 output->x = x;
5955 output->y = y;
5956
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005957 pixman_region32_fini(&output->region);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005958 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06005959 output->width,
5960 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005961}
5962
Marius Vlad55d87362019-06-11 01:15:35 +03005963/**
5964 * \ingroup output
5965 */
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005966WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005967weston_output_move(struct weston_output *output, int x, int y)
5968{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005969 struct weston_head *head;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005970 struct wl_resource *resource;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005971 int ver;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005972
5973 output->move_x = x - output->x;
5974 output->move_y = y - output->y;
5975
5976 if (output->move_x == 0 && output->move_y == 0)
5977 return;
5978
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005979 weston_output_init_geometry(output, x, y);
5980
5981 output->dirty = 1;
5982
5983 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005984 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005985
5986 /* Notify clients of the change for output position. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005987 wl_list_for_each(head, &output->head_list, output_link) {
5988 wl_resource_for_each(resource, &head->resource_list) {
5989 wl_output_send_geometry(resource,
5990 output->x,
5991 output->y,
5992 head->mm_width,
5993 head->mm_height,
5994 head->subpixel,
5995 head->make,
5996 head->model,
5997 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08005998
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005999 ver = wl_resource_get_version(resource);
6000 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
6001 wl_output_send_done(resource);
6002 }
Roman Gilge97391c2019-03-29 13:01:06 +01006003
6004 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
6005 zxdg_output_v1_send_logical_position(resource,
6006 output->x,
6007 output->y);
6008 zxdg_output_v1_send_done(resource);
6009 }
Quanxian Wangb2c86362014-03-14 09:16:25 +08006010 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006011}
6012
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006013/** Signal that a pending output is taken into use.
6014 *
6015 * Removes the output from the pending list and adds it to the compositor's
6016 * list of enabled outputs. The output created signal is emitted.
Giulio Camuffob1147152015-05-06 21:41:57 +03006017 *
Pekka Paalanen2210ad02017-03-30 15:48:06 +03006018 * The output gets an internal ID assigned, and the wl_output global is
6019 * created.
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03006020 *
Giulio Camuffob1147152015-05-06 21:41:57 +03006021 * \param compositor The compositor instance.
6022 * \param output The output to be added.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006023 *
6024 * \internal
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006025 * \ingroup compositor
Giulio Camuffob1147152015-05-06 21:41:57 +03006026 */
Pekka Paalanenf9681b52017-03-29 16:58:48 +03006027static void
Giulio Camuffob1147152015-05-06 21:41:57 +03006028weston_compositor_add_output(struct weston_compositor *compositor,
6029 struct weston_output *output)
6030{
Armin Krezoviće5403842016-08-05 15:28:29 +02006031 struct weston_view *view, *next;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03006032 struct weston_head *head;
Armin Krezoviće5403842016-08-05 15:28:29 +02006033
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006034 assert(!output->enabled);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03006035
6036 /* Verify we haven't reached the limit of 32 available output IDs */
6037 assert(ffs(~compositor->output_id_pool) > 0);
6038
6039 /* Invert the output id pool and look for the lowest numbered
6040 * switch (the least significant bit). Take that bit's position
6041 * as our ID, and mark it used in the compositor's output_id_pool.
6042 */
6043 output->id = ffs(~compositor->output_id_pool) - 1;
6044 compositor->output_id_pool |= 1u << output->id;
6045
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006046 wl_list_remove(&output->link);
Giulio Camuffob1147152015-05-06 21:41:57 +03006047 wl_list_insert(compositor->output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006048 output->enabled = true;
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006049
Pekka Paalanendcac3512017-12-08 14:13:34 +02006050 wl_list_for_each(head, &output->head_list, output_link)
6051 weston_head_add_global(head);
Pekka Paalanen2210ad02017-03-30 15:48:06 +03006052
Giulio Camuffob1147152015-05-06 21:41:57 +03006053 wl_signal_emit(&compositor->output_created_signal, output);
Armin Krezoviće5403842016-08-05 15:28:29 +02006054
6055 wl_list_for_each_safe(view, next, &compositor->view_list, link)
6056 weston_view_geometry_dirty(view);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04006057}
6058
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006059/** Transform device coordinates into global coordinates
6060 *
Marius Vlada2dace22019-06-12 16:05:44 +03006061 * \param output the weston_output object
6062 * \param[in] device_x X coordinate in device units.
6063 * \param[in] device_y Y coordinate in device units.
6064 * \param[out] x X coordinate in the global space.
6065 * \param[out] y Y coordinate in the global space.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006066 *
Marius Vlada2dace22019-06-12 16:05:44 +03006067 * Transforms coordinates from the device coordinate space (physical pixel
6068 * units) to the global coordinate space (logical pixel units). This takes
6069 * into account output transform and scale.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006070 *
Marius Vlad55d87362019-06-11 01:15:35 +03006071 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006072 * \internal
6073 */
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006074WL_EXPORT void
6075weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02006076 double device_x, double device_y,
6077 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006078{
Derek Foreman0f679412014-10-02 13:41:17 -05006079 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02006080 device_x,
6081 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05006082 0.0,
6083 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006084
Derek Foreman67a18b92015-03-24 11:36:14 -05006085 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006086
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02006087 *x = p.f[0] / p.f[3];
6088 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006089}
6090
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006091/** Removes output from compositor's list of enabled outputs
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006092 *
6093 * \param output The weston_output object that is being removed.
6094 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006095 * The following happens:
6096 *
6097 * - The output assignments of all views in the current scenegraph are
6098 * recomputed.
6099 *
6100 * - Presentation feedback is discarded.
6101 *
6102 * - Compositor is notified that outputs were changed and
6103 * applies the necessary changes to re-layout outputs.
6104 *
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006105 * - The output is put back in the pending outputs list.
6106 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006107 * - Signal is emitted to notify all users of the weston_output
6108 * object that the output is being destroyed.
6109 *
6110 * - wl_output protocol objects referencing this weston_output
Pekka Paalanen2210ad02017-03-30 15:48:06 +03006111 * are made inert, and the wl_output global is removed.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006112 *
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03006113 * - The output's internal ID is released.
6114 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006115 * \ingroup compositor
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006116 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006117 */
6118static void
6119weston_compositor_remove_output(struct weston_output *output)
6120{
Pekka Paalanenbccda712017-03-29 16:16:04 +03006121 struct weston_compositor *compositor = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006122 struct weston_view *view;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03006123 struct weston_head *head;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006124
6125 assert(output->destroying);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006126 assert(output->enabled);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006127
Pekka Paalanenbccda712017-03-29 16:16:04 +03006128 wl_list_for_each(view, &compositor->view_list, link) {
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006129 if (view->output_mask & (1u << output->id))
6130 weston_view_assign_output(view);
6131 }
6132
6133 weston_presentation_feedback_discard_list(&output->feedback_list);
6134
Pekka Paalanen9711fd92018-06-21 14:26:18 +03006135 weston_compositor_reflow_outputs(compositor, output, -output->width);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006136
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006137 wl_list_remove(&output->link);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006138 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006139 output->enabled = false;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006140
Pekka Paalanenbccda712017-03-29 16:16:04 +03006141 wl_signal_emit(&compositor->output_destroyed_signal, output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006142 wl_signal_emit(&output->destroy_signal, output);
6143
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02006144 wl_list_for_each(head, &output->head_list, output_link)
6145 weston_head_remove_global(head);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03006146
6147 compositor->output_id_pool &= ~(1u << output->id);
6148 output->id = 0xffffffff; /* invalid */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006149}
6150
6151/** Sets the output scale for a given output.
6152 *
6153 * \param output The weston_output object that the scale is set for.
6154 * \param scale Scale factor for the given output.
6155 *
6156 * It only supports setting scale for an output that
6157 * is not enabled and it can only be ran once.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006158 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05006159 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006160 */
6161WL_EXPORT void
6162weston_output_set_scale(struct weston_output *output,
6163 int32_t scale)
6164{
6165 /* We can only set scale on a disabled output */
6166 assert(!output->enabled);
6167
6168 /* We only want to set scale once */
6169 assert(!output->scale);
6170
6171 output->scale = scale;
6172}
6173
6174/** Sets the output transform for a given output.
6175 *
6176 * \param output The weston_output object that the transform is set for.
6177 * \param transform Transform value for the given output.
6178 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006179 * Refer to wl_output::transform section located at
6180 * https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_output
6181 * for list of values that can be passed to this function.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006182 *
Marius Vlad55d87362019-06-11 01:15:35 +03006183 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006184 */
6185WL_EXPORT void
6186weston_output_set_transform(struct weston_output *output,
6187 uint32_t transform)
6188{
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006189 struct weston_pointer_motion_event ev;
6190 struct wl_resource *resource;
6191 struct weston_seat *seat;
6192 pixman_region32_t old_region;
6193 int mid_x, mid_y;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006194 struct weston_head *head;
6195 int ver;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006196
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006197 if (!output->enabled && output->transform == UINT32_MAX) {
6198 output->transform = transform;
6199 return;
6200 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006201
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006202 weston_output_transform_scale_init(output, transform, output->scale);
6203
6204 pixman_region32_init(&old_region);
6205 pixman_region32_copy(&old_region, &output->region);
6206
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006207 weston_output_init_geometry(output, output->x, output->y);
6208
6209 output->dirty = 1;
6210
6211 /* Notify clients of the change for output transform. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006212 wl_list_for_each(head, &output->head_list, output_link) {
6213 wl_resource_for_each(resource, &head->resource_list) {
6214 wl_output_send_geometry(resource,
6215 output->x,
6216 output->y,
6217 head->mm_width,
6218 head->mm_height,
6219 head->subpixel,
6220 head->make,
6221 head->model,
6222 output->transform);
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006223
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006224 ver = wl_resource_get_version(resource);
6225 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
6226 wl_output_send_done(resource);
6227 }
Roman Gilge97391c2019-03-29 13:01:06 +01006228 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
6229 zxdg_output_v1_send_logical_position(resource,
6230 output->x,
6231 output->y);
6232 zxdg_output_v1_send_logical_size(resource,
6233 output->width,
6234 output->height);
6235 zxdg_output_v1_send_done(resource);
6236 }
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006237 }
6238
6239 /* we must ensure that pointers are inside output, otherwise they disappear */
6240 mid_x = output->x + output->width / 2;
6241 mid_y = output->y + output->height / 2;
6242
6243 ev.mask = WESTON_POINTER_MOTION_ABS;
6244 ev.x = wl_fixed_to_double(wl_fixed_from_int(mid_x));
6245 ev.y = wl_fixed_to_double(wl_fixed_from_int(mid_y));
6246
6247 wl_list_for_each(seat, &output->compositor->seat_list, link) {
6248 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
6249
6250 if (pointer && pixman_region32_contains_point(&old_region,
6251 wl_fixed_to_int(pointer->x),
6252 wl_fixed_to_int(pointer->y),
6253 NULL))
6254 weston_pointer_move(pointer, &ev);
6255 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006256}
6257
Pekka Paalanenb5265af2020-12-02 11:57:58 +02006258/** Make the output use renderer shadow buffer.
6259 *
6260 * \param output The weston_output object to modify.
6261 * \return True on success, false if unsupported.
6262 *
6263 * This can only be set on a disabled output object.
6264 *
6265 * This is a temporary API to demonstrate WESTON_CAP_COLOR_OPS and allow
6266 * testing related features. This will be superseded with color management
6267 * API.
6268 *
6269 * By default, a renderer is not using a shadow buffer of its own. Enabling
6270 * a shadow buffer may enable other color related features.
6271 *
6272 * Support depends on the chosen renderer and the graphics driver stack in use.
6273 *
6274 * \ingroup output
6275 */
6276WL_EXPORT bool
6277weston_output_set_renderer_shadow_buffer(struct weston_output *output)
6278{
6279 struct weston_compositor *compositor = output->compositor;
6280
6281 assert(!output->enabled);
6282
6283 if (compositor->capabilities & WESTON_CAP_COLOR_OPS) {
6284 output->use_renderer_shadow_buffer = true;
6285 return true;
6286 }
6287
6288 return false;
6289}
6290
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006291/** Initializes a weston_output object with enough data so
6292 ** an output can be configured.
6293 *
6294 * \param output The weston_output object to initialize
6295 * \param compositor The compositor instance.
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006296 * \param name Name for the output (the string is copied).
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006297 *
6298 * Sets initial values for fields that are expected to be
6299 * configured either by compositors or backends.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006300 *
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006301 * The name is used in logs, and can be used by compositors as a configuration
6302 * identifier.
6303 *
Marius Vlad55d87362019-06-11 01:15:35 +03006304 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006305 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006306 */
6307WL_EXPORT void
Armin Krezović40087402016-09-30 14:11:12 +02006308weston_output_init(struct weston_output *output,
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006309 struct weston_compositor *compositor,
6310 const char *name)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006311{
6312 output->compositor = compositor;
6313 output->destroying = 0;
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006314 output->name = strdup(name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006315 wl_list_init(&output->link);
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006316 wl_signal_init(&output->user_destroy_signal);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006317 output->enabled = false;
Ankit Nautiyal2690a772019-03-25 17:57:59 +05306318 output->desired_protection = WESTON_HDCP_DISABLE;
Ankit Nautiyal2844f8e2019-04-03 10:14:59 +05306319 output->allow_protection = true;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006320
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006321 wl_list_init(&output->head_list);
6322
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006323 /* Add some (in)sane defaults which can be used
6324 * for checking if an output was properly configured
6325 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006326 output->scale = 0;
6327 /* Can't use -1 on uint32_t and 0 is valid enum value */
6328 output->transform = UINT32_MAX;
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006329
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006330 pixman_region32_init(&output->region);
Pekka Paalanen42704142017-09-06 16:47:52 +03006331 wl_list_init(&output->mode_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006332}
6333
6334/** Adds weston_output object to pending output list.
6335 *
6336 * \param output The weston_output object to add
6337 * \param compositor The compositor instance.
6338 *
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03006339 * The opposite of this operation is built into weston_output_release().
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006340 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006341 * \ingroup compositor
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006342 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006343 */
6344WL_EXPORT void
6345weston_compositor_add_pending_output(struct weston_output *output,
6346 struct weston_compositor *compositor)
6347{
Pekka Paalanene952a012017-03-29 17:14:00 +03006348 assert(output->disable);
6349 assert(output->enable);
6350
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006351 wl_list_remove(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006352 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006353}
6354
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006355/** Create a string with the attached heads' names.
6356 *
6357 * The string must be free()'d.
Marius Vlad55d87362019-06-11 01:15:35 +03006358 *
6359 * \ingroup output
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006360 */
6361static char *
6362weston_output_create_heads_string(struct weston_output *output)
6363{
6364 FILE *fp;
6365 char *str = NULL;
6366 size_t size = 0;
6367 struct weston_head *head;
6368 const char *sep = "";
6369
6370 fp = open_memstream(&str, &size);
6371 if (!fp)
6372 return NULL;
6373
6374 wl_list_for_each(head, &output->head_list, output_link) {
6375 fprintf(fp, "%s%s", sep, head->name);
6376 sep = ", ";
6377 }
6378 fclose(fp);
6379
6380 return str;
6381}
6382
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006383/** Constructs a weston_output object that can be used by the compositor.
6384 *
Pekka Paalanencc201e42017-03-30 15:11:25 +03006385 * \param output The weston_output object that needs to be enabled. Must not
Pekka Paalanenddce54d2017-08-23 16:00:21 +03006386 * be enabled already. Must have at least one head attached.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006387 *
6388 * Output coordinates are calculated and each new output is by default
6389 * assigned to the right of previous one.
6390 *
6391 * Sets up the transformation, zoom, and geometry of the output using
6392 * the properties that need to be configured by the compositor.
6393 *
6394 * Establishes a repaint timer for the output with the relevant display
6395 * object's event loop. See output_repaint_timer_handler().
6396 *
6397 * The output is assigned an ID. Weston can support up to 32 distinct
6398 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
6399 * is referred to and used to find the first available ID number, and
6400 * then this ID is marked as used in output_id_pool.
6401 *
6402 * The output is also assigned a Wayland global with the wl_output
6403 * external interface.
6404 *
6405 * Backend specific function is called to set up the output output.
6406 *
6407 * Output is added to the compositor's output list
6408 *
6409 * If the backend specific function fails, the weston_output object
6410 * is returned to a state it was before calling this function and
6411 * is added to the compositor's pending_output_list in case it needs
6412 * to be reconfigured or just so it can be destroyed at shutdown.
6413 *
6414 * 0 is returned on success, -1 on failure.
Marius Vlad55d87362019-06-11 01:15:35 +03006415 *
6416 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006417 */
6418WL_EXPORT int
6419weston_output_enable(struct weston_output *output)
6420{
Armin Krezović782f5df2016-09-30 14:11:11 +02006421 struct weston_compositor *c = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006422 struct weston_output *iterator;
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03006423 struct weston_head *head;
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006424 char *head_names;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006425 int x = 0, y = 0;
6426
Pekka Paalanencc201e42017-03-30 15:11:25 +03006427 if (output->enabled) {
6428 weston_log("Error: attempt to enable an enabled output '%s'\n",
6429 output->name);
6430 return -1;
6431 }
6432
Pekka Paalanenddce54d2017-08-23 16:00:21 +03006433 if (wl_list_empty(&output->head_list)) {
6434 weston_log("Error: cannot enable output '%s' without heads.\n",
6435 output->name);
6436 return -1;
6437 }
6438
Pekka Paalanen586e1ac2017-09-14 16:17:59 +03006439 if (wl_list_empty(&output->mode_list) || !output->current_mode) {
6440 weston_log("Error: no video mode for output '%s'.\n",
6441 output->name);
6442 return -1;
6443 }
6444
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03006445 wl_list_for_each(head, &output->head_list, output_link) {
6446 assert(head->make);
6447 assert(head->model);
6448 }
6449
Armin Krezović782f5df2016-09-30 14:11:11 +02006450 iterator = container_of(c->output_list.prev,
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006451 struct weston_output, link);
6452
Armin Krezović782f5df2016-09-30 14:11:11 +02006453 if (!wl_list_empty(&c->output_list))
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006454 x = iterator->x + iterator->width;
6455
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006456 /* Make sure the scale is set up */
6457 assert(output->scale);
6458
6459 /* Make sure we have a transform set */
6460 assert(output->transform != UINT32_MAX);
6461
Armin Krezović782f5df2016-09-30 14:11:11 +02006462 output->x = x;
6463 output->y = y;
6464 output->dirty = 1;
6465 output->original_scale = output->scale;
6466
Marius Vlad3a2f8292019-11-04 17:53:46 +02006467 wl_signal_init(&output->frame_signal);
6468 wl_signal_init(&output->destroy_signal);
6469
Armin Krezović782f5df2016-09-30 14:11:11 +02006470 weston_output_transform_scale_init(output, output->transform, output->scale);
6471 weston_output_init_zoom(output);
6472
6473 weston_output_init_geometry(output, x, y);
6474 weston_output_damage(output);
6475
Armin Krezović782f5df2016-09-30 14:11:11 +02006476 wl_list_init(&output->animation_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02006477 wl_list_init(&output->feedback_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02006478
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006479 /* Enable the output (set up the crtc or create a
6480 * window representing the output, set up the
6481 * renderer, etc)
6482 */
6483 if (output->enable(output) < 0) {
6484 weston_log("Enabling output \"%s\" failed.\n", output->name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006485 return -1;
6486 }
6487
6488 weston_compositor_add_output(output->compositor, output);
6489
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006490 head_names = weston_output_create_heads_string(output);
6491 weston_log("Output '%s' enabled with head(s) %s\n",
6492 output->name, head_names);
6493 free(head_names);
6494
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006495 return 0;
6496}
6497
6498/** Converts a weston_output object to a pending output state, so it
6499 ** can be configured again or destroyed.
6500 *
6501 * \param output The weston_output object that needs to be disabled.
6502 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006503 * Calls a backend specific function to disable an output, in case
6504 * such function exists.
6505 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03006506 * The backend specific disable function may choose to postpone the disabling
6507 * by returning a negative value, in which case this function returns early.
6508 * In that case the backend will guarantee the output will be disabled soon
6509 * by the backend calling this function again. One must not attempt to re-enable
6510 * the output until that happens.
6511 *
6512 * Otherwise, if the output is being used by the compositor, it is removed
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006513 * from weston's output_list (see weston_compositor_remove_output())
6514 * and is returned to a state it was before weston_output_enable()
6515 * was ran (see weston_output_enable_undo()).
6516 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03006517 * See weston_output_init() for more information on the
6518 * state output is returned to.
Pekka Paalanencc201e42017-03-30 15:11:25 +03006519 *
6520 * If the output has never been enabled yet, this function can still be
6521 * called to ensure that the output is actually turned off rather than left
6522 * in the state it was discovered in.
Marius Vlad55d87362019-06-11 01:15:35 +03006523 *
6524 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006525 */
6526WL_EXPORT void
6527weston_output_disable(struct weston_output *output)
6528{
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006529 /* Should we rename this? */
6530 output->destroying = 1;
6531
Pekka Paalanenc65df642017-03-29 15:45:46 +03006532 /* Disable is called unconditionally also for not-enabled outputs,
6533 * because at compositor start-up, if there is an output that is
6534 * already on but the compositor wants to turn it off, we have to
6535 * forward the turn-off to the backend so it knows to do it.
6536 * The backend cannot initially turn off everything, because it
6537 * would cause unnecessary mode-sets for all outputs the compositor
6538 * wants to be on.
6539 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006540 if (output->disable(output) < 0)
6541 return;
6542
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006543 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006544 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006545
6546 output->destroying = 0;
6547}
6548
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006549/** Forces a synchronous call to heads_changed hook
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006550 *
6551 * \param compositor The compositor instance
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006552 *
6553 * If there are new or changed heads, calls the heads_changed hook and
6554 * returns after the hook returns.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006555 *
6556 * \ingroup compositor
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006557 */
6558WL_EXPORT void
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006559weston_compositor_flush_heads_changed(struct weston_compositor *compositor)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006560{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03006561 if (compositor->heads_changed_source) {
6562 wl_event_source_remove(compositor->heads_changed_source);
6563 weston_compositor_call_heads_changed(compositor);
6564 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006565}
6566
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006567/** Add destroy callback for an output
6568 *
6569 * \param output The output to watch.
6570 * \param listener The listener to add. The \c notify member must be set.
6571 *
6572 * The listener callback will be called when user destroys an output. This
6573 * may be delayed by a backend in some cases. The main purpose of the
6574 * listener is to allow hooking up custom data to the output. The custom data
6575 * can be fetched via weston_output_get_destroy_listener() followed by
6576 * container_of().
6577 *
6578 * The \c data argument to the notify callback is the weston_output being
6579 * destroyed.
6580 *
6581 * @note This is for the final destruction of an output, not when it gets
6582 * disabled. If you want to keep track of enabled outputs, this is not it.
Marius Vlad55d87362019-06-11 01:15:35 +03006583 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05006584 * \ingroup output
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006585 */
6586WL_EXPORT void
6587weston_output_add_destroy_listener(struct weston_output *output,
6588 struct wl_listener *listener)
6589{
6590 wl_signal_add(&output->user_destroy_signal, listener);
6591}
6592
6593/** Look up destroy listener for an output
6594 *
6595 * \param output The output to query.
6596 * \param notify The notify function used used for the added destroy listener.
6597 * \return The listener, or NULL if not found.
6598 *
6599 * This looks up the previously added destroy listener struct based on the
6600 * notify function it has. The listener can be used to access user data
6601 * through \c container_of().
6602 *
6603 * \sa wl_signal_get() weston_output_add_destroy_listener()
Marius Vlad55d87362019-06-11 01:15:35 +03006604 * \ingroup output
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006605 */
6606WL_EXPORT struct wl_listener *
6607weston_output_get_destroy_listener(struct weston_output *output,
6608 wl_notify_func_t notify)
6609{
6610 return wl_signal_get(&output->user_destroy_signal, notify);
6611}
6612
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006613/** Uninitialize an output
6614 *
6615 * Removes the output from the list of enabled outputs if necessary, but
6616 * does not call the backend's output disable function. The output will no
6617 * longer be in the list of pending outputs either.
6618 *
6619 * All fields of weston_output become uninitialized, i.e. should not be used
6620 * anymore. The caller can free the memory after this.
6621 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05006622 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006623 * \internal
6624 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006625WL_EXPORT void
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03006626weston_output_release(struct weston_output *output)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006627{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006628 struct weston_head *head, *tmp;
6629
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006630 output->destroying = 1;
6631
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006632 wl_signal_emit(&output->user_destroy_signal, output);
6633
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03006634 if (output->idle_repaint_source)
6635 wl_event_source_remove(output->idle_repaint_source);
6636
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006637 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006638 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006639
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006640 pixman_region32_fini(&output->region);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006641 wl_list_remove(&output->link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006642
6643 wl_list_for_each_safe(head, tmp, &output->head_list, output_link)
6644 weston_head_detach(head);
6645
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006646 free(output->name);
6647}
6648
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006649/** Find an output by its given name
6650 *
6651 * \param compositor The compositor to search in.
6652 * \param name The output name to search for.
6653 * \return An existing output with the given name, or NULL if not found.
6654 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006655 * \ingroup compositor
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006656 */
6657WL_EXPORT struct weston_output *
6658weston_compositor_find_output_by_name(struct weston_compositor *compositor,
6659 const char *name)
6660{
6661 struct weston_output *output;
6662
6663 wl_list_for_each(output, &compositor->output_list, link)
6664 if (strcmp(output->name, name) == 0)
6665 return output;
6666
6667 wl_list_for_each(output, &compositor->pending_output_list, link)
6668 if (strcmp(output->name, name) == 0)
6669 return output;
6670
6671 return NULL;
6672}
6673
6674/** Create a named output
6675 *
6676 * \param compositor The compositor.
6677 * \param name The name for the output.
6678 * \return A new \c weston_output, or NULL on failure.
6679 *
6680 * This creates a new weston_output that starts with no heads attached.
6681 *
6682 * An output must be configured and it must have at least one head before
6683 * it can be enabled.
6684 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006685 * \ingroup compositor
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006686 */
6687WL_EXPORT struct weston_output *
6688weston_compositor_create_output(struct weston_compositor *compositor,
6689 const char *name)
6690{
6691 assert(compositor->backend->create_output);
6692
6693 if (weston_compositor_find_output_by_name(compositor, name)) {
6694 weston_log("Warning: attempted to create an output with a "
6695 "duplicate name '%s'.\n", name);
6696 return NULL;
6697 }
6698
6699 return compositor->backend->create_output(compositor, name);
6700}
6701
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006702/** Create an output for an unused head
6703 *
6704 * \param compositor The compositor.
6705 * \param head The head to attach to the output.
6706 * \return A new \c weston_output, or NULL on failure.
6707 *
6708 * This creates a new weston_output that starts with the given head attached.
6709 * The output inherits the name of the head. The head must not be already
6710 * attached to another output.
6711 *
6712 * An output must be configured before it can be enabled.
6713 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006714 * \ingroup compositor
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006715 */
6716WL_EXPORT struct weston_output *
6717weston_compositor_create_output_with_head(struct weston_compositor *compositor,
6718 struct weston_head *head)
6719{
6720 struct weston_output *output;
6721
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006722 output = weston_compositor_create_output(compositor, head->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006723 if (!output)
6724 return NULL;
6725
6726 if (weston_output_attach_head(output, head) < 0) {
Pekka Paalanen42c0e142017-10-27 12:07:49 +03006727 weston_output_destroy(output);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006728 return NULL;
6729 }
6730
6731 return output;
6732}
6733
6734/** Destroy an output
6735 *
6736 * \param output The output to destroy.
6737 *
6738 * The heads attached to the given output are detached and become unused again.
6739 *
6740 * It is not necessary to explicitly destroy all outputs at compositor exit.
Leandro Ribeiroca640d52020-01-27 19:12:01 -03006741 * weston_compositor_destroy() will automatically destroy any remaining
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006742 * outputs.
6743 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05006744 * \ingroup output
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006745 */
6746WL_EXPORT void
6747weston_output_destroy(struct weston_output *output)
6748{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006749 output->destroy(output);
6750}
6751
Pekka Paalanencf0a4762017-04-04 16:36:07 +03006752/** When you need a head...
6753 *
6754 * This function is a hack, used until all code has been converted to become
6755 * multi-head aware.
6756 *
6757 * \param output The weston_output whose head to get.
6758 * \return The first head in the output's list.
Marius Vlad55d87362019-06-11 01:15:35 +03006759 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05006760 * \ingroup output
Pekka Paalanencf0a4762017-04-04 16:36:07 +03006761 */
6762WL_EXPORT struct weston_head *
6763weston_output_get_first_head(struct weston_output *output)
6764{
6765 if (wl_list_empty(&output->head_list))
6766 return NULL;
6767
6768 return container_of(output->head_list.next,
6769 struct weston_head, output_link);
6770}
6771
Ankit Nautiyal2844f8e2019-04-03 10:14:59 +05306772/** Allow/Disallow content-protection support for an output
6773 *
6774 * This function sets the allow_protection member for an output. Setting of
6775 * this field will allow the compositor to attempt content-protection for this
6776 * output, for a backend that supports the content-protection protocol.
6777 *
6778 * \param output The weston_output for whom the content-protection is to be
6779 * allowed.
6780 * \param allow_protection The bool value which is to be set.
6781 */
6782WL_EXPORT void
6783weston_output_allow_protection(struct weston_output *output,
6784 bool allow_protection)
6785{
6786 output->allow_protection = allow_protection;
6787}
6788
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01006789static void
Roman Gilge97391c2019-03-29 13:01:06 +01006790xdg_output_unlist(struct wl_resource *resource)
6791{
6792 wl_list_remove(wl_resource_get_link(resource));
6793}
6794
6795static void
6796xdg_output_destroy(struct wl_client *client, struct wl_resource *resource)
6797{
6798 wl_resource_destroy(resource);
6799}
6800
6801static const struct zxdg_output_v1_interface xdg_output_interface = {
6802 xdg_output_destroy
6803};
6804
6805static void
6806xdg_output_manager_destroy(struct wl_client *client,
6807 struct wl_resource *resource)
6808{
6809 wl_resource_destroy(resource);
6810}
6811
6812static void
6813xdg_output_manager_get_xdg_output(struct wl_client *client,
6814 struct wl_resource *manager,
6815 uint32_t id,
6816 struct wl_resource *output_resource)
6817{
6818 int version = wl_resource_get_version(manager);
6819 struct weston_head *head = wl_resource_get_user_data(output_resource);
6820 struct weston_output *output = head->output;
6821 struct wl_resource *resource;
6822
6823 resource = wl_resource_create(client, &zxdg_output_v1_interface,
6824 version, id);
6825 if (resource == NULL) {
6826 wl_client_post_no_memory(client);
6827 return;
6828 }
6829
6830 wl_list_insert(&head->xdg_output_resource_list,
6831 wl_resource_get_link(resource));
6832
6833 wl_resource_set_implementation(resource, &xdg_output_interface,
6834 NULL, xdg_output_unlist);
6835
6836 zxdg_output_v1_send_logical_position(resource, output->x, output->y);
6837 zxdg_output_v1_send_logical_size(resource,
6838 output->width,
6839 output->height);
6840 if (version >= ZXDG_OUTPUT_V1_NAME_SINCE_VERSION)
6841 zxdg_output_v1_send_name(resource, head->name);
6842
6843 zxdg_output_v1_send_done(resource);
6844}
6845
6846static const struct zxdg_output_manager_v1_interface xdg_output_manager_interface = {
6847 xdg_output_manager_destroy,
6848 xdg_output_manager_get_xdg_output
6849};
6850
6851static void
6852bind_xdg_output_manager(struct wl_client *client,
6853 void *data, uint32_t version, uint32_t id)
6854{
6855 struct wl_resource *resource;
6856
6857 resource = wl_resource_create(client, &zxdg_output_manager_v1_interface,
6858 version, id);
6859 if (resource == NULL) {
6860 wl_client_post_no_memory(client);
6861 return;
6862 }
6863
6864 wl_resource_set_implementation(resource, &xdg_output_manager_interface,
6865 NULL, NULL);
6866}
6867
6868static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006869destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006870{
Jonny Lamb74130762013-11-26 18:19:46 +01006871 struct weston_surface *surface =
6872 wl_resource_get_user_data(resource);
6873
Pekka Paalanen4826f872016-04-22 14:14:38 +03006874 if (!surface)
6875 return;
6876
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006877 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02006878 surface->pending.buffer_viewport.buffer.src_width =
6879 wl_fixed_from_int(-1);
6880 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006881 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006882}
6883
6884static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006885viewport_destroy(struct wl_client *client,
6886 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006887{
6888 wl_resource_destroy(resource);
6889}
6890
6891static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006892viewport_set_source(struct wl_client *client,
6893 struct wl_resource *resource,
6894 wl_fixed_t src_x,
6895 wl_fixed_t src_y,
6896 wl_fixed_t src_width,
6897 wl_fixed_t src_height)
6898{
6899 struct weston_surface *surface =
6900 wl_resource_get_user_data(resource);
6901
Pekka Paalanen4826f872016-04-22 14:14:38 +03006902 if (!surface) {
6903 wl_resource_post_error(resource,
6904 WP_VIEWPORT_ERROR_NO_SURFACE,
6905 "wl_surface for this viewport is no longer exists");
6906 return;
6907 }
6908
6909 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03006910 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006911
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006912 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03006913 src_height == wl_fixed_from_int(-1) &&
6914 src_x == wl_fixed_from_int(-1) &&
6915 src_y == wl_fixed_from_int(-1)) {
6916 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006917 surface->pending.buffer_viewport.buffer.src_width =
6918 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006919 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006920 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006921 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006922
Pekka Paalanen201769a2016-04-26 14:42:11 +03006923 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006924 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006925 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03006926 "wl_surface@%d viewport source "
6927 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
6928 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006929 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03006930 wl_fixed_to_double(src_height),
6931 wl_fixed_to_double(src_x),
6932 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006933 return;
6934 }
6935
6936 surface->pending.buffer_viewport.buffer.src_x = src_x;
6937 surface->pending.buffer_viewport.buffer.src_y = src_y;
6938 surface->pending.buffer_viewport.buffer.src_width = src_width;
6939 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006940 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006941}
6942
6943static void
6944viewport_set_destination(struct wl_client *client,
6945 struct wl_resource *resource,
6946 int32_t dst_width,
6947 int32_t dst_height)
6948{
6949 struct weston_surface *surface =
6950 wl_resource_get_user_data(resource);
6951
Pekka Paalanen4826f872016-04-22 14:14:38 +03006952 if (!surface) {
6953 wl_resource_post_error(resource,
6954 WP_VIEWPORT_ERROR_NO_SURFACE,
6955 "wl_surface for this viewport no longer exists");
6956 return;
6957 }
6958
6959 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006960
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006961 if (dst_width == -1 && dst_height == -1) {
6962 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006963 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006964 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006965 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006966 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006967
6968 if (dst_width <= 0 || dst_height <= 0) {
6969 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006970 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006971 "destination size must be positive (%dx%d)",
6972 dst_width, dst_height);
6973 return;
6974 }
6975
6976 surface->pending.buffer_viewport.surface.width = dst_width;
6977 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006978 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006979}
6980
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006981static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006982 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006983 viewport_set_source,
6984 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01006985};
6986
6987static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006988viewporter_destroy(struct wl_client *client,
6989 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006990{
6991 wl_resource_destroy(resource);
6992}
6993
6994static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006995viewporter_get_viewport(struct wl_client *client,
6996 struct wl_resource *viewporter,
6997 uint32_t id,
6998 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006999{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007000 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007001 struct weston_surface *surface =
7002 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01007003 struct wl_resource *resource;
7004
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007005 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007006 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007007 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007008 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01007009 return;
7010 }
7011
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007012 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007013 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01007014 if (resource == NULL) {
7015 wl_client_post_no_memory(client);
7016 return;
7017 }
7018
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007019 wl_resource_set_implementation(resource, &viewport_interface,
7020 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01007021
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007022 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01007023}
7024
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007025static const struct wp_viewporter_interface viewporter_interface = {
7026 viewporter_destroy,
7027 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01007028};
7029
7030static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007031bind_viewporter(struct wl_client *client,
7032 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01007033{
7034 struct wl_resource *resource;
7035
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007036 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06007037 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01007038 if (resource == NULL) {
7039 wl_client_post_no_memory(client);
7040 return;
7041 }
7042
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007043 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01007044 NULL, NULL);
7045}
7046
7047static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04007048destroy_presentation_feedback(struct wl_resource *feedback_resource)
7049{
7050 struct weston_presentation_feedback *feedback;
7051
7052 feedback = wl_resource_get_user_data(feedback_resource);
7053
7054 wl_list_remove(&feedback->link);
7055 free(feedback);
7056}
7057
7058static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007059presentation_destroy(struct wl_client *client, struct wl_resource *resource)
7060{
7061 wl_resource_destroy(resource);
7062}
7063
7064static void
7065presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04007066 struct wl_resource *presentation_resource,
7067 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007068 uint32_t callback)
7069{
Pekka Paalanen133e4392014-09-23 22:08:46 -04007070 struct weston_surface *surface;
7071 struct weston_presentation_feedback *feedback;
7072
7073 surface = wl_resource_get_user_data(surface_resource);
7074
Bryce Harringtonde16d892014-11-20 22:21:57 -08007075 feedback = zalloc(sizeof *feedback);
7076 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04007077 goto err_calloc;
7078
7079 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007080 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04007081 1, callback);
7082 if (!feedback->resource)
7083 goto err_create;
7084
7085 wl_resource_set_implementation(feedback->resource, NULL, feedback,
7086 destroy_presentation_feedback);
7087
7088 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
7089
7090 return;
7091
7092err_create:
7093 free(feedback);
7094
7095err_calloc:
7096 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007097}
7098
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007099static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007100 presentation_destroy,
7101 presentation_feedback
7102};
7103
7104static void
7105bind_presentation(struct wl_client *client,
7106 void *data, uint32_t version, uint32_t id)
7107{
7108 struct weston_compositor *compositor = data;
7109 struct wl_resource *resource;
7110
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007111 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06007112 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007113 if (resource == NULL) {
7114 wl_client_post_no_memory(client);
7115 return;
7116 }
7117
7118 wl_resource_set_implementation(resource, &presentation_implementation,
7119 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007120 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007121}
7122
7123static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05007124compositor_bind(struct wl_client *client,
7125 void *data, uint32_t version, uint32_t id)
7126{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007127 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05007128 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05007129
Jason Ekstranda85118c2013-06-27 20:17:02 -05007130 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06007131 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07007132 if (resource == NULL) {
7133 wl_client_post_no_memory(client);
7134 return;
7135 }
7136
7137 wl_resource_set_implementation(resource, &compositor_interface,
7138 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05007139}
7140
Daniel Stonece62cb32018-07-20 09:46:24 +01007141static const char *
7142output_repaint_status_text(struct weston_output *output)
7143{
7144 switch (output->repaint_status) {
7145 case REPAINT_NOT_SCHEDULED:
7146 return "no repaint";
7147 case REPAINT_BEGIN_FROM_IDLE:
7148 return "start_repaint_loop scheduled";
7149 case REPAINT_SCHEDULED:
7150 return "repaint scheduled";
7151 case REPAINT_AWAITING_COMPLETION:
7152 return "awaiting completion";
7153 }
7154
7155 assert(!"output_repaint_status_text missing enum");
7156 return NULL;
7157}
7158
7159static void
7160debug_scene_view_print_buffer(FILE *fp, struct weston_view *view)
7161{
7162 struct weston_buffer *buffer = view->surface->buffer_ref.buffer;
7163 struct wl_shm_buffer *shm;
7164 struct linux_dmabuf_buffer *dmabuf;
Marius Vlad00a6e012018-11-20 17:52:31 +02007165 const struct pixel_format_info *pixel_info = NULL;
Daniel Stonece62cb32018-07-20 09:46:24 +01007166
7167 if (!buffer) {
7168 fprintf(fp, "\t\t[buffer not available]\n");
7169 return;
7170 }
7171
7172 shm = wl_shm_buffer_get(buffer->resource);
7173 if (shm) {
Marius Vlad00a6e012018-11-20 17:52:31 +02007174 uint32_t _format = wl_shm_buffer_get_format(shm);
7175 pixel_info = pixel_format_get_info_shm(_format);
Daniel Stonece62cb32018-07-20 09:46:24 +01007176 fprintf(fp, "\t\tSHM buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02007177 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
7178 (unsigned long) _format,
7179 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Daniel Stonece62cb32018-07-20 09:46:24 +01007180 return;
7181 }
7182
7183 dmabuf = linux_dmabuf_buffer_get(buffer->resource);
7184 if (dmabuf) {
Marius Vlad00a6e012018-11-20 17:52:31 +02007185 pixel_info = pixel_format_get_info(dmabuf->attributes.format);
Daniel Stonece62cb32018-07-20 09:46:24 +01007186 fprintf(fp, "\t\tdmabuf buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02007187 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
7188 (unsigned long) dmabuf->attributes.format,
7189 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Daniel Stonece62cb32018-07-20 09:46:24 +01007190 fprintf(fp, "\t\t\tmodifier: 0x%llx\n",
7191 (unsigned long long) dmabuf->attributes.modifier[0]);
7192 return;
7193 }
7194
Marius Vlad253ba9a2019-03-17 18:22:21 +02007195 fprintf(fp, "\t\tEGL buffer\n");
Daniel Stonece62cb32018-07-20 09:46:24 +01007196}
7197
7198static void
7199debug_scene_view_print(FILE *fp, struct weston_view *view, int view_idx)
7200{
7201 struct weston_compositor *ec = view->surface->compositor;
7202 struct weston_output *output;
7203 char desc[512];
7204 pixman_box32_t *box;
7205 uint32_t surface_id = 0;
7206 pid_t pid = 0;
7207
7208 if (view->surface->resource) {
7209 struct wl_resource *resource = view->surface->resource;
7210 wl_client_get_credentials(wl_resource_get_client(resource),
7211 &pid, NULL, NULL);
7212 surface_id = wl_resource_get_id(view->surface->resource);
7213 }
7214
7215 if (!view->surface->get_label ||
7216 view->surface->get_label(view->surface, desc, sizeof(desc)) < 0) {
7217 strcpy(desc, "[no description available]");
7218 }
7219 fprintf(fp, "\tView %d (role %s, PID %d, surface ID %u, %s, %p):\n",
7220 view_idx, view->surface->role_name, pid, surface_id,
7221 desc, view);
7222
7223 box = pixman_region32_extents(&view->transform.boundingbox);
7224 fprintf(fp, "\t\tposition: (%d, %d) -> (%d, %d)\n",
7225 box->x1, box->y1, box->x2, box->y2);
7226 box = pixman_region32_extents(&view->transform.opaque);
7227
Michael Olbrichb7e5f102020-08-11 16:33:35 +02007228 if (weston_view_is_opaque(view, &view->transform.boundingbox)) {
Daniel Stonece62cb32018-07-20 09:46:24 +01007229 fprintf(fp, "\t\t[fully opaque]\n");
7230 } else if (!pixman_region32_not_empty(&view->transform.opaque)) {
7231 fprintf(fp, "\t\t[not opaque]\n");
7232 } else {
7233 fprintf(fp, "\t\t[opaque: (%d, %d) -> (%d, %d)]\n",
7234 box->x1, box->y1, box->x2, box->y2);
7235 }
7236
7237 if (view->alpha < 1.0)
7238 fprintf(fp, "\t\talpha: %f\n", view->alpha);
7239
7240 if (view->output_mask != 0) {
7241 bool first_output = true;
7242 fprintf(fp, "\t\toutputs: ");
7243 wl_list_for_each(output, &ec->output_list, link) {
7244 if (!(view->output_mask & (1 << output->id)))
7245 continue;
7246 fprintf(fp, "%s%d (%s)%s",
7247 (first_output) ? "" : ", ",
7248 output->id, output->name,
7249 (view->output == output) ? " (primary)" : "");
7250 first_output = false;
7251 }
7252 } else {
7253 fprintf(fp, "\t\t[no outputs]");
7254 }
7255
7256 fprintf(fp, "\n");
7257
7258 debug_scene_view_print_buffer(fp, view);
7259}
7260
Marius Vlad433f4e72019-02-17 22:14:23 +02007261static void
7262debug_scene_view_print_tree(struct weston_view *view,
Marius Vlada6acfa82019-03-17 18:10:09 +02007263 FILE *fp, int *view_idx)
Marius Vlad433f4e72019-02-17 22:14:23 +02007264{
7265 struct weston_subsurface *sub;
7266 struct weston_view *ev;
7267
7268 /*
7269 * print the view first, then we recursively go on printing
7270 * sub-surfaces. We bail out once no more sub-surfaces are available.
7271 */
Marius Vlada6acfa82019-03-17 18:10:09 +02007272 debug_scene_view_print(fp, view, *view_idx);
Marius Vlad433f4e72019-02-17 22:14:23 +02007273
7274 /* no more sub-surfaces */
7275 if (wl_list_empty(&view->surface->subsurface_list))
7276 return;
7277
7278 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
7279 wl_list_for_each(ev, &sub->surface->views, surface_link) {
Michael Olbrichef5f3232020-04-29 09:03:15 +02007280 /* only print the child views of the current view */
7281 if (ev->parent_view != view)
Marius Vlad433f4e72019-02-17 22:14:23 +02007282 continue;
Marius Vlada6acfa82019-03-17 18:10:09 +02007283
7284 (*view_idx)++;
Marius Vlad433f4e72019-02-17 22:14:23 +02007285 debug_scene_view_print_tree(ev, fp, view_idx);
7286 }
7287 }
7288}
7289
Daniel Stonece62cb32018-07-20 09:46:24 +01007290/**
7291 * Output information on how libweston is currently composing the scene
7292 * graph.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007293 *
7294 * \ingroup compositor
Daniel Stonece62cb32018-07-20 09:46:24 +01007295 */
7296WL_EXPORT char *
7297weston_compositor_print_scene_graph(struct weston_compositor *ec)
7298{
7299 struct weston_output *output;
7300 struct weston_layer *layer;
7301 struct timespec now;
7302 int layer_idx = 0;
7303 FILE *fp;
7304 char *ret;
7305 size_t len;
7306 int err;
7307
7308 fp = open_memstream(&ret, &len);
7309 assert(fp);
7310
7311 weston_compositor_read_presentation_clock(ec, &now);
7312 fprintf(fp, "Weston scene graph at %ld.%09ld:\n\n",
7313 now.tv_sec, now.tv_nsec);
7314
7315 wl_list_for_each(output, &ec->output_list, link) {
7316 struct weston_head *head;
7317 int head_idx = 0;
7318
7319 fprintf(fp, "Output %d (%s):\n", output->id, output->name);
7320 assert(output->enabled);
7321
7322 fprintf(fp, "\tposition: (%d, %d) -> (%d, %d)\n",
7323 output->x, output->y,
7324 output->x + output->width,
7325 output->y + output->height);
7326 fprintf(fp, "\tmode: %dx%d@%.3fHz\n",
7327 output->current_mode->width,
7328 output->current_mode->height,
7329 output->current_mode->refresh / 1000.0);
7330 fprintf(fp, "\tscale: %d\n", output->scale);
7331
7332 fprintf(fp, "\trepaint status: %s\n",
7333 output_repaint_status_text(output));
7334 if (output->repaint_status == REPAINT_SCHEDULED)
7335 fprintf(fp, "\tnext repaint: %ld.%09ld\n",
7336 output->next_repaint.tv_sec,
7337 output->next_repaint.tv_nsec);
7338
7339 wl_list_for_each(head, &output->head_list, output_link) {
7340 fprintf(fp, "\tHead %d (%s): %sconnected\n",
7341 head_idx++, head->name,
7342 (head->connected) ? "" : "not ");
7343 }
7344 }
7345
7346 fprintf(fp, "\n");
7347
7348 wl_list_for_each(layer, &ec->layer_list, link) {
7349 struct weston_view *view;
7350 int view_idx = 0;
7351
7352 fprintf(fp, "Layer %d (pos 0x%lx):\n", layer_idx++,
7353 (unsigned long) layer->position);
7354
7355 if (!weston_layer_mask_is_infinite(layer)) {
7356 fprintf(fp, "\t[mask: (%d, %d) -> (%d,%d)]\n\n",
7357 layer->mask.x1, layer->mask.y1,
7358 layer->mask.x2, layer->mask.y2);
7359 }
7360
Marius Vlada6acfa82019-03-17 18:10:09 +02007361 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
7362 debug_scene_view_print_tree(view, fp, &view_idx);
7363 view_idx++;
7364 }
Daniel Stonece62cb32018-07-20 09:46:24 +01007365
7366 if (wl_list_empty(&layer->view_list.link))
7367 fprintf(fp, "\t[no views]\n");
7368
7369 fprintf(fp, "\n");
7370 }
7371
7372 err = fclose(fp);
7373 assert(err == 0);
7374
7375 return ret;
7376}
7377
7378/**
7379 * Called when the 'scene-graph' debug scope is bound by a client. This
7380 * one-shot weston-debug scope prints the current scene graph when bound,
7381 * and then terminates the stream.
7382 */
7383static void
Marius Vladdad882a2019-07-17 15:43:53 +03007384debug_scene_graph_cb(struct weston_log_subscription *sub, void *data)
Daniel Stonece62cb32018-07-20 09:46:24 +01007385{
7386 struct weston_compositor *ec = data;
7387 char *str = weston_compositor_print_scene_graph(ec);
7388
Marius Vladdad882a2019-07-17 15:43:53 +03007389 weston_log_subscription_printf(sub, "%s", str);
Daniel Stonece62cb32018-07-20 09:46:24 +01007390 free(str);
Marius Vladdad882a2019-07-17 15:43:53 +03007391 weston_log_subscription_complete(sub);
Daniel Stonece62cb32018-07-20 09:46:24 +01007392}
7393
Leandro Ribeiro32a5acd2020-10-19 16:06:22 -03007394/** Retrieve testsuite data from compositor
7395 *
7396 * The testsuite data can be defined by the test suite of projects that uses
7397 * libweston and given to the compositor at the moment of its creation. This
7398 * function should be used when we need to retrieve the testsuite private data
7399 * from the compositor.
7400 *
7401 * \param ec The weston compositor.
7402 * \return The testsuite data.
7403 *
7404 * \ingroup compositor
7405 * \sa weston_compositor_test_data_init
7406 */
7407WL_EXPORT void *
7408weston_compositor_get_test_data(struct weston_compositor *ec)
7409{
7410 return ec->test_data.test_private_data;
7411}
7412
Giulio Camuffo459137b2014-10-11 23:56:24 +03007413/** Create the compositor.
7414 *
7415 * This functions creates and initializes a compositor instance.
7416 *
7417 * \param display The Wayland display to be used.
7418 * \param user_data A pointer to an object that can later be retrieved
Marius Vlada2dace22019-06-12 16:05:44 +03007419 * \param log_ctx A pointer to weston_debug_compositor
Leandro Ribeirocdb72182020-11-06 11:24:22 -03007420 * \param test_data Optional testsuite data, or NULL.
Giulio Camuffo459137b2014-10-11 23:56:24 +03007421 * using the \ref weston_compositor_get_user_data function.
7422 * \return The compositor instance on success or NULL on failure.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007423 *
7424 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007425 */
7426WL_EXPORT struct weston_compositor *
Marius Vlad880b4852019-04-07 17:07:58 +03007427weston_compositor_create(struct wl_display *display,
Leandro Ribeirocdb72182020-11-06 11:24:22 -03007428 struct weston_log_context *log_ctx, void *user_data,
7429 const struct weston_testsuite_data *test_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04007430{
Giulio Camuffo459137b2014-10-11 23:56:24 +03007431 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05007432 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07007433
Leandro Ribeirobd9c0a62020-01-17 10:47:49 -03007434 if (!log_ctx)
7435 return NULL;
7436
Giulio Camuffo459137b2014-10-11 23:56:24 +03007437 ec = zalloc(sizeof *ec);
7438 if (!ec)
7439 return NULL;
7440
Leandro Ribeirocdb72182020-11-06 11:24:22 -03007441 if (test_data)
7442 ec->test_data = *test_data;
7443
Leandro Ribeirobd9c0a62020-01-17 10:47:49 -03007444 ec->weston_log_ctx = log_ctx;
Giulio Camuffo459137b2014-10-11 23:56:24 +03007445 ec->wl_display = display;
7446 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04007447 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07007448 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04007449 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03007450 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03007451 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02007452 wl_signal_init(&ec->idle_signal);
7453 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02007454 wl_signal_init(&ec->show_input_panel_signal);
7455 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02007456 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01007457 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01007458 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02007459 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02007460 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02007461 wl_signal_init(&ec->output_resized_signal);
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03007462 wl_signal_init(&ec->heads_changed_signal);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02007463 wl_signal_init(&ec->output_heads_changed_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07007464 wl_signal_init(&ec->session_signal);
Robert Beckettc569bdc2019-07-10 16:40:12 +01007465 ec->session_active = true;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04007466
Casey Dahlin58ba1372012-04-19 22:50:08 -04007467 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03007468 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04007469
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02007470 ec->activate_serial = 1;
7471
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05007472 ec->touch_mode = WESTON_TOUCH_MODE_NORMAL;
7473
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05307474 ec->content_protection = NULL;
7475
Derek Foreman152254b2015-11-26 14:17:48 -06007476 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04007477 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007478 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05007479
Giulio Camuffo954f1832014-10-11 18:27:30 +03007480 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04007481 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007482 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03007483
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007484 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007485 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007486 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01007487
Roman Gilge97391c2019-03-29 13:01:06 +01007488 if (!wl_global_create(ec->wl_display, &zxdg_output_manager_v1_interface, 2,
7489 ec, bind_xdg_output_manager))
7490 goto fail;
7491
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007492 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007493 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007494 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007495
Jonas Ådahl30d61d82014-10-22 21:21:17 +02007496 if (weston_input_init(ec) != 0)
7497 goto fail;
7498
Jason Ekstranda7af7042013-10-12 22:38:11 -05007499 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02007500 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007501 wl_list_init(&ec->layer_list);
7502 wl_list_init(&ec->seat_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007503 wl_list_init(&ec->pending_output_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007504 wl_list_init(&ec->output_list);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03007505 wl_list_init(&ec->head_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007506 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01007507 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007508 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01007509 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007510 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02007511 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007512
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007513 wl_list_init(&ec->plugin_api_list);
7514
Xiong Zhang97116532013-10-23 13:58:31 +08007515 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02007516 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04007517
Giulio Camuffo459137b2014-10-11 23:56:24 +03007518 wl_data_device_manager_init(ec->wl_display);
7519
7520 wl_display_init_shm(ec->wl_display);
7521
7522 loop = wl_display_get_event_loop(ec->wl_display);
7523 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Daniel Stone6847b852017-03-01 11:34:08 +00007524 ec->repaint_timer =
7525 wl_event_loop_add_timer(loop, output_repaint_timer_handler,
7526 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007527
Quentin Glidic82681572016-12-17 13:40:51 +01007528 weston_layer_init(&ec->fade_layer, ec);
7529 weston_layer_init(&ec->cursor_layer, ec);
7530
7531 weston_layer_set_position(&ec->fade_layer, WESTON_LAYER_POSITION_FADE);
7532 weston_layer_set_position(&ec->cursor_layer,
7533 WESTON_LAYER_POSITION_CURSOR);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007534
Daniel Stonece62cb32018-07-20 09:46:24 +01007535 ec->debug_scene =
Leandro Ribeiroce100192019-12-26 16:35:49 -03007536 weston_compositor_add_log_scope(ec, "scene-graph",
7537 "Scene graph details\n",
7538 debug_scene_graph_cb, NULL,
7539 ec);
Daniel Stonece62cb32018-07-20 09:46:24 +01007540
Marius Vladda104eb2019-09-05 14:31:01 +03007541 ec->timeline =
Leandro Ribeiroce100192019-12-26 16:35:49 -03007542 weston_compositor_add_log_scope(ec, "timeline",
7543 "Timeline event points\n",
7544 weston_timeline_create_subscription,
7545 weston_timeline_destroy_subscription,
7546 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007547 return ec;
7548
7549fail:
7550 free(ec);
7551 return NULL;
7552}
7553
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007554/** weston_compositor_shutdown
7555 * \ingroup compositor
7556 */
Benjamin Franzkeb8263022011-08-30 11:32:47 +02007557WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007558weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07007559{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007560 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07007561
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007562 wl_event_source_remove(ec->idle_source);
7563
Matt Roper361d2ad2011-08-29 13:52:23 -07007564 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02007565 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07007566 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02007567
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007568 /* Destroy all pending outputs associated with this compositor */
7569 wl_list_for_each_safe(output, next, &ec->pending_output_list, link)
7570 output->destroy(output);
7571
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02007572 if (ec->renderer)
7573 ec->renderer->destroy(ec);
7574
Daniel Stone325fc2d2012-05-30 16:31:58 +01007575 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09007576 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01007577 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01007578 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01007579 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02007580 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007581
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04007582 weston_plane_release(&ec->primary_plane);
Matt Roper361d2ad2011-08-29 13:52:23 -07007583}
7584
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007585/** weston_compositor_exit_with_code
7586 * \ingroup compositor
7587 */
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05007588WL_EXPORT void
Adam Jackson3c3f3b12019-10-16 16:02:59 -04007589weston_compositor_exit_with_code(struct weston_compositor *compositor,
Frederic Plourdec336f062014-10-29 14:44:33 -04007590 int exit_code)
7591{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02007592 if (compositor->exit_code == EXIT_SUCCESS)
7593 compositor->exit_code = exit_code;
7594
Giulio Camuffo459137b2014-10-11 23:56:24 +03007595 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04007596}
7597
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007598/** weston_compositor_set_default_pointer_grab
7599 * \ingroup compositor
7600 */
Frederic Plourdec336f062014-10-29 14:44:33 -04007601WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007602weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
7603 const struct weston_pointer_grab_interface *interface)
7604{
7605 struct weston_seat *seat;
7606
7607 ec->default_pointer_grab = interface;
7608 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05007609 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
7610
7611 if (pointer)
7612 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007613 }
7614}
7615
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007616/** weston_compositor_set_presentation_clock
7617 * \ingroup compositor
7618 */
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007619WL_EXPORT int
7620weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
7621 clockid_t clk_id)
7622{
7623 struct timespec ts;
7624
7625 if (clock_gettime(clk_id, &ts) < 0)
7626 return -1;
7627
7628 compositor->presentation_clock = clk_id;
7629
7630 return 0;
7631}
7632
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007633/** For choosing the software clock, when the display hardware or API
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007634 * does not expose a compatible presentation timestamp.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007635 *
7636 * \ingroup compositor
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007637 */
7638WL_EXPORT int
7639weston_compositor_set_presentation_clock_software(
7640 struct weston_compositor *compositor)
7641{
7642 /* In order of preference */
7643 static const clockid_t clocks[] = {
7644 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
7645 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
7646 CLOCK_MONOTONIC, /* no jumps, may crawl */
7647 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
7648 CLOCK_REALTIME /* may jump and crawl */
7649 };
7650 unsigned i;
7651
7652 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
7653 if (weston_compositor_set_presentation_clock(compositor,
7654 clocks[i]) == 0)
7655 return 0;
7656
7657 weston_log("Error: no suitable presentation clock available.\n");
7658
7659 return -1;
7660}
7661
Pekka Paalanen662f3842015-03-18 12:17:26 +02007662/** Read the current time from the Presentation clock
7663 *
7664 * \param compositor
Marius Vlada2dace22019-06-12 16:05:44 +03007665 * \param[out] ts The current time.
Pekka Paalanen662f3842015-03-18 12:17:26 +02007666 *
7667 * \note Reading the current time in user space is always imprecise to some
7668 * degree.
7669 *
7670 * This function is never meant to fail. If reading the clock does fail,
7671 * an error message is logged and a zero time is returned. Callers are not
7672 * supposed to detect or react to failures.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007673 *
7674 * \ingroup compositor
Pekka Paalanen662f3842015-03-18 12:17:26 +02007675 */
7676WL_EXPORT void
7677weston_compositor_read_presentation_clock(
7678 const struct weston_compositor *compositor,
7679 struct timespec *ts)
7680{
7681 static bool warned;
7682 int ret;
7683
7684 ret = clock_gettime(compositor->presentation_clock, ts);
7685 if (ret < 0) {
7686 ts->tv_sec = 0;
7687 ts->tv_nsec = 0;
7688
7689 if (!warned)
7690 weston_log("Error: failure to read "
Antonio Borneo39578632019-04-26 23:57:31 +02007691 "the presentation clock %#x: '%s' (%d)\n",
7692 compositor->presentation_clock,
7693 strerror(errno), errno);
Pekka Paalanen662f3842015-03-18 12:17:26 +02007694 warned = true;
7695 }
7696}
7697
Pekka Paalanen230f3b12014-09-29 14:18:40 -04007698/** Import dmabuf buffer into current renderer
7699 *
7700 * \param compositor
7701 * \param buffer the dmabuf buffer to import
7702 * \return true on usable buffers, false otherwise
7703 *
7704 * This function tests that the linux_dmabuf_buffer is usable
7705 * for the current renderer. Returns false on unusable buffers. Usually
7706 * usability is tested by importing the dmabufs for composition.
7707 *
7708 * This hook is also used for detecting if the renderer supports
7709 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
7710 * supported.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007711 *
7712 * \ingroup compositor
7713 */
Pekka Paalanen230f3b12014-09-29 14:18:40 -04007714WL_EXPORT bool
7715weston_compositor_import_dmabuf(struct weston_compositor *compositor,
7716 struct linux_dmabuf_buffer *buffer)
7717{
7718 struct weston_renderer *renderer;
7719
7720 renderer = compositor->renderer;
7721
7722 if (renderer->import_dmabuf == NULL)
7723 return false;
7724
7725 return renderer->import_dmabuf(compositor, buffer);
7726}
7727
Marius Vlad5a701542019-11-16 20:26:52 +02007728WL_EXPORT bool
7729weston_compositor_dmabuf_can_scanout(struct weston_compositor *compositor,
7730 struct linux_dmabuf_buffer *buffer)
7731{
7732 struct weston_backend *backend = compositor->backend;
7733
7734 if (backend->can_scanout_dmabuf == NULL)
7735 return false;
7736
7737 return backend->can_scanout_dmabuf(compositor, buffer);
7738}
7739
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007740WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05007741weston_version(int *major, int *minor, int *micro)
7742{
7743 *major = WESTON_VERSION_MAJOR;
7744 *minor = WESTON_VERSION_MINOR;
7745 *micro = WESTON_VERSION_MICRO;
7746}
7747
Daniel Stonee03c1112016-11-24 20:45:45 +00007748/**
7749 * Attempts to find a module path from the module map specified in the
7750 * environment. If found, writes the full path into the path variable.
7751 *
7752 * The module map is a string in environment variable WESTON_MODULE_MAP, where
7753 * each entry is of the form "name=path" and entries are separated by
7754 * semicolons. Whitespace is significant.
7755 *
7756 * \param name The name to search for.
7757 * \param path Where the path is written to if found.
7758 * \param path_len Allocated bytes at \c path .
7759 * \returns The length of the string written to path on success, or 0 if the
7760 * module was not specified in the environment map or path_len was too small.
7761 */
7762WL_EXPORT size_t
7763weston_module_path_from_env(const char *name, char *path, size_t path_len)
7764{
7765 const char *mapping = getenv("WESTON_MODULE_MAP");
7766 const char *end;
7767 const int name_len = strlen(name);
7768
7769 if (!mapping)
7770 return 0;
7771
7772 end = mapping + strlen(mapping);
7773 while (mapping < end && *mapping) {
7774 const char *filename, *next;
7775
7776 /* early out: impossibly short string */
7777 if (end - mapping < name_len + 1)
7778 return 0;
7779
7780 filename = &mapping[name_len + 1];
7781 next = strchrnul(mapping, ';');
7782
7783 if (strncmp(mapping, name, name_len) == 0 &&
7784 mapping[name_len] == '=') {
7785 size_t file_len = next - filename; /* no trailing NUL */
7786 if (file_len >= path_len)
7787 return 0;
7788 strncpy(path, filename, file_len);
7789 path[file_len] = '\0';
7790 return file_len;
7791 }
7792
7793 mapping = next + 1;
7794 }
7795
7796 return 0;
7797}
7798
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03007799WL_EXPORT void *
7800weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007801{
7802 char path[PATH_MAX];
7803 void *module, *init;
Daniel Stonebeb97e52016-11-28 12:13:54 +00007804 size_t len;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007805
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08007806 if (name == NULL)
7807 return NULL;
7808
Derek Foreman3f86e502015-06-08 11:46:54 -05007809 if (name[0] != '/') {
Daniel Stonee03c1112016-11-24 20:45:45 +00007810 len = weston_module_path_from_env(name, path, sizeof path);
7811 if (len == 0)
Daniel Stonebeb97e52016-11-28 12:13:54 +00007812 len = snprintf(path, sizeof path, "%s/%s",
7813 LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05007814 } else {
Daniel Stonebeb97e52016-11-28 12:13:54 +00007815 len = snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05007816 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007817
Daniel Stonebeb97e52016-11-28 12:13:54 +00007818 /* snprintf returns the length of the string it would've written,
7819 * _excluding_ the NUL byte. So even being equal to the size of
7820 * our buffer is an error here. */
7821 if (len >= sizeof path)
7822 return NULL;
7823
Kristian Høgsberga6813d22012-09-12 12:21:01 -04007824 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
7825 if (module) {
7826 weston_log("Module '%s' already loaded\n", path);
Pekka Paalanenf696ee92019-11-04 14:07:51 +02007827 } else {
7828 weston_log("Loading module '%s'\n", path);
7829 module = dlopen(path, RTLD_NOW);
7830 if (!module) {
7831 weston_log("Failed to load module: %s\n", dlerror());
7832 return NULL;
7833 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007834 }
7835
7836 init = dlsym(module, entrypoint);
7837 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03007838 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00007839 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007840 return NULL;
7841 }
7842
7843 return init;
7844}
7845
Pekka Paalanen6ffbba32019-11-06 12:59:32 +02007846/** Add a compositor destroy listener only once
7847 *
7848 * \param compositor The compositor whose destroy to watch for.
7849 * \param listener The listener struct to initialize.
7850 * \param destroy_handler The callback when compositor is destroyed.
7851 * \return True if listener is added, or false if there already is a listener
7852 * with the given \c destroy_handler.
7853 *
7854 * This function does nothing and returns false if the given callback function
7855 * is already present in the weston_compositor destroy callbacks list.
7856 * Otherwise, this function initializes the given listener with the given
7857 * callback pointer and adds it to the compositor's destroy callbacks list.
7858 *
7859 * This can be used to ensure that plugin initialization is done only once
7860 * in case the same plugin is loaded multiple times. If this function returns
7861 * false, the plugin should be already initialized successfully.
7862 *
7863 * All plugins should register a destroy listener for cleaning up. Note, that
7864 * the plugin destruction order is not guaranteed: plugins that depend on other
7865 * plugins must be able to be torn down in arbitrary order.
7866 *
Leandro Ribeiroca640d52020-01-27 19:12:01 -03007867 * \sa weston_compositor_destroy
Pekka Paalanen6ffbba32019-11-06 12:59:32 +02007868 */
7869WL_EXPORT bool
7870weston_compositor_add_destroy_listener_once(struct weston_compositor *compositor,
7871 struct wl_listener *listener,
7872 wl_notify_func_t destroy_handler)
7873{
7874 if (wl_signal_get(&compositor->destroy_signal, destroy_handler))
7875 return false;
7876
7877 listener->notify = destroy_handler;
7878 wl_signal_add(&compositor->destroy_signal, listener);
7879 return true;
7880}
Giulio Camuffo459137b2014-10-11 23:56:24 +03007881
Leandro Ribeiroca640d52020-01-27 19:12:01 -03007882/** Destroys the compositor.
Giulio Camuffo459137b2014-10-11 23:56:24 +03007883 *
Leandro Ribeiroca640d52020-01-27 19:12:01 -03007884 * This function cleans up the compositor state and then destroys it.
Giulio Camuffo459137b2014-10-11 23:56:24 +03007885 *
Leandro Ribeiroca640d52020-01-27 19:12:01 -03007886 * @param compositor The compositor to be destroyed.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007887 *
Marius Vlad284d5342019-06-24 12:00:47 +03007888 * @ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007889 */
7890WL_EXPORT void
Leandro Ribeiroca640d52020-01-27 19:12:01 -03007891weston_compositor_destroy(struct weston_compositor *compositor)
Giulio Camuffo459137b2014-10-11 23:56:24 +03007892{
7893 /* prevent further rendering while shutting down */
7894 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
7895
7896 wl_signal_emit(&compositor->destroy_signal, compositor);
7897
7898 weston_compositor_xkb_destroy(compositor);
7899
Giulio Camuffo2d24e642015-10-03 16:25:15 +03007900 if (compositor->backend)
7901 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007902
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03007903 /* The backend is responsible for destroying the heads. */
7904 assert(wl_list_empty(&compositor->head_list));
7905
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007906 weston_plugin_api_destroy_list(compositor);
7907
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03007908 if (compositor->heads_changed_source)
7909 wl_event_source_remove(compositor->heads_changed_source);
7910
Leandro Ribeirof0149642019-12-18 15:52:18 -03007911 weston_log_scope_destroy(compositor->debug_scene);
Daniel Stonece62cb32018-07-20 09:46:24 +01007912 compositor->debug_scene = NULL;
Marius Vladda104eb2019-09-05 14:31:01 +03007913
Leandro Ribeirof0149642019-12-18 15:52:18 -03007914 weston_log_scope_destroy(compositor->timeline);
Marius Vladda104eb2019-09-05 14:31:01 +03007915 compositor->timeline = NULL;
Pekka Paalanena5630ea2017-10-12 13:13:42 +02007916
Giulio Camuffo459137b2014-10-11 23:56:24 +03007917 free(compositor);
7918}
7919
7920/** Instruct the compositor to exit.
7921 *
7922 * This functions does not directly destroy the compositor object, it merely
7923 * command it to start the tear down process. It is not guaranteed that the
7924 * tear down will happen immediately.
7925 *
7926 * \param compositor The compositor to tear down.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007927 *
7928 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007929 */
7930WL_EXPORT void
7931weston_compositor_exit(struct weston_compositor *compositor)
7932{
7933 compositor->exit(compositor);
7934}
7935
7936/** Return the user data stored in the compositor.
7937 *
7938 * This function returns the user data pointer set with user_data parameter
7939 * to the \ref weston_compositor_create function.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007940 *
7941 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007942 */
7943WL_EXPORT void *
7944weston_compositor_get_user_data(struct weston_compositor *compositor)
7945{
7946 return compositor->user_data;
7947}
Pekka Paalanendd186732016-06-03 14:49:54 +03007948
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007949static const char * const backend_map[] = {
7950 [WESTON_BACKEND_DRM] = "drm-backend.so",
7951 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
7952 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
7953 [WESTON_BACKEND_RDP] = "rdp-backend.so",
7954 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
7955 [WESTON_BACKEND_X11] = "x11-backend.so",
7956};
7957
Pekka Paalanendd186732016-06-03 14:49:54 +03007958/** Load a backend into a weston_compositor
7959 *
7960 * A backend must be loaded to make a weston_compositor work. A backend
7961 * provides input and output capabilities, and determines the renderer to use.
7962 *
7963 * \param compositor A compositor that has not had a backend loaded yet.
7964 * \param backend Name of the backend file.
7965 * \param config_base A pointer to a backend-specific configuration
7966 * structure's 'base' member.
7967 *
7968 * \return 0 on success, or -1 on error.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007969 *
7970 * \ingroup compositor
Pekka Paalanendd186732016-06-03 14:49:54 +03007971 */
7972WL_EXPORT int
7973weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007974 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03007975 struct weston_backend_config *config_base)
7976{
7977 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03007978 struct weston_backend_config *config_base);
7979
Pekka Paalanend7e35112017-08-29 17:04:12 +03007980 if (compositor->backend) {
7981 weston_log("Error: attempt to load a backend when one is already loaded\n");
7982 return -1;
7983 }
7984
Quentin Glidic887c0182016-07-10 11:00:53 +02007985 if (backend >= ARRAY_LENGTH(backend_map))
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007986 return -1;
7987
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01007988 backend_init = weston_load_module(backend_map[backend], "weston_backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03007989 if (!backend_init)
7990 return -1;
7991
Pekka Paalanend7e35112017-08-29 17:04:12 +03007992 if (backend_init(compositor, config_base) < 0) {
7993 compositor->backend = NULL;
7994 return -1;
7995 }
7996
7997 return 0;
Pekka Paalanendd186732016-06-03 14:49:54 +03007998}
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007999
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008000/** weston_compositor_load_xwayland
8001 * \ingroup compositor
8002 */
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008003WL_EXPORT int
8004weston_compositor_load_xwayland(struct weston_compositor *compositor)
8005{
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01008006 int (*module_init)(struct weston_compositor *ec);
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008007
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01008008 module_init = weston_load_module("xwayland.so", "weston_module_init");
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008009 if (!module_init)
8010 return -1;
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01008011 if (module_init(compositor) < 0)
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008012 return -1;
8013 return 0;
8014}
Vasilis Liaskovitis486b4632018-10-10 16:14:55 +02008015
8016/** Resolve an internal compositor error by disconnecting the client.
8017 *
8018 * This function is used in cases when the wl_buffer turns out
8019 * unusable and there is no fallback path.
8020 *
8021 * It is possible the fault is caused by a compositor bug, the underlying
8022 * graphics stack bug or normal behaviour, or perhaps a client mistake.
8023 * In any case, the options are to either composite garbage or nothing,
8024 * or disconnect the client. This is a helper function for the latter.
8025 *
8026 * The error is sent as an INVALID_OBJECT error on the client's wl_display.
8027 *
8028 * \param buffer The weston buffer that is unusable.
8029 * \param msg A custom error message attached to the protocol error.
8030 */
8031WL_EXPORT void
8032weston_buffer_send_server_error(struct weston_buffer *buffer,
8033 const char *msg)
8034{
8035 struct wl_client *client;
8036 struct wl_resource *display_resource;
8037 uint32_t id;
8038
8039 assert(buffer->resource);
8040 id = wl_resource_get_id(buffer->resource);
8041 client = wl_resource_get_client(buffer->resource);
8042 display_resource = wl_client_get_object(client, 1);
8043
8044 assert(display_resource);
8045 wl_resource_post_error(display_resource,
8046 WL_DISPLAY_ERROR_INVALID_OBJECT,
8047 "server error with "
8048 "wl_buffer@%u: %s", id, msg);
8049}
Ankit Nautiyal93dde242019-07-08 11:46:42 +05308050
8051WL_EXPORT void
8052weston_output_disable_planes_incr(struct weston_output *output)
8053{
8054 output->disable_planes++;
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05308055 /*
8056 * If disable_planes changes from 0 to non-zero, it means some type of
8057 * recording of content has started, and therefore protection level of
8058 * the protected surfaces must be updated to avoid the recording of
8059 * the protected content.
8060 */
8061 if (output->disable_planes == 1)
8062 weston_schedule_surface_protection_update(output->compositor);
Ankit Nautiyal93dde242019-07-08 11:46:42 +05308063}
8064
8065WL_EXPORT void
8066weston_output_disable_planes_decr(struct weston_output *output)
8067{
8068 output->disable_planes--;
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05308069 /*
8070 * If disable_planes changes from non-zero to 0, it means no content
8071 * recording is going on any more, and the protected and surfaces can be
8072 * shown without any apprehensions about content being recorded.
8073 */
8074 if (output->disable_planes == 0)
8075 weston_schedule_surface_protection_update(output->compositor);
8076
Ankit Nautiyal93dde242019-07-08 11:46:42 +05308077}