blob: 66d43375afeed94ca78e00f88907343e473cf10d [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"
Alexandros Frantzisfa48c5e2020-07-09 17:31:34 +030071#include "shared/signal.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040072#include "git-version.h"
Pekka Paalanencda14882019-04-04 15:41:02 +030073#include <libweston/version.h>
Pekka Paalanen27b377f2019-03-29 17:07:34 +020074#include <libweston/plugin-registry.h>
Marius Vlad00a6e012018-11-20 17:52:31 +020075#include "pixel-formats.h"
Marius Vlad63ef0782019-07-16 23:34:14 +030076#include "backend.h"
Marius Vlada72e3712019-07-10 13:46:39 +030077#include "libweston-internal.h"
Pekka Paalanen90a5ffa2021-02-25 12:03:28 +020078#include "color.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050079
Marius Vlad4e036292019-07-09 00:36:20 +030080#include "weston-log-internal.h"
81
Marius Vlad78984ee2019-06-11 00:05:08 +030082/**
83 * \defgroup head Head
84 * \defgroup output Output
85 * \defgroup compositor Compositor
86 */
87
Pekka Paalanen0513a952014-05-21 16:17:27 +030088#define DEFAULT_REPAINT_WINDOW 7 /* milliseconds */
89
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020090static void
Pekka Paalanend72bad22017-03-29 17:01:41 +030091weston_output_update_matrix(struct weston_output *output);
92
93static void
Alexander Larsson0b135062013-05-28 16:23:36 +020094weston_output_transform_scale_init(struct weston_output *output,
95 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020096
Rob Bradford27b17932013-06-26 18:08:46 +010097static void
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +030098weston_compositor_build_view_list(struct weston_compositor *compositor,
99 struct weston_output *output);
Rob Bradford27b17932013-06-26 18:08:46 +0100100
Pekka Paalanendcac3512017-12-08 14:13:34 +0200101static char *
102weston_output_create_heads_string(struct weston_output *output);
103
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +0300104static struct weston_paint_node *
105weston_paint_node_create(struct weston_surface *surface,
106 struct weston_view *view,
107 struct weston_output *output)
108{
109 struct weston_paint_node *pnode;
Pekka Paalanen90a5ffa2021-02-25 12:03:28 +0200110 struct weston_paint_node *existing_node;
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +0300111
112 assert(view->surface == surface);
113
114 pnode = zalloc(sizeof *pnode);
115 if (!pnode)
116 return NULL;
117
Pekka Paalanen90a5ffa2021-02-25 12:03:28 +0200118 /*
119 * Invariant: all paint nodes with the same surface+output have the
120 * same surf_xform state.
121 */
Pekka Paalanen26b04f02022-01-24 13:09:01 +0200122 wl_list_for_each(existing_node, &surface->paint_node_list, surface_link) {
Pekka Paalanen90a5ffa2021-02-25 12:03:28 +0200123 assert(existing_node->surface == surface);
124 if (existing_node->output != output)
125 continue;
126
127 weston_surface_color_transform_copy(&pnode->surf_xform,
128 &existing_node->surf_xform);
129 pnode->surf_xform_valid = existing_node->surf_xform_valid;
130 break;
131 }
132
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +0300133 pnode->surface = surface;
134 wl_list_insert(&surface->paint_node_list, &pnode->surface_link);
135
136 pnode->view = view;
137 wl_list_insert(&view->paint_node_list, &pnode->view_link);
138
139 pnode->output = output;
140 wl_list_insert(&output->paint_node_list, &pnode->output_link);
141
Pekka Paalanen2fddc532021-04-30 17:41:29 +0300142 wl_list_init(&pnode->z_order_link);
143
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +0300144 return pnode;
145}
146
147static void
148weston_paint_node_destroy(struct weston_paint_node *pnode)
149{
150 assert(pnode->view->surface == pnode->surface);
151 wl_list_remove(&pnode->surface_link);
152 wl_list_remove(&pnode->view_link);
153 wl_list_remove(&pnode->output_link);
Pekka Paalanen2fddc532021-04-30 17:41:29 +0300154 wl_list_remove(&pnode->z_order_link);
Pekka Paalanen90a5ffa2021-02-25 12:03:28 +0200155 assert(pnode->surf_xform_valid || !pnode->surf_xform.transform);
156 weston_surface_color_transform_fini(&pnode->surf_xform);
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +0300157 free(pnode);
158}
159
Pekka Paalanen6528c032017-03-28 15:27:10 +0300160/** Send wl_output events for mode and scale changes
161 *
162 * \param head Send on all resources bound to this head.
163 * \param mode_changed If true, send the current mode.
164 * \param scale_changed If true, send the current scale.
165 */
166static void
167weston_mode_switch_send_events(struct weston_head *head,
168 bool mode_changed, bool scale_changed)
169{
170 struct weston_output *output = head->output;
171 struct wl_resource *resource;
172 int version;
173
174 wl_resource_for_each(resource, &head->resource_list) {
175 if (mode_changed) {
176 wl_output_send_mode(resource,
177 output->current_mode->flags,
178 output->current_mode->width,
179 output->current_mode->height,
180 output->current_mode->refresh);
181 }
182
183 version = wl_resource_get_version(resource);
184 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION && scale_changed)
185 wl_output_send_scale(resource, output->current_scale);
186
187 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
188 wl_output_send_done(resource);
189 }
Roman Gilge97391c2019-03-29 13:01:06 +0100190 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
191 zxdg_output_v1_send_logical_position(resource,
192 output->x,
193 output->y);
194 zxdg_output_v1_send_logical_size(resource,
195 output->width,
196 output->height);
197 zxdg_output_v1_send_done(resource);
198 }
Pekka Paalanen6528c032017-03-28 15:27:10 +0300199}
200
201static void
202weston_mode_switch_finish(struct weston_output *output,
203 int mode_changed, int scale_changed)
Alex Wu2dda6042012-04-17 17:20:47 +0800204{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200205 struct weston_seat *seat;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300206 struct weston_head *head;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200207 pixman_region32_t old_output_region;
Alexander Larsson355748e2013-05-28 16:23:38 +0200208
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200209 pixman_region32_init(&old_output_region);
210 pixman_region32_copy(&old_output_region, &output->region);
211
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200212 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +0200213 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200214
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200215 pixman_region32_init_rect(&output->region, output->x, output->y,
216 output->width, output->height);
217
218 weston_output_update_matrix(output);
219
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200220 /* If a pointer falls outside the outputs new geometry, move it to its
221 * lower-right corner */
222 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500223 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200224 int32_t x, y;
225
226 if (!pointer)
227 continue;
228
229 x = wl_fixed_to_int(pointer->x);
230 y = wl_fixed_to_int(pointer->y);
231
232 if (!pixman_region32_contains_point(&old_output_region,
233 x, y, NULL) ||
234 pixman_region32_contains_point(&output->region,
235 x, y, NULL))
236 continue;
237
238 if (x >= output->x + output->width)
239 x = output->x + output->width - 1;
240 if (y >= output->y + output->height)
241 y = output->y + output->height - 1;
242
243 pointer->x = wl_fixed_from_int(x);
244 pointer->y = wl_fixed_from_int(y);
245 }
246
247 pixman_region32_fini(&old_output_region);
248
Derek Foremandd4cd332014-11-10 10:29:59 -0600249 if (!mode_changed && !scale_changed)
250 return;
251
Hardening57388e42013-09-18 23:56:36 +0200252 /* notify clients of the changes */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300253 wl_list_for_each(head, &output->head_list, output_link)
254 weston_mode_switch_send_events(head,
255 mode_changed, scale_changed);
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600256}
257
David Fort0de859e2016-05-27 23:22:57 +0200258static void
259weston_compositor_reflow_outputs(struct weston_compositor *compositor,
260 struct weston_output *resized_output, int delta_width);
261
Marius Vlad55d87362019-06-11 01:15:35 +0300262/**
263 * \ingroup output
264 */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600265WL_EXPORT int
266weston_output_mode_set_native(struct weston_output *output,
267 struct weston_mode *mode,
268 int32_t scale)
269{
270 int ret;
271 int mode_changed = 0, scale_changed = 0;
David Fort0de859e2016-05-27 23:22:57 +0200272 int32_t old_width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600273
274 if (!output->switch_mode)
275 return -1;
276
277 if (!output->original_mode) {
278 mode_changed = 1;
279 ret = output->switch_mode(output, mode);
280 if (ret < 0)
281 return ret;
282 if (output->current_scale != scale) {
283 scale_changed = 1;
284 output->current_scale = scale;
Hardening57388e42013-09-18 23:56:36 +0200285 }
286 }
287
David Fort0de859e2016-05-27 23:22:57 +0200288 old_width = output->width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600289 output->native_mode = mode;
290 output->native_scale = scale;
291
292 weston_mode_switch_finish(output, mode_changed, scale_changed);
293
David Fort0de859e2016-05-27 23:22:57 +0200294 if (mode_changed || scale_changed) {
295 weston_compositor_reflow_outputs(output->compositor, output, output->width - old_width);
296
297 wl_signal_emit(&output->compositor->output_resized_signal, output);
298 }
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600299 return 0;
300}
301
Marius Vlad55d87362019-06-11 01:15:35 +0300302/**
303 * \ingroup output
304 */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600305WL_EXPORT int
306weston_output_mode_switch_to_native(struct weston_output *output)
307{
308 int ret;
309 int mode_changed = 0, scale_changed = 0;
310
311 if (!output->switch_mode)
312 return -1;
313
314 if (!output->original_mode) {
315 weston_log("already in the native mode\n");
316 return -1;
317 }
318 /* the non fullscreen clients haven't seen a mode set since we
319 * switched into a temporary, so we need to notify them if the
320 * mode at that time is different from the native mode now.
321 */
322 mode_changed = (output->original_mode != output->native_mode);
323 scale_changed = (output->original_scale != output->native_scale);
324
325 ret = output->switch_mode(output, output->native_mode);
326 if (ret < 0)
327 return ret;
328
329 output->current_scale = output->native_scale;
330
331 output->original_mode = NULL;
332 output->original_scale = 0;
333
334 weston_mode_switch_finish(output, mode_changed, scale_changed);
335
336 return 0;
337}
338
Marius Vlad55d87362019-06-11 01:15:35 +0300339/**
340 * \ingroup output
341 */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600342WL_EXPORT int
343weston_output_mode_switch_to_temporary(struct weston_output *output,
344 struct weston_mode *mode,
345 int32_t scale)
346{
347 int ret;
348
349 if (!output->switch_mode)
350 return -1;
351
352 /* original_mode is the last mode non full screen clients have seen,
353 * so we shouldn't change it if we already have one set.
354 */
355 if (!output->original_mode) {
356 output->original_mode = output->native_mode;
357 output->original_scale = output->native_scale;
358 }
359 ret = output->switch_mode(output, mode);
360 if (ret < 0)
361 return ret;
362
363 output->current_scale = scale;
364
365 weston_mode_switch_finish(output, 0, 0);
366
367 return 0;
Alex Wu2dda6042012-04-17 17:20:47 +0800368}
369
Benjamin Franzke06286262011-05-06 19:12:33 +0200370static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300371region_init_infinite(pixman_region32_t *region)
372{
373 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
374 UINT32_MAX, UINT32_MAX);
375}
376
Pekka Paalanene67858b2013-04-25 13:57:42 +0300377static struct weston_subsurface *
378weston_surface_to_subsurface(struct weston_surface *surface);
379
Jason Ekstranda7af7042013-10-12 22:38:11 -0500380WL_EXPORT struct weston_view *
381weston_view_create(struct weston_surface *surface)
382{
383 struct weston_view *view;
384
Bryce Harringtonde16d892014-11-20 22:21:57 -0800385 view = zalloc(sizeof *view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500386 if (view == NULL)
387 return NULL;
388
389 view->surface = surface;
Daniel Stonefb4869d2016-12-09 16:27:54 +0000390 view->plane = &surface->compositor->primary_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500391
Jason Ekstranda7af7042013-10-12 22:38:11 -0500392 /* Assign to surface */
393 wl_list_insert(&surface->views, &view->surface_link);
394
395 wl_signal_init(&view->destroy_signal);
396 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300397 wl_list_init(&view->layer_link.link);
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +0300398 wl_list_init(&view->paint_node_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500399
Jason Ekstranda7af7042013-10-12 22:38:11 -0500400 pixman_region32_init(&view->clip);
401
402 view->alpha = 1.0;
403 pixman_region32_init(&view->transform.opaque);
404
405 wl_list_init(&view->geometry.transformation_list);
406 wl_list_insert(&view->geometry.transformation_list,
407 &view->transform.position.link);
408 weston_matrix_init(&view->transform.position.matrix);
409 wl_list_init(&view->geometry.child_list);
Pekka Paalanen380adf52015-02-16 14:39:11 +0200410 pixman_region32_init(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500411 pixman_region32_init(&view->transform.boundingbox);
412 view->transform.dirty = 1;
413
Jason Ekstranda7af7042013-10-12 22:38:11 -0500414 return view;
415}
416
Pekka Paalanen133e4392014-09-23 22:08:46 -0400417struct weston_presentation_feedback {
418 struct wl_resource *resource;
419
420 /* XXX: could use just wl_resource_get_link() instead */
421 struct wl_list link;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200422
423 /* The per-surface feedback flags */
424 uint32_t psf_flags;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400425};
426
427static void
428weston_presentation_feedback_discard(
429 struct weston_presentation_feedback *feedback)
430{
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200431 wp_presentation_feedback_send_discarded(feedback->resource);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400432 wl_resource_destroy(feedback->resource);
433}
434
435static void
436weston_presentation_feedback_discard_list(struct wl_list *list)
437{
438 struct weston_presentation_feedback *feedback, *tmp;
439
440 wl_list_for_each_safe(feedback, tmp, list, link)
441 weston_presentation_feedback_discard(feedback);
442}
443
444static void
445weston_presentation_feedback_present(
446 struct weston_presentation_feedback *feedback,
447 struct weston_output *output,
448 uint32_t refresh_nsec,
449 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200450 uint64_t seq,
451 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400452{
453 struct wl_client *client = wl_resource_get_client(feedback->resource);
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300454 struct weston_head *head;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400455 struct wl_resource *o;
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200456 uint32_t tv_sec_hi;
457 uint32_t tv_sec_lo;
458 uint32_t tv_nsec;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300459 bool done = false;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400460
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300461 wl_list_for_each(head, &output->head_list, output_link) {
462 wl_resource_for_each(o, &head->resource_list) {
463 if (wl_resource_get_client(o) != client)
464 continue;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400465
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300466 wp_presentation_feedback_send_sync_output(feedback->resource, o);
467 done = true;
468 }
469
470 /* For clone mode, send it for just one wl_output global,
471 * they are all equivalent anyway.
472 */
473 if (done)
474 break;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400475 }
476
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200477 timespec_to_proto(ts, &tv_sec_hi, &tv_sec_lo, &tv_nsec);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200478 wp_presentation_feedback_send_presented(feedback->resource,
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200479 tv_sec_hi, tv_sec_lo, tv_nsec,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200480 refresh_nsec,
481 seq >> 32, seq & 0xffffffff,
482 flags | feedback->psf_flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400483 wl_resource_destroy(feedback->resource);
484}
485
486static void
487weston_presentation_feedback_present_list(struct wl_list *list,
488 struct weston_output *output,
489 uint32_t refresh_nsec,
490 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200491 uint64_t seq,
492 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400493{
494 struct weston_presentation_feedback *feedback, *tmp;
495
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200496 assert(!(flags & WP_PRESENTATION_FEEDBACK_INVALID) ||
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200497 wl_list_empty(list));
498
Pekka Paalanen133e4392014-09-23 22:08:46 -0400499 wl_list_for_each_safe(feedback, tmp, list, link)
500 weston_presentation_feedback_present(feedback, output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200501 refresh_nsec, ts, seq,
502 flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400503}
504
Jason Ekstrand7b982072014-05-20 14:33:03 -0500505static void
506surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
507{
508 struct weston_surface_state *state =
509 container_of(listener, struct weston_surface_state,
510 buffer_destroy_listener);
511
512 state->buffer = NULL;
513}
514
515static void
516weston_surface_state_init(struct weston_surface_state *state)
517{
518 state->newly_attached = 0;
519 state->buffer = NULL;
520 state->buffer_destroy_listener.notify =
521 surface_state_handle_buffer_destroy;
522 state->sx = 0;
523 state->sy = 0;
524
Derek Foreman152254b2015-11-26 14:17:48 -0600525 pixman_region32_init(&state->damage_surface);
526 pixman_region32_init(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500527 pixman_region32_init(&state->opaque);
528 region_init_infinite(&state->input);
529
530 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400531 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500532
533 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
534 state->buffer_viewport.buffer.scale = 1;
535 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
536 state->buffer_viewport.surface.width = -1;
537 state->buffer_viewport.changed = 0;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300538
539 state->acquire_fence_fd = -1;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +0530540
541 state->desired_protection = WESTON_HDCP_DISABLE;
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +0530542 state->protection_mode = WESTON_SURFACE_PROTECTION_MODE_RELAXED;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500543}
544
545static void
546weston_surface_state_fini(struct weston_surface_state *state)
547{
Vlad Zahorodniic2b97472021-07-29 11:16:51 +0300548 struct wl_resource *cb, *next;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500549
Vlad Zahorodniic2b97472021-07-29 11:16:51 +0300550 wl_resource_for_each_safe(cb, next, &state->frame_callback_list)
551 wl_resource_destroy(cb);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500552
Pekka Paalanen133e4392014-09-23 22:08:46 -0400553 weston_presentation_feedback_discard_list(&state->feedback_list);
554
Jason Ekstrand7b982072014-05-20 14:33:03 -0500555 pixman_region32_fini(&state->input);
556 pixman_region32_fini(&state->opaque);
Derek Foreman152254b2015-11-26 14:17:48 -0600557 pixman_region32_fini(&state->damage_surface);
558 pixman_region32_fini(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500559
560 if (state->buffer)
561 wl_list_remove(&state->buffer_destroy_listener.link);
562 state->buffer = NULL;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300563
564 fd_clear(&state->acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +0300565 weston_buffer_release_reference(&state->buffer_release_ref, NULL);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500566}
567
568static void
569weston_surface_state_set_buffer(struct weston_surface_state *state,
570 struct weston_buffer *buffer)
571{
572 if (state->buffer == buffer)
573 return;
574
575 if (state->buffer)
576 wl_list_remove(&state->buffer_destroy_listener.link);
577 state->buffer = buffer;
578 if (state->buffer)
579 wl_signal_add(&state->buffer->destroy_signal,
580 &state->buffer_destroy_listener);
581}
582
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500583WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500584weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500585{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500586 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400587
Bryce Harringtonde16d892014-11-20 22:21:57 -0800588 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400589 if (surface == NULL)
590 return NULL;
591
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500592 wl_signal_init(&surface->destroy_signal);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +0800593 wl_signal_init(&surface->commit_signal);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500594
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500595 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200596 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400597
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200598 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
599 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200600 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
601 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500602
603 weston_surface_state_init(&surface->pending);
604
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400605 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500606 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300607 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400608
Jason Ekstranda7af7042013-10-12 22:38:11 -0500609 wl_list_init(&surface->views);
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +0300610 wl_list_init(&surface->paint_node_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500611
612 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400613 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500614
Pekka Paalanene67858b2013-04-25 13:57:42 +0300615 wl_list_init(&surface->subsurface_list);
616 wl_list_init(&surface->subsurface_list_pending);
617
Jason Ekstrand1e059042014-10-16 10:55:19 -0500618 weston_matrix_init(&surface->buffer_to_surface_matrix);
619 weston_matrix_init(&surface->surface_to_buffer_matrix);
620
Jonas Ådahld3414f22016-07-22 17:56:31 +0800621 wl_list_init(&surface->pointer_constraints);
622
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300623 surface->acquire_fence_fd = -1;
624
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +0530625 surface->desired_protection = WESTON_HDCP_DISABLE;
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +0530626 surface->current_protection = WESTON_HDCP_DISABLE;
627 surface->protection_mode = WESTON_SURFACE_PROTECTION_MODE_RELAXED;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +0530628
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400629 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500630}
631
Alex Wu8811bf92012-02-28 18:07:54 +0800632WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500633weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200634 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500635{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100636 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Philipp Zabel195dade2018-09-03 19:44:59 +0200637 surface->is_opaque = !(alpha < 1.0);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500638}
639
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400640WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500641weston_view_to_global_float(struct weston_view *view,
642 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200643{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500644 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200645 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
646
Jason Ekstranda7af7042013-10-12 22:38:11 -0500647 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200648
649 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200650 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700651 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200652 v.f[3]);
653 *x = 0;
654 *y = 0;
655 return;
656 }
657
658 *x = v.f[0] / v.f[3];
659 *y = v.f[1] / v.f[3];
660 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500661 *x = sx + view->geometry.x;
662 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200663 }
664}
665
Pekka Paalanen0df44772020-02-14 14:50:03 +0200666/** Transform a point to buffer coordinates
667 *
668 * \param width Surface width.
669 * \param height Surface height.
670 * \param transform Buffer transform.
671 * \param scale Buffer scale.
672 * \param sx Surface x coordinate of a point.
673 * \param sy Surface y coordinate of a point.
674 * \param[out] bx Buffer x coordinate of the point.
675 * \param[out] by Buffer Y coordinate of the point.
676 *
677 * Converts the given surface-local coordinates to buffer coordinates
678 * according to the given buffer transform and scale.
679 * This ignores wp_viewport.
680 *
681 * The given width and height must be the result of inverse scaled and
682 * inverse transformed buffer size.
683 */
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500684WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200685weston_transformed_coord(int width, int height,
686 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200687 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200688 float sx, float sy, float *bx, float *by)
689{
690 switch (transform) {
691 case WL_OUTPUT_TRANSFORM_NORMAL:
692 default:
693 *bx = sx;
694 *by = sy;
695 break;
696 case WL_OUTPUT_TRANSFORM_FLIPPED:
697 *bx = width - sx;
698 *by = sy;
699 break;
700 case WL_OUTPUT_TRANSFORM_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200701 *bx = sy;
702 *by = width - sx;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200703 break;
704 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200705 *bx = sy;
706 *by = sx;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200707 break;
708 case WL_OUTPUT_TRANSFORM_180:
709 *bx = width - sx;
710 *by = height - sy;
711 break;
712 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
713 *bx = sx;
714 *by = height - sy;
715 break;
716 case WL_OUTPUT_TRANSFORM_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200717 *bx = height - sy;
718 *by = sx;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200719 break;
720 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200721 *bx = height - sy;
722 *by = width - sx;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200723 break;
724 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200725
726 *bx *= scale;
727 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200728}
729
Pekka Paalanen0df44772020-02-14 14:50:03 +0200730/** Transform a rectangle to buffer coordinates
731 *
732 * \param width Surface width.
733 * \param height Surface height.
734 * \param transform Buffer transform.
735 * \param scale Buffer scale.
736 * \param rect Rectangle in surface coordinates.
737 * \return Rectangle in buffer coordinates.
738 *
739 * Converts the given surface-local rectangle to buffer coordinates
740 * according to the given buffer transform and scale. The resulting
741 * rectangle is guaranteed to be well-formed.
742 * This ignores wp_viewport.
743 *
744 * The given width and height must be the result of inverse scaled and
745 * inverse transformed buffer size.
746 */
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200747WL_EXPORT pixman_box32_t
748weston_transformed_rect(int width, int height,
749 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200750 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200751 pixman_box32_t rect)
752{
753 float x1, x2, y1, y2;
754
755 pixman_box32_t ret;
756
Alexander Larsson4ea95522013-05-22 14:41:37 +0200757 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200758 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200759 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200760 rect.x2, rect.y2, &x2, &y2);
761
762 if (x1 <= x2) {
763 ret.x1 = x1;
764 ret.x2 = x2;
765 } else {
766 ret.x1 = x2;
767 ret.x2 = x1;
768 }
769
770 if (y1 <= y2) {
771 ret.y1 = y1;
772 ret.y2 = y2;
773 } else {
774 ret.y1 = y2;
775 ret.y2 = y1;
776 }
777
778 return ret;
779}
780
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600781/** Transform a region by a matrix, restricted to axis-aligned transformations
782 *
783 * Warning: This function does not work for projective, affine, or matrices
784 * that encode arbitrary rotations. Only 90-degree step rotations are
785 * supported.
786 */
787WL_EXPORT void
788weston_matrix_transform_region(pixman_region32_t *dest,
789 struct weston_matrix *matrix,
790 pixman_region32_t *src)
791{
792 pixman_box32_t *src_rects, *dest_rects;
793 int nrects, i;
794
795 src_rects = pixman_region32_rectangles(src, &nrects);
796 dest_rects = malloc(nrects * sizeof(*dest_rects));
797 if (!dest_rects)
798 return;
799
800 for (i = 0; i < nrects; i++) {
801 struct weston_vector vec1 = {{
802 src_rects[i].x1, src_rects[i].y1, 0, 1
803 }};
804 weston_matrix_transform(matrix, &vec1);
805 vec1.f[0] /= vec1.f[3];
806 vec1.f[1] /= vec1.f[3];
807
808 struct weston_vector vec2 = {{
809 src_rects[i].x2, src_rects[i].y2, 0, 1
810 }};
811 weston_matrix_transform(matrix, &vec2);
812 vec2.f[0] /= vec2.f[3];
813 vec2.f[1] /= vec2.f[3];
814
815 if (vec1.f[0] < vec2.f[0]) {
816 dest_rects[i].x1 = floor(vec1.f[0]);
817 dest_rects[i].x2 = ceil(vec2.f[0]);
818 } else {
819 dest_rects[i].x1 = floor(vec2.f[0]);
820 dest_rects[i].x2 = ceil(vec1.f[0]);
821 }
822
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600823 if (vec1.f[1] < vec2.f[1]) {
824 dest_rects[i].y1 = floor(vec1.f[1]);
825 dest_rects[i].y2 = ceil(vec2.f[1]);
826 } else {
827 dest_rects[i].y1 = floor(vec2.f[1]);
828 dest_rects[i].y2 = ceil(vec1.f[1]);
829 }
830 }
831
832 pixman_region32_clear(dest);
833 pixman_region32_init_rects(dest, dest_rects, nrects);
834 free(dest_rects);
835}
836
Pekka Paalanen0df44772020-02-14 14:50:03 +0200837/** Transform a region to buffer coordinates
838 *
839 * \param width Surface width.
840 * \param height Surface height.
841 * \param transform Buffer transform.
842 * \param scale Buffer scale.
843 * \param[in] src Region in surface coordinates.
844 * \param[out] dest Resulting region in buffer coordinates.
845 *
846 * Converts the given surface-local region to buffer coordinates
847 * according to the given buffer transform and scale.
848 * This ignores wp_viewport.
849 *
850 * The given width and height must be the result of inverse scaled and
851 * inverse transformed buffer size.
852 *
853 * src and dest are allowed to point to the same memory for in-place conversion.
854 */
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200855WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500856weston_transformed_region(int width, int height,
857 enum wl_output_transform transform,
858 int32_t scale,
859 pixman_region32_t *src, pixman_region32_t *dest)
860{
861 pixman_box32_t *src_rects, *dest_rects;
862 int nrects, i;
863
864 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
865 if (src != dest)
866 pixman_region32_copy(dest, src);
867 return;
868 }
869
870 src_rects = pixman_region32_rectangles(src, &nrects);
871 dest_rects = malloc(nrects * sizeof(*dest_rects));
872 if (!dest_rects)
873 return;
874
875 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
876 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
877 } else {
878 for (i = 0; i < nrects; i++) {
879 switch (transform) {
880 default:
881 case WL_OUTPUT_TRANSFORM_NORMAL:
882 dest_rects[i].x1 = src_rects[i].x1;
883 dest_rects[i].y1 = src_rects[i].y1;
884 dest_rects[i].x2 = src_rects[i].x2;
885 dest_rects[i].y2 = src_rects[i].y2;
886 break;
887 case WL_OUTPUT_TRANSFORM_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200888 dest_rects[i].x1 = src_rects[i].y1;
889 dest_rects[i].y1 = width - src_rects[i].x2;
890 dest_rects[i].x2 = src_rects[i].y2;
891 dest_rects[i].y2 = width - src_rects[i].x1;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500892 break;
893 case WL_OUTPUT_TRANSFORM_180:
894 dest_rects[i].x1 = width - src_rects[i].x2;
895 dest_rects[i].y1 = height - src_rects[i].y2;
896 dest_rects[i].x2 = width - src_rects[i].x1;
897 dest_rects[i].y2 = height - src_rects[i].y1;
898 break;
899 case WL_OUTPUT_TRANSFORM_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200900 dest_rects[i].x1 = height - src_rects[i].y2;
901 dest_rects[i].y1 = src_rects[i].x1;
902 dest_rects[i].x2 = height - src_rects[i].y1;
903 dest_rects[i].y2 = src_rects[i].x2;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500904 break;
905 case WL_OUTPUT_TRANSFORM_FLIPPED:
906 dest_rects[i].x1 = width - src_rects[i].x2;
907 dest_rects[i].y1 = src_rects[i].y1;
908 dest_rects[i].x2 = width - src_rects[i].x1;
909 dest_rects[i].y2 = src_rects[i].y2;
910 break;
911 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200912 dest_rects[i].x1 = src_rects[i].y1;
913 dest_rects[i].y1 = src_rects[i].x1;
914 dest_rects[i].x2 = src_rects[i].y2;
915 dest_rects[i].y2 = src_rects[i].x2;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500916 break;
917 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
918 dest_rects[i].x1 = src_rects[i].x1;
919 dest_rects[i].y1 = height - src_rects[i].y2;
920 dest_rects[i].x2 = src_rects[i].x2;
921 dest_rects[i].y2 = height - src_rects[i].y1;
922 break;
923 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200924 dest_rects[i].x1 = height - src_rects[i].y2;
925 dest_rects[i].y1 = width - src_rects[i].x2;
926 dest_rects[i].x2 = height - src_rects[i].y1;
927 dest_rects[i].y2 = width - src_rects[i].x1;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500928 break;
929 }
930 }
931 }
932
933 if (scale != 1) {
934 for (i = 0; i < nrects; i++) {
935 dest_rects[i].x1 *= scale;
936 dest_rects[i].x2 *= scale;
937 dest_rects[i].y1 *= scale;
938 dest_rects[i].y2 *= scale;
939 }
940 }
941
942 pixman_region32_clear(dest);
943 pixman_region32_init_rects(dest, dest_rects, nrects);
944 free(dest_rects);
945}
946
Jonny Lamb74130762013-11-26 18:19:46 +0100947static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300948viewport_surface_to_buffer(struct weston_surface *surface,
949 float sx, float sy, float *bx, float *by)
Jonny Lamb74130762013-11-26 18:19:46 +0100950{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200951 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200952 double src_width, src_height;
953 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200954
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200955 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
956 if (vp->surface.width == -1) {
957 *bx = sx;
958 *by = sy;
959 return;
960 }
Jonny Lamb74130762013-11-26 18:19:46 +0100961
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200962 src_x = 0.0;
963 src_y = 0.0;
964 src_width = surface->width_from_buffer;
965 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100966 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200967 src_x = wl_fixed_to_double(vp->buffer.src_x);
968 src_y = wl_fixed_to_double(vp->buffer.src_y);
969 src_width = wl_fixed_to_double(vp->buffer.src_width);
970 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100971 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200972
973 *bx = sx * src_width / surface->width + src_x;
974 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100975}
976
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500977WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200978weston_surface_to_buffer_float(struct weston_surface *surface,
979 float sx, float sy, float *bx, float *by)
980{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200981 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
982
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300983 /* first transform coordinates if the viewport is set */
984 viewport_surface_to_buffer(surface, sx, sy, bx, by);
Jonny Lamb74130762013-11-26 18:19:46 +0100985
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500986 weston_transformed_coord(surface->width_from_buffer,
987 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200988 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100989 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200990}
991
Derek Foreman99739672015-12-03 16:38:11 -0600992/** Transform a rectangle from surface coordinates to buffer coordinates
993 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300994 * \param surface The surface to fetch wp_viewport and buffer transformation
Derek Foreman99739672015-12-03 16:38:11 -0600995 * from.
996 * \param rect The rectangle to transform.
997 * \return The transformed rectangle.
998 *
999 * Viewport and buffer transformations can only do translation, scaling,
1000 * and rotations in 90-degree steps. Therefore the only loss in the
1001 * conversion is coordinate rounding.
1002 *
1003 * However, some coordinate rounding takes place as an intermediate
1004 * step before the buffer scale factor is applied, so the rectangle
1005 * boundary may not be exactly as expected.
1006 *
1007 * This is OK for damage tracking since a little extra coverage is
1008 * not a problem.
1009 */
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +02001010WL_EXPORT pixman_box32_t
1011weston_surface_to_buffer_rect(struct weston_surface *surface,
1012 pixman_box32_t rect)
1013{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001014 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +01001015 float xf, yf;
1016
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03001017 /* first transform box coordinates if the viewport is set */
1018 viewport_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
Jonny Lamb74130762013-11-26 18:19:46 +01001019 rect.x1 = floorf(xf);
1020 rect.y1 = floorf(yf);
1021
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03001022 viewport_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
Derek Foremane2e15ac2015-12-01 13:00:43 -06001023 rect.x2 = ceilf(xf);
1024 rect.y2 = ceilf(yf);
Jonny Lamb74130762013-11-26 18:19:46 +01001025
Jason Ekstrandd0cebc32014-04-21 20:56:46 -05001026 return weston_transformed_rect(surface->width_from_buffer,
1027 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001028 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +02001029 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +02001030}
1031
Pekka Paalanene54e31c2015-03-04 14:23:28 +02001032/** Transform a region from surface coordinates to buffer coordinates
1033 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03001034 * \param surface The surface to fetch wp_viewport and buffer transformation
Pekka Paalanene54e31c2015-03-04 14:23:28 +02001035 * from.
Marius Vlada2dace22019-06-12 16:05:44 +03001036 * \param[in] surface_region The region in surface coordinates.
1037 * \param[out] buffer_region The region converted to buffer coordinates.
Pekka Paalanene54e31c2015-03-04 14:23:28 +02001038 *
1039 * Buffer_region must be init'd, but will be completely overwritten.
1040 *
1041 * Viewport and buffer transformations can only do translation, scaling,
1042 * and rotations in 90-degree steps. Therefore the only loss in the
Derek Foreman99739672015-12-03 16:38:11 -06001043 * conversion is from the coordinate rounding that takes place in
1044 * \ref weston_surface_to_buffer_rect.
Marius Vlada2dace22019-06-12 16:05:44 +03001045 *
Pekka Paalanene54e31c2015-03-04 14:23:28 +02001046 */
1047WL_EXPORT void
1048weston_surface_to_buffer_region(struct weston_surface *surface,
1049 pixman_region32_t *surface_region,
1050 pixman_region32_t *buffer_region)
1051{
1052 pixman_box32_t *src_rects, *dest_rects;
1053 int nrects, i;
1054
1055 src_rects = pixman_region32_rectangles(surface_region, &nrects);
1056 dest_rects = malloc(nrects * sizeof(*dest_rects));
1057 if (!dest_rects)
1058 return;
1059
1060 for (i = 0; i < nrects; i++) {
1061 dest_rects[i] = weston_surface_to_buffer_rect(surface,
1062 src_rects[i]);
1063 }
1064
1065 pixman_region32_fini(buffer_region);
1066 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
1067 free(dest_rects);
1068}
1069
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +02001070WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001071weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001072 struct weston_plane *plane)
1073{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001074 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001075 return;
1076
Jason Ekstranda7af7042013-10-12 22:38:11 -05001077 weston_view_damage_below(view);
1078 view->plane = plane;
1079 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001080}
1081
Pekka Paalanen51723d52015-02-17 13:10:01 +02001082/** Inflict damage on the plane where the view is visible.
1083 *
1084 * \param view The view that causes the damage.
1085 *
1086 * If the view is currently on a plane (including the primary plane),
1087 * take the view's boundingbox, subtract all the opaque views that cover it,
1088 * and add the remaining region as damage to the plane. This corresponds
1089 * to the damage inflicted to the plane if this view disappeared.
1090 *
1091 * A repaint is scheduled for this view.
1092 *
1093 * The region of all opaque views covering this view is stored in
1094 * weston_view::clip and updated by view_accumulate_damage() during
1095 * weston_output_repaint(). Specifically, that region matches the
1096 * scenegraph as it was last painted.
1097 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001098WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001099weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001100{
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001101 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001102
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001103 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001104 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001105 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +08001106 if (view->plane)
1107 pixman_region32_union(&view->plane->damage,
1108 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001109 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -08001110 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001111}
1112
Pekka Paalanen01e00682017-03-24 16:21:06 +02001113/** Send wl_surface.enter/leave events
1114 *
1115 * \param surface The surface.
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03001116 * \param head A head of the entered/left output.
Pekka Paalanen01e00682017-03-24 16:21:06 +02001117 * \param enter True if entered.
Marius Vlada2dace22019-06-12 16:05:44 +03001118 * \param leave True if left.
Pekka Paalanen01e00682017-03-24 16:21:06 +02001119 *
1120 * Send the enter/leave events for all protocol objects bound to the given
1121 * output by the client owning the surface.
1122 */
1123static void
1124weston_surface_send_enter_leave(struct weston_surface *surface,
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03001125 struct weston_head *head,
Pekka Paalanen01e00682017-03-24 16:21:06 +02001126 bool enter,
1127 bool leave)
1128{
1129 struct wl_resource *wloutput;
1130 struct wl_client *client;
1131
1132 assert(enter != leave);
1133
1134 client = wl_resource_get_client(surface->resource);
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03001135 wl_resource_for_each(wloutput, &head->resource_list) {
Pekka Paalanen01e00682017-03-24 16:21:06 +02001136 if (wl_resource_get_client(wloutput) != client)
1137 continue;
1138
1139 if (enter)
1140 wl_surface_send_enter(surface->resource, wloutput);
1141 if (leave)
1142 wl_surface_send_leave(surface->resource, wloutput);
1143 }
1144}
1145
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301146static void
1147weston_surface_compute_protection(struct protected_surface *psurface)
1148{
1149 enum weston_hdcp_protection min_protection;
1150 bool min_protection_valid = false;
1151 struct weston_surface *surface = psurface->surface;
1152 struct weston_output *output;
1153
1154 wl_list_for_each(output, &surface->compositor->output_list, link)
1155 if (surface->output_mask & (1u << output->id)) {
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05301156 /*
1157 * If the content-protection is enabled with protection
1158 * mode as RELAXED for a surface, and if
1159 * content-recording features like: screen-shooter,
1160 * recorder, screen-sharing, etc are on, then notify the
1161 * client, that the protection is disabled.
1162 *
1163 * Note: If the protection mode is ENFORCED then there
1164 * is no need to bother the client as the renderer takes
1165 * care of censoring the visibility of the protected
1166 * content.
1167 */
1168
1169 if (output->disable_planes > 0 &&
1170 surface->protection_mode == WESTON_SURFACE_PROTECTION_MODE_RELAXED) {
1171 min_protection = WESTON_HDCP_DISABLE;
1172 min_protection_valid = true;
1173 break;
1174 }
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301175 if (!min_protection_valid) {
1176 min_protection = output->current_protection;
1177 min_protection_valid = true;
1178 }
1179 if (output->current_protection < min_protection)
1180 min_protection = output->current_protection;
1181 }
1182 if (!min_protection_valid)
1183 min_protection = WESTON_HDCP_DISABLE;
1184
1185 surface->current_protection = min_protection;
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05301186
1187 weston_protected_surface_send_event(psurface, surface->current_protection);
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301188}
1189
1190static void
1191notify_surface_protection_change(void *data)
1192{
1193 struct weston_compositor *compositor = data;
1194 struct content_protection *cp;
1195 struct protected_surface *psurface;
1196
1197 cp = compositor->content_protection;
1198 cp->surface_protection_update = NULL;
1199
1200 /* Notify the clients, whose surfaces are changed */
1201 wl_list_for_each(psurface, &cp->protected_list, link)
1202 if (psurface && psurface->surface)
1203 weston_surface_compute_protection(psurface);
1204}
1205
Bryce Harrington3f650b82015-12-23 11:01:58 -08001206/**
Ankit Nautiyalfaa5ab42019-07-08 13:23:24 +05301207 * \param compositor weston_compositor
1208 *
1209 * Schedule an idle task to notify surface about the update in protection,
1210 * if not already scheduled.
1211 */
1212static void
1213weston_schedule_surface_protection_update(struct weston_compositor *compositor)
1214{
1215 struct content_protection *cp = compositor->content_protection;
1216 struct wl_event_loop *loop;
1217
1218 if (!cp || cp->surface_protection_update)
1219 return;
1220 loop = wl_display_get_event_loop(compositor->wl_display);
1221 cp->surface_protection_update = wl_event_loop_add_idle(loop,
1222 notify_surface_protection_change,
1223 compositor);
1224}
1225
1226/**
Bryce Harrington3f650b82015-12-23 11:01:58 -08001227 * \param es The surface
1228 * \param mask The new set of outputs for the surface
1229 *
1230 * Sets the surface's set of outputs to the ones specified by
1231 * the new output mask provided. Identifies the outputs that
1232 * have changed, the posts enter and leave events for these
1233 * outputs as appropriate.
1234 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001235static void
1236weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
1237{
1238 uint32_t different = es->output_mask ^ mask;
1239 uint32_t entered = mask & different;
1240 uint32_t left = es->output_mask & different;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001241 uint32_t output_bit;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001242 struct weston_output *output;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001243 struct weston_head *head;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001244
1245 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001246 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001247 return;
1248 if (different == 0)
1249 return;
1250
1251 wl_list_for_each(output, &es->compositor->output_list, link) {
Pekka Paalanen01e00682017-03-24 16:21:06 +02001252 output_bit = 1u << output->id;
1253 if (!(output_bit & different))
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001254 continue;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001255
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001256 wl_list_for_each(head, &output->head_list, output_link) {
1257 weston_surface_send_enter_leave(es, head,
1258 output_bit & entered,
1259 output_bit & left);
1260 }
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001261 }
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301262 /*
1263 * Change in surfaces' output mask might trigger a change in its
1264 * protection.
1265 */
Ankit Nautiyalfaa5ab42019-07-08 13:23:24 +05301266 weston_schedule_surface_protection_update(es->compositor);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001267}
1268
Semi Malinene7a52fb2018-04-26 11:08:10 +02001269static void
1270notify_view_output_destroy(struct wl_listener *listener, void *data)
1271{
1272 struct weston_view *view =
1273 container_of(listener,
1274 struct weston_view, output_destroy_listener);
1275
1276 view->output = NULL;
1277 view->output_destroy_listener.notify = NULL;
1278}
1279
1280/** Set the primary output of the view
1281 *
1282 * \param view The view whose primary output to set
1283 * \param output The new primary output for the view
1284 *
1285 * Set \a output to be the primary output of the \a view.
1286 *
1287 * Notice that the assignment may be temporary; the primary output could be
1288 * automatically changed. Hence, one cannot rely on the value persisting.
1289 *
1290 * Passing NULL as /a output will set the primary output to NULL.
1291 */
1292WL_EXPORT void
1293weston_view_set_output(struct weston_view *view, struct weston_output *output)
1294{
1295 if (view->output_destroy_listener.notify) {
1296 wl_list_remove(&view->output_destroy_listener.link);
1297 view->output_destroy_listener.notify = NULL;
1298 }
1299 view->output = output;
1300 if (output) {
1301 view->output_destroy_listener.notify =
1302 notify_view_output_destroy;
1303 wl_signal_add(&output->destroy_signal,
1304 &view->output_destroy_listener);
1305 }
1306}
1307
Bryce Harrington3f650b82015-12-23 11:01:58 -08001308/** Recalculate which output(s) the surface has views displayed on
1309 *
1310 * \param es The surface to remap to outputs
1311 *
1312 * Finds the output that is showing the largest amount of one
1313 * of the surface's various views. This output becomes the
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001314 * surface's primary output for vsync and frame callback purposes.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001315 *
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001316 * Also notes all outputs of all of the surface's views
Bryce Harrington3f650b82015-12-23 11:01:58 -08001317 * in the output_mask for the surface.
1318 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001319static void
1320weston_surface_assign_output(struct weston_surface *es)
1321{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001322 struct weston_output *new_output;
1323 struct weston_view *view;
1324 pixman_region32_t region;
1325 uint32_t max, area, mask;
1326 pixman_box32_t *e;
1327
1328 new_output = NULL;
1329 max = 0;
1330 mask = 0;
1331 pixman_region32_init(&region);
1332 wl_list_for_each(view, &es->views, surface_link) {
1333 if (!view->output)
1334 continue;
1335
1336 pixman_region32_intersect(&region, &view->transform.boundingbox,
1337 &view->output->region);
1338
1339 e = pixman_region32_extents(&region);
1340 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1341
1342 mask |= view->output_mask;
1343
1344 if (area >= max) {
1345 new_output = view->output;
1346 max = area;
1347 }
1348 }
1349 pixman_region32_fini(&region);
1350
1351 es->output = new_output;
1352 weston_surface_update_output_mask(es, mask);
1353}
1354
Bryce Harrington3f650b82015-12-23 11:01:58 -08001355/** Recalculate which output(s) the view is displayed on
1356 *
1357 * \param ev The view to remap to outputs
1358 *
1359 * Identifies the set of outputs that the view is visible on,
1360 * noting them into the output_mask. The output that the view
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001361 * is most visible on is set as the view's primary output.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001362 *
1363 * Also does the same for the view's surface. See
1364 * weston_surface_assign_output().
1365 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001366static void
1367weston_view_assign_output(struct weston_view *ev)
1368{
1369 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001370 struct weston_output *output, *new_output;
1371 pixman_region32_t region;
1372 uint32_t max, area, mask;
1373 pixman_box32_t *e;
1374
1375 new_output = NULL;
1376 max = 0;
1377 mask = 0;
1378 pixman_region32_init(&region);
1379 wl_list_for_each(output, &ec->output_list, link) {
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03001380 if (output->destroying)
1381 continue;
1382
Jason Ekstranda7af7042013-10-12 22:38:11 -05001383 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001384 &output->region);
1385
1386 e = pixman_region32_extents(&region);
1387 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1388
1389 if (area > 0)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001390 mask |= 1u << output->id;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001391
1392 if (area >= max) {
1393 new_output = output;
1394 max = area;
1395 }
1396 }
1397 pixman_region32_fini(&region);
1398
Semi Malinene7a52fb2018-04-26 11:08:10 +02001399 weston_view_set_output(ev, new_output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001400 ev->output_mask = mask;
1401
1402 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001403}
1404
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001405static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001406weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1407 int from_x, int from_y, int *to_x, int *to_y)
1408{
1409 float x, y;
1410
1411 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1412 weston_view_from_global_float(to, x, y, &x, &y);
1413
1414 *to_x = round(x);
1415 *to_y = round(y);
1416}
1417
1418static void
1419weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1420{
1421 pixman_box32_t *a;
1422 pixman_box32_t b;
1423
1424 a = pixman_region32_extents(&from->geometry.scissor);
1425
1426 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1427 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1428
1429 pixman_region32_fini(&to->geometry.scissor);
1430 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1431}
1432
1433static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001434view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001435 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001436{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001437 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1438 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001439 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001440 { inbox->x1, inbox->y1 },
1441 { inbox->x1, inbox->y2 },
1442 { inbox->x2, inbox->y1 },
1443 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001444 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001445 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001446 int i;
1447
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001448 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001449 /* avoid rounding empty bbox to 1x1 */
1450 pixman_region32_init(bbox);
1451 return;
1452 }
1453
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001454 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001455 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001456 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001457 if (x < min_x)
1458 min_x = x;
1459 if (x > max_x)
1460 max_x = x;
1461 if (y < min_y)
1462 min_y = y;
1463 if (y > max_y)
1464 max_y = y;
1465 }
1466
Pekka Paalanen219b9822012-02-08 15:38:37 +02001467 int_x = floorf(min_x);
1468 int_y = floorf(min_y);
1469 pixman_region32_init_rect(bbox, int_x, int_y,
1470 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001471}
1472
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001473static void
Michael Olbrichcb04f982020-06-08 10:22:32 +02001474weston_view_update_transform_scissor(struct weston_view *view,
1475 pixman_region32_t *region)
1476{
1477 struct weston_view *parent = view->geometry.parent;
1478
1479 if (parent) {
1480 if (parent->geometry.scissor_enabled) {
1481 view->geometry.scissor_enabled = true;
1482 weston_view_transfer_scissor(parent, view);
1483 } else {
1484 view->geometry.scissor_enabled = false;
1485 }
1486 }
1487
1488 if (view->geometry.scissor_enabled)
1489 pixman_region32_intersect(region, region,
1490 &view->geometry.scissor);
1491}
1492static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001493weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001494{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001495 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001496
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001497 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001498 view->geometry.x = roundf(view->geometry.x);
1499 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001500
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001501 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001502 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1503 view->transform.position.matrix.d[12] = view->geometry.x;
1504 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001505
Jason Ekstranda7af7042013-10-12 22:38:11 -05001506 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001507
Jason Ekstranda7af7042013-10-12 22:38:11 -05001508 view->transform.inverse = view->transform.position.matrix;
1509 view->transform.inverse.d[12] = -view->geometry.x;
1510 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001511
Jason Ekstranda7af7042013-10-12 22:38:11 -05001512 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001513 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001514 view->surface->width,
1515 view->surface->height);
Michael Olbrichcb04f982020-06-08 10:22:32 +02001516
1517 weston_view_update_transform_scissor(view, &view->transform.boundingbox);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001518
1519 pixman_region32_translate(&view->transform.boundingbox,
1520 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001521
Jason Ekstranda7af7042013-10-12 22:38:11 -05001522 if (view->alpha == 1.0) {
1523 pixman_region32_copy(&view->transform.opaque,
1524 &view->surface->opaque);
Derek Foreman713f0232022-01-12 16:30:26 -06001525 if (view->geometry.scissor_enabled)
1526 pixman_region32_intersect(&view->transform.opaque,
1527 &view->transform.opaque,
1528 &view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001529 pixman_region32_translate(&view->transform.opaque,
1530 view->geometry.x,
1531 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001532 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001533}
1534
1535static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001536weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001537{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001538 struct weston_view *parent = view->geometry.parent;
1539 struct weston_matrix *matrix = &view->transform.matrix;
1540 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001541 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001542 pixman_region32_t surfregion;
1543 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001544
Jason Ekstranda7af7042013-10-12 22:38:11 -05001545 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001546
1547 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001548 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1549 view->transform.position.matrix.d[12] = view->geometry.x;
1550 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001551
1552 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001553 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001554 weston_matrix_multiply(matrix, &tform->matrix);
1555
Pekka Paalanen483243f2013-03-08 14:56:50 +02001556 if (parent)
1557 weston_matrix_multiply(matrix, &parent->transform.matrix);
1558
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001559 if (weston_matrix_invert(inverse, matrix) < 0) {
1560 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001561 weston_log("error: weston_view %p"
1562 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001563 return -1;
1564 }
1565
Pekka Paalanen380adf52015-02-16 14:39:11 +02001566 pixman_region32_init_rect(&surfregion, 0, 0,
1567 view->surface->width, view->surface->height);
Michael Olbrichcb04f982020-06-08 10:22:32 +02001568
1569 weston_view_update_transform_scissor(view, &surfregion);
1570
Pekka Paalanen380adf52015-02-16 14:39:11 +02001571 surfbox = pixman_region32_extents(&surfregion);
1572
1573 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1574 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001575
Michael Olbrich0e4f0972020-08-19 11:31:03 +02001576 if (view->alpha == 1.0 &&
1577 matrix->type == WESTON_MATRIX_TRANSFORM_TRANSLATE) {
1578 pixman_region32_copy(&view->transform.opaque,
1579 &view->surface->opaque);
Derek Foreman713f0232022-01-12 16:30:26 -06001580 if (view->geometry.scissor_enabled)
1581 pixman_region32_intersect(&view->transform.opaque,
1582 &view->transform.opaque,
1583 &view->geometry.scissor);
Michael Olbrich0e4f0972020-08-19 11:31:03 +02001584 pixman_region32_translate(&view->transform.opaque,
1585 matrix->d[12],
1586 matrix->d[13]);
1587 }
1588
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001589 return 0;
1590}
1591
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001592static struct weston_layer *
1593get_view_layer(struct weston_view *view)
1594{
1595 if (view->parent_view)
1596 return get_view_layer(view->parent_view);
1597 return view->layer_link.layer;
1598}
1599
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001600WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001601weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001602{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001603 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001604 struct weston_layer *layer;
1605 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001606
Jason Ekstranda7af7042013-10-12 22:38:11 -05001607 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001608 return;
1609
Pekka Paalanen483243f2013-03-08 14:56:50 +02001610 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001611 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001612
Jason Ekstranda7af7042013-10-12 22:38:11 -05001613 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001614
Jason Ekstranda7af7042013-10-12 22:38:11 -05001615 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001616
Jason Ekstranda7af7042013-10-12 22:38:11 -05001617 pixman_region32_fini(&view->transform.boundingbox);
1618 pixman_region32_fini(&view->transform.opaque);
1619 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001620
Pekka Paalanencd403622012-01-25 13:37:39 +02001621 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001622 if (view->geometry.transformation_list.next ==
1623 &view->transform.position.link &&
1624 view->geometry.transformation_list.prev ==
1625 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001626 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001627 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001628 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001629 if (weston_view_update_transform_enable(view) < 0)
1630 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001631 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001632
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001633 layer = get_view_layer(view);
1634 if (layer) {
1635 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001636 pixman_region32_intersect(&view->transform.boundingbox,
1637 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001638 pixman_region32_intersect(&view->transform.opaque,
1639 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001640 pixman_region32_fini(&mask);
1641 }
1642
Jason Ekstranda7af7042013-10-12 22:38:11 -05001643 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001644
Jason Ekstranda7af7042013-10-12 22:38:11 -05001645 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001646
Jason Ekstranda7af7042013-10-12 22:38:11 -05001647 wl_signal_emit(&view->surface->compositor->transform_signal,
1648 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001649}
1650
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001651WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001652weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001653{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001654 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001655
1656 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001657 * The invariant: if view->geometry.dirty, then all views
1658 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001659 * Corollary: if not parent->geometry.dirty, then all ancestors
1660 * are not dirty.
1661 */
1662
Jason Ekstranda7af7042013-10-12 22:38:11 -05001663 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001664 return;
1665
Jason Ekstranda7af7042013-10-12 22:38:11 -05001666 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001667
Jason Ekstranda7af7042013-10-12 22:38:11 -05001668 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001669 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001670 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001671}
1672
1673WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001674weston_view_to_global_fixed(struct weston_view *view,
1675 wl_fixed_t vx, wl_fixed_t vy,
1676 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001677{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001678 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001679
Jason Ekstranda7af7042013-10-12 22:38:11 -05001680 weston_view_to_global_float(view,
1681 wl_fixed_to_double(vx),
1682 wl_fixed_to_double(vy),
1683 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001684 *x = wl_fixed_from_double(xf);
1685 *y = wl_fixed_from_double(yf);
1686}
1687
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001688WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001689weston_view_from_global_float(struct weston_view *view,
1690 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001691{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001692 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001693 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1694
Jason Ekstranda7af7042013-10-12 22:38:11 -05001695 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001696
1697 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001698 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001699 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001700 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001701 *vx = 0;
1702 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001703 return;
1704 }
1705
Jason Ekstranda7af7042013-10-12 22:38:11 -05001706 *vx = v.f[0] / v.f[3];
1707 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001708 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001709 *vx = x - view->geometry.x;
1710 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001711 }
1712}
1713
1714WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001715weston_view_from_global_fixed(struct weston_view *view,
1716 wl_fixed_t x, wl_fixed_t y,
1717 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001718{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001719 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001720
Jason Ekstranda7af7042013-10-12 22:38:11 -05001721 weston_view_from_global_float(view,
1722 wl_fixed_to_double(x),
1723 wl_fixed_to_double(y),
1724 &vxf, &vyf);
1725 *vx = wl_fixed_from_double(vxf);
1726 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001727}
1728
1729WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001730weston_view_from_global(struct weston_view *view,
1731 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001732{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001733 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001734
Jason Ekstranda7af7042013-10-12 22:38:11 -05001735 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1736 *vx = floorf(vxf);
1737 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001738}
1739
Bryce Harrington3f650b82015-12-23 11:01:58 -08001740/**
1741 * \param surface The surface to be repainted
1742 *
1743 * Marks the output(s) that the surface is shown on as needing to be
1744 * repainted. See weston_output_schedule_repaint().
1745 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001746WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001747weston_surface_schedule_repaint(struct weston_surface *surface)
1748{
1749 struct weston_output *output;
1750
1751 wl_list_for_each(output, &surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001752 if (surface->output_mask & (1u << output->id))
Kristian Høgsberg98238702012-08-03 16:29:12 -04001753 weston_output_schedule_repaint(output);
1754}
1755
Bryce Harrington3f650b82015-12-23 11:01:58 -08001756/**
1757 * \param view The view to be repainted
1758 *
1759 * Marks the output(s) that the view is shown on as needing to be
1760 * repainted. See weston_output_schedule_repaint().
1761 */
Kristian Høgsberg98238702012-08-03 16:29:12 -04001762WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001763weston_view_schedule_repaint(struct weston_view *view)
1764{
1765 struct weston_output *output;
1766
1767 wl_list_for_each(output, &view->surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001768 if (view->output_mask & (1u << output->id))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001769 weston_output_schedule_repaint(output);
1770}
1771
Pekka Paalanene508ce62015-02-19 13:59:55 +02001772/**
1773 * XXX: This function does it the wrong way.
1774 * surface->damage is the damage from the client, and causes
1775 * surface_flush_damage() to copy pixels. No window management action can
1776 * cause damage to the client-provided content, warranting re-upload!
1777 *
1778 * Instead of surface->damage, this function should record the damage
1779 * with all the views for this surface to avoid extraneous texture
1780 * uploads.
1781 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001782WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001783weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001784{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001785 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001786 0, 0, surface->width,
1787 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001788
Kristian Høgsberg98238702012-08-03 16:29:12 -04001789 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001790}
1791
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001792WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001793weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001794{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001795 if (view->geometry.x == x && view->geometry.y == y)
1796 return;
1797
Jason Ekstranda7af7042013-10-12 22:38:11 -05001798 view->geometry.x = x;
1799 view->geometry.y = y;
1800 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001801}
1802
Pekka Paalanen483243f2013-03-08 14:56:50 +02001803static void
1804transform_parent_handle_parent_destroy(struct wl_listener *listener,
1805 void *data)
1806{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001807 struct weston_view *view =
1808 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001809 geometry.parent_destroy_listener);
1810
Jason Ekstranda7af7042013-10-12 22:38:11 -05001811 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001812}
1813
1814WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001815weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001816 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001817{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001818 if (view->geometry.parent) {
1819 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1820 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001821
1822 if (!parent)
1823 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001824 }
1825
Jason Ekstranda7af7042013-10-12 22:38:11 -05001826 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001827
Jason Ekstranda7af7042013-10-12 22:38:11 -05001828 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001829 transform_parent_handle_parent_destroy;
1830 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001831 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001832 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001833 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001834 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001835 }
1836
Jason Ekstranda7af7042013-10-12 22:38:11 -05001837 weston_view_geometry_dirty(view);
1838}
1839
Pekka Paalanen380adf52015-02-16 14:39:11 +02001840/** Set a clip mask rectangle on a view
1841 *
1842 * \param view The view to set the clip mask on.
1843 * \param x Top-left corner X coordinate of the clip rectangle.
1844 * \param y Top-left corner Y coordinate of the clip rectangle.
1845 * \param width Width of the clip rectangle, non-negative.
1846 * \param height Height of the clip rectangle, non-negative.
1847 *
1848 * A shell may set a clip mask rectangle on a view. Everything outside
1849 * the rectangle is cut away for input and output purposes: it is
1850 * not drawn and cannot be hit by hit-test based input like pointer
1851 * motion or touch-downs. Everything inside the rectangle will behave
1852 * normally. Clients are unaware of clipping.
1853 *
Yong Bakos4c72e292016-04-28 11:59:10 -05001854 * The rectangle is set in surface-local coordinates. Setting a clip
Pekka Paalanen380adf52015-02-16 14:39:11 +02001855 * mask rectangle does not affect the view position, the view is positioned
1856 * as it would be without a clip. The clip also does not change
1857 * weston_surface::width,height.
1858 *
1859 * The clip mask rectangle is part of transformation inheritance
1860 * (weston_view_set_transform_parent()). A clip set in the root of the
1861 * transformation inheritance tree will affect all views in the tree.
1862 * A clip can be set only on the root view. Attempting to set a clip
1863 * on view that has a transformation parent will fail. Assigning a parent
1864 * to a view that has a clip set will cause the clip to be forgotten.
1865 *
1866 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1867 * on the additional transformations in the child views. These transformations
1868 * may not rotate the coordinate axes, i.e., only translation and scaling
1869 * are allowed. Violating this restriction causes the clipping to malfunction.
1870 * Furthermore, using scaling may cause rounding errors in child clipping.
1871 *
1872 * The clip mask rectangle is not automatically adjusted based on
1873 * wl_surface.attach dx and dy arguments.
1874 *
1875 * A clip mask rectangle can be set only if the compositor capability
1876 * WESTON_CAP_VIEW_CLIP_MASK is present.
1877 *
1878 * This function sets the clip mask rectangle and schedules a repaint for
1879 * the view.
1880 */
1881WL_EXPORT void
1882weston_view_set_mask(struct weston_view *view,
1883 int x, int y, int width, int height)
1884{
1885 struct weston_compositor *compositor = view->surface->compositor;
1886
1887 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1888 weston_log("%s not allowed without capability!\n", __func__);
1889 return;
1890 }
1891
1892 if (view->geometry.parent) {
1893 weston_log("view %p has a parent, clip forbidden!\n", view);
1894 return;
1895 }
1896
1897 if (width < 0 || height < 0) {
1898 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1899 x, y, width, height);
1900 return;
1901 }
1902
1903 pixman_region32_fini(&view->geometry.scissor);
1904 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1905 view->geometry.scissor_enabled = true;
1906 weston_view_geometry_dirty(view);
1907 weston_view_schedule_repaint(view);
1908}
1909
1910/** Remove the clip mask from a view
1911 *
1912 * \param view The view to remove the clip mask from.
1913 *
1914 * Removed the clip mask rectangle and schedules a repaint.
1915 *
1916 * \sa weston_view_set_mask
1917 */
1918WL_EXPORT void
1919weston_view_set_mask_infinite(struct weston_view *view)
1920{
1921 view->geometry.scissor_enabled = false;
1922 weston_view_geometry_dirty(view);
1923 weston_view_schedule_repaint(view);
1924}
1925
Armin Krezović0da12b82016-06-30 06:04:33 +02001926/* Check if view should be displayed
1927 *
1928 * The indicator is set manually when assigning
1929 * a view to a surface.
1930 *
1931 * This needs reworking. See the thread starting at:
1932 *
1933 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1934 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001935WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001936weston_view_is_mapped(struct weston_view *view)
1937{
Armin Krezović0da12b82016-06-30 06:04:33 +02001938 return view->is_mapped;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001939}
1940
Philipp Zabel70decd52018-09-03 20:11:15 +02001941/* Check if view is opaque in specified region
1942 *
1943 * \param view The view to check for opacity.
1944 * \param region The region to check for opacity, in view coordinates.
1945 *
1946 * Returns true if the view is opaque in the specified region, because view
1947 * alpha is 1.0 and either the opaque region set by the client contains the
1948 * specified region, or the buffer pixel format or solid color is opaque.
1949 */
1950WL_EXPORT bool
1951weston_view_is_opaque(struct weston_view *ev, pixman_region32_t *region)
1952{
1953 pixman_region32_t r;
1954 bool ret = false;
1955
1956 if (ev->alpha < 1.0)
1957 return false;
1958
1959 if (ev->surface->is_opaque)
1960 return true;
1961
Marius Vlad67b382c2020-08-21 13:57:54 +03001962 if (ev->transform.dirty)
Philipp Zabel70decd52018-09-03 20:11:15 +02001963 return false;
Philipp Zabel70decd52018-09-03 20:11:15 +02001964
1965 pixman_region32_init(&r);
1966 pixman_region32_subtract(&r, region, &ev->transform.opaque);
1967
1968 if (!pixman_region32_not_empty(&r))
1969 ret = true;
1970
1971 pixman_region32_fini(&r);
1972
1973 return ret;
1974}
1975
Marius Vlad5f6bee42019-09-11 16:41:04 +03001976/** Check if the view has a valid buffer available
1977 *
1978 * @param ev The view to check if it has a valid buffer.
1979 *
1980 * Returns true if the view has a valid buffer or false otherwise.
1981 */
1982WL_EXPORT bool
1983weston_view_has_valid_buffer(struct weston_view *ev)
1984{
1985 return ev->surface->buffer_ref.buffer != NULL;
1986}
1987
Marius Vlad47e3d1e2019-09-11 16:53:08 +03001988/** Check if the view matches the entire output
1989 *
1990 * @param ev The view to check.
1991 * @param output The output to check against.
1992 *
1993 * Returns true if the view does indeed matches the entire output.
1994 */
1995WL_EXPORT bool
1996weston_view_matches_output_entirely(struct weston_view *ev,
1997 struct weston_output *output)
1998{
1999 pixman_box32_t *extents =
2000 pixman_region32_extents(&ev->transform.boundingbox);
2001
2002 if (extents->x1 != output->x ||
2003 extents->y1 != output->y ||
2004 extents->x2 != output->x + output->width ||
2005 extents->y2 != output->y + output->height)
2006 return false;
2007
2008 return true;
2009}
2010
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002011/** Find paint node for the given view and output
2012 */
2013WL_EXPORT struct weston_paint_node *
2014weston_view_find_paint_node(struct weston_view *view,
2015 struct weston_output *output)
2016{
2017 struct weston_paint_node *pnode;
2018
2019 wl_list_for_each(pnode, &view->paint_node_list, view_link) {
2020 assert(pnode->surface == view->surface);
2021 if (pnode->output == output)
2022 return pnode;
2023 }
2024
2025 return NULL;
2026}
2027
Armin Krezović0da12b82016-06-30 06:04:33 +02002028/* Check if a surface has a view assigned to it
2029 *
2030 * The indicator is set manually when mapping
2031 * a surface and creating a view for it.
2032 *
2033 * This needs to go. See the thread starting at:
2034 *
2035 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
2036 *
2037 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05002038WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03002039weston_surface_is_mapped(struct weston_surface *surface)
2040{
Armin Krezović0da12b82016-06-30 06:04:33 +02002041 return surface->is_mapped;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03002042}
2043
Pekka Paalanenda75ee12013-11-26 18:19:43 +01002044static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06002045surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002046{
2047 struct weston_view *view;
2048
2049 if (surface->width == width && surface->height == height)
2050 return;
2051
2052 surface->width = width;
2053 surface->height = height;
2054
2055 wl_list_for_each(view, &surface->views, surface_link)
2056 weston_view_geometry_dirty(view);
2057}
2058
Jason Ekstrand5c11a332013-12-04 20:32:03 -06002059WL_EXPORT void
2060weston_surface_set_size(struct weston_surface *surface,
2061 int32_t width, int32_t height)
2062{
2063 assert(!surface->resource);
2064 surface_set_size(surface, width, height);
2065}
2066
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02002067static int
2068fixed_round_up_to_int(wl_fixed_t f)
2069{
2070 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
2071}
2072
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002073static void
Pekka Paalanen59987fa2016-04-26 15:50:59 +03002074convert_size_by_transform_scale(int32_t *width_out, int32_t *height_out,
2075 int32_t width, int32_t height,
2076 uint32_t transform,
2077 int32_t scale)
2078{
2079 assert(scale > 0);
2080
2081 switch (transform) {
2082 case WL_OUTPUT_TRANSFORM_NORMAL:
2083 case WL_OUTPUT_TRANSFORM_180:
2084 case WL_OUTPUT_TRANSFORM_FLIPPED:
2085 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2086 *width_out = width / scale;
2087 *height_out = height / scale;
2088 break;
2089 case WL_OUTPUT_TRANSFORM_90:
2090 case WL_OUTPUT_TRANSFORM_270:
2091 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2092 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2093 *width_out = height / scale;
2094 *height_out = width / scale;
2095 break;
2096 default:
2097 assert(0 && "invalid transform");
2098 }
2099}
2100
2101static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002102weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002103{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002104 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01002105
2106 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02002107 surface->width_from_buffer = 0;
2108 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01002109 return;
2110 }
2111
Pekka Paalanen59987fa2016-04-26 15:50:59 +03002112 convert_size_by_transform_scale(&surface->width_from_buffer,
2113 &surface->height_from_buffer,
2114 surface->buffer_ref.buffer->width,
2115 surface->buffer_ref.buffer->height,
2116 vp->buffer.transform,
2117 vp->buffer.scale);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002118}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02002119
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002120static void
2121weston_surface_update_size(struct weston_surface *surface)
2122{
2123 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
2124 int32_t width, height;
2125
2126 width = surface->width_from_buffer;
2127 height = surface->height_from_buffer;
2128
2129 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02002130 surface_set_size(surface,
2131 vp->surface.width, vp->surface.height);
2132 return;
2133 }
2134
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002135 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03002136 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
2137 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
2138
2139 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02002140 return;
2141 }
2142
Jason Ekstrand5c11a332013-12-04 20:32:03 -06002143 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002144}
2145
Marius Vlad9fdda7f2019-06-11 16:08:55 +03002146/** weston_compositor_get_time
2147 * \ingroup compositor
2148 */
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02002149WL_EXPORT void
2150weston_compositor_get_time(struct timespec *time)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05002151{
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02002152 clock_gettime(CLOCK_REALTIME, time);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05002153}
2154
Marius Vlad9fdda7f2019-06-11 16:08:55 +03002155/** weston_compositor_pick_view
2156 * \ingroup compositor
2157 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002158WL_EXPORT struct weston_view *
2159weston_compositor_pick_view(struct weston_compositor *compositor,
2160 wl_fixed_t x, wl_fixed_t y,
2161 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02002162{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002163 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02002164 wl_fixed_t view_x, view_y;
2165 int view_ix, view_iy;
2166 int ix = wl_fixed_to_int(x);
2167 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02002168
Pekka Paalanen6551c092021-05-03 16:09:45 +03002169 /* Can't use paint node list: occlusion by input regions, not opaque. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002170 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02002171 if (!pixman_region32_contains_point(
2172 &view->transform.boundingbox, ix, iy, NULL))
2173 continue;
2174
2175 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
2176 view_ix = wl_fixed_to_int(view_x);
2177 view_iy = wl_fixed_to_int(view_y);
2178
2179 if (!pixman_region32_contains_point(&view->surface->input,
2180 view_ix, view_iy, NULL))
2181 continue;
2182
Pekka Paalanen380adf52015-02-16 14:39:11 +02002183 if (view->geometry.scissor_enabled &&
2184 !pixman_region32_contains_point(&view->geometry.scissor,
2185 view_ix, view_iy, NULL))
2186 continue;
2187
Pekka Paalanenfc22a522015-02-18 15:08:29 +02002188 *vx = view_x;
2189 *vy = view_y;
2190 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02002191 }
2192
Derek Foremanf9318d12015-05-11 15:40:11 -05002193 *vx = wl_fixed_from_int(-1000000);
2194 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02002195 return NULL;
2196}
2197
2198static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002199weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002200{
Daniel Stone37816df2012-05-16 18:45:18 +01002201 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002202
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07002203 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002204 return;
2205
Daniel Stone37816df2012-05-16 18:45:18 +01002206 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04002207 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002208}
2209
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002210WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002211weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05002212{
Daniel Stone4dab5db2012-05-30 16:31:53 +01002213 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002214
Jason Ekstranda7af7042013-10-12 22:38:11 -05002215 if (!weston_view_is_mapped(view))
2216 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002217
Jason Ekstranda7af7042013-10-12 22:38:11 -05002218 weston_view_damage_below(view);
Semi Malinene7a52fb2018-04-26 11:08:10 +02002219 weston_view_set_output(view, NULL);
Xiong Zhang97116532013-10-23 13:58:31 +08002220 view->plane = NULL;
Armin Krezovićf8486c32016-06-30 06:04:28 +02002221 view->is_mapped = false;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002222 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002223 wl_list_remove(&view->link);
2224 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002225 view->output_mask = 0;
2226 weston_surface_assign_output(view->surface);
2227
2228 if (weston_surface_is_mapped(view->surface))
2229 return;
2230
2231 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002232 struct weston_touch *touch = weston_seat_get_touch(seat);
2233 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2234 struct weston_keyboard *keyboard =
2235 weston_seat_get_keyboard(seat);
2236
2237 if (keyboard && keyboard->focus == view->surface)
2238 weston_keyboard_set_focus(keyboard, NULL);
2239 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05002240 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05002241 if (touch && touch->focus == view)
2242 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01002243 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002244}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002245
Jason Ekstranda7af7042013-10-12 22:38:11 -05002246WL_EXPORT void
2247weston_surface_unmap(struct weston_surface *surface)
2248{
2249 struct weston_view *view;
2250
Armin Krezovićf8486c32016-06-30 06:04:28 +02002251 surface->is_mapped = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002252 wl_list_for_each(view, &surface->views, surface_link)
2253 weston_view_unmap(view);
2254 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05002255}
2256
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002257static void
2258weston_surface_reset_pending_buffer(struct weston_surface *surface)
2259{
Jason Ekstrand7b982072014-05-20 14:33:03 -05002260 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002261 surface->pending.sx = 0;
2262 surface->pending.sy = 0;
2263 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002264 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002265}
2266
Jason Ekstranda7af7042013-10-12 22:38:11 -05002267WL_EXPORT void
2268weston_view_destroy(struct weston_view *view)
2269{
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002270 struct weston_paint_node *pnode, *pntmp;
2271
Marius Vladf2452d62021-12-09 14:07:21 +02002272 weston_signal_emit_mutable(&view->destroy_signal, view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002273
2274 assert(wl_list_empty(&view->geometry.child_list));
2275
2276 if (weston_view_is_mapped(view)) {
2277 weston_view_unmap(view);
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002278 weston_compositor_build_view_list(view->surface->compositor,
2279 NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002280 }
2281
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002282 wl_list_for_each_safe(pnode, pntmp, &view->paint_node_list, view_link)
2283 weston_paint_node_destroy(pnode);
2284
Jason Ekstranda7af7042013-10-12 22:38:11 -05002285 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002286 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002287
2288 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02002289 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002290 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002291 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002292
2293 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen944fae82018-05-22 13:15:58 +03002294 weston_view_set_output(view, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002295
Jason Ekstranda7af7042013-10-12 22:38:11 -05002296 wl_list_remove(&view->surface_link);
2297
2298 free(view);
2299}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002300
2301WL_EXPORT void
2302weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05002303{
Vlad Zahorodniic2b97472021-07-29 11:16:51 +03002304 struct wl_resource *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002305 struct weston_view *ev, *nv;
Jonas Ådahld3414f22016-07-22 17:56:31 +08002306 struct weston_pointer_constraint *constraint, *next_constraint;
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002307 struct weston_paint_node *pnode, *pntmp;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04002308
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02002309 if (--surface->ref_count > 0)
2310 return;
2311
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03002312 assert(surface->resource == NULL);
2313
Alexandros Frantzisfa48c5e2020-07-09 17:31:34 +03002314 weston_signal_emit_mutable(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02002315
Pekka Paalanene67858b2013-04-25 13:57:42 +03002316 assert(wl_list_empty(&surface->subsurface_list_pending));
2317 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02002318
Leandro Ribeiro8eb84142021-01-18 19:36:48 -03002319 if (surface->dmabuf_feedback)
2320 weston_dmabuf_feedback_destroy(surface->dmabuf_feedback);
2321
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
Vlad Zahorodniic2b97472021-07-29 11:16:51 +03002339 wl_resource_for_each_safe(cb, next, &surface->frame_callback_list)
2340 wl_resource_destroy(cb);
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
Marius Vladf2452d62021-12-09 14:07:21 +02002383 weston_signal_emit_mutable(&buffer->destroy_signal, buffer);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002384 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;
Vlad Zahorodniic2b97472021-07-29 11:16:51 +03002873 struct wl_resource *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
Vlad Zahorodniic2b97472021-07-29 11:16:51 +03002955 wl_resource_for_each_safe(cb, cnext, &frame_callback_list) {
2956 wl_callback_send_done(cb, frame_time_msec);
2957 wl_resource_destroy(cb);
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
nerdopolisd2a320d2021-08-23 21:29:42 -04003224
3225WL_EXPORT void
3226weston_output_repaint_failed(struct weston_output *output)
3227{
3228 weston_log("Clearing repaint status.\n");
3229 assert(output->repaint_status == REPAINT_AWAITING_COMPLETION);
3230 output->repaint_status = REPAINT_NOT_SCHEDULED;
3231}
3232
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003233static void
3234idle_repaint(void *data)
3235{
3236 struct weston_output *output = data;
Antonio Borneoc90fccc2019-06-30 15:51:10 +02003237 int ret;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003238
Daniel Stone05df8c12017-03-03 16:59:42 +00003239 assert(output->repaint_status == REPAINT_BEGIN_FROM_IDLE);
3240 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03003241 output->idle_repaint_source = NULL;
Antonio Borneoc90fccc2019-06-30 15:51:10 +02003242 ret = output->start_repaint_loop(output);
3243 if (ret != 0)
3244 weston_output_schedule_repaint_reset(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003245}
3246
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003247WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003248weston_layer_entry_insert(struct weston_layer_entry *list,
3249 struct weston_layer_entry *entry)
3250{
3251 wl_list_insert(&list->link, &entry->link);
3252 entry->layer = list->layer;
3253}
3254
3255WL_EXPORT void
3256weston_layer_entry_remove(struct weston_layer_entry *entry)
3257{
3258 wl_list_remove(&entry->link);
3259 wl_list_init(&entry->link);
3260 entry->layer = NULL;
3261}
3262
Quentin Glidic82681572016-12-17 13:40:51 +01003263
3264/** Initialize the weston_layer struct.
3265 *
3266 * \param compositor The compositor instance
3267 * \param layer The layer to initialize
3268 */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003269WL_EXPORT void
Quentin Glidic82681572016-12-17 13:40:51 +01003270weston_layer_init(struct weston_layer *layer,
3271 struct weston_compositor *compositor)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003272{
Quentin Glidic82681572016-12-17 13:40:51 +01003273 layer->compositor = compositor;
3274 wl_list_init(&layer->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003275 wl_list_init(&layer->view_list.link);
3276 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003277 weston_layer_set_mask_infinite(layer);
Quentin Glidic82681572016-12-17 13:40:51 +01003278}
3279
Pekka Paalanen87400372021-05-14 14:29:40 +03003280/** Finalize the weston_layer struct.
3281 *
3282 * \param layer The layer to finalize.
3283 */
3284WL_EXPORT void
3285weston_layer_fini(struct weston_layer *layer)
3286{
3287 wl_list_remove(&layer->link);
3288
3289 if (!wl_list_empty(&layer->view_list.link))
3290 weston_log("BUG: finalizing a layer with views still on it.\n");
3291
3292 wl_list_remove(&layer->view_list.link);
3293}
3294
Quentin Glidic82681572016-12-17 13:40:51 +01003295/** Sets the position of the layer in the layer list. The layer will be placed
3296 * below any layer with the same position value, if any.
3297 * This function is safe to call if the layer is already on the list, but the
3298 * layer may be moved below other layers at the same position, if any.
3299 *
3300 * \param layer The layer to modify
3301 * \param position The position the layer will be placed at
3302 */
3303WL_EXPORT void
3304weston_layer_set_position(struct weston_layer *layer,
3305 enum weston_layer_position position)
3306{
3307 struct weston_layer *below;
3308
3309 wl_list_remove(&layer->link);
3310
3311 /* layer_list is ordered from top to bottom, the last layer being the
3312 * background with the smallest position value */
3313
3314 layer->position = position;
3315 wl_list_for_each_reverse(below, &layer->compositor->layer_list, link) {
3316 if (below->position >= layer->position) {
3317 wl_list_insert(&below->link, &layer->link);
3318 return;
3319 }
3320 }
3321 wl_list_insert(&layer->compositor->layer_list, &layer->link);
3322}
3323
3324/** Hide a layer by taking it off the layer list.
3325 * This function is safe to call if the layer is not on the list.
3326 *
3327 * \param layer The layer to hide
3328 */
3329WL_EXPORT void
3330weston_layer_unset_position(struct weston_layer *layer)
3331{
3332 wl_list_remove(&layer->link);
3333 wl_list_init(&layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003334}
3335
3336WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003337weston_layer_set_mask(struct weston_layer *layer,
3338 int x, int y, int width, int height)
3339{
3340 struct weston_view *view;
3341
3342 layer->mask.x1 = x;
3343 layer->mask.x2 = x + width;
3344 layer->mask.y1 = y;
3345 layer->mask.y2 = y + height;
3346
3347 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
3348 weston_view_geometry_dirty(view);
3349 }
3350}
3351
3352WL_EXPORT void
3353weston_layer_set_mask_infinite(struct weston_layer *layer)
3354{
Adam Jackson3c3f3b12019-10-16 16:02:59 -04003355 struct weston_view *view;
3356
3357 layer->mask.x1 = INT32_MIN;
3358 layer->mask.x2 = INT32_MAX;
3359 layer->mask.y1 = INT32_MIN;
3360 layer->mask.y2 = INT32_MAX;
3361
3362 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
3363 weston_view_geometry_dirty(view);
3364 }
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003365}
3366
Daniel Stone3b775632018-07-20 08:38:25 +01003367WL_EXPORT bool
3368weston_layer_mask_is_infinite(struct weston_layer *layer)
3369{
3370 return layer->mask.x1 == INT32_MIN &&
3371 layer->mask.y1 == INT32_MIN &&
Adam Jackson3c3f3b12019-10-16 16:02:59 -04003372 layer->mask.x2 == INT32_MAX &&
3373 layer->mask.y2 == INT32_MAX;
Daniel Stone3b775632018-07-20 08:38:25 +01003374}
3375
Marius Vlad55d87362019-06-11 01:15:35 +03003376/**
3377 * \ingroup output
3378 */
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003379WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003380weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003381{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003382 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04003383 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01003384
Bryce Harrington08976ac2016-08-30 12:05:16 -07003385 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
3386 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003387 return;
3388
Pekka Paalanenb5026542014-11-12 15:09:24 +02003389 if (!output->repaint_needed)
Marius Vlad3203ff62019-09-05 14:56:12 +03003390 TL_POINT(compositor, "core_repaint_req", TLP_OUTPUT(output), TLP_END);
Pekka Paalanenb5026542014-11-12 15:09:24 +02003391
Kristian Høgsbergef044142011-06-21 15:02:12 -04003392 loop = wl_display_get_event_loop(compositor->wl_display);
Daniel Stone09a97e22017-03-01 11:34:06 +00003393 output->repaint_needed = true;
Daniel Stone05df8c12017-03-03 16:59:42 +00003394
3395 /* If we already have a repaint scheduled for our idle handler,
3396 * no need to set it again. If the repaint has been called but
3397 * not finished, then weston_output_finish_frame() will notice
3398 * that a repaint is needed and schedule one. */
3399 if (output->repaint_status != REPAINT_NOT_SCHEDULED)
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003400 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01003401
Daniel Stone05df8c12017-03-03 16:59:42 +00003402 output->repaint_status = REPAINT_BEGIN_FROM_IDLE;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03003403 assert(!output->idle_repaint_source);
3404 output->idle_repaint_source = wl_event_loop_add_idle(loop, idle_repaint,
3405 output);
Marius Vlad3203ff62019-09-05 14:56:12 +03003406 TL_POINT(compositor, "core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003407}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05003408
Marius Vlad9fdda7f2019-06-11 16:08:55 +03003409/** weston_compositor_schedule_repaint
3410 * \ingroup compositor
3411 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003412WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003413weston_compositor_schedule_repaint(struct weston_compositor *compositor)
3414{
3415 struct weston_output *output;
3416
3417 wl_list_for_each(output, &compositor->output_list, link)
3418 weston_output_schedule_repaint(output);
3419}
3420
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003421static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003422surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003423{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003424 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003425}
3426
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003427static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003428surface_attach(struct wl_client *client,
3429 struct wl_resource *resource,
3430 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
3431{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003432 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05003433 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003434
Kristian Høgsbergab19f932013-08-20 11:30:54 -07003435 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05003436 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07003437 if (buffer == NULL) {
3438 wl_client_post_no_memory(client);
3439 return;
3440 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07003441 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04003442
Pekka Paalanende685b82012-12-04 15:58:12 +02003443 /* Attach, attach, without commit in between does not send
3444 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003445 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003446
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003447 surface->pending.sx = sx;
3448 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01003449 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003450}
3451
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003452static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003453surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003454 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003455 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05003456{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003457 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003458
Derek Foreman57e92ed2015-11-17 14:11:35 -06003459 if (width <= 0 || height <= 0)
3460 return;
3461
Derek Foreman152254b2015-11-26 14:17:48 -06003462 pixman_region32_union_rect(&surface->pending.damage_surface,
3463 &surface->pending.damage_surface,
3464 x, y, width, height);
3465}
3466
3467static void
3468surface_damage_buffer(struct wl_client *client,
3469 struct wl_resource *resource,
3470 int32_t x, int32_t y, int32_t width, int32_t height)
3471{
3472 struct weston_surface *surface = wl_resource_get_user_data(resource);
3473
3474 if (width <= 0 || height <= 0)
3475 return;
3476
3477 pixman_region32_union_rect(&surface->pending.damage_buffer,
3478 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04003479 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003480}
3481
Kristian Høgsberg33418202011-08-16 23:01:28 -04003482static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003483destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04003484{
Vlad Zahorodniic2b97472021-07-29 11:16:51 +03003485 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsberg33418202011-08-16 23:01:28 -04003486}
3487
3488static void
3489surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003490 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04003491{
Vlad Zahorodniic2b97472021-07-29 11:16:51 +03003492 struct wl_resource *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003493 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003494
Vlad Zahorodniic2b97472021-07-29 11:16:51 +03003495 cb = wl_resource_create(client, &wl_callback_interface, 1, callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003496 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003497 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003498 return;
3499 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03003500
Vlad Zahorodniic2b97472021-07-29 11:16:51 +03003501 wl_resource_set_implementation(cb, NULL, NULL,
Jason Ekstranda85118c2013-06-27 20:17:02 -05003502 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003503
Vlad Zahorodniic2b97472021-07-29 11:16:51 +03003504 wl_list_insert(surface->pending.frame_callback_list.prev,
3505 wl_resource_get_link(cb));
Kristian Høgsberg33418202011-08-16 23:01:28 -04003506}
3507
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003508static void
3509surface_set_opaque_region(struct wl_client *client,
3510 struct wl_resource *resource,
3511 struct wl_resource *region_resource)
3512{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003513 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003514 struct weston_region *region;
3515
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003516 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003517 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003518 pixman_region32_copy(&surface->pending.opaque,
3519 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003520 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07003521 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003522 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003523}
3524
3525static void
3526surface_set_input_region(struct wl_client *client,
3527 struct wl_resource *resource,
3528 struct wl_resource *region_resource)
3529{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003530 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003531 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003532
3533 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003534 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003535 pixman_region32_copy(&surface->pending.input,
3536 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003537 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003538 pixman_region32_fini(&surface->pending.input);
3539 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003540 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003541}
3542
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003543/* Cause damage to this sub-surface and all its children.
3544 *
3545 * This is useful when there are state changes that need an implicit
3546 * damage, e.g. a z-order change.
3547 */
3548static void
3549weston_surface_damage_subsurfaces(struct weston_subsurface *sub)
3550{
3551 struct weston_subsurface *child;
3552
3553 weston_surface_damage(sub->surface);
3554 sub->reordered = false;
3555
3556 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link)
3557 if (child != sub)
3558 weston_surface_damage_subsurfaces(child);
3559}
3560
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003561static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003562weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003563{
Pekka Paalanene67858b2013-04-25 13:57:42 +03003564 struct weston_subsurface *sub;
3565
3566 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
3567 parent_link_pending) {
3568 wl_list_remove(&sub->parent_link);
3569 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003570
3571 if (sub->reordered)
3572 weston_surface_damage_subsurfaces(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003573 }
3574}
3575
3576static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03003577weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05003578 struct weston_matrix *matrix)
3579{
Pekka Paalanen04baea52016-04-26 15:50:58 +03003580 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05003581 double src_width, src_height, dest_width, dest_height;
3582
3583 weston_matrix_init(matrix);
3584
3585 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
3586 src_width = surface->width_from_buffer;
3587 src_height = surface->height_from_buffer;
3588 } else {
3589 src_width = wl_fixed_to_double(vp->buffer.src_width);
3590 src_height = wl_fixed_to_double(vp->buffer.src_height);
3591 }
3592
3593 if (vp->surface.width == -1) {
3594 dest_width = src_width;
3595 dest_height = src_height;
3596 } else {
3597 dest_width = vp->surface.width;
3598 dest_height = vp->surface.height;
3599 }
3600
3601 if (src_width != dest_width || src_height != dest_height)
3602 weston_matrix_scale(matrix,
3603 src_width / dest_width,
3604 src_height / dest_height, 1);
3605
3606 if (vp->buffer.src_width != wl_fixed_from_int(-1))
3607 weston_matrix_translate(matrix,
3608 wl_fixed_to_double(vp->buffer.src_x),
3609 wl_fixed_to_double(vp->buffer.src_y),
3610 0);
3611
3612 switch (vp->buffer.transform) {
3613 case WL_OUTPUT_TRANSFORM_FLIPPED:
3614 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3615 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3616 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3617 weston_matrix_scale(matrix, -1, 1, 1);
3618 weston_matrix_translate(matrix,
3619 surface->width_from_buffer, 0, 0);
3620 break;
3621 }
3622
3623 switch (vp->buffer.transform) {
3624 default:
3625 case WL_OUTPUT_TRANSFORM_NORMAL:
3626 case WL_OUTPUT_TRANSFORM_FLIPPED:
3627 break;
3628 case WL_OUTPUT_TRANSFORM_90:
3629 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +02003630 weston_matrix_rotate_xy(matrix, 0, -1);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003631 weston_matrix_translate(matrix,
Pekka Paalanen8060d822020-02-06 15:27:54 +02003632 0, surface->width_from_buffer, 0);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003633 break;
3634 case WL_OUTPUT_TRANSFORM_180:
3635 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3636 weston_matrix_rotate_xy(matrix, -1, 0);
3637 weston_matrix_translate(matrix,
3638 surface->width_from_buffer,
3639 surface->height_from_buffer, 0);
3640 break;
3641 case WL_OUTPUT_TRANSFORM_270:
3642 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +02003643 weston_matrix_rotate_xy(matrix, 0, 1);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003644 weston_matrix_translate(matrix,
Pekka Paalanen8060d822020-02-06 15:27:54 +02003645 surface->height_from_buffer, 0, 0);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003646 break;
3647 }
3648
3649 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
3650}
3651
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003652/**
3653 * Compute a + b > c while being safe to overflows.
3654 */
3655static bool
3656fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
3657{
3658 return (int64_t)a + (int64_t)b > (int64_t)c;
3659}
3660
3661static bool
3662weston_surface_is_pending_viewport_source_valid(
3663 const struct weston_surface *surface)
3664{
3665 const struct weston_surface_state *pend = &surface->pending;
3666 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
3667 int width_from_buffer = 0;
3668 int height_from_buffer = 0;
3669 wl_fixed_t w;
3670 wl_fixed_t h;
3671
3672 /* If viewport source rect is not set, it is always ok. */
3673 if (vp->buffer.src_width == wl_fixed_from_int(-1))
3674 return true;
3675
3676 if (pend->newly_attached) {
3677 if (pend->buffer) {
3678 convert_size_by_transform_scale(&width_from_buffer,
3679 &height_from_buffer,
3680 pend->buffer->width,
3681 pend->buffer->height,
3682 vp->buffer.transform,
3683 vp->buffer.scale);
3684 } else {
3685 /* No buffer: viewport is irrelevant. */
3686 return true;
3687 }
3688 } else {
3689 width_from_buffer = surface->width_from_buffer;
3690 height_from_buffer = surface->height_from_buffer;
3691 }
3692
3693 assert((width_from_buffer == 0) == (height_from_buffer == 0));
3694 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
3695
3696 /* No buffer: viewport is irrelevant. */
3697 if (width_from_buffer == 0 || height_from_buffer == 0)
3698 return true;
3699
3700 /* overflow checks for wl_fixed_from_int() */
3701 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
3702 return false;
3703 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
3704 return false;
3705
3706 w = wl_fixed_from_int(width_from_buffer);
3707 h = wl_fixed_from_int(height_from_buffer);
3708
3709 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
3710 return false;
3711 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
3712 return false;
3713
3714 return true;
3715}
3716
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003717static bool
3718fixed_is_integer(wl_fixed_t v)
3719{
3720 return (v & 0xff) == 0;
3721}
3722
3723static bool
3724weston_surface_is_pending_viewport_dst_size_int(
3725 const struct weston_surface *surface)
3726{
3727 const struct weston_buffer_viewport *vp =
3728 &surface->pending.buffer_viewport;
3729
3730 if (vp->surface.width != -1) {
3731 assert(vp->surface.width > 0 && vp->surface.height > 0);
3732 return true;
3733 }
3734
3735 return fixed_is_integer(vp->buffer.src_width) &&
3736 fixed_is_integer(vp->buffer.src_height);
3737}
3738
Derek Foreman152254b2015-11-26 14:17:48 -06003739/* Translate pending damage in buffer co-ordinates to surface
3740 * co-ordinates and union it with a pixman_region32_t.
3741 * This should only be called after the buffer is attached.
3742 */
3743static void
3744apply_damage_buffer(pixman_region32_t *dest,
3745 struct weston_surface *surface,
3746 struct weston_surface_state *state)
3747{
3748 struct weston_buffer *buffer = surface->buffer_ref.buffer;
3749
3750 /* wl_surface.damage_buffer needs to be clipped to the buffer,
3751 * translated into surface co-ordinates and unioned with
3752 * any other surface damage.
3753 * None of this makes sense if there is no buffer though.
3754 */
3755 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
3756 pixman_region32_t buffer_damage;
3757
3758 pixman_region32_intersect_rect(&state->damage_buffer,
3759 &state->damage_buffer,
3760 0, 0, buffer->width,
3761 buffer->height);
3762 pixman_region32_init(&buffer_damage);
3763 weston_matrix_transform_region(&buffer_damage,
3764 &surface->buffer_to_surface_matrix,
3765 &state->damage_buffer);
3766 pixman_region32_union(dest, dest, &buffer_damage);
3767 pixman_region32_fini(&buffer_damage);
3768 }
3769 /* We should clear this on commit even if there was no buffer */
3770 pixman_region32_clear(&state->damage_buffer);
3771}
3772
Jason Ekstrand1e059042014-10-16 10:55:19 -05003773static void
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303774weston_surface_set_desired_protection(struct weston_surface *surface,
3775 enum weston_hdcp_protection protection)
3776{
3777 if (surface->desired_protection == protection)
3778 return;
3779 surface->desired_protection = protection;
3780 weston_surface_damage(surface);
3781}
3782
3783static void
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303784weston_surface_set_protection_mode(struct weston_surface *surface,
3785 enum weston_surface_protection_mode p_mode)
3786{
3787 struct content_protection *cp = surface->compositor->content_protection;
3788 struct protected_surface *psurface;
3789
3790 surface->protection_mode = p_mode;
3791 wl_list_for_each(psurface, &cp->protected_list, link) {
3792 if (!psurface || psurface->surface != surface)
3793 continue;
3794 weston_protected_surface_send_event(psurface,
3795 surface->current_protection);
3796 }
3797}
3798
3799static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05003800weston_surface_commit_state(struct weston_surface *surface,
3801 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003802{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003803 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003804 pixman_region32_t opaque;
3805
Alexander Larsson4ea95522013-05-22 14:41:37 +02003806 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02003807 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03003808 /* wp_viewport.set_source */
3809 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003810 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003811
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003812 /* wl_surface.attach */
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003813 if (state->newly_attached) {
3814 /* zwp_surface_synchronization_v1.set_acquire_fence */
3815 fd_move(&surface->acquire_fence_fd,
3816 &state->acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003817 /* zwp_surface_synchronization_v1.get_release */
3818 weston_buffer_release_move(&surface->buffer_release_ref,
3819 &state->buffer_release_ref);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003820 weston_surface_attach(surface, state->buffer);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003821 }
Jason Ekstrand7b982072014-05-20 14:33:03 -05003822 weston_surface_state_set_buffer(state, NULL);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003823 assert(state->acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003824 assert(state->buffer_release_ref.buffer_release == NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01003825
Jason Ekstrand1e059042014-10-16 10:55:19 -05003826 weston_surface_build_buffer_matrix(surface,
3827 &surface->surface_to_buffer_matrix);
3828 weston_matrix_invert(&surface->buffer_to_surface_matrix,
3829 &surface->surface_to_buffer_matrix);
3830
Jason Ekstrand7b982072014-05-20 14:33:03 -05003831 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003832 weston_surface_update_size(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003833 if (surface->committed)
3834 surface->committed(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003835 }
Giulio Camuffo184df502013-02-21 11:29:21 +01003836
Jason Ekstrand7b982072014-05-20 14:33:03 -05003837 state->sx = 0;
3838 state->sy = 0;
3839 state->newly_attached = 0;
3840 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03003841
Derek Foreman152254b2015-11-26 14:17:48 -06003842 /* wl_surface.damage and wl_surface.damage_buffer */
Marius Vlad2a1b7862019-09-05 13:12:18 +03003843 if (pixman_region32_not_empty(&state->damage_surface) ||
3844 pixman_region32_not_empty(&state->damage_buffer))
Marius Vlad3203ff62019-09-05 14:56:12 +03003845 TL_POINT(surface->compositor, "core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06003846
Pekka Paalanen8e159182012-10-10 12:49:25 +03003847 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06003848 &state->damage_surface);
3849
3850 apply_damage_buffer(&surface->damage, surface, state);
3851
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05003852 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07003853 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06003854 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003855
3856 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003857 pixman_region32_init(&opaque);
3858 pixman_region32_intersect_rect(&opaque, &state->opaque,
3859 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003860
3861 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
3862 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003863 wl_list_for_each(view, &surface->views, surface_link)
3864 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003865 }
3866
3867 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003868
3869 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003870 pixman_region32_intersect_rect(&surface->input, &state->input,
3871 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003872
Pekka Paalanenbc106382012-10-10 12:49:31 +03003873 /* wl_surface.frame */
3874 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05003875 &state->frame_callback_list);
3876 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003877
3878 /* XXX:
3879 * What should happen with a feedback request, if there
3880 * is no wl_buffer attached for this commit?
3881 */
3882
3883 /* presentation.feedback */
3884 wl_list_insert_list(&surface->feedback_list,
3885 &state->feedback_list);
3886 wl_list_init(&state->feedback_list);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003887
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303888 /* weston_protected_surface.enforced/relaxed */
3889 if (surface->protection_mode != state->protection_mode)
3890 weston_surface_set_protection_mode(surface,
3891 state->protection_mode);
3892
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303893 /* weston_protected_surface.set_type */
3894 weston_surface_set_desired_protection(surface, state->desired_protection);
3895
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003896 wl_signal_emit(&surface->commit_signal, surface);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003897}
3898
3899static void
3900weston_surface_commit(struct weston_surface *surface)
3901{
3902 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003903
Pekka Paalanene67858b2013-04-25 13:57:42 +03003904 weston_surface_commit_subsurface_order(surface);
3905
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003906 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003907}
3908
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003909static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003910weston_subsurface_commit(struct weston_subsurface *sub);
3911
3912static void
3913weston_subsurface_parent_commit(struct weston_subsurface *sub,
3914 int parent_is_synchronized);
3915
3916static void
3917surface_commit(struct wl_client *client, struct wl_resource *resource)
3918{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003919 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003920 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
3921
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003922 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
3923 assert(surface->viewport_resource);
3924
3925 wl_resource_post_error(surface->viewport_resource,
3926 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
3927 "wl_surface@%d has viewport source outside buffer",
3928 wl_resource_get_id(resource));
3929 return;
3930 }
3931
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003932 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
3933 assert(surface->viewport_resource);
3934
3935 wl_resource_post_error(surface->viewport_resource,
3936 WP_VIEWPORT_ERROR_BAD_SIZE,
3937 "wl_surface@%d viewport dst size not integer",
3938 wl_resource_get_id(resource));
3939 return;
3940 }
3941
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003942 if (surface->pending.acquire_fence_fd >= 0) {
3943 assert(surface->synchronization_resource);
3944
3945 if (!surface->pending.buffer) {
3946 fd_clear(&surface->pending.acquire_fence_fd);
3947 wl_resource_post_error(surface->synchronization_resource,
3948 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3949 "wl_surface@%"PRIu32" no buffer for synchronization",
3950 wl_resource_get_id(resource));
3951 return;
3952 }
3953
3954 /* We support fences for both wp_linux_dmabuf and opaque EGL
3955 * buffers, as mandated by minor version 2 of the
3956 * zwp_linux_explicit_synchronization_v1 protocol. Since
3957 * renderers that support fences currently only support these
3958 * two buffer types plus SHM buffers, we can just check for the
3959 * SHM buffer case here.
3960 */
3961 if (wl_shm_buffer_get(surface->pending.buffer->resource)) {
3962 fd_clear(&surface->pending.acquire_fence_fd);
3963 wl_resource_post_error(surface->synchronization_resource,
3964 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_UNSUPPORTED_BUFFER,
3965 "wl_surface@%"PRIu32" unsupported buffer for synchronization",
3966 wl_resource_get_id(resource));
3967 return;
3968 }
3969 }
3970
Alexandros Frantzis67629672018-10-19 12:14:11 +03003971 if (surface->pending.buffer_release_ref.buffer_release &&
3972 !surface->pending.buffer) {
3973 assert(surface->synchronization_resource);
3974
3975 wl_resource_post_error(surface->synchronization_resource,
3976 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3977 "wl_surface@%"PRIu32" no buffer for synchronization",
3978 wl_resource_get_id(resource));
3979 return;
3980 }
3981
Pekka Paalanene67858b2013-04-25 13:57:42 +03003982 if (sub) {
3983 weston_subsurface_commit(sub);
3984 return;
3985 }
3986
3987 weston_surface_commit(surface);
3988
3989 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
3990 if (sub->surface != surface)
3991 weston_subsurface_parent_commit(sub, 0);
3992 }
3993}
3994
3995static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003996surface_set_buffer_transform(struct wl_client *client,
3997 struct wl_resource *resource, int transform)
3998{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003999 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004000
Jonny Lamba55f1392014-05-30 12:07:15 +02004001 /* if wl_output.transform grows more members this will need to be updated. */
4002 if (transform < 0 ||
4003 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
4004 wl_resource_post_error(resource,
4005 WL_SURFACE_ERROR_INVALID_TRANSFORM,
4006 "buffer transform must be a valid transform "
4007 "('%d' specified)", transform);
4008 return;
4009 }
4010
Pekka Paalanen952b6c82014-03-14 14:38:15 +02004011 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004012 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004013}
4014
Alexander Larsson4ea95522013-05-22 14:41:37 +02004015static void
4016surface_set_buffer_scale(struct wl_client *client,
4017 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004018 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004019{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004020 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02004021
Jonny Lamba55f1392014-05-30 12:07:15 +02004022 if (scale < 1) {
4023 wl_resource_post_error(resource,
4024 WL_SURFACE_ERROR_INVALID_SCALE,
4025 "buffer scale must be at least one "
4026 "('%d' specified)", scale);
4027 return;
4028 }
4029
Pekka Paalanen952b6c82014-03-14 14:38:15 +02004030 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004031 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02004032}
4033
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04004034static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004035 surface_destroy,
4036 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04004037 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004038 surface_frame,
4039 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03004040 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004041 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02004042 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06004043 surface_set_buffer_scale,
4044 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004045};
4046
Leandro Ribeiro8eb84142021-01-18 19:36:48 -03004047static int
4048create_surface_dmabuf_feedback(struct weston_compositor *ec,
4049 struct weston_surface *surface)
4050{
4051 struct weston_dmabuf_feedback_tranche *tranche;
4052 dev_t main_device = ec->default_dmabuf_feedback->main_device;
4053 uint32_t flags = 0;
4054
4055 surface->dmabuf_feedback = weston_dmabuf_feedback_create(main_device);
4056 if (!surface->dmabuf_feedback)
4057 return -1;
4058
4059 tranche = weston_dmabuf_feedback_tranche_create(surface->dmabuf_feedback,
4060 ec->dmabuf_feedback_format_table,
4061 main_device, flags,
4062 RENDERER_PREF);
4063 if (!tranche) {
4064 weston_dmabuf_feedback_destroy(surface->dmabuf_feedback);
4065 surface->dmabuf_feedback = NULL;
4066 return -1;
4067 }
4068
4069 return 0;
4070}
4071
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004072static void
4073compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04004074 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004075{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04004076 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004077 struct weston_surface *surface;
Leandro Ribeiro8eb84142021-01-18 19:36:48 -03004078 int ret;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004079
Kristian Høgsberg18c93002012-01-27 11:58:31 -05004080 surface = weston_surface_create(ec);
Leandro Ribeiro8eb84142021-01-18 19:36:48 -03004081 if (surface == NULL)
4082 goto err;
4083
4084 if (ec->default_dmabuf_feedback) {
4085 ret = create_surface_dmabuf_feedback(ec, surface);
4086 if (ret < 0)
4087 goto err_dmabuf_feedback;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04004088 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004089
Jason Ekstranda85118c2013-06-27 20:17:02 -05004090 surface->resource =
4091 wl_resource_create(client, &wl_surface_interface,
4092 wl_resource_get_version(resource), id);
Leandro Ribeiro8eb84142021-01-18 19:36:48 -03004093 if (surface->resource == NULL)
4094 goto err_dmabuf_feedback;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004095 wl_resource_set_implementation(surface->resource, &surface_interface,
4096 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07004097
4098 wl_signal_emit(&ec->create_surface_signal, surface);
Leandro Ribeiro8eb84142021-01-18 19:36:48 -03004099
4100 return;
4101
4102err_dmabuf_feedback:
4103 weston_surface_destroy(surface);
4104err:
4105 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004106}
4107
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004108static void
4109destroy_region(struct wl_resource *resource)
4110{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05004111 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004112
4113 pixman_region32_fini(&region->region);
4114 free(region);
4115}
4116
4117static void
4118region_destroy(struct wl_client *client, struct wl_resource *resource)
4119{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004120 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004121}
4122
4123static void
4124region_add(struct wl_client *client, struct wl_resource *resource,
4125 int32_t x, int32_t y, int32_t width, int32_t height)
4126{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05004127 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004128
4129 pixman_region32_union_rect(&region->region, &region->region,
4130 x, y, width, height);
4131}
4132
4133static void
4134region_subtract(struct wl_client *client, struct wl_resource *resource,
4135 int32_t x, int32_t y, int32_t width, int32_t height)
4136{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05004137 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004138 pixman_region32_t rect;
4139
4140 pixman_region32_init_rect(&rect, x, y, width, height);
4141 pixman_region32_subtract(&region->region, &region->region, &rect);
4142 pixman_region32_fini(&rect);
4143}
4144
4145static const struct wl_region_interface region_interface = {
4146 region_destroy,
4147 region_add,
4148 region_subtract
4149};
4150
4151static void
4152compositor_create_region(struct wl_client *client,
4153 struct wl_resource *resource, uint32_t id)
4154{
4155 struct weston_region *region;
4156
4157 region = malloc(sizeof *region);
4158 if (region == NULL) {
4159 wl_resource_post_no_memory(resource);
4160 return;
4161 }
4162
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004163 pixman_region32_init(&region->region);
4164
Jason Ekstranda85118c2013-06-27 20:17:02 -05004165 region->resource =
4166 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004167 if (region->resource == NULL) {
4168 free(region);
4169 wl_resource_post_no_memory(resource);
4170 return;
4171 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05004172 wl_resource_set_implementation(region->resource, &region_interface,
4173 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004174}
4175
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04004176static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004177 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004178 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004179};
4180
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004181static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03004182weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
4183{
4184 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004185
Jason Ekstrand7b982072014-05-20 14:33:03 -05004186 weston_surface_commit_state(surface, &sub->cached);
4187 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004188
4189 weston_surface_commit_subsurface_order(surface);
4190
4191 weston_surface_schedule_repaint(surface);
4192
Jason Ekstrand7b982072014-05-20 14:33:03 -05004193 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004194}
4195
4196static void
4197weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
4198{
4199 struct weston_surface *surface = sub->surface;
4200
4201 /*
4202 * If this commit would cause the surface to move by the
4203 * attach(dx, dy) parameters, the old damage region must be
4204 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05004205 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03004206 */
Derek Foreman152254b2015-11-26 14:17:48 -06004207 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004208 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06004209 pixman_region32_union(&sub->cached.damage_surface,
4210 &sub->cached.damage_surface,
4211 &surface->pending.damage_surface);
4212 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004213
4214 if (surface->pending.newly_attached) {
4215 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05004216 weston_surface_state_set_buffer(&sub->cached,
4217 surface->pending.buffer);
4218 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004219 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04004220 weston_presentation_feedback_discard_list(
4221 &sub->cached.feedback_list);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03004222 /* zwp_surface_synchronization_v1.set_acquire_fence */
4223 fd_move(&sub->cached.acquire_fence_fd,
4224 &surface->pending.acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03004225 /* zwp_surface_synchronization_v1.get_release */
4226 weston_buffer_release_move(&sub->cached.buffer_release_ref,
4227 &surface->pending.buffer_release_ref);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004228 }
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05304229 sub->cached.desired_protection = surface->pending.desired_protection;
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05304230 sub->cached.protection_mode = surface->pending.protection_mode;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03004231 assert(surface->pending.acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03004232 assert(surface->pending.buffer_release_ref.buffer_release == NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004233 sub->cached.sx += surface->pending.sx;
4234 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02004235
Derek Foreman152254b2015-11-26 14:17:48 -06004236 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
4237
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004238 sub->cached.buffer_viewport.changed |=
4239 surface->pending.buffer_viewport.changed;
4240 sub->cached.buffer_viewport.buffer =
4241 surface->pending.buffer_viewport.buffer;
4242 sub->cached.buffer_viewport.surface =
4243 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004244
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004245 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004246
4247 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
4248
4249 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
4250
4251 wl_list_insert_list(&sub->cached.frame_callback_list,
4252 &surface->pending.frame_callback_list);
4253 wl_list_init(&surface->pending.frame_callback_list);
4254
Pekka Paalanen133e4392014-09-23 22:08:46 -04004255 wl_list_insert_list(&sub->cached.feedback_list,
4256 &surface->pending.feedback_list);
4257 wl_list_init(&surface->pending.feedback_list);
4258
Jason Ekstrand7b982072014-05-20 14:33:03 -05004259 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004260}
4261
Derek Foreman280e7dd2014-10-03 13:13:42 -05004262static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03004263weston_subsurface_is_synchronized(struct weston_subsurface *sub)
4264{
4265 while (sub) {
4266 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05004267 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004268
4269 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05004270 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004271
4272 sub = weston_surface_to_subsurface(sub->parent);
4273 }
4274
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01004275 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004276}
4277
4278static void
4279weston_subsurface_commit(struct weston_subsurface *sub)
4280{
4281 struct weston_surface *surface = sub->surface;
4282 struct weston_subsurface *tmp;
4283
4284 /* Recursive check for effectively synchronized. */
4285 if (weston_subsurface_is_synchronized(sub)) {
4286 weston_subsurface_commit_to_cache(sub);
4287 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05004288 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03004289 /* flush accumulated state from cache */
4290 weston_subsurface_commit_to_cache(sub);
4291 weston_subsurface_commit_from_cache(sub);
4292 } else {
4293 weston_surface_commit(surface);
4294 }
4295
4296 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
4297 if (tmp->surface != surface)
4298 weston_subsurface_parent_commit(tmp, 0);
4299 }
4300 }
4301}
4302
4303static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004304weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004305{
4306 struct weston_surface *surface = sub->surface;
4307 struct weston_subsurface *tmp;
4308
Pekka Paalanene67858b2013-04-25 13:57:42 +03004309 /* From now on, commit_from_cache the whole sub-tree, regardless of
4310 * the synchronized mode of each child. This sub-surface or some
4311 * of its ancestors were synchronized, so we are synchronized
4312 * all the way down.
4313 */
4314
Jason Ekstrand7b982072014-05-20 14:33:03 -05004315 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004316 weston_subsurface_commit_from_cache(sub);
4317
4318 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
4319 if (tmp->surface != surface)
4320 weston_subsurface_parent_commit(tmp, 1);
4321 }
4322}
4323
4324static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004325weston_subsurface_parent_commit(struct weston_subsurface *sub,
4326 int parent_is_synchronized)
4327{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004328 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004329 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004330 wl_list_for_each(view, &sub->surface->views, surface_link)
4331 weston_view_set_position(view,
4332 sub->position.x,
4333 sub->position.y);
4334
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004335 sub->position.set = 0;
4336 }
4337
4338 if (parent_is_synchronized || sub->synchronized)
4339 weston_subsurface_synchronized_commit(sub);
4340}
4341
Pekka Paalanen8274d902014-08-06 19:36:51 +03004342static int
4343subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
4344{
4345 return snprintf(buf, len, "sub-surface");
4346}
4347
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004348static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004349subsurface_committed(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004350{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004351 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004352
Jason Ekstranda7af7042013-10-12 22:38:11 -05004353 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004354 weston_view_set_position(view,
4355 view->geometry.x + dx,
4356 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004357
4358 /* No need to check parent mappedness, because if parent is not
4359 * mapped, parent is not in a visible layer, so this sub-surface
4360 * will not be drawn either.
4361 */
Armin Krezovićf8486c32016-06-30 06:04:28 +02004362
Pekka Paalanene67858b2013-04-25 13:57:42 +03004363 if (!weston_surface_is_mapped(surface)) {
Armin Krezovićf8486c32016-06-30 06:04:28 +02004364 surface->is_mapped = true;
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03004365
Derek Foreman4b1a0a12014-09-10 15:37:33 -05004366 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03004367 * because that would call it also for the parent surface,
4368 * which might not be mapped yet. That would lead to
4369 * inconsistent state, where the window could never be
4370 * mapped.
4371 *
Armin Krezovićf8486c32016-06-30 06:04:28 +02004372 * Instead just force the is_mapped flag on, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03004373 * weston_surface_is_mapped() return true, so that when the
4374 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03004375 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03004376 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004377 }
4378}
4379
4380static struct weston_subsurface *
4381weston_surface_to_subsurface(struct weston_surface *surface)
4382{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004383 if (surface->committed == subsurface_committed)
4384 return surface->committed_private;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004385
4386 return NULL;
4387}
4388
Pekka Paalanen01388e22013-04-25 13:57:44 +03004389WL_EXPORT struct weston_surface *
4390weston_surface_get_main_surface(struct weston_surface *surface)
4391{
4392 struct weston_subsurface *sub;
4393
4394 while (surface && (sub = weston_surface_to_subsurface(surface)))
4395 surface = sub->parent;
4396
4397 return surface;
4398}
4399
Pekka Paalanen50b67472014-10-01 15:02:41 +03004400WL_EXPORT int
4401weston_surface_set_role(struct weston_surface *surface,
4402 const char *role_name,
4403 struct wl_resource *error_resource,
4404 uint32_t error_code)
4405{
4406 assert(role_name);
4407
4408 if (surface->role_name == NULL ||
4409 surface->role_name == role_name ||
4410 strcmp(surface->role_name, role_name) == 0) {
4411 surface->role_name = role_name;
4412
4413 return 0;
4414 }
4415
4416 wl_resource_post_error(error_resource, error_code,
4417 "Cannot assign role %s to wl_surface@%d,"
4418 " already has role %s\n",
4419 role_name,
4420 wl_resource_get_id(surface->resource),
4421 surface->role_name);
4422 return -1;
4423}
4424
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02004425WL_EXPORT const char *
4426weston_surface_get_role(struct weston_surface *surface)
4427{
4428 return surface->role_name;
4429}
4430
Pekka Paalanen8274d902014-08-06 19:36:51 +03004431WL_EXPORT void
4432weston_surface_set_label_func(struct weston_surface *surface,
4433 int (*desc)(struct weston_surface *,
4434 char *, size_t))
4435{
4436 surface->get_label = desc;
Marius Vlad5de92972019-10-15 13:25:41 +03004437 weston_timeline_refresh_subscription_objects(surface->compositor,
4438 surface);
Pekka Paalanen8274d902014-08-06 19:36:51 +03004439}
4440
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004441/** Get the size of surface contents
4442 *
4443 * \param surface The surface to query.
4444 * \param width Returns the width of raw contents.
4445 * \param height Returns the height of raw contents.
4446 *
4447 * Retrieves the raw surface content size in pixels for the given surface.
4448 * This is the whole content size in buffer pixels. If the surface
4449 * has no content or the renderer does not implement this feature,
4450 * zeroes are returned.
4451 *
4452 * This function is used to determine the buffer size needed for
4453 * a weston_surface_copy_content() call.
4454 */
4455WL_EXPORT void
4456weston_surface_get_content_size(struct weston_surface *surface,
4457 int *width, int *height)
4458{
4459 struct weston_renderer *rer = surface->compositor->renderer;
4460
4461 if (!rer->surface_get_content_size) {
4462 *width = 0;
4463 *height = 0;
4464 return;
4465 }
4466
4467 rer->surface_get_content_size(surface, width, height);
4468}
4469
Quentin Glidic248dd102016-08-12 10:41:34 +02004470/** Get the bounding box of a surface and its subsurfaces
4471 *
4472 * \param surface The surface to query.
4473 * \return The bounding box relative to the surface origin.
4474 *
4475 */
4476WL_EXPORT struct weston_geometry
4477weston_surface_get_bounding_box(struct weston_surface *surface)
4478{
4479 pixman_region32_t region;
4480 pixman_box32_t *box;
4481 struct weston_subsurface *subsurface;
4482
4483 pixman_region32_init_rect(&region,
4484 0, 0,
4485 surface->width, surface->height);
4486
4487 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link)
4488 pixman_region32_union_rect(&region, &region,
4489 subsurface->position.x,
4490 subsurface->position.y,
4491 subsurface->surface->width,
4492 subsurface->surface->height);
4493
4494 box = pixman_region32_extents(&region);
4495 struct weston_geometry geometry = {
4496 .x = box->x1,
4497 .y = box->y1,
4498 .width = box->x2 - box->x1,
4499 .height = box->y2 - box->y1,
4500 };
4501
4502 pixman_region32_fini(&region);
4503
4504 return geometry;
4505}
4506
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004507/** Copy surface contents to system memory.
4508 *
4509 * \param surface The surface to copy from.
4510 * \param target Pointer to the target memory buffer.
4511 * \param size Size of the target buffer in bytes.
4512 * \param src_x X location on contents to copy from.
4513 * \param src_y Y location on contents to copy from.
4514 * \param width Width in pixels of the area to copy.
4515 * \param height Height in pixels of the area to copy.
4516 * \return 0 for success, -1 for failure.
4517 *
4518 * Surface contents are maintained by the renderer. They can be in a
4519 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
4520 * else.
4521 *
4522 * Surface contents are copied into memory pointed to by target,
4523 * which has size bytes of space available. The target memory
4524 * may be larger than needed, but being smaller returns an error.
4525 * The extra bytes in target may or may not be written; their content is
4526 * unspecified. Size must be large enough to hold the image.
4527 *
4528 * The image in the target memory will be arranged in rows from
4529 * top to bottom, and pixels on a row from left to right. The pixel
4530 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
4531 * width * 4.
4532 *
4533 * Parameters src_x and src_y define the upper-left corner in buffer
4534 * coordinates (pixels) to copy from. Parameters width and height
4535 * define the size of the area to copy in pixels.
4536 *
4537 * The rectangle defined by src_x, src_y, width, height must fit in
4538 * the surface contents. Otherwise an error is returned.
4539 *
Changwoo Chof97d2502017-08-05 00:30:47 +09004540 * Use weston_surface_get_content_size to determine the content size; the
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004541 * needed target buffer size and rectangle limits.
4542 *
4543 * CURRENT IMPLEMENTATION RESTRICTIONS:
4544 * - the machine must be little-endian due to Pixman formats.
4545 *
4546 * NOTE: Pixman formats are premultiplied.
4547 */
4548WL_EXPORT int
4549weston_surface_copy_content(struct weston_surface *surface,
4550 void *target, size_t size,
4551 int src_x, int src_y,
4552 int width, int height)
4553{
4554 struct weston_renderer *rer = surface->compositor->renderer;
4555 int cw, ch;
4556 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
4557
4558 if (!rer->surface_copy_content)
4559 return -1;
4560
4561 weston_surface_get_content_size(surface, &cw, &ch);
4562
4563 if (src_x < 0 || src_y < 0)
4564 return -1;
4565
4566 if (width <= 0 || height <= 0)
4567 return -1;
4568
4569 if (src_x + width > cw || src_y + height > ch)
4570 return -1;
4571
4572 if (width * bytespp * height > size)
4573 return -1;
4574
4575 return rer->surface_copy_content(surface, target, size,
4576 src_x, src_y, width, height);
4577}
4578
Pekka Paalanene67858b2013-04-25 13:57:42 +03004579static void
4580subsurface_set_position(struct wl_client *client,
4581 struct wl_resource *resource, int32_t x, int32_t y)
4582{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004583 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004584
4585 if (!sub)
4586 return;
4587
4588 sub->position.x = x;
4589 sub->position.y = y;
4590 sub->position.set = 1;
4591}
4592
4593static struct weston_subsurface *
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004594subsurface_find_sibling(struct weston_subsurface *sub,
4595 struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004596{
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004597 struct weston_surface *parent = sub->parent;
4598 struct weston_subsurface *sibling;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004599
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004600 wl_list_for_each(sibling, &parent->subsurface_list, parent_link) {
4601 if (sibling->surface == surface && sibling != sub)
4602 return sibling;
4603 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004604
4605 return NULL;
4606}
4607
4608static struct weston_subsurface *
4609subsurface_sibling_check(struct weston_subsurface *sub,
4610 struct weston_surface *surface,
4611 const char *request)
4612{
4613 struct weston_subsurface *sibling;
4614
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004615 sibling = subsurface_find_sibling(sub, surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004616 if (!sibling) {
4617 wl_resource_post_error(sub->resource,
4618 WL_SUBSURFACE_ERROR_BAD_SURFACE,
4619 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004620 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004621 return NULL;
4622 }
4623
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004624 assert(sibling->parent == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004625
4626 return sibling;
4627}
4628
4629static void
4630subsurface_place_above(struct wl_client *client,
4631 struct wl_resource *resource,
4632 struct wl_resource *sibling_resource)
4633{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004634 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004635 struct weston_surface *surface =
4636 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004637 struct weston_subsurface *sibling;
4638
4639 if (!sub)
4640 return;
4641
4642 sibling = subsurface_sibling_check(sub, surface, "place_above");
4643 if (!sibling)
4644 return;
4645
4646 wl_list_remove(&sub->parent_link_pending);
4647 wl_list_insert(sibling->parent_link_pending.prev,
4648 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004649
4650 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004651}
4652
4653static void
4654subsurface_place_below(struct wl_client *client,
4655 struct wl_resource *resource,
4656 struct wl_resource *sibling_resource)
4657{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004658 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004659 struct weston_surface *surface =
4660 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004661 struct weston_subsurface *sibling;
4662
4663 if (!sub)
4664 return;
4665
4666 sibling = subsurface_sibling_check(sub, surface, "place_below");
4667 if (!sibling)
4668 return;
4669
4670 wl_list_remove(&sub->parent_link_pending);
4671 wl_list_insert(&sibling->parent_link_pending,
4672 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004673
4674 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004675}
4676
4677static void
4678subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
4679{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004680 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004681
4682 if (sub)
4683 sub->synchronized = 1;
4684}
4685
4686static void
4687subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
4688{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004689 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004690
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004691 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03004692 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004693
4694 /* If sub became effectively desynchronized, flush. */
4695 if (!weston_subsurface_is_synchronized(sub))
4696 weston_subsurface_synchronized_commit(sub);
4697 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004698}
4699
4700static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03004701weston_subsurface_unlink_parent(struct weston_subsurface *sub)
4702{
4703 wl_list_remove(&sub->parent_link);
4704 wl_list_remove(&sub->parent_link_pending);
4705 wl_list_remove(&sub->parent_destroy_listener.link);
4706 sub->parent = NULL;
4707}
4708
4709static void
4710weston_subsurface_destroy(struct weston_subsurface *sub);
4711
4712static void
4713subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
4714{
4715 struct weston_subsurface *sub =
4716 container_of(listener, struct weston_subsurface,
4717 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004718 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004719
4720 /* The protocol object (wl_resource) is left inert. */
4721 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004722 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004723
4724 weston_subsurface_destroy(sub);
4725}
4726
4727static void
4728subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
4729{
4730 struct weston_subsurface *sub =
4731 container_of(listener, struct weston_subsurface,
4732 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004733 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004734 assert(sub->surface != sub->parent);
4735
4736 if (weston_surface_is_mapped(sub->surface))
4737 weston_surface_unmap(sub->surface);
4738
4739 weston_subsurface_unlink_parent(sub);
4740}
4741
4742static void
4743subsurface_resource_destroy(struct wl_resource *resource)
4744{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004745 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004746
4747 if (sub)
4748 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004749}
4750
4751static void
4752subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
4753{
4754 wl_resource_destroy(resource);
4755}
4756
4757static void
4758weston_subsurface_link_parent(struct weston_subsurface *sub,
4759 struct weston_surface *parent)
4760{
4761 sub->parent = parent;
4762 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004763 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004764 &sub->parent_destroy_listener);
4765
4766 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4767 wl_list_insert(&parent->subsurface_list_pending,
4768 &sub->parent_link_pending);
4769}
4770
4771static void
4772weston_subsurface_link_surface(struct weston_subsurface *sub,
4773 struct weston_surface *surface)
4774{
4775 sub->surface = surface;
4776 sub->surface_destroy_listener.notify =
4777 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004778 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004779 &sub->surface_destroy_listener);
4780}
4781
4782static void
4783weston_subsurface_destroy(struct weston_subsurface *sub)
4784{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004785 struct weston_view *view, *next;
4786
Pekka Paalanene67858b2013-04-25 13:57:42 +03004787 assert(sub->surface);
4788
4789 if (sub->resource) {
4790 assert(weston_surface_to_subsurface(sub->surface) == sub);
4791 assert(sub->parent_destroy_listener.notify ==
4792 subsurface_handle_parent_destroy);
4793
George Kiagiadakised04d382014-06-13 18:10:26 +02004794 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
4795 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004796 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02004797 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05004798
Pekka Paalanene67858b2013-04-25 13:57:42 +03004799 if (sub->parent)
4800 weston_subsurface_unlink_parent(sub);
4801
Jason Ekstrand7b982072014-05-20 14:33:03 -05004802 weston_surface_state_fini(&sub->cached);
4803 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004804
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004805 sub->surface->committed = NULL;
4806 sub->surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004807 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004808 } else {
4809 /* the dummy weston_subsurface for the parent itself */
4810 assert(sub->parent_destroy_listener.notify == NULL);
4811 wl_list_remove(&sub->parent_link);
4812 wl_list_remove(&sub->parent_link_pending);
4813 }
4814
4815 wl_list_remove(&sub->surface_destroy_listener.link);
4816 free(sub);
4817}
4818
4819static const struct wl_subsurface_interface subsurface_implementation = {
4820 subsurface_destroy,
4821 subsurface_set_position,
4822 subsurface_place_above,
4823 subsurface_place_below,
4824 subsurface_set_sync,
4825 subsurface_set_desync
4826};
4827
4828static struct weston_subsurface *
4829weston_subsurface_create(uint32_t id, struct weston_surface *surface,
4830 struct weston_surface *parent)
4831{
4832 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004833 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004834
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
Jason Ekstranda7af7042013-10-12 22:38:11 -05004839 wl_list_init(&sub->unused_views);
4840
Jason Ekstranda85118c2013-06-27 20:17:02 -05004841 sub->resource =
4842 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004843 if (!sub->resource) {
4844 free(sub);
4845 return NULL;
4846 }
4847
Jason Ekstranda85118c2013-06-27 20:17:02 -05004848 wl_resource_set_implementation(sub->resource,
4849 &subsurface_implementation,
4850 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004851 weston_subsurface_link_surface(sub, surface);
4852 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05004853 weston_surface_state_init(&sub->cached);
4854 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004855 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004856
4857 return sub;
4858}
4859
4860/* Create a dummy subsurface for having the parent itself in its
4861 * sub-surface lists. Makes stacking order manipulation easy.
4862 */
4863static struct weston_subsurface *
4864weston_subsurface_create_for_parent(struct weston_surface *parent)
4865{
4866 struct weston_subsurface *sub;
4867
Bryce Harringtonde16d892014-11-20 22:21:57 -08004868 sub = zalloc(sizeof *sub);
4869 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004870 return NULL;
4871
4872 weston_subsurface_link_surface(sub, parent);
4873 sub->parent = parent;
4874 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4875 wl_list_insert(&parent->subsurface_list_pending,
4876 &sub->parent_link_pending);
4877
4878 return sub;
4879}
4880
4881static void
4882subcompositor_get_subsurface(struct wl_client *client,
4883 struct wl_resource *resource,
4884 uint32_t id,
4885 struct wl_resource *surface_resource,
4886 struct wl_resource *parent_resource)
4887{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004888 struct weston_surface *surface =
4889 wl_resource_get_user_data(surface_resource);
4890 struct weston_surface *parent =
4891 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004892 struct weston_subsurface *sub;
4893 static const char where[] = "get_subsurface: wl_subsurface@";
4894
4895 if (surface == parent) {
4896 wl_resource_post_error(resource,
4897 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4898 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004899 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004900 return;
4901 }
4902
4903 if (weston_surface_to_subsurface(surface)) {
4904 wl_resource_post_error(resource,
4905 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4906 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004907 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004908 return;
4909 }
4910
Pekka Paalanen50b67472014-10-01 15:02:41 +03004911 if (weston_surface_set_role(surface, "wl_subsurface", resource,
4912 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004913 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004914
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004915 if (weston_surface_get_main_surface(parent) == surface) {
4916 wl_resource_post_error(resource,
4917 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4918 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004919 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004920 return;
4921 }
4922
Pekka Paalanene67858b2013-04-25 13:57:42 +03004923 /* make sure the parent is in its own list */
4924 if (wl_list_empty(&parent->subsurface_list)) {
4925 if (!weston_subsurface_create_for_parent(parent)) {
4926 wl_resource_post_no_memory(resource);
4927 return;
4928 }
4929 }
4930
4931 sub = weston_subsurface_create(id, surface, parent);
4932 if (!sub) {
4933 wl_resource_post_no_memory(resource);
4934 return;
4935 }
4936
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004937 surface->committed = subsurface_committed;
4938 surface->committed_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004939 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004940}
4941
4942static void
4943subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
4944{
4945 wl_resource_destroy(resource);
4946}
4947
4948static const struct wl_subcompositor_interface subcompositor_interface = {
4949 subcompositor_destroy,
4950 subcompositor_get_subsurface
4951};
4952
4953static void
4954bind_subcompositor(struct wl_client *client,
4955 void *data, uint32_t version, uint32_t id)
4956{
4957 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004958 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004959
Jason Ekstranda85118c2013-06-27 20:17:02 -05004960 resource =
4961 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004962 if (resource == NULL) {
4963 wl_client_post_no_memory(client);
4964 return;
4965 }
4966 wl_resource_set_implementation(resource, &subcompositor_interface,
4967 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004968}
4969
Bryce Harrington0795ece2016-08-30 12:04:26 -07004970/** Set a DPMS mode on all of the compositor's outputs
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004971 *
4972 * \param compositor The compositor instance
4973 * \param state The DPMS state the outputs will be set to
4974 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004975static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004976weston_compositor_dpms(struct weston_compositor *compositor,
4977 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004978{
Emmanuel Gil Peyroteff793a2021-07-31 17:25:41 +02004979 struct weston_output *output;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004980
Emmanuel Gil Peyroteff793a2021-07-31 17:25:41 +02004981 wl_list_for_each(output, &compositor->output_list, link)
Bryce Harrington08976ac2016-08-30 12:05:16 -07004982 if (output->set_dpms)
4983 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004984}
4985
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004986/** Restores the compositor to active status
4987 *
4988 * \param compositor The compositor instance
4989 *
4990 * If the compositor was in a sleeping mode, all outputs are powered
4991 * back on via DPMS. Otherwise if the compositor was inactive
4992 * (idle/locked, offscreen, or sleeping) then the compositor's wake
4993 * signal will fire.
4994 *
4995 * Restarts the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004996 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004997 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004998WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004999weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02005000{
Neil Roberts8b62e202013-09-30 13:14:47 +01005001 uint32_t old_state = compositor->state;
5002
5003 /* The state needs to be changed before emitting the wake
5004 * signal because that may try to schedule a repaint which
5005 * will not work if the compositor is still sleeping */
5006 compositor->state = WESTON_COMPOSITOR_ACTIVE;
5007
5008 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005009 case WESTON_COMPOSITOR_SLEEPING:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005010 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01005011 case WESTON_COMPOSITOR_OFFSCREEN:
Daniel Stone893b9362016-11-08 15:47:09 +00005012 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005013 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005014 /* fall through */
5015 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005016 wl_event_source_timer_update(compositor->idle_source,
5017 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02005018 }
5019}
5020
Bryce Harringtonc9626a32015-12-11 13:11:38 -08005021/** Turns off rendering and frame events for the compositor.
5022 *
5023 * \param compositor The compositor instance
5024 *
5025 * This is used for example to prevent further rendering while the
5026 * compositor is shutting down.
5027 *
Bryce Harringtonc9626a32015-12-11 13:11:38 -08005028 * Stops the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005029 *
5030 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08005031 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005032WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01005033weston_compositor_offscreen(struct weston_compositor *compositor)
5034{
5035 switch (compositor->state) {
5036 case WESTON_COMPOSITOR_OFFSCREEN:
5037 return;
5038 case WESTON_COMPOSITOR_SLEEPING:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01005039 default:
5040 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
5041 wl_event_source_timer_update(compositor->idle_source, 0);
5042 }
5043}
5044
Bryce Harringtonc9626a32015-12-11 13:11:38 -08005045/** Powers down all attached output devices
5046 *
5047 * \param compositor The compositor instance
5048 *
5049 * Causes rendering to the outputs to cease, and no frame events to be
5050 * sent. Only powers down the outputs if the compositor is not already
5051 * in sleep mode.
5052 *
5053 * Stops the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005054 *
5055 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08005056 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01005057WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005058weston_compositor_sleep(struct weston_compositor *compositor)
5059{
5060 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
5061 return;
5062
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01005063 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005064 compositor->state = WESTON_COMPOSITOR_SLEEPING;
5065 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
5066}
5067
Bryce Harringtonc9626a32015-12-11 13:11:38 -08005068/** Sets compositor to idle mode
5069 *
5070 * \param data The compositor instance
5071 *
5072 * This is called when the idle timer fires. Once the compositor is in
5073 * idle mode it requires a wake action (e.g. via
5074 * weston_compositor_wake()) to restore it. The compositor's
5075 * idle_signal will be triggered when the idle event occurs.
5076 *
5077 * Idleness can be inhibited by setting the compositor's idle_inhibit
5078 * property.
5079 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04005080static int
5081idle_handler(void *data)
5082{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005083 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04005084
5085 if (compositor->idle_inhibit)
5086 return 1;
5087
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005088 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005089 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04005090
5091 return 1;
5092}
5093
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005094WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08005095weston_plane_init(struct weston_plane *plane,
5096 struct weston_compositor *ec,
5097 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005098{
5099 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02005100 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005101 plane->x = x;
5102 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08005103 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03005104
5105 /* Init the link so that the call to wl_list_remove() when releasing
5106 * the plane without ever stacking doesn't lead to a crash */
5107 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005108}
5109
5110WL_EXPORT void
5111weston_plane_release(struct weston_plane *plane)
5112{
Xiong Zhang97116532013-10-23 13:58:31 +08005113 struct weston_view *view;
5114
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005115 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02005116 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03005117
Pekka Paalanen6551c092021-05-03 16:09:45 +03005118 /*
5119 * Can't use paint node list here, weston_plane is not specific to an
5120 * output.
5121 */
Xiong Zhang97116532013-10-23 13:58:31 +08005122 wl_list_for_each(view, &plane->compositor->view_list, link) {
5123 if (view->plane == plane)
5124 view->plane = NULL;
5125 }
5126
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03005127 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005128}
5129
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005130/** weston_compositor_stack_plane
5131 * \ingroup compositor
5132 */
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02005133WL_EXPORT void
5134weston_compositor_stack_plane(struct weston_compositor *ec,
5135 struct weston_plane *plane,
5136 struct weston_plane *above)
5137{
5138 if (above)
5139 wl_list_insert(above->link.prev, &plane->link);
5140 else
5141 wl_list_insert(&ec->plane_list, &plane->link);
5142}
5143
Quentin Glidic4ef719c2016-07-05 20:44:33 +02005144static void
5145output_release(struct wl_client *client, struct wl_resource *resource)
5146{
5147 wl_resource_destroy(resource);
5148}
5149
5150static const struct wl_output_interface output_interface = {
5151 output_release,
5152};
5153
5154
Casey Dahlin9074db52012-04-19 22:50:09 -04005155static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04005156{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05005157 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04005158}
5159
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005160static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005161bind_output(struct wl_client *client,
5162 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05005163{
Pekka Paalanen05347622017-03-27 12:24:34 +03005164 struct weston_head *head = data;
5165 struct weston_output *output = head->output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005166 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04005167 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05005168
Jason Ekstranda85118c2013-06-27 20:17:02 -05005169 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06005170 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07005171 if (resource == NULL) {
5172 wl_client_post_no_memory(client);
5173 return;
5174 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04005175
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005176 wl_list_insert(&head->resource_list, wl_resource_get_link(resource));
Pekka Paalanen055c1132017-03-27 16:31:25 +03005177 wl_resource_set_implementation(resource, &output_interface, head,
Pekka Paalanen05347622017-03-27 12:24:34 +03005178 unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04005179
Pekka Paalanen05347622017-03-27 12:24:34 +03005180 assert(output);
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05005181 wl_output_send_geometry(resource,
5182 output->x,
5183 output->y,
Pekka Paalanen01f60212017-03-24 15:39:24 +02005184 head->mm_width,
5185 head->mm_height,
5186 head->subpixel,
5187 head->make, head->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04005188 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04005189 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02005190 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02005191 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005192
5193 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05005194 wl_output_send_mode(resource,
5195 mode->flags,
5196 mode->width,
5197 mode->height,
5198 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005199 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02005200
Jasper St. Pierre0013a292014-08-07 16:43:11 -04005201 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02005202 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05005203}
5204
Pekka Paalanendcac3512017-12-08 14:13:34 +02005205static void
5206weston_head_add_global(struct weston_head *head)
5207{
5208 head->global = wl_global_create(head->compositor->wl_display,
5209 &wl_output_interface, 3,
5210 head, bind_output);
5211}
5212
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02005213/** Remove the global wl_output protocol object
5214 *
5215 * \param head The head whose global to remove.
5216 *
5217 * Also orphans the wl_resources for this head (wl_output).
5218 */
5219static void
5220weston_head_remove_global(struct weston_head *head)
5221{
5222 struct wl_resource *resource, *tmp;
5223
5224 if (head->global)
5225 wl_global_destroy(head->global);
5226 head->global = NULL;
5227
5228 wl_resource_for_each_safe(resource, tmp, &head->resource_list) {
5229 unbind_resource(resource);
5230 wl_resource_set_destructor(resource, NULL);
5231 wl_resource_set_user_data(resource, NULL);
5232 }
Roman Gilge97391c2019-03-29 13:01:06 +01005233
5234 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
5235 /* It's sufficient to unset the destructor, then the list elements
5236 * won't be accessed.
5237 */
5238 wl_resource_set_destructor(resource, NULL);
5239 }
5240 wl_list_init(&head->xdg_output_resource_list);
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02005241}
5242
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005243/** Get the backing object of wl_output
5244 *
5245 * \param resource A wl_output protocol object.
5246 * \return The backing object (user data) of a wl_resource representing a
5247 * wl_output protocol object.
Marius Vlad78984ee2019-06-11 00:05:08 +03005248 *
5249 * \ingroup head
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005250 */
Pekka Paalanen055c1132017-03-27 16:31:25 +03005251WL_EXPORT struct weston_head *
5252weston_head_from_resource(struct wl_resource *resource)
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005253{
5254 assert(wl_resource_instance_of(resource, &wl_output_interface,
5255 &output_interface));
5256
5257 return wl_resource_get_user_data(resource);
5258}
5259
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005260/** Initialize a pre-allocated weston_head
5261 *
5262 * \param head The head to initialize.
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005263 * \param name The head name, e.g. the connector name or equivalent.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005264 *
5265 * The head will be safe to attach, detach and release.
5266 *
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005267 * The name is used in logs, and can be used by compositors as a configuration
5268 * identifier.
5269 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005270 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005271 * \internal
5272 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005273WL_EXPORT void
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005274weston_head_init(struct weston_head *head, const char *name)
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005275{
5276 /* Add some (in)sane defaults which can be used
5277 * for checking if an output was properly configured
5278 */
5279 memset(head, 0, sizeof *head);
5280
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005281 wl_list_init(&head->compositor_link);
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005282 wl_signal_init(&head->destroy_signal);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005283 wl_list_init(&head->output_link);
5284 wl_list_init(&head->resource_list);
Roman Gilge97391c2019-03-29 13:01:06 +01005285 wl_list_init(&head->xdg_output_resource_list);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005286 head->name = strdup(name);
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305287 head->current_protection = WESTON_HDCP_DISABLE;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005288}
5289
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005290/** Send output heads changed signal
5291 *
5292 * \param output The output that changed.
5293 *
5294 * Notify that the enabled output gained and/or lost heads, or that the
5295 * associated heads may have changed their connection status. This does not
5296 * include cases where the output becomes enabled or disabled. The registered
5297 * callbacks are called after the change has successfully happened.
5298 *
5299 * If connection status change causes the compositor to attach or detach a head
5300 * to an enabled output, the registered callbacks may be called multiple times.
Marius Vlad55d87362019-06-11 01:15:35 +03005301 *
5302 * \ingroup output
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005303 */
5304static void
5305weston_output_emit_heads_changed(struct weston_output *output)
5306{
5307 wl_signal_emit(&output->compositor->output_heads_changed_signal,
5308 output);
5309}
5310
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005311/** Idle task for emitting heads_changed_signal */
5312static void
5313weston_compositor_call_heads_changed(void *data)
5314{
5315 struct weston_compositor *compositor = data;
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005316 struct weston_head *head;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005317
5318 compositor->heads_changed_source = NULL;
5319
5320 wl_signal_emit(&compositor->heads_changed_signal, compositor);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005321
5322 wl_list_for_each(head, &compositor->head_list, compositor_link) {
5323 if (head->output && head->output->enabled)
5324 weston_output_emit_heads_changed(head->output);
5325 }
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005326}
5327
5328/** Schedule a call on idle to heads_changed callback
5329 *
5330 * \param compositor The Compositor.
5331 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005332 * \ingroup compositor
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005333 * \internal
5334 */
5335static void
5336weston_compositor_schedule_heads_changed(struct weston_compositor *compositor)
5337{
5338 struct wl_event_loop *loop;
5339
5340 if (compositor->heads_changed_source)
5341 return;
5342
5343 loop = wl_display_get_event_loop(compositor->wl_display);
5344 compositor->heads_changed_source = wl_event_loop_add_idle(loop,
5345 weston_compositor_call_heads_changed, compositor);
5346}
5347
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005348/** Register a new head
5349 *
5350 * \param compositor The compositor.
5351 * \param head The head to register, must not be already registered.
5352 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005353 * This signals the core that a new head has become available, leading to
5354 * heads_changed hook being called later.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005355 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005356 * \ingroup compositor
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005357 * \internal
5358 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005359WL_EXPORT void
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005360weston_compositor_add_head(struct weston_compositor *compositor,
5361 struct weston_head *head)
5362{
5363 assert(wl_list_empty(&head->compositor_link));
5364 assert(head->name);
5365
5366 wl_list_insert(compositor->head_list.prev, &head->compositor_link);
5367 head->compositor = compositor;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005368 weston_compositor_schedule_heads_changed(compositor);
5369}
5370
5371/** Adds a listener to be called when heads change
5372 *
5373 * \param compositor The compositor.
5374 * \param listener The listener to add.
5375 *
Marius Vlada2dace22019-06-12 16:05:44 +03005376 * The listener notify function argument is weston_compositor.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005377 *
5378 * The listener function will be called after heads are added or their
5379 * connection status has changed. Several changes may be accumulated into a
5380 * single call. The user is expected to iterate over the existing heads and
5381 * check their statuses to find out what changed.
5382 *
5383 * \sa weston_compositor_iterate_heads, weston_head_is_connected,
5384 * weston_head_is_enabled
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005385 * \ingroup compositor
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005386 */
5387WL_EXPORT void
5388weston_compositor_add_heads_changed_listener(struct weston_compositor *compositor,
5389 struct wl_listener *listener)
5390{
5391 wl_signal_add(&compositor->heads_changed_signal, listener);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005392}
5393
5394/** Iterate over available heads
5395 *
5396 * \param compositor The compositor.
Marius Vlada2dace22019-06-12 16:05:44 +03005397 * \param iter The iterator, or NULL for start.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005398 * \return The next available head in the list.
5399 *
5400 * Returns all available heads, regardless of being connected or enabled.
5401 *
5402 * You can iterate over all heads as follows:
5403 * \code
5404 * struct weston_head *head = NULL;
5405 *
5406 * while ((head = weston_compositor_iterate_heads(compositor, head))) {
5407 * ...
5408 * }
5409 * \endcode
5410 *
5411 * If you cause \c iter to be removed from the list, you cannot use it to
5412 * continue iterating. Removing any other item is safe.
5413 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005414 * \ingroup compositor
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005415 */
5416WL_EXPORT struct weston_head *
5417weston_compositor_iterate_heads(struct weston_compositor *compositor,
5418 struct weston_head *iter)
5419{
5420 struct wl_list *list = &compositor->head_list;
5421 struct wl_list *node;
5422
5423 assert(compositor);
5424 assert(!iter || iter->compositor == compositor);
5425
5426 if (iter)
5427 node = iter->compositor_link.next;
5428 else
5429 node = list->next;
5430
5431 assert(node);
5432 assert(!iter || node != &iter->compositor_link);
5433
5434 if (node == list)
5435 return NULL;
5436
5437 return container_of(node, struct weston_head, compositor_link);
5438}
5439
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005440/** Iterate over attached heads
5441 *
5442 * \param output The output whose heads to iterate.
Marius Vlada2dace22019-06-12 16:05:44 +03005443 * \param iter The iterator, or NULL for start.
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005444 * \return The next attached head in the list.
5445 *
5446 * Returns all heads currently attached to the output.
5447 *
5448 * You can iterate over all heads as follows:
5449 * \code
5450 * struct weston_head *head = NULL;
5451 *
5452 * while ((head = weston_output_iterate_heads(output, head))) {
5453 * ...
5454 * }
5455 * \endcode
5456 *
5457 * If you cause \c iter to be removed from the list, you cannot use it to
5458 * continue iterating. Removing any other item is safe.
5459 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05005460 * \ingroup output
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005461 */
5462WL_EXPORT struct weston_head *
5463weston_output_iterate_heads(struct weston_output *output,
5464 struct weston_head *iter)
5465{
5466 struct wl_list *list = &output->head_list;
5467 struct wl_list *node;
5468
5469 assert(output);
5470 assert(!iter || iter->output == output);
5471
5472 if (iter)
5473 node = iter->output_link.next;
5474 else
5475 node = list->next;
5476
5477 assert(node);
5478 assert(!iter || node != &iter->output_link);
5479
5480 if (node == list)
5481 return NULL;
5482
5483 return container_of(node, struct weston_head, output_link);
5484}
5485
Pekka Paalanendcac3512017-12-08 14:13:34 +02005486/** Attach a head to an output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005487 *
5488 * \param output The output to attach to.
5489 * \param head The head that is not yet attached.
5490 * \return 0 on success, -1 on failure.
5491 *
5492 * Attaches the given head to the output. All heads of an output are clones
5493 * and share the resolution and timings.
5494 *
5495 * Cloning heads this way uses less resources than creating an output for
5496 * each head, but is not always possible due to environment, driver and hardware
5497 * limitations.
5498 *
5499 * On failure, the head remains unattached. Success of this function does not
5500 * guarantee the output configuration is actually valid. The final checks are
Pekka Paalanendcac3512017-12-08 14:13:34 +02005501 * made on weston_output_enable() unless the output was already enabled.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005502 *
Marius Vlad55d87362019-06-11 01:15:35 +03005503 * \ingroup output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005504 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005505WL_EXPORT int
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005506weston_output_attach_head(struct weston_output *output,
5507 struct weston_head *head)
5508{
Pekka Paalanendcac3512017-12-08 14:13:34 +02005509 char *head_names;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005510
5511 if (!wl_list_empty(&head->output_link))
5512 return -1;
5513
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005514 if (output->attach_head) {
5515 if (output->attach_head(output, head) < 0)
5516 return -1;
5517 } else if (!wl_list_empty(&output->head_list)) {
5518 /* No support for clones in the legacy path. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005519 return -1;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005520 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005521
5522 head->output = output;
5523 wl_list_insert(output->head_list.prev, &head->output_link);
5524
Pekka Paalanendcac3512017-12-08 14:13:34 +02005525 if (output->enabled) {
5526 weston_head_add_global(head);
5527
5528 head_names = weston_output_create_heads_string(output);
5529 weston_log("Output '%s' updated to have head(s) %s\n",
5530 output->name, head_names);
5531 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005532
5533 weston_output_emit_heads_changed(output);
Pekka Paalanendcac3512017-12-08 14:13:34 +02005534 }
5535
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005536 return 0;
5537}
5538
5539/** Detach a head from its output
5540 *
5541 * \param head The head to detach.
5542 *
5543 * It is safe to detach a non-attached head.
5544 *
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005545 * If the head is attached to an enabled output and the output will be left
5546 * with no heads, the output will be disabled.
5547 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005548 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005549 * \sa weston_output_disable
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005550 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005551WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005552weston_head_detach(struct weston_head *head)
5553{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005554 struct weston_output *output = head->output;
Pekka Paalanena0106992017-12-08 16:11:17 +02005555 char *head_names;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005556
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005557 wl_list_remove(&head->output_link);
5558 wl_list_init(&head->output_link);
5559 head->output = NULL;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005560
5561 if (!output)
5562 return;
5563
5564 if (output->detach_head)
5565 output->detach_head(output, head);
5566
5567 if (output->enabled) {
5568 weston_head_remove_global(head);
5569
Pekka Paalanena0106992017-12-08 16:11:17 +02005570 if (wl_list_empty(&output->head_list)) {
5571 weston_log("Output '%s' no heads left, disabling.\n",
5572 output->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005573 weston_output_disable(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02005574 } else {
5575 head_names = weston_output_create_heads_string(output);
5576 weston_log("Output '%s' updated to have head(s) %s\n",
5577 output->name, head_names);
5578 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005579
5580 weston_output_emit_heads_changed(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02005581 }
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005582 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005583}
5584
5585/** Destroy a head
5586 *
5587 * \param head The head to be released.
5588 *
5589 * Destroys the head. The caller is responsible for freeing the memory pointed
5590 * to by \c head.
5591 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005592 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005593 * \internal
5594 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005595WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005596weston_head_release(struct weston_head *head)
5597{
Marius Vladf2452d62021-12-09 14:07:21 +02005598 weston_signal_emit_mutable(&head->destroy_signal, head);
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005599
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005600 weston_head_detach(head);
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005601
5602 free(head->make);
5603 free(head->model);
5604 free(head->serial_number);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005605 free(head->name);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005606
5607 wl_list_remove(&head->compositor_link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005608}
5609
Pekka Paalanene19970f2017-08-28 14:11:02 +03005610static void
5611weston_head_set_device_changed(struct weston_head *head)
5612{
5613 head->device_changed = true;
5614
5615 if (head->compositor)
5616 weston_compositor_schedule_heads_changed(head->compositor);
5617}
5618
5619/** String equal comparison with NULLs being equal */
5620static bool
5621str_null_eq(const char *a, const char *b)
5622{
5623 if (!a && !b)
5624 return true;
5625
5626 if (!!a != !!b)
5627 return false;
5628
5629 return strcmp(a, b) == 0;
5630}
5631
Pekka Paalanen01f60212017-03-24 15:39:24 +02005632/** Store monitor make, model and serial number
5633 *
5634 * \param head The head to modify.
5635 * \param make The monitor make. If EDID is available, the PNP ID. Otherwise
5636 * any string, or NULL for none.
5637 * \param model The monitor model or name, or a made-up string, or NULL for
5638 * none.
5639 * \param serialno The monitor serial number, a made-up string, or NULL for
5640 * none.
5641 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005642 * This may set the device_changed flag.
5643 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005644 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005645 * \internal
5646 */
5647WL_EXPORT void
5648weston_head_set_monitor_strings(struct weston_head *head,
5649 const char *make,
5650 const char *model,
5651 const char *serialno)
5652{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005653 if (str_null_eq(head->make, make) &&
5654 str_null_eq(head->model, model) &&
5655 str_null_eq(head->serial_number, serialno))
5656 return;
5657
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005658 free(head->make);
5659 free(head->model);
5660 free(head->serial_number);
5661
5662 head->make = make ? strdup(make) : NULL;
5663 head->model = model ? strdup(model) : NULL;
5664 head->serial_number = serialno ? strdup(serialno) : NULL;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005665
5666 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005667}
5668
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005669/** Store display non-desktop status
5670 *
5671 * \param head The head to modify.
5672 * \param non_desktop Whether the head connects to a non-desktop display.
5673 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005674 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005675 * \internal
5676 */
5677WL_EXPORT void
5678weston_head_set_non_desktop(struct weston_head *head, bool non_desktop)
5679{
5680 if (head->non_desktop == non_desktop)
5681 return;
5682
5683 head->non_desktop = non_desktop;
5684
5685 weston_head_set_device_changed(head);
5686}
5687
Lucas Stacha69cb712019-11-25 23:29:31 +00005688/** Store display transformation
5689 *
5690 * \param head The head to modify.
5691 * \param transform The transformation to apply for this head
5692 *
5693 * This may set the device_changed flag.
5694 *
5695 * \ingroup head
5696 * \internal
5697 */
5698WL_EXPORT void
5699weston_head_set_transform(struct weston_head *head, uint32_t transform)
5700{
5701 if (head->transform == transform)
5702 return;
5703
5704 head->transform = transform;
5705
5706 weston_head_set_device_changed(head);
5707}
5708
5709
Pekka Paalanen01f60212017-03-24 15:39:24 +02005710/** Store physical image size
5711 *
5712 * \param head The head to modify.
5713 * \param mm_width Image area width in millimeters.
5714 * \param mm_height Image area height in millimeters.
5715 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005716 * This may set the device_changed flag.
5717 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005718 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005719 * \internal
5720 */
5721WL_EXPORT void
5722weston_head_set_physical_size(struct weston_head *head,
5723 int32_t mm_width, int32_t mm_height)
5724{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005725 if (head->mm_width == mm_width &&
5726 head->mm_height == mm_height)
5727 return;
5728
Pekka Paalanen01f60212017-03-24 15:39:24 +02005729 head->mm_width = mm_width;
5730 head->mm_height = mm_height;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005731
5732 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005733}
5734
5735/** Store monitor sub-pixel layout
5736 *
5737 * \param head The head to modify.
5738 * \param sp Sub-pixel layout. The possible values are:
5739 * - WL_OUTPUT_SUBPIXEL_UNKNOWN,
5740 * - WL_OUTPUT_SUBPIXEL_NONE,
5741 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB,
5742 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR,
5743 * - WL_OUTPUT_SUBPIXEL_VERTICAL_RGB,
5744 * - WL_OUTPUT_SUBPIXEL_VERTICAL_BGR
5745 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005746 * This may set the device_changed flag.
5747 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005748 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005749 * \internal
5750 */
5751WL_EXPORT void
5752weston_head_set_subpixel(struct weston_head *head,
5753 enum wl_output_subpixel sp)
5754{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005755 if (head->subpixel == sp)
5756 return;
5757
Pekka Paalanen01f60212017-03-24 15:39:24 +02005758 head->subpixel = sp;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005759
5760 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005761}
5762
5763/** Mark the monitor as internal
5764 *
5765 * This is used for embedded screens, like laptop panels.
5766 *
5767 * \param head The head to mark as internal.
5768 *
5769 * By default a head is external. The type is often inferred from the physical
5770 * connector type.
5771 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005772 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005773 * \internal
5774 */
5775WL_EXPORT void
5776weston_head_set_internal(struct weston_head *head)
5777{
5778 head->connection_internal = true;
5779}
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005780
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005781/** Store connector status
5782 *
5783 * \param head The head to modify.
5784 * \param connected Whether the head is connected.
5785 *
5786 * Connectors are created as disconnected. This function can be used to
5787 * set the connector status.
5788 *
5789 * The status should be set to true when a physical connector is connected to
5790 * a video sink device like a monitor and to false when the connector is
5791 * disconnected. For nested backends, the connection status should reflect the
5792 * connection to the parent display server.
5793 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005794 * When the connection status changes, it schedules a call to the heads_changed
Pekka Paalanene19970f2017-08-28 14:11:02 +03005795 * hook and sets the device_changed flag.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005796 *
5797 * \sa weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005798 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005799 * \internal
5800 */
5801WL_EXPORT void
5802weston_head_set_connection_status(struct weston_head *head, bool connected)
5803{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005804 if (head->connected == connected)
5805 return;
5806
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005807 head->connected = connected;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005808
Pekka Paalanene19970f2017-08-28 14:11:02 +03005809 weston_head_set_device_changed(head);
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005810}
5811
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305812static void
5813weston_output_compute_protection(struct weston_output *output)
5814{
5815 struct weston_head *head;
5816 enum weston_hdcp_protection op_protection;
5817 bool op_protection_valid = false;
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305818 struct weston_compositor *wc = output->compositor;
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305819
5820 wl_list_for_each(head, &output->head_list, output_link) {
5821 if (!op_protection_valid) {
5822 op_protection = head->current_protection;
5823 op_protection_valid = true;
5824 }
5825 if (head->current_protection < op_protection)
5826 op_protection = head->current_protection;
5827 }
5828
5829 if (!op_protection_valid)
5830 op_protection = WESTON_HDCP_DISABLE;
5831
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305832 if (output->current_protection != op_protection) {
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305833 output->current_protection = op_protection;
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305834 weston_output_damage(output);
Ankit Nautiyalfaa5ab42019-07-08 13:23:24 +05305835 weston_schedule_surface_protection_update(wc);
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305836 }
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305837}
5838
5839WL_EXPORT void
5840weston_head_set_content_protection_status(struct weston_head *head,
5841 enum weston_hdcp_protection status)
5842{
5843 head->current_protection = status;
5844 if (head->output)
5845 weston_output_compute_protection(head->output);
5846}
5847
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005848/** Is the head currently connected?
5849 *
5850 * \param head The head to query.
5851 * \return Connection status.
5852 *
5853 * Returns true if the head is physically connected to a monitor, or in
5854 * case of a nested backend returns true when there is a connection to the
5855 * parent display server.
5856 *
5857 * This is independent from the head being enabled.
5858 *
5859 * \sa weston_head_is_enabled
Marius Vlad78984ee2019-06-11 00:05:08 +03005860 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005861 */
5862WL_EXPORT bool
5863weston_head_is_connected(struct weston_head *head)
5864{
5865 return head->connected;
5866}
5867
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005868/** Is the head currently enabled?
5869 *
5870 * \param head The head to query.
5871 * \return Video status.
5872 *
5873 * Returns true if the head is currently transmitting a video stream.
5874 *
5875 * This is independent of the head being connected.
5876 *
5877 * \sa weston_head_is_connected
Marius Vlad78984ee2019-06-11 00:05:08 +03005878 * \ingroup head
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005879 */
5880WL_EXPORT bool
5881weston_head_is_enabled(struct weston_head *head)
5882{
5883 if (!head->output)
5884 return false;
5885
5886 return head->output->enabled;
5887}
5888
Pekka Paalanene19970f2017-08-28 14:11:02 +03005889/** Has the device information changed?
5890 *
5891 * \param head The head to query.
5892 * \return True if the device information has changed since last reset.
5893 *
5894 * The information about the connected display device, e.g. a monitor, may
5895 * change without being disconnected in between. Changing information
5896 * causes a call to the heads_changed hook.
5897 *
5898 * The information includes make, model, serial number, physical size,
5899 * and sub-pixel type. The connection status is also included.
5900 *
5901 * \sa weston_head_reset_device_changed, weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005902 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03005903 */
5904WL_EXPORT bool
5905weston_head_is_device_changed(struct weston_head *head)
5906{
5907 return head->device_changed;
5908}
5909
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005910/** Does the head represent a non-desktop display?
5911 *
5912 * \param head The head to query.
5913 * \return True if the device is a non-desktop display.
5914 *
5915 * Non-desktop heads are not attached to outputs by default.
5916 * This stops weston from extending the desktop onto head mounted displays.
5917 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005918 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005919 */
5920WL_EXPORT bool
5921weston_head_is_non_desktop(struct weston_head *head)
5922{
5923 return head->non_desktop;
5924}
5925
Pekka Paalanene19970f2017-08-28 14:11:02 +03005926/** Acknowledge device information change
5927 *
5928 * \param head The head to acknowledge.
5929 *
5930 * Clears the device changed flag on this head. When a compositor has processed
5931 * device information, it should call this to be able to notice further
5932 * changes.
5933 *
5934 * \sa weston_head_is_device_changed
Marius Vlad78984ee2019-06-11 00:05:08 +03005935 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03005936 */
5937WL_EXPORT void
5938weston_head_reset_device_changed(struct weston_head *head)
5939{
5940 head->device_changed = false;
5941}
5942
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005943/** Get the name of a head
5944 *
5945 * \param head The head to query.
5946 * \return The head's name, not NULL.
5947 *
5948 * The name depends on the backend. The DRM backend uses connector names,
5949 * other backends may use hardcoded names or user-given names.
Marius Vlad78984ee2019-06-11 00:05:08 +03005950 *
5951 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005952 */
5953WL_EXPORT const char *
5954weston_head_get_name(struct weston_head *head)
5955{
5956 return head->name;
5957}
5958
5959/** Get the output the head is attached to
5960 *
5961 * \param head The head to query.
5962 * \return The output the head is attached to, or NULL if detached.
Marius Vlad78984ee2019-06-11 00:05:08 +03005963 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005964 */
5965WL_EXPORT struct weston_output *
5966weston_head_get_output(struct weston_head *head)
5967{
5968 return head->output;
5969}
5970
Lucas Stacha69cb712019-11-25 23:29:31 +00005971/** Get the head's native transformation
5972 *
5973 * \param head The head to query.
5974 * \return The head's native transform, as a WL_OUTPUT_TRANSFORM_* value
5975 *
5976 * A weston_head may have a 'native' transform provided by the backend.
5977 * Examples include panels which are physically rotated, where the rotation
5978 * is recorded and described as part of the system configuration. This call
5979 * will return any known native transform for the head.
5980 *
5981 * \ingroup head
5982 */
5983WL_EXPORT uint32_t
5984weston_head_get_transform(struct weston_head *head)
5985{
5986 return head->transform;
5987}
5988
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005989/** Add destroy callback for a head
5990 *
5991 * \param head The head to watch for.
5992 * \param listener The listener to add. The \c notify member must be set.
5993 *
5994 * Heads may get destroyed for various reasons by the backends. If a head is
5995 * attached to an output, the compositor should listen for head destruction
5996 * and reconfigure or destroy the output if necessary.
5997 *
5998 * The destroy callbacks will be called on weston_head destruction before any
5999 * automatic detaching from an associated weston_output and before any
6000 * weston_head information is lost.
6001 *
6002 * The \c data argument to the notify callback is the weston_head being
6003 * destroyed.
Marius Vlad78984ee2019-06-11 00:05:08 +03006004 *
6005 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03006006 */
6007WL_EXPORT void
6008weston_head_add_destroy_listener(struct weston_head *head,
6009 struct wl_listener *listener)
6010{
6011 wl_signal_add(&head->destroy_signal, listener);
6012}
6013
6014/** Look up destroy listener for a head
6015 *
6016 * \param head The head to query.
6017 * \param notify The notify function used used for the added destroy listener.
6018 * \return The listener, or NULL if not found.
6019 *
6020 * This looks up the previously added destroy listener struct based on the
6021 * notify function it has. The listener can be used to access user data
6022 * through \c container_of().
6023 *
6024 * \sa wl_signal_get()
Marius Vlad78984ee2019-06-11 00:05:08 +03006025 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03006026 */
6027WL_EXPORT struct wl_listener *
6028weston_head_get_destroy_listener(struct weston_head *head,
6029 wl_notify_func_t notify)
6030{
6031 return wl_signal_get(&head->destroy_signal, notify);
6032}
6033
David Fort0de859e2016-05-27 23:22:57 +02006034/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02006035static void
David Fort0de859e2016-05-27 23:22:57 +02006036weston_compositor_reflow_outputs(struct weston_compositor *compositor,
6037 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02006038{
6039 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02006040 bool start_resizing = false;
6041
6042 if (!delta_width)
6043 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02006044
6045 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02006046 if (output == resized_output) {
6047 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02006048 continue;
6049 }
6050
David Fort0de859e2016-05-27 23:22:57 +02006051 if (start_resizing) {
6052 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02006053 output->dirty = 1;
6054 }
6055 }
6056}
6057
Pekka Paalanen06920792020-12-08 15:22:36 +02006058/** Transform a region in-place from global to output coordinates
6059 *
6060 * \param output The output that defines the transformation.
6061 * \param region The region to be transformed in-place.
6062 *
6063 * This takes a region in the global coordinate system, and takes into account
6064 * output position, transform and scale, and the zoom, and converts the region
6065 * into output pixel coordinates in the framebuffer.
6066 *
6067 * Uses floating-point operations if zoom is active, which may round to expand
6068 * the region.
6069 *
6070 * \internal
6071 * \ingroup output
6072 */
6073WL_EXPORT void
6074weston_output_region_from_global(struct weston_output *output,
6075 pixman_region32_t *region)
6076{
6077 if (output->zoom.active) {
6078 weston_matrix_transform_region(region, &output->matrix, region);
6079 } else {
6080 pixman_region32_translate(region, -output->x, -output->y);
6081 weston_transformed_region(output->width, output->height,
6082 output->transform,
6083 output->current_scale,
6084 region, region);
6085 }
6086}
6087
Pekka Paalanend72bad22017-03-29 17:01:41 +03006088static void
Scott Moreauccbf29d2012-02-22 14:21:41 -07006089weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01006090{
Scott Moreau850ca422012-05-21 15:21:25 -06006091 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05006092
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006093 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05006094 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06006095
Scott Moreauccbf29d2012-02-22 14:21:41 -07006096 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06006097 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05006098 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01006099 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05006100 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01006101 weston_matrix_scale(&output->matrix, magnification,
6102 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07006103 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04006104
Jason Ekstrandfb23df72014-10-16 10:55:21 -05006105 switch (output->transform) {
6106 case WL_OUTPUT_TRANSFORM_FLIPPED:
6107 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
6108 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
6109 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
6110 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
6111 weston_matrix_scale(&output->matrix, -1, 1, 1);
6112 break;
6113 }
6114
6115 switch (output->transform) {
6116 default:
6117 case WL_OUTPUT_TRANSFORM_NORMAL:
6118 case WL_OUTPUT_TRANSFORM_FLIPPED:
6119 break;
6120 case WL_OUTPUT_TRANSFORM_90:
6121 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +02006122 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
6123 weston_matrix_rotate_xy(&output->matrix, 0, -1);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05006124 break;
6125 case WL_OUTPUT_TRANSFORM_180:
6126 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
6127 weston_matrix_translate(&output->matrix,
6128 -output->width, -output->height, 0);
6129 weston_matrix_rotate_xy(&output->matrix, -1, 0);
6130 break;
6131 case WL_OUTPUT_TRANSFORM_270:
6132 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +02006133 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
6134 weston_matrix_rotate_xy(&output->matrix, 0, 1);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05006135 break;
6136 }
6137
6138 if (output->current_scale != 1)
6139 weston_matrix_scale(&output->matrix,
6140 output->current_scale,
6141 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01006142
Scott Moreauccbf29d2012-02-22 14:21:41 -07006143 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05006144
6145 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07006146}
6147
Scott Moreau1bad5db2012-08-18 01:04:05 -06006148static void
Alexander Larsson0b135062013-05-28 16:23:36 +02006149weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06006150{
6151 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03006152 output->native_scale = scale;
6153 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06006154
Pekka Paalanen59987fa2016-04-26 15:50:59 +03006155 convert_size_by_transform_scale(&output->width, &output->height,
6156 output->current_mode->width,
6157 output->current_mode->height,
6158 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02006159}
6160
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006161static void
6162weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07006163{
6164 output->x = x;
6165 output->y = y;
6166
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006167 pixman_region32_fini(&output->region);
Scott Moreauccbf29d2012-02-22 14:21:41 -07006168 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06006169 output->width,
6170 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01006171}
6172
Marius Vlad55d87362019-06-11 01:15:35 +03006173/**
6174 * \ingroup output
6175 */
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006176WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006177weston_output_move(struct weston_output *output, int x, int y)
6178{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006179 struct weston_head *head;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006180 struct wl_resource *resource;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006181 int ver;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006182
6183 output->move_x = x - output->x;
6184 output->move_y = y - output->y;
6185
6186 if (output->move_x == 0 && output->move_y == 0)
6187 return;
6188
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006189 weston_output_init_geometry(output, x, y);
6190
6191 output->dirty = 1;
6192
6193 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006194 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006195
6196 /* Notify clients of the change for output position. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006197 wl_list_for_each(head, &output->head_list, output_link) {
6198 wl_resource_for_each(resource, &head->resource_list) {
6199 wl_output_send_geometry(resource,
6200 output->x,
6201 output->y,
6202 head->mm_width,
6203 head->mm_height,
6204 head->subpixel,
6205 head->make,
6206 head->model,
6207 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08006208
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006209 ver = wl_resource_get_version(resource);
6210 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
6211 wl_output_send_done(resource);
6212 }
Roman Gilge97391c2019-03-29 13:01:06 +01006213
6214 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
6215 zxdg_output_v1_send_logical_position(resource,
6216 output->x,
6217 output->y);
6218 zxdg_output_v1_send_done(resource);
6219 }
Quanxian Wangb2c86362014-03-14 09:16:25 +08006220 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006221}
6222
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006223/** Signal that a pending output is taken into use.
6224 *
6225 * Removes the output from the pending list and adds it to the compositor's
6226 * list of enabled outputs. The output created signal is emitted.
Giulio Camuffob1147152015-05-06 21:41:57 +03006227 *
Pekka Paalanen2210ad02017-03-30 15:48:06 +03006228 * The output gets an internal ID assigned, and the wl_output global is
6229 * created.
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03006230 *
Giulio Camuffob1147152015-05-06 21:41:57 +03006231 * \param compositor The compositor instance.
6232 * \param output The output to be added.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006233 *
6234 * \internal
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006235 * \ingroup compositor
Giulio Camuffob1147152015-05-06 21:41:57 +03006236 */
Pekka Paalanenf9681b52017-03-29 16:58:48 +03006237static void
Giulio Camuffob1147152015-05-06 21:41:57 +03006238weston_compositor_add_output(struct weston_compositor *compositor,
6239 struct weston_output *output)
6240{
Armin Krezoviće5403842016-08-05 15:28:29 +02006241 struct weston_view *view, *next;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03006242 struct weston_head *head;
Armin Krezoviće5403842016-08-05 15:28:29 +02006243
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006244 assert(!output->enabled);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03006245
6246 /* Verify we haven't reached the limit of 32 available output IDs */
6247 assert(ffs(~compositor->output_id_pool) > 0);
6248
6249 /* Invert the output id pool and look for the lowest numbered
6250 * switch (the least significant bit). Take that bit's position
6251 * as our ID, and mark it used in the compositor's output_id_pool.
6252 */
6253 output->id = ffs(~compositor->output_id_pool) - 1;
6254 compositor->output_id_pool |= 1u << output->id;
6255
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006256 wl_list_remove(&output->link);
Giulio Camuffob1147152015-05-06 21:41:57 +03006257 wl_list_insert(compositor->output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006258 output->enabled = true;
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006259
Pekka Paalanendcac3512017-12-08 14:13:34 +02006260 wl_list_for_each(head, &output->head_list, output_link)
6261 weston_head_add_global(head);
Pekka Paalanen2210ad02017-03-30 15:48:06 +03006262
Giulio Camuffob1147152015-05-06 21:41:57 +03006263 wl_signal_emit(&compositor->output_created_signal, output);
Armin Krezoviće5403842016-08-05 15:28:29 +02006264
Pekka Paalanen6551c092021-05-03 16:09:45 +03006265 /*
6266 * Use view_list, as paint nodes have not been created for this
6267 * output yet. Any existing view might touch this new output.
6268 */
Armin Krezoviće5403842016-08-05 15:28:29 +02006269 wl_list_for_each_safe(view, next, &compositor->view_list, link)
6270 weston_view_geometry_dirty(view);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04006271}
6272
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006273/** Transform device coordinates into global coordinates
6274 *
Marius Vlada2dace22019-06-12 16:05:44 +03006275 * \param output the weston_output object
6276 * \param[in] device_x X coordinate in device units.
6277 * \param[in] device_y Y coordinate in device units.
6278 * \param[out] x X coordinate in the global space.
6279 * \param[out] y Y coordinate in the global space.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006280 *
Marius Vlada2dace22019-06-12 16:05:44 +03006281 * Transforms coordinates from the device coordinate space (physical pixel
6282 * units) to the global coordinate space (logical pixel units). This takes
6283 * into account output transform and scale.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006284 *
Marius Vlad55d87362019-06-11 01:15:35 +03006285 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006286 * \internal
6287 */
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006288WL_EXPORT void
6289weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02006290 double device_x, double device_y,
6291 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006292{
Derek Foreman0f679412014-10-02 13:41:17 -05006293 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02006294 device_x,
6295 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05006296 0.0,
6297 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006298
Derek Foreman67a18b92015-03-24 11:36:14 -05006299 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006300
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02006301 *x = p.f[0] / p.f[3];
6302 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006303}
6304
Pekka Paalanen1d2eee22021-02-25 12:03:28 +02006305static void
6306weston_output_reset_color_transforms(struct weston_output *output)
6307{
Pekka Paalanencda39512021-03-22 17:32:07 +02006308 weston_color_transform_unref(output->from_sRGB_to_output);
6309 output->from_sRGB_to_output = NULL;
Pekka Paalanen8fb23ed2021-03-23 13:40:43 +02006310 weston_color_transform_unref(output->from_sRGB_to_blend);
6311 output->from_sRGB_to_blend = NULL;
Pekka Paalanen1d2eee22021-02-25 12:03:28 +02006312 weston_color_transform_unref(output->from_blend_to_output);
6313 output->from_blend_to_output = NULL;
6314}
6315
Pekka Paalanen9a0aa372021-06-08 13:38:35 +03006316static bool
6317weston_output_set_color_transforms(struct weston_output *output)
6318{
6319 struct weston_color_manager *cm = output->compositor->color_manager;
6320 struct weston_color_transform *blend_to_output = NULL;
6321 struct weston_color_transform *sRGB_to_output = NULL;
6322 struct weston_color_transform *sRGB_to_blend = NULL;
6323 bool ok;
6324
6325 ok = cm->get_output_color_transform(cm, output, &blend_to_output);
6326 ok = ok && cm->get_sRGB_to_output_color_transform(cm, output,
6327 &sRGB_to_output);
6328 ok = ok && cm->get_sRGB_to_blend_color_transform(cm, output,
6329 &sRGB_to_blend);
6330 if (!ok) {
6331 weston_log("Creating color transformation for output \"%s\" failed.\n",
6332 output->name);
6333 weston_color_transform_unref(blend_to_output);
6334 weston_color_transform_unref(sRGB_to_output);
6335 weston_color_transform_unref(sRGB_to_blend);
6336
6337 return false;
6338 }
6339
6340 weston_output_reset_color_transforms(output);
6341 output->from_blend_to_output = blend_to_output;
6342 output->from_blend_to_output_by_backend = false;
6343 output->from_sRGB_to_output = sRGB_to_output;
6344 output->from_sRGB_to_blend = sRGB_to_blend;
6345
Pekka Paalanen9a9e6ce2021-06-08 14:17:44 +03006346 weston_log("Output '%s' using color profile: %s\n", output->name,
6347 weston_color_profile_get_description(output->color_profile));
6348
Pekka Paalanen9a0aa372021-06-08 13:38:35 +03006349 return true;
6350}
6351
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006352/** Removes output from compositor's list of enabled outputs
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006353 *
6354 * \param output The weston_output object that is being removed.
6355 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006356 * The following happens:
6357 *
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03006358 * - Destroys all paint nodes related to the output.
6359 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006360 * - The output assignments of all views in the current scenegraph are
6361 * recomputed.
6362 *
Pekka Paalanen1d2eee22021-02-25 12:03:28 +02006363 * - Destroys output's color transforms.
6364 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006365 * - Presentation feedback is discarded.
6366 *
6367 * - Compositor is notified that outputs were changed and
6368 * applies the necessary changes to re-layout outputs.
6369 *
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006370 * - The output is put back in the pending outputs list.
6371 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006372 * - Signal is emitted to notify all users of the weston_output
6373 * object that the output is being destroyed.
6374 *
6375 * - wl_output protocol objects referencing this weston_output
Pekka Paalanen2210ad02017-03-30 15:48:06 +03006376 * are made inert, and the wl_output global is removed.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006377 *
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03006378 * - The output's internal ID is released.
6379 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006380 * \ingroup compositor
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006381 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006382 */
6383static void
6384weston_compositor_remove_output(struct weston_output *output)
6385{
Pekka Paalanenbccda712017-03-29 16:16:04 +03006386 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03006387 struct weston_paint_node *pnode, *pntmp;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006388 struct weston_view *view;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03006389 struct weston_head *head;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006390
6391 assert(output->destroying);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006392 assert(output->enabled);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006393
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03006394 wl_list_for_each_safe(pnode, pntmp,
6395 &output->paint_node_list, output_link) {
6396 weston_paint_node_destroy(pnode);
6397 }
Pekka Paalanen2fddc532021-04-30 17:41:29 +03006398 assert(wl_list_empty(&output->paint_node_z_order_list));
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03006399
Pekka Paalanen6551c092021-05-03 16:09:45 +03006400 /*
6401 * Use view_list in case the output did not go through repaint
6402 * after a view came on it, lacking a paint node. Just to be sure.
6403 */
Pekka Paalanenbccda712017-03-29 16:16:04 +03006404 wl_list_for_each(view, &compositor->view_list, link) {
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006405 if (view->output_mask & (1u << output->id))
6406 weston_view_assign_output(view);
6407 }
6408
Pekka Paalanen1d2eee22021-02-25 12:03:28 +02006409 weston_output_reset_color_transforms(output);
6410
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006411 weston_presentation_feedback_discard_list(&output->feedback_list);
6412
Pekka Paalanen9711fd92018-06-21 14:26:18 +03006413 weston_compositor_reflow_outputs(compositor, output, -output->width);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006414
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006415 wl_list_remove(&output->link);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006416 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006417 output->enabled = false;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006418
Marius Vlad2015dfc2021-12-02 11:15:15 +02006419 weston_signal_emit_mutable(&compositor->output_destroyed_signal, output);
6420 weston_signal_emit_mutable(&output->destroy_signal, output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006421
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02006422 wl_list_for_each(head, &output->head_list, output_link)
6423 weston_head_remove_global(head);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03006424
6425 compositor->output_id_pool &= ~(1u << output->id);
6426 output->id = 0xffffffff; /* invalid */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006427}
6428
6429/** Sets the output scale for a given output.
6430 *
6431 * \param output The weston_output object that the scale is set for.
6432 * \param scale Scale factor for the given output.
6433 *
6434 * It only supports setting scale for an output that
6435 * is not enabled and it can only be ran once.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006436 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05006437 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006438 */
6439WL_EXPORT void
6440weston_output_set_scale(struct weston_output *output,
6441 int32_t scale)
6442{
6443 /* We can only set scale on a disabled output */
6444 assert(!output->enabled);
6445
6446 /* We only want to set scale once */
6447 assert(!output->scale);
6448
6449 output->scale = scale;
6450}
6451
6452/** Sets the output transform for a given output.
6453 *
6454 * \param output The weston_output object that the transform is set for.
6455 * \param transform Transform value for the given output.
6456 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006457 * Refer to wl_output::transform section located at
6458 * https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_output
6459 * for list of values that can be passed to this function.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006460 *
Marius Vlad55d87362019-06-11 01:15:35 +03006461 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006462 */
6463WL_EXPORT void
6464weston_output_set_transform(struct weston_output *output,
6465 uint32_t transform)
6466{
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006467 struct weston_pointer_motion_event ev;
6468 struct wl_resource *resource;
6469 struct weston_seat *seat;
6470 pixman_region32_t old_region;
6471 int mid_x, mid_y;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006472 struct weston_head *head;
6473 int ver;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006474
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006475 if (!output->enabled && output->transform == UINT32_MAX) {
6476 output->transform = transform;
6477 return;
6478 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006479
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006480 weston_output_transform_scale_init(output, transform, output->scale);
6481
6482 pixman_region32_init(&old_region);
6483 pixman_region32_copy(&old_region, &output->region);
6484
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006485 weston_output_init_geometry(output, output->x, output->y);
6486
6487 output->dirty = 1;
6488
6489 /* Notify clients of the change for output transform. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006490 wl_list_for_each(head, &output->head_list, output_link) {
6491 wl_resource_for_each(resource, &head->resource_list) {
6492 wl_output_send_geometry(resource,
6493 output->x,
6494 output->y,
6495 head->mm_width,
6496 head->mm_height,
6497 head->subpixel,
6498 head->make,
6499 head->model,
6500 output->transform);
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006501
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006502 ver = wl_resource_get_version(resource);
6503 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
6504 wl_output_send_done(resource);
6505 }
Roman Gilge97391c2019-03-29 13:01:06 +01006506 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
6507 zxdg_output_v1_send_logical_position(resource,
6508 output->x,
6509 output->y);
6510 zxdg_output_v1_send_logical_size(resource,
6511 output->width,
6512 output->height);
6513 zxdg_output_v1_send_done(resource);
6514 }
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006515 }
6516
6517 /* we must ensure that pointers are inside output, otherwise they disappear */
6518 mid_x = output->x + output->width / 2;
6519 mid_y = output->y + output->height / 2;
6520
6521 ev.mask = WESTON_POINTER_MOTION_ABS;
6522 ev.x = wl_fixed_to_double(wl_fixed_from_int(mid_x));
6523 ev.y = wl_fixed_to_double(wl_fixed_from_int(mid_y));
6524
6525 wl_list_for_each(seat, &output->compositor->seat_list, link) {
6526 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
6527
6528 if (pointer && pixman_region32_contains_point(&old_region,
6529 wl_fixed_to_int(pointer->x),
6530 wl_fixed_to_int(pointer->y),
6531 NULL))
6532 weston_pointer_move(pointer, &ev);
6533 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006534}
6535
Pekka Paalanen9a9e6ce2021-06-08 14:17:44 +03006536/** Set output's color profile
6537 *
6538 * \param output The output to change.
6539 * \param cprof The color profile to set. Can be NULL for default sRGB profile.
6540 * \return True on success, or false on failure.
6541 *
6542 * Calling this function changes the color profile of the output. This causes
6543 * all existing weston_color_transform objects related to this output via
6544 * paint nodes to be unreferenced and later re-created on demand.
6545 *
6546 * This function may not be called from within weston_output_repaint().
6547 *
6548 * On failure, nothing is changed.
6549 *
6550 * \ingroup output
6551 */
6552WL_EXPORT bool
6553weston_output_set_color_profile(struct weston_output *output,
6554 struct weston_color_profile *cprof)
6555{
6556 struct weston_color_profile *old;
6557 struct weston_paint_node *pnode;
6558
6559 old = output->color_profile;
6560 output->color_profile = weston_color_profile_ref(cprof);
6561
6562 if (output->enabled) {
6563 if (!weston_output_set_color_transforms(output)) {
6564 /* Failed, roll back */
6565 weston_color_profile_unref(output->color_profile);
6566 output->color_profile = old;
6567 return false;
6568 }
6569
6570 /* Remove outdated cached color transformations */
6571 wl_list_for_each(pnode, &output->paint_node_list, output_link) {
6572 weston_surface_color_transform_fini(&pnode->surf_xform);
6573 pnode->surf_xform_valid = false;
6574 }
6575 }
6576
6577 weston_color_profile_unref(old);
6578
6579 return true;
6580}
6581
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006582/** Initializes a weston_output object with enough data so
6583 ** an output can be configured.
6584 *
6585 * \param output The weston_output object to initialize
6586 * \param compositor The compositor instance.
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006587 * \param name Name for the output (the string is copied).
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006588 *
6589 * Sets initial values for fields that are expected to be
6590 * configured either by compositors or backends.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006591 *
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006592 * The name is used in logs, and can be used by compositors as a configuration
6593 * identifier.
6594 *
Marius Vlad55d87362019-06-11 01:15:35 +03006595 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006596 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006597 */
6598WL_EXPORT void
Armin Krezović40087402016-09-30 14:11:12 +02006599weston_output_init(struct weston_output *output,
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006600 struct weston_compositor *compositor,
6601 const char *name)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006602{
6603 output->compositor = compositor;
6604 output->destroying = 0;
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006605 output->name = strdup(name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006606 wl_list_init(&output->link);
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006607 wl_signal_init(&output->user_destroy_signal);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006608 output->enabled = false;
Ankit Nautiyal2690a772019-03-25 17:57:59 +05306609 output->desired_protection = WESTON_HDCP_DISABLE;
Ankit Nautiyal2844f8e2019-04-03 10:14:59 +05306610 output->allow_protection = true;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006611
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006612 wl_list_init(&output->head_list);
6613
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006614 /* Add some (in)sane defaults which can be used
6615 * for checking if an output was properly configured
6616 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006617 output->scale = 0;
6618 /* Can't use -1 on uint32_t and 0 is valid enum value */
6619 output->transform = UINT32_MAX;
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006620
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006621 pixman_region32_init(&output->region);
Pekka Paalanen42704142017-09-06 16:47:52 +03006622 wl_list_init(&output->mode_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006623}
6624
6625/** Adds weston_output object to pending output list.
6626 *
6627 * \param output The weston_output object to add
6628 * \param compositor The compositor instance.
6629 *
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03006630 * The opposite of this operation is built into weston_output_release().
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006631 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006632 * \ingroup compositor
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006633 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006634 */
6635WL_EXPORT void
6636weston_compositor_add_pending_output(struct weston_output *output,
6637 struct weston_compositor *compositor)
6638{
Pekka Paalanene952a012017-03-29 17:14:00 +03006639 assert(output->disable);
6640 assert(output->enable);
6641
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006642 wl_list_remove(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006643 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006644}
6645
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006646/** Create a string with the attached heads' names.
6647 *
6648 * The string must be free()'d.
Marius Vlad55d87362019-06-11 01:15:35 +03006649 *
6650 * \ingroup output
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006651 */
6652static char *
6653weston_output_create_heads_string(struct weston_output *output)
6654{
6655 FILE *fp;
6656 char *str = NULL;
6657 size_t size = 0;
6658 struct weston_head *head;
6659 const char *sep = "";
6660
6661 fp = open_memstream(&str, &size);
6662 if (!fp)
6663 return NULL;
6664
6665 wl_list_for_each(head, &output->head_list, output_link) {
6666 fprintf(fp, "%s%s", sep, head->name);
6667 sep = ", ";
6668 }
6669 fclose(fp);
6670
6671 return str;
6672}
6673
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006674/** Constructs a weston_output object that can be used by the compositor.
6675 *
Pekka Paalanencc201e42017-03-30 15:11:25 +03006676 * \param output The weston_output object that needs to be enabled. Must not
Pekka Paalanenddce54d2017-08-23 16:00:21 +03006677 * be enabled already. Must have at least one head attached.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006678 *
6679 * Output coordinates are calculated and each new output is by default
6680 * assigned to the right of previous one.
6681 *
6682 * Sets up the transformation, zoom, and geometry of the output using
6683 * the properties that need to be configured by the compositor.
6684 *
6685 * Establishes a repaint timer for the output with the relevant display
6686 * object's event loop. See output_repaint_timer_handler().
6687 *
6688 * The output is assigned an ID. Weston can support up to 32 distinct
6689 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
6690 * is referred to and used to find the first available ID number, and
6691 * then this ID is marked as used in output_id_pool.
6692 *
6693 * The output is also assigned a Wayland global with the wl_output
6694 * external interface.
6695 *
6696 * Backend specific function is called to set up the output output.
6697 *
6698 * Output is added to the compositor's output list
6699 *
6700 * If the backend specific function fails, the weston_output object
6701 * is returned to a state it was before calling this function and
6702 * is added to the compositor's pending_output_list in case it needs
6703 * to be reconfigured or just so it can be destroyed at shutdown.
6704 *
6705 * 0 is returned on success, -1 on failure.
Marius Vlad55d87362019-06-11 01:15:35 +03006706 *
6707 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006708 */
6709WL_EXPORT int
6710weston_output_enable(struct weston_output *output)
6711{
Armin Krezović782f5df2016-09-30 14:11:11 +02006712 struct weston_compositor *c = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006713 struct weston_output *iterator;
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03006714 struct weston_head *head;
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006715 char *head_names;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006716 int x = 0, y = 0;
6717
Pekka Paalanencc201e42017-03-30 15:11:25 +03006718 if (output->enabled) {
6719 weston_log("Error: attempt to enable an enabled output '%s'\n",
6720 output->name);
6721 return -1;
6722 }
6723
Pekka Paalanenddce54d2017-08-23 16:00:21 +03006724 if (wl_list_empty(&output->head_list)) {
6725 weston_log("Error: cannot enable output '%s' without heads.\n",
6726 output->name);
6727 return -1;
6728 }
6729
Pekka Paalanen586e1ac2017-09-14 16:17:59 +03006730 if (wl_list_empty(&output->mode_list) || !output->current_mode) {
6731 weston_log("Error: no video mode for output '%s'.\n",
6732 output->name);
6733 return -1;
6734 }
6735
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03006736 wl_list_for_each(head, &output->head_list, output_link) {
6737 assert(head->make);
6738 assert(head->model);
6739 }
6740
Armin Krezović782f5df2016-09-30 14:11:11 +02006741 iterator = container_of(c->output_list.prev,
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006742 struct weston_output, link);
6743
Armin Krezović782f5df2016-09-30 14:11:11 +02006744 if (!wl_list_empty(&c->output_list))
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006745 x = iterator->x + iterator->width;
6746
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006747 /* Make sure the scale is set up */
6748 assert(output->scale);
6749
6750 /* Make sure we have a transform set */
6751 assert(output->transform != UINT32_MAX);
6752
Armin Krezović782f5df2016-09-30 14:11:11 +02006753 output->x = x;
6754 output->y = y;
6755 output->dirty = 1;
6756 output->original_scale = output->scale;
6757
Marius Vlad3a2f8292019-11-04 17:53:46 +02006758 wl_signal_init(&output->frame_signal);
6759 wl_signal_init(&output->destroy_signal);
6760
Armin Krezović782f5df2016-09-30 14:11:11 +02006761 weston_output_transform_scale_init(output, output->transform, output->scale);
6762 weston_output_init_zoom(output);
6763
6764 weston_output_init_geometry(output, x, y);
6765 weston_output_damage(output);
6766
Armin Krezović782f5df2016-09-30 14:11:11 +02006767 wl_list_init(&output->animation_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02006768 wl_list_init(&output->feedback_list);
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03006769 wl_list_init(&output->paint_node_list);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03006770 wl_list_init(&output->paint_node_z_order_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02006771
Pekka Paalanen9a0aa372021-06-08 13:38:35 +03006772 if (!weston_output_set_color_transforms(output))
Pekka Paalanen1d2eee22021-02-25 12:03:28 +02006773 return -1;
Pekka Paalanen1d2eee22021-02-25 12:03:28 +02006774
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006775 /* Enable the output (set up the crtc or create a
6776 * window representing the output, set up the
6777 * renderer, etc)
6778 */
6779 if (output->enable(output) < 0) {
6780 weston_log("Enabling output \"%s\" failed.\n", output->name);
Pekka Paalanen1d2eee22021-02-25 12:03:28 +02006781 weston_output_reset_color_transforms(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006782 return -1;
6783 }
6784
6785 weston_compositor_add_output(output->compositor, output);
6786
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006787 head_names = weston_output_create_heads_string(output);
6788 weston_log("Output '%s' enabled with head(s) %s\n",
6789 output->name, head_names);
6790 free(head_names);
6791
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006792 return 0;
6793}
6794
6795/** Converts a weston_output object to a pending output state, so it
6796 ** can be configured again or destroyed.
6797 *
6798 * \param output The weston_output object that needs to be disabled.
6799 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006800 * Calls a backend specific function to disable an output, in case
6801 * such function exists.
6802 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03006803 * The backend specific disable function may choose to postpone the disabling
6804 * by returning a negative value, in which case this function returns early.
6805 * In that case the backend will guarantee the output will be disabled soon
6806 * by the backend calling this function again. One must not attempt to re-enable
6807 * the output until that happens.
6808 *
6809 * Otherwise, if the output is being used by the compositor, it is removed
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006810 * from weston's output_list (see weston_compositor_remove_output())
6811 * and is returned to a state it was before weston_output_enable()
6812 * was ran (see weston_output_enable_undo()).
6813 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03006814 * See weston_output_init() for more information on the
6815 * state output is returned to.
Pekka Paalanencc201e42017-03-30 15:11:25 +03006816 *
6817 * If the output has never been enabled yet, this function can still be
6818 * called to ensure that the output is actually turned off rather than left
6819 * in the state it was discovered in.
Marius Vlad55d87362019-06-11 01:15:35 +03006820 *
6821 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006822 */
6823WL_EXPORT void
6824weston_output_disable(struct weston_output *output)
6825{
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006826 /* Should we rename this? */
6827 output->destroying = 1;
6828
Pekka Paalanenc65df642017-03-29 15:45:46 +03006829 /* Disable is called unconditionally also for not-enabled outputs,
6830 * because at compositor start-up, if there is an output that is
6831 * already on but the compositor wants to turn it off, we have to
6832 * forward the turn-off to the backend so it knows to do it.
6833 * The backend cannot initially turn off everything, because it
6834 * would cause unnecessary mode-sets for all outputs the compositor
6835 * wants to be on.
6836 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006837 if (output->disable(output) < 0)
6838 return;
6839
Pekka Paalanen2ec15eb2021-06-09 16:14:45 +03006840 if (output->enabled) {
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006841 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006842
Pekka Paalanen2ec15eb2021-06-09 16:14:45 +03006843 assert(wl_list_empty(&output->paint_node_list));
6844 }
6845
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006846 output->destroying = 0;
6847}
6848
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006849/** Forces a synchronous call to heads_changed hook
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006850 *
6851 * \param compositor The compositor instance
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006852 *
6853 * If there are new or changed heads, calls the heads_changed hook and
6854 * returns after the hook returns.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006855 *
6856 * \ingroup compositor
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006857 */
6858WL_EXPORT void
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006859weston_compositor_flush_heads_changed(struct weston_compositor *compositor)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006860{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03006861 if (compositor->heads_changed_source) {
6862 wl_event_source_remove(compositor->heads_changed_source);
6863 weston_compositor_call_heads_changed(compositor);
6864 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006865}
6866
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006867/** Add destroy callback for an output
6868 *
6869 * \param output The output to watch.
6870 * \param listener The listener to add. The \c notify member must be set.
6871 *
6872 * The listener callback will be called when user destroys an output. This
6873 * may be delayed by a backend in some cases. The main purpose of the
6874 * listener is to allow hooking up custom data to the output. The custom data
6875 * can be fetched via weston_output_get_destroy_listener() followed by
6876 * container_of().
6877 *
6878 * The \c data argument to the notify callback is the weston_output being
6879 * destroyed.
6880 *
6881 * @note This is for the final destruction of an output, not when it gets
6882 * disabled. If you want to keep track of enabled outputs, this is not it.
Marius Vlad55d87362019-06-11 01:15:35 +03006883 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05006884 * \ingroup output
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006885 */
6886WL_EXPORT void
6887weston_output_add_destroy_listener(struct weston_output *output,
6888 struct wl_listener *listener)
6889{
6890 wl_signal_add(&output->user_destroy_signal, listener);
6891}
6892
6893/** Look up destroy listener for an output
6894 *
6895 * \param output The output to query.
6896 * \param notify The notify function used used for the added destroy listener.
6897 * \return The listener, or NULL if not found.
6898 *
6899 * This looks up the previously added destroy listener struct based on the
6900 * notify function it has. The listener can be used to access user data
6901 * through \c container_of().
6902 *
6903 * \sa wl_signal_get() weston_output_add_destroy_listener()
Marius Vlad55d87362019-06-11 01:15:35 +03006904 * \ingroup output
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006905 */
6906WL_EXPORT struct wl_listener *
6907weston_output_get_destroy_listener(struct weston_output *output,
6908 wl_notify_func_t notify)
6909{
6910 return wl_signal_get(&output->user_destroy_signal, notify);
6911}
6912
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006913/** Uninitialize an output
6914 *
6915 * Removes the output from the list of enabled outputs if necessary, but
6916 * does not call the backend's output disable function. The output will no
6917 * longer be in the list of pending outputs either.
6918 *
6919 * All fields of weston_output become uninitialized, i.e. should not be used
6920 * anymore. The caller can free the memory after this.
6921 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05006922 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006923 * \internal
6924 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006925WL_EXPORT void
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03006926weston_output_release(struct weston_output *output)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006927{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006928 struct weston_head *head, *tmp;
6929
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006930 output->destroying = 1;
6931
Marius Vladf2452d62021-12-09 14:07:21 +02006932 weston_signal_emit_mutable(&output->user_destroy_signal, output);
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006933
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03006934 if (output->idle_repaint_source)
6935 wl_event_source_remove(output->idle_repaint_source);
6936
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006937 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006938 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006939
Pekka Paalanen9a9e6ce2021-06-08 14:17:44 +03006940 weston_color_profile_unref(output->color_profile);
6941
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006942 pixman_region32_fini(&output->region);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006943 wl_list_remove(&output->link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006944
6945 wl_list_for_each_safe(head, tmp, &output->head_list, output_link)
6946 weston_head_detach(head);
6947
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006948 free(output->name);
6949}
6950
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006951/** Find an output by its given name
6952 *
6953 * \param compositor The compositor to search in.
6954 * \param name The output name to search for.
6955 * \return An existing output with the given name, or NULL if not found.
6956 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006957 * \ingroup compositor
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006958 */
6959WL_EXPORT struct weston_output *
6960weston_compositor_find_output_by_name(struct weston_compositor *compositor,
6961 const char *name)
6962{
6963 struct weston_output *output;
6964
6965 wl_list_for_each(output, &compositor->output_list, link)
6966 if (strcmp(output->name, name) == 0)
6967 return output;
6968
6969 wl_list_for_each(output, &compositor->pending_output_list, link)
6970 if (strcmp(output->name, name) == 0)
6971 return output;
6972
6973 return NULL;
6974}
6975
6976/** Create a named output
6977 *
6978 * \param compositor The compositor.
6979 * \param name The name for the output.
6980 * \return A new \c weston_output, or NULL on failure.
6981 *
6982 * This creates a new weston_output that starts with no heads attached.
6983 *
6984 * An output must be configured and it must have at least one head before
6985 * it can be enabled.
6986 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006987 * \ingroup compositor
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006988 */
6989WL_EXPORT struct weston_output *
6990weston_compositor_create_output(struct weston_compositor *compositor,
6991 const char *name)
6992{
6993 assert(compositor->backend->create_output);
6994
6995 if (weston_compositor_find_output_by_name(compositor, name)) {
6996 weston_log("Warning: attempted to create an output with a "
6997 "duplicate name '%s'.\n", name);
6998 return NULL;
6999 }
7000
7001 return compositor->backend->create_output(compositor, name);
7002}
7003
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03007004/** Create an output for an unused head
7005 *
7006 * \param compositor The compositor.
7007 * \param head The head to attach to the output.
7008 * \return A new \c weston_output, or NULL on failure.
7009 *
7010 * This creates a new weston_output that starts with the given head attached.
7011 * The output inherits the name of the head. The head must not be already
7012 * attached to another output.
7013 *
7014 * An output must be configured before it can be enabled.
7015 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007016 * \ingroup compositor
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03007017 */
7018WL_EXPORT struct weston_output *
7019weston_compositor_create_output_with_head(struct weston_compositor *compositor,
7020 struct weston_head *head)
7021{
7022 struct weston_output *output;
7023
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02007024 output = weston_compositor_create_output(compositor, head->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03007025 if (!output)
7026 return NULL;
7027
7028 if (weston_output_attach_head(output, head) < 0) {
Pekka Paalanen42c0e142017-10-27 12:07:49 +03007029 weston_output_destroy(output);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03007030 return NULL;
7031 }
7032
7033 return output;
7034}
7035
7036/** Destroy an output
7037 *
7038 * \param output The output to destroy.
7039 *
7040 * The heads attached to the given output are detached and become unused again.
7041 *
7042 * It is not necessary to explicitly destroy all outputs at compositor exit.
Leandro Ribeiroca640d52020-01-27 19:12:01 -03007043 * weston_compositor_destroy() will automatically destroy any remaining
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03007044 * outputs.
7045 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05007046 * \ingroup output
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03007047 */
7048WL_EXPORT void
7049weston_output_destroy(struct weston_output *output)
7050{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03007051 output->destroy(output);
7052}
7053
Pekka Paalanencf0a4762017-04-04 16:36:07 +03007054/** When you need a head...
7055 *
7056 * This function is a hack, used until all code has been converted to become
7057 * multi-head aware.
7058 *
7059 * \param output The weston_output whose head to get.
7060 * \return The first head in the output's list.
Marius Vlad55d87362019-06-11 01:15:35 +03007061 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05007062 * \ingroup output
Pekka Paalanencf0a4762017-04-04 16:36:07 +03007063 */
7064WL_EXPORT struct weston_head *
7065weston_output_get_first_head(struct weston_output *output)
7066{
7067 if (wl_list_empty(&output->head_list))
7068 return NULL;
7069
7070 return container_of(output->head_list.next,
7071 struct weston_head, output_link);
7072}
7073
Ankit Nautiyal2844f8e2019-04-03 10:14:59 +05307074/** Allow/Disallow content-protection support for an output
7075 *
7076 * This function sets the allow_protection member for an output. Setting of
7077 * this field will allow the compositor to attempt content-protection for this
7078 * output, for a backend that supports the content-protection protocol.
7079 *
7080 * \param output The weston_output for whom the content-protection is to be
7081 * allowed.
7082 * \param allow_protection The bool value which is to be set.
7083 */
7084WL_EXPORT void
7085weston_output_allow_protection(struct weston_output *output,
7086 bool allow_protection)
7087{
7088 output->allow_protection = allow_protection;
7089}
7090
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01007091static void
Roman Gilge97391c2019-03-29 13:01:06 +01007092xdg_output_unlist(struct wl_resource *resource)
7093{
7094 wl_list_remove(wl_resource_get_link(resource));
7095}
7096
7097static void
7098xdg_output_destroy(struct wl_client *client, struct wl_resource *resource)
7099{
7100 wl_resource_destroy(resource);
7101}
7102
7103static const struct zxdg_output_v1_interface xdg_output_interface = {
7104 xdg_output_destroy
7105};
7106
7107static void
7108xdg_output_manager_destroy(struct wl_client *client,
7109 struct wl_resource *resource)
7110{
7111 wl_resource_destroy(resource);
7112}
7113
7114static void
7115xdg_output_manager_get_xdg_output(struct wl_client *client,
7116 struct wl_resource *manager,
7117 uint32_t id,
7118 struct wl_resource *output_resource)
7119{
7120 int version = wl_resource_get_version(manager);
7121 struct weston_head *head = wl_resource_get_user_data(output_resource);
7122 struct weston_output *output = head->output;
7123 struct wl_resource *resource;
7124
7125 resource = wl_resource_create(client, &zxdg_output_v1_interface,
7126 version, id);
7127 if (resource == NULL) {
7128 wl_client_post_no_memory(client);
7129 return;
7130 }
7131
7132 wl_list_insert(&head->xdg_output_resource_list,
7133 wl_resource_get_link(resource));
7134
7135 wl_resource_set_implementation(resource, &xdg_output_interface,
7136 NULL, xdg_output_unlist);
7137
7138 zxdg_output_v1_send_logical_position(resource, output->x, output->y);
7139 zxdg_output_v1_send_logical_size(resource,
7140 output->width,
7141 output->height);
7142 if (version >= ZXDG_OUTPUT_V1_NAME_SINCE_VERSION)
7143 zxdg_output_v1_send_name(resource, head->name);
7144
7145 zxdg_output_v1_send_done(resource);
7146}
7147
7148static const struct zxdg_output_manager_v1_interface xdg_output_manager_interface = {
7149 xdg_output_manager_destroy,
7150 xdg_output_manager_get_xdg_output
7151};
7152
7153static void
7154bind_xdg_output_manager(struct wl_client *client,
7155 void *data, uint32_t version, uint32_t id)
7156{
7157 struct wl_resource *resource;
7158
7159 resource = wl_resource_create(client, &zxdg_output_manager_v1_interface,
7160 version, id);
7161 if (resource == NULL) {
7162 wl_client_post_no_memory(client);
7163 return;
7164 }
7165
7166 wl_resource_set_implementation(resource, &xdg_output_manager_interface,
7167 NULL, NULL);
7168}
7169
7170static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007171destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01007172{
Jonny Lamb74130762013-11-26 18:19:46 +01007173 struct weston_surface *surface =
7174 wl_resource_get_user_data(resource);
7175
Pekka Paalanen4826f872016-04-22 14:14:38 +03007176 if (!surface)
7177 return;
7178
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007179 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02007180 surface->pending.buffer_viewport.buffer.src_width =
7181 wl_fixed_from_int(-1);
7182 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02007183 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01007184}
7185
7186static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007187viewport_destroy(struct wl_client *client,
7188 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01007189{
7190 wl_resource_destroy(resource);
7191}
7192
7193static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007194viewport_set_source(struct wl_client *client,
7195 struct wl_resource *resource,
7196 wl_fixed_t src_x,
7197 wl_fixed_t src_y,
7198 wl_fixed_t src_width,
7199 wl_fixed_t src_height)
7200{
7201 struct weston_surface *surface =
7202 wl_resource_get_user_data(resource);
7203
Pekka Paalanen4826f872016-04-22 14:14:38 +03007204 if (!surface) {
7205 wl_resource_post_error(resource,
7206 WP_VIEWPORT_ERROR_NO_SURFACE,
7207 "wl_surface for this viewport is no longer exists");
7208 return;
7209 }
7210
7211 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03007212 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007213
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007214 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03007215 src_height == wl_fixed_from_int(-1) &&
7216 src_x == wl_fixed_from_int(-1) &&
7217 src_y == wl_fixed_from_int(-1)) {
7218 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007219 surface->pending.buffer_viewport.buffer.src_width =
7220 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02007221 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007222 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007223 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007224
Pekka Paalanen201769a2016-04-26 14:42:11 +03007225 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007226 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007227 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03007228 "wl_surface@%d viewport source "
7229 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
7230 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007231 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03007232 wl_fixed_to_double(src_height),
7233 wl_fixed_to_double(src_x),
7234 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007235 return;
7236 }
7237
7238 surface->pending.buffer_viewport.buffer.src_x = src_x;
7239 surface->pending.buffer_viewport.buffer.src_y = src_y;
7240 surface->pending.buffer_viewport.buffer.src_width = src_width;
7241 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02007242 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007243}
7244
7245static void
7246viewport_set_destination(struct wl_client *client,
7247 struct wl_resource *resource,
7248 int32_t dst_width,
7249 int32_t dst_height)
7250{
7251 struct weston_surface *surface =
7252 wl_resource_get_user_data(resource);
7253
Pekka Paalanen4826f872016-04-22 14:14:38 +03007254 if (!surface) {
7255 wl_resource_post_error(resource,
7256 WP_VIEWPORT_ERROR_NO_SURFACE,
7257 "wl_surface for this viewport no longer exists");
7258 return;
7259 }
7260
7261 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007262
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007263 if (dst_width == -1 && dst_height == -1) {
7264 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007265 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02007266 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007267 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007268 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007269
7270 if (dst_width <= 0 || dst_height <= 0) {
7271 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007272 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007273 "destination size must be positive (%dx%d)",
7274 dst_width, dst_height);
7275 return;
7276 }
7277
7278 surface->pending.buffer_viewport.surface.width = dst_width;
7279 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02007280 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007281}
7282
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007283static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007284 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007285 viewport_set_source,
7286 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01007287};
7288
7289static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007290viewporter_destroy(struct wl_client *client,
7291 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01007292{
7293 wl_resource_destroy(resource);
7294}
7295
7296static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007297viewporter_get_viewport(struct wl_client *client,
7298 struct wl_resource *viewporter,
7299 uint32_t id,
7300 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01007301{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007302 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007303 struct weston_surface *surface =
7304 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01007305 struct wl_resource *resource;
7306
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007307 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007308 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007309 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007310 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01007311 return;
7312 }
7313
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007314 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007315 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01007316 if (resource == NULL) {
7317 wl_client_post_no_memory(client);
7318 return;
7319 }
7320
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007321 wl_resource_set_implementation(resource, &viewport_interface,
7322 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01007323
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007324 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01007325}
7326
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007327static const struct wp_viewporter_interface viewporter_interface = {
7328 viewporter_destroy,
7329 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01007330};
7331
7332static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007333bind_viewporter(struct wl_client *client,
7334 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01007335{
7336 struct wl_resource *resource;
7337
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007338 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06007339 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01007340 if (resource == NULL) {
7341 wl_client_post_no_memory(client);
7342 return;
7343 }
7344
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007345 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01007346 NULL, NULL);
7347}
7348
7349static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04007350destroy_presentation_feedback(struct wl_resource *feedback_resource)
7351{
7352 struct weston_presentation_feedback *feedback;
7353
7354 feedback = wl_resource_get_user_data(feedback_resource);
7355
7356 wl_list_remove(&feedback->link);
7357 free(feedback);
7358}
7359
7360static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007361presentation_destroy(struct wl_client *client, struct wl_resource *resource)
7362{
7363 wl_resource_destroy(resource);
7364}
7365
7366static void
7367presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04007368 struct wl_resource *presentation_resource,
7369 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007370 uint32_t callback)
7371{
Pekka Paalanen133e4392014-09-23 22:08:46 -04007372 struct weston_surface *surface;
7373 struct weston_presentation_feedback *feedback;
7374
7375 surface = wl_resource_get_user_data(surface_resource);
7376
Bryce Harringtonde16d892014-11-20 22:21:57 -08007377 feedback = zalloc(sizeof *feedback);
7378 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04007379 goto err_calloc;
7380
7381 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007382 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04007383 1, callback);
7384 if (!feedback->resource)
7385 goto err_create;
7386
7387 wl_resource_set_implementation(feedback->resource, NULL, feedback,
7388 destroy_presentation_feedback);
7389
7390 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
7391
7392 return;
7393
7394err_create:
7395 free(feedback);
7396
7397err_calloc:
7398 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007399}
7400
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007401static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007402 presentation_destroy,
7403 presentation_feedback
7404};
7405
7406static void
7407bind_presentation(struct wl_client *client,
7408 void *data, uint32_t version, uint32_t id)
7409{
7410 struct weston_compositor *compositor = data;
7411 struct wl_resource *resource;
7412
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007413 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06007414 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007415 if (resource == NULL) {
7416 wl_client_post_no_memory(client);
7417 return;
7418 }
7419
7420 wl_resource_set_implementation(resource, &presentation_implementation,
7421 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007422 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007423}
7424
7425static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05007426compositor_bind(struct wl_client *client,
7427 void *data, uint32_t version, uint32_t id)
7428{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007429 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05007430 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05007431
Jason Ekstranda85118c2013-06-27 20:17:02 -05007432 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06007433 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07007434 if (resource == NULL) {
7435 wl_client_post_no_memory(client);
7436 return;
7437 }
7438
7439 wl_resource_set_implementation(resource, &compositor_interface,
7440 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05007441}
7442
Daniel Stonece62cb32018-07-20 09:46:24 +01007443static const char *
7444output_repaint_status_text(struct weston_output *output)
7445{
7446 switch (output->repaint_status) {
7447 case REPAINT_NOT_SCHEDULED:
7448 return "no repaint";
7449 case REPAINT_BEGIN_FROM_IDLE:
7450 return "start_repaint_loop scheduled";
7451 case REPAINT_SCHEDULED:
7452 return "repaint scheduled";
7453 case REPAINT_AWAITING_COMPLETION:
7454 return "awaiting completion";
7455 }
7456
7457 assert(!"output_repaint_status_text missing enum");
7458 return NULL;
7459}
7460
7461static void
7462debug_scene_view_print_buffer(FILE *fp, struct weston_view *view)
7463{
7464 struct weston_buffer *buffer = view->surface->buffer_ref.buffer;
7465 struct wl_shm_buffer *shm;
7466 struct linux_dmabuf_buffer *dmabuf;
Marius Vlad00a6e012018-11-20 17:52:31 +02007467 const struct pixel_format_info *pixel_info = NULL;
Daniel Stonece62cb32018-07-20 09:46:24 +01007468
7469 if (!buffer) {
7470 fprintf(fp, "\t\t[buffer not available]\n");
7471 return;
7472 }
7473
7474 shm = wl_shm_buffer_get(buffer->resource);
7475 if (shm) {
Marius Vlad00a6e012018-11-20 17:52:31 +02007476 uint32_t _format = wl_shm_buffer_get_format(shm);
7477 pixel_info = pixel_format_get_info_shm(_format);
Daniel Stonece62cb32018-07-20 09:46:24 +01007478 fprintf(fp, "\t\tSHM buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02007479 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
7480 (unsigned long) _format,
7481 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Daniel Stonece62cb32018-07-20 09:46:24 +01007482 return;
7483 }
7484
7485 dmabuf = linux_dmabuf_buffer_get(buffer->resource);
7486 if (dmabuf) {
Marius Vlad6f6fd262021-07-12 12:58:34 +03007487 uint64_t modifier = dmabuf->attributes.modifier[0];
7488 char *modifier_name = pixel_format_get_modifier(modifier);
Marius Vlad00a6e012018-11-20 17:52:31 +02007489 pixel_info = pixel_format_get_info(dmabuf->attributes.format);
Daniel Stonece62cb32018-07-20 09:46:24 +01007490 fprintf(fp, "\t\tdmabuf buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02007491 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
7492 (unsigned long) dmabuf->attributes.format,
7493 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Marius Vlad6f6fd262021-07-12 12:58:34 +03007494
7495 fprintf(fp, "\t\t\tmodifier: %s\n", modifier_name ? modifier_name :
7496 "Failed to convert to a modifier name");
7497 free(modifier_name);
Daniel Stonece62cb32018-07-20 09:46:24 +01007498 return;
7499 }
7500
Marius Vlad253ba9a2019-03-17 18:22:21 +02007501 fprintf(fp, "\t\tEGL buffer\n");
Daniel Stonece62cb32018-07-20 09:46:24 +01007502}
7503
7504static void
7505debug_scene_view_print(FILE *fp, struct weston_view *view, int view_idx)
7506{
7507 struct weston_compositor *ec = view->surface->compositor;
7508 struct weston_output *output;
7509 char desc[512];
7510 pixman_box32_t *box;
7511 uint32_t surface_id = 0;
7512 pid_t pid = 0;
7513
7514 if (view->surface->resource) {
7515 struct wl_resource *resource = view->surface->resource;
7516 wl_client_get_credentials(wl_resource_get_client(resource),
Dongjin Kimba89f002024-09-30 17:49:36 +09007517 &pid, NULL, NULL);
Daniel Stonece62cb32018-07-20 09:46:24 +01007518 surface_id = wl_resource_get_id(view->surface->resource);
7519 }
7520
7521 if (!view->surface->get_label ||
7522 view->surface->get_label(view->surface, desc, sizeof(desc)) < 0) {
7523 strcpy(desc, "[no description available]");
7524 }
7525 fprintf(fp, "\tView %d (role %s, PID %d, surface ID %u, %s, %p):\n",
7526 view_idx, view->surface->role_name, pid, surface_id,
7527 desc, view);
7528
7529 box = pixman_region32_extents(&view->transform.boundingbox);
7530 fprintf(fp, "\t\tposition: (%d, %d) -> (%d, %d)\n",
7531 box->x1, box->y1, box->x2, box->y2);
7532 box = pixman_region32_extents(&view->transform.opaque);
7533
Michael Olbrichb7e5f102020-08-11 16:33:35 +02007534 if (weston_view_is_opaque(view, &view->transform.boundingbox)) {
Daniel Stonece62cb32018-07-20 09:46:24 +01007535 fprintf(fp, "\t\t[fully opaque]\n");
7536 } else if (!pixman_region32_not_empty(&view->transform.opaque)) {
7537 fprintf(fp, "\t\t[not opaque]\n");
7538 } else {
7539 fprintf(fp, "\t\t[opaque: (%d, %d) -> (%d, %d)]\n",
7540 box->x1, box->y1, box->x2, box->y2);
7541 }
7542
7543 if (view->alpha < 1.0)
7544 fprintf(fp, "\t\talpha: %f\n", view->alpha);
7545
7546 if (view->output_mask != 0) {
7547 bool first_output = true;
7548 fprintf(fp, "\t\toutputs: ");
7549 wl_list_for_each(output, &ec->output_list, link) {
7550 if (!(view->output_mask & (1 << output->id)))
7551 continue;
7552 fprintf(fp, "%s%d (%s)%s",
7553 (first_output) ? "" : ", ",
7554 output->id, output->name,
7555 (view->output == output) ? " (primary)" : "");
7556 first_output = false;
7557 }
7558 } else {
7559 fprintf(fp, "\t\t[no outputs]");
7560 }
7561
7562 fprintf(fp, "\n");
7563
7564 debug_scene_view_print_buffer(fp, view);
7565}
7566
Marius Vlad433f4e72019-02-17 22:14:23 +02007567static void
7568debug_scene_view_print_tree(struct weston_view *view,
Marius Vlada6acfa82019-03-17 18:10:09 +02007569 FILE *fp, int *view_idx)
Marius Vlad433f4e72019-02-17 22:14:23 +02007570{
7571 struct weston_subsurface *sub;
7572 struct weston_view *ev;
7573
7574 /*
7575 * print the view first, then we recursively go on printing
7576 * sub-surfaces. We bail out once no more sub-surfaces are available.
7577 */
Marius Vlada6acfa82019-03-17 18:10:09 +02007578 debug_scene_view_print(fp, view, *view_idx);
Marius Vlad433f4e72019-02-17 22:14:23 +02007579
7580 /* no more sub-surfaces */
7581 if (wl_list_empty(&view->surface->subsurface_list))
7582 return;
7583
7584 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
7585 wl_list_for_each(ev, &sub->surface->views, surface_link) {
Michael Olbrichef5f3232020-04-29 09:03:15 +02007586 /* only print the child views of the current view */
7587 if (ev->parent_view != view)
Marius Vlad433f4e72019-02-17 22:14:23 +02007588 continue;
Marius Vlada6acfa82019-03-17 18:10:09 +02007589
7590 (*view_idx)++;
Marius Vlad433f4e72019-02-17 22:14:23 +02007591 debug_scene_view_print_tree(ev, fp, view_idx);
7592 }
7593 }
7594}
7595
Daniel Stonece62cb32018-07-20 09:46:24 +01007596/**
7597 * Output information on how libweston is currently composing the scene
7598 * graph.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007599 *
7600 * \ingroup compositor
Daniel Stonece62cb32018-07-20 09:46:24 +01007601 */
7602WL_EXPORT char *
7603weston_compositor_print_scene_graph(struct weston_compositor *ec)
7604{
7605 struct weston_output *output;
7606 struct weston_layer *layer;
7607 struct timespec now;
7608 int layer_idx = 0;
7609 FILE *fp;
7610 char *ret;
7611 size_t len;
7612 int err;
7613
7614 fp = open_memstream(&ret, &len);
7615 assert(fp);
7616
7617 weston_compositor_read_presentation_clock(ec, &now);
7618 fprintf(fp, "Weston scene graph at %ld.%09ld:\n\n",
7619 now.tv_sec, now.tv_nsec);
7620
7621 wl_list_for_each(output, &ec->output_list, link) {
7622 struct weston_head *head;
7623 int head_idx = 0;
7624
7625 fprintf(fp, "Output %d (%s):\n", output->id, output->name);
7626 assert(output->enabled);
7627
7628 fprintf(fp, "\tposition: (%d, %d) -> (%d, %d)\n",
7629 output->x, output->y,
7630 output->x + output->width,
7631 output->y + output->height);
7632 fprintf(fp, "\tmode: %dx%d@%.3fHz\n",
7633 output->current_mode->width,
7634 output->current_mode->height,
7635 output->current_mode->refresh / 1000.0);
7636 fprintf(fp, "\tscale: %d\n", output->scale);
7637
7638 fprintf(fp, "\trepaint status: %s\n",
7639 output_repaint_status_text(output));
7640 if (output->repaint_status == REPAINT_SCHEDULED)
7641 fprintf(fp, "\tnext repaint: %ld.%09ld\n",
7642 output->next_repaint.tv_sec,
7643 output->next_repaint.tv_nsec);
7644
7645 wl_list_for_each(head, &output->head_list, output_link) {
7646 fprintf(fp, "\tHead %d (%s): %sconnected\n",
7647 head_idx++, head->name,
7648 (head->connected) ? "" : "not ");
7649 }
7650 }
7651
7652 fprintf(fp, "\n");
7653
7654 wl_list_for_each(layer, &ec->layer_list, link) {
7655 struct weston_view *view;
7656 int view_idx = 0;
7657
7658 fprintf(fp, "Layer %d (pos 0x%lx):\n", layer_idx++,
7659 (unsigned long) layer->position);
7660
7661 if (!weston_layer_mask_is_infinite(layer)) {
7662 fprintf(fp, "\t[mask: (%d, %d) -> (%d,%d)]\n\n",
7663 layer->mask.x1, layer->mask.y1,
7664 layer->mask.x2, layer->mask.y2);
7665 }
7666
Marius Vlada6acfa82019-03-17 18:10:09 +02007667 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
7668 debug_scene_view_print_tree(view, fp, &view_idx);
7669 view_idx++;
7670 }
Daniel Stonece62cb32018-07-20 09:46:24 +01007671
7672 if (wl_list_empty(&layer->view_list.link))
7673 fprintf(fp, "\t[no views]\n");
7674
7675 fprintf(fp, "\n");
7676 }
7677
7678 err = fclose(fp);
7679 assert(err == 0);
7680
7681 return ret;
7682}
7683
7684/**
7685 * Called when the 'scene-graph' debug scope is bound by a client. This
7686 * one-shot weston-debug scope prints the current scene graph when bound,
7687 * and then terminates the stream.
7688 */
7689static void
Marius Vladdad882a2019-07-17 15:43:53 +03007690debug_scene_graph_cb(struct weston_log_subscription *sub, void *data)
Daniel Stonece62cb32018-07-20 09:46:24 +01007691{
7692 struct weston_compositor *ec = data;
7693 char *str = weston_compositor_print_scene_graph(ec);
7694
Marius Vladdad882a2019-07-17 15:43:53 +03007695 weston_log_subscription_printf(sub, "%s", str);
Daniel Stonece62cb32018-07-20 09:46:24 +01007696 free(str);
Marius Vladdad882a2019-07-17 15:43:53 +03007697 weston_log_subscription_complete(sub);
Daniel Stonece62cb32018-07-20 09:46:24 +01007698}
7699
Leandro Ribeiro32a5acd2020-10-19 16:06:22 -03007700/** Retrieve testsuite data from compositor
7701 *
7702 * The testsuite data can be defined by the test suite of projects that uses
7703 * libweston and given to the compositor at the moment of its creation. This
7704 * function should be used when we need to retrieve the testsuite private data
7705 * from the compositor.
7706 *
7707 * \param ec The weston compositor.
7708 * \return The testsuite data.
7709 *
7710 * \ingroup compositor
7711 * \sa weston_compositor_test_data_init
7712 */
7713WL_EXPORT void *
7714weston_compositor_get_test_data(struct weston_compositor *ec)
7715{
7716 return ec->test_data.test_private_data;
7717}
7718
Giulio Camuffo459137b2014-10-11 23:56:24 +03007719/** Create the compositor.
7720 *
7721 * This functions creates and initializes a compositor instance.
7722 *
7723 * \param display The Wayland display to be used.
7724 * \param user_data A pointer to an object that can later be retrieved
Marius Vlada2dace22019-06-12 16:05:44 +03007725 * \param log_ctx A pointer to weston_debug_compositor
Leandro Ribeirocdb72182020-11-06 11:24:22 -03007726 * \param test_data Optional testsuite data, or NULL.
Giulio Camuffo459137b2014-10-11 23:56:24 +03007727 * using the \ref weston_compositor_get_user_data function.
7728 * \return The compositor instance on success or NULL on failure.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007729 *
7730 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007731 */
7732WL_EXPORT struct weston_compositor *
Marius Vlad880b4852019-04-07 17:07:58 +03007733weston_compositor_create(struct wl_display *display,
Leandro Ribeirocdb72182020-11-06 11:24:22 -03007734 struct weston_log_context *log_ctx, void *user_data,
7735 const struct weston_testsuite_data *test_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04007736{
Giulio Camuffo459137b2014-10-11 23:56:24 +03007737 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05007738 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07007739
Leandro Ribeirobd9c0a62020-01-17 10:47:49 -03007740 if (!log_ctx)
7741 return NULL;
7742
Giulio Camuffo459137b2014-10-11 23:56:24 +03007743 ec = zalloc(sizeof *ec);
7744 if (!ec)
7745 return NULL;
7746
Leandro Ribeirocdb72182020-11-06 11:24:22 -03007747 if (test_data)
7748 ec->test_data = *test_data;
7749
Leandro Ribeirobd9c0a62020-01-17 10:47:49 -03007750 ec->weston_log_ctx = log_ctx;
Giulio Camuffo459137b2014-10-11 23:56:24 +03007751 ec->wl_display = display;
7752 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04007753 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07007754 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04007755 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03007756 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03007757 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02007758 wl_signal_init(&ec->idle_signal);
7759 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02007760 wl_signal_init(&ec->show_input_panel_signal);
7761 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02007762 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01007763 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01007764 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02007765 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02007766 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02007767 wl_signal_init(&ec->output_resized_signal);
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03007768 wl_signal_init(&ec->heads_changed_signal);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02007769 wl_signal_init(&ec->output_heads_changed_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07007770 wl_signal_init(&ec->session_signal);
Robert Beckettc569bdc2019-07-10 16:40:12 +01007771 ec->session_active = true;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04007772
Casey Dahlin58ba1372012-04-19 22:50:08 -04007773 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03007774 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04007775
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02007776 ec->activate_serial = 1;
7777
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05007778 ec->touch_mode = WESTON_TOUCH_MODE_NORMAL;
7779
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05307780 ec->content_protection = NULL;
7781
Derek Foreman152254b2015-11-26 14:17:48 -06007782 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04007783 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007784 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05007785
Giulio Camuffo954f1832014-10-11 18:27:30 +03007786 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04007787 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007788 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03007789
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007790 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007791 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007792 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01007793
Roman Gilge97391c2019-03-29 13:01:06 +01007794 if (!wl_global_create(ec->wl_display, &zxdg_output_manager_v1_interface, 2,
7795 ec, bind_xdg_output_manager))
7796 goto fail;
7797
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007798 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007799 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007800 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007801
Jonas Ådahl30d61d82014-10-22 21:21:17 +02007802 if (weston_input_init(ec) != 0)
7803 goto fail;
7804
Jason Ekstranda7af7042013-10-12 22:38:11 -05007805 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02007806 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007807 wl_list_init(&ec->layer_list);
7808 wl_list_init(&ec->seat_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007809 wl_list_init(&ec->pending_output_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007810 wl_list_init(&ec->output_list);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03007811 wl_list_init(&ec->head_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007812 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01007813 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007814 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01007815 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007816 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02007817 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007818
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007819 wl_list_init(&ec->plugin_api_list);
7820
Xiong Zhang97116532013-10-23 13:58:31 +08007821 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02007822 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04007823
Giulio Camuffo459137b2014-10-11 23:56:24 +03007824 wl_data_device_manager_init(ec->wl_display);
7825
7826 wl_display_init_shm(ec->wl_display);
7827
7828 loop = wl_display_get_event_loop(ec->wl_display);
7829 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Daniel Stone6847b852017-03-01 11:34:08 +00007830 ec->repaint_timer =
7831 wl_event_loop_add_timer(loop, output_repaint_timer_handler,
7832 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007833
Quentin Glidic82681572016-12-17 13:40:51 +01007834 weston_layer_init(&ec->fade_layer, ec);
7835 weston_layer_init(&ec->cursor_layer, ec);
7836
7837 weston_layer_set_position(&ec->fade_layer, WESTON_LAYER_POSITION_FADE);
7838 weston_layer_set_position(&ec->cursor_layer,
7839 WESTON_LAYER_POSITION_CURSOR);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007840
Daniel Stonece62cb32018-07-20 09:46:24 +01007841 ec->debug_scene =
Leandro Ribeiroce100192019-12-26 16:35:49 -03007842 weston_compositor_add_log_scope(ec, "scene-graph",
7843 "Scene graph details\n",
7844 debug_scene_graph_cb, NULL,
7845 ec);
Daniel Stonece62cb32018-07-20 09:46:24 +01007846
Marius Vladda104eb2019-09-05 14:31:01 +03007847 ec->timeline =
Leandro Ribeiroce100192019-12-26 16:35:49 -03007848 weston_compositor_add_log_scope(ec, "timeline",
7849 "Timeline event points\n",
7850 weston_timeline_create_subscription,
7851 weston_timeline_destroy_subscription,
7852 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007853 return ec;
7854
7855fail:
7856 free(ec);
7857 return NULL;
7858}
7859
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007860/** weston_compositor_shutdown
7861 * \ingroup compositor
7862 */
Benjamin Franzkeb8263022011-08-30 11:32:47 +02007863WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007864weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07007865{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007866 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07007867
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007868 wl_event_source_remove(ec->idle_source);
Lujin Wangd4b46cd2019-08-12 12:03:29 -07007869 wl_event_source_remove(ec->repaint_timer);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007870
Matt Roper361d2ad2011-08-29 13:52:23 -07007871 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02007872 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07007873 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02007874
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007875 /* Destroy all pending outputs associated with this compositor */
7876 wl_list_for_each_safe(output, next, &ec->pending_output_list, link)
7877 output->destroy(output);
7878
Pekka Paalanen90a5ffa2021-02-25 12:03:28 +02007879 /* Color manager objects may have renderer hooks */
7880 if (ec->color_manager) {
7881 ec->color_manager->destroy(ec->color_manager);
7882 ec->color_manager = NULL;
7883 }
7884
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02007885 if (ec->renderer)
7886 ec->renderer->destroy(ec);
7887
Daniel Stone325fc2d2012-05-30 16:31:58 +01007888 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09007889 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01007890 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01007891 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01007892 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02007893 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007894
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04007895 weston_plane_release(&ec->primary_plane);
Pekka Paalanen87400372021-05-14 14:29:40 +03007896
7897 weston_layer_fini(&ec->fade_layer);
7898 weston_layer_fini(&ec->cursor_layer);
7899
7900 if (!wl_list_empty(&ec->layer_list))
Dongjin Kimba89f002024-09-30 17:49:36 +09007901 weston_log("BUG: layer_list is not empty after shutdown. Calls to weston_layer_fini() are missing somewhere.\n");
Matt Roper361d2ad2011-08-29 13:52:23 -07007902}
7903
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007904/** weston_compositor_exit_with_code
7905 * \ingroup compositor
7906 */
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05007907WL_EXPORT void
Adam Jackson3c3f3b12019-10-16 16:02:59 -04007908weston_compositor_exit_with_code(struct weston_compositor *compositor,
Frederic Plourdec336f062014-10-29 14:44:33 -04007909 int exit_code)
7910{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02007911 if (compositor->exit_code == EXIT_SUCCESS)
7912 compositor->exit_code = exit_code;
7913
Giulio Camuffo459137b2014-10-11 23:56:24 +03007914 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04007915}
7916
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007917/** weston_compositor_set_default_pointer_grab
7918 * \ingroup compositor
7919 */
Frederic Plourdec336f062014-10-29 14:44:33 -04007920WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007921weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
7922 const struct weston_pointer_grab_interface *interface)
7923{
7924 struct weston_seat *seat;
7925
7926 ec->default_pointer_grab = interface;
7927 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05007928 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
7929
7930 if (pointer)
7931 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007932 }
7933}
7934
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007935/** weston_compositor_set_presentation_clock
7936 * \ingroup compositor
7937 */
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007938WL_EXPORT int
7939weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
7940 clockid_t clk_id)
7941{
7942 struct timespec ts;
7943
7944 if (clock_gettime(clk_id, &ts) < 0)
7945 return -1;
7946
7947 compositor->presentation_clock = clk_id;
7948
7949 return 0;
7950}
7951
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007952/** For choosing the software clock, when the display hardware or API
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007953 * does not expose a compatible presentation timestamp.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007954 *
7955 * \ingroup compositor
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007956 */
7957WL_EXPORT int
7958weston_compositor_set_presentation_clock_software(
7959 struct weston_compositor *compositor)
7960{
7961 /* In order of preference */
7962 static const clockid_t clocks[] = {
7963 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
7964 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
7965 CLOCK_MONOTONIC, /* no jumps, may crawl */
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007966 };
7967 unsigned i;
7968
7969 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
7970 if (weston_compositor_set_presentation_clock(compositor,
7971 clocks[i]) == 0)
7972 return 0;
7973
7974 weston_log("Error: no suitable presentation clock available.\n");
7975
7976 return -1;
7977}
7978
Pekka Paalanen662f3842015-03-18 12:17:26 +02007979/** Read the current time from the Presentation clock
7980 *
7981 * \param compositor
Marius Vlada2dace22019-06-12 16:05:44 +03007982 * \param[out] ts The current time.
Pekka Paalanen662f3842015-03-18 12:17:26 +02007983 *
7984 * \note Reading the current time in user space is always imprecise to some
7985 * degree.
7986 *
7987 * This function is never meant to fail. If reading the clock does fail,
7988 * an error message is logged and a zero time is returned. Callers are not
7989 * supposed to detect or react to failures.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007990 *
7991 * \ingroup compositor
Pekka Paalanen662f3842015-03-18 12:17:26 +02007992 */
7993WL_EXPORT void
7994weston_compositor_read_presentation_clock(
7995 const struct weston_compositor *compositor,
7996 struct timespec *ts)
7997{
7998 static bool warned;
7999 int ret;
8000
8001 ret = clock_gettime(compositor->presentation_clock, ts);
8002 if (ret < 0) {
8003 ts->tv_sec = 0;
8004 ts->tv_nsec = 0;
8005
8006 if (!warned)
8007 weston_log("Error: failure to read "
Antonio Borneo39578632019-04-26 23:57:31 +02008008 "the presentation clock %#x: '%s' (%d)\n",
8009 compositor->presentation_clock,
8010 strerror(errno), errno);
Pekka Paalanen662f3842015-03-18 12:17:26 +02008011 warned = true;
8012 }
8013}
8014
Pekka Paalanen230f3b12014-09-29 14:18:40 -04008015/** Import dmabuf buffer into current renderer
8016 *
8017 * \param compositor
8018 * \param buffer the dmabuf buffer to import
8019 * \return true on usable buffers, false otherwise
8020 *
8021 * This function tests that the linux_dmabuf_buffer is usable
8022 * for the current renderer. Returns false on unusable buffers. Usually
8023 * usability is tested by importing the dmabufs for composition.
8024 *
8025 * This hook is also used for detecting if the renderer supports
8026 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
8027 * supported.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008028 *
8029 * \ingroup compositor
8030 */
Pekka Paalanen230f3b12014-09-29 14:18:40 -04008031WL_EXPORT bool
8032weston_compositor_import_dmabuf(struct weston_compositor *compositor,
8033 struct linux_dmabuf_buffer *buffer)
8034{
8035 struct weston_renderer *renderer;
8036
8037 renderer = compositor->renderer;
8038
8039 if (renderer->import_dmabuf == NULL)
8040 return false;
8041
8042 return renderer->import_dmabuf(compositor, buffer);
8043}
8044
Marius Vlad5a701542019-11-16 20:26:52 +02008045WL_EXPORT bool
8046weston_compositor_dmabuf_can_scanout(struct weston_compositor *compositor,
8047 struct linux_dmabuf_buffer *buffer)
8048{
8049 struct weston_backend *backend = compositor->backend;
8050
8051 if (backend->can_scanout_dmabuf == NULL)
8052 return false;
8053
8054 return backend->can_scanout_dmabuf(compositor, buffer);
8055}
8056
Giulio Camuffocdb4d292013-11-14 23:42:53 +01008057WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05008058weston_version(int *major, int *minor, int *micro)
8059{
8060 *major = WESTON_VERSION_MAJOR;
8061 *minor = WESTON_VERSION_MINOR;
8062 *micro = WESTON_VERSION_MICRO;
8063}
8064
Daniel Stonee03c1112016-11-24 20:45:45 +00008065/**
8066 * Attempts to find a module path from the module map specified in the
8067 * environment. If found, writes the full path into the path variable.
8068 *
8069 * The module map is a string in environment variable WESTON_MODULE_MAP, where
8070 * each entry is of the form "name=path" and entries are separated by
8071 * semicolons. Whitespace is significant.
8072 *
8073 * \param name The name to search for.
8074 * \param path Where the path is written to if found.
8075 * \param path_len Allocated bytes at \c path .
8076 * \returns The length of the string written to path on success, or 0 if the
8077 * module was not specified in the environment map or path_len was too small.
8078 */
8079WL_EXPORT size_t
8080weston_module_path_from_env(const char *name, char *path, size_t path_len)
8081{
8082 const char *mapping = getenv("WESTON_MODULE_MAP");
8083 const char *end;
8084 const int name_len = strlen(name);
8085
8086 if (!mapping)
8087 return 0;
8088
8089 end = mapping + strlen(mapping);
8090 while (mapping < end && *mapping) {
8091 const char *filename, *next;
8092
8093 /* early out: impossibly short string */
8094 if (end - mapping < name_len + 1)
8095 return 0;
8096
8097 filename = &mapping[name_len + 1];
8098 next = strchrnul(mapping, ';');
8099
8100 if (strncmp(mapping, name, name_len) == 0 &&
8101 mapping[name_len] == '=') {
8102 size_t file_len = next - filename; /* no trailing NUL */
8103 if (file_len >= path_len)
8104 return 0;
8105 strncpy(path, filename, file_len);
8106 path[file_len] = '\0';
8107 return file_len;
8108 }
8109
8110 mapping = next + 1;
8111 }
8112
8113 return 0;
8114}
8115
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03008116WL_EXPORT void *
8117weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04008118{
8119 char path[PATH_MAX];
8120 void *module, *init;
Daniel Stonebeb97e52016-11-28 12:13:54 +00008121 size_t len;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04008122
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08008123 if (name == NULL)
8124 return NULL;
8125
Derek Foreman3f86e502015-06-08 11:46:54 -05008126 if (name[0] != '/') {
Daniel Stonee03c1112016-11-24 20:45:45 +00008127 len = weston_module_path_from_env(name, path, sizeof path);
8128 if (len == 0)
Daniel Stonebeb97e52016-11-28 12:13:54 +00008129 len = snprintf(path, sizeof path, "%s/%s",
8130 LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05008131 } else {
Daniel Stonebeb97e52016-11-28 12:13:54 +00008132 len = snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05008133 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04008134
Daniel Stonebeb97e52016-11-28 12:13:54 +00008135 /* snprintf returns the length of the string it would've written,
8136 * _excluding_ the NUL byte. So even being equal to the size of
8137 * our buffer is an error here. */
8138 if (len >= sizeof path)
8139 return NULL;
8140
Kristian Høgsberga6813d22012-09-12 12:21:01 -04008141 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
8142 if (module) {
8143 weston_log("Module '%s' already loaded\n", path);
Pekka Paalanenf696ee92019-11-04 14:07:51 +02008144 } else {
8145 weston_log("Loading module '%s'\n", path);
8146 module = dlopen(path, RTLD_NOW);
8147 if (!module) {
8148 weston_log("Failed to load module: %s\n", dlerror());
8149 return NULL;
8150 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04008151 }
8152
8153 init = dlsym(module, entrypoint);
8154 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03008155 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00008156 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04008157 return NULL;
8158 }
8159
8160 return init;
8161}
8162
Pekka Paalanen6ffbba32019-11-06 12:59:32 +02008163/** Add a compositor destroy listener only once
8164 *
8165 * \param compositor The compositor whose destroy to watch for.
8166 * \param listener The listener struct to initialize.
8167 * \param destroy_handler The callback when compositor is destroyed.
8168 * \return True if listener is added, or false if there already is a listener
8169 * with the given \c destroy_handler.
8170 *
8171 * This function does nothing and returns false if the given callback function
8172 * is already present in the weston_compositor destroy callbacks list.
8173 * Otherwise, this function initializes the given listener with the given
8174 * callback pointer and adds it to the compositor's destroy callbacks list.
8175 *
8176 * This can be used to ensure that plugin initialization is done only once
8177 * in case the same plugin is loaded multiple times. If this function returns
8178 * false, the plugin should be already initialized successfully.
8179 *
8180 * All plugins should register a destroy listener for cleaning up. Note, that
8181 * the plugin destruction order is not guaranteed: plugins that depend on other
8182 * plugins must be able to be torn down in arbitrary order.
8183 *
Leandro Ribeiroca640d52020-01-27 19:12:01 -03008184 * \sa weston_compositor_destroy
Pekka Paalanen6ffbba32019-11-06 12:59:32 +02008185 */
8186WL_EXPORT bool
8187weston_compositor_add_destroy_listener_once(struct weston_compositor *compositor,
8188 struct wl_listener *listener,
8189 wl_notify_func_t destroy_handler)
8190{
8191 if (wl_signal_get(&compositor->destroy_signal, destroy_handler))
8192 return false;
8193
8194 listener->notify = destroy_handler;
8195 wl_signal_add(&compositor->destroy_signal, listener);
8196 return true;
8197}
Giulio Camuffo459137b2014-10-11 23:56:24 +03008198
Leandro Ribeiroca640d52020-01-27 19:12:01 -03008199/** Destroys the compositor.
Giulio Camuffo459137b2014-10-11 23:56:24 +03008200 *
Leandro Ribeiroca640d52020-01-27 19:12:01 -03008201 * This function cleans up the compositor state and then destroys it.
Giulio Camuffo459137b2014-10-11 23:56:24 +03008202 *
Leandro Ribeiroca640d52020-01-27 19:12:01 -03008203 * @param compositor The compositor to be destroyed.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008204 *
Marius Vlad284d5342019-06-24 12:00:47 +03008205 * @ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03008206 */
8207WL_EXPORT void
Leandro Ribeiroca640d52020-01-27 19:12:01 -03008208weston_compositor_destroy(struct weston_compositor *compositor)
Giulio Camuffo459137b2014-10-11 23:56:24 +03008209{
8210 /* prevent further rendering while shutting down */
8211 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
8212
Marius Vladf2452d62021-12-09 14:07:21 +02008213 weston_signal_emit_mutable(&compositor->destroy_signal, compositor);
Giulio Camuffo459137b2014-10-11 23:56:24 +03008214
8215 weston_compositor_xkb_destroy(compositor);
8216
Giulio Camuffo2d24e642015-10-03 16:25:15 +03008217 if (compositor->backend)
8218 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02008219
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03008220 /* The backend is responsible for destroying the heads. */
8221 assert(wl_list_empty(&compositor->head_list));
8222
Pekka Paalanen827b5d22016-06-29 11:54:26 +02008223 weston_plugin_api_destroy_list(compositor);
8224
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03008225 if (compositor->heads_changed_source)
8226 wl_event_source_remove(compositor->heads_changed_source);
8227
Leandro Ribeirof0149642019-12-18 15:52:18 -03008228 weston_log_scope_destroy(compositor->debug_scene);
Daniel Stonece62cb32018-07-20 09:46:24 +01008229 compositor->debug_scene = NULL;
Marius Vladda104eb2019-09-05 14:31:01 +03008230
Leandro Ribeirof0149642019-12-18 15:52:18 -03008231 weston_log_scope_destroy(compositor->timeline);
Marius Vladda104eb2019-09-05 14:31:01 +03008232 compositor->timeline = NULL;
Pekka Paalanena5630ea2017-10-12 13:13:42 +02008233
Leandro Ribeiro8eb84142021-01-18 19:36:48 -03008234 if (compositor->default_dmabuf_feedback) {
8235 weston_dmabuf_feedback_destroy(compositor->default_dmabuf_feedback);
8236 weston_dmabuf_feedback_format_table_destroy(compositor->dmabuf_feedback_format_table);
8237 }
8238
Giulio Camuffo459137b2014-10-11 23:56:24 +03008239 free(compositor);
8240}
8241
8242/** Instruct the compositor to exit.
8243 *
8244 * This functions does not directly destroy the compositor object, it merely
8245 * command it to start the tear down process. It is not guaranteed that the
8246 * tear down will happen immediately.
8247 *
8248 * \param compositor The compositor to tear down.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008249 *
8250 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03008251 */
8252WL_EXPORT void
8253weston_compositor_exit(struct weston_compositor *compositor)
8254{
8255 compositor->exit(compositor);
8256}
8257
8258/** Return the user data stored in the compositor.
8259 *
8260 * This function returns the user data pointer set with user_data parameter
8261 * to the \ref weston_compositor_create function.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008262 *
8263 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03008264 */
8265WL_EXPORT void *
8266weston_compositor_get_user_data(struct weston_compositor *compositor)
8267{
8268 return compositor->user_data;
8269}
Pekka Paalanendd186732016-06-03 14:49:54 +03008270
Pekka Paalanen50dbf382016-06-03 15:23:46 +03008271static const char * const backend_map[] = {
8272 [WESTON_BACKEND_DRM] = "drm-backend.so",
8273 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
8274 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
8275 [WESTON_BACKEND_RDP] = "rdp-backend.so",
8276 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
8277 [WESTON_BACKEND_X11] = "x11-backend.so",
8278};
8279
Pekka Paalanendd186732016-06-03 14:49:54 +03008280/** Load a backend into a weston_compositor
8281 *
8282 * A backend must be loaded to make a weston_compositor work. A backend
8283 * provides input and output capabilities, and determines the renderer to use.
8284 *
8285 * \param compositor A compositor that has not had a backend loaded yet.
8286 * \param backend Name of the backend file.
8287 * \param config_base A pointer to a backend-specific configuration
8288 * structure's 'base' member.
8289 *
8290 * \return 0 on success, or -1 on error.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008291 *
8292 * \ingroup compositor
Pekka Paalanendd186732016-06-03 14:49:54 +03008293 */
8294WL_EXPORT int
8295weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03008296 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03008297 struct weston_backend_config *config_base)
8298{
8299 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03008300 struct weston_backend_config *config_base);
8301
Pekka Paalanend7e35112017-08-29 17:04:12 +03008302 if (compositor->backend) {
8303 weston_log("Error: attempt to load a backend when one is already loaded\n");
8304 return -1;
8305 }
8306
Quentin Glidic887c0182016-07-10 11:00:53 +02008307 if (backend >= ARRAY_LENGTH(backend_map))
Pekka Paalanen50dbf382016-06-03 15:23:46 +03008308 return -1;
8309
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01008310 backend_init = weston_load_module(backend_map[backend], "weston_backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03008311 if (!backend_init)
8312 return -1;
8313
Pekka Paalanend7e35112017-08-29 17:04:12 +03008314 if (backend_init(compositor, config_base) < 0) {
8315 compositor->backend = NULL;
8316 return -1;
8317 }
8318
Pekka Paalanen90a5ffa2021-02-25 12:03:28 +02008319 if (!compositor->color_manager) {
8320 compositor->color_manager =
8321 weston_color_manager_noop_create(compositor);
8322 }
8323
8324 if (!compositor->color_manager)
8325 return -1;
8326
8327 if (!compositor->color_manager->init(compositor->color_manager))
8328 return -1;
8329
8330 weston_log("Color manager: %s\n", compositor->color_manager->name);
8331
Pekka Paalanend7e35112017-08-29 17:04:12 +03008332 return 0;
Pekka Paalanendd186732016-06-03 14:49:54 +03008333}
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008334
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008335/** weston_compositor_load_xwayland
8336 * \ingroup compositor
8337 */
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008338WL_EXPORT int
8339weston_compositor_load_xwayland(struct weston_compositor *compositor)
8340{
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01008341 int (*module_init)(struct weston_compositor *ec);
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008342
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01008343 module_init = weston_load_module("xwayland.so", "weston_module_init");
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008344 if (!module_init)
8345 return -1;
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01008346 if (module_init(compositor) < 0)
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008347 return -1;
8348 return 0;
8349}
Vasilis Liaskovitis486b4632018-10-10 16:14:55 +02008350
Pekka Paalanen5e79dd42021-03-29 14:11:49 +03008351/** Load Little CMS color manager plugin
8352 *
8353 * Calling this function before loading any backend sets Little CMS
8354 * as the active color matching module (CMM) instead of the default no-op
8355 * color manager.
8356 *
8357 * \ingroup compositor
8358 */
8359WL_EXPORT int
8360weston_compositor_load_color_manager(struct weston_compositor *compositor)
8361{
8362 struct weston_color_manager *
8363 (*cm_create)(struct weston_compositor *compositor);
8364
8365 if (compositor->color_manager) {
8366 weston_log("Error: Color manager '%s' is loaded, cannot load another.\n",
8367 compositor->color_manager->name);
8368 return -1;
8369 }
8370
8371 cm_create = weston_load_module("color-lcms.so", "weston_color_manager_create");
8372 if (!cm_create) {
8373 weston_log("Error: Could not load color-lcms.so.\n");
8374 return -1;
8375 }
8376
8377 compositor->color_manager = cm_create(compositor);
8378 if (!compositor->color_manager) {
8379 weston_log("Error: loading color-lcms.so failed.\n");
8380 return -1;
8381 }
8382
8383 return 0;
8384}
8385
Vasilis Liaskovitis486b4632018-10-10 16:14:55 +02008386/** Resolve an internal compositor error by disconnecting the client.
8387 *
8388 * This function is used in cases when the wl_buffer turns out
8389 * unusable and there is no fallback path.
8390 *
8391 * It is possible the fault is caused by a compositor bug, the underlying
8392 * graphics stack bug or normal behaviour, or perhaps a client mistake.
8393 * In any case, the options are to either composite garbage or nothing,
8394 * or disconnect the client. This is a helper function for the latter.
8395 *
8396 * The error is sent as an INVALID_OBJECT error on the client's wl_display.
8397 *
8398 * \param buffer The weston buffer that is unusable.
8399 * \param msg A custom error message attached to the protocol error.
8400 */
8401WL_EXPORT void
8402weston_buffer_send_server_error(struct weston_buffer *buffer,
8403 const char *msg)
8404{
8405 struct wl_client *client;
8406 struct wl_resource *display_resource;
8407 uint32_t id;
8408
8409 assert(buffer->resource);
8410 id = wl_resource_get_id(buffer->resource);
8411 client = wl_resource_get_client(buffer->resource);
8412 display_resource = wl_client_get_object(client, 1);
8413
8414 assert(display_resource);
8415 wl_resource_post_error(display_resource,
8416 WL_DISPLAY_ERROR_INVALID_OBJECT,
8417 "server error with "
8418 "wl_buffer@%u: %s", id, msg);
8419}
Ankit Nautiyal93dde242019-07-08 11:46:42 +05308420
8421WL_EXPORT void
8422weston_output_disable_planes_incr(struct weston_output *output)
8423{
8424 output->disable_planes++;
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05308425 /*
8426 * If disable_planes changes from 0 to non-zero, it means some type of
8427 * recording of content has started, and therefore protection level of
8428 * the protected surfaces must be updated to avoid the recording of
8429 * the protected content.
8430 */
8431 if (output->disable_planes == 1)
8432 weston_schedule_surface_protection_update(output->compositor);
Ankit Nautiyal93dde242019-07-08 11:46:42 +05308433}
8434
8435WL_EXPORT void
8436weston_output_disable_planes_decr(struct weston_output *output)
8437{
8438 output->disable_planes--;
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05308439 /*
8440 * If disable_planes changes from non-zero to 0, it means no content
8441 * recording is going on any more, and the protected and surfaces can be
8442 * shown without any apprehensions about content being recorded.
8443 */
8444 if (output->disable_planes == 0)
8445 weston_schedule_surface_protection_update(output->compositor);
8446
Ankit Nautiyal93dde242019-07-08 11:46:42 +05308447}