blob: 117c6aa30addb689742330366d58d8453432dc2a [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 Paalanen1a4f87d2021-04-30 14:48:52 +03004 * Copyright © 2012-2018, 2021 Collabora, Ltd.
Pekka Paalanen925788f2018-04-19 14:20:01 +03005 * 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"
Pekka Paalanen90a5ffa2021-02-25 12:03:28 +020077#include "color.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050078
Marius Vlad4e036292019-07-09 00:36:20 +030079#include "weston-log-internal.h"
80
Marius Vlad78984ee2019-06-11 00:05:08 +030081/**
82 * \defgroup head Head
83 * \defgroup output Output
84 * \defgroup compositor Compositor
85 */
86
Pekka Paalanen0513a952014-05-21 16:17:27 +030087#define DEFAULT_REPAINT_WINDOW 7 /* milliseconds */
88
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020089static void
Pekka Paalanend72bad22017-03-29 17:01:41 +030090weston_output_update_matrix(struct weston_output *output);
91
92static void
Alexander Larsson0b135062013-05-28 16:23:36 +020093weston_output_transform_scale_init(struct weston_output *output,
94 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020095
Rob Bradford27b17932013-06-26 18:08:46 +010096static void
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +030097weston_compositor_build_view_list(struct weston_compositor *compositor,
98 struct weston_output *output);
Rob Bradford27b17932013-06-26 18:08:46 +010099
Pekka Paalanendcac3512017-12-08 14:13:34 +0200100static char *
101weston_output_create_heads_string(struct weston_output *output);
102
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +0300103static struct weston_paint_node *
104weston_paint_node_create(struct weston_surface *surface,
105 struct weston_view *view,
106 struct weston_output *output)
107{
108 struct weston_paint_node *pnode;
Pekka Paalanen90a5ffa2021-02-25 12:03:28 +0200109 struct weston_paint_node *existing_node;
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +0300110
111 assert(view->surface == surface);
112
113 pnode = zalloc(sizeof *pnode);
114 if (!pnode)
115 return NULL;
116
Pekka Paalanen90a5ffa2021-02-25 12:03:28 +0200117 /*
118 * Invariant: all paint nodes with the same surface+output have the
119 * same surf_xform state.
120 */
121 wl_list_for_each(existing_node, &view->paint_node_list, view_link) {
122 assert(existing_node->surface == surface);
123 if (existing_node->output != output)
124 continue;
125
126 weston_surface_color_transform_copy(&pnode->surf_xform,
127 &existing_node->surf_xform);
128 pnode->surf_xform_valid = existing_node->surf_xform_valid;
129 break;
130 }
131
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +0300132 pnode->surface = surface;
133 wl_list_insert(&surface->paint_node_list, &pnode->surface_link);
134
135 pnode->view = view;
136 wl_list_insert(&view->paint_node_list, &pnode->view_link);
137
138 pnode->output = output;
139 wl_list_insert(&output->paint_node_list, &pnode->output_link);
140
Pekka Paalanen2fddc532021-04-30 17:41:29 +0300141 wl_list_init(&pnode->z_order_link);
142
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +0300143 return pnode;
144}
145
146static void
147weston_paint_node_destroy(struct weston_paint_node *pnode)
148{
149 assert(pnode->view->surface == pnode->surface);
150 wl_list_remove(&pnode->surface_link);
151 wl_list_remove(&pnode->view_link);
152 wl_list_remove(&pnode->output_link);
Pekka Paalanen2fddc532021-04-30 17:41:29 +0300153 wl_list_remove(&pnode->z_order_link);
Pekka Paalanen90a5ffa2021-02-25 12:03:28 +0200154 assert(pnode->surf_xform_valid || !pnode->surf_xform.transform);
155 weston_surface_color_transform_fini(&pnode->surf_xform);
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +0300156 free(pnode);
157}
158
Pekka Paalanen6528c032017-03-28 15:27:10 +0300159/** Send wl_output events for mode and scale changes
160 *
161 * \param head Send on all resources bound to this head.
162 * \param mode_changed If true, send the current mode.
163 * \param scale_changed If true, send the current scale.
164 */
165static void
166weston_mode_switch_send_events(struct weston_head *head,
167 bool mode_changed, bool scale_changed)
168{
169 struct weston_output *output = head->output;
170 struct wl_resource *resource;
171 int version;
172
173 wl_resource_for_each(resource, &head->resource_list) {
174 if (mode_changed) {
175 wl_output_send_mode(resource,
176 output->current_mode->flags,
177 output->current_mode->width,
178 output->current_mode->height,
179 output->current_mode->refresh);
180 }
181
182 version = wl_resource_get_version(resource);
183 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION && scale_changed)
184 wl_output_send_scale(resource, output->current_scale);
185
186 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
187 wl_output_send_done(resource);
188 }
Roman Gilge97391c2019-03-29 13:01:06 +0100189 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
190 zxdg_output_v1_send_logical_position(resource,
191 output->x,
192 output->y);
193 zxdg_output_v1_send_logical_size(resource,
194 output->width,
195 output->height);
196 zxdg_output_v1_send_done(resource);
197 }
Pekka Paalanen6528c032017-03-28 15:27:10 +0300198}
199
200static void
201weston_mode_switch_finish(struct weston_output *output,
202 int mode_changed, int scale_changed)
Alex Wu2dda6042012-04-17 17:20:47 +0800203{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200204 struct weston_seat *seat;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300205 struct weston_head *head;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200206 pixman_region32_t old_output_region;
Alexander Larsson355748e2013-05-28 16:23:38 +0200207
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200208 pixman_region32_init(&old_output_region);
209 pixman_region32_copy(&old_output_region, &output->region);
210
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200211 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +0200212 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200213
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200214 pixman_region32_init_rect(&output->region, output->x, output->y,
215 output->width, output->height);
216
217 weston_output_update_matrix(output);
218
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200219 /* If a pointer falls outside the outputs new geometry, move it to its
220 * lower-right corner */
221 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500222 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200223 int32_t x, y;
224
225 if (!pointer)
226 continue;
227
228 x = wl_fixed_to_int(pointer->x);
229 y = wl_fixed_to_int(pointer->y);
230
231 if (!pixman_region32_contains_point(&old_output_region,
232 x, y, NULL) ||
233 pixman_region32_contains_point(&output->region,
234 x, y, NULL))
235 continue;
236
237 if (x >= output->x + output->width)
238 x = output->x + output->width - 1;
239 if (y >= output->y + output->height)
240 y = output->y + output->height - 1;
241
242 pointer->x = wl_fixed_from_int(x);
243 pointer->y = wl_fixed_from_int(y);
244 }
245
246 pixman_region32_fini(&old_output_region);
247
Derek Foremandd4cd332014-11-10 10:29:59 -0600248 if (!mode_changed && !scale_changed)
249 return;
250
Hardening57388e42013-09-18 23:56:36 +0200251 /* notify clients of the changes */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300252 wl_list_for_each(head, &output->head_list, output_link)
253 weston_mode_switch_send_events(head,
254 mode_changed, scale_changed);
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600255}
256
David Fort0de859e2016-05-27 23:22:57 +0200257static void
258weston_compositor_reflow_outputs(struct weston_compositor *compositor,
259 struct weston_output *resized_output, int delta_width);
260
Marius Vlad55d87362019-06-11 01:15:35 +0300261/**
262 * \ingroup output
263 */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600264WL_EXPORT int
265weston_output_mode_set_native(struct weston_output *output,
266 struct weston_mode *mode,
267 int32_t scale)
268{
269 int ret;
270 int mode_changed = 0, scale_changed = 0;
David Fort0de859e2016-05-27 23:22:57 +0200271 int32_t old_width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600272
273 if (!output->switch_mode)
274 return -1;
275
276 if (!output->original_mode) {
277 mode_changed = 1;
278 ret = output->switch_mode(output, mode);
279 if (ret < 0)
280 return ret;
281 if (output->current_scale != scale) {
282 scale_changed = 1;
283 output->current_scale = scale;
Hardening57388e42013-09-18 23:56:36 +0200284 }
285 }
286
David Fort0de859e2016-05-27 23:22:57 +0200287 old_width = output->width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600288 output->native_mode = mode;
289 output->native_scale = scale;
290
291 weston_mode_switch_finish(output, mode_changed, scale_changed);
292
David Fort0de859e2016-05-27 23:22:57 +0200293 if (mode_changed || scale_changed) {
294 weston_compositor_reflow_outputs(output->compositor, output, output->width - old_width);
295
296 wl_signal_emit(&output->compositor->output_resized_signal, output);
297 }
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600298 return 0;
299}
300
Marius Vlad55d87362019-06-11 01:15:35 +0300301/**
302 * \ingroup output
303 */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600304WL_EXPORT int
305weston_output_mode_switch_to_native(struct weston_output *output)
306{
307 int ret;
308 int mode_changed = 0, scale_changed = 0;
309
310 if (!output->switch_mode)
311 return -1;
312
313 if (!output->original_mode) {
314 weston_log("already in the native mode\n");
315 return -1;
316 }
317 /* the non fullscreen clients haven't seen a mode set since we
318 * switched into a temporary, so we need to notify them if the
319 * mode at that time is different from the native mode now.
320 */
321 mode_changed = (output->original_mode != output->native_mode);
322 scale_changed = (output->original_scale != output->native_scale);
323
324 ret = output->switch_mode(output, output->native_mode);
325 if (ret < 0)
326 return ret;
327
328 output->current_scale = output->native_scale;
329
330 output->original_mode = NULL;
331 output->original_scale = 0;
332
333 weston_mode_switch_finish(output, mode_changed, scale_changed);
334
335 return 0;
336}
337
Marius Vlad55d87362019-06-11 01:15:35 +0300338/**
339 * \ingroup output
340 */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600341WL_EXPORT int
342weston_output_mode_switch_to_temporary(struct weston_output *output,
343 struct weston_mode *mode,
344 int32_t scale)
345{
346 int ret;
347
348 if (!output->switch_mode)
349 return -1;
350
351 /* original_mode is the last mode non full screen clients have seen,
352 * so we shouldn't change it if we already have one set.
353 */
354 if (!output->original_mode) {
355 output->original_mode = output->native_mode;
356 output->original_scale = output->native_scale;
357 }
358 ret = output->switch_mode(output, mode);
359 if (ret < 0)
360 return ret;
361
362 output->current_scale = scale;
363
364 weston_mode_switch_finish(output, 0, 0);
365
366 return 0;
Alex Wu2dda6042012-04-17 17:20:47 +0800367}
368
Benjamin Franzke06286262011-05-06 19:12:33 +0200369static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300370region_init_infinite(pixman_region32_t *region)
371{
372 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
373 UINT32_MAX, UINT32_MAX);
374}
375
Pekka Paalanene67858b2013-04-25 13:57:42 +0300376static struct weston_subsurface *
377weston_surface_to_subsurface(struct weston_surface *surface);
378
Jason Ekstranda7af7042013-10-12 22:38:11 -0500379WL_EXPORT struct weston_view *
380weston_view_create(struct weston_surface *surface)
381{
382 struct weston_view *view;
383
Bryce Harringtonde16d892014-11-20 22:21:57 -0800384 view = zalloc(sizeof *view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500385 if (view == NULL)
386 return NULL;
387
388 view->surface = surface;
Daniel Stonefb4869d2016-12-09 16:27:54 +0000389 view->plane = &surface->compositor->primary_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500390
Jason Ekstranda7af7042013-10-12 22:38:11 -0500391 /* Assign to surface */
392 wl_list_insert(&surface->views, &view->surface_link);
393
394 wl_signal_init(&view->destroy_signal);
395 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300396 wl_list_init(&view->layer_link.link);
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +0300397 wl_list_init(&view->paint_node_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500398
Jason Ekstranda7af7042013-10-12 22:38:11 -0500399 pixman_region32_init(&view->clip);
400
401 view->alpha = 1.0;
402 pixman_region32_init(&view->transform.opaque);
403
404 wl_list_init(&view->geometry.transformation_list);
405 wl_list_insert(&view->geometry.transformation_list,
406 &view->transform.position.link);
407 weston_matrix_init(&view->transform.position.matrix);
408 wl_list_init(&view->geometry.child_list);
Pekka Paalanen380adf52015-02-16 14:39:11 +0200409 pixman_region32_init(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500410 pixman_region32_init(&view->transform.boundingbox);
411 view->transform.dirty = 1;
412
Jason Ekstranda7af7042013-10-12 22:38:11 -0500413 return view;
414}
415
Jason Ekstrand108865d2014-06-26 10:04:49 -0700416struct weston_frame_callback {
417 struct wl_resource *resource;
418 struct wl_list link;
419};
420
Pekka Paalanen133e4392014-09-23 22:08:46 -0400421struct weston_presentation_feedback {
422 struct wl_resource *resource;
423
424 /* XXX: could use just wl_resource_get_link() instead */
425 struct wl_list link;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200426
427 /* The per-surface feedback flags */
428 uint32_t psf_flags;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400429};
430
431static void
432weston_presentation_feedback_discard(
433 struct weston_presentation_feedback *feedback)
434{
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200435 wp_presentation_feedback_send_discarded(feedback->resource);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400436 wl_resource_destroy(feedback->resource);
437}
438
439static void
440weston_presentation_feedback_discard_list(struct wl_list *list)
441{
442 struct weston_presentation_feedback *feedback, *tmp;
443
444 wl_list_for_each_safe(feedback, tmp, list, link)
445 weston_presentation_feedback_discard(feedback);
446}
447
448static void
449weston_presentation_feedback_present(
450 struct weston_presentation_feedback *feedback,
451 struct weston_output *output,
452 uint32_t refresh_nsec,
453 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200454 uint64_t seq,
455 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400456{
457 struct wl_client *client = wl_resource_get_client(feedback->resource);
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300458 struct weston_head *head;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400459 struct wl_resource *o;
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200460 uint32_t tv_sec_hi;
461 uint32_t tv_sec_lo;
462 uint32_t tv_nsec;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300463 bool done = false;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400464
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300465 wl_list_for_each(head, &output->head_list, output_link) {
466 wl_resource_for_each(o, &head->resource_list) {
467 if (wl_resource_get_client(o) != client)
468 continue;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400469
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300470 wp_presentation_feedback_send_sync_output(feedback->resource, o);
471 done = true;
472 }
473
474 /* For clone mode, send it for just one wl_output global,
475 * they are all equivalent anyway.
476 */
477 if (done)
478 break;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400479 }
480
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200481 timespec_to_proto(ts, &tv_sec_hi, &tv_sec_lo, &tv_nsec);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200482 wp_presentation_feedback_send_presented(feedback->resource,
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200483 tv_sec_hi, tv_sec_lo, tv_nsec,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200484 refresh_nsec,
485 seq >> 32, seq & 0xffffffff,
486 flags | feedback->psf_flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400487 wl_resource_destroy(feedback->resource);
488}
489
490static void
491weston_presentation_feedback_present_list(struct wl_list *list,
492 struct weston_output *output,
493 uint32_t refresh_nsec,
494 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200495 uint64_t seq,
496 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400497{
498 struct weston_presentation_feedback *feedback, *tmp;
499
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200500 assert(!(flags & WP_PRESENTATION_FEEDBACK_INVALID) ||
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200501 wl_list_empty(list));
502
Pekka Paalanen133e4392014-09-23 22:08:46 -0400503 wl_list_for_each_safe(feedback, tmp, list, link)
504 weston_presentation_feedback_present(feedback, output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200505 refresh_nsec, ts, seq,
506 flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400507}
508
Jason Ekstrand7b982072014-05-20 14:33:03 -0500509static void
510surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
511{
512 struct weston_surface_state *state =
513 container_of(listener, struct weston_surface_state,
514 buffer_destroy_listener);
515
516 state->buffer = NULL;
517}
518
519static void
520weston_surface_state_init(struct weston_surface_state *state)
521{
522 state->newly_attached = 0;
523 state->buffer = NULL;
524 state->buffer_destroy_listener.notify =
525 surface_state_handle_buffer_destroy;
526 state->sx = 0;
527 state->sy = 0;
528
Derek Foreman152254b2015-11-26 14:17:48 -0600529 pixman_region32_init(&state->damage_surface);
530 pixman_region32_init(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500531 pixman_region32_init(&state->opaque);
532 region_init_infinite(&state->input);
533
534 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400535 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500536
537 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
538 state->buffer_viewport.buffer.scale = 1;
539 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
540 state->buffer_viewport.surface.width = -1;
541 state->buffer_viewport.changed = 0;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300542
543 state->acquire_fence_fd = -1;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +0530544
545 state->desired_protection = WESTON_HDCP_DISABLE;
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +0530546 state->protection_mode = WESTON_SURFACE_PROTECTION_MODE_RELAXED;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500547}
548
549static void
550weston_surface_state_fini(struct weston_surface_state *state)
551{
552 struct weston_frame_callback *cb, *next;
553
554 wl_list_for_each_safe(cb, next,
555 &state->frame_callback_list, link)
556 wl_resource_destroy(cb->resource);
557
Pekka Paalanen133e4392014-09-23 22:08:46 -0400558 weston_presentation_feedback_discard_list(&state->feedback_list);
559
Jason Ekstrand7b982072014-05-20 14:33:03 -0500560 pixman_region32_fini(&state->input);
561 pixman_region32_fini(&state->opaque);
Derek Foreman152254b2015-11-26 14:17:48 -0600562 pixman_region32_fini(&state->damage_surface);
563 pixman_region32_fini(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500564
565 if (state->buffer)
566 wl_list_remove(&state->buffer_destroy_listener.link);
567 state->buffer = NULL;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300568
569 fd_clear(&state->acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +0300570 weston_buffer_release_reference(&state->buffer_release_ref, NULL);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500571}
572
573static void
574weston_surface_state_set_buffer(struct weston_surface_state *state,
575 struct weston_buffer *buffer)
576{
577 if (state->buffer == buffer)
578 return;
579
580 if (state->buffer)
581 wl_list_remove(&state->buffer_destroy_listener.link);
582 state->buffer = buffer;
583 if (state->buffer)
584 wl_signal_add(&state->buffer->destroy_signal,
585 &state->buffer_destroy_listener);
586}
587
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500588WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500589weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500590{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500591 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400592
Bryce Harringtonde16d892014-11-20 22:21:57 -0800593 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400594 if (surface == NULL)
595 return NULL;
596
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500597 wl_signal_init(&surface->destroy_signal);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +0800598 wl_signal_init(&surface->commit_signal);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500599
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500600 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200601 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400602
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200603 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
604 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200605 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
606 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500607
608 weston_surface_state_init(&surface->pending);
609
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400610 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500611 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300612 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400613
Jason Ekstranda7af7042013-10-12 22:38:11 -0500614 wl_list_init(&surface->views);
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +0300615 wl_list_init(&surface->paint_node_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500616
617 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400618 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500619
Pekka Paalanene67858b2013-04-25 13:57:42 +0300620 wl_list_init(&surface->subsurface_list);
621 wl_list_init(&surface->subsurface_list_pending);
622
Jason Ekstrand1e059042014-10-16 10:55:19 -0500623 weston_matrix_init(&surface->buffer_to_surface_matrix);
624 weston_matrix_init(&surface->surface_to_buffer_matrix);
625
Jonas Ådahld3414f22016-07-22 17:56:31 +0800626 wl_list_init(&surface->pointer_constraints);
627
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300628 surface->acquire_fence_fd = -1;
629
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +0530630 surface->desired_protection = WESTON_HDCP_DISABLE;
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +0530631 surface->current_protection = WESTON_HDCP_DISABLE;
632 surface->protection_mode = WESTON_SURFACE_PROTECTION_MODE_RELAXED;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +0530633
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400634 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500635}
636
Alex Wu8811bf92012-02-28 18:07:54 +0800637WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500638weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200639 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500640{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100641 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Philipp Zabel195dade2018-09-03 19:44:59 +0200642 surface->is_opaque = !(alpha < 1.0);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500643}
644
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400645WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500646weston_view_to_global_float(struct weston_view *view,
647 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200648{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500649 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200650 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
651
Jason Ekstranda7af7042013-10-12 22:38:11 -0500652 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200653
654 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200655 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700656 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200657 v.f[3]);
658 *x = 0;
659 *y = 0;
660 return;
661 }
662
663 *x = v.f[0] / v.f[3];
664 *y = v.f[1] / v.f[3];
665 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500666 *x = sx + view->geometry.x;
667 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200668 }
669}
670
Pekka Paalanen0df44772020-02-14 14:50:03 +0200671/** Transform a point to buffer coordinates
672 *
673 * \param width Surface width.
674 * \param height Surface height.
675 * \param transform Buffer transform.
676 * \param scale Buffer scale.
677 * \param sx Surface x coordinate of a point.
678 * \param sy Surface y coordinate of a point.
679 * \param[out] bx Buffer x coordinate of the point.
680 * \param[out] by Buffer Y coordinate of the point.
681 *
682 * Converts the given surface-local coordinates to buffer coordinates
683 * according to the given buffer transform and scale.
684 * This ignores wp_viewport.
685 *
686 * The given width and height must be the result of inverse scaled and
687 * inverse transformed buffer size.
688 */
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500689WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200690weston_transformed_coord(int width, int height,
691 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200692 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200693 float sx, float sy, float *bx, float *by)
694{
695 switch (transform) {
696 case WL_OUTPUT_TRANSFORM_NORMAL:
697 default:
698 *bx = sx;
699 *by = sy;
700 break;
701 case WL_OUTPUT_TRANSFORM_FLIPPED:
702 *bx = width - sx;
703 *by = sy;
704 break;
705 case WL_OUTPUT_TRANSFORM_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200706 *bx = sy;
707 *by = width - sx;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200708 break;
709 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200710 *bx = sy;
711 *by = sx;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200712 break;
713 case WL_OUTPUT_TRANSFORM_180:
714 *bx = width - sx;
715 *by = height - sy;
716 break;
717 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
718 *bx = sx;
719 *by = height - sy;
720 break;
721 case WL_OUTPUT_TRANSFORM_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200722 *bx = height - sy;
723 *by = sx;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200724 break;
725 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200726 *bx = height - sy;
727 *by = width - sx;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200728 break;
729 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200730
731 *bx *= scale;
732 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200733}
734
Pekka Paalanen0df44772020-02-14 14:50:03 +0200735/** Transform a rectangle to buffer coordinates
736 *
737 * \param width Surface width.
738 * \param height Surface height.
739 * \param transform Buffer transform.
740 * \param scale Buffer scale.
741 * \param rect Rectangle in surface coordinates.
742 * \return Rectangle in buffer coordinates.
743 *
744 * Converts the given surface-local rectangle to buffer coordinates
745 * according to the given buffer transform and scale. The resulting
746 * rectangle is guaranteed to be well-formed.
747 * This ignores wp_viewport.
748 *
749 * The given width and height must be the result of inverse scaled and
750 * inverse transformed buffer size.
751 */
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200752WL_EXPORT pixman_box32_t
753weston_transformed_rect(int width, int height,
754 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200755 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200756 pixman_box32_t rect)
757{
758 float x1, x2, y1, y2;
759
760 pixman_box32_t ret;
761
Alexander Larsson4ea95522013-05-22 14:41:37 +0200762 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200763 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200764 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200765 rect.x2, rect.y2, &x2, &y2);
766
767 if (x1 <= x2) {
768 ret.x1 = x1;
769 ret.x2 = x2;
770 } else {
771 ret.x1 = x2;
772 ret.x2 = x1;
773 }
774
775 if (y1 <= y2) {
776 ret.y1 = y1;
777 ret.y2 = y2;
778 } else {
779 ret.y1 = y2;
780 ret.y2 = y1;
781 }
782
783 return ret;
784}
785
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600786/** Transform a region by a matrix, restricted to axis-aligned transformations
787 *
788 * Warning: This function does not work for projective, affine, or matrices
789 * that encode arbitrary rotations. Only 90-degree step rotations are
790 * supported.
791 */
792WL_EXPORT void
793weston_matrix_transform_region(pixman_region32_t *dest,
794 struct weston_matrix *matrix,
795 pixman_region32_t *src)
796{
797 pixman_box32_t *src_rects, *dest_rects;
798 int nrects, i;
799
800 src_rects = pixman_region32_rectangles(src, &nrects);
801 dest_rects = malloc(nrects * sizeof(*dest_rects));
802 if (!dest_rects)
803 return;
804
805 for (i = 0; i < nrects; i++) {
806 struct weston_vector vec1 = {{
807 src_rects[i].x1, src_rects[i].y1, 0, 1
808 }};
809 weston_matrix_transform(matrix, &vec1);
810 vec1.f[0] /= vec1.f[3];
811 vec1.f[1] /= vec1.f[3];
812
813 struct weston_vector vec2 = {{
814 src_rects[i].x2, src_rects[i].y2, 0, 1
815 }};
816 weston_matrix_transform(matrix, &vec2);
817 vec2.f[0] /= vec2.f[3];
818 vec2.f[1] /= vec2.f[3];
819
820 if (vec1.f[0] < vec2.f[0]) {
821 dest_rects[i].x1 = floor(vec1.f[0]);
822 dest_rects[i].x2 = ceil(vec2.f[0]);
823 } else {
824 dest_rects[i].x1 = floor(vec2.f[0]);
825 dest_rects[i].x2 = ceil(vec1.f[0]);
826 }
827
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600828 if (vec1.f[1] < vec2.f[1]) {
829 dest_rects[i].y1 = floor(vec1.f[1]);
830 dest_rects[i].y2 = ceil(vec2.f[1]);
831 } else {
832 dest_rects[i].y1 = floor(vec2.f[1]);
833 dest_rects[i].y2 = ceil(vec1.f[1]);
834 }
835 }
836
837 pixman_region32_clear(dest);
838 pixman_region32_init_rects(dest, dest_rects, nrects);
839 free(dest_rects);
840}
841
Pekka Paalanen0df44772020-02-14 14:50:03 +0200842/** Transform a region to buffer coordinates
843 *
844 * \param width Surface width.
845 * \param height Surface height.
846 * \param transform Buffer transform.
847 * \param scale Buffer scale.
848 * \param[in] src Region in surface coordinates.
849 * \param[out] dest Resulting region in buffer coordinates.
850 *
851 * Converts the given surface-local region to buffer coordinates
852 * according to the given buffer transform and scale.
853 * This ignores wp_viewport.
854 *
855 * The given width and height must be the result of inverse scaled and
856 * inverse transformed buffer size.
857 *
858 * src and dest are allowed to point to the same memory for in-place conversion.
859 */
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200860WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500861weston_transformed_region(int width, int height,
862 enum wl_output_transform transform,
863 int32_t scale,
864 pixman_region32_t *src, pixman_region32_t *dest)
865{
866 pixman_box32_t *src_rects, *dest_rects;
867 int nrects, i;
868
869 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
870 if (src != dest)
871 pixman_region32_copy(dest, src);
872 return;
873 }
874
875 src_rects = pixman_region32_rectangles(src, &nrects);
876 dest_rects = malloc(nrects * sizeof(*dest_rects));
877 if (!dest_rects)
878 return;
879
880 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
881 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
882 } else {
883 for (i = 0; i < nrects; i++) {
884 switch (transform) {
885 default:
886 case WL_OUTPUT_TRANSFORM_NORMAL:
887 dest_rects[i].x1 = src_rects[i].x1;
888 dest_rects[i].y1 = src_rects[i].y1;
889 dest_rects[i].x2 = src_rects[i].x2;
890 dest_rects[i].y2 = src_rects[i].y2;
891 break;
892 case WL_OUTPUT_TRANSFORM_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200893 dest_rects[i].x1 = src_rects[i].y1;
894 dest_rects[i].y1 = width - src_rects[i].x2;
895 dest_rects[i].x2 = src_rects[i].y2;
896 dest_rects[i].y2 = width - src_rects[i].x1;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500897 break;
898 case WL_OUTPUT_TRANSFORM_180:
899 dest_rects[i].x1 = width - src_rects[i].x2;
900 dest_rects[i].y1 = height - src_rects[i].y2;
901 dest_rects[i].x2 = width - src_rects[i].x1;
902 dest_rects[i].y2 = height - src_rects[i].y1;
903 break;
904 case WL_OUTPUT_TRANSFORM_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200905 dest_rects[i].x1 = height - src_rects[i].y2;
906 dest_rects[i].y1 = src_rects[i].x1;
907 dest_rects[i].x2 = height - src_rects[i].y1;
908 dest_rects[i].y2 = src_rects[i].x2;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500909 break;
910 case WL_OUTPUT_TRANSFORM_FLIPPED:
911 dest_rects[i].x1 = width - src_rects[i].x2;
912 dest_rects[i].y1 = src_rects[i].y1;
913 dest_rects[i].x2 = width - src_rects[i].x1;
914 dest_rects[i].y2 = src_rects[i].y2;
915 break;
916 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200917 dest_rects[i].x1 = src_rects[i].y1;
918 dest_rects[i].y1 = src_rects[i].x1;
919 dest_rects[i].x2 = src_rects[i].y2;
920 dest_rects[i].y2 = src_rects[i].x2;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500921 break;
922 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
923 dest_rects[i].x1 = src_rects[i].x1;
924 dest_rects[i].y1 = height - src_rects[i].y2;
925 dest_rects[i].x2 = src_rects[i].x2;
926 dest_rects[i].y2 = height - src_rects[i].y1;
927 break;
928 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200929 dest_rects[i].x1 = height - src_rects[i].y2;
930 dest_rects[i].y1 = width - src_rects[i].x2;
931 dest_rects[i].x2 = height - src_rects[i].y1;
932 dest_rects[i].y2 = width - src_rects[i].x1;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500933 break;
934 }
935 }
936 }
937
938 if (scale != 1) {
939 for (i = 0; i < nrects; i++) {
940 dest_rects[i].x1 *= scale;
941 dest_rects[i].x2 *= scale;
942 dest_rects[i].y1 *= scale;
943 dest_rects[i].y2 *= scale;
944 }
945 }
946
947 pixman_region32_clear(dest);
948 pixman_region32_init_rects(dest, dest_rects, nrects);
949 free(dest_rects);
950}
951
Jonny Lamb74130762013-11-26 18:19:46 +0100952static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300953viewport_surface_to_buffer(struct weston_surface *surface,
954 float sx, float sy, float *bx, float *by)
Jonny Lamb74130762013-11-26 18:19:46 +0100955{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200956 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200957 double src_width, src_height;
958 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200959
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200960 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
961 if (vp->surface.width == -1) {
962 *bx = sx;
963 *by = sy;
964 return;
965 }
Jonny Lamb74130762013-11-26 18:19:46 +0100966
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200967 src_x = 0.0;
968 src_y = 0.0;
969 src_width = surface->width_from_buffer;
970 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100971 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200972 src_x = wl_fixed_to_double(vp->buffer.src_x);
973 src_y = wl_fixed_to_double(vp->buffer.src_y);
974 src_width = wl_fixed_to_double(vp->buffer.src_width);
975 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100976 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200977
978 *bx = sx * src_width / surface->width + src_x;
979 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100980}
981
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500982WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200983weston_surface_to_buffer_float(struct weston_surface *surface,
984 float sx, float sy, float *bx, float *by)
985{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200986 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
987
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300988 /* first transform coordinates if the viewport is set */
989 viewport_surface_to_buffer(surface, sx, sy, bx, by);
Jonny Lamb74130762013-11-26 18:19:46 +0100990
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500991 weston_transformed_coord(surface->width_from_buffer,
992 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200993 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100994 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200995}
996
Derek Foreman99739672015-12-03 16:38:11 -0600997/** Transform a rectangle from surface coordinates to buffer coordinates
998 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300999 * \param surface The surface to fetch wp_viewport and buffer transformation
Derek Foreman99739672015-12-03 16:38:11 -06001000 * from.
1001 * \param rect The rectangle to transform.
1002 * \return The transformed rectangle.
1003 *
1004 * Viewport and buffer transformations can only do translation, scaling,
1005 * and rotations in 90-degree steps. Therefore the only loss in the
1006 * conversion is coordinate rounding.
1007 *
1008 * However, some coordinate rounding takes place as an intermediate
1009 * step before the buffer scale factor is applied, so the rectangle
1010 * boundary may not be exactly as expected.
1011 *
1012 * This is OK for damage tracking since a little extra coverage is
1013 * not a problem.
1014 */
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +02001015WL_EXPORT pixman_box32_t
1016weston_surface_to_buffer_rect(struct weston_surface *surface,
1017 pixman_box32_t rect)
1018{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001019 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +01001020 float xf, yf;
1021
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03001022 /* first transform box coordinates if the viewport is set */
1023 viewport_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
Jonny Lamb74130762013-11-26 18:19:46 +01001024 rect.x1 = floorf(xf);
1025 rect.y1 = floorf(yf);
1026
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03001027 viewport_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
Derek Foremane2e15ac2015-12-01 13:00:43 -06001028 rect.x2 = ceilf(xf);
1029 rect.y2 = ceilf(yf);
Jonny Lamb74130762013-11-26 18:19:46 +01001030
Jason Ekstrandd0cebc32014-04-21 20:56:46 -05001031 return weston_transformed_rect(surface->width_from_buffer,
1032 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001033 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +02001034 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +02001035}
1036
Pekka Paalanene54e31c2015-03-04 14:23:28 +02001037/** Transform a region from surface coordinates to buffer coordinates
1038 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03001039 * \param surface The surface to fetch wp_viewport and buffer transformation
Pekka Paalanene54e31c2015-03-04 14:23:28 +02001040 * from.
Marius Vlada2dace22019-06-12 16:05:44 +03001041 * \param[in] surface_region The region in surface coordinates.
1042 * \param[out] buffer_region The region converted to buffer coordinates.
Pekka Paalanene54e31c2015-03-04 14:23:28 +02001043 *
1044 * Buffer_region must be init'd, but will be completely overwritten.
1045 *
1046 * Viewport and buffer transformations can only do translation, scaling,
1047 * and rotations in 90-degree steps. Therefore the only loss in the
Derek Foreman99739672015-12-03 16:38:11 -06001048 * conversion is from the coordinate rounding that takes place in
1049 * \ref weston_surface_to_buffer_rect.
Marius Vlada2dace22019-06-12 16:05:44 +03001050 *
Pekka Paalanene54e31c2015-03-04 14:23:28 +02001051 */
1052WL_EXPORT void
1053weston_surface_to_buffer_region(struct weston_surface *surface,
1054 pixman_region32_t *surface_region,
1055 pixman_region32_t *buffer_region)
1056{
1057 pixman_box32_t *src_rects, *dest_rects;
1058 int nrects, i;
1059
1060 src_rects = pixman_region32_rectangles(surface_region, &nrects);
1061 dest_rects = malloc(nrects * sizeof(*dest_rects));
1062 if (!dest_rects)
1063 return;
1064
1065 for (i = 0; i < nrects; i++) {
1066 dest_rects[i] = weston_surface_to_buffer_rect(surface,
1067 src_rects[i]);
1068 }
1069
1070 pixman_region32_fini(buffer_region);
1071 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
1072 free(dest_rects);
1073}
1074
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +02001075WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001076weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001077 struct weston_plane *plane)
1078{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001079 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001080 return;
1081
Jason Ekstranda7af7042013-10-12 22:38:11 -05001082 weston_view_damage_below(view);
1083 view->plane = plane;
1084 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001085}
1086
Pekka Paalanen51723d52015-02-17 13:10:01 +02001087/** Inflict damage on the plane where the view is visible.
1088 *
1089 * \param view The view that causes the damage.
1090 *
1091 * If the view is currently on a plane (including the primary plane),
1092 * take the view's boundingbox, subtract all the opaque views that cover it,
1093 * and add the remaining region as damage to the plane. This corresponds
1094 * to the damage inflicted to the plane if this view disappeared.
1095 *
1096 * A repaint is scheduled for this view.
1097 *
1098 * The region of all opaque views covering this view is stored in
1099 * weston_view::clip and updated by view_accumulate_damage() during
1100 * weston_output_repaint(). Specifically, that region matches the
1101 * scenegraph as it was last painted.
1102 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001103WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001104weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001105{
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001106 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001107
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001108 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001109 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001110 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +08001111 if (view->plane)
1112 pixman_region32_union(&view->plane->damage,
1113 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001114 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -08001115 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001116}
1117
Pekka Paalanen01e00682017-03-24 16:21:06 +02001118/** Send wl_surface.enter/leave events
1119 *
1120 * \param surface The surface.
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03001121 * \param head A head of the entered/left output.
Pekka Paalanen01e00682017-03-24 16:21:06 +02001122 * \param enter True if entered.
Marius Vlada2dace22019-06-12 16:05:44 +03001123 * \param leave True if left.
Pekka Paalanen01e00682017-03-24 16:21:06 +02001124 *
1125 * Send the enter/leave events for all protocol objects bound to the given
1126 * output by the client owning the surface.
1127 */
1128static void
1129weston_surface_send_enter_leave(struct weston_surface *surface,
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03001130 struct weston_head *head,
Pekka Paalanen01e00682017-03-24 16:21:06 +02001131 bool enter,
1132 bool leave)
1133{
1134 struct wl_resource *wloutput;
1135 struct wl_client *client;
1136
1137 assert(enter != leave);
1138
1139 client = wl_resource_get_client(surface->resource);
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03001140 wl_resource_for_each(wloutput, &head->resource_list) {
Pekka Paalanen01e00682017-03-24 16:21:06 +02001141 if (wl_resource_get_client(wloutput) != client)
1142 continue;
1143
1144 if (enter)
1145 wl_surface_send_enter(surface->resource, wloutput);
1146 if (leave)
1147 wl_surface_send_leave(surface->resource, wloutput);
1148 }
1149}
1150
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301151static void
1152weston_surface_compute_protection(struct protected_surface *psurface)
1153{
1154 enum weston_hdcp_protection min_protection;
1155 bool min_protection_valid = false;
1156 struct weston_surface *surface = psurface->surface;
1157 struct weston_output *output;
1158
1159 wl_list_for_each(output, &surface->compositor->output_list, link)
1160 if (surface->output_mask & (1u << output->id)) {
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05301161 /*
1162 * If the content-protection is enabled with protection
1163 * mode as RELAXED for a surface, and if
1164 * content-recording features like: screen-shooter,
1165 * recorder, screen-sharing, etc are on, then notify the
1166 * client, that the protection is disabled.
1167 *
1168 * Note: If the protection mode is ENFORCED then there
1169 * is no need to bother the client as the renderer takes
1170 * care of censoring the visibility of the protected
1171 * content.
1172 */
1173
1174 if (output->disable_planes > 0 &&
1175 surface->protection_mode == WESTON_SURFACE_PROTECTION_MODE_RELAXED) {
1176 min_protection = WESTON_HDCP_DISABLE;
1177 min_protection_valid = true;
1178 break;
1179 }
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301180 if (!min_protection_valid) {
1181 min_protection = output->current_protection;
1182 min_protection_valid = true;
1183 }
1184 if (output->current_protection < min_protection)
1185 min_protection = output->current_protection;
1186 }
1187 if (!min_protection_valid)
1188 min_protection = WESTON_HDCP_DISABLE;
1189
1190 surface->current_protection = min_protection;
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05301191
1192 weston_protected_surface_send_event(psurface, surface->current_protection);
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301193}
1194
1195static void
1196notify_surface_protection_change(void *data)
1197{
1198 struct weston_compositor *compositor = data;
1199 struct content_protection *cp;
1200 struct protected_surface *psurface;
1201
1202 cp = compositor->content_protection;
1203 cp->surface_protection_update = NULL;
1204
1205 /* Notify the clients, whose surfaces are changed */
1206 wl_list_for_each(psurface, &cp->protected_list, link)
1207 if (psurface && psurface->surface)
1208 weston_surface_compute_protection(psurface);
1209}
1210
Bryce Harrington3f650b82015-12-23 11:01:58 -08001211/**
Ankit Nautiyalfaa5ab42019-07-08 13:23:24 +05301212 * \param compositor weston_compositor
1213 *
1214 * Schedule an idle task to notify surface about the update in protection,
1215 * if not already scheduled.
1216 */
1217static void
1218weston_schedule_surface_protection_update(struct weston_compositor *compositor)
1219{
1220 struct content_protection *cp = compositor->content_protection;
1221 struct wl_event_loop *loop;
1222
1223 if (!cp || cp->surface_protection_update)
1224 return;
1225 loop = wl_display_get_event_loop(compositor->wl_display);
1226 cp->surface_protection_update = wl_event_loop_add_idle(loop,
1227 notify_surface_protection_change,
1228 compositor);
1229}
1230
1231/**
Bryce Harrington3f650b82015-12-23 11:01:58 -08001232 * \param es The surface
1233 * \param mask The new set of outputs for the surface
1234 *
1235 * Sets the surface's set of outputs to the ones specified by
1236 * the new output mask provided. Identifies the outputs that
1237 * have changed, the posts enter and leave events for these
1238 * outputs as appropriate.
1239 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001240static void
1241weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
1242{
1243 uint32_t different = es->output_mask ^ mask;
1244 uint32_t entered = mask & different;
1245 uint32_t left = es->output_mask & different;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001246 uint32_t output_bit;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001247 struct weston_output *output;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001248 struct weston_head *head;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001249
1250 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001251 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001252 return;
1253 if (different == 0)
1254 return;
1255
1256 wl_list_for_each(output, &es->compositor->output_list, link) {
Pekka Paalanen01e00682017-03-24 16:21:06 +02001257 output_bit = 1u << output->id;
1258 if (!(output_bit & different))
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001259 continue;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001260
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001261 wl_list_for_each(head, &output->head_list, output_link) {
1262 weston_surface_send_enter_leave(es, head,
1263 output_bit & entered,
1264 output_bit & left);
1265 }
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001266 }
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301267 /*
1268 * Change in surfaces' output mask might trigger a change in its
1269 * protection.
1270 */
Ankit Nautiyalfaa5ab42019-07-08 13:23:24 +05301271 weston_schedule_surface_protection_update(es->compositor);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001272}
1273
Semi Malinene7a52fb2018-04-26 11:08:10 +02001274static void
1275notify_view_output_destroy(struct wl_listener *listener, void *data)
1276{
1277 struct weston_view *view =
1278 container_of(listener,
1279 struct weston_view, output_destroy_listener);
1280
1281 view->output = NULL;
1282 view->output_destroy_listener.notify = NULL;
1283}
1284
1285/** Set the primary output of the view
1286 *
1287 * \param view The view whose primary output to set
1288 * \param output The new primary output for the view
1289 *
1290 * Set \a output to be the primary output of the \a view.
1291 *
1292 * Notice that the assignment may be temporary; the primary output could be
1293 * automatically changed. Hence, one cannot rely on the value persisting.
1294 *
1295 * Passing NULL as /a output will set the primary output to NULL.
1296 */
1297WL_EXPORT void
1298weston_view_set_output(struct weston_view *view, struct weston_output *output)
1299{
1300 if (view->output_destroy_listener.notify) {
1301 wl_list_remove(&view->output_destroy_listener.link);
1302 view->output_destroy_listener.notify = NULL;
1303 }
1304 view->output = output;
1305 if (output) {
1306 view->output_destroy_listener.notify =
1307 notify_view_output_destroy;
1308 wl_signal_add(&output->destroy_signal,
1309 &view->output_destroy_listener);
1310 }
1311}
1312
Bryce Harrington3f650b82015-12-23 11:01:58 -08001313/** Recalculate which output(s) the surface has views displayed on
1314 *
1315 * \param es The surface to remap to outputs
1316 *
1317 * Finds the output that is showing the largest amount of one
1318 * of the surface's various views. This output becomes the
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001319 * surface's primary output for vsync and frame callback purposes.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001320 *
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001321 * Also notes all outputs of all of the surface's views
Bryce Harrington3f650b82015-12-23 11:01:58 -08001322 * in the output_mask for the surface.
1323 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001324static void
1325weston_surface_assign_output(struct weston_surface *es)
1326{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001327 struct weston_output *new_output;
1328 struct weston_view *view;
1329 pixman_region32_t region;
1330 uint32_t max, area, mask;
1331 pixman_box32_t *e;
1332
1333 new_output = NULL;
1334 max = 0;
1335 mask = 0;
1336 pixman_region32_init(&region);
1337 wl_list_for_each(view, &es->views, surface_link) {
1338 if (!view->output)
1339 continue;
1340
1341 pixman_region32_intersect(&region, &view->transform.boundingbox,
1342 &view->output->region);
1343
1344 e = pixman_region32_extents(&region);
1345 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1346
1347 mask |= view->output_mask;
1348
1349 if (area >= max) {
1350 new_output = view->output;
1351 max = area;
1352 }
1353 }
1354 pixman_region32_fini(&region);
1355
1356 es->output = new_output;
1357 weston_surface_update_output_mask(es, mask);
1358}
1359
Bryce Harrington3f650b82015-12-23 11:01:58 -08001360/** Recalculate which output(s) the view is displayed on
1361 *
1362 * \param ev The view to remap to outputs
1363 *
1364 * Identifies the set of outputs that the view is visible on,
1365 * noting them into the output_mask. The output that the view
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001366 * is most visible on is set as the view's primary output.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001367 *
1368 * Also does the same for the view's surface. See
1369 * weston_surface_assign_output().
1370 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001371static void
1372weston_view_assign_output(struct weston_view *ev)
1373{
1374 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001375 struct weston_output *output, *new_output;
1376 pixman_region32_t region;
1377 uint32_t max, area, mask;
1378 pixman_box32_t *e;
1379
1380 new_output = NULL;
1381 max = 0;
1382 mask = 0;
1383 pixman_region32_init(&region);
1384 wl_list_for_each(output, &ec->output_list, link) {
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03001385 if (output->destroying)
1386 continue;
1387
Jason Ekstranda7af7042013-10-12 22:38:11 -05001388 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001389 &output->region);
1390
1391 e = pixman_region32_extents(&region);
1392 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1393
1394 if (area > 0)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001395 mask |= 1u << output->id;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001396
1397 if (area >= max) {
1398 new_output = output;
1399 max = area;
1400 }
1401 }
1402 pixman_region32_fini(&region);
1403
Semi Malinene7a52fb2018-04-26 11:08:10 +02001404 weston_view_set_output(ev, new_output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001405 ev->output_mask = mask;
1406
1407 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001408}
1409
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001410static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001411weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1412 int from_x, int from_y, int *to_x, int *to_y)
1413{
1414 float x, y;
1415
1416 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1417 weston_view_from_global_float(to, x, y, &x, &y);
1418
1419 *to_x = round(x);
1420 *to_y = round(y);
1421}
1422
1423static void
1424weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1425{
1426 pixman_box32_t *a;
1427 pixman_box32_t b;
1428
1429 a = pixman_region32_extents(&from->geometry.scissor);
1430
1431 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1432 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1433
1434 pixman_region32_fini(&to->geometry.scissor);
1435 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1436}
1437
1438static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001439view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001440 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001441{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001442 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1443 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001444 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001445 { inbox->x1, inbox->y1 },
1446 { inbox->x1, inbox->y2 },
1447 { inbox->x2, inbox->y1 },
1448 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001449 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001450 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001451 int i;
1452
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001453 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001454 /* avoid rounding empty bbox to 1x1 */
1455 pixman_region32_init(bbox);
1456 return;
1457 }
1458
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001459 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001460 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001461 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001462 if (x < min_x)
1463 min_x = x;
1464 if (x > max_x)
1465 max_x = x;
1466 if (y < min_y)
1467 min_y = y;
1468 if (y > max_y)
1469 max_y = y;
1470 }
1471
Pekka Paalanen219b9822012-02-08 15:38:37 +02001472 int_x = floorf(min_x);
1473 int_y = floorf(min_y);
1474 pixman_region32_init_rect(bbox, int_x, int_y,
1475 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001476}
1477
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001478static void
Michael Olbrichcb04f982020-06-08 10:22:32 +02001479weston_view_update_transform_scissor(struct weston_view *view,
1480 pixman_region32_t *region)
1481{
1482 struct weston_view *parent = view->geometry.parent;
1483
1484 if (parent) {
1485 if (parent->geometry.scissor_enabled) {
1486 view->geometry.scissor_enabled = true;
1487 weston_view_transfer_scissor(parent, view);
1488 } else {
1489 view->geometry.scissor_enabled = false;
1490 }
1491 }
1492
1493 if (view->geometry.scissor_enabled)
1494 pixman_region32_intersect(region, region,
1495 &view->geometry.scissor);
1496}
1497static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001498weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001499{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001500 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001501
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001502 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001503 view->geometry.x = roundf(view->geometry.x);
1504 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001505
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001506 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001507 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1508 view->transform.position.matrix.d[12] = view->geometry.x;
1509 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001510
Jason Ekstranda7af7042013-10-12 22:38:11 -05001511 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001512
Jason Ekstranda7af7042013-10-12 22:38:11 -05001513 view->transform.inverse = view->transform.position.matrix;
1514 view->transform.inverse.d[12] = -view->geometry.x;
1515 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001516
Jason Ekstranda7af7042013-10-12 22:38:11 -05001517 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001518 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001519 view->surface->width,
1520 view->surface->height);
Michael Olbrichcb04f982020-06-08 10:22:32 +02001521
1522 weston_view_update_transform_scissor(view, &view->transform.boundingbox);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001523
1524 pixman_region32_translate(&view->transform.boundingbox,
1525 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001526
Jason Ekstranda7af7042013-10-12 22:38:11 -05001527 if (view->alpha == 1.0) {
1528 pixman_region32_copy(&view->transform.opaque,
1529 &view->surface->opaque);
Michael Olbrich0e4f0972020-08-19 11:31:03 +02001530 pixman_region32_intersect(&view->transform.opaque,
1531 &view->transform.opaque,
1532 &view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001533 pixman_region32_translate(&view->transform.opaque,
1534 view->geometry.x,
1535 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001536 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001537}
1538
1539static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001540weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001541{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001542 struct weston_view *parent = view->geometry.parent;
1543 struct weston_matrix *matrix = &view->transform.matrix;
1544 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001545 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001546 pixman_region32_t surfregion;
1547 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001548
Jason Ekstranda7af7042013-10-12 22:38:11 -05001549 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001550
1551 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001552 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1553 view->transform.position.matrix.d[12] = view->geometry.x;
1554 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001555
1556 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001557 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001558 weston_matrix_multiply(matrix, &tform->matrix);
1559
Pekka Paalanen483243f2013-03-08 14:56:50 +02001560 if (parent)
1561 weston_matrix_multiply(matrix, &parent->transform.matrix);
1562
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001563 if (weston_matrix_invert(inverse, matrix) < 0) {
1564 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001565 weston_log("error: weston_view %p"
1566 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001567 return -1;
1568 }
1569
Pekka Paalanen380adf52015-02-16 14:39:11 +02001570 pixman_region32_init_rect(&surfregion, 0, 0,
1571 view->surface->width, view->surface->height);
Michael Olbrichcb04f982020-06-08 10:22:32 +02001572
1573 weston_view_update_transform_scissor(view, &surfregion);
1574
Pekka Paalanen380adf52015-02-16 14:39:11 +02001575 surfbox = pixman_region32_extents(&surfregion);
1576
1577 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1578 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001579
Michael Olbrich0e4f0972020-08-19 11:31:03 +02001580 if (view->alpha == 1.0 &&
1581 matrix->type == WESTON_MATRIX_TRANSFORM_TRANSLATE) {
1582 pixman_region32_copy(&view->transform.opaque,
1583 &view->surface->opaque);
1584 pixman_region32_intersect(&view->transform.opaque,
1585 &view->transform.opaque,
1586 &view->geometry.scissor);
1587 pixman_region32_translate(&view->transform.opaque,
1588 matrix->d[12],
1589 matrix->d[13]);
1590 }
1591
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001592 return 0;
1593}
1594
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001595static struct weston_layer *
1596get_view_layer(struct weston_view *view)
1597{
1598 if (view->parent_view)
1599 return get_view_layer(view->parent_view);
1600 return view->layer_link.layer;
1601}
1602
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001603WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001604weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001605{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001606 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001607 struct weston_layer *layer;
1608 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001609
Jason Ekstranda7af7042013-10-12 22:38:11 -05001610 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001611 return;
1612
Pekka Paalanen483243f2013-03-08 14:56:50 +02001613 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001614 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001615
Jason Ekstranda7af7042013-10-12 22:38:11 -05001616 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001617
Jason Ekstranda7af7042013-10-12 22:38:11 -05001618 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001619
Jason Ekstranda7af7042013-10-12 22:38:11 -05001620 pixman_region32_fini(&view->transform.boundingbox);
1621 pixman_region32_fini(&view->transform.opaque);
1622 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001623
Pekka Paalanencd403622012-01-25 13:37:39 +02001624 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001625 if (view->geometry.transformation_list.next ==
1626 &view->transform.position.link &&
1627 view->geometry.transformation_list.prev ==
1628 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001629 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001630 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001631 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001632 if (weston_view_update_transform_enable(view) < 0)
1633 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001634 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001635
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001636 layer = get_view_layer(view);
1637 if (layer) {
1638 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001639 pixman_region32_intersect(&view->transform.boundingbox,
1640 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001641 pixman_region32_intersect(&view->transform.opaque,
1642 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001643 pixman_region32_fini(&mask);
1644 }
1645
Jason Ekstranda7af7042013-10-12 22:38:11 -05001646 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001647
Jason Ekstranda7af7042013-10-12 22:38:11 -05001648 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001649
Jason Ekstranda7af7042013-10-12 22:38:11 -05001650 wl_signal_emit(&view->surface->compositor->transform_signal,
1651 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001652}
1653
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001654WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001655weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001656{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001657 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001658
1659 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001660 * The invariant: if view->geometry.dirty, then all views
1661 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001662 * Corollary: if not parent->geometry.dirty, then all ancestors
1663 * are not dirty.
1664 */
1665
Jason Ekstranda7af7042013-10-12 22:38:11 -05001666 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001667 return;
1668
Jason Ekstranda7af7042013-10-12 22:38:11 -05001669 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001670
Jason Ekstranda7af7042013-10-12 22:38:11 -05001671 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001672 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001673 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001674}
1675
1676WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001677weston_view_to_global_fixed(struct weston_view *view,
1678 wl_fixed_t vx, wl_fixed_t vy,
1679 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001680{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001681 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001682
Jason Ekstranda7af7042013-10-12 22:38:11 -05001683 weston_view_to_global_float(view,
1684 wl_fixed_to_double(vx),
1685 wl_fixed_to_double(vy),
1686 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001687 *x = wl_fixed_from_double(xf);
1688 *y = wl_fixed_from_double(yf);
1689}
1690
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001691WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001692weston_view_from_global_float(struct weston_view *view,
1693 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001694{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001695 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001696 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1697
Jason Ekstranda7af7042013-10-12 22:38:11 -05001698 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001699
1700 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001701 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001702 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001703 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001704 *vx = 0;
1705 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001706 return;
1707 }
1708
Jason Ekstranda7af7042013-10-12 22:38:11 -05001709 *vx = v.f[0] / v.f[3];
1710 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001711 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001712 *vx = x - view->geometry.x;
1713 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001714 }
1715}
1716
1717WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001718weston_view_from_global_fixed(struct weston_view *view,
1719 wl_fixed_t x, wl_fixed_t y,
1720 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001721{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001722 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001723
Jason Ekstranda7af7042013-10-12 22:38:11 -05001724 weston_view_from_global_float(view,
1725 wl_fixed_to_double(x),
1726 wl_fixed_to_double(y),
1727 &vxf, &vyf);
1728 *vx = wl_fixed_from_double(vxf);
1729 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001730}
1731
1732WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001733weston_view_from_global(struct weston_view *view,
1734 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001735{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001736 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001737
Jason Ekstranda7af7042013-10-12 22:38:11 -05001738 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1739 *vx = floorf(vxf);
1740 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001741}
1742
Bryce Harrington3f650b82015-12-23 11:01:58 -08001743/**
1744 * \param surface The surface to be repainted
1745 *
1746 * Marks the output(s) that the surface is shown on as needing to be
1747 * repainted. See weston_output_schedule_repaint().
1748 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001749WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001750weston_surface_schedule_repaint(struct weston_surface *surface)
1751{
1752 struct weston_output *output;
1753
1754 wl_list_for_each(output, &surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001755 if (surface->output_mask & (1u << output->id))
Kristian Høgsberg98238702012-08-03 16:29:12 -04001756 weston_output_schedule_repaint(output);
1757}
1758
Bryce Harrington3f650b82015-12-23 11:01:58 -08001759/**
1760 * \param view The view to be repainted
1761 *
1762 * Marks the output(s) that the view is shown on as needing to be
1763 * repainted. See weston_output_schedule_repaint().
1764 */
Kristian Høgsberg98238702012-08-03 16:29:12 -04001765WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001766weston_view_schedule_repaint(struct weston_view *view)
1767{
1768 struct weston_output *output;
1769
1770 wl_list_for_each(output, &view->surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001771 if (view->output_mask & (1u << output->id))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001772 weston_output_schedule_repaint(output);
1773}
1774
Pekka Paalanene508ce62015-02-19 13:59:55 +02001775/**
1776 * XXX: This function does it the wrong way.
1777 * surface->damage is the damage from the client, and causes
1778 * surface_flush_damage() to copy pixels. No window management action can
1779 * cause damage to the client-provided content, warranting re-upload!
1780 *
1781 * Instead of surface->damage, this function should record the damage
1782 * with all the views for this surface to avoid extraneous texture
1783 * uploads.
1784 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001785WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001786weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001787{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001788 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001789 0, 0, surface->width,
1790 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001791
Kristian Høgsberg98238702012-08-03 16:29:12 -04001792 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001793}
1794
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001795WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001796weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001797{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001798 if (view->geometry.x == x && view->geometry.y == y)
1799 return;
1800
Jason Ekstranda7af7042013-10-12 22:38:11 -05001801 view->geometry.x = x;
1802 view->geometry.y = y;
1803 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001804}
1805
Pekka Paalanen483243f2013-03-08 14:56:50 +02001806static void
1807transform_parent_handle_parent_destroy(struct wl_listener *listener,
1808 void *data)
1809{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001810 struct weston_view *view =
1811 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001812 geometry.parent_destroy_listener);
1813
Jason Ekstranda7af7042013-10-12 22:38:11 -05001814 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001815}
1816
1817WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001818weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001819 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001820{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001821 if (view->geometry.parent) {
1822 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1823 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001824
1825 if (!parent)
1826 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001827 }
1828
Jason Ekstranda7af7042013-10-12 22:38:11 -05001829 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001830
Jason Ekstranda7af7042013-10-12 22:38:11 -05001831 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001832 transform_parent_handle_parent_destroy;
1833 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001834 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001835 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001836 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001837 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001838 }
1839
Jason Ekstranda7af7042013-10-12 22:38:11 -05001840 weston_view_geometry_dirty(view);
1841}
1842
Pekka Paalanen380adf52015-02-16 14:39:11 +02001843/** Set a clip mask rectangle on a view
1844 *
1845 * \param view The view to set the clip mask on.
1846 * \param x Top-left corner X coordinate of the clip rectangle.
1847 * \param y Top-left corner Y coordinate of the clip rectangle.
1848 * \param width Width of the clip rectangle, non-negative.
1849 * \param height Height of the clip rectangle, non-negative.
1850 *
1851 * A shell may set a clip mask rectangle on a view. Everything outside
1852 * the rectangle is cut away for input and output purposes: it is
1853 * not drawn and cannot be hit by hit-test based input like pointer
1854 * motion or touch-downs. Everything inside the rectangle will behave
1855 * normally. Clients are unaware of clipping.
1856 *
Yong Bakos4c72e292016-04-28 11:59:10 -05001857 * The rectangle is set in surface-local coordinates. Setting a clip
Pekka Paalanen380adf52015-02-16 14:39:11 +02001858 * mask rectangle does not affect the view position, the view is positioned
1859 * as it would be without a clip. The clip also does not change
1860 * weston_surface::width,height.
1861 *
1862 * The clip mask rectangle is part of transformation inheritance
1863 * (weston_view_set_transform_parent()). A clip set in the root of the
1864 * transformation inheritance tree will affect all views in the tree.
1865 * A clip can be set only on the root view. Attempting to set a clip
1866 * on view that has a transformation parent will fail. Assigning a parent
1867 * to a view that has a clip set will cause the clip to be forgotten.
1868 *
1869 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1870 * on the additional transformations in the child views. These transformations
1871 * may not rotate the coordinate axes, i.e., only translation and scaling
1872 * are allowed. Violating this restriction causes the clipping to malfunction.
1873 * Furthermore, using scaling may cause rounding errors in child clipping.
1874 *
1875 * The clip mask rectangle is not automatically adjusted based on
1876 * wl_surface.attach dx and dy arguments.
1877 *
1878 * A clip mask rectangle can be set only if the compositor capability
1879 * WESTON_CAP_VIEW_CLIP_MASK is present.
1880 *
1881 * This function sets the clip mask rectangle and schedules a repaint for
1882 * the view.
1883 */
1884WL_EXPORT void
1885weston_view_set_mask(struct weston_view *view,
1886 int x, int y, int width, int height)
1887{
1888 struct weston_compositor *compositor = view->surface->compositor;
1889
1890 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1891 weston_log("%s not allowed without capability!\n", __func__);
1892 return;
1893 }
1894
1895 if (view->geometry.parent) {
1896 weston_log("view %p has a parent, clip forbidden!\n", view);
1897 return;
1898 }
1899
1900 if (width < 0 || height < 0) {
1901 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1902 x, y, width, height);
1903 return;
1904 }
1905
1906 pixman_region32_fini(&view->geometry.scissor);
1907 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1908 view->geometry.scissor_enabled = true;
1909 weston_view_geometry_dirty(view);
1910 weston_view_schedule_repaint(view);
1911}
1912
1913/** Remove the clip mask from a view
1914 *
1915 * \param view The view to remove the clip mask from.
1916 *
1917 * Removed the clip mask rectangle and schedules a repaint.
1918 *
1919 * \sa weston_view_set_mask
1920 */
1921WL_EXPORT void
1922weston_view_set_mask_infinite(struct weston_view *view)
1923{
1924 view->geometry.scissor_enabled = false;
1925 weston_view_geometry_dirty(view);
1926 weston_view_schedule_repaint(view);
1927}
1928
Armin Krezović0da12b82016-06-30 06:04:33 +02001929/* Check if view should be displayed
1930 *
1931 * The indicator is set manually when assigning
1932 * a view to a surface.
1933 *
1934 * This needs reworking. See the thread starting at:
1935 *
1936 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1937 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001938WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001939weston_view_is_mapped(struct weston_view *view)
1940{
Armin Krezović0da12b82016-06-30 06:04:33 +02001941 return view->is_mapped;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001942}
1943
Philipp Zabel70decd52018-09-03 20:11:15 +02001944/* Check if view is opaque in specified region
1945 *
1946 * \param view The view to check for opacity.
1947 * \param region The region to check for opacity, in view coordinates.
1948 *
1949 * Returns true if the view is opaque in the specified region, because view
1950 * alpha is 1.0 and either the opaque region set by the client contains the
1951 * specified region, or the buffer pixel format or solid color is opaque.
1952 */
1953WL_EXPORT bool
1954weston_view_is_opaque(struct weston_view *ev, pixman_region32_t *region)
1955{
1956 pixman_region32_t r;
1957 bool ret = false;
1958
1959 if (ev->alpha < 1.0)
1960 return false;
1961
1962 if (ev->surface->is_opaque)
1963 return true;
1964
Marius Vlad67b382c2020-08-21 13:57:54 +03001965 if (ev->transform.dirty)
Philipp Zabel70decd52018-09-03 20:11:15 +02001966 return false;
Philipp Zabel70decd52018-09-03 20:11:15 +02001967
1968 pixman_region32_init(&r);
1969 pixman_region32_subtract(&r, region, &ev->transform.opaque);
1970
1971 if (!pixman_region32_not_empty(&r))
1972 ret = true;
1973
1974 pixman_region32_fini(&r);
1975
1976 return ret;
1977}
1978
Marius Vlad5f6bee42019-09-11 16:41:04 +03001979/** Check if the view has a valid buffer available
1980 *
1981 * @param ev The view to check if it has a valid buffer.
1982 *
1983 * Returns true if the view has a valid buffer or false otherwise.
1984 */
1985WL_EXPORT bool
1986weston_view_has_valid_buffer(struct weston_view *ev)
1987{
1988 return ev->surface->buffer_ref.buffer != NULL;
1989}
1990
Marius Vlad47e3d1e2019-09-11 16:53:08 +03001991/** Check if the view matches the entire output
1992 *
1993 * @param ev The view to check.
1994 * @param output The output to check against.
1995 *
1996 * Returns true if the view does indeed matches the entire output.
1997 */
1998WL_EXPORT bool
1999weston_view_matches_output_entirely(struct weston_view *ev,
2000 struct weston_output *output)
2001{
2002 pixman_box32_t *extents =
2003 pixman_region32_extents(&ev->transform.boundingbox);
2004
2005 if (extents->x1 != output->x ||
2006 extents->y1 != output->y ||
2007 extents->x2 != output->x + output->width ||
2008 extents->y2 != output->y + output->height)
2009 return false;
2010
2011 return true;
2012}
2013
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002014/** Find paint node for the given view and output
2015 */
2016WL_EXPORT struct weston_paint_node *
2017weston_view_find_paint_node(struct weston_view *view,
2018 struct weston_output *output)
2019{
2020 struct weston_paint_node *pnode;
2021
2022 wl_list_for_each(pnode, &view->paint_node_list, view_link) {
2023 assert(pnode->surface == view->surface);
2024 if (pnode->output == output)
2025 return pnode;
2026 }
2027
2028 return NULL;
2029}
2030
Armin Krezović0da12b82016-06-30 06:04:33 +02002031/* Check if a surface has a view assigned to it
2032 *
2033 * The indicator is set manually when mapping
2034 * a surface and creating a view for it.
2035 *
2036 * This needs to go. See the thread starting at:
2037 *
2038 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
2039 *
2040 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05002041WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03002042weston_surface_is_mapped(struct weston_surface *surface)
2043{
Armin Krezović0da12b82016-06-30 06:04:33 +02002044 return surface->is_mapped;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03002045}
2046
Pekka Paalanenda75ee12013-11-26 18:19:43 +01002047static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06002048surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002049{
2050 struct weston_view *view;
2051
2052 if (surface->width == width && surface->height == height)
2053 return;
2054
2055 surface->width = width;
2056 surface->height = height;
2057
2058 wl_list_for_each(view, &surface->views, surface_link)
2059 weston_view_geometry_dirty(view);
2060}
2061
Jason Ekstrand5c11a332013-12-04 20:32:03 -06002062WL_EXPORT void
2063weston_surface_set_size(struct weston_surface *surface,
2064 int32_t width, int32_t height)
2065{
2066 assert(!surface->resource);
2067 surface_set_size(surface, width, height);
2068}
2069
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02002070static int
2071fixed_round_up_to_int(wl_fixed_t f)
2072{
2073 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
2074}
2075
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002076static void
Pekka Paalanen59987fa2016-04-26 15:50:59 +03002077convert_size_by_transform_scale(int32_t *width_out, int32_t *height_out,
2078 int32_t width, int32_t height,
2079 uint32_t transform,
2080 int32_t scale)
2081{
2082 assert(scale > 0);
2083
2084 switch (transform) {
2085 case WL_OUTPUT_TRANSFORM_NORMAL:
2086 case WL_OUTPUT_TRANSFORM_180:
2087 case WL_OUTPUT_TRANSFORM_FLIPPED:
2088 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2089 *width_out = width / scale;
2090 *height_out = height / scale;
2091 break;
2092 case WL_OUTPUT_TRANSFORM_90:
2093 case WL_OUTPUT_TRANSFORM_270:
2094 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2095 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2096 *width_out = height / scale;
2097 *height_out = width / scale;
2098 break;
2099 default:
2100 assert(0 && "invalid transform");
2101 }
2102}
2103
2104static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002105weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002106{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002107 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01002108
2109 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02002110 surface->width_from_buffer = 0;
2111 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01002112 return;
2113 }
2114
Pekka Paalanen59987fa2016-04-26 15:50:59 +03002115 convert_size_by_transform_scale(&surface->width_from_buffer,
2116 &surface->height_from_buffer,
2117 surface->buffer_ref.buffer->width,
2118 surface->buffer_ref.buffer->height,
2119 vp->buffer.transform,
2120 vp->buffer.scale);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002121}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02002122
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002123static void
2124weston_surface_update_size(struct weston_surface *surface)
2125{
2126 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
2127 int32_t width, height;
2128
2129 width = surface->width_from_buffer;
2130 height = surface->height_from_buffer;
2131
2132 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02002133 surface_set_size(surface,
2134 vp->surface.width, vp->surface.height);
2135 return;
2136 }
2137
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002138 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03002139 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
2140 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
2141
2142 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02002143 return;
2144 }
2145
Jason Ekstrand5c11a332013-12-04 20:32:03 -06002146 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002147}
2148
Marius Vlad9fdda7f2019-06-11 16:08:55 +03002149/** weston_compositor_get_time
2150 * \ingroup compositor
2151 */
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02002152WL_EXPORT void
2153weston_compositor_get_time(struct timespec *time)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05002154{
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02002155 clock_gettime(CLOCK_REALTIME, time);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05002156}
2157
Marius Vlad9fdda7f2019-06-11 16:08:55 +03002158/** weston_compositor_pick_view
2159 * \ingroup compositor
2160 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002161WL_EXPORT struct weston_view *
2162weston_compositor_pick_view(struct weston_compositor *compositor,
2163 wl_fixed_t x, wl_fixed_t y,
2164 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02002165{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002166 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02002167 wl_fixed_t view_x, view_y;
2168 int view_ix, view_iy;
2169 int ix = wl_fixed_to_int(x);
2170 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02002171
Pekka Paalanen6551c092021-05-03 16:09:45 +03002172 /* Can't use paint node list: occlusion by input regions, not opaque. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002173 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02002174 if (!pixman_region32_contains_point(
2175 &view->transform.boundingbox, ix, iy, NULL))
2176 continue;
2177
2178 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
2179 view_ix = wl_fixed_to_int(view_x);
2180 view_iy = wl_fixed_to_int(view_y);
2181
2182 if (!pixman_region32_contains_point(&view->surface->input,
2183 view_ix, view_iy, NULL))
2184 continue;
2185
Pekka Paalanen380adf52015-02-16 14:39:11 +02002186 if (view->geometry.scissor_enabled &&
2187 !pixman_region32_contains_point(&view->geometry.scissor,
2188 view_ix, view_iy, NULL))
2189 continue;
2190
Pekka Paalanenfc22a522015-02-18 15:08:29 +02002191 *vx = view_x;
2192 *vy = view_y;
2193 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02002194 }
2195
Derek Foremanf9318d12015-05-11 15:40:11 -05002196 *vx = wl_fixed_from_int(-1000000);
2197 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02002198 return NULL;
2199}
2200
2201static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002202weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002203{
Daniel Stone37816df2012-05-16 18:45:18 +01002204 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002205
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07002206 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002207 return;
2208
Daniel Stone37816df2012-05-16 18:45:18 +01002209 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04002210 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002211}
2212
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002213WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002214weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05002215{
Daniel Stone4dab5db2012-05-30 16:31:53 +01002216 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002217
Jason Ekstranda7af7042013-10-12 22:38:11 -05002218 if (!weston_view_is_mapped(view))
2219 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002220
Jason Ekstranda7af7042013-10-12 22:38:11 -05002221 weston_view_damage_below(view);
Semi Malinene7a52fb2018-04-26 11:08:10 +02002222 weston_view_set_output(view, NULL);
Xiong Zhang97116532013-10-23 13:58:31 +08002223 view->plane = NULL;
Armin Krezovićf8486c32016-06-30 06:04:28 +02002224 view->is_mapped = false;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002225 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002226 wl_list_remove(&view->link);
2227 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002228 view->output_mask = 0;
2229 weston_surface_assign_output(view->surface);
2230
2231 if (weston_surface_is_mapped(view->surface))
2232 return;
2233
2234 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002235 struct weston_touch *touch = weston_seat_get_touch(seat);
2236 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2237 struct weston_keyboard *keyboard =
2238 weston_seat_get_keyboard(seat);
2239
2240 if (keyboard && keyboard->focus == view->surface)
2241 weston_keyboard_set_focus(keyboard, NULL);
2242 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05002243 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05002244 if (touch && touch->focus == view)
2245 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01002246 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002247}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002248
Jason Ekstranda7af7042013-10-12 22:38:11 -05002249WL_EXPORT void
2250weston_surface_unmap(struct weston_surface *surface)
2251{
2252 struct weston_view *view;
2253
Armin Krezovićf8486c32016-06-30 06:04:28 +02002254 surface->is_mapped = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002255 wl_list_for_each(view, &surface->views, surface_link)
2256 weston_view_unmap(view);
2257 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05002258}
2259
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002260static void
2261weston_surface_reset_pending_buffer(struct weston_surface *surface)
2262{
Jason Ekstrand7b982072014-05-20 14:33:03 -05002263 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002264 surface->pending.sx = 0;
2265 surface->pending.sy = 0;
2266 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002267 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002268}
2269
Jason Ekstranda7af7042013-10-12 22:38:11 -05002270WL_EXPORT void
2271weston_view_destroy(struct weston_view *view)
2272{
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002273 struct weston_paint_node *pnode, *pntmp;
2274
Jason Ekstranda7af7042013-10-12 22:38:11 -05002275 wl_signal_emit(&view->destroy_signal, view);
2276
2277 assert(wl_list_empty(&view->geometry.child_list));
2278
2279 if (weston_view_is_mapped(view)) {
2280 weston_view_unmap(view);
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002281 weston_compositor_build_view_list(view->surface->compositor,
2282 NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002283 }
2284
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002285 wl_list_for_each_safe(pnode, pntmp, &view->paint_node_list, view_link)
2286 weston_paint_node_destroy(pnode);
2287
Jason Ekstranda7af7042013-10-12 22:38:11 -05002288 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002289 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002290
2291 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02002292 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002293 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002294 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002295
2296 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen944fae82018-05-22 13:15:58 +03002297 weston_view_set_output(view, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002298
Jason Ekstranda7af7042013-10-12 22:38:11 -05002299 wl_list_remove(&view->surface_link);
2300
2301 free(view);
2302}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002303
2304WL_EXPORT void
2305weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05002306{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002307 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002308 struct weston_view *ev, *nv;
Jonas Ådahld3414f22016-07-22 17:56:31 +08002309 struct weston_pointer_constraint *constraint, *next_constraint;
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002310 struct weston_paint_node *pnode, *pntmp;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04002311
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02002312 if (--surface->ref_count > 0)
2313 return;
2314
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03002315 assert(surface->resource == NULL);
2316
Pekka Paalanenca790762015-04-17 14:23:38 +03002317 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02002318
Pekka Paalanene67858b2013-04-25 13:57:42 +03002319 assert(wl_list_empty(&surface->subsurface_list_pending));
2320 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02002321
Jason Ekstranda7af7042013-10-12 22:38:11 -05002322 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
2323 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002324
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002325 wl_list_for_each_safe(pnode, pntmp,
2326 &surface->paint_node_list, surface_link) {
2327 weston_paint_node_destroy(pnode);
2328 }
2329
Jason Ekstrand7b982072014-05-20 14:33:03 -05002330 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002331
Pekka Paalanende685b82012-12-04 15:58:12 +02002332 weston_buffer_reference(&surface->buffer_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002333 weston_buffer_release_reference(&surface->buffer_release_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04002334
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02002335 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002336 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002337 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02002338
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002339 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002340 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002341
Pekka Paalanen133e4392014-09-23 22:08:46 -04002342 weston_presentation_feedback_discard_list(&surface->feedback_list);
2343
Jonas Ådahld3414f22016-07-22 17:56:31 +08002344 wl_list_for_each_safe(constraint, next_constraint,
2345 &surface->pointer_constraints,
2346 link)
2347 weston_pointer_constraint_destroy(constraint);
2348
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03002349 fd_clear(&surface->acquire_fence_fd);
2350
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04002351 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05002352}
2353
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002354static void
2355destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08002356{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002357 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08002358
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03002359 assert(surface);
2360
Giulio Camuffo0d379742013-11-15 22:06:15 +01002361 /* Set the resource to NULL, since we don't want to leave a
2362 * dangling pointer if the surface was refcounted and survives
2363 * the weston_surface_destroy() call. */
2364 surface->resource = NULL;
Pekka Paalanen4826f872016-04-22 14:14:38 +03002365
2366 if (surface->viewport_resource)
2367 wl_resource_set_user_data(surface->viewport_resource, NULL);
2368
Alexandros Frantzis27d7c392018-10-19 12:14:11 +03002369 if (surface->synchronization_resource) {
2370 wl_resource_set_user_data(surface->synchronization_resource,
2371 NULL);
2372 }
2373
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002374 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08002375}
2376
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002377static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002378weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
2379{
2380 struct weston_buffer *buffer =
2381 container_of(listener, struct weston_buffer, destroy_listener);
2382
2383 wl_signal_emit(&buffer->destroy_signal, buffer);
2384 free(buffer);
2385}
2386
Giulio Camuffoe058cd12013-12-12 14:14:29 +01002387WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002388weston_buffer_from_resource(struct wl_resource *resource)
2389{
2390 struct weston_buffer *buffer;
2391 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002392
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002393 listener = wl_resource_get_destroy_listener(resource,
2394 weston_buffer_destroy_handler);
2395
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002396 if (listener)
2397 return container_of(listener, struct weston_buffer,
2398 destroy_listener);
2399
2400 buffer = zalloc(sizeof *buffer);
2401 if (buffer == NULL)
2402 return NULL;
2403
2404 buffer->resource = resource;
2405 wl_signal_init(&buffer->destroy_signal);
2406 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04002407 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002408 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002409
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002410 return buffer;
2411}
2412
2413static void
Pekka Paalanende685b82012-12-04 15:58:12 +02002414weston_buffer_reference_handle_destroy(struct wl_listener *listener,
2415 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002416{
Pekka Paalanende685b82012-12-04 15:58:12 +02002417 struct weston_buffer_reference *ref =
2418 container_of(listener, struct weston_buffer_reference,
2419 destroy_listener);
2420
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002421 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02002422 ref->buffer = NULL;
2423}
2424
2425WL_EXPORT void
2426weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002427 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002428{
2429 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05002430 ref->buffer->busy_count--;
2431 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002432 assert(wl_resource_get_client(ref->buffer->resource));
Matt Hoosier3052bc72017-09-26 08:09:40 -05002433 wl_buffer_send_release(ref->buffer->resource);
Kristian Høgsberg20347802013-03-04 12:07:46 -05002434 }
Pekka Paalanende685b82012-12-04 15:58:12 +02002435 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002436 }
2437
Pekka Paalanende685b82012-12-04 15:58:12 +02002438 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002439 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002440 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02002441 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02002442 }
2443
Pekka Paalanende685b82012-12-04 15:58:12 +02002444 ref->buffer = buffer;
2445 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
2446}
2447
2448static void
Alexandros Frantzis67629672018-10-19 12:14:11 +03002449weston_buffer_release_reference_handle_destroy(struct wl_listener *listener,
2450 void *data)
2451{
2452 struct weston_buffer_release_reference *ref =
2453 container_of(listener, struct weston_buffer_release_reference,
2454 destroy_listener);
2455
2456 assert((struct wl_resource *)data == ref->buffer_release->resource);
2457 ref->buffer_release = NULL;
2458}
2459
2460static void
2461weston_buffer_release_destroy(struct weston_buffer_release *buffer_release)
2462{
2463 struct wl_resource *resource = buffer_release->resource;
2464 int release_fence_fd = buffer_release->fence_fd;
2465
2466 if (release_fence_fd >= 0) {
2467 zwp_linux_buffer_release_v1_send_fenced_release(
2468 resource, release_fence_fd);
2469 } else {
2470 zwp_linux_buffer_release_v1_send_immediate_release(
2471 resource);
2472 }
2473
2474 wl_resource_destroy(resource);
2475}
2476
2477WL_EXPORT void
2478weston_buffer_release_reference(struct weston_buffer_release_reference *ref,
2479 struct weston_buffer_release *buffer_release)
2480{
2481 if (buffer_release == ref->buffer_release)
2482 return;
2483
2484 if (ref->buffer_release) {
2485 ref->buffer_release->ref_count--;
2486 wl_list_remove(&ref->destroy_listener.link);
2487 if (ref->buffer_release->ref_count == 0)
2488 weston_buffer_release_destroy(ref->buffer_release);
2489 }
2490
2491 if (buffer_release) {
2492 buffer_release->ref_count++;
2493 wl_resource_add_destroy_listener(buffer_release->resource,
2494 &ref->destroy_listener);
2495 }
2496
2497 ref->buffer_release = buffer_release;
2498 ref->destroy_listener.notify =
2499 weston_buffer_release_reference_handle_destroy;
2500}
2501
2502WL_EXPORT void
2503weston_buffer_release_move(struct weston_buffer_release_reference *dest,
2504 struct weston_buffer_release_reference *src)
2505{
2506 weston_buffer_release_reference(dest, src->buffer_release);
2507 weston_buffer_release_reference(src, NULL);
2508}
2509
2510static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002511weston_surface_attach(struct weston_surface *surface,
2512 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002513{
2514 weston_buffer_reference(&surface->buffer_ref, buffer);
2515
Pekka Paalanena6421c42012-12-04 15:58:10 +02002516 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002517 if (weston_surface_is_mapped(surface))
2518 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002519 }
2520
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002521 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02002522
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002523 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002524 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002525}
2526
Marius Vlad9fdda7f2019-06-11 16:08:55 +03002527/** weston_compositor_damage_all
2528 * \ingroup compositor
2529 */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002530WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002531weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002532{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002533 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002534
2535 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002536 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002537}
2538
Marius Vlad55d87362019-06-11 01:15:35 +03002539/**
2540 * \ingroup output
2541 */
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05002542WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002543weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002544{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002545 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002546
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002547 pixman_region32_union(&compositor->primary_plane.damage,
2548 &compositor->primary_plane.damage,
2549 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002550 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002551}
2552
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04002553static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002554surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002555{
Pekka Paalanende685b82012-12-04 15:58:12 +02002556 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002557 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002558 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002559
Marius Vlad2a1b7862019-09-05 13:12:18 +03002560 if (pixman_region32_not_empty(&surface->damage))
Marius Vlad3203ff62019-09-05 14:56:12 +03002561 TL_POINT(surface->compositor, "core_flush_damage", TLP_SURFACE(surface),
Pekka Paalanenb5026542014-11-12 15:09:24 +02002562 TLP_OUTPUT(surface->output), TLP_END);
2563
Jason Ekstrandef540082014-06-26 10:37:36 -07002564 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002565}
2566
2567static void
2568view_accumulate_damage(struct weston_view *view,
2569 pixman_region32_t *opaque)
2570{
2571 pixman_region32_t damage;
2572
2573 pixman_region32_init(&damage);
2574 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002575 pixman_box32_t *extents;
2576
Jason Ekstranda7af7042013-10-12 22:38:11 -05002577 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002578 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002579 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002580 pixman_region32_copy(&damage, &view->surface->damage);
2581 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002582 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002583 }
2584
Pekka Paalanen380adf52015-02-16 14:39:11 +02002585 pixman_region32_intersect(&damage, &damage,
2586 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002587 pixman_region32_subtract(&damage, &damage, opaque);
2588 pixman_region32_union(&view->plane->damage,
2589 &view->plane->damage, &damage);
2590 pixman_region32_fini(&damage);
2591 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002592 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002593}
2594
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002595static void
Michael Olbrichc5ea4952020-08-11 12:42:35 +02002596output_accumulate_damage(struct weston_output *output)
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002597{
Michael Olbrichc5ea4952020-08-11 12:42:35 +02002598 struct weston_compositor *ec = output->compositor;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002599 struct weston_plane *plane;
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002600 struct weston_paint_node *pnode;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002601 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002602
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002603 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002604
2605 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002606 pixman_region32_copy(&plane->clip, &clip);
2607
2608 pixman_region32_init(&opaque);
2609
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002610 wl_list_for_each(pnode, &output->paint_node_z_order_list,
2611 z_order_link) {
2612 if (pnode->view->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002613 continue;
2614
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002615 view_accumulate_damage(pnode->view, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002616 }
2617
2618 pixman_region32_union(&clip, &clip, &opaque);
2619 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002620 }
2621
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002622 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002623
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002624 wl_list_for_each(pnode, &output->paint_node_z_order_list,
2625 z_order_link) {
2626 pnode->surface->touched = false;
2627 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002628
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002629 wl_list_for_each(pnode, &output->paint_node_z_order_list,
2630 z_order_link) {
Michael Olbrichc5ea4952020-08-11 12:42:35 +02002631 /* Ignore views not visible on the current output */
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002632 /* TODO: turn this into assert once z_order_list is pruned. */
2633 if (!(pnode->view->output_mask & (1u << output->id)))
Michael Olbrichc5ea4952020-08-11 12:42:35 +02002634 continue;
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002635 if (pnode->surface->touched)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002636 continue;
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002637 pnode->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002638
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002639 surface_flush_damage(pnode->surface);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002640
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002641 /* Both the renderer and the backend have seen the buffer
2642 * by now. If renderer needs the buffer, it has its own
2643 * reference set. If the backend wants to keep the buffer
2644 * around for migrating the surface into a non-primary plane
2645 * later, keep_buffer is true. Otherwise, drop the core
2646 * reference now, and allow early buffer release. This enables
2647 * clients to use single-buffering.
2648 */
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002649 if (!pnode->surface->keep_buffer) {
2650 weston_buffer_reference(&pnode->surface->buffer_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002651 weston_buffer_release_reference(
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002652 &pnode->surface->buffer_release_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002653 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002654 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002655}
2656
2657static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002658surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002659{
2660 struct weston_subsurface *sub;
2661
Pekka Paalanene67858b2013-04-25 13:57:42 +03002662 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002663 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002664 continue;
2665
Jason Ekstranda7af7042013-10-12 22:38:11 -05002666 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2667 wl_list_init(&sub->surface->views);
2668
2669 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002670 }
2671}
2672
2673static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002674surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002675{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002676 struct weston_subsurface *sub;
2677 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002678
Jason Ekstranda7af7042013-10-12 22:38:11 -05002679 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2680 if (sub->surface == surface)
2681 continue;
2682
George Kiagiadakised04d382014-06-13 18:10:26 +02002683 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2684 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002685 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002686 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002687
2688 surface_free_unused_subsurface_views(sub->surface);
2689 }
2690}
2691
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002692static struct weston_paint_node *
2693view_ensure_paint_node(struct weston_view *view, struct weston_output *output)
2694{
2695 struct weston_paint_node *pnode;
2696
2697 if (!output)
2698 return NULL;
2699
2700 pnode = weston_view_find_paint_node(view, output);
2701 if (pnode)
2702 return pnode;
2703
2704 return weston_paint_node_create(view->surface, view, output);
2705}
2706
Jason Ekstranda7af7042013-10-12 22:38:11 -05002707static void
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002708add_to_z_order_list(struct weston_output *output,
2709 struct weston_paint_node *pnode)
2710{
2711 if (!pnode)
2712 return;
2713
2714 wl_list_remove(&pnode->z_order_link);
2715 wl_list_insert(output->paint_node_z_order_list.prev,
2716 &pnode->z_order_link);
Pekka Paalanen90a5ffa2021-02-25 12:03:28 +02002717
2718 /*
2719 * Building weston_output::paint_node_z_order_list ensures all
2720 * necessary color transform objects are installed.
2721 */
2722 weston_paint_node_ensure_color_transform(pnode);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002723}
2724
2725static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002726view_list_add_subsurface_view(struct weston_compositor *compositor,
2727 struct weston_subsurface *sub,
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002728 struct weston_view *parent,
2729 struct weston_output *output)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002730{
2731 struct weston_subsurface *child;
2732 struct weston_view *view = NULL, *iv;
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002733 struct weston_paint_node *pnode;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002734
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002735 if (!weston_surface_is_mapped(sub->surface))
2736 return;
2737
Jason Ekstranda7af7042013-10-12 22:38:11 -05002738 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2739 if (iv->geometry.parent == parent) {
2740 view = iv;
2741 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002742 }
2743 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002744
2745 if (view) {
2746 /* Put it back in the surface's list of views */
2747 wl_list_remove(&view->surface_link);
2748 wl_list_insert(&sub->surface->views, &view->surface_link);
2749 } else {
2750 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002751 weston_view_set_position(view,
2752 sub->position.x,
2753 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002754 weston_view_set_transform_parent(view, parent);
2755 }
2756
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002757 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002758 weston_view_update_transform(view);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002759 view->is_mapped = true;
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002760 pnode = view_ensure_paint_node(view, output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002761
Pekka Paalanenb188e912013-11-19 14:03:35 +02002762 if (wl_list_empty(&sub->surface->subsurface_list)) {
2763 wl_list_insert(compositor->view_list.prev, &view->link);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002764 add_to_z_order_list(output, pnode);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002765 return;
2766 }
2767
2768 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002769 if (child->surface == sub->surface) {
Pekka Paalanenb188e912013-11-19 14:03:35 +02002770 wl_list_insert(compositor->view_list.prev, &view->link);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002771 add_to_z_order_list(output, pnode);
2772 } else {
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002773 view_list_add_subsurface_view(compositor, child, view, output);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002774 }
Pekka Paalanenb188e912013-11-19 14:03:35 +02002775 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002776}
2777
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002778/* This recursively adds the sub-surfaces for a view, relying on the
2779 * sub-surface order. Thus, if a client restacks the sub-surfaces, that
2780 * change first happens to the sub-surface list, and then automatically
2781 * propagates here. See weston_surface_damage_subsurfaces() for how the
2782 * sub-surfaces receive damage when the client changes the state.
2783 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002784static void
2785view_list_add(struct weston_compositor *compositor,
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002786 struct weston_view *view,
2787 struct weston_output *output)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002788{
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002789 struct weston_paint_node *pnode;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002790 struct weston_subsurface *sub;
2791
2792 weston_view_update_transform(view);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002793 pnode = view_ensure_paint_node(view, output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002794
Pekka Paalanenb188e912013-11-19 14:03:35 +02002795 if (wl_list_empty(&view->surface->subsurface_list)) {
2796 wl_list_insert(compositor->view_list.prev, &view->link);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002797 add_to_z_order_list(output, pnode);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002798 return;
2799 }
2800
2801 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002802 if (sub->surface == view->surface) {
Pekka Paalanenb188e912013-11-19 14:03:35 +02002803 wl_list_insert(compositor->view_list.prev, &view->link);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002804 add_to_z_order_list(output, pnode);
2805 } else {
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002806 view_list_add_subsurface_view(compositor, sub, view, output);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002807 }
Pekka Paalanenb188e912013-11-19 14:03:35 +02002808 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002809}
2810
2811static void
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002812weston_compositor_build_view_list(struct weston_compositor *compositor,
2813 struct weston_output *output)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002814{
Loïc Yhuel267b16e2019-09-17 20:14:56 +02002815 struct weston_view *view, *tmp;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002816 struct weston_layer *layer;
2817
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002818 if (output) {
2819 wl_list_remove(&output->paint_node_z_order_list);
2820 wl_list_init(&output->paint_node_z_order_list);
2821 }
2822
Jason Ekstranda7af7042013-10-12 22:38:11 -05002823 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002824 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002825 surface_stash_subsurface_views(view->surface);
2826
Loïc Yhuel267b16e2019-09-17 20:14:56 +02002827 wl_list_for_each_safe(view, tmp, &compositor->view_list, link)
2828 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002829 wl_list_init(&compositor->view_list);
Loïc Yhuel267b16e2019-09-17 20:14:56 +02002830
Jason Ekstranda7af7042013-10-12 22:38:11 -05002831 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002832 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002833 view_list_add(compositor, view, output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002834 }
2835 }
2836
2837 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002838 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002839 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002840}
2841
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002842static void
2843weston_output_take_feedback_list(struct weston_output *output,
2844 struct weston_surface *surface)
2845{
2846 struct weston_view *view;
2847 struct weston_presentation_feedback *feedback;
2848 uint32_t flags = 0xffffffff;
2849
2850 if (wl_list_empty(&surface->feedback_list))
2851 return;
2852
2853 /* All views must have the flag for the flag to survive. */
2854 wl_list_for_each(view, &surface->views, surface_link) {
2855 /* ignore views that are not on this output at all */
2856 if (view->output_mask & (1u << output->id))
2857 flags &= view->psf_flags;
2858 }
2859
2860 wl_list_for_each(feedback, &surface->feedback_list, link)
2861 feedback->psf_flags = flags;
2862
2863 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2864 wl_list_init(&surface->feedback_list);
2865}
2866
David Herrmann1edf44c2013-10-22 17:11:26 +02002867static int
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002868weston_output_repaint(struct weston_output *output, void *repaint_data)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002869{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002870 struct weston_compositor *ec = output->compositor;
Pekka Paalanen6d6b3672021-05-03 14:06:55 +03002871 struct weston_paint_node *pnode;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002872 struct weston_animation *animation, *next;
2873 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002874 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002875 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002876 int r;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002877 uint32_t frame_time_msec;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05302878 enum weston_hdcp_protection highest_requested = WESTON_HDCP_DISABLE;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002879
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002880 if (output->destroying)
2881 return 0;
2882
Marius Vlad3203ff62019-09-05 14:56:12 +03002883 TL_POINT(ec, "core_repaint_begin", TLP_OUTPUT(output), TLP_END);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002884
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002885 /* Rebuild the surface list and update surface transforms up front. */
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002886 weston_compositor_build_view_list(ec, output);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002887
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05302888 /* Find the highest protection desired for an output */
Pekka Paalanen6d6b3672021-05-03 14:06:55 +03002889 wl_list_for_each(pnode, &output->paint_node_z_order_list,
2890 z_order_link) {
2891 /* TODO: turn this into assert once z_order_list is pruned. */
2892 if ((pnode->surface->output_mask & (1u << output->id)) == 0)
2893 continue;
2894
2895 /*
2896 * The desired_protection of the output should be the
2897 * maximum of the desired_protection of the surfaces,
2898 * that are displayed on that output, to avoid
2899 * reducing the protection for existing surfaces.
2900 */
2901 if (pnode->surface->desired_protection > highest_requested)
2902 highest_requested = pnode->surface->desired_protection;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05302903 }
2904
2905 output->desired_protection = highest_requested;
2906
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002907 if (output->assign_planes && !output->disable_planes) {
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002908 output->assign_planes(output, repaint_data);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002909 } else {
Pekka Paalanen6d6b3672021-05-03 14:06:55 +03002910 wl_list_for_each(pnode, &output->paint_node_z_order_list,
2911 z_order_link) {
2912 weston_view_move_to_plane(pnode->view, &ec->primary_plane);
2913 pnode->view->psf_flags = 0;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002914 }
2915 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002916
Pekka Paalanene67858b2013-04-25 13:57:42 +03002917 wl_list_init(&frame_callback_list);
Pekka Paalanen6d6b3672021-05-03 14:06:55 +03002918 wl_list_for_each(pnode, &output->paint_node_z_order_list,
2919 z_order_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002920 /* Note: This operation is safe to do multiple times on the
2921 * same surface.
2922 */
Pekka Paalanen6d6b3672021-05-03 14:06:55 +03002923 if (pnode->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002924 wl_list_insert_list(&frame_callback_list,
Pekka Paalanen6d6b3672021-05-03 14:06:55 +03002925 &pnode->surface->frame_callback_list);
2926 wl_list_init(&pnode->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002927
Pekka Paalanen6d6b3672021-05-03 14:06:55 +03002928 weston_output_take_feedback_list(output, pnode->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002929 }
2930 }
2931
Michael Olbrichc5ea4952020-08-11 12:42:35 +02002932 output_accumulate_damage(output);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002933
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002934 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002935 pixman_region32_intersect(&output_damage,
2936 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002937 pixman_region32_subtract(&output_damage,
2938 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002939
Scott Moreauccbf29d2012-02-22 14:21:41 -07002940 if (output->dirty)
2941 weston_output_update_matrix(output);
2942
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002943 r = output->repaint(output, &output_damage, repaint_data);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002944
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002945 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002946
Daniel Stone09a97e22017-03-01 11:34:06 +00002947 output->repaint_needed = false;
Daniel Stone05df8c12017-03-03 16:59:42 +00002948 if (r == 0)
2949 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002950
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002951 weston_compositor_repick(ec);
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002952
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002953 frame_time_msec = timespec_to_msec(&output->frame_time);
2954
Jonas Ådahldb773762012-06-13 00:01:21 +02002955 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002956 wl_callback_send_done(cb->resource, frame_time_msec);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002957 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002958 }
2959
Scott Moreaud64cf212012-06-08 19:40:54 -06002960 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002961 animation->frame_counter++;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002962 animation->frame(animation, output, &output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002963 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002964
Marius Vlad3203ff62019-09-05 14:56:12 +03002965 TL_POINT(ec, "core_repaint_posted", TLP_OUTPUT(output), TLP_END);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002966
David Herrmann1edf44c2013-10-22 17:11:26 +02002967 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002968}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002969
Pekka Paalanen82919792014-05-21 13:51:49 +03002970static void
2971weston_output_schedule_repaint_reset(struct weston_output *output)
2972{
Daniel Stone05df8c12017-03-03 16:59:42 +00002973 output->repaint_status = REPAINT_NOT_SCHEDULED;
Marius Vlad3203ff62019-09-05 14:56:12 +03002974 TL_POINT(output->compositor, "core_repaint_exit_loop",
2975 TLP_OUTPUT(output), TLP_END);
Pekka Paalanen82919792014-05-21 13:51:49 +03002976}
2977
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002978static int
2979weston_output_maybe_repaint(struct weston_output *output, struct timespec *now,
2980 void *repaint_data)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002981{
Pekka Paalanen0513a952014-05-21 16:17:27 +03002982 struct weston_compositor *compositor = output->compositor;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002983 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002984 int64_t msec_to_repaint;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002985
Daniel Stone6847b852017-03-01 11:34:08 +00002986 /* We're not ready yet; come back to make a decision later. */
2987 if (output->repaint_status != REPAINT_SCHEDULED)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002988 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002989
2990 msec_to_repaint = timespec_sub_to_msec(&output->next_repaint, now);
2991 if (msec_to_repaint > 1)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002992 return ret;
Daniel Stone05df8c12017-03-03 16:59:42 +00002993
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002994 /* If we're sleeping, drop the repaint machinery entirely; we will
2995 * explicitly repaint all outputs when we come back. */
2996 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2997 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
2998 goto err;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002999
Daniel Stonecd1a1c32017-01-16 15:38:54 +00003000 /* We don't actually need to repaint this output; drop it from
3001 * repaint until something causes damage. */
3002 if (!output->repaint_needed)
3003 goto err;
3004
3005 /* If repaint fails, we aren't going to get weston_output_finish_frame
3006 * to trigger a new repaint, so drop it from repaint and hope
Daniel Stone6847b852017-03-01 11:34:08 +00003007 * something schedules a successful repaint later. As repainting may
3008 * take some time, re-read our clock as a courtesy to the next
3009 * output. */
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003010 ret = weston_output_repaint(output, repaint_data);
Daniel Stone6847b852017-03-01 11:34:08 +00003011 weston_compositor_read_presentation_clock(compositor, now);
Daniel Stonecd1a1c32017-01-16 15:38:54 +00003012 if (ret != 0)
3013 goto err;
3014
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09003015 output->repainted = true;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003016 return ret;
Daniel Stonecd1a1c32017-01-16 15:38:54 +00003017
3018err:
Pekka Paalanen0513a952014-05-21 16:17:27 +03003019 weston_output_schedule_repaint_reset(output);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003020 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00003021}
3022
3023static void
3024output_repaint_timer_arm(struct weston_compositor *compositor)
3025{
3026 struct weston_output *output;
3027 bool any_should_repaint = false;
3028 struct timespec now;
Sergi Granellb4c08862017-03-18 13:01:15 +01003029 int64_t msec_to_next = INT64_MAX;
Daniel Stone6847b852017-03-01 11:34:08 +00003030
3031 weston_compositor_read_presentation_clock(compositor, &now);
3032
3033 wl_list_for_each(output, &compositor->output_list, link) {
3034 int64_t msec_to_this;
3035
3036 if (output->repaint_status != REPAINT_SCHEDULED)
3037 continue;
3038
3039 msec_to_this = timespec_sub_to_msec(&output->next_repaint,
3040 &now);
3041 if (!any_should_repaint || msec_to_this < msec_to_next)
3042 msec_to_next = msec_to_this;
3043
3044 any_should_repaint = true;
3045 }
3046
3047 if (!any_should_repaint)
3048 return;
3049
3050 /* Even if we should repaint immediately, add the minimum 1 ms delay.
3051 * This is a workaround to allow coalescing multiple output repaints
3052 * particularly from weston_output_finish_frame()
3053 * into the same call, which would not happen if we called
3054 * output_repaint_timer_handler() directly.
3055 */
3056 if (msec_to_next < 1)
3057 msec_to_next = 1;
3058
3059 wl_event_source_timer_update(compositor->repaint_timer, msec_to_next);
3060}
3061
3062static int
3063output_repaint_timer_handler(void *data)
3064{
3065 struct weston_compositor *compositor = data;
3066 struct weston_output *output;
3067 struct timespec now;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003068 void *repaint_data = NULL;
Emre Ucane479ed82018-03-20 15:29:40 +01003069 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00003070
3071 weston_compositor_read_presentation_clock(compositor, &now);
Pekka Paalanen1ed2cad2021-02-10 12:33:03 +02003072 compositor->last_repaint_start = now;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003073
3074 if (compositor->backend->repaint_begin)
3075 repaint_data = compositor->backend->repaint_begin(compositor);
3076
3077 wl_list_for_each(output, &compositor->output_list, link) {
3078 ret = weston_output_maybe_repaint(output, &now, repaint_data);
3079 if (ret)
3080 break;
3081 }
3082
3083 if (ret == 0) {
Tomohito Esaki09bfcd62018-06-05 10:37:05 +09003084 if (compositor->backend->repaint_flush)
Antonio Borneoc90fccc2019-06-30 15:51:10 +02003085 ret = compositor->backend->repaint_flush(compositor,
3086 repaint_data);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003087 } else {
Antonio Borneoc90fccc2019-06-30 15:51:10 +02003088 if (compositor->backend->repaint_cancel)
3089 compositor->backend->repaint_cancel(compositor,
3090 repaint_data);
3091 }
3092
3093 if (ret != 0) {
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09003094 wl_list_for_each(output, &compositor->output_list, link) {
3095 if (output->repainted)
3096 weston_output_schedule_repaint_reset(output);
3097 }
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003098 }
Daniel Stone6847b852017-03-01 11:34:08 +00003099
Tomohito Esakiddaf95c2018-07-10 11:47:15 +09003100 wl_list_for_each(output, &compositor->output_list, link)
3101 output->repainted = false;
3102
Daniel Stone6847b852017-03-01 11:34:08 +00003103 output_repaint_timer_arm(compositor);
3104
Pekka Paalanen0513a952014-05-21 16:17:27 +03003105 return 0;
3106}
3107
Pekka Paalanen50aa3a72020-05-28 11:34:04 +03003108/** Convert a presentation timestamp to another clock domain
3109 *
3110 * \param compositor The compositor defines the presentation clock domain.
3111 * \param presentation_stamp The timestamp in presentation clock domain.
3112 * \param presentation_now Current time in presentation clock domain.
3113 * \param target_clock Defines the target clock domain.
3114 *
3115 * This approximation relies on presentation_stamp to be close to current time.
3116 * The further it is from current time and the bigger the speed difference
3117 * between the two clock domains, the bigger the conversion error.
3118 *
3119 * Conversion error due to system load is biased and unbounded.
3120 */
3121static struct timespec
3122convert_presentation_time_now(struct weston_compositor *compositor,
3123 const struct timespec *presentation_stamp,
3124 const struct timespec *presentation_now,
3125 clockid_t target_clock)
3126{
3127 struct timespec target_now = {};
3128 struct timespec target_stamp;
3129 int64_t delta_ns;
3130
3131 if (compositor->presentation_clock == target_clock)
3132 return *presentation_stamp;
3133
3134 clock_gettime(target_clock, &target_now);
3135 delta_ns = timespec_sub_to_nsec(presentation_stamp, presentation_now);
3136 timespec_add_nsec(&target_stamp, &target_now, delta_ns);
3137
3138 return target_stamp;
3139}
3140
Marius Vlad55d87362019-06-11 01:15:35 +03003141/**
3142 * \ingroup output
3143 */
Kristian Høgsbergef044142011-06-21 15:02:12 -04003144WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04003145weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02003146 const struct timespec *stamp,
3147 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04003148{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003149 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03003150 int32_t refresh_nsec;
3151 struct timespec now;
Pekka Paalanen50aa3a72020-05-28 11:34:04 +03003152 struct timespec vblank_monotonic;
Daniel Stone6847b852017-03-01 11:34:08 +00003153 int64_t msec_rel;
Pekka Paalanen133e4392014-09-23 22:08:46 -04003154
Daniel Stone05df8c12017-03-03 16:59:42 +00003155 assert(output->repaint_status == REPAINT_AWAITING_COMPLETION);
Pekka Paalanen98b4e202021-05-10 11:33:23 +03003156
3157 /*
3158 * If timestamp of latest vblank is given, it must always go forwards.
3159 * If not given, INVALID flag must be set.
3160 */
3161 if (stamp)
3162 assert(timespec_sub_to_nsec(stamp, &output->frame_time) >= 0);
3163 else
3164 assert(presented_flags & WP_PRESENTATION_FEEDBACK_INVALID);
Daniel Stone3615ce12017-03-01 11:34:05 +00003165
Daniel Stone6847b852017-03-01 11:34:08 +00003166 weston_compositor_read_presentation_clock(compositor, &now);
3167
Daniel Stone3615ce12017-03-01 11:34:05 +00003168 /* If we haven't been supplied any timestamp at all, we don't have a
3169 * timebase to work against, so any delay just wastes time. Push a
3170 * repaint as soon as possible so we can get on with it. */
Daniel Stone6847b852017-03-01 11:34:08 +00003171 if (!stamp) {
3172 output->next_repaint = now;
Daniel Stone3615ce12017-03-01 11:34:05 +00003173 goto out;
Daniel Stone6847b852017-03-01 11:34:08 +00003174 }
Daniel Stone3615ce12017-03-01 11:34:05 +00003175
Pekka Paalanen50aa3a72020-05-28 11:34:04 +03003176 vblank_monotonic = convert_presentation_time_now(compositor,
3177 stamp, &now,
3178 CLOCK_MONOTONIC);
Marius Vlad3203ff62019-09-05 14:56:12 +03003179 TL_POINT(compositor, "core_repaint_finished", TLP_OUTPUT(output),
Pekka Paalanen50aa3a72020-05-28 11:34:04 +03003180 TLP_VBLANK(&vblank_monotonic), TLP_END);
Marius Vladdf9278a2018-03-06 18:56:23 +02003181
Pekka Paalanend7894d02015-07-03 15:08:53 +03003182 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003183 weston_presentation_feedback_present_list(&output->feedback_list,
3184 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02003185 output->msc,
3186 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003187
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02003188 output->frame_time = *stamp;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003189
Daniel Stone6847b852017-03-01 11:34:08 +00003190 timespec_add_nsec(&output->next_repaint, stamp, refresh_nsec);
3191 timespec_add_msec(&output->next_repaint, &output->next_repaint,
3192 -compositor->repaint_msec);
3193 msec_rel = timespec_sub_to_msec(&output->next_repaint, &now);
Daniel Stone84aff5c2017-03-01 11:34:04 +00003194
3195 if (msec_rel < -1000 || msec_rel > 1000) {
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02003196 static bool warned;
3197
3198 if (!warned)
3199 weston_log("Warning: computed repaint delay is "
Daniel Stone6847b852017-03-01 11:34:08 +00003200 "insane: %lld msec\n", (long long) msec_rel);
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02003201 warned = true;
3202
Daniel Stone6847b852017-03-01 11:34:08 +00003203 output->next_repaint = now;
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02003204 }
3205
Mario Kleinerb7df04e2015-06-21 21:25:15 +02003206 /* Called from restart_repaint_loop and restart happens already after
3207 * the deadline given by repaint_msec? In that case we delay until
3208 * the deadline of the next frame, to give clients a more predictable
3209 * timing of the repaint cycle to lock on. */
Daniel Stoneeca5cca2017-02-28 21:53:51 +00003210 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID &&
3211 msec_rel < 0) {
3212 while (timespec_sub_to_nsec(&output->next_repaint, &now) < 0) {
3213 timespec_add_nsec(&output->next_repaint,
3214 &output->next_repaint,
3215 refresh_nsec);
3216 }
3217 }
Mario Kleinerb7df04e2015-06-21 21:25:15 +02003218
Daniel Stone3615ce12017-03-01 11:34:05 +00003219out:
Daniel Stone05df8c12017-03-03 16:59:42 +00003220 output->repaint_status = REPAINT_SCHEDULED;
Daniel Stone6847b852017-03-01 11:34:08 +00003221 output_repaint_timer_arm(compositor);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003222}
3223
3224static void
3225idle_repaint(void *data)
3226{
3227 struct weston_output *output = data;
Antonio Borneoc90fccc2019-06-30 15:51:10 +02003228 int ret;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003229
Daniel Stone05df8c12017-03-03 16:59:42 +00003230 assert(output->repaint_status == REPAINT_BEGIN_FROM_IDLE);
3231 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03003232 output->idle_repaint_source = NULL;
Antonio Borneoc90fccc2019-06-30 15:51:10 +02003233 ret = output->start_repaint_loop(output);
3234 if (ret != 0)
3235 weston_output_schedule_repaint_reset(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003236}
3237
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003238WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003239weston_layer_entry_insert(struct weston_layer_entry *list,
3240 struct weston_layer_entry *entry)
3241{
3242 wl_list_insert(&list->link, &entry->link);
3243 entry->layer = list->layer;
3244}
3245
3246WL_EXPORT void
3247weston_layer_entry_remove(struct weston_layer_entry *entry)
3248{
3249 wl_list_remove(&entry->link);
3250 wl_list_init(&entry->link);
3251 entry->layer = NULL;
3252}
3253
Quentin Glidic82681572016-12-17 13:40:51 +01003254
3255/** Initialize the weston_layer struct.
3256 *
3257 * \param compositor The compositor instance
3258 * \param layer The layer to initialize
3259 */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003260WL_EXPORT void
Quentin Glidic82681572016-12-17 13:40:51 +01003261weston_layer_init(struct weston_layer *layer,
3262 struct weston_compositor *compositor)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003263{
Quentin Glidic82681572016-12-17 13:40:51 +01003264 layer->compositor = compositor;
3265 wl_list_init(&layer->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003266 wl_list_init(&layer->view_list.link);
3267 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003268 weston_layer_set_mask_infinite(layer);
Quentin Glidic82681572016-12-17 13:40:51 +01003269}
3270
Pekka Paalanen87400372021-05-14 14:29:40 +03003271/** Finalize the weston_layer struct.
3272 *
3273 * \param layer The layer to finalize.
3274 */
3275WL_EXPORT void
3276weston_layer_fini(struct weston_layer *layer)
3277{
3278 wl_list_remove(&layer->link);
3279
3280 if (!wl_list_empty(&layer->view_list.link))
3281 weston_log("BUG: finalizing a layer with views still on it.\n");
3282
3283 wl_list_remove(&layer->view_list.link);
3284}
3285
Quentin Glidic82681572016-12-17 13:40:51 +01003286/** Sets the position of the layer in the layer list. The layer will be placed
3287 * below any layer with the same position value, if any.
3288 * This function is safe to call if the layer is already on the list, but the
3289 * layer may be moved below other layers at the same position, if any.
3290 *
3291 * \param layer The layer to modify
3292 * \param position The position the layer will be placed at
3293 */
3294WL_EXPORT void
3295weston_layer_set_position(struct weston_layer *layer,
3296 enum weston_layer_position position)
3297{
3298 struct weston_layer *below;
3299
3300 wl_list_remove(&layer->link);
3301
3302 /* layer_list is ordered from top to bottom, the last layer being the
3303 * background with the smallest position value */
3304
3305 layer->position = position;
3306 wl_list_for_each_reverse(below, &layer->compositor->layer_list, link) {
3307 if (below->position >= layer->position) {
3308 wl_list_insert(&below->link, &layer->link);
3309 return;
3310 }
3311 }
3312 wl_list_insert(&layer->compositor->layer_list, &layer->link);
3313}
3314
3315/** Hide a layer by taking it off the layer list.
3316 * This function is safe to call if the layer is not on the list.
3317 *
3318 * \param layer The layer to hide
3319 */
3320WL_EXPORT void
3321weston_layer_unset_position(struct weston_layer *layer)
3322{
3323 wl_list_remove(&layer->link);
3324 wl_list_init(&layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003325}
3326
3327WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003328weston_layer_set_mask(struct weston_layer *layer,
3329 int x, int y, int width, int height)
3330{
3331 struct weston_view *view;
3332
3333 layer->mask.x1 = x;
3334 layer->mask.x2 = x + width;
3335 layer->mask.y1 = y;
3336 layer->mask.y2 = y + height;
3337
3338 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
3339 weston_view_geometry_dirty(view);
3340 }
3341}
3342
3343WL_EXPORT void
3344weston_layer_set_mask_infinite(struct weston_layer *layer)
3345{
Adam Jackson3c3f3b12019-10-16 16:02:59 -04003346 struct weston_view *view;
3347
3348 layer->mask.x1 = INT32_MIN;
3349 layer->mask.x2 = INT32_MAX;
3350 layer->mask.y1 = INT32_MIN;
3351 layer->mask.y2 = INT32_MAX;
3352
3353 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
3354 weston_view_geometry_dirty(view);
3355 }
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003356}
3357
Daniel Stone3b775632018-07-20 08:38:25 +01003358WL_EXPORT bool
3359weston_layer_mask_is_infinite(struct weston_layer *layer)
3360{
3361 return layer->mask.x1 == INT32_MIN &&
3362 layer->mask.y1 == INT32_MIN &&
Adam Jackson3c3f3b12019-10-16 16:02:59 -04003363 layer->mask.x2 == INT32_MAX &&
3364 layer->mask.y2 == INT32_MAX;
Daniel Stone3b775632018-07-20 08:38:25 +01003365}
3366
Marius Vlad55d87362019-06-11 01:15:35 +03003367/**
3368 * \ingroup output
3369 */
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003370WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003371weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003372{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003373 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04003374 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01003375
Bryce Harrington08976ac2016-08-30 12:05:16 -07003376 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
3377 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003378 return;
3379
Pekka Paalanenb5026542014-11-12 15:09:24 +02003380 if (!output->repaint_needed)
Marius Vlad3203ff62019-09-05 14:56:12 +03003381 TL_POINT(compositor, "core_repaint_req", TLP_OUTPUT(output), TLP_END);
Pekka Paalanenb5026542014-11-12 15:09:24 +02003382
Kristian Høgsbergef044142011-06-21 15:02:12 -04003383 loop = wl_display_get_event_loop(compositor->wl_display);
Daniel Stone09a97e22017-03-01 11:34:06 +00003384 output->repaint_needed = true;
Daniel Stone05df8c12017-03-03 16:59:42 +00003385
3386 /* If we already have a repaint scheduled for our idle handler,
3387 * no need to set it again. If the repaint has been called but
3388 * not finished, then weston_output_finish_frame() will notice
3389 * that a repaint is needed and schedule one. */
3390 if (output->repaint_status != REPAINT_NOT_SCHEDULED)
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003391 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01003392
Daniel Stone05df8c12017-03-03 16:59:42 +00003393 output->repaint_status = REPAINT_BEGIN_FROM_IDLE;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03003394 assert(!output->idle_repaint_source);
3395 output->idle_repaint_source = wl_event_loop_add_idle(loop, idle_repaint,
3396 output);
Marius Vlad3203ff62019-09-05 14:56:12 +03003397 TL_POINT(compositor, "core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003398}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05003399
Marius Vlad9fdda7f2019-06-11 16:08:55 +03003400/** weston_compositor_schedule_repaint
3401 * \ingroup compositor
3402 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003403WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003404weston_compositor_schedule_repaint(struct weston_compositor *compositor)
3405{
3406 struct weston_output *output;
3407
3408 wl_list_for_each(output, &compositor->output_list, link)
3409 weston_output_schedule_repaint(output);
3410}
3411
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003412static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003413surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003414{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003415 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003416}
3417
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003418static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003419surface_attach(struct wl_client *client,
3420 struct wl_resource *resource,
3421 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
3422{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003423 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05003424 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003425
Kristian Høgsbergab19f932013-08-20 11:30:54 -07003426 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05003427 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07003428 if (buffer == NULL) {
3429 wl_client_post_no_memory(client);
3430 return;
3431 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07003432 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04003433
Pekka Paalanende685b82012-12-04 15:58:12 +02003434 /* Attach, attach, without commit in between does not send
3435 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003436 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003437
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003438 surface->pending.sx = sx;
3439 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01003440 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003441}
3442
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003443static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003444surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003445 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003446 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05003447{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003448 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003449
Derek Foreman57e92ed2015-11-17 14:11:35 -06003450 if (width <= 0 || height <= 0)
3451 return;
3452
Derek Foreman152254b2015-11-26 14:17:48 -06003453 pixman_region32_union_rect(&surface->pending.damage_surface,
3454 &surface->pending.damage_surface,
3455 x, y, width, height);
3456}
3457
3458static void
3459surface_damage_buffer(struct wl_client *client,
3460 struct wl_resource *resource,
3461 int32_t x, int32_t y, int32_t width, int32_t height)
3462{
3463 struct weston_surface *surface = wl_resource_get_user_data(resource);
3464
3465 if (width <= 0 || height <= 0)
3466 return;
3467
3468 pixman_region32_union_rect(&surface->pending.damage_buffer,
3469 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04003470 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003471}
3472
Kristian Høgsberg33418202011-08-16 23:01:28 -04003473static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003474destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04003475{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05003476 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003477
3478 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02003479 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003480}
3481
3482static void
3483surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003484 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04003485{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003486 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003487 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003488
3489 cb = malloc(sizeof *cb);
3490 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003491 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003492 return;
3493 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03003494
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003495 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
3496 callback);
3497 if (cb->resource == NULL) {
3498 free(cb);
3499 wl_resource_post_no_memory(resource);
3500 return;
3501 }
3502
Jason Ekstranda85118c2013-06-27 20:17:02 -05003503 wl_resource_set_implementation(cb->resource, NULL, cb,
3504 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003505
Pekka Paalanenbc106382012-10-10 12:49:31 +03003506 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003507}
3508
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003509static void
3510surface_set_opaque_region(struct wl_client *client,
3511 struct wl_resource *resource,
3512 struct wl_resource *region_resource)
3513{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003514 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003515 struct weston_region *region;
3516
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003517 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003518 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003519 pixman_region32_copy(&surface->pending.opaque,
3520 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003521 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07003522 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003523 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003524}
3525
3526static void
3527surface_set_input_region(struct wl_client *client,
3528 struct wl_resource *resource,
3529 struct wl_resource *region_resource)
3530{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003531 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003532 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003533
3534 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003535 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003536 pixman_region32_copy(&surface->pending.input,
3537 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003538 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003539 pixman_region32_fini(&surface->pending.input);
3540 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003541 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003542}
3543
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003544/* Cause damage to this sub-surface and all its children.
3545 *
3546 * This is useful when there are state changes that need an implicit
3547 * damage, e.g. a z-order change.
3548 */
3549static void
3550weston_surface_damage_subsurfaces(struct weston_subsurface *sub)
3551{
3552 struct weston_subsurface *child;
3553
3554 weston_surface_damage(sub->surface);
3555 sub->reordered = false;
3556
3557 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link)
3558 if (child != sub)
3559 weston_surface_damage_subsurfaces(child);
3560}
3561
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003562static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003563weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003564{
Pekka Paalanene67858b2013-04-25 13:57:42 +03003565 struct weston_subsurface *sub;
3566
3567 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
3568 parent_link_pending) {
3569 wl_list_remove(&sub->parent_link);
3570 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003571
3572 if (sub->reordered)
3573 weston_surface_damage_subsurfaces(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003574 }
3575}
3576
3577static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03003578weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05003579 struct weston_matrix *matrix)
3580{
Pekka Paalanen04baea52016-04-26 15:50:58 +03003581 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05003582 double src_width, src_height, dest_width, dest_height;
3583
3584 weston_matrix_init(matrix);
3585
3586 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
3587 src_width = surface->width_from_buffer;
3588 src_height = surface->height_from_buffer;
3589 } else {
3590 src_width = wl_fixed_to_double(vp->buffer.src_width);
3591 src_height = wl_fixed_to_double(vp->buffer.src_height);
3592 }
3593
3594 if (vp->surface.width == -1) {
3595 dest_width = src_width;
3596 dest_height = src_height;
3597 } else {
3598 dest_width = vp->surface.width;
3599 dest_height = vp->surface.height;
3600 }
3601
3602 if (src_width != dest_width || src_height != dest_height)
3603 weston_matrix_scale(matrix,
3604 src_width / dest_width,
3605 src_height / dest_height, 1);
3606
3607 if (vp->buffer.src_width != wl_fixed_from_int(-1))
3608 weston_matrix_translate(matrix,
3609 wl_fixed_to_double(vp->buffer.src_x),
3610 wl_fixed_to_double(vp->buffer.src_y),
3611 0);
3612
3613 switch (vp->buffer.transform) {
3614 case WL_OUTPUT_TRANSFORM_FLIPPED:
3615 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3616 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3617 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3618 weston_matrix_scale(matrix, -1, 1, 1);
3619 weston_matrix_translate(matrix,
3620 surface->width_from_buffer, 0, 0);
3621 break;
3622 }
3623
3624 switch (vp->buffer.transform) {
3625 default:
3626 case WL_OUTPUT_TRANSFORM_NORMAL:
3627 case WL_OUTPUT_TRANSFORM_FLIPPED:
3628 break;
3629 case WL_OUTPUT_TRANSFORM_90:
3630 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +02003631 weston_matrix_rotate_xy(matrix, 0, -1);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003632 weston_matrix_translate(matrix,
Pekka Paalanen8060d822020-02-06 15:27:54 +02003633 0, surface->width_from_buffer, 0);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003634 break;
3635 case WL_OUTPUT_TRANSFORM_180:
3636 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3637 weston_matrix_rotate_xy(matrix, -1, 0);
3638 weston_matrix_translate(matrix,
3639 surface->width_from_buffer,
3640 surface->height_from_buffer, 0);
3641 break;
3642 case WL_OUTPUT_TRANSFORM_270:
3643 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +02003644 weston_matrix_rotate_xy(matrix, 0, 1);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003645 weston_matrix_translate(matrix,
Pekka Paalanen8060d822020-02-06 15:27:54 +02003646 surface->height_from_buffer, 0, 0);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003647 break;
3648 }
3649
3650 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
3651}
3652
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003653/**
3654 * Compute a + b > c while being safe to overflows.
3655 */
3656static bool
3657fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
3658{
3659 return (int64_t)a + (int64_t)b > (int64_t)c;
3660}
3661
3662static bool
3663weston_surface_is_pending_viewport_source_valid(
3664 const struct weston_surface *surface)
3665{
3666 const struct weston_surface_state *pend = &surface->pending;
3667 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
3668 int width_from_buffer = 0;
3669 int height_from_buffer = 0;
3670 wl_fixed_t w;
3671 wl_fixed_t h;
3672
3673 /* If viewport source rect is not set, it is always ok. */
3674 if (vp->buffer.src_width == wl_fixed_from_int(-1))
3675 return true;
3676
3677 if (pend->newly_attached) {
3678 if (pend->buffer) {
3679 convert_size_by_transform_scale(&width_from_buffer,
3680 &height_from_buffer,
3681 pend->buffer->width,
3682 pend->buffer->height,
3683 vp->buffer.transform,
3684 vp->buffer.scale);
3685 } else {
3686 /* No buffer: viewport is irrelevant. */
3687 return true;
3688 }
3689 } else {
3690 width_from_buffer = surface->width_from_buffer;
3691 height_from_buffer = surface->height_from_buffer;
3692 }
3693
3694 assert((width_from_buffer == 0) == (height_from_buffer == 0));
3695 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
3696
3697 /* No buffer: viewport is irrelevant. */
3698 if (width_from_buffer == 0 || height_from_buffer == 0)
3699 return true;
3700
3701 /* overflow checks for wl_fixed_from_int() */
3702 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
3703 return false;
3704 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
3705 return false;
3706
3707 w = wl_fixed_from_int(width_from_buffer);
3708 h = wl_fixed_from_int(height_from_buffer);
3709
3710 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
3711 return false;
3712 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
3713 return false;
3714
3715 return true;
3716}
3717
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003718static bool
3719fixed_is_integer(wl_fixed_t v)
3720{
3721 return (v & 0xff) == 0;
3722}
3723
3724static bool
3725weston_surface_is_pending_viewport_dst_size_int(
3726 const struct weston_surface *surface)
3727{
3728 const struct weston_buffer_viewport *vp =
3729 &surface->pending.buffer_viewport;
3730
3731 if (vp->surface.width != -1) {
3732 assert(vp->surface.width > 0 && vp->surface.height > 0);
3733 return true;
3734 }
3735
3736 return fixed_is_integer(vp->buffer.src_width) &&
3737 fixed_is_integer(vp->buffer.src_height);
3738}
3739
Derek Foreman152254b2015-11-26 14:17:48 -06003740/* Translate pending damage in buffer co-ordinates to surface
3741 * co-ordinates and union it with a pixman_region32_t.
3742 * This should only be called after the buffer is attached.
3743 */
3744static void
3745apply_damage_buffer(pixman_region32_t *dest,
3746 struct weston_surface *surface,
3747 struct weston_surface_state *state)
3748{
3749 struct weston_buffer *buffer = surface->buffer_ref.buffer;
3750
3751 /* wl_surface.damage_buffer needs to be clipped to the buffer,
3752 * translated into surface co-ordinates and unioned with
3753 * any other surface damage.
3754 * None of this makes sense if there is no buffer though.
3755 */
3756 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
3757 pixman_region32_t buffer_damage;
3758
3759 pixman_region32_intersect_rect(&state->damage_buffer,
3760 &state->damage_buffer,
3761 0, 0, buffer->width,
3762 buffer->height);
3763 pixman_region32_init(&buffer_damage);
3764 weston_matrix_transform_region(&buffer_damage,
3765 &surface->buffer_to_surface_matrix,
3766 &state->damage_buffer);
3767 pixman_region32_union(dest, dest, &buffer_damage);
3768 pixman_region32_fini(&buffer_damage);
3769 }
3770 /* We should clear this on commit even if there was no buffer */
3771 pixman_region32_clear(&state->damage_buffer);
3772}
3773
Jason Ekstrand1e059042014-10-16 10:55:19 -05003774static void
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303775weston_surface_set_desired_protection(struct weston_surface *surface,
3776 enum weston_hdcp_protection protection)
3777{
3778 if (surface->desired_protection == protection)
3779 return;
3780 surface->desired_protection = protection;
3781 weston_surface_damage(surface);
3782}
3783
3784static void
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303785weston_surface_set_protection_mode(struct weston_surface *surface,
3786 enum weston_surface_protection_mode p_mode)
3787{
3788 struct content_protection *cp = surface->compositor->content_protection;
3789 struct protected_surface *psurface;
3790
3791 surface->protection_mode = p_mode;
3792 wl_list_for_each(psurface, &cp->protected_list, link) {
3793 if (!psurface || psurface->surface != surface)
3794 continue;
3795 weston_protected_surface_send_event(psurface,
3796 surface->current_protection);
3797 }
3798}
3799
3800static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05003801weston_surface_commit_state(struct weston_surface *surface,
3802 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003803{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003804 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003805 pixman_region32_t opaque;
3806
Alexander Larsson4ea95522013-05-22 14:41:37 +02003807 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02003808 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03003809 /* wp_viewport.set_source */
3810 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003811 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003812
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003813 /* wl_surface.attach */
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003814 if (state->newly_attached) {
3815 /* zwp_surface_synchronization_v1.set_acquire_fence */
3816 fd_move(&surface->acquire_fence_fd,
3817 &state->acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003818 /* zwp_surface_synchronization_v1.get_release */
3819 weston_buffer_release_move(&surface->buffer_release_ref,
3820 &state->buffer_release_ref);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003821 weston_surface_attach(surface, state->buffer);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003822 }
Jason Ekstrand7b982072014-05-20 14:33:03 -05003823 weston_surface_state_set_buffer(state, NULL);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003824 assert(state->acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003825 assert(state->buffer_release_ref.buffer_release == NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01003826
Jason Ekstrand1e059042014-10-16 10:55:19 -05003827 weston_surface_build_buffer_matrix(surface,
3828 &surface->surface_to_buffer_matrix);
3829 weston_matrix_invert(&surface->buffer_to_surface_matrix,
3830 &surface->surface_to_buffer_matrix);
3831
Jason Ekstrand7b982072014-05-20 14:33:03 -05003832 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003833 weston_surface_update_size(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003834 if (surface->committed)
3835 surface->committed(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003836 }
Giulio Camuffo184df502013-02-21 11:29:21 +01003837
Jason Ekstrand7b982072014-05-20 14:33:03 -05003838 state->sx = 0;
3839 state->sy = 0;
3840 state->newly_attached = 0;
3841 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03003842
Derek Foreman152254b2015-11-26 14:17:48 -06003843 /* wl_surface.damage and wl_surface.damage_buffer */
Marius Vlad2a1b7862019-09-05 13:12:18 +03003844 if (pixman_region32_not_empty(&state->damage_surface) ||
3845 pixman_region32_not_empty(&state->damage_buffer))
Marius Vlad3203ff62019-09-05 14:56:12 +03003846 TL_POINT(surface->compositor, "core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06003847
Pekka Paalanen8e159182012-10-10 12:49:25 +03003848 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06003849 &state->damage_surface);
3850
3851 apply_damage_buffer(&surface->damage, surface, state);
3852
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05003853 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07003854 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06003855 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003856
3857 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003858 pixman_region32_init(&opaque);
3859 pixman_region32_intersect_rect(&opaque, &state->opaque,
3860 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003861
3862 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
3863 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003864 wl_list_for_each(view, &surface->views, surface_link)
3865 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003866 }
3867
3868 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003869
3870 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003871 pixman_region32_intersect_rect(&surface->input, &state->input,
3872 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003873
Pekka Paalanenbc106382012-10-10 12:49:31 +03003874 /* wl_surface.frame */
3875 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05003876 &state->frame_callback_list);
3877 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003878
3879 /* XXX:
3880 * What should happen with a feedback request, if there
3881 * is no wl_buffer attached for this commit?
3882 */
3883
3884 /* presentation.feedback */
3885 wl_list_insert_list(&surface->feedback_list,
3886 &state->feedback_list);
3887 wl_list_init(&state->feedback_list);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003888
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303889 /* weston_protected_surface.enforced/relaxed */
3890 if (surface->protection_mode != state->protection_mode)
3891 weston_surface_set_protection_mode(surface,
3892 state->protection_mode);
3893
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303894 /* weston_protected_surface.set_type */
3895 weston_surface_set_desired_protection(surface, state->desired_protection);
3896
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003897 wl_signal_emit(&surface->commit_signal, surface);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003898}
3899
3900static void
3901weston_surface_commit(struct weston_surface *surface)
3902{
3903 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003904
Pekka Paalanene67858b2013-04-25 13:57:42 +03003905 weston_surface_commit_subsurface_order(surface);
3906
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003907 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003908}
3909
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003910static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003911weston_subsurface_commit(struct weston_subsurface *sub);
3912
3913static void
3914weston_subsurface_parent_commit(struct weston_subsurface *sub,
3915 int parent_is_synchronized);
3916
3917static void
3918surface_commit(struct wl_client *client, struct wl_resource *resource)
3919{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003920 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003921 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
3922
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003923 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
3924 assert(surface->viewport_resource);
3925
3926 wl_resource_post_error(surface->viewport_resource,
3927 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
3928 "wl_surface@%d has viewport source outside buffer",
3929 wl_resource_get_id(resource));
3930 return;
3931 }
3932
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003933 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
3934 assert(surface->viewport_resource);
3935
3936 wl_resource_post_error(surface->viewport_resource,
3937 WP_VIEWPORT_ERROR_BAD_SIZE,
3938 "wl_surface@%d viewport dst size not integer",
3939 wl_resource_get_id(resource));
3940 return;
3941 }
3942
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003943 if (surface->pending.acquire_fence_fd >= 0) {
3944 assert(surface->synchronization_resource);
3945
3946 if (!surface->pending.buffer) {
3947 fd_clear(&surface->pending.acquire_fence_fd);
3948 wl_resource_post_error(surface->synchronization_resource,
3949 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3950 "wl_surface@%"PRIu32" no buffer for synchronization",
3951 wl_resource_get_id(resource));
3952 return;
3953 }
3954
3955 /* We support fences for both wp_linux_dmabuf and opaque EGL
3956 * buffers, as mandated by minor version 2 of the
3957 * zwp_linux_explicit_synchronization_v1 protocol. Since
3958 * renderers that support fences currently only support these
3959 * two buffer types plus SHM buffers, we can just check for the
3960 * SHM buffer case here.
3961 */
3962 if (wl_shm_buffer_get(surface->pending.buffer->resource)) {
3963 fd_clear(&surface->pending.acquire_fence_fd);
3964 wl_resource_post_error(surface->synchronization_resource,
3965 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_UNSUPPORTED_BUFFER,
3966 "wl_surface@%"PRIu32" unsupported buffer for synchronization",
3967 wl_resource_get_id(resource));
3968 return;
3969 }
3970 }
3971
Alexandros Frantzis67629672018-10-19 12:14:11 +03003972 if (surface->pending.buffer_release_ref.buffer_release &&
3973 !surface->pending.buffer) {
3974 assert(surface->synchronization_resource);
3975
3976 wl_resource_post_error(surface->synchronization_resource,
3977 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3978 "wl_surface@%"PRIu32" no buffer for synchronization",
3979 wl_resource_get_id(resource));
3980 return;
3981 }
3982
Pekka Paalanene67858b2013-04-25 13:57:42 +03003983 if (sub) {
3984 weston_subsurface_commit(sub);
3985 return;
3986 }
3987
3988 weston_surface_commit(surface);
3989
3990 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
3991 if (sub->surface != surface)
3992 weston_subsurface_parent_commit(sub, 0);
3993 }
3994}
3995
3996static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003997surface_set_buffer_transform(struct wl_client *client,
3998 struct wl_resource *resource, int transform)
3999{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004000 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004001
Jonny Lamba55f1392014-05-30 12:07:15 +02004002 /* if wl_output.transform grows more members this will need to be updated. */
4003 if (transform < 0 ||
4004 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
4005 wl_resource_post_error(resource,
4006 WL_SURFACE_ERROR_INVALID_TRANSFORM,
4007 "buffer transform must be a valid transform "
4008 "('%d' specified)", transform);
4009 return;
4010 }
4011
Pekka Paalanen952b6c82014-03-14 14:38:15 +02004012 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004013 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004014}
4015
Alexander Larsson4ea95522013-05-22 14:41:37 +02004016static void
4017surface_set_buffer_scale(struct wl_client *client,
4018 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004019 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004020{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004021 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02004022
Jonny Lamba55f1392014-05-30 12:07:15 +02004023 if (scale < 1) {
4024 wl_resource_post_error(resource,
4025 WL_SURFACE_ERROR_INVALID_SCALE,
4026 "buffer scale must be at least one "
4027 "('%d' specified)", scale);
4028 return;
4029 }
4030
Pekka Paalanen952b6c82014-03-14 14:38:15 +02004031 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004032 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02004033}
4034
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04004035static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004036 surface_destroy,
4037 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04004038 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004039 surface_frame,
4040 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03004041 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004042 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02004043 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06004044 surface_set_buffer_scale,
4045 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004046};
4047
4048static void
4049compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04004050 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004051{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04004052 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004053 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004054
Kristian Høgsberg18c93002012-01-27 11:58:31 -05004055 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04004056 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04004057 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004058 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04004059 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004060
Jason Ekstranda85118c2013-06-27 20:17:02 -05004061 surface->resource =
4062 wl_resource_create(client, &wl_surface_interface,
4063 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004064 if (surface->resource == NULL) {
4065 weston_surface_destroy(surface);
4066 wl_resource_post_no_memory(resource);
4067 return;
4068 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05004069 wl_resource_set_implementation(surface->resource, &surface_interface,
4070 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07004071
4072 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004073}
4074
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004075static void
4076destroy_region(struct wl_resource *resource)
4077{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05004078 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004079
4080 pixman_region32_fini(&region->region);
4081 free(region);
4082}
4083
4084static void
4085region_destroy(struct wl_client *client, struct wl_resource *resource)
4086{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004087 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004088}
4089
4090static void
4091region_add(struct wl_client *client, struct wl_resource *resource,
4092 int32_t x, int32_t y, int32_t width, int32_t height)
4093{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05004094 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004095
4096 pixman_region32_union_rect(&region->region, &region->region,
4097 x, y, width, height);
4098}
4099
4100static void
4101region_subtract(struct wl_client *client, struct wl_resource *resource,
4102 int32_t x, int32_t y, int32_t width, int32_t height)
4103{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05004104 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004105 pixman_region32_t rect;
4106
4107 pixman_region32_init_rect(&rect, x, y, width, height);
4108 pixman_region32_subtract(&region->region, &region->region, &rect);
4109 pixman_region32_fini(&rect);
4110}
4111
4112static const struct wl_region_interface region_interface = {
4113 region_destroy,
4114 region_add,
4115 region_subtract
4116};
4117
4118static void
4119compositor_create_region(struct wl_client *client,
4120 struct wl_resource *resource, uint32_t id)
4121{
4122 struct weston_region *region;
4123
4124 region = malloc(sizeof *region);
4125 if (region == NULL) {
4126 wl_resource_post_no_memory(resource);
4127 return;
4128 }
4129
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004130 pixman_region32_init(&region->region);
4131
Jason Ekstranda85118c2013-06-27 20:17:02 -05004132 region->resource =
4133 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004134 if (region->resource == NULL) {
4135 free(region);
4136 wl_resource_post_no_memory(resource);
4137 return;
4138 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05004139 wl_resource_set_implementation(region->resource, &region_interface,
4140 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004141}
4142
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04004143static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004144 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004145 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004146};
4147
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004148static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03004149weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
4150{
4151 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004152
Jason Ekstrand7b982072014-05-20 14:33:03 -05004153 weston_surface_commit_state(surface, &sub->cached);
4154 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004155
4156 weston_surface_commit_subsurface_order(surface);
4157
4158 weston_surface_schedule_repaint(surface);
4159
Jason Ekstrand7b982072014-05-20 14:33:03 -05004160 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004161}
4162
4163static void
4164weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
4165{
4166 struct weston_surface *surface = sub->surface;
4167
4168 /*
4169 * If this commit would cause the surface to move by the
4170 * attach(dx, dy) parameters, the old damage region must be
4171 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05004172 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03004173 */
Derek Foreman152254b2015-11-26 14:17:48 -06004174 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004175 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06004176 pixman_region32_union(&sub->cached.damage_surface,
4177 &sub->cached.damage_surface,
4178 &surface->pending.damage_surface);
4179 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004180
4181 if (surface->pending.newly_attached) {
4182 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05004183 weston_surface_state_set_buffer(&sub->cached,
4184 surface->pending.buffer);
4185 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004186 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04004187 weston_presentation_feedback_discard_list(
4188 &sub->cached.feedback_list);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03004189 /* zwp_surface_synchronization_v1.set_acquire_fence */
4190 fd_move(&sub->cached.acquire_fence_fd,
4191 &surface->pending.acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03004192 /* zwp_surface_synchronization_v1.get_release */
4193 weston_buffer_release_move(&sub->cached.buffer_release_ref,
4194 &surface->pending.buffer_release_ref);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004195 }
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05304196 sub->cached.desired_protection = surface->pending.desired_protection;
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05304197 sub->cached.protection_mode = surface->pending.protection_mode;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03004198 assert(surface->pending.acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03004199 assert(surface->pending.buffer_release_ref.buffer_release == NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004200 sub->cached.sx += surface->pending.sx;
4201 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02004202
Derek Foreman152254b2015-11-26 14:17:48 -06004203 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
4204
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004205 sub->cached.buffer_viewport.changed |=
4206 surface->pending.buffer_viewport.changed;
4207 sub->cached.buffer_viewport.buffer =
4208 surface->pending.buffer_viewport.buffer;
4209 sub->cached.buffer_viewport.surface =
4210 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004211
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004212 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004213
4214 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
4215
4216 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
4217
4218 wl_list_insert_list(&sub->cached.frame_callback_list,
4219 &surface->pending.frame_callback_list);
4220 wl_list_init(&surface->pending.frame_callback_list);
4221
Pekka Paalanen133e4392014-09-23 22:08:46 -04004222 wl_list_insert_list(&sub->cached.feedback_list,
4223 &surface->pending.feedback_list);
4224 wl_list_init(&surface->pending.feedback_list);
4225
Jason Ekstrand7b982072014-05-20 14:33:03 -05004226 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004227}
4228
Derek Foreman280e7dd2014-10-03 13:13:42 -05004229static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03004230weston_subsurface_is_synchronized(struct weston_subsurface *sub)
4231{
4232 while (sub) {
4233 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05004234 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004235
4236 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05004237 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004238
4239 sub = weston_surface_to_subsurface(sub->parent);
4240 }
4241
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01004242 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004243}
4244
4245static void
4246weston_subsurface_commit(struct weston_subsurface *sub)
4247{
4248 struct weston_surface *surface = sub->surface;
4249 struct weston_subsurface *tmp;
4250
4251 /* Recursive check for effectively synchronized. */
4252 if (weston_subsurface_is_synchronized(sub)) {
4253 weston_subsurface_commit_to_cache(sub);
4254 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05004255 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03004256 /* flush accumulated state from cache */
4257 weston_subsurface_commit_to_cache(sub);
4258 weston_subsurface_commit_from_cache(sub);
4259 } else {
4260 weston_surface_commit(surface);
4261 }
4262
4263 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
4264 if (tmp->surface != surface)
4265 weston_subsurface_parent_commit(tmp, 0);
4266 }
4267 }
4268}
4269
4270static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004271weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004272{
4273 struct weston_surface *surface = sub->surface;
4274 struct weston_subsurface *tmp;
4275
Pekka Paalanene67858b2013-04-25 13:57:42 +03004276 /* From now on, commit_from_cache the whole sub-tree, regardless of
4277 * the synchronized mode of each child. This sub-surface or some
4278 * of its ancestors were synchronized, so we are synchronized
4279 * all the way down.
4280 */
4281
Jason Ekstrand7b982072014-05-20 14:33:03 -05004282 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004283 weston_subsurface_commit_from_cache(sub);
4284
4285 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
4286 if (tmp->surface != surface)
4287 weston_subsurface_parent_commit(tmp, 1);
4288 }
4289}
4290
4291static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004292weston_subsurface_parent_commit(struct weston_subsurface *sub,
4293 int parent_is_synchronized)
4294{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004295 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004296 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004297 wl_list_for_each(view, &sub->surface->views, surface_link)
4298 weston_view_set_position(view,
4299 sub->position.x,
4300 sub->position.y);
4301
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004302 sub->position.set = 0;
4303 }
4304
4305 if (parent_is_synchronized || sub->synchronized)
4306 weston_subsurface_synchronized_commit(sub);
4307}
4308
Pekka Paalanen8274d902014-08-06 19:36:51 +03004309static int
4310subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
4311{
4312 return snprintf(buf, len, "sub-surface");
4313}
4314
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004315static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004316subsurface_committed(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004317{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004318 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004319
Jason Ekstranda7af7042013-10-12 22:38:11 -05004320 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004321 weston_view_set_position(view,
4322 view->geometry.x + dx,
4323 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004324
4325 /* No need to check parent mappedness, because if parent is not
4326 * mapped, parent is not in a visible layer, so this sub-surface
4327 * will not be drawn either.
4328 */
Armin Krezovićf8486c32016-06-30 06:04:28 +02004329
Pekka Paalanene67858b2013-04-25 13:57:42 +03004330 if (!weston_surface_is_mapped(surface)) {
Armin Krezovićf8486c32016-06-30 06:04:28 +02004331 surface->is_mapped = true;
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03004332
Derek Foreman4b1a0a12014-09-10 15:37:33 -05004333 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03004334 * because that would call it also for the parent surface,
4335 * which might not be mapped yet. That would lead to
4336 * inconsistent state, where the window could never be
4337 * mapped.
4338 *
Armin Krezovićf8486c32016-06-30 06:04:28 +02004339 * Instead just force the is_mapped flag on, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03004340 * weston_surface_is_mapped() return true, so that when the
4341 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03004342 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03004343 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004344 }
4345}
4346
4347static struct weston_subsurface *
4348weston_surface_to_subsurface(struct weston_surface *surface)
4349{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004350 if (surface->committed == subsurface_committed)
4351 return surface->committed_private;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004352
4353 return NULL;
4354}
4355
Pekka Paalanen01388e22013-04-25 13:57:44 +03004356WL_EXPORT struct weston_surface *
4357weston_surface_get_main_surface(struct weston_surface *surface)
4358{
4359 struct weston_subsurface *sub;
4360
4361 while (surface && (sub = weston_surface_to_subsurface(surface)))
4362 surface = sub->parent;
4363
4364 return surface;
4365}
4366
Pekka Paalanen50b67472014-10-01 15:02:41 +03004367WL_EXPORT int
4368weston_surface_set_role(struct weston_surface *surface,
4369 const char *role_name,
4370 struct wl_resource *error_resource,
4371 uint32_t error_code)
4372{
4373 assert(role_name);
4374
4375 if (surface->role_name == NULL ||
4376 surface->role_name == role_name ||
4377 strcmp(surface->role_name, role_name) == 0) {
4378 surface->role_name = role_name;
4379
4380 return 0;
4381 }
4382
4383 wl_resource_post_error(error_resource, error_code,
4384 "Cannot assign role %s to wl_surface@%d,"
4385 " already has role %s\n",
4386 role_name,
4387 wl_resource_get_id(surface->resource),
4388 surface->role_name);
4389 return -1;
4390}
4391
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02004392WL_EXPORT const char *
4393weston_surface_get_role(struct weston_surface *surface)
4394{
4395 return surface->role_name;
4396}
4397
Pekka Paalanen8274d902014-08-06 19:36:51 +03004398WL_EXPORT void
4399weston_surface_set_label_func(struct weston_surface *surface,
4400 int (*desc)(struct weston_surface *,
4401 char *, size_t))
4402{
4403 surface->get_label = desc;
Marius Vlad5de92972019-10-15 13:25:41 +03004404 weston_timeline_refresh_subscription_objects(surface->compositor,
4405 surface);
Pekka Paalanen8274d902014-08-06 19:36:51 +03004406}
4407
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004408/** Get the size of surface contents
4409 *
4410 * \param surface The surface to query.
4411 * \param width Returns the width of raw contents.
4412 * \param height Returns the height of raw contents.
4413 *
4414 * Retrieves the raw surface content size in pixels for the given surface.
4415 * This is the whole content size in buffer pixels. If the surface
4416 * has no content or the renderer does not implement this feature,
4417 * zeroes are returned.
4418 *
4419 * This function is used to determine the buffer size needed for
4420 * a weston_surface_copy_content() call.
4421 */
4422WL_EXPORT void
4423weston_surface_get_content_size(struct weston_surface *surface,
4424 int *width, int *height)
4425{
4426 struct weston_renderer *rer = surface->compositor->renderer;
4427
4428 if (!rer->surface_get_content_size) {
4429 *width = 0;
4430 *height = 0;
4431 return;
4432 }
4433
4434 rer->surface_get_content_size(surface, width, height);
4435}
4436
Quentin Glidic248dd102016-08-12 10:41:34 +02004437/** Get the bounding box of a surface and its subsurfaces
4438 *
4439 * \param surface The surface to query.
4440 * \return The bounding box relative to the surface origin.
4441 *
4442 */
4443WL_EXPORT struct weston_geometry
4444weston_surface_get_bounding_box(struct weston_surface *surface)
4445{
4446 pixman_region32_t region;
4447 pixman_box32_t *box;
4448 struct weston_subsurface *subsurface;
4449
4450 pixman_region32_init_rect(&region,
4451 0, 0,
4452 surface->width, surface->height);
4453
4454 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link)
4455 pixman_region32_union_rect(&region, &region,
4456 subsurface->position.x,
4457 subsurface->position.y,
4458 subsurface->surface->width,
4459 subsurface->surface->height);
4460
4461 box = pixman_region32_extents(&region);
4462 struct weston_geometry geometry = {
4463 .x = box->x1,
4464 .y = box->y1,
4465 .width = box->x2 - box->x1,
4466 .height = box->y2 - box->y1,
4467 };
4468
4469 pixman_region32_fini(&region);
4470
4471 return geometry;
4472}
4473
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004474/** Copy surface contents to system memory.
4475 *
4476 * \param surface The surface to copy from.
4477 * \param target Pointer to the target memory buffer.
4478 * \param size Size of the target buffer in bytes.
4479 * \param src_x X location on contents to copy from.
4480 * \param src_y Y location on contents to copy from.
4481 * \param width Width in pixels of the area to copy.
4482 * \param height Height in pixels of the area to copy.
4483 * \return 0 for success, -1 for failure.
4484 *
4485 * Surface contents are maintained by the renderer. They can be in a
4486 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
4487 * else.
4488 *
4489 * Surface contents are copied into memory pointed to by target,
4490 * which has size bytes of space available. The target memory
4491 * may be larger than needed, but being smaller returns an error.
4492 * The extra bytes in target may or may not be written; their content is
4493 * unspecified. Size must be large enough to hold the image.
4494 *
4495 * The image in the target memory will be arranged in rows from
4496 * top to bottom, and pixels on a row from left to right. The pixel
4497 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
4498 * width * 4.
4499 *
4500 * Parameters src_x and src_y define the upper-left corner in buffer
4501 * coordinates (pixels) to copy from. Parameters width and height
4502 * define the size of the area to copy in pixels.
4503 *
4504 * The rectangle defined by src_x, src_y, width, height must fit in
4505 * the surface contents. Otherwise an error is returned.
4506 *
Changwoo Chof97d2502017-08-05 00:30:47 +09004507 * Use weston_surface_get_content_size to determine the content size; the
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004508 * needed target buffer size and rectangle limits.
4509 *
4510 * CURRENT IMPLEMENTATION RESTRICTIONS:
4511 * - the machine must be little-endian due to Pixman formats.
4512 *
4513 * NOTE: Pixman formats are premultiplied.
4514 */
4515WL_EXPORT int
4516weston_surface_copy_content(struct weston_surface *surface,
4517 void *target, size_t size,
4518 int src_x, int src_y,
4519 int width, int height)
4520{
4521 struct weston_renderer *rer = surface->compositor->renderer;
4522 int cw, ch;
4523 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
4524
4525 if (!rer->surface_copy_content)
4526 return -1;
4527
4528 weston_surface_get_content_size(surface, &cw, &ch);
4529
4530 if (src_x < 0 || src_y < 0)
4531 return -1;
4532
4533 if (width <= 0 || height <= 0)
4534 return -1;
4535
4536 if (src_x + width > cw || src_y + height > ch)
4537 return -1;
4538
4539 if (width * bytespp * height > size)
4540 return -1;
4541
4542 return rer->surface_copy_content(surface, target, size,
4543 src_x, src_y, width, height);
4544}
4545
Pekka Paalanene67858b2013-04-25 13:57:42 +03004546static void
4547subsurface_set_position(struct wl_client *client,
4548 struct wl_resource *resource, int32_t x, int32_t y)
4549{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004550 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004551
4552 if (!sub)
4553 return;
4554
4555 sub->position.x = x;
4556 sub->position.y = y;
4557 sub->position.set = 1;
4558}
4559
4560static struct weston_subsurface *
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004561subsurface_find_sibling(struct weston_subsurface *sub,
4562 struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004563{
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004564 struct weston_surface *parent = sub->parent;
4565 struct weston_subsurface *sibling;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004566
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004567 wl_list_for_each(sibling, &parent->subsurface_list, parent_link) {
4568 if (sibling->surface == surface && sibling != sub)
4569 return sibling;
4570 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004571
4572 return NULL;
4573}
4574
4575static struct weston_subsurface *
4576subsurface_sibling_check(struct weston_subsurface *sub,
4577 struct weston_surface *surface,
4578 const char *request)
4579{
4580 struct weston_subsurface *sibling;
4581
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004582 sibling = subsurface_find_sibling(sub, surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004583 if (!sibling) {
4584 wl_resource_post_error(sub->resource,
4585 WL_SUBSURFACE_ERROR_BAD_SURFACE,
4586 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004587 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004588 return NULL;
4589 }
4590
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004591 assert(sibling->parent == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004592
4593 return sibling;
4594}
4595
4596static void
4597subsurface_place_above(struct wl_client *client,
4598 struct wl_resource *resource,
4599 struct wl_resource *sibling_resource)
4600{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004601 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004602 struct weston_surface *surface =
4603 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004604 struct weston_subsurface *sibling;
4605
4606 if (!sub)
4607 return;
4608
4609 sibling = subsurface_sibling_check(sub, surface, "place_above");
4610 if (!sibling)
4611 return;
4612
4613 wl_list_remove(&sub->parent_link_pending);
4614 wl_list_insert(sibling->parent_link_pending.prev,
4615 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004616
4617 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004618}
4619
4620static void
4621subsurface_place_below(struct wl_client *client,
4622 struct wl_resource *resource,
4623 struct wl_resource *sibling_resource)
4624{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004625 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004626 struct weston_surface *surface =
4627 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004628 struct weston_subsurface *sibling;
4629
4630 if (!sub)
4631 return;
4632
4633 sibling = subsurface_sibling_check(sub, surface, "place_below");
4634 if (!sibling)
4635 return;
4636
4637 wl_list_remove(&sub->parent_link_pending);
4638 wl_list_insert(&sibling->parent_link_pending,
4639 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004640
4641 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004642}
4643
4644static void
4645subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
4646{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004647 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004648
4649 if (sub)
4650 sub->synchronized = 1;
4651}
4652
4653static void
4654subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
4655{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004656 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004657
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004658 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03004659 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004660
4661 /* If sub became effectively desynchronized, flush. */
4662 if (!weston_subsurface_is_synchronized(sub))
4663 weston_subsurface_synchronized_commit(sub);
4664 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004665}
4666
4667static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03004668weston_subsurface_unlink_parent(struct weston_subsurface *sub)
4669{
4670 wl_list_remove(&sub->parent_link);
4671 wl_list_remove(&sub->parent_link_pending);
4672 wl_list_remove(&sub->parent_destroy_listener.link);
4673 sub->parent = NULL;
4674}
4675
4676static void
4677weston_subsurface_destroy(struct weston_subsurface *sub);
4678
4679static void
4680subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
4681{
4682 struct weston_subsurface *sub =
4683 container_of(listener, struct weston_subsurface,
4684 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004685 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004686
4687 /* The protocol object (wl_resource) is left inert. */
4688 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004689 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004690
4691 weston_subsurface_destroy(sub);
4692}
4693
4694static void
4695subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
4696{
4697 struct weston_subsurface *sub =
4698 container_of(listener, struct weston_subsurface,
4699 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004700 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004701 assert(sub->surface != sub->parent);
4702
4703 if (weston_surface_is_mapped(sub->surface))
4704 weston_surface_unmap(sub->surface);
4705
4706 weston_subsurface_unlink_parent(sub);
4707}
4708
4709static void
4710subsurface_resource_destroy(struct wl_resource *resource)
4711{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004712 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004713
4714 if (sub)
4715 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004716}
4717
4718static void
4719subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
4720{
4721 wl_resource_destroy(resource);
4722}
4723
4724static void
4725weston_subsurface_link_parent(struct weston_subsurface *sub,
4726 struct weston_surface *parent)
4727{
4728 sub->parent = parent;
4729 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004730 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004731 &sub->parent_destroy_listener);
4732
4733 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4734 wl_list_insert(&parent->subsurface_list_pending,
4735 &sub->parent_link_pending);
4736}
4737
4738static void
4739weston_subsurface_link_surface(struct weston_subsurface *sub,
4740 struct weston_surface *surface)
4741{
4742 sub->surface = surface;
4743 sub->surface_destroy_listener.notify =
4744 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004745 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004746 &sub->surface_destroy_listener);
4747}
4748
4749static void
4750weston_subsurface_destroy(struct weston_subsurface *sub)
4751{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004752 struct weston_view *view, *next;
4753
Pekka Paalanene67858b2013-04-25 13:57:42 +03004754 assert(sub->surface);
4755
4756 if (sub->resource) {
4757 assert(weston_surface_to_subsurface(sub->surface) == sub);
4758 assert(sub->parent_destroy_listener.notify ==
4759 subsurface_handle_parent_destroy);
4760
George Kiagiadakised04d382014-06-13 18:10:26 +02004761 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
4762 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004763 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02004764 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05004765
Pekka Paalanene67858b2013-04-25 13:57:42 +03004766 if (sub->parent)
4767 weston_subsurface_unlink_parent(sub);
4768
Jason Ekstrand7b982072014-05-20 14:33:03 -05004769 weston_surface_state_fini(&sub->cached);
4770 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004771
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004772 sub->surface->committed = NULL;
4773 sub->surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004774 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004775 } else {
4776 /* the dummy weston_subsurface for the parent itself */
4777 assert(sub->parent_destroy_listener.notify == NULL);
4778 wl_list_remove(&sub->parent_link);
4779 wl_list_remove(&sub->parent_link_pending);
4780 }
4781
4782 wl_list_remove(&sub->surface_destroy_listener.link);
4783 free(sub);
4784}
4785
4786static const struct wl_subsurface_interface subsurface_implementation = {
4787 subsurface_destroy,
4788 subsurface_set_position,
4789 subsurface_place_above,
4790 subsurface_place_below,
4791 subsurface_set_sync,
4792 subsurface_set_desync
4793};
4794
4795static struct weston_subsurface *
4796weston_subsurface_create(uint32_t id, struct weston_surface *surface,
4797 struct weston_surface *parent)
4798{
4799 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004800 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004801
Bryce Harringtonde16d892014-11-20 22:21:57 -08004802 sub = zalloc(sizeof *sub);
4803 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004804 return NULL;
4805
Jason Ekstranda7af7042013-10-12 22:38:11 -05004806 wl_list_init(&sub->unused_views);
4807
Jason Ekstranda85118c2013-06-27 20:17:02 -05004808 sub->resource =
4809 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004810 if (!sub->resource) {
4811 free(sub);
4812 return NULL;
4813 }
4814
Jason Ekstranda85118c2013-06-27 20:17:02 -05004815 wl_resource_set_implementation(sub->resource,
4816 &subsurface_implementation,
4817 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004818 weston_subsurface_link_surface(sub, surface);
4819 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05004820 weston_surface_state_init(&sub->cached);
4821 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004822 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004823
4824 return sub;
4825}
4826
4827/* Create a dummy subsurface for having the parent itself in its
4828 * sub-surface lists. Makes stacking order manipulation easy.
4829 */
4830static struct weston_subsurface *
4831weston_subsurface_create_for_parent(struct weston_surface *parent)
4832{
4833 struct weston_subsurface *sub;
4834
Bryce Harringtonde16d892014-11-20 22:21:57 -08004835 sub = zalloc(sizeof *sub);
4836 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004837 return NULL;
4838
4839 weston_subsurface_link_surface(sub, parent);
4840 sub->parent = parent;
4841 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4842 wl_list_insert(&parent->subsurface_list_pending,
4843 &sub->parent_link_pending);
4844
4845 return sub;
4846}
4847
4848static void
4849subcompositor_get_subsurface(struct wl_client *client,
4850 struct wl_resource *resource,
4851 uint32_t id,
4852 struct wl_resource *surface_resource,
4853 struct wl_resource *parent_resource)
4854{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004855 struct weston_surface *surface =
4856 wl_resource_get_user_data(surface_resource);
4857 struct weston_surface *parent =
4858 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004859 struct weston_subsurface *sub;
4860 static const char where[] = "get_subsurface: wl_subsurface@";
4861
4862 if (surface == parent) {
4863 wl_resource_post_error(resource,
4864 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4865 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004866 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004867 return;
4868 }
4869
4870 if (weston_surface_to_subsurface(surface)) {
4871 wl_resource_post_error(resource,
4872 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4873 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004874 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004875 return;
4876 }
4877
Pekka Paalanen50b67472014-10-01 15:02:41 +03004878 if (weston_surface_set_role(surface, "wl_subsurface", resource,
4879 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004880 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004881
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004882 if (weston_surface_get_main_surface(parent) == surface) {
4883 wl_resource_post_error(resource,
4884 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4885 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004886 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004887 return;
4888 }
4889
Pekka Paalanene67858b2013-04-25 13:57:42 +03004890 /* make sure the parent is in its own list */
4891 if (wl_list_empty(&parent->subsurface_list)) {
4892 if (!weston_subsurface_create_for_parent(parent)) {
4893 wl_resource_post_no_memory(resource);
4894 return;
4895 }
4896 }
4897
4898 sub = weston_subsurface_create(id, surface, parent);
4899 if (!sub) {
4900 wl_resource_post_no_memory(resource);
4901 return;
4902 }
4903
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004904 surface->committed = subsurface_committed;
4905 surface->committed_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004906 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004907}
4908
4909static void
4910subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
4911{
4912 wl_resource_destroy(resource);
4913}
4914
4915static const struct wl_subcompositor_interface subcompositor_interface = {
4916 subcompositor_destroy,
4917 subcompositor_get_subsurface
4918};
4919
4920static void
4921bind_subcompositor(struct wl_client *client,
4922 void *data, uint32_t version, uint32_t id)
4923{
4924 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004925 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004926
Jason Ekstranda85118c2013-06-27 20:17:02 -05004927 resource =
4928 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004929 if (resource == NULL) {
4930 wl_client_post_no_memory(client);
4931 return;
4932 }
4933 wl_resource_set_implementation(resource, &subcompositor_interface,
4934 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004935}
4936
Bryce Harrington0795ece2016-08-30 12:04:26 -07004937/** Set a DPMS mode on all of the compositor's outputs
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004938 *
4939 * \param compositor The compositor instance
4940 * \param state The DPMS state the outputs will be set to
4941 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004942static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004943weston_compositor_dpms(struct weston_compositor *compositor,
4944 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004945{
4946 struct weston_output *output;
4947
Bryce Harrington08976ac2016-08-30 12:05:16 -07004948 wl_list_for_each(output, &compositor->output_list, link)
4949 if (output->set_dpms)
4950 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004951}
4952
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004953/** Restores the compositor to active status
4954 *
4955 * \param compositor The compositor instance
4956 *
4957 * If the compositor was in a sleeping mode, all outputs are powered
4958 * back on via DPMS. Otherwise if the compositor was inactive
4959 * (idle/locked, offscreen, or sleeping) then the compositor's wake
4960 * signal will fire.
4961 *
4962 * Restarts the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004963 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004964 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004965WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004966weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004967{
Neil Roberts8b62e202013-09-30 13:14:47 +01004968 uint32_t old_state = compositor->state;
4969
4970 /* The state needs to be changed before emitting the wake
4971 * signal because that may try to schedule a repaint which
4972 * will not work if the compositor is still sleeping */
4973 compositor->state = WESTON_COMPOSITOR_ACTIVE;
4974
4975 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004976 case WESTON_COMPOSITOR_SLEEPING:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004977 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004978 case WESTON_COMPOSITOR_OFFSCREEN:
Daniel Stone893b9362016-11-08 15:47:09 +00004979 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004980 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004981 /* fall through */
4982 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004983 wl_event_source_timer_update(compositor->idle_source,
4984 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004985 }
4986}
4987
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004988/** Turns off rendering and frame events for the compositor.
4989 *
4990 * \param compositor The compositor instance
4991 *
4992 * This is used for example to prevent further rendering while the
4993 * compositor is shutting down.
4994 *
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004995 * Stops the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004996 *
4997 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004998 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004999WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01005000weston_compositor_offscreen(struct weston_compositor *compositor)
5001{
5002 switch (compositor->state) {
5003 case WESTON_COMPOSITOR_OFFSCREEN:
5004 return;
5005 case WESTON_COMPOSITOR_SLEEPING:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01005006 default:
5007 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
5008 wl_event_source_timer_update(compositor->idle_source, 0);
5009 }
5010}
5011
Bryce Harringtonc9626a32015-12-11 13:11:38 -08005012/** Powers down all attached output devices
5013 *
5014 * \param compositor The compositor instance
5015 *
5016 * Causes rendering to the outputs to cease, and no frame events to be
5017 * sent. Only powers down the outputs if the compositor is not already
5018 * in sleep mode.
5019 *
5020 * Stops the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005021 *
5022 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08005023 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01005024WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005025weston_compositor_sleep(struct weston_compositor *compositor)
5026{
5027 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
5028 return;
5029
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01005030 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005031 compositor->state = WESTON_COMPOSITOR_SLEEPING;
5032 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
5033}
5034
Bryce Harringtonc9626a32015-12-11 13:11:38 -08005035/** Sets compositor to idle mode
5036 *
5037 * \param data The compositor instance
5038 *
5039 * This is called when the idle timer fires. Once the compositor is in
5040 * idle mode it requires a wake action (e.g. via
5041 * weston_compositor_wake()) to restore it. The compositor's
5042 * idle_signal will be triggered when the idle event occurs.
5043 *
5044 * Idleness can be inhibited by setting the compositor's idle_inhibit
5045 * property.
5046 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04005047static int
5048idle_handler(void *data)
5049{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005050 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04005051
5052 if (compositor->idle_inhibit)
5053 return 1;
5054
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005055 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005056 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04005057
5058 return 1;
5059}
5060
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005061WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08005062weston_plane_init(struct weston_plane *plane,
5063 struct weston_compositor *ec,
5064 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005065{
5066 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02005067 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005068 plane->x = x;
5069 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08005070 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03005071
5072 /* Init the link so that the call to wl_list_remove() when releasing
5073 * the plane without ever stacking doesn't lead to a crash */
5074 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005075}
5076
5077WL_EXPORT void
5078weston_plane_release(struct weston_plane *plane)
5079{
Xiong Zhang97116532013-10-23 13:58:31 +08005080 struct weston_view *view;
5081
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005082 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02005083 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03005084
Pekka Paalanen6551c092021-05-03 16:09:45 +03005085 /*
5086 * Can't use paint node list here, weston_plane is not specific to an
5087 * output.
5088 */
Xiong Zhang97116532013-10-23 13:58:31 +08005089 wl_list_for_each(view, &plane->compositor->view_list, link) {
5090 if (view->plane == plane)
5091 view->plane = NULL;
5092 }
5093
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03005094 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005095}
5096
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005097/** weston_compositor_stack_plane
5098 * \ingroup compositor
5099 */
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02005100WL_EXPORT void
5101weston_compositor_stack_plane(struct weston_compositor *ec,
5102 struct weston_plane *plane,
5103 struct weston_plane *above)
5104{
5105 if (above)
5106 wl_list_insert(above->link.prev, &plane->link);
5107 else
5108 wl_list_insert(&ec->plane_list, &plane->link);
5109}
5110
Quentin Glidic4ef719c2016-07-05 20:44:33 +02005111static void
5112output_release(struct wl_client *client, struct wl_resource *resource)
5113{
5114 wl_resource_destroy(resource);
5115}
5116
5117static const struct wl_output_interface output_interface = {
5118 output_release,
5119};
5120
5121
Casey Dahlin9074db52012-04-19 22:50:09 -04005122static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04005123{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05005124 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04005125}
5126
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005127static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005128bind_output(struct wl_client *client,
5129 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05005130{
Pekka Paalanen05347622017-03-27 12:24:34 +03005131 struct weston_head *head = data;
5132 struct weston_output *output = head->output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005133 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04005134 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05005135
Jason Ekstranda85118c2013-06-27 20:17:02 -05005136 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06005137 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07005138 if (resource == NULL) {
5139 wl_client_post_no_memory(client);
5140 return;
5141 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04005142
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005143 wl_list_insert(&head->resource_list, wl_resource_get_link(resource));
Pekka Paalanen055c1132017-03-27 16:31:25 +03005144 wl_resource_set_implementation(resource, &output_interface, head,
Pekka Paalanen05347622017-03-27 12:24:34 +03005145 unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04005146
Pekka Paalanen05347622017-03-27 12:24:34 +03005147 assert(output);
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05005148 wl_output_send_geometry(resource,
5149 output->x,
5150 output->y,
Pekka Paalanen01f60212017-03-24 15:39:24 +02005151 head->mm_width,
5152 head->mm_height,
5153 head->subpixel,
5154 head->make, head->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04005155 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04005156 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02005157 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02005158 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005159
5160 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05005161 wl_output_send_mode(resource,
5162 mode->flags,
5163 mode->width,
5164 mode->height,
5165 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005166 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02005167
Jasper St. Pierre0013a292014-08-07 16:43:11 -04005168 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02005169 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05005170}
5171
Pekka Paalanendcac3512017-12-08 14:13:34 +02005172static void
5173weston_head_add_global(struct weston_head *head)
5174{
5175 head->global = wl_global_create(head->compositor->wl_display,
5176 &wl_output_interface, 3,
5177 head, bind_output);
5178}
5179
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02005180/** Remove the global wl_output protocol object
5181 *
5182 * \param head The head whose global to remove.
5183 *
5184 * Also orphans the wl_resources for this head (wl_output).
5185 */
5186static void
5187weston_head_remove_global(struct weston_head *head)
5188{
5189 struct wl_resource *resource, *tmp;
5190
5191 if (head->global)
5192 wl_global_destroy(head->global);
5193 head->global = NULL;
5194
5195 wl_resource_for_each_safe(resource, tmp, &head->resource_list) {
5196 unbind_resource(resource);
5197 wl_resource_set_destructor(resource, NULL);
5198 wl_resource_set_user_data(resource, NULL);
5199 }
Roman Gilge97391c2019-03-29 13:01:06 +01005200
5201 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
5202 /* It's sufficient to unset the destructor, then the list elements
5203 * won't be accessed.
5204 */
5205 wl_resource_set_destructor(resource, NULL);
5206 }
5207 wl_list_init(&head->xdg_output_resource_list);
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02005208}
5209
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005210/** Get the backing object of wl_output
5211 *
5212 * \param resource A wl_output protocol object.
5213 * \return The backing object (user data) of a wl_resource representing a
5214 * wl_output protocol object.
Marius Vlad78984ee2019-06-11 00:05:08 +03005215 *
5216 * \ingroup head
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005217 */
Pekka Paalanen055c1132017-03-27 16:31:25 +03005218WL_EXPORT struct weston_head *
5219weston_head_from_resource(struct wl_resource *resource)
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005220{
5221 assert(wl_resource_instance_of(resource, &wl_output_interface,
5222 &output_interface));
5223
5224 return wl_resource_get_user_data(resource);
5225}
5226
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005227/** Initialize a pre-allocated weston_head
5228 *
5229 * \param head The head to initialize.
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005230 * \param name The head name, e.g. the connector name or equivalent.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005231 *
5232 * The head will be safe to attach, detach and release.
5233 *
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005234 * The name is used in logs, and can be used by compositors as a configuration
5235 * identifier.
5236 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005237 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005238 * \internal
5239 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005240WL_EXPORT void
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005241weston_head_init(struct weston_head *head, const char *name)
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005242{
5243 /* Add some (in)sane defaults which can be used
5244 * for checking if an output was properly configured
5245 */
5246 memset(head, 0, sizeof *head);
5247
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005248 wl_list_init(&head->compositor_link);
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005249 wl_signal_init(&head->destroy_signal);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005250 wl_list_init(&head->output_link);
5251 wl_list_init(&head->resource_list);
Roman Gilge97391c2019-03-29 13:01:06 +01005252 wl_list_init(&head->xdg_output_resource_list);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005253 head->name = strdup(name);
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305254 head->current_protection = WESTON_HDCP_DISABLE;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005255}
5256
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005257/** Send output heads changed signal
5258 *
5259 * \param output The output that changed.
5260 *
5261 * Notify that the enabled output gained and/or lost heads, or that the
5262 * associated heads may have changed their connection status. This does not
5263 * include cases where the output becomes enabled or disabled. The registered
5264 * callbacks are called after the change has successfully happened.
5265 *
5266 * If connection status change causes the compositor to attach or detach a head
5267 * to an enabled output, the registered callbacks may be called multiple times.
Marius Vlad55d87362019-06-11 01:15:35 +03005268 *
5269 * \ingroup output
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005270 */
5271static void
5272weston_output_emit_heads_changed(struct weston_output *output)
5273{
5274 wl_signal_emit(&output->compositor->output_heads_changed_signal,
5275 output);
5276}
5277
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005278/** Idle task for emitting heads_changed_signal */
5279static void
5280weston_compositor_call_heads_changed(void *data)
5281{
5282 struct weston_compositor *compositor = data;
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005283 struct weston_head *head;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005284
5285 compositor->heads_changed_source = NULL;
5286
5287 wl_signal_emit(&compositor->heads_changed_signal, compositor);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005288
5289 wl_list_for_each(head, &compositor->head_list, compositor_link) {
5290 if (head->output && head->output->enabled)
5291 weston_output_emit_heads_changed(head->output);
5292 }
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005293}
5294
5295/** Schedule a call on idle to heads_changed callback
5296 *
5297 * \param compositor The Compositor.
5298 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005299 * \ingroup compositor
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005300 * \internal
5301 */
5302static void
5303weston_compositor_schedule_heads_changed(struct weston_compositor *compositor)
5304{
5305 struct wl_event_loop *loop;
5306
5307 if (compositor->heads_changed_source)
5308 return;
5309
5310 loop = wl_display_get_event_loop(compositor->wl_display);
5311 compositor->heads_changed_source = wl_event_loop_add_idle(loop,
5312 weston_compositor_call_heads_changed, compositor);
5313}
5314
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005315/** Register a new head
5316 *
5317 * \param compositor The compositor.
5318 * \param head The head to register, must not be already registered.
5319 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005320 * This signals the core that a new head has become available, leading to
5321 * heads_changed hook being called later.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005322 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005323 * \ingroup compositor
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005324 * \internal
5325 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005326WL_EXPORT void
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005327weston_compositor_add_head(struct weston_compositor *compositor,
5328 struct weston_head *head)
5329{
5330 assert(wl_list_empty(&head->compositor_link));
5331 assert(head->name);
5332
5333 wl_list_insert(compositor->head_list.prev, &head->compositor_link);
5334 head->compositor = compositor;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005335 weston_compositor_schedule_heads_changed(compositor);
5336}
5337
5338/** Adds a listener to be called when heads change
5339 *
5340 * \param compositor The compositor.
5341 * \param listener The listener to add.
5342 *
Marius Vlada2dace22019-06-12 16:05:44 +03005343 * The listener notify function argument is weston_compositor.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005344 *
5345 * The listener function will be called after heads are added or their
5346 * connection status has changed. Several changes may be accumulated into a
5347 * single call. The user is expected to iterate over the existing heads and
5348 * check their statuses to find out what changed.
5349 *
5350 * \sa weston_compositor_iterate_heads, weston_head_is_connected,
5351 * weston_head_is_enabled
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005352 * \ingroup compositor
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005353 */
5354WL_EXPORT void
5355weston_compositor_add_heads_changed_listener(struct weston_compositor *compositor,
5356 struct wl_listener *listener)
5357{
5358 wl_signal_add(&compositor->heads_changed_signal, listener);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005359}
5360
5361/** Iterate over available heads
5362 *
5363 * \param compositor The compositor.
Marius Vlada2dace22019-06-12 16:05:44 +03005364 * \param iter The iterator, or NULL for start.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005365 * \return The next available head in the list.
5366 *
5367 * Returns all available heads, regardless of being connected or enabled.
5368 *
5369 * You can iterate over all heads as follows:
5370 * \code
5371 * struct weston_head *head = NULL;
5372 *
5373 * while ((head = weston_compositor_iterate_heads(compositor, head))) {
5374 * ...
5375 * }
5376 * \endcode
5377 *
5378 * If you cause \c iter to be removed from the list, you cannot use it to
5379 * continue iterating. Removing any other item is safe.
5380 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005381 * \ingroup compositor
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005382 */
5383WL_EXPORT struct weston_head *
5384weston_compositor_iterate_heads(struct weston_compositor *compositor,
5385 struct weston_head *iter)
5386{
5387 struct wl_list *list = &compositor->head_list;
5388 struct wl_list *node;
5389
5390 assert(compositor);
5391 assert(!iter || iter->compositor == compositor);
5392
5393 if (iter)
5394 node = iter->compositor_link.next;
5395 else
5396 node = list->next;
5397
5398 assert(node);
5399 assert(!iter || node != &iter->compositor_link);
5400
5401 if (node == list)
5402 return NULL;
5403
5404 return container_of(node, struct weston_head, compositor_link);
5405}
5406
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005407/** Iterate over attached heads
5408 *
5409 * \param output The output whose heads to iterate.
Marius Vlada2dace22019-06-12 16:05:44 +03005410 * \param iter The iterator, or NULL for start.
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005411 * \return The next attached head in the list.
5412 *
5413 * Returns all heads currently attached to the output.
5414 *
5415 * You can iterate over all heads as follows:
5416 * \code
5417 * struct weston_head *head = NULL;
5418 *
5419 * while ((head = weston_output_iterate_heads(output, head))) {
5420 * ...
5421 * }
5422 * \endcode
5423 *
5424 * If you cause \c iter to be removed from the list, you cannot use it to
5425 * continue iterating. Removing any other item is safe.
5426 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05005427 * \ingroup output
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005428 */
5429WL_EXPORT struct weston_head *
5430weston_output_iterate_heads(struct weston_output *output,
5431 struct weston_head *iter)
5432{
5433 struct wl_list *list = &output->head_list;
5434 struct wl_list *node;
5435
5436 assert(output);
5437 assert(!iter || iter->output == output);
5438
5439 if (iter)
5440 node = iter->output_link.next;
5441 else
5442 node = list->next;
5443
5444 assert(node);
5445 assert(!iter || node != &iter->output_link);
5446
5447 if (node == list)
5448 return NULL;
5449
5450 return container_of(node, struct weston_head, output_link);
5451}
5452
Pekka Paalanendcac3512017-12-08 14:13:34 +02005453/** Attach a head to an output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005454 *
5455 * \param output The output to attach to.
5456 * \param head The head that is not yet attached.
5457 * \return 0 on success, -1 on failure.
5458 *
5459 * Attaches the given head to the output. All heads of an output are clones
5460 * and share the resolution and timings.
5461 *
5462 * Cloning heads this way uses less resources than creating an output for
5463 * each head, but is not always possible due to environment, driver and hardware
5464 * limitations.
5465 *
5466 * On failure, the head remains unattached. Success of this function does not
5467 * guarantee the output configuration is actually valid. The final checks are
Pekka Paalanendcac3512017-12-08 14:13:34 +02005468 * made on weston_output_enable() unless the output was already enabled.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005469 *
Marius Vlad55d87362019-06-11 01:15:35 +03005470 * \ingroup output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005471 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005472WL_EXPORT int
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005473weston_output_attach_head(struct weston_output *output,
5474 struct weston_head *head)
5475{
Pekka Paalanendcac3512017-12-08 14:13:34 +02005476 char *head_names;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005477
5478 if (!wl_list_empty(&head->output_link))
5479 return -1;
5480
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005481 if (output->attach_head) {
5482 if (output->attach_head(output, head) < 0)
5483 return -1;
5484 } else if (!wl_list_empty(&output->head_list)) {
5485 /* No support for clones in the legacy path. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005486 return -1;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005487 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005488
5489 head->output = output;
5490 wl_list_insert(output->head_list.prev, &head->output_link);
5491
Pekka Paalanendcac3512017-12-08 14:13:34 +02005492 if (output->enabled) {
5493 weston_head_add_global(head);
5494
5495 head_names = weston_output_create_heads_string(output);
5496 weston_log("Output '%s' updated to have head(s) %s\n",
5497 output->name, head_names);
5498 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005499
5500 weston_output_emit_heads_changed(output);
Pekka Paalanendcac3512017-12-08 14:13:34 +02005501 }
5502
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005503 return 0;
5504}
5505
5506/** Detach a head from its output
5507 *
5508 * \param head The head to detach.
5509 *
5510 * It is safe to detach a non-attached head.
5511 *
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005512 * If the head is attached to an enabled output and the output will be left
5513 * with no heads, the output will be disabled.
5514 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005515 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005516 * \sa weston_output_disable
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005517 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005518WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005519weston_head_detach(struct weston_head *head)
5520{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005521 struct weston_output *output = head->output;
Pekka Paalanena0106992017-12-08 16:11:17 +02005522 char *head_names;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005523
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005524 wl_list_remove(&head->output_link);
5525 wl_list_init(&head->output_link);
5526 head->output = NULL;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005527
5528 if (!output)
5529 return;
5530
5531 if (output->detach_head)
5532 output->detach_head(output, head);
5533
5534 if (output->enabled) {
5535 weston_head_remove_global(head);
5536
Pekka Paalanena0106992017-12-08 16:11:17 +02005537 if (wl_list_empty(&output->head_list)) {
5538 weston_log("Output '%s' no heads left, disabling.\n",
5539 output->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005540 weston_output_disable(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02005541 } else {
5542 head_names = weston_output_create_heads_string(output);
5543 weston_log("Output '%s' updated to have head(s) %s\n",
5544 output->name, head_names);
5545 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005546
5547 weston_output_emit_heads_changed(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02005548 }
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005549 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005550}
5551
5552/** Destroy a head
5553 *
5554 * \param head The head to be released.
5555 *
5556 * Destroys the head. The caller is responsible for freeing the memory pointed
5557 * to by \c head.
5558 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005559 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005560 * \internal
5561 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005562WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005563weston_head_release(struct weston_head *head)
5564{
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005565 wl_signal_emit(&head->destroy_signal, head);
5566
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005567 weston_head_detach(head);
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005568
5569 free(head->make);
5570 free(head->model);
5571 free(head->serial_number);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005572 free(head->name);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005573
5574 wl_list_remove(&head->compositor_link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005575}
5576
Pekka Paalanene19970f2017-08-28 14:11:02 +03005577static void
5578weston_head_set_device_changed(struct weston_head *head)
5579{
5580 head->device_changed = true;
5581
5582 if (head->compositor)
5583 weston_compositor_schedule_heads_changed(head->compositor);
5584}
5585
5586/** String equal comparison with NULLs being equal */
5587static bool
5588str_null_eq(const char *a, const char *b)
5589{
5590 if (!a && !b)
5591 return true;
5592
5593 if (!!a != !!b)
5594 return false;
5595
5596 return strcmp(a, b) == 0;
5597}
5598
Pekka Paalanen01f60212017-03-24 15:39:24 +02005599/** Store monitor make, model and serial number
5600 *
5601 * \param head The head to modify.
5602 * \param make The monitor make. If EDID is available, the PNP ID. Otherwise
5603 * any string, or NULL for none.
5604 * \param model The monitor model or name, or a made-up string, or NULL for
5605 * none.
5606 * \param serialno The monitor serial number, a made-up string, or NULL for
5607 * none.
5608 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005609 * This may set the device_changed flag.
5610 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005611 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005612 * \internal
5613 */
5614WL_EXPORT void
5615weston_head_set_monitor_strings(struct weston_head *head,
5616 const char *make,
5617 const char *model,
5618 const char *serialno)
5619{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005620 if (str_null_eq(head->make, make) &&
5621 str_null_eq(head->model, model) &&
5622 str_null_eq(head->serial_number, serialno))
5623 return;
5624
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005625 free(head->make);
5626 free(head->model);
5627 free(head->serial_number);
5628
5629 head->make = make ? strdup(make) : NULL;
5630 head->model = model ? strdup(model) : NULL;
5631 head->serial_number = serialno ? strdup(serialno) : NULL;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005632
5633 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005634}
5635
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005636/** Store display non-desktop status
5637 *
5638 * \param head The head to modify.
5639 * \param non_desktop Whether the head connects to a non-desktop display.
5640 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005641 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005642 * \internal
5643 */
5644WL_EXPORT void
5645weston_head_set_non_desktop(struct weston_head *head, bool non_desktop)
5646{
5647 if (head->non_desktop == non_desktop)
5648 return;
5649
5650 head->non_desktop = non_desktop;
5651
5652 weston_head_set_device_changed(head);
5653}
5654
Lucas Stacha69cb712019-11-25 23:29:31 +00005655/** Store display transformation
5656 *
5657 * \param head The head to modify.
5658 * \param transform The transformation to apply for this head
5659 *
5660 * This may set the device_changed flag.
5661 *
5662 * \ingroup head
5663 * \internal
5664 */
5665WL_EXPORT void
5666weston_head_set_transform(struct weston_head *head, uint32_t transform)
5667{
5668 if (head->transform == transform)
5669 return;
5670
5671 head->transform = transform;
5672
5673 weston_head_set_device_changed(head);
5674}
5675
5676
Pekka Paalanen01f60212017-03-24 15:39:24 +02005677/** Store physical image size
5678 *
5679 * \param head The head to modify.
5680 * \param mm_width Image area width in millimeters.
5681 * \param mm_height Image area height in millimeters.
5682 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005683 * This may set the device_changed flag.
5684 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005685 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005686 * \internal
5687 */
5688WL_EXPORT void
5689weston_head_set_physical_size(struct weston_head *head,
5690 int32_t mm_width, int32_t mm_height)
5691{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005692 if (head->mm_width == mm_width &&
5693 head->mm_height == mm_height)
5694 return;
5695
Pekka Paalanen01f60212017-03-24 15:39:24 +02005696 head->mm_width = mm_width;
5697 head->mm_height = mm_height;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005698
5699 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005700}
5701
5702/** Store monitor sub-pixel layout
5703 *
5704 * \param head The head to modify.
5705 * \param sp Sub-pixel layout. The possible values are:
5706 * - WL_OUTPUT_SUBPIXEL_UNKNOWN,
5707 * - WL_OUTPUT_SUBPIXEL_NONE,
5708 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB,
5709 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR,
5710 * - WL_OUTPUT_SUBPIXEL_VERTICAL_RGB,
5711 * - WL_OUTPUT_SUBPIXEL_VERTICAL_BGR
5712 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005713 * This may set the device_changed flag.
5714 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005715 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005716 * \internal
5717 */
5718WL_EXPORT void
5719weston_head_set_subpixel(struct weston_head *head,
5720 enum wl_output_subpixel sp)
5721{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005722 if (head->subpixel == sp)
5723 return;
5724
Pekka Paalanen01f60212017-03-24 15:39:24 +02005725 head->subpixel = sp;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005726
5727 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005728}
5729
5730/** Mark the monitor as internal
5731 *
5732 * This is used for embedded screens, like laptop panels.
5733 *
5734 * \param head The head to mark as internal.
5735 *
5736 * By default a head is external. The type is often inferred from the physical
5737 * connector type.
5738 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005739 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005740 * \internal
5741 */
5742WL_EXPORT void
5743weston_head_set_internal(struct weston_head *head)
5744{
5745 head->connection_internal = true;
5746}
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005747
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005748/** Store connector status
5749 *
5750 * \param head The head to modify.
5751 * \param connected Whether the head is connected.
5752 *
5753 * Connectors are created as disconnected. This function can be used to
5754 * set the connector status.
5755 *
5756 * The status should be set to true when a physical connector is connected to
5757 * a video sink device like a monitor and to false when the connector is
5758 * disconnected. For nested backends, the connection status should reflect the
5759 * connection to the parent display server.
5760 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005761 * When the connection status changes, it schedules a call to the heads_changed
Pekka Paalanene19970f2017-08-28 14:11:02 +03005762 * hook and sets the device_changed flag.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005763 *
5764 * \sa weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005765 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005766 * \internal
5767 */
5768WL_EXPORT void
5769weston_head_set_connection_status(struct weston_head *head, bool connected)
5770{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005771 if (head->connected == connected)
5772 return;
5773
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005774 head->connected = connected;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005775
Pekka Paalanene19970f2017-08-28 14:11:02 +03005776 weston_head_set_device_changed(head);
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005777}
5778
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305779static void
5780weston_output_compute_protection(struct weston_output *output)
5781{
5782 struct weston_head *head;
5783 enum weston_hdcp_protection op_protection;
5784 bool op_protection_valid = false;
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305785 struct weston_compositor *wc = output->compositor;
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305786
5787 wl_list_for_each(head, &output->head_list, output_link) {
5788 if (!op_protection_valid) {
5789 op_protection = head->current_protection;
5790 op_protection_valid = true;
5791 }
5792 if (head->current_protection < op_protection)
5793 op_protection = head->current_protection;
5794 }
5795
5796 if (!op_protection_valid)
5797 op_protection = WESTON_HDCP_DISABLE;
5798
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305799 if (output->current_protection != op_protection) {
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305800 output->current_protection = op_protection;
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305801 weston_output_damage(output);
Ankit Nautiyalfaa5ab42019-07-08 13:23:24 +05305802 weston_schedule_surface_protection_update(wc);
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305803 }
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305804}
5805
5806WL_EXPORT void
5807weston_head_set_content_protection_status(struct weston_head *head,
5808 enum weston_hdcp_protection status)
5809{
5810 head->current_protection = status;
5811 if (head->output)
5812 weston_output_compute_protection(head->output);
5813}
5814
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005815/** Is the head currently connected?
5816 *
5817 * \param head The head to query.
5818 * \return Connection status.
5819 *
5820 * Returns true if the head is physically connected to a monitor, or in
5821 * case of a nested backend returns true when there is a connection to the
5822 * parent display server.
5823 *
5824 * This is independent from the head being enabled.
5825 *
5826 * \sa weston_head_is_enabled
Marius Vlad78984ee2019-06-11 00:05:08 +03005827 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005828 */
5829WL_EXPORT bool
5830weston_head_is_connected(struct weston_head *head)
5831{
5832 return head->connected;
5833}
5834
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005835/** Is the head currently enabled?
5836 *
5837 * \param head The head to query.
5838 * \return Video status.
5839 *
5840 * Returns true if the head is currently transmitting a video stream.
5841 *
5842 * This is independent of the head being connected.
5843 *
5844 * \sa weston_head_is_connected
Marius Vlad78984ee2019-06-11 00:05:08 +03005845 * \ingroup head
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005846 */
5847WL_EXPORT bool
5848weston_head_is_enabled(struct weston_head *head)
5849{
5850 if (!head->output)
5851 return false;
5852
5853 return head->output->enabled;
5854}
5855
Pekka Paalanene19970f2017-08-28 14:11:02 +03005856/** Has the device information changed?
5857 *
5858 * \param head The head to query.
5859 * \return True if the device information has changed since last reset.
5860 *
5861 * The information about the connected display device, e.g. a monitor, may
5862 * change without being disconnected in between. Changing information
5863 * causes a call to the heads_changed hook.
5864 *
5865 * The information includes make, model, serial number, physical size,
5866 * and sub-pixel type. The connection status is also included.
5867 *
5868 * \sa weston_head_reset_device_changed, weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005869 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03005870 */
5871WL_EXPORT bool
5872weston_head_is_device_changed(struct weston_head *head)
5873{
5874 return head->device_changed;
5875}
5876
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005877/** Does the head represent a non-desktop display?
5878 *
5879 * \param head The head to query.
5880 * \return True if the device is a non-desktop display.
5881 *
5882 * Non-desktop heads are not attached to outputs by default.
5883 * This stops weston from extending the desktop onto head mounted displays.
5884 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005885 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005886 */
5887WL_EXPORT bool
5888weston_head_is_non_desktop(struct weston_head *head)
5889{
5890 return head->non_desktop;
5891}
5892
Pekka Paalanene19970f2017-08-28 14:11:02 +03005893/** Acknowledge device information change
5894 *
5895 * \param head The head to acknowledge.
5896 *
5897 * Clears the device changed flag on this head. When a compositor has processed
5898 * device information, it should call this to be able to notice further
5899 * changes.
5900 *
5901 * \sa weston_head_is_device_changed
Marius Vlad78984ee2019-06-11 00:05:08 +03005902 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03005903 */
5904WL_EXPORT void
5905weston_head_reset_device_changed(struct weston_head *head)
5906{
5907 head->device_changed = false;
5908}
5909
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005910/** Get the name of a head
5911 *
5912 * \param head The head to query.
5913 * \return The head's name, not NULL.
5914 *
5915 * The name depends on the backend. The DRM backend uses connector names,
5916 * other backends may use hardcoded names or user-given names.
Marius Vlad78984ee2019-06-11 00:05:08 +03005917 *
5918 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005919 */
5920WL_EXPORT const char *
5921weston_head_get_name(struct weston_head *head)
5922{
5923 return head->name;
5924}
5925
5926/** Get the output the head is attached to
5927 *
5928 * \param head The head to query.
5929 * \return The output the head is attached to, or NULL if detached.
Marius Vlad78984ee2019-06-11 00:05:08 +03005930 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005931 */
5932WL_EXPORT struct weston_output *
5933weston_head_get_output(struct weston_head *head)
5934{
5935 return head->output;
5936}
5937
Lucas Stacha69cb712019-11-25 23:29:31 +00005938/** Get the head's native transformation
5939 *
5940 * \param head The head to query.
5941 * \return The head's native transform, as a WL_OUTPUT_TRANSFORM_* value
5942 *
5943 * A weston_head may have a 'native' transform provided by the backend.
5944 * Examples include panels which are physically rotated, where the rotation
5945 * is recorded and described as part of the system configuration. This call
5946 * will return any known native transform for the head.
5947 *
5948 * \ingroup head
5949 */
5950WL_EXPORT uint32_t
5951weston_head_get_transform(struct weston_head *head)
5952{
5953 return head->transform;
5954}
5955
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005956/** Add destroy callback for a head
5957 *
5958 * \param head The head to watch for.
5959 * \param listener The listener to add. The \c notify member must be set.
5960 *
5961 * Heads may get destroyed for various reasons by the backends. If a head is
5962 * attached to an output, the compositor should listen for head destruction
5963 * and reconfigure or destroy the output if necessary.
5964 *
5965 * The destroy callbacks will be called on weston_head destruction before any
5966 * automatic detaching from an associated weston_output and before any
5967 * weston_head information is lost.
5968 *
5969 * The \c data argument to the notify callback is the weston_head being
5970 * destroyed.
Marius Vlad78984ee2019-06-11 00:05:08 +03005971 *
5972 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005973 */
5974WL_EXPORT void
5975weston_head_add_destroy_listener(struct weston_head *head,
5976 struct wl_listener *listener)
5977{
5978 wl_signal_add(&head->destroy_signal, listener);
5979}
5980
5981/** Look up destroy listener for a head
5982 *
5983 * \param head The head to query.
5984 * \param notify The notify function used used for the added destroy listener.
5985 * \return The listener, or NULL if not found.
5986 *
5987 * This looks up the previously added destroy listener struct based on the
5988 * notify function it has. The listener can be used to access user data
5989 * through \c container_of().
5990 *
5991 * \sa wl_signal_get()
Marius Vlad78984ee2019-06-11 00:05:08 +03005992 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005993 */
5994WL_EXPORT struct wl_listener *
5995weston_head_get_destroy_listener(struct weston_head *head,
5996 wl_notify_func_t notify)
5997{
5998 return wl_signal_get(&head->destroy_signal, notify);
5999}
6000
David Fort0de859e2016-05-27 23:22:57 +02006001/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02006002static void
David Fort0de859e2016-05-27 23:22:57 +02006003weston_compositor_reflow_outputs(struct weston_compositor *compositor,
6004 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02006005{
6006 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02006007 bool start_resizing = false;
6008
6009 if (!delta_width)
6010 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02006011
6012 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02006013 if (output == resized_output) {
6014 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02006015 continue;
6016 }
6017
David Fort0de859e2016-05-27 23:22:57 +02006018 if (start_resizing) {
6019 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02006020 output->dirty = 1;
6021 }
6022 }
6023}
6024
Pekka Paalanen06920792020-12-08 15:22:36 +02006025/** Transform a region in-place from global to output coordinates
6026 *
6027 * \param output The output that defines the transformation.
6028 * \param region The region to be transformed in-place.
6029 *
6030 * This takes a region in the global coordinate system, and takes into account
6031 * output position, transform and scale, and the zoom, and converts the region
6032 * into output pixel coordinates in the framebuffer.
6033 *
6034 * Uses floating-point operations if zoom is active, which may round to expand
6035 * the region.
6036 *
6037 * \internal
6038 * \ingroup output
6039 */
6040WL_EXPORT void
6041weston_output_region_from_global(struct weston_output *output,
6042 pixman_region32_t *region)
6043{
6044 if (output->zoom.active) {
6045 weston_matrix_transform_region(region, &output->matrix, region);
6046 } else {
6047 pixman_region32_translate(region, -output->x, -output->y);
6048 weston_transformed_region(output->width, output->height,
6049 output->transform,
6050 output->current_scale,
6051 region, region);
6052 }
6053}
6054
Pekka Paalanend72bad22017-03-29 17:01:41 +03006055static void
Scott Moreauccbf29d2012-02-22 14:21:41 -07006056weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01006057{
Scott Moreau850ca422012-05-21 15:21:25 -06006058 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05006059
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006060 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05006061 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06006062
Scott Moreauccbf29d2012-02-22 14:21:41 -07006063 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06006064 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05006065 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01006066 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05006067 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01006068 weston_matrix_scale(&output->matrix, magnification,
6069 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07006070 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04006071
Jason Ekstrandfb23df72014-10-16 10:55:21 -05006072 switch (output->transform) {
6073 case WL_OUTPUT_TRANSFORM_FLIPPED:
6074 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
6075 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
6076 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
6077 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
6078 weston_matrix_scale(&output->matrix, -1, 1, 1);
6079 break;
6080 }
6081
6082 switch (output->transform) {
6083 default:
6084 case WL_OUTPUT_TRANSFORM_NORMAL:
6085 case WL_OUTPUT_TRANSFORM_FLIPPED:
6086 break;
6087 case WL_OUTPUT_TRANSFORM_90:
6088 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +02006089 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
6090 weston_matrix_rotate_xy(&output->matrix, 0, -1);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05006091 break;
6092 case WL_OUTPUT_TRANSFORM_180:
6093 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
6094 weston_matrix_translate(&output->matrix,
6095 -output->width, -output->height, 0);
6096 weston_matrix_rotate_xy(&output->matrix, -1, 0);
6097 break;
6098 case WL_OUTPUT_TRANSFORM_270:
6099 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +02006100 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
6101 weston_matrix_rotate_xy(&output->matrix, 0, 1);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05006102 break;
6103 }
6104
6105 if (output->current_scale != 1)
6106 weston_matrix_scale(&output->matrix,
6107 output->current_scale,
6108 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01006109
Scott Moreauccbf29d2012-02-22 14:21:41 -07006110 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05006111
6112 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07006113}
6114
Scott Moreau1bad5db2012-08-18 01:04:05 -06006115static void
Alexander Larsson0b135062013-05-28 16:23:36 +02006116weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06006117{
6118 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03006119 output->native_scale = scale;
6120 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06006121
Pekka Paalanen59987fa2016-04-26 15:50:59 +03006122 convert_size_by_transform_scale(&output->width, &output->height,
6123 output->current_mode->width,
6124 output->current_mode->height,
6125 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02006126}
6127
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006128static void
6129weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07006130{
6131 output->x = x;
6132 output->y = y;
6133
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006134 pixman_region32_fini(&output->region);
Scott Moreauccbf29d2012-02-22 14:21:41 -07006135 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06006136 output->width,
6137 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01006138}
6139
Marius Vlad55d87362019-06-11 01:15:35 +03006140/**
6141 * \ingroup output
6142 */
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006143WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006144weston_output_move(struct weston_output *output, int x, int y)
6145{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006146 struct weston_head *head;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006147 struct wl_resource *resource;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006148 int ver;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006149
6150 output->move_x = x - output->x;
6151 output->move_y = y - output->y;
6152
6153 if (output->move_x == 0 && output->move_y == 0)
6154 return;
6155
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006156 weston_output_init_geometry(output, x, y);
6157
6158 output->dirty = 1;
6159
6160 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006161 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006162
6163 /* Notify clients of the change for output position. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006164 wl_list_for_each(head, &output->head_list, output_link) {
6165 wl_resource_for_each(resource, &head->resource_list) {
6166 wl_output_send_geometry(resource,
6167 output->x,
6168 output->y,
6169 head->mm_width,
6170 head->mm_height,
6171 head->subpixel,
6172 head->make,
6173 head->model,
6174 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08006175
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006176 ver = wl_resource_get_version(resource);
6177 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
6178 wl_output_send_done(resource);
6179 }
Roman Gilge97391c2019-03-29 13:01:06 +01006180
6181 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
6182 zxdg_output_v1_send_logical_position(resource,
6183 output->x,
6184 output->y);
6185 zxdg_output_v1_send_done(resource);
6186 }
Quanxian Wangb2c86362014-03-14 09:16:25 +08006187 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006188}
6189
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006190/** Signal that a pending output is taken into use.
6191 *
6192 * Removes the output from the pending list and adds it to the compositor's
6193 * list of enabled outputs. The output created signal is emitted.
Giulio Camuffob1147152015-05-06 21:41:57 +03006194 *
Pekka Paalanen2210ad02017-03-30 15:48:06 +03006195 * The output gets an internal ID assigned, and the wl_output global is
6196 * created.
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03006197 *
Giulio Camuffob1147152015-05-06 21:41:57 +03006198 * \param compositor The compositor instance.
6199 * \param output The output to be added.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006200 *
6201 * \internal
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006202 * \ingroup compositor
Giulio Camuffob1147152015-05-06 21:41:57 +03006203 */
Pekka Paalanenf9681b52017-03-29 16:58:48 +03006204static void
Giulio Camuffob1147152015-05-06 21:41:57 +03006205weston_compositor_add_output(struct weston_compositor *compositor,
6206 struct weston_output *output)
6207{
Armin Krezoviće5403842016-08-05 15:28:29 +02006208 struct weston_view *view, *next;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03006209 struct weston_head *head;
Armin Krezoviće5403842016-08-05 15:28:29 +02006210
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006211 assert(!output->enabled);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03006212
6213 /* Verify we haven't reached the limit of 32 available output IDs */
6214 assert(ffs(~compositor->output_id_pool) > 0);
6215
6216 /* Invert the output id pool and look for the lowest numbered
6217 * switch (the least significant bit). Take that bit's position
6218 * as our ID, and mark it used in the compositor's output_id_pool.
6219 */
6220 output->id = ffs(~compositor->output_id_pool) - 1;
6221 compositor->output_id_pool |= 1u << output->id;
6222
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006223 wl_list_remove(&output->link);
Giulio Camuffob1147152015-05-06 21:41:57 +03006224 wl_list_insert(compositor->output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006225 output->enabled = true;
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006226
Pekka Paalanendcac3512017-12-08 14:13:34 +02006227 wl_list_for_each(head, &output->head_list, output_link)
6228 weston_head_add_global(head);
Pekka Paalanen2210ad02017-03-30 15:48:06 +03006229
Giulio Camuffob1147152015-05-06 21:41:57 +03006230 wl_signal_emit(&compositor->output_created_signal, output);
Armin Krezoviće5403842016-08-05 15:28:29 +02006231
Pekka Paalanen6551c092021-05-03 16:09:45 +03006232 /*
6233 * Use view_list, as paint nodes have not been created for this
6234 * output yet. Any existing view might touch this new output.
6235 */
Armin Krezoviće5403842016-08-05 15:28:29 +02006236 wl_list_for_each_safe(view, next, &compositor->view_list, link)
6237 weston_view_geometry_dirty(view);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04006238}
6239
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006240/** Transform device coordinates into global coordinates
6241 *
Marius Vlada2dace22019-06-12 16:05:44 +03006242 * \param output the weston_output object
6243 * \param[in] device_x X coordinate in device units.
6244 * \param[in] device_y Y coordinate in device units.
6245 * \param[out] x X coordinate in the global space.
6246 * \param[out] y Y coordinate in the global space.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006247 *
Marius Vlada2dace22019-06-12 16:05:44 +03006248 * Transforms coordinates from the device coordinate space (physical pixel
6249 * units) to the global coordinate space (logical pixel units). This takes
6250 * into account output transform and scale.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006251 *
Marius Vlad55d87362019-06-11 01:15:35 +03006252 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006253 * \internal
6254 */
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006255WL_EXPORT void
6256weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02006257 double device_x, double device_y,
6258 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006259{
Derek Foreman0f679412014-10-02 13:41:17 -05006260 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02006261 device_x,
6262 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05006263 0.0,
6264 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006265
Derek Foreman67a18b92015-03-24 11:36:14 -05006266 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006267
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02006268 *x = p.f[0] / p.f[3];
6269 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006270}
6271
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006272/** Removes output from compositor's list of enabled outputs
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006273 *
6274 * \param output The weston_output object that is being removed.
6275 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006276 * The following happens:
6277 *
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03006278 * - Destroys all paint nodes related to the output.
6279 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006280 * - The output assignments of all views in the current scenegraph are
6281 * recomputed.
6282 *
6283 * - Presentation feedback is discarded.
6284 *
6285 * - Compositor is notified that outputs were changed and
6286 * applies the necessary changes to re-layout outputs.
6287 *
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006288 * - The output is put back in the pending outputs list.
6289 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006290 * - Signal is emitted to notify all users of the weston_output
6291 * object that the output is being destroyed.
6292 *
6293 * - wl_output protocol objects referencing this weston_output
Pekka Paalanen2210ad02017-03-30 15:48:06 +03006294 * are made inert, and the wl_output global is removed.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006295 *
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03006296 * - The output's internal ID is released.
6297 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006298 * \ingroup compositor
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006299 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006300 */
6301static void
6302weston_compositor_remove_output(struct weston_output *output)
6303{
Pekka Paalanenbccda712017-03-29 16:16:04 +03006304 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03006305 struct weston_paint_node *pnode, *pntmp;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006306 struct weston_view *view;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03006307 struct weston_head *head;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006308
6309 assert(output->destroying);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006310 assert(output->enabled);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006311
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03006312 wl_list_for_each_safe(pnode, pntmp,
6313 &output->paint_node_list, output_link) {
6314 weston_paint_node_destroy(pnode);
6315 }
Pekka Paalanen2fddc532021-04-30 17:41:29 +03006316 assert(wl_list_empty(&output->paint_node_z_order_list));
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03006317
Pekka Paalanen6551c092021-05-03 16:09:45 +03006318 /*
6319 * Use view_list in case the output did not go through repaint
6320 * after a view came on it, lacking a paint node. Just to be sure.
6321 */
Pekka Paalanenbccda712017-03-29 16:16:04 +03006322 wl_list_for_each(view, &compositor->view_list, link) {
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006323 if (view->output_mask & (1u << output->id))
6324 weston_view_assign_output(view);
6325 }
6326
6327 weston_presentation_feedback_discard_list(&output->feedback_list);
6328
Pekka Paalanen9711fd92018-06-21 14:26:18 +03006329 weston_compositor_reflow_outputs(compositor, output, -output->width);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006330
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006331 wl_list_remove(&output->link);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006332 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006333 output->enabled = false;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006334
Pekka Paalanenbccda712017-03-29 16:16:04 +03006335 wl_signal_emit(&compositor->output_destroyed_signal, output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006336 wl_signal_emit(&output->destroy_signal, output);
6337
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02006338 wl_list_for_each(head, &output->head_list, output_link)
6339 weston_head_remove_global(head);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03006340
6341 compositor->output_id_pool &= ~(1u << output->id);
6342 output->id = 0xffffffff; /* invalid */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006343}
6344
6345/** Sets the output scale for a given output.
6346 *
6347 * \param output The weston_output object that the scale is set for.
6348 * \param scale Scale factor for the given output.
6349 *
6350 * It only supports setting scale for an output that
6351 * is not enabled and it can only be ran once.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006352 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05006353 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006354 */
6355WL_EXPORT void
6356weston_output_set_scale(struct weston_output *output,
6357 int32_t scale)
6358{
6359 /* We can only set scale on a disabled output */
6360 assert(!output->enabled);
6361
6362 /* We only want to set scale once */
6363 assert(!output->scale);
6364
6365 output->scale = scale;
6366}
6367
6368/** Sets the output transform for a given output.
6369 *
6370 * \param output The weston_output object that the transform is set for.
6371 * \param transform Transform value for the given output.
6372 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006373 * Refer to wl_output::transform section located at
6374 * https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_output
6375 * for list of values that can be passed to this function.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006376 *
Marius Vlad55d87362019-06-11 01:15:35 +03006377 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006378 */
6379WL_EXPORT void
6380weston_output_set_transform(struct weston_output *output,
6381 uint32_t transform)
6382{
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006383 struct weston_pointer_motion_event ev;
6384 struct wl_resource *resource;
6385 struct weston_seat *seat;
6386 pixman_region32_t old_region;
6387 int mid_x, mid_y;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006388 struct weston_head *head;
6389 int ver;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006390
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006391 if (!output->enabled && output->transform == UINT32_MAX) {
6392 output->transform = transform;
6393 return;
6394 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006395
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006396 weston_output_transform_scale_init(output, transform, output->scale);
6397
6398 pixman_region32_init(&old_region);
6399 pixman_region32_copy(&old_region, &output->region);
6400
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006401 weston_output_init_geometry(output, output->x, output->y);
6402
6403 output->dirty = 1;
6404
6405 /* Notify clients of the change for output transform. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006406 wl_list_for_each(head, &output->head_list, output_link) {
6407 wl_resource_for_each(resource, &head->resource_list) {
6408 wl_output_send_geometry(resource,
6409 output->x,
6410 output->y,
6411 head->mm_width,
6412 head->mm_height,
6413 head->subpixel,
6414 head->make,
6415 head->model,
6416 output->transform);
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006417
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006418 ver = wl_resource_get_version(resource);
6419 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
6420 wl_output_send_done(resource);
6421 }
Roman Gilge97391c2019-03-29 13:01:06 +01006422 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
6423 zxdg_output_v1_send_logical_position(resource,
6424 output->x,
6425 output->y);
6426 zxdg_output_v1_send_logical_size(resource,
6427 output->width,
6428 output->height);
6429 zxdg_output_v1_send_done(resource);
6430 }
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006431 }
6432
6433 /* we must ensure that pointers are inside output, otherwise they disappear */
6434 mid_x = output->x + output->width / 2;
6435 mid_y = output->y + output->height / 2;
6436
6437 ev.mask = WESTON_POINTER_MOTION_ABS;
6438 ev.x = wl_fixed_to_double(wl_fixed_from_int(mid_x));
6439 ev.y = wl_fixed_to_double(wl_fixed_from_int(mid_y));
6440
6441 wl_list_for_each(seat, &output->compositor->seat_list, link) {
6442 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
6443
6444 if (pointer && pixman_region32_contains_point(&old_region,
6445 wl_fixed_to_int(pointer->x),
6446 wl_fixed_to_int(pointer->y),
6447 NULL))
6448 weston_pointer_move(pointer, &ev);
6449 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006450}
6451
Pekka Paalanenb5265af2020-12-02 11:57:58 +02006452/** Make the output use renderer shadow buffer.
6453 *
6454 * \param output The weston_output object to modify.
6455 * \return True on success, false if unsupported.
6456 *
6457 * This can only be set on a disabled output object.
6458 *
6459 * This is a temporary API to demonstrate WESTON_CAP_COLOR_OPS and allow
6460 * testing related features. This will be superseded with color management
6461 * API.
6462 *
6463 * By default, a renderer is not using a shadow buffer of its own. Enabling
6464 * a shadow buffer may enable other color related features.
6465 *
6466 * Support depends on the chosen renderer and the graphics driver stack in use.
6467 *
6468 * \ingroup output
6469 */
6470WL_EXPORT bool
6471weston_output_set_renderer_shadow_buffer(struct weston_output *output)
6472{
6473 struct weston_compositor *compositor = output->compositor;
6474
6475 assert(!output->enabled);
6476
6477 if (compositor->capabilities & WESTON_CAP_COLOR_OPS) {
6478 output->use_renderer_shadow_buffer = true;
6479 return true;
6480 }
6481
6482 return false;
6483}
6484
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006485/** Initializes a weston_output object with enough data so
6486 ** an output can be configured.
6487 *
6488 * \param output The weston_output object to initialize
6489 * \param compositor The compositor instance.
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006490 * \param name Name for the output (the string is copied).
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006491 *
6492 * Sets initial values for fields that are expected to be
6493 * configured either by compositors or backends.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006494 *
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006495 * The name is used in logs, and can be used by compositors as a configuration
6496 * identifier.
6497 *
Marius Vlad55d87362019-06-11 01:15:35 +03006498 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006499 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006500 */
6501WL_EXPORT void
Armin Krezović40087402016-09-30 14:11:12 +02006502weston_output_init(struct weston_output *output,
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006503 struct weston_compositor *compositor,
6504 const char *name)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006505{
6506 output->compositor = compositor;
6507 output->destroying = 0;
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006508 output->name = strdup(name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006509 wl_list_init(&output->link);
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006510 wl_signal_init(&output->user_destroy_signal);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006511 output->enabled = false;
Ankit Nautiyal2690a772019-03-25 17:57:59 +05306512 output->desired_protection = WESTON_HDCP_DISABLE;
Ankit Nautiyal2844f8e2019-04-03 10:14:59 +05306513 output->allow_protection = true;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006514
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006515 wl_list_init(&output->head_list);
6516
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006517 /* Add some (in)sane defaults which can be used
6518 * for checking if an output was properly configured
6519 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006520 output->scale = 0;
6521 /* Can't use -1 on uint32_t and 0 is valid enum value */
6522 output->transform = UINT32_MAX;
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006523
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006524 pixman_region32_init(&output->region);
Pekka Paalanen42704142017-09-06 16:47:52 +03006525 wl_list_init(&output->mode_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006526}
6527
6528/** Adds weston_output object to pending output list.
6529 *
6530 * \param output The weston_output object to add
6531 * \param compositor The compositor instance.
6532 *
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03006533 * The opposite of this operation is built into weston_output_release().
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006534 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006535 * \ingroup compositor
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006536 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006537 */
6538WL_EXPORT void
6539weston_compositor_add_pending_output(struct weston_output *output,
6540 struct weston_compositor *compositor)
6541{
Pekka Paalanene952a012017-03-29 17:14:00 +03006542 assert(output->disable);
6543 assert(output->enable);
6544
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006545 wl_list_remove(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006546 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006547}
6548
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006549/** Create a string with the attached heads' names.
6550 *
6551 * The string must be free()'d.
Marius Vlad55d87362019-06-11 01:15:35 +03006552 *
6553 * \ingroup output
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006554 */
6555static char *
6556weston_output_create_heads_string(struct weston_output *output)
6557{
6558 FILE *fp;
6559 char *str = NULL;
6560 size_t size = 0;
6561 struct weston_head *head;
6562 const char *sep = "";
6563
6564 fp = open_memstream(&str, &size);
6565 if (!fp)
6566 return NULL;
6567
6568 wl_list_for_each(head, &output->head_list, output_link) {
6569 fprintf(fp, "%s%s", sep, head->name);
6570 sep = ", ";
6571 }
6572 fclose(fp);
6573
6574 return str;
6575}
6576
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006577/** Constructs a weston_output object that can be used by the compositor.
6578 *
Pekka Paalanencc201e42017-03-30 15:11:25 +03006579 * \param output The weston_output object that needs to be enabled. Must not
Pekka Paalanenddce54d2017-08-23 16:00:21 +03006580 * be enabled already. Must have at least one head attached.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006581 *
6582 * Output coordinates are calculated and each new output is by default
6583 * assigned to the right of previous one.
6584 *
6585 * Sets up the transformation, zoom, and geometry of the output using
6586 * the properties that need to be configured by the compositor.
6587 *
6588 * Establishes a repaint timer for the output with the relevant display
6589 * object's event loop. See output_repaint_timer_handler().
6590 *
6591 * The output is assigned an ID. Weston can support up to 32 distinct
6592 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
6593 * is referred to and used to find the first available ID number, and
6594 * then this ID is marked as used in output_id_pool.
6595 *
6596 * The output is also assigned a Wayland global with the wl_output
6597 * external interface.
6598 *
6599 * Backend specific function is called to set up the output output.
6600 *
6601 * Output is added to the compositor's output list
6602 *
6603 * If the backend specific function fails, the weston_output object
6604 * is returned to a state it was before calling this function and
6605 * is added to the compositor's pending_output_list in case it needs
6606 * to be reconfigured or just so it can be destroyed at shutdown.
6607 *
6608 * 0 is returned on success, -1 on failure.
Marius Vlad55d87362019-06-11 01:15:35 +03006609 *
6610 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006611 */
6612WL_EXPORT int
6613weston_output_enable(struct weston_output *output)
6614{
Armin Krezović782f5df2016-09-30 14:11:11 +02006615 struct weston_compositor *c = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006616 struct weston_output *iterator;
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03006617 struct weston_head *head;
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006618 char *head_names;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006619 int x = 0, y = 0;
6620
Pekka Paalanencc201e42017-03-30 15:11:25 +03006621 if (output->enabled) {
6622 weston_log("Error: attempt to enable an enabled output '%s'\n",
6623 output->name);
6624 return -1;
6625 }
6626
Pekka Paalanenddce54d2017-08-23 16:00:21 +03006627 if (wl_list_empty(&output->head_list)) {
6628 weston_log("Error: cannot enable output '%s' without heads.\n",
6629 output->name);
6630 return -1;
6631 }
6632
Pekka Paalanen586e1ac2017-09-14 16:17:59 +03006633 if (wl_list_empty(&output->mode_list) || !output->current_mode) {
6634 weston_log("Error: no video mode for output '%s'.\n",
6635 output->name);
6636 return -1;
6637 }
6638
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03006639 wl_list_for_each(head, &output->head_list, output_link) {
6640 assert(head->make);
6641 assert(head->model);
6642 }
6643
Armin Krezović782f5df2016-09-30 14:11:11 +02006644 iterator = container_of(c->output_list.prev,
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006645 struct weston_output, link);
6646
Armin Krezović782f5df2016-09-30 14:11:11 +02006647 if (!wl_list_empty(&c->output_list))
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006648 x = iterator->x + iterator->width;
6649
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006650 /* Make sure the scale is set up */
6651 assert(output->scale);
6652
6653 /* Make sure we have a transform set */
6654 assert(output->transform != UINT32_MAX);
6655
Armin Krezović782f5df2016-09-30 14:11:11 +02006656 output->x = x;
6657 output->y = y;
6658 output->dirty = 1;
6659 output->original_scale = output->scale;
6660
Marius Vlad3a2f8292019-11-04 17:53:46 +02006661 wl_signal_init(&output->frame_signal);
6662 wl_signal_init(&output->destroy_signal);
6663
Armin Krezović782f5df2016-09-30 14:11:11 +02006664 weston_output_transform_scale_init(output, output->transform, output->scale);
6665 weston_output_init_zoom(output);
6666
6667 weston_output_init_geometry(output, x, y);
6668 weston_output_damage(output);
6669
Armin Krezović782f5df2016-09-30 14:11:11 +02006670 wl_list_init(&output->animation_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02006671 wl_list_init(&output->feedback_list);
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03006672 wl_list_init(&output->paint_node_list);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03006673 wl_list_init(&output->paint_node_z_order_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02006674
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006675 /* Enable the output (set up the crtc or create a
6676 * window representing the output, set up the
6677 * renderer, etc)
6678 */
6679 if (output->enable(output) < 0) {
6680 weston_log("Enabling output \"%s\" failed.\n", output->name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006681 return -1;
6682 }
6683
6684 weston_compositor_add_output(output->compositor, output);
6685
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006686 head_names = weston_output_create_heads_string(output);
6687 weston_log("Output '%s' enabled with head(s) %s\n",
6688 output->name, head_names);
6689 free(head_names);
6690
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006691 return 0;
6692}
6693
6694/** Converts a weston_output object to a pending output state, so it
6695 ** can be configured again or destroyed.
6696 *
6697 * \param output The weston_output object that needs to be disabled.
6698 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006699 * Calls a backend specific function to disable an output, in case
6700 * such function exists.
6701 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03006702 * The backend specific disable function may choose to postpone the disabling
6703 * by returning a negative value, in which case this function returns early.
6704 * In that case the backend will guarantee the output will be disabled soon
6705 * by the backend calling this function again. One must not attempt to re-enable
6706 * the output until that happens.
6707 *
6708 * Otherwise, if the output is being used by the compositor, it is removed
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006709 * from weston's output_list (see weston_compositor_remove_output())
6710 * and is returned to a state it was before weston_output_enable()
6711 * was ran (see weston_output_enable_undo()).
6712 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03006713 * See weston_output_init() for more information on the
6714 * state output is returned to.
Pekka Paalanencc201e42017-03-30 15:11:25 +03006715 *
6716 * If the output has never been enabled yet, this function can still be
6717 * called to ensure that the output is actually turned off rather than left
6718 * in the state it was discovered in.
Marius Vlad55d87362019-06-11 01:15:35 +03006719 *
6720 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006721 */
6722WL_EXPORT void
6723weston_output_disable(struct weston_output *output)
6724{
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006725 /* Should we rename this? */
6726 output->destroying = 1;
6727
Pekka Paalanenc65df642017-03-29 15:45:46 +03006728 /* Disable is called unconditionally also for not-enabled outputs,
6729 * because at compositor start-up, if there is an output that is
6730 * already on but the compositor wants to turn it off, we have to
6731 * forward the turn-off to the backend so it knows to do it.
6732 * The backend cannot initially turn off everything, because it
6733 * would cause unnecessary mode-sets for all outputs the compositor
6734 * wants to be on.
6735 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006736 if (output->disable(output) < 0)
6737 return;
6738
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006739 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006740 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006741
6742 output->destroying = 0;
6743}
6744
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006745/** Forces a synchronous call to heads_changed hook
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006746 *
6747 * \param compositor The compositor instance
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006748 *
6749 * If there are new or changed heads, calls the heads_changed hook and
6750 * returns after the hook returns.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006751 *
6752 * \ingroup compositor
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006753 */
6754WL_EXPORT void
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006755weston_compositor_flush_heads_changed(struct weston_compositor *compositor)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006756{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03006757 if (compositor->heads_changed_source) {
6758 wl_event_source_remove(compositor->heads_changed_source);
6759 weston_compositor_call_heads_changed(compositor);
6760 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006761}
6762
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006763/** Add destroy callback for an output
6764 *
6765 * \param output The output to watch.
6766 * \param listener The listener to add. The \c notify member must be set.
6767 *
6768 * The listener callback will be called when user destroys an output. This
6769 * may be delayed by a backend in some cases. The main purpose of the
6770 * listener is to allow hooking up custom data to the output. The custom data
6771 * can be fetched via weston_output_get_destroy_listener() followed by
6772 * container_of().
6773 *
6774 * The \c data argument to the notify callback is the weston_output being
6775 * destroyed.
6776 *
6777 * @note This is for the final destruction of an output, not when it gets
6778 * disabled. If you want to keep track of enabled outputs, this is not it.
Marius Vlad55d87362019-06-11 01:15:35 +03006779 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05006780 * \ingroup output
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006781 */
6782WL_EXPORT void
6783weston_output_add_destroy_listener(struct weston_output *output,
6784 struct wl_listener *listener)
6785{
6786 wl_signal_add(&output->user_destroy_signal, listener);
6787}
6788
6789/** Look up destroy listener for an output
6790 *
6791 * \param output The output to query.
6792 * \param notify The notify function used used for the added destroy listener.
6793 * \return The listener, or NULL if not found.
6794 *
6795 * This looks up the previously added destroy listener struct based on the
6796 * notify function it has. The listener can be used to access user data
6797 * through \c container_of().
6798 *
6799 * \sa wl_signal_get() weston_output_add_destroy_listener()
Marius Vlad55d87362019-06-11 01:15:35 +03006800 * \ingroup output
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006801 */
6802WL_EXPORT struct wl_listener *
6803weston_output_get_destroy_listener(struct weston_output *output,
6804 wl_notify_func_t notify)
6805{
6806 return wl_signal_get(&output->user_destroy_signal, notify);
6807}
6808
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006809/** Uninitialize an output
6810 *
6811 * Removes the output from the list of enabled outputs if necessary, but
6812 * does not call the backend's output disable function. The output will no
6813 * longer be in the list of pending outputs either.
6814 *
6815 * All fields of weston_output become uninitialized, i.e. should not be used
6816 * anymore. The caller can free the memory after this.
6817 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05006818 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006819 * \internal
6820 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006821WL_EXPORT void
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03006822weston_output_release(struct weston_output *output)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006823{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006824 struct weston_head *head, *tmp;
6825
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006826 output->destroying = 1;
6827
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006828 wl_signal_emit(&output->user_destroy_signal, output);
6829
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03006830 if (output->idle_repaint_source)
6831 wl_event_source_remove(output->idle_repaint_source);
6832
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006833 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006834 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006835
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03006836 assert(wl_list_empty(&output->paint_node_list));
6837
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006838 pixman_region32_fini(&output->region);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006839 wl_list_remove(&output->link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006840
6841 wl_list_for_each_safe(head, tmp, &output->head_list, output_link)
6842 weston_head_detach(head);
6843
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006844 free(output->name);
6845}
6846
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006847/** Find an output by its given name
6848 *
6849 * \param compositor The compositor to search in.
6850 * \param name The output name to search for.
6851 * \return An existing output with the given name, or NULL if not found.
6852 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006853 * \ingroup compositor
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006854 */
6855WL_EXPORT struct weston_output *
6856weston_compositor_find_output_by_name(struct weston_compositor *compositor,
6857 const char *name)
6858{
6859 struct weston_output *output;
6860
6861 wl_list_for_each(output, &compositor->output_list, link)
6862 if (strcmp(output->name, name) == 0)
6863 return output;
6864
6865 wl_list_for_each(output, &compositor->pending_output_list, link)
6866 if (strcmp(output->name, name) == 0)
6867 return output;
6868
6869 return NULL;
6870}
6871
6872/** Create a named output
6873 *
6874 * \param compositor The compositor.
6875 * \param name The name for the output.
6876 * \return A new \c weston_output, or NULL on failure.
6877 *
6878 * This creates a new weston_output that starts with no heads attached.
6879 *
6880 * An output must be configured and it must have at least one head before
6881 * it can be enabled.
6882 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006883 * \ingroup compositor
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006884 */
6885WL_EXPORT struct weston_output *
6886weston_compositor_create_output(struct weston_compositor *compositor,
6887 const char *name)
6888{
6889 assert(compositor->backend->create_output);
6890
6891 if (weston_compositor_find_output_by_name(compositor, name)) {
6892 weston_log("Warning: attempted to create an output with a "
6893 "duplicate name '%s'.\n", name);
6894 return NULL;
6895 }
6896
6897 return compositor->backend->create_output(compositor, name);
6898}
6899
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006900/** Create an output for an unused head
6901 *
6902 * \param compositor The compositor.
6903 * \param head The head to attach to the output.
6904 * \return A new \c weston_output, or NULL on failure.
6905 *
6906 * This creates a new weston_output that starts with the given head attached.
6907 * The output inherits the name of the head. The head must not be already
6908 * attached to another output.
6909 *
6910 * An output must be configured before it can be enabled.
6911 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006912 * \ingroup compositor
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006913 */
6914WL_EXPORT struct weston_output *
6915weston_compositor_create_output_with_head(struct weston_compositor *compositor,
6916 struct weston_head *head)
6917{
6918 struct weston_output *output;
6919
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006920 output = weston_compositor_create_output(compositor, head->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006921 if (!output)
6922 return NULL;
6923
6924 if (weston_output_attach_head(output, head) < 0) {
Pekka Paalanen42c0e142017-10-27 12:07:49 +03006925 weston_output_destroy(output);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006926 return NULL;
6927 }
6928
6929 return output;
6930}
6931
6932/** Destroy an output
6933 *
6934 * \param output The output to destroy.
6935 *
6936 * The heads attached to the given output are detached and become unused again.
6937 *
6938 * It is not necessary to explicitly destroy all outputs at compositor exit.
Leandro Ribeiroca640d52020-01-27 19:12:01 -03006939 * weston_compositor_destroy() will automatically destroy any remaining
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006940 * outputs.
6941 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05006942 * \ingroup output
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006943 */
6944WL_EXPORT void
6945weston_output_destroy(struct weston_output *output)
6946{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006947 output->destroy(output);
6948}
6949
Pekka Paalanencf0a4762017-04-04 16:36:07 +03006950/** When you need a head...
6951 *
6952 * This function is a hack, used until all code has been converted to become
6953 * multi-head aware.
6954 *
6955 * \param output The weston_output whose head to get.
6956 * \return The first head in the output's list.
Marius Vlad55d87362019-06-11 01:15:35 +03006957 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05006958 * \ingroup output
Pekka Paalanencf0a4762017-04-04 16:36:07 +03006959 */
6960WL_EXPORT struct weston_head *
6961weston_output_get_first_head(struct weston_output *output)
6962{
6963 if (wl_list_empty(&output->head_list))
6964 return NULL;
6965
6966 return container_of(output->head_list.next,
6967 struct weston_head, output_link);
6968}
6969
Ankit Nautiyal2844f8e2019-04-03 10:14:59 +05306970/** Allow/Disallow content-protection support for an output
6971 *
6972 * This function sets the allow_protection member for an output. Setting of
6973 * this field will allow the compositor to attempt content-protection for this
6974 * output, for a backend that supports the content-protection protocol.
6975 *
6976 * \param output The weston_output for whom the content-protection is to be
6977 * allowed.
6978 * \param allow_protection The bool value which is to be set.
6979 */
6980WL_EXPORT void
6981weston_output_allow_protection(struct weston_output *output,
6982 bool allow_protection)
6983{
6984 output->allow_protection = allow_protection;
6985}
6986
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01006987static void
Roman Gilge97391c2019-03-29 13:01:06 +01006988xdg_output_unlist(struct wl_resource *resource)
6989{
6990 wl_list_remove(wl_resource_get_link(resource));
6991}
6992
6993static void
6994xdg_output_destroy(struct wl_client *client, struct wl_resource *resource)
6995{
6996 wl_resource_destroy(resource);
6997}
6998
6999static const struct zxdg_output_v1_interface xdg_output_interface = {
7000 xdg_output_destroy
7001};
7002
7003static void
7004xdg_output_manager_destroy(struct wl_client *client,
7005 struct wl_resource *resource)
7006{
7007 wl_resource_destroy(resource);
7008}
7009
7010static void
7011xdg_output_manager_get_xdg_output(struct wl_client *client,
7012 struct wl_resource *manager,
7013 uint32_t id,
7014 struct wl_resource *output_resource)
7015{
7016 int version = wl_resource_get_version(manager);
7017 struct weston_head *head = wl_resource_get_user_data(output_resource);
7018 struct weston_output *output = head->output;
7019 struct wl_resource *resource;
7020
7021 resource = wl_resource_create(client, &zxdg_output_v1_interface,
7022 version, id);
7023 if (resource == NULL) {
7024 wl_client_post_no_memory(client);
7025 return;
7026 }
7027
7028 wl_list_insert(&head->xdg_output_resource_list,
7029 wl_resource_get_link(resource));
7030
7031 wl_resource_set_implementation(resource, &xdg_output_interface,
7032 NULL, xdg_output_unlist);
7033
7034 zxdg_output_v1_send_logical_position(resource, output->x, output->y);
7035 zxdg_output_v1_send_logical_size(resource,
7036 output->width,
7037 output->height);
7038 if (version >= ZXDG_OUTPUT_V1_NAME_SINCE_VERSION)
7039 zxdg_output_v1_send_name(resource, head->name);
7040
7041 zxdg_output_v1_send_done(resource);
7042}
7043
7044static const struct zxdg_output_manager_v1_interface xdg_output_manager_interface = {
7045 xdg_output_manager_destroy,
7046 xdg_output_manager_get_xdg_output
7047};
7048
7049static void
7050bind_xdg_output_manager(struct wl_client *client,
7051 void *data, uint32_t version, uint32_t id)
7052{
7053 struct wl_resource *resource;
7054
7055 resource = wl_resource_create(client, &zxdg_output_manager_v1_interface,
7056 version, id);
7057 if (resource == NULL) {
7058 wl_client_post_no_memory(client);
7059 return;
7060 }
7061
7062 wl_resource_set_implementation(resource, &xdg_output_manager_interface,
7063 NULL, NULL);
7064}
7065
7066static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007067destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01007068{
Jonny Lamb74130762013-11-26 18:19:46 +01007069 struct weston_surface *surface =
7070 wl_resource_get_user_data(resource);
7071
Pekka Paalanen4826f872016-04-22 14:14:38 +03007072 if (!surface)
7073 return;
7074
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007075 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02007076 surface->pending.buffer_viewport.buffer.src_width =
7077 wl_fixed_from_int(-1);
7078 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02007079 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01007080}
7081
7082static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007083viewport_destroy(struct wl_client *client,
7084 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01007085{
7086 wl_resource_destroy(resource);
7087}
7088
7089static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007090viewport_set_source(struct wl_client *client,
7091 struct wl_resource *resource,
7092 wl_fixed_t src_x,
7093 wl_fixed_t src_y,
7094 wl_fixed_t src_width,
7095 wl_fixed_t src_height)
7096{
7097 struct weston_surface *surface =
7098 wl_resource_get_user_data(resource);
7099
Pekka Paalanen4826f872016-04-22 14:14:38 +03007100 if (!surface) {
7101 wl_resource_post_error(resource,
7102 WP_VIEWPORT_ERROR_NO_SURFACE,
7103 "wl_surface for this viewport is no longer exists");
7104 return;
7105 }
7106
7107 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03007108 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007109
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007110 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03007111 src_height == wl_fixed_from_int(-1) &&
7112 src_x == wl_fixed_from_int(-1) &&
7113 src_y == wl_fixed_from_int(-1)) {
7114 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007115 surface->pending.buffer_viewport.buffer.src_width =
7116 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02007117 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007118 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007119 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007120
Pekka Paalanen201769a2016-04-26 14:42:11 +03007121 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007122 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007123 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03007124 "wl_surface@%d viewport source "
7125 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
7126 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007127 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03007128 wl_fixed_to_double(src_height),
7129 wl_fixed_to_double(src_x),
7130 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007131 return;
7132 }
7133
7134 surface->pending.buffer_viewport.buffer.src_x = src_x;
7135 surface->pending.buffer_viewport.buffer.src_y = src_y;
7136 surface->pending.buffer_viewport.buffer.src_width = src_width;
7137 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02007138 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007139}
7140
7141static void
7142viewport_set_destination(struct wl_client *client,
7143 struct wl_resource *resource,
7144 int32_t dst_width,
7145 int32_t dst_height)
7146{
7147 struct weston_surface *surface =
7148 wl_resource_get_user_data(resource);
7149
Pekka Paalanen4826f872016-04-22 14:14:38 +03007150 if (!surface) {
7151 wl_resource_post_error(resource,
7152 WP_VIEWPORT_ERROR_NO_SURFACE,
7153 "wl_surface for this viewport no longer exists");
7154 return;
7155 }
7156
7157 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007158
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007159 if (dst_width == -1 && dst_height == -1) {
7160 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007161 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02007162 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007163 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007164 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007165
7166 if (dst_width <= 0 || dst_height <= 0) {
7167 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007168 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007169 "destination size must be positive (%dx%d)",
7170 dst_width, dst_height);
7171 return;
7172 }
7173
7174 surface->pending.buffer_viewport.surface.width = dst_width;
7175 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02007176 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007177}
7178
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007179static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007180 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007181 viewport_set_source,
7182 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01007183};
7184
7185static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007186viewporter_destroy(struct wl_client *client,
7187 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01007188{
7189 wl_resource_destroy(resource);
7190}
7191
7192static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007193viewporter_get_viewport(struct wl_client *client,
7194 struct wl_resource *viewporter,
7195 uint32_t id,
7196 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01007197{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007198 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007199 struct weston_surface *surface =
7200 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01007201 struct wl_resource *resource;
7202
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007203 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007204 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007205 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007206 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01007207 return;
7208 }
7209
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007210 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007211 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01007212 if (resource == NULL) {
7213 wl_client_post_no_memory(client);
7214 return;
7215 }
7216
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007217 wl_resource_set_implementation(resource, &viewport_interface,
7218 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01007219
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007220 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01007221}
7222
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007223static const struct wp_viewporter_interface viewporter_interface = {
7224 viewporter_destroy,
7225 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01007226};
7227
7228static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007229bind_viewporter(struct wl_client *client,
7230 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01007231{
7232 struct wl_resource *resource;
7233
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007234 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06007235 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01007236 if (resource == NULL) {
7237 wl_client_post_no_memory(client);
7238 return;
7239 }
7240
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007241 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01007242 NULL, NULL);
7243}
7244
7245static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04007246destroy_presentation_feedback(struct wl_resource *feedback_resource)
7247{
7248 struct weston_presentation_feedback *feedback;
7249
7250 feedback = wl_resource_get_user_data(feedback_resource);
7251
7252 wl_list_remove(&feedback->link);
7253 free(feedback);
7254}
7255
7256static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007257presentation_destroy(struct wl_client *client, struct wl_resource *resource)
7258{
7259 wl_resource_destroy(resource);
7260}
7261
7262static void
7263presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04007264 struct wl_resource *presentation_resource,
7265 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007266 uint32_t callback)
7267{
Pekka Paalanen133e4392014-09-23 22:08:46 -04007268 struct weston_surface *surface;
7269 struct weston_presentation_feedback *feedback;
7270
7271 surface = wl_resource_get_user_data(surface_resource);
7272
Bryce Harringtonde16d892014-11-20 22:21:57 -08007273 feedback = zalloc(sizeof *feedback);
7274 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04007275 goto err_calloc;
7276
7277 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007278 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04007279 1, callback);
7280 if (!feedback->resource)
7281 goto err_create;
7282
7283 wl_resource_set_implementation(feedback->resource, NULL, feedback,
7284 destroy_presentation_feedback);
7285
7286 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
7287
7288 return;
7289
7290err_create:
7291 free(feedback);
7292
7293err_calloc:
7294 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007295}
7296
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007297static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007298 presentation_destroy,
7299 presentation_feedback
7300};
7301
7302static void
7303bind_presentation(struct wl_client *client,
7304 void *data, uint32_t version, uint32_t id)
7305{
7306 struct weston_compositor *compositor = data;
7307 struct wl_resource *resource;
7308
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007309 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06007310 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007311 if (resource == NULL) {
7312 wl_client_post_no_memory(client);
7313 return;
7314 }
7315
7316 wl_resource_set_implementation(resource, &presentation_implementation,
7317 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007318 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007319}
7320
7321static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05007322compositor_bind(struct wl_client *client,
7323 void *data, uint32_t version, uint32_t id)
7324{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007325 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05007326 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05007327
Jason Ekstranda85118c2013-06-27 20:17:02 -05007328 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06007329 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07007330 if (resource == NULL) {
7331 wl_client_post_no_memory(client);
7332 return;
7333 }
7334
7335 wl_resource_set_implementation(resource, &compositor_interface,
7336 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05007337}
7338
Daniel Stonece62cb32018-07-20 09:46:24 +01007339static const char *
7340output_repaint_status_text(struct weston_output *output)
7341{
7342 switch (output->repaint_status) {
7343 case REPAINT_NOT_SCHEDULED:
7344 return "no repaint";
7345 case REPAINT_BEGIN_FROM_IDLE:
7346 return "start_repaint_loop scheduled";
7347 case REPAINT_SCHEDULED:
7348 return "repaint scheduled";
7349 case REPAINT_AWAITING_COMPLETION:
7350 return "awaiting completion";
7351 }
7352
7353 assert(!"output_repaint_status_text missing enum");
7354 return NULL;
7355}
7356
7357static void
7358debug_scene_view_print_buffer(FILE *fp, struct weston_view *view)
7359{
7360 struct weston_buffer *buffer = view->surface->buffer_ref.buffer;
7361 struct wl_shm_buffer *shm;
7362 struct linux_dmabuf_buffer *dmabuf;
Marius Vlad00a6e012018-11-20 17:52:31 +02007363 const struct pixel_format_info *pixel_info = NULL;
Daniel Stonece62cb32018-07-20 09:46:24 +01007364
7365 if (!buffer) {
7366 fprintf(fp, "\t\t[buffer not available]\n");
7367 return;
7368 }
7369
7370 shm = wl_shm_buffer_get(buffer->resource);
7371 if (shm) {
Marius Vlad00a6e012018-11-20 17:52:31 +02007372 uint32_t _format = wl_shm_buffer_get_format(shm);
7373 pixel_info = pixel_format_get_info_shm(_format);
Daniel Stonece62cb32018-07-20 09:46:24 +01007374 fprintf(fp, "\t\tSHM buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02007375 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
7376 (unsigned long) _format,
7377 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Daniel Stonece62cb32018-07-20 09:46:24 +01007378 return;
7379 }
7380
7381 dmabuf = linux_dmabuf_buffer_get(buffer->resource);
7382 if (dmabuf) {
Marius Vlad00a6e012018-11-20 17:52:31 +02007383 pixel_info = pixel_format_get_info(dmabuf->attributes.format);
Daniel Stonece62cb32018-07-20 09:46:24 +01007384 fprintf(fp, "\t\tdmabuf buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02007385 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
7386 (unsigned long) dmabuf->attributes.format,
7387 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Daniel Stonece62cb32018-07-20 09:46:24 +01007388 fprintf(fp, "\t\t\tmodifier: 0x%llx\n",
7389 (unsigned long long) dmabuf->attributes.modifier[0]);
7390 return;
7391 }
7392
Marius Vlad253ba9a2019-03-17 18:22:21 +02007393 fprintf(fp, "\t\tEGL buffer\n");
Daniel Stonece62cb32018-07-20 09:46:24 +01007394}
7395
7396static void
7397debug_scene_view_print(FILE *fp, struct weston_view *view, int view_idx)
7398{
7399 struct weston_compositor *ec = view->surface->compositor;
7400 struct weston_output *output;
7401 char desc[512];
7402 pixman_box32_t *box;
7403 uint32_t surface_id = 0;
7404 pid_t pid = 0;
7405
7406 if (view->surface->resource) {
7407 struct wl_resource *resource = view->surface->resource;
7408 wl_client_get_credentials(wl_resource_get_client(resource),
7409 &pid, NULL, NULL);
7410 surface_id = wl_resource_get_id(view->surface->resource);
7411 }
7412
7413 if (!view->surface->get_label ||
7414 view->surface->get_label(view->surface, desc, sizeof(desc)) < 0) {
7415 strcpy(desc, "[no description available]");
7416 }
7417 fprintf(fp, "\tView %d (role %s, PID %d, surface ID %u, %s, %p):\n",
7418 view_idx, view->surface->role_name, pid, surface_id,
7419 desc, view);
7420
7421 box = pixman_region32_extents(&view->transform.boundingbox);
7422 fprintf(fp, "\t\tposition: (%d, %d) -> (%d, %d)\n",
7423 box->x1, box->y1, box->x2, box->y2);
7424 box = pixman_region32_extents(&view->transform.opaque);
7425
Michael Olbrichb7e5f102020-08-11 16:33:35 +02007426 if (weston_view_is_opaque(view, &view->transform.boundingbox)) {
Daniel Stonece62cb32018-07-20 09:46:24 +01007427 fprintf(fp, "\t\t[fully opaque]\n");
7428 } else if (!pixman_region32_not_empty(&view->transform.opaque)) {
7429 fprintf(fp, "\t\t[not opaque]\n");
7430 } else {
7431 fprintf(fp, "\t\t[opaque: (%d, %d) -> (%d, %d)]\n",
7432 box->x1, box->y1, box->x2, box->y2);
7433 }
7434
7435 if (view->alpha < 1.0)
7436 fprintf(fp, "\t\talpha: %f\n", view->alpha);
7437
7438 if (view->output_mask != 0) {
7439 bool first_output = true;
7440 fprintf(fp, "\t\toutputs: ");
7441 wl_list_for_each(output, &ec->output_list, link) {
7442 if (!(view->output_mask & (1 << output->id)))
7443 continue;
7444 fprintf(fp, "%s%d (%s)%s",
7445 (first_output) ? "" : ", ",
7446 output->id, output->name,
7447 (view->output == output) ? " (primary)" : "");
7448 first_output = false;
7449 }
7450 } else {
7451 fprintf(fp, "\t\t[no outputs]");
7452 }
7453
7454 fprintf(fp, "\n");
7455
7456 debug_scene_view_print_buffer(fp, view);
7457}
7458
Marius Vlad433f4e72019-02-17 22:14:23 +02007459static void
7460debug_scene_view_print_tree(struct weston_view *view,
Marius Vlada6acfa82019-03-17 18:10:09 +02007461 FILE *fp, int *view_idx)
Marius Vlad433f4e72019-02-17 22:14:23 +02007462{
7463 struct weston_subsurface *sub;
7464 struct weston_view *ev;
7465
7466 /*
7467 * print the view first, then we recursively go on printing
7468 * sub-surfaces. We bail out once no more sub-surfaces are available.
7469 */
Marius Vlada6acfa82019-03-17 18:10:09 +02007470 debug_scene_view_print(fp, view, *view_idx);
Marius Vlad433f4e72019-02-17 22:14:23 +02007471
7472 /* no more sub-surfaces */
7473 if (wl_list_empty(&view->surface->subsurface_list))
7474 return;
7475
7476 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
7477 wl_list_for_each(ev, &sub->surface->views, surface_link) {
Michael Olbrichef5f3232020-04-29 09:03:15 +02007478 /* only print the child views of the current view */
7479 if (ev->parent_view != view)
Marius Vlad433f4e72019-02-17 22:14:23 +02007480 continue;
Marius Vlada6acfa82019-03-17 18:10:09 +02007481
7482 (*view_idx)++;
Marius Vlad433f4e72019-02-17 22:14:23 +02007483 debug_scene_view_print_tree(ev, fp, view_idx);
7484 }
7485 }
7486}
7487
Daniel Stonece62cb32018-07-20 09:46:24 +01007488/**
7489 * Output information on how libweston is currently composing the scene
7490 * graph.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007491 *
7492 * \ingroup compositor
Daniel Stonece62cb32018-07-20 09:46:24 +01007493 */
7494WL_EXPORT char *
7495weston_compositor_print_scene_graph(struct weston_compositor *ec)
7496{
7497 struct weston_output *output;
7498 struct weston_layer *layer;
7499 struct timespec now;
7500 int layer_idx = 0;
7501 FILE *fp;
7502 char *ret;
7503 size_t len;
7504 int err;
7505
7506 fp = open_memstream(&ret, &len);
7507 assert(fp);
7508
7509 weston_compositor_read_presentation_clock(ec, &now);
7510 fprintf(fp, "Weston scene graph at %ld.%09ld:\n\n",
7511 now.tv_sec, now.tv_nsec);
7512
7513 wl_list_for_each(output, &ec->output_list, link) {
7514 struct weston_head *head;
7515 int head_idx = 0;
7516
7517 fprintf(fp, "Output %d (%s):\n", output->id, output->name);
7518 assert(output->enabled);
7519
7520 fprintf(fp, "\tposition: (%d, %d) -> (%d, %d)\n",
7521 output->x, output->y,
7522 output->x + output->width,
7523 output->y + output->height);
7524 fprintf(fp, "\tmode: %dx%d@%.3fHz\n",
7525 output->current_mode->width,
7526 output->current_mode->height,
7527 output->current_mode->refresh / 1000.0);
7528 fprintf(fp, "\tscale: %d\n", output->scale);
7529
7530 fprintf(fp, "\trepaint status: %s\n",
7531 output_repaint_status_text(output));
7532 if (output->repaint_status == REPAINT_SCHEDULED)
7533 fprintf(fp, "\tnext repaint: %ld.%09ld\n",
7534 output->next_repaint.tv_sec,
7535 output->next_repaint.tv_nsec);
7536
7537 wl_list_for_each(head, &output->head_list, output_link) {
7538 fprintf(fp, "\tHead %d (%s): %sconnected\n",
7539 head_idx++, head->name,
7540 (head->connected) ? "" : "not ");
7541 }
7542 }
7543
7544 fprintf(fp, "\n");
7545
7546 wl_list_for_each(layer, &ec->layer_list, link) {
7547 struct weston_view *view;
7548 int view_idx = 0;
7549
7550 fprintf(fp, "Layer %d (pos 0x%lx):\n", layer_idx++,
7551 (unsigned long) layer->position);
7552
7553 if (!weston_layer_mask_is_infinite(layer)) {
7554 fprintf(fp, "\t[mask: (%d, %d) -> (%d,%d)]\n\n",
7555 layer->mask.x1, layer->mask.y1,
7556 layer->mask.x2, layer->mask.y2);
7557 }
7558
Marius Vlada6acfa82019-03-17 18:10:09 +02007559 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
7560 debug_scene_view_print_tree(view, fp, &view_idx);
7561 view_idx++;
7562 }
Daniel Stonece62cb32018-07-20 09:46:24 +01007563
7564 if (wl_list_empty(&layer->view_list.link))
7565 fprintf(fp, "\t[no views]\n");
7566
7567 fprintf(fp, "\n");
7568 }
7569
7570 err = fclose(fp);
7571 assert(err == 0);
7572
7573 return ret;
7574}
7575
7576/**
7577 * Called when the 'scene-graph' debug scope is bound by a client. This
7578 * one-shot weston-debug scope prints the current scene graph when bound,
7579 * and then terminates the stream.
7580 */
7581static void
Marius Vladdad882a2019-07-17 15:43:53 +03007582debug_scene_graph_cb(struct weston_log_subscription *sub, void *data)
Daniel Stonece62cb32018-07-20 09:46:24 +01007583{
7584 struct weston_compositor *ec = data;
7585 char *str = weston_compositor_print_scene_graph(ec);
7586
Marius Vladdad882a2019-07-17 15:43:53 +03007587 weston_log_subscription_printf(sub, "%s", str);
Daniel Stonece62cb32018-07-20 09:46:24 +01007588 free(str);
Marius Vladdad882a2019-07-17 15:43:53 +03007589 weston_log_subscription_complete(sub);
Daniel Stonece62cb32018-07-20 09:46:24 +01007590}
7591
Leandro Ribeiro32a5acd2020-10-19 16:06:22 -03007592/** Retrieve testsuite data from compositor
7593 *
7594 * The testsuite data can be defined by the test suite of projects that uses
7595 * libweston and given to the compositor at the moment of its creation. This
7596 * function should be used when we need to retrieve the testsuite private data
7597 * from the compositor.
7598 *
7599 * \param ec The weston compositor.
7600 * \return The testsuite data.
7601 *
7602 * \ingroup compositor
7603 * \sa weston_compositor_test_data_init
7604 */
7605WL_EXPORT void *
7606weston_compositor_get_test_data(struct weston_compositor *ec)
7607{
7608 return ec->test_data.test_private_data;
7609}
7610
Giulio Camuffo459137b2014-10-11 23:56:24 +03007611/** Create the compositor.
7612 *
7613 * This functions creates and initializes a compositor instance.
7614 *
7615 * \param display The Wayland display to be used.
7616 * \param user_data A pointer to an object that can later be retrieved
Marius Vlada2dace22019-06-12 16:05:44 +03007617 * \param log_ctx A pointer to weston_debug_compositor
Leandro Ribeirocdb72182020-11-06 11:24:22 -03007618 * \param test_data Optional testsuite data, or NULL.
Giulio Camuffo459137b2014-10-11 23:56:24 +03007619 * using the \ref weston_compositor_get_user_data function.
7620 * \return The compositor instance on success or NULL on failure.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007621 *
7622 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007623 */
7624WL_EXPORT struct weston_compositor *
Marius Vlad880b4852019-04-07 17:07:58 +03007625weston_compositor_create(struct wl_display *display,
Leandro Ribeirocdb72182020-11-06 11:24:22 -03007626 struct weston_log_context *log_ctx, void *user_data,
7627 const struct weston_testsuite_data *test_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04007628{
Giulio Camuffo459137b2014-10-11 23:56:24 +03007629 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05007630 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07007631
Leandro Ribeirobd9c0a62020-01-17 10:47:49 -03007632 if (!log_ctx)
7633 return NULL;
7634
Giulio Camuffo459137b2014-10-11 23:56:24 +03007635 ec = zalloc(sizeof *ec);
7636 if (!ec)
7637 return NULL;
7638
Leandro Ribeirocdb72182020-11-06 11:24:22 -03007639 if (test_data)
7640 ec->test_data = *test_data;
7641
Leandro Ribeirobd9c0a62020-01-17 10:47:49 -03007642 ec->weston_log_ctx = log_ctx;
Giulio Camuffo459137b2014-10-11 23:56:24 +03007643 ec->wl_display = display;
7644 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04007645 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07007646 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04007647 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03007648 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03007649 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02007650 wl_signal_init(&ec->idle_signal);
7651 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02007652 wl_signal_init(&ec->show_input_panel_signal);
7653 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02007654 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01007655 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01007656 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02007657 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02007658 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02007659 wl_signal_init(&ec->output_resized_signal);
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03007660 wl_signal_init(&ec->heads_changed_signal);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02007661 wl_signal_init(&ec->output_heads_changed_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07007662 wl_signal_init(&ec->session_signal);
Robert Beckettc569bdc2019-07-10 16:40:12 +01007663 ec->session_active = true;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04007664
Casey Dahlin58ba1372012-04-19 22:50:08 -04007665 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03007666 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04007667
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02007668 ec->activate_serial = 1;
7669
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05007670 ec->touch_mode = WESTON_TOUCH_MODE_NORMAL;
7671
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05307672 ec->content_protection = NULL;
7673
Derek Foreman152254b2015-11-26 14:17:48 -06007674 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04007675 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007676 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05007677
Giulio Camuffo954f1832014-10-11 18:27:30 +03007678 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04007679 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007680 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03007681
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007682 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007683 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007684 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01007685
Roman Gilge97391c2019-03-29 13:01:06 +01007686 if (!wl_global_create(ec->wl_display, &zxdg_output_manager_v1_interface, 2,
7687 ec, bind_xdg_output_manager))
7688 goto fail;
7689
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007690 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007691 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007692 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007693
Jonas Ådahl30d61d82014-10-22 21:21:17 +02007694 if (weston_input_init(ec) != 0)
7695 goto fail;
7696
Jason Ekstranda7af7042013-10-12 22:38:11 -05007697 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02007698 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007699 wl_list_init(&ec->layer_list);
7700 wl_list_init(&ec->seat_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007701 wl_list_init(&ec->pending_output_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007702 wl_list_init(&ec->output_list);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03007703 wl_list_init(&ec->head_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007704 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01007705 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007706 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01007707 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007708 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02007709 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007710
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007711 wl_list_init(&ec->plugin_api_list);
7712
Xiong Zhang97116532013-10-23 13:58:31 +08007713 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02007714 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04007715
Giulio Camuffo459137b2014-10-11 23:56:24 +03007716 wl_data_device_manager_init(ec->wl_display);
7717
7718 wl_display_init_shm(ec->wl_display);
7719
7720 loop = wl_display_get_event_loop(ec->wl_display);
7721 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Daniel Stone6847b852017-03-01 11:34:08 +00007722 ec->repaint_timer =
7723 wl_event_loop_add_timer(loop, output_repaint_timer_handler,
7724 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007725
Quentin Glidic82681572016-12-17 13:40:51 +01007726 weston_layer_init(&ec->fade_layer, ec);
7727 weston_layer_init(&ec->cursor_layer, ec);
7728
7729 weston_layer_set_position(&ec->fade_layer, WESTON_LAYER_POSITION_FADE);
7730 weston_layer_set_position(&ec->cursor_layer,
7731 WESTON_LAYER_POSITION_CURSOR);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007732
Daniel Stonece62cb32018-07-20 09:46:24 +01007733 ec->debug_scene =
Leandro Ribeiroce100192019-12-26 16:35:49 -03007734 weston_compositor_add_log_scope(ec, "scene-graph",
7735 "Scene graph details\n",
7736 debug_scene_graph_cb, NULL,
7737 ec);
Daniel Stonece62cb32018-07-20 09:46:24 +01007738
Marius Vladda104eb2019-09-05 14:31:01 +03007739 ec->timeline =
Leandro Ribeiroce100192019-12-26 16:35:49 -03007740 weston_compositor_add_log_scope(ec, "timeline",
7741 "Timeline event points\n",
7742 weston_timeline_create_subscription,
7743 weston_timeline_destroy_subscription,
7744 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007745 return ec;
7746
7747fail:
7748 free(ec);
7749 return NULL;
7750}
7751
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007752/** weston_compositor_shutdown
7753 * \ingroup compositor
7754 */
Benjamin Franzkeb8263022011-08-30 11:32:47 +02007755WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007756weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07007757{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007758 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07007759
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007760 wl_event_source_remove(ec->idle_source);
Lujin Wangd4b46cd2019-08-12 12:03:29 -07007761 wl_event_source_remove(ec->repaint_timer);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007762
Matt Roper361d2ad2011-08-29 13:52:23 -07007763 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02007764 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07007765 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02007766
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007767 /* Destroy all pending outputs associated with this compositor */
7768 wl_list_for_each_safe(output, next, &ec->pending_output_list, link)
7769 output->destroy(output);
7770
Pekka Paalanen90a5ffa2021-02-25 12:03:28 +02007771 /* Color manager objects may have renderer hooks */
7772 if (ec->color_manager) {
7773 ec->color_manager->destroy(ec->color_manager);
7774 ec->color_manager = NULL;
7775 }
7776
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02007777 if (ec->renderer)
7778 ec->renderer->destroy(ec);
7779
Daniel Stone325fc2d2012-05-30 16:31:58 +01007780 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09007781 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01007782 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01007783 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01007784 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02007785 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007786
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04007787 weston_plane_release(&ec->primary_plane);
Pekka Paalanen87400372021-05-14 14:29:40 +03007788
7789 weston_layer_fini(&ec->fade_layer);
7790 weston_layer_fini(&ec->cursor_layer);
7791
7792 if (!wl_list_empty(&ec->layer_list))
7793 weston_log("BUG: layer_list is not empty after shutdown. Calls to weston_layer_fini() are missing somwhere.\n");
Matt Roper361d2ad2011-08-29 13:52:23 -07007794}
7795
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007796/** weston_compositor_exit_with_code
7797 * \ingroup compositor
7798 */
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05007799WL_EXPORT void
Adam Jackson3c3f3b12019-10-16 16:02:59 -04007800weston_compositor_exit_with_code(struct weston_compositor *compositor,
Frederic Plourdec336f062014-10-29 14:44:33 -04007801 int exit_code)
7802{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02007803 if (compositor->exit_code == EXIT_SUCCESS)
7804 compositor->exit_code = exit_code;
7805
Giulio Camuffo459137b2014-10-11 23:56:24 +03007806 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04007807}
7808
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007809/** weston_compositor_set_default_pointer_grab
7810 * \ingroup compositor
7811 */
Frederic Plourdec336f062014-10-29 14:44:33 -04007812WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007813weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
7814 const struct weston_pointer_grab_interface *interface)
7815{
7816 struct weston_seat *seat;
7817
7818 ec->default_pointer_grab = interface;
7819 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05007820 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
7821
7822 if (pointer)
7823 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007824 }
7825}
7826
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007827/** weston_compositor_set_presentation_clock
7828 * \ingroup compositor
7829 */
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007830WL_EXPORT int
7831weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
7832 clockid_t clk_id)
7833{
7834 struct timespec ts;
7835
7836 if (clock_gettime(clk_id, &ts) < 0)
7837 return -1;
7838
7839 compositor->presentation_clock = clk_id;
7840
7841 return 0;
7842}
7843
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007844/** For choosing the software clock, when the display hardware or API
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007845 * does not expose a compatible presentation timestamp.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007846 *
7847 * \ingroup compositor
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007848 */
7849WL_EXPORT int
7850weston_compositor_set_presentation_clock_software(
7851 struct weston_compositor *compositor)
7852{
7853 /* In order of preference */
7854 static const clockid_t clocks[] = {
7855 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
7856 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
7857 CLOCK_MONOTONIC, /* no jumps, may crawl */
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007858 };
7859 unsigned i;
7860
7861 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
7862 if (weston_compositor_set_presentation_clock(compositor,
7863 clocks[i]) == 0)
7864 return 0;
7865
7866 weston_log("Error: no suitable presentation clock available.\n");
7867
7868 return -1;
7869}
7870
Pekka Paalanen662f3842015-03-18 12:17:26 +02007871/** Read the current time from the Presentation clock
7872 *
7873 * \param compositor
Marius Vlada2dace22019-06-12 16:05:44 +03007874 * \param[out] ts The current time.
Pekka Paalanen662f3842015-03-18 12:17:26 +02007875 *
7876 * \note Reading the current time in user space is always imprecise to some
7877 * degree.
7878 *
7879 * This function is never meant to fail. If reading the clock does fail,
7880 * an error message is logged and a zero time is returned. Callers are not
7881 * supposed to detect or react to failures.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007882 *
7883 * \ingroup compositor
Pekka Paalanen662f3842015-03-18 12:17:26 +02007884 */
7885WL_EXPORT void
7886weston_compositor_read_presentation_clock(
7887 const struct weston_compositor *compositor,
7888 struct timespec *ts)
7889{
7890 static bool warned;
7891 int ret;
7892
7893 ret = clock_gettime(compositor->presentation_clock, ts);
7894 if (ret < 0) {
7895 ts->tv_sec = 0;
7896 ts->tv_nsec = 0;
7897
7898 if (!warned)
7899 weston_log("Error: failure to read "
Antonio Borneo39578632019-04-26 23:57:31 +02007900 "the presentation clock %#x: '%s' (%d)\n",
7901 compositor->presentation_clock,
7902 strerror(errno), errno);
Pekka Paalanen662f3842015-03-18 12:17:26 +02007903 warned = true;
7904 }
7905}
7906
Pekka Paalanen230f3b12014-09-29 14:18:40 -04007907/** Import dmabuf buffer into current renderer
7908 *
7909 * \param compositor
7910 * \param buffer the dmabuf buffer to import
7911 * \return true on usable buffers, false otherwise
7912 *
7913 * This function tests that the linux_dmabuf_buffer is usable
7914 * for the current renderer. Returns false on unusable buffers. Usually
7915 * usability is tested by importing the dmabufs for composition.
7916 *
7917 * This hook is also used for detecting if the renderer supports
7918 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
7919 * supported.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007920 *
7921 * \ingroup compositor
7922 */
Pekka Paalanen230f3b12014-09-29 14:18:40 -04007923WL_EXPORT bool
7924weston_compositor_import_dmabuf(struct weston_compositor *compositor,
7925 struct linux_dmabuf_buffer *buffer)
7926{
7927 struct weston_renderer *renderer;
7928
7929 renderer = compositor->renderer;
7930
7931 if (renderer->import_dmabuf == NULL)
7932 return false;
7933
7934 return renderer->import_dmabuf(compositor, buffer);
7935}
7936
Marius Vlad5a701542019-11-16 20:26:52 +02007937WL_EXPORT bool
7938weston_compositor_dmabuf_can_scanout(struct weston_compositor *compositor,
7939 struct linux_dmabuf_buffer *buffer)
7940{
7941 struct weston_backend *backend = compositor->backend;
7942
7943 if (backend->can_scanout_dmabuf == NULL)
7944 return false;
7945
7946 return backend->can_scanout_dmabuf(compositor, buffer);
7947}
7948
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007949WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05007950weston_version(int *major, int *minor, int *micro)
7951{
7952 *major = WESTON_VERSION_MAJOR;
7953 *minor = WESTON_VERSION_MINOR;
7954 *micro = WESTON_VERSION_MICRO;
7955}
7956
Daniel Stonee03c1112016-11-24 20:45:45 +00007957/**
7958 * Attempts to find a module path from the module map specified in the
7959 * environment. If found, writes the full path into the path variable.
7960 *
7961 * The module map is a string in environment variable WESTON_MODULE_MAP, where
7962 * each entry is of the form "name=path" and entries are separated by
7963 * semicolons. Whitespace is significant.
7964 *
7965 * \param name The name to search for.
7966 * \param path Where the path is written to if found.
7967 * \param path_len Allocated bytes at \c path .
7968 * \returns The length of the string written to path on success, or 0 if the
7969 * module was not specified in the environment map or path_len was too small.
7970 */
7971WL_EXPORT size_t
7972weston_module_path_from_env(const char *name, char *path, size_t path_len)
7973{
7974 const char *mapping = getenv("WESTON_MODULE_MAP");
7975 const char *end;
7976 const int name_len = strlen(name);
7977
7978 if (!mapping)
7979 return 0;
7980
7981 end = mapping + strlen(mapping);
7982 while (mapping < end && *mapping) {
7983 const char *filename, *next;
7984
7985 /* early out: impossibly short string */
7986 if (end - mapping < name_len + 1)
7987 return 0;
7988
7989 filename = &mapping[name_len + 1];
7990 next = strchrnul(mapping, ';');
7991
7992 if (strncmp(mapping, name, name_len) == 0 &&
7993 mapping[name_len] == '=') {
7994 size_t file_len = next - filename; /* no trailing NUL */
7995 if (file_len >= path_len)
7996 return 0;
7997 strncpy(path, filename, file_len);
7998 path[file_len] = '\0';
7999 return file_len;
8000 }
8001
8002 mapping = next + 1;
8003 }
8004
8005 return 0;
8006}
8007
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03008008WL_EXPORT void *
8009weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04008010{
8011 char path[PATH_MAX];
8012 void *module, *init;
Daniel Stonebeb97e52016-11-28 12:13:54 +00008013 size_t len;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04008014
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08008015 if (name == NULL)
8016 return NULL;
8017
Derek Foreman3f86e502015-06-08 11:46:54 -05008018 if (name[0] != '/') {
Daniel Stonee03c1112016-11-24 20:45:45 +00008019 len = weston_module_path_from_env(name, path, sizeof path);
8020 if (len == 0)
Daniel Stonebeb97e52016-11-28 12:13:54 +00008021 len = snprintf(path, sizeof path, "%s/%s",
8022 LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05008023 } else {
Daniel Stonebeb97e52016-11-28 12:13:54 +00008024 len = snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05008025 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04008026
Daniel Stonebeb97e52016-11-28 12:13:54 +00008027 /* snprintf returns the length of the string it would've written,
8028 * _excluding_ the NUL byte. So even being equal to the size of
8029 * our buffer is an error here. */
8030 if (len >= sizeof path)
8031 return NULL;
8032
Kristian Høgsberga6813d22012-09-12 12:21:01 -04008033 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
8034 if (module) {
8035 weston_log("Module '%s' already loaded\n", path);
Pekka Paalanenf696ee92019-11-04 14:07:51 +02008036 } else {
8037 weston_log("Loading module '%s'\n", path);
8038 module = dlopen(path, RTLD_NOW);
8039 if (!module) {
8040 weston_log("Failed to load module: %s\n", dlerror());
8041 return NULL;
8042 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04008043 }
8044
8045 init = dlsym(module, entrypoint);
8046 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03008047 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00008048 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04008049 return NULL;
8050 }
8051
8052 return init;
8053}
8054
Pekka Paalanen6ffbba32019-11-06 12:59:32 +02008055/** Add a compositor destroy listener only once
8056 *
8057 * \param compositor The compositor whose destroy to watch for.
8058 * \param listener The listener struct to initialize.
8059 * \param destroy_handler The callback when compositor is destroyed.
8060 * \return True if listener is added, or false if there already is a listener
8061 * with the given \c destroy_handler.
8062 *
8063 * This function does nothing and returns false if the given callback function
8064 * is already present in the weston_compositor destroy callbacks list.
8065 * Otherwise, this function initializes the given listener with the given
8066 * callback pointer and adds it to the compositor's destroy callbacks list.
8067 *
8068 * This can be used to ensure that plugin initialization is done only once
8069 * in case the same plugin is loaded multiple times. If this function returns
8070 * false, the plugin should be already initialized successfully.
8071 *
8072 * All plugins should register a destroy listener for cleaning up. Note, that
8073 * the plugin destruction order is not guaranteed: plugins that depend on other
8074 * plugins must be able to be torn down in arbitrary order.
8075 *
Leandro Ribeiroca640d52020-01-27 19:12:01 -03008076 * \sa weston_compositor_destroy
Pekka Paalanen6ffbba32019-11-06 12:59:32 +02008077 */
8078WL_EXPORT bool
8079weston_compositor_add_destroy_listener_once(struct weston_compositor *compositor,
8080 struct wl_listener *listener,
8081 wl_notify_func_t destroy_handler)
8082{
8083 if (wl_signal_get(&compositor->destroy_signal, destroy_handler))
8084 return false;
8085
8086 listener->notify = destroy_handler;
8087 wl_signal_add(&compositor->destroy_signal, listener);
8088 return true;
8089}
Giulio Camuffo459137b2014-10-11 23:56:24 +03008090
Leandro Ribeiroca640d52020-01-27 19:12:01 -03008091/** Destroys the compositor.
Giulio Camuffo459137b2014-10-11 23:56:24 +03008092 *
Leandro Ribeiroca640d52020-01-27 19:12:01 -03008093 * This function cleans up the compositor state and then destroys it.
Giulio Camuffo459137b2014-10-11 23:56:24 +03008094 *
Leandro Ribeiroca640d52020-01-27 19:12:01 -03008095 * @param compositor The compositor to be destroyed.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008096 *
Marius Vlad284d5342019-06-24 12:00:47 +03008097 * @ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03008098 */
8099WL_EXPORT void
Leandro Ribeiroca640d52020-01-27 19:12:01 -03008100weston_compositor_destroy(struct weston_compositor *compositor)
Giulio Camuffo459137b2014-10-11 23:56:24 +03008101{
8102 /* prevent further rendering while shutting down */
8103 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
8104
8105 wl_signal_emit(&compositor->destroy_signal, compositor);
8106
8107 weston_compositor_xkb_destroy(compositor);
8108
Giulio Camuffo2d24e642015-10-03 16:25:15 +03008109 if (compositor->backend)
8110 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02008111
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03008112 /* The backend is responsible for destroying the heads. */
8113 assert(wl_list_empty(&compositor->head_list));
8114
Pekka Paalanen827b5d22016-06-29 11:54:26 +02008115 weston_plugin_api_destroy_list(compositor);
8116
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03008117 if (compositor->heads_changed_source)
8118 wl_event_source_remove(compositor->heads_changed_source);
8119
Leandro Ribeirof0149642019-12-18 15:52:18 -03008120 weston_log_scope_destroy(compositor->debug_scene);
Daniel Stonece62cb32018-07-20 09:46:24 +01008121 compositor->debug_scene = NULL;
Marius Vladda104eb2019-09-05 14:31:01 +03008122
Leandro Ribeirof0149642019-12-18 15:52:18 -03008123 weston_log_scope_destroy(compositor->timeline);
Marius Vladda104eb2019-09-05 14:31:01 +03008124 compositor->timeline = NULL;
Pekka Paalanena5630ea2017-10-12 13:13:42 +02008125
Giulio Camuffo459137b2014-10-11 23:56:24 +03008126 free(compositor);
8127}
8128
8129/** Instruct the compositor to exit.
8130 *
8131 * This functions does not directly destroy the compositor object, it merely
8132 * command it to start the tear down process. It is not guaranteed that the
8133 * tear down will happen immediately.
8134 *
8135 * \param compositor The compositor to tear down.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008136 *
8137 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03008138 */
8139WL_EXPORT void
8140weston_compositor_exit(struct weston_compositor *compositor)
8141{
8142 compositor->exit(compositor);
8143}
8144
8145/** Return the user data stored in the compositor.
8146 *
8147 * This function returns the user data pointer set with user_data parameter
8148 * to the \ref weston_compositor_create function.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008149 *
8150 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03008151 */
8152WL_EXPORT void *
8153weston_compositor_get_user_data(struct weston_compositor *compositor)
8154{
8155 return compositor->user_data;
8156}
Pekka Paalanendd186732016-06-03 14:49:54 +03008157
Pekka Paalanen50dbf382016-06-03 15:23:46 +03008158static const char * const backend_map[] = {
8159 [WESTON_BACKEND_DRM] = "drm-backend.so",
8160 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
8161 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
8162 [WESTON_BACKEND_RDP] = "rdp-backend.so",
8163 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
8164 [WESTON_BACKEND_X11] = "x11-backend.so",
8165};
8166
Pekka Paalanendd186732016-06-03 14:49:54 +03008167/** Load a backend into a weston_compositor
8168 *
8169 * A backend must be loaded to make a weston_compositor work. A backend
8170 * provides input and output capabilities, and determines the renderer to use.
8171 *
8172 * \param compositor A compositor that has not had a backend loaded yet.
8173 * \param backend Name of the backend file.
8174 * \param config_base A pointer to a backend-specific configuration
8175 * structure's 'base' member.
8176 *
8177 * \return 0 on success, or -1 on error.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008178 *
8179 * \ingroup compositor
Pekka Paalanendd186732016-06-03 14:49:54 +03008180 */
8181WL_EXPORT int
8182weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03008183 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03008184 struct weston_backend_config *config_base)
8185{
8186 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03008187 struct weston_backend_config *config_base);
8188
Pekka Paalanend7e35112017-08-29 17:04:12 +03008189 if (compositor->backend) {
8190 weston_log("Error: attempt to load a backend when one is already loaded\n");
8191 return -1;
8192 }
8193
Quentin Glidic887c0182016-07-10 11:00:53 +02008194 if (backend >= ARRAY_LENGTH(backend_map))
Pekka Paalanen50dbf382016-06-03 15:23:46 +03008195 return -1;
8196
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01008197 backend_init = weston_load_module(backend_map[backend], "weston_backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03008198 if (!backend_init)
8199 return -1;
8200
Pekka Paalanend7e35112017-08-29 17:04:12 +03008201 if (backend_init(compositor, config_base) < 0) {
8202 compositor->backend = NULL;
8203 return -1;
8204 }
8205
Pekka Paalanen90a5ffa2021-02-25 12:03:28 +02008206 if (!compositor->color_manager) {
8207 compositor->color_manager =
8208 weston_color_manager_noop_create(compositor);
8209 }
8210
8211 if (!compositor->color_manager)
8212 return -1;
8213
8214 if (!compositor->color_manager->init(compositor->color_manager))
8215 return -1;
8216
8217 weston_log("Color manager: %s\n", compositor->color_manager->name);
8218
Pekka Paalanend7e35112017-08-29 17:04:12 +03008219 return 0;
Pekka Paalanendd186732016-06-03 14:49:54 +03008220}
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008221
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008222/** weston_compositor_load_xwayland
8223 * \ingroup compositor
8224 */
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008225WL_EXPORT int
8226weston_compositor_load_xwayland(struct weston_compositor *compositor)
8227{
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01008228 int (*module_init)(struct weston_compositor *ec);
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008229
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01008230 module_init = weston_load_module("xwayland.so", "weston_module_init");
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008231 if (!module_init)
8232 return -1;
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01008233 if (module_init(compositor) < 0)
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008234 return -1;
8235 return 0;
8236}
Vasilis Liaskovitis486b4632018-10-10 16:14:55 +02008237
8238/** Resolve an internal compositor error by disconnecting the client.
8239 *
8240 * This function is used in cases when the wl_buffer turns out
8241 * unusable and there is no fallback path.
8242 *
8243 * It is possible the fault is caused by a compositor bug, the underlying
8244 * graphics stack bug or normal behaviour, or perhaps a client mistake.
8245 * In any case, the options are to either composite garbage or nothing,
8246 * or disconnect the client. This is a helper function for the latter.
8247 *
8248 * The error is sent as an INVALID_OBJECT error on the client's wl_display.
8249 *
8250 * \param buffer The weston buffer that is unusable.
8251 * \param msg A custom error message attached to the protocol error.
8252 */
8253WL_EXPORT void
8254weston_buffer_send_server_error(struct weston_buffer *buffer,
8255 const char *msg)
8256{
8257 struct wl_client *client;
8258 struct wl_resource *display_resource;
8259 uint32_t id;
8260
8261 assert(buffer->resource);
8262 id = wl_resource_get_id(buffer->resource);
8263 client = wl_resource_get_client(buffer->resource);
8264 display_resource = wl_client_get_object(client, 1);
8265
8266 assert(display_resource);
8267 wl_resource_post_error(display_resource,
8268 WL_DISPLAY_ERROR_INVALID_OBJECT,
8269 "server error with "
8270 "wl_buffer@%u: %s", id, msg);
8271}
Ankit Nautiyal93dde242019-07-08 11:46:42 +05308272
8273WL_EXPORT void
8274weston_output_disable_planes_incr(struct weston_output *output)
8275{
8276 output->disable_planes++;
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05308277 /*
8278 * If disable_planes changes from 0 to non-zero, it means some type of
8279 * recording of content has started, and therefore protection level of
8280 * the protected surfaces must be updated to avoid the recording of
8281 * the protected content.
8282 */
8283 if (output->disable_planes == 1)
8284 weston_schedule_surface_protection_update(output->compositor);
Ankit Nautiyal93dde242019-07-08 11:46:42 +05308285}
8286
8287WL_EXPORT void
8288weston_output_disable_planes_decr(struct weston_output *output)
8289{
8290 output->disable_planes--;
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05308291 /*
8292 * If disable_planes changes from non-zero to 0, it means no content
8293 * recording is going on any more, and the protected and surfaces can be
8294 * shown without any apprehensions about content being recorded.
8295 */
8296 if (output->disable_planes == 0)
8297 weston_schedule_surface_protection_update(output->compositor);
8298
Ankit Nautiyal93dde242019-07-08 11:46:42 +05308299}