blob: fb3b70949aa74519b9b4312e0375bba514242e31 [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 */
122 wl_list_for_each(existing_node, &view->paint_node_list, view_link) {
123 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);
Michael Olbrich0e4f0972020-08-19 11:31:03 +02001525 pixman_region32_intersect(&view->transform.opaque,
1526 &view->transform.opaque,
1527 &view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001528 pixman_region32_translate(&view->transform.opaque,
1529 view->geometry.x,
1530 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001531 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001532}
1533
1534static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001535weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001536{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001537 struct weston_view *parent = view->geometry.parent;
1538 struct weston_matrix *matrix = &view->transform.matrix;
1539 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001540 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001541 pixman_region32_t surfregion;
1542 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001543
Jason Ekstranda7af7042013-10-12 22:38:11 -05001544 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001545
1546 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001547 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1548 view->transform.position.matrix.d[12] = view->geometry.x;
1549 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001550
1551 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001552 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001553 weston_matrix_multiply(matrix, &tform->matrix);
1554
Pekka Paalanen483243f2013-03-08 14:56:50 +02001555 if (parent)
1556 weston_matrix_multiply(matrix, &parent->transform.matrix);
1557
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001558 if (weston_matrix_invert(inverse, matrix) < 0) {
1559 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001560 weston_log("error: weston_view %p"
1561 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001562 return -1;
1563 }
1564
Pekka Paalanen380adf52015-02-16 14:39:11 +02001565 pixman_region32_init_rect(&surfregion, 0, 0,
1566 view->surface->width, view->surface->height);
Michael Olbrichcb04f982020-06-08 10:22:32 +02001567
1568 weston_view_update_transform_scissor(view, &surfregion);
1569
Pekka Paalanen380adf52015-02-16 14:39:11 +02001570 surfbox = pixman_region32_extents(&surfregion);
1571
1572 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1573 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001574
Michael Olbrich0e4f0972020-08-19 11:31:03 +02001575 if (view->alpha == 1.0 &&
1576 matrix->type == WESTON_MATRIX_TRANSFORM_TRANSLATE) {
1577 pixman_region32_copy(&view->transform.opaque,
1578 &view->surface->opaque);
1579 pixman_region32_intersect(&view->transform.opaque,
1580 &view->transform.opaque,
1581 &view->geometry.scissor);
1582 pixman_region32_translate(&view->transform.opaque,
1583 matrix->d[12],
1584 matrix->d[13]);
1585 }
1586
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001587 return 0;
1588}
1589
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001590static struct weston_layer *
1591get_view_layer(struct weston_view *view)
1592{
1593 if (view->parent_view)
1594 return get_view_layer(view->parent_view);
1595 return view->layer_link.layer;
1596}
1597
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001598WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001599weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001600{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001601 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001602 struct weston_layer *layer;
1603 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001604
Jason Ekstranda7af7042013-10-12 22:38:11 -05001605 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001606 return;
1607
Pekka Paalanen483243f2013-03-08 14:56:50 +02001608 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001609 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001610
Jason Ekstranda7af7042013-10-12 22:38:11 -05001611 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001612
Jason Ekstranda7af7042013-10-12 22:38:11 -05001613 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001614
Jason Ekstranda7af7042013-10-12 22:38:11 -05001615 pixman_region32_fini(&view->transform.boundingbox);
1616 pixman_region32_fini(&view->transform.opaque);
1617 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001618
Pekka Paalanencd403622012-01-25 13:37:39 +02001619 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001620 if (view->geometry.transformation_list.next ==
1621 &view->transform.position.link &&
1622 view->geometry.transformation_list.prev ==
1623 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001624 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001625 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001626 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001627 if (weston_view_update_transform_enable(view) < 0)
1628 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001629 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001630
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001631 layer = get_view_layer(view);
1632 if (layer) {
1633 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001634 pixman_region32_intersect(&view->transform.boundingbox,
1635 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001636 pixman_region32_intersect(&view->transform.opaque,
1637 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001638 pixman_region32_fini(&mask);
1639 }
1640
Jason Ekstranda7af7042013-10-12 22:38:11 -05001641 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001642
Jason Ekstranda7af7042013-10-12 22:38:11 -05001643 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001644
Jason Ekstranda7af7042013-10-12 22:38:11 -05001645 wl_signal_emit(&view->surface->compositor->transform_signal,
1646 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001647}
1648
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001649WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001650weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001651{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001652 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001653
1654 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001655 * The invariant: if view->geometry.dirty, then all views
1656 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001657 * Corollary: if not parent->geometry.dirty, then all ancestors
1658 * are not dirty.
1659 */
1660
Jason Ekstranda7af7042013-10-12 22:38:11 -05001661 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001662 return;
1663
Jason Ekstranda7af7042013-10-12 22:38:11 -05001664 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001665
Jason Ekstranda7af7042013-10-12 22:38:11 -05001666 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001667 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001668 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001669}
1670
1671WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001672weston_view_to_global_fixed(struct weston_view *view,
1673 wl_fixed_t vx, wl_fixed_t vy,
1674 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001675{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001676 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001677
Jason Ekstranda7af7042013-10-12 22:38:11 -05001678 weston_view_to_global_float(view,
1679 wl_fixed_to_double(vx),
1680 wl_fixed_to_double(vy),
1681 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001682 *x = wl_fixed_from_double(xf);
1683 *y = wl_fixed_from_double(yf);
1684}
1685
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001686WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001687weston_view_from_global_float(struct weston_view *view,
1688 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001689{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001690 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001691 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1692
Jason Ekstranda7af7042013-10-12 22:38:11 -05001693 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001694
1695 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001696 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001697 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001698 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001699 *vx = 0;
1700 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001701 return;
1702 }
1703
Jason Ekstranda7af7042013-10-12 22:38:11 -05001704 *vx = v.f[0] / v.f[3];
1705 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001706 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001707 *vx = x - view->geometry.x;
1708 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001709 }
1710}
1711
1712WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001713weston_view_from_global_fixed(struct weston_view *view,
1714 wl_fixed_t x, wl_fixed_t y,
1715 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001716{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001717 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001718
Jason Ekstranda7af7042013-10-12 22:38:11 -05001719 weston_view_from_global_float(view,
1720 wl_fixed_to_double(x),
1721 wl_fixed_to_double(y),
1722 &vxf, &vyf);
1723 *vx = wl_fixed_from_double(vxf);
1724 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001725}
1726
1727WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001728weston_view_from_global(struct weston_view *view,
1729 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001730{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001731 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001732
Jason Ekstranda7af7042013-10-12 22:38:11 -05001733 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1734 *vx = floorf(vxf);
1735 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001736}
1737
Bryce Harrington3f650b82015-12-23 11:01:58 -08001738/**
1739 * \param surface The surface to be repainted
1740 *
1741 * Marks the output(s) that the surface is shown on as needing to be
1742 * repainted. See weston_output_schedule_repaint().
1743 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001744WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001745weston_surface_schedule_repaint(struct weston_surface *surface)
1746{
1747 struct weston_output *output;
1748
1749 wl_list_for_each(output, &surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001750 if (surface->output_mask & (1u << output->id))
Kristian Høgsberg98238702012-08-03 16:29:12 -04001751 weston_output_schedule_repaint(output);
1752}
1753
Bryce Harrington3f650b82015-12-23 11:01:58 -08001754/**
1755 * \param view The view to be repainted
1756 *
1757 * Marks the output(s) that the view is shown on as needing to be
1758 * repainted. See weston_output_schedule_repaint().
1759 */
Kristian Høgsberg98238702012-08-03 16:29:12 -04001760WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001761weston_view_schedule_repaint(struct weston_view *view)
1762{
1763 struct weston_output *output;
1764
1765 wl_list_for_each(output, &view->surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001766 if (view->output_mask & (1u << output->id))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001767 weston_output_schedule_repaint(output);
1768}
1769
Pekka Paalanene508ce62015-02-19 13:59:55 +02001770/**
1771 * XXX: This function does it the wrong way.
1772 * surface->damage is the damage from the client, and causes
1773 * surface_flush_damage() to copy pixels. No window management action can
1774 * cause damage to the client-provided content, warranting re-upload!
1775 *
1776 * Instead of surface->damage, this function should record the damage
1777 * with all the views for this surface to avoid extraneous texture
1778 * uploads.
1779 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001780WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001781weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001782{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001783 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001784 0, 0, surface->width,
1785 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001786
Kristian Høgsberg98238702012-08-03 16:29:12 -04001787 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001788}
1789
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001790WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001791weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001792{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001793 if (view->geometry.x == x && view->geometry.y == y)
1794 return;
1795
Jason Ekstranda7af7042013-10-12 22:38:11 -05001796 view->geometry.x = x;
1797 view->geometry.y = y;
1798 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001799}
1800
Pekka Paalanen483243f2013-03-08 14:56:50 +02001801static void
1802transform_parent_handle_parent_destroy(struct wl_listener *listener,
1803 void *data)
1804{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001805 struct weston_view *view =
1806 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001807 geometry.parent_destroy_listener);
1808
Jason Ekstranda7af7042013-10-12 22:38:11 -05001809 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001810}
1811
1812WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001813weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001814 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001815{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001816 if (view->geometry.parent) {
1817 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1818 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001819
1820 if (!parent)
1821 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001822 }
1823
Jason Ekstranda7af7042013-10-12 22:38:11 -05001824 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001825
Jason Ekstranda7af7042013-10-12 22:38:11 -05001826 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001827 transform_parent_handle_parent_destroy;
1828 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001829 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001830 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001831 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001832 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001833 }
1834
Jason Ekstranda7af7042013-10-12 22:38:11 -05001835 weston_view_geometry_dirty(view);
1836}
1837
Pekka Paalanen380adf52015-02-16 14:39:11 +02001838/** Set a clip mask rectangle on a view
1839 *
1840 * \param view The view to set the clip mask on.
1841 * \param x Top-left corner X coordinate of the clip rectangle.
1842 * \param y Top-left corner Y coordinate of the clip rectangle.
1843 * \param width Width of the clip rectangle, non-negative.
1844 * \param height Height of the clip rectangle, non-negative.
1845 *
1846 * A shell may set a clip mask rectangle on a view. Everything outside
1847 * the rectangle is cut away for input and output purposes: it is
1848 * not drawn and cannot be hit by hit-test based input like pointer
1849 * motion or touch-downs. Everything inside the rectangle will behave
1850 * normally. Clients are unaware of clipping.
1851 *
Yong Bakos4c72e292016-04-28 11:59:10 -05001852 * The rectangle is set in surface-local coordinates. Setting a clip
Pekka Paalanen380adf52015-02-16 14:39:11 +02001853 * mask rectangle does not affect the view position, the view is positioned
1854 * as it would be without a clip. The clip also does not change
1855 * weston_surface::width,height.
1856 *
1857 * The clip mask rectangle is part of transformation inheritance
1858 * (weston_view_set_transform_parent()). A clip set in the root of the
1859 * transformation inheritance tree will affect all views in the tree.
1860 * A clip can be set only on the root view. Attempting to set a clip
1861 * on view that has a transformation parent will fail. Assigning a parent
1862 * to a view that has a clip set will cause the clip to be forgotten.
1863 *
1864 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1865 * on the additional transformations in the child views. These transformations
1866 * may not rotate the coordinate axes, i.e., only translation and scaling
1867 * are allowed. Violating this restriction causes the clipping to malfunction.
1868 * Furthermore, using scaling may cause rounding errors in child clipping.
1869 *
1870 * The clip mask rectangle is not automatically adjusted based on
1871 * wl_surface.attach dx and dy arguments.
1872 *
1873 * A clip mask rectangle can be set only if the compositor capability
1874 * WESTON_CAP_VIEW_CLIP_MASK is present.
1875 *
1876 * This function sets the clip mask rectangle and schedules a repaint for
1877 * the view.
1878 */
1879WL_EXPORT void
1880weston_view_set_mask(struct weston_view *view,
1881 int x, int y, int width, int height)
1882{
1883 struct weston_compositor *compositor = view->surface->compositor;
1884
1885 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1886 weston_log("%s not allowed without capability!\n", __func__);
1887 return;
1888 }
1889
1890 if (view->geometry.parent) {
1891 weston_log("view %p has a parent, clip forbidden!\n", view);
1892 return;
1893 }
1894
1895 if (width < 0 || height < 0) {
1896 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1897 x, y, width, height);
1898 return;
1899 }
1900
1901 pixman_region32_fini(&view->geometry.scissor);
1902 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1903 view->geometry.scissor_enabled = true;
1904 weston_view_geometry_dirty(view);
1905 weston_view_schedule_repaint(view);
1906}
1907
1908/** Remove the clip mask from a view
1909 *
1910 * \param view The view to remove the clip mask from.
1911 *
1912 * Removed the clip mask rectangle and schedules a repaint.
1913 *
1914 * \sa weston_view_set_mask
1915 */
1916WL_EXPORT void
1917weston_view_set_mask_infinite(struct weston_view *view)
1918{
1919 view->geometry.scissor_enabled = false;
1920 weston_view_geometry_dirty(view);
1921 weston_view_schedule_repaint(view);
1922}
1923
Armin Krezović0da12b82016-06-30 06:04:33 +02001924/* Check if view should be displayed
1925 *
1926 * The indicator is set manually when assigning
1927 * a view to a surface.
1928 *
1929 * This needs reworking. See the thread starting at:
1930 *
1931 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1932 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001933WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001934weston_view_is_mapped(struct weston_view *view)
1935{
Armin Krezović0da12b82016-06-30 06:04:33 +02001936 return view->is_mapped;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001937}
1938
Philipp Zabel70decd52018-09-03 20:11:15 +02001939/* Check if view is opaque in specified region
1940 *
1941 * \param view The view to check for opacity.
1942 * \param region The region to check for opacity, in view coordinates.
1943 *
1944 * Returns true if the view is opaque in the specified region, because view
1945 * alpha is 1.0 and either the opaque region set by the client contains the
1946 * specified region, or the buffer pixel format or solid color is opaque.
1947 */
1948WL_EXPORT bool
1949weston_view_is_opaque(struct weston_view *ev, pixman_region32_t *region)
1950{
1951 pixman_region32_t r;
1952 bool ret = false;
1953
1954 if (ev->alpha < 1.0)
1955 return false;
1956
1957 if (ev->surface->is_opaque)
1958 return true;
1959
Marius Vlad67b382c2020-08-21 13:57:54 +03001960 if (ev->transform.dirty)
Philipp Zabel70decd52018-09-03 20:11:15 +02001961 return false;
Philipp Zabel70decd52018-09-03 20:11:15 +02001962
1963 pixman_region32_init(&r);
1964 pixman_region32_subtract(&r, region, &ev->transform.opaque);
1965
1966 if (!pixman_region32_not_empty(&r))
1967 ret = true;
1968
1969 pixman_region32_fini(&r);
1970
1971 return ret;
1972}
1973
Marius Vlad5f6bee42019-09-11 16:41:04 +03001974/** Check if the view has a valid buffer available
1975 *
1976 * @param ev The view to check if it has a valid buffer.
1977 *
1978 * Returns true if the view has a valid buffer or false otherwise.
1979 */
1980WL_EXPORT bool
1981weston_view_has_valid_buffer(struct weston_view *ev)
1982{
1983 return ev->surface->buffer_ref.buffer != NULL;
1984}
1985
Marius Vlad47e3d1e2019-09-11 16:53:08 +03001986/** Check if the view matches the entire output
1987 *
1988 * @param ev The view to check.
1989 * @param output The output to check against.
1990 *
1991 * Returns true if the view does indeed matches the entire output.
1992 */
1993WL_EXPORT bool
1994weston_view_matches_output_entirely(struct weston_view *ev,
1995 struct weston_output *output)
1996{
1997 pixman_box32_t *extents =
1998 pixman_region32_extents(&ev->transform.boundingbox);
1999
2000 if (extents->x1 != output->x ||
2001 extents->y1 != output->y ||
2002 extents->x2 != output->x + output->width ||
2003 extents->y2 != output->y + output->height)
2004 return false;
2005
2006 return true;
2007}
2008
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002009/** Find paint node for the given view and output
2010 */
2011WL_EXPORT struct weston_paint_node *
2012weston_view_find_paint_node(struct weston_view *view,
2013 struct weston_output *output)
2014{
2015 struct weston_paint_node *pnode;
2016
2017 wl_list_for_each(pnode, &view->paint_node_list, view_link) {
2018 assert(pnode->surface == view->surface);
2019 if (pnode->output == output)
2020 return pnode;
2021 }
2022
2023 return NULL;
2024}
2025
Armin Krezović0da12b82016-06-30 06:04:33 +02002026/* Check if a surface has a view assigned to it
2027 *
2028 * The indicator is set manually when mapping
2029 * a surface and creating a view for it.
2030 *
2031 * This needs to go. See the thread starting at:
2032 *
2033 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
2034 *
2035 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05002036WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03002037weston_surface_is_mapped(struct weston_surface *surface)
2038{
Armin Krezović0da12b82016-06-30 06:04:33 +02002039 return surface->is_mapped;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03002040}
2041
Pekka Paalanenda75ee12013-11-26 18:19:43 +01002042static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06002043surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002044{
2045 struct weston_view *view;
2046
2047 if (surface->width == width && surface->height == height)
2048 return;
2049
2050 surface->width = width;
2051 surface->height = height;
2052
2053 wl_list_for_each(view, &surface->views, surface_link)
2054 weston_view_geometry_dirty(view);
2055}
2056
Jason Ekstrand5c11a332013-12-04 20:32:03 -06002057WL_EXPORT void
2058weston_surface_set_size(struct weston_surface *surface,
2059 int32_t width, int32_t height)
2060{
2061 assert(!surface->resource);
2062 surface_set_size(surface, width, height);
2063}
2064
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02002065static int
2066fixed_round_up_to_int(wl_fixed_t f)
2067{
2068 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
2069}
2070
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002071static void
Pekka Paalanen59987fa2016-04-26 15:50:59 +03002072convert_size_by_transform_scale(int32_t *width_out, int32_t *height_out,
2073 int32_t width, int32_t height,
2074 uint32_t transform,
2075 int32_t scale)
2076{
2077 assert(scale > 0);
2078
2079 switch (transform) {
2080 case WL_OUTPUT_TRANSFORM_NORMAL:
2081 case WL_OUTPUT_TRANSFORM_180:
2082 case WL_OUTPUT_TRANSFORM_FLIPPED:
2083 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2084 *width_out = width / scale;
2085 *height_out = height / scale;
2086 break;
2087 case WL_OUTPUT_TRANSFORM_90:
2088 case WL_OUTPUT_TRANSFORM_270:
2089 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2090 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2091 *width_out = height / scale;
2092 *height_out = width / scale;
2093 break;
2094 default:
2095 assert(0 && "invalid transform");
2096 }
2097}
2098
2099static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002100weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002101{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002102 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01002103
2104 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02002105 surface->width_from_buffer = 0;
2106 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01002107 return;
2108 }
2109
Pekka Paalanen59987fa2016-04-26 15:50:59 +03002110 convert_size_by_transform_scale(&surface->width_from_buffer,
2111 &surface->height_from_buffer,
2112 surface->buffer_ref.buffer->width,
2113 surface->buffer_ref.buffer->height,
2114 vp->buffer.transform,
2115 vp->buffer.scale);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002116}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02002117
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002118static void
2119weston_surface_update_size(struct weston_surface *surface)
2120{
2121 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
2122 int32_t width, height;
2123
2124 width = surface->width_from_buffer;
2125 height = surface->height_from_buffer;
2126
2127 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02002128 surface_set_size(surface,
2129 vp->surface.width, vp->surface.height);
2130 return;
2131 }
2132
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002133 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03002134 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
2135 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
2136
2137 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02002138 return;
2139 }
2140
Jason Ekstrand5c11a332013-12-04 20:32:03 -06002141 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002142}
2143
Marius Vlad9fdda7f2019-06-11 16:08:55 +03002144/** weston_compositor_get_time
2145 * \ingroup compositor
2146 */
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02002147WL_EXPORT void
2148weston_compositor_get_time(struct timespec *time)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05002149{
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02002150 clock_gettime(CLOCK_REALTIME, time);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05002151}
2152
Marius Vlad9fdda7f2019-06-11 16:08:55 +03002153/** weston_compositor_pick_view
2154 * \ingroup compositor
2155 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002156WL_EXPORT struct weston_view *
2157weston_compositor_pick_view(struct weston_compositor *compositor,
2158 wl_fixed_t x, wl_fixed_t y,
2159 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02002160{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002161 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02002162 wl_fixed_t view_x, view_y;
2163 int view_ix, view_iy;
2164 int ix = wl_fixed_to_int(x);
2165 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02002166
Pekka Paalanen6551c092021-05-03 16:09:45 +03002167 /* Can't use paint node list: occlusion by input regions, not opaque. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002168 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02002169 if (!pixman_region32_contains_point(
2170 &view->transform.boundingbox, ix, iy, NULL))
2171 continue;
2172
2173 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
2174 view_ix = wl_fixed_to_int(view_x);
2175 view_iy = wl_fixed_to_int(view_y);
2176
2177 if (!pixman_region32_contains_point(&view->surface->input,
2178 view_ix, view_iy, NULL))
2179 continue;
2180
Pekka Paalanen380adf52015-02-16 14:39:11 +02002181 if (view->geometry.scissor_enabled &&
2182 !pixman_region32_contains_point(&view->geometry.scissor,
2183 view_ix, view_iy, NULL))
2184 continue;
2185
Pekka Paalanenfc22a522015-02-18 15:08:29 +02002186 *vx = view_x;
2187 *vy = view_y;
2188 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02002189 }
2190
Derek Foremanf9318d12015-05-11 15:40:11 -05002191 *vx = wl_fixed_from_int(-1000000);
2192 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02002193 return NULL;
2194}
2195
2196static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002197weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002198{
Daniel Stone37816df2012-05-16 18:45:18 +01002199 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002200
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07002201 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002202 return;
2203
Daniel Stone37816df2012-05-16 18:45:18 +01002204 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04002205 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002206}
2207
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002208WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002209weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05002210{
Daniel Stone4dab5db2012-05-30 16:31:53 +01002211 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002212
Jason Ekstranda7af7042013-10-12 22:38:11 -05002213 if (!weston_view_is_mapped(view))
2214 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002215
Jason Ekstranda7af7042013-10-12 22:38:11 -05002216 weston_view_damage_below(view);
Semi Malinene7a52fb2018-04-26 11:08:10 +02002217 weston_view_set_output(view, NULL);
Xiong Zhang97116532013-10-23 13:58:31 +08002218 view->plane = NULL;
Armin Krezovićf8486c32016-06-30 06:04:28 +02002219 view->is_mapped = false;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002220 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002221 wl_list_remove(&view->link);
2222 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002223 view->output_mask = 0;
2224 weston_surface_assign_output(view->surface);
2225
2226 if (weston_surface_is_mapped(view->surface))
2227 return;
2228
2229 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002230 struct weston_touch *touch = weston_seat_get_touch(seat);
2231 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2232 struct weston_keyboard *keyboard =
2233 weston_seat_get_keyboard(seat);
2234
2235 if (keyboard && keyboard->focus == view->surface)
2236 weston_keyboard_set_focus(keyboard, NULL);
2237 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05002238 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05002239 if (touch && touch->focus == view)
2240 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01002241 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002242}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002243
Jason Ekstranda7af7042013-10-12 22:38:11 -05002244WL_EXPORT void
2245weston_surface_unmap(struct weston_surface *surface)
2246{
2247 struct weston_view *view;
2248
Armin Krezovićf8486c32016-06-30 06:04:28 +02002249 surface->is_mapped = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002250 wl_list_for_each(view, &surface->views, surface_link)
2251 weston_view_unmap(view);
2252 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05002253}
2254
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002255static void
2256weston_surface_reset_pending_buffer(struct weston_surface *surface)
2257{
Jason Ekstrand7b982072014-05-20 14:33:03 -05002258 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002259 surface->pending.sx = 0;
2260 surface->pending.sy = 0;
2261 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002262 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002263}
2264
Jason Ekstranda7af7042013-10-12 22:38:11 -05002265WL_EXPORT void
2266weston_view_destroy(struct weston_view *view)
2267{
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002268 struct weston_paint_node *pnode, *pntmp;
2269
Jason Ekstranda7af7042013-10-12 22:38:11 -05002270 wl_signal_emit(&view->destroy_signal, view);
2271
2272 assert(wl_list_empty(&view->geometry.child_list));
2273
2274 if (weston_view_is_mapped(view)) {
2275 weston_view_unmap(view);
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002276 weston_compositor_build_view_list(view->surface->compositor,
2277 NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002278 }
2279
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002280 wl_list_for_each_safe(pnode, pntmp, &view->paint_node_list, view_link)
2281 weston_paint_node_destroy(pnode);
2282
Jason Ekstranda7af7042013-10-12 22:38:11 -05002283 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002284 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002285
2286 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02002287 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002288 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002289 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002290
2291 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen944fae82018-05-22 13:15:58 +03002292 weston_view_set_output(view, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002293
Jason Ekstranda7af7042013-10-12 22:38:11 -05002294 wl_list_remove(&view->surface_link);
2295
2296 free(view);
2297}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002298
2299WL_EXPORT void
2300weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05002301{
Vlad Zahorodniic2b97472021-07-29 11:16:51 +03002302 struct wl_resource *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002303 struct weston_view *ev, *nv;
Jonas Ådahld3414f22016-07-22 17:56:31 +08002304 struct weston_pointer_constraint *constraint, *next_constraint;
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002305 struct weston_paint_node *pnode, *pntmp;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04002306
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02002307 if (--surface->ref_count > 0)
2308 return;
2309
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03002310 assert(surface->resource == NULL);
2311
Alexandros Frantzisfa48c5e2020-07-09 17:31:34 +03002312 weston_signal_emit_mutable(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02002313
Pekka Paalanene67858b2013-04-25 13:57:42 +03002314 assert(wl_list_empty(&surface->subsurface_list_pending));
2315 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02002316
Leandro Ribeiro8eb84142021-01-18 19:36:48 -03002317 if (surface->dmabuf_feedback)
2318 weston_dmabuf_feedback_destroy(surface->dmabuf_feedback);
2319
Jason Ekstranda7af7042013-10-12 22:38:11 -05002320 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
2321 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002322
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002323 wl_list_for_each_safe(pnode, pntmp,
2324 &surface->paint_node_list, surface_link) {
2325 weston_paint_node_destroy(pnode);
2326 }
2327
Jason Ekstrand7b982072014-05-20 14:33:03 -05002328 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002329
Pekka Paalanende685b82012-12-04 15:58:12 +02002330 weston_buffer_reference(&surface->buffer_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002331 weston_buffer_release_reference(&surface->buffer_release_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04002332
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02002333 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002334 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002335 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02002336
Vlad Zahorodniic2b97472021-07-29 11:16:51 +03002337 wl_resource_for_each_safe(cb, next, &surface->frame_callback_list)
2338 wl_resource_destroy(cb);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002339
Pekka Paalanen133e4392014-09-23 22:08:46 -04002340 weston_presentation_feedback_discard_list(&surface->feedback_list);
2341
Jonas Ådahld3414f22016-07-22 17:56:31 +08002342 wl_list_for_each_safe(constraint, next_constraint,
2343 &surface->pointer_constraints,
2344 link)
2345 weston_pointer_constraint_destroy(constraint);
2346
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03002347 fd_clear(&surface->acquire_fence_fd);
2348
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04002349 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05002350}
2351
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002352static void
2353destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08002354{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002355 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08002356
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03002357 assert(surface);
2358
Giulio Camuffo0d379742013-11-15 22:06:15 +01002359 /* Set the resource to NULL, since we don't want to leave a
2360 * dangling pointer if the surface was refcounted and survives
2361 * the weston_surface_destroy() call. */
2362 surface->resource = NULL;
Pekka Paalanen4826f872016-04-22 14:14:38 +03002363
2364 if (surface->viewport_resource)
2365 wl_resource_set_user_data(surface->viewport_resource, NULL);
2366
Alexandros Frantzis27d7c392018-10-19 12:14:11 +03002367 if (surface->synchronization_resource) {
2368 wl_resource_set_user_data(surface->synchronization_resource,
2369 NULL);
2370 }
2371
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002372 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08002373}
2374
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002375static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002376weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
2377{
2378 struct weston_buffer *buffer =
2379 container_of(listener, struct weston_buffer, destroy_listener);
2380
2381 wl_signal_emit(&buffer->destroy_signal, buffer);
2382 free(buffer);
2383}
2384
Giulio Camuffoe058cd12013-12-12 14:14:29 +01002385WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002386weston_buffer_from_resource(struct wl_resource *resource)
2387{
2388 struct weston_buffer *buffer;
2389 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002390
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002391 listener = wl_resource_get_destroy_listener(resource,
2392 weston_buffer_destroy_handler);
2393
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002394 if (listener)
2395 return container_of(listener, struct weston_buffer,
2396 destroy_listener);
2397
2398 buffer = zalloc(sizeof *buffer);
2399 if (buffer == NULL)
2400 return NULL;
2401
2402 buffer->resource = resource;
2403 wl_signal_init(&buffer->destroy_signal);
2404 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04002405 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002406 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002407
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002408 return buffer;
2409}
2410
2411static void
Pekka Paalanende685b82012-12-04 15:58:12 +02002412weston_buffer_reference_handle_destroy(struct wl_listener *listener,
2413 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002414{
Pekka Paalanende685b82012-12-04 15:58:12 +02002415 struct weston_buffer_reference *ref =
2416 container_of(listener, struct weston_buffer_reference,
2417 destroy_listener);
2418
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002419 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02002420 ref->buffer = NULL;
2421}
2422
2423WL_EXPORT void
2424weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002425 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002426{
2427 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05002428 ref->buffer->busy_count--;
2429 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002430 assert(wl_resource_get_client(ref->buffer->resource));
Matt Hoosier3052bc72017-09-26 08:09:40 -05002431 wl_buffer_send_release(ref->buffer->resource);
Kristian Høgsberg20347802013-03-04 12:07:46 -05002432 }
Pekka Paalanende685b82012-12-04 15:58:12 +02002433 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002434 }
2435
Pekka Paalanende685b82012-12-04 15:58:12 +02002436 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002437 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002438 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02002439 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02002440 }
2441
Pekka Paalanende685b82012-12-04 15:58:12 +02002442 ref->buffer = buffer;
2443 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
2444}
2445
2446static void
Alexandros Frantzis67629672018-10-19 12:14:11 +03002447weston_buffer_release_reference_handle_destroy(struct wl_listener *listener,
2448 void *data)
2449{
2450 struct weston_buffer_release_reference *ref =
2451 container_of(listener, struct weston_buffer_release_reference,
2452 destroy_listener);
2453
2454 assert((struct wl_resource *)data == ref->buffer_release->resource);
2455 ref->buffer_release = NULL;
2456}
2457
2458static void
2459weston_buffer_release_destroy(struct weston_buffer_release *buffer_release)
2460{
2461 struct wl_resource *resource = buffer_release->resource;
2462 int release_fence_fd = buffer_release->fence_fd;
2463
2464 if (release_fence_fd >= 0) {
2465 zwp_linux_buffer_release_v1_send_fenced_release(
2466 resource, release_fence_fd);
2467 } else {
2468 zwp_linux_buffer_release_v1_send_immediate_release(
2469 resource);
2470 }
2471
2472 wl_resource_destroy(resource);
2473}
2474
2475WL_EXPORT void
2476weston_buffer_release_reference(struct weston_buffer_release_reference *ref,
2477 struct weston_buffer_release *buffer_release)
2478{
2479 if (buffer_release == ref->buffer_release)
2480 return;
2481
2482 if (ref->buffer_release) {
2483 ref->buffer_release->ref_count--;
2484 wl_list_remove(&ref->destroy_listener.link);
2485 if (ref->buffer_release->ref_count == 0)
2486 weston_buffer_release_destroy(ref->buffer_release);
2487 }
2488
2489 if (buffer_release) {
2490 buffer_release->ref_count++;
2491 wl_resource_add_destroy_listener(buffer_release->resource,
2492 &ref->destroy_listener);
2493 }
2494
2495 ref->buffer_release = buffer_release;
2496 ref->destroy_listener.notify =
2497 weston_buffer_release_reference_handle_destroy;
2498}
2499
2500WL_EXPORT void
2501weston_buffer_release_move(struct weston_buffer_release_reference *dest,
2502 struct weston_buffer_release_reference *src)
2503{
2504 weston_buffer_release_reference(dest, src->buffer_release);
2505 weston_buffer_release_reference(src, NULL);
2506}
2507
2508static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002509weston_surface_attach(struct weston_surface *surface,
2510 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002511{
2512 weston_buffer_reference(&surface->buffer_ref, buffer);
2513
Pekka Paalanena6421c42012-12-04 15:58:10 +02002514 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002515 if (weston_surface_is_mapped(surface))
2516 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002517 }
2518
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002519 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02002520
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002521 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002522 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002523}
2524
Marius Vlad9fdda7f2019-06-11 16:08:55 +03002525/** weston_compositor_damage_all
2526 * \ingroup compositor
2527 */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002528WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002529weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002530{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002531 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002532
2533 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002534 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002535}
2536
Marius Vlad55d87362019-06-11 01:15:35 +03002537/**
2538 * \ingroup output
2539 */
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05002540WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002541weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002542{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002543 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002544
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002545 pixman_region32_union(&compositor->primary_plane.damage,
2546 &compositor->primary_plane.damage,
2547 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002548 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002549}
2550
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04002551static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002552surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002553{
Pekka Paalanende685b82012-12-04 15:58:12 +02002554 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002555 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002556 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002557
Marius Vlad2a1b7862019-09-05 13:12:18 +03002558 if (pixman_region32_not_empty(&surface->damage))
Marius Vlad3203ff62019-09-05 14:56:12 +03002559 TL_POINT(surface->compositor, "core_flush_damage", TLP_SURFACE(surface),
Pekka Paalanenb5026542014-11-12 15:09:24 +02002560 TLP_OUTPUT(surface->output), TLP_END);
2561
Jason Ekstrandef540082014-06-26 10:37:36 -07002562 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002563}
2564
2565static void
2566view_accumulate_damage(struct weston_view *view,
2567 pixman_region32_t *opaque)
2568{
2569 pixman_region32_t damage;
2570
2571 pixman_region32_init(&damage);
2572 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002573 pixman_box32_t *extents;
2574
Jason Ekstranda7af7042013-10-12 22:38:11 -05002575 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002576 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002577 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002578 pixman_region32_copy(&damage, &view->surface->damage);
2579 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002580 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002581 }
2582
Pekka Paalanen380adf52015-02-16 14:39:11 +02002583 pixman_region32_intersect(&damage, &damage,
2584 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002585 pixman_region32_subtract(&damage, &damage, opaque);
2586 pixman_region32_union(&view->plane->damage,
2587 &view->plane->damage, &damage);
2588 pixman_region32_fini(&damage);
2589 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002590 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002591}
2592
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002593static void
Michael Olbrichc5ea4952020-08-11 12:42:35 +02002594output_accumulate_damage(struct weston_output *output)
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002595{
Michael Olbrichc5ea4952020-08-11 12:42:35 +02002596 struct weston_compositor *ec = output->compositor;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002597 struct weston_plane *plane;
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002598 struct weston_paint_node *pnode;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002599 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002600
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002601 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002602
2603 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002604 pixman_region32_copy(&plane->clip, &clip);
2605
2606 pixman_region32_init(&opaque);
2607
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002608 wl_list_for_each(pnode, &output->paint_node_z_order_list,
2609 z_order_link) {
2610 if (pnode->view->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002611 continue;
2612
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002613 view_accumulate_damage(pnode->view, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002614 }
2615
2616 pixman_region32_union(&clip, &clip, &opaque);
2617 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002618 }
2619
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002620 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002621
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002622 wl_list_for_each(pnode, &output->paint_node_z_order_list,
2623 z_order_link) {
2624 pnode->surface->touched = false;
2625 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002626
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002627 wl_list_for_each(pnode, &output->paint_node_z_order_list,
2628 z_order_link) {
Michael Olbrichc5ea4952020-08-11 12:42:35 +02002629 /* Ignore views not visible on the current output */
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002630 /* TODO: turn this into assert once z_order_list is pruned. */
2631 if (!(pnode->view->output_mask & (1u << output->id)))
Michael Olbrichc5ea4952020-08-11 12:42:35 +02002632 continue;
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002633 if (pnode->surface->touched)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002634 continue;
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002635 pnode->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002636
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002637 surface_flush_damage(pnode->surface);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002638
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002639 /* Both the renderer and the backend have seen the buffer
2640 * by now. If renderer needs the buffer, it has its own
2641 * reference set. If the backend wants to keep the buffer
2642 * around for migrating the surface into a non-primary plane
2643 * later, keep_buffer is true. Otherwise, drop the core
2644 * reference now, and allow early buffer release. This enables
2645 * clients to use single-buffering.
2646 */
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002647 if (!pnode->surface->keep_buffer) {
2648 weston_buffer_reference(&pnode->surface->buffer_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002649 weston_buffer_release_reference(
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002650 &pnode->surface->buffer_release_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002651 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002652 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002653}
2654
2655static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002656surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002657{
2658 struct weston_subsurface *sub;
2659
Pekka Paalanene67858b2013-04-25 13:57:42 +03002660 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002661 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002662 continue;
2663
Jason Ekstranda7af7042013-10-12 22:38:11 -05002664 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2665 wl_list_init(&sub->surface->views);
2666
2667 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002668 }
2669}
2670
2671static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002672surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002673{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002674 struct weston_subsurface *sub;
2675 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002676
Jason Ekstranda7af7042013-10-12 22:38:11 -05002677 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2678 if (sub->surface == surface)
2679 continue;
2680
George Kiagiadakised04d382014-06-13 18:10:26 +02002681 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2682 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002683 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002684 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002685
2686 surface_free_unused_subsurface_views(sub->surface);
2687 }
2688}
2689
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002690static struct weston_paint_node *
2691view_ensure_paint_node(struct weston_view *view, struct weston_output *output)
2692{
2693 struct weston_paint_node *pnode;
2694
2695 if (!output)
2696 return NULL;
2697
2698 pnode = weston_view_find_paint_node(view, output);
2699 if (pnode)
2700 return pnode;
2701
2702 return weston_paint_node_create(view->surface, view, output);
2703}
2704
Jason Ekstranda7af7042013-10-12 22:38:11 -05002705static void
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002706add_to_z_order_list(struct weston_output *output,
2707 struct weston_paint_node *pnode)
2708{
2709 if (!pnode)
2710 return;
2711
2712 wl_list_remove(&pnode->z_order_link);
2713 wl_list_insert(output->paint_node_z_order_list.prev,
2714 &pnode->z_order_link);
Pekka Paalanen90a5ffa2021-02-25 12:03:28 +02002715
2716 /*
2717 * Building weston_output::paint_node_z_order_list ensures all
2718 * necessary color transform objects are installed.
2719 */
2720 weston_paint_node_ensure_color_transform(pnode);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002721}
2722
2723static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002724view_list_add_subsurface_view(struct weston_compositor *compositor,
2725 struct weston_subsurface *sub,
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002726 struct weston_view *parent,
2727 struct weston_output *output)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002728{
2729 struct weston_subsurface *child;
2730 struct weston_view *view = NULL, *iv;
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002731 struct weston_paint_node *pnode;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002732
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002733 if (!weston_surface_is_mapped(sub->surface))
2734 return;
2735
Jason Ekstranda7af7042013-10-12 22:38:11 -05002736 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2737 if (iv->geometry.parent == parent) {
2738 view = iv;
2739 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002740 }
2741 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002742
2743 if (view) {
2744 /* Put it back in the surface's list of views */
2745 wl_list_remove(&view->surface_link);
2746 wl_list_insert(&sub->surface->views, &view->surface_link);
2747 } else {
2748 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002749 weston_view_set_position(view,
2750 sub->position.x,
2751 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002752 weston_view_set_transform_parent(view, parent);
2753 }
2754
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002755 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002756 weston_view_update_transform(view);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002757 view->is_mapped = true;
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002758 pnode = view_ensure_paint_node(view, output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002759
Pekka Paalanenb188e912013-11-19 14:03:35 +02002760 if (wl_list_empty(&sub->surface->subsurface_list)) {
2761 wl_list_insert(compositor->view_list.prev, &view->link);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002762 add_to_z_order_list(output, pnode);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002763 return;
2764 }
2765
2766 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002767 if (child->surface == sub->surface) {
Pekka Paalanenb188e912013-11-19 14:03:35 +02002768 wl_list_insert(compositor->view_list.prev, &view->link);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002769 add_to_z_order_list(output, pnode);
2770 } else {
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002771 view_list_add_subsurface_view(compositor, child, view, output);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002772 }
Pekka Paalanenb188e912013-11-19 14:03:35 +02002773 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002774}
2775
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002776/* This recursively adds the sub-surfaces for a view, relying on the
2777 * sub-surface order. Thus, if a client restacks the sub-surfaces, that
2778 * change first happens to the sub-surface list, and then automatically
2779 * propagates here. See weston_surface_damage_subsurfaces() for how the
2780 * sub-surfaces receive damage when the client changes the state.
2781 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002782static void
2783view_list_add(struct weston_compositor *compositor,
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002784 struct weston_view *view,
2785 struct weston_output *output)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002786{
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002787 struct weston_paint_node *pnode;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002788 struct weston_subsurface *sub;
2789
2790 weston_view_update_transform(view);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002791 pnode = view_ensure_paint_node(view, output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002792
Pekka Paalanenb188e912013-11-19 14:03:35 +02002793 if (wl_list_empty(&view->surface->subsurface_list)) {
2794 wl_list_insert(compositor->view_list.prev, &view->link);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002795 add_to_z_order_list(output, pnode);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002796 return;
2797 }
2798
2799 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002800 if (sub->surface == view->surface) {
Pekka Paalanenb188e912013-11-19 14:03:35 +02002801 wl_list_insert(compositor->view_list.prev, &view->link);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002802 add_to_z_order_list(output, pnode);
2803 } else {
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002804 view_list_add_subsurface_view(compositor, sub, view, output);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002805 }
Pekka Paalanenb188e912013-11-19 14:03:35 +02002806 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002807}
2808
2809static void
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002810weston_compositor_build_view_list(struct weston_compositor *compositor,
2811 struct weston_output *output)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002812{
Loïc Yhuel267b16e2019-09-17 20:14:56 +02002813 struct weston_view *view, *tmp;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002814 struct weston_layer *layer;
2815
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002816 if (output) {
2817 wl_list_remove(&output->paint_node_z_order_list);
2818 wl_list_init(&output->paint_node_z_order_list);
2819 }
2820
Jason Ekstranda7af7042013-10-12 22:38:11 -05002821 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002822 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002823 surface_stash_subsurface_views(view->surface);
2824
Loïc Yhuel267b16e2019-09-17 20:14:56 +02002825 wl_list_for_each_safe(view, tmp, &compositor->view_list, link)
2826 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002827 wl_list_init(&compositor->view_list);
Loïc Yhuel267b16e2019-09-17 20:14:56 +02002828
Jason Ekstranda7af7042013-10-12 22:38:11 -05002829 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002830 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002831 view_list_add(compositor, view, output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002832 }
2833 }
2834
2835 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002836 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002837 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002838}
2839
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002840static void
2841weston_output_take_feedback_list(struct weston_output *output,
2842 struct weston_surface *surface)
2843{
2844 struct weston_view *view;
2845 struct weston_presentation_feedback *feedback;
2846 uint32_t flags = 0xffffffff;
2847
2848 if (wl_list_empty(&surface->feedback_list))
2849 return;
2850
2851 /* All views must have the flag for the flag to survive. */
2852 wl_list_for_each(view, &surface->views, surface_link) {
2853 /* ignore views that are not on this output at all */
2854 if (view->output_mask & (1u << output->id))
2855 flags &= view->psf_flags;
2856 }
2857
2858 wl_list_for_each(feedback, &surface->feedback_list, link)
2859 feedback->psf_flags = flags;
2860
2861 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2862 wl_list_init(&surface->feedback_list);
2863}
2864
David Herrmann1edf44c2013-10-22 17:11:26 +02002865static int
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002866weston_output_repaint(struct weston_output *output, void *repaint_data)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002867{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002868 struct weston_compositor *ec = output->compositor;
Pekka Paalanen6d6b3672021-05-03 14:06:55 +03002869 struct weston_paint_node *pnode;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002870 struct weston_animation *animation, *next;
Vlad Zahorodniic2b97472021-07-29 11:16:51 +03002871 struct wl_resource *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002872 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002873 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002874 int r;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002875 uint32_t frame_time_msec;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05302876 enum weston_hdcp_protection highest_requested = WESTON_HDCP_DISABLE;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002877
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002878 if (output->destroying)
2879 return 0;
2880
Marius Vlad3203ff62019-09-05 14:56:12 +03002881 TL_POINT(ec, "core_repaint_begin", TLP_OUTPUT(output), TLP_END);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002882
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002883 /* Rebuild the surface list and update surface transforms up front. */
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002884 weston_compositor_build_view_list(ec, output);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002885
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05302886 /* Find the highest protection desired for an output */
Pekka Paalanen6d6b3672021-05-03 14:06:55 +03002887 wl_list_for_each(pnode, &output->paint_node_z_order_list,
2888 z_order_link) {
2889 /* TODO: turn this into assert once z_order_list is pruned. */
2890 if ((pnode->surface->output_mask & (1u << output->id)) == 0)
2891 continue;
2892
2893 /*
2894 * The desired_protection of the output should be the
2895 * maximum of the desired_protection of the surfaces,
2896 * that are displayed on that output, to avoid
2897 * reducing the protection for existing surfaces.
2898 */
2899 if (pnode->surface->desired_protection > highest_requested)
2900 highest_requested = pnode->surface->desired_protection;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05302901 }
2902
2903 output->desired_protection = highest_requested;
2904
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002905 if (output->assign_planes && !output->disable_planes) {
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002906 output->assign_planes(output, repaint_data);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002907 } else {
Pekka Paalanen6d6b3672021-05-03 14:06:55 +03002908 wl_list_for_each(pnode, &output->paint_node_z_order_list,
2909 z_order_link) {
2910 weston_view_move_to_plane(pnode->view, &ec->primary_plane);
2911 pnode->view->psf_flags = 0;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002912 }
2913 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002914
Pekka Paalanene67858b2013-04-25 13:57:42 +03002915 wl_list_init(&frame_callback_list);
Pekka Paalanen6d6b3672021-05-03 14:06:55 +03002916 wl_list_for_each(pnode, &output->paint_node_z_order_list,
2917 z_order_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002918 /* Note: This operation is safe to do multiple times on the
2919 * same surface.
2920 */
Pekka Paalanen6d6b3672021-05-03 14:06:55 +03002921 if (pnode->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002922 wl_list_insert_list(&frame_callback_list,
Pekka Paalanen6d6b3672021-05-03 14:06:55 +03002923 &pnode->surface->frame_callback_list);
2924 wl_list_init(&pnode->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002925
Pekka Paalanen6d6b3672021-05-03 14:06:55 +03002926 weston_output_take_feedback_list(output, pnode->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002927 }
2928 }
2929
Michael Olbrichc5ea4952020-08-11 12:42:35 +02002930 output_accumulate_damage(output);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002931
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002932 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002933 pixman_region32_intersect(&output_damage,
2934 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002935 pixman_region32_subtract(&output_damage,
2936 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002937
Scott Moreauccbf29d2012-02-22 14:21:41 -07002938 if (output->dirty)
2939 weston_output_update_matrix(output);
2940
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002941 r = output->repaint(output, &output_damage, repaint_data);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002942
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002943 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002944
Daniel Stone09a97e22017-03-01 11:34:06 +00002945 output->repaint_needed = false;
Daniel Stone05df8c12017-03-03 16:59:42 +00002946 if (r == 0)
2947 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002948
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002949 weston_compositor_repick(ec);
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002950
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002951 frame_time_msec = timespec_to_msec(&output->frame_time);
2952
Vlad Zahorodniic2b97472021-07-29 11:16:51 +03002953 wl_resource_for_each_safe(cb, cnext, &frame_callback_list) {
2954 wl_callback_send_done(cb, frame_time_msec);
2955 wl_resource_destroy(cb);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002956 }
2957
Scott Moreaud64cf212012-06-08 19:40:54 -06002958 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002959 animation->frame_counter++;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002960 animation->frame(animation, output, &output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002961 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002962
Marius Vlad3203ff62019-09-05 14:56:12 +03002963 TL_POINT(ec, "core_repaint_posted", TLP_OUTPUT(output), TLP_END);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002964
David Herrmann1edf44c2013-10-22 17:11:26 +02002965 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002966}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002967
Pekka Paalanen82919792014-05-21 13:51:49 +03002968static void
2969weston_output_schedule_repaint_reset(struct weston_output *output)
2970{
Daniel Stone05df8c12017-03-03 16:59:42 +00002971 output->repaint_status = REPAINT_NOT_SCHEDULED;
Marius Vlad3203ff62019-09-05 14:56:12 +03002972 TL_POINT(output->compositor, "core_repaint_exit_loop",
2973 TLP_OUTPUT(output), TLP_END);
Pekka Paalanen82919792014-05-21 13:51:49 +03002974}
2975
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002976static int
2977weston_output_maybe_repaint(struct weston_output *output, struct timespec *now,
2978 void *repaint_data)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002979{
Pekka Paalanen0513a952014-05-21 16:17:27 +03002980 struct weston_compositor *compositor = output->compositor;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002981 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002982 int64_t msec_to_repaint;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002983
Daniel Stone6847b852017-03-01 11:34:08 +00002984 /* We're not ready yet; come back to make a decision later. */
2985 if (output->repaint_status != REPAINT_SCHEDULED)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002986 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002987
2988 msec_to_repaint = timespec_sub_to_msec(&output->next_repaint, now);
2989 if (msec_to_repaint > 1)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002990 return ret;
Daniel Stone05df8c12017-03-03 16:59:42 +00002991
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002992 /* If we're sleeping, drop the repaint machinery entirely; we will
2993 * explicitly repaint all outputs when we come back. */
2994 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2995 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
2996 goto err;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002997
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002998 /* We don't actually need to repaint this output; drop it from
2999 * repaint until something causes damage. */
3000 if (!output->repaint_needed)
3001 goto err;
3002
3003 /* If repaint fails, we aren't going to get weston_output_finish_frame
3004 * to trigger a new repaint, so drop it from repaint and hope
Daniel Stone6847b852017-03-01 11:34:08 +00003005 * something schedules a successful repaint later. As repainting may
3006 * take some time, re-read our clock as a courtesy to the next
3007 * output. */
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003008 ret = weston_output_repaint(output, repaint_data);
Daniel Stone6847b852017-03-01 11:34:08 +00003009 weston_compositor_read_presentation_clock(compositor, now);
Daniel Stonecd1a1c32017-01-16 15:38:54 +00003010 if (ret != 0)
3011 goto err;
3012
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09003013 output->repainted = true;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003014 return ret;
Daniel Stonecd1a1c32017-01-16 15:38:54 +00003015
3016err:
Pekka Paalanen0513a952014-05-21 16:17:27 +03003017 weston_output_schedule_repaint_reset(output);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003018 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00003019}
3020
3021static void
3022output_repaint_timer_arm(struct weston_compositor *compositor)
3023{
3024 struct weston_output *output;
3025 bool any_should_repaint = false;
3026 struct timespec now;
Sergi Granellb4c08862017-03-18 13:01:15 +01003027 int64_t msec_to_next = INT64_MAX;
Daniel Stone6847b852017-03-01 11:34:08 +00003028
3029 weston_compositor_read_presentation_clock(compositor, &now);
3030
3031 wl_list_for_each(output, &compositor->output_list, link) {
3032 int64_t msec_to_this;
3033
3034 if (output->repaint_status != REPAINT_SCHEDULED)
3035 continue;
3036
3037 msec_to_this = timespec_sub_to_msec(&output->next_repaint,
3038 &now);
3039 if (!any_should_repaint || msec_to_this < msec_to_next)
3040 msec_to_next = msec_to_this;
3041
3042 any_should_repaint = true;
3043 }
3044
3045 if (!any_should_repaint)
3046 return;
3047
3048 /* Even if we should repaint immediately, add the minimum 1 ms delay.
3049 * This is a workaround to allow coalescing multiple output repaints
3050 * particularly from weston_output_finish_frame()
3051 * into the same call, which would not happen if we called
3052 * output_repaint_timer_handler() directly.
3053 */
3054 if (msec_to_next < 1)
3055 msec_to_next = 1;
3056
3057 wl_event_source_timer_update(compositor->repaint_timer, msec_to_next);
3058}
3059
3060static int
3061output_repaint_timer_handler(void *data)
3062{
3063 struct weston_compositor *compositor = data;
3064 struct weston_output *output;
3065 struct timespec now;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003066 void *repaint_data = NULL;
Emre Ucane479ed82018-03-20 15:29:40 +01003067 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00003068
3069 weston_compositor_read_presentation_clock(compositor, &now);
Pekka Paalanen1ed2cad2021-02-10 12:33:03 +02003070 compositor->last_repaint_start = now;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003071
3072 if (compositor->backend->repaint_begin)
3073 repaint_data = compositor->backend->repaint_begin(compositor);
3074
3075 wl_list_for_each(output, &compositor->output_list, link) {
3076 ret = weston_output_maybe_repaint(output, &now, repaint_data);
3077 if (ret)
3078 break;
3079 }
3080
3081 if (ret == 0) {
Tomohito Esaki09bfcd62018-06-05 10:37:05 +09003082 if (compositor->backend->repaint_flush)
Antonio Borneoc90fccc2019-06-30 15:51:10 +02003083 ret = compositor->backend->repaint_flush(compositor,
3084 repaint_data);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003085 } else {
Antonio Borneoc90fccc2019-06-30 15:51:10 +02003086 if (compositor->backend->repaint_cancel)
3087 compositor->backend->repaint_cancel(compositor,
3088 repaint_data);
3089 }
3090
3091 if (ret != 0) {
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09003092 wl_list_for_each(output, &compositor->output_list, link) {
3093 if (output->repainted)
3094 weston_output_schedule_repaint_reset(output);
3095 }
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003096 }
Daniel Stone6847b852017-03-01 11:34:08 +00003097
Tomohito Esakiddaf95c2018-07-10 11:47:15 +09003098 wl_list_for_each(output, &compositor->output_list, link)
3099 output->repainted = false;
3100
Daniel Stone6847b852017-03-01 11:34:08 +00003101 output_repaint_timer_arm(compositor);
3102
Pekka Paalanen0513a952014-05-21 16:17:27 +03003103 return 0;
3104}
3105
Pekka Paalanen50aa3a72020-05-28 11:34:04 +03003106/** Convert a presentation timestamp to another clock domain
3107 *
3108 * \param compositor The compositor defines the presentation clock domain.
3109 * \param presentation_stamp The timestamp in presentation clock domain.
3110 * \param presentation_now Current time in presentation clock domain.
3111 * \param target_clock Defines the target clock domain.
3112 *
3113 * This approximation relies on presentation_stamp to be close to current time.
3114 * The further it is from current time and the bigger the speed difference
3115 * between the two clock domains, the bigger the conversion error.
3116 *
3117 * Conversion error due to system load is biased and unbounded.
3118 */
3119static struct timespec
3120convert_presentation_time_now(struct weston_compositor *compositor,
3121 const struct timespec *presentation_stamp,
3122 const struct timespec *presentation_now,
3123 clockid_t target_clock)
3124{
3125 struct timespec target_now = {};
3126 struct timespec target_stamp;
3127 int64_t delta_ns;
3128
3129 if (compositor->presentation_clock == target_clock)
3130 return *presentation_stamp;
3131
3132 clock_gettime(target_clock, &target_now);
3133 delta_ns = timespec_sub_to_nsec(presentation_stamp, presentation_now);
3134 timespec_add_nsec(&target_stamp, &target_now, delta_ns);
3135
3136 return target_stamp;
3137}
3138
Marius Vlad55d87362019-06-11 01:15:35 +03003139/**
3140 * \ingroup output
3141 */
Kristian Høgsbergef044142011-06-21 15:02:12 -04003142WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04003143weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02003144 const struct timespec *stamp,
3145 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04003146{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003147 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03003148 int32_t refresh_nsec;
3149 struct timespec now;
Pekka Paalanen50aa3a72020-05-28 11:34:04 +03003150 struct timespec vblank_monotonic;
Daniel Stone6847b852017-03-01 11:34:08 +00003151 int64_t msec_rel;
Pekka Paalanen133e4392014-09-23 22:08:46 -04003152
Daniel Stone05df8c12017-03-03 16:59:42 +00003153 assert(output->repaint_status == REPAINT_AWAITING_COMPLETION);
Pekka Paalanen98b4e202021-05-10 11:33:23 +03003154
3155 /*
3156 * If timestamp of latest vblank is given, it must always go forwards.
3157 * If not given, INVALID flag must be set.
3158 */
3159 if (stamp)
3160 assert(timespec_sub_to_nsec(stamp, &output->frame_time) >= 0);
3161 else
3162 assert(presented_flags & WP_PRESENTATION_FEEDBACK_INVALID);
Daniel Stone3615ce12017-03-01 11:34:05 +00003163
Daniel Stone6847b852017-03-01 11:34:08 +00003164 weston_compositor_read_presentation_clock(compositor, &now);
3165
Daniel Stone3615ce12017-03-01 11:34:05 +00003166 /* If we haven't been supplied any timestamp at all, we don't have a
3167 * timebase to work against, so any delay just wastes time. Push a
3168 * repaint as soon as possible so we can get on with it. */
Daniel Stone6847b852017-03-01 11:34:08 +00003169 if (!stamp) {
3170 output->next_repaint = now;
Daniel Stone3615ce12017-03-01 11:34:05 +00003171 goto out;
Daniel Stone6847b852017-03-01 11:34:08 +00003172 }
Daniel Stone3615ce12017-03-01 11:34:05 +00003173
Pekka Paalanen50aa3a72020-05-28 11:34:04 +03003174 vblank_monotonic = convert_presentation_time_now(compositor,
3175 stamp, &now,
3176 CLOCK_MONOTONIC);
Marius Vlad3203ff62019-09-05 14:56:12 +03003177 TL_POINT(compositor, "core_repaint_finished", TLP_OUTPUT(output),
Pekka Paalanen50aa3a72020-05-28 11:34:04 +03003178 TLP_VBLANK(&vblank_monotonic), TLP_END);
Marius Vladdf9278a2018-03-06 18:56:23 +02003179
Pekka Paalanend7894d02015-07-03 15:08:53 +03003180 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003181 weston_presentation_feedback_present_list(&output->feedback_list,
3182 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02003183 output->msc,
3184 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003185
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02003186 output->frame_time = *stamp;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003187
Daniel Stone6847b852017-03-01 11:34:08 +00003188 timespec_add_nsec(&output->next_repaint, stamp, refresh_nsec);
3189 timespec_add_msec(&output->next_repaint, &output->next_repaint,
3190 -compositor->repaint_msec);
3191 msec_rel = timespec_sub_to_msec(&output->next_repaint, &now);
Daniel Stone84aff5c2017-03-01 11:34:04 +00003192
3193 if (msec_rel < -1000 || msec_rel > 1000) {
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02003194 static bool warned;
3195
3196 if (!warned)
3197 weston_log("Warning: computed repaint delay is "
Daniel Stone6847b852017-03-01 11:34:08 +00003198 "insane: %lld msec\n", (long long) msec_rel);
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02003199 warned = true;
3200
Daniel Stone6847b852017-03-01 11:34:08 +00003201 output->next_repaint = now;
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02003202 }
3203
Mario Kleinerb7df04e2015-06-21 21:25:15 +02003204 /* Called from restart_repaint_loop and restart happens already after
3205 * the deadline given by repaint_msec? In that case we delay until
3206 * the deadline of the next frame, to give clients a more predictable
3207 * timing of the repaint cycle to lock on. */
Daniel Stoneeca5cca2017-02-28 21:53:51 +00003208 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID &&
3209 msec_rel < 0) {
3210 while (timespec_sub_to_nsec(&output->next_repaint, &now) < 0) {
3211 timespec_add_nsec(&output->next_repaint,
3212 &output->next_repaint,
3213 refresh_nsec);
3214 }
3215 }
Mario Kleinerb7df04e2015-06-21 21:25:15 +02003216
Daniel Stone3615ce12017-03-01 11:34:05 +00003217out:
Daniel Stone05df8c12017-03-03 16:59:42 +00003218 output->repaint_status = REPAINT_SCHEDULED;
Daniel Stone6847b852017-03-01 11:34:08 +00003219 output_repaint_timer_arm(compositor);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003220}
3221
nerdopolisd2a320d2021-08-23 21:29:42 -04003222
3223WL_EXPORT void
3224weston_output_repaint_failed(struct weston_output *output)
3225{
3226 weston_log("Clearing repaint status.\n");
3227 assert(output->repaint_status == REPAINT_AWAITING_COMPLETION);
3228 output->repaint_status = REPAINT_NOT_SCHEDULED;
3229}
3230
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003231static void
3232idle_repaint(void *data)
3233{
3234 struct weston_output *output = data;
Antonio Borneoc90fccc2019-06-30 15:51:10 +02003235 int ret;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003236
Daniel Stone05df8c12017-03-03 16:59:42 +00003237 assert(output->repaint_status == REPAINT_BEGIN_FROM_IDLE);
3238 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03003239 output->idle_repaint_source = NULL;
Antonio Borneoc90fccc2019-06-30 15:51:10 +02003240 ret = output->start_repaint_loop(output);
3241 if (ret != 0)
3242 weston_output_schedule_repaint_reset(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003243}
3244
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003245WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003246weston_layer_entry_insert(struct weston_layer_entry *list,
3247 struct weston_layer_entry *entry)
3248{
3249 wl_list_insert(&list->link, &entry->link);
3250 entry->layer = list->layer;
3251}
3252
3253WL_EXPORT void
3254weston_layer_entry_remove(struct weston_layer_entry *entry)
3255{
3256 wl_list_remove(&entry->link);
3257 wl_list_init(&entry->link);
3258 entry->layer = NULL;
3259}
3260
Quentin Glidic82681572016-12-17 13:40:51 +01003261
3262/** Initialize the weston_layer struct.
3263 *
3264 * \param compositor The compositor instance
3265 * \param layer The layer to initialize
3266 */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003267WL_EXPORT void
Quentin Glidic82681572016-12-17 13:40:51 +01003268weston_layer_init(struct weston_layer *layer,
3269 struct weston_compositor *compositor)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003270{
Quentin Glidic82681572016-12-17 13:40:51 +01003271 layer->compositor = compositor;
3272 wl_list_init(&layer->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003273 wl_list_init(&layer->view_list.link);
3274 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003275 weston_layer_set_mask_infinite(layer);
Quentin Glidic82681572016-12-17 13:40:51 +01003276}
3277
Pekka Paalanen87400372021-05-14 14:29:40 +03003278/** Finalize the weston_layer struct.
3279 *
3280 * \param layer The layer to finalize.
3281 */
3282WL_EXPORT void
3283weston_layer_fini(struct weston_layer *layer)
3284{
3285 wl_list_remove(&layer->link);
3286
3287 if (!wl_list_empty(&layer->view_list.link))
3288 weston_log("BUG: finalizing a layer with views still on it.\n");
3289
3290 wl_list_remove(&layer->view_list.link);
3291}
3292
Quentin Glidic82681572016-12-17 13:40:51 +01003293/** Sets the position of the layer in the layer list. The layer will be placed
3294 * below any layer with the same position value, if any.
3295 * This function is safe to call if the layer is already on the list, but the
3296 * layer may be moved below other layers at the same position, if any.
3297 *
3298 * \param layer The layer to modify
3299 * \param position The position the layer will be placed at
3300 */
3301WL_EXPORT void
3302weston_layer_set_position(struct weston_layer *layer,
3303 enum weston_layer_position position)
3304{
3305 struct weston_layer *below;
3306
3307 wl_list_remove(&layer->link);
3308
3309 /* layer_list is ordered from top to bottom, the last layer being the
3310 * background with the smallest position value */
3311
3312 layer->position = position;
3313 wl_list_for_each_reverse(below, &layer->compositor->layer_list, link) {
3314 if (below->position >= layer->position) {
3315 wl_list_insert(&below->link, &layer->link);
3316 return;
3317 }
3318 }
3319 wl_list_insert(&layer->compositor->layer_list, &layer->link);
3320}
3321
3322/** Hide a layer by taking it off the layer list.
3323 * This function is safe to call if the layer is not on the list.
3324 *
3325 * \param layer The layer to hide
3326 */
3327WL_EXPORT void
3328weston_layer_unset_position(struct weston_layer *layer)
3329{
3330 wl_list_remove(&layer->link);
3331 wl_list_init(&layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003332}
3333
3334WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003335weston_layer_set_mask(struct weston_layer *layer,
3336 int x, int y, int width, int height)
3337{
3338 struct weston_view *view;
3339
3340 layer->mask.x1 = x;
3341 layer->mask.x2 = x + width;
3342 layer->mask.y1 = y;
3343 layer->mask.y2 = y + height;
3344
3345 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
3346 weston_view_geometry_dirty(view);
3347 }
3348}
3349
3350WL_EXPORT void
3351weston_layer_set_mask_infinite(struct weston_layer *layer)
3352{
Adam Jackson3c3f3b12019-10-16 16:02:59 -04003353 struct weston_view *view;
3354
3355 layer->mask.x1 = INT32_MIN;
3356 layer->mask.x2 = INT32_MAX;
3357 layer->mask.y1 = INT32_MIN;
3358 layer->mask.y2 = INT32_MAX;
3359
3360 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
3361 weston_view_geometry_dirty(view);
3362 }
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003363}
3364
Daniel Stone3b775632018-07-20 08:38:25 +01003365WL_EXPORT bool
3366weston_layer_mask_is_infinite(struct weston_layer *layer)
3367{
3368 return layer->mask.x1 == INT32_MIN &&
3369 layer->mask.y1 == INT32_MIN &&
Adam Jackson3c3f3b12019-10-16 16:02:59 -04003370 layer->mask.x2 == INT32_MAX &&
3371 layer->mask.y2 == INT32_MAX;
Daniel Stone3b775632018-07-20 08:38:25 +01003372}
3373
Marius Vlad55d87362019-06-11 01:15:35 +03003374/**
3375 * \ingroup output
3376 */
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003377WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003378weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003379{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003380 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04003381 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01003382
Bryce Harrington08976ac2016-08-30 12:05:16 -07003383 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
3384 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003385 return;
3386
Pekka Paalanenb5026542014-11-12 15:09:24 +02003387 if (!output->repaint_needed)
Marius Vlad3203ff62019-09-05 14:56:12 +03003388 TL_POINT(compositor, "core_repaint_req", TLP_OUTPUT(output), TLP_END);
Pekka Paalanenb5026542014-11-12 15:09:24 +02003389
Kristian Høgsbergef044142011-06-21 15:02:12 -04003390 loop = wl_display_get_event_loop(compositor->wl_display);
Daniel Stone09a97e22017-03-01 11:34:06 +00003391 output->repaint_needed = true;
Daniel Stone05df8c12017-03-03 16:59:42 +00003392
3393 /* If we already have a repaint scheduled for our idle handler,
3394 * no need to set it again. If the repaint has been called but
3395 * not finished, then weston_output_finish_frame() will notice
3396 * that a repaint is needed and schedule one. */
3397 if (output->repaint_status != REPAINT_NOT_SCHEDULED)
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003398 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01003399
Daniel Stone05df8c12017-03-03 16:59:42 +00003400 output->repaint_status = REPAINT_BEGIN_FROM_IDLE;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03003401 assert(!output->idle_repaint_source);
3402 output->idle_repaint_source = wl_event_loop_add_idle(loop, idle_repaint,
3403 output);
Marius Vlad3203ff62019-09-05 14:56:12 +03003404 TL_POINT(compositor, "core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003405}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05003406
Marius Vlad9fdda7f2019-06-11 16:08:55 +03003407/** weston_compositor_schedule_repaint
3408 * \ingroup compositor
3409 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003410WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003411weston_compositor_schedule_repaint(struct weston_compositor *compositor)
3412{
3413 struct weston_output *output;
3414
3415 wl_list_for_each(output, &compositor->output_list, link)
3416 weston_output_schedule_repaint(output);
3417}
3418
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003419static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003420surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003421{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003422 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003423}
3424
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003425static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003426surface_attach(struct wl_client *client,
3427 struct wl_resource *resource,
3428 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
3429{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003430 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05003431 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003432
Kristian Høgsbergab19f932013-08-20 11:30:54 -07003433 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05003434 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07003435 if (buffer == NULL) {
3436 wl_client_post_no_memory(client);
3437 return;
3438 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07003439 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04003440
Pekka Paalanende685b82012-12-04 15:58:12 +02003441 /* Attach, attach, without commit in between does not send
3442 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003443 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003444
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003445 surface->pending.sx = sx;
3446 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01003447 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003448}
3449
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003450static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003451surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003452 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003453 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05003454{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003455 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003456
Derek Foreman57e92ed2015-11-17 14:11:35 -06003457 if (width <= 0 || height <= 0)
3458 return;
3459
Derek Foreman152254b2015-11-26 14:17:48 -06003460 pixman_region32_union_rect(&surface->pending.damage_surface,
3461 &surface->pending.damage_surface,
3462 x, y, width, height);
3463}
3464
3465static void
3466surface_damage_buffer(struct wl_client *client,
3467 struct wl_resource *resource,
3468 int32_t x, int32_t y, int32_t width, int32_t height)
3469{
3470 struct weston_surface *surface = wl_resource_get_user_data(resource);
3471
3472 if (width <= 0 || height <= 0)
3473 return;
3474
3475 pixman_region32_union_rect(&surface->pending.damage_buffer,
3476 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04003477 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003478}
3479
Kristian Høgsberg33418202011-08-16 23:01:28 -04003480static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003481destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04003482{
Vlad Zahorodniic2b97472021-07-29 11:16:51 +03003483 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsberg33418202011-08-16 23:01:28 -04003484}
3485
3486static void
3487surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003488 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04003489{
Vlad Zahorodniic2b97472021-07-29 11:16:51 +03003490 struct wl_resource *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003491 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003492
Vlad Zahorodniic2b97472021-07-29 11:16:51 +03003493 cb = wl_resource_create(client, &wl_callback_interface, 1, callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003494 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003495 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003496 return;
3497 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03003498
Vlad Zahorodniic2b97472021-07-29 11:16:51 +03003499 wl_resource_set_implementation(cb, NULL, NULL,
Jason Ekstranda85118c2013-06-27 20:17:02 -05003500 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003501
Vlad Zahorodniic2b97472021-07-29 11:16:51 +03003502 wl_list_insert(surface->pending.frame_callback_list.prev,
3503 wl_resource_get_link(cb));
Kristian Høgsberg33418202011-08-16 23:01:28 -04003504}
3505
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003506static void
3507surface_set_opaque_region(struct wl_client *client,
3508 struct wl_resource *resource,
3509 struct wl_resource *region_resource)
3510{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003511 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003512 struct weston_region *region;
3513
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003514 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003515 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003516 pixman_region32_copy(&surface->pending.opaque,
3517 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003518 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07003519 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003520 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003521}
3522
3523static void
3524surface_set_input_region(struct wl_client *client,
3525 struct wl_resource *resource,
3526 struct wl_resource *region_resource)
3527{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003528 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003529 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003530
3531 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003532 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003533 pixman_region32_copy(&surface->pending.input,
3534 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003535 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003536 pixman_region32_fini(&surface->pending.input);
3537 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003538 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003539}
3540
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003541/* Cause damage to this sub-surface and all its children.
3542 *
3543 * This is useful when there are state changes that need an implicit
3544 * damage, e.g. a z-order change.
3545 */
3546static void
3547weston_surface_damage_subsurfaces(struct weston_subsurface *sub)
3548{
3549 struct weston_subsurface *child;
3550
3551 weston_surface_damage(sub->surface);
3552 sub->reordered = false;
3553
3554 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link)
3555 if (child != sub)
3556 weston_surface_damage_subsurfaces(child);
3557}
3558
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003559static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003560weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003561{
Pekka Paalanene67858b2013-04-25 13:57:42 +03003562 struct weston_subsurface *sub;
3563
3564 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
3565 parent_link_pending) {
3566 wl_list_remove(&sub->parent_link);
3567 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003568
3569 if (sub->reordered)
3570 weston_surface_damage_subsurfaces(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003571 }
3572}
3573
3574static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03003575weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05003576 struct weston_matrix *matrix)
3577{
Pekka Paalanen04baea52016-04-26 15:50:58 +03003578 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05003579 double src_width, src_height, dest_width, dest_height;
3580
3581 weston_matrix_init(matrix);
3582
3583 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
3584 src_width = surface->width_from_buffer;
3585 src_height = surface->height_from_buffer;
3586 } else {
3587 src_width = wl_fixed_to_double(vp->buffer.src_width);
3588 src_height = wl_fixed_to_double(vp->buffer.src_height);
3589 }
3590
3591 if (vp->surface.width == -1) {
3592 dest_width = src_width;
3593 dest_height = src_height;
3594 } else {
3595 dest_width = vp->surface.width;
3596 dest_height = vp->surface.height;
3597 }
3598
3599 if (src_width != dest_width || src_height != dest_height)
3600 weston_matrix_scale(matrix,
3601 src_width / dest_width,
3602 src_height / dest_height, 1);
3603
3604 if (vp->buffer.src_width != wl_fixed_from_int(-1))
3605 weston_matrix_translate(matrix,
3606 wl_fixed_to_double(vp->buffer.src_x),
3607 wl_fixed_to_double(vp->buffer.src_y),
3608 0);
3609
3610 switch (vp->buffer.transform) {
3611 case WL_OUTPUT_TRANSFORM_FLIPPED:
3612 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3613 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3614 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3615 weston_matrix_scale(matrix, -1, 1, 1);
3616 weston_matrix_translate(matrix,
3617 surface->width_from_buffer, 0, 0);
3618 break;
3619 }
3620
3621 switch (vp->buffer.transform) {
3622 default:
3623 case WL_OUTPUT_TRANSFORM_NORMAL:
3624 case WL_OUTPUT_TRANSFORM_FLIPPED:
3625 break;
3626 case WL_OUTPUT_TRANSFORM_90:
3627 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +02003628 weston_matrix_rotate_xy(matrix, 0, -1);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003629 weston_matrix_translate(matrix,
Pekka Paalanen8060d822020-02-06 15:27:54 +02003630 0, surface->width_from_buffer, 0);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003631 break;
3632 case WL_OUTPUT_TRANSFORM_180:
3633 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3634 weston_matrix_rotate_xy(matrix, -1, 0);
3635 weston_matrix_translate(matrix,
3636 surface->width_from_buffer,
3637 surface->height_from_buffer, 0);
3638 break;
3639 case WL_OUTPUT_TRANSFORM_270:
3640 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +02003641 weston_matrix_rotate_xy(matrix, 0, 1);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003642 weston_matrix_translate(matrix,
Pekka Paalanen8060d822020-02-06 15:27:54 +02003643 surface->height_from_buffer, 0, 0);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003644 break;
3645 }
3646
3647 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
3648}
3649
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003650/**
3651 * Compute a + b > c while being safe to overflows.
3652 */
3653static bool
3654fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
3655{
3656 return (int64_t)a + (int64_t)b > (int64_t)c;
3657}
3658
3659static bool
3660weston_surface_is_pending_viewport_source_valid(
3661 const struct weston_surface *surface)
3662{
3663 const struct weston_surface_state *pend = &surface->pending;
3664 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
3665 int width_from_buffer = 0;
3666 int height_from_buffer = 0;
3667 wl_fixed_t w;
3668 wl_fixed_t h;
3669
3670 /* If viewport source rect is not set, it is always ok. */
3671 if (vp->buffer.src_width == wl_fixed_from_int(-1))
3672 return true;
3673
3674 if (pend->newly_attached) {
3675 if (pend->buffer) {
3676 convert_size_by_transform_scale(&width_from_buffer,
3677 &height_from_buffer,
3678 pend->buffer->width,
3679 pend->buffer->height,
3680 vp->buffer.transform,
3681 vp->buffer.scale);
3682 } else {
3683 /* No buffer: viewport is irrelevant. */
3684 return true;
3685 }
3686 } else {
3687 width_from_buffer = surface->width_from_buffer;
3688 height_from_buffer = surface->height_from_buffer;
3689 }
3690
3691 assert((width_from_buffer == 0) == (height_from_buffer == 0));
3692 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
3693
3694 /* No buffer: viewport is irrelevant. */
3695 if (width_from_buffer == 0 || height_from_buffer == 0)
3696 return true;
3697
3698 /* overflow checks for wl_fixed_from_int() */
3699 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
3700 return false;
3701 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
3702 return false;
3703
3704 w = wl_fixed_from_int(width_from_buffer);
3705 h = wl_fixed_from_int(height_from_buffer);
3706
3707 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
3708 return false;
3709 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
3710 return false;
3711
3712 return true;
3713}
3714
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003715static bool
3716fixed_is_integer(wl_fixed_t v)
3717{
3718 return (v & 0xff) == 0;
3719}
3720
3721static bool
3722weston_surface_is_pending_viewport_dst_size_int(
3723 const struct weston_surface *surface)
3724{
3725 const struct weston_buffer_viewport *vp =
3726 &surface->pending.buffer_viewport;
3727
3728 if (vp->surface.width != -1) {
3729 assert(vp->surface.width > 0 && vp->surface.height > 0);
3730 return true;
3731 }
3732
3733 return fixed_is_integer(vp->buffer.src_width) &&
3734 fixed_is_integer(vp->buffer.src_height);
3735}
3736
Derek Foreman152254b2015-11-26 14:17:48 -06003737/* Translate pending damage in buffer co-ordinates to surface
3738 * co-ordinates and union it with a pixman_region32_t.
3739 * This should only be called after the buffer is attached.
3740 */
3741static void
3742apply_damage_buffer(pixman_region32_t *dest,
3743 struct weston_surface *surface,
3744 struct weston_surface_state *state)
3745{
3746 struct weston_buffer *buffer = surface->buffer_ref.buffer;
3747
3748 /* wl_surface.damage_buffer needs to be clipped to the buffer,
3749 * translated into surface co-ordinates and unioned with
3750 * any other surface damage.
3751 * None of this makes sense if there is no buffer though.
3752 */
3753 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
3754 pixman_region32_t buffer_damage;
3755
3756 pixman_region32_intersect_rect(&state->damage_buffer,
3757 &state->damage_buffer,
3758 0, 0, buffer->width,
3759 buffer->height);
3760 pixman_region32_init(&buffer_damage);
3761 weston_matrix_transform_region(&buffer_damage,
3762 &surface->buffer_to_surface_matrix,
3763 &state->damage_buffer);
3764 pixman_region32_union(dest, dest, &buffer_damage);
3765 pixman_region32_fini(&buffer_damage);
3766 }
3767 /* We should clear this on commit even if there was no buffer */
3768 pixman_region32_clear(&state->damage_buffer);
3769}
3770
Jason Ekstrand1e059042014-10-16 10:55:19 -05003771static void
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303772weston_surface_set_desired_protection(struct weston_surface *surface,
3773 enum weston_hdcp_protection protection)
3774{
3775 if (surface->desired_protection == protection)
3776 return;
3777 surface->desired_protection = protection;
3778 weston_surface_damage(surface);
3779}
3780
3781static void
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303782weston_surface_set_protection_mode(struct weston_surface *surface,
3783 enum weston_surface_protection_mode p_mode)
3784{
3785 struct content_protection *cp = surface->compositor->content_protection;
3786 struct protected_surface *psurface;
3787
3788 surface->protection_mode = p_mode;
3789 wl_list_for_each(psurface, &cp->protected_list, link) {
3790 if (!psurface || psurface->surface != surface)
3791 continue;
3792 weston_protected_surface_send_event(psurface,
3793 surface->current_protection);
3794 }
3795}
3796
3797static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05003798weston_surface_commit_state(struct weston_surface *surface,
3799 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003800{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003801 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003802 pixman_region32_t opaque;
3803
Alexander Larsson4ea95522013-05-22 14:41:37 +02003804 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02003805 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03003806 /* wp_viewport.set_source */
3807 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003808 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003809
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003810 /* wl_surface.attach */
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003811 if (state->newly_attached) {
3812 /* zwp_surface_synchronization_v1.set_acquire_fence */
3813 fd_move(&surface->acquire_fence_fd,
3814 &state->acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003815 /* zwp_surface_synchronization_v1.get_release */
3816 weston_buffer_release_move(&surface->buffer_release_ref,
3817 &state->buffer_release_ref);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003818 weston_surface_attach(surface, state->buffer);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003819 }
Jason Ekstrand7b982072014-05-20 14:33:03 -05003820 weston_surface_state_set_buffer(state, NULL);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003821 assert(state->acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003822 assert(state->buffer_release_ref.buffer_release == NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01003823
Jason Ekstrand1e059042014-10-16 10:55:19 -05003824 weston_surface_build_buffer_matrix(surface,
3825 &surface->surface_to_buffer_matrix);
3826 weston_matrix_invert(&surface->buffer_to_surface_matrix,
3827 &surface->surface_to_buffer_matrix);
3828
Jason Ekstrand7b982072014-05-20 14:33:03 -05003829 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003830 weston_surface_update_size(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003831 if (surface->committed)
3832 surface->committed(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003833 }
Giulio Camuffo184df502013-02-21 11:29:21 +01003834
Jason Ekstrand7b982072014-05-20 14:33:03 -05003835 state->sx = 0;
3836 state->sy = 0;
3837 state->newly_attached = 0;
3838 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03003839
Derek Foreman152254b2015-11-26 14:17:48 -06003840 /* wl_surface.damage and wl_surface.damage_buffer */
Marius Vlad2a1b7862019-09-05 13:12:18 +03003841 if (pixman_region32_not_empty(&state->damage_surface) ||
3842 pixman_region32_not_empty(&state->damage_buffer))
Marius Vlad3203ff62019-09-05 14:56:12 +03003843 TL_POINT(surface->compositor, "core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06003844
Pekka Paalanen8e159182012-10-10 12:49:25 +03003845 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06003846 &state->damage_surface);
3847
3848 apply_damage_buffer(&surface->damage, surface, state);
3849
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05003850 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07003851 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06003852 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003853
3854 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003855 pixman_region32_init(&opaque);
3856 pixman_region32_intersect_rect(&opaque, &state->opaque,
3857 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003858
3859 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
3860 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003861 wl_list_for_each(view, &surface->views, surface_link)
3862 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003863 }
3864
3865 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003866
3867 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003868 pixman_region32_intersect_rect(&surface->input, &state->input,
3869 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003870
Pekka Paalanenbc106382012-10-10 12:49:31 +03003871 /* wl_surface.frame */
3872 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05003873 &state->frame_callback_list);
3874 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003875
3876 /* XXX:
3877 * What should happen with a feedback request, if there
3878 * is no wl_buffer attached for this commit?
3879 */
3880
3881 /* presentation.feedback */
3882 wl_list_insert_list(&surface->feedback_list,
3883 &state->feedback_list);
3884 wl_list_init(&state->feedback_list);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003885
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303886 /* weston_protected_surface.enforced/relaxed */
3887 if (surface->protection_mode != state->protection_mode)
3888 weston_surface_set_protection_mode(surface,
3889 state->protection_mode);
3890
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303891 /* weston_protected_surface.set_type */
3892 weston_surface_set_desired_protection(surface, state->desired_protection);
3893
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003894 wl_signal_emit(&surface->commit_signal, surface);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003895}
3896
3897static void
3898weston_surface_commit(struct weston_surface *surface)
3899{
3900 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003901
Pekka Paalanene67858b2013-04-25 13:57:42 +03003902 weston_surface_commit_subsurface_order(surface);
3903
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003904 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003905}
3906
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003907static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003908weston_subsurface_commit(struct weston_subsurface *sub);
3909
3910static void
3911weston_subsurface_parent_commit(struct weston_subsurface *sub,
3912 int parent_is_synchronized);
3913
3914static void
3915surface_commit(struct wl_client *client, struct wl_resource *resource)
3916{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003917 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003918 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
3919
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003920 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
3921 assert(surface->viewport_resource);
3922
3923 wl_resource_post_error(surface->viewport_resource,
3924 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
3925 "wl_surface@%d has viewport source outside buffer",
3926 wl_resource_get_id(resource));
3927 return;
3928 }
3929
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003930 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
3931 assert(surface->viewport_resource);
3932
3933 wl_resource_post_error(surface->viewport_resource,
3934 WP_VIEWPORT_ERROR_BAD_SIZE,
3935 "wl_surface@%d viewport dst size not integer",
3936 wl_resource_get_id(resource));
3937 return;
3938 }
3939
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003940 if (surface->pending.acquire_fence_fd >= 0) {
3941 assert(surface->synchronization_resource);
3942
3943 if (!surface->pending.buffer) {
3944 fd_clear(&surface->pending.acquire_fence_fd);
3945 wl_resource_post_error(surface->synchronization_resource,
3946 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3947 "wl_surface@%"PRIu32" no buffer for synchronization",
3948 wl_resource_get_id(resource));
3949 return;
3950 }
3951
3952 /* We support fences for both wp_linux_dmabuf and opaque EGL
3953 * buffers, as mandated by minor version 2 of the
3954 * zwp_linux_explicit_synchronization_v1 protocol. Since
3955 * renderers that support fences currently only support these
3956 * two buffer types plus SHM buffers, we can just check for the
3957 * SHM buffer case here.
3958 */
3959 if (wl_shm_buffer_get(surface->pending.buffer->resource)) {
3960 fd_clear(&surface->pending.acquire_fence_fd);
3961 wl_resource_post_error(surface->synchronization_resource,
3962 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_UNSUPPORTED_BUFFER,
3963 "wl_surface@%"PRIu32" unsupported buffer for synchronization",
3964 wl_resource_get_id(resource));
3965 return;
3966 }
3967 }
3968
Alexandros Frantzis67629672018-10-19 12:14:11 +03003969 if (surface->pending.buffer_release_ref.buffer_release &&
3970 !surface->pending.buffer) {
3971 assert(surface->synchronization_resource);
3972
3973 wl_resource_post_error(surface->synchronization_resource,
3974 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3975 "wl_surface@%"PRIu32" no buffer for synchronization",
3976 wl_resource_get_id(resource));
3977 return;
3978 }
3979
Pekka Paalanene67858b2013-04-25 13:57:42 +03003980 if (sub) {
3981 weston_subsurface_commit(sub);
3982 return;
3983 }
3984
3985 weston_surface_commit(surface);
3986
3987 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
3988 if (sub->surface != surface)
3989 weston_subsurface_parent_commit(sub, 0);
3990 }
3991}
3992
3993static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003994surface_set_buffer_transform(struct wl_client *client,
3995 struct wl_resource *resource, int transform)
3996{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003997 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003998
Jonny Lamba55f1392014-05-30 12:07:15 +02003999 /* if wl_output.transform grows more members this will need to be updated. */
4000 if (transform < 0 ||
4001 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
4002 wl_resource_post_error(resource,
4003 WL_SURFACE_ERROR_INVALID_TRANSFORM,
4004 "buffer transform must be a valid transform "
4005 "('%d' specified)", transform);
4006 return;
4007 }
4008
Pekka Paalanen952b6c82014-03-14 14:38:15 +02004009 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004010 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004011}
4012
Alexander Larsson4ea95522013-05-22 14:41:37 +02004013static void
4014surface_set_buffer_scale(struct wl_client *client,
4015 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004016 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004017{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004018 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02004019
Jonny Lamba55f1392014-05-30 12:07:15 +02004020 if (scale < 1) {
4021 wl_resource_post_error(resource,
4022 WL_SURFACE_ERROR_INVALID_SCALE,
4023 "buffer scale must be at least one "
4024 "('%d' specified)", scale);
4025 return;
4026 }
4027
Pekka Paalanen952b6c82014-03-14 14:38:15 +02004028 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004029 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02004030}
4031
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04004032static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004033 surface_destroy,
4034 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04004035 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004036 surface_frame,
4037 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03004038 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004039 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02004040 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06004041 surface_set_buffer_scale,
4042 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004043};
4044
Leandro Ribeiro8eb84142021-01-18 19:36:48 -03004045static int
4046create_surface_dmabuf_feedback(struct weston_compositor *ec,
4047 struct weston_surface *surface)
4048{
4049 struct weston_dmabuf_feedback_tranche *tranche;
4050 dev_t main_device = ec->default_dmabuf_feedback->main_device;
4051 uint32_t flags = 0;
4052
4053 surface->dmabuf_feedback = weston_dmabuf_feedback_create(main_device);
4054 if (!surface->dmabuf_feedback)
4055 return -1;
4056
4057 tranche = weston_dmabuf_feedback_tranche_create(surface->dmabuf_feedback,
4058 ec->dmabuf_feedback_format_table,
4059 main_device, flags,
4060 RENDERER_PREF);
4061 if (!tranche) {
4062 weston_dmabuf_feedback_destroy(surface->dmabuf_feedback);
4063 surface->dmabuf_feedback = NULL;
4064 return -1;
4065 }
4066
4067 return 0;
4068}
4069
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004070static void
4071compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04004072 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004073{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04004074 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004075 struct weston_surface *surface;
Leandro Ribeiro8eb84142021-01-18 19:36:48 -03004076 int ret;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004077
Kristian Høgsberg18c93002012-01-27 11:58:31 -05004078 surface = weston_surface_create(ec);
Leandro Ribeiro8eb84142021-01-18 19:36:48 -03004079 if (surface == NULL)
4080 goto err;
4081
4082 if (ec->default_dmabuf_feedback) {
4083 ret = create_surface_dmabuf_feedback(ec, surface);
4084 if (ret < 0)
4085 goto err_dmabuf_feedback;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04004086 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004087
Jason Ekstranda85118c2013-06-27 20:17:02 -05004088 surface->resource =
4089 wl_resource_create(client, &wl_surface_interface,
4090 wl_resource_get_version(resource), id);
Leandro Ribeiro8eb84142021-01-18 19:36:48 -03004091 if (surface->resource == NULL)
4092 goto err_dmabuf_feedback;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004093 wl_resource_set_implementation(surface->resource, &surface_interface,
4094 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07004095
4096 wl_signal_emit(&ec->create_surface_signal, surface);
Leandro Ribeiro8eb84142021-01-18 19:36:48 -03004097
4098 return;
4099
4100err_dmabuf_feedback:
4101 weston_surface_destroy(surface);
4102err:
4103 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004104}
4105
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004106static void
4107destroy_region(struct wl_resource *resource)
4108{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05004109 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004110
4111 pixman_region32_fini(&region->region);
4112 free(region);
4113}
4114
4115static void
4116region_destroy(struct wl_client *client, struct wl_resource *resource)
4117{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004118 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004119}
4120
4121static void
4122region_add(struct wl_client *client, struct wl_resource *resource,
4123 int32_t x, int32_t y, int32_t width, int32_t height)
4124{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05004125 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004126
4127 pixman_region32_union_rect(&region->region, &region->region,
4128 x, y, width, height);
4129}
4130
4131static void
4132region_subtract(struct wl_client *client, struct wl_resource *resource,
4133 int32_t x, int32_t y, int32_t width, int32_t height)
4134{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05004135 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004136 pixman_region32_t rect;
4137
4138 pixman_region32_init_rect(&rect, x, y, width, height);
4139 pixman_region32_subtract(&region->region, &region->region, &rect);
4140 pixman_region32_fini(&rect);
4141}
4142
4143static const struct wl_region_interface region_interface = {
4144 region_destroy,
4145 region_add,
4146 region_subtract
4147};
4148
4149static void
4150compositor_create_region(struct wl_client *client,
4151 struct wl_resource *resource, uint32_t id)
4152{
4153 struct weston_region *region;
4154
4155 region = malloc(sizeof *region);
4156 if (region == NULL) {
4157 wl_resource_post_no_memory(resource);
4158 return;
4159 }
4160
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004161 pixman_region32_init(&region->region);
4162
Jason Ekstranda85118c2013-06-27 20:17:02 -05004163 region->resource =
4164 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004165 if (region->resource == NULL) {
4166 free(region);
4167 wl_resource_post_no_memory(resource);
4168 return;
4169 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05004170 wl_resource_set_implementation(region->resource, &region_interface,
4171 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004172}
4173
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04004174static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004175 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004176 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004177};
4178
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004179static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03004180weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
4181{
4182 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004183
Jason Ekstrand7b982072014-05-20 14:33:03 -05004184 weston_surface_commit_state(surface, &sub->cached);
4185 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004186
4187 weston_surface_commit_subsurface_order(surface);
4188
4189 weston_surface_schedule_repaint(surface);
4190
Jason Ekstrand7b982072014-05-20 14:33:03 -05004191 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004192}
4193
4194static void
4195weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
4196{
4197 struct weston_surface *surface = sub->surface;
4198
4199 /*
4200 * If this commit would cause the surface to move by the
4201 * attach(dx, dy) parameters, the old damage region must be
4202 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05004203 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03004204 */
Derek Foreman152254b2015-11-26 14:17:48 -06004205 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004206 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06004207 pixman_region32_union(&sub->cached.damage_surface,
4208 &sub->cached.damage_surface,
4209 &surface->pending.damage_surface);
4210 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004211
4212 if (surface->pending.newly_attached) {
4213 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05004214 weston_surface_state_set_buffer(&sub->cached,
4215 surface->pending.buffer);
4216 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004217 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04004218 weston_presentation_feedback_discard_list(
4219 &sub->cached.feedback_list);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03004220 /* zwp_surface_synchronization_v1.set_acquire_fence */
4221 fd_move(&sub->cached.acquire_fence_fd,
4222 &surface->pending.acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03004223 /* zwp_surface_synchronization_v1.get_release */
4224 weston_buffer_release_move(&sub->cached.buffer_release_ref,
4225 &surface->pending.buffer_release_ref);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004226 }
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05304227 sub->cached.desired_protection = surface->pending.desired_protection;
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05304228 sub->cached.protection_mode = surface->pending.protection_mode;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03004229 assert(surface->pending.acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03004230 assert(surface->pending.buffer_release_ref.buffer_release == NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004231 sub->cached.sx += surface->pending.sx;
4232 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02004233
Derek Foreman152254b2015-11-26 14:17:48 -06004234 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
4235
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004236 sub->cached.buffer_viewport.changed |=
4237 surface->pending.buffer_viewport.changed;
4238 sub->cached.buffer_viewport.buffer =
4239 surface->pending.buffer_viewport.buffer;
4240 sub->cached.buffer_viewport.surface =
4241 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004242
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004243 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004244
4245 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
4246
4247 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
4248
4249 wl_list_insert_list(&sub->cached.frame_callback_list,
4250 &surface->pending.frame_callback_list);
4251 wl_list_init(&surface->pending.frame_callback_list);
4252
Pekka Paalanen133e4392014-09-23 22:08:46 -04004253 wl_list_insert_list(&sub->cached.feedback_list,
4254 &surface->pending.feedback_list);
4255 wl_list_init(&surface->pending.feedback_list);
4256
Jason Ekstrand7b982072014-05-20 14:33:03 -05004257 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004258}
4259
Derek Foreman280e7dd2014-10-03 13:13:42 -05004260static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03004261weston_subsurface_is_synchronized(struct weston_subsurface *sub)
4262{
4263 while (sub) {
4264 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05004265 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004266
4267 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05004268 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004269
4270 sub = weston_surface_to_subsurface(sub->parent);
4271 }
4272
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01004273 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004274}
4275
4276static void
4277weston_subsurface_commit(struct weston_subsurface *sub)
4278{
4279 struct weston_surface *surface = sub->surface;
4280 struct weston_subsurface *tmp;
4281
4282 /* Recursive check for effectively synchronized. */
4283 if (weston_subsurface_is_synchronized(sub)) {
4284 weston_subsurface_commit_to_cache(sub);
4285 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05004286 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03004287 /* flush accumulated state from cache */
4288 weston_subsurface_commit_to_cache(sub);
4289 weston_subsurface_commit_from_cache(sub);
4290 } else {
4291 weston_surface_commit(surface);
4292 }
4293
4294 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
4295 if (tmp->surface != surface)
4296 weston_subsurface_parent_commit(tmp, 0);
4297 }
4298 }
4299}
4300
4301static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004302weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004303{
4304 struct weston_surface *surface = sub->surface;
4305 struct weston_subsurface *tmp;
4306
Pekka Paalanene67858b2013-04-25 13:57:42 +03004307 /* From now on, commit_from_cache the whole sub-tree, regardless of
4308 * the synchronized mode of each child. This sub-surface or some
4309 * of its ancestors were synchronized, so we are synchronized
4310 * all the way down.
4311 */
4312
Jason Ekstrand7b982072014-05-20 14:33:03 -05004313 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004314 weston_subsurface_commit_from_cache(sub);
4315
4316 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
4317 if (tmp->surface != surface)
4318 weston_subsurface_parent_commit(tmp, 1);
4319 }
4320}
4321
4322static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004323weston_subsurface_parent_commit(struct weston_subsurface *sub,
4324 int parent_is_synchronized)
4325{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004326 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004327 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004328 wl_list_for_each(view, &sub->surface->views, surface_link)
4329 weston_view_set_position(view,
4330 sub->position.x,
4331 sub->position.y);
4332
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004333 sub->position.set = 0;
4334 }
4335
4336 if (parent_is_synchronized || sub->synchronized)
4337 weston_subsurface_synchronized_commit(sub);
4338}
4339
Pekka Paalanen8274d902014-08-06 19:36:51 +03004340static int
4341subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
4342{
4343 return snprintf(buf, len, "sub-surface");
4344}
4345
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004346static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004347subsurface_committed(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004348{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004349 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004350
Jason Ekstranda7af7042013-10-12 22:38:11 -05004351 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004352 weston_view_set_position(view,
4353 view->geometry.x + dx,
4354 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004355
4356 /* No need to check parent mappedness, because if parent is not
4357 * mapped, parent is not in a visible layer, so this sub-surface
4358 * will not be drawn either.
4359 */
Armin Krezovićf8486c32016-06-30 06:04:28 +02004360
Pekka Paalanene67858b2013-04-25 13:57:42 +03004361 if (!weston_surface_is_mapped(surface)) {
Armin Krezovićf8486c32016-06-30 06:04:28 +02004362 surface->is_mapped = true;
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03004363
Derek Foreman4b1a0a12014-09-10 15:37:33 -05004364 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03004365 * because that would call it also for the parent surface,
4366 * which might not be mapped yet. That would lead to
4367 * inconsistent state, where the window could never be
4368 * mapped.
4369 *
Armin Krezovićf8486c32016-06-30 06:04:28 +02004370 * Instead just force the is_mapped flag on, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03004371 * weston_surface_is_mapped() return true, so that when the
4372 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03004373 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03004374 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004375 }
4376}
4377
4378static struct weston_subsurface *
4379weston_surface_to_subsurface(struct weston_surface *surface)
4380{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004381 if (surface->committed == subsurface_committed)
4382 return surface->committed_private;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004383
4384 return NULL;
4385}
4386
Pekka Paalanen01388e22013-04-25 13:57:44 +03004387WL_EXPORT struct weston_surface *
4388weston_surface_get_main_surface(struct weston_surface *surface)
4389{
4390 struct weston_subsurface *sub;
4391
4392 while (surface && (sub = weston_surface_to_subsurface(surface)))
4393 surface = sub->parent;
4394
4395 return surface;
4396}
4397
Pekka Paalanen50b67472014-10-01 15:02:41 +03004398WL_EXPORT int
4399weston_surface_set_role(struct weston_surface *surface,
4400 const char *role_name,
4401 struct wl_resource *error_resource,
4402 uint32_t error_code)
4403{
4404 assert(role_name);
4405
4406 if (surface->role_name == NULL ||
4407 surface->role_name == role_name ||
4408 strcmp(surface->role_name, role_name) == 0) {
4409 surface->role_name = role_name;
4410
4411 return 0;
4412 }
4413
4414 wl_resource_post_error(error_resource, error_code,
4415 "Cannot assign role %s to wl_surface@%d,"
4416 " already has role %s\n",
4417 role_name,
4418 wl_resource_get_id(surface->resource),
4419 surface->role_name);
4420 return -1;
4421}
4422
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02004423WL_EXPORT const char *
4424weston_surface_get_role(struct weston_surface *surface)
4425{
4426 return surface->role_name;
4427}
4428
Pekka Paalanen8274d902014-08-06 19:36:51 +03004429WL_EXPORT void
4430weston_surface_set_label_func(struct weston_surface *surface,
4431 int (*desc)(struct weston_surface *,
4432 char *, size_t))
4433{
4434 surface->get_label = desc;
Marius Vlad5de92972019-10-15 13:25:41 +03004435 weston_timeline_refresh_subscription_objects(surface->compositor,
4436 surface);
Pekka Paalanen8274d902014-08-06 19:36:51 +03004437}
4438
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004439/** Get the size of surface contents
4440 *
4441 * \param surface The surface to query.
4442 * \param width Returns the width of raw contents.
4443 * \param height Returns the height of raw contents.
4444 *
4445 * Retrieves the raw surface content size in pixels for the given surface.
4446 * This is the whole content size in buffer pixels. If the surface
4447 * has no content or the renderer does not implement this feature,
4448 * zeroes are returned.
4449 *
4450 * This function is used to determine the buffer size needed for
4451 * a weston_surface_copy_content() call.
4452 */
4453WL_EXPORT void
4454weston_surface_get_content_size(struct weston_surface *surface,
4455 int *width, int *height)
4456{
4457 struct weston_renderer *rer = surface->compositor->renderer;
4458
4459 if (!rer->surface_get_content_size) {
4460 *width = 0;
4461 *height = 0;
4462 return;
4463 }
4464
4465 rer->surface_get_content_size(surface, width, height);
4466}
4467
Quentin Glidic248dd102016-08-12 10:41:34 +02004468/** Get the bounding box of a surface and its subsurfaces
4469 *
4470 * \param surface The surface to query.
4471 * \return The bounding box relative to the surface origin.
4472 *
4473 */
4474WL_EXPORT struct weston_geometry
4475weston_surface_get_bounding_box(struct weston_surface *surface)
4476{
4477 pixman_region32_t region;
4478 pixman_box32_t *box;
4479 struct weston_subsurface *subsurface;
4480
4481 pixman_region32_init_rect(&region,
4482 0, 0,
4483 surface->width, surface->height);
4484
4485 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link)
4486 pixman_region32_union_rect(&region, &region,
4487 subsurface->position.x,
4488 subsurface->position.y,
4489 subsurface->surface->width,
4490 subsurface->surface->height);
4491
4492 box = pixman_region32_extents(&region);
4493 struct weston_geometry geometry = {
4494 .x = box->x1,
4495 .y = box->y1,
4496 .width = box->x2 - box->x1,
4497 .height = box->y2 - box->y1,
4498 };
4499
4500 pixman_region32_fini(&region);
4501
4502 return geometry;
4503}
4504
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004505/** Copy surface contents to system memory.
4506 *
4507 * \param surface The surface to copy from.
4508 * \param target Pointer to the target memory buffer.
4509 * \param size Size of the target buffer in bytes.
4510 * \param src_x X location on contents to copy from.
4511 * \param src_y Y location on contents to copy from.
4512 * \param width Width in pixels of the area to copy.
4513 * \param height Height in pixels of the area to copy.
4514 * \return 0 for success, -1 for failure.
4515 *
4516 * Surface contents are maintained by the renderer. They can be in a
4517 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
4518 * else.
4519 *
4520 * Surface contents are copied into memory pointed to by target,
4521 * which has size bytes of space available. The target memory
4522 * may be larger than needed, but being smaller returns an error.
4523 * The extra bytes in target may or may not be written; their content is
4524 * unspecified. Size must be large enough to hold the image.
4525 *
4526 * The image in the target memory will be arranged in rows from
4527 * top to bottom, and pixels on a row from left to right. The pixel
4528 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
4529 * width * 4.
4530 *
4531 * Parameters src_x and src_y define the upper-left corner in buffer
4532 * coordinates (pixels) to copy from. Parameters width and height
4533 * define the size of the area to copy in pixels.
4534 *
4535 * The rectangle defined by src_x, src_y, width, height must fit in
4536 * the surface contents. Otherwise an error is returned.
4537 *
Changwoo Chof97d2502017-08-05 00:30:47 +09004538 * Use weston_surface_get_content_size to determine the content size; the
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004539 * needed target buffer size and rectangle limits.
4540 *
4541 * CURRENT IMPLEMENTATION RESTRICTIONS:
4542 * - the machine must be little-endian due to Pixman formats.
4543 *
4544 * NOTE: Pixman formats are premultiplied.
4545 */
4546WL_EXPORT int
4547weston_surface_copy_content(struct weston_surface *surface,
4548 void *target, size_t size,
4549 int src_x, int src_y,
4550 int width, int height)
4551{
4552 struct weston_renderer *rer = surface->compositor->renderer;
4553 int cw, ch;
4554 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
4555
4556 if (!rer->surface_copy_content)
4557 return -1;
4558
4559 weston_surface_get_content_size(surface, &cw, &ch);
4560
4561 if (src_x < 0 || src_y < 0)
4562 return -1;
4563
4564 if (width <= 0 || height <= 0)
4565 return -1;
4566
4567 if (src_x + width > cw || src_y + height > ch)
4568 return -1;
4569
4570 if (width * bytespp * height > size)
4571 return -1;
4572
4573 return rer->surface_copy_content(surface, target, size,
4574 src_x, src_y, width, height);
4575}
4576
Pekka Paalanene67858b2013-04-25 13:57:42 +03004577static void
4578subsurface_set_position(struct wl_client *client,
4579 struct wl_resource *resource, int32_t x, int32_t y)
4580{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004581 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004582
4583 if (!sub)
4584 return;
4585
4586 sub->position.x = x;
4587 sub->position.y = y;
4588 sub->position.set = 1;
4589}
4590
4591static struct weston_subsurface *
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004592subsurface_find_sibling(struct weston_subsurface *sub,
4593 struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004594{
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004595 struct weston_surface *parent = sub->parent;
4596 struct weston_subsurface *sibling;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004597
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004598 wl_list_for_each(sibling, &parent->subsurface_list, parent_link) {
4599 if (sibling->surface == surface && sibling != sub)
4600 return sibling;
4601 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004602
4603 return NULL;
4604}
4605
4606static struct weston_subsurface *
4607subsurface_sibling_check(struct weston_subsurface *sub,
4608 struct weston_surface *surface,
4609 const char *request)
4610{
4611 struct weston_subsurface *sibling;
4612
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004613 sibling = subsurface_find_sibling(sub, surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004614 if (!sibling) {
4615 wl_resource_post_error(sub->resource,
4616 WL_SUBSURFACE_ERROR_BAD_SURFACE,
4617 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004618 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004619 return NULL;
4620 }
4621
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004622 assert(sibling->parent == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004623
4624 return sibling;
4625}
4626
4627static void
4628subsurface_place_above(struct wl_client *client,
4629 struct wl_resource *resource,
4630 struct wl_resource *sibling_resource)
4631{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004632 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004633 struct weston_surface *surface =
4634 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004635 struct weston_subsurface *sibling;
4636
4637 if (!sub)
4638 return;
4639
4640 sibling = subsurface_sibling_check(sub, surface, "place_above");
4641 if (!sibling)
4642 return;
4643
4644 wl_list_remove(&sub->parent_link_pending);
4645 wl_list_insert(sibling->parent_link_pending.prev,
4646 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004647
4648 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004649}
4650
4651static void
4652subsurface_place_below(struct wl_client *client,
4653 struct wl_resource *resource,
4654 struct wl_resource *sibling_resource)
4655{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004656 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004657 struct weston_surface *surface =
4658 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004659 struct weston_subsurface *sibling;
4660
4661 if (!sub)
4662 return;
4663
4664 sibling = subsurface_sibling_check(sub, surface, "place_below");
4665 if (!sibling)
4666 return;
4667
4668 wl_list_remove(&sub->parent_link_pending);
4669 wl_list_insert(&sibling->parent_link_pending,
4670 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004671
4672 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004673}
4674
4675static void
4676subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
4677{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004678 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004679
4680 if (sub)
4681 sub->synchronized = 1;
4682}
4683
4684static void
4685subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
4686{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004687 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004688
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004689 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03004690 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004691
4692 /* If sub became effectively desynchronized, flush. */
4693 if (!weston_subsurface_is_synchronized(sub))
4694 weston_subsurface_synchronized_commit(sub);
4695 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004696}
4697
4698static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03004699weston_subsurface_unlink_parent(struct weston_subsurface *sub)
4700{
4701 wl_list_remove(&sub->parent_link);
4702 wl_list_remove(&sub->parent_link_pending);
4703 wl_list_remove(&sub->parent_destroy_listener.link);
4704 sub->parent = NULL;
4705}
4706
4707static void
4708weston_subsurface_destroy(struct weston_subsurface *sub);
4709
4710static void
4711subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
4712{
4713 struct weston_subsurface *sub =
4714 container_of(listener, struct weston_subsurface,
4715 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004716 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004717
4718 /* The protocol object (wl_resource) is left inert. */
4719 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004720 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004721
4722 weston_subsurface_destroy(sub);
4723}
4724
4725static void
4726subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
4727{
4728 struct weston_subsurface *sub =
4729 container_of(listener, struct weston_subsurface,
4730 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004731 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004732 assert(sub->surface != sub->parent);
4733
4734 if (weston_surface_is_mapped(sub->surface))
4735 weston_surface_unmap(sub->surface);
4736
4737 weston_subsurface_unlink_parent(sub);
4738}
4739
4740static void
4741subsurface_resource_destroy(struct wl_resource *resource)
4742{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004743 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004744
4745 if (sub)
4746 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004747}
4748
4749static void
4750subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
4751{
4752 wl_resource_destroy(resource);
4753}
4754
4755static void
4756weston_subsurface_link_parent(struct weston_subsurface *sub,
4757 struct weston_surface *parent)
4758{
4759 sub->parent = parent;
4760 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004761 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004762 &sub->parent_destroy_listener);
4763
4764 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4765 wl_list_insert(&parent->subsurface_list_pending,
4766 &sub->parent_link_pending);
4767}
4768
4769static void
4770weston_subsurface_link_surface(struct weston_subsurface *sub,
4771 struct weston_surface *surface)
4772{
4773 sub->surface = surface;
4774 sub->surface_destroy_listener.notify =
4775 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004776 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004777 &sub->surface_destroy_listener);
4778}
4779
4780static void
4781weston_subsurface_destroy(struct weston_subsurface *sub)
4782{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004783 struct weston_view *view, *next;
4784
Pekka Paalanene67858b2013-04-25 13:57:42 +03004785 assert(sub->surface);
4786
4787 if (sub->resource) {
4788 assert(weston_surface_to_subsurface(sub->surface) == sub);
4789 assert(sub->parent_destroy_listener.notify ==
4790 subsurface_handle_parent_destroy);
4791
George Kiagiadakised04d382014-06-13 18:10:26 +02004792 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
4793 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004794 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02004795 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05004796
Pekka Paalanene67858b2013-04-25 13:57:42 +03004797 if (sub->parent)
4798 weston_subsurface_unlink_parent(sub);
4799
Jason Ekstrand7b982072014-05-20 14:33:03 -05004800 weston_surface_state_fini(&sub->cached);
4801 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004802
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004803 sub->surface->committed = NULL;
4804 sub->surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004805 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004806 } else {
4807 /* the dummy weston_subsurface for the parent itself */
4808 assert(sub->parent_destroy_listener.notify == NULL);
4809 wl_list_remove(&sub->parent_link);
4810 wl_list_remove(&sub->parent_link_pending);
4811 }
4812
4813 wl_list_remove(&sub->surface_destroy_listener.link);
4814 free(sub);
4815}
4816
4817static const struct wl_subsurface_interface subsurface_implementation = {
4818 subsurface_destroy,
4819 subsurface_set_position,
4820 subsurface_place_above,
4821 subsurface_place_below,
4822 subsurface_set_sync,
4823 subsurface_set_desync
4824};
4825
4826static struct weston_subsurface *
4827weston_subsurface_create(uint32_t id, struct weston_surface *surface,
4828 struct weston_surface *parent)
4829{
4830 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004831 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004832
Bryce Harringtonde16d892014-11-20 22:21:57 -08004833 sub = zalloc(sizeof *sub);
4834 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004835 return NULL;
4836
Jason Ekstranda7af7042013-10-12 22:38:11 -05004837 wl_list_init(&sub->unused_views);
4838
Jason Ekstranda85118c2013-06-27 20:17:02 -05004839 sub->resource =
4840 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004841 if (!sub->resource) {
4842 free(sub);
4843 return NULL;
4844 }
4845
Jason Ekstranda85118c2013-06-27 20:17:02 -05004846 wl_resource_set_implementation(sub->resource,
4847 &subsurface_implementation,
4848 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004849 weston_subsurface_link_surface(sub, surface);
4850 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05004851 weston_surface_state_init(&sub->cached);
4852 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004853 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004854
4855 return sub;
4856}
4857
4858/* Create a dummy subsurface for having the parent itself in its
4859 * sub-surface lists. Makes stacking order manipulation easy.
4860 */
4861static struct weston_subsurface *
4862weston_subsurface_create_for_parent(struct weston_surface *parent)
4863{
4864 struct weston_subsurface *sub;
4865
Bryce Harringtonde16d892014-11-20 22:21:57 -08004866 sub = zalloc(sizeof *sub);
4867 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004868 return NULL;
4869
4870 weston_subsurface_link_surface(sub, parent);
4871 sub->parent = parent;
4872 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4873 wl_list_insert(&parent->subsurface_list_pending,
4874 &sub->parent_link_pending);
4875
4876 return sub;
4877}
4878
4879static void
4880subcompositor_get_subsurface(struct wl_client *client,
4881 struct wl_resource *resource,
4882 uint32_t id,
4883 struct wl_resource *surface_resource,
4884 struct wl_resource *parent_resource)
4885{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004886 struct weston_surface *surface =
4887 wl_resource_get_user_data(surface_resource);
4888 struct weston_surface *parent =
4889 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004890 struct weston_subsurface *sub;
4891 static const char where[] = "get_subsurface: wl_subsurface@";
4892
4893 if (surface == parent) {
4894 wl_resource_post_error(resource,
4895 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4896 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004897 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004898 return;
4899 }
4900
4901 if (weston_surface_to_subsurface(surface)) {
4902 wl_resource_post_error(resource,
4903 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4904 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004905 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004906 return;
4907 }
4908
Pekka Paalanen50b67472014-10-01 15:02:41 +03004909 if (weston_surface_set_role(surface, "wl_subsurface", resource,
4910 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004911 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004912
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004913 if (weston_surface_get_main_surface(parent) == surface) {
4914 wl_resource_post_error(resource,
4915 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4916 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004917 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004918 return;
4919 }
4920
Pekka Paalanene67858b2013-04-25 13:57:42 +03004921 /* make sure the parent is in its own list */
4922 if (wl_list_empty(&parent->subsurface_list)) {
4923 if (!weston_subsurface_create_for_parent(parent)) {
4924 wl_resource_post_no_memory(resource);
4925 return;
4926 }
4927 }
4928
4929 sub = weston_subsurface_create(id, surface, parent);
4930 if (!sub) {
4931 wl_resource_post_no_memory(resource);
4932 return;
4933 }
4934
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004935 surface->committed = subsurface_committed;
4936 surface->committed_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004937 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004938}
4939
4940static void
4941subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
4942{
4943 wl_resource_destroy(resource);
4944}
4945
4946static const struct wl_subcompositor_interface subcompositor_interface = {
4947 subcompositor_destroy,
4948 subcompositor_get_subsurface
4949};
4950
4951static void
4952bind_subcompositor(struct wl_client *client,
4953 void *data, uint32_t version, uint32_t id)
4954{
4955 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004956 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004957
Jason Ekstranda85118c2013-06-27 20:17:02 -05004958 resource =
4959 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004960 if (resource == NULL) {
4961 wl_client_post_no_memory(client);
4962 return;
4963 }
4964 wl_resource_set_implementation(resource, &subcompositor_interface,
4965 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004966}
4967
Bryce Harrington0795ece2016-08-30 12:04:26 -07004968/** Set a DPMS mode on all of the compositor's outputs
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004969 *
4970 * \param compositor The compositor instance
4971 * \param state The DPMS state the outputs will be set to
4972 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004973static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004974weston_compositor_dpms(struct weston_compositor *compositor,
4975 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004976{
Emmanuel Gil Peyroteff793a2021-07-31 17:25:41 +02004977 struct weston_output *output;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004978
Emmanuel Gil Peyroteff793a2021-07-31 17:25:41 +02004979 wl_list_for_each(output, &compositor->output_list, link)
Bryce Harrington08976ac2016-08-30 12:05:16 -07004980 if (output->set_dpms)
4981 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004982}
4983
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004984/** Restores the compositor to active status
4985 *
4986 * \param compositor The compositor instance
4987 *
4988 * If the compositor was in a sleeping mode, all outputs are powered
4989 * back on via DPMS. Otherwise if the compositor was inactive
4990 * (idle/locked, offscreen, or sleeping) then the compositor's wake
4991 * signal will fire.
4992 *
4993 * Restarts the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004994 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004995 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004996WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004997weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004998{
Neil Roberts8b62e202013-09-30 13:14:47 +01004999 uint32_t old_state = compositor->state;
5000
5001 /* The state needs to be changed before emitting the wake
5002 * signal because that may try to schedule a repaint which
5003 * will not work if the compositor is still sleeping */
5004 compositor->state = WESTON_COMPOSITOR_ACTIVE;
5005
5006 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005007 case WESTON_COMPOSITOR_SLEEPING:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005008 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01005009 case WESTON_COMPOSITOR_OFFSCREEN:
Daniel Stone893b9362016-11-08 15:47:09 +00005010 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005011 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005012 /* fall through */
5013 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005014 wl_event_source_timer_update(compositor->idle_source,
5015 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02005016 }
5017}
5018
Bryce Harringtonc9626a32015-12-11 13:11:38 -08005019/** Turns off rendering and frame events for the compositor.
5020 *
5021 * \param compositor The compositor instance
5022 *
5023 * This is used for example to prevent further rendering while the
5024 * compositor is shutting down.
5025 *
Bryce Harringtonc9626a32015-12-11 13:11:38 -08005026 * Stops the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005027 *
5028 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08005029 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005030WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01005031weston_compositor_offscreen(struct weston_compositor *compositor)
5032{
5033 switch (compositor->state) {
5034 case WESTON_COMPOSITOR_OFFSCREEN:
5035 return;
5036 case WESTON_COMPOSITOR_SLEEPING:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01005037 default:
5038 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
5039 wl_event_source_timer_update(compositor->idle_source, 0);
5040 }
5041}
5042
Bryce Harringtonc9626a32015-12-11 13:11:38 -08005043/** Powers down all attached output devices
5044 *
5045 * \param compositor The compositor instance
5046 *
5047 * Causes rendering to the outputs to cease, and no frame events to be
5048 * sent. Only powers down the outputs if the compositor is not already
5049 * in sleep mode.
5050 *
5051 * Stops the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005052 *
5053 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08005054 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01005055WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005056weston_compositor_sleep(struct weston_compositor *compositor)
5057{
5058 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
5059 return;
5060
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01005061 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005062 compositor->state = WESTON_COMPOSITOR_SLEEPING;
5063 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
5064}
5065
Bryce Harringtonc9626a32015-12-11 13:11:38 -08005066/** Sets compositor to idle mode
5067 *
5068 * \param data The compositor instance
5069 *
5070 * This is called when the idle timer fires. Once the compositor is in
5071 * idle mode it requires a wake action (e.g. via
5072 * weston_compositor_wake()) to restore it. The compositor's
5073 * idle_signal will be triggered when the idle event occurs.
5074 *
5075 * Idleness can be inhibited by setting the compositor's idle_inhibit
5076 * property.
5077 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04005078static int
5079idle_handler(void *data)
5080{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005081 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04005082
5083 if (compositor->idle_inhibit)
5084 return 1;
5085
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005086 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005087 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04005088
5089 return 1;
5090}
5091
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005092WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08005093weston_plane_init(struct weston_plane *plane,
5094 struct weston_compositor *ec,
5095 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005096{
5097 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02005098 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005099 plane->x = x;
5100 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08005101 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03005102
5103 /* Init the link so that the call to wl_list_remove() when releasing
5104 * the plane without ever stacking doesn't lead to a crash */
5105 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005106}
5107
5108WL_EXPORT void
5109weston_plane_release(struct weston_plane *plane)
5110{
Xiong Zhang97116532013-10-23 13:58:31 +08005111 struct weston_view *view;
5112
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005113 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02005114 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03005115
Pekka Paalanen6551c092021-05-03 16:09:45 +03005116 /*
5117 * Can't use paint node list here, weston_plane is not specific to an
5118 * output.
5119 */
Xiong Zhang97116532013-10-23 13:58:31 +08005120 wl_list_for_each(view, &plane->compositor->view_list, link) {
5121 if (view->plane == plane)
5122 view->plane = NULL;
5123 }
5124
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03005125 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005126}
5127
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005128/** weston_compositor_stack_plane
5129 * \ingroup compositor
5130 */
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02005131WL_EXPORT void
5132weston_compositor_stack_plane(struct weston_compositor *ec,
5133 struct weston_plane *plane,
5134 struct weston_plane *above)
5135{
5136 if (above)
5137 wl_list_insert(above->link.prev, &plane->link);
5138 else
5139 wl_list_insert(&ec->plane_list, &plane->link);
5140}
5141
Quentin Glidic4ef719c2016-07-05 20:44:33 +02005142static void
5143output_release(struct wl_client *client, struct wl_resource *resource)
5144{
5145 wl_resource_destroy(resource);
5146}
5147
5148static const struct wl_output_interface output_interface = {
5149 output_release,
5150};
5151
5152
Casey Dahlin9074db52012-04-19 22:50:09 -04005153static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04005154{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05005155 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04005156}
5157
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005158static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005159bind_output(struct wl_client *client,
5160 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05005161{
Pekka Paalanen05347622017-03-27 12:24:34 +03005162 struct weston_head *head = data;
5163 struct weston_output *output = head->output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005164 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04005165 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05005166
Jason Ekstranda85118c2013-06-27 20:17:02 -05005167 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06005168 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07005169 if (resource == NULL) {
5170 wl_client_post_no_memory(client);
5171 return;
5172 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04005173
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005174 wl_list_insert(&head->resource_list, wl_resource_get_link(resource));
Pekka Paalanen055c1132017-03-27 16:31:25 +03005175 wl_resource_set_implementation(resource, &output_interface, head,
Pekka Paalanen05347622017-03-27 12:24:34 +03005176 unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04005177
Pekka Paalanen05347622017-03-27 12:24:34 +03005178 assert(output);
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05005179 wl_output_send_geometry(resource,
5180 output->x,
5181 output->y,
Pekka Paalanen01f60212017-03-24 15:39:24 +02005182 head->mm_width,
5183 head->mm_height,
5184 head->subpixel,
5185 head->make, head->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04005186 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04005187 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02005188 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02005189 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005190
5191 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05005192 wl_output_send_mode(resource,
5193 mode->flags,
5194 mode->width,
5195 mode->height,
5196 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005197 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02005198
Jasper St. Pierre0013a292014-08-07 16:43:11 -04005199 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02005200 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05005201}
5202
Pekka Paalanendcac3512017-12-08 14:13:34 +02005203static void
5204weston_head_add_global(struct weston_head *head)
5205{
5206 head->global = wl_global_create(head->compositor->wl_display,
5207 &wl_output_interface, 3,
5208 head, bind_output);
5209}
5210
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02005211/** Remove the global wl_output protocol object
5212 *
5213 * \param head The head whose global to remove.
5214 *
5215 * Also orphans the wl_resources for this head (wl_output).
5216 */
5217static void
5218weston_head_remove_global(struct weston_head *head)
5219{
5220 struct wl_resource *resource, *tmp;
5221
5222 if (head->global)
5223 wl_global_destroy(head->global);
5224 head->global = NULL;
5225
5226 wl_resource_for_each_safe(resource, tmp, &head->resource_list) {
5227 unbind_resource(resource);
5228 wl_resource_set_destructor(resource, NULL);
5229 wl_resource_set_user_data(resource, NULL);
5230 }
Roman Gilge97391c2019-03-29 13:01:06 +01005231
5232 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
5233 /* It's sufficient to unset the destructor, then the list elements
5234 * won't be accessed.
5235 */
5236 wl_resource_set_destructor(resource, NULL);
5237 }
5238 wl_list_init(&head->xdg_output_resource_list);
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02005239}
5240
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005241/** Get the backing object of wl_output
5242 *
5243 * \param resource A wl_output protocol object.
5244 * \return The backing object (user data) of a wl_resource representing a
5245 * wl_output protocol object.
Marius Vlad78984ee2019-06-11 00:05:08 +03005246 *
5247 * \ingroup head
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005248 */
Pekka Paalanen055c1132017-03-27 16:31:25 +03005249WL_EXPORT struct weston_head *
5250weston_head_from_resource(struct wl_resource *resource)
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005251{
5252 assert(wl_resource_instance_of(resource, &wl_output_interface,
5253 &output_interface));
5254
5255 return wl_resource_get_user_data(resource);
5256}
5257
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005258/** Initialize a pre-allocated weston_head
5259 *
5260 * \param head The head to initialize.
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005261 * \param name The head name, e.g. the connector name or equivalent.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005262 *
5263 * The head will be safe to attach, detach and release.
5264 *
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005265 * The name is used in logs, and can be used by compositors as a configuration
5266 * identifier.
5267 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005268 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005269 * \internal
5270 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005271WL_EXPORT void
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005272weston_head_init(struct weston_head *head, const char *name)
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005273{
5274 /* Add some (in)sane defaults which can be used
5275 * for checking if an output was properly configured
5276 */
5277 memset(head, 0, sizeof *head);
5278
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005279 wl_list_init(&head->compositor_link);
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005280 wl_signal_init(&head->destroy_signal);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005281 wl_list_init(&head->output_link);
5282 wl_list_init(&head->resource_list);
Roman Gilge97391c2019-03-29 13:01:06 +01005283 wl_list_init(&head->xdg_output_resource_list);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005284 head->name = strdup(name);
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305285 head->current_protection = WESTON_HDCP_DISABLE;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005286}
5287
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005288/** Send output heads changed signal
5289 *
5290 * \param output The output that changed.
5291 *
5292 * Notify that the enabled output gained and/or lost heads, or that the
5293 * associated heads may have changed their connection status. This does not
5294 * include cases where the output becomes enabled or disabled. The registered
5295 * callbacks are called after the change has successfully happened.
5296 *
5297 * If connection status change causes the compositor to attach or detach a head
5298 * to an enabled output, the registered callbacks may be called multiple times.
Marius Vlad55d87362019-06-11 01:15:35 +03005299 *
5300 * \ingroup output
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005301 */
5302static void
5303weston_output_emit_heads_changed(struct weston_output *output)
5304{
5305 wl_signal_emit(&output->compositor->output_heads_changed_signal,
5306 output);
5307}
5308
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005309/** Idle task for emitting heads_changed_signal */
5310static void
5311weston_compositor_call_heads_changed(void *data)
5312{
5313 struct weston_compositor *compositor = data;
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005314 struct weston_head *head;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005315
5316 compositor->heads_changed_source = NULL;
5317
5318 wl_signal_emit(&compositor->heads_changed_signal, compositor);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005319
5320 wl_list_for_each(head, &compositor->head_list, compositor_link) {
5321 if (head->output && head->output->enabled)
5322 weston_output_emit_heads_changed(head->output);
5323 }
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005324}
5325
5326/** Schedule a call on idle to heads_changed callback
5327 *
5328 * \param compositor The Compositor.
5329 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005330 * \ingroup compositor
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005331 * \internal
5332 */
5333static void
5334weston_compositor_schedule_heads_changed(struct weston_compositor *compositor)
5335{
5336 struct wl_event_loop *loop;
5337
5338 if (compositor->heads_changed_source)
5339 return;
5340
5341 loop = wl_display_get_event_loop(compositor->wl_display);
5342 compositor->heads_changed_source = wl_event_loop_add_idle(loop,
5343 weston_compositor_call_heads_changed, compositor);
5344}
5345
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005346/** Register a new head
5347 *
5348 * \param compositor The compositor.
5349 * \param head The head to register, must not be already registered.
5350 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005351 * This signals the core that a new head has become available, leading to
5352 * heads_changed hook being called later.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005353 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005354 * \ingroup compositor
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005355 * \internal
5356 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005357WL_EXPORT void
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005358weston_compositor_add_head(struct weston_compositor *compositor,
5359 struct weston_head *head)
5360{
5361 assert(wl_list_empty(&head->compositor_link));
5362 assert(head->name);
5363
5364 wl_list_insert(compositor->head_list.prev, &head->compositor_link);
5365 head->compositor = compositor;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005366 weston_compositor_schedule_heads_changed(compositor);
5367}
5368
5369/** Adds a listener to be called when heads change
5370 *
5371 * \param compositor The compositor.
5372 * \param listener The listener to add.
5373 *
Marius Vlada2dace22019-06-12 16:05:44 +03005374 * The listener notify function argument is weston_compositor.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005375 *
5376 * The listener function will be called after heads are added or their
5377 * connection status has changed. Several changes may be accumulated into a
5378 * single call. The user is expected to iterate over the existing heads and
5379 * check their statuses to find out what changed.
5380 *
5381 * \sa weston_compositor_iterate_heads, weston_head_is_connected,
5382 * weston_head_is_enabled
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005383 * \ingroup compositor
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005384 */
5385WL_EXPORT void
5386weston_compositor_add_heads_changed_listener(struct weston_compositor *compositor,
5387 struct wl_listener *listener)
5388{
5389 wl_signal_add(&compositor->heads_changed_signal, listener);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005390}
5391
5392/** Iterate over available heads
5393 *
5394 * \param compositor The compositor.
Marius Vlada2dace22019-06-12 16:05:44 +03005395 * \param iter The iterator, or NULL for start.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005396 * \return The next available head in the list.
5397 *
5398 * Returns all available heads, regardless of being connected or enabled.
5399 *
5400 * You can iterate over all heads as follows:
5401 * \code
5402 * struct weston_head *head = NULL;
5403 *
5404 * while ((head = weston_compositor_iterate_heads(compositor, head))) {
5405 * ...
5406 * }
5407 * \endcode
5408 *
5409 * If you cause \c iter to be removed from the list, you cannot use it to
5410 * continue iterating. Removing any other item is safe.
5411 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005412 * \ingroup compositor
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005413 */
5414WL_EXPORT struct weston_head *
5415weston_compositor_iterate_heads(struct weston_compositor *compositor,
5416 struct weston_head *iter)
5417{
5418 struct wl_list *list = &compositor->head_list;
5419 struct wl_list *node;
5420
5421 assert(compositor);
5422 assert(!iter || iter->compositor == compositor);
5423
5424 if (iter)
5425 node = iter->compositor_link.next;
5426 else
5427 node = list->next;
5428
5429 assert(node);
5430 assert(!iter || node != &iter->compositor_link);
5431
5432 if (node == list)
5433 return NULL;
5434
5435 return container_of(node, struct weston_head, compositor_link);
5436}
5437
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005438/** Iterate over attached heads
5439 *
5440 * \param output The output whose heads to iterate.
Marius Vlada2dace22019-06-12 16:05:44 +03005441 * \param iter The iterator, or NULL for start.
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005442 * \return The next attached head in the list.
5443 *
5444 * Returns all heads currently attached to the output.
5445 *
5446 * You can iterate over all heads as follows:
5447 * \code
5448 * struct weston_head *head = NULL;
5449 *
5450 * while ((head = weston_output_iterate_heads(output, head))) {
5451 * ...
5452 * }
5453 * \endcode
5454 *
5455 * If you cause \c iter to be removed from the list, you cannot use it to
5456 * continue iterating. Removing any other item is safe.
5457 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05005458 * \ingroup output
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005459 */
5460WL_EXPORT struct weston_head *
5461weston_output_iterate_heads(struct weston_output *output,
5462 struct weston_head *iter)
5463{
5464 struct wl_list *list = &output->head_list;
5465 struct wl_list *node;
5466
5467 assert(output);
5468 assert(!iter || iter->output == output);
5469
5470 if (iter)
5471 node = iter->output_link.next;
5472 else
5473 node = list->next;
5474
5475 assert(node);
5476 assert(!iter || node != &iter->output_link);
5477
5478 if (node == list)
5479 return NULL;
5480
5481 return container_of(node, struct weston_head, output_link);
5482}
5483
Pekka Paalanendcac3512017-12-08 14:13:34 +02005484/** Attach a head to an output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005485 *
5486 * \param output The output to attach to.
5487 * \param head The head that is not yet attached.
5488 * \return 0 on success, -1 on failure.
5489 *
5490 * Attaches the given head to the output. All heads of an output are clones
5491 * and share the resolution and timings.
5492 *
5493 * Cloning heads this way uses less resources than creating an output for
5494 * each head, but is not always possible due to environment, driver and hardware
5495 * limitations.
5496 *
5497 * On failure, the head remains unattached. Success of this function does not
5498 * guarantee the output configuration is actually valid. The final checks are
Pekka Paalanendcac3512017-12-08 14:13:34 +02005499 * made on weston_output_enable() unless the output was already enabled.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005500 *
Marius Vlad55d87362019-06-11 01:15:35 +03005501 * \ingroup output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005502 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005503WL_EXPORT int
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005504weston_output_attach_head(struct weston_output *output,
5505 struct weston_head *head)
5506{
Pekka Paalanendcac3512017-12-08 14:13:34 +02005507 char *head_names;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005508
5509 if (!wl_list_empty(&head->output_link))
5510 return -1;
5511
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005512 if (output->attach_head) {
5513 if (output->attach_head(output, head) < 0)
5514 return -1;
5515 } else if (!wl_list_empty(&output->head_list)) {
5516 /* No support for clones in the legacy path. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005517 return -1;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005518 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005519
5520 head->output = output;
5521 wl_list_insert(output->head_list.prev, &head->output_link);
5522
Pekka Paalanendcac3512017-12-08 14:13:34 +02005523 if (output->enabled) {
5524 weston_head_add_global(head);
5525
5526 head_names = weston_output_create_heads_string(output);
5527 weston_log("Output '%s' updated to have head(s) %s\n",
5528 output->name, head_names);
5529 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005530
5531 weston_output_emit_heads_changed(output);
Pekka Paalanendcac3512017-12-08 14:13:34 +02005532 }
5533
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005534 return 0;
5535}
5536
5537/** Detach a head from its output
5538 *
5539 * \param head The head to detach.
5540 *
5541 * It is safe to detach a non-attached head.
5542 *
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005543 * If the head is attached to an enabled output and the output will be left
5544 * with no heads, the output will be disabled.
5545 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005546 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005547 * \sa weston_output_disable
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005548 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005549WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005550weston_head_detach(struct weston_head *head)
5551{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005552 struct weston_output *output = head->output;
Pekka Paalanena0106992017-12-08 16:11:17 +02005553 char *head_names;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005554
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005555 wl_list_remove(&head->output_link);
5556 wl_list_init(&head->output_link);
5557 head->output = NULL;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005558
5559 if (!output)
5560 return;
5561
5562 if (output->detach_head)
5563 output->detach_head(output, head);
5564
5565 if (output->enabled) {
5566 weston_head_remove_global(head);
5567
Pekka Paalanena0106992017-12-08 16:11:17 +02005568 if (wl_list_empty(&output->head_list)) {
5569 weston_log("Output '%s' no heads left, disabling.\n",
5570 output->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005571 weston_output_disable(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02005572 } else {
5573 head_names = weston_output_create_heads_string(output);
5574 weston_log("Output '%s' updated to have head(s) %s\n",
5575 output->name, head_names);
5576 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005577
5578 weston_output_emit_heads_changed(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02005579 }
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005580 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005581}
5582
5583/** Destroy a head
5584 *
5585 * \param head The head to be released.
5586 *
5587 * Destroys the head. The caller is responsible for freeing the memory pointed
5588 * to by \c head.
5589 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005590 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005591 * \internal
5592 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005593WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005594weston_head_release(struct weston_head *head)
5595{
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005596 wl_signal_emit(&head->destroy_signal, head);
5597
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005598 weston_head_detach(head);
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005599
5600 free(head->make);
5601 free(head->model);
5602 free(head->serial_number);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005603 free(head->name);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005604
5605 wl_list_remove(&head->compositor_link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005606}
5607
Pekka Paalanene19970f2017-08-28 14:11:02 +03005608static void
5609weston_head_set_device_changed(struct weston_head *head)
5610{
5611 head->device_changed = true;
5612
5613 if (head->compositor)
5614 weston_compositor_schedule_heads_changed(head->compositor);
5615}
5616
5617/** String equal comparison with NULLs being equal */
5618static bool
5619str_null_eq(const char *a, const char *b)
5620{
5621 if (!a && !b)
5622 return true;
5623
5624 if (!!a != !!b)
5625 return false;
5626
5627 return strcmp(a, b) == 0;
5628}
5629
Pekka Paalanen01f60212017-03-24 15:39:24 +02005630/** Store monitor make, model and serial number
5631 *
5632 * \param head The head to modify.
5633 * \param make The monitor make. If EDID is available, the PNP ID. Otherwise
5634 * any string, or NULL for none.
5635 * \param model The monitor model or name, or a made-up string, or NULL for
5636 * none.
5637 * \param serialno The monitor serial number, a made-up string, or NULL for
5638 * none.
5639 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005640 * This may set the device_changed flag.
5641 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005642 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005643 * \internal
5644 */
5645WL_EXPORT void
5646weston_head_set_monitor_strings(struct weston_head *head,
5647 const char *make,
5648 const char *model,
5649 const char *serialno)
5650{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005651 if (str_null_eq(head->make, make) &&
5652 str_null_eq(head->model, model) &&
5653 str_null_eq(head->serial_number, serialno))
5654 return;
5655
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005656 free(head->make);
5657 free(head->model);
5658 free(head->serial_number);
5659
5660 head->make = make ? strdup(make) : NULL;
5661 head->model = model ? strdup(model) : NULL;
5662 head->serial_number = serialno ? strdup(serialno) : NULL;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005663
5664 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005665}
5666
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005667/** Store display non-desktop status
5668 *
5669 * \param head The head to modify.
5670 * \param non_desktop Whether the head connects to a non-desktop display.
5671 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005672 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005673 * \internal
5674 */
5675WL_EXPORT void
5676weston_head_set_non_desktop(struct weston_head *head, bool non_desktop)
5677{
5678 if (head->non_desktop == non_desktop)
5679 return;
5680
5681 head->non_desktop = non_desktop;
5682
5683 weston_head_set_device_changed(head);
5684}
5685
Lucas Stacha69cb712019-11-25 23:29:31 +00005686/** Store display transformation
5687 *
5688 * \param head The head to modify.
5689 * \param transform The transformation to apply for this head
5690 *
5691 * This may set the device_changed flag.
5692 *
5693 * \ingroup head
5694 * \internal
5695 */
5696WL_EXPORT void
5697weston_head_set_transform(struct weston_head *head, uint32_t transform)
5698{
5699 if (head->transform == transform)
5700 return;
5701
5702 head->transform = transform;
5703
5704 weston_head_set_device_changed(head);
5705}
5706
5707
Pekka Paalanen01f60212017-03-24 15:39:24 +02005708/** Store physical image size
5709 *
5710 * \param head The head to modify.
5711 * \param mm_width Image area width in millimeters.
5712 * \param mm_height Image area height in millimeters.
5713 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005714 * This may set the device_changed flag.
5715 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005716 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005717 * \internal
5718 */
5719WL_EXPORT void
5720weston_head_set_physical_size(struct weston_head *head,
5721 int32_t mm_width, int32_t mm_height)
5722{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005723 if (head->mm_width == mm_width &&
5724 head->mm_height == mm_height)
5725 return;
5726
Pekka Paalanen01f60212017-03-24 15:39:24 +02005727 head->mm_width = mm_width;
5728 head->mm_height = mm_height;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005729
5730 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005731}
5732
5733/** Store monitor sub-pixel layout
5734 *
5735 * \param head The head to modify.
5736 * \param sp Sub-pixel layout. The possible values are:
5737 * - WL_OUTPUT_SUBPIXEL_UNKNOWN,
5738 * - WL_OUTPUT_SUBPIXEL_NONE,
5739 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB,
5740 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR,
5741 * - WL_OUTPUT_SUBPIXEL_VERTICAL_RGB,
5742 * - WL_OUTPUT_SUBPIXEL_VERTICAL_BGR
5743 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005744 * This may set the device_changed flag.
5745 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005746 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005747 * \internal
5748 */
5749WL_EXPORT void
5750weston_head_set_subpixel(struct weston_head *head,
5751 enum wl_output_subpixel sp)
5752{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005753 if (head->subpixel == sp)
5754 return;
5755
Pekka Paalanen01f60212017-03-24 15:39:24 +02005756 head->subpixel = sp;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005757
5758 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005759}
5760
5761/** Mark the monitor as internal
5762 *
5763 * This is used for embedded screens, like laptop panels.
5764 *
5765 * \param head The head to mark as internal.
5766 *
5767 * By default a head is external. The type is often inferred from the physical
5768 * connector type.
5769 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005770 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005771 * \internal
5772 */
5773WL_EXPORT void
5774weston_head_set_internal(struct weston_head *head)
5775{
5776 head->connection_internal = true;
5777}
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005778
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005779/** Store connector status
5780 *
5781 * \param head The head to modify.
5782 * \param connected Whether the head is connected.
5783 *
5784 * Connectors are created as disconnected. This function can be used to
5785 * set the connector status.
5786 *
5787 * The status should be set to true when a physical connector is connected to
5788 * a video sink device like a monitor and to false when the connector is
5789 * disconnected. For nested backends, the connection status should reflect the
5790 * connection to the parent display server.
5791 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005792 * When the connection status changes, it schedules a call to the heads_changed
Pekka Paalanene19970f2017-08-28 14:11:02 +03005793 * hook and sets the device_changed flag.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005794 *
5795 * \sa weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005796 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005797 * \internal
5798 */
5799WL_EXPORT void
5800weston_head_set_connection_status(struct weston_head *head, bool connected)
5801{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005802 if (head->connected == connected)
5803 return;
5804
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005805 head->connected = connected;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005806
Pekka Paalanene19970f2017-08-28 14:11:02 +03005807 weston_head_set_device_changed(head);
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005808}
5809
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305810static void
5811weston_output_compute_protection(struct weston_output *output)
5812{
5813 struct weston_head *head;
5814 enum weston_hdcp_protection op_protection;
5815 bool op_protection_valid = false;
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305816 struct weston_compositor *wc = output->compositor;
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305817
5818 wl_list_for_each(head, &output->head_list, output_link) {
5819 if (!op_protection_valid) {
5820 op_protection = head->current_protection;
5821 op_protection_valid = true;
5822 }
5823 if (head->current_protection < op_protection)
5824 op_protection = head->current_protection;
5825 }
5826
5827 if (!op_protection_valid)
5828 op_protection = WESTON_HDCP_DISABLE;
5829
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305830 if (output->current_protection != op_protection) {
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305831 output->current_protection = op_protection;
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305832 weston_output_damage(output);
Ankit Nautiyalfaa5ab42019-07-08 13:23:24 +05305833 weston_schedule_surface_protection_update(wc);
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305834 }
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305835}
5836
5837WL_EXPORT void
5838weston_head_set_content_protection_status(struct weston_head *head,
5839 enum weston_hdcp_protection status)
5840{
5841 head->current_protection = status;
5842 if (head->output)
5843 weston_output_compute_protection(head->output);
5844}
5845
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005846/** Is the head currently connected?
5847 *
5848 * \param head The head to query.
5849 * \return Connection status.
5850 *
5851 * Returns true if the head is physically connected to a monitor, or in
5852 * case of a nested backend returns true when there is a connection to the
5853 * parent display server.
5854 *
5855 * This is independent from the head being enabled.
5856 *
5857 * \sa weston_head_is_enabled
Marius Vlad78984ee2019-06-11 00:05:08 +03005858 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005859 */
5860WL_EXPORT bool
5861weston_head_is_connected(struct weston_head *head)
5862{
5863 return head->connected;
5864}
5865
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005866/** Is the head currently enabled?
5867 *
5868 * \param head The head to query.
5869 * \return Video status.
5870 *
5871 * Returns true if the head is currently transmitting a video stream.
5872 *
5873 * This is independent of the head being connected.
5874 *
5875 * \sa weston_head_is_connected
Marius Vlad78984ee2019-06-11 00:05:08 +03005876 * \ingroup head
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005877 */
5878WL_EXPORT bool
5879weston_head_is_enabled(struct weston_head *head)
5880{
5881 if (!head->output)
5882 return false;
5883
5884 return head->output->enabled;
5885}
5886
Pekka Paalanene19970f2017-08-28 14:11:02 +03005887/** Has the device information changed?
5888 *
5889 * \param head The head to query.
5890 * \return True if the device information has changed since last reset.
5891 *
5892 * The information about the connected display device, e.g. a monitor, may
5893 * change without being disconnected in between. Changing information
5894 * causes a call to the heads_changed hook.
5895 *
5896 * The information includes make, model, serial number, physical size,
5897 * and sub-pixel type. The connection status is also included.
5898 *
5899 * \sa weston_head_reset_device_changed, weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005900 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03005901 */
5902WL_EXPORT bool
5903weston_head_is_device_changed(struct weston_head *head)
5904{
5905 return head->device_changed;
5906}
5907
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005908/** Does the head represent a non-desktop display?
5909 *
5910 * \param head The head to query.
5911 * \return True if the device is a non-desktop display.
5912 *
5913 * Non-desktop heads are not attached to outputs by default.
5914 * This stops weston from extending the desktop onto head mounted displays.
5915 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005916 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005917 */
5918WL_EXPORT bool
5919weston_head_is_non_desktop(struct weston_head *head)
5920{
5921 return head->non_desktop;
5922}
5923
Pekka Paalanene19970f2017-08-28 14:11:02 +03005924/** Acknowledge device information change
5925 *
5926 * \param head The head to acknowledge.
5927 *
5928 * Clears the device changed flag on this head. When a compositor has processed
5929 * device information, it should call this to be able to notice further
5930 * changes.
5931 *
5932 * \sa weston_head_is_device_changed
Marius Vlad78984ee2019-06-11 00:05:08 +03005933 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03005934 */
5935WL_EXPORT void
5936weston_head_reset_device_changed(struct weston_head *head)
5937{
5938 head->device_changed = false;
5939}
5940
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005941/** Get the name of a head
5942 *
5943 * \param head The head to query.
5944 * \return The head's name, not NULL.
5945 *
5946 * The name depends on the backend. The DRM backend uses connector names,
5947 * other backends may use hardcoded names or user-given names.
Marius Vlad78984ee2019-06-11 00:05:08 +03005948 *
5949 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005950 */
5951WL_EXPORT const char *
5952weston_head_get_name(struct weston_head *head)
5953{
5954 return head->name;
5955}
5956
5957/** Get the output the head is attached to
5958 *
5959 * \param head The head to query.
5960 * \return The output the head is attached to, or NULL if detached.
Marius Vlad78984ee2019-06-11 00:05:08 +03005961 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005962 */
5963WL_EXPORT struct weston_output *
5964weston_head_get_output(struct weston_head *head)
5965{
5966 return head->output;
5967}
5968
Lucas Stacha69cb712019-11-25 23:29:31 +00005969/** Get the head's native transformation
5970 *
5971 * \param head The head to query.
5972 * \return The head's native transform, as a WL_OUTPUT_TRANSFORM_* value
5973 *
5974 * A weston_head may have a 'native' transform provided by the backend.
5975 * Examples include panels which are physically rotated, where the rotation
5976 * is recorded and described as part of the system configuration. This call
5977 * will return any known native transform for the head.
5978 *
5979 * \ingroup head
5980 */
5981WL_EXPORT uint32_t
5982weston_head_get_transform(struct weston_head *head)
5983{
5984 return head->transform;
5985}
5986
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005987/** Add destroy callback for a head
5988 *
5989 * \param head The head to watch for.
5990 * \param listener The listener to add. The \c notify member must be set.
5991 *
5992 * Heads may get destroyed for various reasons by the backends. If a head is
5993 * attached to an output, the compositor should listen for head destruction
5994 * and reconfigure or destroy the output if necessary.
5995 *
5996 * The destroy callbacks will be called on weston_head destruction before any
5997 * automatic detaching from an associated weston_output and before any
5998 * weston_head information is lost.
5999 *
6000 * The \c data argument to the notify callback is the weston_head being
6001 * destroyed.
Marius Vlad78984ee2019-06-11 00:05:08 +03006002 *
6003 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03006004 */
6005WL_EXPORT void
6006weston_head_add_destroy_listener(struct weston_head *head,
6007 struct wl_listener *listener)
6008{
6009 wl_signal_add(&head->destroy_signal, listener);
6010}
6011
6012/** Look up destroy listener for a head
6013 *
6014 * \param head The head to query.
6015 * \param notify The notify function used used for the added destroy listener.
6016 * \return The listener, or NULL if not found.
6017 *
6018 * This looks up the previously added destroy listener struct based on the
6019 * notify function it has. The listener can be used to access user data
6020 * through \c container_of().
6021 *
6022 * \sa wl_signal_get()
Marius Vlad78984ee2019-06-11 00:05:08 +03006023 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03006024 */
6025WL_EXPORT struct wl_listener *
6026weston_head_get_destroy_listener(struct weston_head *head,
6027 wl_notify_func_t notify)
6028{
6029 return wl_signal_get(&head->destroy_signal, notify);
6030}
6031
David Fort0de859e2016-05-27 23:22:57 +02006032/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02006033static void
David Fort0de859e2016-05-27 23:22:57 +02006034weston_compositor_reflow_outputs(struct weston_compositor *compositor,
6035 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02006036{
6037 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02006038 bool start_resizing = false;
6039
6040 if (!delta_width)
6041 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02006042
6043 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02006044 if (output == resized_output) {
6045 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02006046 continue;
6047 }
6048
David Fort0de859e2016-05-27 23:22:57 +02006049 if (start_resizing) {
6050 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02006051 output->dirty = 1;
6052 }
6053 }
6054}
6055
Pekka Paalanen06920792020-12-08 15:22:36 +02006056/** Transform a region in-place from global to output coordinates
6057 *
6058 * \param output The output that defines the transformation.
6059 * \param region The region to be transformed in-place.
6060 *
6061 * This takes a region in the global coordinate system, and takes into account
6062 * output position, transform and scale, and the zoom, and converts the region
6063 * into output pixel coordinates in the framebuffer.
6064 *
6065 * Uses floating-point operations if zoom is active, which may round to expand
6066 * the region.
6067 *
6068 * \internal
6069 * \ingroup output
6070 */
6071WL_EXPORT void
6072weston_output_region_from_global(struct weston_output *output,
6073 pixman_region32_t *region)
6074{
6075 if (output->zoom.active) {
6076 weston_matrix_transform_region(region, &output->matrix, region);
6077 } else {
6078 pixman_region32_translate(region, -output->x, -output->y);
6079 weston_transformed_region(output->width, output->height,
6080 output->transform,
6081 output->current_scale,
6082 region, region);
6083 }
6084}
6085
Pekka Paalanend72bad22017-03-29 17:01:41 +03006086static void
Scott Moreauccbf29d2012-02-22 14:21:41 -07006087weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01006088{
Scott Moreau850ca422012-05-21 15:21:25 -06006089 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05006090
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006091 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05006092 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06006093
Scott Moreauccbf29d2012-02-22 14:21:41 -07006094 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06006095 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05006096 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01006097 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05006098 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01006099 weston_matrix_scale(&output->matrix, magnification,
6100 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07006101 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04006102
Jason Ekstrandfb23df72014-10-16 10:55:21 -05006103 switch (output->transform) {
6104 case WL_OUTPUT_TRANSFORM_FLIPPED:
6105 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
6106 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
6107 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
6108 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
6109 weston_matrix_scale(&output->matrix, -1, 1, 1);
6110 break;
6111 }
6112
6113 switch (output->transform) {
6114 default:
6115 case WL_OUTPUT_TRANSFORM_NORMAL:
6116 case WL_OUTPUT_TRANSFORM_FLIPPED:
6117 break;
6118 case WL_OUTPUT_TRANSFORM_90:
6119 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +02006120 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
6121 weston_matrix_rotate_xy(&output->matrix, 0, -1);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05006122 break;
6123 case WL_OUTPUT_TRANSFORM_180:
6124 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
6125 weston_matrix_translate(&output->matrix,
6126 -output->width, -output->height, 0);
6127 weston_matrix_rotate_xy(&output->matrix, -1, 0);
6128 break;
6129 case WL_OUTPUT_TRANSFORM_270:
6130 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +02006131 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
6132 weston_matrix_rotate_xy(&output->matrix, 0, 1);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05006133 break;
6134 }
6135
6136 if (output->current_scale != 1)
6137 weston_matrix_scale(&output->matrix,
6138 output->current_scale,
6139 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01006140
Scott Moreauccbf29d2012-02-22 14:21:41 -07006141 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05006142
6143 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07006144}
6145
Scott Moreau1bad5db2012-08-18 01:04:05 -06006146static void
Alexander Larsson0b135062013-05-28 16:23:36 +02006147weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06006148{
6149 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03006150 output->native_scale = scale;
6151 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06006152
Pekka Paalanen59987fa2016-04-26 15:50:59 +03006153 convert_size_by_transform_scale(&output->width, &output->height,
6154 output->current_mode->width,
6155 output->current_mode->height,
6156 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02006157}
6158
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006159static void
6160weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07006161{
6162 output->x = x;
6163 output->y = y;
6164
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006165 pixman_region32_fini(&output->region);
Scott Moreauccbf29d2012-02-22 14:21:41 -07006166 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06006167 output->width,
6168 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01006169}
6170
Marius Vlad55d87362019-06-11 01:15:35 +03006171/**
6172 * \ingroup output
6173 */
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006174WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006175weston_output_move(struct weston_output *output, int x, int y)
6176{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006177 struct weston_head *head;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006178 struct wl_resource *resource;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006179 int ver;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006180
6181 output->move_x = x - output->x;
6182 output->move_y = y - output->y;
6183
6184 if (output->move_x == 0 && output->move_y == 0)
6185 return;
6186
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006187 weston_output_init_geometry(output, x, y);
6188
6189 output->dirty = 1;
6190
6191 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006192 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006193
6194 /* Notify clients of the change for output position. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006195 wl_list_for_each(head, &output->head_list, output_link) {
6196 wl_resource_for_each(resource, &head->resource_list) {
6197 wl_output_send_geometry(resource,
6198 output->x,
6199 output->y,
6200 head->mm_width,
6201 head->mm_height,
6202 head->subpixel,
6203 head->make,
6204 head->model,
6205 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08006206
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006207 ver = wl_resource_get_version(resource);
6208 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
6209 wl_output_send_done(resource);
6210 }
Roman Gilge97391c2019-03-29 13:01:06 +01006211
6212 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
6213 zxdg_output_v1_send_logical_position(resource,
6214 output->x,
6215 output->y);
6216 zxdg_output_v1_send_done(resource);
6217 }
Quanxian Wangb2c86362014-03-14 09:16:25 +08006218 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006219}
6220
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006221/** Signal that a pending output is taken into use.
6222 *
6223 * Removes the output from the pending list and adds it to the compositor's
6224 * list of enabled outputs. The output created signal is emitted.
Giulio Camuffob1147152015-05-06 21:41:57 +03006225 *
Pekka Paalanen2210ad02017-03-30 15:48:06 +03006226 * The output gets an internal ID assigned, and the wl_output global is
6227 * created.
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03006228 *
Giulio Camuffob1147152015-05-06 21:41:57 +03006229 * \param compositor The compositor instance.
6230 * \param output The output to be added.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006231 *
6232 * \internal
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006233 * \ingroup compositor
Giulio Camuffob1147152015-05-06 21:41:57 +03006234 */
Pekka Paalanenf9681b52017-03-29 16:58:48 +03006235static void
Giulio Camuffob1147152015-05-06 21:41:57 +03006236weston_compositor_add_output(struct weston_compositor *compositor,
6237 struct weston_output *output)
6238{
Armin Krezoviće5403842016-08-05 15:28:29 +02006239 struct weston_view *view, *next;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03006240 struct weston_head *head;
Armin Krezoviće5403842016-08-05 15:28:29 +02006241
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006242 assert(!output->enabled);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03006243
6244 /* Verify we haven't reached the limit of 32 available output IDs */
6245 assert(ffs(~compositor->output_id_pool) > 0);
6246
6247 /* Invert the output id pool and look for the lowest numbered
6248 * switch (the least significant bit). Take that bit's position
6249 * as our ID, and mark it used in the compositor's output_id_pool.
6250 */
6251 output->id = ffs(~compositor->output_id_pool) - 1;
6252 compositor->output_id_pool |= 1u << output->id;
6253
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006254 wl_list_remove(&output->link);
Giulio Camuffob1147152015-05-06 21:41:57 +03006255 wl_list_insert(compositor->output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006256 output->enabled = true;
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006257
Pekka Paalanendcac3512017-12-08 14:13:34 +02006258 wl_list_for_each(head, &output->head_list, output_link)
6259 weston_head_add_global(head);
Pekka Paalanen2210ad02017-03-30 15:48:06 +03006260
Giulio Camuffob1147152015-05-06 21:41:57 +03006261 wl_signal_emit(&compositor->output_created_signal, output);
Armin Krezoviće5403842016-08-05 15:28:29 +02006262
Pekka Paalanen6551c092021-05-03 16:09:45 +03006263 /*
6264 * Use view_list, as paint nodes have not been created for this
6265 * output yet. Any existing view might touch this new output.
6266 */
Armin Krezoviće5403842016-08-05 15:28:29 +02006267 wl_list_for_each_safe(view, next, &compositor->view_list, link)
6268 weston_view_geometry_dirty(view);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04006269}
6270
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006271/** Transform device coordinates into global coordinates
6272 *
Marius Vlada2dace22019-06-12 16:05:44 +03006273 * \param output the weston_output object
6274 * \param[in] device_x X coordinate in device units.
6275 * \param[in] device_y Y coordinate in device units.
6276 * \param[out] x X coordinate in the global space.
6277 * \param[out] y Y coordinate in the global space.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006278 *
Marius Vlada2dace22019-06-12 16:05:44 +03006279 * Transforms coordinates from the device coordinate space (physical pixel
6280 * units) to the global coordinate space (logical pixel units). This takes
6281 * into account output transform and scale.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006282 *
Marius Vlad55d87362019-06-11 01:15:35 +03006283 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006284 * \internal
6285 */
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006286WL_EXPORT void
6287weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02006288 double device_x, double device_y,
6289 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006290{
Derek Foreman0f679412014-10-02 13:41:17 -05006291 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02006292 device_x,
6293 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05006294 0.0,
6295 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006296
Derek Foreman67a18b92015-03-24 11:36:14 -05006297 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006298
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02006299 *x = p.f[0] / p.f[3];
6300 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006301}
6302
Pekka Paalanen1d2eee22021-02-25 12:03:28 +02006303static void
6304weston_output_reset_color_transforms(struct weston_output *output)
6305{
Pekka Paalanencda39512021-03-22 17:32:07 +02006306 weston_color_transform_unref(output->from_sRGB_to_output);
6307 output->from_sRGB_to_output = NULL;
Pekka Paalanen8fb23ed2021-03-23 13:40:43 +02006308 weston_color_transform_unref(output->from_sRGB_to_blend);
6309 output->from_sRGB_to_blend = NULL;
Pekka Paalanen1d2eee22021-02-25 12:03:28 +02006310 weston_color_transform_unref(output->from_blend_to_output);
6311 output->from_blend_to_output = NULL;
6312}
6313
Pekka Paalanen9a0aa372021-06-08 13:38:35 +03006314static bool
6315weston_output_set_color_transforms(struct weston_output *output)
6316{
6317 struct weston_color_manager *cm = output->compositor->color_manager;
6318 struct weston_color_transform *blend_to_output = NULL;
6319 struct weston_color_transform *sRGB_to_output = NULL;
6320 struct weston_color_transform *sRGB_to_blend = NULL;
6321 bool ok;
6322
6323 ok = cm->get_output_color_transform(cm, output, &blend_to_output);
6324 ok = ok && cm->get_sRGB_to_output_color_transform(cm, output,
6325 &sRGB_to_output);
6326 ok = ok && cm->get_sRGB_to_blend_color_transform(cm, output,
6327 &sRGB_to_blend);
6328 if (!ok) {
6329 weston_log("Creating color transformation for output \"%s\" failed.\n",
6330 output->name);
6331 weston_color_transform_unref(blend_to_output);
6332 weston_color_transform_unref(sRGB_to_output);
6333 weston_color_transform_unref(sRGB_to_blend);
6334
6335 return false;
6336 }
6337
6338 weston_output_reset_color_transforms(output);
6339 output->from_blend_to_output = blend_to_output;
6340 output->from_blend_to_output_by_backend = false;
6341 output->from_sRGB_to_output = sRGB_to_output;
6342 output->from_sRGB_to_blend = sRGB_to_blend;
6343
Pekka Paalanen9a9e6ce2021-06-08 14:17:44 +03006344 weston_log("Output '%s' using color profile: %s\n", output->name,
6345 weston_color_profile_get_description(output->color_profile));
6346
Pekka Paalanen9a0aa372021-06-08 13:38:35 +03006347 return true;
6348}
6349
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006350/** Removes output from compositor's list of enabled outputs
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006351 *
6352 * \param output The weston_output object that is being removed.
6353 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006354 * The following happens:
6355 *
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03006356 * - Destroys all paint nodes related to the output.
6357 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006358 * - The output assignments of all views in the current scenegraph are
6359 * recomputed.
6360 *
Pekka Paalanen1d2eee22021-02-25 12:03:28 +02006361 * - Destroys output's color transforms.
6362 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006363 * - Presentation feedback is discarded.
6364 *
6365 * - Compositor is notified that outputs were changed and
6366 * applies the necessary changes to re-layout outputs.
6367 *
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006368 * - The output is put back in the pending outputs list.
6369 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006370 * - Signal is emitted to notify all users of the weston_output
6371 * object that the output is being destroyed.
6372 *
6373 * - wl_output protocol objects referencing this weston_output
Pekka Paalanen2210ad02017-03-30 15:48:06 +03006374 * are made inert, and the wl_output global is removed.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006375 *
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03006376 * - The output's internal ID is released.
6377 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006378 * \ingroup compositor
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006379 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006380 */
6381static void
6382weston_compositor_remove_output(struct weston_output *output)
6383{
Pekka Paalanenbccda712017-03-29 16:16:04 +03006384 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03006385 struct weston_paint_node *pnode, *pntmp;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006386 struct weston_view *view;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03006387 struct weston_head *head;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006388
6389 assert(output->destroying);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006390 assert(output->enabled);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006391
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03006392 wl_list_for_each_safe(pnode, pntmp,
6393 &output->paint_node_list, output_link) {
6394 weston_paint_node_destroy(pnode);
6395 }
Pekka Paalanen2fddc532021-04-30 17:41:29 +03006396 assert(wl_list_empty(&output->paint_node_z_order_list));
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03006397
Pekka Paalanen6551c092021-05-03 16:09:45 +03006398 /*
6399 * Use view_list in case the output did not go through repaint
6400 * after a view came on it, lacking a paint node. Just to be sure.
6401 */
Pekka Paalanenbccda712017-03-29 16:16:04 +03006402 wl_list_for_each(view, &compositor->view_list, link) {
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006403 if (view->output_mask & (1u << output->id))
6404 weston_view_assign_output(view);
6405 }
6406
Pekka Paalanen1d2eee22021-02-25 12:03:28 +02006407 weston_output_reset_color_transforms(output);
6408
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006409 weston_presentation_feedback_discard_list(&output->feedback_list);
6410
Pekka Paalanen9711fd92018-06-21 14:26:18 +03006411 weston_compositor_reflow_outputs(compositor, output, -output->width);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006412
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006413 wl_list_remove(&output->link);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006414 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006415 output->enabled = false;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006416
Marius Vlad2015dfc2021-12-02 11:15:15 +02006417 weston_signal_emit_mutable(&compositor->output_destroyed_signal, output);
6418 weston_signal_emit_mutable(&output->destroy_signal, output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006419
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02006420 wl_list_for_each(head, &output->head_list, output_link)
6421 weston_head_remove_global(head);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03006422
6423 compositor->output_id_pool &= ~(1u << output->id);
6424 output->id = 0xffffffff; /* invalid */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006425}
6426
6427/** Sets the output scale for a given output.
6428 *
6429 * \param output The weston_output object that the scale is set for.
6430 * \param scale Scale factor for the given output.
6431 *
6432 * It only supports setting scale for an output that
6433 * is not enabled and it can only be ran once.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006434 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05006435 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006436 */
6437WL_EXPORT void
6438weston_output_set_scale(struct weston_output *output,
6439 int32_t scale)
6440{
6441 /* We can only set scale on a disabled output */
6442 assert(!output->enabled);
6443
6444 /* We only want to set scale once */
6445 assert(!output->scale);
6446
6447 output->scale = scale;
6448}
6449
6450/** Sets the output transform for a given output.
6451 *
6452 * \param output The weston_output object that the transform is set for.
6453 * \param transform Transform value for the given output.
6454 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006455 * Refer to wl_output::transform section located at
6456 * https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_output
6457 * for list of values that can be passed to this function.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006458 *
Marius Vlad55d87362019-06-11 01:15:35 +03006459 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006460 */
6461WL_EXPORT void
6462weston_output_set_transform(struct weston_output *output,
6463 uint32_t transform)
6464{
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006465 struct weston_pointer_motion_event ev;
6466 struct wl_resource *resource;
6467 struct weston_seat *seat;
6468 pixman_region32_t old_region;
6469 int mid_x, mid_y;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006470 struct weston_head *head;
6471 int ver;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006472
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006473 if (!output->enabled && output->transform == UINT32_MAX) {
6474 output->transform = transform;
6475 return;
6476 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006477
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006478 weston_output_transform_scale_init(output, transform, output->scale);
6479
6480 pixman_region32_init(&old_region);
6481 pixman_region32_copy(&old_region, &output->region);
6482
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006483 weston_output_init_geometry(output, output->x, output->y);
6484
6485 output->dirty = 1;
6486
6487 /* Notify clients of the change for output transform. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006488 wl_list_for_each(head, &output->head_list, output_link) {
6489 wl_resource_for_each(resource, &head->resource_list) {
6490 wl_output_send_geometry(resource,
6491 output->x,
6492 output->y,
6493 head->mm_width,
6494 head->mm_height,
6495 head->subpixel,
6496 head->make,
6497 head->model,
6498 output->transform);
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006499
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006500 ver = wl_resource_get_version(resource);
6501 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
6502 wl_output_send_done(resource);
6503 }
Roman Gilge97391c2019-03-29 13:01:06 +01006504 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
6505 zxdg_output_v1_send_logical_position(resource,
6506 output->x,
6507 output->y);
6508 zxdg_output_v1_send_logical_size(resource,
6509 output->width,
6510 output->height);
6511 zxdg_output_v1_send_done(resource);
6512 }
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006513 }
6514
6515 /* we must ensure that pointers are inside output, otherwise they disappear */
6516 mid_x = output->x + output->width / 2;
6517 mid_y = output->y + output->height / 2;
6518
6519 ev.mask = WESTON_POINTER_MOTION_ABS;
6520 ev.x = wl_fixed_to_double(wl_fixed_from_int(mid_x));
6521 ev.y = wl_fixed_to_double(wl_fixed_from_int(mid_y));
6522
6523 wl_list_for_each(seat, &output->compositor->seat_list, link) {
6524 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
6525
6526 if (pointer && pixman_region32_contains_point(&old_region,
6527 wl_fixed_to_int(pointer->x),
6528 wl_fixed_to_int(pointer->y),
6529 NULL))
6530 weston_pointer_move(pointer, &ev);
6531 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006532}
6533
Pekka Paalanen9a9e6ce2021-06-08 14:17:44 +03006534/** Set output's color profile
6535 *
6536 * \param output The output to change.
6537 * \param cprof The color profile to set. Can be NULL for default sRGB profile.
6538 * \return True on success, or false on failure.
6539 *
6540 * Calling this function changes the color profile of the output. This causes
6541 * all existing weston_color_transform objects related to this output via
6542 * paint nodes to be unreferenced and later re-created on demand.
6543 *
6544 * This function may not be called from within weston_output_repaint().
6545 *
6546 * On failure, nothing is changed.
6547 *
6548 * \ingroup output
6549 */
6550WL_EXPORT bool
6551weston_output_set_color_profile(struct weston_output *output,
6552 struct weston_color_profile *cprof)
6553{
6554 struct weston_color_profile *old;
6555 struct weston_paint_node *pnode;
6556
6557 old = output->color_profile;
6558 output->color_profile = weston_color_profile_ref(cprof);
6559
6560 if (output->enabled) {
6561 if (!weston_output_set_color_transforms(output)) {
6562 /* Failed, roll back */
6563 weston_color_profile_unref(output->color_profile);
6564 output->color_profile = old;
6565 return false;
6566 }
6567
6568 /* Remove outdated cached color transformations */
6569 wl_list_for_each(pnode, &output->paint_node_list, output_link) {
6570 weston_surface_color_transform_fini(&pnode->surf_xform);
6571 pnode->surf_xform_valid = false;
6572 }
6573 }
6574
6575 weston_color_profile_unref(old);
6576
6577 return true;
6578}
6579
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006580/** Initializes a weston_output object with enough data so
6581 ** an output can be configured.
6582 *
6583 * \param output The weston_output object to initialize
6584 * \param compositor The compositor instance.
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006585 * \param name Name for the output (the string is copied).
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006586 *
6587 * Sets initial values for fields that are expected to be
6588 * configured either by compositors or backends.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006589 *
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006590 * The name is used in logs, and can be used by compositors as a configuration
6591 * identifier.
6592 *
Marius Vlad55d87362019-06-11 01:15:35 +03006593 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006594 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006595 */
6596WL_EXPORT void
Armin Krezović40087402016-09-30 14:11:12 +02006597weston_output_init(struct weston_output *output,
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006598 struct weston_compositor *compositor,
6599 const char *name)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006600{
6601 output->compositor = compositor;
6602 output->destroying = 0;
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006603 output->name = strdup(name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006604 wl_list_init(&output->link);
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006605 wl_signal_init(&output->user_destroy_signal);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006606 output->enabled = false;
Ankit Nautiyal2690a772019-03-25 17:57:59 +05306607 output->desired_protection = WESTON_HDCP_DISABLE;
Ankit Nautiyal2844f8e2019-04-03 10:14:59 +05306608 output->allow_protection = true;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006609
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006610 wl_list_init(&output->head_list);
6611
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006612 /* Add some (in)sane defaults which can be used
6613 * for checking if an output was properly configured
6614 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006615 output->scale = 0;
6616 /* Can't use -1 on uint32_t and 0 is valid enum value */
6617 output->transform = UINT32_MAX;
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006618
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006619 pixman_region32_init(&output->region);
Pekka Paalanen42704142017-09-06 16:47:52 +03006620 wl_list_init(&output->mode_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006621}
6622
6623/** Adds weston_output object to pending output list.
6624 *
6625 * \param output The weston_output object to add
6626 * \param compositor The compositor instance.
6627 *
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03006628 * The opposite of this operation is built into weston_output_release().
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006629 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006630 * \ingroup compositor
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006631 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006632 */
6633WL_EXPORT void
6634weston_compositor_add_pending_output(struct weston_output *output,
6635 struct weston_compositor *compositor)
6636{
Pekka Paalanene952a012017-03-29 17:14:00 +03006637 assert(output->disable);
6638 assert(output->enable);
6639
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006640 wl_list_remove(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006641 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006642}
6643
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006644/** Create a string with the attached heads' names.
6645 *
6646 * The string must be free()'d.
Marius Vlad55d87362019-06-11 01:15:35 +03006647 *
6648 * \ingroup output
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006649 */
6650static char *
6651weston_output_create_heads_string(struct weston_output *output)
6652{
6653 FILE *fp;
6654 char *str = NULL;
6655 size_t size = 0;
6656 struct weston_head *head;
6657 const char *sep = "";
6658
6659 fp = open_memstream(&str, &size);
6660 if (!fp)
6661 return NULL;
6662
6663 wl_list_for_each(head, &output->head_list, output_link) {
6664 fprintf(fp, "%s%s", sep, head->name);
6665 sep = ", ";
6666 }
6667 fclose(fp);
6668
6669 return str;
6670}
6671
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006672/** Constructs a weston_output object that can be used by the compositor.
6673 *
Pekka Paalanencc201e42017-03-30 15:11:25 +03006674 * \param output The weston_output object that needs to be enabled. Must not
Pekka Paalanenddce54d2017-08-23 16:00:21 +03006675 * be enabled already. Must have at least one head attached.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006676 *
6677 * Output coordinates are calculated and each new output is by default
6678 * assigned to the right of previous one.
6679 *
6680 * Sets up the transformation, zoom, and geometry of the output using
6681 * the properties that need to be configured by the compositor.
6682 *
6683 * Establishes a repaint timer for the output with the relevant display
6684 * object's event loop. See output_repaint_timer_handler().
6685 *
6686 * The output is assigned an ID. Weston can support up to 32 distinct
6687 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
6688 * is referred to and used to find the first available ID number, and
6689 * then this ID is marked as used in output_id_pool.
6690 *
6691 * The output is also assigned a Wayland global with the wl_output
6692 * external interface.
6693 *
6694 * Backend specific function is called to set up the output output.
6695 *
6696 * Output is added to the compositor's output list
6697 *
6698 * If the backend specific function fails, the weston_output object
6699 * is returned to a state it was before calling this function and
6700 * is added to the compositor's pending_output_list in case it needs
6701 * to be reconfigured or just so it can be destroyed at shutdown.
6702 *
6703 * 0 is returned on success, -1 on failure.
Marius Vlad55d87362019-06-11 01:15:35 +03006704 *
6705 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006706 */
6707WL_EXPORT int
6708weston_output_enable(struct weston_output *output)
6709{
Armin Krezović782f5df2016-09-30 14:11:11 +02006710 struct weston_compositor *c = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006711 struct weston_output *iterator;
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03006712 struct weston_head *head;
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006713 char *head_names;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006714 int x = 0, y = 0;
6715
Pekka Paalanencc201e42017-03-30 15:11:25 +03006716 if (output->enabled) {
6717 weston_log("Error: attempt to enable an enabled output '%s'\n",
6718 output->name);
6719 return -1;
6720 }
6721
Pekka Paalanenddce54d2017-08-23 16:00:21 +03006722 if (wl_list_empty(&output->head_list)) {
6723 weston_log("Error: cannot enable output '%s' without heads.\n",
6724 output->name);
6725 return -1;
6726 }
6727
Pekka Paalanen586e1ac2017-09-14 16:17:59 +03006728 if (wl_list_empty(&output->mode_list) || !output->current_mode) {
6729 weston_log("Error: no video mode for output '%s'.\n",
6730 output->name);
6731 return -1;
6732 }
6733
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03006734 wl_list_for_each(head, &output->head_list, output_link) {
6735 assert(head->make);
6736 assert(head->model);
6737 }
6738
Armin Krezović782f5df2016-09-30 14:11:11 +02006739 iterator = container_of(c->output_list.prev,
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006740 struct weston_output, link);
6741
Armin Krezović782f5df2016-09-30 14:11:11 +02006742 if (!wl_list_empty(&c->output_list))
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006743 x = iterator->x + iterator->width;
6744
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006745 /* Make sure the scale is set up */
6746 assert(output->scale);
6747
6748 /* Make sure we have a transform set */
6749 assert(output->transform != UINT32_MAX);
6750
Armin Krezović782f5df2016-09-30 14:11:11 +02006751 output->x = x;
6752 output->y = y;
6753 output->dirty = 1;
6754 output->original_scale = output->scale;
6755
Marius Vlad3a2f8292019-11-04 17:53:46 +02006756 wl_signal_init(&output->frame_signal);
6757 wl_signal_init(&output->destroy_signal);
6758
Armin Krezović782f5df2016-09-30 14:11:11 +02006759 weston_output_transform_scale_init(output, output->transform, output->scale);
6760 weston_output_init_zoom(output);
6761
6762 weston_output_init_geometry(output, x, y);
6763 weston_output_damage(output);
6764
Armin Krezović782f5df2016-09-30 14:11:11 +02006765 wl_list_init(&output->animation_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02006766 wl_list_init(&output->feedback_list);
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03006767 wl_list_init(&output->paint_node_list);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03006768 wl_list_init(&output->paint_node_z_order_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02006769
Pekka Paalanen9a0aa372021-06-08 13:38:35 +03006770 if (!weston_output_set_color_transforms(output))
Pekka Paalanen1d2eee22021-02-25 12:03:28 +02006771 return -1;
Pekka Paalanen1d2eee22021-02-25 12:03:28 +02006772
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006773 /* Enable the output (set up the crtc or create a
6774 * window representing the output, set up the
6775 * renderer, etc)
6776 */
6777 if (output->enable(output) < 0) {
6778 weston_log("Enabling output \"%s\" failed.\n", output->name);
Pekka Paalanen1d2eee22021-02-25 12:03:28 +02006779 weston_output_reset_color_transforms(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006780 return -1;
6781 }
6782
6783 weston_compositor_add_output(output->compositor, output);
6784
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006785 head_names = weston_output_create_heads_string(output);
6786 weston_log("Output '%s' enabled with head(s) %s\n",
6787 output->name, head_names);
6788 free(head_names);
6789
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006790 return 0;
6791}
6792
6793/** Converts a weston_output object to a pending output state, so it
6794 ** can be configured again or destroyed.
6795 *
6796 * \param output The weston_output object that needs to be disabled.
6797 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006798 * Calls a backend specific function to disable an output, in case
6799 * such function exists.
6800 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03006801 * The backend specific disable function may choose to postpone the disabling
6802 * by returning a negative value, in which case this function returns early.
6803 * In that case the backend will guarantee the output will be disabled soon
6804 * by the backend calling this function again. One must not attempt to re-enable
6805 * the output until that happens.
6806 *
6807 * Otherwise, if the output is being used by the compositor, it is removed
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006808 * from weston's output_list (see weston_compositor_remove_output())
6809 * and is returned to a state it was before weston_output_enable()
6810 * was ran (see weston_output_enable_undo()).
6811 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03006812 * See weston_output_init() for more information on the
6813 * state output is returned to.
Pekka Paalanencc201e42017-03-30 15:11:25 +03006814 *
6815 * If the output has never been enabled yet, this function can still be
6816 * called to ensure that the output is actually turned off rather than left
6817 * in the state it was discovered in.
Marius Vlad55d87362019-06-11 01:15:35 +03006818 *
6819 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006820 */
6821WL_EXPORT void
6822weston_output_disable(struct weston_output *output)
6823{
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006824 /* Should we rename this? */
6825 output->destroying = 1;
6826
Pekka Paalanenc65df642017-03-29 15:45:46 +03006827 /* Disable is called unconditionally also for not-enabled outputs,
6828 * because at compositor start-up, if there is an output that is
6829 * already on but the compositor wants to turn it off, we have to
6830 * forward the turn-off to the backend so it knows to do it.
6831 * The backend cannot initially turn off everything, because it
6832 * would cause unnecessary mode-sets for all outputs the compositor
6833 * wants to be on.
6834 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006835 if (output->disable(output) < 0)
6836 return;
6837
Pekka Paalanen2ec15eb2021-06-09 16:14:45 +03006838 if (output->enabled) {
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006839 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006840
Pekka Paalanen2ec15eb2021-06-09 16:14:45 +03006841 assert(wl_list_empty(&output->paint_node_list));
6842 }
6843
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006844 output->destroying = 0;
6845}
6846
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006847/** Forces a synchronous call to heads_changed hook
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006848 *
6849 * \param compositor The compositor instance
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006850 *
6851 * If there are new or changed heads, calls the heads_changed hook and
6852 * returns after the hook returns.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006853 *
6854 * \ingroup compositor
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006855 */
6856WL_EXPORT void
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006857weston_compositor_flush_heads_changed(struct weston_compositor *compositor)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006858{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03006859 if (compositor->heads_changed_source) {
6860 wl_event_source_remove(compositor->heads_changed_source);
6861 weston_compositor_call_heads_changed(compositor);
6862 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006863}
6864
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006865/** Add destroy callback for an output
6866 *
6867 * \param output The output to watch.
6868 * \param listener The listener to add. The \c notify member must be set.
6869 *
6870 * The listener callback will be called when user destroys an output. This
6871 * may be delayed by a backend in some cases. The main purpose of the
6872 * listener is to allow hooking up custom data to the output. The custom data
6873 * can be fetched via weston_output_get_destroy_listener() followed by
6874 * container_of().
6875 *
6876 * The \c data argument to the notify callback is the weston_output being
6877 * destroyed.
6878 *
6879 * @note This is for the final destruction of an output, not when it gets
6880 * disabled. If you want to keep track of enabled outputs, this is not it.
Marius Vlad55d87362019-06-11 01:15:35 +03006881 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05006882 * \ingroup output
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006883 */
6884WL_EXPORT void
6885weston_output_add_destroy_listener(struct weston_output *output,
6886 struct wl_listener *listener)
6887{
6888 wl_signal_add(&output->user_destroy_signal, listener);
6889}
6890
6891/** Look up destroy listener for an output
6892 *
6893 * \param output The output to query.
6894 * \param notify The notify function used used for the added destroy listener.
6895 * \return The listener, or NULL if not found.
6896 *
6897 * This looks up the previously added destroy listener struct based on the
6898 * notify function it has. The listener can be used to access user data
6899 * through \c container_of().
6900 *
6901 * \sa wl_signal_get() weston_output_add_destroy_listener()
Marius Vlad55d87362019-06-11 01:15:35 +03006902 * \ingroup output
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006903 */
6904WL_EXPORT struct wl_listener *
6905weston_output_get_destroy_listener(struct weston_output *output,
6906 wl_notify_func_t notify)
6907{
6908 return wl_signal_get(&output->user_destroy_signal, notify);
6909}
6910
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006911/** Uninitialize an output
6912 *
6913 * Removes the output from the list of enabled outputs if necessary, but
6914 * does not call the backend's output disable function. The output will no
6915 * longer be in the list of pending outputs either.
6916 *
6917 * All fields of weston_output become uninitialized, i.e. should not be used
6918 * anymore. The caller can free the memory after this.
6919 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05006920 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006921 * \internal
6922 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006923WL_EXPORT void
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03006924weston_output_release(struct weston_output *output)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006925{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006926 struct weston_head *head, *tmp;
6927
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006928 output->destroying = 1;
6929
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006930 wl_signal_emit(&output->user_destroy_signal, output);
6931
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03006932 if (output->idle_repaint_source)
6933 wl_event_source_remove(output->idle_repaint_source);
6934
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006935 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006936 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006937
Pekka Paalanen9a9e6ce2021-06-08 14:17:44 +03006938 weston_color_profile_unref(output->color_profile);
6939
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006940 pixman_region32_fini(&output->region);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006941 wl_list_remove(&output->link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006942
6943 wl_list_for_each_safe(head, tmp, &output->head_list, output_link)
6944 weston_head_detach(head);
6945
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006946 free(output->name);
6947}
6948
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006949/** Find an output by its given name
6950 *
6951 * \param compositor The compositor to search in.
6952 * \param name The output name to search for.
6953 * \return An existing output with the given name, or NULL if not found.
6954 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006955 * \ingroup compositor
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006956 */
6957WL_EXPORT struct weston_output *
6958weston_compositor_find_output_by_name(struct weston_compositor *compositor,
6959 const char *name)
6960{
6961 struct weston_output *output;
6962
6963 wl_list_for_each(output, &compositor->output_list, link)
6964 if (strcmp(output->name, name) == 0)
6965 return output;
6966
6967 wl_list_for_each(output, &compositor->pending_output_list, link)
6968 if (strcmp(output->name, name) == 0)
6969 return output;
6970
6971 return NULL;
6972}
6973
6974/** Create a named output
6975 *
6976 * \param compositor The compositor.
6977 * \param name The name for the output.
6978 * \return A new \c weston_output, or NULL on failure.
6979 *
6980 * This creates a new weston_output that starts with no heads attached.
6981 *
6982 * An output must be configured and it must have at least one head before
6983 * it can be enabled.
6984 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006985 * \ingroup compositor
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006986 */
6987WL_EXPORT struct weston_output *
6988weston_compositor_create_output(struct weston_compositor *compositor,
6989 const char *name)
6990{
6991 assert(compositor->backend->create_output);
6992
6993 if (weston_compositor_find_output_by_name(compositor, name)) {
6994 weston_log("Warning: attempted to create an output with a "
6995 "duplicate name '%s'.\n", name);
6996 return NULL;
6997 }
6998
6999 return compositor->backend->create_output(compositor, name);
7000}
7001
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03007002/** Create an output for an unused head
7003 *
7004 * \param compositor The compositor.
7005 * \param head The head to attach to the output.
7006 * \return A new \c weston_output, or NULL on failure.
7007 *
7008 * This creates a new weston_output that starts with the given head attached.
7009 * The output inherits the name of the head. The head must not be already
7010 * attached to another output.
7011 *
7012 * An output must be configured before it can be enabled.
7013 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007014 * \ingroup compositor
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03007015 */
7016WL_EXPORT struct weston_output *
7017weston_compositor_create_output_with_head(struct weston_compositor *compositor,
7018 struct weston_head *head)
7019{
7020 struct weston_output *output;
7021
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02007022 output = weston_compositor_create_output(compositor, head->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03007023 if (!output)
7024 return NULL;
7025
7026 if (weston_output_attach_head(output, head) < 0) {
Pekka Paalanen42c0e142017-10-27 12:07:49 +03007027 weston_output_destroy(output);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03007028 return NULL;
7029 }
7030
7031 return output;
7032}
7033
7034/** Destroy an output
7035 *
7036 * \param output The output to destroy.
7037 *
7038 * The heads attached to the given output are detached and become unused again.
7039 *
7040 * It is not necessary to explicitly destroy all outputs at compositor exit.
Leandro Ribeiroca640d52020-01-27 19:12:01 -03007041 * weston_compositor_destroy() will automatically destroy any remaining
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03007042 * outputs.
7043 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05007044 * \ingroup output
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03007045 */
7046WL_EXPORT void
7047weston_output_destroy(struct weston_output *output)
7048{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03007049 output->destroy(output);
7050}
7051
Pekka Paalanencf0a4762017-04-04 16:36:07 +03007052/** When you need a head...
7053 *
7054 * This function is a hack, used until all code has been converted to become
7055 * multi-head aware.
7056 *
7057 * \param output The weston_output whose head to get.
7058 * \return The first head in the output's list.
Marius Vlad55d87362019-06-11 01:15:35 +03007059 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05007060 * \ingroup output
Pekka Paalanencf0a4762017-04-04 16:36:07 +03007061 */
7062WL_EXPORT struct weston_head *
7063weston_output_get_first_head(struct weston_output *output)
7064{
7065 if (wl_list_empty(&output->head_list))
7066 return NULL;
7067
7068 return container_of(output->head_list.next,
7069 struct weston_head, output_link);
7070}
7071
Ankit Nautiyal2844f8e2019-04-03 10:14:59 +05307072/** Allow/Disallow content-protection support for an output
7073 *
7074 * This function sets the allow_protection member for an output. Setting of
7075 * this field will allow the compositor to attempt content-protection for this
7076 * output, for a backend that supports the content-protection protocol.
7077 *
7078 * \param output The weston_output for whom the content-protection is to be
7079 * allowed.
7080 * \param allow_protection The bool value which is to be set.
7081 */
7082WL_EXPORT void
7083weston_output_allow_protection(struct weston_output *output,
7084 bool allow_protection)
7085{
7086 output->allow_protection = allow_protection;
7087}
7088
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01007089static void
Roman Gilge97391c2019-03-29 13:01:06 +01007090xdg_output_unlist(struct wl_resource *resource)
7091{
7092 wl_list_remove(wl_resource_get_link(resource));
7093}
7094
7095static void
7096xdg_output_destroy(struct wl_client *client, struct wl_resource *resource)
7097{
7098 wl_resource_destroy(resource);
7099}
7100
7101static const struct zxdg_output_v1_interface xdg_output_interface = {
7102 xdg_output_destroy
7103};
7104
7105static void
7106xdg_output_manager_destroy(struct wl_client *client,
7107 struct wl_resource *resource)
7108{
7109 wl_resource_destroy(resource);
7110}
7111
7112static void
7113xdg_output_manager_get_xdg_output(struct wl_client *client,
7114 struct wl_resource *manager,
7115 uint32_t id,
7116 struct wl_resource *output_resource)
7117{
7118 int version = wl_resource_get_version(manager);
7119 struct weston_head *head = wl_resource_get_user_data(output_resource);
7120 struct weston_output *output = head->output;
7121 struct wl_resource *resource;
7122
7123 resource = wl_resource_create(client, &zxdg_output_v1_interface,
7124 version, id);
7125 if (resource == NULL) {
7126 wl_client_post_no_memory(client);
7127 return;
7128 }
7129
7130 wl_list_insert(&head->xdg_output_resource_list,
7131 wl_resource_get_link(resource));
7132
7133 wl_resource_set_implementation(resource, &xdg_output_interface,
7134 NULL, xdg_output_unlist);
7135
7136 zxdg_output_v1_send_logical_position(resource, output->x, output->y);
7137 zxdg_output_v1_send_logical_size(resource,
7138 output->width,
7139 output->height);
7140 if (version >= ZXDG_OUTPUT_V1_NAME_SINCE_VERSION)
7141 zxdg_output_v1_send_name(resource, head->name);
7142
7143 zxdg_output_v1_send_done(resource);
7144}
7145
7146static const struct zxdg_output_manager_v1_interface xdg_output_manager_interface = {
7147 xdg_output_manager_destroy,
7148 xdg_output_manager_get_xdg_output
7149};
7150
7151static void
7152bind_xdg_output_manager(struct wl_client *client,
7153 void *data, uint32_t version, uint32_t id)
7154{
7155 struct wl_resource *resource;
7156
7157 resource = wl_resource_create(client, &zxdg_output_manager_v1_interface,
7158 version, id);
7159 if (resource == NULL) {
7160 wl_client_post_no_memory(client);
7161 return;
7162 }
7163
7164 wl_resource_set_implementation(resource, &xdg_output_manager_interface,
7165 NULL, NULL);
7166}
7167
7168static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007169destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01007170{
Jonny Lamb74130762013-11-26 18:19:46 +01007171 struct weston_surface *surface =
7172 wl_resource_get_user_data(resource);
7173
Pekka Paalanen4826f872016-04-22 14:14:38 +03007174 if (!surface)
7175 return;
7176
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007177 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02007178 surface->pending.buffer_viewport.buffer.src_width =
7179 wl_fixed_from_int(-1);
7180 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02007181 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01007182}
7183
7184static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007185viewport_destroy(struct wl_client *client,
7186 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01007187{
7188 wl_resource_destroy(resource);
7189}
7190
7191static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007192viewport_set_source(struct wl_client *client,
7193 struct wl_resource *resource,
7194 wl_fixed_t src_x,
7195 wl_fixed_t src_y,
7196 wl_fixed_t src_width,
7197 wl_fixed_t src_height)
7198{
7199 struct weston_surface *surface =
7200 wl_resource_get_user_data(resource);
7201
Pekka Paalanen4826f872016-04-22 14:14:38 +03007202 if (!surface) {
7203 wl_resource_post_error(resource,
7204 WP_VIEWPORT_ERROR_NO_SURFACE,
7205 "wl_surface for this viewport is no longer exists");
7206 return;
7207 }
7208
7209 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03007210 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007211
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007212 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03007213 src_height == wl_fixed_from_int(-1) &&
7214 src_x == wl_fixed_from_int(-1) &&
7215 src_y == wl_fixed_from_int(-1)) {
7216 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007217 surface->pending.buffer_viewport.buffer.src_width =
7218 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02007219 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007220 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007221 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007222
Pekka Paalanen201769a2016-04-26 14:42:11 +03007223 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007224 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007225 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03007226 "wl_surface@%d viewport source "
7227 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
7228 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007229 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03007230 wl_fixed_to_double(src_height),
7231 wl_fixed_to_double(src_x),
7232 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007233 return;
7234 }
7235
7236 surface->pending.buffer_viewport.buffer.src_x = src_x;
7237 surface->pending.buffer_viewport.buffer.src_y = src_y;
7238 surface->pending.buffer_viewport.buffer.src_width = src_width;
7239 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02007240 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007241}
7242
7243static void
7244viewport_set_destination(struct wl_client *client,
7245 struct wl_resource *resource,
7246 int32_t dst_width,
7247 int32_t dst_height)
7248{
7249 struct weston_surface *surface =
7250 wl_resource_get_user_data(resource);
7251
Pekka Paalanen4826f872016-04-22 14:14:38 +03007252 if (!surface) {
7253 wl_resource_post_error(resource,
7254 WP_VIEWPORT_ERROR_NO_SURFACE,
7255 "wl_surface for this viewport no longer exists");
7256 return;
7257 }
7258
7259 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007260
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007261 if (dst_width == -1 && dst_height == -1) {
7262 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007263 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02007264 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007265 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007266 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007267
7268 if (dst_width <= 0 || dst_height <= 0) {
7269 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007270 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007271 "destination size must be positive (%dx%d)",
7272 dst_width, dst_height);
7273 return;
7274 }
7275
7276 surface->pending.buffer_viewport.surface.width = dst_width;
7277 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02007278 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007279}
7280
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007281static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007282 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007283 viewport_set_source,
7284 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01007285};
7286
7287static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007288viewporter_destroy(struct wl_client *client,
7289 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01007290{
7291 wl_resource_destroy(resource);
7292}
7293
7294static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007295viewporter_get_viewport(struct wl_client *client,
7296 struct wl_resource *viewporter,
7297 uint32_t id,
7298 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01007299{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007300 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007301 struct weston_surface *surface =
7302 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01007303 struct wl_resource *resource;
7304
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007305 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007306 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007307 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007308 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01007309 return;
7310 }
7311
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007312 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007313 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01007314 if (resource == NULL) {
7315 wl_client_post_no_memory(client);
7316 return;
7317 }
7318
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007319 wl_resource_set_implementation(resource, &viewport_interface,
7320 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01007321
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007322 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01007323}
7324
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007325static const struct wp_viewporter_interface viewporter_interface = {
7326 viewporter_destroy,
7327 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01007328};
7329
7330static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007331bind_viewporter(struct wl_client *client,
7332 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01007333{
7334 struct wl_resource *resource;
7335
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007336 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06007337 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01007338 if (resource == NULL) {
7339 wl_client_post_no_memory(client);
7340 return;
7341 }
7342
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007343 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01007344 NULL, NULL);
7345}
7346
7347static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04007348destroy_presentation_feedback(struct wl_resource *feedback_resource)
7349{
7350 struct weston_presentation_feedback *feedback;
7351
7352 feedback = wl_resource_get_user_data(feedback_resource);
7353
7354 wl_list_remove(&feedback->link);
7355 free(feedback);
7356}
7357
7358static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007359presentation_destroy(struct wl_client *client, struct wl_resource *resource)
7360{
7361 wl_resource_destroy(resource);
7362}
7363
7364static void
7365presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04007366 struct wl_resource *presentation_resource,
7367 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007368 uint32_t callback)
7369{
Pekka Paalanen133e4392014-09-23 22:08:46 -04007370 struct weston_surface *surface;
7371 struct weston_presentation_feedback *feedback;
7372
7373 surface = wl_resource_get_user_data(surface_resource);
7374
Bryce Harringtonde16d892014-11-20 22:21:57 -08007375 feedback = zalloc(sizeof *feedback);
7376 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04007377 goto err_calloc;
7378
7379 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007380 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04007381 1, callback);
7382 if (!feedback->resource)
7383 goto err_create;
7384
7385 wl_resource_set_implementation(feedback->resource, NULL, feedback,
7386 destroy_presentation_feedback);
7387
7388 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
7389
7390 return;
7391
7392err_create:
7393 free(feedback);
7394
7395err_calloc:
7396 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007397}
7398
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007399static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007400 presentation_destroy,
7401 presentation_feedback
7402};
7403
7404static void
7405bind_presentation(struct wl_client *client,
7406 void *data, uint32_t version, uint32_t id)
7407{
7408 struct weston_compositor *compositor = data;
7409 struct wl_resource *resource;
7410
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007411 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06007412 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007413 if (resource == NULL) {
7414 wl_client_post_no_memory(client);
7415 return;
7416 }
7417
7418 wl_resource_set_implementation(resource, &presentation_implementation,
7419 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007420 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007421}
7422
7423static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05007424compositor_bind(struct wl_client *client,
7425 void *data, uint32_t version, uint32_t id)
7426{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007427 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05007428 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05007429
Jason Ekstranda85118c2013-06-27 20:17:02 -05007430 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06007431 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07007432 if (resource == NULL) {
7433 wl_client_post_no_memory(client);
7434 return;
7435 }
7436
7437 wl_resource_set_implementation(resource, &compositor_interface,
7438 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05007439}
7440
Daniel Stonece62cb32018-07-20 09:46:24 +01007441static const char *
7442output_repaint_status_text(struct weston_output *output)
7443{
7444 switch (output->repaint_status) {
7445 case REPAINT_NOT_SCHEDULED:
7446 return "no repaint";
7447 case REPAINT_BEGIN_FROM_IDLE:
7448 return "start_repaint_loop scheduled";
7449 case REPAINT_SCHEDULED:
7450 return "repaint scheduled";
7451 case REPAINT_AWAITING_COMPLETION:
7452 return "awaiting completion";
7453 }
7454
7455 assert(!"output_repaint_status_text missing enum");
7456 return NULL;
7457}
7458
7459static void
7460debug_scene_view_print_buffer(FILE *fp, struct weston_view *view)
7461{
7462 struct weston_buffer *buffer = view->surface->buffer_ref.buffer;
7463 struct wl_shm_buffer *shm;
7464 struct linux_dmabuf_buffer *dmabuf;
Marius Vlad00a6e012018-11-20 17:52:31 +02007465 const struct pixel_format_info *pixel_info = NULL;
Daniel Stonece62cb32018-07-20 09:46:24 +01007466
7467 if (!buffer) {
7468 fprintf(fp, "\t\t[buffer not available]\n");
7469 return;
7470 }
7471
7472 shm = wl_shm_buffer_get(buffer->resource);
7473 if (shm) {
Marius Vlad00a6e012018-11-20 17:52:31 +02007474 uint32_t _format = wl_shm_buffer_get_format(shm);
7475 pixel_info = pixel_format_get_info_shm(_format);
Daniel Stonece62cb32018-07-20 09:46:24 +01007476 fprintf(fp, "\t\tSHM buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02007477 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
7478 (unsigned long) _format,
7479 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Daniel Stonece62cb32018-07-20 09:46:24 +01007480 return;
7481 }
7482
7483 dmabuf = linux_dmabuf_buffer_get(buffer->resource);
7484 if (dmabuf) {
Marius Vlad6f6fd262021-07-12 12:58:34 +03007485 uint64_t modifier = dmabuf->attributes.modifier[0];
7486 char *modifier_name = pixel_format_get_modifier(modifier);
Marius Vlad00a6e012018-11-20 17:52:31 +02007487 pixel_info = pixel_format_get_info(dmabuf->attributes.format);
Daniel Stonece62cb32018-07-20 09:46:24 +01007488 fprintf(fp, "\t\tdmabuf buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02007489 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
7490 (unsigned long) dmabuf->attributes.format,
7491 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Marius Vlad6f6fd262021-07-12 12:58:34 +03007492
7493 fprintf(fp, "\t\t\tmodifier: %s\n", modifier_name ? modifier_name :
7494 "Failed to convert to a modifier name");
7495 free(modifier_name);
Daniel Stonece62cb32018-07-20 09:46:24 +01007496 return;
7497 }
7498
Marius Vlad253ba9a2019-03-17 18:22:21 +02007499 fprintf(fp, "\t\tEGL buffer\n");
Daniel Stonece62cb32018-07-20 09:46:24 +01007500}
7501
7502static void
7503debug_scene_view_print(FILE *fp, struct weston_view *view, int view_idx)
7504{
7505 struct weston_compositor *ec = view->surface->compositor;
7506 struct weston_output *output;
7507 char desc[512];
7508 pixman_box32_t *box;
7509 uint32_t surface_id = 0;
7510 pid_t pid = 0;
7511
7512 if (view->surface->resource) {
7513 struct wl_resource *resource = view->surface->resource;
7514 wl_client_get_credentials(wl_resource_get_client(resource),
7515 &pid, NULL, NULL);
7516 surface_id = wl_resource_get_id(view->surface->resource);
7517 }
7518
7519 if (!view->surface->get_label ||
7520 view->surface->get_label(view->surface, desc, sizeof(desc)) < 0) {
7521 strcpy(desc, "[no description available]");
7522 }
7523 fprintf(fp, "\tView %d (role %s, PID %d, surface ID %u, %s, %p):\n",
7524 view_idx, view->surface->role_name, pid, surface_id,
7525 desc, view);
7526
7527 box = pixman_region32_extents(&view->transform.boundingbox);
7528 fprintf(fp, "\t\tposition: (%d, %d) -> (%d, %d)\n",
7529 box->x1, box->y1, box->x2, box->y2);
7530 box = pixman_region32_extents(&view->transform.opaque);
7531
Michael Olbrichb7e5f102020-08-11 16:33:35 +02007532 if (weston_view_is_opaque(view, &view->transform.boundingbox)) {
Daniel Stonece62cb32018-07-20 09:46:24 +01007533 fprintf(fp, "\t\t[fully opaque]\n");
7534 } else if (!pixman_region32_not_empty(&view->transform.opaque)) {
7535 fprintf(fp, "\t\t[not opaque]\n");
7536 } else {
7537 fprintf(fp, "\t\t[opaque: (%d, %d) -> (%d, %d)]\n",
7538 box->x1, box->y1, box->x2, box->y2);
7539 }
7540
7541 if (view->alpha < 1.0)
7542 fprintf(fp, "\t\talpha: %f\n", view->alpha);
7543
7544 if (view->output_mask != 0) {
7545 bool first_output = true;
7546 fprintf(fp, "\t\toutputs: ");
7547 wl_list_for_each(output, &ec->output_list, link) {
7548 if (!(view->output_mask & (1 << output->id)))
7549 continue;
7550 fprintf(fp, "%s%d (%s)%s",
7551 (first_output) ? "" : ", ",
7552 output->id, output->name,
7553 (view->output == output) ? " (primary)" : "");
7554 first_output = false;
7555 }
7556 } else {
7557 fprintf(fp, "\t\t[no outputs]");
7558 }
7559
7560 fprintf(fp, "\n");
7561
7562 debug_scene_view_print_buffer(fp, view);
7563}
7564
Marius Vlad433f4e72019-02-17 22:14:23 +02007565static void
7566debug_scene_view_print_tree(struct weston_view *view,
Marius Vlada6acfa82019-03-17 18:10:09 +02007567 FILE *fp, int *view_idx)
Marius Vlad433f4e72019-02-17 22:14:23 +02007568{
7569 struct weston_subsurface *sub;
7570 struct weston_view *ev;
7571
7572 /*
7573 * print the view first, then we recursively go on printing
7574 * sub-surfaces. We bail out once no more sub-surfaces are available.
7575 */
Marius Vlada6acfa82019-03-17 18:10:09 +02007576 debug_scene_view_print(fp, view, *view_idx);
Marius Vlad433f4e72019-02-17 22:14:23 +02007577
7578 /* no more sub-surfaces */
7579 if (wl_list_empty(&view->surface->subsurface_list))
7580 return;
7581
7582 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
7583 wl_list_for_each(ev, &sub->surface->views, surface_link) {
Michael Olbrichef5f3232020-04-29 09:03:15 +02007584 /* only print the child views of the current view */
7585 if (ev->parent_view != view)
Marius Vlad433f4e72019-02-17 22:14:23 +02007586 continue;
Marius Vlada6acfa82019-03-17 18:10:09 +02007587
7588 (*view_idx)++;
Marius Vlad433f4e72019-02-17 22:14:23 +02007589 debug_scene_view_print_tree(ev, fp, view_idx);
7590 }
7591 }
7592}
7593
Daniel Stonece62cb32018-07-20 09:46:24 +01007594/**
7595 * Output information on how libweston is currently composing the scene
7596 * graph.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007597 *
7598 * \ingroup compositor
Daniel Stonece62cb32018-07-20 09:46:24 +01007599 */
7600WL_EXPORT char *
7601weston_compositor_print_scene_graph(struct weston_compositor *ec)
7602{
7603 struct weston_output *output;
7604 struct weston_layer *layer;
7605 struct timespec now;
7606 int layer_idx = 0;
7607 FILE *fp;
7608 char *ret;
7609 size_t len;
7610 int err;
7611
7612 fp = open_memstream(&ret, &len);
7613 assert(fp);
7614
7615 weston_compositor_read_presentation_clock(ec, &now);
7616 fprintf(fp, "Weston scene graph at %ld.%09ld:\n\n",
7617 now.tv_sec, now.tv_nsec);
7618
7619 wl_list_for_each(output, &ec->output_list, link) {
7620 struct weston_head *head;
7621 int head_idx = 0;
7622
7623 fprintf(fp, "Output %d (%s):\n", output->id, output->name);
7624 assert(output->enabled);
7625
7626 fprintf(fp, "\tposition: (%d, %d) -> (%d, %d)\n",
7627 output->x, output->y,
7628 output->x + output->width,
7629 output->y + output->height);
7630 fprintf(fp, "\tmode: %dx%d@%.3fHz\n",
7631 output->current_mode->width,
7632 output->current_mode->height,
7633 output->current_mode->refresh / 1000.0);
7634 fprintf(fp, "\tscale: %d\n", output->scale);
7635
7636 fprintf(fp, "\trepaint status: %s\n",
7637 output_repaint_status_text(output));
7638 if (output->repaint_status == REPAINT_SCHEDULED)
7639 fprintf(fp, "\tnext repaint: %ld.%09ld\n",
7640 output->next_repaint.tv_sec,
7641 output->next_repaint.tv_nsec);
7642
7643 wl_list_for_each(head, &output->head_list, output_link) {
7644 fprintf(fp, "\tHead %d (%s): %sconnected\n",
7645 head_idx++, head->name,
7646 (head->connected) ? "" : "not ");
7647 }
7648 }
7649
7650 fprintf(fp, "\n");
7651
7652 wl_list_for_each(layer, &ec->layer_list, link) {
7653 struct weston_view *view;
7654 int view_idx = 0;
7655
7656 fprintf(fp, "Layer %d (pos 0x%lx):\n", layer_idx++,
7657 (unsigned long) layer->position);
7658
7659 if (!weston_layer_mask_is_infinite(layer)) {
7660 fprintf(fp, "\t[mask: (%d, %d) -> (%d,%d)]\n\n",
7661 layer->mask.x1, layer->mask.y1,
7662 layer->mask.x2, layer->mask.y2);
7663 }
7664
Marius Vlada6acfa82019-03-17 18:10:09 +02007665 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
7666 debug_scene_view_print_tree(view, fp, &view_idx);
7667 view_idx++;
7668 }
Daniel Stonece62cb32018-07-20 09:46:24 +01007669
7670 if (wl_list_empty(&layer->view_list.link))
7671 fprintf(fp, "\t[no views]\n");
7672
7673 fprintf(fp, "\n");
7674 }
7675
7676 err = fclose(fp);
7677 assert(err == 0);
7678
7679 return ret;
7680}
7681
7682/**
7683 * Called when the 'scene-graph' debug scope is bound by a client. This
7684 * one-shot weston-debug scope prints the current scene graph when bound,
7685 * and then terminates the stream.
7686 */
7687static void
Marius Vladdad882a2019-07-17 15:43:53 +03007688debug_scene_graph_cb(struct weston_log_subscription *sub, void *data)
Daniel Stonece62cb32018-07-20 09:46:24 +01007689{
7690 struct weston_compositor *ec = data;
7691 char *str = weston_compositor_print_scene_graph(ec);
7692
Marius Vladdad882a2019-07-17 15:43:53 +03007693 weston_log_subscription_printf(sub, "%s", str);
Daniel Stonece62cb32018-07-20 09:46:24 +01007694 free(str);
Marius Vladdad882a2019-07-17 15:43:53 +03007695 weston_log_subscription_complete(sub);
Daniel Stonece62cb32018-07-20 09:46:24 +01007696}
7697
Leandro Ribeiro32a5acd2020-10-19 16:06:22 -03007698/** Retrieve testsuite data from compositor
7699 *
7700 * The testsuite data can be defined by the test suite of projects that uses
7701 * libweston and given to the compositor at the moment of its creation. This
7702 * function should be used when we need to retrieve the testsuite private data
7703 * from the compositor.
7704 *
7705 * \param ec The weston compositor.
7706 * \return The testsuite data.
7707 *
7708 * \ingroup compositor
7709 * \sa weston_compositor_test_data_init
7710 */
7711WL_EXPORT void *
7712weston_compositor_get_test_data(struct weston_compositor *ec)
7713{
7714 return ec->test_data.test_private_data;
7715}
7716
Giulio Camuffo459137b2014-10-11 23:56:24 +03007717/** Create the compositor.
7718 *
7719 * This functions creates and initializes a compositor instance.
7720 *
7721 * \param display The Wayland display to be used.
7722 * \param user_data A pointer to an object that can later be retrieved
Marius Vlada2dace22019-06-12 16:05:44 +03007723 * \param log_ctx A pointer to weston_debug_compositor
Leandro Ribeirocdb72182020-11-06 11:24:22 -03007724 * \param test_data Optional testsuite data, or NULL.
Giulio Camuffo459137b2014-10-11 23:56:24 +03007725 * using the \ref weston_compositor_get_user_data function.
7726 * \return The compositor instance on success or NULL on failure.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007727 *
7728 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007729 */
7730WL_EXPORT struct weston_compositor *
Marius Vlad880b4852019-04-07 17:07:58 +03007731weston_compositor_create(struct wl_display *display,
Leandro Ribeirocdb72182020-11-06 11:24:22 -03007732 struct weston_log_context *log_ctx, void *user_data,
7733 const struct weston_testsuite_data *test_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04007734{
Giulio Camuffo459137b2014-10-11 23:56:24 +03007735 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05007736 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07007737
Leandro Ribeirobd9c0a62020-01-17 10:47:49 -03007738 if (!log_ctx)
7739 return NULL;
7740
Giulio Camuffo459137b2014-10-11 23:56:24 +03007741 ec = zalloc(sizeof *ec);
7742 if (!ec)
7743 return NULL;
7744
Leandro Ribeirocdb72182020-11-06 11:24:22 -03007745 if (test_data)
7746 ec->test_data = *test_data;
7747
Leandro Ribeirobd9c0a62020-01-17 10:47:49 -03007748 ec->weston_log_ctx = log_ctx;
Giulio Camuffo459137b2014-10-11 23:56:24 +03007749 ec->wl_display = display;
7750 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04007751 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07007752 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04007753 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03007754 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03007755 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02007756 wl_signal_init(&ec->idle_signal);
7757 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02007758 wl_signal_init(&ec->show_input_panel_signal);
7759 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02007760 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01007761 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01007762 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02007763 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02007764 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02007765 wl_signal_init(&ec->output_resized_signal);
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03007766 wl_signal_init(&ec->heads_changed_signal);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02007767 wl_signal_init(&ec->output_heads_changed_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07007768 wl_signal_init(&ec->session_signal);
Robert Beckettc569bdc2019-07-10 16:40:12 +01007769 ec->session_active = true;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04007770
Casey Dahlin58ba1372012-04-19 22:50:08 -04007771 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03007772 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04007773
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02007774 ec->activate_serial = 1;
7775
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05007776 ec->touch_mode = WESTON_TOUCH_MODE_NORMAL;
7777
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05307778 ec->content_protection = NULL;
7779
Derek Foreman152254b2015-11-26 14:17:48 -06007780 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04007781 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007782 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05007783
Giulio Camuffo954f1832014-10-11 18:27:30 +03007784 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04007785 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007786 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03007787
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007788 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007789 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007790 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01007791
Roman Gilge97391c2019-03-29 13:01:06 +01007792 if (!wl_global_create(ec->wl_display, &zxdg_output_manager_v1_interface, 2,
7793 ec, bind_xdg_output_manager))
7794 goto fail;
7795
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007796 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007797 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007798 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007799
Jonas Ådahl30d61d82014-10-22 21:21:17 +02007800 if (weston_input_init(ec) != 0)
7801 goto fail;
7802
Jason Ekstranda7af7042013-10-12 22:38:11 -05007803 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02007804 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007805 wl_list_init(&ec->layer_list);
7806 wl_list_init(&ec->seat_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007807 wl_list_init(&ec->pending_output_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007808 wl_list_init(&ec->output_list);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03007809 wl_list_init(&ec->head_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007810 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01007811 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007812 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01007813 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007814 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02007815 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007816
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007817 wl_list_init(&ec->plugin_api_list);
7818
Xiong Zhang97116532013-10-23 13:58:31 +08007819 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02007820 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04007821
Giulio Camuffo459137b2014-10-11 23:56:24 +03007822 wl_data_device_manager_init(ec->wl_display);
7823
7824 wl_display_init_shm(ec->wl_display);
7825
7826 loop = wl_display_get_event_loop(ec->wl_display);
7827 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Daniel Stone6847b852017-03-01 11:34:08 +00007828 ec->repaint_timer =
7829 wl_event_loop_add_timer(loop, output_repaint_timer_handler,
7830 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007831
Quentin Glidic82681572016-12-17 13:40:51 +01007832 weston_layer_init(&ec->fade_layer, ec);
7833 weston_layer_init(&ec->cursor_layer, ec);
7834
7835 weston_layer_set_position(&ec->fade_layer, WESTON_LAYER_POSITION_FADE);
7836 weston_layer_set_position(&ec->cursor_layer,
7837 WESTON_LAYER_POSITION_CURSOR);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007838
Daniel Stonece62cb32018-07-20 09:46:24 +01007839 ec->debug_scene =
Leandro Ribeiroce100192019-12-26 16:35:49 -03007840 weston_compositor_add_log_scope(ec, "scene-graph",
7841 "Scene graph details\n",
7842 debug_scene_graph_cb, NULL,
7843 ec);
Daniel Stonece62cb32018-07-20 09:46:24 +01007844
Marius Vladda104eb2019-09-05 14:31:01 +03007845 ec->timeline =
Leandro Ribeiroce100192019-12-26 16:35:49 -03007846 weston_compositor_add_log_scope(ec, "timeline",
7847 "Timeline event points\n",
7848 weston_timeline_create_subscription,
7849 weston_timeline_destroy_subscription,
7850 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007851 return ec;
7852
7853fail:
7854 free(ec);
7855 return NULL;
7856}
7857
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007858/** weston_compositor_shutdown
7859 * \ingroup compositor
7860 */
Benjamin Franzkeb8263022011-08-30 11:32:47 +02007861WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007862weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07007863{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007864 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07007865
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007866 wl_event_source_remove(ec->idle_source);
Lujin Wangd4b46cd2019-08-12 12:03:29 -07007867 wl_event_source_remove(ec->repaint_timer);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007868
Matt Roper361d2ad2011-08-29 13:52:23 -07007869 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02007870 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07007871 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02007872
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007873 /* Destroy all pending outputs associated with this compositor */
7874 wl_list_for_each_safe(output, next, &ec->pending_output_list, link)
7875 output->destroy(output);
7876
Pekka Paalanen90a5ffa2021-02-25 12:03:28 +02007877 /* Color manager objects may have renderer hooks */
7878 if (ec->color_manager) {
7879 ec->color_manager->destroy(ec->color_manager);
7880 ec->color_manager = NULL;
7881 }
7882
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02007883 if (ec->renderer)
7884 ec->renderer->destroy(ec);
7885
Daniel Stone325fc2d2012-05-30 16:31:58 +01007886 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09007887 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01007888 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01007889 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01007890 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02007891 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007892
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04007893 weston_plane_release(&ec->primary_plane);
Pekka Paalanen87400372021-05-14 14:29:40 +03007894
7895 weston_layer_fini(&ec->fade_layer);
7896 weston_layer_fini(&ec->cursor_layer);
7897
7898 if (!wl_list_empty(&ec->layer_list))
7899 weston_log("BUG: layer_list is not empty after shutdown. Calls to weston_layer_fini() are missing somwhere.\n");
Matt Roper361d2ad2011-08-29 13:52:23 -07007900}
7901
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007902/** weston_compositor_exit_with_code
7903 * \ingroup compositor
7904 */
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05007905WL_EXPORT void
Adam Jackson3c3f3b12019-10-16 16:02:59 -04007906weston_compositor_exit_with_code(struct weston_compositor *compositor,
Frederic Plourdec336f062014-10-29 14:44:33 -04007907 int exit_code)
7908{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02007909 if (compositor->exit_code == EXIT_SUCCESS)
7910 compositor->exit_code = exit_code;
7911
Giulio Camuffo459137b2014-10-11 23:56:24 +03007912 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04007913}
7914
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007915/** weston_compositor_set_default_pointer_grab
7916 * \ingroup compositor
7917 */
Frederic Plourdec336f062014-10-29 14:44:33 -04007918WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007919weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
7920 const struct weston_pointer_grab_interface *interface)
7921{
7922 struct weston_seat *seat;
7923
7924 ec->default_pointer_grab = interface;
7925 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05007926 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
7927
7928 if (pointer)
7929 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007930 }
7931}
7932
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007933/** weston_compositor_set_presentation_clock
7934 * \ingroup compositor
7935 */
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007936WL_EXPORT int
7937weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
7938 clockid_t clk_id)
7939{
7940 struct timespec ts;
7941
7942 if (clock_gettime(clk_id, &ts) < 0)
7943 return -1;
7944
7945 compositor->presentation_clock = clk_id;
7946
7947 return 0;
7948}
7949
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007950/** For choosing the software clock, when the display hardware or API
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007951 * does not expose a compatible presentation timestamp.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007952 *
7953 * \ingroup compositor
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007954 */
7955WL_EXPORT int
7956weston_compositor_set_presentation_clock_software(
7957 struct weston_compositor *compositor)
7958{
7959 /* In order of preference */
7960 static const clockid_t clocks[] = {
7961 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
7962 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
7963 CLOCK_MONOTONIC, /* no jumps, may crawl */
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007964 };
7965 unsigned i;
7966
7967 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
7968 if (weston_compositor_set_presentation_clock(compositor,
7969 clocks[i]) == 0)
7970 return 0;
7971
7972 weston_log("Error: no suitable presentation clock available.\n");
7973
7974 return -1;
7975}
7976
Pekka Paalanen662f3842015-03-18 12:17:26 +02007977/** Read the current time from the Presentation clock
7978 *
7979 * \param compositor
Marius Vlada2dace22019-06-12 16:05:44 +03007980 * \param[out] ts The current time.
Pekka Paalanen662f3842015-03-18 12:17:26 +02007981 *
7982 * \note Reading the current time in user space is always imprecise to some
7983 * degree.
7984 *
7985 * This function is never meant to fail. If reading the clock does fail,
7986 * an error message is logged and a zero time is returned. Callers are not
7987 * supposed to detect or react to failures.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007988 *
7989 * \ingroup compositor
Pekka Paalanen662f3842015-03-18 12:17:26 +02007990 */
7991WL_EXPORT void
7992weston_compositor_read_presentation_clock(
7993 const struct weston_compositor *compositor,
7994 struct timespec *ts)
7995{
7996 static bool warned;
7997 int ret;
7998
7999 ret = clock_gettime(compositor->presentation_clock, ts);
8000 if (ret < 0) {
8001 ts->tv_sec = 0;
8002 ts->tv_nsec = 0;
8003
8004 if (!warned)
8005 weston_log("Error: failure to read "
Antonio Borneo39578632019-04-26 23:57:31 +02008006 "the presentation clock %#x: '%s' (%d)\n",
8007 compositor->presentation_clock,
8008 strerror(errno), errno);
Pekka Paalanen662f3842015-03-18 12:17:26 +02008009 warned = true;
8010 }
8011}
8012
Pekka Paalanen230f3b12014-09-29 14:18:40 -04008013/** Import dmabuf buffer into current renderer
8014 *
8015 * \param compositor
8016 * \param buffer the dmabuf buffer to import
8017 * \return true on usable buffers, false otherwise
8018 *
8019 * This function tests that the linux_dmabuf_buffer is usable
8020 * for the current renderer. Returns false on unusable buffers. Usually
8021 * usability is tested by importing the dmabufs for composition.
8022 *
8023 * This hook is also used for detecting if the renderer supports
8024 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
8025 * supported.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008026 *
8027 * \ingroup compositor
8028 */
Pekka Paalanen230f3b12014-09-29 14:18:40 -04008029WL_EXPORT bool
8030weston_compositor_import_dmabuf(struct weston_compositor *compositor,
8031 struct linux_dmabuf_buffer *buffer)
8032{
8033 struct weston_renderer *renderer;
8034
8035 renderer = compositor->renderer;
8036
8037 if (renderer->import_dmabuf == NULL)
8038 return false;
8039
8040 return renderer->import_dmabuf(compositor, buffer);
8041}
8042
Marius Vlad5a701542019-11-16 20:26:52 +02008043WL_EXPORT bool
8044weston_compositor_dmabuf_can_scanout(struct weston_compositor *compositor,
8045 struct linux_dmabuf_buffer *buffer)
8046{
8047 struct weston_backend *backend = compositor->backend;
8048
8049 if (backend->can_scanout_dmabuf == NULL)
8050 return false;
8051
8052 return backend->can_scanout_dmabuf(compositor, buffer);
8053}
8054
Giulio Camuffocdb4d292013-11-14 23:42:53 +01008055WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05008056weston_version(int *major, int *minor, int *micro)
8057{
8058 *major = WESTON_VERSION_MAJOR;
8059 *minor = WESTON_VERSION_MINOR;
8060 *micro = WESTON_VERSION_MICRO;
8061}
8062
Daniel Stonee03c1112016-11-24 20:45:45 +00008063/**
8064 * Attempts to find a module path from the module map specified in the
8065 * environment. If found, writes the full path into the path variable.
8066 *
8067 * The module map is a string in environment variable WESTON_MODULE_MAP, where
8068 * each entry is of the form "name=path" and entries are separated by
8069 * semicolons. Whitespace is significant.
8070 *
8071 * \param name The name to search for.
8072 * \param path Where the path is written to if found.
8073 * \param path_len Allocated bytes at \c path .
8074 * \returns The length of the string written to path on success, or 0 if the
8075 * module was not specified in the environment map or path_len was too small.
8076 */
8077WL_EXPORT size_t
8078weston_module_path_from_env(const char *name, char *path, size_t path_len)
8079{
8080 const char *mapping = getenv("WESTON_MODULE_MAP");
8081 const char *end;
8082 const int name_len = strlen(name);
8083
8084 if (!mapping)
8085 return 0;
8086
8087 end = mapping + strlen(mapping);
8088 while (mapping < end && *mapping) {
8089 const char *filename, *next;
8090
8091 /* early out: impossibly short string */
8092 if (end - mapping < name_len + 1)
8093 return 0;
8094
8095 filename = &mapping[name_len + 1];
8096 next = strchrnul(mapping, ';');
8097
8098 if (strncmp(mapping, name, name_len) == 0 &&
8099 mapping[name_len] == '=') {
8100 size_t file_len = next - filename; /* no trailing NUL */
8101 if (file_len >= path_len)
8102 return 0;
8103 strncpy(path, filename, file_len);
8104 path[file_len] = '\0';
8105 return file_len;
8106 }
8107
8108 mapping = next + 1;
8109 }
8110
8111 return 0;
8112}
8113
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03008114WL_EXPORT void *
8115weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04008116{
8117 char path[PATH_MAX];
8118 void *module, *init;
Daniel Stonebeb97e52016-11-28 12:13:54 +00008119 size_t len;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04008120
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08008121 if (name == NULL)
8122 return NULL;
8123
Derek Foreman3f86e502015-06-08 11:46:54 -05008124 if (name[0] != '/') {
Daniel Stonee03c1112016-11-24 20:45:45 +00008125 len = weston_module_path_from_env(name, path, sizeof path);
8126 if (len == 0)
Daniel Stonebeb97e52016-11-28 12:13:54 +00008127 len = snprintf(path, sizeof path, "%s/%s",
8128 LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05008129 } else {
Daniel Stonebeb97e52016-11-28 12:13:54 +00008130 len = snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05008131 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04008132
Daniel Stonebeb97e52016-11-28 12:13:54 +00008133 /* snprintf returns the length of the string it would've written,
8134 * _excluding_ the NUL byte. So even being equal to the size of
8135 * our buffer is an error here. */
8136 if (len >= sizeof path)
8137 return NULL;
8138
Kristian Høgsberga6813d22012-09-12 12:21:01 -04008139 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
8140 if (module) {
8141 weston_log("Module '%s' already loaded\n", path);
Pekka Paalanenf696ee92019-11-04 14:07:51 +02008142 } else {
8143 weston_log("Loading module '%s'\n", path);
8144 module = dlopen(path, RTLD_NOW);
8145 if (!module) {
8146 weston_log("Failed to load module: %s\n", dlerror());
8147 return NULL;
8148 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04008149 }
8150
8151 init = dlsym(module, entrypoint);
8152 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03008153 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00008154 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04008155 return NULL;
8156 }
8157
8158 return init;
8159}
8160
Pekka Paalanen6ffbba32019-11-06 12:59:32 +02008161/** Add a compositor destroy listener only once
8162 *
8163 * \param compositor The compositor whose destroy to watch for.
8164 * \param listener The listener struct to initialize.
8165 * \param destroy_handler The callback when compositor is destroyed.
8166 * \return True if listener is added, or false if there already is a listener
8167 * with the given \c destroy_handler.
8168 *
8169 * This function does nothing and returns false if the given callback function
8170 * is already present in the weston_compositor destroy callbacks list.
8171 * Otherwise, this function initializes the given listener with the given
8172 * callback pointer and adds it to the compositor's destroy callbacks list.
8173 *
8174 * This can be used to ensure that plugin initialization is done only once
8175 * in case the same plugin is loaded multiple times. If this function returns
8176 * false, the plugin should be already initialized successfully.
8177 *
8178 * All plugins should register a destroy listener for cleaning up. Note, that
8179 * the plugin destruction order is not guaranteed: plugins that depend on other
8180 * plugins must be able to be torn down in arbitrary order.
8181 *
Leandro Ribeiroca640d52020-01-27 19:12:01 -03008182 * \sa weston_compositor_destroy
Pekka Paalanen6ffbba32019-11-06 12:59:32 +02008183 */
8184WL_EXPORT bool
8185weston_compositor_add_destroy_listener_once(struct weston_compositor *compositor,
8186 struct wl_listener *listener,
8187 wl_notify_func_t destroy_handler)
8188{
8189 if (wl_signal_get(&compositor->destroy_signal, destroy_handler))
8190 return false;
8191
8192 listener->notify = destroy_handler;
8193 wl_signal_add(&compositor->destroy_signal, listener);
8194 return true;
8195}
Giulio Camuffo459137b2014-10-11 23:56:24 +03008196
Leandro Ribeiroca640d52020-01-27 19:12:01 -03008197/** Destroys the compositor.
Giulio Camuffo459137b2014-10-11 23:56:24 +03008198 *
Leandro Ribeiroca640d52020-01-27 19:12:01 -03008199 * This function cleans up the compositor state and then destroys it.
Giulio Camuffo459137b2014-10-11 23:56:24 +03008200 *
Leandro Ribeiroca640d52020-01-27 19:12:01 -03008201 * @param compositor The compositor to be destroyed.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008202 *
Marius Vlad284d5342019-06-24 12:00:47 +03008203 * @ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03008204 */
8205WL_EXPORT void
Leandro Ribeiroca640d52020-01-27 19:12:01 -03008206weston_compositor_destroy(struct weston_compositor *compositor)
Giulio Camuffo459137b2014-10-11 23:56:24 +03008207{
8208 /* prevent further rendering while shutting down */
8209 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
8210
8211 wl_signal_emit(&compositor->destroy_signal, compositor);
8212
8213 weston_compositor_xkb_destroy(compositor);
8214
Giulio Camuffo2d24e642015-10-03 16:25:15 +03008215 if (compositor->backend)
8216 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02008217
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03008218 /* The backend is responsible for destroying the heads. */
8219 assert(wl_list_empty(&compositor->head_list));
8220
Pekka Paalanen827b5d22016-06-29 11:54:26 +02008221 weston_plugin_api_destroy_list(compositor);
8222
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03008223 if (compositor->heads_changed_source)
8224 wl_event_source_remove(compositor->heads_changed_source);
8225
Leandro Ribeirof0149642019-12-18 15:52:18 -03008226 weston_log_scope_destroy(compositor->debug_scene);
Daniel Stonece62cb32018-07-20 09:46:24 +01008227 compositor->debug_scene = NULL;
Marius Vladda104eb2019-09-05 14:31:01 +03008228
Leandro Ribeirof0149642019-12-18 15:52:18 -03008229 weston_log_scope_destroy(compositor->timeline);
Marius Vladda104eb2019-09-05 14:31:01 +03008230 compositor->timeline = NULL;
Pekka Paalanena5630ea2017-10-12 13:13:42 +02008231
Leandro Ribeiro8eb84142021-01-18 19:36:48 -03008232 if (compositor->default_dmabuf_feedback) {
8233 weston_dmabuf_feedback_destroy(compositor->default_dmabuf_feedback);
8234 weston_dmabuf_feedback_format_table_destroy(compositor->dmabuf_feedback_format_table);
8235 }
8236
Giulio Camuffo459137b2014-10-11 23:56:24 +03008237 free(compositor);
8238}
8239
8240/** Instruct the compositor to exit.
8241 *
8242 * This functions does not directly destroy the compositor object, it merely
8243 * command it to start the tear down process. It is not guaranteed that the
8244 * tear down will happen immediately.
8245 *
8246 * \param compositor The compositor to tear down.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008247 *
8248 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03008249 */
8250WL_EXPORT void
8251weston_compositor_exit(struct weston_compositor *compositor)
8252{
8253 compositor->exit(compositor);
8254}
8255
8256/** Return the user data stored in the compositor.
8257 *
8258 * This function returns the user data pointer set with user_data parameter
8259 * to the \ref weston_compositor_create function.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008260 *
8261 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03008262 */
8263WL_EXPORT void *
8264weston_compositor_get_user_data(struct weston_compositor *compositor)
8265{
8266 return compositor->user_data;
8267}
Pekka Paalanendd186732016-06-03 14:49:54 +03008268
Pekka Paalanen50dbf382016-06-03 15:23:46 +03008269static const char * const backend_map[] = {
8270 [WESTON_BACKEND_DRM] = "drm-backend.so",
8271 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
8272 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
8273 [WESTON_BACKEND_RDP] = "rdp-backend.so",
8274 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
8275 [WESTON_BACKEND_X11] = "x11-backend.so",
8276};
8277
Pekka Paalanendd186732016-06-03 14:49:54 +03008278/** Load a backend into a weston_compositor
8279 *
8280 * A backend must be loaded to make a weston_compositor work. A backend
8281 * provides input and output capabilities, and determines the renderer to use.
8282 *
8283 * \param compositor A compositor that has not had a backend loaded yet.
8284 * \param backend Name of the backend file.
8285 * \param config_base A pointer to a backend-specific configuration
8286 * structure's 'base' member.
8287 *
8288 * \return 0 on success, or -1 on error.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008289 *
8290 * \ingroup compositor
Pekka Paalanendd186732016-06-03 14:49:54 +03008291 */
8292WL_EXPORT int
8293weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03008294 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03008295 struct weston_backend_config *config_base)
8296{
8297 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03008298 struct weston_backend_config *config_base);
8299
Pekka Paalanend7e35112017-08-29 17:04:12 +03008300 if (compositor->backend) {
8301 weston_log("Error: attempt to load a backend when one is already loaded\n");
8302 return -1;
8303 }
8304
Quentin Glidic887c0182016-07-10 11:00:53 +02008305 if (backend >= ARRAY_LENGTH(backend_map))
Pekka Paalanen50dbf382016-06-03 15:23:46 +03008306 return -1;
8307
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01008308 backend_init = weston_load_module(backend_map[backend], "weston_backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03008309 if (!backend_init)
8310 return -1;
8311
Pekka Paalanend7e35112017-08-29 17:04:12 +03008312 if (backend_init(compositor, config_base) < 0) {
8313 compositor->backend = NULL;
8314 return -1;
8315 }
8316
Pekka Paalanen90a5ffa2021-02-25 12:03:28 +02008317 if (!compositor->color_manager) {
8318 compositor->color_manager =
8319 weston_color_manager_noop_create(compositor);
8320 }
8321
8322 if (!compositor->color_manager)
8323 return -1;
8324
8325 if (!compositor->color_manager->init(compositor->color_manager))
8326 return -1;
8327
8328 weston_log("Color manager: %s\n", compositor->color_manager->name);
8329
Pekka Paalanend7e35112017-08-29 17:04:12 +03008330 return 0;
Pekka Paalanendd186732016-06-03 14:49:54 +03008331}
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008332
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008333/** weston_compositor_load_xwayland
8334 * \ingroup compositor
8335 */
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008336WL_EXPORT int
8337weston_compositor_load_xwayland(struct weston_compositor *compositor)
8338{
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01008339 int (*module_init)(struct weston_compositor *ec);
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008340
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01008341 module_init = weston_load_module("xwayland.so", "weston_module_init");
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008342 if (!module_init)
8343 return -1;
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01008344 if (module_init(compositor) < 0)
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008345 return -1;
8346 return 0;
8347}
Vasilis Liaskovitis486b4632018-10-10 16:14:55 +02008348
Pekka Paalanen5e79dd42021-03-29 14:11:49 +03008349/** Load Little CMS color manager plugin
8350 *
8351 * Calling this function before loading any backend sets Little CMS
8352 * as the active color matching module (CMM) instead of the default no-op
8353 * color manager.
8354 *
8355 * \ingroup compositor
8356 */
8357WL_EXPORT int
8358weston_compositor_load_color_manager(struct weston_compositor *compositor)
8359{
8360 struct weston_color_manager *
8361 (*cm_create)(struct weston_compositor *compositor);
8362
8363 if (compositor->color_manager) {
8364 weston_log("Error: Color manager '%s' is loaded, cannot load another.\n",
8365 compositor->color_manager->name);
8366 return -1;
8367 }
8368
8369 cm_create = weston_load_module("color-lcms.so", "weston_color_manager_create");
8370 if (!cm_create) {
8371 weston_log("Error: Could not load color-lcms.so.\n");
8372 return -1;
8373 }
8374
8375 compositor->color_manager = cm_create(compositor);
8376 if (!compositor->color_manager) {
8377 weston_log("Error: loading color-lcms.so failed.\n");
8378 return -1;
8379 }
8380
8381 return 0;
8382}
8383
Vasilis Liaskovitis486b4632018-10-10 16:14:55 +02008384/** Resolve an internal compositor error by disconnecting the client.
8385 *
8386 * This function is used in cases when the wl_buffer turns out
8387 * unusable and there is no fallback path.
8388 *
8389 * It is possible the fault is caused by a compositor bug, the underlying
8390 * graphics stack bug or normal behaviour, or perhaps a client mistake.
8391 * In any case, the options are to either composite garbage or nothing,
8392 * or disconnect the client. This is a helper function for the latter.
8393 *
8394 * The error is sent as an INVALID_OBJECT error on the client's wl_display.
8395 *
8396 * \param buffer The weston buffer that is unusable.
8397 * \param msg A custom error message attached to the protocol error.
8398 */
8399WL_EXPORT void
8400weston_buffer_send_server_error(struct weston_buffer *buffer,
8401 const char *msg)
8402{
8403 struct wl_client *client;
8404 struct wl_resource *display_resource;
8405 uint32_t id;
8406
8407 assert(buffer->resource);
8408 id = wl_resource_get_id(buffer->resource);
8409 client = wl_resource_get_client(buffer->resource);
8410 display_resource = wl_client_get_object(client, 1);
8411
8412 assert(display_resource);
8413 wl_resource_post_error(display_resource,
8414 WL_DISPLAY_ERROR_INVALID_OBJECT,
8415 "server error with "
8416 "wl_buffer@%u: %s", id, msg);
8417}
Ankit Nautiyal93dde242019-07-08 11:46:42 +05308418
8419WL_EXPORT void
8420weston_output_disable_planes_incr(struct weston_output *output)
8421{
8422 output->disable_planes++;
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05308423 /*
8424 * If disable_planes changes from 0 to non-zero, it means some type of
8425 * recording of content has started, and therefore protection level of
8426 * the protected surfaces must be updated to avoid the recording of
8427 * the protected content.
8428 */
8429 if (output->disable_planes == 1)
8430 weston_schedule_surface_protection_update(output->compositor);
Ankit Nautiyal93dde242019-07-08 11:46:42 +05308431}
8432
8433WL_EXPORT void
8434weston_output_disable_planes_decr(struct weston_output *output)
8435{
8436 output->disable_planes--;
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05308437 /*
8438 * If disable_planes changes from non-zero to 0, it means no content
8439 * recording is going on any more, and the protected and surfaces can be
8440 * shown without any apprehensions about content being recorded.
8441 */
8442 if (output->disable_planes == 0)
8443 weston_schedule_surface_protection_update(output->compositor);
8444
Ankit Nautiyal93dde242019-07-08 11:46:42 +05308445}