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