blob: 90321e8603096efe6ee0a03b023da2597280cdce [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"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050077
Marius Vlad4e036292019-07-09 00:36:20 +030078#include "weston-log-internal.h"
79
Marius Vlad78984ee2019-06-11 00:05:08 +030080/**
81 * \defgroup head Head
82 * \defgroup output Output
83 * \defgroup compositor Compositor
84 */
85
Pekka Paalanen0513a952014-05-21 16:17:27 +030086#define DEFAULT_REPAINT_WINDOW 7 /* milliseconds */
87
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020088static void
Pekka Paalanend72bad22017-03-29 17:01:41 +030089weston_output_update_matrix(struct weston_output *output);
90
91static void
Alexander Larsson0b135062013-05-28 16:23:36 +020092weston_output_transform_scale_init(struct weston_output *output,
93 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020094
Rob Bradford27b17932013-06-26 18:08:46 +010095static void
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +030096weston_compositor_build_view_list(struct weston_compositor *compositor,
97 struct weston_output *output);
Rob Bradford27b17932013-06-26 18:08:46 +010098
Pekka Paalanendcac3512017-12-08 14:13:34 +020099static char *
100weston_output_create_heads_string(struct weston_output *output);
101
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +0300102static struct weston_paint_node *
103weston_paint_node_create(struct weston_surface *surface,
104 struct weston_view *view,
105 struct weston_output *output)
106{
107 struct weston_paint_node *pnode;
108
109 assert(view->surface == surface);
110
111 pnode = zalloc(sizeof *pnode);
112 if (!pnode)
113 return NULL;
114
115 pnode->surface = surface;
116 wl_list_insert(&surface->paint_node_list, &pnode->surface_link);
117
118 pnode->view = view;
119 wl_list_insert(&view->paint_node_list, &pnode->view_link);
120
121 pnode->output = output;
122 wl_list_insert(&output->paint_node_list, &pnode->output_link);
123
Pekka Paalanen2fddc532021-04-30 17:41:29 +0300124 wl_list_init(&pnode->z_order_link);
125
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +0300126 return pnode;
127}
128
129static void
130weston_paint_node_destroy(struct weston_paint_node *pnode)
131{
132 assert(pnode->view->surface == pnode->surface);
133 wl_list_remove(&pnode->surface_link);
134 wl_list_remove(&pnode->view_link);
135 wl_list_remove(&pnode->output_link);
Pekka Paalanen2fddc532021-04-30 17:41:29 +0300136 wl_list_remove(&pnode->z_order_link);
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +0300137 free(pnode);
138}
139
Pekka Paalanen6528c032017-03-28 15:27:10 +0300140/** Send wl_output events for mode and scale changes
141 *
142 * \param head Send on all resources bound to this head.
143 * \param mode_changed If true, send the current mode.
144 * \param scale_changed If true, send the current scale.
145 */
146static void
147weston_mode_switch_send_events(struct weston_head *head,
148 bool mode_changed, bool scale_changed)
149{
150 struct weston_output *output = head->output;
151 struct wl_resource *resource;
152 int version;
153
154 wl_resource_for_each(resource, &head->resource_list) {
155 if (mode_changed) {
156 wl_output_send_mode(resource,
157 output->current_mode->flags,
158 output->current_mode->width,
159 output->current_mode->height,
160 output->current_mode->refresh);
161 }
162
163 version = wl_resource_get_version(resource);
164 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION && scale_changed)
165 wl_output_send_scale(resource, output->current_scale);
166
167 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
168 wl_output_send_done(resource);
169 }
Roman Gilge97391c2019-03-29 13:01:06 +0100170 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
171 zxdg_output_v1_send_logical_position(resource,
172 output->x,
173 output->y);
174 zxdg_output_v1_send_logical_size(resource,
175 output->width,
176 output->height);
177 zxdg_output_v1_send_done(resource);
178 }
Pekka Paalanen6528c032017-03-28 15:27:10 +0300179}
180
181static void
182weston_mode_switch_finish(struct weston_output *output,
183 int mode_changed, int scale_changed)
Alex Wu2dda6042012-04-17 17:20:47 +0800184{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200185 struct weston_seat *seat;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300186 struct weston_head *head;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200187 pixman_region32_t old_output_region;
Alexander Larsson355748e2013-05-28 16:23:38 +0200188
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200189 pixman_region32_init(&old_output_region);
190 pixman_region32_copy(&old_output_region, &output->region);
191
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200192 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +0200193 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200194
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200195 pixman_region32_init_rect(&output->region, output->x, output->y,
196 output->width, output->height);
197
198 weston_output_update_matrix(output);
199
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200200 /* If a pointer falls outside the outputs new geometry, move it to its
201 * lower-right corner */
202 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500203 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200204 int32_t x, y;
205
206 if (!pointer)
207 continue;
208
209 x = wl_fixed_to_int(pointer->x);
210 y = wl_fixed_to_int(pointer->y);
211
212 if (!pixman_region32_contains_point(&old_output_region,
213 x, y, NULL) ||
214 pixman_region32_contains_point(&output->region,
215 x, y, NULL))
216 continue;
217
218 if (x >= output->x + output->width)
219 x = output->x + output->width - 1;
220 if (y >= output->y + output->height)
221 y = output->y + output->height - 1;
222
223 pointer->x = wl_fixed_from_int(x);
224 pointer->y = wl_fixed_from_int(y);
225 }
226
227 pixman_region32_fini(&old_output_region);
228
Derek Foremandd4cd332014-11-10 10:29:59 -0600229 if (!mode_changed && !scale_changed)
230 return;
231
Hardening57388e42013-09-18 23:56:36 +0200232 /* notify clients of the changes */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300233 wl_list_for_each(head, &output->head_list, output_link)
234 weston_mode_switch_send_events(head,
235 mode_changed, scale_changed);
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600236}
237
David Fort0de859e2016-05-27 23:22:57 +0200238static void
239weston_compositor_reflow_outputs(struct weston_compositor *compositor,
240 struct weston_output *resized_output, int delta_width);
241
Marius Vlad55d87362019-06-11 01:15:35 +0300242/**
243 * \ingroup output
244 */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600245WL_EXPORT int
246weston_output_mode_set_native(struct weston_output *output,
247 struct weston_mode *mode,
248 int32_t scale)
249{
250 int ret;
251 int mode_changed = 0, scale_changed = 0;
David Fort0de859e2016-05-27 23:22:57 +0200252 int32_t old_width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600253
254 if (!output->switch_mode)
255 return -1;
256
257 if (!output->original_mode) {
258 mode_changed = 1;
259 ret = output->switch_mode(output, mode);
260 if (ret < 0)
261 return ret;
262 if (output->current_scale != scale) {
263 scale_changed = 1;
264 output->current_scale = scale;
Hardening57388e42013-09-18 23:56:36 +0200265 }
266 }
267
David Fort0de859e2016-05-27 23:22:57 +0200268 old_width = output->width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600269 output->native_mode = mode;
270 output->native_scale = scale;
271
272 weston_mode_switch_finish(output, mode_changed, scale_changed);
273
David Fort0de859e2016-05-27 23:22:57 +0200274 if (mode_changed || scale_changed) {
275 weston_compositor_reflow_outputs(output->compositor, output, output->width - old_width);
276
277 wl_signal_emit(&output->compositor->output_resized_signal, output);
278 }
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600279 return 0;
280}
281
Marius Vlad55d87362019-06-11 01:15:35 +0300282/**
283 * \ingroup output
284 */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600285WL_EXPORT int
286weston_output_mode_switch_to_native(struct weston_output *output)
287{
288 int ret;
289 int mode_changed = 0, scale_changed = 0;
290
291 if (!output->switch_mode)
292 return -1;
293
294 if (!output->original_mode) {
295 weston_log("already in the native mode\n");
296 return -1;
297 }
298 /* the non fullscreen clients haven't seen a mode set since we
299 * switched into a temporary, so we need to notify them if the
300 * mode at that time is different from the native mode now.
301 */
302 mode_changed = (output->original_mode != output->native_mode);
303 scale_changed = (output->original_scale != output->native_scale);
304
305 ret = output->switch_mode(output, output->native_mode);
306 if (ret < 0)
307 return ret;
308
309 output->current_scale = output->native_scale;
310
311 output->original_mode = NULL;
312 output->original_scale = 0;
313
314 weston_mode_switch_finish(output, mode_changed, scale_changed);
315
316 return 0;
317}
318
Marius Vlad55d87362019-06-11 01:15:35 +0300319/**
320 * \ingroup output
321 */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600322WL_EXPORT int
323weston_output_mode_switch_to_temporary(struct weston_output *output,
324 struct weston_mode *mode,
325 int32_t scale)
326{
327 int ret;
328
329 if (!output->switch_mode)
330 return -1;
331
332 /* original_mode is the last mode non full screen clients have seen,
333 * so we shouldn't change it if we already have one set.
334 */
335 if (!output->original_mode) {
336 output->original_mode = output->native_mode;
337 output->original_scale = output->native_scale;
338 }
339 ret = output->switch_mode(output, mode);
340 if (ret < 0)
341 return ret;
342
343 output->current_scale = scale;
344
345 weston_mode_switch_finish(output, 0, 0);
346
347 return 0;
Alex Wu2dda6042012-04-17 17:20:47 +0800348}
349
Benjamin Franzke06286262011-05-06 19:12:33 +0200350static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300351region_init_infinite(pixman_region32_t *region)
352{
353 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
354 UINT32_MAX, UINT32_MAX);
355}
356
Pekka Paalanene67858b2013-04-25 13:57:42 +0300357static struct weston_subsurface *
358weston_surface_to_subsurface(struct weston_surface *surface);
359
Jason Ekstranda7af7042013-10-12 22:38:11 -0500360WL_EXPORT struct weston_view *
361weston_view_create(struct weston_surface *surface)
362{
363 struct weston_view *view;
364
Bryce Harringtonde16d892014-11-20 22:21:57 -0800365 view = zalloc(sizeof *view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500366 if (view == NULL)
367 return NULL;
368
369 view->surface = surface;
Daniel Stonefb4869d2016-12-09 16:27:54 +0000370 view->plane = &surface->compositor->primary_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500371
Jason Ekstranda7af7042013-10-12 22:38:11 -0500372 /* Assign to surface */
373 wl_list_insert(&surface->views, &view->surface_link);
374
375 wl_signal_init(&view->destroy_signal);
376 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300377 wl_list_init(&view->layer_link.link);
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +0300378 wl_list_init(&view->paint_node_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500379
Jason Ekstranda7af7042013-10-12 22:38:11 -0500380 pixman_region32_init(&view->clip);
381
382 view->alpha = 1.0;
383 pixman_region32_init(&view->transform.opaque);
384
385 wl_list_init(&view->geometry.transformation_list);
386 wl_list_insert(&view->geometry.transformation_list,
387 &view->transform.position.link);
388 weston_matrix_init(&view->transform.position.matrix);
389 wl_list_init(&view->geometry.child_list);
Pekka Paalanen380adf52015-02-16 14:39:11 +0200390 pixman_region32_init(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500391 pixman_region32_init(&view->transform.boundingbox);
392 view->transform.dirty = 1;
393
Jason Ekstranda7af7042013-10-12 22:38:11 -0500394 return view;
395}
396
Jason Ekstrand108865d2014-06-26 10:04:49 -0700397struct weston_frame_callback {
398 struct wl_resource *resource;
399 struct wl_list link;
400};
401
Pekka Paalanen133e4392014-09-23 22:08:46 -0400402struct weston_presentation_feedback {
403 struct wl_resource *resource;
404
405 /* XXX: could use just wl_resource_get_link() instead */
406 struct wl_list link;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200407
408 /* The per-surface feedback flags */
409 uint32_t psf_flags;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400410};
411
412static void
413weston_presentation_feedback_discard(
414 struct weston_presentation_feedback *feedback)
415{
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200416 wp_presentation_feedback_send_discarded(feedback->resource);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400417 wl_resource_destroy(feedback->resource);
418}
419
420static void
421weston_presentation_feedback_discard_list(struct wl_list *list)
422{
423 struct weston_presentation_feedback *feedback, *tmp;
424
425 wl_list_for_each_safe(feedback, tmp, list, link)
426 weston_presentation_feedback_discard(feedback);
427}
428
429static void
430weston_presentation_feedback_present(
431 struct weston_presentation_feedback *feedback,
432 struct weston_output *output,
433 uint32_t refresh_nsec,
434 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200435 uint64_t seq,
436 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400437{
438 struct wl_client *client = wl_resource_get_client(feedback->resource);
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300439 struct weston_head *head;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400440 struct wl_resource *o;
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200441 uint32_t tv_sec_hi;
442 uint32_t tv_sec_lo;
443 uint32_t tv_nsec;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300444 bool done = false;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400445
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300446 wl_list_for_each(head, &output->head_list, output_link) {
447 wl_resource_for_each(o, &head->resource_list) {
448 if (wl_resource_get_client(o) != client)
449 continue;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400450
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300451 wp_presentation_feedback_send_sync_output(feedback->resource, o);
452 done = true;
453 }
454
455 /* For clone mode, send it for just one wl_output global,
456 * they are all equivalent anyway.
457 */
458 if (done)
459 break;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400460 }
461
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200462 timespec_to_proto(ts, &tv_sec_hi, &tv_sec_lo, &tv_nsec);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200463 wp_presentation_feedback_send_presented(feedback->resource,
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200464 tv_sec_hi, tv_sec_lo, tv_nsec,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200465 refresh_nsec,
466 seq >> 32, seq & 0xffffffff,
467 flags | feedback->psf_flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400468 wl_resource_destroy(feedback->resource);
469}
470
471static void
472weston_presentation_feedback_present_list(struct wl_list *list,
473 struct weston_output *output,
474 uint32_t refresh_nsec,
475 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200476 uint64_t seq,
477 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400478{
479 struct weston_presentation_feedback *feedback, *tmp;
480
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200481 assert(!(flags & WP_PRESENTATION_FEEDBACK_INVALID) ||
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200482 wl_list_empty(list));
483
Pekka Paalanen133e4392014-09-23 22:08:46 -0400484 wl_list_for_each_safe(feedback, tmp, list, link)
485 weston_presentation_feedback_present(feedback, output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200486 refresh_nsec, ts, seq,
487 flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400488}
489
Jason Ekstrand7b982072014-05-20 14:33:03 -0500490static void
491surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
492{
493 struct weston_surface_state *state =
494 container_of(listener, struct weston_surface_state,
495 buffer_destroy_listener);
496
497 state->buffer = NULL;
498}
499
500static void
501weston_surface_state_init(struct weston_surface_state *state)
502{
503 state->newly_attached = 0;
504 state->buffer = NULL;
505 state->buffer_destroy_listener.notify =
506 surface_state_handle_buffer_destroy;
507 state->sx = 0;
508 state->sy = 0;
509
Derek Foreman152254b2015-11-26 14:17:48 -0600510 pixman_region32_init(&state->damage_surface);
511 pixman_region32_init(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500512 pixman_region32_init(&state->opaque);
513 region_init_infinite(&state->input);
514
515 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400516 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500517
518 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
519 state->buffer_viewport.buffer.scale = 1;
520 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
521 state->buffer_viewport.surface.width = -1;
522 state->buffer_viewport.changed = 0;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300523
524 state->acquire_fence_fd = -1;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +0530525
526 state->desired_protection = WESTON_HDCP_DISABLE;
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +0530527 state->protection_mode = WESTON_SURFACE_PROTECTION_MODE_RELAXED;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500528}
529
530static void
531weston_surface_state_fini(struct weston_surface_state *state)
532{
533 struct weston_frame_callback *cb, *next;
534
535 wl_list_for_each_safe(cb, next,
536 &state->frame_callback_list, link)
537 wl_resource_destroy(cb->resource);
538
Pekka Paalanen133e4392014-09-23 22:08:46 -0400539 weston_presentation_feedback_discard_list(&state->feedback_list);
540
Jason Ekstrand7b982072014-05-20 14:33:03 -0500541 pixman_region32_fini(&state->input);
542 pixman_region32_fini(&state->opaque);
Derek Foreman152254b2015-11-26 14:17:48 -0600543 pixman_region32_fini(&state->damage_surface);
544 pixman_region32_fini(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500545
546 if (state->buffer)
547 wl_list_remove(&state->buffer_destroy_listener.link);
548 state->buffer = NULL;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300549
550 fd_clear(&state->acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +0300551 weston_buffer_release_reference(&state->buffer_release_ref, NULL);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500552}
553
554static void
555weston_surface_state_set_buffer(struct weston_surface_state *state,
556 struct weston_buffer *buffer)
557{
558 if (state->buffer == buffer)
559 return;
560
561 if (state->buffer)
562 wl_list_remove(&state->buffer_destroy_listener.link);
563 state->buffer = buffer;
564 if (state->buffer)
565 wl_signal_add(&state->buffer->destroy_signal,
566 &state->buffer_destroy_listener);
567}
568
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500569WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500570weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500571{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500572 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400573
Bryce Harringtonde16d892014-11-20 22:21:57 -0800574 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400575 if (surface == NULL)
576 return NULL;
577
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500578 wl_signal_init(&surface->destroy_signal);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +0800579 wl_signal_init(&surface->commit_signal);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500580
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500581 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200582 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400583
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200584 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
585 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200586 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
587 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500588
589 weston_surface_state_init(&surface->pending);
590
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400591 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500592 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300593 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400594
Jason Ekstranda7af7042013-10-12 22:38:11 -0500595 wl_list_init(&surface->views);
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +0300596 wl_list_init(&surface->paint_node_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500597
598 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400599 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500600
Pekka Paalanene67858b2013-04-25 13:57:42 +0300601 wl_list_init(&surface->subsurface_list);
602 wl_list_init(&surface->subsurface_list_pending);
603
Jason Ekstrand1e059042014-10-16 10:55:19 -0500604 weston_matrix_init(&surface->buffer_to_surface_matrix);
605 weston_matrix_init(&surface->surface_to_buffer_matrix);
606
Jonas Ådahld3414f22016-07-22 17:56:31 +0800607 wl_list_init(&surface->pointer_constraints);
608
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300609 surface->acquire_fence_fd = -1;
610
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +0530611 surface->desired_protection = WESTON_HDCP_DISABLE;
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +0530612 surface->current_protection = WESTON_HDCP_DISABLE;
613 surface->protection_mode = WESTON_SURFACE_PROTECTION_MODE_RELAXED;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +0530614
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400615 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500616}
617
Alex Wu8811bf92012-02-28 18:07:54 +0800618WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500619weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200620 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500621{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100622 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Philipp Zabel195dade2018-09-03 19:44:59 +0200623 surface->is_opaque = !(alpha < 1.0);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500624}
625
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400626WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500627weston_view_to_global_float(struct weston_view *view,
628 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200629{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500630 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200631 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
632
Jason Ekstranda7af7042013-10-12 22:38:11 -0500633 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200634
635 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200636 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700637 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200638 v.f[3]);
639 *x = 0;
640 *y = 0;
641 return;
642 }
643
644 *x = v.f[0] / v.f[3];
645 *y = v.f[1] / v.f[3];
646 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500647 *x = sx + view->geometry.x;
648 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200649 }
650}
651
Pekka Paalanen0df44772020-02-14 14:50:03 +0200652/** Transform a point to buffer coordinates
653 *
654 * \param width Surface width.
655 * \param height Surface height.
656 * \param transform Buffer transform.
657 * \param scale Buffer scale.
658 * \param sx Surface x coordinate of a point.
659 * \param sy Surface y coordinate of a point.
660 * \param[out] bx Buffer x coordinate of the point.
661 * \param[out] by Buffer Y coordinate of the point.
662 *
663 * Converts the given surface-local coordinates to buffer coordinates
664 * according to the given buffer transform and scale.
665 * This ignores wp_viewport.
666 *
667 * The given width and height must be the result of inverse scaled and
668 * inverse transformed buffer size.
669 */
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500670WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200671weston_transformed_coord(int width, int height,
672 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200673 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200674 float sx, float sy, float *bx, float *by)
675{
676 switch (transform) {
677 case WL_OUTPUT_TRANSFORM_NORMAL:
678 default:
679 *bx = sx;
680 *by = sy;
681 break;
682 case WL_OUTPUT_TRANSFORM_FLIPPED:
683 *bx = width - sx;
684 *by = sy;
685 break;
686 case WL_OUTPUT_TRANSFORM_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200687 *bx = sy;
688 *by = width - sx;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200689 break;
690 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200691 *bx = sy;
692 *by = sx;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200693 break;
694 case WL_OUTPUT_TRANSFORM_180:
695 *bx = width - sx;
696 *by = height - sy;
697 break;
698 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
699 *bx = sx;
700 *by = height - sy;
701 break;
702 case WL_OUTPUT_TRANSFORM_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200703 *bx = height - sy;
704 *by = sx;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200705 break;
706 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200707 *bx = height - sy;
708 *by = width - sx;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200709 break;
710 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200711
712 *bx *= scale;
713 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200714}
715
Pekka Paalanen0df44772020-02-14 14:50:03 +0200716/** Transform a rectangle to buffer coordinates
717 *
718 * \param width Surface width.
719 * \param height Surface height.
720 * \param transform Buffer transform.
721 * \param scale Buffer scale.
722 * \param rect Rectangle in surface coordinates.
723 * \return Rectangle in buffer coordinates.
724 *
725 * Converts the given surface-local rectangle to buffer coordinates
726 * according to the given buffer transform and scale. The resulting
727 * rectangle is guaranteed to be well-formed.
728 * This ignores wp_viewport.
729 *
730 * The given width and height must be the result of inverse scaled and
731 * inverse transformed buffer size.
732 */
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200733WL_EXPORT pixman_box32_t
734weston_transformed_rect(int width, int height,
735 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200736 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200737 pixman_box32_t rect)
738{
739 float x1, x2, y1, y2;
740
741 pixman_box32_t ret;
742
Alexander Larsson4ea95522013-05-22 14:41:37 +0200743 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200744 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200745 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200746 rect.x2, rect.y2, &x2, &y2);
747
748 if (x1 <= x2) {
749 ret.x1 = x1;
750 ret.x2 = x2;
751 } else {
752 ret.x1 = x2;
753 ret.x2 = x1;
754 }
755
756 if (y1 <= y2) {
757 ret.y1 = y1;
758 ret.y2 = y2;
759 } else {
760 ret.y1 = y2;
761 ret.y2 = y1;
762 }
763
764 return ret;
765}
766
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600767/** Transform a region by a matrix, restricted to axis-aligned transformations
768 *
769 * Warning: This function does not work for projective, affine, or matrices
770 * that encode arbitrary rotations. Only 90-degree step rotations are
771 * supported.
772 */
773WL_EXPORT void
774weston_matrix_transform_region(pixman_region32_t *dest,
775 struct weston_matrix *matrix,
776 pixman_region32_t *src)
777{
778 pixman_box32_t *src_rects, *dest_rects;
779 int nrects, i;
780
781 src_rects = pixman_region32_rectangles(src, &nrects);
782 dest_rects = malloc(nrects * sizeof(*dest_rects));
783 if (!dest_rects)
784 return;
785
786 for (i = 0; i < nrects; i++) {
787 struct weston_vector vec1 = {{
788 src_rects[i].x1, src_rects[i].y1, 0, 1
789 }};
790 weston_matrix_transform(matrix, &vec1);
791 vec1.f[0] /= vec1.f[3];
792 vec1.f[1] /= vec1.f[3];
793
794 struct weston_vector vec2 = {{
795 src_rects[i].x2, src_rects[i].y2, 0, 1
796 }};
797 weston_matrix_transform(matrix, &vec2);
798 vec2.f[0] /= vec2.f[3];
799 vec2.f[1] /= vec2.f[3];
800
801 if (vec1.f[0] < vec2.f[0]) {
802 dest_rects[i].x1 = floor(vec1.f[0]);
803 dest_rects[i].x2 = ceil(vec2.f[0]);
804 } else {
805 dest_rects[i].x1 = floor(vec2.f[0]);
806 dest_rects[i].x2 = ceil(vec1.f[0]);
807 }
808
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600809 if (vec1.f[1] < vec2.f[1]) {
810 dest_rects[i].y1 = floor(vec1.f[1]);
811 dest_rects[i].y2 = ceil(vec2.f[1]);
812 } else {
813 dest_rects[i].y1 = floor(vec2.f[1]);
814 dest_rects[i].y2 = ceil(vec1.f[1]);
815 }
816 }
817
818 pixman_region32_clear(dest);
819 pixman_region32_init_rects(dest, dest_rects, nrects);
820 free(dest_rects);
821}
822
Pekka Paalanen0df44772020-02-14 14:50:03 +0200823/** Transform a region to buffer coordinates
824 *
825 * \param width Surface width.
826 * \param height Surface height.
827 * \param transform Buffer transform.
828 * \param scale Buffer scale.
829 * \param[in] src Region in surface coordinates.
830 * \param[out] dest Resulting region in buffer coordinates.
831 *
832 * Converts the given surface-local region to buffer coordinates
833 * according to the given buffer transform and scale.
834 * This ignores wp_viewport.
835 *
836 * The given width and height must be the result of inverse scaled and
837 * inverse transformed buffer size.
838 *
839 * src and dest are allowed to point to the same memory for in-place conversion.
840 */
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200841WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500842weston_transformed_region(int width, int height,
843 enum wl_output_transform transform,
844 int32_t scale,
845 pixman_region32_t *src, pixman_region32_t *dest)
846{
847 pixman_box32_t *src_rects, *dest_rects;
848 int nrects, i;
849
850 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
851 if (src != dest)
852 pixman_region32_copy(dest, src);
853 return;
854 }
855
856 src_rects = pixman_region32_rectangles(src, &nrects);
857 dest_rects = malloc(nrects * sizeof(*dest_rects));
858 if (!dest_rects)
859 return;
860
861 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
862 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
863 } else {
864 for (i = 0; i < nrects; i++) {
865 switch (transform) {
866 default:
867 case WL_OUTPUT_TRANSFORM_NORMAL:
868 dest_rects[i].x1 = src_rects[i].x1;
869 dest_rects[i].y1 = src_rects[i].y1;
870 dest_rects[i].x2 = src_rects[i].x2;
871 dest_rects[i].y2 = src_rects[i].y2;
872 break;
873 case WL_OUTPUT_TRANSFORM_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200874 dest_rects[i].x1 = src_rects[i].y1;
875 dest_rects[i].y1 = width - src_rects[i].x2;
876 dest_rects[i].x2 = src_rects[i].y2;
877 dest_rects[i].y2 = width - src_rects[i].x1;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500878 break;
879 case WL_OUTPUT_TRANSFORM_180:
880 dest_rects[i].x1 = width - src_rects[i].x2;
881 dest_rects[i].y1 = height - src_rects[i].y2;
882 dest_rects[i].x2 = width - src_rects[i].x1;
883 dest_rects[i].y2 = height - src_rects[i].y1;
884 break;
885 case WL_OUTPUT_TRANSFORM_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200886 dest_rects[i].x1 = height - src_rects[i].y2;
887 dest_rects[i].y1 = src_rects[i].x1;
888 dest_rects[i].x2 = height - src_rects[i].y1;
889 dest_rects[i].y2 = src_rects[i].x2;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500890 break;
891 case WL_OUTPUT_TRANSFORM_FLIPPED:
892 dest_rects[i].x1 = width - src_rects[i].x2;
893 dest_rects[i].y1 = src_rects[i].y1;
894 dest_rects[i].x2 = width - src_rects[i].x1;
895 dest_rects[i].y2 = src_rects[i].y2;
896 break;
897 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200898 dest_rects[i].x1 = src_rects[i].y1;
899 dest_rects[i].y1 = src_rects[i].x1;
900 dest_rects[i].x2 = src_rects[i].y2;
901 dest_rects[i].y2 = src_rects[i].x2;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500902 break;
903 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
904 dest_rects[i].x1 = src_rects[i].x1;
905 dest_rects[i].y1 = height - src_rects[i].y2;
906 dest_rects[i].x2 = src_rects[i].x2;
907 dest_rects[i].y2 = height - src_rects[i].y1;
908 break;
909 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200910 dest_rects[i].x1 = height - src_rects[i].y2;
911 dest_rects[i].y1 = width - src_rects[i].x2;
912 dest_rects[i].x2 = height - src_rects[i].y1;
913 dest_rects[i].y2 = width - src_rects[i].x1;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500914 break;
915 }
916 }
917 }
918
919 if (scale != 1) {
920 for (i = 0; i < nrects; i++) {
921 dest_rects[i].x1 *= scale;
922 dest_rects[i].x2 *= scale;
923 dest_rects[i].y1 *= scale;
924 dest_rects[i].y2 *= scale;
925 }
926 }
927
928 pixman_region32_clear(dest);
929 pixman_region32_init_rects(dest, dest_rects, nrects);
930 free(dest_rects);
931}
932
Jonny Lamb74130762013-11-26 18:19:46 +0100933static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300934viewport_surface_to_buffer(struct weston_surface *surface,
935 float sx, float sy, float *bx, float *by)
Jonny Lamb74130762013-11-26 18:19:46 +0100936{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200937 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200938 double src_width, src_height;
939 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200940
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200941 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
942 if (vp->surface.width == -1) {
943 *bx = sx;
944 *by = sy;
945 return;
946 }
Jonny Lamb74130762013-11-26 18:19:46 +0100947
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200948 src_x = 0.0;
949 src_y = 0.0;
950 src_width = surface->width_from_buffer;
951 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100952 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200953 src_x = wl_fixed_to_double(vp->buffer.src_x);
954 src_y = wl_fixed_to_double(vp->buffer.src_y);
955 src_width = wl_fixed_to_double(vp->buffer.src_width);
956 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100957 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200958
959 *bx = sx * src_width / surface->width + src_x;
960 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100961}
962
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500963WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200964weston_surface_to_buffer_float(struct weston_surface *surface,
965 float sx, float sy, float *bx, float *by)
966{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200967 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
968
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300969 /* first transform coordinates if the viewport is set */
970 viewport_surface_to_buffer(surface, sx, sy, bx, by);
Jonny Lamb74130762013-11-26 18:19:46 +0100971
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500972 weston_transformed_coord(surface->width_from_buffer,
973 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200974 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100975 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200976}
977
Derek Foreman99739672015-12-03 16:38:11 -0600978/** Transform a rectangle from surface coordinates to buffer coordinates
979 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300980 * \param surface The surface to fetch wp_viewport and buffer transformation
Derek Foreman99739672015-12-03 16:38:11 -0600981 * from.
982 * \param rect The rectangle to transform.
983 * \return The transformed rectangle.
984 *
985 * Viewport and buffer transformations can only do translation, scaling,
986 * and rotations in 90-degree steps. Therefore the only loss in the
987 * conversion is coordinate rounding.
988 *
989 * However, some coordinate rounding takes place as an intermediate
990 * step before the buffer scale factor is applied, so the rectangle
991 * boundary may not be exactly as expected.
992 *
993 * This is OK for damage tracking since a little extra coverage is
994 * not a problem.
995 */
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200996WL_EXPORT pixman_box32_t
997weston_surface_to_buffer_rect(struct weston_surface *surface,
998 pixman_box32_t rect)
999{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001000 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +01001001 float xf, yf;
1002
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03001003 /* first transform box coordinates if the viewport is set */
1004 viewport_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
Jonny Lamb74130762013-11-26 18:19:46 +01001005 rect.x1 = floorf(xf);
1006 rect.y1 = floorf(yf);
1007
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03001008 viewport_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
Derek Foremane2e15ac2015-12-01 13:00:43 -06001009 rect.x2 = ceilf(xf);
1010 rect.y2 = ceilf(yf);
Jonny Lamb74130762013-11-26 18:19:46 +01001011
Jason Ekstrandd0cebc32014-04-21 20:56:46 -05001012 return weston_transformed_rect(surface->width_from_buffer,
1013 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001014 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +02001015 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +02001016}
1017
Pekka Paalanene54e31c2015-03-04 14:23:28 +02001018/** Transform a region from surface coordinates to buffer coordinates
1019 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03001020 * \param surface The surface to fetch wp_viewport and buffer transformation
Pekka Paalanene54e31c2015-03-04 14:23:28 +02001021 * from.
Marius Vlada2dace22019-06-12 16:05:44 +03001022 * \param[in] surface_region The region in surface coordinates.
1023 * \param[out] buffer_region The region converted to buffer coordinates.
Pekka Paalanene54e31c2015-03-04 14:23:28 +02001024 *
1025 * Buffer_region must be init'd, but will be completely overwritten.
1026 *
1027 * Viewport and buffer transformations can only do translation, scaling,
1028 * and rotations in 90-degree steps. Therefore the only loss in the
Derek Foreman99739672015-12-03 16:38:11 -06001029 * conversion is from the coordinate rounding that takes place in
1030 * \ref weston_surface_to_buffer_rect.
Marius Vlada2dace22019-06-12 16:05:44 +03001031 *
Pekka Paalanene54e31c2015-03-04 14:23:28 +02001032 */
1033WL_EXPORT void
1034weston_surface_to_buffer_region(struct weston_surface *surface,
1035 pixman_region32_t *surface_region,
1036 pixman_region32_t *buffer_region)
1037{
1038 pixman_box32_t *src_rects, *dest_rects;
1039 int nrects, i;
1040
1041 src_rects = pixman_region32_rectangles(surface_region, &nrects);
1042 dest_rects = malloc(nrects * sizeof(*dest_rects));
1043 if (!dest_rects)
1044 return;
1045
1046 for (i = 0; i < nrects; i++) {
1047 dest_rects[i] = weston_surface_to_buffer_rect(surface,
1048 src_rects[i]);
1049 }
1050
1051 pixman_region32_fini(buffer_region);
1052 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
1053 free(dest_rects);
1054}
1055
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +02001056WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001057weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001058 struct weston_plane *plane)
1059{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001060 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001061 return;
1062
Jason Ekstranda7af7042013-10-12 22:38:11 -05001063 weston_view_damage_below(view);
1064 view->plane = plane;
1065 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001066}
1067
Pekka Paalanen51723d52015-02-17 13:10:01 +02001068/** Inflict damage on the plane where the view is visible.
1069 *
1070 * \param view The view that causes the damage.
1071 *
1072 * If the view is currently on a plane (including the primary plane),
1073 * take the view's boundingbox, subtract all the opaque views that cover it,
1074 * and add the remaining region as damage to the plane. This corresponds
1075 * to the damage inflicted to the plane if this view disappeared.
1076 *
1077 * A repaint is scheduled for this view.
1078 *
1079 * The region of all opaque views covering this view is stored in
1080 * weston_view::clip and updated by view_accumulate_damage() during
1081 * weston_output_repaint(). Specifically, that region matches the
1082 * scenegraph as it was last painted.
1083 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001084WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001085weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001086{
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001087 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001088
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001089 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001090 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001091 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +08001092 if (view->plane)
1093 pixman_region32_union(&view->plane->damage,
1094 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001095 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -08001096 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001097}
1098
Pekka Paalanen01e00682017-03-24 16:21:06 +02001099/** Send wl_surface.enter/leave events
1100 *
1101 * \param surface The surface.
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03001102 * \param head A head of the entered/left output.
Pekka Paalanen01e00682017-03-24 16:21:06 +02001103 * \param enter True if entered.
Marius Vlada2dace22019-06-12 16:05:44 +03001104 * \param leave True if left.
Pekka Paalanen01e00682017-03-24 16:21:06 +02001105 *
1106 * Send the enter/leave events for all protocol objects bound to the given
1107 * output by the client owning the surface.
1108 */
1109static void
1110weston_surface_send_enter_leave(struct weston_surface *surface,
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03001111 struct weston_head *head,
Pekka Paalanen01e00682017-03-24 16:21:06 +02001112 bool enter,
1113 bool leave)
1114{
1115 struct wl_resource *wloutput;
1116 struct wl_client *client;
1117
1118 assert(enter != leave);
1119
1120 client = wl_resource_get_client(surface->resource);
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03001121 wl_resource_for_each(wloutput, &head->resource_list) {
Pekka Paalanen01e00682017-03-24 16:21:06 +02001122 if (wl_resource_get_client(wloutput) != client)
1123 continue;
1124
1125 if (enter)
1126 wl_surface_send_enter(surface->resource, wloutput);
1127 if (leave)
1128 wl_surface_send_leave(surface->resource, wloutput);
1129 }
1130}
1131
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301132static void
1133weston_surface_compute_protection(struct protected_surface *psurface)
1134{
1135 enum weston_hdcp_protection min_protection;
1136 bool min_protection_valid = false;
1137 struct weston_surface *surface = psurface->surface;
1138 struct weston_output *output;
1139
1140 wl_list_for_each(output, &surface->compositor->output_list, link)
1141 if (surface->output_mask & (1u << output->id)) {
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05301142 /*
1143 * If the content-protection is enabled with protection
1144 * mode as RELAXED for a surface, and if
1145 * content-recording features like: screen-shooter,
1146 * recorder, screen-sharing, etc are on, then notify the
1147 * client, that the protection is disabled.
1148 *
1149 * Note: If the protection mode is ENFORCED then there
1150 * is no need to bother the client as the renderer takes
1151 * care of censoring the visibility of the protected
1152 * content.
1153 */
1154
1155 if (output->disable_planes > 0 &&
1156 surface->protection_mode == WESTON_SURFACE_PROTECTION_MODE_RELAXED) {
1157 min_protection = WESTON_HDCP_DISABLE;
1158 min_protection_valid = true;
1159 break;
1160 }
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301161 if (!min_protection_valid) {
1162 min_protection = output->current_protection;
1163 min_protection_valid = true;
1164 }
1165 if (output->current_protection < min_protection)
1166 min_protection = output->current_protection;
1167 }
1168 if (!min_protection_valid)
1169 min_protection = WESTON_HDCP_DISABLE;
1170
1171 surface->current_protection = min_protection;
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05301172
1173 weston_protected_surface_send_event(psurface, surface->current_protection);
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301174}
1175
1176static void
1177notify_surface_protection_change(void *data)
1178{
1179 struct weston_compositor *compositor = data;
1180 struct content_protection *cp;
1181 struct protected_surface *psurface;
1182
1183 cp = compositor->content_protection;
1184 cp->surface_protection_update = NULL;
1185
1186 /* Notify the clients, whose surfaces are changed */
1187 wl_list_for_each(psurface, &cp->protected_list, link)
1188 if (psurface && psurface->surface)
1189 weston_surface_compute_protection(psurface);
1190}
1191
Bryce Harrington3f650b82015-12-23 11:01:58 -08001192/**
Ankit Nautiyalfaa5ab42019-07-08 13:23:24 +05301193 * \param compositor weston_compositor
1194 *
1195 * Schedule an idle task to notify surface about the update in protection,
1196 * if not already scheduled.
1197 */
1198static void
1199weston_schedule_surface_protection_update(struct weston_compositor *compositor)
1200{
1201 struct content_protection *cp = compositor->content_protection;
1202 struct wl_event_loop *loop;
1203
1204 if (!cp || cp->surface_protection_update)
1205 return;
1206 loop = wl_display_get_event_loop(compositor->wl_display);
1207 cp->surface_protection_update = wl_event_loop_add_idle(loop,
1208 notify_surface_protection_change,
1209 compositor);
1210}
1211
1212/**
Bryce Harrington3f650b82015-12-23 11:01:58 -08001213 * \param es The surface
1214 * \param mask The new set of outputs for the surface
1215 *
1216 * Sets the surface's set of outputs to the ones specified by
1217 * the new output mask provided. Identifies the outputs that
1218 * have changed, the posts enter and leave events for these
1219 * outputs as appropriate.
1220 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001221static void
1222weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
1223{
1224 uint32_t different = es->output_mask ^ mask;
1225 uint32_t entered = mask & different;
1226 uint32_t left = es->output_mask & different;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001227 uint32_t output_bit;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001228 struct weston_output *output;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001229 struct weston_head *head;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001230
1231 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001232 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001233 return;
1234 if (different == 0)
1235 return;
1236
1237 wl_list_for_each(output, &es->compositor->output_list, link) {
Pekka Paalanen01e00682017-03-24 16:21:06 +02001238 output_bit = 1u << output->id;
1239 if (!(output_bit & different))
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001240 continue;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001241
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001242 wl_list_for_each(head, &output->head_list, output_link) {
1243 weston_surface_send_enter_leave(es, head,
1244 output_bit & entered,
1245 output_bit & left);
1246 }
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001247 }
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301248 /*
1249 * Change in surfaces' output mask might trigger a change in its
1250 * protection.
1251 */
Ankit Nautiyalfaa5ab42019-07-08 13:23:24 +05301252 weston_schedule_surface_protection_update(es->compositor);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001253}
1254
Semi Malinene7a52fb2018-04-26 11:08:10 +02001255static void
1256notify_view_output_destroy(struct wl_listener *listener, void *data)
1257{
1258 struct weston_view *view =
1259 container_of(listener,
1260 struct weston_view, output_destroy_listener);
1261
1262 view->output = NULL;
1263 view->output_destroy_listener.notify = NULL;
1264}
1265
1266/** Set the primary output of the view
1267 *
1268 * \param view The view whose primary output to set
1269 * \param output The new primary output for the view
1270 *
1271 * Set \a output to be the primary output of the \a view.
1272 *
1273 * Notice that the assignment may be temporary; the primary output could be
1274 * automatically changed. Hence, one cannot rely on the value persisting.
1275 *
1276 * Passing NULL as /a output will set the primary output to NULL.
1277 */
1278WL_EXPORT void
1279weston_view_set_output(struct weston_view *view, struct weston_output *output)
1280{
1281 if (view->output_destroy_listener.notify) {
1282 wl_list_remove(&view->output_destroy_listener.link);
1283 view->output_destroy_listener.notify = NULL;
1284 }
1285 view->output = output;
1286 if (output) {
1287 view->output_destroy_listener.notify =
1288 notify_view_output_destroy;
1289 wl_signal_add(&output->destroy_signal,
1290 &view->output_destroy_listener);
1291 }
1292}
1293
Bryce Harrington3f650b82015-12-23 11:01:58 -08001294/** Recalculate which output(s) the surface has views displayed on
1295 *
1296 * \param es The surface to remap to outputs
1297 *
1298 * Finds the output that is showing the largest amount of one
1299 * of the surface's various views. This output becomes the
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001300 * surface's primary output for vsync and frame callback purposes.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001301 *
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001302 * Also notes all outputs of all of the surface's views
Bryce Harrington3f650b82015-12-23 11:01:58 -08001303 * in the output_mask for the surface.
1304 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001305static void
1306weston_surface_assign_output(struct weston_surface *es)
1307{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001308 struct weston_output *new_output;
1309 struct weston_view *view;
1310 pixman_region32_t region;
1311 uint32_t max, area, mask;
1312 pixman_box32_t *e;
1313
1314 new_output = NULL;
1315 max = 0;
1316 mask = 0;
1317 pixman_region32_init(&region);
1318 wl_list_for_each(view, &es->views, surface_link) {
1319 if (!view->output)
1320 continue;
1321
1322 pixman_region32_intersect(&region, &view->transform.boundingbox,
1323 &view->output->region);
1324
1325 e = pixman_region32_extents(&region);
1326 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1327
1328 mask |= view->output_mask;
1329
1330 if (area >= max) {
1331 new_output = view->output;
1332 max = area;
1333 }
1334 }
1335 pixman_region32_fini(&region);
1336
1337 es->output = new_output;
1338 weston_surface_update_output_mask(es, mask);
1339}
1340
Bryce Harrington3f650b82015-12-23 11:01:58 -08001341/** Recalculate which output(s) the view is displayed on
1342 *
1343 * \param ev The view to remap to outputs
1344 *
1345 * Identifies the set of outputs that the view is visible on,
1346 * noting them into the output_mask. The output that the view
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001347 * is most visible on is set as the view's primary output.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001348 *
1349 * Also does the same for the view's surface. See
1350 * weston_surface_assign_output().
1351 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001352static void
1353weston_view_assign_output(struct weston_view *ev)
1354{
1355 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001356 struct weston_output *output, *new_output;
1357 pixman_region32_t region;
1358 uint32_t max, area, mask;
1359 pixman_box32_t *e;
1360
1361 new_output = NULL;
1362 max = 0;
1363 mask = 0;
1364 pixman_region32_init(&region);
1365 wl_list_for_each(output, &ec->output_list, link) {
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03001366 if (output->destroying)
1367 continue;
1368
Jason Ekstranda7af7042013-10-12 22:38:11 -05001369 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001370 &output->region);
1371
1372 e = pixman_region32_extents(&region);
1373 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1374
1375 if (area > 0)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001376 mask |= 1u << output->id;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001377
1378 if (area >= max) {
1379 new_output = output;
1380 max = area;
1381 }
1382 }
1383 pixman_region32_fini(&region);
1384
Semi Malinene7a52fb2018-04-26 11:08:10 +02001385 weston_view_set_output(ev, new_output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001386 ev->output_mask = mask;
1387
1388 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001389}
1390
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001391static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001392weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1393 int from_x, int from_y, int *to_x, int *to_y)
1394{
1395 float x, y;
1396
1397 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1398 weston_view_from_global_float(to, x, y, &x, &y);
1399
1400 *to_x = round(x);
1401 *to_y = round(y);
1402}
1403
1404static void
1405weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1406{
1407 pixman_box32_t *a;
1408 pixman_box32_t b;
1409
1410 a = pixman_region32_extents(&from->geometry.scissor);
1411
1412 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1413 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1414
1415 pixman_region32_fini(&to->geometry.scissor);
1416 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1417}
1418
1419static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001420view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001421 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001422{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001423 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1424 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001425 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001426 { inbox->x1, inbox->y1 },
1427 { inbox->x1, inbox->y2 },
1428 { inbox->x2, inbox->y1 },
1429 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001430 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001431 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001432 int i;
1433
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001434 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001435 /* avoid rounding empty bbox to 1x1 */
1436 pixman_region32_init(bbox);
1437 return;
1438 }
1439
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001440 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001441 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001442 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001443 if (x < min_x)
1444 min_x = x;
1445 if (x > max_x)
1446 max_x = x;
1447 if (y < min_y)
1448 min_y = y;
1449 if (y > max_y)
1450 max_y = y;
1451 }
1452
Pekka Paalanen219b9822012-02-08 15:38:37 +02001453 int_x = floorf(min_x);
1454 int_y = floorf(min_y);
1455 pixman_region32_init_rect(bbox, int_x, int_y,
1456 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001457}
1458
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001459static void
Michael Olbrichcb04f982020-06-08 10:22:32 +02001460weston_view_update_transform_scissor(struct weston_view *view,
1461 pixman_region32_t *region)
1462{
1463 struct weston_view *parent = view->geometry.parent;
1464
1465 if (parent) {
1466 if (parent->geometry.scissor_enabled) {
1467 view->geometry.scissor_enabled = true;
1468 weston_view_transfer_scissor(parent, view);
1469 } else {
1470 view->geometry.scissor_enabled = false;
1471 }
1472 }
1473
1474 if (view->geometry.scissor_enabled)
1475 pixman_region32_intersect(region, region,
1476 &view->geometry.scissor);
1477}
1478static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001479weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001480{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001481 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001482
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001483 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001484 view->geometry.x = roundf(view->geometry.x);
1485 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001486
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001487 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001488 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1489 view->transform.position.matrix.d[12] = view->geometry.x;
1490 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001491
Jason Ekstranda7af7042013-10-12 22:38:11 -05001492 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001493
Jason Ekstranda7af7042013-10-12 22:38:11 -05001494 view->transform.inverse = view->transform.position.matrix;
1495 view->transform.inverse.d[12] = -view->geometry.x;
1496 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001497
Jason Ekstranda7af7042013-10-12 22:38:11 -05001498 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001499 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001500 view->surface->width,
1501 view->surface->height);
Michael Olbrichcb04f982020-06-08 10:22:32 +02001502
1503 weston_view_update_transform_scissor(view, &view->transform.boundingbox);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001504
1505 pixman_region32_translate(&view->transform.boundingbox,
1506 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001507
Jason Ekstranda7af7042013-10-12 22:38:11 -05001508 if (view->alpha == 1.0) {
1509 pixman_region32_copy(&view->transform.opaque,
1510 &view->surface->opaque);
Michael Olbrich0e4f0972020-08-19 11:31:03 +02001511 pixman_region32_intersect(&view->transform.opaque,
1512 &view->transform.opaque,
1513 &view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001514 pixman_region32_translate(&view->transform.opaque,
1515 view->geometry.x,
1516 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001517 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001518}
1519
1520static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001521weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001522{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001523 struct weston_view *parent = view->geometry.parent;
1524 struct weston_matrix *matrix = &view->transform.matrix;
1525 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001526 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001527 pixman_region32_t surfregion;
1528 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001529
Jason Ekstranda7af7042013-10-12 22:38:11 -05001530 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001531
1532 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001533 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1534 view->transform.position.matrix.d[12] = view->geometry.x;
1535 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001536
1537 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001538 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001539 weston_matrix_multiply(matrix, &tform->matrix);
1540
Pekka Paalanen483243f2013-03-08 14:56:50 +02001541 if (parent)
1542 weston_matrix_multiply(matrix, &parent->transform.matrix);
1543
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001544 if (weston_matrix_invert(inverse, matrix) < 0) {
1545 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001546 weston_log("error: weston_view %p"
1547 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001548 return -1;
1549 }
1550
Pekka Paalanen380adf52015-02-16 14:39:11 +02001551 pixman_region32_init_rect(&surfregion, 0, 0,
1552 view->surface->width, view->surface->height);
Michael Olbrichcb04f982020-06-08 10:22:32 +02001553
1554 weston_view_update_transform_scissor(view, &surfregion);
1555
Pekka Paalanen380adf52015-02-16 14:39:11 +02001556 surfbox = pixman_region32_extents(&surfregion);
1557
1558 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1559 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001560
Michael Olbrich0e4f0972020-08-19 11:31:03 +02001561 if (view->alpha == 1.0 &&
1562 matrix->type == WESTON_MATRIX_TRANSFORM_TRANSLATE) {
1563 pixman_region32_copy(&view->transform.opaque,
1564 &view->surface->opaque);
1565 pixman_region32_intersect(&view->transform.opaque,
1566 &view->transform.opaque,
1567 &view->geometry.scissor);
1568 pixman_region32_translate(&view->transform.opaque,
1569 matrix->d[12],
1570 matrix->d[13]);
1571 }
1572
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001573 return 0;
1574}
1575
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001576static struct weston_layer *
1577get_view_layer(struct weston_view *view)
1578{
1579 if (view->parent_view)
1580 return get_view_layer(view->parent_view);
1581 return view->layer_link.layer;
1582}
1583
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001584WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001585weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001586{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001587 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001588 struct weston_layer *layer;
1589 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001590
Jason Ekstranda7af7042013-10-12 22:38:11 -05001591 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001592 return;
1593
Pekka Paalanen483243f2013-03-08 14:56:50 +02001594 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001595 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001596
Jason Ekstranda7af7042013-10-12 22:38:11 -05001597 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001598
Jason Ekstranda7af7042013-10-12 22:38:11 -05001599 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001600
Jason Ekstranda7af7042013-10-12 22:38:11 -05001601 pixman_region32_fini(&view->transform.boundingbox);
1602 pixman_region32_fini(&view->transform.opaque);
1603 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001604
Pekka Paalanencd403622012-01-25 13:37:39 +02001605 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001606 if (view->geometry.transformation_list.next ==
1607 &view->transform.position.link &&
1608 view->geometry.transformation_list.prev ==
1609 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001610 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001611 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001612 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001613 if (weston_view_update_transform_enable(view) < 0)
1614 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001615 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001616
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001617 layer = get_view_layer(view);
1618 if (layer) {
1619 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001620 pixman_region32_intersect(&view->transform.boundingbox,
1621 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001622 pixman_region32_intersect(&view->transform.opaque,
1623 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001624 pixman_region32_fini(&mask);
1625 }
1626
Jason Ekstranda7af7042013-10-12 22:38:11 -05001627 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001628
Jason Ekstranda7af7042013-10-12 22:38:11 -05001629 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001630
Jason Ekstranda7af7042013-10-12 22:38:11 -05001631 wl_signal_emit(&view->surface->compositor->transform_signal,
1632 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001633}
1634
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001635WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001636weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001637{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001638 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001639
1640 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001641 * The invariant: if view->geometry.dirty, then all views
1642 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001643 * Corollary: if not parent->geometry.dirty, then all ancestors
1644 * are not dirty.
1645 */
1646
Jason Ekstranda7af7042013-10-12 22:38:11 -05001647 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001648 return;
1649
Jason Ekstranda7af7042013-10-12 22:38:11 -05001650 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001651
Jason Ekstranda7af7042013-10-12 22:38:11 -05001652 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001653 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001654 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001655}
1656
1657WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001658weston_view_to_global_fixed(struct weston_view *view,
1659 wl_fixed_t vx, wl_fixed_t vy,
1660 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001661{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001662 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001663
Jason Ekstranda7af7042013-10-12 22:38:11 -05001664 weston_view_to_global_float(view,
1665 wl_fixed_to_double(vx),
1666 wl_fixed_to_double(vy),
1667 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001668 *x = wl_fixed_from_double(xf);
1669 *y = wl_fixed_from_double(yf);
1670}
1671
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001672WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001673weston_view_from_global_float(struct weston_view *view,
1674 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001675{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001676 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001677 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1678
Jason Ekstranda7af7042013-10-12 22:38:11 -05001679 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001680
1681 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001682 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001683 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001684 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001685 *vx = 0;
1686 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001687 return;
1688 }
1689
Jason Ekstranda7af7042013-10-12 22:38:11 -05001690 *vx = v.f[0] / v.f[3];
1691 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001692 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001693 *vx = x - view->geometry.x;
1694 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001695 }
1696}
1697
1698WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001699weston_view_from_global_fixed(struct weston_view *view,
1700 wl_fixed_t x, wl_fixed_t y,
1701 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001702{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001703 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001704
Jason Ekstranda7af7042013-10-12 22:38:11 -05001705 weston_view_from_global_float(view,
1706 wl_fixed_to_double(x),
1707 wl_fixed_to_double(y),
1708 &vxf, &vyf);
1709 *vx = wl_fixed_from_double(vxf);
1710 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001711}
1712
1713WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001714weston_view_from_global(struct weston_view *view,
1715 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001716{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001717 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001718
Jason Ekstranda7af7042013-10-12 22:38:11 -05001719 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1720 *vx = floorf(vxf);
1721 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001722}
1723
Bryce Harrington3f650b82015-12-23 11:01:58 -08001724/**
1725 * \param surface The surface to be repainted
1726 *
1727 * Marks the output(s) that the surface is shown on as needing to be
1728 * repainted. See weston_output_schedule_repaint().
1729 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001730WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001731weston_surface_schedule_repaint(struct weston_surface *surface)
1732{
1733 struct weston_output *output;
1734
1735 wl_list_for_each(output, &surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001736 if (surface->output_mask & (1u << output->id))
Kristian Høgsberg98238702012-08-03 16:29:12 -04001737 weston_output_schedule_repaint(output);
1738}
1739
Bryce Harrington3f650b82015-12-23 11:01:58 -08001740/**
1741 * \param view The view to be repainted
1742 *
1743 * Marks the output(s) that the view is shown on as needing to be
1744 * repainted. See weston_output_schedule_repaint().
1745 */
Kristian Høgsberg98238702012-08-03 16:29:12 -04001746WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001747weston_view_schedule_repaint(struct weston_view *view)
1748{
1749 struct weston_output *output;
1750
1751 wl_list_for_each(output, &view->surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001752 if (view->output_mask & (1u << output->id))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001753 weston_output_schedule_repaint(output);
1754}
1755
Pekka Paalanene508ce62015-02-19 13:59:55 +02001756/**
1757 * XXX: This function does it the wrong way.
1758 * surface->damage is the damage from the client, and causes
1759 * surface_flush_damage() to copy pixels. No window management action can
1760 * cause damage to the client-provided content, warranting re-upload!
1761 *
1762 * Instead of surface->damage, this function should record the damage
1763 * with all the views for this surface to avoid extraneous texture
1764 * uploads.
1765 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001766WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001767weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001768{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001769 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001770 0, 0, surface->width,
1771 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001772
Kristian Høgsberg98238702012-08-03 16:29:12 -04001773 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001774}
1775
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001776WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001777weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001778{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001779 if (view->geometry.x == x && view->geometry.y == y)
1780 return;
1781
Jason Ekstranda7af7042013-10-12 22:38:11 -05001782 view->geometry.x = x;
1783 view->geometry.y = y;
1784 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001785}
1786
Pekka Paalanen483243f2013-03-08 14:56:50 +02001787static void
1788transform_parent_handle_parent_destroy(struct wl_listener *listener,
1789 void *data)
1790{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001791 struct weston_view *view =
1792 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001793 geometry.parent_destroy_listener);
1794
Jason Ekstranda7af7042013-10-12 22:38:11 -05001795 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001796}
1797
1798WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001799weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001800 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001801{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001802 if (view->geometry.parent) {
1803 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1804 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001805
1806 if (!parent)
1807 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001808 }
1809
Jason Ekstranda7af7042013-10-12 22:38:11 -05001810 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001811
Jason Ekstranda7af7042013-10-12 22:38:11 -05001812 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001813 transform_parent_handle_parent_destroy;
1814 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001815 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001816 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001817 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001818 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001819 }
1820
Jason Ekstranda7af7042013-10-12 22:38:11 -05001821 weston_view_geometry_dirty(view);
1822}
1823
Pekka Paalanen380adf52015-02-16 14:39:11 +02001824/** Set a clip mask rectangle on a view
1825 *
1826 * \param view The view to set the clip mask on.
1827 * \param x Top-left corner X coordinate of the clip rectangle.
1828 * \param y Top-left corner Y coordinate of the clip rectangle.
1829 * \param width Width of the clip rectangle, non-negative.
1830 * \param height Height of the clip rectangle, non-negative.
1831 *
1832 * A shell may set a clip mask rectangle on a view. Everything outside
1833 * the rectangle is cut away for input and output purposes: it is
1834 * not drawn and cannot be hit by hit-test based input like pointer
1835 * motion or touch-downs. Everything inside the rectangle will behave
1836 * normally. Clients are unaware of clipping.
1837 *
Yong Bakos4c72e292016-04-28 11:59:10 -05001838 * The rectangle is set in surface-local coordinates. Setting a clip
Pekka Paalanen380adf52015-02-16 14:39:11 +02001839 * mask rectangle does not affect the view position, the view is positioned
1840 * as it would be without a clip. The clip also does not change
1841 * weston_surface::width,height.
1842 *
1843 * The clip mask rectangle is part of transformation inheritance
1844 * (weston_view_set_transform_parent()). A clip set in the root of the
1845 * transformation inheritance tree will affect all views in the tree.
1846 * A clip can be set only on the root view. Attempting to set a clip
1847 * on view that has a transformation parent will fail. Assigning a parent
1848 * to a view that has a clip set will cause the clip to be forgotten.
1849 *
1850 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1851 * on the additional transformations in the child views. These transformations
1852 * may not rotate the coordinate axes, i.e., only translation and scaling
1853 * are allowed. Violating this restriction causes the clipping to malfunction.
1854 * Furthermore, using scaling may cause rounding errors in child clipping.
1855 *
1856 * The clip mask rectangle is not automatically adjusted based on
1857 * wl_surface.attach dx and dy arguments.
1858 *
1859 * A clip mask rectangle can be set only if the compositor capability
1860 * WESTON_CAP_VIEW_CLIP_MASK is present.
1861 *
1862 * This function sets the clip mask rectangle and schedules a repaint for
1863 * the view.
1864 */
1865WL_EXPORT void
1866weston_view_set_mask(struct weston_view *view,
1867 int x, int y, int width, int height)
1868{
1869 struct weston_compositor *compositor = view->surface->compositor;
1870
1871 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1872 weston_log("%s not allowed without capability!\n", __func__);
1873 return;
1874 }
1875
1876 if (view->geometry.parent) {
1877 weston_log("view %p has a parent, clip forbidden!\n", view);
1878 return;
1879 }
1880
1881 if (width < 0 || height < 0) {
1882 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1883 x, y, width, height);
1884 return;
1885 }
1886
1887 pixman_region32_fini(&view->geometry.scissor);
1888 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1889 view->geometry.scissor_enabled = true;
1890 weston_view_geometry_dirty(view);
1891 weston_view_schedule_repaint(view);
1892}
1893
1894/** Remove the clip mask from a view
1895 *
1896 * \param view The view to remove the clip mask from.
1897 *
1898 * Removed the clip mask rectangle and schedules a repaint.
1899 *
1900 * \sa weston_view_set_mask
1901 */
1902WL_EXPORT void
1903weston_view_set_mask_infinite(struct weston_view *view)
1904{
1905 view->geometry.scissor_enabled = false;
1906 weston_view_geometry_dirty(view);
1907 weston_view_schedule_repaint(view);
1908}
1909
Armin Krezović0da12b82016-06-30 06:04:33 +02001910/* Check if view should be displayed
1911 *
1912 * The indicator is set manually when assigning
1913 * a view to a surface.
1914 *
1915 * This needs reworking. See the thread starting at:
1916 *
1917 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1918 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001919WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001920weston_view_is_mapped(struct weston_view *view)
1921{
Armin Krezović0da12b82016-06-30 06:04:33 +02001922 return view->is_mapped;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001923}
1924
Philipp Zabel70decd52018-09-03 20:11:15 +02001925/* Check if view is opaque in specified region
1926 *
1927 * \param view The view to check for opacity.
1928 * \param region The region to check for opacity, in view coordinates.
1929 *
1930 * Returns true if the view is opaque in the specified region, because view
1931 * alpha is 1.0 and either the opaque region set by the client contains the
1932 * specified region, or the buffer pixel format or solid color is opaque.
1933 */
1934WL_EXPORT bool
1935weston_view_is_opaque(struct weston_view *ev, pixman_region32_t *region)
1936{
1937 pixman_region32_t r;
1938 bool ret = false;
1939
1940 if (ev->alpha < 1.0)
1941 return false;
1942
1943 if (ev->surface->is_opaque)
1944 return true;
1945
Marius Vlad67b382c2020-08-21 13:57:54 +03001946 if (ev->transform.dirty)
Philipp Zabel70decd52018-09-03 20:11:15 +02001947 return false;
Philipp Zabel70decd52018-09-03 20:11:15 +02001948
1949 pixman_region32_init(&r);
1950 pixman_region32_subtract(&r, region, &ev->transform.opaque);
1951
1952 if (!pixman_region32_not_empty(&r))
1953 ret = true;
1954
1955 pixman_region32_fini(&r);
1956
1957 return ret;
1958}
1959
Marius Vlad5f6bee42019-09-11 16:41:04 +03001960/** Check if the view has a valid buffer available
1961 *
1962 * @param ev The view to check if it has a valid buffer.
1963 *
1964 * Returns true if the view has a valid buffer or false otherwise.
1965 */
1966WL_EXPORT bool
1967weston_view_has_valid_buffer(struct weston_view *ev)
1968{
1969 return ev->surface->buffer_ref.buffer != NULL;
1970}
1971
Marius Vlad47e3d1e2019-09-11 16:53:08 +03001972/** Check if the view matches the entire output
1973 *
1974 * @param ev The view to check.
1975 * @param output The output to check against.
1976 *
1977 * Returns true if the view does indeed matches the entire output.
1978 */
1979WL_EXPORT bool
1980weston_view_matches_output_entirely(struct weston_view *ev,
1981 struct weston_output *output)
1982{
1983 pixman_box32_t *extents =
1984 pixman_region32_extents(&ev->transform.boundingbox);
1985
1986 if (extents->x1 != output->x ||
1987 extents->y1 != output->y ||
1988 extents->x2 != output->x + output->width ||
1989 extents->y2 != output->y + output->height)
1990 return false;
1991
1992 return true;
1993}
1994
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03001995/** Find paint node for the given view and output
1996 */
1997WL_EXPORT struct weston_paint_node *
1998weston_view_find_paint_node(struct weston_view *view,
1999 struct weston_output *output)
2000{
2001 struct weston_paint_node *pnode;
2002
2003 wl_list_for_each(pnode, &view->paint_node_list, view_link) {
2004 assert(pnode->surface == view->surface);
2005 if (pnode->output == output)
2006 return pnode;
2007 }
2008
2009 return NULL;
2010}
2011
Armin Krezović0da12b82016-06-30 06:04:33 +02002012/* Check if a surface has a view assigned to it
2013 *
2014 * The indicator is set manually when mapping
2015 * a surface and creating a view for it.
2016 *
2017 * This needs to go. See the thread starting at:
2018 *
2019 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
2020 *
2021 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05002022WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03002023weston_surface_is_mapped(struct weston_surface *surface)
2024{
Armin Krezović0da12b82016-06-30 06:04:33 +02002025 return surface->is_mapped;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03002026}
2027
Pekka Paalanenda75ee12013-11-26 18:19:43 +01002028static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06002029surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002030{
2031 struct weston_view *view;
2032
2033 if (surface->width == width && surface->height == height)
2034 return;
2035
2036 surface->width = width;
2037 surface->height = height;
2038
2039 wl_list_for_each(view, &surface->views, surface_link)
2040 weston_view_geometry_dirty(view);
2041}
2042
Jason Ekstrand5c11a332013-12-04 20:32:03 -06002043WL_EXPORT void
2044weston_surface_set_size(struct weston_surface *surface,
2045 int32_t width, int32_t height)
2046{
2047 assert(!surface->resource);
2048 surface_set_size(surface, width, height);
2049}
2050
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02002051static int
2052fixed_round_up_to_int(wl_fixed_t f)
2053{
2054 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
2055}
2056
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002057static void
Pekka Paalanen59987fa2016-04-26 15:50:59 +03002058convert_size_by_transform_scale(int32_t *width_out, int32_t *height_out,
2059 int32_t width, int32_t height,
2060 uint32_t transform,
2061 int32_t scale)
2062{
2063 assert(scale > 0);
2064
2065 switch (transform) {
2066 case WL_OUTPUT_TRANSFORM_NORMAL:
2067 case WL_OUTPUT_TRANSFORM_180:
2068 case WL_OUTPUT_TRANSFORM_FLIPPED:
2069 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2070 *width_out = width / scale;
2071 *height_out = height / scale;
2072 break;
2073 case WL_OUTPUT_TRANSFORM_90:
2074 case WL_OUTPUT_TRANSFORM_270:
2075 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2076 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2077 *width_out = height / scale;
2078 *height_out = width / scale;
2079 break;
2080 default:
2081 assert(0 && "invalid transform");
2082 }
2083}
2084
2085static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002086weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002087{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002088 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01002089
2090 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02002091 surface->width_from_buffer = 0;
2092 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01002093 return;
2094 }
2095
Pekka Paalanen59987fa2016-04-26 15:50:59 +03002096 convert_size_by_transform_scale(&surface->width_from_buffer,
2097 &surface->height_from_buffer,
2098 surface->buffer_ref.buffer->width,
2099 surface->buffer_ref.buffer->height,
2100 vp->buffer.transform,
2101 vp->buffer.scale);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002102}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02002103
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002104static void
2105weston_surface_update_size(struct weston_surface *surface)
2106{
2107 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
2108 int32_t width, height;
2109
2110 width = surface->width_from_buffer;
2111 height = surface->height_from_buffer;
2112
2113 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02002114 surface_set_size(surface,
2115 vp->surface.width, vp->surface.height);
2116 return;
2117 }
2118
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002119 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03002120 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
2121 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
2122
2123 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02002124 return;
2125 }
2126
Jason Ekstrand5c11a332013-12-04 20:32:03 -06002127 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002128}
2129
Marius Vlad9fdda7f2019-06-11 16:08:55 +03002130/** weston_compositor_get_time
2131 * \ingroup compositor
2132 */
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02002133WL_EXPORT void
2134weston_compositor_get_time(struct timespec *time)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05002135{
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02002136 clock_gettime(CLOCK_REALTIME, time);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05002137}
2138
Marius Vlad9fdda7f2019-06-11 16:08:55 +03002139/** weston_compositor_pick_view
2140 * \ingroup compositor
2141 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002142WL_EXPORT struct weston_view *
2143weston_compositor_pick_view(struct weston_compositor *compositor,
2144 wl_fixed_t x, wl_fixed_t y,
2145 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02002146{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002147 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02002148 wl_fixed_t view_x, view_y;
2149 int view_ix, view_iy;
2150 int ix = wl_fixed_to_int(x);
2151 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02002152
Pekka Paalanen6551c092021-05-03 16:09:45 +03002153 /* Can't use paint node list: occlusion by input regions, not opaque. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002154 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02002155 if (!pixman_region32_contains_point(
2156 &view->transform.boundingbox, ix, iy, NULL))
2157 continue;
2158
2159 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
2160 view_ix = wl_fixed_to_int(view_x);
2161 view_iy = wl_fixed_to_int(view_y);
2162
2163 if (!pixman_region32_contains_point(&view->surface->input,
2164 view_ix, view_iy, NULL))
2165 continue;
2166
Pekka Paalanen380adf52015-02-16 14:39:11 +02002167 if (view->geometry.scissor_enabled &&
2168 !pixman_region32_contains_point(&view->geometry.scissor,
2169 view_ix, view_iy, NULL))
2170 continue;
2171
Pekka Paalanenfc22a522015-02-18 15:08:29 +02002172 *vx = view_x;
2173 *vy = view_y;
2174 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02002175 }
2176
Derek Foremanf9318d12015-05-11 15:40:11 -05002177 *vx = wl_fixed_from_int(-1000000);
2178 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02002179 return NULL;
2180}
2181
2182static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002183weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002184{
Daniel Stone37816df2012-05-16 18:45:18 +01002185 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002186
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07002187 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002188 return;
2189
Daniel Stone37816df2012-05-16 18:45:18 +01002190 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04002191 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002192}
2193
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002194WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002195weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05002196{
Daniel Stone4dab5db2012-05-30 16:31:53 +01002197 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002198
Jason Ekstranda7af7042013-10-12 22:38:11 -05002199 if (!weston_view_is_mapped(view))
2200 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002201
Jason Ekstranda7af7042013-10-12 22:38:11 -05002202 weston_view_damage_below(view);
Semi Malinene7a52fb2018-04-26 11:08:10 +02002203 weston_view_set_output(view, NULL);
Xiong Zhang97116532013-10-23 13:58:31 +08002204 view->plane = NULL;
Armin Krezovićf8486c32016-06-30 06:04:28 +02002205 view->is_mapped = false;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002206 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002207 wl_list_remove(&view->link);
2208 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002209 view->output_mask = 0;
2210 weston_surface_assign_output(view->surface);
2211
2212 if (weston_surface_is_mapped(view->surface))
2213 return;
2214
2215 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002216 struct weston_touch *touch = weston_seat_get_touch(seat);
2217 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2218 struct weston_keyboard *keyboard =
2219 weston_seat_get_keyboard(seat);
2220
2221 if (keyboard && keyboard->focus == view->surface)
2222 weston_keyboard_set_focus(keyboard, NULL);
2223 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05002224 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05002225 if (touch && touch->focus == view)
2226 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01002227 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002228}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002229
Jason Ekstranda7af7042013-10-12 22:38:11 -05002230WL_EXPORT void
2231weston_surface_unmap(struct weston_surface *surface)
2232{
2233 struct weston_view *view;
2234
Armin Krezovićf8486c32016-06-30 06:04:28 +02002235 surface->is_mapped = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002236 wl_list_for_each(view, &surface->views, surface_link)
2237 weston_view_unmap(view);
2238 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05002239}
2240
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002241static void
2242weston_surface_reset_pending_buffer(struct weston_surface *surface)
2243{
Jason Ekstrand7b982072014-05-20 14:33:03 -05002244 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002245 surface->pending.sx = 0;
2246 surface->pending.sy = 0;
2247 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002248 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002249}
2250
Jason Ekstranda7af7042013-10-12 22:38:11 -05002251WL_EXPORT void
2252weston_view_destroy(struct weston_view *view)
2253{
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002254 struct weston_paint_node *pnode, *pntmp;
2255
Jason Ekstranda7af7042013-10-12 22:38:11 -05002256 wl_signal_emit(&view->destroy_signal, view);
2257
2258 assert(wl_list_empty(&view->geometry.child_list));
2259
2260 if (weston_view_is_mapped(view)) {
2261 weston_view_unmap(view);
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002262 weston_compositor_build_view_list(view->surface->compositor,
2263 NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002264 }
2265
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002266 wl_list_for_each_safe(pnode, pntmp, &view->paint_node_list, view_link)
2267 weston_paint_node_destroy(pnode);
2268
Jason Ekstranda7af7042013-10-12 22:38:11 -05002269 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002270 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002271
2272 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02002273 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002274 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002275 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002276
2277 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen944fae82018-05-22 13:15:58 +03002278 weston_view_set_output(view, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002279
Jason Ekstranda7af7042013-10-12 22:38:11 -05002280 wl_list_remove(&view->surface_link);
2281
2282 free(view);
2283}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002284
2285WL_EXPORT void
2286weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05002287{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002288 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002289 struct weston_view *ev, *nv;
Jonas Ådahld3414f22016-07-22 17:56:31 +08002290 struct weston_pointer_constraint *constraint, *next_constraint;
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002291 struct weston_paint_node *pnode, *pntmp;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04002292
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02002293 if (--surface->ref_count > 0)
2294 return;
2295
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03002296 assert(surface->resource == NULL);
2297
Pekka Paalanenca790762015-04-17 14:23:38 +03002298 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02002299
Pekka Paalanene67858b2013-04-25 13:57:42 +03002300 assert(wl_list_empty(&surface->subsurface_list_pending));
2301 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02002302
Jason Ekstranda7af7042013-10-12 22:38:11 -05002303 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
2304 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002305
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002306 wl_list_for_each_safe(pnode, pntmp,
2307 &surface->paint_node_list, surface_link) {
2308 weston_paint_node_destroy(pnode);
2309 }
2310
Jason Ekstrand7b982072014-05-20 14:33:03 -05002311 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002312
Pekka Paalanende685b82012-12-04 15:58:12 +02002313 weston_buffer_reference(&surface->buffer_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002314 weston_buffer_release_reference(&surface->buffer_release_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04002315
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02002316 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002317 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002318 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02002319
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002320 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002321 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002322
Pekka Paalanen133e4392014-09-23 22:08:46 -04002323 weston_presentation_feedback_discard_list(&surface->feedback_list);
2324
Jonas Ådahld3414f22016-07-22 17:56:31 +08002325 wl_list_for_each_safe(constraint, next_constraint,
2326 &surface->pointer_constraints,
2327 link)
2328 weston_pointer_constraint_destroy(constraint);
2329
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03002330 fd_clear(&surface->acquire_fence_fd);
2331
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04002332 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05002333}
2334
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002335static void
2336destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08002337{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002338 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08002339
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03002340 assert(surface);
2341
Giulio Camuffo0d379742013-11-15 22:06:15 +01002342 /* Set the resource to NULL, since we don't want to leave a
2343 * dangling pointer if the surface was refcounted and survives
2344 * the weston_surface_destroy() call. */
2345 surface->resource = NULL;
Pekka Paalanen4826f872016-04-22 14:14:38 +03002346
2347 if (surface->viewport_resource)
2348 wl_resource_set_user_data(surface->viewport_resource, NULL);
2349
Alexandros Frantzis27d7c392018-10-19 12:14:11 +03002350 if (surface->synchronization_resource) {
2351 wl_resource_set_user_data(surface->synchronization_resource,
2352 NULL);
2353 }
2354
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002355 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08002356}
2357
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002358static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002359weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
2360{
2361 struct weston_buffer *buffer =
2362 container_of(listener, struct weston_buffer, destroy_listener);
2363
2364 wl_signal_emit(&buffer->destroy_signal, buffer);
2365 free(buffer);
2366}
2367
Giulio Camuffoe058cd12013-12-12 14:14:29 +01002368WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002369weston_buffer_from_resource(struct wl_resource *resource)
2370{
2371 struct weston_buffer *buffer;
2372 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002373
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002374 listener = wl_resource_get_destroy_listener(resource,
2375 weston_buffer_destroy_handler);
2376
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002377 if (listener)
2378 return container_of(listener, struct weston_buffer,
2379 destroy_listener);
2380
2381 buffer = zalloc(sizeof *buffer);
2382 if (buffer == NULL)
2383 return NULL;
2384
2385 buffer->resource = resource;
2386 wl_signal_init(&buffer->destroy_signal);
2387 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04002388 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002389 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002390
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002391 return buffer;
2392}
2393
2394static void
Pekka Paalanende685b82012-12-04 15:58:12 +02002395weston_buffer_reference_handle_destroy(struct wl_listener *listener,
2396 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002397{
Pekka Paalanende685b82012-12-04 15:58:12 +02002398 struct weston_buffer_reference *ref =
2399 container_of(listener, struct weston_buffer_reference,
2400 destroy_listener);
2401
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002402 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02002403 ref->buffer = NULL;
2404}
2405
2406WL_EXPORT void
2407weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002408 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002409{
2410 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05002411 ref->buffer->busy_count--;
2412 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002413 assert(wl_resource_get_client(ref->buffer->resource));
Matt Hoosier3052bc72017-09-26 08:09:40 -05002414 wl_buffer_send_release(ref->buffer->resource);
Kristian Høgsberg20347802013-03-04 12:07:46 -05002415 }
Pekka Paalanende685b82012-12-04 15:58:12 +02002416 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002417 }
2418
Pekka Paalanende685b82012-12-04 15:58:12 +02002419 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002420 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002421 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02002422 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02002423 }
2424
Pekka Paalanende685b82012-12-04 15:58:12 +02002425 ref->buffer = buffer;
2426 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
2427}
2428
2429static void
Alexandros Frantzis67629672018-10-19 12:14:11 +03002430weston_buffer_release_reference_handle_destroy(struct wl_listener *listener,
2431 void *data)
2432{
2433 struct weston_buffer_release_reference *ref =
2434 container_of(listener, struct weston_buffer_release_reference,
2435 destroy_listener);
2436
2437 assert((struct wl_resource *)data == ref->buffer_release->resource);
2438 ref->buffer_release = NULL;
2439}
2440
2441static void
2442weston_buffer_release_destroy(struct weston_buffer_release *buffer_release)
2443{
2444 struct wl_resource *resource = buffer_release->resource;
2445 int release_fence_fd = buffer_release->fence_fd;
2446
2447 if (release_fence_fd >= 0) {
2448 zwp_linux_buffer_release_v1_send_fenced_release(
2449 resource, release_fence_fd);
2450 } else {
2451 zwp_linux_buffer_release_v1_send_immediate_release(
2452 resource);
2453 }
2454
2455 wl_resource_destroy(resource);
2456}
2457
2458WL_EXPORT void
2459weston_buffer_release_reference(struct weston_buffer_release_reference *ref,
2460 struct weston_buffer_release *buffer_release)
2461{
2462 if (buffer_release == ref->buffer_release)
2463 return;
2464
2465 if (ref->buffer_release) {
2466 ref->buffer_release->ref_count--;
2467 wl_list_remove(&ref->destroy_listener.link);
2468 if (ref->buffer_release->ref_count == 0)
2469 weston_buffer_release_destroy(ref->buffer_release);
2470 }
2471
2472 if (buffer_release) {
2473 buffer_release->ref_count++;
2474 wl_resource_add_destroy_listener(buffer_release->resource,
2475 &ref->destroy_listener);
2476 }
2477
2478 ref->buffer_release = buffer_release;
2479 ref->destroy_listener.notify =
2480 weston_buffer_release_reference_handle_destroy;
2481}
2482
2483WL_EXPORT void
2484weston_buffer_release_move(struct weston_buffer_release_reference *dest,
2485 struct weston_buffer_release_reference *src)
2486{
2487 weston_buffer_release_reference(dest, src->buffer_release);
2488 weston_buffer_release_reference(src, NULL);
2489}
2490
2491static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002492weston_surface_attach(struct weston_surface *surface,
2493 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002494{
2495 weston_buffer_reference(&surface->buffer_ref, buffer);
2496
Pekka Paalanena6421c42012-12-04 15:58:10 +02002497 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002498 if (weston_surface_is_mapped(surface))
2499 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002500 }
2501
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002502 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02002503
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002504 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002505 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002506}
2507
Marius Vlad9fdda7f2019-06-11 16:08:55 +03002508/** weston_compositor_damage_all
2509 * \ingroup compositor
2510 */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002511WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002512weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002513{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002514 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002515
2516 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002517 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002518}
2519
Marius Vlad55d87362019-06-11 01:15:35 +03002520/**
2521 * \ingroup output
2522 */
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05002523WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002524weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002525{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002526 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002527
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002528 pixman_region32_union(&compositor->primary_plane.damage,
2529 &compositor->primary_plane.damage,
2530 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002531 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002532}
2533
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04002534static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002535surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002536{
Pekka Paalanende685b82012-12-04 15:58:12 +02002537 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002538 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002539 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002540
Marius Vlad2a1b7862019-09-05 13:12:18 +03002541 if (pixman_region32_not_empty(&surface->damage))
Marius Vlad3203ff62019-09-05 14:56:12 +03002542 TL_POINT(surface->compositor, "core_flush_damage", TLP_SURFACE(surface),
Pekka Paalanenb5026542014-11-12 15:09:24 +02002543 TLP_OUTPUT(surface->output), TLP_END);
2544
Jason Ekstrandef540082014-06-26 10:37:36 -07002545 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002546}
2547
2548static void
2549view_accumulate_damage(struct weston_view *view,
2550 pixman_region32_t *opaque)
2551{
2552 pixman_region32_t damage;
2553
2554 pixman_region32_init(&damage);
2555 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002556 pixman_box32_t *extents;
2557
Jason Ekstranda7af7042013-10-12 22:38:11 -05002558 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002559 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002560 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002561 pixman_region32_copy(&damage, &view->surface->damage);
2562 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002563 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002564 }
2565
Pekka Paalanen380adf52015-02-16 14:39:11 +02002566 pixman_region32_intersect(&damage, &damage,
2567 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002568 pixman_region32_subtract(&damage, &damage, opaque);
2569 pixman_region32_union(&view->plane->damage,
2570 &view->plane->damage, &damage);
2571 pixman_region32_fini(&damage);
2572 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002573 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002574}
2575
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002576static void
Michael Olbrichc5ea4952020-08-11 12:42:35 +02002577output_accumulate_damage(struct weston_output *output)
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002578{
Michael Olbrichc5ea4952020-08-11 12:42:35 +02002579 struct weston_compositor *ec = output->compositor;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002580 struct weston_plane *plane;
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002581 struct weston_paint_node *pnode;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002582 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002583
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002584 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002585
2586 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002587 pixman_region32_copy(&plane->clip, &clip);
2588
2589 pixman_region32_init(&opaque);
2590
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002591 wl_list_for_each(pnode, &output->paint_node_z_order_list,
2592 z_order_link) {
2593 if (pnode->view->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002594 continue;
2595
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002596 view_accumulate_damage(pnode->view, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002597 }
2598
2599 pixman_region32_union(&clip, &clip, &opaque);
2600 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002601 }
2602
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002603 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002604
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002605 wl_list_for_each(pnode, &output->paint_node_z_order_list,
2606 z_order_link) {
2607 pnode->surface->touched = false;
2608 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002609
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002610 wl_list_for_each(pnode, &output->paint_node_z_order_list,
2611 z_order_link) {
Michael Olbrichc5ea4952020-08-11 12:42:35 +02002612 /* Ignore views not visible on the current output */
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002613 /* TODO: turn this into assert once z_order_list is pruned. */
2614 if (!(pnode->view->output_mask & (1u << output->id)))
Michael Olbrichc5ea4952020-08-11 12:42:35 +02002615 continue;
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002616 if (pnode->surface->touched)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002617 continue;
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002618 pnode->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002619
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002620 surface_flush_damage(pnode->surface);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002621
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002622 /* Both the renderer and the backend have seen the buffer
2623 * by now. If renderer needs the buffer, it has its own
2624 * reference set. If the backend wants to keep the buffer
2625 * around for migrating the surface into a non-primary plane
2626 * later, keep_buffer is true. Otherwise, drop the core
2627 * reference now, and allow early buffer release. This enables
2628 * clients to use single-buffering.
2629 */
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002630 if (!pnode->surface->keep_buffer) {
2631 weston_buffer_reference(&pnode->surface->buffer_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002632 weston_buffer_release_reference(
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002633 &pnode->surface->buffer_release_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002634 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002635 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002636}
2637
2638static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002639surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002640{
2641 struct weston_subsurface *sub;
2642
Pekka Paalanene67858b2013-04-25 13:57:42 +03002643 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002644 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002645 continue;
2646
Jason Ekstranda7af7042013-10-12 22:38:11 -05002647 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2648 wl_list_init(&sub->surface->views);
2649
2650 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002651 }
2652}
2653
2654static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002655surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002656{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002657 struct weston_subsurface *sub;
2658 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002659
Jason Ekstranda7af7042013-10-12 22:38:11 -05002660 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2661 if (sub->surface == surface)
2662 continue;
2663
George Kiagiadakised04d382014-06-13 18:10:26 +02002664 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2665 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002666 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002667 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002668
2669 surface_free_unused_subsurface_views(sub->surface);
2670 }
2671}
2672
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002673static struct weston_paint_node *
2674view_ensure_paint_node(struct weston_view *view, struct weston_output *output)
2675{
2676 struct weston_paint_node *pnode;
2677
2678 if (!output)
2679 return NULL;
2680
2681 pnode = weston_view_find_paint_node(view, output);
2682 if (pnode)
2683 return pnode;
2684
2685 return weston_paint_node_create(view->surface, view, output);
2686}
2687
Jason Ekstranda7af7042013-10-12 22:38:11 -05002688static void
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002689add_to_z_order_list(struct weston_output *output,
2690 struct weston_paint_node *pnode)
2691{
2692 if (!pnode)
2693 return;
2694
2695 wl_list_remove(&pnode->z_order_link);
2696 wl_list_insert(output->paint_node_z_order_list.prev,
2697 &pnode->z_order_link);
2698}
2699
2700static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002701view_list_add_subsurface_view(struct weston_compositor *compositor,
2702 struct weston_subsurface *sub,
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002703 struct weston_view *parent,
2704 struct weston_output *output)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002705{
2706 struct weston_subsurface *child;
2707 struct weston_view *view = NULL, *iv;
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002708 struct weston_paint_node *pnode;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002709
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002710 if (!weston_surface_is_mapped(sub->surface))
2711 return;
2712
Jason Ekstranda7af7042013-10-12 22:38:11 -05002713 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2714 if (iv->geometry.parent == parent) {
2715 view = iv;
2716 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002717 }
2718 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002719
2720 if (view) {
2721 /* Put it back in the surface's list of views */
2722 wl_list_remove(&view->surface_link);
2723 wl_list_insert(&sub->surface->views, &view->surface_link);
2724 } else {
2725 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002726 weston_view_set_position(view,
2727 sub->position.x,
2728 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002729 weston_view_set_transform_parent(view, parent);
2730 }
2731
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002732 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002733 weston_view_update_transform(view);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002734 view->is_mapped = true;
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002735 pnode = view_ensure_paint_node(view, output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002736
Pekka Paalanenb188e912013-11-19 14:03:35 +02002737 if (wl_list_empty(&sub->surface->subsurface_list)) {
2738 wl_list_insert(compositor->view_list.prev, &view->link);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002739 add_to_z_order_list(output, pnode);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002740 return;
2741 }
2742
2743 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002744 if (child->surface == sub->surface) {
Pekka Paalanenb188e912013-11-19 14:03:35 +02002745 wl_list_insert(compositor->view_list.prev, &view->link);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002746 add_to_z_order_list(output, pnode);
2747 } else {
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002748 view_list_add_subsurface_view(compositor, child, view, output);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002749 }
Pekka Paalanenb188e912013-11-19 14:03:35 +02002750 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002751}
2752
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002753/* This recursively adds the sub-surfaces for a view, relying on the
2754 * sub-surface order. Thus, if a client restacks the sub-surfaces, that
2755 * change first happens to the sub-surface list, and then automatically
2756 * propagates here. See weston_surface_damage_subsurfaces() for how the
2757 * sub-surfaces receive damage when the client changes the state.
2758 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002759static void
2760view_list_add(struct weston_compositor *compositor,
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002761 struct weston_view *view,
2762 struct weston_output *output)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002763{
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002764 struct weston_paint_node *pnode;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002765 struct weston_subsurface *sub;
2766
2767 weston_view_update_transform(view);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002768 pnode = view_ensure_paint_node(view, output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002769
Pekka Paalanenb188e912013-11-19 14:03:35 +02002770 if (wl_list_empty(&view->surface->subsurface_list)) {
2771 wl_list_insert(compositor->view_list.prev, &view->link);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002772 add_to_z_order_list(output, pnode);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002773 return;
2774 }
2775
2776 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002777 if (sub->surface == view->surface) {
Pekka Paalanenb188e912013-11-19 14:03:35 +02002778 wl_list_insert(compositor->view_list.prev, &view->link);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002779 add_to_z_order_list(output, pnode);
2780 } else {
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002781 view_list_add_subsurface_view(compositor, sub, view, output);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002782 }
Pekka Paalanenb188e912013-11-19 14:03:35 +02002783 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002784}
2785
2786static void
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002787weston_compositor_build_view_list(struct weston_compositor *compositor,
2788 struct weston_output *output)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002789{
Loïc Yhuel267b16e2019-09-17 20:14:56 +02002790 struct weston_view *view, *tmp;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002791 struct weston_layer *layer;
2792
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002793 if (output) {
2794 wl_list_remove(&output->paint_node_z_order_list);
2795 wl_list_init(&output->paint_node_z_order_list);
2796 }
2797
Jason Ekstranda7af7042013-10-12 22:38:11 -05002798 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002799 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002800 surface_stash_subsurface_views(view->surface);
2801
Loïc Yhuel267b16e2019-09-17 20:14:56 +02002802 wl_list_for_each_safe(view, tmp, &compositor->view_list, link)
2803 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002804 wl_list_init(&compositor->view_list);
Loïc Yhuel267b16e2019-09-17 20:14:56 +02002805
Jason Ekstranda7af7042013-10-12 22:38:11 -05002806 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002807 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002808 view_list_add(compositor, view, output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002809 }
2810 }
2811
2812 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002813 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002814 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002815}
2816
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002817static void
2818weston_output_take_feedback_list(struct weston_output *output,
2819 struct weston_surface *surface)
2820{
2821 struct weston_view *view;
2822 struct weston_presentation_feedback *feedback;
2823 uint32_t flags = 0xffffffff;
2824
2825 if (wl_list_empty(&surface->feedback_list))
2826 return;
2827
2828 /* All views must have the flag for the flag to survive. */
2829 wl_list_for_each(view, &surface->views, surface_link) {
2830 /* ignore views that are not on this output at all */
2831 if (view->output_mask & (1u << output->id))
2832 flags &= view->psf_flags;
2833 }
2834
2835 wl_list_for_each(feedback, &surface->feedback_list, link)
2836 feedback->psf_flags = flags;
2837
2838 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2839 wl_list_init(&surface->feedback_list);
2840}
2841
David Herrmann1edf44c2013-10-22 17:11:26 +02002842static int
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002843weston_output_repaint(struct weston_output *output, void *repaint_data)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002844{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002845 struct weston_compositor *ec = output->compositor;
Pekka Paalanen6d6b3672021-05-03 14:06:55 +03002846 struct weston_paint_node *pnode;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002847 struct weston_animation *animation, *next;
2848 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002849 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002850 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002851 int r;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002852 uint32_t frame_time_msec;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05302853 enum weston_hdcp_protection highest_requested = WESTON_HDCP_DISABLE;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002854
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002855 if (output->destroying)
2856 return 0;
2857
Marius Vlad3203ff62019-09-05 14:56:12 +03002858 TL_POINT(ec, "core_repaint_begin", TLP_OUTPUT(output), TLP_END);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002859
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002860 /* Rebuild the surface list and update surface transforms up front. */
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002861 weston_compositor_build_view_list(ec, output);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002862
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05302863 /* Find the highest protection desired for an output */
Pekka Paalanen6d6b3672021-05-03 14:06:55 +03002864 wl_list_for_each(pnode, &output->paint_node_z_order_list,
2865 z_order_link) {
2866 /* TODO: turn this into assert once z_order_list is pruned. */
2867 if ((pnode->surface->output_mask & (1u << output->id)) == 0)
2868 continue;
2869
2870 /*
2871 * The desired_protection of the output should be the
2872 * maximum of the desired_protection of the surfaces,
2873 * that are displayed on that output, to avoid
2874 * reducing the protection for existing surfaces.
2875 */
2876 if (pnode->surface->desired_protection > highest_requested)
2877 highest_requested = pnode->surface->desired_protection;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05302878 }
2879
2880 output->desired_protection = highest_requested;
2881
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002882 if (output->assign_planes && !output->disable_planes) {
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002883 output->assign_planes(output, repaint_data);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002884 } else {
Pekka Paalanen6d6b3672021-05-03 14:06:55 +03002885 wl_list_for_each(pnode, &output->paint_node_z_order_list,
2886 z_order_link) {
2887 weston_view_move_to_plane(pnode->view, &ec->primary_plane);
2888 pnode->view->psf_flags = 0;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002889 }
2890 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002891
Pekka Paalanene67858b2013-04-25 13:57:42 +03002892 wl_list_init(&frame_callback_list);
Pekka Paalanen6d6b3672021-05-03 14:06:55 +03002893 wl_list_for_each(pnode, &output->paint_node_z_order_list,
2894 z_order_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002895 /* Note: This operation is safe to do multiple times on the
2896 * same surface.
2897 */
Pekka Paalanen6d6b3672021-05-03 14:06:55 +03002898 if (pnode->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002899 wl_list_insert_list(&frame_callback_list,
Pekka Paalanen6d6b3672021-05-03 14:06:55 +03002900 &pnode->surface->frame_callback_list);
2901 wl_list_init(&pnode->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002902
Pekka Paalanen6d6b3672021-05-03 14:06:55 +03002903 weston_output_take_feedback_list(output, pnode->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002904 }
2905 }
2906
Michael Olbrichc5ea4952020-08-11 12:42:35 +02002907 output_accumulate_damage(output);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002908
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002909 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002910 pixman_region32_intersect(&output_damage,
2911 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002912 pixman_region32_subtract(&output_damage,
2913 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002914
Scott Moreauccbf29d2012-02-22 14:21:41 -07002915 if (output->dirty)
2916 weston_output_update_matrix(output);
2917
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002918 r = output->repaint(output, &output_damage, repaint_data);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002919
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002920 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002921
Daniel Stone09a97e22017-03-01 11:34:06 +00002922 output->repaint_needed = false;
Daniel Stone05df8c12017-03-03 16:59:42 +00002923 if (r == 0)
2924 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002925
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002926 weston_compositor_repick(ec);
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002927
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002928 frame_time_msec = timespec_to_msec(&output->frame_time);
2929
Jonas Ådahldb773762012-06-13 00:01:21 +02002930 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002931 wl_callback_send_done(cb->resource, frame_time_msec);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002932 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002933 }
2934
Scott Moreaud64cf212012-06-08 19:40:54 -06002935 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002936 animation->frame_counter++;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002937 animation->frame(animation, output, &output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002938 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002939
Marius Vlad3203ff62019-09-05 14:56:12 +03002940 TL_POINT(ec, "core_repaint_posted", TLP_OUTPUT(output), TLP_END);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002941
David Herrmann1edf44c2013-10-22 17:11:26 +02002942 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002943}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002944
Pekka Paalanen82919792014-05-21 13:51:49 +03002945static void
2946weston_output_schedule_repaint_reset(struct weston_output *output)
2947{
Daniel Stone05df8c12017-03-03 16:59:42 +00002948 output->repaint_status = REPAINT_NOT_SCHEDULED;
Marius Vlad3203ff62019-09-05 14:56:12 +03002949 TL_POINT(output->compositor, "core_repaint_exit_loop",
2950 TLP_OUTPUT(output), TLP_END);
Pekka Paalanen82919792014-05-21 13:51:49 +03002951}
2952
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002953static int
2954weston_output_maybe_repaint(struct weston_output *output, struct timespec *now,
2955 void *repaint_data)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002956{
Pekka Paalanen0513a952014-05-21 16:17:27 +03002957 struct weston_compositor *compositor = output->compositor;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002958 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002959 int64_t msec_to_repaint;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002960
Daniel Stone6847b852017-03-01 11:34:08 +00002961 /* We're not ready yet; come back to make a decision later. */
2962 if (output->repaint_status != REPAINT_SCHEDULED)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002963 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002964
2965 msec_to_repaint = timespec_sub_to_msec(&output->next_repaint, now);
2966 if (msec_to_repaint > 1)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002967 return ret;
Daniel Stone05df8c12017-03-03 16:59:42 +00002968
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002969 /* If we're sleeping, drop the repaint machinery entirely; we will
2970 * explicitly repaint all outputs when we come back. */
2971 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2972 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
2973 goto err;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002974
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002975 /* We don't actually need to repaint this output; drop it from
2976 * repaint until something causes damage. */
2977 if (!output->repaint_needed)
2978 goto err;
2979
2980 /* If repaint fails, we aren't going to get weston_output_finish_frame
2981 * to trigger a new repaint, so drop it from repaint and hope
Daniel Stone6847b852017-03-01 11:34:08 +00002982 * something schedules a successful repaint later. As repainting may
2983 * take some time, re-read our clock as a courtesy to the next
2984 * output. */
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002985 ret = weston_output_repaint(output, repaint_data);
Daniel Stone6847b852017-03-01 11:34:08 +00002986 weston_compositor_read_presentation_clock(compositor, now);
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002987 if (ret != 0)
2988 goto err;
2989
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09002990 output->repainted = true;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002991 return ret;
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002992
2993err:
Pekka Paalanen0513a952014-05-21 16:17:27 +03002994 weston_output_schedule_repaint_reset(output);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002995 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002996}
2997
2998static void
2999output_repaint_timer_arm(struct weston_compositor *compositor)
3000{
3001 struct weston_output *output;
3002 bool any_should_repaint = false;
3003 struct timespec now;
Sergi Granellb4c08862017-03-18 13:01:15 +01003004 int64_t msec_to_next = INT64_MAX;
Daniel Stone6847b852017-03-01 11:34:08 +00003005
3006 weston_compositor_read_presentation_clock(compositor, &now);
3007
3008 wl_list_for_each(output, &compositor->output_list, link) {
3009 int64_t msec_to_this;
3010
3011 if (output->repaint_status != REPAINT_SCHEDULED)
3012 continue;
3013
3014 msec_to_this = timespec_sub_to_msec(&output->next_repaint,
3015 &now);
3016 if (!any_should_repaint || msec_to_this < msec_to_next)
3017 msec_to_next = msec_to_this;
3018
3019 any_should_repaint = true;
3020 }
3021
3022 if (!any_should_repaint)
3023 return;
3024
3025 /* Even if we should repaint immediately, add the minimum 1 ms delay.
3026 * This is a workaround to allow coalescing multiple output repaints
3027 * particularly from weston_output_finish_frame()
3028 * into the same call, which would not happen if we called
3029 * output_repaint_timer_handler() directly.
3030 */
3031 if (msec_to_next < 1)
3032 msec_to_next = 1;
3033
3034 wl_event_source_timer_update(compositor->repaint_timer, msec_to_next);
3035}
3036
3037static int
3038output_repaint_timer_handler(void *data)
3039{
3040 struct weston_compositor *compositor = data;
3041 struct weston_output *output;
3042 struct timespec now;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003043 void *repaint_data = NULL;
Emre Ucane479ed82018-03-20 15:29:40 +01003044 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00003045
3046 weston_compositor_read_presentation_clock(compositor, &now);
Pekka Paalanen1ed2cad2021-02-10 12:33:03 +02003047 compositor->last_repaint_start = now;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003048
3049 if (compositor->backend->repaint_begin)
3050 repaint_data = compositor->backend->repaint_begin(compositor);
3051
3052 wl_list_for_each(output, &compositor->output_list, link) {
3053 ret = weston_output_maybe_repaint(output, &now, repaint_data);
3054 if (ret)
3055 break;
3056 }
3057
3058 if (ret == 0) {
Tomohito Esaki09bfcd62018-06-05 10:37:05 +09003059 if (compositor->backend->repaint_flush)
Antonio Borneoc90fccc2019-06-30 15:51:10 +02003060 ret = compositor->backend->repaint_flush(compositor,
3061 repaint_data);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003062 } else {
Antonio Borneoc90fccc2019-06-30 15:51:10 +02003063 if (compositor->backend->repaint_cancel)
3064 compositor->backend->repaint_cancel(compositor,
3065 repaint_data);
3066 }
3067
3068 if (ret != 0) {
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09003069 wl_list_for_each(output, &compositor->output_list, link) {
3070 if (output->repainted)
3071 weston_output_schedule_repaint_reset(output);
3072 }
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003073 }
Daniel Stone6847b852017-03-01 11:34:08 +00003074
Tomohito Esakiddaf95c2018-07-10 11:47:15 +09003075 wl_list_for_each(output, &compositor->output_list, link)
3076 output->repainted = false;
3077
Daniel Stone6847b852017-03-01 11:34:08 +00003078 output_repaint_timer_arm(compositor);
3079
Pekka Paalanen0513a952014-05-21 16:17:27 +03003080 return 0;
3081}
3082
Pekka Paalanen50aa3a72020-05-28 11:34:04 +03003083/** Convert a presentation timestamp to another clock domain
3084 *
3085 * \param compositor The compositor defines the presentation clock domain.
3086 * \param presentation_stamp The timestamp in presentation clock domain.
3087 * \param presentation_now Current time in presentation clock domain.
3088 * \param target_clock Defines the target clock domain.
3089 *
3090 * This approximation relies on presentation_stamp to be close to current time.
3091 * The further it is from current time and the bigger the speed difference
3092 * between the two clock domains, the bigger the conversion error.
3093 *
3094 * Conversion error due to system load is biased and unbounded.
3095 */
3096static struct timespec
3097convert_presentation_time_now(struct weston_compositor *compositor,
3098 const struct timespec *presentation_stamp,
3099 const struct timespec *presentation_now,
3100 clockid_t target_clock)
3101{
3102 struct timespec target_now = {};
3103 struct timespec target_stamp;
3104 int64_t delta_ns;
3105
3106 if (compositor->presentation_clock == target_clock)
3107 return *presentation_stamp;
3108
3109 clock_gettime(target_clock, &target_now);
3110 delta_ns = timespec_sub_to_nsec(presentation_stamp, presentation_now);
3111 timespec_add_nsec(&target_stamp, &target_now, delta_ns);
3112
3113 return target_stamp;
3114}
3115
Marius Vlad55d87362019-06-11 01:15:35 +03003116/**
3117 * \ingroup output
3118 */
Kristian Høgsbergef044142011-06-21 15:02:12 -04003119WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04003120weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02003121 const struct timespec *stamp,
3122 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04003123{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003124 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03003125 int32_t refresh_nsec;
3126 struct timespec now;
Pekka Paalanen50aa3a72020-05-28 11:34:04 +03003127 struct timespec vblank_monotonic;
Daniel Stone6847b852017-03-01 11:34:08 +00003128 int64_t msec_rel;
Pekka Paalanen133e4392014-09-23 22:08:46 -04003129
Daniel Stone05df8c12017-03-03 16:59:42 +00003130 assert(output->repaint_status == REPAINT_AWAITING_COMPLETION);
Pekka Paalanen98b4e202021-05-10 11:33:23 +03003131
3132 /*
3133 * If timestamp of latest vblank is given, it must always go forwards.
3134 * If not given, INVALID flag must be set.
3135 */
3136 if (stamp)
3137 assert(timespec_sub_to_nsec(stamp, &output->frame_time) >= 0);
3138 else
3139 assert(presented_flags & WP_PRESENTATION_FEEDBACK_INVALID);
Daniel Stone3615ce12017-03-01 11:34:05 +00003140
Daniel Stone6847b852017-03-01 11:34:08 +00003141 weston_compositor_read_presentation_clock(compositor, &now);
3142
Daniel Stone3615ce12017-03-01 11:34:05 +00003143 /* If we haven't been supplied any timestamp at all, we don't have a
3144 * timebase to work against, so any delay just wastes time. Push a
3145 * repaint as soon as possible so we can get on with it. */
Daniel Stone6847b852017-03-01 11:34:08 +00003146 if (!stamp) {
3147 output->next_repaint = now;
Daniel Stone3615ce12017-03-01 11:34:05 +00003148 goto out;
Daniel Stone6847b852017-03-01 11:34:08 +00003149 }
Daniel Stone3615ce12017-03-01 11:34:05 +00003150
Pekka Paalanen50aa3a72020-05-28 11:34:04 +03003151 vblank_monotonic = convert_presentation_time_now(compositor,
3152 stamp, &now,
3153 CLOCK_MONOTONIC);
Marius Vlad3203ff62019-09-05 14:56:12 +03003154 TL_POINT(compositor, "core_repaint_finished", TLP_OUTPUT(output),
Pekka Paalanen50aa3a72020-05-28 11:34:04 +03003155 TLP_VBLANK(&vblank_monotonic), TLP_END);
Marius Vladdf9278a2018-03-06 18:56:23 +02003156
Pekka Paalanend7894d02015-07-03 15:08:53 +03003157 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003158 weston_presentation_feedback_present_list(&output->feedback_list,
3159 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02003160 output->msc,
3161 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003162
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02003163 output->frame_time = *stamp;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003164
Daniel Stone6847b852017-03-01 11:34:08 +00003165 timespec_add_nsec(&output->next_repaint, stamp, refresh_nsec);
3166 timespec_add_msec(&output->next_repaint, &output->next_repaint,
3167 -compositor->repaint_msec);
3168 msec_rel = timespec_sub_to_msec(&output->next_repaint, &now);
Daniel Stone84aff5c2017-03-01 11:34:04 +00003169
3170 if (msec_rel < -1000 || msec_rel > 1000) {
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02003171 static bool warned;
3172
3173 if (!warned)
3174 weston_log("Warning: computed repaint delay is "
Daniel Stone6847b852017-03-01 11:34:08 +00003175 "insane: %lld msec\n", (long long) msec_rel);
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02003176 warned = true;
3177
Daniel Stone6847b852017-03-01 11:34:08 +00003178 output->next_repaint = now;
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02003179 }
3180
Mario Kleinerb7df04e2015-06-21 21:25:15 +02003181 /* Called from restart_repaint_loop and restart happens already after
3182 * the deadline given by repaint_msec? In that case we delay until
3183 * the deadline of the next frame, to give clients a more predictable
3184 * timing of the repaint cycle to lock on. */
Daniel Stoneeca5cca2017-02-28 21:53:51 +00003185 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID &&
3186 msec_rel < 0) {
3187 while (timespec_sub_to_nsec(&output->next_repaint, &now) < 0) {
3188 timespec_add_nsec(&output->next_repaint,
3189 &output->next_repaint,
3190 refresh_nsec);
3191 }
3192 }
Mario Kleinerb7df04e2015-06-21 21:25:15 +02003193
Daniel Stone3615ce12017-03-01 11:34:05 +00003194out:
Daniel Stone05df8c12017-03-03 16:59:42 +00003195 output->repaint_status = REPAINT_SCHEDULED;
Daniel Stone6847b852017-03-01 11:34:08 +00003196 output_repaint_timer_arm(compositor);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003197}
3198
3199static void
3200idle_repaint(void *data)
3201{
3202 struct weston_output *output = data;
Antonio Borneoc90fccc2019-06-30 15:51:10 +02003203 int ret;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003204
Daniel Stone05df8c12017-03-03 16:59:42 +00003205 assert(output->repaint_status == REPAINT_BEGIN_FROM_IDLE);
3206 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03003207 output->idle_repaint_source = NULL;
Antonio Borneoc90fccc2019-06-30 15:51:10 +02003208 ret = output->start_repaint_loop(output);
3209 if (ret != 0)
3210 weston_output_schedule_repaint_reset(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003211}
3212
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003213WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003214weston_layer_entry_insert(struct weston_layer_entry *list,
3215 struct weston_layer_entry *entry)
3216{
3217 wl_list_insert(&list->link, &entry->link);
3218 entry->layer = list->layer;
3219}
3220
3221WL_EXPORT void
3222weston_layer_entry_remove(struct weston_layer_entry *entry)
3223{
3224 wl_list_remove(&entry->link);
3225 wl_list_init(&entry->link);
3226 entry->layer = NULL;
3227}
3228
Quentin Glidic82681572016-12-17 13:40:51 +01003229
3230/** Initialize the weston_layer struct.
3231 *
3232 * \param compositor The compositor instance
3233 * \param layer The layer to initialize
3234 */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003235WL_EXPORT void
Quentin Glidic82681572016-12-17 13:40:51 +01003236weston_layer_init(struct weston_layer *layer,
3237 struct weston_compositor *compositor)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003238{
Quentin Glidic82681572016-12-17 13:40:51 +01003239 layer->compositor = compositor;
3240 wl_list_init(&layer->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003241 wl_list_init(&layer->view_list.link);
3242 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003243 weston_layer_set_mask_infinite(layer);
Quentin Glidic82681572016-12-17 13:40:51 +01003244}
3245
Pekka Paalanen87400372021-05-14 14:29:40 +03003246/** Finalize the weston_layer struct.
3247 *
3248 * \param layer The layer to finalize.
3249 */
3250WL_EXPORT void
3251weston_layer_fini(struct weston_layer *layer)
3252{
3253 wl_list_remove(&layer->link);
3254
3255 if (!wl_list_empty(&layer->view_list.link))
3256 weston_log("BUG: finalizing a layer with views still on it.\n");
3257
3258 wl_list_remove(&layer->view_list.link);
3259}
3260
Quentin Glidic82681572016-12-17 13:40:51 +01003261/** Sets the position of the layer in the layer list. The layer will be placed
3262 * below any layer with the same position value, if any.
3263 * This function is safe to call if the layer is already on the list, but the
3264 * layer may be moved below other layers at the same position, if any.
3265 *
3266 * \param layer The layer to modify
3267 * \param position The position the layer will be placed at
3268 */
3269WL_EXPORT void
3270weston_layer_set_position(struct weston_layer *layer,
3271 enum weston_layer_position position)
3272{
3273 struct weston_layer *below;
3274
3275 wl_list_remove(&layer->link);
3276
3277 /* layer_list is ordered from top to bottom, the last layer being the
3278 * background with the smallest position value */
3279
3280 layer->position = position;
3281 wl_list_for_each_reverse(below, &layer->compositor->layer_list, link) {
3282 if (below->position >= layer->position) {
3283 wl_list_insert(&below->link, &layer->link);
3284 return;
3285 }
3286 }
3287 wl_list_insert(&layer->compositor->layer_list, &layer->link);
3288}
3289
3290/** Hide a layer by taking it off the layer list.
3291 * This function is safe to call if the layer is not on the list.
3292 *
3293 * \param layer The layer to hide
3294 */
3295WL_EXPORT void
3296weston_layer_unset_position(struct weston_layer *layer)
3297{
3298 wl_list_remove(&layer->link);
3299 wl_list_init(&layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003300}
3301
3302WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003303weston_layer_set_mask(struct weston_layer *layer,
3304 int x, int y, int width, int height)
3305{
3306 struct weston_view *view;
3307
3308 layer->mask.x1 = x;
3309 layer->mask.x2 = x + width;
3310 layer->mask.y1 = y;
3311 layer->mask.y2 = y + height;
3312
3313 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
3314 weston_view_geometry_dirty(view);
3315 }
3316}
3317
3318WL_EXPORT void
3319weston_layer_set_mask_infinite(struct weston_layer *layer)
3320{
Adam Jackson3c3f3b12019-10-16 16:02:59 -04003321 struct weston_view *view;
3322
3323 layer->mask.x1 = INT32_MIN;
3324 layer->mask.x2 = INT32_MAX;
3325 layer->mask.y1 = INT32_MIN;
3326 layer->mask.y2 = INT32_MAX;
3327
3328 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
3329 weston_view_geometry_dirty(view);
3330 }
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003331}
3332
Daniel Stone3b775632018-07-20 08:38:25 +01003333WL_EXPORT bool
3334weston_layer_mask_is_infinite(struct weston_layer *layer)
3335{
3336 return layer->mask.x1 == INT32_MIN &&
3337 layer->mask.y1 == INT32_MIN &&
Adam Jackson3c3f3b12019-10-16 16:02:59 -04003338 layer->mask.x2 == INT32_MAX &&
3339 layer->mask.y2 == INT32_MAX;
Daniel Stone3b775632018-07-20 08:38:25 +01003340}
3341
Marius Vlad55d87362019-06-11 01:15:35 +03003342/**
3343 * \ingroup output
3344 */
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003345WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003346weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003347{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003348 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04003349 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01003350
Bryce Harrington08976ac2016-08-30 12:05:16 -07003351 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
3352 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003353 return;
3354
Pekka Paalanenb5026542014-11-12 15:09:24 +02003355 if (!output->repaint_needed)
Marius Vlad3203ff62019-09-05 14:56:12 +03003356 TL_POINT(compositor, "core_repaint_req", TLP_OUTPUT(output), TLP_END);
Pekka Paalanenb5026542014-11-12 15:09:24 +02003357
Kristian Høgsbergef044142011-06-21 15:02:12 -04003358 loop = wl_display_get_event_loop(compositor->wl_display);
Daniel Stone09a97e22017-03-01 11:34:06 +00003359 output->repaint_needed = true;
Daniel Stone05df8c12017-03-03 16:59:42 +00003360
3361 /* If we already have a repaint scheduled for our idle handler,
3362 * no need to set it again. If the repaint has been called but
3363 * not finished, then weston_output_finish_frame() will notice
3364 * that a repaint is needed and schedule one. */
3365 if (output->repaint_status != REPAINT_NOT_SCHEDULED)
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003366 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01003367
Daniel Stone05df8c12017-03-03 16:59:42 +00003368 output->repaint_status = REPAINT_BEGIN_FROM_IDLE;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03003369 assert(!output->idle_repaint_source);
3370 output->idle_repaint_source = wl_event_loop_add_idle(loop, idle_repaint,
3371 output);
Marius Vlad3203ff62019-09-05 14:56:12 +03003372 TL_POINT(compositor, "core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003373}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05003374
Marius Vlad9fdda7f2019-06-11 16:08:55 +03003375/** weston_compositor_schedule_repaint
3376 * \ingroup compositor
3377 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003378WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003379weston_compositor_schedule_repaint(struct weston_compositor *compositor)
3380{
3381 struct weston_output *output;
3382
3383 wl_list_for_each(output, &compositor->output_list, link)
3384 weston_output_schedule_repaint(output);
3385}
3386
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003387static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003388surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003389{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003390 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003391}
3392
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003393static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003394surface_attach(struct wl_client *client,
3395 struct wl_resource *resource,
3396 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
3397{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003398 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05003399 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003400
Kristian Høgsbergab19f932013-08-20 11:30:54 -07003401 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05003402 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07003403 if (buffer == NULL) {
3404 wl_client_post_no_memory(client);
3405 return;
3406 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07003407 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04003408
Pekka Paalanende685b82012-12-04 15:58:12 +02003409 /* Attach, attach, without commit in between does not send
3410 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003411 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003412
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003413 surface->pending.sx = sx;
3414 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01003415 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003416}
3417
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003418static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003419surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003420 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003421 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05003422{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003423 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003424
Derek Foreman57e92ed2015-11-17 14:11:35 -06003425 if (width <= 0 || height <= 0)
3426 return;
3427
Derek Foreman152254b2015-11-26 14:17:48 -06003428 pixman_region32_union_rect(&surface->pending.damage_surface,
3429 &surface->pending.damage_surface,
3430 x, y, width, height);
3431}
3432
3433static void
3434surface_damage_buffer(struct wl_client *client,
3435 struct wl_resource *resource,
3436 int32_t x, int32_t y, int32_t width, int32_t height)
3437{
3438 struct weston_surface *surface = wl_resource_get_user_data(resource);
3439
3440 if (width <= 0 || height <= 0)
3441 return;
3442
3443 pixman_region32_union_rect(&surface->pending.damage_buffer,
3444 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04003445 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003446}
3447
Kristian Høgsberg33418202011-08-16 23:01:28 -04003448static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003449destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04003450{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05003451 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003452
3453 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02003454 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003455}
3456
3457static void
3458surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003459 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04003460{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003461 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003462 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003463
3464 cb = malloc(sizeof *cb);
3465 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003466 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003467 return;
3468 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03003469
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003470 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
3471 callback);
3472 if (cb->resource == NULL) {
3473 free(cb);
3474 wl_resource_post_no_memory(resource);
3475 return;
3476 }
3477
Jason Ekstranda85118c2013-06-27 20:17:02 -05003478 wl_resource_set_implementation(cb->resource, NULL, cb,
3479 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003480
Pekka Paalanenbc106382012-10-10 12:49:31 +03003481 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003482}
3483
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003484static void
3485surface_set_opaque_region(struct wl_client *client,
3486 struct wl_resource *resource,
3487 struct wl_resource *region_resource)
3488{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003489 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003490 struct weston_region *region;
3491
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003492 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003493 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003494 pixman_region32_copy(&surface->pending.opaque,
3495 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003496 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07003497 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003498 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003499}
3500
3501static void
3502surface_set_input_region(struct wl_client *client,
3503 struct wl_resource *resource,
3504 struct wl_resource *region_resource)
3505{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003506 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003507 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003508
3509 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003510 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003511 pixman_region32_copy(&surface->pending.input,
3512 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003513 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003514 pixman_region32_fini(&surface->pending.input);
3515 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003516 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003517}
3518
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003519/* Cause damage to this sub-surface and all its children.
3520 *
3521 * This is useful when there are state changes that need an implicit
3522 * damage, e.g. a z-order change.
3523 */
3524static void
3525weston_surface_damage_subsurfaces(struct weston_subsurface *sub)
3526{
3527 struct weston_subsurface *child;
3528
3529 weston_surface_damage(sub->surface);
3530 sub->reordered = false;
3531
3532 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link)
3533 if (child != sub)
3534 weston_surface_damage_subsurfaces(child);
3535}
3536
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003537static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003538weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003539{
Pekka Paalanene67858b2013-04-25 13:57:42 +03003540 struct weston_subsurface *sub;
3541
3542 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
3543 parent_link_pending) {
3544 wl_list_remove(&sub->parent_link);
3545 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003546
3547 if (sub->reordered)
3548 weston_surface_damage_subsurfaces(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003549 }
3550}
3551
3552static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03003553weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05003554 struct weston_matrix *matrix)
3555{
Pekka Paalanen04baea52016-04-26 15:50:58 +03003556 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05003557 double src_width, src_height, dest_width, dest_height;
3558
3559 weston_matrix_init(matrix);
3560
3561 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
3562 src_width = surface->width_from_buffer;
3563 src_height = surface->height_from_buffer;
3564 } else {
3565 src_width = wl_fixed_to_double(vp->buffer.src_width);
3566 src_height = wl_fixed_to_double(vp->buffer.src_height);
3567 }
3568
3569 if (vp->surface.width == -1) {
3570 dest_width = src_width;
3571 dest_height = src_height;
3572 } else {
3573 dest_width = vp->surface.width;
3574 dest_height = vp->surface.height;
3575 }
3576
3577 if (src_width != dest_width || src_height != dest_height)
3578 weston_matrix_scale(matrix,
3579 src_width / dest_width,
3580 src_height / dest_height, 1);
3581
3582 if (vp->buffer.src_width != wl_fixed_from_int(-1))
3583 weston_matrix_translate(matrix,
3584 wl_fixed_to_double(vp->buffer.src_x),
3585 wl_fixed_to_double(vp->buffer.src_y),
3586 0);
3587
3588 switch (vp->buffer.transform) {
3589 case WL_OUTPUT_TRANSFORM_FLIPPED:
3590 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3591 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3592 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3593 weston_matrix_scale(matrix, -1, 1, 1);
3594 weston_matrix_translate(matrix,
3595 surface->width_from_buffer, 0, 0);
3596 break;
3597 }
3598
3599 switch (vp->buffer.transform) {
3600 default:
3601 case WL_OUTPUT_TRANSFORM_NORMAL:
3602 case WL_OUTPUT_TRANSFORM_FLIPPED:
3603 break;
3604 case WL_OUTPUT_TRANSFORM_90:
3605 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +02003606 weston_matrix_rotate_xy(matrix, 0, -1);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003607 weston_matrix_translate(matrix,
Pekka Paalanen8060d822020-02-06 15:27:54 +02003608 0, surface->width_from_buffer, 0);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003609 break;
3610 case WL_OUTPUT_TRANSFORM_180:
3611 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3612 weston_matrix_rotate_xy(matrix, -1, 0);
3613 weston_matrix_translate(matrix,
3614 surface->width_from_buffer,
3615 surface->height_from_buffer, 0);
3616 break;
3617 case WL_OUTPUT_TRANSFORM_270:
3618 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +02003619 weston_matrix_rotate_xy(matrix, 0, 1);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003620 weston_matrix_translate(matrix,
Pekka Paalanen8060d822020-02-06 15:27:54 +02003621 surface->height_from_buffer, 0, 0);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003622 break;
3623 }
3624
3625 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
3626}
3627
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003628/**
3629 * Compute a + b > c while being safe to overflows.
3630 */
3631static bool
3632fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
3633{
3634 return (int64_t)a + (int64_t)b > (int64_t)c;
3635}
3636
3637static bool
3638weston_surface_is_pending_viewport_source_valid(
3639 const struct weston_surface *surface)
3640{
3641 const struct weston_surface_state *pend = &surface->pending;
3642 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
3643 int width_from_buffer = 0;
3644 int height_from_buffer = 0;
3645 wl_fixed_t w;
3646 wl_fixed_t h;
3647
3648 /* If viewport source rect is not set, it is always ok. */
3649 if (vp->buffer.src_width == wl_fixed_from_int(-1))
3650 return true;
3651
3652 if (pend->newly_attached) {
3653 if (pend->buffer) {
3654 convert_size_by_transform_scale(&width_from_buffer,
3655 &height_from_buffer,
3656 pend->buffer->width,
3657 pend->buffer->height,
3658 vp->buffer.transform,
3659 vp->buffer.scale);
3660 } else {
3661 /* No buffer: viewport is irrelevant. */
3662 return true;
3663 }
3664 } else {
3665 width_from_buffer = surface->width_from_buffer;
3666 height_from_buffer = surface->height_from_buffer;
3667 }
3668
3669 assert((width_from_buffer == 0) == (height_from_buffer == 0));
3670 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
3671
3672 /* No buffer: viewport is irrelevant. */
3673 if (width_from_buffer == 0 || height_from_buffer == 0)
3674 return true;
3675
3676 /* overflow checks for wl_fixed_from_int() */
3677 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
3678 return false;
3679 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
3680 return false;
3681
3682 w = wl_fixed_from_int(width_from_buffer);
3683 h = wl_fixed_from_int(height_from_buffer);
3684
3685 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
3686 return false;
3687 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
3688 return false;
3689
3690 return true;
3691}
3692
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003693static bool
3694fixed_is_integer(wl_fixed_t v)
3695{
3696 return (v & 0xff) == 0;
3697}
3698
3699static bool
3700weston_surface_is_pending_viewport_dst_size_int(
3701 const struct weston_surface *surface)
3702{
3703 const struct weston_buffer_viewport *vp =
3704 &surface->pending.buffer_viewport;
3705
3706 if (vp->surface.width != -1) {
3707 assert(vp->surface.width > 0 && vp->surface.height > 0);
3708 return true;
3709 }
3710
3711 return fixed_is_integer(vp->buffer.src_width) &&
3712 fixed_is_integer(vp->buffer.src_height);
3713}
3714
Derek Foreman152254b2015-11-26 14:17:48 -06003715/* Translate pending damage in buffer co-ordinates to surface
3716 * co-ordinates and union it with a pixman_region32_t.
3717 * This should only be called after the buffer is attached.
3718 */
3719static void
3720apply_damage_buffer(pixman_region32_t *dest,
3721 struct weston_surface *surface,
3722 struct weston_surface_state *state)
3723{
3724 struct weston_buffer *buffer = surface->buffer_ref.buffer;
3725
3726 /* wl_surface.damage_buffer needs to be clipped to the buffer,
3727 * translated into surface co-ordinates and unioned with
3728 * any other surface damage.
3729 * None of this makes sense if there is no buffer though.
3730 */
3731 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
3732 pixman_region32_t buffer_damage;
3733
3734 pixman_region32_intersect_rect(&state->damage_buffer,
3735 &state->damage_buffer,
3736 0, 0, buffer->width,
3737 buffer->height);
3738 pixman_region32_init(&buffer_damage);
3739 weston_matrix_transform_region(&buffer_damage,
3740 &surface->buffer_to_surface_matrix,
3741 &state->damage_buffer);
3742 pixman_region32_union(dest, dest, &buffer_damage);
3743 pixman_region32_fini(&buffer_damage);
3744 }
3745 /* We should clear this on commit even if there was no buffer */
3746 pixman_region32_clear(&state->damage_buffer);
3747}
3748
Jason Ekstrand1e059042014-10-16 10:55:19 -05003749static void
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303750weston_surface_set_desired_protection(struct weston_surface *surface,
3751 enum weston_hdcp_protection protection)
3752{
3753 if (surface->desired_protection == protection)
3754 return;
3755 surface->desired_protection = protection;
3756 weston_surface_damage(surface);
3757}
3758
3759static void
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303760weston_surface_set_protection_mode(struct weston_surface *surface,
3761 enum weston_surface_protection_mode p_mode)
3762{
3763 struct content_protection *cp = surface->compositor->content_protection;
3764 struct protected_surface *psurface;
3765
3766 surface->protection_mode = p_mode;
3767 wl_list_for_each(psurface, &cp->protected_list, link) {
3768 if (!psurface || psurface->surface != surface)
3769 continue;
3770 weston_protected_surface_send_event(psurface,
3771 surface->current_protection);
3772 }
3773}
3774
3775static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05003776weston_surface_commit_state(struct weston_surface *surface,
3777 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003778{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003779 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003780 pixman_region32_t opaque;
3781
Alexander Larsson4ea95522013-05-22 14:41:37 +02003782 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02003783 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03003784 /* wp_viewport.set_source */
3785 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003786 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003787
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003788 /* wl_surface.attach */
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003789 if (state->newly_attached) {
3790 /* zwp_surface_synchronization_v1.set_acquire_fence */
3791 fd_move(&surface->acquire_fence_fd,
3792 &state->acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003793 /* zwp_surface_synchronization_v1.get_release */
3794 weston_buffer_release_move(&surface->buffer_release_ref,
3795 &state->buffer_release_ref);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003796 weston_surface_attach(surface, state->buffer);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003797 }
Jason Ekstrand7b982072014-05-20 14:33:03 -05003798 weston_surface_state_set_buffer(state, NULL);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003799 assert(state->acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003800 assert(state->buffer_release_ref.buffer_release == NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01003801
Jason Ekstrand1e059042014-10-16 10:55:19 -05003802 weston_surface_build_buffer_matrix(surface,
3803 &surface->surface_to_buffer_matrix);
3804 weston_matrix_invert(&surface->buffer_to_surface_matrix,
3805 &surface->surface_to_buffer_matrix);
3806
Jason Ekstrand7b982072014-05-20 14:33:03 -05003807 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003808 weston_surface_update_size(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003809 if (surface->committed)
3810 surface->committed(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003811 }
Giulio Camuffo184df502013-02-21 11:29:21 +01003812
Jason Ekstrand7b982072014-05-20 14:33:03 -05003813 state->sx = 0;
3814 state->sy = 0;
3815 state->newly_attached = 0;
3816 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03003817
Derek Foreman152254b2015-11-26 14:17:48 -06003818 /* wl_surface.damage and wl_surface.damage_buffer */
Marius Vlad2a1b7862019-09-05 13:12:18 +03003819 if (pixman_region32_not_empty(&state->damage_surface) ||
3820 pixman_region32_not_empty(&state->damage_buffer))
Marius Vlad3203ff62019-09-05 14:56:12 +03003821 TL_POINT(surface->compositor, "core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06003822
Pekka Paalanen8e159182012-10-10 12:49:25 +03003823 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06003824 &state->damage_surface);
3825
3826 apply_damage_buffer(&surface->damage, surface, state);
3827
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05003828 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07003829 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06003830 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003831
3832 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003833 pixman_region32_init(&opaque);
3834 pixman_region32_intersect_rect(&opaque, &state->opaque,
3835 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003836
3837 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
3838 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003839 wl_list_for_each(view, &surface->views, surface_link)
3840 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003841 }
3842
3843 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003844
3845 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003846 pixman_region32_intersect_rect(&surface->input, &state->input,
3847 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003848
Pekka Paalanenbc106382012-10-10 12:49:31 +03003849 /* wl_surface.frame */
3850 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05003851 &state->frame_callback_list);
3852 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003853
3854 /* XXX:
3855 * What should happen with a feedback request, if there
3856 * is no wl_buffer attached for this commit?
3857 */
3858
3859 /* presentation.feedback */
3860 wl_list_insert_list(&surface->feedback_list,
3861 &state->feedback_list);
3862 wl_list_init(&state->feedback_list);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003863
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303864 /* weston_protected_surface.enforced/relaxed */
3865 if (surface->protection_mode != state->protection_mode)
3866 weston_surface_set_protection_mode(surface,
3867 state->protection_mode);
3868
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303869 /* weston_protected_surface.set_type */
3870 weston_surface_set_desired_protection(surface, state->desired_protection);
3871
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003872 wl_signal_emit(&surface->commit_signal, surface);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003873}
3874
3875static void
3876weston_surface_commit(struct weston_surface *surface)
3877{
3878 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003879
Pekka Paalanene67858b2013-04-25 13:57:42 +03003880 weston_surface_commit_subsurface_order(surface);
3881
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003882 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003883}
3884
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003885static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003886weston_subsurface_commit(struct weston_subsurface *sub);
3887
3888static void
3889weston_subsurface_parent_commit(struct weston_subsurface *sub,
3890 int parent_is_synchronized);
3891
3892static void
3893surface_commit(struct wl_client *client, struct wl_resource *resource)
3894{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003895 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003896 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
3897
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003898 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
3899 assert(surface->viewport_resource);
3900
3901 wl_resource_post_error(surface->viewport_resource,
3902 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
3903 "wl_surface@%d has viewport source outside buffer",
3904 wl_resource_get_id(resource));
3905 return;
3906 }
3907
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003908 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
3909 assert(surface->viewport_resource);
3910
3911 wl_resource_post_error(surface->viewport_resource,
3912 WP_VIEWPORT_ERROR_BAD_SIZE,
3913 "wl_surface@%d viewport dst size not integer",
3914 wl_resource_get_id(resource));
3915 return;
3916 }
3917
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003918 if (surface->pending.acquire_fence_fd >= 0) {
3919 assert(surface->synchronization_resource);
3920
3921 if (!surface->pending.buffer) {
3922 fd_clear(&surface->pending.acquire_fence_fd);
3923 wl_resource_post_error(surface->synchronization_resource,
3924 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3925 "wl_surface@%"PRIu32" no buffer for synchronization",
3926 wl_resource_get_id(resource));
3927 return;
3928 }
3929
3930 /* We support fences for both wp_linux_dmabuf and opaque EGL
3931 * buffers, as mandated by minor version 2 of the
3932 * zwp_linux_explicit_synchronization_v1 protocol. Since
3933 * renderers that support fences currently only support these
3934 * two buffer types plus SHM buffers, we can just check for the
3935 * SHM buffer case here.
3936 */
3937 if (wl_shm_buffer_get(surface->pending.buffer->resource)) {
3938 fd_clear(&surface->pending.acquire_fence_fd);
3939 wl_resource_post_error(surface->synchronization_resource,
3940 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_UNSUPPORTED_BUFFER,
3941 "wl_surface@%"PRIu32" unsupported buffer for synchronization",
3942 wl_resource_get_id(resource));
3943 return;
3944 }
3945 }
3946
Alexandros Frantzis67629672018-10-19 12:14:11 +03003947 if (surface->pending.buffer_release_ref.buffer_release &&
3948 !surface->pending.buffer) {
3949 assert(surface->synchronization_resource);
3950
3951 wl_resource_post_error(surface->synchronization_resource,
3952 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3953 "wl_surface@%"PRIu32" no buffer for synchronization",
3954 wl_resource_get_id(resource));
3955 return;
3956 }
3957
Pekka Paalanene67858b2013-04-25 13:57:42 +03003958 if (sub) {
3959 weston_subsurface_commit(sub);
3960 return;
3961 }
3962
3963 weston_surface_commit(surface);
3964
3965 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
3966 if (sub->surface != surface)
3967 weston_subsurface_parent_commit(sub, 0);
3968 }
3969}
3970
3971static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003972surface_set_buffer_transform(struct wl_client *client,
3973 struct wl_resource *resource, int transform)
3974{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003975 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003976
Jonny Lamba55f1392014-05-30 12:07:15 +02003977 /* if wl_output.transform grows more members this will need to be updated. */
3978 if (transform < 0 ||
3979 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
3980 wl_resource_post_error(resource,
3981 WL_SURFACE_ERROR_INVALID_TRANSFORM,
3982 "buffer transform must be a valid transform "
3983 "('%d' specified)", transform);
3984 return;
3985 }
3986
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003987 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003988 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003989}
3990
Alexander Larsson4ea95522013-05-22 14:41:37 +02003991static void
3992surface_set_buffer_scale(struct wl_client *client,
3993 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003994 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003995{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003996 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003997
Jonny Lamba55f1392014-05-30 12:07:15 +02003998 if (scale < 1) {
3999 wl_resource_post_error(resource,
4000 WL_SURFACE_ERROR_INVALID_SCALE,
4001 "buffer scale must be at least one "
4002 "('%d' specified)", scale);
4003 return;
4004 }
4005
Pekka Paalanen952b6c82014-03-14 14:38:15 +02004006 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004007 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02004008}
4009
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04004010static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004011 surface_destroy,
4012 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04004013 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004014 surface_frame,
4015 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03004016 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004017 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02004018 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06004019 surface_set_buffer_scale,
4020 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004021};
4022
4023static void
4024compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04004025 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004026{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04004027 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004028 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004029
Kristian Høgsberg18c93002012-01-27 11:58:31 -05004030 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04004031 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04004032 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004033 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04004034 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004035
Jason Ekstranda85118c2013-06-27 20:17:02 -05004036 surface->resource =
4037 wl_resource_create(client, &wl_surface_interface,
4038 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004039 if (surface->resource == NULL) {
4040 weston_surface_destroy(surface);
4041 wl_resource_post_no_memory(resource);
4042 return;
4043 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05004044 wl_resource_set_implementation(surface->resource, &surface_interface,
4045 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07004046
4047 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004048}
4049
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004050static void
4051destroy_region(struct wl_resource *resource)
4052{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05004053 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004054
4055 pixman_region32_fini(&region->region);
4056 free(region);
4057}
4058
4059static void
4060region_destroy(struct wl_client *client, struct wl_resource *resource)
4061{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004062 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004063}
4064
4065static void
4066region_add(struct wl_client *client, struct wl_resource *resource,
4067 int32_t x, int32_t y, int32_t width, int32_t height)
4068{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05004069 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004070
4071 pixman_region32_union_rect(&region->region, &region->region,
4072 x, y, width, height);
4073}
4074
4075static void
4076region_subtract(struct wl_client *client, struct wl_resource *resource,
4077 int32_t x, int32_t y, int32_t width, int32_t height)
4078{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05004079 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004080 pixman_region32_t rect;
4081
4082 pixman_region32_init_rect(&rect, x, y, width, height);
4083 pixman_region32_subtract(&region->region, &region->region, &rect);
4084 pixman_region32_fini(&rect);
4085}
4086
4087static const struct wl_region_interface region_interface = {
4088 region_destroy,
4089 region_add,
4090 region_subtract
4091};
4092
4093static void
4094compositor_create_region(struct wl_client *client,
4095 struct wl_resource *resource, uint32_t id)
4096{
4097 struct weston_region *region;
4098
4099 region = malloc(sizeof *region);
4100 if (region == NULL) {
4101 wl_resource_post_no_memory(resource);
4102 return;
4103 }
4104
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004105 pixman_region32_init(&region->region);
4106
Jason Ekstranda85118c2013-06-27 20:17:02 -05004107 region->resource =
4108 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004109 if (region->resource == NULL) {
4110 free(region);
4111 wl_resource_post_no_memory(resource);
4112 return;
4113 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05004114 wl_resource_set_implementation(region->resource, &region_interface,
4115 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004116}
4117
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04004118static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004119 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004120 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004121};
4122
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004123static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03004124weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
4125{
4126 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004127
Jason Ekstrand7b982072014-05-20 14:33:03 -05004128 weston_surface_commit_state(surface, &sub->cached);
4129 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004130
4131 weston_surface_commit_subsurface_order(surface);
4132
4133 weston_surface_schedule_repaint(surface);
4134
Jason Ekstrand7b982072014-05-20 14:33:03 -05004135 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004136}
4137
4138static void
4139weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
4140{
4141 struct weston_surface *surface = sub->surface;
4142
4143 /*
4144 * If this commit would cause the surface to move by the
4145 * attach(dx, dy) parameters, the old damage region must be
4146 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05004147 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03004148 */
Derek Foreman152254b2015-11-26 14:17:48 -06004149 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004150 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06004151 pixman_region32_union(&sub->cached.damage_surface,
4152 &sub->cached.damage_surface,
4153 &surface->pending.damage_surface);
4154 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004155
4156 if (surface->pending.newly_attached) {
4157 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05004158 weston_surface_state_set_buffer(&sub->cached,
4159 surface->pending.buffer);
4160 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004161 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04004162 weston_presentation_feedback_discard_list(
4163 &sub->cached.feedback_list);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03004164 /* zwp_surface_synchronization_v1.set_acquire_fence */
4165 fd_move(&sub->cached.acquire_fence_fd,
4166 &surface->pending.acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03004167 /* zwp_surface_synchronization_v1.get_release */
4168 weston_buffer_release_move(&sub->cached.buffer_release_ref,
4169 &surface->pending.buffer_release_ref);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004170 }
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05304171 sub->cached.desired_protection = surface->pending.desired_protection;
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05304172 sub->cached.protection_mode = surface->pending.protection_mode;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03004173 assert(surface->pending.acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03004174 assert(surface->pending.buffer_release_ref.buffer_release == NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004175 sub->cached.sx += surface->pending.sx;
4176 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02004177
Derek Foreman152254b2015-11-26 14:17:48 -06004178 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
4179
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004180 sub->cached.buffer_viewport.changed |=
4181 surface->pending.buffer_viewport.changed;
4182 sub->cached.buffer_viewport.buffer =
4183 surface->pending.buffer_viewport.buffer;
4184 sub->cached.buffer_viewport.surface =
4185 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004186
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004187 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004188
4189 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
4190
4191 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
4192
4193 wl_list_insert_list(&sub->cached.frame_callback_list,
4194 &surface->pending.frame_callback_list);
4195 wl_list_init(&surface->pending.frame_callback_list);
4196
Pekka Paalanen133e4392014-09-23 22:08:46 -04004197 wl_list_insert_list(&sub->cached.feedback_list,
4198 &surface->pending.feedback_list);
4199 wl_list_init(&surface->pending.feedback_list);
4200
Jason Ekstrand7b982072014-05-20 14:33:03 -05004201 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004202}
4203
Derek Foreman280e7dd2014-10-03 13:13:42 -05004204static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03004205weston_subsurface_is_synchronized(struct weston_subsurface *sub)
4206{
4207 while (sub) {
4208 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05004209 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004210
4211 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05004212 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004213
4214 sub = weston_surface_to_subsurface(sub->parent);
4215 }
4216
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01004217 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004218}
4219
4220static void
4221weston_subsurface_commit(struct weston_subsurface *sub)
4222{
4223 struct weston_surface *surface = sub->surface;
4224 struct weston_subsurface *tmp;
4225
4226 /* Recursive check for effectively synchronized. */
4227 if (weston_subsurface_is_synchronized(sub)) {
4228 weston_subsurface_commit_to_cache(sub);
4229 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05004230 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03004231 /* flush accumulated state from cache */
4232 weston_subsurface_commit_to_cache(sub);
4233 weston_subsurface_commit_from_cache(sub);
4234 } else {
4235 weston_surface_commit(surface);
4236 }
4237
4238 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
4239 if (tmp->surface != surface)
4240 weston_subsurface_parent_commit(tmp, 0);
4241 }
4242 }
4243}
4244
4245static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004246weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004247{
4248 struct weston_surface *surface = sub->surface;
4249 struct weston_subsurface *tmp;
4250
Pekka Paalanene67858b2013-04-25 13:57:42 +03004251 /* From now on, commit_from_cache the whole sub-tree, regardless of
4252 * the synchronized mode of each child. This sub-surface or some
4253 * of its ancestors were synchronized, so we are synchronized
4254 * all the way down.
4255 */
4256
Jason Ekstrand7b982072014-05-20 14:33:03 -05004257 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004258 weston_subsurface_commit_from_cache(sub);
4259
4260 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
4261 if (tmp->surface != surface)
4262 weston_subsurface_parent_commit(tmp, 1);
4263 }
4264}
4265
4266static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004267weston_subsurface_parent_commit(struct weston_subsurface *sub,
4268 int parent_is_synchronized)
4269{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004270 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004271 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004272 wl_list_for_each(view, &sub->surface->views, surface_link)
4273 weston_view_set_position(view,
4274 sub->position.x,
4275 sub->position.y);
4276
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004277 sub->position.set = 0;
4278 }
4279
4280 if (parent_is_synchronized || sub->synchronized)
4281 weston_subsurface_synchronized_commit(sub);
4282}
4283
Pekka Paalanen8274d902014-08-06 19:36:51 +03004284static int
4285subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
4286{
4287 return snprintf(buf, len, "sub-surface");
4288}
4289
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004290static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004291subsurface_committed(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004292{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004293 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004294
Jason Ekstranda7af7042013-10-12 22:38:11 -05004295 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004296 weston_view_set_position(view,
4297 view->geometry.x + dx,
4298 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004299
4300 /* No need to check parent mappedness, because if parent is not
4301 * mapped, parent is not in a visible layer, so this sub-surface
4302 * will not be drawn either.
4303 */
Armin Krezovićf8486c32016-06-30 06:04:28 +02004304
Pekka Paalanene67858b2013-04-25 13:57:42 +03004305 if (!weston_surface_is_mapped(surface)) {
Armin Krezovićf8486c32016-06-30 06:04:28 +02004306 surface->is_mapped = true;
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03004307
Derek Foreman4b1a0a12014-09-10 15:37:33 -05004308 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03004309 * because that would call it also for the parent surface,
4310 * which might not be mapped yet. That would lead to
4311 * inconsistent state, where the window could never be
4312 * mapped.
4313 *
Armin Krezovićf8486c32016-06-30 06:04:28 +02004314 * Instead just force the is_mapped flag on, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03004315 * weston_surface_is_mapped() return true, so that when the
4316 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03004317 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03004318 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004319 }
4320}
4321
4322static struct weston_subsurface *
4323weston_surface_to_subsurface(struct weston_surface *surface)
4324{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004325 if (surface->committed == subsurface_committed)
4326 return surface->committed_private;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004327
4328 return NULL;
4329}
4330
Pekka Paalanen01388e22013-04-25 13:57:44 +03004331WL_EXPORT struct weston_surface *
4332weston_surface_get_main_surface(struct weston_surface *surface)
4333{
4334 struct weston_subsurface *sub;
4335
4336 while (surface && (sub = weston_surface_to_subsurface(surface)))
4337 surface = sub->parent;
4338
4339 return surface;
4340}
4341
Pekka Paalanen50b67472014-10-01 15:02:41 +03004342WL_EXPORT int
4343weston_surface_set_role(struct weston_surface *surface,
4344 const char *role_name,
4345 struct wl_resource *error_resource,
4346 uint32_t error_code)
4347{
4348 assert(role_name);
4349
4350 if (surface->role_name == NULL ||
4351 surface->role_name == role_name ||
4352 strcmp(surface->role_name, role_name) == 0) {
4353 surface->role_name = role_name;
4354
4355 return 0;
4356 }
4357
4358 wl_resource_post_error(error_resource, error_code,
4359 "Cannot assign role %s to wl_surface@%d,"
4360 " already has role %s\n",
4361 role_name,
4362 wl_resource_get_id(surface->resource),
4363 surface->role_name);
4364 return -1;
4365}
4366
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02004367WL_EXPORT const char *
4368weston_surface_get_role(struct weston_surface *surface)
4369{
4370 return surface->role_name;
4371}
4372
Pekka Paalanen8274d902014-08-06 19:36:51 +03004373WL_EXPORT void
4374weston_surface_set_label_func(struct weston_surface *surface,
4375 int (*desc)(struct weston_surface *,
4376 char *, size_t))
4377{
4378 surface->get_label = desc;
Marius Vlad5de92972019-10-15 13:25:41 +03004379 weston_timeline_refresh_subscription_objects(surface->compositor,
4380 surface);
Pekka Paalanen8274d902014-08-06 19:36:51 +03004381}
4382
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004383/** Get the size of surface contents
4384 *
4385 * \param surface The surface to query.
4386 * \param width Returns the width of raw contents.
4387 * \param height Returns the height of raw contents.
4388 *
4389 * Retrieves the raw surface content size in pixels for the given surface.
4390 * This is the whole content size in buffer pixels. If the surface
4391 * has no content or the renderer does not implement this feature,
4392 * zeroes are returned.
4393 *
4394 * This function is used to determine the buffer size needed for
4395 * a weston_surface_copy_content() call.
4396 */
4397WL_EXPORT void
4398weston_surface_get_content_size(struct weston_surface *surface,
4399 int *width, int *height)
4400{
4401 struct weston_renderer *rer = surface->compositor->renderer;
4402
4403 if (!rer->surface_get_content_size) {
4404 *width = 0;
4405 *height = 0;
4406 return;
4407 }
4408
4409 rer->surface_get_content_size(surface, width, height);
4410}
4411
Quentin Glidic248dd102016-08-12 10:41:34 +02004412/** Get the bounding box of a surface and its subsurfaces
4413 *
4414 * \param surface The surface to query.
4415 * \return The bounding box relative to the surface origin.
4416 *
4417 */
4418WL_EXPORT struct weston_geometry
4419weston_surface_get_bounding_box(struct weston_surface *surface)
4420{
4421 pixman_region32_t region;
4422 pixman_box32_t *box;
4423 struct weston_subsurface *subsurface;
4424
4425 pixman_region32_init_rect(&region,
4426 0, 0,
4427 surface->width, surface->height);
4428
4429 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link)
4430 pixman_region32_union_rect(&region, &region,
4431 subsurface->position.x,
4432 subsurface->position.y,
4433 subsurface->surface->width,
4434 subsurface->surface->height);
4435
4436 box = pixman_region32_extents(&region);
4437 struct weston_geometry geometry = {
4438 .x = box->x1,
4439 .y = box->y1,
4440 .width = box->x2 - box->x1,
4441 .height = box->y2 - box->y1,
4442 };
4443
4444 pixman_region32_fini(&region);
4445
4446 return geometry;
4447}
4448
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004449/** Copy surface contents to system memory.
4450 *
4451 * \param surface The surface to copy from.
4452 * \param target Pointer to the target memory buffer.
4453 * \param size Size of the target buffer in bytes.
4454 * \param src_x X location on contents to copy from.
4455 * \param src_y Y location on contents to copy from.
4456 * \param width Width in pixels of the area to copy.
4457 * \param height Height in pixels of the area to copy.
4458 * \return 0 for success, -1 for failure.
4459 *
4460 * Surface contents are maintained by the renderer. They can be in a
4461 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
4462 * else.
4463 *
4464 * Surface contents are copied into memory pointed to by target,
4465 * which has size bytes of space available. The target memory
4466 * may be larger than needed, but being smaller returns an error.
4467 * The extra bytes in target may or may not be written; their content is
4468 * unspecified. Size must be large enough to hold the image.
4469 *
4470 * The image in the target memory will be arranged in rows from
4471 * top to bottom, and pixels on a row from left to right. The pixel
4472 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
4473 * width * 4.
4474 *
4475 * Parameters src_x and src_y define the upper-left corner in buffer
4476 * coordinates (pixels) to copy from. Parameters width and height
4477 * define the size of the area to copy in pixels.
4478 *
4479 * The rectangle defined by src_x, src_y, width, height must fit in
4480 * the surface contents. Otherwise an error is returned.
4481 *
Changwoo Chof97d2502017-08-05 00:30:47 +09004482 * Use weston_surface_get_content_size to determine the content size; the
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004483 * needed target buffer size and rectangle limits.
4484 *
4485 * CURRENT IMPLEMENTATION RESTRICTIONS:
4486 * - the machine must be little-endian due to Pixman formats.
4487 *
4488 * NOTE: Pixman formats are premultiplied.
4489 */
4490WL_EXPORT int
4491weston_surface_copy_content(struct weston_surface *surface,
4492 void *target, size_t size,
4493 int src_x, int src_y,
4494 int width, int height)
4495{
4496 struct weston_renderer *rer = surface->compositor->renderer;
4497 int cw, ch;
4498 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
4499
4500 if (!rer->surface_copy_content)
4501 return -1;
4502
4503 weston_surface_get_content_size(surface, &cw, &ch);
4504
4505 if (src_x < 0 || src_y < 0)
4506 return -1;
4507
4508 if (width <= 0 || height <= 0)
4509 return -1;
4510
4511 if (src_x + width > cw || src_y + height > ch)
4512 return -1;
4513
4514 if (width * bytespp * height > size)
4515 return -1;
4516
4517 return rer->surface_copy_content(surface, target, size,
4518 src_x, src_y, width, height);
4519}
4520
Pekka Paalanene67858b2013-04-25 13:57:42 +03004521static void
4522subsurface_set_position(struct wl_client *client,
4523 struct wl_resource *resource, int32_t x, int32_t y)
4524{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004525 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004526
4527 if (!sub)
4528 return;
4529
4530 sub->position.x = x;
4531 sub->position.y = y;
4532 sub->position.set = 1;
4533}
4534
4535static struct weston_subsurface *
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004536subsurface_find_sibling(struct weston_subsurface *sub,
4537 struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004538{
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004539 struct weston_surface *parent = sub->parent;
4540 struct weston_subsurface *sibling;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004541
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004542 wl_list_for_each(sibling, &parent->subsurface_list, parent_link) {
4543 if (sibling->surface == surface && sibling != sub)
4544 return sibling;
4545 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004546
4547 return NULL;
4548}
4549
4550static struct weston_subsurface *
4551subsurface_sibling_check(struct weston_subsurface *sub,
4552 struct weston_surface *surface,
4553 const char *request)
4554{
4555 struct weston_subsurface *sibling;
4556
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004557 sibling = subsurface_find_sibling(sub, surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004558 if (!sibling) {
4559 wl_resource_post_error(sub->resource,
4560 WL_SUBSURFACE_ERROR_BAD_SURFACE,
4561 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004562 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004563 return NULL;
4564 }
4565
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004566 assert(sibling->parent == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004567
4568 return sibling;
4569}
4570
4571static void
4572subsurface_place_above(struct wl_client *client,
4573 struct wl_resource *resource,
4574 struct wl_resource *sibling_resource)
4575{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004576 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004577 struct weston_surface *surface =
4578 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004579 struct weston_subsurface *sibling;
4580
4581 if (!sub)
4582 return;
4583
4584 sibling = subsurface_sibling_check(sub, surface, "place_above");
4585 if (!sibling)
4586 return;
4587
4588 wl_list_remove(&sub->parent_link_pending);
4589 wl_list_insert(sibling->parent_link_pending.prev,
4590 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004591
4592 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004593}
4594
4595static void
4596subsurface_place_below(struct wl_client *client,
4597 struct wl_resource *resource,
4598 struct wl_resource *sibling_resource)
4599{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004600 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004601 struct weston_surface *surface =
4602 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004603 struct weston_subsurface *sibling;
4604
4605 if (!sub)
4606 return;
4607
4608 sibling = subsurface_sibling_check(sub, surface, "place_below");
4609 if (!sibling)
4610 return;
4611
4612 wl_list_remove(&sub->parent_link_pending);
4613 wl_list_insert(&sibling->parent_link_pending,
4614 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004615
4616 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004617}
4618
4619static void
4620subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
4621{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004622 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004623
4624 if (sub)
4625 sub->synchronized = 1;
4626}
4627
4628static void
4629subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
4630{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004631 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004632
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004633 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03004634 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004635
4636 /* If sub became effectively desynchronized, flush. */
4637 if (!weston_subsurface_is_synchronized(sub))
4638 weston_subsurface_synchronized_commit(sub);
4639 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004640}
4641
4642static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03004643weston_subsurface_unlink_parent(struct weston_subsurface *sub)
4644{
4645 wl_list_remove(&sub->parent_link);
4646 wl_list_remove(&sub->parent_link_pending);
4647 wl_list_remove(&sub->parent_destroy_listener.link);
4648 sub->parent = NULL;
4649}
4650
4651static void
4652weston_subsurface_destroy(struct weston_subsurface *sub);
4653
4654static void
4655subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
4656{
4657 struct weston_subsurface *sub =
4658 container_of(listener, struct weston_subsurface,
4659 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004660 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004661
4662 /* The protocol object (wl_resource) is left inert. */
4663 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004664 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004665
4666 weston_subsurface_destroy(sub);
4667}
4668
4669static void
4670subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
4671{
4672 struct weston_subsurface *sub =
4673 container_of(listener, struct weston_subsurface,
4674 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004675 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004676 assert(sub->surface != sub->parent);
4677
4678 if (weston_surface_is_mapped(sub->surface))
4679 weston_surface_unmap(sub->surface);
4680
4681 weston_subsurface_unlink_parent(sub);
4682}
4683
4684static void
4685subsurface_resource_destroy(struct wl_resource *resource)
4686{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004687 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004688
4689 if (sub)
4690 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004691}
4692
4693static void
4694subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
4695{
4696 wl_resource_destroy(resource);
4697}
4698
4699static void
4700weston_subsurface_link_parent(struct weston_subsurface *sub,
4701 struct weston_surface *parent)
4702{
4703 sub->parent = parent;
4704 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004705 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004706 &sub->parent_destroy_listener);
4707
4708 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4709 wl_list_insert(&parent->subsurface_list_pending,
4710 &sub->parent_link_pending);
4711}
4712
4713static void
4714weston_subsurface_link_surface(struct weston_subsurface *sub,
4715 struct weston_surface *surface)
4716{
4717 sub->surface = surface;
4718 sub->surface_destroy_listener.notify =
4719 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004720 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004721 &sub->surface_destroy_listener);
4722}
4723
4724static void
4725weston_subsurface_destroy(struct weston_subsurface *sub)
4726{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004727 struct weston_view *view, *next;
4728
Pekka Paalanene67858b2013-04-25 13:57:42 +03004729 assert(sub->surface);
4730
4731 if (sub->resource) {
4732 assert(weston_surface_to_subsurface(sub->surface) == sub);
4733 assert(sub->parent_destroy_listener.notify ==
4734 subsurface_handle_parent_destroy);
4735
George Kiagiadakised04d382014-06-13 18:10:26 +02004736 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
4737 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004738 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02004739 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05004740
Pekka Paalanene67858b2013-04-25 13:57:42 +03004741 if (sub->parent)
4742 weston_subsurface_unlink_parent(sub);
4743
Jason Ekstrand7b982072014-05-20 14:33:03 -05004744 weston_surface_state_fini(&sub->cached);
4745 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004746
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004747 sub->surface->committed = NULL;
4748 sub->surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004749 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004750 } else {
4751 /* the dummy weston_subsurface for the parent itself */
4752 assert(sub->parent_destroy_listener.notify == NULL);
4753 wl_list_remove(&sub->parent_link);
4754 wl_list_remove(&sub->parent_link_pending);
4755 }
4756
4757 wl_list_remove(&sub->surface_destroy_listener.link);
4758 free(sub);
4759}
4760
4761static const struct wl_subsurface_interface subsurface_implementation = {
4762 subsurface_destroy,
4763 subsurface_set_position,
4764 subsurface_place_above,
4765 subsurface_place_below,
4766 subsurface_set_sync,
4767 subsurface_set_desync
4768};
4769
4770static struct weston_subsurface *
4771weston_subsurface_create(uint32_t id, struct weston_surface *surface,
4772 struct weston_surface *parent)
4773{
4774 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004775 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004776
Bryce Harringtonde16d892014-11-20 22:21:57 -08004777 sub = zalloc(sizeof *sub);
4778 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004779 return NULL;
4780
Jason Ekstranda7af7042013-10-12 22:38:11 -05004781 wl_list_init(&sub->unused_views);
4782
Jason Ekstranda85118c2013-06-27 20:17:02 -05004783 sub->resource =
4784 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004785 if (!sub->resource) {
4786 free(sub);
4787 return NULL;
4788 }
4789
Jason Ekstranda85118c2013-06-27 20:17:02 -05004790 wl_resource_set_implementation(sub->resource,
4791 &subsurface_implementation,
4792 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004793 weston_subsurface_link_surface(sub, surface);
4794 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05004795 weston_surface_state_init(&sub->cached);
4796 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004797 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004798
4799 return sub;
4800}
4801
4802/* Create a dummy subsurface for having the parent itself in its
4803 * sub-surface lists. Makes stacking order manipulation easy.
4804 */
4805static struct weston_subsurface *
4806weston_subsurface_create_for_parent(struct weston_surface *parent)
4807{
4808 struct weston_subsurface *sub;
4809
Bryce Harringtonde16d892014-11-20 22:21:57 -08004810 sub = zalloc(sizeof *sub);
4811 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004812 return NULL;
4813
4814 weston_subsurface_link_surface(sub, parent);
4815 sub->parent = parent;
4816 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4817 wl_list_insert(&parent->subsurface_list_pending,
4818 &sub->parent_link_pending);
4819
4820 return sub;
4821}
4822
4823static void
4824subcompositor_get_subsurface(struct wl_client *client,
4825 struct wl_resource *resource,
4826 uint32_t id,
4827 struct wl_resource *surface_resource,
4828 struct wl_resource *parent_resource)
4829{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004830 struct weston_surface *surface =
4831 wl_resource_get_user_data(surface_resource);
4832 struct weston_surface *parent =
4833 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004834 struct weston_subsurface *sub;
4835 static const char where[] = "get_subsurface: wl_subsurface@";
4836
4837 if (surface == parent) {
4838 wl_resource_post_error(resource,
4839 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4840 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004841 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004842 return;
4843 }
4844
4845 if (weston_surface_to_subsurface(surface)) {
4846 wl_resource_post_error(resource,
4847 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4848 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004849 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004850 return;
4851 }
4852
Pekka Paalanen50b67472014-10-01 15:02:41 +03004853 if (weston_surface_set_role(surface, "wl_subsurface", resource,
4854 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004855 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004856
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004857 if (weston_surface_get_main_surface(parent) == surface) {
4858 wl_resource_post_error(resource,
4859 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4860 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004861 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004862 return;
4863 }
4864
Pekka Paalanene67858b2013-04-25 13:57:42 +03004865 /* make sure the parent is in its own list */
4866 if (wl_list_empty(&parent->subsurface_list)) {
4867 if (!weston_subsurface_create_for_parent(parent)) {
4868 wl_resource_post_no_memory(resource);
4869 return;
4870 }
4871 }
4872
4873 sub = weston_subsurface_create(id, surface, parent);
4874 if (!sub) {
4875 wl_resource_post_no_memory(resource);
4876 return;
4877 }
4878
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004879 surface->committed = subsurface_committed;
4880 surface->committed_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004881 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004882}
4883
4884static void
4885subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
4886{
4887 wl_resource_destroy(resource);
4888}
4889
4890static const struct wl_subcompositor_interface subcompositor_interface = {
4891 subcompositor_destroy,
4892 subcompositor_get_subsurface
4893};
4894
4895static void
4896bind_subcompositor(struct wl_client *client,
4897 void *data, uint32_t version, uint32_t id)
4898{
4899 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004900 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004901
Jason Ekstranda85118c2013-06-27 20:17:02 -05004902 resource =
4903 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004904 if (resource == NULL) {
4905 wl_client_post_no_memory(client);
4906 return;
4907 }
4908 wl_resource_set_implementation(resource, &subcompositor_interface,
4909 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004910}
4911
Bryce Harrington0795ece2016-08-30 12:04:26 -07004912/** Set a DPMS mode on all of the compositor's outputs
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004913 *
4914 * \param compositor The compositor instance
4915 * \param state The DPMS state the outputs will be set to
4916 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004917static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004918weston_compositor_dpms(struct weston_compositor *compositor,
4919 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004920{
4921 struct weston_output *output;
4922
Bryce Harrington08976ac2016-08-30 12:05:16 -07004923 wl_list_for_each(output, &compositor->output_list, link)
4924 if (output->set_dpms)
4925 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004926}
4927
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004928/** Restores the compositor to active status
4929 *
4930 * \param compositor The compositor instance
4931 *
4932 * If the compositor was in a sleeping mode, all outputs are powered
4933 * back on via DPMS. Otherwise if the compositor was inactive
4934 * (idle/locked, offscreen, or sleeping) then the compositor's wake
4935 * signal will fire.
4936 *
4937 * Restarts the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004938 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004939 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004940WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004941weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004942{
Neil Roberts8b62e202013-09-30 13:14:47 +01004943 uint32_t old_state = compositor->state;
4944
4945 /* The state needs to be changed before emitting the wake
4946 * signal because that may try to schedule a repaint which
4947 * will not work if the compositor is still sleeping */
4948 compositor->state = WESTON_COMPOSITOR_ACTIVE;
4949
4950 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004951 case WESTON_COMPOSITOR_SLEEPING:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004952 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004953 case WESTON_COMPOSITOR_OFFSCREEN:
Daniel Stone893b9362016-11-08 15:47:09 +00004954 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004955 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004956 /* fall through */
4957 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004958 wl_event_source_timer_update(compositor->idle_source,
4959 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004960 }
4961}
4962
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004963/** Turns off rendering and frame events for the compositor.
4964 *
4965 * \param compositor The compositor instance
4966 *
4967 * This is used for example to prevent further rendering while the
4968 * compositor is shutting down.
4969 *
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004970 * Stops the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004971 *
4972 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004973 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004974WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004975weston_compositor_offscreen(struct weston_compositor *compositor)
4976{
4977 switch (compositor->state) {
4978 case WESTON_COMPOSITOR_OFFSCREEN:
4979 return;
4980 case WESTON_COMPOSITOR_SLEEPING:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004981 default:
4982 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4983 wl_event_source_timer_update(compositor->idle_source, 0);
4984 }
4985}
4986
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004987/** Powers down all attached output devices
4988 *
4989 * \param compositor The compositor instance
4990 *
4991 * Causes rendering to the outputs to cease, and no frame events to be
4992 * sent. Only powers down the outputs if the compositor is not already
4993 * in sleep mode.
4994 *
4995 * Stops the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004996 *
4997 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004998 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004999WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005000weston_compositor_sleep(struct weston_compositor *compositor)
5001{
5002 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
5003 return;
5004
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01005005 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005006 compositor->state = WESTON_COMPOSITOR_SLEEPING;
5007 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
5008}
5009
Bryce Harringtonc9626a32015-12-11 13:11:38 -08005010/** Sets compositor to idle mode
5011 *
5012 * \param data The compositor instance
5013 *
5014 * This is called when the idle timer fires. Once the compositor is in
5015 * idle mode it requires a wake action (e.g. via
5016 * weston_compositor_wake()) to restore it. The compositor's
5017 * idle_signal will be triggered when the idle event occurs.
5018 *
5019 * Idleness can be inhibited by setting the compositor's idle_inhibit
5020 * property.
5021 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04005022static int
5023idle_handler(void *data)
5024{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005025 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04005026
5027 if (compositor->idle_inhibit)
5028 return 1;
5029
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005030 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005031 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04005032
5033 return 1;
5034}
5035
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005036WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08005037weston_plane_init(struct weston_plane *plane,
5038 struct weston_compositor *ec,
5039 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005040{
5041 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02005042 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005043 plane->x = x;
5044 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08005045 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03005046
5047 /* Init the link so that the call to wl_list_remove() when releasing
5048 * the plane without ever stacking doesn't lead to a crash */
5049 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005050}
5051
5052WL_EXPORT void
5053weston_plane_release(struct weston_plane *plane)
5054{
Xiong Zhang97116532013-10-23 13:58:31 +08005055 struct weston_view *view;
5056
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005057 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02005058 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03005059
Pekka Paalanen6551c092021-05-03 16:09:45 +03005060 /*
5061 * Can't use paint node list here, weston_plane is not specific to an
5062 * output.
5063 */
Xiong Zhang97116532013-10-23 13:58:31 +08005064 wl_list_for_each(view, &plane->compositor->view_list, link) {
5065 if (view->plane == plane)
5066 view->plane = NULL;
5067 }
5068
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03005069 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005070}
5071
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005072/** weston_compositor_stack_plane
5073 * \ingroup compositor
5074 */
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02005075WL_EXPORT void
5076weston_compositor_stack_plane(struct weston_compositor *ec,
5077 struct weston_plane *plane,
5078 struct weston_plane *above)
5079{
5080 if (above)
5081 wl_list_insert(above->link.prev, &plane->link);
5082 else
5083 wl_list_insert(&ec->plane_list, &plane->link);
5084}
5085
Quentin Glidic4ef719c2016-07-05 20:44:33 +02005086static void
5087output_release(struct wl_client *client, struct wl_resource *resource)
5088{
5089 wl_resource_destroy(resource);
5090}
5091
5092static const struct wl_output_interface output_interface = {
5093 output_release,
5094};
5095
5096
Casey Dahlin9074db52012-04-19 22:50:09 -04005097static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04005098{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05005099 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04005100}
5101
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005102static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005103bind_output(struct wl_client *client,
5104 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05005105{
Pekka Paalanen05347622017-03-27 12:24:34 +03005106 struct weston_head *head = data;
5107 struct weston_output *output = head->output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005108 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04005109 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05005110
Jason Ekstranda85118c2013-06-27 20:17:02 -05005111 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06005112 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07005113 if (resource == NULL) {
5114 wl_client_post_no_memory(client);
5115 return;
5116 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04005117
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005118 wl_list_insert(&head->resource_list, wl_resource_get_link(resource));
Pekka Paalanen055c1132017-03-27 16:31:25 +03005119 wl_resource_set_implementation(resource, &output_interface, head,
Pekka Paalanen05347622017-03-27 12:24:34 +03005120 unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04005121
Pekka Paalanen05347622017-03-27 12:24:34 +03005122 assert(output);
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05005123 wl_output_send_geometry(resource,
5124 output->x,
5125 output->y,
Pekka Paalanen01f60212017-03-24 15:39:24 +02005126 head->mm_width,
5127 head->mm_height,
5128 head->subpixel,
5129 head->make, head->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04005130 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04005131 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02005132 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02005133 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005134
5135 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05005136 wl_output_send_mode(resource,
5137 mode->flags,
5138 mode->width,
5139 mode->height,
5140 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005141 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02005142
Jasper St. Pierre0013a292014-08-07 16:43:11 -04005143 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02005144 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05005145}
5146
Pekka Paalanendcac3512017-12-08 14:13:34 +02005147static void
5148weston_head_add_global(struct weston_head *head)
5149{
5150 head->global = wl_global_create(head->compositor->wl_display,
5151 &wl_output_interface, 3,
5152 head, bind_output);
5153}
5154
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02005155/** Remove the global wl_output protocol object
5156 *
5157 * \param head The head whose global to remove.
5158 *
5159 * Also orphans the wl_resources for this head (wl_output).
5160 */
5161static void
5162weston_head_remove_global(struct weston_head *head)
5163{
5164 struct wl_resource *resource, *tmp;
5165
5166 if (head->global)
5167 wl_global_destroy(head->global);
5168 head->global = NULL;
5169
5170 wl_resource_for_each_safe(resource, tmp, &head->resource_list) {
5171 unbind_resource(resource);
5172 wl_resource_set_destructor(resource, NULL);
5173 wl_resource_set_user_data(resource, NULL);
5174 }
Roman Gilge97391c2019-03-29 13:01:06 +01005175
5176 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
5177 /* It's sufficient to unset the destructor, then the list elements
5178 * won't be accessed.
5179 */
5180 wl_resource_set_destructor(resource, NULL);
5181 }
5182 wl_list_init(&head->xdg_output_resource_list);
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02005183}
5184
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005185/** Get the backing object of wl_output
5186 *
5187 * \param resource A wl_output protocol object.
5188 * \return The backing object (user data) of a wl_resource representing a
5189 * wl_output protocol object.
Marius Vlad78984ee2019-06-11 00:05:08 +03005190 *
5191 * \ingroup head
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005192 */
Pekka Paalanen055c1132017-03-27 16:31:25 +03005193WL_EXPORT struct weston_head *
5194weston_head_from_resource(struct wl_resource *resource)
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005195{
5196 assert(wl_resource_instance_of(resource, &wl_output_interface,
5197 &output_interface));
5198
5199 return wl_resource_get_user_data(resource);
5200}
5201
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005202/** Initialize a pre-allocated weston_head
5203 *
5204 * \param head The head to initialize.
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005205 * \param name The head name, e.g. the connector name or equivalent.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005206 *
5207 * The head will be safe to attach, detach and release.
5208 *
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005209 * The name is used in logs, and can be used by compositors as a configuration
5210 * identifier.
5211 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005212 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005213 * \internal
5214 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005215WL_EXPORT void
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005216weston_head_init(struct weston_head *head, const char *name)
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005217{
5218 /* Add some (in)sane defaults which can be used
5219 * for checking if an output was properly configured
5220 */
5221 memset(head, 0, sizeof *head);
5222
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005223 wl_list_init(&head->compositor_link);
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005224 wl_signal_init(&head->destroy_signal);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005225 wl_list_init(&head->output_link);
5226 wl_list_init(&head->resource_list);
Roman Gilge97391c2019-03-29 13:01:06 +01005227 wl_list_init(&head->xdg_output_resource_list);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005228 head->name = strdup(name);
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305229 head->current_protection = WESTON_HDCP_DISABLE;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005230}
5231
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005232/** Send output heads changed signal
5233 *
5234 * \param output The output that changed.
5235 *
5236 * Notify that the enabled output gained and/or lost heads, or that the
5237 * associated heads may have changed their connection status. This does not
5238 * include cases where the output becomes enabled or disabled. The registered
5239 * callbacks are called after the change has successfully happened.
5240 *
5241 * If connection status change causes the compositor to attach or detach a head
5242 * to an enabled output, the registered callbacks may be called multiple times.
Marius Vlad55d87362019-06-11 01:15:35 +03005243 *
5244 * \ingroup output
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005245 */
5246static void
5247weston_output_emit_heads_changed(struct weston_output *output)
5248{
5249 wl_signal_emit(&output->compositor->output_heads_changed_signal,
5250 output);
5251}
5252
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005253/** Idle task for emitting heads_changed_signal */
5254static void
5255weston_compositor_call_heads_changed(void *data)
5256{
5257 struct weston_compositor *compositor = data;
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005258 struct weston_head *head;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005259
5260 compositor->heads_changed_source = NULL;
5261
5262 wl_signal_emit(&compositor->heads_changed_signal, compositor);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005263
5264 wl_list_for_each(head, &compositor->head_list, compositor_link) {
5265 if (head->output && head->output->enabled)
5266 weston_output_emit_heads_changed(head->output);
5267 }
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005268}
5269
5270/** Schedule a call on idle to heads_changed callback
5271 *
5272 * \param compositor The Compositor.
5273 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005274 * \ingroup compositor
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005275 * \internal
5276 */
5277static void
5278weston_compositor_schedule_heads_changed(struct weston_compositor *compositor)
5279{
5280 struct wl_event_loop *loop;
5281
5282 if (compositor->heads_changed_source)
5283 return;
5284
5285 loop = wl_display_get_event_loop(compositor->wl_display);
5286 compositor->heads_changed_source = wl_event_loop_add_idle(loop,
5287 weston_compositor_call_heads_changed, compositor);
5288}
5289
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005290/** Register a new head
5291 *
5292 * \param compositor The compositor.
5293 * \param head The head to register, must not be already registered.
5294 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005295 * This signals the core that a new head has become available, leading to
5296 * heads_changed hook being called later.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005297 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005298 * \ingroup compositor
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005299 * \internal
5300 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005301WL_EXPORT void
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005302weston_compositor_add_head(struct weston_compositor *compositor,
5303 struct weston_head *head)
5304{
5305 assert(wl_list_empty(&head->compositor_link));
5306 assert(head->name);
5307
5308 wl_list_insert(compositor->head_list.prev, &head->compositor_link);
5309 head->compositor = compositor;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005310 weston_compositor_schedule_heads_changed(compositor);
5311}
5312
5313/** Adds a listener to be called when heads change
5314 *
5315 * \param compositor The compositor.
5316 * \param listener The listener to add.
5317 *
Marius Vlada2dace22019-06-12 16:05:44 +03005318 * The listener notify function argument is weston_compositor.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005319 *
5320 * The listener function will be called after heads are added or their
5321 * connection status has changed. Several changes may be accumulated into a
5322 * single call. The user is expected to iterate over the existing heads and
5323 * check their statuses to find out what changed.
5324 *
5325 * \sa weston_compositor_iterate_heads, weston_head_is_connected,
5326 * weston_head_is_enabled
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005327 * \ingroup compositor
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005328 */
5329WL_EXPORT void
5330weston_compositor_add_heads_changed_listener(struct weston_compositor *compositor,
5331 struct wl_listener *listener)
5332{
5333 wl_signal_add(&compositor->heads_changed_signal, listener);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005334}
5335
5336/** Iterate over available heads
5337 *
5338 * \param compositor The compositor.
Marius Vlada2dace22019-06-12 16:05:44 +03005339 * \param iter The iterator, or NULL for start.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005340 * \return The next available head in the list.
5341 *
5342 * Returns all available heads, regardless of being connected or enabled.
5343 *
5344 * You can iterate over all heads as follows:
5345 * \code
5346 * struct weston_head *head = NULL;
5347 *
5348 * while ((head = weston_compositor_iterate_heads(compositor, head))) {
5349 * ...
5350 * }
5351 * \endcode
5352 *
5353 * If you cause \c iter to be removed from the list, you cannot use it to
5354 * continue iterating. Removing any other item is safe.
5355 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005356 * \ingroup compositor
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005357 */
5358WL_EXPORT struct weston_head *
5359weston_compositor_iterate_heads(struct weston_compositor *compositor,
5360 struct weston_head *iter)
5361{
5362 struct wl_list *list = &compositor->head_list;
5363 struct wl_list *node;
5364
5365 assert(compositor);
5366 assert(!iter || iter->compositor == compositor);
5367
5368 if (iter)
5369 node = iter->compositor_link.next;
5370 else
5371 node = list->next;
5372
5373 assert(node);
5374 assert(!iter || node != &iter->compositor_link);
5375
5376 if (node == list)
5377 return NULL;
5378
5379 return container_of(node, struct weston_head, compositor_link);
5380}
5381
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005382/** Iterate over attached heads
5383 *
5384 * \param output The output whose heads to iterate.
Marius Vlada2dace22019-06-12 16:05:44 +03005385 * \param iter The iterator, or NULL for start.
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005386 * \return The next attached head in the list.
5387 *
5388 * Returns all heads currently attached to the output.
5389 *
5390 * You can iterate over all heads as follows:
5391 * \code
5392 * struct weston_head *head = NULL;
5393 *
5394 * while ((head = weston_output_iterate_heads(output, head))) {
5395 * ...
5396 * }
5397 * \endcode
5398 *
5399 * If you cause \c iter to be removed from the list, you cannot use it to
5400 * continue iterating. Removing any other item is safe.
5401 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05005402 * \ingroup output
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005403 */
5404WL_EXPORT struct weston_head *
5405weston_output_iterate_heads(struct weston_output *output,
5406 struct weston_head *iter)
5407{
5408 struct wl_list *list = &output->head_list;
5409 struct wl_list *node;
5410
5411 assert(output);
5412 assert(!iter || iter->output == output);
5413
5414 if (iter)
5415 node = iter->output_link.next;
5416 else
5417 node = list->next;
5418
5419 assert(node);
5420 assert(!iter || node != &iter->output_link);
5421
5422 if (node == list)
5423 return NULL;
5424
5425 return container_of(node, struct weston_head, output_link);
5426}
5427
Pekka Paalanendcac3512017-12-08 14:13:34 +02005428/** Attach a head to an output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005429 *
5430 * \param output The output to attach to.
5431 * \param head The head that is not yet attached.
5432 * \return 0 on success, -1 on failure.
5433 *
5434 * Attaches the given head to the output. All heads of an output are clones
5435 * and share the resolution and timings.
5436 *
5437 * Cloning heads this way uses less resources than creating an output for
5438 * each head, but is not always possible due to environment, driver and hardware
5439 * limitations.
5440 *
5441 * On failure, the head remains unattached. Success of this function does not
5442 * guarantee the output configuration is actually valid. The final checks are
Pekka Paalanendcac3512017-12-08 14:13:34 +02005443 * made on weston_output_enable() unless the output was already enabled.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005444 *
Marius Vlad55d87362019-06-11 01:15:35 +03005445 * \ingroup output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005446 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005447WL_EXPORT int
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005448weston_output_attach_head(struct weston_output *output,
5449 struct weston_head *head)
5450{
Pekka Paalanendcac3512017-12-08 14:13:34 +02005451 char *head_names;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005452
5453 if (!wl_list_empty(&head->output_link))
5454 return -1;
5455
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005456 if (output->attach_head) {
5457 if (output->attach_head(output, head) < 0)
5458 return -1;
5459 } else if (!wl_list_empty(&output->head_list)) {
5460 /* No support for clones in the legacy path. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005461 return -1;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005462 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005463
5464 head->output = output;
5465 wl_list_insert(output->head_list.prev, &head->output_link);
5466
Pekka Paalanendcac3512017-12-08 14:13:34 +02005467 if (output->enabled) {
5468 weston_head_add_global(head);
5469
5470 head_names = weston_output_create_heads_string(output);
5471 weston_log("Output '%s' updated to have head(s) %s\n",
5472 output->name, head_names);
5473 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005474
5475 weston_output_emit_heads_changed(output);
Pekka Paalanendcac3512017-12-08 14:13:34 +02005476 }
5477
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005478 return 0;
5479}
5480
5481/** Detach a head from its output
5482 *
5483 * \param head The head to detach.
5484 *
5485 * It is safe to detach a non-attached head.
5486 *
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005487 * If the head is attached to an enabled output and the output will be left
5488 * with no heads, the output will be disabled.
5489 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005490 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005491 * \sa weston_output_disable
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005492 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005493WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005494weston_head_detach(struct weston_head *head)
5495{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005496 struct weston_output *output = head->output;
Pekka Paalanena0106992017-12-08 16:11:17 +02005497 char *head_names;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005498
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005499 wl_list_remove(&head->output_link);
5500 wl_list_init(&head->output_link);
5501 head->output = NULL;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005502
5503 if (!output)
5504 return;
5505
5506 if (output->detach_head)
5507 output->detach_head(output, head);
5508
5509 if (output->enabled) {
5510 weston_head_remove_global(head);
5511
Pekka Paalanena0106992017-12-08 16:11:17 +02005512 if (wl_list_empty(&output->head_list)) {
5513 weston_log("Output '%s' no heads left, disabling.\n",
5514 output->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005515 weston_output_disable(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02005516 } else {
5517 head_names = weston_output_create_heads_string(output);
5518 weston_log("Output '%s' updated to have head(s) %s\n",
5519 output->name, head_names);
5520 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005521
5522 weston_output_emit_heads_changed(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02005523 }
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005524 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005525}
5526
5527/** Destroy a head
5528 *
5529 * \param head The head to be released.
5530 *
5531 * Destroys the head. The caller is responsible for freeing the memory pointed
5532 * to by \c head.
5533 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005534 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005535 * \internal
5536 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005537WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005538weston_head_release(struct weston_head *head)
5539{
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005540 wl_signal_emit(&head->destroy_signal, head);
5541
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005542 weston_head_detach(head);
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005543
5544 free(head->make);
5545 free(head->model);
5546 free(head->serial_number);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005547 free(head->name);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005548
5549 wl_list_remove(&head->compositor_link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005550}
5551
Pekka Paalanene19970f2017-08-28 14:11:02 +03005552static void
5553weston_head_set_device_changed(struct weston_head *head)
5554{
5555 head->device_changed = true;
5556
5557 if (head->compositor)
5558 weston_compositor_schedule_heads_changed(head->compositor);
5559}
5560
5561/** String equal comparison with NULLs being equal */
5562static bool
5563str_null_eq(const char *a, const char *b)
5564{
5565 if (!a && !b)
5566 return true;
5567
5568 if (!!a != !!b)
5569 return false;
5570
5571 return strcmp(a, b) == 0;
5572}
5573
Pekka Paalanen01f60212017-03-24 15:39:24 +02005574/** Store monitor make, model and serial number
5575 *
5576 * \param head The head to modify.
5577 * \param make The monitor make. If EDID is available, the PNP ID. Otherwise
5578 * any string, or NULL for none.
5579 * \param model The monitor model or name, or a made-up string, or NULL for
5580 * none.
5581 * \param serialno The monitor serial number, a made-up string, or NULL for
5582 * none.
5583 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005584 * This may set the device_changed flag.
5585 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005586 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005587 * \internal
5588 */
5589WL_EXPORT void
5590weston_head_set_monitor_strings(struct weston_head *head,
5591 const char *make,
5592 const char *model,
5593 const char *serialno)
5594{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005595 if (str_null_eq(head->make, make) &&
5596 str_null_eq(head->model, model) &&
5597 str_null_eq(head->serial_number, serialno))
5598 return;
5599
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005600 free(head->make);
5601 free(head->model);
5602 free(head->serial_number);
5603
5604 head->make = make ? strdup(make) : NULL;
5605 head->model = model ? strdup(model) : NULL;
5606 head->serial_number = serialno ? strdup(serialno) : NULL;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005607
5608 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005609}
5610
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005611/** Store display non-desktop status
5612 *
5613 * \param head The head to modify.
5614 * \param non_desktop Whether the head connects to a non-desktop display.
5615 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005616 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005617 * \internal
5618 */
5619WL_EXPORT void
5620weston_head_set_non_desktop(struct weston_head *head, bool non_desktop)
5621{
5622 if (head->non_desktop == non_desktop)
5623 return;
5624
5625 head->non_desktop = non_desktop;
5626
5627 weston_head_set_device_changed(head);
5628}
5629
Lucas Stacha69cb712019-11-25 23:29:31 +00005630/** Store display transformation
5631 *
5632 * \param head The head to modify.
5633 * \param transform The transformation to apply for this head
5634 *
5635 * This may set the device_changed flag.
5636 *
5637 * \ingroup head
5638 * \internal
5639 */
5640WL_EXPORT void
5641weston_head_set_transform(struct weston_head *head, uint32_t transform)
5642{
5643 if (head->transform == transform)
5644 return;
5645
5646 head->transform = transform;
5647
5648 weston_head_set_device_changed(head);
5649}
5650
5651
Pekka Paalanen01f60212017-03-24 15:39:24 +02005652/** Store physical image size
5653 *
5654 * \param head The head to modify.
5655 * \param mm_width Image area width in millimeters.
5656 * \param mm_height Image area height in millimeters.
5657 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005658 * This may set the device_changed flag.
5659 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005660 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005661 * \internal
5662 */
5663WL_EXPORT void
5664weston_head_set_physical_size(struct weston_head *head,
5665 int32_t mm_width, int32_t mm_height)
5666{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005667 if (head->mm_width == mm_width &&
5668 head->mm_height == mm_height)
5669 return;
5670
Pekka Paalanen01f60212017-03-24 15:39:24 +02005671 head->mm_width = mm_width;
5672 head->mm_height = mm_height;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005673
5674 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005675}
5676
5677/** Store monitor sub-pixel layout
5678 *
5679 * \param head The head to modify.
5680 * \param sp Sub-pixel layout. The possible values are:
5681 * - WL_OUTPUT_SUBPIXEL_UNKNOWN,
5682 * - WL_OUTPUT_SUBPIXEL_NONE,
5683 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB,
5684 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR,
5685 * - WL_OUTPUT_SUBPIXEL_VERTICAL_RGB,
5686 * - WL_OUTPUT_SUBPIXEL_VERTICAL_BGR
5687 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005688 * This may set the device_changed flag.
5689 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005690 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005691 * \internal
5692 */
5693WL_EXPORT void
5694weston_head_set_subpixel(struct weston_head *head,
5695 enum wl_output_subpixel sp)
5696{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005697 if (head->subpixel == sp)
5698 return;
5699
Pekka Paalanen01f60212017-03-24 15:39:24 +02005700 head->subpixel = sp;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005701
5702 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005703}
5704
5705/** Mark the monitor as internal
5706 *
5707 * This is used for embedded screens, like laptop panels.
5708 *
5709 * \param head The head to mark as internal.
5710 *
5711 * By default a head is external. The type is often inferred from the physical
5712 * connector type.
5713 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005714 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005715 * \internal
5716 */
5717WL_EXPORT void
5718weston_head_set_internal(struct weston_head *head)
5719{
5720 head->connection_internal = true;
5721}
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005722
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005723/** Store connector status
5724 *
5725 * \param head The head to modify.
5726 * \param connected Whether the head is connected.
5727 *
5728 * Connectors are created as disconnected. This function can be used to
5729 * set the connector status.
5730 *
5731 * The status should be set to true when a physical connector is connected to
5732 * a video sink device like a monitor and to false when the connector is
5733 * disconnected. For nested backends, the connection status should reflect the
5734 * connection to the parent display server.
5735 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005736 * When the connection status changes, it schedules a call to the heads_changed
Pekka Paalanene19970f2017-08-28 14:11:02 +03005737 * hook and sets the device_changed flag.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005738 *
5739 * \sa weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005740 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005741 * \internal
5742 */
5743WL_EXPORT void
5744weston_head_set_connection_status(struct weston_head *head, bool connected)
5745{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005746 if (head->connected == connected)
5747 return;
5748
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005749 head->connected = connected;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005750
Pekka Paalanene19970f2017-08-28 14:11:02 +03005751 weston_head_set_device_changed(head);
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005752}
5753
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305754static void
5755weston_output_compute_protection(struct weston_output *output)
5756{
5757 struct weston_head *head;
5758 enum weston_hdcp_protection op_protection;
5759 bool op_protection_valid = false;
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305760 struct weston_compositor *wc = output->compositor;
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305761
5762 wl_list_for_each(head, &output->head_list, output_link) {
5763 if (!op_protection_valid) {
5764 op_protection = head->current_protection;
5765 op_protection_valid = true;
5766 }
5767 if (head->current_protection < op_protection)
5768 op_protection = head->current_protection;
5769 }
5770
5771 if (!op_protection_valid)
5772 op_protection = WESTON_HDCP_DISABLE;
5773
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305774 if (output->current_protection != op_protection) {
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305775 output->current_protection = op_protection;
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305776 weston_output_damage(output);
Ankit Nautiyalfaa5ab42019-07-08 13:23:24 +05305777 weston_schedule_surface_protection_update(wc);
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305778 }
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305779}
5780
5781WL_EXPORT void
5782weston_head_set_content_protection_status(struct weston_head *head,
5783 enum weston_hdcp_protection status)
5784{
5785 head->current_protection = status;
5786 if (head->output)
5787 weston_output_compute_protection(head->output);
5788}
5789
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005790/** Is the head currently connected?
5791 *
5792 * \param head The head to query.
5793 * \return Connection status.
5794 *
5795 * Returns true if the head is physically connected to a monitor, or in
5796 * case of a nested backend returns true when there is a connection to the
5797 * parent display server.
5798 *
5799 * This is independent from the head being enabled.
5800 *
5801 * \sa weston_head_is_enabled
Marius Vlad78984ee2019-06-11 00:05:08 +03005802 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005803 */
5804WL_EXPORT bool
5805weston_head_is_connected(struct weston_head *head)
5806{
5807 return head->connected;
5808}
5809
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005810/** Is the head currently enabled?
5811 *
5812 * \param head The head to query.
5813 * \return Video status.
5814 *
5815 * Returns true if the head is currently transmitting a video stream.
5816 *
5817 * This is independent of the head being connected.
5818 *
5819 * \sa weston_head_is_connected
Marius Vlad78984ee2019-06-11 00:05:08 +03005820 * \ingroup head
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005821 */
5822WL_EXPORT bool
5823weston_head_is_enabled(struct weston_head *head)
5824{
5825 if (!head->output)
5826 return false;
5827
5828 return head->output->enabled;
5829}
5830
Pekka Paalanene19970f2017-08-28 14:11:02 +03005831/** Has the device information changed?
5832 *
5833 * \param head The head to query.
5834 * \return True if the device information has changed since last reset.
5835 *
5836 * The information about the connected display device, e.g. a monitor, may
5837 * change without being disconnected in between. Changing information
5838 * causes a call to the heads_changed hook.
5839 *
5840 * The information includes make, model, serial number, physical size,
5841 * and sub-pixel type. The connection status is also included.
5842 *
5843 * \sa weston_head_reset_device_changed, weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005844 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03005845 */
5846WL_EXPORT bool
5847weston_head_is_device_changed(struct weston_head *head)
5848{
5849 return head->device_changed;
5850}
5851
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005852/** Does the head represent a non-desktop display?
5853 *
5854 * \param head The head to query.
5855 * \return True if the device is a non-desktop display.
5856 *
5857 * Non-desktop heads are not attached to outputs by default.
5858 * This stops weston from extending the desktop onto head mounted displays.
5859 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005860 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005861 */
5862WL_EXPORT bool
5863weston_head_is_non_desktop(struct weston_head *head)
5864{
5865 return head->non_desktop;
5866}
5867
Pekka Paalanene19970f2017-08-28 14:11:02 +03005868/** Acknowledge device information change
5869 *
5870 * \param head The head to acknowledge.
5871 *
5872 * Clears the device changed flag on this head. When a compositor has processed
5873 * device information, it should call this to be able to notice further
5874 * changes.
5875 *
5876 * \sa weston_head_is_device_changed
Marius Vlad78984ee2019-06-11 00:05:08 +03005877 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03005878 */
5879WL_EXPORT void
5880weston_head_reset_device_changed(struct weston_head *head)
5881{
5882 head->device_changed = false;
5883}
5884
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005885/** Get the name of a head
5886 *
5887 * \param head The head to query.
5888 * \return The head's name, not NULL.
5889 *
5890 * The name depends on the backend. The DRM backend uses connector names,
5891 * other backends may use hardcoded names or user-given names.
Marius Vlad78984ee2019-06-11 00:05:08 +03005892 *
5893 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005894 */
5895WL_EXPORT const char *
5896weston_head_get_name(struct weston_head *head)
5897{
5898 return head->name;
5899}
5900
5901/** Get the output the head is attached to
5902 *
5903 * \param head The head to query.
5904 * \return The output the head is attached to, or NULL if detached.
Marius Vlad78984ee2019-06-11 00:05:08 +03005905 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005906 */
5907WL_EXPORT struct weston_output *
5908weston_head_get_output(struct weston_head *head)
5909{
5910 return head->output;
5911}
5912
Lucas Stacha69cb712019-11-25 23:29:31 +00005913/** Get the head's native transformation
5914 *
5915 * \param head The head to query.
5916 * \return The head's native transform, as a WL_OUTPUT_TRANSFORM_* value
5917 *
5918 * A weston_head may have a 'native' transform provided by the backend.
5919 * Examples include panels which are physically rotated, where the rotation
5920 * is recorded and described as part of the system configuration. This call
5921 * will return any known native transform for the head.
5922 *
5923 * \ingroup head
5924 */
5925WL_EXPORT uint32_t
5926weston_head_get_transform(struct weston_head *head)
5927{
5928 return head->transform;
5929}
5930
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005931/** Add destroy callback for a head
5932 *
5933 * \param head The head to watch for.
5934 * \param listener The listener to add. The \c notify member must be set.
5935 *
5936 * Heads may get destroyed for various reasons by the backends. If a head is
5937 * attached to an output, the compositor should listen for head destruction
5938 * and reconfigure or destroy the output if necessary.
5939 *
5940 * The destroy callbacks will be called on weston_head destruction before any
5941 * automatic detaching from an associated weston_output and before any
5942 * weston_head information is lost.
5943 *
5944 * The \c data argument to the notify callback is the weston_head being
5945 * destroyed.
Marius Vlad78984ee2019-06-11 00:05:08 +03005946 *
5947 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005948 */
5949WL_EXPORT void
5950weston_head_add_destroy_listener(struct weston_head *head,
5951 struct wl_listener *listener)
5952{
5953 wl_signal_add(&head->destroy_signal, listener);
5954}
5955
5956/** Look up destroy listener for a head
5957 *
5958 * \param head The head to query.
5959 * \param notify The notify function used used for the added destroy listener.
5960 * \return The listener, or NULL if not found.
5961 *
5962 * This looks up the previously added destroy listener struct based on the
5963 * notify function it has. The listener can be used to access user data
5964 * through \c container_of().
5965 *
5966 * \sa wl_signal_get()
Marius Vlad78984ee2019-06-11 00:05:08 +03005967 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005968 */
5969WL_EXPORT struct wl_listener *
5970weston_head_get_destroy_listener(struct weston_head *head,
5971 wl_notify_func_t notify)
5972{
5973 return wl_signal_get(&head->destroy_signal, notify);
5974}
5975
David Fort0de859e2016-05-27 23:22:57 +02005976/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005977static void
David Fort0de859e2016-05-27 23:22:57 +02005978weston_compositor_reflow_outputs(struct weston_compositor *compositor,
5979 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005980{
5981 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02005982 bool start_resizing = false;
5983
5984 if (!delta_width)
5985 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005986
5987 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02005988 if (output == resized_output) {
5989 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005990 continue;
5991 }
5992
David Fort0de859e2016-05-27 23:22:57 +02005993 if (start_resizing) {
5994 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005995 output->dirty = 1;
5996 }
5997 }
5998}
5999
Pekka Paalanen06920792020-12-08 15:22:36 +02006000/** Transform a region in-place from global to output coordinates
6001 *
6002 * \param output The output that defines the transformation.
6003 * \param region The region to be transformed in-place.
6004 *
6005 * This takes a region in the global coordinate system, and takes into account
6006 * output position, transform and scale, and the zoom, and converts the region
6007 * into output pixel coordinates in the framebuffer.
6008 *
6009 * Uses floating-point operations if zoom is active, which may round to expand
6010 * the region.
6011 *
6012 * \internal
6013 * \ingroup output
6014 */
6015WL_EXPORT void
6016weston_output_region_from_global(struct weston_output *output,
6017 pixman_region32_t *region)
6018{
6019 if (output->zoom.active) {
6020 weston_matrix_transform_region(region, &output->matrix, region);
6021 } else {
6022 pixman_region32_translate(region, -output->x, -output->y);
6023 weston_transformed_region(output->width, output->height,
6024 output->transform,
6025 output->current_scale,
6026 region, region);
6027 }
6028}
6029
Pekka Paalanend72bad22017-03-29 17:01:41 +03006030static void
Scott Moreauccbf29d2012-02-22 14:21:41 -07006031weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01006032{
Scott Moreau850ca422012-05-21 15:21:25 -06006033 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05006034
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006035 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05006036 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06006037
Scott Moreauccbf29d2012-02-22 14:21:41 -07006038 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06006039 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05006040 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01006041 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05006042 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01006043 weston_matrix_scale(&output->matrix, magnification,
6044 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07006045 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04006046
Jason Ekstrandfb23df72014-10-16 10:55:21 -05006047 switch (output->transform) {
6048 case WL_OUTPUT_TRANSFORM_FLIPPED:
6049 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
6050 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
6051 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
6052 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
6053 weston_matrix_scale(&output->matrix, -1, 1, 1);
6054 break;
6055 }
6056
6057 switch (output->transform) {
6058 default:
6059 case WL_OUTPUT_TRANSFORM_NORMAL:
6060 case WL_OUTPUT_TRANSFORM_FLIPPED:
6061 break;
6062 case WL_OUTPUT_TRANSFORM_90:
6063 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +02006064 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
6065 weston_matrix_rotate_xy(&output->matrix, 0, -1);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05006066 break;
6067 case WL_OUTPUT_TRANSFORM_180:
6068 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
6069 weston_matrix_translate(&output->matrix,
6070 -output->width, -output->height, 0);
6071 weston_matrix_rotate_xy(&output->matrix, -1, 0);
6072 break;
6073 case WL_OUTPUT_TRANSFORM_270:
6074 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +02006075 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
6076 weston_matrix_rotate_xy(&output->matrix, 0, 1);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05006077 break;
6078 }
6079
6080 if (output->current_scale != 1)
6081 weston_matrix_scale(&output->matrix,
6082 output->current_scale,
6083 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01006084
Scott Moreauccbf29d2012-02-22 14:21:41 -07006085 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05006086
6087 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07006088}
6089
Scott Moreau1bad5db2012-08-18 01:04:05 -06006090static void
Alexander Larsson0b135062013-05-28 16:23:36 +02006091weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06006092{
6093 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03006094 output->native_scale = scale;
6095 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06006096
Pekka Paalanen59987fa2016-04-26 15:50:59 +03006097 convert_size_by_transform_scale(&output->width, &output->height,
6098 output->current_mode->width,
6099 output->current_mode->height,
6100 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02006101}
6102
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006103static void
6104weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07006105{
6106 output->x = x;
6107 output->y = y;
6108
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006109 pixman_region32_fini(&output->region);
Scott Moreauccbf29d2012-02-22 14:21:41 -07006110 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06006111 output->width,
6112 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01006113}
6114
Marius Vlad55d87362019-06-11 01:15:35 +03006115/**
6116 * \ingroup output
6117 */
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006118WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006119weston_output_move(struct weston_output *output, int x, int y)
6120{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006121 struct weston_head *head;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006122 struct wl_resource *resource;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006123 int ver;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006124
6125 output->move_x = x - output->x;
6126 output->move_y = y - output->y;
6127
6128 if (output->move_x == 0 && output->move_y == 0)
6129 return;
6130
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006131 weston_output_init_geometry(output, x, y);
6132
6133 output->dirty = 1;
6134
6135 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006136 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006137
6138 /* Notify clients of the change for output position. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006139 wl_list_for_each(head, &output->head_list, output_link) {
6140 wl_resource_for_each(resource, &head->resource_list) {
6141 wl_output_send_geometry(resource,
6142 output->x,
6143 output->y,
6144 head->mm_width,
6145 head->mm_height,
6146 head->subpixel,
6147 head->make,
6148 head->model,
6149 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08006150
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006151 ver = wl_resource_get_version(resource);
6152 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
6153 wl_output_send_done(resource);
6154 }
Roman Gilge97391c2019-03-29 13:01:06 +01006155
6156 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
6157 zxdg_output_v1_send_logical_position(resource,
6158 output->x,
6159 output->y);
6160 zxdg_output_v1_send_done(resource);
6161 }
Quanxian Wangb2c86362014-03-14 09:16:25 +08006162 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006163}
6164
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006165/** Signal that a pending output is taken into use.
6166 *
6167 * Removes the output from the pending list and adds it to the compositor's
6168 * list of enabled outputs. The output created signal is emitted.
Giulio Camuffob1147152015-05-06 21:41:57 +03006169 *
Pekka Paalanen2210ad02017-03-30 15:48:06 +03006170 * The output gets an internal ID assigned, and the wl_output global is
6171 * created.
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03006172 *
Giulio Camuffob1147152015-05-06 21:41:57 +03006173 * \param compositor The compositor instance.
6174 * \param output The output to be added.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006175 *
6176 * \internal
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006177 * \ingroup compositor
Giulio Camuffob1147152015-05-06 21:41:57 +03006178 */
Pekka Paalanenf9681b52017-03-29 16:58:48 +03006179static void
Giulio Camuffob1147152015-05-06 21:41:57 +03006180weston_compositor_add_output(struct weston_compositor *compositor,
6181 struct weston_output *output)
6182{
Armin Krezoviće5403842016-08-05 15:28:29 +02006183 struct weston_view *view, *next;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03006184 struct weston_head *head;
Armin Krezoviće5403842016-08-05 15:28:29 +02006185
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006186 assert(!output->enabled);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03006187
6188 /* Verify we haven't reached the limit of 32 available output IDs */
6189 assert(ffs(~compositor->output_id_pool) > 0);
6190
6191 /* Invert the output id pool and look for the lowest numbered
6192 * switch (the least significant bit). Take that bit's position
6193 * as our ID, and mark it used in the compositor's output_id_pool.
6194 */
6195 output->id = ffs(~compositor->output_id_pool) - 1;
6196 compositor->output_id_pool |= 1u << output->id;
6197
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006198 wl_list_remove(&output->link);
Giulio Camuffob1147152015-05-06 21:41:57 +03006199 wl_list_insert(compositor->output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006200 output->enabled = true;
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006201
Pekka Paalanendcac3512017-12-08 14:13:34 +02006202 wl_list_for_each(head, &output->head_list, output_link)
6203 weston_head_add_global(head);
Pekka Paalanen2210ad02017-03-30 15:48:06 +03006204
Giulio Camuffob1147152015-05-06 21:41:57 +03006205 wl_signal_emit(&compositor->output_created_signal, output);
Armin Krezoviće5403842016-08-05 15:28:29 +02006206
Pekka Paalanen6551c092021-05-03 16:09:45 +03006207 /*
6208 * Use view_list, as paint nodes have not been created for this
6209 * output yet. Any existing view might touch this new output.
6210 */
Armin Krezoviće5403842016-08-05 15:28:29 +02006211 wl_list_for_each_safe(view, next, &compositor->view_list, link)
6212 weston_view_geometry_dirty(view);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04006213}
6214
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006215/** Transform device coordinates into global coordinates
6216 *
Marius Vlada2dace22019-06-12 16:05:44 +03006217 * \param output the weston_output object
6218 * \param[in] device_x X coordinate in device units.
6219 * \param[in] device_y Y coordinate in device units.
6220 * \param[out] x X coordinate in the global space.
6221 * \param[out] y Y coordinate in the global space.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006222 *
Marius Vlada2dace22019-06-12 16:05:44 +03006223 * Transforms coordinates from the device coordinate space (physical pixel
6224 * units) to the global coordinate space (logical pixel units). This takes
6225 * into account output transform and scale.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006226 *
Marius Vlad55d87362019-06-11 01:15:35 +03006227 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006228 * \internal
6229 */
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006230WL_EXPORT void
6231weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02006232 double device_x, double device_y,
6233 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006234{
Derek Foreman0f679412014-10-02 13:41:17 -05006235 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02006236 device_x,
6237 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05006238 0.0,
6239 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006240
Derek Foreman67a18b92015-03-24 11:36:14 -05006241 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006242
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02006243 *x = p.f[0] / p.f[3];
6244 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006245}
6246
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006247/** Removes output from compositor's list of enabled outputs
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006248 *
6249 * \param output The weston_output object that is being removed.
6250 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006251 * The following happens:
6252 *
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03006253 * - Destroys all paint nodes related to the output.
6254 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006255 * - The output assignments of all views in the current scenegraph are
6256 * recomputed.
6257 *
6258 * - Presentation feedback is discarded.
6259 *
6260 * - Compositor is notified that outputs were changed and
6261 * applies the necessary changes to re-layout outputs.
6262 *
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006263 * - The output is put back in the pending outputs list.
6264 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006265 * - Signal is emitted to notify all users of the weston_output
6266 * object that the output is being destroyed.
6267 *
6268 * - wl_output protocol objects referencing this weston_output
Pekka Paalanen2210ad02017-03-30 15:48:06 +03006269 * are made inert, and the wl_output global is removed.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006270 *
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03006271 * - The output's internal ID is released.
6272 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006273 * \ingroup compositor
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006274 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006275 */
6276static void
6277weston_compositor_remove_output(struct weston_output *output)
6278{
Pekka Paalanenbccda712017-03-29 16:16:04 +03006279 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03006280 struct weston_paint_node *pnode, *pntmp;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006281 struct weston_view *view;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03006282 struct weston_head *head;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006283
6284 assert(output->destroying);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006285 assert(output->enabled);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006286
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03006287 wl_list_for_each_safe(pnode, pntmp,
6288 &output->paint_node_list, output_link) {
6289 weston_paint_node_destroy(pnode);
6290 }
Pekka Paalanen2fddc532021-04-30 17:41:29 +03006291 assert(wl_list_empty(&output->paint_node_z_order_list));
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03006292
Pekka Paalanen6551c092021-05-03 16:09:45 +03006293 /*
6294 * Use view_list in case the output did not go through repaint
6295 * after a view came on it, lacking a paint node. Just to be sure.
6296 */
Pekka Paalanenbccda712017-03-29 16:16:04 +03006297 wl_list_for_each(view, &compositor->view_list, link) {
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006298 if (view->output_mask & (1u << output->id))
6299 weston_view_assign_output(view);
6300 }
6301
6302 weston_presentation_feedback_discard_list(&output->feedback_list);
6303
Pekka Paalanen9711fd92018-06-21 14:26:18 +03006304 weston_compositor_reflow_outputs(compositor, output, -output->width);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006305
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006306 wl_list_remove(&output->link);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006307 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006308 output->enabled = false;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006309
Pekka Paalanenbccda712017-03-29 16:16:04 +03006310 wl_signal_emit(&compositor->output_destroyed_signal, output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006311 wl_signal_emit(&output->destroy_signal, output);
6312
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02006313 wl_list_for_each(head, &output->head_list, output_link)
6314 weston_head_remove_global(head);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03006315
6316 compositor->output_id_pool &= ~(1u << output->id);
6317 output->id = 0xffffffff; /* invalid */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006318}
6319
6320/** Sets the output scale for a given output.
6321 *
6322 * \param output The weston_output object that the scale is set for.
6323 * \param scale Scale factor for the given output.
6324 *
6325 * It only supports setting scale for an output that
6326 * is not enabled and it can only be ran once.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006327 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05006328 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006329 */
6330WL_EXPORT void
6331weston_output_set_scale(struct weston_output *output,
6332 int32_t scale)
6333{
6334 /* We can only set scale on a disabled output */
6335 assert(!output->enabled);
6336
6337 /* We only want to set scale once */
6338 assert(!output->scale);
6339
6340 output->scale = scale;
6341}
6342
6343/** Sets the output transform for a given output.
6344 *
6345 * \param output The weston_output object that the transform is set for.
6346 * \param transform Transform value for the given output.
6347 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006348 * Refer to wl_output::transform section located at
6349 * https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_output
6350 * for list of values that can be passed to this function.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006351 *
Marius Vlad55d87362019-06-11 01:15:35 +03006352 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006353 */
6354WL_EXPORT void
6355weston_output_set_transform(struct weston_output *output,
6356 uint32_t transform)
6357{
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006358 struct weston_pointer_motion_event ev;
6359 struct wl_resource *resource;
6360 struct weston_seat *seat;
6361 pixman_region32_t old_region;
6362 int mid_x, mid_y;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006363 struct weston_head *head;
6364 int ver;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006365
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006366 if (!output->enabled && output->transform == UINT32_MAX) {
6367 output->transform = transform;
6368 return;
6369 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006370
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006371 weston_output_transform_scale_init(output, transform, output->scale);
6372
6373 pixman_region32_init(&old_region);
6374 pixman_region32_copy(&old_region, &output->region);
6375
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006376 weston_output_init_geometry(output, output->x, output->y);
6377
6378 output->dirty = 1;
6379
6380 /* Notify clients of the change for output transform. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006381 wl_list_for_each(head, &output->head_list, output_link) {
6382 wl_resource_for_each(resource, &head->resource_list) {
6383 wl_output_send_geometry(resource,
6384 output->x,
6385 output->y,
6386 head->mm_width,
6387 head->mm_height,
6388 head->subpixel,
6389 head->make,
6390 head->model,
6391 output->transform);
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006392
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006393 ver = wl_resource_get_version(resource);
6394 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
6395 wl_output_send_done(resource);
6396 }
Roman Gilge97391c2019-03-29 13:01:06 +01006397 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
6398 zxdg_output_v1_send_logical_position(resource,
6399 output->x,
6400 output->y);
6401 zxdg_output_v1_send_logical_size(resource,
6402 output->width,
6403 output->height);
6404 zxdg_output_v1_send_done(resource);
6405 }
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006406 }
6407
6408 /* we must ensure that pointers are inside output, otherwise they disappear */
6409 mid_x = output->x + output->width / 2;
6410 mid_y = output->y + output->height / 2;
6411
6412 ev.mask = WESTON_POINTER_MOTION_ABS;
6413 ev.x = wl_fixed_to_double(wl_fixed_from_int(mid_x));
6414 ev.y = wl_fixed_to_double(wl_fixed_from_int(mid_y));
6415
6416 wl_list_for_each(seat, &output->compositor->seat_list, link) {
6417 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
6418
6419 if (pointer && pixman_region32_contains_point(&old_region,
6420 wl_fixed_to_int(pointer->x),
6421 wl_fixed_to_int(pointer->y),
6422 NULL))
6423 weston_pointer_move(pointer, &ev);
6424 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006425}
6426
Pekka Paalanenb5265af2020-12-02 11:57:58 +02006427/** Make the output use renderer shadow buffer.
6428 *
6429 * \param output The weston_output object to modify.
6430 * \return True on success, false if unsupported.
6431 *
6432 * This can only be set on a disabled output object.
6433 *
6434 * This is a temporary API to demonstrate WESTON_CAP_COLOR_OPS and allow
6435 * testing related features. This will be superseded with color management
6436 * API.
6437 *
6438 * By default, a renderer is not using a shadow buffer of its own. Enabling
6439 * a shadow buffer may enable other color related features.
6440 *
6441 * Support depends on the chosen renderer and the graphics driver stack in use.
6442 *
6443 * \ingroup output
6444 */
6445WL_EXPORT bool
6446weston_output_set_renderer_shadow_buffer(struct weston_output *output)
6447{
6448 struct weston_compositor *compositor = output->compositor;
6449
6450 assert(!output->enabled);
6451
6452 if (compositor->capabilities & WESTON_CAP_COLOR_OPS) {
6453 output->use_renderer_shadow_buffer = true;
6454 return true;
6455 }
6456
6457 return false;
6458}
6459
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006460/** Initializes a weston_output object with enough data so
6461 ** an output can be configured.
6462 *
6463 * \param output The weston_output object to initialize
6464 * \param compositor The compositor instance.
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006465 * \param name Name for the output (the string is copied).
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006466 *
6467 * Sets initial values for fields that are expected to be
6468 * configured either by compositors or backends.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006469 *
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006470 * The name is used in logs, and can be used by compositors as a configuration
6471 * identifier.
6472 *
Marius Vlad55d87362019-06-11 01:15:35 +03006473 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006474 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006475 */
6476WL_EXPORT void
Armin Krezović40087402016-09-30 14:11:12 +02006477weston_output_init(struct weston_output *output,
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006478 struct weston_compositor *compositor,
6479 const char *name)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006480{
6481 output->compositor = compositor;
6482 output->destroying = 0;
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006483 output->name = strdup(name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006484 wl_list_init(&output->link);
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006485 wl_signal_init(&output->user_destroy_signal);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006486 output->enabled = false;
Ankit Nautiyal2690a772019-03-25 17:57:59 +05306487 output->desired_protection = WESTON_HDCP_DISABLE;
Ankit Nautiyal2844f8e2019-04-03 10:14:59 +05306488 output->allow_protection = true;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006489
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006490 wl_list_init(&output->head_list);
6491
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006492 /* Add some (in)sane defaults which can be used
6493 * for checking if an output was properly configured
6494 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006495 output->scale = 0;
6496 /* Can't use -1 on uint32_t and 0 is valid enum value */
6497 output->transform = UINT32_MAX;
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006498
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006499 pixman_region32_init(&output->region);
Pekka Paalanen42704142017-09-06 16:47:52 +03006500 wl_list_init(&output->mode_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006501}
6502
6503/** Adds weston_output object to pending output list.
6504 *
6505 * \param output The weston_output object to add
6506 * \param compositor The compositor instance.
6507 *
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03006508 * The opposite of this operation is built into weston_output_release().
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006509 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006510 * \ingroup compositor
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006511 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006512 */
6513WL_EXPORT void
6514weston_compositor_add_pending_output(struct weston_output *output,
6515 struct weston_compositor *compositor)
6516{
Pekka Paalanene952a012017-03-29 17:14:00 +03006517 assert(output->disable);
6518 assert(output->enable);
6519
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006520 wl_list_remove(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006521 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006522}
6523
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006524/** Create a string with the attached heads' names.
6525 *
6526 * The string must be free()'d.
Marius Vlad55d87362019-06-11 01:15:35 +03006527 *
6528 * \ingroup output
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006529 */
6530static char *
6531weston_output_create_heads_string(struct weston_output *output)
6532{
6533 FILE *fp;
6534 char *str = NULL;
6535 size_t size = 0;
6536 struct weston_head *head;
6537 const char *sep = "";
6538
6539 fp = open_memstream(&str, &size);
6540 if (!fp)
6541 return NULL;
6542
6543 wl_list_for_each(head, &output->head_list, output_link) {
6544 fprintf(fp, "%s%s", sep, head->name);
6545 sep = ", ";
6546 }
6547 fclose(fp);
6548
6549 return str;
6550}
6551
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006552/** Constructs a weston_output object that can be used by the compositor.
6553 *
Pekka Paalanencc201e42017-03-30 15:11:25 +03006554 * \param output The weston_output object that needs to be enabled. Must not
Pekka Paalanenddce54d2017-08-23 16:00:21 +03006555 * be enabled already. Must have at least one head attached.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006556 *
6557 * Output coordinates are calculated and each new output is by default
6558 * assigned to the right of previous one.
6559 *
6560 * Sets up the transformation, zoom, and geometry of the output using
6561 * the properties that need to be configured by the compositor.
6562 *
6563 * Establishes a repaint timer for the output with the relevant display
6564 * object's event loop. See output_repaint_timer_handler().
6565 *
6566 * The output is assigned an ID. Weston can support up to 32 distinct
6567 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
6568 * is referred to and used to find the first available ID number, and
6569 * then this ID is marked as used in output_id_pool.
6570 *
6571 * The output is also assigned a Wayland global with the wl_output
6572 * external interface.
6573 *
6574 * Backend specific function is called to set up the output output.
6575 *
6576 * Output is added to the compositor's output list
6577 *
6578 * If the backend specific function fails, the weston_output object
6579 * is returned to a state it was before calling this function and
6580 * is added to the compositor's pending_output_list in case it needs
6581 * to be reconfigured or just so it can be destroyed at shutdown.
6582 *
6583 * 0 is returned on success, -1 on failure.
Marius Vlad55d87362019-06-11 01:15:35 +03006584 *
6585 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006586 */
6587WL_EXPORT int
6588weston_output_enable(struct weston_output *output)
6589{
Armin Krezović782f5df2016-09-30 14:11:11 +02006590 struct weston_compositor *c = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006591 struct weston_output *iterator;
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03006592 struct weston_head *head;
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006593 char *head_names;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006594 int x = 0, y = 0;
6595
Pekka Paalanencc201e42017-03-30 15:11:25 +03006596 if (output->enabled) {
6597 weston_log("Error: attempt to enable an enabled output '%s'\n",
6598 output->name);
6599 return -1;
6600 }
6601
Pekka Paalanenddce54d2017-08-23 16:00:21 +03006602 if (wl_list_empty(&output->head_list)) {
6603 weston_log("Error: cannot enable output '%s' without heads.\n",
6604 output->name);
6605 return -1;
6606 }
6607
Pekka Paalanen586e1ac2017-09-14 16:17:59 +03006608 if (wl_list_empty(&output->mode_list) || !output->current_mode) {
6609 weston_log("Error: no video mode for output '%s'.\n",
6610 output->name);
6611 return -1;
6612 }
6613
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03006614 wl_list_for_each(head, &output->head_list, output_link) {
6615 assert(head->make);
6616 assert(head->model);
6617 }
6618
Armin Krezović782f5df2016-09-30 14:11:11 +02006619 iterator = container_of(c->output_list.prev,
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006620 struct weston_output, link);
6621
Armin Krezović782f5df2016-09-30 14:11:11 +02006622 if (!wl_list_empty(&c->output_list))
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006623 x = iterator->x + iterator->width;
6624
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006625 /* Make sure the scale is set up */
6626 assert(output->scale);
6627
6628 /* Make sure we have a transform set */
6629 assert(output->transform != UINT32_MAX);
6630
Armin Krezović782f5df2016-09-30 14:11:11 +02006631 output->x = x;
6632 output->y = y;
6633 output->dirty = 1;
6634 output->original_scale = output->scale;
6635
Marius Vlad3a2f8292019-11-04 17:53:46 +02006636 wl_signal_init(&output->frame_signal);
6637 wl_signal_init(&output->destroy_signal);
6638
Armin Krezović782f5df2016-09-30 14:11:11 +02006639 weston_output_transform_scale_init(output, output->transform, output->scale);
6640 weston_output_init_zoom(output);
6641
6642 weston_output_init_geometry(output, x, y);
6643 weston_output_damage(output);
6644
Armin Krezović782f5df2016-09-30 14:11:11 +02006645 wl_list_init(&output->animation_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02006646 wl_list_init(&output->feedback_list);
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03006647 wl_list_init(&output->paint_node_list);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03006648 wl_list_init(&output->paint_node_z_order_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02006649
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006650 /* Enable the output (set up the crtc or create a
6651 * window representing the output, set up the
6652 * renderer, etc)
6653 */
6654 if (output->enable(output) < 0) {
6655 weston_log("Enabling output \"%s\" failed.\n", output->name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006656 return -1;
6657 }
6658
6659 weston_compositor_add_output(output->compositor, output);
6660
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006661 head_names = weston_output_create_heads_string(output);
6662 weston_log("Output '%s' enabled with head(s) %s\n",
6663 output->name, head_names);
6664 free(head_names);
6665
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006666 return 0;
6667}
6668
6669/** Converts a weston_output object to a pending output state, so it
6670 ** can be configured again or destroyed.
6671 *
6672 * \param output The weston_output object that needs to be disabled.
6673 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006674 * Calls a backend specific function to disable an output, in case
6675 * such function exists.
6676 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03006677 * The backend specific disable function may choose to postpone the disabling
6678 * by returning a negative value, in which case this function returns early.
6679 * In that case the backend will guarantee the output will be disabled soon
6680 * by the backend calling this function again. One must not attempt to re-enable
6681 * the output until that happens.
6682 *
6683 * Otherwise, if the output is being used by the compositor, it is removed
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006684 * from weston's output_list (see weston_compositor_remove_output())
6685 * and is returned to a state it was before weston_output_enable()
6686 * was ran (see weston_output_enable_undo()).
6687 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03006688 * See weston_output_init() for more information on the
6689 * state output is returned to.
Pekka Paalanencc201e42017-03-30 15:11:25 +03006690 *
6691 * If the output has never been enabled yet, this function can still be
6692 * called to ensure that the output is actually turned off rather than left
6693 * in the state it was discovered in.
Marius Vlad55d87362019-06-11 01:15:35 +03006694 *
6695 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006696 */
6697WL_EXPORT void
6698weston_output_disable(struct weston_output *output)
6699{
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006700 /* Should we rename this? */
6701 output->destroying = 1;
6702
Pekka Paalanenc65df642017-03-29 15:45:46 +03006703 /* Disable is called unconditionally also for not-enabled outputs,
6704 * because at compositor start-up, if there is an output that is
6705 * already on but the compositor wants to turn it off, we have to
6706 * forward the turn-off to the backend so it knows to do it.
6707 * The backend cannot initially turn off everything, because it
6708 * would cause unnecessary mode-sets for all outputs the compositor
6709 * wants to be on.
6710 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006711 if (output->disable(output) < 0)
6712 return;
6713
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006714 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006715 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006716
6717 output->destroying = 0;
6718}
6719
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006720/** Forces a synchronous call to heads_changed hook
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006721 *
6722 * \param compositor The compositor instance
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006723 *
6724 * If there are new or changed heads, calls the heads_changed hook and
6725 * returns after the hook returns.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006726 *
6727 * \ingroup compositor
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006728 */
6729WL_EXPORT void
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006730weston_compositor_flush_heads_changed(struct weston_compositor *compositor)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006731{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03006732 if (compositor->heads_changed_source) {
6733 wl_event_source_remove(compositor->heads_changed_source);
6734 weston_compositor_call_heads_changed(compositor);
6735 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006736}
6737
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006738/** Add destroy callback for an output
6739 *
6740 * \param output The output to watch.
6741 * \param listener The listener to add. The \c notify member must be set.
6742 *
6743 * The listener callback will be called when user destroys an output. This
6744 * may be delayed by a backend in some cases. The main purpose of the
6745 * listener is to allow hooking up custom data to the output. The custom data
6746 * can be fetched via weston_output_get_destroy_listener() followed by
6747 * container_of().
6748 *
6749 * The \c data argument to the notify callback is the weston_output being
6750 * destroyed.
6751 *
6752 * @note This is for the final destruction of an output, not when it gets
6753 * disabled. If you want to keep track of enabled outputs, this is not it.
Marius Vlad55d87362019-06-11 01:15:35 +03006754 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05006755 * \ingroup output
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006756 */
6757WL_EXPORT void
6758weston_output_add_destroy_listener(struct weston_output *output,
6759 struct wl_listener *listener)
6760{
6761 wl_signal_add(&output->user_destroy_signal, listener);
6762}
6763
6764/** Look up destroy listener for an output
6765 *
6766 * \param output The output to query.
6767 * \param notify The notify function used used for the added destroy listener.
6768 * \return The listener, or NULL if not found.
6769 *
6770 * This looks up the previously added destroy listener struct based on the
6771 * notify function it has. The listener can be used to access user data
6772 * through \c container_of().
6773 *
6774 * \sa wl_signal_get() weston_output_add_destroy_listener()
Marius Vlad55d87362019-06-11 01:15:35 +03006775 * \ingroup output
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006776 */
6777WL_EXPORT struct wl_listener *
6778weston_output_get_destroy_listener(struct weston_output *output,
6779 wl_notify_func_t notify)
6780{
6781 return wl_signal_get(&output->user_destroy_signal, notify);
6782}
6783
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006784/** Uninitialize an output
6785 *
6786 * Removes the output from the list of enabled outputs if necessary, but
6787 * does not call the backend's output disable function. The output will no
6788 * longer be in the list of pending outputs either.
6789 *
6790 * All fields of weston_output become uninitialized, i.e. should not be used
6791 * anymore. The caller can free the memory after this.
6792 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05006793 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006794 * \internal
6795 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006796WL_EXPORT void
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03006797weston_output_release(struct weston_output *output)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006798{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006799 struct weston_head *head, *tmp;
6800
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006801 output->destroying = 1;
6802
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006803 wl_signal_emit(&output->user_destroy_signal, output);
6804
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03006805 if (output->idle_repaint_source)
6806 wl_event_source_remove(output->idle_repaint_source);
6807
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006808 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006809 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006810
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03006811 assert(wl_list_empty(&output->paint_node_list));
6812
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006813 pixman_region32_fini(&output->region);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006814 wl_list_remove(&output->link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006815
6816 wl_list_for_each_safe(head, tmp, &output->head_list, output_link)
6817 weston_head_detach(head);
6818
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006819 free(output->name);
6820}
6821
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006822/** Find an output by its given name
6823 *
6824 * \param compositor The compositor to search in.
6825 * \param name The output name to search for.
6826 * \return An existing output with the given name, or NULL if not found.
6827 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006828 * \ingroup compositor
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006829 */
6830WL_EXPORT struct weston_output *
6831weston_compositor_find_output_by_name(struct weston_compositor *compositor,
6832 const char *name)
6833{
6834 struct weston_output *output;
6835
6836 wl_list_for_each(output, &compositor->output_list, link)
6837 if (strcmp(output->name, name) == 0)
6838 return output;
6839
6840 wl_list_for_each(output, &compositor->pending_output_list, link)
6841 if (strcmp(output->name, name) == 0)
6842 return output;
6843
6844 return NULL;
6845}
6846
6847/** Create a named output
6848 *
6849 * \param compositor The compositor.
6850 * \param name The name for the output.
6851 * \return A new \c weston_output, or NULL on failure.
6852 *
6853 * This creates a new weston_output that starts with no heads attached.
6854 *
6855 * An output must be configured and it must have at least one head before
6856 * it can be enabled.
6857 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006858 * \ingroup compositor
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006859 */
6860WL_EXPORT struct weston_output *
6861weston_compositor_create_output(struct weston_compositor *compositor,
6862 const char *name)
6863{
6864 assert(compositor->backend->create_output);
6865
6866 if (weston_compositor_find_output_by_name(compositor, name)) {
6867 weston_log("Warning: attempted to create an output with a "
6868 "duplicate name '%s'.\n", name);
6869 return NULL;
6870 }
6871
6872 return compositor->backend->create_output(compositor, name);
6873}
6874
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006875/** Create an output for an unused head
6876 *
6877 * \param compositor The compositor.
6878 * \param head The head to attach to the output.
6879 * \return A new \c weston_output, or NULL on failure.
6880 *
6881 * This creates a new weston_output that starts with the given head attached.
6882 * The output inherits the name of the head. The head must not be already
6883 * attached to another output.
6884 *
6885 * An output must be configured before it can be enabled.
6886 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006887 * \ingroup compositor
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006888 */
6889WL_EXPORT struct weston_output *
6890weston_compositor_create_output_with_head(struct weston_compositor *compositor,
6891 struct weston_head *head)
6892{
6893 struct weston_output *output;
6894
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006895 output = weston_compositor_create_output(compositor, head->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006896 if (!output)
6897 return NULL;
6898
6899 if (weston_output_attach_head(output, head) < 0) {
Pekka Paalanen42c0e142017-10-27 12:07:49 +03006900 weston_output_destroy(output);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006901 return NULL;
6902 }
6903
6904 return output;
6905}
6906
6907/** Destroy an output
6908 *
6909 * \param output The output to destroy.
6910 *
6911 * The heads attached to the given output are detached and become unused again.
6912 *
6913 * It is not necessary to explicitly destroy all outputs at compositor exit.
Leandro Ribeiroca640d52020-01-27 19:12:01 -03006914 * weston_compositor_destroy() will automatically destroy any remaining
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006915 * outputs.
6916 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05006917 * \ingroup output
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006918 */
6919WL_EXPORT void
6920weston_output_destroy(struct weston_output *output)
6921{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006922 output->destroy(output);
6923}
6924
Pekka Paalanencf0a4762017-04-04 16:36:07 +03006925/** When you need a head...
6926 *
6927 * This function is a hack, used until all code has been converted to become
6928 * multi-head aware.
6929 *
6930 * \param output The weston_output whose head to get.
6931 * \return The first head in the output's list.
Marius Vlad55d87362019-06-11 01:15:35 +03006932 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05006933 * \ingroup output
Pekka Paalanencf0a4762017-04-04 16:36:07 +03006934 */
6935WL_EXPORT struct weston_head *
6936weston_output_get_first_head(struct weston_output *output)
6937{
6938 if (wl_list_empty(&output->head_list))
6939 return NULL;
6940
6941 return container_of(output->head_list.next,
6942 struct weston_head, output_link);
6943}
6944
Ankit Nautiyal2844f8e2019-04-03 10:14:59 +05306945/** Allow/Disallow content-protection support for an output
6946 *
6947 * This function sets the allow_protection member for an output. Setting of
6948 * this field will allow the compositor to attempt content-protection for this
6949 * output, for a backend that supports the content-protection protocol.
6950 *
6951 * \param output The weston_output for whom the content-protection is to be
6952 * allowed.
6953 * \param allow_protection The bool value which is to be set.
6954 */
6955WL_EXPORT void
6956weston_output_allow_protection(struct weston_output *output,
6957 bool allow_protection)
6958{
6959 output->allow_protection = allow_protection;
6960}
6961
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01006962static void
Roman Gilge97391c2019-03-29 13:01:06 +01006963xdg_output_unlist(struct wl_resource *resource)
6964{
6965 wl_list_remove(wl_resource_get_link(resource));
6966}
6967
6968static void
6969xdg_output_destroy(struct wl_client *client, struct wl_resource *resource)
6970{
6971 wl_resource_destroy(resource);
6972}
6973
6974static const struct zxdg_output_v1_interface xdg_output_interface = {
6975 xdg_output_destroy
6976};
6977
6978static void
6979xdg_output_manager_destroy(struct wl_client *client,
6980 struct wl_resource *resource)
6981{
6982 wl_resource_destroy(resource);
6983}
6984
6985static void
6986xdg_output_manager_get_xdg_output(struct wl_client *client,
6987 struct wl_resource *manager,
6988 uint32_t id,
6989 struct wl_resource *output_resource)
6990{
6991 int version = wl_resource_get_version(manager);
6992 struct weston_head *head = wl_resource_get_user_data(output_resource);
6993 struct weston_output *output = head->output;
6994 struct wl_resource *resource;
6995
6996 resource = wl_resource_create(client, &zxdg_output_v1_interface,
6997 version, id);
6998 if (resource == NULL) {
6999 wl_client_post_no_memory(client);
7000 return;
7001 }
7002
7003 wl_list_insert(&head->xdg_output_resource_list,
7004 wl_resource_get_link(resource));
7005
7006 wl_resource_set_implementation(resource, &xdg_output_interface,
7007 NULL, xdg_output_unlist);
7008
7009 zxdg_output_v1_send_logical_position(resource, output->x, output->y);
7010 zxdg_output_v1_send_logical_size(resource,
7011 output->width,
7012 output->height);
7013 if (version >= ZXDG_OUTPUT_V1_NAME_SINCE_VERSION)
7014 zxdg_output_v1_send_name(resource, head->name);
7015
7016 zxdg_output_v1_send_done(resource);
7017}
7018
7019static const struct zxdg_output_manager_v1_interface xdg_output_manager_interface = {
7020 xdg_output_manager_destroy,
7021 xdg_output_manager_get_xdg_output
7022};
7023
7024static void
7025bind_xdg_output_manager(struct wl_client *client,
7026 void *data, uint32_t version, uint32_t id)
7027{
7028 struct wl_resource *resource;
7029
7030 resource = wl_resource_create(client, &zxdg_output_manager_v1_interface,
7031 version, id);
7032 if (resource == NULL) {
7033 wl_client_post_no_memory(client);
7034 return;
7035 }
7036
7037 wl_resource_set_implementation(resource, &xdg_output_manager_interface,
7038 NULL, NULL);
7039}
7040
7041static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007042destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01007043{
Jonny Lamb74130762013-11-26 18:19:46 +01007044 struct weston_surface *surface =
7045 wl_resource_get_user_data(resource);
7046
Pekka Paalanen4826f872016-04-22 14:14:38 +03007047 if (!surface)
7048 return;
7049
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007050 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02007051 surface->pending.buffer_viewport.buffer.src_width =
7052 wl_fixed_from_int(-1);
7053 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02007054 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01007055}
7056
7057static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007058viewport_destroy(struct wl_client *client,
7059 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01007060{
7061 wl_resource_destroy(resource);
7062}
7063
7064static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007065viewport_set_source(struct wl_client *client,
7066 struct wl_resource *resource,
7067 wl_fixed_t src_x,
7068 wl_fixed_t src_y,
7069 wl_fixed_t src_width,
7070 wl_fixed_t src_height)
7071{
7072 struct weston_surface *surface =
7073 wl_resource_get_user_data(resource);
7074
Pekka Paalanen4826f872016-04-22 14:14:38 +03007075 if (!surface) {
7076 wl_resource_post_error(resource,
7077 WP_VIEWPORT_ERROR_NO_SURFACE,
7078 "wl_surface for this viewport is no longer exists");
7079 return;
7080 }
7081
7082 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03007083 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007084
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007085 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03007086 src_height == wl_fixed_from_int(-1) &&
7087 src_x == wl_fixed_from_int(-1) &&
7088 src_y == wl_fixed_from_int(-1)) {
7089 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007090 surface->pending.buffer_viewport.buffer.src_width =
7091 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02007092 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007093 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007094 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007095
Pekka Paalanen201769a2016-04-26 14:42:11 +03007096 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007097 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007098 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03007099 "wl_surface@%d viewport source "
7100 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
7101 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007102 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03007103 wl_fixed_to_double(src_height),
7104 wl_fixed_to_double(src_x),
7105 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007106 return;
7107 }
7108
7109 surface->pending.buffer_viewport.buffer.src_x = src_x;
7110 surface->pending.buffer_viewport.buffer.src_y = src_y;
7111 surface->pending.buffer_viewport.buffer.src_width = src_width;
7112 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02007113 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007114}
7115
7116static void
7117viewport_set_destination(struct wl_client *client,
7118 struct wl_resource *resource,
7119 int32_t dst_width,
7120 int32_t dst_height)
7121{
7122 struct weston_surface *surface =
7123 wl_resource_get_user_data(resource);
7124
Pekka Paalanen4826f872016-04-22 14:14:38 +03007125 if (!surface) {
7126 wl_resource_post_error(resource,
7127 WP_VIEWPORT_ERROR_NO_SURFACE,
7128 "wl_surface for this viewport no longer exists");
7129 return;
7130 }
7131
7132 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007133
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007134 if (dst_width == -1 && dst_height == -1) {
7135 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007136 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02007137 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007138 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007139 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007140
7141 if (dst_width <= 0 || dst_height <= 0) {
7142 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007143 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007144 "destination size must be positive (%dx%d)",
7145 dst_width, dst_height);
7146 return;
7147 }
7148
7149 surface->pending.buffer_viewport.surface.width = dst_width;
7150 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02007151 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007152}
7153
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007154static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007155 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007156 viewport_set_source,
7157 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01007158};
7159
7160static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007161viewporter_destroy(struct wl_client *client,
7162 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01007163{
7164 wl_resource_destroy(resource);
7165}
7166
7167static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007168viewporter_get_viewport(struct wl_client *client,
7169 struct wl_resource *viewporter,
7170 uint32_t id,
7171 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01007172{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007173 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007174 struct weston_surface *surface =
7175 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01007176 struct wl_resource *resource;
7177
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007178 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007179 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007180 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007181 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01007182 return;
7183 }
7184
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007185 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007186 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01007187 if (resource == NULL) {
7188 wl_client_post_no_memory(client);
7189 return;
7190 }
7191
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007192 wl_resource_set_implementation(resource, &viewport_interface,
7193 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01007194
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007195 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01007196}
7197
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007198static const struct wp_viewporter_interface viewporter_interface = {
7199 viewporter_destroy,
7200 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01007201};
7202
7203static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007204bind_viewporter(struct wl_client *client,
7205 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01007206{
7207 struct wl_resource *resource;
7208
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007209 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06007210 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01007211 if (resource == NULL) {
7212 wl_client_post_no_memory(client);
7213 return;
7214 }
7215
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007216 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01007217 NULL, NULL);
7218}
7219
7220static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04007221destroy_presentation_feedback(struct wl_resource *feedback_resource)
7222{
7223 struct weston_presentation_feedback *feedback;
7224
7225 feedback = wl_resource_get_user_data(feedback_resource);
7226
7227 wl_list_remove(&feedback->link);
7228 free(feedback);
7229}
7230
7231static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007232presentation_destroy(struct wl_client *client, struct wl_resource *resource)
7233{
7234 wl_resource_destroy(resource);
7235}
7236
7237static void
7238presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04007239 struct wl_resource *presentation_resource,
7240 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007241 uint32_t callback)
7242{
Pekka Paalanen133e4392014-09-23 22:08:46 -04007243 struct weston_surface *surface;
7244 struct weston_presentation_feedback *feedback;
7245
7246 surface = wl_resource_get_user_data(surface_resource);
7247
Bryce Harringtonde16d892014-11-20 22:21:57 -08007248 feedback = zalloc(sizeof *feedback);
7249 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04007250 goto err_calloc;
7251
7252 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007253 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04007254 1, callback);
7255 if (!feedback->resource)
7256 goto err_create;
7257
7258 wl_resource_set_implementation(feedback->resource, NULL, feedback,
7259 destroy_presentation_feedback);
7260
7261 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
7262
7263 return;
7264
7265err_create:
7266 free(feedback);
7267
7268err_calloc:
7269 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007270}
7271
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007272static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007273 presentation_destroy,
7274 presentation_feedback
7275};
7276
7277static void
7278bind_presentation(struct wl_client *client,
7279 void *data, uint32_t version, uint32_t id)
7280{
7281 struct weston_compositor *compositor = data;
7282 struct wl_resource *resource;
7283
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007284 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06007285 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007286 if (resource == NULL) {
7287 wl_client_post_no_memory(client);
7288 return;
7289 }
7290
7291 wl_resource_set_implementation(resource, &presentation_implementation,
7292 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007293 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007294}
7295
7296static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05007297compositor_bind(struct wl_client *client,
7298 void *data, uint32_t version, uint32_t id)
7299{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007300 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05007301 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05007302
Jason Ekstranda85118c2013-06-27 20:17:02 -05007303 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06007304 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07007305 if (resource == NULL) {
7306 wl_client_post_no_memory(client);
7307 return;
7308 }
7309
7310 wl_resource_set_implementation(resource, &compositor_interface,
7311 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05007312}
7313
Daniel Stonece62cb32018-07-20 09:46:24 +01007314static const char *
7315output_repaint_status_text(struct weston_output *output)
7316{
7317 switch (output->repaint_status) {
7318 case REPAINT_NOT_SCHEDULED:
7319 return "no repaint";
7320 case REPAINT_BEGIN_FROM_IDLE:
7321 return "start_repaint_loop scheduled";
7322 case REPAINT_SCHEDULED:
7323 return "repaint scheduled";
7324 case REPAINT_AWAITING_COMPLETION:
7325 return "awaiting completion";
7326 }
7327
7328 assert(!"output_repaint_status_text missing enum");
7329 return NULL;
7330}
7331
7332static void
7333debug_scene_view_print_buffer(FILE *fp, struct weston_view *view)
7334{
7335 struct weston_buffer *buffer = view->surface->buffer_ref.buffer;
7336 struct wl_shm_buffer *shm;
7337 struct linux_dmabuf_buffer *dmabuf;
Marius Vlad00a6e012018-11-20 17:52:31 +02007338 const struct pixel_format_info *pixel_info = NULL;
Daniel Stonece62cb32018-07-20 09:46:24 +01007339
7340 if (!buffer) {
7341 fprintf(fp, "\t\t[buffer not available]\n");
7342 return;
7343 }
7344
7345 shm = wl_shm_buffer_get(buffer->resource);
7346 if (shm) {
Marius Vlad00a6e012018-11-20 17:52:31 +02007347 uint32_t _format = wl_shm_buffer_get_format(shm);
7348 pixel_info = pixel_format_get_info_shm(_format);
Daniel Stonece62cb32018-07-20 09:46:24 +01007349 fprintf(fp, "\t\tSHM buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02007350 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
7351 (unsigned long) _format,
7352 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Daniel Stonece62cb32018-07-20 09:46:24 +01007353 return;
7354 }
7355
7356 dmabuf = linux_dmabuf_buffer_get(buffer->resource);
7357 if (dmabuf) {
Marius Vlad00a6e012018-11-20 17:52:31 +02007358 pixel_info = pixel_format_get_info(dmabuf->attributes.format);
Daniel Stonece62cb32018-07-20 09:46:24 +01007359 fprintf(fp, "\t\tdmabuf buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02007360 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
7361 (unsigned long) dmabuf->attributes.format,
7362 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Daniel Stonece62cb32018-07-20 09:46:24 +01007363 fprintf(fp, "\t\t\tmodifier: 0x%llx\n",
7364 (unsigned long long) dmabuf->attributes.modifier[0]);
7365 return;
7366 }
7367
Marius Vlad253ba9a2019-03-17 18:22:21 +02007368 fprintf(fp, "\t\tEGL buffer\n");
Daniel Stonece62cb32018-07-20 09:46:24 +01007369}
7370
7371static void
7372debug_scene_view_print(FILE *fp, struct weston_view *view, int view_idx)
7373{
7374 struct weston_compositor *ec = view->surface->compositor;
7375 struct weston_output *output;
7376 char desc[512];
7377 pixman_box32_t *box;
7378 uint32_t surface_id = 0;
7379 pid_t pid = 0;
7380
7381 if (view->surface->resource) {
7382 struct wl_resource *resource = view->surface->resource;
7383 wl_client_get_credentials(wl_resource_get_client(resource),
7384 &pid, NULL, NULL);
7385 surface_id = wl_resource_get_id(view->surface->resource);
7386 }
7387
7388 if (!view->surface->get_label ||
7389 view->surface->get_label(view->surface, desc, sizeof(desc)) < 0) {
7390 strcpy(desc, "[no description available]");
7391 }
7392 fprintf(fp, "\tView %d (role %s, PID %d, surface ID %u, %s, %p):\n",
7393 view_idx, view->surface->role_name, pid, surface_id,
7394 desc, view);
7395
7396 box = pixman_region32_extents(&view->transform.boundingbox);
7397 fprintf(fp, "\t\tposition: (%d, %d) -> (%d, %d)\n",
7398 box->x1, box->y1, box->x2, box->y2);
7399 box = pixman_region32_extents(&view->transform.opaque);
7400
Michael Olbrichb7e5f102020-08-11 16:33:35 +02007401 if (weston_view_is_opaque(view, &view->transform.boundingbox)) {
Daniel Stonece62cb32018-07-20 09:46:24 +01007402 fprintf(fp, "\t\t[fully opaque]\n");
7403 } else if (!pixman_region32_not_empty(&view->transform.opaque)) {
7404 fprintf(fp, "\t\t[not opaque]\n");
7405 } else {
7406 fprintf(fp, "\t\t[opaque: (%d, %d) -> (%d, %d)]\n",
7407 box->x1, box->y1, box->x2, box->y2);
7408 }
7409
7410 if (view->alpha < 1.0)
7411 fprintf(fp, "\t\talpha: %f\n", view->alpha);
7412
7413 if (view->output_mask != 0) {
7414 bool first_output = true;
7415 fprintf(fp, "\t\toutputs: ");
7416 wl_list_for_each(output, &ec->output_list, link) {
7417 if (!(view->output_mask & (1 << output->id)))
7418 continue;
7419 fprintf(fp, "%s%d (%s)%s",
7420 (first_output) ? "" : ", ",
7421 output->id, output->name,
7422 (view->output == output) ? " (primary)" : "");
7423 first_output = false;
7424 }
7425 } else {
7426 fprintf(fp, "\t\t[no outputs]");
7427 }
7428
7429 fprintf(fp, "\n");
7430
7431 debug_scene_view_print_buffer(fp, view);
7432}
7433
Marius Vlad433f4e72019-02-17 22:14:23 +02007434static void
7435debug_scene_view_print_tree(struct weston_view *view,
Marius Vlada6acfa82019-03-17 18:10:09 +02007436 FILE *fp, int *view_idx)
Marius Vlad433f4e72019-02-17 22:14:23 +02007437{
7438 struct weston_subsurface *sub;
7439 struct weston_view *ev;
7440
7441 /*
7442 * print the view first, then we recursively go on printing
7443 * sub-surfaces. We bail out once no more sub-surfaces are available.
7444 */
Marius Vlada6acfa82019-03-17 18:10:09 +02007445 debug_scene_view_print(fp, view, *view_idx);
Marius Vlad433f4e72019-02-17 22:14:23 +02007446
7447 /* no more sub-surfaces */
7448 if (wl_list_empty(&view->surface->subsurface_list))
7449 return;
7450
7451 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
7452 wl_list_for_each(ev, &sub->surface->views, surface_link) {
Michael Olbrichef5f3232020-04-29 09:03:15 +02007453 /* only print the child views of the current view */
7454 if (ev->parent_view != view)
Marius Vlad433f4e72019-02-17 22:14:23 +02007455 continue;
Marius Vlada6acfa82019-03-17 18:10:09 +02007456
7457 (*view_idx)++;
Marius Vlad433f4e72019-02-17 22:14:23 +02007458 debug_scene_view_print_tree(ev, fp, view_idx);
7459 }
7460 }
7461}
7462
Daniel Stonece62cb32018-07-20 09:46:24 +01007463/**
7464 * Output information on how libweston is currently composing the scene
7465 * graph.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007466 *
7467 * \ingroup compositor
Daniel Stonece62cb32018-07-20 09:46:24 +01007468 */
7469WL_EXPORT char *
7470weston_compositor_print_scene_graph(struct weston_compositor *ec)
7471{
7472 struct weston_output *output;
7473 struct weston_layer *layer;
7474 struct timespec now;
7475 int layer_idx = 0;
7476 FILE *fp;
7477 char *ret;
7478 size_t len;
7479 int err;
7480
7481 fp = open_memstream(&ret, &len);
7482 assert(fp);
7483
7484 weston_compositor_read_presentation_clock(ec, &now);
7485 fprintf(fp, "Weston scene graph at %ld.%09ld:\n\n",
7486 now.tv_sec, now.tv_nsec);
7487
7488 wl_list_for_each(output, &ec->output_list, link) {
7489 struct weston_head *head;
7490 int head_idx = 0;
7491
7492 fprintf(fp, "Output %d (%s):\n", output->id, output->name);
7493 assert(output->enabled);
7494
7495 fprintf(fp, "\tposition: (%d, %d) -> (%d, %d)\n",
7496 output->x, output->y,
7497 output->x + output->width,
7498 output->y + output->height);
7499 fprintf(fp, "\tmode: %dx%d@%.3fHz\n",
7500 output->current_mode->width,
7501 output->current_mode->height,
7502 output->current_mode->refresh / 1000.0);
7503 fprintf(fp, "\tscale: %d\n", output->scale);
7504
7505 fprintf(fp, "\trepaint status: %s\n",
7506 output_repaint_status_text(output));
7507 if (output->repaint_status == REPAINT_SCHEDULED)
7508 fprintf(fp, "\tnext repaint: %ld.%09ld\n",
7509 output->next_repaint.tv_sec,
7510 output->next_repaint.tv_nsec);
7511
7512 wl_list_for_each(head, &output->head_list, output_link) {
7513 fprintf(fp, "\tHead %d (%s): %sconnected\n",
7514 head_idx++, head->name,
7515 (head->connected) ? "" : "not ");
7516 }
7517 }
7518
7519 fprintf(fp, "\n");
7520
7521 wl_list_for_each(layer, &ec->layer_list, link) {
7522 struct weston_view *view;
7523 int view_idx = 0;
7524
7525 fprintf(fp, "Layer %d (pos 0x%lx):\n", layer_idx++,
7526 (unsigned long) layer->position);
7527
7528 if (!weston_layer_mask_is_infinite(layer)) {
7529 fprintf(fp, "\t[mask: (%d, %d) -> (%d,%d)]\n\n",
7530 layer->mask.x1, layer->mask.y1,
7531 layer->mask.x2, layer->mask.y2);
7532 }
7533
Marius Vlada6acfa82019-03-17 18:10:09 +02007534 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
7535 debug_scene_view_print_tree(view, fp, &view_idx);
7536 view_idx++;
7537 }
Daniel Stonece62cb32018-07-20 09:46:24 +01007538
7539 if (wl_list_empty(&layer->view_list.link))
7540 fprintf(fp, "\t[no views]\n");
7541
7542 fprintf(fp, "\n");
7543 }
7544
7545 err = fclose(fp);
7546 assert(err == 0);
7547
7548 return ret;
7549}
7550
7551/**
7552 * Called when the 'scene-graph' debug scope is bound by a client. This
7553 * one-shot weston-debug scope prints the current scene graph when bound,
7554 * and then terminates the stream.
7555 */
7556static void
Marius Vladdad882a2019-07-17 15:43:53 +03007557debug_scene_graph_cb(struct weston_log_subscription *sub, void *data)
Daniel Stonece62cb32018-07-20 09:46:24 +01007558{
7559 struct weston_compositor *ec = data;
7560 char *str = weston_compositor_print_scene_graph(ec);
7561
Marius Vladdad882a2019-07-17 15:43:53 +03007562 weston_log_subscription_printf(sub, "%s", str);
Daniel Stonece62cb32018-07-20 09:46:24 +01007563 free(str);
Marius Vladdad882a2019-07-17 15:43:53 +03007564 weston_log_subscription_complete(sub);
Daniel Stonece62cb32018-07-20 09:46:24 +01007565}
7566
Leandro Ribeiro32a5acd2020-10-19 16:06:22 -03007567/** Retrieve testsuite data from compositor
7568 *
7569 * The testsuite data can be defined by the test suite of projects that uses
7570 * libweston and given to the compositor at the moment of its creation. This
7571 * function should be used when we need to retrieve the testsuite private data
7572 * from the compositor.
7573 *
7574 * \param ec The weston compositor.
7575 * \return The testsuite data.
7576 *
7577 * \ingroup compositor
7578 * \sa weston_compositor_test_data_init
7579 */
7580WL_EXPORT void *
7581weston_compositor_get_test_data(struct weston_compositor *ec)
7582{
7583 return ec->test_data.test_private_data;
7584}
7585
Giulio Camuffo459137b2014-10-11 23:56:24 +03007586/** Create the compositor.
7587 *
7588 * This functions creates and initializes a compositor instance.
7589 *
7590 * \param display The Wayland display to be used.
7591 * \param user_data A pointer to an object that can later be retrieved
Marius Vlada2dace22019-06-12 16:05:44 +03007592 * \param log_ctx A pointer to weston_debug_compositor
Leandro Ribeirocdb72182020-11-06 11:24:22 -03007593 * \param test_data Optional testsuite data, or NULL.
Giulio Camuffo459137b2014-10-11 23:56:24 +03007594 * using the \ref weston_compositor_get_user_data function.
7595 * \return The compositor instance on success or NULL on failure.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007596 *
7597 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007598 */
7599WL_EXPORT struct weston_compositor *
Marius Vlad880b4852019-04-07 17:07:58 +03007600weston_compositor_create(struct wl_display *display,
Leandro Ribeirocdb72182020-11-06 11:24:22 -03007601 struct weston_log_context *log_ctx, void *user_data,
7602 const struct weston_testsuite_data *test_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04007603{
Giulio Camuffo459137b2014-10-11 23:56:24 +03007604 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05007605 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07007606
Leandro Ribeirobd9c0a62020-01-17 10:47:49 -03007607 if (!log_ctx)
7608 return NULL;
7609
Giulio Camuffo459137b2014-10-11 23:56:24 +03007610 ec = zalloc(sizeof *ec);
7611 if (!ec)
7612 return NULL;
7613
Leandro Ribeirocdb72182020-11-06 11:24:22 -03007614 if (test_data)
7615 ec->test_data = *test_data;
7616
Leandro Ribeirobd9c0a62020-01-17 10:47:49 -03007617 ec->weston_log_ctx = log_ctx;
Giulio Camuffo459137b2014-10-11 23:56:24 +03007618 ec->wl_display = display;
7619 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04007620 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07007621 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04007622 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03007623 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03007624 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02007625 wl_signal_init(&ec->idle_signal);
7626 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02007627 wl_signal_init(&ec->show_input_panel_signal);
7628 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02007629 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01007630 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01007631 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02007632 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02007633 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02007634 wl_signal_init(&ec->output_resized_signal);
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03007635 wl_signal_init(&ec->heads_changed_signal);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02007636 wl_signal_init(&ec->output_heads_changed_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07007637 wl_signal_init(&ec->session_signal);
Robert Beckettc569bdc2019-07-10 16:40:12 +01007638 ec->session_active = true;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04007639
Casey Dahlin58ba1372012-04-19 22:50:08 -04007640 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03007641 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04007642
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02007643 ec->activate_serial = 1;
7644
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05007645 ec->touch_mode = WESTON_TOUCH_MODE_NORMAL;
7646
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05307647 ec->content_protection = NULL;
7648
Derek Foreman152254b2015-11-26 14:17:48 -06007649 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04007650 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007651 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05007652
Giulio Camuffo954f1832014-10-11 18:27:30 +03007653 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04007654 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007655 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03007656
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007657 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007658 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007659 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01007660
Roman Gilge97391c2019-03-29 13:01:06 +01007661 if (!wl_global_create(ec->wl_display, &zxdg_output_manager_v1_interface, 2,
7662 ec, bind_xdg_output_manager))
7663 goto fail;
7664
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007665 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007666 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007667 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007668
Jonas Ådahl30d61d82014-10-22 21:21:17 +02007669 if (weston_input_init(ec) != 0)
7670 goto fail;
7671
Jason Ekstranda7af7042013-10-12 22:38:11 -05007672 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02007673 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007674 wl_list_init(&ec->layer_list);
7675 wl_list_init(&ec->seat_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007676 wl_list_init(&ec->pending_output_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007677 wl_list_init(&ec->output_list);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03007678 wl_list_init(&ec->head_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007679 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01007680 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007681 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01007682 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007683 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02007684 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007685
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007686 wl_list_init(&ec->plugin_api_list);
7687
Xiong Zhang97116532013-10-23 13:58:31 +08007688 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02007689 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04007690
Giulio Camuffo459137b2014-10-11 23:56:24 +03007691 wl_data_device_manager_init(ec->wl_display);
7692
7693 wl_display_init_shm(ec->wl_display);
7694
7695 loop = wl_display_get_event_loop(ec->wl_display);
7696 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Daniel Stone6847b852017-03-01 11:34:08 +00007697 ec->repaint_timer =
7698 wl_event_loop_add_timer(loop, output_repaint_timer_handler,
7699 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007700
Quentin Glidic82681572016-12-17 13:40:51 +01007701 weston_layer_init(&ec->fade_layer, ec);
7702 weston_layer_init(&ec->cursor_layer, ec);
7703
7704 weston_layer_set_position(&ec->fade_layer, WESTON_LAYER_POSITION_FADE);
7705 weston_layer_set_position(&ec->cursor_layer,
7706 WESTON_LAYER_POSITION_CURSOR);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007707
Daniel Stonece62cb32018-07-20 09:46:24 +01007708 ec->debug_scene =
Leandro Ribeiroce100192019-12-26 16:35:49 -03007709 weston_compositor_add_log_scope(ec, "scene-graph",
7710 "Scene graph details\n",
7711 debug_scene_graph_cb, NULL,
7712 ec);
Daniel Stonece62cb32018-07-20 09:46:24 +01007713
Marius Vladda104eb2019-09-05 14:31:01 +03007714 ec->timeline =
Leandro Ribeiroce100192019-12-26 16:35:49 -03007715 weston_compositor_add_log_scope(ec, "timeline",
7716 "Timeline event points\n",
7717 weston_timeline_create_subscription,
7718 weston_timeline_destroy_subscription,
7719 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007720 return ec;
7721
7722fail:
7723 free(ec);
7724 return NULL;
7725}
7726
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007727/** weston_compositor_shutdown
7728 * \ingroup compositor
7729 */
Benjamin Franzkeb8263022011-08-30 11:32:47 +02007730WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007731weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07007732{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007733 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07007734
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007735 wl_event_source_remove(ec->idle_source);
Lujin Wangd4b46cd2019-08-12 12:03:29 -07007736 wl_event_source_remove(ec->repaint_timer);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007737
Matt Roper361d2ad2011-08-29 13:52:23 -07007738 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02007739 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07007740 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02007741
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007742 /* Destroy all pending outputs associated with this compositor */
7743 wl_list_for_each_safe(output, next, &ec->pending_output_list, link)
7744 output->destroy(output);
7745
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02007746 if (ec->renderer)
7747 ec->renderer->destroy(ec);
7748
Daniel Stone325fc2d2012-05-30 16:31:58 +01007749 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09007750 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01007751 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01007752 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01007753 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02007754 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007755
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04007756 weston_plane_release(&ec->primary_plane);
Pekka Paalanen87400372021-05-14 14:29:40 +03007757
7758 weston_layer_fini(&ec->fade_layer);
7759 weston_layer_fini(&ec->cursor_layer);
7760
7761 if (!wl_list_empty(&ec->layer_list))
7762 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 -07007763}
7764
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007765/** weston_compositor_exit_with_code
7766 * \ingroup compositor
7767 */
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05007768WL_EXPORT void
Adam Jackson3c3f3b12019-10-16 16:02:59 -04007769weston_compositor_exit_with_code(struct weston_compositor *compositor,
Frederic Plourdec336f062014-10-29 14:44:33 -04007770 int exit_code)
7771{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02007772 if (compositor->exit_code == EXIT_SUCCESS)
7773 compositor->exit_code = exit_code;
7774
Giulio Camuffo459137b2014-10-11 23:56:24 +03007775 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04007776}
7777
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007778/** weston_compositor_set_default_pointer_grab
7779 * \ingroup compositor
7780 */
Frederic Plourdec336f062014-10-29 14:44:33 -04007781WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007782weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
7783 const struct weston_pointer_grab_interface *interface)
7784{
7785 struct weston_seat *seat;
7786
7787 ec->default_pointer_grab = interface;
7788 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05007789 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
7790
7791 if (pointer)
7792 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007793 }
7794}
7795
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007796/** weston_compositor_set_presentation_clock
7797 * \ingroup compositor
7798 */
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007799WL_EXPORT int
7800weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
7801 clockid_t clk_id)
7802{
7803 struct timespec ts;
7804
7805 if (clock_gettime(clk_id, &ts) < 0)
7806 return -1;
7807
7808 compositor->presentation_clock = clk_id;
7809
7810 return 0;
7811}
7812
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007813/** For choosing the software clock, when the display hardware or API
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007814 * does not expose a compatible presentation timestamp.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007815 *
7816 * \ingroup compositor
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007817 */
7818WL_EXPORT int
7819weston_compositor_set_presentation_clock_software(
7820 struct weston_compositor *compositor)
7821{
7822 /* In order of preference */
7823 static const clockid_t clocks[] = {
7824 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
7825 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
7826 CLOCK_MONOTONIC, /* no jumps, may crawl */
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007827 };
7828 unsigned i;
7829
7830 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
7831 if (weston_compositor_set_presentation_clock(compositor,
7832 clocks[i]) == 0)
7833 return 0;
7834
7835 weston_log("Error: no suitable presentation clock available.\n");
7836
7837 return -1;
7838}
7839
Pekka Paalanen662f3842015-03-18 12:17:26 +02007840/** Read the current time from the Presentation clock
7841 *
7842 * \param compositor
Marius Vlada2dace22019-06-12 16:05:44 +03007843 * \param[out] ts The current time.
Pekka Paalanen662f3842015-03-18 12:17:26 +02007844 *
7845 * \note Reading the current time in user space is always imprecise to some
7846 * degree.
7847 *
7848 * This function is never meant to fail. If reading the clock does fail,
7849 * an error message is logged and a zero time is returned. Callers are not
7850 * supposed to detect or react to failures.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007851 *
7852 * \ingroup compositor
Pekka Paalanen662f3842015-03-18 12:17:26 +02007853 */
7854WL_EXPORT void
7855weston_compositor_read_presentation_clock(
7856 const struct weston_compositor *compositor,
7857 struct timespec *ts)
7858{
7859 static bool warned;
7860 int ret;
7861
7862 ret = clock_gettime(compositor->presentation_clock, ts);
7863 if (ret < 0) {
7864 ts->tv_sec = 0;
7865 ts->tv_nsec = 0;
7866
7867 if (!warned)
7868 weston_log("Error: failure to read "
Antonio Borneo39578632019-04-26 23:57:31 +02007869 "the presentation clock %#x: '%s' (%d)\n",
7870 compositor->presentation_clock,
7871 strerror(errno), errno);
Pekka Paalanen662f3842015-03-18 12:17:26 +02007872 warned = true;
7873 }
7874}
7875
Pekka Paalanen230f3b12014-09-29 14:18:40 -04007876/** Import dmabuf buffer into current renderer
7877 *
7878 * \param compositor
7879 * \param buffer the dmabuf buffer to import
7880 * \return true on usable buffers, false otherwise
7881 *
7882 * This function tests that the linux_dmabuf_buffer is usable
7883 * for the current renderer. Returns false on unusable buffers. Usually
7884 * usability is tested by importing the dmabufs for composition.
7885 *
7886 * This hook is also used for detecting if the renderer supports
7887 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
7888 * supported.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007889 *
7890 * \ingroup compositor
7891 */
Pekka Paalanen230f3b12014-09-29 14:18:40 -04007892WL_EXPORT bool
7893weston_compositor_import_dmabuf(struct weston_compositor *compositor,
7894 struct linux_dmabuf_buffer *buffer)
7895{
7896 struct weston_renderer *renderer;
7897
7898 renderer = compositor->renderer;
7899
7900 if (renderer->import_dmabuf == NULL)
7901 return false;
7902
7903 return renderer->import_dmabuf(compositor, buffer);
7904}
7905
Marius Vlad5a701542019-11-16 20:26:52 +02007906WL_EXPORT bool
7907weston_compositor_dmabuf_can_scanout(struct weston_compositor *compositor,
7908 struct linux_dmabuf_buffer *buffer)
7909{
7910 struct weston_backend *backend = compositor->backend;
7911
7912 if (backend->can_scanout_dmabuf == NULL)
7913 return false;
7914
7915 return backend->can_scanout_dmabuf(compositor, buffer);
7916}
7917
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007918WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05007919weston_version(int *major, int *minor, int *micro)
7920{
7921 *major = WESTON_VERSION_MAJOR;
7922 *minor = WESTON_VERSION_MINOR;
7923 *micro = WESTON_VERSION_MICRO;
7924}
7925
Daniel Stonee03c1112016-11-24 20:45:45 +00007926/**
7927 * Attempts to find a module path from the module map specified in the
7928 * environment. If found, writes the full path into the path variable.
7929 *
7930 * The module map is a string in environment variable WESTON_MODULE_MAP, where
7931 * each entry is of the form "name=path" and entries are separated by
7932 * semicolons. Whitespace is significant.
7933 *
7934 * \param name The name to search for.
7935 * \param path Where the path is written to if found.
7936 * \param path_len Allocated bytes at \c path .
7937 * \returns The length of the string written to path on success, or 0 if the
7938 * module was not specified in the environment map or path_len was too small.
7939 */
7940WL_EXPORT size_t
7941weston_module_path_from_env(const char *name, char *path, size_t path_len)
7942{
7943 const char *mapping = getenv("WESTON_MODULE_MAP");
7944 const char *end;
7945 const int name_len = strlen(name);
7946
7947 if (!mapping)
7948 return 0;
7949
7950 end = mapping + strlen(mapping);
7951 while (mapping < end && *mapping) {
7952 const char *filename, *next;
7953
7954 /* early out: impossibly short string */
7955 if (end - mapping < name_len + 1)
7956 return 0;
7957
7958 filename = &mapping[name_len + 1];
7959 next = strchrnul(mapping, ';');
7960
7961 if (strncmp(mapping, name, name_len) == 0 &&
7962 mapping[name_len] == '=') {
7963 size_t file_len = next - filename; /* no trailing NUL */
7964 if (file_len >= path_len)
7965 return 0;
7966 strncpy(path, filename, file_len);
7967 path[file_len] = '\0';
7968 return file_len;
7969 }
7970
7971 mapping = next + 1;
7972 }
7973
7974 return 0;
7975}
7976
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03007977WL_EXPORT void *
7978weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007979{
7980 char path[PATH_MAX];
7981 void *module, *init;
Daniel Stonebeb97e52016-11-28 12:13:54 +00007982 size_t len;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007983
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08007984 if (name == NULL)
7985 return NULL;
7986
Derek Foreman3f86e502015-06-08 11:46:54 -05007987 if (name[0] != '/') {
Daniel Stonee03c1112016-11-24 20:45:45 +00007988 len = weston_module_path_from_env(name, path, sizeof path);
7989 if (len == 0)
Daniel Stonebeb97e52016-11-28 12:13:54 +00007990 len = snprintf(path, sizeof path, "%s/%s",
7991 LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05007992 } else {
Daniel Stonebeb97e52016-11-28 12:13:54 +00007993 len = snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05007994 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007995
Daniel Stonebeb97e52016-11-28 12:13:54 +00007996 /* snprintf returns the length of the string it would've written,
7997 * _excluding_ the NUL byte. So even being equal to the size of
7998 * our buffer is an error here. */
7999 if (len >= sizeof path)
8000 return NULL;
8001
Kristian Høgsberga6813d22012-09-12 12:21:01 -04008002 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
8003 if (module) {
8004 weston_log("Module '%s' already loaded\n", path);
Pekka Paalanenf696ee92019-11-04 14:07:51 +02008005 } else {
8006 weston_log("Loading module '%s'\n", path);
8007 module = dlopen(path, RTLD_NOW);
8008 if (!module) {
8009 weston_log("Failed to load module: %s\n", dlerror());
8010 return NULL;
8011 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04008012 }
8013
8014 init = dlsym(module, entrypoint);
8015 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03008016 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00008017 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04008018 return NULL;
8019 }
8020
8021 return init;
8022}
8023
Pekka Paalanen6ffbba32019-11-06 12:59:32 +02008024/** Add a compositor destroy listener only once
8025 *
8026 * \param compositor The compositor whose destroy to watch for.
8027 * \param listener The listener struct to initialize.
8028 * \param destroy_handler The callback when compositor is destroyed.
8029 * \return True if listener is added, or false if there already is a listener
8030 * with the given \c destroy_handler.
8031 *
8032 * This function does nothing and returns false if the given callback function
8033 * is already present in the weston_compositor destroy callbacks list.
8034 * Otherwise, this function initializes the given listener with the given
8035 * callback pointer and adds it to the compositor's destroy callbacks list.
8036 *
8037 * This can be used to ensure that plugin initialization is done only once
8038 * in case the same plugin is loaded multiple times. If this function returns
8039 * false, the plugin should be already initialized successfully.
8040 *
8041 * All plugins should register a destroy listener for cleaning up. Note, that
8042 * the plugin destruction order is not guaranteed: plugins that depend on other
8043 * plugins must be able to be torn down in arbitrary order.
8044 *
Leandro Ribeiroca640d52020-01-27 19:12:01 -03008045 * \sa weston_compositor_destroy
Pekka Paalanen6ffbba32019-11-06 12:59:32 +02008046 */
8047WL_EXPORT bool
8048weston_compositor_add_destroy_listener_once(struct weston_compositor *compositor,
8049 struct wl_listener *listener,
8050 wl_notify_func_t destroy_handler)
8051{
8052 if (wl_signal_get(&compositor->destroy_signal, destroy_handler))
8053 return false;
8054
8055 listener->notify = destroy_handler;
8056 wl_signal_add(&compositor->destroy_signal, listener);
8057 return true;
8058}
Giulio Camuffo459137b2014-10-11 23:56:24 +03008059
Leandro Ribeiroca640d52020-01-27 19:12:01 -03008060/** Destroys the compositor.
Giulio Camuffo459137b2014-10-11 23:56:24 +03008061 *
Leandro Ribeiroca640d52020-01-27 19:12:01 -03008062 * This function cleans up the compositor state and then destroys it.
Giulio Camuffo459137b2014-10-11 23:56:24 +03008063 *
Leandro Ribeiroca640d52020-01-27 19:12:01 -03008064 * @param compositor The compositor to be destroyed.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008065 *
Marius Vlad284d5342019-06-24 12:00:47 +03008066 * @ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03008067 */
8068WL_EXPORT void
Leandro Ribeiroca640d52020-01-27 19:12:01 -03008069weston_compositor_destroy(struct weston_compositor *compositor)
Giulio Camuffo459137b2014-10-11 23:56:24 +03008070{
8071 /* prevent further rendering while shutting down */
8072 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
8073
8074 wl_signal_emit(&compositor->destroy_signal, compositor);
8075
8076 weston_compositor_xkb_destroy(compositor);
8077
Giulio Camuffo2d24e642015-10-03 16:25:15 +03008078 if (compositor->backend)
8079 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02008080
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03008081 /* The backend is responsible for destroying the heads. */
8082 assert(wl_list_empty(&compositor->head_list));
8083
Pekka Paalanen827b5d22016-06-29 11:54:26 +02008084 weston_plugin_api_destroy_list(compositor);
8085
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03008086 if (compositor->heads_changed_source)
8087 wl_event_source_remove(compositor->heads_changed_source);
8088
Leandro Ribeirof0149642019-12-18 15:52:18 -03008089 weston_log_scope_destroy(compositor->debug_scene);
Daniel Stonece62cb32018-07-20 09:46:24 +01008090 compositor->debug_scene = NULL;
Marius Vladda104eb2019-09-05 14:31:01 +03008091
Leandro Ribeirof0149642019-12-18 15:52:18 -03008092 weston_log_scope_destroy(compositor->timeline);
Marius Vladda104eb2019-09-05 14:31:01 +03008093 compositor->timeline = NULL;
Pekka Paalanena5630ea2017-10-12 13:13:42 +02008094
Giulio Camuffo459137b2014-10-11 23:56:24 +03008095 free(compositor);
8096}
8097
8098/** Instruct the compositor to exit.
8099 *
8100 * This functions does not directly destroy the compositor object, it merely
8101 * command it to start the tear down process. It is not guaranteed that the
8102 * tear down will happen immediately.
8103 *
8104 * \param compositor The compositor to tear down.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008105 *
8106 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03008107 */
8108WL_EXPORT void
8109weston_compositor_exit(struct weston_compositor *compositor)
8110{
8111 compositor->exit(compositor);
8112}
8113
8114/** Return the user data stored in the compositor.
8115 *
8116 * This function returns the user data pointer set with user_data parameter
8117 * to the \ref weston_compositor_create function.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008118 *
8119 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03008120 */
8121WL_EXPORT void *
8122weston_compositor_get_user_data(struct weston_compositor *compositor)
8123{
8124 return compositor->user_data;
8125}
Pekka Paalanendd186732016-06-03 14:49:54 +03008126
Pekka Paalanen50dbf382016-06-03 15:23:46 +03008127static const char * const backend_map[] = {
8128 [WESTON_BACKEND_DRM] = "drm-backend.so",
8129 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
8130 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
8131 [WESTON_BACKEND_RDP] = "rdp-backend.so",
8132 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
8133 [WESTON_BACKEND_X11] = "x11-backend.so",
8134};
8135
Pekka Paalanendd186732016-06-03 14:49:54 +03008136/** Load a backend into a weston_compositor
8137 *
8138 * A backend must be loaded to make a weston_compositor work. A backend
8139 * provides input and output capabilities, and determines the renderer to use.
8140 *
8141 * \param compositor A compositor that has not had a backend loaded yet.
8142 * \param backend Name of the backend file.
8143 * \param config_base A pointer to a backend-specific configuration
8144 * structure's 'base' member.
8145 *
8146 * \return 0 on success, or -1 on error.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008147 *
8148 * \ingroup compositor
Pekka Paalanendd186732016-06-03 14:49:54 +03008149 */
8150WL_EXPORT int
8151weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03008152 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03008153 struct weston_backend_config *config_base)
8154{
8155 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03008156 struct weston_backend_config *config_base);
8157
Pekka Paalanend7e35112017-08-29 17:04:12 +03008158 if (compositor->backend) {
8159 weston_log("Error: attempt to load a backend when one is already loaded\n");
8160 return -1;
8161 }
8162
Quentin Glidic887c0182016-07-10 11:00:53 +02008163 if (backend >= ARRAY_LENGTH(backend_map))
Pekka Paalanen50dbf382016-06-03 15:23:46 +03008164 return -1;
8165
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01008166 backend_init = weston_load_module(backend_map[backend], "weston_backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03008167 if (!backend_init)
8168 return -1;
8169
Pekka Paalanend7e35112017-08-29 17:04:12 +03008170 if (backend_init(compositor, config_base) < 0) {
8171 compositor->backend = NULL;
8172 return -1;
8173 }
8174
8175 return 0;
Pekka Paalanendd186732016-06-03 14:49:54 +03008176}
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008177
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008178/** weston_compositor_load_xwayland
8179 * \ingroup compositor
8180 */
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008181WL_EXPORT int
8182weston_compositor_load_xwayland(struct weston_compositor *compositor)
8183{
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01008184 int (*module_init)(struct weston_compositor *ec);
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008185
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01008186 module_init = weston_load_module("xwayland.so", "weston_module_init");
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008187 if (!module_init)
8188 return -1;
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01008189 if (module_init(compositor) < 0)
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008190 return -1;
8191 return 0;
8192}
Vasilis Liaskovitis486b4632018-10-10 16:14:55 +02008193
8194/** Resolve an internal compositor error by disconnecting the client.
8195 *
8196 * This function is used in cases when the wl_buffer turns out
8197 * unusable and there is no fallback path.
8198 *
8199 * It is possible the fault is caused by a compositor bug, the underlying
8200 * graphics stack bug or normal behaviour, or perhaps a client mistake.
8201 * In any case, the options are to either composite garbage or nothing,
8202 * or disconnect the client. This is a helper function for the latter.
8203 *
8204 * The error is sent as an INVALID_OBJECT error on the client's wl_display.
8205 *
8206 * \param buffer The weston buffer that is unusable.
8207 * \param msg A custom error message attached to the protocol error.
8208 */
8209WL_EXPORT void
8210weston_buffer_send_server_error(struct weston_buffer *buffer,
8211 const char *msg)
8212{
8213 struct wl_client *client;
8214 struct wl_resource *display_resource;
8215 uint32_t id;
8216
8217 assert(buffer->resource);
8218 id = wl_resource_get_id(buffer->resource);
8219 client = wl_resource_get_client(buffer->resource);
8220 display_resource = wl_client_get_object(client, 1);
8221
8222 assert(display_resource);
8223 wl_resource_post_error(display_resource,
8224 WL_DISPLAY_ERROR_INVALID_OBJECT,
8225 "server error with "
8226 "wl_buffer@%u: %s", id, msg);
8227}
Ankit Nautiyal93dde242019-07-08 11:46:42 +05308228
8229WL_EXPORT void
8230weston_output_disable_planes_incr(struct weston_output *output)
8231{
8232 output->disable_planes++;
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05308233 /*
8234 * If disable_planes changes from 0 to non-zero, it means some type of
8235 * recording of content has started, and therefore protection level of
8236 * the protected surfaces must be updated to avoid the recording of
8237 * the protected content.
8238 */
8239 if (output->disable_planes == 1)
8240 weston_schedule_surface_protection_update(output->compositor);
Ankit Nautiyal93dde242019-07-08 11:46:42 +05308241}
8242
8243WL_EXPORT void
8244weston_output_disable_planes_decr(struct weston_output *output)
8245{
8246 output->disable_planes--;
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05308247 /*
8248 * If disable_planes changes from non-zero to 0, it means no content
8249 * recording is going on any more, and the protected and surfaces can be
8250 * shown without any apprehensions about content being recorded.
8251 */
8252 if (output->disable_planes == 0)
8253 weston_schedule_surface_protection_update(output->compositor);
8254
Ankit Nautiyal93dde242019-07-08 11:46:42 +05308255}