blob: dc6ecb2ce6fda41f3396d2ad2cc15db7961b655f [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
3246/** Sets the position of the layer in the layer list. The layer will be placed
3247 * below any layer with the same position value, if any.
3248 * This function is safe to call if the layer is already on the list, but the
3249 * layer may be moved below other layers at the same position, if any.
3250 *
3251 * \param layer The layer to modify
3252 * \param position The position the layer will be placed at
3253 */
3254WL_EXPORT void
3255weston_layer_set_position(struct weston_layer *layer,
3256 enum weston_layer_position position)
3257{
3258 struct weston_layer *below;
3259
3260 wl_list_remove(&layer->link);
3261
3262 /* layer_list is ordered from top to bottom, the last layer being the
3263 * background with the smallest position value */
3264
3265 layer->position = position;
3266 wl_list_for_each_reverse(below, &layer->compositor->layer_list, link) {
3267 if (below->position >= layer->position) {
3268 wl_list_insert(&below->link, &layer->link);
3269 return;
3270 }
3271 }
3272 wl_list_insert(&layer->compositor->layer_list, &layer->link);
3273}
3274
3275/** Hide a layer by taking it off the layer list.
3276 * This function is safe to call if the layer is not on the list.
3277 *
3278 * \param layer The layer to hide
3279 */
3280WL_EXPORT void
3281weston_layer_unset_position(struct weston_layer *layer)
3282{
3283 wl_list_remove(&layer->link);
3284 wl_list_init(&layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003285}
3286
3287WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003288weston_layer_set_mask(struct weston_layer *layer,
3289 int x, int y, int width, int height)
3290{
3291 struct weston_view *view;
3292
3293 layer->mask.x1 = x;
3294 layer->mask.x2 = x + width;
3295 layer->mask.y1 = y;
3296 layer->mask.y2 = y + height;
3297
3298 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
3299 weston_view_geometry_dirty(view);
3300 }
3301}
3302
3303WL_EXPORT void
3304weston_layer_set_mask_infinite(struct weston_layer *layer)
3305{
Adam Jackson3c3f3b12019-10-16 16:02:59 -04003306 struct weston_view *view;
3307
3308 layer->mask.x1 = INT32_MIN;
3309 layer->mask.x2 = INT32_MAX;
3310 layer->mask.y1 = INT32_MIN;
3311 layer->mask.y2 = INT32_MAX;
3312
3313 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
3314 weston_view_geometry_dirty(view);
3315 }
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003316}
3317
Daniel Stone3b775632018-07-20 08:38:25 +01003318WL_EXPORT bool
3319weston_layer_mask_is_infinite(struct weston_layer *layer)
3320{
3321 return layer->mask.x1 == INT32_MIN &&
3322 layer->mask.y1 == INT32_MIN &&
Adam Jackson3c3f3b12019-10-16 16:02:59 -04003323 layer->mask.x2 == INT32_MAX &&
3324 layer->mask.y2 == INT32_MAX;
Daniel Stone3b775632018-07-20 08:38:25 +01003325}
3326
Marius Vlad55d87362019-06-11 01:15:35 +03003327/**
3328 * \ingroup output
3329 */
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003330WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003331weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003332{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003333 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04003334 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01003335
Bryce Harrington08976ac2016-08-30 12:05:16 -07003336 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
3337 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003338 return;
3339
Pekka Paalanenb5026542014-11-12 15:09:24 +02003340 if (!output->repaint_needed)
Marius Vlad3203ff62019-09-05 14:56:12 +03003341 TL_POINT(compositor, "core_repaint_req", TLP_OUTPUT(output), TLP_END);
Pekka Paalanenb5026542014-11-12 15:09:24 +02003342
Kristian Høgsbergef044142011-06-21 15:02:12 -04003343 loop = wl_display_get_event_loop(compositor->wl_display);
Daniel Stone09a97e22017-03-01 11:34:06 +00003344 output->repaint_needed = true;
Daniel Stone05df8c12017-03-03 16:59:42 +00003345
3346 /* If we already have a repaint scheduled for our idle handler,
3347 * no need to set it again. If the repaint has been called but
3348 * not finished, then weston_output_finish_frame() will notice
3349 * that a repaint is needed and schedule one. */
3350 if (output->repaint_status != REPAINT_NOT_SCHEDULED)
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003351 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01003352
Daniel Stone05df8c12017-03-03 16:59:42 +00003353 output->repaint_status = REPAINT_BEGIN_FROM_IDLE;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03003354 assert(!output->idle_repaint_source);
3355 output->idle_repaint_source = wl_event_loop_add_idle(loop, idle_repaint,
3356 output);
Marius Vlad3203ff62019-09-05 14:56:12 +03003357 TL_POINT(compositor, "core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003358}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05003359
Marius Vlad9fdda7f2019-06-11 16:08:55 +03003360/** weston_compositor_schedule_repaint
3361 * \ingroup compositor
3362 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003363WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003364weston_compositor_schedule_repaint(struct weston_compositor *compositor)
3365{
3366 struct weston_output *output;
3367
3368 wl_list_for_each(output, &compositor->output_list, link)
3369 weston_output_schedule_repaint(output);
3370}
3371
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003372static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003373surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003374{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003375 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003376}
3377
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003378static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003379surface_attach(struct wl_client *client,
3380 struct wl_resource *resource,
3381 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
3382{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003383 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05003384 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003385
Kristian Høgsbergab19f932013-08-20 11:30:54 -07003386 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05003387 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07003388 if (buffer == NULL) {
3389 wl_client_post_no_memory(client);
3390 return;
3391 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07003392 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04003393
Pekka Paalanende685b82012-12-04 15:58:12 +02003394 /* Attach, attach, without commit in between does not send
3395 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003396 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003397
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003398 surface->pending.sx = sx;
3399 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01003400 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003401}
3402
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003403static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003404surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003405 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003406 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05003407{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003408 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003409
Derek Foreman57e92ed2015-11-17 14:11:35 -06003410 if (width <= 0 || height <= 0)
3411 return;
3412
Derek Foreman152254b2015-11-26 14:17:48 -06003413 pixman_region32_union_rect(&surface->pending.damage_surface,
3414 &surface->pending.damage_surface,
3415 x, y, width, height);
3416}
3417
3418static void
3419surface_damage_buffer(struct wl_client *client,
3420 struct wl_resource *resource,
3421 int32_t x, int32_t y, int32_t width, int32_t height)
3422{
3423 struct weston_surface *surface = wl_resource_get_user_data(resource);
3424
3425 if (width <= 0 || height <= 0)
3426 return;
3427
3428 pixman_region32_union_rect(&surface->pending.damage_buffer,
3429 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04003430 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003431}
3432
Kristian Høgsberg33418202011-08-16 23:01:28 -04003433static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003434destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04003435{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05003436 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003437
3438 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02003439 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003440}
3441
3442static void
3443surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003444 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04003445{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003446 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003447 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003448
3449 cb = malloc(sizeof *cb);
3450 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003451 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003452 return;
3453 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03003454
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003455 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
3456 callback);
3457 if (cb->resource == NULL) {
3458 free(cb);
3459 wl_resource_post_no_memory(resource);
3460 return;
3461 }
3462
Jason Ekstranda85118c2013-06-27 20:17:02 -05003463 wl_resource_set_implementation(cb->resource, NULL, cb,
3464 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003465
Pekka Paalanenbc106382012-10-10 12:49:31 +03003466 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003467}
3468
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003469static void
3470surface_set_opaque_region(struct wl_client *client,
3471 struct wl_resource *resource,
3472 struct wl_resource *region_resource)
3473{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003474 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003475 struct weston_region *region;
3476
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003477 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003478 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003479 pixman_region32_copy(&surface->pending.opaque,
3480 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003481 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07003482 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003483 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003484}
3485
3486static void
3487surface_set_input_region(struct wl_client *client,
3488 struct wl_resource *resource,
3489 struct wl_resource *region_resource)
3490{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003491 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003492 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003493
3494 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003495 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003496 pixman_region32_copy(&surface->pending.input,
3497 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003498 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003499 pixman_region32_fini(&surface->pending.input);
3500 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003501 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003502}
3503
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003504/* Cause damage to this sub-surface and all its children.
3505 *
3506 * This is useful when there are state changes that need an implicit
3507 * damage, e.g. a z-order change.
3508 */
3509static void
3510weston_surface_damage_subsurfaces(struct weston_subsurface *sub)
3511{
3512 struct weston_subsurface *child;
3513
3514 weston_surface_damage(sub->surface);
3515 sub->reordered = false;
3516
3517 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link)
3518 if (child != sub)
3519 weston_surface_damage_subsurfaces(child);
3520}
3521
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003522static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003523weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003524{
Pekka Paalanene67858b2013-04-25 13:57:42 +03003525 struct weston_subsurface *sub;
3526
3527 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
3528 parent_link_pending) {
3529 wl_list_remove(&sub->parent_link);
3530 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003531
3532 if (sub->reordered)
3533 weston_surface_damage_subsurfaces(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003534 }
3535}
3536
3537static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03003538weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05003539 struct weston_matrix *matrix)
3540{
Pekka Paalanen04baea52016-04-26 15:50:58 +03003541 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05003542 double src_width, src_height, dest_width, dest_height;
3543
3544 weston_matrix_init(matrix);
3545
3546 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
3547 src_width = surface->width_from_buffer;
3548 src_height = surface->height_from_buffer;
3549 } else {
3550 src_width = wl_fixed_to_double(vp->buffer.src_width);
3551 src_height = wl_fixed_to_double(vp->buffer.src_height);
3552 }
3553
3554 if (vp->surface.width == -1) {
3555 dest_width = src_width;
3556 dest_height = src_height;
3557 } else {
3558 dest_width = vp->surface.width;
3559 dest_height = vp->surface.height;
3560 }
3561
3562 if (src_width != dest_width || src_height != dest_height)
3563 weston_matrix_scale(matrix,
3564 src_width / dest_width,
3565 src_height / dest_height, 1);
3566
3567 if (vp->buffer.src_width != wl_fixed_from_int(-1))
3568 weston_matrix_translate(matrix,
3569 wl_fixed_to_double(vp->buffer.src_x),
3570 wl_fixed_to_double(vp->buffer.src_y),
3571 0);
3572
3573 switch (vp->buffer.transform) {
3574 case WL_OUTPUT_TRANSFORM_FLIPPED:
3575 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3576 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3577 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3578 weston_matrix_scale(matrix, -1, 1, 1);
3579 weston_matrix_translate(matrix,
3580 surface->width_from_buffer, 0, 0);
3581 break;
3582 }
3583
3584 switch (vp->buffer.transform) {
3585 default:
3586 case WL_OUTPUT_TRANSFORM_NORMAL:
3587 case WL_OUTPUT_TRANSFORM_FLIPPED:
3588 break;
3589 case WL_OUTPUT_TRANSFORM_90:
3590 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +02003591 weston_matrix_rotate_xy(matrix, 0, -1);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003592 weston_matrix_translate(matrix,
Pekka Paalanen8060d822020-02-06 15:27:54 +02003593 0, surface->width_from_buffer, 0);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003594 break;
3595 case WL_OUTPUT_TRANSFORM_180:
3596 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3597 weston_matrix_rotate_xy(matrix, -1, 0);
3598 weston_matrix_translate(matrix,
3599 surface->width_from_buffer,
3600 surface->height_from_buffer, 0);
3601 break;
3602 case WL_OUTPUT_TRANSFORM_270:
3603 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +02003604 weston_matrix_rotate_xy(matrix, 0, 1);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003605 weston_matrix_translate(matrix,
Pekka Paalanen8060d822020-02-06 15:27:54 +02003606 surface->height_from_buffer, 0, 0);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003607 break;
3608 }
3609
3610 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
3611}
3612
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003613/**
3614 * Compute a + b > c while being safe to overflows.
3615 */
3616static bool
3617fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
3618{
3619 return (int64_t)a + (int64_t)b > (int64_t)c;
3620}
3621
3622static bool
3623weston_surface_is_pending_viewport_source_valid(
3624 const struct weston_surface *surface)
3625{
3626 const struct weston_surface_state *pend = &surface->pending;
3627 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
3628 int width_from_buffer = 0;
3629 int height_from_buffer = 0;
3630 wl_fixed_t w;
3631 wl_fixed_t h;
3632
3633 /* If viewport source rect is not set, it is always ok. */
3634 if (vp->buffer.src_width == wl_fixed_from_int(-1))
3635 return true;
3636
3637 if (pend->newly_attached) {
3638 if (pend->buffer) {
3639 convert_size_by_transform_scale(&width_from_buffer,
3640 &height_from_buffer,
3641 pend->buffer->width,
3642 pend->buffer->height,
3643 vp->buffer.transform,
3644 vp->buffer.scale);
3645 } else {
3646 /* No buffer: viewport is irrelevant. */
3647 return true;
3648 }
3649 } else {
3650 width_from_buffer = surface->width_from_buffer;
3651 height_from_buffer = surface->height_from_buffer;
3652 }
3653
3654 assert((width_from_buffer == 0) == (height_from_buffer == 0));
3655 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
3656
3657 /* No buffer: viewport is irrelevant. */
3658 if (width_from_buffer == 0 || height_from_buffer == 0)
3659 return true;
3660
3661 /* overflow checks for wl_fixed_from_int() */
3662 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
3663 return false;
3664 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
3665 return false;
3666
3667 w = wl_fixed_from_int(width_from_buffer);
3668 h = wl_fixed_from_int(height_from_buffer);
3669
3670 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
3671 return false;
3672 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
3673 return false;
3674
3675 return true;
3676}
3677
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003678static bool
3679fixed_is_integer(wl_fixed_t v)
3680{
3681 return (v & 0xff) == 0;
3682}
3683
3684static bool
3685weston_surface_is_pending_viewport_dst_size_int(
3686 const struct weston_surface *surface)
3687{
3688 const struct weston_buffer_viewport *vp =
3689 &surface->pending.buffer_viewport;
3690
3691 if (vp->surface.width != -1) {
3692 assert(vp->surface.width > 0 && vp->surface.height > 0);
3693 return true;
3694 }
3695
3696 return fixed_is_integer(vp->buffer.src_width) &&
3697 fixed_is_integer(vp->buffer.src_height);
3698}
3699
Derek Foreman152254b2015-11-26 14:17:48 -06003700/* Translate pending damage in buffer co-ordinates to surface
3701 * co-ordinates and union it with a pixman_region32_t.
3702 * This should only be called after the buffer is attached.
3703 */
3704static void
3705apply_damage_buffer(pixman_region32_t *dest,
3706 struct weston_surface *surface,
3707 struct weston_surface_state *state)
3708{
3709 struct weston_buffer *buffer = surface->buffer_ref.buffer;
3710
3711 /* wl_surface.damage_buffer needs to be clipped to the buffer,
3712 * translated into surface co-ordinates and unioned with
3713 * any other surface damage.
3714 * None of this makes sense if there is no buffer though.
3715 */
3716 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
3717 pixman_region32_t buffer_damage;
3718
3719 pixman_region32_intersect_rect(&state->damage_buffer,
3720 &state->damage_buffer,
3721 0, 0, buffer->width,
3722 buffer->height);
3723 pixman_region32_init(&buffer_damage);
3724 weston_matrix_transform_region(&buffer_damage,
3725 &surface->buffer_to_surface_matrix,
3726 &state->damage_buffer);
3727 pixman_region32_union(dest, dest, &buffer_damage);
3728 pixman_region32_fini(&buffer_damage);
3729 }
3730 /* We should clear this on commit even if there was no buffer */
3731 pixman_region32_clear(&state->damage_buffer);
3732}
3733
Jason Ekstrand1e059042014-10-16 10:55:19 -05003734static void
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303735weston_surface_set_desired_protection(struct weston_surface *surface,
3736 enum weston_hdcp_protection protection)
3737{
3738 if (surface->desired_protection == protection)
3739 return;
3740 surface->desired_protection = protection;
3741 weston_surface_damage(surface);
3742}
3743
3744static void
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303745weston_surface_set_protection_mode(struct weston_surface *surface,
3746 enum weston_surface_protection_mode p_mode)
3747{
3748 struct content_protection *cp = surface->compositor->content_protection;
3749 struct protected_surface *psurface;
3750
3751 surface->protection_mode = p_mode;
3752 wl_list_for_each(psurface, &cp->protected_list, link) {
3753 if (!psurface || psurface->surface != surface)
3754 continue;
3755 weston_protected_surface_send_event(psurface,
3756 surface->current_protection);
3757 }
3758}
3759
3760static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05003761weston_surface_commit_state(struct weston_surface *surface,
3762 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003763{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003764 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003765 pixman_region32_t opaque;
3766
Alexander Larsson4ea95522013-05-22 14:41:37 +02003767 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02003768 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03003769 /* wp_viewport.set_source */
3770 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003771 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003772
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003773 /* wl_surface.attach */
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003774 if (state->newly_attached) {
3775 /* zwp_surface_synchronization_v1.set_acquire_fence */
3776 fd_move(&surface->acquire_fence_fd,
3777 &state->acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003778 /* zwp_surface_synchronization_v1.get_release */
3779 weston_buffer_release_move(&surface->buffer_release_ref,
3780 &state->buffer_release_ref);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003781 weston_surface_attach(surface, state->buffer);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003782 }
Jason Ekstrand7b982072014-05-20 14:33:03 -05003783 weston_surface_state_set_buffer(state, NULL);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003784 assert(state->acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003785 assert(state->buffer_release_ref.buffer_release == NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01003786
Jason Ekstrand1e059042014-10-16 10:55:19 -05003787 weston_surface_build_buffer_matrix(surface,
3788 &surface->surface_to_buffer_matrix);
3789 weston_matrix_invert(&surface->buffer_to_surface_matrix,
3790 &surface->surface_to_buffer_matrix);
3791
Jason Ekstrand7b982072014-05-20 14:33:03 -05003792 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003793 weston_surface_update_size(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003794 if (surface->committed)
3795 surface->committed(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003796 }
Giulio Camuffo184df502013-02-21 11:29:21 +01003797
Jason Ekstrand7b982072014-05-20 14:33:03 -05003798 state->sx = 0;
3799 state->sy = 0;
3800 state->newly_attached = 0;
3801 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03003802
Derek Foreman152254b2015-11-26 14:17:48 -06003803 /* wl_surface.damage and wl_surface.damage_buffer */
Marius Vlad2a1b7862019-09-05 13:12:18 +03003804 if (pixman_region32_not_empty(&state->damage_surface) ||
3805 pixman_region32_not_empty(&state->damage_buffer))
Marius Vlad3203ff62019-09-05 14:56:12 +03003806 TL_POINT(surface->compositor, "core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06003807
Pekka Paalanen8e159182012-10-10 12:49:25 +03003808 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06003809 &state->damage_surface);
3810
3811 apply_damage_buffer(&surface->damage, surface, state);
3812
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05003813 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07003814 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06003815 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003816
3817 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003818 pixman_region32_init(&opaque);
3819 pixman_region32_intersect_rect(&opaque, &state->opaque,
3820 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003821
3822 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
3823 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003824 wl_list_for_each(view, &surface->views, surface_link)
3825 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003826 }
3827
3828 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003829
3830 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003831 pixman_region32_intersect_rect(&surface->input, &state->input,
3832 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003833
Pekka Paalanenbc106382012-10-10 12:49:31 +03003834 /* wl_surface.frame */
3835 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05003836 &state->frame_callback_list);
3837 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003838
3839 /* XXX:
3840 * What should happen with a feedback request, if there
3841 * is no wl_buffer attached for this commit?
3842 */
3843
3844 /* presentation.feedback */
3845 wl_list_insert_list(&surface->feedback_list,
3846 &state->feedback_list);
3847 wl_list_init(&state->feedback_list);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003848
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303849 /* weston_protected_surface.enforced/relaxed */
3850 if (surface->protection_mode != state->protection_mode)
3851 weston_surface_set_protection_mode(surface,
3852 state->protection_mode);
3853
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303854 /* weston_protected_surface.set_type */
3855 weston_surface_set_desired_protection(surface, state->desired_protection);
3856
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003857 wl_signal_emit(&surface->commit_signal, surface);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003858}
3859
3860static void
3861weston_surface_commit(struct weston_surface *surface)
3862{
3863 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003864
Pekka Paalanene67858b2013-04-25 13:57:42 +03003865 weston_surface_commit_subsurface_order(surface);
3866
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003867 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003868}
3869
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003870static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003871weston_subsurface_commit(struct weston_subsurface *sub);
3872
3873static void
3874weston_subsurface_parent_commit(struct weston_subsurface *sub,
3875 int parent_is_synchronized);
3876
3877static void
3878surface_commit(struct wl_client *client, struct wl_resource *resource)
3879{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003880 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003881 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
3882
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003883 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
3884 assert(surface->viewport_resource);
3885
3886 wl_resource_post_error(surface->viewport_resource,
3887 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
3888 "wl_surface@%d has viewport source outside buffer",
3889 wl_resource_get_id(resource));
3890 return;
3891 }
3892
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003893 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
3894 assert(surface->viewport_resource);
3895
3896 wl_resource_post_error(surface->viewport_resource,
3897 WP_VIEWPORT_ERROR_BAD_SIZE,
3898 "wl_surface@%d viewport dst size not integer",
3899 wl_resource_get_id(resource));
3900 return;
3901 }
3902
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003903 if (surface->pending.acquire_fence_fd >= 0) {
3904 assert(surface->synchronization_resource);
3905
3906 if (!surface->pending.buffer) {
3907 fd_clear(&surface->pending.acquire_fence_fd);
3908 wl_resource_post_error(surface->synchronization_resource,
3909 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3910 "wl_surface@%"PRIu32" no buffer for synchronization",
3911 wl_resource_get_id(resource));
3912 return;
3913 }
3914
3915 /* We support fences for both wp_linux_dmabuf and opaque EGL
3916 * buffers, as mandated by minor version 2 of the
3917 * zwp_linux_explicit_synchronization_v1 protocol. Since
3918 * renderers that support fences currently only support these
3919 * two buffer types plus SHM buffers, we can just check for the
3920 * SHM buffer case here.
3921 */
3922 if (wl_shm_buffer_get(surface->pending.buffer->resource)) {
3923 fd_clear(&surface->pending.acquire_fence_fd);
3924 wl_resource_post_error(surface->synchronization_resource,
3925 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_UNSUPPORTED_BUFFER,
3926 "wl_surface@%"PRIu32" unsupported buffer for synchronization",
3927 wl_resource_get_id(resource));
3928 return;
3929 }
3930 }
3931
Alexandros Frantzis67629672018-10-19 12:14:11 +03003932 if (surface->pending.buffer_release_ref.buffer_release &&
3933 !surface->pending.buffer) {
3934 assert(surface->synchronization_resource);
3935
3936 wl_resource_post_error(surface->synchronization_resource,
3937 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3938 "wl_surface@%"PRIu32" no buffer for synchronization",
3939 wl_resource_get_id(resource));
3940 return;
3941 }
3942
Pekka Paalanene67858b2013-04-25 13:57:42 +03003943 if (sub) {
3944 weston_subsurface_commit(sub);
3945 return;
3946 }
3947
3948 weston_surface_commit(surface);
3949
3950 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
3951 if (sub->surface != surface)
3952 weston_subsurface_parent_commit(sub, 0);
3953 }
3954}
3955
3956static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003957surface_set_buffer_transform(struct wl_client *client,
3958 struct wl_resource *resource, int transform)
3959{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003960 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003961
Jonny Lamba55f1392014-05-30 12:07:15 +02003962 /* if wl_output.transform grows more members this will need to be updated. */
3963 if (transform < 0 ||
3964 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
3965 wl_resource_post_error(resource,
3966 WL_SURFACE_ERROR_INVALID_TRANSFORM,
3967 "buffer transform must be a valid transform "
3968 "('%d' specified)", transform);
3969 return;
3970 }
3971
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003972 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003973 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003974}
3975
Alexander Larsson4ea95522013-05-22 14:41:37 +02003976static void
3977surface_set_buffer_scale(struct wl_client *client,
3978 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003979 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003980{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003981 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003982
Jonny Lamba55f1392014-05-30 12:07:15 +02003983 if (scale < 1) {
3984 wl_resource_post_error(resource,
3985 WL_SURFACE_ERROR_INVALID_SCALE,
3986 "buffer scale must be at least one "
3987 "('%d' specified)", scale);
3988 return;
3989 }
3990
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003991 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003992 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003993}
3994
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003995static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003996 surface_destroy,
3997 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04003998 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003999 surface_frame,
4000 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03004001 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004002 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02004003 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06004004 surface_set_buffer_scale,
4005 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004006};
4007
4008static void
4009compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04004010 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004011{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04004012 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004013 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004014
Kristian Høgsberg18c93002012-01-27 11:58:31 -05004015 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04004016 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04004017 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004018 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04004019 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004020
Jason Ekstranda85118c2013-06-27 20:17:02 -05004021 surface->resource =
4022 wl_resource_create(client, &wl_surface_interface,
4023 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004024 if (surface->resource == NULL) {
4025 weston_surface_destroy(surface);
4026 wl_resource_post_no_memory(resource);
4027 return;
4028 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05004029 wl_resource_set_implementation(surface->resource, &surface_interface,
4030 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07004031
4032 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004033}
4034
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004035static void
4036destroy_region(struct wl_resource *resource)
4037{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05004038 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004039
4040 pixman_region32_fini(&region->region);
4041 free(region);
4042}
4043
4044static void
4045region_destroy(struct wl_client *client, struct wl_resource *resource)
4046{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004047 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004048}
4049
4050static void
4051region_add(struct wl_client *client, struct wl_resource *resource,
4052 int32_t x, int32_t y, int32_t width, int32_t height)
4053{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05004054 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004055
4056 pixman_region32_union_rect(&region->region, &region->region,
4057 x, y, width, height);
4058}
4059
4060static void
4061region_subtract(struct wl_client *client, struct wl_resource *resource,
4062 int32_t x, int32_t y, int32_t width, int32_t height)
4063{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05004064 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004065 pixman_region32_t rect;
4066
4067 pixman_region32_init_rect(&rect, x, y, width, height);
4068 pixman_region32_subtract(&region->region, &region->region, &rect);
4069 pixman_region32_fini(&rect);
4070}
4071
4072static const struct wl_region_interface region_interface = {
4073 region_destroy,
4074 region_add,
4075 region_subtract
4076};
4077
4078static void
4079compositor_create_region(struct wl_client *client,
4080 struct wl_resource *resource, uint32_t id)
4081{
4082 struct weston_region *region;
4083
4084 region = malloc(sizeof *region);
4085 if (region == NULL) {
4086 wl_resource_post_no_memory(resource);
4087 return;
4088 }
4089
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004090 pixman_region32_init(&region->region);
4091
Jason Ekstranda85118c2013-06-27 20:17:02 -05004092 region->resource =
4093 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004094 if (region->resource == NULL) {
4095 free(region);
4096 wl_resource_post_no_memory(resource);
4097 return;
4098 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05004099 wl_resource_set_implementation(region->resource, &region_interface,
4100 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004101}
4102
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04004103static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004104 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004105 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004106};
4107
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004108static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03004109weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
4110{
4111 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004112
Jason Ekstrand7b982072014-05-20 14:33:03 -05004113 weston_surface_commit_state(surface, &sub->cached);
4114 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004115
4116 weston_surface_commit_subsurface_order(surface);
4117
4118 weston_surface_schedule_repaint(surface);
4119
Jason Ekstrand7b982072014-05-20 14:33:03 -05004120 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004121}
4122
4123static void
4124weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
4125{
4126 struct weston_surface *surface = sub->surface;
4127
4128 /*
4129 * If this commit would cause the surface to move by the
4130 * attach(dx, dy) parameters, the old damage region must be
4131 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05004132 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03004133 */
Derek Foreman152254b2015-11-26 14:17:48 -06004134 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004135 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06004136 pixman_region32_union(&sub->cached.damage_surface,
4137 &sub->cached.damage_surface,
4138 &surface->pending.damage_surface);
4139 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004140
4141 if (surface->pending.newly_attached) {
4142 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05004143 weston_surface_state_set_buffer(&sub->cached,
4144 surface->pending.buffer);
4145 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004146 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04004147 weston_presentation_feedback_discard_list(
4148 &sub->cached.feedback_list);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03004149 /* zwp_surface_synchronization_v1.set_acquire_fence */
4150 fd_move(&sub->cached.acquire_fence_fd,
4151 &surface->pending.acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03004152 /* zwp_surface_synchronization_v1.get_release */
4153 weston_buffer_release_move(&sub->cached.buffer_release_ref,
4154 &surface->pending.buffer_release_ref);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004155 }
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05304156 sub->cached.desired_protection = surface->pending.desired_protection;
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05304157 sub->cached.protection_mode = surface->pending.protection_mode;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03004158 assert(surface->pending.acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03004159 assert(surface->pending.buffer_release_ref.buffer_release == NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004160 sub->cached.sx += surface->pending.sx;
4161 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02004162
Derek Foreman152254b2015-11-26 14:17:48 -06004163 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
4164
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004165 sub->cached.buffer_viewport.changed |=
4166 surface->pending.buffer_viewport.changed;
4167 sub->cached.buffer_viewport.buffer =
4168 surface->pending.buffer_viewport.buffer;
4169 sub->cached.buffer_viewport.surface =
4170 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004171
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004172 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004173
4174 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
4175
4176 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
4177
4178 wl_list_insert_list(&sub->cached.frame_callback_list,
4179 &surface->pending.frame_callback_list);
4180 wl_list_init(&surface->pending.frame_callback_list);
4181
Pekka Paalanen133e4392014-09-23 22:08:46 -04004182 wl_list_insert_list(&sub->cached.feedback_list,
4183 &surface->pending.feedback_list);
4184 wl_list_init(&surface->pending.feedback_list);
4185
Jason Ekstrand7b982072014-05-20 14:33:03 -05004186 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004187}
4188
Derek Foreman280e7dd2014-10-03 13:13:42 -05004189static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03004190weston_subsurface_is_synchronized(struct weston_subsurface *sub)
4191{
4192 while (sub) {
4193 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05004194 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004195
4196 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05004197 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004198
4199 sub = weston_surface_to_subsurface(sub->parent);
4200 }
4201
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01004202 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004203}
4204
4205static void
4206weston_subsurface_commit(struct weston_subsurface *sub)
4207{
4208 struct weston_surface *surface = sub->surface;
4209 struct weston_subsurface *tmp;
4210
4211 /* Recursive check for effectively synchronized. */
4212 if (weston_subsurface_is_synchronized(sub)) {
4213 weston_subsurface_commit_to_cache(sub);
4214 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05004215 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03004216 /* flush accumulated state from cache */
4217 weston_subsurface_commit_to_cache(sub);
4218 weston_subsurface_commit_from_cache(sub);
4219 } else {
4220 weston_surface_commit(surface);
4221 }
4222
4223 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
4224 if (tmp->surface != surface)
4225 weston_subsurface_parent_commit(tmp, 0);
4226 }
4227 }
4228}
4229
4230static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004231weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004232{
4233 struct weston_surface *surface = sub->surface;
4234 struct weston_subsurface *tmp;
4235
Pekka Paalanene67858b2013-04-25 13:57:42 +03004236 /* From now on, commit_from_cache the whole sub-tree, regardless of
4237 * the synchronized mode of each child. This sub-surface or some
4238 * of its ancestors were synchronized, so we are synchronized
4239 * all the way down.
4240 */
4241
Jason Ekstrand7b982072014-05-20 14:33:03 -05004242 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004243 weston_subsurface_commit_from_cache(sub);
4244
4245 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
4246 if (tmp->surface != surface)
4247 weston_subsurface_parent_commit(tmp, 1);
4248 }
4249}
4250
4251static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004252weston_subsurface_parent_commit(struct weston_subsurface *sub,
4253 int parent_is_synchronized)
4254{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004255 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004256 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004257 wl_list_for_each(view, &sub->surface->views, surface_link)
4258 weston_view_set_position(view,
4259 sub->position.x,
4260 sub->position.y);
4261
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004262 sub->position.set = 0;
4263 }
4264
4265 if (parent_is_synchronized || sub->synchronized)
4266 weston_subsurface_synchronized_commit(sub);
4267}
4268
Pekka Paalanen8274d902014-08-06 19:36:51 +03004269static int
4270subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
4271{
4272 return snprintf(buf, len, "sub-surface");
4273}
4274
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004275static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004276subsurface_committed(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004277{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004278 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004279
Jason Ekstranda7af7042013-10-12 22:38:11 -05004280 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004281 weston_view_set_position(view,
4282 view->geometry.x + dx,
4283 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004284
4285 /* No need to check parent mappedness, because if parent is not
4286 * mapped, parent is not in a visible layer, so this sub-surface
4287 * will not be drawn either.
4288 */
Armin Krezovićf8486c32016-06-30 06:04:28 +02004289
Pekka Paalanene67858b2013-04-25 13:57:42 +03004290 if (!weston_surface_is_mapped(surface)) {
Armin Krezovićf8486c32016-06-30 06:04:28 +02004291 surface->is_mapped = true;
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03004292
Derek Foreman4b1a0a12014-09-10 15:37:33 -05004293 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03004294 * because that would call it also for the parent surface,
4295 * which might not be mapped yet. That would lead to
4296 * inconsistent state, where the window could never be
4297 * mapped.
4298 *
Armin Krezovićf8486c32016-06-30 06:04:28 +02004299 * Instead just force the is_mapped flag on, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03004300 * weston_surface_is_mapped() return true, so that when the
4301 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03004302 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03004303 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004304 }
4305}
4306
4307static struct weston_subsurface *
4308weston_surface_to_subsurface(struct weston_surface *surface)
4309{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004310 if (surface->committed == subsurface_committed)
4311 return surface->committed_private;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004312
4313 return NULL;
4314}
4315
Pekka Paalanen01388e22013-04-25 13:57:44 +03004316WL_EXPORT struct weston_surface *
4317weston_surface_get_main_surface(struct weston_surface *surface)
4318{
4319 struct weston_subsurface *sub;
4320
4321 while (surface && (sub = weston_surface_to_subsurface(surface)))
4322 surface = sub->parent;
4323
4324 return surface;
4325}
4326
Pekka Paalanen50b67472014-10-01 15:02:41 +03004327WL_EXPORT int
4328weston_surface_set_role(struct weston_surface *surface,
4329 const char *role_name,
4330 struct wl_resource *error_resource,
4331 uint32_t error_code)
4332{
4333 assert(role_name);
4334
4335 if (surface->role_name == NULL ||
4336 surface->role_name == role_name ||
4337 strcmp(surface->role_name, role_name) == 0) {
4338 surface->role_name = role_name;
4339
4340 return 0;
4341 }
4342
4343 wl_resource_post_error(error_resource, error_code,
4344 "Cannot assign role %s to wl_surface@%d,"
4345 " already has role %s\n",
4346 role_name,
4347 wl_resource_get_id(surface->resource),
4348 surface->role_name);
4349 return -1;
4350}
4351
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02004352WL_EXPORT const char *
4353weston_surface_get_role(struct weston_surface *surface)
4354{
4355 return surface->role_name;
4356}
4357
Pekka Paalanen8274d902014-08-06 19:36:51 +03004358WL_EXPORT void
4359weston_surface_set_label_func(struct weston_surface *surface,
4360 int (*desc)(struct weston_surface *,
4361 char *, size_t))
4362{
4363 surface->get_label = desc;
Marius Vlad5de92972019-10-15 13:25:41 +03004364 weston_timeline_refresh_subscription_objects(surface->compositor,
4365 surface);
Pekka Paalanen8274d902014-08-06 19:36:51 +03004366}
4367
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004368/** Get the size of surface contents
4369 *
4370 * \param surface The surface to query.
4371 * \param width Returns the width of raw contents.
4372 * \param height Returns the height of raw contents.
4373 *
4374 * Retrieves the raw surface content size in pixels for the given surface.
4375 * This is the whole content size in buffer pixels. If the surface
4376 * has no content or the renderer does not implement this feature,
4377 * zeroes are returned.
4378 *
4379 * This function is used to determine the buffer size needed for
4380 * a weston_surface_copy_content() call.
4381 */
4382WL_EXPORT void
4383weston_surface_get_content_size(struct weston_surface *surface,
4384 int *width, int *height)
4385{
4386 struct weston_renderer *rer = surface->compositor->renderer;
4387
4388 if (!rer->surface_get_content_size) {
4389 *width = 0;
4390 *height = 0;
4391 return;
4392 }
4393
4394 rer->surface_get_content_size(surface, width, height);
4395}
4396
Quentin Glidic248dd102016-08-12 10:41:34 +02004397/** Get the bounding box of a surface and its subsurfaces
4398 *
4399 * \param surface The surface to query.
4400 * \return The bounding box relative to the surface origin.
4401 *
4402 */
4403WL_EXPORT struct weston_geometry
4404weston_surface_get_bounding_box(struct weston_surface *surface)
4405{
4406 pixman_region32_t region;
4407 pixman_box32_t *box;
4408 struct weston_subsurface *subsurface;
4409
4410 pixman_region32_init_rect(&region,
4411 0, 0,
4412 surface->width, surface->height);
4413
4414 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link)
4415 pixman_region32_union_rect(&region, &region,
4416 subsurface->position.x,
4417 subsurface->position.y,
4418 subsurface->surface->width,
4419 subsurface->surface->height);
4420
4421 box = pixman_region32_extents(&region);
4422 struct weston_geometry geometry = {
4423 .x = box->x1,
4424 .y = box->y1,
4425 .width = box->x2 - box->x1,
4426 .height = box->y2 - box->y1,
4427 };
4428
4429 pixman_region32_fini(&region);
4430
4431 return geometry;
4432}
4433
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004434/** Copy surface contents to system memory.
4435 *
4436 * \param surface The surface to copy from.
4437 * \param target Pointer to the target memory buffer.
4438 * \param size Size of the target buffer in bytes.
4439 * \param src_x X location on contents to copy from.
4440 * \param src_y Y location on contents to copy from.
4441 * \param width Width in pixels of the area to copy.
4442 * \param height Height in pixels of the area to copy.
4443 * \return 0 for success, -1 for failure.
4444 *
4445 * Surface contents are maintained by the renderer. They can be in a
4446 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
4447 * else.
4448 *
4449 * Surface contents are copied into memory pointed to by target,
4450 * which has size bytes of space available. The target memory
4451 * may be larger than needed, but being smaller returns an error.
4452 * The extra bytes in target may or may not be written; their content is
4453 * unspecified. Size must be large enough to hold the image.
4454 *
4455 * The image in the target memory will be arranged in rows from
4456 * top to bottom, and pixels on a row from left to right. The pixel
4457 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
4458 * width * 4.
4459 *
4460 * Parameters src_x and src_y define the upper-left corner in buffer
4461 * coordinates (pixels) to copy from. Parameters width and height
4462 * define the size of the area to copy in pixels.
4463 *
4464 * The rectangle defined by src_x, src_y, width, height must fit in
4465 * the surface contents. Otherwise an error is returned.
4466 *
Changwoo Chof97d2502017-08-05 00:30:47 +09004467 * Use weston_surface_get_content_size to determine the content size; the
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004468 * needed target buffer size and rectangle limits.
4469 *
4470 * CURRENT IMPLEMENTATION RESTRICTIONS:
4471 * - the machine must be little-endian due to Pixman formats.
4472 *
4473 * NOTE: Pixman formats are premultiplied.
4474 */
4475WL_EXPORT int
4476weston_surface_copy_content(struct weston_surface *surface,
4477 void *target, size_t size,
4478 int src_x, int src_y,
4479 int width, int height)
4480{
4481 struct weston_renderer *rer = surface->compositor->renderer;
4482 int cw, ch;
4483 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
4484
4485 if (!rer->surface_copy_content)
4486 return -1;
4487
4488 weston_surface_get_content_size(surface, &cw, &ch);
4489
4490 if (src_x < 0 || src_y < 0)
4491 return -1;
4492
4493 if (width <= 0 || height <= 0)
4494 return -1;
4495
4496 if (src_x + width > cw || src_y + height > ch)
4497 return -1;
4498
4499 if (width * bytespp * height > size)
4500 return -1;
4501
4502 return rer->surface_copy_content(surface, target, size,
4503 src_x, src_y, width, height);
4504}
4505
Pekka Paalanene67858b2013-04-25 13:57:42 +03004506static void
4507subsurface_set_position(struct wl_client *client,
4508 struct wl_resource *resource, int32_t x, int32_t y)
4509{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004510 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004511
4512 if (!sub)
4513 return;
4514
4515 sub->position.x = x;
4516 sub->position.y = y;
4517 sub->position.set = 1;
4518}
4519
4520static struct weston_subsurface *
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004521subsurface_find_sibling(struct weston_subsurface *sub,
4522 struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004523{
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004524 struct weston_surface *parent = sub->parent;
4525 struct weston_subsurface *sibling;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004526
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004527 wl_list_for_each(sibling, &parent->subsurface_list, parent_link) {
4528 if (sibling->surface == surface && sibling != sub)
4529 return sibling;
4530 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004531
4532 return NULL;
4533}
4534
4535static struct weston_subsurface *
4536subsurface_sibling_check(struct weston_subsurface *sub,
4537 struct weston_surface *surface,
4538 const char *request)
4539{
4540 struct weston_subsurface *sibling;
4541
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004542 sibling = subsurface_find_sibling(sub, surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004543 if (!sibling) {
4544 wl_resource_post_error(sub->resource,
4545 WL_SUBSURFACE_ERROR_BAD_SURFACE,
4546 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004547 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004548 return NULL;
4549 }
4550
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004551 assert(sibling->parent == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004552
4553 return sibling;
4554}
4555
4556static void
4557subsurface_place_above(struct wl_client *client,
4558 struct wl_resource *resource,
4559 struct wl_resource *sibling_resource)
4560{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004561 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004562 struct weston_surface *surface =
4563 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004564 struct weston_subsurface *sibling;
4565
4566 if (!sub)
4567 return;
4568
4569 sibling = subsurface_sibling_check(sub, surface, "place_above");
4570 if (!sibling)
4571 return;
4572
4573 wl_list_remove(&sub->parent_link_pending);
4574 wl_list_insert(sibling->parent_link_pending.prev,
4575 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004576
4577 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004578}
4579
4580static void
4581subsurface_place_below(struct wl_client *client,
4582 struct wl_resource *resource,
4583 struct wl_resource *sibling_resource)
4584{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004585 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004586 struct weston_surface *surface =
4587 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004588 struct weston_subsurface *sibling;
4589
4590 if (!sub)
4591 return;
4592
4593 sibling = subsurface_sibling_check(sub, surface, "place_below");
4594 if (!sibling)
4595 return;
4596
4597 wl_list_remove(&sub->parent_link_pending);
4598 wl_list_insert(&sibling->parent_link_pending,
4599 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004600
4601 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004602}
4603
4604static void
4605subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
4606{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004607 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004608
4609 if (sub)
4610 sub->synchronized = 1;
4611}
4612
4613static void
4614subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
4615{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004616 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004617
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004618 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03004619 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004620
4621 /* If sub became effectively desynchronized, flush. */
4622 if (!weston_subsurface_is_synchronized(sub))
4623 weston_subsurface_synchronized_commit(sub);
4624 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004625}
4626
4627static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03004628weston_subsurface_unlink_parent(struct weston_subsurface *sub)
4629{
4630 wl_list_remove(&sub->parent_link);
4631 wl_list_remove(&sub->parent_link_pending);
4632 wl_list_remove(&sub->parent_destroy_listener.link);
4633 sub->parent = NULL;
4634}
4635
4636static void
4637weston_subsurface_destroy(struct weston_subsurface *sub);
4638
4639static void
4640subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
4641{
4642 struct weston_subsurface *sub =
4643 container_of(listener, struct weston_subsurface,
4644 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004645 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004646
4647 /* The protocol object (wl_resource) is left inert. */
4648 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004649 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004650
4651 weston_subsurface_destroy(sub);
4652}
4653
4654static void
4655subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
4656{
4657 struct weston_subsurface *sub =
4658 container_of(listener, struct weston_subsurface,
4659 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004660 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004661 assert(sub->surface != sub->parent);
4662
4663 if (weston_surface_is_mapped(sub->surface))
4664 weston_surface_unmap(sub->surface);
4665
4666 weston_subsurface_unlink_parent(sub);
4667}
4668
4669static void
4670subsurface_resource_destroy(struct wl_resource *resource)
4671{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004672 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004673
4674 if (sub)
4675 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004676}
4677
4678static void
4679subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
4680{
4681 wl_resource_destroy(resource);
4682}
4683
4684static void
4685weston_subsurface_link_parent(struct weston_subsurface *sub,
4686 struct weston_surface *parent)
4687{
4688 sub->parent = parent;
4689 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004690 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004691 &sub->parent_destroy_listener);
4692
4693 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4694 wl_list_insert(&parent->subsurface_list_pending,
4695 &sub->parent_link_pending);
4696}
4697
4698static void
4699weston_subsurface_link_surface(struct weston_subsurface *sub,
4700 struct weston_surface *surface)
4701{
4702 sub->surface = surface;
4703 sub->surface_destroy_listener.notify =
4704 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004705 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004706 &sub->surface_destroy_listener);
4707}
4708
4709static void
4710weston_subsurface_destroy(struct weston_subsurface *sub)
4711{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004712 struct weston_view *view, *next;
4713
Pekka Paalanene67858b2013-04-25 13:57:42 +03004714 assert(sub->surface);
4715
4716 if (sub->resource) {
4717 assert(weston_surface_to_subsurface(sub->surface) == sub);
4718 assert(sub->parent_destroy_listener.notify ==
4719 subsurface_handle_parent_destroy);
4720
George Kiagiadakised04d382014-06-13 18:10:26 +02004721 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
4722 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004723 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02004724 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05004725
Pekka Paalanene67858b2013-04-25 13:57:42 +03004726 if (sub->parent)
4727 weston_subsurface_unlink_parent(sub);
4728
Jason Ekstrand7b982072014-05-20 14:33:03 -05004729 weston_surface_state_fini(&sub->cached);
4730 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004731
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004732 sub->surface->committed = NULL;
4733 sub->surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004734 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004735 } else {
4736 /* the dummy weston_subsurface for the parent itself */
4737 assert(sub->parent_destroy_listener.notify == NULL);
4738 wl_list_remove(&sub->parent_link);
4739 wl_list_remove(&sub->parent_link_pending);
4740 }
4741
4742 wl_list_remove(&sub->surface_destroy_listener.link);
4743 free(sub);
4744}
4745
4746static const struct wl_subsurface_interface subsurface_implementation = {
4747 subsurface_destroy,
4748 subsurface_set_position,
4749 subsurface_place_above,
4750 subsurface_place_below,
4751 subsurface_set_sync,
4752 subsurface_set_desync
4753};
4754
4755static struct weston_subsurface *
4756weston_subsurface_create(uint32_t id, struct weston_surface *surface,
4757 struct weston_surface *parent)
4758{
4759 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004760 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004761
Bryce Harringtonde16d892014-11-20 22:21:57 -08004762 sub = zalloc(sizeof *sub);
4763 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004764 return NULL;
4765
Jason Ekstranda7af7042013-10-12 22:38:11 -05004766 wl_list_init(&sub->unused_views);
4767
Jason Ekstranda85118c2013-06-27 20:17:02 -05004768 sub->resource =
4769 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004770 if (!sub->resource) {
4771 free(sub);
4772 return NULL;
4773 }
4774
Jason Ekstranda85118c2013-06-27 20:17:02 -05004775 wl_resource_set_implementation(sub->resource,
4776 &subsurface_implementation,
4777 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004778 weston_subsurface_link_surface(sub, surface);
4779 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05004780 weston_surface_state_init(&sub->cached);
4781 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004782 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004783
4784 return sub;
4785}
4786
4787/* Create a dummy subsurface for having the parent itself in its
4788 * sub-surface lists. Makes stacking order manipulation easy.
4789 */
4790static struct weston_subsurface *
4791weston_subsurface_create_for_parent(struct weston_surface *parent)
4792{
4793 struct weston_subsurface *sub;
4794
Bryce Harringtonde16d892014-11-20 22:21:57 -08004795 sub = zalloc(sizeof *sub);
4796 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004797 return NULL;
4798
4799 weston_subsurface_link_surface(sub, parent);
4800 sub->parent = parent;
4801 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4802 wl_list_insert(&parent->subsurface_list_pending,
4803 &sub->parent_link_pending);
4804
4805 return sub;
4806}
4807
4808static void
4809subcompositor_get_subsurface(struct wl_client *client,
4810 struct wl_resource *resource,
4811 uint32_t id,
4812 struct wl_resource *surface_resource,
4813 struct wl_resource *parent_resource)
4814{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004815 struct weston_surface *surface =
4816 wl_resource_get_user_data(surface_resource);
4817 struct weston_surface *parent =
4818 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004819 struct weston_subsurface *sub;
4820 static const char where[] = "get_subsurface: wl_subsurface@";
4821
4822 if (surface == parent) {
4823 wl_resource_post_error(resource,
4824 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4825 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004826 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004827 return;
4828 }
4829
4830 if (weston_surface_to_subsurface(surface)) {
4831 wl_resource_post_error(resource,
4832 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4833 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004834 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004835 return;
4836 }
4837
Pekka Paalanen50b67472014-10-01 15:02:41 +03004838 if (weston_surface_set_role(surface, "wl_subsurface", resource,
4839 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004840 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004841
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004842 if (weston_surface_get_main_surface(parent) == surface) {
4843 wl_resource_post_error(resource,
4844 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4845 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004846 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004847 return;
4848 }
4849
Pekka Paalanene67858b2013-04-25 13:57:42 +03004850 /* make sure the parent is in its own list */
4851 if (wl_list_empty(&parent->subsurface_list)) {
4852 if (!weston_subsurface_create_for_parent(parent)) {
4853 wl_resource_post_no_memory(resource);
4854 return;
4855 }
4856 }
4857
4858 sub = weston_subsurface_create(id, surface, parent);
4859 if (!sub) {
4860 wl_resource_post_no_memory(resource);
4861 return;
4862 }
4863
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004864 surface->committed = subsurface_committed;
4865 surface->committed_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004866 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004867}
4868
4869static void
4870subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
4871{
4872 wl_resource_destroy(resource);
4873}
4874
4875static const struct wl_subcompositor_interface subcompositor_interface = {
4876 subcompositor_destroy,
4877 subcompositor_get_subsurface
4878};
4879
4880static void
4881bind_subcompositor(struct wl_client *client,
4882 void *data, uint32_t version, uint32_t id)
4883{
4884 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004885 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004886
Jason Ekstranda85118c2013-06-27 20:17:02 -05004887 resource =
4888 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004889 if (resource == NULL) {
4890 wl_client_post_no_memory(client);
4891 return;
4892 }
4893 wl_resource_set_implementation(resource, &subcompositor_interface,
4894 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004895}
4896
Bryce Harrington0795ece2016-08-30 12:04:26 -07004897/** Set a DPMS mode on all of the compositor's outputs
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004898 *
4899 * \param compositor The compositor instance
4900 * \param state The DPMS state the outputs will be set to
4901 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004902static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004903weston_compositor_dpms(struct weston_compositor *compositor,
4904 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004905{
4906 struct weston_output *output;
4907
Bryce Harrington08976ac2016-08-30 12:05:16 -07004908 wl_list_for_each(output, &compositor->output_list, link)
4909 if (output->set_dpms)
4910 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004911}
4912
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004913/** Restores the compositor to active status
4914 *
4915 * \param compositor The compositor instance
4916 *
4917 * If the compositor was in a sleeping mode, all outputs are powered
4918 * back on via DPMS. Otherwise if the compositor was inactive
4919 * (idle/locked, offscreen, or sleeping) then the compositor's wake
4920 * signal will fire.
4921 *
4922 * Restarts the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004923 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004924 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004925WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004926weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004927{
Neil Roberts8b62e202013-09-30 13:14:47 +01004928 uint32_t old_state = compositor->state;
4929
4930 /* The state needs to be changed before emitting the wake
4931 * signal because that may try to schedule a repaint which
4932 * will not work if the compositor is still sleeping */
4933 compositor->state = WESTON_COMPOSITOR_ACTIVE;
4934
4935 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004936 case WESTON_COMPOSITOR_SLEEPING:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004937 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004938 case WESTON_COMPOSITOR_OFFSCREEN:
Daniel Stone893b9362016-11-08 15:47:09 +00004939 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004940 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004941 /* fall through */
4942 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004943 wl_event_source_timer_update(compositor->idle_source,
4944 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004945 }
4946}
4947
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004948/** Turns off rendering and frame events for the compositor.
4949 *
4950 * \param compositor The compositor instance
4951 *
4952 * This is used for example to prevent further rendering while the
4953 * compositor is shutting down.
4954 *
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004955 * Stops the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004956 *
4957 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004958 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004959WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004960weston_compositor_offscreen(struct weston_compositor *compositor)
4961{
4962 switch (compositor->state) {
4963 case WESTON_COMPOSITOR_OFFSCREEN:
4964 return;
4965 case WESTON_COMPOSITOR_SLEEPING:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004966 default:
4967 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4968 wl_event_source_timer_update(compositor->idle_source, 0);
4969 }
4970}
4971
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004972/** Powers down all attached output devices
4973 *
4974 * \param compositor The compositor instance
4975 *
4976 * Causes rendering to the outputs to cease, and no frame events to be
4977 * sent. Only powers down the outputs if the compositor is not already
4978 * in sleep mode.
4979 *
4980 * Stops the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004981 *
4982 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004983 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004984WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004985weston_compositor_sleep(struct weston_compositor *compositor)
4986{
4987 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
4988 return;
4989
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004990 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004991 compositor->state = WESTON_COMPOSITOR_SLEEPING;
4992 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
4993}
4994
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004995/** Sets compositor to idle mode
4996 *
4997 * \param data The compositor instance
4998 *
4999 * This is called when the idle timer fires. Once the compositor is in
5000 * idle mode it requires a wake action (e.g. via
5001 * weston_compositor_wake()) to restore it. The compositor's
5002 * idle_signal will be triggered when the idle event occurs.
5003 *
5004 * Idleness can be inhibited by setting the compositor's idle_inhibit
5005 * property.
5006 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04005007static int
5008idle_handler(void *data)
5009{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005010 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04005011
5012 if (compositor->idle_inhibit)
5013 return 1;
5014
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005015 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005016 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04005017
5018 return 1;
5019}
5020
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005021WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08005022weston_plane_init(struct weston_plane *plane,
5023 struct weston_compositor *ec,
5024 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005025{
5026 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02005027 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005028 plane->x = x;
5029 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08005030 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03005031
5032 /* Init the link so that the call to wl_list_remove() when releasing
5033 * the plane without ever stacking doesn't lead to a crash */
5034 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005035}
5036
5037WL_EXPORT void
5038weston_plane_release(struct weston_plane *plane)
5039{
Xiong Zhang97116532013-10-23 13:58:31 +08005040 struct weston_view *view;
5041
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005042 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02005043 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03005044
Pekka Paalanen6551c092021-05-03 16:09:45 +03005045 /*
5046 * Can't use paint node list here, weston_plane is not specific to an
5047 * output.
5048 */
Xiong Zhang97116532013-10-23 13:58:31 +08005049 wl_list_for_each(view, &plane->compositor->view_list, link) {
5050 if (view->plane == plane)
5051 view->plane = NULL;
5052 }
5053
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03005054 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005055}
5056
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005057/** weston_compositor_stack_plane
5058 * \ingroup compositor
5059 */
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02005060WL_EXPORT void
5061weston_compositor_stack_plane(struct weston_compositor *ec,
5062 struct weston_plane *plane,
5063 struct weston_plane *above)
5064{
5065 if (above)
5066 wl_list_insert(above->link.prev, &plane->link);
5067 else
5068 wl_list_insert(&ec->plane_list, &plane->link);
5069}
5070
Quentin Glidic4ef719c2016-07-05 20:44:33 +02005071static void
5072output_release(struct wl_client *client, struct wl_resource *resource)
5073{
5074 wl_resource_destroy(resource);
5075}
5076
5077static const struct wl_output_interface output_interface = {
5078 output_release,
5079};
5080
5081
Casey Dahlin9074db52012-04-19 22:50:09 -04005082static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04005083{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05005084 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04005085}
5086
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005087static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005088bind_output(struct wl_client *client,
5089 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05005090{
Pekka Paalanen05347622017-03-27 12:24:34 +03005091 struct weston_head *head = data;
5092 struct weston_output *output = head->output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005093 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04005094 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05005095
Jason Ekstranda85118c2013-06-27 20:17:02 -05005096 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06005097 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07005098 if (resource == NULL) {
5099 wl_client_post_no_memory(client);
5100 return;
5101 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04005102
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005103 wl_list_insert(&head->resource_list, wl_resource_get_link(resource));
Pekka Paalanen055c1132017-03-27 16:31:25 +03005104 wl_resource_set_implementation(resource, &output_interface, head,
Pekka Paalanen05347622017-03-27 12:24:34 +03005105 unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04005106
Pekka Paalanen05347622017-03-27 12:24:34 +03005107 assert(output);
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05005108 wl_output_send_geometry(resource,
5109 output->x,
5110 output->y,
Pekka Paalanen01f60212017-03-24 15:39:24 +02005111 head->mm_width,
5112 head->mm_height,
5113 head->subpixel,
5114 head->make, head->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04005115 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04005116 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02005117 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02005118 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005119
5120 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05005121 wl_output_send_mode(resource,
5122 mode->flags,
5123 mode->width,
5124 mode->height,
5125 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005126 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02005127
Jasper St. Pierre0013a292014-08-07 16:43:11 -04005128 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02005129 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05005130}
5131
Pekka Paalanendcac3512017-12-08 14:13:34 +02005132static void
5133weston_head_add_global(struct weston_head *head)
5134{
5135 head->global = wl_global_create(head->compositor->wl_display,
5136 &wl_output_interface, 3,
5137 head, bind_output);
5138}
5139
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02005140/** Remove the global wl_output protocol object
5141 *
5142 * \param head The head whose global to remove.
5143 *
5144 * Also orphans the wl_resources for this head (wl_output).
5145 */
5146static void
5147weston_head_remove_global(struct weston_head *head)
5148{
5149 struct wl_resource *resource, *tmp;
5150
5151 if (head->global)
5152 wl_global_destroy(head->global);
5153 head->global = NULL;
5154
5155 wl_resource_for_each_safe(resource, tmp, &head->resource_list) {
5156 unbind_resource(resource);
5157 wl_resource_set_destructor(resource, NULL);
5158 wl_resource_set_user_data(resource, NULL);
5159 }
Roman Gilge97391c2019-03-29 13:01:06 +01005160
5161 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
5162 /* It's sufficient to unset the destructor, then the list elements
5163 * won't be accessed.
5164 */
5165 wl_resource_set_destructor(resource, NULL);
5166 }
5167 wl_list_init(&head->xdg_output_resource_list);
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02005168}
5169
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005170/** Get the backing object of wl_output
5171 *
5172 * \param resource A wl_output protocol object.
5173 * \return The backing object (user data) of a wl_resource representing a
5174 * wl_output protocol object.
Marius Vlad78984ee2019-06-11 00:05:08 +03005175 *
5176 * \ingroup head
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005177 */
Pekka Paalanen055c1132017-03-27 16:31:25 +03005178WL_EXPORT struct weston_head *
5179weston_head_from_resource(struct wl_resource *resource)
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005180{
5181 assert(wl_resource_instance_of(resource, &wl_output_interface,
5182 &output_interface));
5183
5184 return wl_resource_get_user_data(resource);
5185}
5186
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005187/** Initialize a pre-allocated weston_head
5188 *
5189 * \param head The head to initialize.
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005190 * \param name The head name, e.g. the connector name or equivalent.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005191 *
5192 * The head will be safe to attach, detach and release.
5193 *
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005194 * The name is used in logs, and can be used by compositors as a configuration
5195 * identifier.
5196 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005197 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005198 * \internal
5199 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005200WL_EXPORT void
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005201weston_head_init(struct weston_head *head, const char *name)
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005202{
5203 /* Add some (in)sane defaults which can be used
5204 * for checking if an output was properly configured
5205 */
5206 memset(head, 0, sizeof *head);
5207
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005208 wl_list_init(&head->compositor_link);
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005209 wl_signal_init(&head->destroy_signal);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005210 wl_list_init(&head->output_link);
5211 wl_list_init(&head->resource_list);
Roman Gilge97391c2019-03-29 13:01:06 +01005212 wl_list_init(&head->xdg_output_resource_list);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005213 head->name = strdup(name);
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305214 head->current_protection = WESTON_HDCP_DISABLE;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005215}
5216
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005217/** Send output heads changed signal
5218 *
5219 * \param output The output that changed.
5220 *
5221 * Notify that the enabled output gained and/or lost heads, or that the
5222 * associated heads may have changed their connection status. This does not
5223 * include cases where the output becomes enabled or disabled. The registered
5224 * callbacks are called after the change has successfully happened.
5225 *
5226 * If connection status change causes the compositor to attach or detach a head
5227 * to an enabled output, the registered callbacks may be called multiple times.
Marius Vlad55d87362019-06-11 01:15:35 +03005228 *
5229 * \ingroup output
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005230 */
5231static void
5232weston_output_emit_heads_changed(struct weston_output *output)
5233{
5234 wl_signal_emit(&output->compositor->output_heads_changed_signal,
5235 output);
5236}
5237
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005238/** Idle task for emitting heads_changed_signal */
5239static void
5240weston_compositor_call_heads_changed(void *data)
5241{
5242 struct weston_compositor *compositor = data;
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005243 struct weston_head *head;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005244
5245 compositor->heads_changed_source = NULL;
5246
5247 wl_signal_emit(&compositor->heads_changed_signal, compositor);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005248
5249 wl_list_for_each(head, &compositor->head_list, compositor_link) {
5250 if (head->output && head->output->enabled)
5251 weston_output_emit_heads_changed(head->output);
5252 }
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005253}
5254
5255/** Schedule a call on idle to heads_changed callback
5256 *
5257 * \param compositor The Compositor.
5258 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005259 * \ingroup compositor
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005260 * \internal
5261 */
5262static void
5263weston_compositor_schedule_heads_changed(struct weston_compositor *compositor)
5264{
5265 struct wl_event_loop *loop;
5266
5267 if (compositor->heads_changed_source)
5268 return;
5269
5270 loop = wl_display_get_event_loop(compositor->wl_display);
5271 compositor->heads_changed_source = wl_event_loop_add_idle(loop,
5272 weston_compositor_call_heads_changed, compositor);
5273}
5274
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005275/** Register a new head
5276 *
5277 * \param compositor The compositor.
5278 * \param head The head to register, must not be already registered.
5279 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005280 * This signals the core that a new head has become available, leading to
5281 * heads_changed hook being called later.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005282 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005283 * \ingroup compositor
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005284 * \internal
5285 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005286WL_EXPORT void
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005287weston_compositor_add_head(struct weston_compositor *compositor,
5288 struct weston_head *head)
5289{
5290 assert(wl_list_empty(&head->compositor_link));
5291 assert(head->name);
5292
5293 wl_list_insert(compositor->head_list.prev, &head->compositor_link);
5294 head->compositor = compositor;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005295 weston_compositor_schedule_heads_changed(compositor);
5296}
5297
5298/** Adds a listener to be called when heads change
5299 *
5300 * \param compositor The compositor.
5301 * \param listener The listener to add.
5302 *
Marius Vlada2dace22019-06-12 16:05:44 +03005303 * The listener notify function argument is weston_compositor.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005304 *
5305 * The listener function will be called after heads are added or their
5306 * connection status has changed. Several changes may be accumulated into a
5307 * single call. The user is expected to iterate over the existing heads and
5308 * check their statuses to find out what changed.
5309 *
5310 * \sa weston_compositor_iterate_heads, weston_head_is_connected,
5311 * weston_head_is_enabled
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005312 * \ingroup compositor
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005313 */
5314WL_EXPORT void
5315weston_compositor_add_heads_changed_listener(struct weston_compositor *compositor,
5316 struct wl_listener *listener)
5317{
5318 wl_signal_add(&compositor->heads_changed_signal, listener);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005319}
5320
5321/** Iterate over available heads
5322 *
5323 * \param compositor The compositor.
Marius Vlada2dace22019-06-12 16:05:44 +03005324 * \param iter The iterator, or NULL for start.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005325 * \return The next available head in the list.
5326 *
5327 * Returns all available heads, regardless of being connected or enabled.
5328 *
5329 * You can iterate over all heads as follows:
5330 * \code
5331 * struct weston_head *head = NULL;
5332 *
5333 * while ((head = weston_compositor_iterate_heads(compositor, head))) {
5334 * ...
5335 * }
5336 * \endcode
5337 *
5338 * If you cause \c iter to be removed from the list, you cannot use it to
5339 * continue iterating. Removing any other item is safe.
5340 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005341 * \ingroup compositor
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005342 */
5343WL_EXPORT struct weston_head *
5344weston_compositor_iterate_heads(struct weston_compositor *compositor,
5345 struct weston_head *iter)
5346{
5347 struct wl_list *list = &compositor->head_list;
5348 struct wl_list *node;
5349
5350 assert(compositor);
5351 assert(!iter || iter->compositor == compositor);
5352
5353 if (iter)
5354 node = iter->compositor_link.next;
5355 else
5356 node = list->next;
5357
5358 assert(node);
5359 assert(!iter || node != &iter->compositor_link);
5360
5361 if (node == list)
5362 return NULL;
5363
5364 return container_of(node, struct weston_head, compositor_link);
5365}
5366
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005367/** Iterate over attached heads
5368 *
5369 * \param output The output whose heads to iterate.
Marius Vlada2dace22019-06-12 16:05:44 +03005370 * \param iter The iterator, or NULL for start.
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005371 * \return The next attached head in the list.
5372 *
5373 * Returns all heads currently attached to the output.
5374 *
5375 * You can iterate over all heads as follows:
5376 * \code
5377 * struct weston_head *head = NULL;
5378 *
5379 * while ((head = weston_output_iterate_heads(output, head))) {
5380 * ...
5381 * }
5382 * \endcode
5383 *
5384 * If you cause \c iter to be removed from the list, you cannot use it to
5385 * continue iterating. Removing any other item is safe.
5386 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05005387 * \ingroup output
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005388 */
5389WL_EXPORT struct weston_head *
5390weston_output_iterate_heads(struct weston_output *output,
5391 struct weston_head *iter)
5392{
5393 struct wl_list *list = &output->head_list;
5394 struct wl_list *node;
5395
5396 assert(output);
5397 assert(!iter || iter->output == output);
5398
5399 if (iter)
5400 node = iter->output_link.next;
5401 else
5402 node = list->next;
5403
5404 assert(node);
5405 assert(!iter || node != &iter->output_link);
5406
5407 if (node == list)
5408 return NULL;
5409
5410 return container_of(node, struct weston_head, output_link);
5411}
5412
Pekka Paalanendcac3512017-12-08 14:13:34 +02005413/** Attach a head to an output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005414 *
5415 * \param output The output to attach to.
5416 * \param head The head that is not yet attached.
5417 * \return 0 on success, -1 on failure.
5418 *
5419 * Attaches the given head to the output. All heads of an output are clones
5420 * and share the resolution and timings.
5421 *
5422 * Cloning heads this way uses less resources than creating an output for
5423 * each head, but is not always possible due to environment, driver and hardware
5424 * limitations.
5425 *
5426 * On failure, the head remains unattached. Success of this function does not
5427 * guarantee the output configuration is actually valid. The final checks are
Pekka Paalanendcac3512017-12-08 14:13:34 +02005428 * made on weston_output_enable() unless the output was already enabled.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005429 *
Marius Vlad55d87362019-06-11 01:15:35 +03005430 * \ingroup output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005431 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005432WL_EXPORT int
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005433weston_output_attach_head(struct weston_output *output,
5434 struct weston_head *head)
5435{
Pekka Paalanendcac3512017-12-08 14:13:34 +02005436 char *head_names;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005437
5438 if (!wl_list_empty(&head->output_link))
5439 return -1;
5440
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005441 if (output->attach_head) {
5442 if (output->attach_head(output, head) < 0)
5443 return -1;
5444 } else if (!wl_list_empty(&output->head_list)) {
5445 /* No support for clones in the legacy path. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005446 return -1;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005447 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005448
5449 head->output = output;
5450 wl_list_insert(output->head_list.prev, &head->output_link);
5451
Pekka Paalanendcac3512017-12-08 14:13:34 +02005452 if (output->enabled) {
5453 weston_head_add_global(head);
5454
5455 head_names = weston_output_create_heads_string(output);
5456 weston_log("Output '%s' updated to have head(s) %s\n",
5457 output->name, head_names);
5458 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005459
5460 weston_output_emit_heads_changed(output);
Pekka Paalanendcac3512017-12-08 14:13:34 +02005461 }
5462
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005463 return 0;
5464}
5465
5466/** Detach a head from its output
5467 *
5468 * \param head The head to detach.
5469 *
5470 * It is safe to detach a non-attached head.
5471 *
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005472 * If the head is attached to an enabled output and the output will be left
5473 * with no heads, the output will be disabled.
5474 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005475 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005476 * \sa weston_output_disable
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005477 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005478WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005479weston_head_detach(struct weston_head *head)
5480{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005481 struct weston_output *output = head->output;
Pekka Paalanena0106992017-12-08 16:11:17 +02005482 char *head_names;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005483
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005484 wl_list_remove(&head->output_link);
5485 wl_list_init(&head->output_link);
5486 head->output = NULL;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005487
5488 if (!output)
5489 return;
5490
5491 if (output->detach_head)
5492 output->detach_head(output, head);
5493
5494 if (output->enabled) {
5495 weston_head_remove_global(head);
5496
Pekka Paalanena0106992017-12-08 16:11:17 +02005497 if (wl_list_empty(&output->head_list)) {
5498 weston_log("Output '%s' no heads left, disabling.\n",
5499 output->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005500 weston_output_disable(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02005501 } else {
5502 head_names = weston_output_create_heads_string(output);
5503 weston_log("Output '%s' updated to have head(s) %s\n",
5504 output->name, head_names);
5505 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005506
5507 weston_output_emit_heads_changed(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02005508 }
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005509 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005510}
5511
5512/** Destroy a head
5513 *
5514 * \param head The head to be released.
5515 *
5516 * Destroys the head. The caller is responsible for freeing the memory pointed
5517 * to by \c head.
5518 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005519 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005520 * \internal
5521 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005522WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005523weston_head_release(struct weston_head *head)
5524{
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005525 wl_signal_emit(&head->destroy_signal, head);
5526
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005527 weston_head_detach(head);
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005528
5529 free(head->make);
5530 free(head->model);
5531 free(head->serial_number);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005532 free(head->name);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005533
5534 wl_list_remove(&head->compositor_link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005535}
5536
Pekka Paalanene19970f2017-08-28 14:11:02 +03005537static void
5538weston_head_set_device_changed(struct weston_head *head)
5539{
5540 head->device_changed = true;
5541
5542 if (head->compositor)
5543 weston_compositor_schedule_heads_changed(head->compositor);
5544}
5545
5546/** String equal comparison with NULLs being equal */
5547static bool
5548str_null_eq(const char *a, const char *b)
5549{
5550 if (!a && !b)
5551 return true;
5552
5553 if (!!a != !!b)
5554 return false;
5555
5556 return strcmp(a, b) == 0;
5557}
5558
Pekka Paalanen01f60212017-03-24 15:39:24 +02005559/** Store monitor make, model and serial number
5560 *
5561 * \param head The head to modify.
5562 * \param make The monitor make. If EDID is available, the PNP ID. Otherwise
5563 * any string, or NULL for none.
5564 * \param model The monitor model or name, or a made-up string, or NULL for
5565 * none.
5566 * \param serialno The monitor serial number, a made-up string, or NULL for
5567 * none.
5568 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005569 * This may set the device_changed flag.
5570 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005571 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005572 * \internal
5573 */
5574WL_EXPORT void
5575weston_head_set_monitor_strings(struct weston_head *head,
5576 const char *make,
5577 const char *model,
5578 const char *serialno)
5579{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005580 if (str_null_eq(head->make, make) &&
5581 str_null_eq(head->model, model) &&
5582 str_null_eq(head->serial_number, serialno))
5583 return;
5584
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005585 free(head->make);
5586 free(head->model);
5587 free(head->serial_number);
5588
5589 head->make = make ? strdup(make) : NULL;
5590 head->model = model ? strdup(model) : NULL;
5591 head->serial_number = serialno ? strdup(serialno) : NULL;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005592
5593 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005594}
5595
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005596/** Store display non-desktop status
5597 *
5598 * \param head The head to modify.
5599 * \param non_desktop Whether the head connects to a non-desktop display.
5600 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005601 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005602 * \internal
5603 */
5604WL_EXPORT void
5605weston_head_set_non_desktop(struct weston_head *head, bool non_desktop)
5606{
5607 if (head->non_desktop == non_desktop)
5608 return;
5609
5610 head->non_desktop = non_desktop;
5611
5612 weston_head_set_device_changed(head);
5613}
5614
Lucas Stacha69cb712019-11-25 23:29:31 +00005615/** Store display transformation
5616 *
5617 * \param head The head to modify.
5618 * \param transform The transformation to apply for this head
5619 *
5620 * This may set the device_changed flag.
5621 *
5622 * \ingroup head
5623 * \internal
5624 */
5625WL_EXPORT void
5626weston_head_set_transform(struct weston_head *head, uint32_t transform)
5627{
5628 if (head->transform == transform)
5629 return;
5630
5631 head->transform = transform;
5632
5633 weston_head_set_device_changed(head);
5634}
5635
5636
Pekka Paalanen01f60212017-03-24 15:39:24 +02005637/** Store physical image size
5638 *
5639 * \param head The head to modify.
5640 * \param mm_width Image area width in millimeters.
5641 * \param mm_height Image area height in millimeters.
5642 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005643 * This may set the device_changed flag.
5644 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005645 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005646 * \internal
5647 */
5648WL_EXPORT void
5649weston_head_set_physical_size(struct weston_head *head,
5650 int32_t mm_width, int32_t mm_height)
5651{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005652 if (head->mm_width == mm_width &&
5653 head->mm_height == mm_height)
5654 return;
5655
Pekka Paalanen01f60212017-03-24 15:39:24 +02005656 head->mm_width = mm_width;
5657 head->mm_height = mm_height;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005658
5659 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005660}
5661
5662/** Store monitor sub-pixel layout
5663 *
5664 * \param head The head to modify.
5665 * \param sp Sub-pixel layout. The possible values are:
5666 * - WL_OUTPUT_SUBPIXEL_UNKNOWN,
5667 * - WL_OUTPUT_SUBPIXEL_NONE,
5668 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB,
5669 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR,
5670 * - WL_OUTPUT_SUBPIXEL_VERTICAL_RGB,
5671 * - WL_OUTPUT_SUBPIXEL_VERTICAL_BGR
5672 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005673 * This may set the device_changed flag.
5674 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005675 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005676 * \internal
5677 */
5678WL_EXPORT void
5679weston_head_set_subpixel(struct weston_head *head,
5680 enum wl_output_subpixel sp)
5681{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005682 if (head->subpixel == sp)
5683 return;
5684
Pekka Paalanen01f60212017-03-24 15:39:24 +02005685 head->subpixel = sp;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005686
5687 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005688}
5689
5690/** Mark the monitor as internal
5691 *
5692 * This is used for embedded screens, like laptop panels.
5693 *
5694 * \param head The head to mark as internal.
5695 *
5696 * By default a head is external. The type is often inferred from the physical
5697 * connector type.
5698 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005699 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005700 * \internal
5701 */
5702WL_EXPORT void
5703weston_head_set_internal(struct weston_head *head)
5704{
5705 head->connection_internal = true;
5706}
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005707
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005708/** Store connector status
5709 *
5710 * \param head The head to modify.
5711 * \param connected Whether the head is connected.
5712 *
5713 * Connectors are created as disconnected. This function can be used to
5714 * set the connector status.
5715 *
5716 * The status should be set to true when a physical connector is connected to
5717 * a video sink device like a monitor and to false when the connector is
5718 * disconnected. For nested backends, the connection status should reflect the
5719 * connection to the parent display server.
5720 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005721 * When the connection status changes, it schedules a call to the heads_changed
Pekka Paalanene19970f2017-08-28 14:11:02 +03005722 * hook and sets the device_changed flag.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005723 *
5724 * \sa weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005725 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005726 * \internal
5727 */
5728WL_EXPORT void
5729weston_head_set_connection_status(struct weston_head *head, bool connected)
5730{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005731 if (head->connected == connected)
5732 return;
5733
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005734 head->connected = connected;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005735
Pekka Paalanene19970f2017-08-28 14:11:02 +03005736 weston_head_set_device_changed(head);
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005737}
5738
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305739static void
5740weston_output_compute_protection(struct weston_output *output)
5741{
5742 struct weston_head *head;
5743 enum weston_hdcp_protection op_protection;
5744 bool op_protection_valid = false;
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305745 struct weston_compositor *wc = output->compositor;
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305746
5747 wl_list_for_each(head, &output->head_list, output_link) {
5748 if (!op_protection_valid) {
5749 op_protection = head->current_protection;
5750 op_protection_valid = true;
5751 }
5752 if (head->current_protection < op_protection)
5753 op_protection = head->current_protection;
5754 }
5755
5756 if (!op_protection_valid)
5757 op_protection = WESTON_HDCP_DISABLE;
5758
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305759 if (output->current_protection != op_protection) {
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305760 output->current_protection = op_protection;
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305761 weston_output_damage(output);
Ankit Nautiyalfaa5ab42019-07-08 13:23:24 +05305762 weston_schedule_surface_protection_update(wc);
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305763 }
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305764}
5765
5766WL_EXPORT void
5767weston_head_set_content_protection_status(struct weston_head *head,
5768 enum weston_hdcp_protection status)
5769{
5770 head->current_protection = status;
5771 if (head->output)
5772 weston_output_compute_protection(head->output);
5773}
5774
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005775/** Is the head currently connected?
5776 *
5777 * \param head The head to query.
5778 * \return Connection status.
5779 *
5780 * Returns true if the head is physically connected to a monitor, or in
5781 * case of a nested backend returns true when there is a connection to the
5782 * parent display server.
5783 *
5784 * This is independent from the head being enabled.
5785 *
5786 * \sa weston_head_is_enabled
Marius Vlad78984ee2019-06-11 00:05:08 +03005787 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005788 */
5789WL_EXPORT bool
5790weston_head_is_connected(struct weston_head *head)
5791{
5792 return head->connected;
5793}
5794
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005795/** Is the head currently enabled?
5796 *
5797 * \param head The head to query.
5798 * \return Video status.
5799 *
5800 * Returns true if the head is currently transmitting a video stream.
5801 *
5802 * This is independent of the head being connected.
5803 *
5804 * \sa weston_head_is_connected
Marius Vlad78984ee2019-06-11 00:05:08 +03005805 * \ingroup head
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005806 */
5807WL_EXPORT bool
5808weston_head_is_enabled(struct weston_head *head)
5809{
5810 if (!head->output)
5811 return false;
5812
5813 return head->output->enabled;
5814}
5815
Pekka Paalanene19970f2017-08-28 14:11:02 +03005816/** Has the device information changed?
5817 *
5818 * \param head The head to query.
5819 * \return True if the device information has changed since last reset.
5820 *
5821 * The information about the connected display device, e.g. a monitor, may
5822 * change without being disconnected in between. Changing information
5823 * causes a call to the heads_changed hook.
5824 *
5825 * The information includes make, model, serial number, physical size,
5826 * and sub-pixel type. The connection status is also included.
5827 *
5828 * \sa weston_head_reset_device_changed, weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005829 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03005830 */
5831WL_EXPORT bool
5832weston_head_is_device_changed(struct weston_head *head)
5833{
5834 return head->device_changed;
5835}
5836
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005837/** Does the head represent a non-desktop display?
5838 *
5839 * \param head The head to query.
5840 * \return True if the device is a non-desktop display.
5841 *
5842 * Non-desktop heads are not attached to outputs by default.
5843 * This stops weston from extending the desktop onto head mounted displays.
5844 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005845 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005846 */
5847WL_EXPORT bool
5848weston_head_is_non_desktop(struct weston_head *head)
5849{
5850 return head->non_desktop;
5851}
5852
Pekka Paalanene19970f2017-08-28 14:11:02 +03005853/** Acknowledge device information change
5854 *
5855 * \param head The head to acknowledge.
5856 *
5857 * Clears the device changed flag on this head. When a compositor has processed
5858 * device information, it should call this to be able to notice further
5859 * changes.
5860 *
5861 * \sa weston_head_is_device_changed
Marius Vlad78984ee2019-06-11 00:05:08 +03005862 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03005863 */
5864WL_EXPORT void
5865weston_head_reset_device_changed(struct weston_head *head)
5866{
5867 head->device_changed = false;
5868}
5869
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005870/** Get the name of a head
5871 *
5872 * \param head The head to query.
5873 * \return The head's name, not NULL.
5874 *
5875 * The name depends on the backend. The DRM backend uses connector names,
5876 * other backends may use hardcoded names or user-given names.
Marius Vlad78984ee2019-06-11 00:05:08 +03005877 *
5878 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005879 */
5880WL_EXPORT const char *
5881weston_head_get_name(struct weston_head *head)
5882{
5883 return head->name;
5884}
5885
5886/** Get the output the head is attached to
5887 *
5888 * \param head The head to query.
5889 * \return The output the head is attached to, or NULL if detached.
Marius Vlad78984ee2019-06-11 00:05:08 +03005890 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005891 */
5892WL_EXPORT struct weston_output *
5893weston_head_get_output(struct weston_head *head)
5894{
5895 return head->output;
5896}
5897
Lucas Stacha69cb712019-11-25 23:29:31 +00005898/** Get the head's native transformation
5899 *
5900 * \param head The head to query.
5901 * \return The head's native transform, as a WL_OUTPUT_TRANSFORM_* value
5902 *
5903 * A weston_head may have a 'native' transform provided by the backend.
5904 * Examples include panels which are physically rotated, where the rotation
5905 * is recorded and described as part of the system configuration. This call
5906 * will return any known native transform for the head.
5907 *
5908 * \ingroup head
5909 */
5910WL_EXPORT uint32_t
5911weston_head_get_transform(struct weston_head *head)
5912{
5913 return head->transform;
5914}
5915
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005916/** Add destroy callback for a head
5917 *
5918 * \param head The head to watch for.
5919 * \param listener The listener to add. The \c notify member must be set.
5920 *
5921 * Heads may get destroyed for various reasons by the backends. If a head is
5922 * attached to an output, the compositor should listen for head destruction
5923 * and reconfigure or destroy the output if necessary.
5924 *
5925 * The destroy callbacks will be called on weston_head destruction before any
5926 * automatic detaching from an associated weston_output and before any
5927 * weston_head information is lost.
5928 *
5929 * The \c data argument to the notify callback is the weston_head being
5930 * destroyed.
Marius Vlad78984ee2019-06-11 00:05:08 +03005931 *
5932 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005933 */
5934WL_EXPORT void
5935weston_head_add_destroy_listener(struct weston_head *head,
5936 struct wl_listener *listener)
5937{
5938 wl_signal_add(&head->destroy_signal, listener);
5939}
5940
5941/** Look up destroy listener for a head
5942 *
5943 * \param head The head to query.
5944 * \param notify The notify function used used for the added destroy listener.
5945 * \return The listener, or NULL if not found.
5946 *
5947 * This looks up the previously added destroy listener struct based on the
5948 * notify function it has. The listener can be used to access user data
5949 * through \c container_of().
5950 *
5951 * \sa wl_signal_get()
Marius Vlad78984ee2019-06-11 00:05:08 +03005952 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005953 */
5954WL_EXPORT struct wl_listener *
5955weston_head_get_destroy_listener(struct weston_head *head,
5956 wl_notify_func_t notify)
5957{
5958 return wl_signal_get(&head->destroy_signal, notify);
5959}
5960
David Fort0de859e2016-05-27 23:22:57 +02005961/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005962static void
David Fort0de859e2016-05-27 23:22:57 +02005963weston_compositor_reflow_outputs(struct weston_compositor *compositor,
5964 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005965{
5966 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02005967 bool start_resizing = false;
5968
5969 if (!delta_width)
5970 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005971
5972 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02005973 if (output == resized_output) {
5974 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005975 continue;
5976 }
5977
David Fort0de859e2016-05-27 23:22:57 +02005978 if (start_resizing) {
5979 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005980 output->dirty = 1;
5981 }
5982 }
5983}
5984
Pekka Paalanen06920792020-12-08 15:22:36 +02005985/** Transform a region in-place from global to output coordinates
5986 *
5987 * \param output The output that defines the transformation.
5988 * \param region The region to be transformed in-place.
5989 *
5990 * This takes a region in the global coordinate system, and takes into account
5991 * output position, transform and scale, and the zoom, and converts the region
5992 * into output pixel coordinates in the framebuffer.
5993 *
5994 * Uses floating-point operations if zoom is active, which may round to expand
5995 * the region.
5996 *
5997 * \internal
5998 * \ingroup output
5999 */
6000WL_EXPORT void
6001weston_output_region_from_global(struct weston_output *output,
6002 pixman_region32_t *region)
6003{
6004 if (output->zoom.active) {
6005 weston_matrix_transform_region(region, &output->matrix, region);
6006 } else {
6007 pixman_region32_translate(region, -output->x, -output->y);
6008 weston_transformed_region(output->width, output->height,
6009 output->transform,
6010 output->current_scale,
6011 region, region);
6012 }
6013}
6014
Pekka Paalanend72bad22017-03-29 17:01:41 +03006015static void
Scott Moreauccbf29d2012-02-22 14:21:41 -07006016weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01006017{
Scott Moreau850ca422012-05-21 15:21:25 -06006018 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05006019
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006020 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05006021 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06006022
Scott Moreauccbf29d2012-02-22 14:21:41 -07006023 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06006024 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05006025 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01006026 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05006027 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01006028 weston_matrix_scale(&output->matrix, magnification,
6029 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07006030 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04006031
Jason Ekstrandfb23df72014-10-16 10:55:21 -05006032 switch (output->transform) {
6033 case WL_OUTPUT_TRANSFORM_FLIPPED:
6034 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
6035 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
6036 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
6037 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
6038 weston_matrix_scale(&output->matrix, -1, 1, 1);
6039 break;
6040 }
6041
6042 switch (output->transform) {
6043 default:
6044 case WL_OUTPUT_TRANSFORM_NORMAL:
6045 case WL_OUTPUT_TRANSFORM_FLIPPED:
6046 break;
6047 case WL_OUTPUT_TRANSFORM_90:
6048 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +02006049 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
6050 weston_matrix_rotate_xy(&output->matrix, 0, -1);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05006051 break;
6052 case WL_OUTPUT_TRANSFORM_180:
6053 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
6054 weston_matrix_translate(&output->matrix,
6055 -output->width, -output->height, 0);
6056 weston_matrix_rotate_xy(&output->matrix, -1, 0);
6057 break;
6058 case WL_OUTPUT_TRANSFORM_270:
6059 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +02006060 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
6061 weston_matrix_rotate_xy(&output->matrix, 0, 1);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05006062 break;
6063 }
6064
6065 if (output->current_scale != 1)
6066 weston_matrix_scale(&output->matrix,
6067 output->current_scale,
6068 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01006069
Scott Moreauccbf29d2012-02-22 14:21:41 -07006070 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05006071
6072 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07006073}
6074
Scott Moreau1bad5db2012-08-18 01:04:05 -06006075static void
Alexander Larsson0b135062013-05-28 16:23:36 +02006076weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06006077{
6078 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03006079 output->native_scale = scale;
6080 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06006081
Pekka Paalanen59987fa2016-04-26 15:50:59 +03006082 convert_size_by_transform_scale(&output->width, &output->height,
6083 output->current_mode->width,
6084 output->current_mode->height,
6085 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02006086}
6087
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006088static void
6089weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07006090{
6091 output->x = x;
6092 output->y = y;
6093
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006094 pixman_region32_fini(&output->region);
Scott Moreauccbf29d2012-02-22 14:21:41 -07006095 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06006096 output->width,
6097 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01006098}
6099
Marius Vlad55d87362019-06-11 01:15:35 +03006100/**
6101 * \ingroup output
6102 */
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006103WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006104weston_output_move(struct weston_output *output, int x, int y)
6105{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006106 struct weston_head *head;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006107 struct wl_resource *resource;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006108 int ver;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006109
6110 output->move_x = x - output->x;
6111 output->move_y = y - output->y;
6112
6113 if (output->move_x == 0 && output->move_y == 0)
6114 return;
6115
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006116 weston_output_init_geometry(output, x, y);
6117
6118 output->dirty = 1;
6119
6120 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006121 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006122
6123 /* Notify clients of the change for output position. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006124 wl_list_for_each(head, &output->head_list, output_link) {
6125 wl_resource_for_each(resource, &head->resource_list) {
6126 wl_output_send_geometry(resource,
6127 output->x,
6128 output->y,
6129 head->mm_width,
6130 head->mm_height,
6131 head->subpixel,
6132 head->make,
6133 head->model,
6134 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08006135
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006136 ver = wl_resource_get_version(resource);
6137 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
6138 wl_output_send_done(resource);
6139 }
Roman Gilge97391c2019-03-29 13:01:06 +01006140
6141 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
6142 zxdg_output_v1_send_logical_position(resource,
6143 output->x,
6144 output->y);
6145 zxdg_output_v1_send_done(resource);
6146 }
Quanxian Wangb2c86362014-03-14 09:16:25 +08006147 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006148}
6149
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006150/** Signal that a pending output is taken into use.
6151 *
6152 * Removes the output from the pending list and adds it to the compositor's
6153 * list of enabled outputs. The output created signal is emitted.
Giulio Camuffob1147152015-05-06 21:41:57 +03006154 *
Pekka Paalanen2210ad02017-03-30 15:48:06 +03006155 * The output gets an internal ID assigned, and the wl_output global is
6156 * created.
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03006157 *
Giulio Camuffob1147152015-05-06 21:41:57 +03006158 * \param compositor The compositor instance.
6159 * \param output The output to be added.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006160 *
6161 * \internal
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006162 * \ingroup compositor
Giulio Camuffob1147152015-05-06 21:41:57 +03006163 */
Pekka Paalanenf9681b52017-03-29 16:58:48 +03006164static void
Giulio Camuffob1147152015-05-06 21:41:57 +03006165weston_compositor_add_output(struct weston_compositor *compositor,
6166 struct weston_output *output)
6167{
Armin Krezoviće5403842016-08-05 15:28:29 +02006168 struct weston_view *view, *next;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03006169 struct weston_head *head;
Armin Krezoviće5403842016-08-05 15:28:29 +02006170
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006171 assert(!output->enabled);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03006172
6173 /* Verify we haven't reached the limit of 32 available output IDs */
6174 assert(ffs(~compositor->output_id_pool) > 0);
6175
6176 /* Invert the output id pool and look for the lowest numbered
6177 * switch (the least significant bit). Take that bit's position
6178 * as our ID, and mark it used in the compositor's output_id_pool.
6179 */
6180 output->id = ffs(~compositor->output_id_pool) - 1;
6181 compositor->output_id_pool |= 1u << output->id;
6182
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006183 wl_list_remove(&output->link);
Giulio Camuffob1147152015-05-06 21:41:57 +03006184 wl_list_insert(compositor->output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006185 output->enabled = true;
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006186
Pekka Paalanendcac3512017-12-08 14:13:34 +02006187 wl_list_for_each(head, &output->head_list, output_link)
6188 weston_head_add_global(head);
Pekka Paalanen2210ad02017-03-30 15:48:06 +03006189
Giulio Camuffob1147152015-05-06 21:41:57 +03006190 wl_signal_emit(&compositor->output_created_signal, output);
Armin Krezoviće5403842016-08-05 15:28:29 +02006191
Pekka Paalanen6551c092021-05-03 16:09:45 +03006192 /*
6193 * Use view_list, as paint nodes have not been created for this
6194 * output yet. Any existing view might touch this new output.
6195 */
Armin Krezoviće5403842016-08-05 15:28:29 +02006196 wl_list_for_each_safe(view, next, &compositor->view_list, link)
6197 weston_view_geometry_dirty(view);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04006198}
6199
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006200/** Transform device coordinates into global coordinates
6201 *
Marius Vlada2dace22019-06-12 16:05:44 +03006202 * \param output the weston_output object
6203 * \param[in] device_x X coordinate in device units.
6204 * \param[in] device_y Y coordinate in device units.
6205 * \param[out] x X coordinate in the global space.
6206 * \param[out] y Y coordinate in the global space.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006207 *
Marius Vlada2dace22019-06-12 16:05:44 +03006208 * Transforms coordinates from the device coordinate space (physical pixel
6209 * units) to the global coordinate space (logical pixel units). This takes
6210 * into account output transform and scale.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006211 *
Marius Vlad55d87362019-06-11 01:15:35 +03006212 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006213 * \internal
6214 */
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006215WL_EXPORT void
6216weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02006217 double device_x, double device_y,
6218 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006219{
Derek Foreman0f679412014-10-02 13:41:17 -05006220 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02006221 device_x,
6222 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05006223 0.0,
6224 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006225
Derek Foreman67a18b92015-03-24 11:36:14 -05006226 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006227
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02006228 *x = p.f[0] / p.f[3];
6229 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006230}
6231
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006232/** Removes output from compositor's list of enabled outputs
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006233 *
6234 * \param output The weston_output object that is being removed.
6235 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006236 * The following happens:
6237 *
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03006238 * - Destroys all paint nodes related to the output.
6239 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006240 * - The output assignments of all views in the current scenegraph are
6241 * recomputed.
6242 *
6243 * - Presentation feedback is discarded.
6244 *
6245 * - Compositor is notified that outputs were changed and
6246 * applies the necessary changes to re-layout outputs.
6247 *
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006248 * - The output is put back in the pending outputs list.
6249 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006250 * - Signal is emitted to notify all users of the weston_output
6251 * object that the output is being destroyed.
6252 *
6253 * - wl_output protocol objects referencing this weston_output
Pekka Paalanen2210ad02017-03-30 15:48:06 +03006254 * are made inert, and the wl_output global is removed.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006255 *
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03006256 * - The output's internal ID is released.
6257 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006258 * \ingroup compositor
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006259 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006260 */
6261static void
6262weston_compositor_remove_output(struct weston_output *output)
6263{
Pekka Paalanenbccda712017-03-29 16:16:04 +03006264 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03006265 struct weston_paint_node *pnode, *pntmp;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006266 struct weston_view *view;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03006267 struct weston_head *head;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006268
6269 assert(output->destroying);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006270 assert(output->enabled);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006271
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03006272 wl_list_for_each_safe(pnode, pntmp,
6273 &output->paint_node_list, output_link) {
6274 weston_paint_node_destroy(pnode);
6275 }
Pekka Paalanen2fddc532021-04-30 17:41:29 +03006276 assert(wl_list_empty(&output->paint_node_z_order_list));
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03006277
Pekka Paalanen6551c092021-05-03 16:09:45 +03006278 /*
6279 * Use view_list in case the output did not go through repaint
6280 * after a view came on it, lacking a paint node. Just to be sure.
6281 */
Pekka Paalanenbccda712017-03-29 16:16:04 +03006282 wl_list_for_each(view, &compositor->view_list, link) {
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006283 if (view->output_mask & (1u << output->id))
6284 weston_view_assign_output(view);
6285 }
6286
6287 weston_presentation_feedback_discard_list(&output->feedback_list);
6288
Pekka Paalanen9711fd92018-06-21 14:26:18 +03006289 weston_compositor_reflow_outputs(compositor, output, -output->width);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006290
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006291 wl_list_remove(&output->link);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006292 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006293 output->enabled = false;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006294
Pekka Paalanenbccda712017-03-29 16:16:04 +03006295 wl_signal_emit(&compositor->output_destroyed_signal, output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006296 wl_signal_emit(&output->destroy_signal, output);
6297
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02006298 wl_list_for_each(head, &output->head_list, output_link)
6299 weston_head_remove_global(head);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03006300
6301 compositor->output_id_pool &= ~(1u << output->id);
6302 output->id = 0xffffffff; /* invalid */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006303}
6304
6305/** Sets the output scale for a given output.
6306 *
6307 * \param output The weston_output object that the scale is set for.
6308 * \param scale Scale factor for the given output.
6309 *
6310 * It only supports setting scale for an output that
6311 * is not enabled and it can only be ran once.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006312 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05006313 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006314 */
6315WL_EXPORT void
6316weston_output_set_scale(struct weston_output *output,
6317 int32_t scale)
6318{
6319 /* We can only set scale on a disabled output */
6320 assert(!output->enabled);
6321
6322 /* We only want to set scale once */
6323 assert(!output->scale);
6324
6325 output->scale = scale;
6326}
6327
6328/** Sets the output transform for a given output.
6329 *
6330 * \param output The weston_output object that the transform is set for.
6331 * \param transform Transform value for the given output.
6332 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006333 * Refer to wl_output::transform section located at
6334 * https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_output
6335 * for list of values that can be passed to this function.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006336 *
Marius Vlad55d87362019-06-11 01:15:35 +03006337 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006338 */
6339WL_EXPORT void
6340weston_output_set_transform(struct weston_output *output,
6341 uint32_t transform)
6342{
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006343 struct weston_pointer_motion_event ev;
6344 struct wl_resource *resource;
6345 struct weston_seat *seat;
6346 pixman_region32_t old_region;
6347 int mid_x, mid_y;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006348 struct weston_head *head;
6349 int ver;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006350
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006351 if (!output->enabled && output->transform == UINT32_MAX) {
6352 output->transform = transform;
6353 return;
6354 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006355
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006356 weston_output_transform_scale_init(output, transform, output->scale);
6357
6358 pixman_region32_init(&old_region);
6359 pixman_region32_copy(&old_region, &output->region);
6360
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006361 weston_output_init_geometry(output, output->x, output->y);
6362
6363 output->dirty = 1;
6364
6365 /* Notify clients of the change for output transform. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006366 wl_list_for_each(head, &output->head_list, output_link) {
6367 wl_resource_for_each(resource, &head->resource_list) {
6368 wl_output_send_geometry(resource,
6369 output->x,
6370 output->y,
6371 head->mm_width,
6372 head->mm_height,
6373 head->subpixel,
6374 head->make,
6375 head->model,
6376 output->transform);
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006377
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006378 ver = wl_resource_get_version(resource);
6379 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
6380 wl_output_send_done(resource);
6381 }
Roman Gilge97391c2019-03-29 13:01:06 +01006382 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
6383 zxdg_output_v1_send_logical_position(resource,
6384 output->x,
6385 output->y);
6386 zxdg_output_v1_send_logical_size(resource,
6387 output->width,
6388 output->height);
6389 zxdg_output_v1_send_done(resource);
6390 }
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006391 }
6392
6393 /* we must ensure that pointers are inside output, otherwise they disappear */
6394 mid_x = output->x + output->width / 2;
6395 mid_y = output->y + output->height / 2;
6396
6397 ev.mask = WESTON_POINTER_MOTION_ABS;
6398 ev.x = wl_fixed_to_double(wl_fixed_from_int(mid_x));
6399 ev.y = wl_fixed_to_double(wl_fixed_from_int(mid_y));
6400
6401 wl_list_for_each(seat, &output->compositor->seat_list, link) {
6402 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
6403
6404 if (pointer && pixman_region32_contains_point(&old_region,
6405 wl_fixed_to_int(pointer->x),
6406 wl_fixed_to_int(pointer->y),
6407 NULL))
6408 weston_pointer_move(pointer, &ev);
6409 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006410}
6411
Pekka Paalanenb5265af2020-12-02 11:57:58 +02006412/** Make the output use renderer shadow buffer.
6413 *
6414 * \param output The weston_output object to modify.
6415 * \return True on success, false if unsupported.
6416 *
6417 * This can only be set on a disabled output object.
6418 *
6419 * This is a temporary API to demonstrate WESTON_CAP_COLOR_OPS and allow
6420 * testing related features. This will be superseded with color management
6421 * API.
6422 *
6423 * By default, a renderer is not using a shadow buffer of its own. Enabling
6424 * a shadow buffer may enable other color related features.
6425 *
6426 * Support depends on the chosen renderer and the graphics driver stack in use.
6427 *
6428 * \ingroup output
6429 */
6430WL_EXPORT bool
6431weston_output_set_renderer_shadow_buffer(struct weston_output *output)
6432{
6433 struct weston_compositor *compositor = output->compositor;
6434
6435 assert(!output->enabled);
6436
6437 if (compositor->capabilities & WESTON_CAP_COLOR_OPS) {
6438 output->use_renderer_shadow_buffer = true;
6439 return true;
6440 }
6441
6442 return false;
6443}
6444
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006445/** Initializes a weston_output object with enough data so
6446 ** an output can be configured.
6447 *
6448 * \param output The weston_output object to initialize
6449 * \param compositor The compositor instance.
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006450 * \param name Name for the output (the string is copied).
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006451 *
6452 * Sets initial values for fields that are expected to be
6453 * configured either by compositors or backends.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006454 *
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006455 * The name is used in logs, and can be used by compositors as a configuration
6456 * identifier.
6457 *
Marius Vlad55d87362019-06-11 01:15:35 +03006458 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006459 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006460 */
6461WL_EXPORT void
Armin Krezović40087402016-09-30 14:11:12 +02006462weston_output_init(struct weston_output *output,
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006463 struct weston_compositor *compositor,
6464 const char *name)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006465{
6466 output->compositor = compositor;
6467 output->destroying = 0;
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006468 output->name = strdup(name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006469 wl_list_init(&output->link);
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006470 wl_signal_init(&output->user_destroy_signal);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006471 output->enabled = false;
Ankit Nautiyal2690a772019-03-25 17:57:59 +05306472 output->desired_protection = WESTON_HDCP_DISABLE;
Ankit Nautiyal2844f8e2019-04-03 10:14:59 +05306473 output->allow_protection = true;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006474
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006475 wl_list_init(&output->head_list);
6476
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006477 /* Add some (in)sane defaults which can be used
6478 * for checking if an output was properly configured
6479 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006480 output->scale = 0;
6481 /* Can't use -1 on uint32_t and 0 is valid enum value */
6482 output->transform = UINT32_MAX;
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006483
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006484 pixman_region32_init(&output->region);
Pekka Paalanen42704142017-09-06 16:47:52 +03006485 wl_list_init(&output->mode_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006486}
6487
6488/** Adds weston_output object to pending output list.
6489 *
6490 * \param output The weston_output object to add
6491 * \param compositor The compositor instance.
6492 *
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03006493 * The opposite of this operation is built into weston_output_release().
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006494 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006495 * \ingroup compositor
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006496 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006497 */
6498WL_EXPORT void
6499weston_compositor_add_pending_output(struct weston_output *output,
6500 struct weston_compositor *compositor)
6501{
Pekka Paalanene952a012017-03-29 17:14:00 +03006502 assert(output->disable);
6503 assert(output->enable);
6504
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006505 wl_list_remove(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006506 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006507}
6508
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006509/** Create a string with the attached heads' names.
6510 *
6511 * The string must be free()'d.
Marius Vlad55d87362019-06-11 01:15:35 +03006512 *
6513 * \ingroup output
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006514 */
6515static char *
6516weston_output_create_heads_string(struct weston_output *output)
6517{
6518 FILE *fp;
6519 char *str = NULL;
6520 size_t size = 0;
6521 struct weston_head *head;
6522 const char *sep = "";
6523
6524 fp = open_memstream(&str, &size);
6525 if (!fp)
6526 return NULL;
6527
6528 wl_list_for_each(head, &output->head_list, output_link) {
6529 fprintf(fp, "%s%s", sep, head->name);
6530 sep = ", ";
6531 }
6532 fclose(fp);
6533
6534 return str;
6535}
6536
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006537/** Constructs a weston_output object that can be used by the compositor.
6538 *
Pekka Paalanencc201e42017-03-30 15:11:25 +03006539 * \param output The weston_output object that needs to be enabled. Must not
Pekka Paalanenddce54d2017-08-23 16:00:21 +03006540 * be enabled already. Must have at least one head attached.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006541 *
6542 * Output coordinates are calculated and each new output is by default
6543 * assigned to the right of previous one.
6544 *
6545 * Sets up the transformation, zoom, and geometry of the output using
6546 * the properties that need to be configured by the compositor.
6547 *
6548 * Establishes a repaint timer for the output with the relevant display
6549 * object's event loop. See output_repaint_timer_handler().
6550 *
6551 * The output is assigned an ID. Weston can support up to 32 distinct
6552 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
6553 * is referred to and used to find the first available ID number, and
6554 * then this ID is marked as used in output_id_pool.
6555 *
6556 * The output is also assigned a Wayland global with the wl_output
6557 * external interface.
6558 *
6559 * Backend specific function is called to set up the output output.
6560 *
6561 * Output is added to the compositor's output list
6562 *
6563 * If the backend specific function fails, the weston_output object
6564 * is returned to a state it was before calling this function and
6565 * is added to the compositor's pending_output_list in case it needs
6566 * to be reconfigured or just so it can be destroyed at shutdown.
6567 *
6568 * 0 is returned on success, -1 on failure.
Marius Vlad55d87362019-06-11 01:15:35 +03006569 *
6570 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006571 */
6572WL_EXPORT int
6573weston_output_enable(struct weston_output *output)
6574{
Armin Krezović782f5df2016-09-30 14:11:11 +02006575 struct weston_compositor *c = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006576 struct weston_output *iterator;
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03006577 struct weston_head *head;
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006578 char *head_names;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006579 int x = 0, y = 0;
6580
Pekka Paalanencc201e42017-03-30 15:11:25 +03006581 if (output->enabled) {
6582 weston_log("Error: attempt to enable an enabled output '%s'\n",
6583 output->name);
6584 return -1;
6585 }
6586
Pekka Paalanenddce54d2017-08-23 16:00:21 +03006587 if (wl_list_empty(&output->head_list)) {
6588 weston_log("Error: cannot enable output '%s' without heads.\n",
6589 output->name);
6590 return -1;
6591 }
6592
Pekka Paalanen586e1ac2017-09-14 16:17:59 +03006593 if (wl_list_empty(&output->mode_list) || !output->current_mode) {
6594 weston_log("Error: no video mode for output '%s'.\n",
6595 output->name);
6596 return -1;
6597 }
6598
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03006599 wl_list_for_each(head, &output->head_list, output_link) {
6600 assert(head->make);
6601 assert(head->model);
6602 }
6603
Armin Krezović782f5df2016-09-30 14:11:11 +02006604 iterator = container_of(c->output_list.prev,
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006605 struct weston_output, link);
6606
Armin Krezović782f5df2016-09-30 14:11:11 +02006607 if (!wl_list_empty(&c->output_list))
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006608 x = iterator->x + iterator->width;
6609
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006610 /* Make sure the scale is set up */
6611 assert(output->scale);
6612
6613 /* Make sure we have a transform set */
6614 assert(output->transform != UINT32_MAX);
6615
Armin Krezović782f5df2016-09-30 14:11:11 +02006616 output->x = x;
6617 output->y = y;
6618 output->dirty = 1;
6619 output->original_scale = output->scale;
6620
Marius Vlad3a2f8292019-11-04 17:53:46 +02006621 wl_signal_init(&output->frame_signal);
6622 wl_signal_init(&output->destroy_signal);
6623
Armin Krezović782f5df2016-09-30 14:11:11 +02006624 weston_output_transform_scale_init(output, output->transform, output->scale);
6625 weston_output_init_zoom(output);
6626
6627 weston_output_init_geometry(output, x, y);
6628 weston_output_damage(output);
6629
Armin Krezović782f5df2016-09-30 14:11:11 +02006630 wl_list_init(&output->animation_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02006631 wl_list_init(&output->feedback_list);
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03006632 wl_list_init(&output->paint_node_list);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03006633 wl_list_init(&output->paint_node_z_order_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02006634
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006635 /* Enable the output (set up the crtc or create a
6636 * window representing the output, set up the
6637 * renderer, etc)
6638 */
6639 if (output->enable(output) < 0) {
6640 weston_log("Enabling output \"%s\" failed.\n", output->name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006641 return -1;
6642 }
6643
6644 weston_compositor_add_output(output->compositor, output);
6645
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006646 head_names = weston_output_create_heads_string(output);
6647 weston_log("Output '%s' enabled with head(s) %s\n",
6648 output->name, head_names);
6649 free(head_names);
6650
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006651 return 0;
6652}
6653
6654/** Converts a weston_output object to a pending output state, so it
6655 ** can be configured again or destroyed.
6656 *
6657 * \param output The weston_output object that needs to be disabled.
6658 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006659 * Calls a backend specific function to disable an output, in case
6660 * such function exists.
6661 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03006662 * The backend specific disable function may choose to postpone the disabling
6663 * by returning a negative value, in which case this function returns early.
6664 * In that case the backend will guarantee the output will be disabled soon
6665 * by the backend calling this function again. One must not attempt to re-enable
6666 * the output until that happens.
6667 *
6668 * Otherwise, if the output is being used by the compositor, it is removed
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006669 * from weston's output_list (see weston_compositor_remove_output())
6670 * and is returned to a state it was before weston_output_enable()
6671 * was ran (see weston_output_enable_undo()).
6672 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03006673 * See weston_output_init() for more information on the
6674 * state output is returned to.
Pekka Paalanencc201e42017-03-30 15:11:25 +03006675 *
6676 * If the output has never been enabled yet, this function can still be
6677 * called to ensure that the output is actually turned off rather than left
6678 * in the state it was discovered in.
Marius Vlad55d87362019-06-11 01:15:35 +03006679 *
6680 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006681 */
6682WL_EXPORT void
6683weston_output_disable(struct weston_output *output)
6684{
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006685 /* Should we rename this? */
6686 output->destroying = 1;
6687
Pekka Paalanenc65df642017-03-29 15:45:46 +03006688 /* Disable is called unconditionally also for not-enabled outputs,
6689 * because at compositor start-up, if there is an output that is
6690 * already on but the compositor wants to turn it off, we have to
6691 * forward the turn-off to the backend so it knows to do it.
6692 * The backend cannot initially turn off everything, because it
6693 * would cause unnecessary mode-sets for all outputs the compositor
6694 * wants to be on.
6695 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006696 if (output->disable(output) < 0)
6697 return;
6698
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006699 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006700 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006701
6702 output->destroying = 0;
6703}
6704
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006705/** Forces a synchronous call to heads_changed hook
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006706 *
6707 * \param compositor The compositor instance
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006708 *
6709 * If there are new or changed heads, calls the heads_changed hook and
6710 * returns after the hook returns.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006711 *
6712 * \ingroup compositor
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006713 */
6714WL_EXPORT void
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006715weston_compositor_flush_heads_changed(struct weston_compositor *compositor)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006716{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03006717 if (compositor->heads_changed_source) {
6718 wl_event_source_remove(compositor->heads_changed_source);
6719 weston_compositor_call_heads_changed(compositor);
6720 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006721}
6722
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006723/** Add destroy callback for an output
6724 *
6725 * \param output The output to watch.
6726 * \param listener The listener to add. The \c notify member must be set.
6727 *
6728 * The listener callback will be called when user destroys an output. This
6729 * may be delayed by a backend in some cases. The main purpose of the
6730 * listener is to allow hooking up custom data to the output. The custom data
6731 * can be fetched via weston_output_get_destroy_listener() followed by
6732 * container_of().
6733 *
6734 * The \c data argument to the notify callback is the weston_output being
6735 * destroyed.
6736 *
6737 * @note This is for the final destruction of an output, not when it gets
6738 * disabled. If you want to keep track of enabled outputs, this is not it.
Marius Vlad55d87362019-06-11 01:15:35 +03006739 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05006740 * \ingroup output
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006741 */
6742WL_EXPORT void
6743weston_output_add_destroy_listener(struct weston_output *output,
6744 struct wl_listener *listener)
6745{
6746 wl_signal_add(&output->user_destroy_signal, listener);
6747}
6748
6749/** Look up destroy listener for an output
6750 *
6751 * \param output The output to query.
6752 * \param notify The notify function used used for the added destroy listener.
6753 * \return The listener, or NULL if not found.
6754 *
6755 * This looks up the previously added destroy listener struct based on the
6756 * notify function it has. The listener can be used to access user data
6757 * through \c container_of().
6758 *
6759 * \sa wl_signal_get() weston_output_add_destroy_listener()
Marius Vlad55d87362019-06-11 01:15:35 +03006760 * \ingroup output
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006761 */
6762WL_EXPORT struct wl_listener *
6763weston_output_get_destroy_listener(struct weston_output *output,
6764 wl_notify_func_t notify)
6765{
6766 return wl_signal_get(&output->user_destroy_signal, notify);
6767}
6768
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006769/** Uninitialize an output
6770 *
6771 * Removes the output from the list of enabled outputs if necessary, but
6772 * does not call the backend's output disable function. The output will no
6773 * longer be in the list of pending outputs either.
6774 *
6775 * All fields of weston_output become uninitialized, i.e. should not be used
6776 * anymore. The caller can free the memory after this.
6777 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05006778 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006779 * \internal
6780 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006781WL_EXPORT void
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03006782weston_output_release(struct weston_output *output)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006783{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006784 struct weston_head *head, *tmp;
6785
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006786 output->destroying = 1;
6787
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006788 wl_signal_emit(&output->user_destroy_signal, output);
6789
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03006790 if (output->idle_repaint_source)
6791 wl_event_source_remove(output->idle_repaint_source);
6792
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006793 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006794 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006795
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03006796 assert(wl_list_empty(&output->paint_node_list));
6797
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006798 pixman_region32_fini(&output->region);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006799 wl_list_remove(&output->link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006800
6801 wl_list_for_each_safe(head, tmp, &output->head_list, output_link)
6802 weston_head_detach(head);
6803
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006804 free(output->name);
6805}
6806
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006807/** Find an output by its given name
6808 *
6809 * \param compositor The compositor to search in.
6810 * \param name The output name to search for.
6811 * \return An existing output with the given name, or NULL if not found.
6812 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006813 * \ingroup compositor
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006814 */
6815WL_EXPORT struct weston_output *
6816weston_compositor_find_output_by_name(struct weston_compositor *compositor,
6817 const char *name)
6818{
6819 struct weston_output *output;
6820
6821 wl_list_for_each(output, &compositor->output_list, link)
6822 if (strcmp(output->name, name) == 0)
6823 return output;
6824
6825 wl_list_for_each(output, &compositor->pending_output_list, link)
6826 if (strcmp(output->name, name) == 0)
6827 return output;
6828
6829 return NULL;
6830}
6831
6832/** Create a named output
6833 *
6834 * \param compositor The compositor.
6835 * \param name The name for the output.
6836 * \return A new \c weston_output, or NULL on failure.
6837 *
6838 * This creates a new weston_output that starts with no heads attached.
6839 *
6840 * An output must be configured and it must have at least one head before
6841 * it can be enabled.
6842 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006843 * \ingroup compositor
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006844 */
6845WL_EXPORT struct weston_output *
6846weston_compositor_create_output(struct weston_compositor *compositor,
6847 const char *name)
6848{
6849 assert(compositor->backend->create_output);
6850
6851 if (weston_compositor_find_output_by_name(compositor, name)) {
6852 weston_log("Warning: attempted to create an output with a "
6853 "duplicate name '%s'.\n", name);
6854 return NULL;
6855 }
6856
6857 return compositor->backend->create_output(compositor, name);
6858}
6859
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006860/** Create an output for an unused head
6861 *
6862 * \param compositor The compositor.
6863 * \param head The head to attach to the output.
6864 * \return A new \c weston_output, or NULL on failure.
6865 *
6866 * This creates a new weston_output that starts with the given head attached.
6867 * The output inherits the name of the head. The head must not be already
6868 * attached to another output.
6869 *
6870 * An output must be configured before it can be enabled.
6871 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006872 * \ingroup compositor
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006873 */
6874WL_EXPORT struct weston_output *
6875weston_compositor_create_output_with_head(struct weston_compositor *compositor,
6876 struct weston_head *head)
6877{
6878 struct weston_output *output;
6879
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006880 output = weston_compositor_create_output(compositor, head->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006881 if (!output)
6882 return NULL;
6883
6884 if (weston_output_attach_head(output, head) < 0) {
Pekka Paalanen42c0e142017-10-27 12:07:49 +03006885 weston_output_destroy(output);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006886 return NULL;
6887 }
6888
6889 return output;
6890}
6891
6892/** Destroy an output
6893 *
6894 * \param output The output to destroy.
6895 *
6896 * The heads attached to the given output are detached and become unused again.
6897 *
6898 * It is not necessary to explicitly destroy all outputs at compositor exit.
Leandro Ribeiroca640d52020-01-27 19:12:01 -03006899 * weston_compositor_destroy() will automatically destroy any remaining
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006900 * outputs.
6901 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05006902 * \ingroup output
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006903 */
6904WL_EXPORT void
6905weston_output_destroy(struct weston_output *output)
6906{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006907 output->destroy(output);
6908}
6909
Pekka Paalanencf0a4762017-04-04 16:36:07 +03006910/** When you need a head...
6911 *
6912 * This function is a hack, used until all code has been converted to become
6913 * multi-head aware.
6914 *
6915 * \param output The weston_output whose head to get.
6916 * \return The first head in the output's list.
Marius Vlad55d87362019-06-11 01:15:35 +03006917 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05006918 * \ingroup output
Pekka Paalanencf0a4762017-04-04 16:36:07 +03006919 */
6920WL_EXPORT struct weston_head *
6921weston_output_get_first_head(struct weston_output *output)
6922{
6923 if (wl_list_empty(&output->head_list))
6924 return NULL;
6925
6926 return container_of(output->head_list.next,
6927 struct weston_head, output_link);
6928}
6929
Ankit Nautiyal2844f8e2019-04-03 10:14:59 +05306930/** Allow/Disallow content-protection support for an output
6931 *
6932 * This function sets the allow_protection member for an output. Setting of
6933 * this field will allow the compositor to attempt content-protection for this
6934 * output, for a backend that supports the content-protection protocol.
6935 *
6936 * \param output The weston_output for whom the content-protection is to be
6937 * allowed.
6938 * \param allow_protection The bool value which is to be set.
6939 */
6940WL_EXPORT void
6941weston_output_allow_protection(struct weston_output *output,
6942 bool allow_protection)
6943{
6944 output->allow_protection = allow_protection;
6945}
6946
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01006947static void
Roman Gilge97391c2019-03-29 13:01:06 +01006948xdg_output_unlist(struct wl_resource *resource)
6949{
6950 wl_list_remove(wl_resource_get_link(resource));
6951}
6952
6953static void
6954xdg_output_destroy(struct wl_client *client, struct wl_resource *resource)
6955{
6956 wl_resource_destroy(resource);
6957}
6958
6959static const struct zxdg_output_v1_interface xdg_output_interface = {
6960 xdg_output_destroy
6961};
6962
6963static void
6964xdg_output_manager_destroy(struct wl_client *client,
6965 struct wl_resource *resource)
6966{
6967 wl_resource_destroy(resource);
6968}
6969
6970static void
6971xdg_output_manager_get_xdg_output(struct wl_client *client,
6972 struct wl_resource *manager,
6973 uint32_t id,
6974 struct wl_resource *output_resource)
6975{
6976 int version = wl_resource_get_version(manager);
6977 struct weston_head *head = wl_resource_get_user_data(output_resource);
6978 struct weston_output *output = head->output;
6979 struct wl_resource *resource;
6980
6981 resource = wl_resource_create(client, &zxdg_output_v1_interface,
6982 version, id);
6983 if (resource == NULL) {
6984 wl_client_post_no_memory(client);
6985 return;
6986 }
6987
6988 wl_list_insert(&head->xdg_output_resource_list,
6989 wl_resource_get_link(resource));
6990
6991 wl_resource_set_implementation(resource, &xdg_output_interface,
6992 NULL, xdg_output_unlist);
6993
6994 zxdg_output_v1_send_logical_position(resource, output->x, output->y);
6995 zxdg_output_v1_send_logical_size(resource,
6996 output->width,
6997 output->height);
6998 if (version >= ZXDG_OUTPUT_V1_NAME_SINCE_VERSION)
6999 zxdg_output_v1_send_name(resource, head->name);
7000
7001 zxdg_output_v1_send_done(resource);
7002}
7003
7004static const struct zxdg_output_manager_v1_interface xdg_output_manager_interface = {
7005 xdg_output_manager_destroy,
7006 xdg_output_manager_get_xdg_output
7007};
7008
7009static void
7010bind_xdg_output_manager(struct wl_client *client,
7011 void *data, uint32_t version, uint32_t id)
7012{
7013 struct wl_resource *resource;
7014
7015 resource = wl_resource_create(client, &zxdg_output_manager_v1_interface,
7016 version, id);
7017 if (resource == NULL) {
7018 wl_client_post_no_memory(client);
7019 return;
7020 }
7021
7022 wl_resource_set_implementation(resource, &xdg_output_manager_interface,
7023 NULL, NULL);
7024}
7025
7026static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007027destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01007028{
Jonny Lamb74130762013-11-26 18:19:46 +01007029 struct weston_surface *surface =
7030 wl_resource_get_user_data(resource);
7031
Pekka Paalanen4826f872016-04-22 14:14:38 +03007032 if (!surface)
7033 return;
7034
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007035 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02007036 surface->pending.buffer_viewport.buffer.src_width =
7037 wl_fixed_from_int(-1);
7038 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02007039 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01007040}
7041
7042static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007043viewport_destroy(struct wl_client *client,
7044 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01007045{
7046 wl_resource_destroy(resource);
7047}
7048
7049static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007050viewport_set_source(struct wl_client *client,
7051 struct wl_resource *resource,
7052 wl_fixed_t src_x,
7053 wl_fixed_t src_y,
7054 wl_fixed_t src_width,
7055 wl_fixed_t src_height)
7056{
7057 struct weston_surface *surface =
7058 wl_resource_get_user_data(resource);
7059
Pekka Paalanen4826f872016-04-22 14:14:38 +03007060 if (!surface) {
7061 wl_resource_post_error(resource,
7062 WP_VIEWPORT_ERROR_NO_SURFACE,
7063 "wl_surface for this viewport is no longer exists");
7064 return;
7065 }
7066
7067 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03007068 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007069
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007070 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03007071 src_height == wl_fixed_from_int(-1) &&
7072 src_x == wl_fixed_from_int(-1) &&
7073 src_y == wl_fixed_from_int(-1)) {
7074 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007075 surface->pending.buffer_viewport.buffer.src_width =
7076 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02007077 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007078 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007079 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007080
Pekka Paalanen201769a2016-04-26 14:42:11 +03007081 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007082 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007083 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03007084 "wl_surface@%d viewport source "
7085 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
7086 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007087 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03007088 wl_fixed_to_double(src_height),
7089 wl_fixed_to_double(src_x),
7090 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007091 return;
7092 }
7093
7094 surface->pending.buffer_viewport.buffer.src_x = src_x;
7095 surface->pending.buffer_viewport.buffer.src_y = src_y;
7096 surface->pending.buffer_viewport.buffer.src_width = src_width;
7097 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02007098 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007099}
7100
7101static void
7102viewport_set_destination(struct wl_client *client,
7103 struct wl_resource *resource,
7104 int32_t dst_width,
7105 int32_t dst_height)
7106{
7107 struct weston_surface *surface =
7108 wl_resource_get_user_data(resource);
7109
Pekka Paalanen4826f872016-04-22 14:14:38 +03007110 if (!surface) {
7111 wl_resource_post_error(resource,
7112 WP_VIEWPORT_ERROR_NO_SURFACE,
7113 "wl_surface for this viewport no longer exists");
7114 return;
7115 }
7116
7117 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007118
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007119 if (dst_width == -1 && dst_height == -1) {
7120 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007121 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02007122 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007123 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007124 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007125
7126 if (dst_width <= 0 || dst_height <= 0) {
7127 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007128 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007129 "destination size must be positive (%dx%d)",
7130 dst_width, dst_height);
7131 return;
7132 }
7133
7134 surface->pending.buffer_viewport.surface.width = dst_width;
7135 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02007136 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007137}
7138
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007139static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007140 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007141 viewport_set_source,
7142 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01007143};
7144
7145static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007146viewporter_destroy(struct wl_client *client,
7147 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01007148{
7149 wl_resource_destroy(resource);
7150}
7151
7152static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007153viewporter_get_viewport(struct wl_client *client,
7154 struct wl_resource *viewporter,
7155 uint32_t id,
7156 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01007157{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007158 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007159 struct weston_surface *surface =
7160 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01007161 struct wl_resource *resource;
7162
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007163 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007164 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007165 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007166 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01007167 return;
7168 }
7169
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007170 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007171 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01007172 if (resource == NULL) {
7173 wl_client_post_no_memory(client);
7174 return;
7175 }
7176
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007177 wl_resource_set_implementation(resource, &viewport_interface,
7178 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01007179
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007180 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01007181}
7182
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007183static const struct wp_viewporter_interface viewporter_interface = {
7184 viewporter_destroy,
7185 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01007186};
7187
7188static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007189bind_viewporter(struct wl_client *client,
7190 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01007191{
7192 struct wl_resource *resource;
7193
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007194 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06007195 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01007196 if (resource == NULL) {
7197 wl_client_post_no_memory(client);
7198 return;
7199 }
7200
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007201 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01007202 NULL, NULL);
7203}
7204
7205static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04007206destroy_presentation_feedback(struct wl_resource *feedback_resource)
7207{
7208 struct weston_presentation_feedback *feedback;
7209
7210 feedback = wl_resource_get_user_data(feedback_resource);
7211
7212 wl_list_remove(&feedback->link);
7213 free(feedback);
7214}
7215
7216static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007217presentation_destroy(struct wl_client *client, struct wl_resource *resource)
7218{
7219 wl_resource_destroy(resource);
7220}
7221
7222static void
7223presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04007224 struct wl_resource *presentation_resource,
7225 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007226 uint32_t callback)
7227{
Pekka Paalanen133e4392014-09-23 22:08:46 -04007228 struct weston_surface *surface;
7229 struct weston_presentation_feedback *feedback;
7230
7231 surface = wl_resource_get_user_data(surface_resource);
7232
Bryce Harringtonde16d892014-11-20 22:21:57 -08007233 feedback = zalloc(sizeof *feedback);
7234 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04007235 goto err_calloc;
7236
7237 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007238 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04007239 1, callback);
7240 if (!feedback->resource)
7241 goto err_create;
7242
7243 wl_resource_set_implementation(feedback->resource, NULL, feedback,
7244 destroy_presentation_feedback);
7245
7246 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
7247
7248 return;
7249
7250err_create:
7251 free(feedback);
7252
7253err_calloc:
7254 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007255}
7256
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007257static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007258 presentation_destroy,
7259 presentation_feedback
7260};
7261
7262static void
7263bind_presentation(struct wl_client *client,
7264 void *data, uint32_t version, uint32_t id)
7265{
7266 struct weston_compositor *compositor = data;
7267 struct wl_resource *resource;
7268
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007269 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06007270 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007271 if (resource == NULL) {
7272 wl_client_post_no_memory(client);
7273 return;
7274 }
7275
7276 wl_resource_set_implementation(resource, &presentation_implementation,
7277 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007278 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007279}
7280
7281static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05007282compositor_bind(struct wl_client *client,
7283 void *data, uint32_t version, uint32_t id)
7284{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007285 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05007286 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05007287
Jason Ekstranda85118c2013-06-27 20:17:02 -05007288 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06007289 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07007290 if (resource == NULL) {
7291 wl_client_post_no_memory(client);
7292 return;
7293 }
7294
7295 wl_resource_set_implementation(resource, &compositor_interface,
7296 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05007297}
7298
Daniel Stonece62cb32018-07-20 09:46:24 +01007299static const char *
7300output_repaint_status_text(struct weston_output *output)
7301{
7302 switch (output->repaint_status) {
7303 case REPAINT_NOT_SCHEDULED:
7304 return "no repaint";
7305 case REPAINT_BEGIN_FROM_IDLE:
7306 return "start_repaint_loop scheduled";
7307 case REPAINT_SCHEDULED:
7308 return "repaint scheduled";
7309 case REPAINT_AWAITING_COMPLETION:
7310 return "awaiting completion";
7311 }
7312
7313 assert(!"output_repaint_status_text missing enum");
7314 return NULL;
7315}
7316
7317static void
7318debug_scene_view_print_buffer(FILE *fp, struct weston_view *view)
7319{
7320 struct weston_buffer *buffer = view->surface->buffer_ref.buffer;
7321 struct wl_shm_buffer *shm;
7322 struct linux_dmabuf_buffer *dmabuf;
Marius Vlad00a6e012018-11-20 17:52:31 +02007323 const struct pixel_format_info *pixel_info = NULL;
Daniel Stonece62cb32018-07-20 09:46:24 +01007324
7325 if (!buffer) {
7326 fprintf(fp, "\t\t[buffer not available]\n");
7327 return;
7328 }
7329
7330 shm = wl_shm_buffer_get(buffer->resource);
7331 if (shm) {
Marius Vlad00a6e012018-11-20 17:52:31 +02007332 uint32_t _format = wl_shm_buffer_get_format(shm);
7333 pixel_info = pixel_format_get_info_shm(_format);
Daniel Stonece62cb32018-07-20 09:46:24 +01007334 fprintf(fp, "\t\tSHM buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02007335 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
7336 (unsigned long) _format,
7337 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Daniel Stonece62cb32018-07-20 09:46:24 +01007338 return;
7339 }
7340
7341 dmabuf = linux_dmabuf_buffer_get(buffer->resource);
7342 if (dmabuf) {
Marius Vlad00a6e012018-11-20 17:52:31 +02007343 pixel_info = pixel_format_get_info(dmabuf->attributes.format);
Daniel Stonece62cb32018-07-20 09:46:24 +01007344 fprintf(fp, "\t\tdmabuf buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02007345 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
7346 (unsigned long) dmabuf->attributes.format,
7347 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Daniel Stonece62cb32018-07-20 09:46:24 +01007348 fprintf(fp, "\t\t\tmodifier: 0x%llx\n",
7349 (unsigned long long) dmabuf->attributes.modifier[0]);
7350 return;
7351 }
7352
Marius Vlad253ba9a2019-03-17 18:22:21 +02007353 fprintf(fp, "\t\tEGL buffer\n");
Daniel Stonece62cb32018-07-20 09:46:24 +01007354}
7355
7356static void
7357debug_scene_view_print(FILE *fp, struct weston_view *view, int view_idx)
7358{
7359 struct weston_compositor *ec = view->surface->compositor;
7360 struct weston_output *output;
7361 char desc[512];
7362 pixman_box32_t *box;
7363 uint32_t surface_id = 0;
7364 pid_t pid = 0;
7365
7366 if (view->surface->resource) {
7367 struct wl_resource *resource = view->surface->resource;
7368 wl_client_get_credentials(wl_resource_get_client(resource),
7369 &pid, NULL, NULL);
7370 surface_id = wl_resource_get_id(view->surface->resource);
7371 }
7372
7373 if (!view->surface->get_label ||
7374 view->surface->get_label(view->surface, desc, sizeof(desc)) < 0) {
7375 strcpy(desc, "[no description available]");
7376 }
7377 fprintf(fp, "\tView %d (role %s, PID %d, surface ID %u, %s, %p):\n",
7378 view_idx, view->surface->role_name, pid, surface_id,
7379 desc, view);
7380
7381 box = pixman_region32_extents(&view->transform.boundingbox);
7382 fprintf(fp, "\t\tposition: (%d, %d) -> (%d, %d)\n",
7383 box->x1, box->y1, box->x2, box->y2);
7384 box = pixman_region32_extents(&view->transform.opaque);
7385
Michael Olbrichb7e5f102020-08-11 16:33:35 +02007386 if (weston_view_is_opaque(view, &view->transform.boundingbox)) {
Daniel Stonece62cb32018-07-20 09:46:24 +01007387 fprintf(fp, "\t\t[fully opaque]\n");
7388 } else if (!pixman_region32_not_empty(&view->transform.opaque)) {
7389 fprintf(fp, "\t\t[not opaque]\n");
7390 } else {
7391 fprintf(fp, "\t\t[opaque: (%d, %d) -> (%d, %d)]\n",
7392 box->x1, box->y1, box->x2, box->y2);
7393 }
7394
7395 if (view->alpha < 1.0)
7396 fprintf(fp, "\t\talpha: %f\n", view->alpha);
7397
7398 if (view->output_mask != 0) {
7399 bool first_output = true;
7400 fprintf(fp, "\t\toutputs: ");
7401 wl_list_for_each(output, &ec->output_list, link) {
7402 if (!(view->output_mask & (1 << output->id)))
7403 continue;
7404 fprintf(fp, "%s%d (%s)%s",
7405 (first_output) ? "" : ", ",
7406 output->id, output->name,
7407 (view->output == output) ? " (primary)" : "");
7408 first_output = false;
7409 }
7410 } else {
7411 fprintf(fp, "\t\t[no outputs]");
7412 }
7413
7414 fprintf(fp, "\n");
7415
7416 debug_scene_view_print_buffer(fp, view);
7417}
7418
Marius Vlad433f4e72019-02-17 22:14:23 +02007419static void
7420debug_scene_view_print_tree(struct weston_view *view,
Marius Vlada6acfa82019-03-17 18:10:09 +02007421 FILE *fp, int *view_idx)
Marius Vlad433f4e72019-02-17 22:14:23 +02007422{
7423 struct weston_subsurface *sub;
7424 struct weston_view *ev;
7425
7426 /*
7427 * print the view first, then we recursively go on printing
7428 * sub-surfaces. We bail out once no more sub-surfaces are available.
7429 */
Marius Vlada6acfa82019-03-17 18:10:09 +02007430 debug_scene_view_print(fp, view, *view_idx);
Marius Vlad433f4e72019-02-17 22:14:23 +02007431
7432 /* no more sub-surfaces */
7433 if (wl_list_empty(&view->surface->subsurface_list))
7434 return;
7435
7436 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
7437 wl_list_for_each(ev, &sub->surface->views, surface_link) {
Michael Olbrichef5f3232020-04-29 09:03:15 +02007438 /* only print the child views of the current view */
7439 if (ev->parent_view != view)
Marius Vlad433f4e72019-02-17 22:14:23 +02007440 continue;
Marius Vlada6acfa82019-03-17 18:10:09 +02007441
7442 (*view_idx)++;
Marius Vlad433f4e72019-02-17 22:14:23 +02007443 debug_scene_view_print_tree(ev, fp, view_idx);
7444 }
7445 }
7446}
7447
Daniel Stonece62cb32018-07-20 09:46:24 +01007448/**
7449 * Output information on how libweston is currently composing the scene
7450 * graph.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007451 *
7452 * \ingroup compositor
Daniel Stonece62cb32018-07-20 09:46:24 +01007453 */
7454WL_EXPORT char *
7455weston_compositor_print_scene_graph(struct weston_compositor *ec)
7456{
7457 struct weston_output *output;
7458 struct weston_layer *layer;
7459 struct timespec now;
7460 int layer_idx = 0;
7461 FILE *fp;
7462 char *ret;
7463 size_t len;
7464 int err;
7465
7466 fp = open_memstream(&ret, &len);
7467 assert(fp);
7468
7469 weston_compositor_read_presentation_clock(ec, &now);
7470 fprintf(fp, "Weston scene graph at %ld.%09ld:\n\n",
7471 now.tv_sec, now.tv_nsec);
7472
7473 wl_list_for_each(output, &ec->output_list, link) {
7474 struct weston_head *head;
7475 int head_idx = 0;
7476
7477 fprintf(fp, "Output %d (%s):\n", output->id, output->name);
7478 assert(output->enabled);
7479
7480 fprintf(fp, "\tposition: (%d, %d) -> (%d, %d)\n",
7481 output->x, output->y,
7482 output->x + output->width,
7483 output->y + output->height);
7484 fprintf(fp, "\tmode: %dx%d@%.3fHz\n",
7485 output->current_mode->width,
7486 output->current_mode->height,
7487 output->current_mode->refresh / 1000.0);
7488 fprintf(fp, "\tscale: %d\n", output->scale);
7489
7490 fprintf(fp, "\trepaint status: %s\n",
7491 output_repaint_status_text(output));
7492 if (output->repaint_status == REPAINT_SCHEDULED)
7493 fprintf(fp, "\tnext repaint: %ld.%09ld\n",
7494 output->next_repaint.tv_sec,
7495 output->next_repaint.tv_nsec);
7496
7497 wl_list_for_each(head, &output->head_list, output_link) {
7498 fprintf(fp, "\tHead %d (%s): %sconnected\n",
7499 head_idx++, head->name,
7500 (head->connected) ? "" : "not ");
7501 }
7502 }
7503
7504 fprintf(fp, "\n");
7505
7506 wl_list_for_each(layer, &ec->layer_list, link) {
7507 struct weston_view *view;
7508 int view_idx = 0;
7509
7510 fprintf(fp, "Layer %d (pos 0x%lx):\n", layer_idx++,
7511 (unsigned long) layer->position);
7512
7513 if (!weston_layer_mask_is_infinite(layer)) {
7514 fprintf(fp, "\t[mask: (%d, %d) -> (%d,%d)]\n\n",
7515 layer->mask.x1, layer->mask.y1,
7516 layer->mask.x2, layer->mask.y2);
7517 }
7518
Marius Vlada6acfa82019-03-17 18:10:09 +02007519 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
7520 debug_scene_view_print_tree(view, fp, &view_idx);
7521 view_idx++;
7522 }
Daniel Stonece62cb32018-07-20 09:46:24 +01007523
7524 if (wl_list_empty(&layer->view_list.link))
7525 fprintf(fp, "\t[no views]\n");
7526
7527 fprintf(fp, "\n");
7528 }
7529
7530 err = fclose(fp);
7531 assert(err == 0);
7532
7533 return ret;
7534}
7535
7536/**
7537 * Called when the 'scene-graph' debug scope is bound by a client. This
7538 * one-shot weston-debug scope prints the current scene graph when bound,
7539 * and then terminates the stream.
7540 */
7541static void
Marius Vladdad882a2019-07-17 15:43:53 +03007542debug_scene_graph_cb(struct weston_log_subscription *sub, void *data)
Daniel Stonece62cb32018-07-20 09:46:24 +01007543{
7544 struct weston_compositor *ec = data;
7545 char *str = weston_compositor_print_scene_graph(ec);
7546
Marius Vladdad882a2019-07-17 15:43:53 +03007547 weston_log_subscription_printf(sub, "%s", str);
Daniel Stonece62cb32018-07-20 09:46:24 +01007548 free(str);
Marius Vladdad882a2019-07-17 15:43:53 +03007549 weston_log_subscription_complete(sub);
Daniel Stonece62cb32018-07-20 09:46:24 +01007550}
7551
Leandro Ribeiro32a5acd2020-10-19 16:06:22 -03007552/** Retrieve testsuite data from compositor
7553 *
7554 * The testsuite data can be defined by the test suite of projects that uses
7555 * libweston and given to the compositor at the moment of its creation. This
7556 * function should be used when we need to retrieve the testsuite private data
7557 * from the compositor.
7558 *
7559 * \param ec The weston compositor.
7560 * \return The testsuite data.
7561 *
7562 * \ingroup compositor
7563 * \sa weston_compositor_test_data_init
7564 */
7565WL_EXPORT void *
7566weston_compositor_get_test_data(struct weston_compositor *ec)
7567{
7568 return ec->test_data.test_private_data;
7569}
7570
Giulio Camuffo459137b2014-10-11 23:56:24 +03007571/** Create the compositor.
7572 *
7573 * This functions creates and initializes a compositor instance.
7574 *
7575 * \param display The Wayland display to be used.
7576 * \param user_data A pointer to an object that can later be retrieved
Marius Vlada2dace22019-06-12 16:05:44 +03007577 * \param log_ctx A pointer to weston_debug_compositor
Leandro Ribeirocdb72182020-11-06 11:24:22 -03007578 * \param test_data Optional testsuite data, or NULL.
Giulio Camuffo459137b2014-10-11 23:56:24 +03007579 * using the \ref weston_compositor_get_user_data function.
7580 * \return The compositor instance on success or NULL on failure.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007581 *
7582 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007583 */
7584WL_EXPORT struct weston_compositor *
Marius Vlad880b4852019-04-07 17:07:58 +03007585weston_compositor_create(struct wl_display *display,
Leandro Ribeirocdb72182020-11-06 11:24:22 -03007586 struct weston_log_context *log_ctx, void *user_data,
7587 const struct weston_testsuite_data *test_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04007588{
Giulio Camuffo459137b2014-10-11 23:56:24 +03007589 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05007590 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07007591
Leandro Ribeirobd9c0a62020-01-17 10:47:49 -03007592 if (!log_ctx)
7593 return NULL;
7594
Giulio Camuffo459137b2014-10-11 23:56:24 +03007595 ec = zalloc(sizeof *ec);
7596 if (!ec)
7597 return NULL;
7598
Leandro Ribeirocdb72182020-11-06 11:24:22 -03007599 if (test_data)
7600 ec->test_data = *test_data;
7601
Leandro Ribeirobd9c0a62020-01-17 10:47:49 -03007602 ec->weston_log_ctx = log_ctx;
Giulio Camuffo459137b2014-10-11 23:56:24 +03007603 ec->wl_display = display;
7604 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04007605 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07007606 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04007607 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03007608 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03007609 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02007610 wl_signal_init(&ec->idle_signal);
7611 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02007612 wl_signal_init(&ec->show_input_panel_signal);
7613 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02007614 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01007615 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01007616 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02007617 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02007618 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02007619 wl_signal_init(&ec->output_resized_signal);
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03007620 wl_signal_init(&ec->heads_changed_signal);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02007621 wl_signal_init(&ec->output_heads_changed_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07007622 wl_signal_init(&ec->session_signal);
Robert Beckettc569bdc2019-07-10 16:40:12 +01007623 ec->session_active = true;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04007624
Casey Dahlin58ba1372012-04-19 22:50:08 -04007625 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03007626 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04007627
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02007628 ec->activate_serial = 1;
7629
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05007630 ec->touch_mode = WESTON_TOUCH_MODE_NORMAL;
7631
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05307632 ec->content_protection = NULL;
7633
Derek Foreman152254b2015-11-26 14:17:48 -06007634 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04007635 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007636 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05007637
Giulio Camuffo954f1832014-10-11 18:27:30 +03007638 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04007639 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007640 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03007641
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007642 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007643 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007644 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01007645
Roman Gilge97391c2019-03-29 13:01:06 +01007646 if (!wl_global_create(ec->wl_display, &zxdg_output_manager_v1_interface, 2,
7647 ec, bind_xdg_output_manager))
7648 goto fail;
7649
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007650 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007651 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007652 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007653
Jonas Ådahl30d61d82014-10-22 21:21:17 +02007654 if (weston_input_init(ec) != 0)
7655 goto fail;
7656
Jason Ekstranda7af7042013-10-12 22:38:11 -05007657 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02007658 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007659 wl_list_init(&ec->layer_list);
7660 wl_list_init(&ec->seat_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007661 wl_list_init(&ec->pending_output_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007662 wl_list_init(&ec->output_list);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03007663 wl_list_init(&ec->head_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007664 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01007665 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007666 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01007667 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007668 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02007669 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007670
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007671 wl_list_init(&ec->plugin_api_list);
7672
Xiong Zhang97116532013-10-23 13:58:31 +08007673 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02007674 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04007675
Giulio Camuffo459137b2014-10-11 23:56:24 +03007676 wl_data_device_manager_init(ec->wl_display);
7677
7678 wl_display_init_shm(ec->wl_display);
7679
7680 loop = wl_display_get_event_loop(ec->wl_display);
7681 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Daniel Stone6847b852017-03-01 11:34:08 +00007682 ec->repaint_timer =
7683 wl_event_loop_add_timer(loop, output_repaint_timer_handler,
7684 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007685
Quentin Glidic82681572016-12-17 13:40:51 +01007686 weston_layer_init(&ec->fade_layer, ec);
7687 weston_layer_init(&ec->cursor_layer, ec);
7688
7689 weston_layer_set_position(&ec->fade_layer, WESTON_LAYER_POSITION_FADE);
7690 weston_layer_set_position(&ec->cursor_layer,
7691 WESTON_LAYER_POSITION_CURSOR);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007692
Daniel Stonece62cb32018-07-20 09:46:24 +01007693 ec->debug_scene =
Leandro Ribeiroce100192019-12-26 16:35:49 -03007694 weston_compositor_add_log_scope(ec, "scene-graph",
7695 "Scene graph details\n",
7696 debug_scene_graph_cb, NULL,
7697 ec);
Daniel Stonece62cb32018-07-20 09:46:24 +01007698
Marius Vladda104eb2019-09-05 14:31:01 +03007699 ec->timeline =
Leandro Ribeiroce100192019-12-26 16:35:49 -03007700 weston_compositor_add_log_scope(ec, "timeline",
7701 "Timeline event points\n",
7702 weston_timeline_create_subscription,
7703 weston_timeline_destroy_subscription,
7704 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007705 return ec;
7706
7707fail:
7708 free(ec);
7709 return NULL;
7710}
7711
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007712/** weston_compositor_shutdown
7713 * \ingroup compositor
7714 */
Benjamin Franzkeb8263022011-08-30 11:32:47 +02007715WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007716weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07007717{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007718 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07007719
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007720 wl_event_source_remove(ec->idle_source);
7721
Matt Roper361d2ad2011-08-29 13:52:23 -07007722 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02007723 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07007724 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02007725
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007726 /* Destroy all pending outputs associated with this compositor */
7727 wl_list_for_each_safe(output, next, &ec->pending_output_list, link)
7728 output->destroy(output);
7729
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02007730 if (ec->renderer)
7731 ec->renderer->destroy(ec);
7732
Daniel Stone325fc2d2012-05-30 16:31:58 +01007733 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09007734 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01007735 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01007736 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01007737 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02007738 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007739
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04007740 weston_plane_release(&ec->primary_plane);
Matt Roper361d2ad2011-08-29 13:52:23 -07007741}
7742
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007743/** weston_compositor_exit_with_code
7744 * \ingroup compositor
7745 */
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05007746WL_EXPORT void
Adam Jackson3c3f3b12019-10-16 16:02:59 -04007747weston_compositor_exit_with_code(struct weston_compositor *compositor,
Frederic Plourdec336f062014-10-29 14:44:33 -04007748 int exit_code)
7749{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02007750 if (compositor->exit_code == EXIT_SUCCESS)
7751 compositor->exit_code = exit_code;
7752
Giulio Camuffo459137b2014-10-11 23:56:24 +03007753 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04007754}
7755
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007756/** weston_compositor_set_default_pointer_grab
7757 * \ingroup compositor
7758 */
Frederic Plourdec336f062014-10-29 14:44:33 -04007759WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007760weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
7761 const struct weston_pointer_grab_interface *interface)
7762{
7763 struct weston_seat *seat;
7764
7765 ec->default_pointer_grab = interface;
7766 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05007767 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
7768
7769 if (pointer)
7770 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007771 }
7772}
7773
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007774/** weston_compositor_set_presentation_clock
7775 * \ingroup compositor
7776 */
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007777WL_EXPORT int
7778weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
7779 clockid_t clk_id)
7780{
7781 struct timespec ts;
7782
7783 if (clock_gettime(clk_id, &ts) < 0)
7784 return -1;
7785
7786 compositor->presentation_clock = clk_id;
7787
7788 return 0;
7789}
7790
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007791/** For choosing the software clock, when the display hardware or API
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007792 * does not expose a compatible presentation timestamp.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007793 *
7794 * \ingroup compositor
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007795 */
7796WL_EXPORT int
7797weston_compositor_set_presentation_clock_software(
7798 struct weston_compositor *compositor)
7799{
7800 /* In order of preference */
7801 static const clockid_t clocks[] = {
7802 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
7803 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
7804 CLOCK_MONOTONIC, /* no jumps, may crawl */
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007805 };
7806 unsigned i;
7807
7808 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
7809 if (weston_compositor_set_presentation_clock(compositor,
7810 clocks[i]) == 0)
7811 return 0;
7812
7813 weston_log("Error: no suitable presentation clock available.\n");
7814
7815 return -1;
7816}
7817
Pekka Paalanen662f3842015-03-18 12:17:26 +02007818/** Read the current time from the Presentation clock
7819 *
7820 * \param compositor
Marius Vlada2dace22019-06-12 16:05:44 +03007821 * \param[out] ts The current time.
Pekka Paalanen662f3842015-03-18 12:17:26 +02007822 *
7823 * \note Reading the current time in user space is always imprecise to some
7824 * degree.
7825 *
7826 * This function is never meant to fail. If reading the clock does fail,
7827 * an error message is logged and a zero time is returned. Callers are not
7828 * supposed to detect or react to failures.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007829 *
7830 * \ingroup compositor
Pekka Paalanen662f3842015-03-18 12:17:26 +02007831 */
7832WL_EXPORT void
7833weston_compositor_read_presentation_clock(
7834 const struct weston_compositor *compositor,
7835 struct timespec *ts)
7836{
7837 static bool warned;
7838 int ret;
7839
7840 ret = clock_gettime(compositor->presentation_clock, ts);
7841 if (ret < 0) {
7842 ts->tv_sec = 0;
7843 ts->tv_nsec = 0;
7844
7845 if (!warned)
7846 weston_log("Error: failure to read "
Antonio Borneo39578632019-04-26 23:57:31 +02007847 "the presentation clock %#x: '%s' (%d)\n",
7848 compositor->presentation_clock,
7849 strerror(errno), errno);
Pekka Paalanen662f3842015-03-18 12:17:26 +02007850 warned = true;
7851 }
7852}
7853
Pekka Paalanen230f3b12014-09-29 14:18:40 -04007854/** Import dmabuf buffer into current renderer
7855 *
7856 * \param compositor
7857 * \param buffer the dmabuf buffer to import
7858 * \return true on usable buffers, false otherwise
7859 *
7860 * This function tests that the linux_dmabuf_buffer is usable
7861 * for the current renderer. Returns false on unusable buffers. Usually
7862 * usability is tested by importing the dmabufs for composition.
7863 *
7864 * This hook is also used for detecting if the renderer supports
7865 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
7866 * supported.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007867 *
7868 * \ingroup compositor
7869 */
Pekka Paalanen230f3b12014-09-29 14:18:40 -04007870WL_EXPORT bool
7871weston_compositor_import_dmabuf(struct weston_compositor *compositor,
7872 struct linux_dmabuf_buffer *buffer)
7873{
7874 struct weston_renderer *renderer;
7875
7876 renderer = compositor->renderer;
7877
7878 if (renderer->import_dmabuf == NULL)
7879 return false;
7880
7881 return renderer->import_dmabuf(compositor, buffer);
7882}
7883
Marius Vlad5a701542019-11-16 20:26:52 +02007884WL_EXPORT bool
7885weston_compositor_dmabuf_can_scanout(struct weston_compositor *compositor,
7886 struct linux_dmabuf_buffer *buffer)
7887{
7888 struct weston_backend *backend = compositor->backend;
7889
7890 if (backend->can_scanout_dmabuf == NULL)
7891 return false;
7892
7893 return backend->can_scanout_dmabuf(compositor, buffer);
7894}
7895
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007896WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05007897weston_version(int *major, int *minor, int *micro)
7898{
7899 *major = WESTON_VERSION_MAJOR;
7900 *minor = WESTON_VERSION_MINOR;
7901 *micro = WESTON_VERSION_MICRO;
7902}
7903
Daniel Stonee03c1112016-11-24 20:45:45 +00007904/**
7905 * Attempts to find a module path from the module map specified in the
7906 * environment. If found, writes the full path into the path variable.
7907 *
7908 * The module map is a string in environment variable WESTON_MODULE_MAP, where
7909 * each entry is of the form "name=path" and entries are separated by
7910 * semicolons. Whitespace is significant.
7911 *
7912 * \param name The name to search for.
7913 * \param path Where the path is written to if found.
7914 * \param path_len Allocated bytes at \c path .
7915 * \returns The length of the string written to path on success, or 0 if the
7916 * module was not specified in the environment map or path_len was too small.
7917 */
7918WL_EXPORT size_t
7919weston_module_path_from_env(const char *name, char *path, size_t path_len)
7920{
7921 const char *mapping = getenv("WESTON_MODULE_MAP");
7922 const char *end;
7923 const int name_len = strlen(name);
7924
7925 if (!mapping)
7926 return 0;
7927
7928 end = mapping + strlen(mapping);
7929 while (mapping < end && *mapping) {
7930 const char *filename, *next;
7931
7932 /* early out: impossibly short string */
7933 if (end - mapping < name_len + 1)
7934 return 0;
7935
7936 filename = &mapping[name_len + 1];
7937 next = strchrnul(mapping, ';');
7938
7939 if (strncmp(mapping, name, name_len) == 0 &&
7940 mapping[name_len] == '=') {
7941 size_t file_len = next - filename; /* no trailing NUL */
7942 if (file_len >= path_len)
7943 return 0;
7944 strncpy(path, filename, file_len);
7945 path[file_len] = '\0';
7946 return file_len;
7947 }
7948
7949 mapping = next + 1;
7950 }
7951
7952 return 0;
7953}
7954
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03007955WL_EXPORT void *
7956weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007957{
7958 char path[PATH_MAX];
7959 void *module, *init;
Daniel Stonebeb97e52016-11-28 12:13:54 +00007960 size_t len;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007961
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08007962 if (name == NULL)
7963 return NULL;
7964
Derek Foreman3f86e502015-06-08 11:46:54 -05007965 if (name[0] != '/') {
Daniel Stonee03c1112016-11-24 20:45:45 +00007966 len = weston_module_path_from_env(name, path, sizeof path);
7967 if (len == 0)
Daniel Stonebeb97e52016-11-28 12:13:54 +00007968 len = snprintf(path, sizeof path, "%s/%s",
7969 LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05007970 } else {
Daniel Stonebeb97e52016-11-28 12:13:54 +00007971 len = snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05007972 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007973
Daniel Stonebeb97e52016-11-28 12:13:54 +00007974 /* snprintf returns the length of the string it would've written,
7975 * _excluding_ the NUL byte. So even being equal to the size of
7976 * our buffer is an error here. */
7977 if (len >= sizeof path)
7978 return NULL;
7979
Kristian Høgsberga6813d22012-09-12 12:21:01 -04007980 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
7981 if (module) {
7982 weston_log("Module '%s' already loaded\n", path);
Pekka Paalanenf696ee92019-11-04 14:07:51 +02007983 } else {
7984 weston_log("Loading module '%s'\n", path);
7985 module = dlopen(path, RTLD_NOW);
7986 if (!module) {
7987 weston_log("Failed to load module: %s\n", dlerror());
7988 return NULL;
7989 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007990 }
7991
7992 init = dlsym(module, entrypoint);
7993 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03007994 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00007995 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007996 return NULL;
7997 }
7998
7999 return init;
8000}
8001
Pekka Paalanen6ffbba32019-11-06 12:59:32 +02008002/** Add a compositor destroy listener only once
8003 *
8004 * \param compositor The compositor whose destroy to watch for.
8005 * \param listener The listener struct to initialize.
8006 * \param destroy_handler The callback when compositor is destroyed.
8007 * \return True if listener is added, or false if there already is a listener
8008 * with the given \c destroy_handler.
8009 *
8010 * This function does nothing and returns false if the given callback function
8011 * is already present in the weston_compositor destroy callbacks list.
8012 * Otherwise, this function initializes the given listener with the given
8013 * callback pointer and adds it to the compositor's destroy callbacks list.
8014 *
8015 * This can be used to ensure that plugin initialization is done only once
8016 * in case the same plugin is loaded multiple times. If this function returns
8017 * false, the plugin should be already initialized successfully.
8018 *
8019 * All plugins should register a destroy listener for cleaning up. Note, that
8020 * the plugin destruction order is not guaranteed: plugins that depend on other
8021 * plugins must be able to be torn down in arbitrary order.
8022 *
Leandro Ribeiroca640d52020-01-27 19:12:01 -03008023 * \sa weston_compositor_destroy
Pekka Paalanen6ffbba32019-11-06 12:59:32 +02008024 */
8025WL_EXPORT bool
8026weston_compositor_add_destroy_listener_once(struct weston_compositor *compositor,
8027 struct wl_listener *listener,
8028 wl_notify_func_t destroy_handler)
8029{
8030 if (wl_signal_get(&compositor->destroy_signal, destroy_handler))
8031 return false;
8032
8033 listener->notify = destroy_handler;
8034 wl_signal_add(&compositor->destroy_signal, listener);
8035 return true;
8036}
Giulio Camuffo459137b2014-10-11 23:56:24 +03008037
Leandro Ribeiroca640d52020-01-27 19:12:01 -03008038/** Destroys the compositor.
Giulio Camuffo459137b2014-10-11 23:56:24 +03008039 *
Leandro Ribeiroca640d52020-01-27 19:12:01 -03008040 * This function cleans up the compositor state and then destroys it.
Giulio Camuffo459137b2014-10-11 23:56:24 +03008041 *
Leandro Ribeiroca640d52020-01-27 19:12:01 -03008042 * @param compositor The compositor to be destroyed.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008043 *
Marius Vlad284d5342019-06-24 12:00:47 +03008044 * @ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03008045 */
8046WL_EXPORT void
Leandro Ribeiroca640d52020-01-27 19:12:01 -03008047weston_compositor_destroy(struct weston_compositor *compositor)
Giulio Camuffo459137b2014-10-11 23:56:24 +03008048{
8049 /* prevent further rendering while shutting down */
8050 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
8051
8052 wl_signal_emit(&compositor->destroy_signal, compositor);
8053
8054 weston_compositor_xkb_destroy(compositor);
8055
Giulio Camuffo2d24e642015-10-03 16:25:15 +03008056 if (compositor->backend)
8057 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02008058
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03008059 /* The backend is responsible for destroying the heads. */
8060 assert(wl_list_empty(&compositor->head_list));
8061
Pekka Paalanen827b5d22016-06-29 11:54:26 +02008062 weston_plugin_api_destroy_list(compositor);
8063
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03008064 if (compositor->heads_changed_source)
8065 wl_event_source_remove(compositor->heads_changed_source);
8066
Leandro Ribeirof0149642019-12-18 15:52:18 -03008067 weston_log_scope_destroy(compositor->debug_scene);
Daniel Stonece62cb32018-07-20 09:46:24 +01008068 compositor->debug_scene = NULL;
Marius Vladda104eb2019-09-05 14:31:01 +03008069
Leandro Ribeirof0149642019-12-18 15:52:18 -03008070 weston_log_scope_destroy(compositor->timeline);
Marius Vladda104eb2019-09-05 14:31:01 +03008071 compositor->timeline = NULL;
Pekka Paalanena5630ea2017-10-12 13:13:42 +02008072
Giulio Camuffo459137b2014-10-11 23:56:24 +03008073 free(compositor);
8074}
8075
8076/** Instruct the compositor to exit.
8077 *
8078 * This functions does not directly destroy the compositor object, it merely
8079 * command it to start the tear down process. It is not guaranteed that the
8080 * tear down will happen immediately.
8081 *
8082 * \param compositor The compositor to tear down.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008083 *
8084 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03008085 */
8086WL_EXPORT void
8087weston_compositor_exit(struct weston_compositor *compositor)
8088{
8089 compositor->exit(compositor);
8090}
8091
8092/** Return the user data stored in the compositor.
8093 *
8094 * This function returns the user data pointer set with user_data parameter
8095 * to the \ref weston_compositor_create function.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008096 *
8097 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03008098 */
8099WL_EXPORT void *
8100weston_compositor_get_user_data(struct weston_compositor *compositor)
8101{
8102 return compositor->user_data;
8103}
Pekka Paalanendd186732016-06-03 14:49:54 +03008104
Pekka Paalanen50dbf382016-06-03 15:23:46 +03008105static const char * const backend_map[] = {
8106 [WESTON_BACKEND_DRM] = "drm-backend.so",
8107 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
8108 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
8109 [WESTON_BACKEND_RDP] = "rdp-backend.so",
8110 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
8111 [WESTON_BACKEND_X11] = "x11-backend.so",
8112};
8113
Pekka Paalanendd186732016-06-03 14:49:54 +03008114/** Load a backend into a weston_compositor
8115 *
8116 * A backend must be loaded to make a weston_compositor work. A backend
8117 * provides input and output capabilities, and determines the renderer to use.
8118 *
8119 * \param compositor A compositor that has not had a backend loaded yet.
8120 * \param backend Name of the backend file.
8121 * \param config_base A pointer to a backend-specific configuration
8122 * structure's 'base' member.
8123 *
8124 * \return 0 on success, or -1 on error.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008125 *
8126 * \ingroup compositor
Pekka Paalanendd186732016-06-03 14:49:54 +03008127 */
8128WL_EXPORT int
8129weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03008130 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03008131 struct weston_backend_config *config_base)
8132{
8133 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03008134 struct weston_backend_config *config_base);
8135
Pekka Paalanend7e35112017-08-29 17:04:12 +03008136 if (compositor->backend) {
8137 weston_log("Error: attempt to load a backend when one is already loaded\n");
8138 return -1;
8139 }
8140
Quentin Glidic887c0182016-07-10 11:00:53 +02008141 if (backend >= ARRAY_LENGTH(backend_map))
Pekka Paalanen50dbf382016-06-03 15:23:46 +03008142 return -1;
8143
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01008144 backend_init = weston_load_module(backend_map[backend], "weston_backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03008145 if (!backend_init)
8146 return -1;
8147
Pekka Paalanend7e35112017-08-29 17:04:12 +03008148 if (backend_init(compositor, config_base) < 0) {
8149 compositor->backend = NULL;
8150 return -1;
8151 }
8152
8153 return 0;
Pekka Paalanendd186732016-06-03 14:49:54 +03008154}
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008155
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008156/** weston_compositor_load_xwayland
8157 * \ingroup compositor
8158 */
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008159WL_EXPORT int
8160weston_compositor_load_xwayland(struct weston_compositor *compositor)
8161{
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01008162 int (*module_init)(struct weston_compositor *ec);
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008163
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01008164 module_init = weston_load_module("xwayland.so", "weston_module_init");
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008165 if (!module_init)
8166 return -1;
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01008167 if (module_init(compositor) < 0)
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008168 return -1;
8169 return 0;
8170}
Vasilis Liaskovitis486b4632018-10-10 16:14:55 +02008171
8172/** Resolve an internal compositor error by disconnecting the client.
8173 *
8174 * This function is used in cases when the wl_buffer turns out
8175 * unusable and there is no fallback path.
8176 *
8177 * It is possible the fault is caused by a compositor bug, the underlying
8178 * graphics stack bug or normal behaviour, or perhaps a client mistake.
8179 * In any case, the options are to either composite garbage or nothing,
8180 * or disconnect the client. This is a helper function for the latter.
8181 *
8182 * The error is sent as an INVALID_OBJECT error on the client's wl_display.
8183 *
8184 * \param buffer The weston buffer that is unusable.
8185 * \param msg A custom error message attached to the protocol error.
8186 */
8187WL_EXPORT void
8188weston_buffer_send_server_error(struct weston_buffer *buffer,
8189 const char *msg)
8190{
8191 struct wl_client *client;
8192 struct wl_resource *display_resource;
8193 uint32_t id;
8194
8195 assert(buffer->resource);
8196 id = wl_resource_get_id(buffer->resource);
8197 client = wl_resource_get_client(buffer->resource);
8198 display_resource = wl_client_get_object(client, 1);
8199
8200 assert(display_resource);
8201 wl_resource_post_error(display_resource,
8202 WL_DISPLAY_ERROR_INVALID_OBJECT,
8203 "server error with "
8204 "wl_buffer@%u: %s", id, msg);
8205}
Ankit Nautiyal93dde242019-07-08 11:46:42 +05308206
8207WL_EXPORT void
8208weston_output_disable_planes_incr(struct weston_output *output)
8209{
8210 output->disable_planes++;
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05308211 /*
8212 * If disable_planes changes from 0 to non-zero, it means some type of
8213 * recording of content has started, and therefore protection level of
8214 * the protected surfaces must be updated to avoid the recording of
8215 * the protected content.
8216 */
8217 if (output->disable_planes == 1)
8218 weston_schedule_surface_protection_update(output->compositor);
Ankit Nautiyal93dde242019-07-08 11:46:42 +05308219}
8220
8221WL_EXPORT void
8222weston_output_disable_planes_decr(struct weston_output *output)
8223{
8224 output->disable_planes--;
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05308225 /*
8226 * If disable_planes changes from non-zero to 0, it means no content
8227 * recording is going on any more, and the protected and surfaces can be
8228 * shown without any apprehensions about content being recorded.
8229 */
8230 if (output->disable_planes == 0)
8231 weston_schedule_surface_protection_update(output->compositor);
8232
Ankit Nautiyal93dde242019-07-08 11:46:42 +05308233}