blob: 33525e992868c525b872f3b1e41c1ed5de01ddcf [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
Jason Ekstranda7af7042013-10-12 22:38:11 -05002153 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02002154 if (!pixman_region32_contains_point(
2155 &view->transform.boundingbox, ix, iy, NULL))
2156 continue;
2157
2158 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
2159 view_ix = wl_fixed_to_int(view_x);
2160 view_iy = wl_fixed_to_int(view_y);
2161
2162 if (!pixman_region32_contains_point(&view->surface->input,
2163 view_ix, view_iy, NULL))
2164 continue;
2165
Pekka Paalanen380adf52015-02-16 14:39:11 +02002166 if (view->geometry.scissor_enabled &&
2167 !pixman_region32_contains_point(&view->geometry.scissor,
2168 view_ix, view_iy, NULL))
2169 continue;
2170
Pekka Paalanenfc22a522015-02-18 15:08:29 +02002171 *vx = view_x;
2172 *vy = view_y;
2173 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02002174 }
2175
Derek Foremanf9318d12015-05-11 15:40:11 -05002176 *vx = wl_fixed_from_int(-1000000);
2177 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02002178 return NULL;
2179}
2180
2181static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002182weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002183{
Daniel Stone37816df2012-05-16 18:45:18 +01002184 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002185
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07002186 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002187 return;
2188
Daniel Stone37816df2012-05-16 18:45:18 +01002189 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04002190 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002191}
2192
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002193WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002194weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05002195{
Daniel Stone4dab5db2012-05-30 16:31:53 +01002196 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002197
Jason Ekstranda7af7042013-10-12 22:38:11 -05002198 if (!weston_view_is_mapped(view))
2199 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002200
Jason Ekstranda7af7042013-10-12 22:38:11 -05002201 weston_view_damage_below(view);
Semi Malinene7a52fb2018-04-26 11:08:10 +02002202 weston_view_set_output(view, NULL);
Xiong Zhang97116532013-10-23 13:58:31 +08002203 view->plane = NULL;
Armin Krezovićf8486c32016-06-30 06:04:28 +02002204 view->is_mapped = false;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002205 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002206 wl_list_remove(&view->link);
2207 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002208 view->output_mask = 0;
2209 weston_surface_assign_output(view->surface);
2210
2211 if (weston_surface_is_mapped(view->surface))
2212 return;
2213
2214 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002215 struct weston_touch *touch = weston_seat_get_touch(seat);
2216 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2217 struct weston_keyboard *keyboard =
2218 weston_seat_get_keyboard(seat);
2219
2220 if (keyboard && keyboard->focus == view->surface)
2221 weston_keyboard_set_focus(keyboard, NULL);
2222 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05002223 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05002224 if (touch && touch->focus == view)
2225 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01002226 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002227}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002228
Jason Ekstranda7af7042013-10-12 22:38:11 -05002229WL_EXPORT void
2230weston_surface_unmap(struct weston_surface *surface)
2231{
2232 struct weston_view *view;
2233
Armin Krezovićf8486c32016-06-30 06:04:28 +02002234 surface->is_mapped = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002235 wl_list_for_each(view, &surface->views, surface_link)
2236 weston_view_unmap(view);
2237 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05002238}
2239
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002240static void
2241weston_surface_reset_pending_buffer(struct weston_surface *surface)
2242{
Jason Ekstrand7b982072014-05-20 14:33:03 -05002243 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002244 surface->pending.sx = 0;
2245 surface->pending.sy = 0;
2246 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002247 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002248}
2249
Jason Ekstranda7af7042013-10-12 22:38:11 -05002250WL_EXPORT void
2251weston_view_destroy(struct weston_view *view)
2252{
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002253 struct weston_paint_node *pnode, *pntmp;
2254
Jason Ekstranda7af7042013-10-12 22:38:11 -05002255 wl_signal_emit(&view->destroy_signal, view);
2256
2257 assert(wl_list_empty(&view->geometry.child_list));
2258
2259 if (weston_view_is_mapped(view)) {
2260 weston_view_unmap(view);
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002261 weston_compositor_build_view_list(view->surface->compositor,
2262 NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002263 }
2264
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002265 wl_list_for_each_safe(pnode, pntmp, &view->paint_node_list, view_link)
2266 weston_paint_node_destroy(pnode);
2267
Jason Ekstranda7af7042013-10-12 22:38:11 -05002268 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002269 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002270
2271 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02002272 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002273 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002274 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002275
2276 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen944fae82018-05-22 13:15:58 +03002277 weston_view_set_output(view, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002278
Jason Ekstranda7af7042013-10-12 22:38:11 -05002279 wl_list_remove(&view->surface_link);
2280
2281 free(view);
2282}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002283
2284WL_EXPORT void
2285weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05002286{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002287 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002288 struct weston_view *ev, *nv;
Jonas Ådahld3414f22016-07-22 17:56:31 +08002289 struct weston_pointer_constraint *constraint, *next_constraint;
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002290 struct weston_paint_node *pnode, *pntmp;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04002291
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02002292 if (--surface->ref_count > 0)
2293 return;
2294
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03002295 assert(surface->resource == NULL);
2296
Pekka Paalanenca790762015-04-17 14:23:38 +03002297 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02002298
Pekka Paalanene67858b2013-04-25 13:57:42 +03002299 assert(wl_list_empty(&surface->subsurface_list_pending));
2300 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02002301
Jason Ekstranda7af7042013-10-12 22:38:11 -05002302 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
2303 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002304
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002305 wl_list_for_each_safe(pnode, pntmp,
2306 &surface->paint_node_list, surface_link) {
2307 weston_paint_node_destroy(pnode);
2308 }
2309
Jason Ekstrand7b982072014-05-20 14:33:03 -05002310 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002311
Pekka Paalanende685b82012-12-04 15:58:12 +02002312 weston_buffer_reference(&surface->buffer_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002313 weston_buffer_release_reference(&surface->buffer_release_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04002314
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02002315 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002316 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002317 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02002318
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002319 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002320 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002321
Pekka Paalanen133e4392014-09-23 22:08:46 -04002322 weston_presentation_feedback_discard_list(&surface->feedback_list);
2323
Jonas Ådahld3414f22016-07-22 17:56:31 +08002324 wl_list_for_each_safe(constraint, next_constraint,
2325 &surface->pointer_constraints,
2326 link)
2327 weston_pointer_constraint_destroy(constraint);
2328
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03002329 fd_clear(&surface->acquire_fence_fd);
2330
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04002331 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05002332}
2333
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002334static void
2335destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08002336{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002337 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08002338
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03002339 assert(surface);
2340
Giulio Camuffo0d379742013-11-15 22:06:15 +01002341 /* Set the resource to NULL, since we don't want to leave a
2342 * dangling pointer if the surface was refcounted and survives
2343 * the weston_surface_destroy() call. */
2344 surface->resource = NULL;
Pekka Paalanen4826f872016-04-22 14:14:38 +03002345
2346 if (surface->viewport_resource)
2347 wl_resource_set_user_data(surface->viewport_resource, NULL);
2348
Alexandros Frantzis27d7c392018-10-19 12:14:11 +03002349 if (surface->synchronization_resource) {
2350 wl_resource_set_user_data(surface->synchronization_resource,
2351 NULL);
2352 }
2353
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002354 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08002355}
2356
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002357static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002358weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
2359{
2360 struct weston_buffer *buffer =
2361 container_of(listener, struct weston_buffer, destroy_listener);
2362
2363 wl_signal_emit(&buffer->destroy_signal, buffer);
2364 free(buffer);
2365}
2366
Giulio Camuffoe058cd12013-12-12 14:14:29 +01002367WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002368weston_buffer_from_resource(struct wl_resource *resource)
2369{
2370 struct weston_buffer *buffer;
2371 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002372
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002373 listener = wl_resource_get_destroy_listener(resource,
2374 weston_buffer_destroy_handler);
2375
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002376 if (listener)
2377 return container_of(listener, struct weston_buffer,
2378 destroy_listener);
2379
2380 buffer = zalloc(sizeof *buffer);
2381 if (buffer == NULL)
2382 return NULL;
2383
2384 buffer->resource = resource;
2385 wl_signal_init(&buffer->destroy_signal);
2386 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04002387 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002388 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002389
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002390 return buffer;
2391}
2392
2393static void
Pekka Paalanende685b82012-12-04 15:58:12 +02002394weston_buffer_reference_handle_destroy(struct wl_listener *listener,
2395 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002396{
Pekka Paalanende685b82012-12-04 15:58:12 +02002397 struct weston_buffer_reference *ref =
2398 container_of(listener, struct weston_buffer_reference,
2399 destroy_listener);
2400
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002401 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02002402 ref->buffer = NULL;
2403}
2404
2405WL_EXPORT void
2406weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002407 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002408{
2409 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05002410 ref->buffer->busy_count--;
2411 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002412 assert(wl_resource_get_client(ref->buffer->resource));
Matt Hoosier3052bc72017-09-26 08:09:40 -05002413 wl_buffer_send_release(ref->buffer->resource);
Kristian Høgsberg20347802013-03-04 12:07:46 -05002414 }
Pekka Paalanende685b82012-12-04 15:58:12 +02002415 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002416 }
2417
Pekka Paalanende685b82012-12-04 15:58:12 +02002418 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002419 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002420 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02002421 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02002422 }
2423
Pekka Paalanende685b82012-12-04 15:58:12 +02002424 ref->buffer = buffer;
2425 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
2426}
2427
2428static void
Alexandros Frantzis67629672018-10-19 12:14:11 +03002429weston_buffer_release_reference_handle_destroy(struct wl_listener *listener,
2430 void *data)
2431{
2432 struct weston_buffer_release_reference *ref =
2433 container_of(listener, struct weston_buffer_release_reference,
2434 destroy_listener);
2435
2436 assert((struct wl_resource *)data == ref->buffer_release->resource);
2437 ref->buffer_release = NULL;
2438}
2439
2440static void
2441weston_buffer_release_destroy(struct weston_buffer_release *buffer_release)
2442{
2443 struct wl_resource *resource = buffer_release->resource;
2444 int release_fence_fd = buffer_release->fence_fd;
2445
2446 if (release_fence_fd >= 0) {
2447 zwp_linux_buffer_release_v1_send_fenced_release(
2448 resource, release_fence_fd);
2449 } else {
2450 zwp_linux_buffer_release_v1_send_immediate_release(
2451 resource);
2452 }
2453
2454 wl_resource_destroy(resource);
2455}
2456
2457WL_EXPORT void
2458weston_buffer_release_reference(struct weston_buffer_release_reference *ref,
2459 struct weston_buffer_release *buffer_release)
2460{
2461 if (buffer_release == ref->buffer_release)
2462 return;
2463
2464 if (ref->buffer_release) {
2465 ref->buffer_release->ref_count--;
2466 wl_list_remove(&ref->destroy_listener.link);
2467 if (ref->buffer_release->ref_count == 0)
2468 weston_buffer_release_destroy(ref->buffer_release);
2469 }
2470
2471 if (buffer_release) {
2472 buffer_release->ref_count++;
2473 wl_resource_add_destroy_listener(buffer_release->resource,
2474 &ref->destroy_listener);
2475 }
2476
2477 ref->buffer_release = buffer_release;
2478 ref->destroy_listener.notify =
2479 weston_buffer_release_reference_handle_destroy;
2480}
2481
2482WL_EXPORT void
2483weston_buffer_release_move(struct weston_buffer_release_reference *dest,
2484 struct weston_buffer_release_reference *src)
2485{
2486 weston_buffer_release_reference(dest, src->buffer_release);
2487 weston_buffer_release_reference(src, NULL);
2488}
2489
2490static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002491weston_surface_attach(struct weston_surface *surface,
2492 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002493{
2494 weston_buffer_reference(&surface->buffer_ref, buffer);
2495
Pekka Paalanena6421c42012-12-04 15:58:10 +02002496 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002497 if (weston_surface_is_mapped(surface))
2498 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002499 }
2500
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002501 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02002502
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002503 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002504 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002505}
2506
Marius Vlad9fdda7f2019-06-11 16:08:55 +03002507/** weston_compositor_damage_all
2508 * \ingroup compositor
2509 */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002510WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002511weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002512{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002513 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002514
2515 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002516 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002517}
2518
Marius Vlad55d87362019-06-11 01:15:35 +03002519/**
2520 * \ingroup output
2521 */
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05002522WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002523weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002524{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002525 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002526
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002527 pixman_region32_union(&compositor->primary_plane.damage,
2528 &compositor->primary_plane.damage,
2529 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002530 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002531}
2532
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04002533static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002534surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002535{
Pekka Paalanende685b82012-12-04 15:58:12 +02002536 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002537 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002538 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002539
Marius Vlad2a1b7862019-09-05 13:12:18 +03002540 if (pixman_region32_not_empty(&surface->damage))
Marius Vlad3203ff62019-09-05 14:56:12 +03002541 TL_POINT(surface->compositor, "core_flush_damage", TLP_SURFACE(surface),
Pekka Paalanenb5026542014-11-12 15:09:24 +02002542 TLP_OUTPUT(surface->output), TLP_END);
2543
Jason Ekstrandef540082014-06-26 10:37:36 -07002544 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002545}
2546
2547static void
2548view_accumulate_damage(struct weston_view *view,
2549 pixman_region32_t *opaque)
2550{
2551 pixman_region32_t damage;
2552
2553 pixman_region32_init(&damage);
2554 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002555 pixman_box32_t *extents;
2556
Jason Ekstranda7af7042013-10-12 22:38:11 -05002557 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002558 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002559 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002560 pixman_region32_copy(&damage, &view->surface->damage);
2561 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002562 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002563 }
2564
Pekka Paalanen380adf52015-02-16 14:39:11 +02002565 pixman_region32_intersect(&damage, &damage,
2566 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002567 pixman_region32_subtract(&damage, &damage, opaque);
2568 pixman_region32_union(&view->plane->damage,
2569 &view->plane->damage, &damage);
2570 pixman_region32_fini(&damage);
2571 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002572 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002573}
2574
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002575static void
Michael Olbrichc5ea4952020-08-11 12:42:35 +02002576output_accumulate_damage(struct weston_output *output)
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002577{
Michael Olbrichc5ea4952020-08-11 12:42:35 +02002578 struct weston_compositor *ec = output->compositor;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002579 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002580 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002581 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002582
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002583 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002584
2585 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002586 pixman_region32_copy(&plane->clip, &clip);
2587
2588 pixman_region32_init(&opaque);
2589
Jason Ekstranda7af7042013-10-12 22:38:11 -05002590 wl_list_for_each(ev, &ec->view_list, link) {
2591 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002592 continue;
2593
Jason Ekstranda7af7042013-10-12 22:38:11 -05002594 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002595 }
2596
2597 pixman_region32_union(&clip, &clip, &opaque);
2598 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002599 }
2600
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002601 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002602
Jason Ekstranda7af7042013-10-12 22:38:11 -05002603 wl_list_for_each(ev, &ec->view_list, link)
Derek Foreman060cf112015-11-18 16:32:26 -06002604 ev->surface->touched = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002605
2606 wl_list_for_each(ev, &ec->view_list, link) {
Michael Olbrichc5ea4952020-08-11 12:42:35 +02002607 /* Ignore views not visible on the current output */
2608 if (!(ev->output_mask & (1u << output->id)))
2609 continue;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002610 if (ev->surface->touched)
2611 continue;
Derek Foreman060cf112015-11-18 16:32:26 -06002612 ev->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002613
2614 surface_flush_damage(ev->surface);
2615
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002616 /* Both the renderer and the backend have seen the buffer
2617 * by now. If renderer needs the buffer, it has its own
2618 * reference set. If the backend wants to keep the buffer
2619 * around for migrating the surface into a non-primary plane
2620 * later, keep_buffer is true. Otherwise, drop the core
2621 * reference now, and allow early buffer release. This enables
2622 * clients to use single-buffering.
2623 */
Alexandros Frantzis67629672018-10-19 12:14:11 +03002624 if (!ev->surface->keep_buffer) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002625 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002626 weston_buffer_release_reference(
2627 &ev->surface->buffer_release_ref, NULL);
2628 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002629 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002630}
2631
2632static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002633surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002634{
2635 struct weston_subsurface *sub;
2636
Pekka Paalanene67858b2013-04-25 13:57:42 +03002637 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002638 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002639 continue;
2640
Jason Ekstranda7af7042013-10-12 22:38:11 -05002641 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2642 wl_list_init(&sub->surface->views);
2643
2644 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002645 }
2646}
2647
2648static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002649surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002650{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002651 struct weston_subsurface *sub;
2652 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002653
Jason Ekstranda7af7042013-10-12 22:38:11 -05002654 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2655 if (sub->surface == surface)
2656 continue;
2657
George Kiagiadakised04d382014-06-13 18:10:26 +02002658 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2659 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002660 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002661 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002662
2663 surface_free_unused_subsurface_views(sub->surface);
2664 }
2665}
2666
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002667static struct weston_paint_node *
2668view_ensure_paint_node(struct weston_view *view, struct weston_output *output)
2669{
2670 struct weston_paint_node *pnode;
2671
2672 if (!output)
2673 return NULL;
2674
2675 pnode = weston_view_find_paint_node(view, output);
2676 if (pnode)
2677 return pnode;
2678
2679 return weston_paint_node_create(view->surface, view, output);
2680}
2681
Jason Ekstranda7af7042013-10-12 22:38:11 -05002682static void
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002683add_to_z_order_list(struct weston_output *output,
2684 struct weston_paint_node *pnode)
2685{
2686 if (!pnode)
2687 return;
2688
2689 wl_list_remove(&pnode->z_order_link);
2690 wl_list_insert(output->paint_node_z_order_list.prev,
2691 &pnode->z_order_link);
2692}
2693
2694static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002695view_list_add_subsurface_view(struct weston_compositor *compositor,
2696 struct weston_subsurface *sub,
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002697 struct weston_view *parent,
2698 struct weston_output *output)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002699{
2700 struct weston_subsurface *child;
2701 struct weston_view *view = NULL, *iv;
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002702 struct weston_paint_node *pnode;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002703
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002704 if (!weston_surface_is_mapped(sub->surface))
2705 return;
2706
Jason Ekstranda7af7042013-10-12 22:38:11 -05002707 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2708 if (iv->geometry.parent == parent) {
2709 view = iv;
2710 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002711 }
2712 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002713
2714 if (view) {
2715 /* Put it back in the surface's list of views */
2716 wl_list_remove(&view->surface_link);
2717 wl_list_insert(&sub->surface->views, &view->surface_link);
2718 } else {
2719 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002720 weston_view_set_position(view,
2721 sub->position.x,
2722 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002723 weston_view_set_transform_parent(view, parent);
2724 }
2725
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002726 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002727 weston_view_update_transform(view);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002728 view->is_mapped = true;
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002729 pnode = view_ensure_paint_node(view, output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002730
Pekka Paalanenb188e912013-11-19 14:03:35 +02002731 if (wl_list_empty(&sub->surface->subsurface_list)) {
2732 wl_list_insert(compositor->view_list.prev, &view->link);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002733 add_to_z_order_list(output, pnode);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002734 return;
2735 }
2736
2737 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002738 if (child->surface == sub->surface) {
Pekka Paalanenb188e912013-11-19 14:03:35 +02002739 wl_list_insert(compositor->view_list.prev, &view->link);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002740 add_to_z_order_list(output, pnode);
2741 } else {
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002742 view_list_add_subsurface_view(compositor, child, view, output);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002743 }
Pekka Paalanenb188e912013-11-19 14:03:35 +02002744 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002745}
2746
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002747/* This recursively adds the sub-surfaces for a view, relying on the
2748 * sub-surface order. Thus, if a client restacks the sub-surfaces, that
2749 * change first happens to the sub-surface list, and then automatically
2750 * propagates here. See weston_surface_damage_subsurfaces() for how the
2751 * sub-surfaces receive damage when the client changes the state.
2752 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002753static void
2754view_list_add(struct weston_compositor *compositor,
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002755 struct weston_view *view,
2756 struct weston_output *output)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002757{
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002758 struct weston_paint_node *pnode;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002759 struct weston_subsurface *sub;
2760
2761 weston_view_update_transform(view);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002762 pnode = view_ensure_paint_node(view, output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002763
Pekka Paalanenb188e912013-11-19 14:03:35 +02002764 if (wl_list_empty(&view->surface->subsurface_list)) {
2765 wl_list_insert(compositor->view_list.prev, &view->link);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002766 add_to_z_order_list(output, pnode);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002767 return;
2768 }
2769
2770 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002771 if (sub->surface == view->surface) {
Pekka Paalanenb188e912013-11-19 14:03:35 +02002772 wl_list_insert(compositor->view_list.prev, &view->link);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002773 add_to_z_order_list(output, pnode);
2774 } else {
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002775 view_list_add_subsurface_view(compositor, sub, view, output);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002776 }
Pekka Paalanenb188e912013-11-19 14:03:35 +02002777 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002778}
2779
2780static void
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002781weston_compositor_build_view_list(struct weston_compositor *compositor,
2782 struct weston_output *output)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002783{
Loïc Yhuel267b16e2019-09-17 20:14:56 +02002784 struct weston_view *view, *tmp;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002785 struct weston_layer *layer;
2786
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002787 if (output) {
2788 wl_list_remove(&output->paint_node_z_order_list);
2789 wl_list_init(&output->paint_node_z_order_list);
2790 }
2791
Jason Ekstranda7af7042013-10-12 22:38:11 -05002792 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002793 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002794 surface_stash_subsurface_views(view->surface);
2795
Loïc Yhuel267b16e2019-09-17 20:14:56 +02002796 wl_list_for_each_safe(view, tmp, &compositor->view_list, link)
2797 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002798 wl_list_init(&compositor->view_list);
Loïc Yhuel267b16e2019-09-17 20:14:56 +02002799
Jason Ekstranda7af7042013-10-12 22:38:11 -05002800 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002801 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002802 view_list_add(compositor, view, output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002803 }
2804 }
2805
2806 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)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002808 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002809}
2810
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002811static void
2812weston_output_take_feedback_list(struct weston_output *output,
2813 struct weston_surface *surface)
2814{
2815 struct weston_view *view;
2816 struct weston_presentation_feedback *feedback;
2817 uint32_t flags = 0xffffffff;
2818
2819 if (wl_list_empty(&surface->feedback_list))
2820 return;
2821
2822 /* All views must have the flag for the flag to survive. */
2823 wl_list_for_each(view, &surface->views, surface_link) {
2824 /* ignore views that are not on this output at all */
2825 if (view->output_mask & (1u << output->id))
2826 flags &= view->psf_flags;
2827 }
2828
2829 wl_list_for_each(feedback, &surface->feedback_list, link)
2830 feedback->psf_flags = flags;
2831
2832 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2833 wl_list_init(&surface->feedback_list);
2834}
2835
David Herrmann1edf44c2013-10-22 17:11:26 +02002836static int
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002837weston_output_repaint(struct weston_output *output, void *repaint_data)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002838{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002839 struct weston_compositor *ec = output->compositor;
Pekka Paalanen6d6b3672021-05-03 14:06:55 +03002840 struct weston_paint_node *pnode;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002841 struct weston_animation *animation, *next;
2842 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002843 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002844 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002845 int r;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002846 uint32_t frame_time_msec;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05302847 enum weston_hdcp_protection highest_requested = WESTON_HDCP_DISABLE;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002848
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002849 if (output->destroying)
2850 return 0;
2851
Marius Vlad3203ff62019-09-05 14:56:12 +03002852 TL_POINT(ec, "core_repaint_begin", TLP_OUTPUT(output), TLP_END);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002853
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002854 /* Rebuild the surface list and update surface transforms up front. */
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002855 weston_compositor_build_view_list(ec, output);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002856
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05302857 /* Find the highest protection desired for an output */
Pekka Paalanen6d6b3672021-05-03 14:06:55 +03002858 wl_list_for_each(pnode, &output->paint_node_z_order_list,
2859 z_order_link) {
2860 /* TODO: turn this into assert once z_order_list is pruned. */
2861 if ((pnode->surface->output_mask & (1u << output->id)) == 0)
2862 continue;
2863
2864 /*
2865 * The desired_protection of the output should be the
2866 * maximum of the desired_protection of the surfaces,
2867 * that are displayed on that output, to avoid
2868 * reducing the protection for existing surfaces.
2869 */
2870 if (pnode->surface->desired_protection > highest_requested)
2871 highest_requested = pnode->surface->desired_protection;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05302872 }
2873
2874 output->desired_protection = highest_requested;
2875
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002876 if (output->assign_planes && !output->disable_planes) {
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002877 output->assign_planes(output, repaint_data);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002878 } else {
Pekka Paalanen6d6b3672021-05-03 14:06:55 +03002879 wl_list_for_each(pnode, &output->paint_node_z_order_list,
2880 z_order_link) {
2881 weston_view_move_to_plane(pnode->view, &ec->primary_plane);
2882 pnode->view->psf_flags = 0;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002883 }
2884 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002885
Pekka Paalanene67858b2013-04-25 13:57:42 +03002886 wl_list_init(&frame_callback_list);
Pekka Paalanen6d6b3672021-05-03 14:06:55 +03002887 wl_list_for_each(pnode, &output->paint_node_z_order_list,
2888 z_order_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002889 /* Note: This operation is safe to do multiple times on the
2890 * same surface.
2891 */
Pekka Paalanen6d6b3672021-05-03 14:06:55 +03002892 if (pnode->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002893 wl_list_insert_list(&frame_callback_list,
Pekka Paalanen6d6b3672021-05-03 14:06:55 +03002894 &pnode->surface->frame_callback_list);
2895 wl_list_init(&pnode->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002896
Pekka Paalanen6d6b3672021-05-03 14:06:55 +03002897 weston_output_take_feedback_list(output, pnode->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002898 }
2899 }
2900
Michael Olbrichc5ea4952020-08-11 12:42:35 +02002901 output_accumulate_damage(output);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002902
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002903 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002904 pixman_region32_intersect(&output_damage,
2905 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002906 pixman_region32_subtract(&output_damage,
2907 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002908
Scott Moreauccbf29d2012-02-22 14:21:41 -07002909 if (output->dirty)
2910 weston_output_update_matrix(output);
2911
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002912 r = output->repaint(output, &output_damage, repaint_data);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002913
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002914 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002915
Daniel Stone09a97e22017-03-01 11:34:06 +00002916 output->repaint_needed = false;
Daniel Stone05df8c12017-03-03 16:59:42 +00002917 if (r == 0)
2918 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002919
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002920 weston_compositor_repick(ec);
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002921
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002922 frame_time_msec = timespec_to_msec(&output->frame_time);
2923
Jonas Ådahldb773762012-06-13 00:01:21 +02002924 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002925 wl_callback_send_done(cb->resource, frame_time_msec);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002926 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002927 }
2928
Scott Moreaud64cf212012-06-08 19:40:54 -06002929 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002930 animation->frame_counter++;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002931 animation->frame(animation, output, &output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002932 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002933
Marius Vlad3203ff62019-09-05 14:56:12 +03002934 TL_POINT(ec, "core_repaint_posted", TLP_OUTPUT(output), TLP_END);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002935
David Herrmann1edf44c2013-10-22 17:11:26 +02002936 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002937}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002938
Pekka Paalanen82919792014-05-21 13:51:49 +03002939static void
2940weston_output_schedule_repaint_reset(struct weston_output *output)
2941{
Daniel Stone05df8c12017-03-03 16:59:42 +00002942 output->repaint_status = REPAINT_NOT_SCHEDULED;
Marius Vlad3203ff62019-09-05 14:56:12 +03002943 TL_POINT(output->compositor, "core_repaint_exit_loop",
2944 TLP_OUTPUT(output), TLP_END);
Pekka Paalanen82919792014-05-21 13:51:49 +03002945}
2946
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002947static int
2948weston_output_maybe_repaint(struct weston_output *output, struct timespec *now,
2949 void *repaint_data)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002950{
Pekka Paalanen0513a952014-05-21 16:17:27 +03002951 struct weston_compositor *compositor = output->compositor;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002952 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002953 int64_t msec_to_repaint;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002954
Daniel Stone6847b852017-03-01 11:34:08 +00002955 /* We're not ready yet; come back to make a decision later. */
2956 if (output->repaint_status != REPAINT_SCHEDULED)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002957 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002958
2959 msec_to_repaint = timespec_sub_to_msec(&output->next_repaint, now);
2960 if (msec_to_repaint > 1)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002961 return ret;
Daniel Stone05df8c12017-03-03 16:59:42 +00002962
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002963 /* If we're sleeping, drop the repaint machinery entirely; we will
2964 * explicitly repaint all outputs when we come back. */
2965 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2966 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
2967 goto err;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002968
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002969 /* We don't actually need to repaint this output; drop it from
2970 * repaint until something causes damage. */
2971 if (!output->repaint_needed)
2972 goto err;
2973
2974 /* If repaint fails, we aren't going to get weston_output_finish_frame
2975 * to trigger a new repaint, so drop it from repaint and hope
Daniel Stone6847b852017-03-01 11:34:08 +00002976 * something schedules a successful repaint later. As repainting may
2977 * take some time, re-read our clock as a courtesy to the next
2978 * output. */
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002979 ret = weston_output_repaint(output, repaint_data);
Daniel Stone6847b852017-03-01 11:34:08 +00002980 weston_compositor_read_presentation_clock(compositor, now);
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002981 if (ret != 0)
2982 goto err;
2983
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09002984 output->repainted = true;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002985 return ret;
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002986
2987err:
Pekka Paalanen0513a952014-05-21 16:17:27 +03002988 weston_output_schedule_repaint_reset(output);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002989 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002990}
2991
2992static void
2993output_repaint_timer_arm(struct weston_compositor *compositor)
2994{
2995 struct weston_output *output;
2996 bool any_should_repaint = false;
2997 struct timespec now;
Sergi Granellb4c08862017-03-18 13:01:15 +01002998 int64_t msec_to_next = INT64_MAX;
Daniel Stone6847b852017-03-01 11:34:08 +00002999
3000 weston_compositor_read_presentation_clock(compositor, &now);
3001
3002 wl_list_for_each(output, &compositor->output_list, link) {
3003 int64_t msec_to_this;
3004
3005 if (output->repaint_status != REPAINT_SCHEDULED)
3006 continue;
3007
3008 msec_to_this = timespec_sub_to_msec(&output->next_repaint,
3009 &now);
3010 if (!any_should_repaint || msec_to_this < msec_to_next)
3011 msec_to_next = msec_to_this;
3012
3013 any_should_repaint = true;
3014 }
3015
3016 if (!any_should_repaint)
3017 return;
3018
3019 /* Even if we should repaint immediately, add the minimum 1 ms delay.
3020 * This is a workaround to allow coalescing multiple output repaints
3021 * particularly from weston_output_finish_frame()
3022 * into the same call, which would not happen if we called
3023 * output_repaint_timer_handler() directly.
3024 */
3025 if (msec_to_next < 1)
3026 msec_to_next = 1;
3027
3028 wl_event_source_timer_update(compositor->repaint_timer, msec_to_next);
3029}
3030
3031static int
3032output_repaint_timer_handler(void *data)
3033{
3034 struct weston_compositor *compositor = data;
3035 struct weston_output *output;
3036 struct timespec now;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003037 void *repaint_data = NULL;
Emre Ucane479ed82018-03-20 15:29:40 +01003038 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00003039
3040 weston_compositor_read_presentation_clock(compositor, &now);
Pekka Paalanen1ed2cad2021-02-10 12:33:03 +02003041 compositor->last_repaint_start = now;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003042
3043 if (compositor->backend->repaint_begin)
3044 repaint_data = compositor->backend->repaint_begin(compositor);
3045
3046 wl_list_for_each(output, &compositor->output_list, link) {
3047 ret = weston_output_maybe_repaint(output, &now, repaint_data);
3048 if (ret)
3049 break;
3050 }
3051
3052 if (ret == 0) {
Tomohito Esaki09bfcd62018-06-05 10:37:05 +09003053 if (compositor->backend->repaint_flush)
Antonio Borneoc90fccc2019-06-30 15:51:10 +02003054 ret = compositor->backend->repaint_flush(compositor,
3055 repaint_data);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003056 } else {
Antonio Borneoc90fccc2019-06-30 15:51:10 +02003057 if (compositor->backend->repaint_cancel)
3058 compositor->backend->repaint_cancel(compositor,
3059 repaint_data);
3060 }
3061
3062 if (ret != 0) {
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09003063 wl_list_for_each(output, &compositor->output_list, link) {
3064 if (output->repainted)
3065 weston_output_schedule_repaint_reset(output);
3066 }
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003067 }
Daniel Stone6847b852017-03-01 11:34:08 +00003068
Tomohito Esakiddaf95c2018-07-10 11:47:15 +09003069 wl_list_for_each(output, &compositor->output_list, link)
3070 output->repainted = false;
3071
Daniel Stone6847b852017-03-01 11:34:08 +00003072 output_repaint_timer_arm(compositor);
3073
Pekka Paalanen0513a952014-05-21 16:17:27 +03003074 return 0;
3075}
3076
Pekka Paalanen50aa3a72020-05-28 11:34:04 +03003077/** Convert a presentation timestamp to another clock domain
3078 *
3079 * \param compositor The compositor defines the presentation clock domain.
3080 * \param presentation_stamp The timestamp in presentation clock domain.
3081 * \param presentation_now Current time in presentation clock domain.
3082 * \param target_clock Defines the target clock domain.
3083 *
3084 * This approximation relies on presentation_stamp to be close to current time.
3085 * The further it is from current time and the bigger the speed difference
3086 * between the two clock domains, the bigger the conversion error.
3087 *
3088 * Conversion error due to system load is biased and unbounded.
3089 */
3090static struct timespec
3091convert_presentation_time_now(struct weston_compositor *compositor,
3092 const struct timespec *presentation_stamp,
3093 const struct timespec *presentation_now,
3094 clockid_t target_clock)
3095{
3096 struct timespec target_now = {};
3097 struct timespec target_stamp;
3098 int64_t delta_ns;
3099
3100 if (compositor->presentation_clock == target_clock)
3101 return *presentation_stamp;
3102
3103 clock_gettime(target_clock, &target_now);
3104 delta_ns = timespec_sub_to_nsec(presentation_stamp, presentation_now);
3105 timespec_add_nsec(&target_stamp, &target_now, delta_ns);
3106
3107 return target_stamp;
3108}
3109
Marius Vlad55d87362019-06-11 01:15:35 +03003110/**
3111 * \ingroup output
3112 */
Kristian Høgsbergef044142011-06-21 15:02:12 -04003113WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04003114weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02003115 const struct timespec *stamp,
3116 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04003117{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003118 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03003119 int32_t refresh_nsec;
3120 struct timespec now;
Pekka Paalanen50aa3a72020-05-28 11:34:04 +03003121 struct timespec vblank_monotonic;
Daniel Stone6847b852017-03-01 11:34:08 +00003122 int64_t msec_rel;
Pekka Paalanen133e4392014-09-23 22:08:46 -04003123
Daniel Stone05df8c12017-03-03 16:59:42 +00003124 assert(output->repaint_status == REPAINT_AWAITING_COMPLETION);
Pekka Paalanen98b4e202021-05-10 11:33:23 +03003125
3126 /*
3127 * If timestamp of latest vblank is given, it must always go forwards.
3128 * If not given, INVALID flag must be set.
3129 */
3130 if (stamp)
3131 assert(timespec_sub_to_nsec(stamp, &output->frame_time) >= 0);
3132 else
3133 assert(presented_flags & WP_PRESENTATION_FEEDBACK_INVALID);
Daniel Stone3615ce12017-03-01 11:34:05 +00003134
Daniel Stone6847b852017-03-01 11:34:08 +00003135 weston_compositor_read_presentation_clock(compositor, &now);
3136
Daniel Stone3615ce12017-03-01 11:34:05 +00003137 /* If we haven't been supplied any timestamp at all, we don't have a
3138 * timebase to work against, so any delay just wastes time. Push a
3139 * repaint as soon as possible so we can get on with it. */
Daniel Stone6847b852017-03-01 11:34:08 +00003140 if (!stamp) {
3141 output->next_repaint = now;
Daniel Stone3615ce12017-03-01 11:34:05 +00003142 goto out;
Daniel Stone6847b852017-03-01 11:34:08 +00003143 }
Daniel Stone3615ce12017-03-01 11:34:05 +00003144
Pekka Paalanen50aa3a72020-05-28 11:34:04 +03003145 vblank_monotonic = convert_presentation_time_now(compositor,
3146 stamp, &now,
3147 CLOCK_MONOTONIC);
Marius Vlad3203ff62019-09-05 14:56:12 +03003148 TL_POINT(compositor, "core_repaint_finished", TLP_OUTPUT(output),
Pekka Paalanen50aa3a72020-05-28 11:34:04 +03003149 TLP_VBLANK(&vblank_monotonic), TLP_END);
Marius Vladdf9278a2018-03-06 18:56:23 +02003150
Pekka Paalanend7894d02015-07-03 15:08:53 +03003151 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003152 weston_presentation_feedback_present_list(&output->feedback_list,
3153 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02003154 output->msc,
3155 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003156
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02003157 output->frame_time = *stamp;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003158
Daniel Stone6847b852017-03-01 11:34:08 +00003159 timespec_add_nsec(&output->next_repaint, stamp, refresh_nsec);
3160 timespec_add_msec(&output->next_repaint, &output->next_repaint,
3161 -compositor->repaint_msec);
3162 msec_rel = timespec_sub_to_msec(&output->next_repaint, &now);
Daniel Stone84aff5c2017-03-01 11:34:04 +00003163
3164 if (msec_rel < -1000 || msec_rel > 1000) {
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02003165 static bool warned;
3166
3167 if (!warned)
3168 weston_log("Warning: computed repaint delay is "
Daniel Stone6847b852017-03-01 11:34:08 +00003169 "insane: %lld msec\n", (long long) msec_rel);
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02003170 warned = true;
3171
Daniel Stone6847b852017-03-01 11:34:08 +00003172 output->next_repaint = now;
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02003173 }
3174
Mario Kleinerb7df04e2015-06-21 21:25:15 +02003175 /* Called from restart_repaint_loop and restart happens already after
3176 * the deadline given by repaint_msec? In that case we delay until
3177 * the deadline of the next frame, to give clients a more predictable
3178 * timing of the repaint cycle to lock on. */
Daniel Stoneeca5cca2017-02-28 21:53:51 +00003179 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID &&
3180 msec_rel < 0) {
3181 while (timespec_sub_to_nsec(&output->next_repaint, &now) < 0) {
3182 timespec_add_nsec(&output->next_repaint,
3183 &output->next_repaint,
3184 refresh_nsec);
3185 }
3186 }
Mario Kleinerb7df04e2015-06-21 21:25:15 +02003187
Daniel Stone3615ce12017-03-01 11:34:05 +00003188out:
Daniel Stone05df8c12017-03-03 16:59:42 +00003189 output->repaint_status = REPAINT_SCHEDULED;
Daniel Stone6847b852017-03-01 11:34:08 +00003190 output_repaint_timer_arm(compositor);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003191}
3192
3193static void
3194idle_repaint(void *data)
3195{
3196 struct weston_output *output = data;
Antonio Borneoc90fccc2019-06-30 15:51:10 +02003197 int ret;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003198
Daniel Stone05df8c12017-03-03 16:59:42 +00003199 assert(output->repaint_status == REPAINT_BEGIN_FROM_IDLE);
3200 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03003201 output->idle_repaint_source = NULL;
Antonio Borneoc90fccc2019-06-30 15:51:10 +02003202 ret = output->start_repaint_loop(output);
3203 if (ret != 0)
3204 weston_output_schedule_repaint_reset(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003205}
3206
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003207WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003208weston_layer_entry_insert(struct weston_layer_entry *list,
3209 struct weston_layer_entry *entry)
3210{
3211 wl_list_insert(&list->link, &entry->link);
3212 entry->layer = list->layer;
3213}
3214
3215WL_EXPORT void
3216weston_layer_entry_remove(struct weston_layer_entry *entry)
3217{
3218 wl_list_remove(&entry->link);
3219 wl_list_init(&entry->link);
3220 entry->layer = NULL;
3221}
3222
Quentin Glidic82681572016-12-17 13:40:51 +01003223
3224/** Initialize the weston_layer struct.
3225 *
3226 * \param compositor The compositor instance
3227 * \param layer The layer to initialize
3228 */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003229WL_EXPORT void
Quentin Glidic82681572016-12-17 13:40:51 +01003230weston_layer_init(struct weston_layer *layer,
3231 struct weston_compositor *compositor)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003232{
Quentin Glidic82681572016-12-17 13:40:51 +01003233 layer->compositor = compositor;
3234 wl_list_init(&layer->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003235 wl_list_init(&layer->view_list.link);
3236 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003237 weston_layer_set_mask_infinite(layer);
Quentin Glidic82681572016-12-17 13:40:51 +01003238}
3239
3240/** Sets the position of the layer in the layer list. The layer will be placed
3241 * below any layer with the same position value, if any.
3242 * This function is safe to call if the layer is already on the list, but the
3243 * layer may be moved below other layers at the same position, if any.
3244 *
3245 * \param layer The layer to modify
3246 * \param position The position the layer will be placed at
3247 */
3248WL_EXPORT void
3249weston_layer_set_position(struct weston_layer *layer,
3250 enum weston_layer_position position)
3251{
3252 struct weston_layer *below;
3253
3254 wl_list_remove(&layer->link);
3255
3256 /* layer_list is ordered from top to bottom, the last layer being the
3257 * background with the smallest position value */
3258
3259 layer->position = position;
3260 wl_list_for_each_reverse(below, &layer->compositor->layer_list, link) {
3261 if (below->position >= layer->position) {
3262 wl_list_insert(&below->link, &layer->link);
3263 return;
3264 }
3265 }
3266 wl_list_insert(&layer->compositor->layer_list, &layer->link);
3267}
3268
3269/** Hide a layer by taking it off the layer list.
3270 * This function is safe to call if the layer is not on the list.
3271 *
3272 * \param layer The layer to hide
3273 */
3274WL_EXPORT void
3275weston_layer_unset_position(struct weston_layer *layer)
3276{
3277 wl_list_remove(&layer->link);
3278 wl_list_init(&layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003279}
3280
3281WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003282weston_layer_set_mask(struct weston_layer *layer,
3283 int x, int y, int width, int height)
3284{
3285 struct weston_view *view;
3286
3287 layer->mask.x1 = x;
3288 layer->mask.x2 = x + width;
3289 layer->mask.y1 = y;
3290 layer->mask.y2 = y + height;
3291
3292 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
3293 weston_view_geometry_dirty(view);
3294 }
3295}
3296
3297WL_EXPORT void
3298weston_layer_set_mask_infinite(struct weston_layer *layer)
3299{
Adam Jackson3c3f3b12019-10-16 16:02:59 -04003300 struct weston_view *view;
3301
3302 layer->mask.x1 = INT32_MIN;
3303 layer->mask.x2 = INT32_MAX;
3304 layer->mask.y1 = INT32_MIN;
3305 layer->mask.y2 = INT32_MAX;
3306
3307 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
3308 weston_view_geometry_dirty(view);
3309 }
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003310}
3311
Daniel Stone3b775632018-07-20 08:38:25 +01003312WL_EXPORT bool
3313weston_layer_mask_is_infinite(struct weston_layer *layer)
3314{
3315 return layer->mask.x1 == INT32_MIN &&
3316 layer->mask.y1 == INT32_MIN &&
Adam Jackson3c3f3b12019-10-16 16:02:59 -04003317 layer->mask.x2 == INT32_MAX &&
3318 layer->mask.y2 == INT32_MAX;
Daniel Stone3b775632018-07-20 08:38:25 +01003319}
3320
Marius Vlad55d87362019-06-11 01:15:35 +03003321/**
3322 * \ingroup output
3323 */
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003324WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003325weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003326{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003327 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04003328 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01003329
Bryce Harrington08976ac2016-08-30 12:05:16 -07003330 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
3331 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003332 return;
3333
Pekka Paalanenb5026542014-11-12 15:09:24 +02003334 if (!output->repaint_needed)
Marius Vlad3203ff62019-09-05 14:56:12 +03003335 TL_POINT(compositor, "core_repaint_req", TLP_OUTPUT(output), TLP_END);
Pekka Paalanenb5026542014-11-12 15:09:24 +02003336
Kristian Høgsbergef044142011-06-21 15:02:12 -04003337 loop = wl_display_get_event_loop(compositor->wl_display);
Daniel Stone09a97e22017-03-01 11:34:06 +00003338 output->repaint_needed = true;
Daniel Stone05df8c12017-03-03 16:59:42 +00003339
3340 /* If we already have a repaint scheduled for our idle handler,
3341 * no need to set it again. If the repaint has been called but
3342 * not finished, then weston_output_finish_frame() will notice
3343 * that a repaint is needed and schedule one. */
3344 if (output->repaint_status != REPAINT_NOT_SCHEDULED)
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003345 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01003346
Daniel Stone05df8c12017-03-03 16:59:42 +00003347 output->repaint_status = REPAINT_BEGIN_FROM_IDLE;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03003348 assert(!output->idle_repaint_source);
3349 output->idle_repaint_source = wl_event_loop_add_idle(loop, idle_repaint,
3350 output);
Marius Vlad3203ff62019-09-05 14:56:12 +03003351 TL_POINT(compositor, "core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003352}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05003353
Marius Vlad9fdda7f2019-06-11 16:08:55 +03003354/** weston_compositor_schedule_repaint
3355 * \ingroup compositor
3356 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003357WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003358weston_compositor_schedule_repaint(struct weston_compositor *compositor)
3359{
3360 struct weston_output *output;
3361
3362 wl_list_for_each(output, &compositor->output_list, link)
3363 weston_output_schedule_repaint(output);
3364}
3365
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003366static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003367surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003368{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003369 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003370}
3371
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003372static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003373surface_attach(struct wl_client *client,
3374 struct wl_resource *resource,
3375 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
3376{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003377 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05003378 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003379
Kristian Høgsbergab19f932013-08-20 11:30:54 -07003380 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05003381 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07003382 if (buffer == NULL) {
3383 wl_client_post_no_memory(client);
3384 return;
3385 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07003386 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04003387
Pekka Paalanende685b82012-12-04 15:58:12 +02003388 /* Attach, attach, without commit in between does not send
3389 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003390 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003391
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003392 surface->pending.sx = sx;
3393 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01003394 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003395}
3396
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003397static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003398surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003399 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003400 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05003401{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003402 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003403
Derek Foreman57e92ed2015-11-17 14:11:35 -06003404 if (width <= 0 || height <= 0)
3405 return;
3406
Derek Foreman152254b2015-11-26 14:17:48 -06003407 pixman_region32_union_rect(&surface->pending.damage_surface,
3408 &surface->pending.damage_surface,
3409 x, y, width, height);
3410}
3411
3412static void
3413surface_damage_buffer(struct wl_client *client,
3414 struct wl_resource *resource,
3415 int32_t x, int32_t y, int32_t width, int32_t height)
3416{
3417 struct weston_surface *surface = wl_resource_get_user_data(resource);
3418
3419 if (width <= 0 || height <= 0)
3420 return;
3421
3422 pixman_region32_union_rect(&surface->pending.damage_buffer,
3423 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04003424 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003425}
3426
Kristian Høgsberg33418202011-08-16 23:01:28 -04003427static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003428destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04003429{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05003430 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003431
3432 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02003433 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003434}
3435
3436static void
3437surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003438 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04003439{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003440 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003441 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003442
3443 cb = malloc(sizeof *cb);
3444 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003445 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003446 return;
3447 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03003448
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003449 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
3450 callback);
3451 if (cb->resource == NULL) {
3452 free(cb);
3453 wl_resource_post_no_memory(resource);
3454 return;
3455 }
3456
Jason Ekstranda85118c2013-06-27 20:17:02 -05003457 wl_resource_set_implementation(cb->resource, NULL, cb,
3458 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003459
Pekka Paalanenbc106382012-10-10 12:49:31 +03003460 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003461}
3462
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003463static void
3464surface_set_opaque_region(struct wl_client *client,
3465 struct wl_resource *resource,
3466 struct wl_resource *region_resource)
3467{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003468 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003469 struct weston_region *region;
3470
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003471 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003472 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003473 pixman_region32_copy(&surface->pending.opaque,
3474 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003475 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07003476 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003477 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003478}
3479
3480static void
3481surface_set_input_region(struct wl_client *client,
3482 struct wl_resource *resource,
3483 struct wl_resource *region_resource)
3484{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003485 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003486 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003487
3488 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003489 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003490 pixman_region32_copy(&surface->pending.input,
3491 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003492 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003493 pixman_region32_fini(&surface->pending.input);
3494 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003495 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003496}
3497
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003498/* Cause damage to this sub-surface and all its children.
3499 *
3500 * This is useful when there are state changes that need an implicit
3501 * damage, e.g. a z-order change.
3502 */
3503static void
3504weston_surface_damage_subsurfaces(struct weston_subsurface *sub)
3505{
3506 struct weston_subsurface *child;
3507
3508 weston_surface_damage(sub->surface);
3509 sub->reordered = false;
3510
3511 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link)
3512 if (child != sub)
3513 weston_surface_damage_subsurfaces(child);
3514}
3515
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003516static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003517weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003518{
Pekka Paalanene67858b2013-04-25 13:57:42 +03003519 struct weston_subsurface *sub;
3520
3521 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
3522 parent_link_pending) {
3523 wl_list_remove(&sub->parent_link);
3524 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003525
3526 if (sub->reordered)
3527 weston_surface_damage_subsurfaces(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003528 }
3529}
3530
3531static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03003532weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05003533 struct weston_matrix *matrix)
3534{
Pekka Paalanen04baea52016-04-26 15:50:58 +03003535 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05003536 double src_width, src_height, dest_width, dest_height;
3537
3538 weston_matrix_init(matrix);
3539
3540 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
3541 src_width = surface->width_from_buffer;
3542 src_height = surface->height_from_buffer;
3543 } else {
3544 src_width = wl_fixed_to_double(vp->buffer.src_width);
3545 src_height = wl_fixed_to_double(vp->buffer.src_height);
3546 }
3547
3548 if (vp->surface.width == -1) {
3549 dest_width = src_width;
3550 dest_height = src_height;
3551 } else {
3552 dest_width = vp->surface.width;
3553 dest_height = vp->surface.height;
3554 }
3555
3556 if (src_width != dest_width || src_height != dest_height)
3557 weston_matrix_scale(matrix,
3558 src_width / dest_width,
3559 src_height / dest_height, 1);
3560
3561 if (vp->buffer.src_width != wl_fixed_from_int(-1))
3562 weston_matrix_translate(matrix,
3563 wl_fixed_to_double(vp->buffer.src_x),
3564 wl_fixed_to_double(vp->buffer.src_y),
3565 0);
3566
3567 switch (vp->buffer.transform) {
3568 case WL_OUTPUT_TRANSFORM_FLIPPED:
3569 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3570 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3571 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3572 weston_matrix_scale(matrix, -1, 1, 1);
3573 weston_matrix_translate(matrix,
3574 surface->width_from_buffer, 0, 0);
3575 break;
3576 }
3577
3578 switch (vp->buffer.transform) {
3579 default:
3580 case WL_OUTPUT_TRANSFORM_NORMAL:
3581 case WL_OUTPUT_TRANSFORM_FLIPPED:
3582 break;
3583 case WL_OUTPUT_TRANSFORM_90:
3584 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +02003585 weston_matrix_rotate_xy(matrix, 0, -1);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003586 weston_matrix_translate(matrix,
Pekka Paalanen8060d822020-02-06 15:27:54 +02003587 0, surface->width_from_buffer, 0);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003588 break;
3589 case WL_OUTPUT_TRANSFORM_180:
3590 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3591 weston_matrix_rotate_xy(matrix, -1, 0);
3592 weston_matrix_translate(matrix,
3593 surface->width_from_buffer,
3594 surface->height_from_buffer, 0);
3595 break;
3596 case WL_OUTPUT_TRANSFORM_270:
3597 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +02003598 weston_matrix_rotate_xy(matrix, 0, 1);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003599 weston_matrix_translate(matrix,
Pekka Paalanen8060d822020-02-06 15:27:54 +02003600 surface->height_from_buffer, 0, 0);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003601 break;
3602 }
3603
3604 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
3605}
3606
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003607/**
3608 * Compute a + b > c while being safe to overflows.
3609 */
3610static bool
3611fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
3612{
3613 return (int64_t)a + (int64_t)b > (int64_t)c;
3614}
3615
3616static bool
3617weston_surface_is_pending_viewport_source_valid(
3618 const struct weston_surface *surface)
3619{
3620 const struct weston_surface_state *pend = &surface->pending;
3621 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
3622 int width_from_buffer = 0;
3623 int height_from_buffer = 0;
3624 wl_fixed_t w;
3625 wl_fixed_t h;
3626
3627 /* If viewport source rect is not set, it is always ok. */
3628 if (vp->buffer.src_width == wl_fixed_from_int(-1))
3629 return true;
3630
3631 if (pend->newly_attached) {
3632 if (pend->buffer) {
3633 convert_size_by_transform_scale(&width_from_buffer,
3634 &height_from_buffer,
3635 pend->buffer->width,
3636 pend->buffer->height,
3637 vp->buffer.transform,
3638 vp->buffer.scale);
3639 } else {
3640 /* No buffer: viewport is irrelevant. */
3641 return true;
3642 }
3643 } else {
3644 width_from_buffer = surface->width_from_buffer;
3645 height_from_buffer = surface->height_from_buffer;
3646 }
3647
3648 assert((width_from_buffer == 0) == (height_from_buffer == 0));
3649 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
3650
3651 /* No buffer: viewport is irrelevant. */
3652 if (width_from_buffer == 0 || height_from_buffer == 0)
3653 return true;
3654
3655 /* overflow checks for wl_fixed_from_int() */
3656 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
3657 return false;
3658 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
3659 return false;
3660
3661 w = wl_fixed_from_int(width_from_buffer);
3662 h = wl_fixed_from_int(height_from_buffer);
3663
3664 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
3665 return false;
3666 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
3667 return false;
3668
3669 return true;
3670}
3671
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003672static bool
3673fixed_is_integer(wl_fixed_t v)
3674{
3675 return (v & 0xff) == 0;
3676}
3677
3678static bool
3679weston_surface_is_pending_viewport_dst_size_int(
3680 const struct weston_surface *surface)
3681{
3682 const struct weston_buffer_viewport *vp =
3683 &surface->pending.buffer_viewport;
3684
3685 if (vp->surface.width != -1) {
3686 assert(vp->surface.width > 0 && vp->surface.height > 0);
3687 return true;
3688 }
3689
3690 return fixed_is_integer(vp->buffer.src_width) &&
3691 fixed_is_integer(vp->buffer.src_height);
3692}
3693
Derek Foreman152254b2015-11-26 14:17:48 -06003694/* Translate pending damage in buffer co-ordinates to surface
3695 * co-ordinates and union it with a pixman_region32_t.
3696 * This should only be called after the buffer is attached.
3697 */
3698static void
3699apply_damage_buffer(pixman_region32_t *dest,
3700 struct weston_surface *surface,
3701 struct weston_surface_state *state)
3702{
3703 struct weston_buffer *buffer = surface->buffer_ref.buffer;
3704
3705 /* wl_surface.damage_buffer needs to be clipped to the buffer,
3706 * translated into surface co-ordinates and unioned with
3707 * any other surface damage.
3708 * None of this makes sense if there is no buffer though.
3709 */
3710 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
3711 pixman_region32_t buffer_damage;
3712
3713 pixman_region32_intersect_rect(&state->damage_buffer,
3714 &state->damage_buffer,
3715 0, 0, buffer->width,
3716 buffer->height);
3717 pixman_region32_init(&buffer_damage);
3718 weston_matrix_transform_region(&buffer_damage,
3719 &surface->buffer_to_surface_matrix,
3720 &state->damage_buffer);
3721 pixman_region32_union(dest, dest, &buffer_damage);
3722 pixman_region32_fini(&buffer_damage);
3723 }
3724 /* We should clear this on commit even if there was no buffer */
3725 pixman_region32_clear(&state->damage_buffer);
3726}
3727
Jason Ekstrand1e059042014-10-16 10:55:19 -05003728static void
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303729weston_surface_set_desired_protection(struct weston_surface *surface,
3730 enum weston_hdcp_protection protection)
3731{
3732 if (surface->desired_protection == protection)
3733 return;
3734 surface->desired_protection = protection;
3735 weston_surface_damage(surface);
3736}
3737
3738static void
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303739weston_surface_set_protection_mode(struct weston_surface *surface,
3740 enum weston_surface_protection_mode p_mode)
3741{
3742 struct content_protection *cp = surface->compositor->content_protection;
3743 struct protected_surface *psurface;
3744
3745 surface->protection_mode = p_mode;
3746 wl_list_for_each(psurface, &cp->protected_list, link) {
3747 if (!psurface || psurface->surface != surface)
3748 continue;
3749 weston_protected_surface_send_event(psurface,
3750 surface->current_protection);
3751 }
3752}
3753
3754static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05003755weston_surface_commit_state(struct weston_surface *surface,
3756 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003757{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003758 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003759 pixman_region32_t opaque;
3760
Alexander Larsson4ea95522013-05-22 14:41:37 +02003761 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02003762 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03003763 /* wp_viewport.set_source */
3764 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003765 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003766
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003767 /* wl_surface.attach */
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003768 if (state->newly_attached) {
3769 /* zwp_surface_synchronization_v1.set_acquire_fence */
3770 fd_move(&surface->acquire_fence_fd,
3771 &state->acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003772 /* zwp_surface_synchronization_v1.get_release */
3773 weston_buffer_release_move(&surface->buffer_release_ref,
3774 &state->buffer_release_ref);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003775 weston_surface_attach(surface, state->buffer);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003776 }
Jason Ekstrand7b982072014-05-20 14:33:03 -05003777 weston_surface_state_set_buffer(state, NULL);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003778 assert(state->acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003779 assert(state->buffer_release_ref.buffer_release == NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01003780
Jason Ekstrand1e059042014-10-16 10:55:19 -05003781 weston_surface_build_buffer_matrix(surface,
3782 &surface->surface_to_buffer_matrix);
3783 weston_matrix_invert(&surface->buffer_to_surface_matrix,
3784 &surface->surface_to_buffer_matrix);
3785
Jason Ekstrand7b982072014-05-20 14:33:03 -05003786 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003787 weston_surface_update_size(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003788 if (surface->committed)
3789 surface->committed(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003790 }
Giulio Camuffo184df502013-02-21 11:29:21 +01003791
Jason Ekstrand7b982072014-05-20 14:33:03 -05003792 state->sx = 0;
3793 state->sy = 0;
3794 state->newly_attached = 0;
3795 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03003796
Derek Foreman152254b2015-11-26 14:17:48 -06003797 /* wl_surface.damage and wl_surface.damage_buffer */
Marius Vlad2a1b7862019-09-05 13:12:18 +03003798 if (pixman_region32_not_empty(&state->damage_surface) ||
3799 pixman_region32_not_empty(&state->damage_buffer))
Marius Vlad3203ff62019-09-05 14:56:12 +03003800 TL_POINT(surface->compositor, "core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06003801
Pekka Paalanen8e159182012-10-10 12:49:25 +03003802 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06003803 &state->damage_surface);
3804
3805 apply_damage_buffer(&surface->damage, surface, state);
3806
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05003807 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07003808 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06003809 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003810
3811 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003812 pixman_region32_init(&opaque);
3813 pixman_region32_intersect_rect(&opaque, &state->opaque,
3814 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003815
3816 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
3817 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003818 wl_list_for_each(view, &surface->views, surface_link)
3819 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003820 }
3821
3822 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003823
3824 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003825 pixman_region32_intersect_rect(&surface->input, &state->input,
3826 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003827
Pekka Paalanenbc106382012-10-10 12:49:31 +03003828 /* wl_surface.frame */
3829 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05003830 &state->frame_callback_list);
3831 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003832
3833 /* XXX:
3834 * What should happen with a feedback request, if there
3835 * is no wl_buffer attached for this commit?
3836 */
3837
3838 /* presentation.feedback */
3839 wl_list_insert_list(&surface->feedback_list,
3840 &state->feedback_list);
3841 wl_list_init(&state->feedback_list);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003842
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303843 /* weston_protected_surface.enforced/relaxed */
3844 if (surface->protection_mode != state->protection_mode)
3845 weston_surface_set_protection_mode(surface,
3846 state->protection_mode);
3847
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303848 /* weston_protected_surface.set_type */
3849 weston_surface_set_desired_protection(surface, state->desired_protection);
3850
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003851 wl_signal_emit(&surface->commit_signal, surface);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003852}
3853
3854static void
3855weston_surface_commit(struct weston_surface *surface)
3856{
3857 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003858
Pekka Paalanene67858b2013-04-25 13:57:42 +03003859 weston_surface_commit_subsurface_order(surface);
3860
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003861 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003862}
3863
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003864static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003865weston_subsurface_commit(struct weston_subsurface *sub);
3866
3867static void
3868weston_subsurface_parent_commit(struct weston_subsurface *sub,
3869 int parent_is_synchronized);
3870
3871static void
3872surface_commit(struct wl_client *client, struct wl_resource *resource)
3873{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003874 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003875 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
3876
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003877 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
3878 assert(surface->viewport_resource);
3879
3880 wl_resource_post_error(surface->viewport_resource,
3881 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
3882 "wl_surface@%d has viewport source outside buffer",
3883 wl_resource_get_id(resource));
3884 return;
3885 }
3886
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003887 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
3888 assert(surface->viewport_resource);
3889
3890 wl_resource_post_error(surface->viewport_resource,
3891 WP_VIEWPORT_ERROR_BAD_SIZE,
3892 "wl_surface@%d viewport dst size not integer",
3893 wl_resource_get_id(resource));
3894 return;
3895 }
3896
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003897 if (surface->pending.acquire_fence_fd >= 0) {
3898 assert(surface->synchronization_resource);
3899
3900 if (!surface->pending.buffer) {
3901 fd_clear(&surface->pending.acquire_fence_fd);
3902 wl_resource_post_error(surface->synchronization_resource,
3903 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3904 "wl_surface@%"PRIu32" no buffer for synchronization",
3905 wl_resource_get_id(resource));
3906 return;
3907 }
3908
3909 /* We support fences for both wp_linux_dmabuf and opaque EGL
3910 * buffers, as mandated by minor version 2 of the
3911 * zwp_linux_explicit_synchronization_v1 protocol. Since
3912 * renderers that support fences currently only support these
3913 * two buffer types plus SHM buffers, we can just check for the
3914 * SHM buffer case here.
3915 */
3916 if (wl_shm_buffer_get(surface->pending.buffer->resource)) {
3917 fd_clear(&surface->pending.acquire_fence_fd);
3918 wl_resource_post_error(surface->synchronization_resource,
3919 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_UNSUPPORTED_BUFFER,
3920 "wl_surface@%"PRIu32" unsupported buffer for synchronization",
3921 wl_resource_get_id(resource));
3922 return;
3923 }
3924 }
3925
Alexandros Frantzis67629672018-10-19 12:14:11 +03003926 if (surface->pending.buffer_release_ref.buffer_release &&
3927 !surface->pending.buffer) {
3928 assert(surface->synchronization_resource);
3929
3930 wl_resource_post_error(surface->synchronization_resource,
3931 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3932 "wl_surface@%"PRIu32" no buffer for synchronization",
3933 wl_resource_get_id(resource));
3934 return;
3935 }
3936
Pekka Paalanene67858b2013-04-25 13:57:42 +03003937 if (sub) {
3938 weston_subsurface_commit(sub);
3939 return;
3940 }
3941
3942 weston_surface_commit(surface);
3943
3944 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
3945 if (sub->surface != surface)
3946 weston_subsurface_parent_commit(sub, 0);
3947 }
3948}
3949
3950static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003951surface_set_buffer_transform(struct wl_client *client,
3952 struct wl_resource *resource, int transform)
3953{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003954 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003955
Jonny Lamba55f1392014-05-30 12:07:15 +02003956 /* if wl_output.transform grows more members this will need to be updated. */
3957 if (transform < 0 ||
3958 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
3959 wl_resource_post_error(resource,
3960 WL_SURFACE_ERROR_INVALID_TRANSFORM,
3961 "buffer transform must be a valid transform "
3962 "('%d' specified)", transform);
3963 return;
3964 }
3965
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003966 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003967 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003968}
3969
Alexander Larsson4ea95522013-05-22 14:41:37 +02003970static void
3971surface_set_buffer_scale(struct wl_client *client,
3972 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003973 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003974{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003975 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003976
Jonny Lamba55f1392014-05-30 12:07:15 +02003977 if (scale < 1) {
3978 wl_resource_post_error(resource,
3979 WL_SURFACE_ERROR_INVALID_SCALE,
3980 "buffer scale must be at least one "
3981 "('%d' specified)", scale);
3982 return;
3983 }
3984
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003985 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003986 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003987}
3988
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003989static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003990 surface_destroy,
3991 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04003992 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003993 surface_frame,
3994 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003995 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003996 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02003997 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06003998 surface_set_buffer_scale,
3999 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004000};
4001
4002static void
4003compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04004004 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004005{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04004006 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004007 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004008
Kristian Høgsberg18c93002012-01-27 11:58:31 -05004009 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04004010 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04004011 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004012 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04004013 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004014
Jason Ekstranda85118c2013-06-27 20:17:02 -05004015 surface->resource =
4016 wl_resource_create(client, &wl_surface_interface,
4017 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004018 if (surface->resource == NULL) {
4019 weston_surface_destroy(surface);
4020 wl_resource_post_no_memory(resource);
4021 return;
4022 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05004023 wl_resource_set_implementation(surface->resource, &surface_interface,
4024 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07004025
4026 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004027}
4028
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004029static void
4030destroy_region(struct wl_resource *resource)
4031{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05004032 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004033
4034 pixman_region32_fini(&region->region);
4035 free(region);
4036}
4037
4038static void
4039region_destroy(struct wl_client *client, struct wl_resource *resource)
4040{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004041 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004042}
4043
4044static void
4045region_add(struct wl_client *client, struct wl_resource *resource,
4046 int32_t x, int32_t y, int32_t width, int32_t height)
4047{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05004048 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004049
4050 pixman_region32_union_rect(&region->region, &region->region,
4051 x, y, width, height);
4052}
4053
4054static void
4055region_subtract(struct wl_client *client, struct wl_resource *resource,
4056 int32_t x, int32_t y, int32_t width, int32_t height)
4057{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05004058 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004059 pixman_region32_t rect;
4060
4061 pixman_region32_init_rect(&rect, x, y, width, height);
4062 pixman_region32_subtract(&region->region, &region->region, &rect);
4063 pixman_region32_fini(&rect);
4064}
4065
4066static const struct wl_region_interface region_interface = {
4067 region_destroy,
4068 region_add,
4069 region_subtract
4070};
4071
4072static void
4073compositor_create_region(struct wl_client *client,
4074 struct wl_resource *resource, uint32_t id)
4075{
4076 struct weston_region *region;
4077
4078 region = malloc(sizeof *region);
4079 if (region == NULL) {
4080 wl_resource_post_no_memory(resource);
4081 return;
4082 }
4083
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004084 pixman_region32_init(&region->region);
4085
Jason Ekstranda85118c2013-06-27 20:17:02 -05004086 region->resource =
4087 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004088 if (region->resource == NULL) {
4089 free(region);
4090 wl_resource_post_no_memory(resource);
4091 return;
4092 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05004093 wl_resource_set_implementation(region->resource, &region_interface,
4094 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004095}
4096
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04004097static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004098 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004099 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004100};
4101
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004102static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03004103weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
4104{
4105 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004106
Jason Ekstrand7b982072014-05-20 14:33:03 -05004107 weston_surface_commit_state(surface, &sub->cached);
4108 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004109
4110 weston_surface_commit_subsurface_order(surface);
4111
4112 weston_surface_schedule_repaint(surface);
4113
Jason Ekstrand7b982072014-05-20 14:33:03 -05004114 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004115}
4116
4117static void
4118weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
4119{
4120 struct weston_surface *surface = sub->surface;
4121
4122 /*
4123 * If this commit would cause the surface to move by the
4124 * attach(dx, dy) parameters, the old damage region must be
4125 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05004126 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03004127 */
Derek Foreman152254b2015-11-26 14:17:48 -06004128 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004129 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06004130 pixman_region32_union(&sub->cached.damage_surface,
4131 &sub->cached.damage_surface,
4132 &surface->pending.damage_surface);
4133 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004134
4135 if (surface->pending.newly_attached) {
4136 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05004137 weston_surface_state_set_buffer(&sub->cached,
4138 surface->pending.buffer);
4139 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004140 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04004141 weston_presentation_feedback_discard_list(
4142 &sub->cached.feedback_list);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03004143 /* zwp_surface_synchronization_v1.set_acquire_fence */
4144 fd_move(&sub->cached.acquire_fence_fd,
4145 &surface->pending.acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03004146 /* zwp_surface_synchronization_v1.get_release */
4147 weston_buffer_release_move(&sub->cached.buffer_release_ref,
4148 &surface->pending.buffer_release_ref);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004149 }
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05304150 sub->cached.desired_protection = surface->pending.desired_protection;
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05304151 sub->cached.protection_mode = surface->pending.protection_mode;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03004152 assert(surface->pending.acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03004153 assert(surface->pending.buffer_release_ref.buffer_release == NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004154 sub->cached.sx += surface->pending.sx;
4155 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02004156
Derek Foreman152254b2015-11-26 14:17:48 -06004157 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
4158
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004159 sub->cached.buffer_viewport.changed |=
4160 surface->pending.buffer_viewport.changed;
4161 sub->cached.buffer_viewport.buffer =
4162 surface->pending.buffer_viewport.buffer;
4163 sub->cached.buffer_viewport.surface =
4164 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004165
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004166 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004167
4168 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
4169
4170 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
4171
4172 wl_list_insert_list(&sub->cached.frame_callback_list,
4173 &surface->pending.frame_callback_list);
4174 wl_list_init(&surface->pending.frame_callback_list);
4175
Pekka Paalanen133e4392014-09-23 22:08:46 -04004176 wl_list_insert_list(&sub->cached.feedback_list,
4177 &surface->pending.feedback_list);
4178 wl_list_init(&surface->pending.feedback_list);
4179
Jason Ekstrand7b982072014-05-20 14:33:03 -05004180 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004181}
4182
Derek Foreman280e7dd2014-10-03 13:13:42 -05004183static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03004184weston_subsurface_is_synchronized(struct weston_subsurface *sub)
4185{
4186 while (sub) {
4187 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05004188 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004189
4190 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05004191 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004192
4193 sub = weston_surface_to_subsurface(sub->parent);
4194 }
4195
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01004196 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004197}
4198
4199static void
4200weston_subsurface_commit(struct weston_subsurface *sub)
4201{
4202 struct weston_surface *surface = sub->surface;
4203 struct weston_subsurface *tmp;
4204
4205 /* Recursive check for effectively synchronized. */
4206 if (weston_subsurface_is_synchronized(sub)) {
4207 weston_subsurface_commit_to_cache(sub);
4208 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05004209 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03004210 /* flush accumulated state from cache */
4211 weston_subsurface_commit_to_cache(sub);
4212 weston_subsurface_commit_from_cache(sub);
4213 } else {
4214 weston_surface_commit(surface);
4215 }
4216
4217 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
4218 if (tmp->surface != surface)
4219 weston_subsurface_parent_commit(tmp, 0);
4220 }
4221 }
4222}
4223
4224static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004225weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004226{
4227 struct weston_surface *surface = sub->surface;
4228 struct weston_subsurface *tmp;
4229
Pekka Paalanene67858b2013-04-25 13:57:42 +03004230 /* From now on, commit_from_cache the whole sub-tree, regardless of
4231 * the synchronized mode of each child. This sub-surface or some
4232 * of its ancestors were synchronized, so we are synchronized
4233 * all the way down.
4234 */
4235
Jason Ekstrand7b982072014-05-20 14:33:03 -05004236 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004237 weston_subsurface_commit_from_cache(sub);
4238
4239 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
4240 if (tmp->surface != surface)
4241 weston_subsurface_parent_commit(tmp, 1);
4242 }
4243}
4244
4245static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004246weston_subsurface_parent_commit(struct weston_subsurface *sub,
4247 int parent_is_synchronized)
4248{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004249 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004250 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004251 wl_list_for_each(view, &sub->surface->views, surface_link)
4252 weston_view_set_position(view,
4253 sub->position.x,
4254 sub->position.y);
4255
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004256 sub->position.set = 0;
4257 }
4258
4259 if (parent_is_synchronized || sub->synchronized)
4260 weston_subsurface_synchronized_commit(sub);
4261}
4262
Pekka Paalanen8274d902014-08-06 19:36:51 +03004263static int
4264subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
4265{
4266 return snprintf(buf, len, "sub-surface");
4267}
4268
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004269static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004270subsurface_committed(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004271{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004272 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004273
Jason Ekstranda7af7042013-10-12 22:38:11 -05004274 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004275 weston_view_set_position(view,
4276 view->geometry.x + dx,
4277 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004278
4279 /* No need to check parent mappedness, because if parent is not
4280 * mapped, parent is not in a visible layer, so this sub-surface
4281 * will not be drawn either.
4282 */
Armin Krezovićf8486c32016-06-30 06:04:28 +02004283
Pekka Paalanene67858b2013-04-25 13:57:42 +03004284 if (!weston_surface_is_mapped(surface)) {
Armin Krezovićf8486c32016-06-30 06:04:28 +02004285 surface->is_mapped = true;
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03004286
Derek Foreman4b1a0a12014-09-10 15:37:33 -05004287 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03004288 * because that would call it also for the parent surface,
4289 * which might not be mapped yet. That would lead to
4290 * inconsistent state, where the window could never be
4291 * mapped.
4292 *
Armin Krezovićf8486c32016-06-30 06:04:28 +02004293 * Instead just force the is_mapped flag on, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03004294 * weston_surface_is_mapped() return true, so that when the
4295 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03004296 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03004297 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004298 }
4299}
4300
4301static struct weston_subsurface *
4302weston_surface_to_subsurface(struct weston_surface *surface)
4303{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004304 if (surface->committed == subsurface_committed)
4305 return surface->committed_private;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004306
4307 return NULL;
4308}
4309
Pekka Paalanen01388e22013-04-25 13:57:44 +03004310WL_EXPORT struct weston_surface *
4311weston_surface_get_main_surface(struct weston_surface *surface)
4312{
4313 struct weston_subsurface *sub;
4314
4315 while (surface && (sub = weston_surface_to_subsurface(surface)))
4316 surface = sub->parent;
4317
4318 return surface;
4319}
4320
Pekka Paalanen50b67472014-10-01 15:02:41 +03004321WL_EXPORT int
4322weston_surface_set_role(struct weston_surface *surface,
4323 const char *role_name,
4324 struct wl_resource *error_resource,
4325 uint32_t error_code)
4326{
4327 assert(role_name);
4328
4329 if (surface->role_name == NULL ||
4330 surface->role_name == role_name ||
4331 strcmp(surface->role_name, role_name) == 0) {
4332 surface->role_name = role_name;
4333
4334 return 0;
4335 }
4336
4337 wl_resource_post_error(error_resource, error_code,
4338 "Cannot assign role %s to wl_surface@%d,"
4339 " already has role %s\n",
4340 role_name,
4341 wl_resource_get_id(surface->resource),
4342 surface->role_name);
4343 return -1;
4344}
4345
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02004346WL_EXPORT const char *
4347weston_surface_get_role(struct weston_surface *surface)
4348{
4349 return surface->role_name;
4350}
4351
Pekka Paalanen8274d902014-08-06 19:36:51 +03004352WL_EXPORT void
4353weston_surface_set_label_func(struct weston_surface *surface,
4354 int (*desc)(struct weston_surface *,
4355 char *, size_t))
4356{
4357 surface->get_label = desc;
Marius Vlad5de92972019-10-15 13:25:41 +03004358 weston_timeline_refresh_subscription_objects(surface->compositor,
4359 surface);
Pekka Paalanen8274d902014-08-06 19:36:51 +03004360}
4361
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004362/** Get the size of surface contents
4363 *
4364 * \param surface The surface to query.
4365 * \param width Returns the width of raw contents.
4366 * \param height Returns the height of raw contents.
4367 *
4368 * Retrieves the raw surface content size in pixels for the given surface.
4369 * This is the whole content size in buffer pixels. If the surface
4370 * has no content or the renderer does not implement this feature,
4371 * zeroes are returned.
4372 *
4373 * This function is used to determine the buffer size needed for
4374 * a weston_surface_copy_content() call.
4375 */
4376WL_EXPORT void
4377weston_surface_get_content_size(struct weston_surface *surface,
4378 int *width, int *height)
4379{
4380 struct weston_renderer *rer = surface->compositor->renderer;
4381
4382 if (!rer->surface_get_content_size) {
4383 *width = 0;
4384 *height = 0;
4385 return;
4386 }
4387
4388 rer->surface_get_content_size(surface, width, height);
4389}
4390
Quentin Glidic248dd102016-08-12 10:41:34 +02004391/** Get the bounding box of a surface and its subsurfaces
4392 *
4393 * \param surface The surface to query.
4394 * \return The bounding box relative to the surface origin.
4395 *
4396 */
4397WL_EXPORT struct weston_geometry
4398weston_surface_get_bounding_box(struct weston_surface *surface)
4399{
4400 pixman_region32_t region;
4401 pixman_box32_t *box;
4402 struct weston_subsurface *subsurface;
4403
4404 pixman_region32_init_rect(&region,
4405 0, 0,
4406 surface->width, surface->height);
4407
4408 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link)
4409 pixman_region32_union_rect(&region, &region,
4410 subsurface->position.x,
4411 subsurface->position.y,
4412 subsurface->surface->width,
4413 subsurface->surface->height);
4414
4415 box = pixman_region32_extents(&region);
4416 struct weston_geometry geometry = {
4417 .x = box->x1,
4418 .y = box->y1,
4419 .width = box->x2 - box->x1,
4420 .height = box->y2 - box->y1,
4421 };
4422
4423 pixman_region32_fini(&region);
4424
4425 return geometry;
4426}
4427
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004428/** Copy surface contents to system memory.
4429 *
4430 * \param surface The surface to copy from.
4431 * \param target Pointer to the target memory buffer.
4432 * \param size Size of the target buffer in bytes.
4433 * \param src_x X location on contents to copy from.
4434 * \param src_y Y location on contents to copy from.
4435 * \param width Width in pixels of the area to copy.
4436 * \param height Height in pixels of the area to copy.
4437 * \return 0 for success, -1 for failure.
4438 *
4439 * Surface contents are maintained by the renderer. They can be in a
4440 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
4441 * else.
4442 *
4443 * Surface contents are copied into memory pointed to by target,
4444 * which has size bytes of space available. The target memory
4445 * may be larger than needed, but being smaller returns an error.
4446 * The extra bytes in target may or may not be written; their content is
4447 * unspecified. Size must be large enough to hold the image.
4448 *
4449 * The image in the target memory will be arranged in rows from
4450 * top to bottom, and pixels on a row from left to right. The pixel
4451 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
4452 * width * 4.
4453 *
4454 * Parameters src_x and src_y define the upper-left corner in buffer
4455 * coordinates (pixels) to copy from. Parameters width and height
4456 * define the size of the area to copy in pixels.
4457 *
4458 * The rectangle defined by src_x, src_y, width, height must fit in
4459 * the surface contents. Otherwise an error is returned.
4460 *
Changwoo Chof97d2502017-08-05 00:30:47 +09004461 * Use weston_surface_get_content_size to determine the content size; the
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004462 * needed target buffer size and rectangle limits.
4463 *
4464 * CURRENT IMPLEMENTATION RESTRICTIONS:
4465 * - the machine must be little-endian due to Pixman formats.
4466 *
4467 * NOTE: Pixman formats are premultiplied.
4468 */
4469WL_EXPORT int
4470weston_surface_copy_content(struct weston_surface *surface,
4471 void *target, size_t size,
4472 int src_x, int src_y,
4473 int width, int height)
4474{
4475 struct weston_renderer *rer = surface->compositor->renderer;
4476 int cw, ch;
4477 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
4478
4479 if (!rer->surface_copy_content)
4480 return -1;
4481
4482 weston_surface_get_content_size(surface, &cw, &ch);
4483
4484 if (src_x < 0 || src_y < 0)
4485 return -1;
4486
4487 if (width <= 0 || height <= 0)
4488 return -1;
4489
4490 if (src_x + width > cw || src_y + height > ch)
4491 return -1;
4492
4493 if (width * bytespp * height > size)
4494 return -1;
4495
4496 return rer->surface_copy_content(surface, target, size,
4497 src_x, src_y, width, height);
4498}
4499
Pekka Paalanene67858b2013-04-25 13:57:42 +03004500static void
4501subsurface_set_position(struct wl_client *client,
4502 struct wl_resource *resource, int32_t x, int32_t y)
4503{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004504 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004505
4506 if (!sub)
4507 return;
4508
4509 sub->position.x = x;
4510 sub->position.y = y;
4511 sub->position.set = 1;
4512}
4513
4514static struct weston_subsurface *
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004515subsurface_find_sibling(struct weston_subsurface *sub,
4516 struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004517{
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004518 struct weston_surface *parent = sub->parent;
4519 struct weston_subsurface *sibling;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004520
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004521 wl_list_for_each(sibling, &parent->subsurface_list, parent_link) {
4522 if (sibling->surface == surface && sibling != sub)
4523 return sibling;
4524 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004525
4526 return NULL;
4527}
4528
4529static struct weston_subsurface *
4530subsurface_sibling_check(struct weston_subsurface *sub,
4531 struct weston_surface *surface,
4532 const char *request)
4533{
4534 struct weston_subsurface *sibling;
4535
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004536 sibling = subsurface_find_sibling(sub, surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004537 if (!sibling) {
4538 wl_resource_post_error(sub->resource,
4539 WL_SUBSURFACE_ERROR_BAD_SURFACE,
4540 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004541 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004542 return NULL;
4543 }
4544
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004545 assert(sibling->parent == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004546
4547 return sibling;
4548}
4549
4550static void
4551subsurface_place_above(struct wl_client *client,
4552 struct wl_resource *resource,
4553 struct wl_resource *sibling_resource)
4554{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004555 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004556 struct weston_surface *surface =
4557 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004558 struct weston_subsurface *sibling;
4559
4560 if (!sub)
4561 return;
4562
4563 sibling = subsurface_sibling_check(sub, surface, "place_above");
4564 if (!sibling)
4565 return;
4566
4567 wl_list_remove(&sub->parent_link_pending);
4568 wl_list_insert(sibling->parent_link_pending.prev,
4569 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004570
4571 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004572}
4573
4574static void
4575subsurface_place_below(struct wl_client *client,
4576 struct wl_resource *resource,
4577 struct wl_resource *sibling_resource)
4578{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004579 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004580 struct weston_surface *surface =
4581 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004582 struct weston_subsurface *sibling;
4583
4584 if (!sub)
4585 return;
4586
4587 sibling = subsurface_sibling_check(sub, surface, "place_below");
4588 if (!sibling)
4589 return;
4590
4591 wl_list_remove(&sub->parent_link_pending);
4592 wl_list_insert(&sibling->parent_link_pending,
4593 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004594
4595 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004596}
4597
4598static void
4599subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
4600{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004601 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004602
4603 if (sub)
4604 sub->synchronized = 1;
4605}
4606
4607static void
4608subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
4609{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004610 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004611
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004612 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03004613 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004614
4615 /* If sub became effectively desynchronized, flush. */
4616 if (!weston_subsurface_is_synchronized(sub))
4617 weston_subsurface_synchronized_commit(sub);
4618 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004619}
4620
4621static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03004622weston_subsurface_unlink_parent(struct weston_subsurface *sub)
4623{
4624 wl_list_remove(&sub->parent_link);
4625 wl_list_remove(&sub->parent_link_pending);
4626 wl_list_remove(&sub->parent_destroy_listener.link);
4627 sub->parent = NULL;
4628}
4629
4630static void
4631weston_subsurface_destroy(struct weston_subsurface *sub);
4632
4633static void
4634subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
4635{
4636 struct weston_subsurface *sub =
4637 container_of(listener, struct weston_subsurface,
4638 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004639 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004640
4641 /* The protocol object (wl_resource) is left inert. */
4642 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004643 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004644
4645 weston_subsurface_destroy(sub);
4646}
4647
4648static void
4649subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
4650{
4651 struct weston_subsurface *sub =
4652 container_of(listener, struct weston_subsurface,
4653 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004654 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004655 assert(sub->surface != sub->parent);
4656
4657 if (weston_surface_is_mapped(sub->surface))
4658 weston_surface_unmap(sub->surface);
4659
4660 weston_subsurface_unlink_parent(sub);
4661}
4662
4663static void
4664subsurface_resource_destroy(struct wl_resource *resource)
4665{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004666 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004667
4668 if (sub)
4669 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004670}
4671
4672static void
4673subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
4674{
4675 wl_resource_destroy(resource);
4676}
4677
4678static void
4679weston_subsurface_link_parent(struct weston_subsurface *sub,
4680 struct weston_surface *parent)
4681{
4682 sub->parent = parent;
4683 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004684 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004685 &sub->parent_destroy_listener);
4686
4687 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4688 wl_list_insert(&parent->subsurface_list_pending,
4689 &sub->parent_link_pending);
4690}
4691
4692static void
4693weston_subsurface_link_surface(struct weston_subsurface *sub,
4694 struct weston_surface *surface)
4695{
4696 sub->surface = surface;
4697 sub->surface_destroy_listener.notify =
4698 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004699 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004700 &sub->surface_destroy_listener);
4701}
4702
4703static void
4704weston_subsurface_destroy(struct weston_subsurface *sub)
4705{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004706 struct weston_view *view, *next;
4707
Pekka Paalanene67858b2013-04-25 13:57:42 +03004708 assert(sub->surface);
4709
4710 if (sub->resource) {
4711 assert(weston_surface_to_subsurface(sub->surface) == sub);
4712 assert(sub->parent_destroy_listener.notify ==
4713 subsurface_handle_parent_destroy);
4714
George Kiagiadakised04d382014-06-13 18:10:26 +02004715 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
4716 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004717 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02004718 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05004719
Pekka Paalanene67858b2013-04-25 13:57:42 +03004720 if (sub->parent)
4721 weston_subsurface_unlink_parent(sub);
4722
Jason Ekstrand7b982072014-05-20 14:33:03 -05004723 weston_surface_state_fini(&sub->cached);
4724 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004725
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004726 sub->surface->committed = NULL;
4727 sub->surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004728 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004729 } else {
4730 /* the dummy weston_subsurface for the parent itself */
4731 assert(sub->parent_destroy_listener.notify == NULL);
4732 wl_list_remove(&sub->parent_link);
4733 wl_list_remove(&sub->parent_link_pending);
4734 }
4735
4736 wl_list_remove(&sub->surface_destroy_listener.link);
4737 free(sub);
4738}
4739
4740static const struct wl_subsurface_interface subsurface_implementation = {
4741 subsurface_destroy,
4742 subsurface_set_position,
4743 subsurface_place_above,
4744 subsurface_place_below,
4745 subsurface_set_sync,
4746 subsurface_set_desync
4747};
4748
4749static struct weston_subsurface *
4750weston_subsurface_create(uint32_t id, struct weston_surface *surface,
4751 struct weston_surface *parent)
4752{
4753 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004754 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004755
Bryce Harringtonde16d892014-11-20 22:21:57 -08004756 sub = zalloc(sizeof *sub);
4757 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004758 return NULL;
4759
Jason Ekstranda7af7042013-10-12 22:38:11 -05004760 wl_list_init(&sub->unused_views);
4761
Jason Ekstranda85118c2013-06-27 20:17:02 -05004762 sub->resource =
4763 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004764 if (!sub->resource) {
4765 free(sub);
4766 return NULL;
4767 }
4768
Jason Ekstranda85118c2013-06-27 20:17:02 -05004769 wl_resource_set_implementation(sub->resource,
4770 &subsurface_implementation,
4771 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004772 weston_subsurface_link_surface(sub, surface);
4773 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05004774 weston_surface_state_init(&sub->cached);
4775 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004776 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004777
4778 return sub;
4779}
4780
4781/* Create a dummy subsurface for having the parent itself in its
4782 * sub-surface lists. Makes stacking order manipulation easy.
4783 */
4784static struct weston_subsurface *
4785weston_subsurface_create_for_parent(struct weston_surface *parent)
4786{
4787 struct weston_subsurface *sub;
4788
Bryce Harringtonde16d892014-11-20 22:21:57 -08004789 sub = zalloc(sizeof *sub);
4790 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004791 return NULL;
4792
4793 weston_subsurface_link_surface(sub, parent);
4794 sub->parent = parent;
4795 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4796 wl_list_insert(&parent->subsurface_list_pending,
4797 &sub->parent_link_pending);
4798
4799 return sub;
4800}
4801
4802static void
4803subcompositor_get_subsurface(struct wl_client *client,
4804 struct wl_resource *resource,
4805 uint32_t id,
4806 struct wl_resource *surface_resource,
4807 struct wl_resource *parent_resource)
4808{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004809 struct weston_surface *surface =
4810 wl_resource_get_user_data(surface_resource);
4811 struct weston_surface *parent =
4812 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004813 struct weston_subsurface *sub;
4814 static const char where[] = "get_subsurface: wl_subsurface@";
4815
4816 if (surface == parent) {
4817 wl_resource_post_error(resource,
4818 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4819 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004820 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004821 return;
4822 }
4823
4824 if (weston_surface_to_subsurface(surface)) {
4825 wl_resource_post_error(resource,
4826 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4827 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004828 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004829 return;
4830 }
4831
Pekka Paalanen50b67472014-10-01 15:02:41 +03004832 if (weston_surface_set_role(surface, "wl_subsurface", resource,
4833 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004834 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004835
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004836 if (weston_surface_get_main_surface(parent) == surface) {
4837 wl_resource_post_error(resource,
4838 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4839 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004840 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004841 return;
4842 }
4843
Pekka Paalanene67858b2013-04-25 13:57:42 +03004844 /* make sure the parent is in its own list */
4845 if (wl_list_empty(&parent->subsurface_list)) {
4846 if (!weston_subsurface_create_for_parent(parent)) {
4847 wl_resource_post_no_memory(resource);
4848 return;
4849 }
4850 }
4851
4852 sub = weston_subsurface_create(id, surface, parent);
4853 if (!sub) {
4854 wl_resource_post_no_memory(resource);
4855 return;
4856 }
4857
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004858 surface->committed = subsurface_committed;
4859 surface->committed_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004860 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004861}
4862
4863static void
4864subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
4865{
4866 wl_resource_destroy(resource);
4867}
4868
4869static const struct wl_subcompositor_interface subcompositor_interface = {
4870 subcompositor_destroy,
4871 subcompositor_get_subsurface
4872};
4873
4874static void
4875bind_subcompositor(struct wl_client *client,
4876 void *data, uint32_t version, uint32_t id)
4877{
4878 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004879 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004880
Jason Ekstranda85118c2013-06-27 20:17:02 -05004881 resource =
4882 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004883 if (resource == NULL) {
4884 wl_client_post_no_memory(client);
4885 return;
4886 }
4887 wl_resource_set_implementation(resource, &subcompositor_interface,
4888 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004889}
4890
Bryce Harrington0795ece2016-08-30 12:04:26 -07004891/** Set a DPMS mode on all of the compositor's outputs
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004892 *
4893 * \param compositor The compositor instance
4894 * \param state The DPMS state the outputs will be set to
4895 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004896static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004897weston_compositor_dpms(struct weston_compositor *compositor,
4898 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004899{
4900 struct weston_output *output;
4901
Bryce Harrington08976ac2016-08-30 12:05:16 -07004902 wl_list_for_each(output, &compositor->output_list, link)
4903 if (output->set_dpms)
4904 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004905}
4906
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004907/** Restores the compositor to active status
4908 *
4909 * \param compositor The compositor instance
4910 *
4911 * If the compositor was in a sleeping mode, all outputs are powered
4912 * back on via DPMS. Otherwise if the compositor was inactive
4913 * (idle/locked, offscreen, or sleeping) then the compositor's wake
4914 * signal will fire.
4915 *
4916 * Restarts the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004917 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004918 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004919WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004920weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004921{
Neil Roberts8b62e202013-09-30 13:14:47 +01004922 uint32_t old_state = compositor->state;
4923
4924 /* The state needs to be changed before emitting the wake
4925 * signal because that may try to schedule a repaint which
4926 * will not work if the compositor is still sleeping */
4927 compositor->state = WESTON_COMPOSITOR_ACTIVE;
4928
4929 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004930 case WESTON_COMPOSITOR_SLEEPING:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004931 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004932 case WESTON_COMPOSITOR_OFFSCREEN:
Daniel Stone893b9362016-11-08 15:47:09 +00004933 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004934 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004935 /* fall through */
4936 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004937 wl_event_source_timer_update(compositor->idle_source,
4938 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004939 }
4940}
4941
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004942/** Turns off rendering and frame events for the compositor.
4943 *
4944 * \param compositor The compositor instance
4945 *
4946 * This is used for example to prevent further rendering while the
4947 * compositor is shutting down.
4948 *
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004949 * Stops the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004950 *
4951 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004952 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004953WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004954weston_compositor_offscreen(struct weston_compositor *compositor)
4955{
4956 switch (compositor->state) {
4957 case WESTON_COMPOSITOR_OFFSCREEN:
4958 return;
4959 case WESTON_COMPOSITOR_SLEEPING:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004960 default:
4961 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4962 wl_event_source_timer_update(compositor->idle_source, 0);
4963 }
4964}
4965
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004966/** Powers down all attached output devices
4967 *
4968 * \param compositor The compositor instance
4969 *
4970 * Causes rendering to the outputs to cease, and no frame events to be
4971 * sent. Only powers down the outputs if the compositor is not already
4972 * in sleep mode.
4973 *
4974 * Stops the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004975 *
4976 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004977 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004978WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004979weston_compositor_sleep(struct weston_compositor *compositor)
4980{
4981 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
4982 return;
4983
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004984 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004985 compositor->state = WESTON_COMPOSITOR_SLEEPING;
4986 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
4987}
4988
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004989/** Sets compositor to idle mode
4990 *
4991 * \param data The compositor instance
4992 *
4993 * This is called when the idle timer fires. Once the compositor is in
4994 * idle mode it requires a wake action (e.g. via
4995 * weston_compositor_wake()) to restore it. The compositor's
4996 * idle_signal will be triggered when the idle event occurs.
4997 *
4998 * Idleness can be inhibited by setting the compositor's idle_inhibit
4999 * property.
5000 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04005001static int
5002idle_handler(void *data)
5003{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005004 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04005005
5006 if (compositor->idle_inhibit)
5007 return 1;
5008
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005009 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005010 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04005011
5012 return 1;
5013}
5014
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005015WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08005016weston_plane_init(struct weston_plane *plane,
5017 struct weston_compositor *ec,
5018 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005019{
5020 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02005021 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005022 plane->x = x;
5023 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08005024 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03005025
5026 /* Init the link so that the call to wl_list_remove() when releasing
5027 * the plane without ever stacking doesn't lead to a crash */
5028 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005029}
5030
5031WL_EXPORT void
5032weston_plane_release(struct weston_plane *plane)
5033{
Xiong Zhang97116532013-10-23 13:58:31 +08005034 struct weston_view *view;
5035
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005036 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02005037 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03005038
Xiong Zhang97116532013-10-23 13:58:31 +08005039 wl_list_for_each(view, &plane->compositor->view_list, link) {
5040 if (view->plane == plane)
5041 view->plane = NULL;
5042 }
5043
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03005044 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005045}
5046
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005047/** weston_compositor_stack_plane
5048 * \ingroup compositor
5049 */
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02005050WL_EXPORT void
5051weston_compositor_stack_plane(struct weston_compositor *ec,
5052 struct weston_plane *plane,
5053 struct weston_plane *above)
5054{
5055 if (above)
5056 wl_list_insert(above->link.prev, &plane->link);
5057 else
5058 wl_list_insert(&ec->plane_list, &plane->link);
5059}
5060
Quentin Glidic4ef719c2016-07-05 20:44:33 +02005061static void
5062output_release(struct wl_client *client, struct wl_resource *resource)
5063{
5064 wl_resource_destroy(resource);
5065}
5066
5067static const struct wl_output_interface output_interface = {
5068 output_release,
5069};
5070
5071
Casey Dahlin9074db52012-04-19 22:50:09 -04005072static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04005073{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05005074 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04005075}
5076
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005077static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005078bind_output(struct wl_client *client,
5079 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05005080{
Pekka Paalanen05347622017-03-27 12:24:34 +03005081 struct weston_head *head = data;
5082 struct weston_output *output = head->output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005083 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04005084 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05005085
Jason Ekstranda85118c2013-06-27 20:17:02 -05005086 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06005087 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07005088 if (resource == NULL) {
5089 wl_client_post_no_memory(client);
5090 return;
5091 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04005092
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005093 wl_list_insert(&head->resource_list, wl_resource_get_link(resource));
Pekka Paalanen055c1132017-03-27 16:31:25 +03005094 wl_resource_set_implementation(resource, &output_interface, head,
Pekka Paalanen05347622017-03-27 12:24:34 +03005095 unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04005096
Pekka Paalanen05347622017-03-27 12:24:34 +03005097 assert(output);
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05005098 wl_output_send_geometry(resource,
5099 output->x,
5100 output->y,
Pekka Paalanen01f60212017-03-24 15:39:24 +02005101 head->mm_width,
5102 head->mm_height,
5103 head->subpixel,
5104 head->make, head->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04005105 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04005106 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02005107 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02005108 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005109
5110 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05005111 wl_output_send_mode(resource,
5112 mode->flags,
5113 mode->width,
5114 mode->height,
5115 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005116 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02005117
Jasper St. Pierre0013a292014-08-07 16:43:11 -04005118 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02005119 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05005120}
5121
Pekka Paalanendcac3512017-12-08 14:13:34 +02005122static void
5123weston_head_add_global(struct weston_head *head)
5124{
5125 head->global = wl_global_create(head->compositor->wl_display,
5126 &wl_output_interface, 3,
5127 head, bind_output);
5128}
5129
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02005130/** Remove the global wl_output protocol object
5131 *
5132 * \param head The head whose global to remove.
5133 *
5134 * Also orphans the wl_resources for this head (wl_output).
5135 */
5136static void
5137weston_head_remove_global(struct weston_head *head)
5138{
5139 struct wl_resource *resource, *tmp;
5140
5141 if (head->global)
5142 wl_global_destroy(head->global);
5143 head->global = NULL;
5144
5145 wl_resource_for_each_safe(resource, tmp, &head->resource_list) {
5146 unbind_resource(resource);
5147 wl_resource_set_destructor(resource, NULL);
5148 wl_resource_set_user_data(resource, NULL);
5149 }
Roman Gilge97391c2019-03-29 13:01:06 +01005150
5151 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
5152 /* It's sufficient to unset the destructor, then the list elements
5153 * won't be accessed.
5154 */
5155 wl_resource_set_destructor(resource, NULL);
5156 }
5157 wl_list_init(&head->xdg_output_resource_list);
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02005158}
5159
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005160/** Get the backing object of wl_output
5161 *
5162 * \param resource A wl_output protocol object.
5163 * \return The backing object (user data) of a wl_resource representing a
5164 * wl_output protocol object.
Marius Vlad78984ee2019-06-11 00:05:08 +03005165 *
5166 * \ingroup head
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005167 */
Pekka Paalanen055c1132017-03-27 16:31:25 +03005168WL_EXPORT struct weston_head *
5169weston_head_from_resource(struct wl_resource *resource)
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005170{
5171 assert(wl_resource_instance_of(resource, &wl_output_interface,
5172 &output_interface));
5173
5174 return wl_resource_get_user_data(resource);
5175}
5176
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005177/** Initialize a pre-allocated weston_head
5178 *
5179 * \param head The head to initialize.
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005180 * \param name The head name, e.g. the connector name or equivalent.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005181 *
5182 * The head will be safe to attach, detach and release.
5183 *
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005184 * The name is used in logs, and can be used by compositors as a configuration
5185 * identifier.
5186 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005187 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005188 * \internal
5189 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005190WL_EXPORT void
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005191weston_head_init(struct weston_head *head, const char *name)
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005192{
5193 /* Add some (in)sane defaults which can be used
5194 * for checking if an output was properly configured
5195 */
5196 memset(head, 0, sizeof *head);
5197
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005198 wl_list_init(&head->compositor_link);
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005199 wl_signal_init(&head->destroy_signal);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005200 wl_list_init(&head->output_link);
5201 wl_list_init(&head->resource_list);
Roman Gilge97391c2019-03-29 13:01:06 +01005202 wl_list_init(&head->xdg_output_resource_list);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005203 head->name = strdup(name);
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305204 head->current_protection = WESTON_HDCP_DISABLE;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005205}
5206
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005207/** Send output heads changed signal
5208 *
5209 * \param output The output that changed.
5210 *
5211 * Notify that the enabled output gained and/or lost heads, or that the
5212 * associated heads may have changed their connection status. This does not
5213 * include cases where the output becomes enabled or disabled. The registered
5214 * callbacks are called after the change has successfully happened.
5215 *
5216 * If connection status change causes the compositor to attach or detach a head
5217 * to an enabled output, the registered callbacks may be called multiple times.
Marius Vlad55d87362019-06-11 01:15:35 +03005218 *
5219 * \ingroup output
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005220 */
5221static void
5222weston_output_emit_heads_changed(struct weston_output *output)
5223{
5224 wl_signal_emit(&output->compositor->output_heads_changed_signal,
5225 output);
5226}
5227
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005228/** Idle task for emitting heads_changed_signal */
5229static void
5230weston_compositor_call_heads_changed(void *data)
5231{
5232 struct weston_compositor *compositor = data;
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005233 struct weston_head *head;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005234
5235 compositor->heads_changed_source = NULL;
5236
5237 wl_signal_emit(&compositor->heads_changed_signal, compositor);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005238
5239 wl_list_for_each(head, &compositor->head_list, compositor_link) {
5240 if (head->output && head->output->enabled)
5241 weston_output_emit_heads_changed(head->output);
5242 }
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005243}
5244
5245/** Schedule a call on idle to heads_changed callback
5246 *
5247 * \param compositor The Compositor.
5248 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005249 * \ingroup compositor
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005250 * \internal
5251 */
5252static void
5253weston_compositor_schedule_heads_changed(struct weston_compositor *compositor)
5254{
5255 struct wl_event_loop *loop;
5256
5257 if (compositor->heads_changed_source)
5258 return;
5259
5260 loop = wl_display_get_event_loop(compositor->wl_display);
5261 compositor->heads_changed_source = wl_event_loop_add_idle(loop,
5262 weston_compositor_call_heads_changed, compositor);
5263}
5264
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005265/** Register a new head
5266 *
5267 * \param compositor The compositor.
5268 * \param head The head to register, must not be already registered.
5269 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005270 * This signals the core that a new head has become available, leading to
5271 * heads_changed hook being called later.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005272 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005273 * \ingroup compositor
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005274 * \internal
5275 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005276WL_EXPORT void
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005277weston_compositor_add_head(struct weston_compositor *compositor,
5278 struct weston_head *head)
5279{
5280 assert(wl_list_empty(&head->compositor_link));
5281 assert(head->name);
5282
5283 wl_list_insert(compositor->head_list.prev, &head->compositor_link);
5284 head->compositor = compositor;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005285 weston_compositor_schedule_heads_changed(compositor);
5286}
5287
5288/** Adds a listener to be called when heads change
5289 *
5290 * \param compositor The compositor.
5291 * \param listener The listener to add.
5292 *
Marius Vlada2dace22019-06-12 16:05:44 +03005293 * The listener notify function argument is weston_compositor.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005294 *
5295 * The listener function will be called after heads are added or their
5296 * connection status has changed. Several changes may be accumulated into a
5297 * single call. The user is expected to iterate over the existing heads and
5298 * check their statuses to find out what changed.
5299 *
5300 * \sa weston_compositor_iterate_heads, weston_head_is_connected,
5301 * weston_head_is_enabled
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005302 * \ingroup compositor
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005303 */
5304WL_EXPORT void
5305weston_compositor_add_heads_changed_listener(struct weston_compositor *compositor,
5306 struct wl_listener *listener)
5307{
5308 wl_signal_add(&compositor->heads_changed_signal, listener);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005309}
5310
5311/** Iterate over available heads
5312 *
5313 * \param compositor The compositor.
Marius Vlada2dace22019-06-12 16:05:44 +03005314 * \param iter The iterator, or NULL for start.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005315 * \return The next available head in the list.
5316 *
5317 * Returns all available heads, regardless of being connected or enabled.
5318 *
5319 * You can iterate over all heads as follows:
5320 * \code
5321 * struct weston_head *head = NULL;
5322 *
5323 * while ((head = weston_compositor_iterate_heads(compositor, head))) {
5324 * ...
5325 * }
5326 * \endcode
5327 *
5328 * If you cause \c iter to be removed from the list, you cannot use it to
5329 * continue iterating. Removing any other item is safe.
5330 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005331 * \ingroup compositor
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005332 */
5333WL_EXPORT struct weston_head *
5334weston_compositor_iterate_heads(struct weston_compositor *compositor,
5335 struct weston_head *iter)
5336{
5337 struct wl_list *list = &compositor->head_list;
5338 struct wl_list *node;
5339
5340 assert(compositor);
5341 assert(!iter || iter->compositor == compositor);
5342
5343 if (iter)
5344 node = iter->compositor_link.next;
5345 else
5346 node = list->next;
5347
5348 assert(node);
5349 assert(!iter || node != &iter->compositor_link);
5350
5351 if (node == list)
5352 return NULL;
5353
5354 return container_of(node, struct weston_head, compositor_link);
5355}
5356
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005357/** Iterate over attached heads
5358 *
5359 * \param output The output whose heads to iterate.
Marius Vlada2dace22019-06-12 16:05:44 +03005360 * \param iter The iterator, or NULL for start.
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005361 * \return The next attached head in the list.
5362 *
5363 * Returns all heads currently attached to the output.
5364 *
5365 * You can iterate over all heads as follows:
5366 * \code
5367 * struct weston_head *head = NULL;
5368 *
5369 * while ((head = weston_output_iterate_heads(output, head))) {
5370 * ...
5371 * }
5372 * \endcode
5373 *
5374 * If you cause \c iter to be removed from the list, you cannot use it to
5375 * continue iterating. Removing any other item is safe.
5376 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05005377 * \ingroup output
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005378 */
5379WL_EXPORT struct weston_head *
5380weston_output_iterate_heads(struct weston_output *output,
5381 struct weston_head *iter)
5382{
5383 struct wl_list *list = &output->head_list;
5384 struct wl_list *node;
5385
5386 assert(output);
5387 assert(!iter || iter->output == output);
5388
5389 if (iter)
5390 node = iter->output_link.next;
5391 else
5392 node = list->next;
5393
5394 assert(node);
5395 assert(!iter || node != &iter->output_link);
5396
5397 if (node == list)
5398 return NULL;
5399
5400 return container_of(node, struct weston_head, output_link);
5401}
5402
Pekka Paalanendcac3512017-12-08 14:13:34 +02005403/** Attach a head to an output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005404 *
5405 * \param output The output to attach to.
5406 * \param head The head that is not yet attached.
5407 * \return 0 on success, -1 on failure.
5408 *
5409 * Attaches the given head to the output. All heads of an output are clones
5410 * and share the resolution and timings.
5411 *
5412 * Cloning heads this way uses less resources than creating an output for
5413 * each head, but is not always possible due to environment, driver and hardware
5414 * limitations.
5415 *
5416 * On failure, the head remains unattached. Success of this function does not
5417 * guarantee the output configuration is actually valid. The final checks are
Pekka Paalanendcac3512017-12-08 14:13:34 +02005418 * made on weston_output_enable() unless the output was already enabled.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005419 *
Marius Vlad55d87362019-06-11 01:15:35 +03005420 * \ingroup output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005421 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005422WL_EXPORT int
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005423weston_output_attach_head(struct weston_output *output,
5424 struct weston_head *head)
5425{
Pekka Paalanendcac3512017-12-08 14:13:34 +02005426 char *head_names;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005427
5428 if (!wl_list_empty(&head->output_link))
5429 return -1;
5430
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005431 if (output->attach_head) {
5432 if (output->attach_head(output, head) < 0)
5433 return -1;
5434 } else if (!wl_list_empty(&output->head_list)) {
5435 /* No support for clones in the legacy path. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005436 return -1;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005437 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005438
5439 head->output = output;
5440 wl_list_insert(output->head_list.prev, &head->output_link);
5441
Pekka Paalanendcac3512017-12-08 14:13:34 +02005442 if (output->enabled) {
5443 weston_head_add_global(head);
5444
5445 head_names = weston_output_create_heads_string(output);
5446 weston_log("Output '%s' updated to have head(s) %s\n",
5447 output->name, head_names);
5448 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005449
5450 weston_output_emit_heads_changed(output);
Pekka Paalanendcac3512017-12-08 14:13:34 +02005451 }
5452
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005453 return 0;
5454}
5455
5456/** Detach a head from its output
5457 *
5458 * \param head The head to detach.
5459 *
5460 * It is safe to detach a non-attached head.
5461 *
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005462 * If the head is attached to an enabled output and the output will be left
5463 * with no heads, the output will be disabled.
5464 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005465 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005466 * \sa weston_output_disable
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005467 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005468WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005469weston_head_detach(struct weston_head *head)
5470{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005471 struct weston_output *output = head->output;
Pekka Paalanena0106992017-12-08 16:11:17 +02005472 char *head_names;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005473
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005474 wl_list_remove(&head->output_link);
5475 wl_list_init(&head->output_link);
5476 head->output = NULL;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005477
5478 if (!output)
5479 return;
5480
5481 if (output->detach_head)
5482 output->detach_head(output, head);
5483
5484 if (output->enabled) {
5485 weston_head_remove_global(head);
5486
Pekka Paalanena0106992017-12-08 16:11:17 +02005487 if (wl_list_empty(&output->head_list)) {
5488 weston_log("Output '%s' no heads left, disabling.\n",
5489 output->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005490 weston_output_disable(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02005491 } else {
5492 head_names = weston_output_create_heads_string(output);
5493 weston_log("Output '%s' updated to have head(s) %s\n",
5494 output->name, head_names);
5495 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005496
5497 weston_output_emit_heads_changed(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02005498 }
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005499 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005500}
5501
5502/** Destroy a head
5503 *
5504 * \param head The head to be released.
5505 *
5506 * Destroys the head. The caller is responsible for freeing the memory pointed
5507 * to by \c head.
5508 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005509 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005510 * \internal
5511 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005512WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005513weston_head_release(struct weston_head *head)
5514{
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005515 wl_signal_emit(&head->destroy_signal, head);
5516
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005517 weston_head_detach(head);
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005518
5519 free(head->make);
5520 free(head->model);
5521 free(head->serial_number);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005522 free(head->name);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005523
5524 wl_list_remove(&head->compositor_link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005525}
5526
Pekka Paalanene19970f2017-08-28 14:11:02 +03005527static void
5528weston_head_set_device_changed(struct weston_head *head)
5529{
5530 head->device_changed = true;
5531
5532 if (head->compositor)
5533 weston_compositor_schedule_heads_changed(head->compositor);
5534}
5535
5536/** String equal comparison with NULLs being equal */
5537static bool
5538str_null_eq(const char *a, const char *b)
5539{
5540 if (!a && !b)
5541 return true;
5542
5543 if (!!a != !!b)
5544 return false;
5545
5546 return strcmp(a, b) == 0;
5547}
5548
Pekka Paalanen01f60212017-03-24 15:39:24 +02005549/** Store monitor make, model and serial number
5550 *
5551 * \param head The head to modify.
5552 * \param make The monitor make. If EDID is available, the PNP ID. Otherwise
5553 * any string, or NULL for none.
5554 * \param model The monitor model or name, or a made-up string, or NULL for
5555 * none.
5556 * \param serialno The monitor serial number, a made-up string, or NULL for
5557 * none.
5558 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005559 * This may set the device_changed flag.
5560 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005561 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005562 * \internal
5563 */
5564WL_EXPORT void
5565weston_head_set_monitor_strings(struct weston_head *head,
5566 const char *make,
5567 const char *model,
5568 const char *serialno)
5569{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005570 if (str_null_eq(head->make, make) &&
5571 str_null_eq(head->model, model) &&
5572 str_null_eq(head->serial_number, serialno))
5573 return;
5574
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005575 free(head->make);
5576 free(head->model);
5577 free(head->serial_number);
5578
5579 head->make = make ? strdup(make) : NULL;
5580 head->model = model ? strdup(model) : NULL;
5581 head->serial_number = serialno ? strdup(serialno) : NULL;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005582
5583 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005584}
5585
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005586/** Store display non-desktop status
5587 *
5588 * \param head The head to modify.
5589 * \param non_desktop Whether the head connects to a non-desktop display.
5590 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005591 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005592 * \internal
5593 */
5594WL_EXPORT void
5595weston_head_set_non_desktop(struct weston_head *head, bool non_desktop)
5596{
5597 if (head->non_desktop == non_desktop)
5598 return;
5599
5600 head->non_desktop = non_desktop;
5601
5602 weston_head_set_device_changed(head);
5603}
5604
Lucas Stacha69cb712019-11-25 23:29:31 +00005605/** Store display transformation
5606 *
5607 * \param head The head to modify.
5608 * \param transform The transformation to apply for this head
5609 *
5610 * This may set the device_changed flag.
5611 *
5612 * \ingroup head
5613 * \internal
5614 */
5615WL_EXPORT void
5616weston_head_set_transform(struct weston_head *head, uint32_t transform)
5617{
5618 if (head->transform == transform)
5619 return;
5620
5621 head->transform = transform;
5622
5623 weston_head_set_device_changed(head);
5624}
5625
5626
Pekka Paalanen01f60212017-03-24 15:39:24 +02005627/** Store physical image size
5628 *
5629 * \param head The head to modify.
5630 * \param mm_width Image area width in millimeters.
5631 * \param mm_height Image area height in millimeters.
5632 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005633 * This may set the device_changed flag.
5634 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005635 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005636 * \internal
5637 */
5638WL_EXPORT void
5639weston_head_set_physical_size(struct weston_head *head,
5640 int32_t mm_width, int32_t mm_height)
5641{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005642 if (head->mm_width == mm_width &&
5643 head->mm_height == mm_height)
5644 return;
5645
Pekka Paalanen01f60212017-03-24 15:39:24 +02005646 head->mm_width = mm_width;
5647 head->mm_height = mm_height;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005648
5649 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005650}
5651
5652/** Store monitor sub-pixel layout
5653 *
5654 * \param head The head to modify.
5655 * \param sp Sub-pixel layout. The possible values are:
5656 * - WL_OUTPUT_SUBPIXEL_UNKNOWN,
5657 * - WL_OUTPUT_SUBPIXEL_NONE,
5658 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB,
5659 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR,
5660 * - WL_OUTPUT_SUBPIXEL_VERTICAL_RGB,
5661 * - WL_OUTPUT_SUBPIXEL_VERTICAL_BGR
5662 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005663 * This may set the device_changed flag.
5664 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005665 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005666 * \internal
5667 */
5668WL_EXPORT void
5669weston_head_set_subpixel(struct weston_head *head,
5670 enum wl_output_subpixel sp)
5671{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005672 if (head->subpixel == sp)
5673 return;
5674
Pekka Paalanen01f60212017-03-24 15:39:24 +02005675 head->subpixel = sp;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005676
5677 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005678}
5679
5680/** Mark the monitor as internal
5681 *
5682 * This is used for embedded screens, like laptop panels.
5683 *
5684 * \param head The head to mark as internal.
5685 *
5686 * By default a head is external. The type is often inferred from the physical
5687 * connector type.
5688 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005689 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005690 * \internal
5691 */
5692WL_EXPORT void
5693weston_head_set_internal(struct weston_head *head)
5694{
5695 head->connection_internal = true;
5696}
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005697
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005698/** Store connector status
5699 *
5700 * \param head The head to modify.
5701 * \param connected Whether the head is connected.
5702 *
5703 * Connectors are created as disconnected. This function can be used to
5704 * set the connector status.
5705 *
5706 * The status should be set to true when a physical connector is connected to
5707 * a video sink device like a monitor and to false when the connector is
5708 * disconnected. For nested backends, the connection status should reflect the
5709 * connection to the parent display server.
5710 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005711 * When the connection status changes, it schedules a call to the heads_changed
Pekka Paalanene19970f2017-08-28 14:11:02 +03005712 * hook and sets the device_changed flag.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005713 *
5714 * \sa weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005715 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005716 * \internal
5717 */
5718WL_EXPORT void
5719weston_head_set_connection_status(struct weston_head *head, bool connected)
5720{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005721 if (head->connected == connected)
5722 return;
5723
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005724 head->connected = connected;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005725
Pekka Paalanene19970f2017-08-28 14:11:02 +03005726 weston_head_set_device_changed(head);
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005727}
5728
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305729static void
5730weston_output_compute_protection(struct weston_output *output)
5731{
5732 struct weston_head *head;
5733 enum weston_hdcp_protection op_protection;
5734 bool op_protection_valid = false;
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305735 struct weston_compositor *wc = output->compositor;
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305736
5737 wl_list_for_each(head, &output->head_list, output_link) {
5738 if (!op_protection_valid) {
5739 op_protection = head->current_protection;
5740 op_protection_valid = true;
5741 }
5742 if (head->current_protection < op_protection)
5743 op_protection = head->current_protection;
5744 }
5745
5746 if (!op_protection_valid)
5747 op_protection = WESTON_HDCP_DISABLE;
5748
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305749 if (output->current_protection != op_protection) {
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305750 output->current_protection = op_protection;
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305751 weston_output_damage(output);
Ankit Nautiyalfaa5ab42019-07-08 13:23:24 +05305752 weston_schedule_surface_protection_update(wc);
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305753 }
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305754}
5755
5756WL_EXPORT void
5757weston_head_set_content_protection_status(struct weston_head *head,
5758 enum weston_hdcp_protection status)
5759{
5760 head->current_protection = status;
5761 if (head->output)
5762 weston_output_compute_protection(head->output);
5763}
5764
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005765/** Is the head currently connected?
5766 *
5767 * \param head The head to query.
5768 * \return Connection status.
5769 *
5770 * Returns true if the head is physically connected to a monitor, or in
5771 * case of a nested backend returns true when there is a connection to the
5772 * parent display server.
5773 *
5774 * This is independent from the head being enabled.
5775 *
5776 * \sa weston_head_is_enabled
Marius Vlad78984ee2019-06-11 00:05:08 +03005777 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005778 */
5779WL_EXPORT bool
5780weston_head_is_connected(struct weston_head *head)
5781{
5782 return head->connected;
5783}
5784
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005785/** Is the head currently enabled?
5786 *
5787 * \param head The head to query.
5788 * \return Video status.
5789 *
5790 * Returns true if the head is currently transmitting a video stream.
5791 *
5792 * This is independent of the head being connected.
5793 *
5794 * \sa weston_head_is_connected
Marius Vlad78984ee2019-06-11 00:05:08 +03005795 * \ingroup head
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005796 */
5797WL_EXPORT bool
5798weston_head_is_enabled(struct weston_head *head)
5799{
5800 if (!head->output)
5801 return false;
5802
5803 return head->output->enabled;
5804}
5805
Pekka Paalanene19970f2017-08-28 14:11:02 +03005806/** Has the device information changed?
5807 *
5808 * \param head The head to query.
5809 * \return True if the device information has changed since last reset.
5810 *
5811 * The information about the connected display device, e.g. a monitor, may
5812 * change without being disconnected in between. Changing information
5813 * causes a call to the heads_changed hook.
5814 *
5815 * The information includes make, model, serial number, physical size,
5816 * and sub-pixel type. The connection status is also included.
5817 *
5818 * \sa weston_head_reset_device_changed, weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005819 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03005820 */
5821WL_EXPORT bool
5822weston_head_is_device_changed(struct weston_head *head)
5823{
5824 return head->device_changed;
5825}
5826
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005827/** Does the head represent a non-desktop display?
5828 *
5829 * \param head The head to query.
5830 * \return True if the device is a non-desktop display.
5831 *
5832 * Non-desktop heads are not attached to outputs by default.
5833 * This stops weston from extending the desktop onto head mounted displays.
5834 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005835 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005836 */
5837WL_EXPORT bool
5838weston_head_is_non_desktop(struct weston_head *head)
5839{
5840 return head->non_desktop;
5841}
5842
Pekka Paalanene19970f2017-08-28 14:11:02 +03005843/** Acknowledge device information change
5844 *
5845 * \param head The head to acknowledge.
5846 *
5847 * Clears the device changed flag on this head. When a compositor has processed
5848 * device information, it should call this to be able to notice further
5849 * changes.
5850 *
5851 * \sa weston_head_is_device_changed
Marius Vlad78984ee2019-06-11 00:05:08 +03005852 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03005853 */
5854WL_EXPORT void
5855weston_head_reset_device_changed(struct weston_head *head)
5856{
5857 head->device_changed = false;
5858}
5859
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005860/** Get the name of a head
5861 *
5862 * \param head The head to query.
5863 * \return The head's name, not NULL.
5864 *
5865 * The name depends on the backend. The DRM backend uses connector names,
5866 * other backends may use hardcoded names or user-given names.
Marius Vlad78984ee2019-06-11 00:05:08 +03005867 *
5868 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005869 */
5870WL_EXPORT const char *
5871weston_head_get_name(struct weston_head *head)
5872{
5873 return head->name;
5874}
5875
5876/** Get the output the head is attached to
5877 *
5878 * \param head The head to query.
5879 * \return The output the head is attached to, or NULL if detached.
Marius Vlad78984ee2019-06-11 00:05:08 +03005880 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005881 */
5882WL_EXPORT struct weston_output *
5883weston_head_get_output(struct weston_head *head)
5884{
5885 return head->output;
5886}
5887
Lucas Stacha69cb712019-11-25 23:29:31 +00005888/** Get the head's native transformation
5889 *
5890 * \param head The head to query.
5891 * \return The head's native transform, as a WL_OUTPUT_TRANSFORM_* value
5892 *
5893 * A weston_head may have a 'native' transform provided by the backend.
5894 * Examples include panels which are physically rotated, where the rotation
5895 * is recorded and described as part of the system configuration. This call
5896 * will return any known native transform for the head.
5897 *
5898 * \ingroup head
5899 */
5900WL_EXPORT uint32_t
5901weston_head_get_transform(struct weston_head *head)
5902{
5903 return head->transform;
5904}
5905
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005906/** Add destroy callback for a head
5907 *
5908 * \param head The head to watch for.
5909 * \param listener The listener to add. The \c notify member must be set.
5910 *
5911 * Heads may get destroyed for various reasons by the backends. If a head is
5912 * attached to an output, the compositor should listen for head destruction
5913 * and reconfigure or destroy the output if necessary.
5914 *
5915 * The destroy callbacks will be called on weston_head destruction before any
5916 * automatic detaching from an associated weston_output and before any
5917 * weston_head information is lost.
5918 *
5919 * The \c data argument to the notify callback is the weston_head being
5920 * destroyed.
Marius Vlad78984ee2019-06-11 00:05:08 +03005921 *
5922 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005923 */
5924WL_EXPORT void
5925weston_head_add_destroy_listener(struct weston_head *head,
5926 struct wl_listener *listener)
5927{
5928 wl_signal_add(&head->destroy_signal, listener);
5929}
5930
5931/** Look up destroy listener for a head
5932 *
5933 * \param head The head to query.
5934 * \param notify The notify function used used for the added destroy listener.
5935 * \return The listener, or NULL if not found.
5936 *
5937 * This looks up the previously added destroy listener struct based on the
5938 * notify function it has. The listener can be used to access user data
5939 * through \c container_of().
5940 *
5941 * \sa wl_signal_get()
Marius Vlad78984ee2019-06-11 00:05:08 +03005942 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005943 */
5944WL_EXPORT struct wl_listener *
5945weston_head_get_destroy_listener(struct weston_head *head,
5946 wl_notify_func_t notify)
5947{
5948 return wl_signal_get(&head->destroy_signal, notify);
5949}
5950
David Fort0de859e2016-05-27 23:22:57 +02005951/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005952static void
David Fort0de859e2016-05-27 23:22:57 +02005953weston_compositor_reflow_outputs(struct weston_compositor *compositor,
5954 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005955{
5956 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02005957 bool start_resizing = false;
5958
5959 if (!delta_width)
5960 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005961
5962 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02005963 if (output == resized_output) {
5964 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005965 continue;
5966 }
5967
David Fort0de859e2016-05-27 23:22:57 +02005968 if (start_resizing) {
5969 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005970 output->dirty = 1;
5971 }
5972 }
5973}
5974
Pekka Paalanen06920792020-12-08 15:22:36 +02005975/** Transform a region in-place from global to output coordinates
5976 *
5977 * \param output The output that defines the transformation.
5978 * \param region The region to be transformed in-place.
5979 *
5980 * This takes a region in the global coordinate system, and takes into account
5981 * output position, transform and scale, and the zoom, and converts the region
5982 * into output pixel coordinates in the framebuffer.
5983 *
5984 * Uses floating-point operations if zoom is active, which may round to expand
5985 * the region.
5986 *
5987 * \internal
5988 * \ingroup output
5989 */
5990WL_EXPORT void
5991weston_output_region_from_global(struct weston_output *output,
5992 pixman_region32_t *region)
5993{
5994 if (output->zoom.active) {
5995 weston_matrix_transform_region(region, &output->matrix, region);
5996 } else {
5997 pixman_region32_translate(region, -output->x, -output->y);
5998 weston_transformed_region(output->width, output->height,
5999 output->transform,
6000 output->current_scale,
6001 region, region);
6002 }
6003}
6004
Pekka Paalanend72bad22017-03-29 17:01:41 +03006005static void
Scott Moreauccbf29d2012-02-22 14:21:41 -07006006weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01006007{
Scott Moreau850ca422012-05-21 15:21:25 -06006008 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05006009
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006010 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05006011 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06006012
Scott Moreauccbf29d2012-02-22 14:21:41 -07006013 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06006014 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05006015 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01006016 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05006017 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01006018 weston_matrix_scale(&output->matrix, magnification,
6019 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07006020 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04006021
Jason Ekstrandfb23df72014-10-16 10:55:21 -05006022 switch (output->transform) {
6023 case WL_OUTPUT_TRANSFORM_FLIPPED:
6024 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
6025 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
6026 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
6027 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
6028 weston_matrix_scale(&output->matrix, -1, 1, 1);
6029 break;
6030 }
6031
6032 switch (output->transform) {
6033 default:
6034 case WL_OUTPUT_TRANSFORM_NORMAL:
6035 case WL_OUTPUT_TRANSFORM_FLIPPED:
6036 break;
6037 case WL_OUTPUT_TRANSFORM_90:
6038 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +02006039 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
6040 weston_matrix_rotate_xy(&output->matrix, 0, -1);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05006041 break;
6042 case WL_OUTPUT_TRANSFORM_180:
6043 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
6044 weston_matrix_translate(&output->matrix,
6045 -output->width, -output->height, 0);
6046 weston_matrix_rotate_xy(&output->matrix, -1, 0);
6047 break;
6048 case WL_OUTPUT_TRANSFORM_270:
6049 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +02006050 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
6051 weston_matrix_rotate_xy(&output->matrix, 0, 1);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05006052 break;
6053 }
6054
6055 if (output->current_scale != 1)
6056 weston_matrix_scale(&output->matrix,
6057 output->current_scale,
6058 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01006059
Scott Moreauccbf29d2012-02-22 14:21:41 -07006060 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05006061
6062 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07006063}
6064
Scott Moreau1bad5db2012-08-18 01:04:05 -06006065static void
Alexander Larsson0b135062013-05-28 16:23:36 +02006066weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06006067{
6068 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03006069 output->native_scale = scale;
6070 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06006071
Pekka Paalanen59987fa2016-04-26 15:50:59 +03006072 convert_size_by_transform_scale(&output->width, &output->height,
6073 output->current_mode->width,
6074 output->current_mode->height,
6075 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02006076}
6077
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006078static void
6079weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07006080{
6081 output->x = x;
6082 output->y = y;
6083
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006084 pixman_region32_fini(&output->region);
Scott Moreauccbf29d2012-02-22 14:21:41 -07006085 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06006086 output->width,
6087 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01006088}
6089
Marius Vlad55d87362019-06-11 01:15:35 +03006090/**
6091 * \ingroup output
6092 */
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006093WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006094weston_output_move(struct weston_output *output, int x, int y)
6095{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006096 struct weston_head *head;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006097 struct wl_resource *resource;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006098 int ver;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006099
6100 output->move_x = x - output->x;
6101 output->move_y = y - output->y;
6102
6103 if (output->move_x == 0 && output->move_y == 0)
6104 return;
6105
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006106 weston_output_init_geometry(output, x, y);
6107
6108 output->dirty = 1;
6109
6110 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006111 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006112
6113 /* Notify clients of the change for output position. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006114 wl_list_for_each(head, &output->head_list, output_link) {
6115 wl_resource_for_each(resource, &head->resource_list) {
6116 wl_output_send_geometry(resource,
6117 output->x,
6118 output->y,
6119 head->mm_width,
6120 head->mm_height,
6121 head->subpixel,
6122 head->make,
6123 head->model,
6124 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08006125
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006126 ver = wl_resource_get_version(resource);
6127 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
6128 wl_output_send_done(resource);
6129 }
Roman Gilge97391c2019-03-29 13:01:06 +01006130
6131 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
6132 zxdg_output_v1_send_logical_position(resource,
6133 output->x,
6134 output->y);
6135 zxdg_output_v1_send_done(resource);
6136 }
Quanxian Wangb2c86362014-03-14 09:16:25 +08006137 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006138}
6139
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006140/** Signal that a pending output is taken into use.
6141 *
6142 * Removes the output from the pending list and adds it to the compositor's
6143 * list of enabled outputs. The output created signal is emitted.
Giulio Camuffob1147152015-05-06 21:41:57 +03006144 *
Pekka Paalanen2210ad02017-03-30 15:48:06 +03006145 * The output gets an internal ID assigned, and the wl_output global is
6146 * created.
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03006147 *
Giulio Camuffob1147152015-05-06 21:41:57 +03006148 * \param compositor The compositor instance.
6149 * \param output The output to be added.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006150 *
6151 * \internal
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006152 * \ingroup compositor
Giulio Camuffob1147152015-05-06 21:41:57 +03006153 */
Pekka Paalanenf9681b52017-03-29 16:58:48 +03006154static void
Giulio Camuffob1147152015-05-06 21:41:57 +03006155weston_compositor_add_output(struct weston_compositor *compositor,
6156 struct weston_output *output)
6157{
Armin Krezoviće5403842016-08-05 15:28:29 +02006158 struct weston_view *view, *next;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03006159 struct weston_head *head;
Armin Krezoviće5403842016-08-05 15:28:29 +02006160
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006161 assert(!output->enabled);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03006162
6163 /* Verify we haven't reached the limit of 32 available output IDs */
6164 assert(ffs(~compositor->output_id_pool) > 0);
6165
6166 /* Invert the output id pool and look for the lowest numbered
6167 * switch (the least significant bit). Take that bit's position
6168 * as our ID, and mark it used in the compositor's output_id_pool.
6169 */
6170 output->id = ffs(~compositor->output_id_pool) - 1;
6171 compositor->output_id_pool |= 1u << output->id;
6172
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006173 wl_list_remove(&output->link);
Giulio Camuffob1147152015-05-06 21:41:57 +03006174 wl_list_insert(compositor->output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006175 output->enabled = true;
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006176
Pekka Paalanendcac3512017-12-08 14:13:34 +02006177 wl_list_for_each(head, &output->head_list, output_link)
6178 weston_head_add_global(head);
Pekka Paalanen2210ad02017-03-30 15:48:06 +03006179
Giulio Camuffob1147152015-05-06 21:41:57 +03006180 wl_signal_emit(&compositor->output_created_signal, output);
Armin Krezoviće5403842016-08-05 15:28:29 +02006181
6182 wl_list_for_each_safe(view, next, &compositor->view_list, link)
6183 weston_view_geometry_dirty(view);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04006184}
6185
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006186/** Transform device coordinates into global coordinates
6187 *
Marius Vlada2dace22019-06-12 16:05:44 +03006188 * \param output the weston_output object
6189 * \param[in] device_x X coordinate in device units.
6190 * \param[in] device_y Y coordinate in device units.
6191 * \param[out] x X coordinate in the global space.
6192 * \param[out] y Y coordinate in the global space.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006193 *
Marius Vlada2dace22019-06-12 16:05:44 +03006194 * Transforms coordinates from the device coordinate space (physical pixel
6195 * units) to the global coordinate space (logical pixel units). This takes
6196 * into account output transform and scale.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006197 *
Marius Vlad55d87362019-06-11 01:15:35 +03006198 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006199 * \internal
6200 */
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006201WL_EXPORT void
6202weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02006203 double device_x, double device_y,
6204 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006205{
Derek Foreman0f679412014-10-02 13:41:17 -05006206 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02006207 device_x,
6208 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05006209 0.0,
6210 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006211
Derek Foreman67a18b92015-03-24 11:36:14 -05006212 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006213
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02006214 *x = p.f[0] / p.f[3];
6215 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006216}
6217
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006218/** Removes output from compositor's list of enabled outputs
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006219 *
6220 * \param output The weston_output object that is being removed.
6221 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006222 * The following happens:
6223 *
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03006224 * - Destroys all paint nodes related to the output.
6225 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006226 * - The output assignments of all views in the current scenegraph are
6227 * recomputed.
6228 *
6229 * - Presentation feedback is discarded.
6230 *
6231 * - Compositor is notified that outputs were changed and
6232 * applies the necessary changes to re-layout outputs.
6233 *
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006234 * - The output is put back in the pending outputs list.
6235 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006236 * - Signal is emitted to notify all users of the weston_output
6237 * object that the output is being destroyed.
6238 *
6239 * - wl_output protocol objects referencing this weston_output
Pekka Paalanen2210ad02017-03-30 15:48:06 +03006240 * are made inert, and the wl_output global is removed.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006241 *
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03006242 * - The output's internal ID is released.
6243 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006244 * \ingroup compositor
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006245 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006246 */
6247static void
6248weston_compositor_remove_output(struct weston_output *output)
6249{
Pekka Paalanenbccda712017-03-29 16:16:04 +03006250 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03006251 struct weston_paint_node *pnode, *pntmp;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006252 struct weston_view *view;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03006253 struct weston_head *head;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006254
6255 assert(output->destroying);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006256 assert(output->enabled);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006257
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03006258 wl_list_for_each_safe(pnode, pntmp,
6259 &output->paint_node_list, output_link) {
6260 weston_paint_node_destroy(pnode);
6261 }
Pekka Paalanen2fddc532021-04-30 17:41:29 +03006262 assert(wl_list_empty(&output->paint_node_z_order_list));
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03006263
Pekka Paalanenbccda712017-03-29 16:16:04 +03006264 wl_list_for_each(view, &compositor->view_list, link) {
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006265 if (view->output_mask & (1u << output->id))
6266 weston_view_assign_output(view);
6267 }
6268
6269 weston_presentation_feedback_discard_list(&output->feedback_list);
6270
Pekka Paalanen9711fd92018-06-21 14:26:18 +03006271 weston_compositor_reflow_outputs(compositor, output, -output->width);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006272
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006273 wl_list_remove(&output->link);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006274 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006275 output->enabled = false;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006276
Pekka Paalanenbccda712017-03-29 16:16:04 +03006277 wl_signal_emit(&compositor->output_destroyed_signal, output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006278 wl_signal_emit(&output->destroy_signal, output);
6279
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02006280 wl_list_for_each(head, &output->head_list, output_link)
6281 weston_head_remove_global(head);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03006282
6283 compositor->output_id_pool &= ~(1u << output->id);
6284 output->id = 0xffffffff; /* invalid */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006285}
6286
6287/** Sets the output scale for a given output.
6288 *
6289 * \param output The weston_output object that the scale is set for.
6290 * \param scale Scale factor for the given output.
6291 *
6292 * It only supports setting scale for an output that
6293 * is not enabled and it can only be ran once.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006294 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05006295 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006296 */
6297WL_EXPORT void
6298weston_output_set_scale(struct weston_output *output,
6299 int32_t scale)
6300{
6301 /* We can only set scale on a disabled output */
6302 assert(!output->enabled);
6303
6304 /* We only want to set scale once */
6305 assert(!output->scale);
6306
6307 output->scale = scale;
6308}
6309
6310/** Sets the output transform for a given output.
6311 *
6312 * \param output The weston_output object that the transform is set for.
6313 * \param transform Transform value for the given output.
6314 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006315 * Refer to wl_output::transform section located at
6316 * https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_output
6317 * for list of values that can be passed to this function.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006318 *
Marius Vlad55d87362019-06-11 01:15:35 +03006319 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006320 */
6321WL_EXPORT void
6322weston_output_set_transform(struct weston_output *output,
6323 uint32_t transform)
6324{
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006325 struct weston_pointer_motion_event ev;
6326 struct wl_resource *resource;
6327 struct weston_seat *seat;
6328 pixman_region32_t old_region;
6329 int mid_x, mid_y;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006330 struct weston_head *head;
6331 int ver;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006332
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006333 if (!output->enabled && output->transform == UINT32_MAX) {
6334 output->transform = transform;
6335 return;
6336 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006337
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006338 weston_output_transform_scale_init(output, transform, output->scale);
6339
6340 pixman_region32_init(&old_region);
6341 pixman_region32_copy(&old_region, &output->region);
6342
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006343 weston_output_init_geometry(output, output->x, output->y);
6344
6345 output->dirty = 1;
6346
6347 /* Notify clients of the change for output transform. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006348 wl_list_for_each(head, &output->head_list, output_link) {
6349 wl_resource_for_each(resource, &head->resource_list) {
6350 wl_output_send_geometry(resource,
6351 output->x,
6352 output->y,
6353 head->mm_width,
6354 head->mm_height,
6355 head->subpixel,
6356 head->make,
6357 head->model,
6358 output->transform);
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006359
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006360 ver = wl_resource_get_version(resource);
6361 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
6362 wl_output_send_done(resource);
6363 }
Roman Gilge97391c2019-03-29 13:01:06 +01006364 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
6365 zxdg_output_v1_send_logical_position(resource,
6366 output->x,
6367 output->y);
6368 zxdg_output_v1_send_logical_size(resource,
6369 output->width,
6370 output->height);
6371 zxdg_output_v1_send_done(resource);
6372 }
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006373 }
6374
6375 /* we must ensure that pointers are inside output, otherwise they disappear */
6376 mid_x = output->x + output->width / 2;
6377 mid_y = output->y + output->height / 2;
6378
6379 ev.mask = WESTON_POINTER_MOTION_ABS;
6380 ev.x = wl_fixed_to_double(wl_fixed_from_int(mid_x));
6381 ev.y = wl_fixed_to_double(wl_fixed_from_int(mid_y));
6382
6383 wl_list_for_each(seat, &output->compositor->seat_list, link) {
6384 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
6385
6386 if (pointer && pixman_region32_contains_point(&old_region,
6387 wl_fixed_to_int(pointer->x),
6388 wl_fixed_to_int(pointer->y),
6389 NULL))
6390 weston_pointer_move(pointer, &ev);
6391 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006392}
6393
Pekka Paalanenb5265af2020-12-02 11:57:58 +02006394/** Make the output use renderer shadow buffer.
6395 *
6396 * \param output The weston_output object to modify.
6397 * \return True on success, false if unsupported.
6398 *
6399 * This can only be set on a disabled output object.
6400 *
6401 * This is a temporary API to demonstrate WESTON_CAP_COLOR_OPS and allow
6402 * testing related features. This will be superseded with color management
6403 * API.
6404 *
6405 * By default, a renderer is not using a shadow buffer of its own. Enabling
6406 * a shadow buffer may enable other color related features.
6407 *
6408 * Support depends on the chosen renderer and the graphics driver stack in use.
6409 *
6410 * \ingroup output
6411 */
6412WL_EXPORT bool
6413weston_output_set_renderer_shadow_buffer(struct weston_output *output)
6414{
6415 struct weston_compositor *compositor = output->compositor;
6416
6417 assert(!output->enabled);
6418
6419 if (compositor->capabilities & WESTON_CAP_COLOR_OPS) {
6420 output->use_renderer_shadow_buffer = true;
6421 return true;
6422 }
6423
6424 return false;
6425}
6426
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006427/** Initializes a weston_output object with enough data so
6428 ** an output can be configured.
6429 *
6430 * \param output The weston_output object to initialize
6431 * \param compositor The compositor instance.
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006432 * \param name Name for the output (the string is copied).
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006433 *
6434 * Sets initial values for fields that are expected to be
6435 * configured either by compositors or backends.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006436 *
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006437 * The name is used in logs, and can be used by compositors as a configuration
6438 * identifier.
6439 *
Marius Vlad55d87362019-06-11 01:15:35 +03006440 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006441 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006442 */
6443WL_EXPORT void
Armin Krezović40087402016-09-30 14:11:12 +02006444weston_output_init(struct weston_output *output,
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006445 struct weston_compositor *compositor,
6446 const char *name)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006447{
6448 output->compositor = compositor;
6449 output->destroying = 0;
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006450 output->name = strdup(name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006451 wl_list_init(&output->link);
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006452 wl_signal_init(&output->user_destroy_signal);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006453 output->enabled = false;
Ankit Nautiyal2690a772019-03-25 17:57:59 +05306454 output->desired_protection = WESTON_HDCP_DISABLE;
Ankit Nautiyal2844f8e2019-04-03 10:14:59 +05306455 output->allow_protection = true;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006456
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006457 wl_list_init(&output->head_list);
6458
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006459 /* Add some (in)sane defaults which can be used
6460 * for checking if an output was properly configured
6461 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006462 output->scale = 0;
6463 /* Can't use -1 on uint32_t and 0 is valid enum value */
6464 output->transform = UINT32_MAX;
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006465
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006466 pixman_region32_init(&output->region);
Pekka Paalanen42704142017-09-06 16:47:52 +03006467 wl_list_init(&output->mode_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006468}
6469
6470/** Adds weston_output object to pending output list.
6471 *
6472 * \param output The weston_output object to add
6473 * \param compositor The compositor instance.
6474 *
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03006475 * The opposite of this operation is built into weston_output_release().
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006476 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006477 * \ingroup compositor
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006478 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006479 */
6480WL_EXPORT void
6481weston_compositor_add_pending_output(struct weston_output *output,
6482 struct weston_compositor *compositor)
6483{
Pekka Paalanene952a012017-03-29 17:14:00 +03006484 assert(output->disable);
6485 assert(output->enable);
6486
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006487 wl_list_remove(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006488 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006489}
6490
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006491/** Create a string with the attached heads' names.
6492 *
6493 * The string must be free()'d.
Marius Vlad55d87362019-06-11 01:15:35 +03006494 *
6495 * \ingroup output
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006496 */
6497static char *
6498weston_output_create_heads_string(struct weston_output *output)
6499{
6500 FILE *fp;
6501 char *str = NULL;
6502 size_t size = 0;
6503 struct weston_head *head;
6504 const char *sep = "";
6505
6506 fp = open_memstream(&str, &size);
6507 if (!fp)
6508 return NULL;
6509
6510 wl_list_for_each(head, &output->head_list, output_link) {
6511 fprintf(fp, "%s%s", sep, head->name);
6512 sep = ", ";
6513 }
6514 fclose(fp);
6515
6516 return str;
6517}
6518
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006519/** Constructs a weston_output object that can be used by the compositor.
6520 *
Pekka Paalanencc201e42017-03-30 15:11:25 +03006521 * \param output The weston_output object that needs to be enabled. Must not
Pekka Paalanenddce54d2017-08-23 16:00:21 +03006522 * be enabled already. Must have at least one head attached.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006523 *
6524 * Output coordinates are calculated and each new output is by default
6525 * assigned to the right of previous one.
6526 *
6527 * Sets up the transformation, zoom, and geometry of the output using
6528 * the properties that need to be configured by the compositor.
6529 *
6530 * Establishes a repaint timer for the output with the relevant display
6531 * object's event loop. See output_repaint_timer_handler().
6532 *
6533 * The output is assigned an ID. Weston can support up to 32 distinct
6534 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
6535 * is referred to and used to find the first available ID number, and
6536 * then this ID is marked as used in output_id_pool.
6537 *
6538 * The output is also assigned a Wayland global with the wl_output
6539 * external interface.
6540 *
6541 * Backend specific function is called to set up the output output.
6542 *
6543 * Output is added to the compositor's output list
6544 *
6545 * If the backend specific function fails, the weston_output object
6546 * is returned to a state it was before calling this function and
6547 * is added to the compositor's pending_output_list in case it needs
6548 * to be reconfigured or just so it can be destroyed at shutdown.
6549 *
6550 * 0 is returned on success, -1 on failure.
Marius Vlad55d87362019-06-11 01:15:35 +03006551 *
6552 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006553 */
6554WL_EXPORT int
6555weston_output_enable(struct weston_output *output)
6556{
Armin Krezović782f5df2016-09-30 14:11:11 +02006557 struct weston_compositor *c = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006558 struct weston_output *iterator;
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03006559 struct weston_head *head;
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006560 char *head_names;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006561 int x = 0, y = 0;
6562
Pekka Paalanencc201e42017-03-30 15:11:25 +03006563 if (output->enabled) {
6564 weston_log("Error: attempt to enable an enabled output '%s'\n",
6565 output->name);
6566 return -1;
6567 }
6568
Pekka Paalanenddce54d2017-08-23 16:00:21 +03006569 if (wl_list_empty(&output->head_list)) {
6570 weston_log("Error: cannot enable output '%s' without heads.\n",
6571 output->name);
6572 return -1;
6573 }
6574
Pekka Paalanen586e1ac2017-09-14 16:17:59 +03006575 if (wl_list_empty(&output->mode_list) || !output->current_mode) {
6576 weston_log("Error: no video mode for output '%s'.\n",
6577 output->name);
6578 return -1;
6579 }
6580
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03006581 wl_list_for_each(head, &output->head_list, output_link) {
6582 assert(head->make);
6583 assert(head->model);
6584 }
6585
Armin Krezović782f5df2016-09-30 14:11:11 +02006586 iterator = container_of(c->output_list.prev,
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006587 struct weston_output, link);
6588
Armin Krezović782f5df2016-09-30 14:11:11 +02006589 if (!wl_list_empty(&c->output_list))
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006590 x = iterator->x + iterator->width;
6591
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006592 /* Make sure the scale is set up */
6593 assert(output->scale);
6594
6595 /* Make sure we have a transform set */
6596 assert(output->transform != UINT32_MAX);
6597
Armin Krezović782f5df2016-09-30 14:11:11 +02006598 output->x = x;
6599 output->y = y;
6600 output->dirty = 1;
6601 output->original_scale = output->scale;
6602
Marius Vlad3a2f8292019-11-04 17:53:46 +02006603 wl_signal_init(&output->frame_signal);
6604 wl_signal_init(&output->destroy_signal);
6605
Armin Krezović782f5df2016-09-30 14:11:11 +02006606 weston_output_transform_scale_init(output, output->transform, output->scale);
6607 weston_output_init_zoom(output);
6608
6609 weston_output_init_geometry(output, x, y);
6610 weston_output_damage(output);
6611
Armin Krezović782f5df2016-09-30 14:11:11 +02006612 wl_list_init(&output->animation_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02006613 wl_list_init(&output->feedback_list);
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03006614 wl_list_init(&output->paint_node_list);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03006615 wl_list_init(&output->paint_node_z_order_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02006616
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006617 /* Enable the output (set up the crtc or create a
6618 * window representing the output, set up the
6619 * renderer, etc)
6620 */
6621 if (output->enable(output) < 0) {
6622 weston_log("Enabling output \"%s\" failed.\n", output->name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006623 return -1;
6624 }
6625
6626 weston_compositor_add_output(output->compositor, output);
6627
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006628 head_names = weston_output_create_heads_string(output);
6629 weston_log("Output '%s' enabled with head(s) %s\n",
6630 output->name, head_names);
6631 free(head_names);
6632
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006633 return 0;
6634}
6635
6636/** Converts a weston_output object to a pending output state, so it
6637 ** can be configured again or destroyed.
6638 *
6639 * \param output The weston_output object that needs to be disabled.
6640 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006641 * Calls a backend specific function to disable an output, in case
6642 * such function exists.
6643 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03006644 * The backend specific disable function may choose to postpone the disabling
6645 * by returning a negative value, in which case this function returns early.
6646 * In that case the backend will guarantee the output will be disabled soon
6647 * by the backend calling this function again. One must not attempt to re-enable
6648 * the output until that happens.
6649 *
6650 * Otherwise, if the output is being used by the compositor, it is removed
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006651 * from weston's output_list (see weston_compositor_remove_output())
6652 * and is returned to a state it was before weston_output_enable()
6653 * was ran (see weston_output_enable_undo()).
6654 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03006655 * See weston_output_init() for more information on the
6656 * state output is returned to.
Pekka Paalanencc201e42017-03-30 15:11:25 +03006657 *
6658 * If the output has never been enabled yet, this function can still be
6659 * called to ensure that the output is actually turned off rather than left
6660 * in the state it was discovered in.
Marius Vlad55d87362019-06-11 01:15:35 +03006661 *
6662 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006663 */
6664WL_EXPORT void
6665weston_output_disable(struct weston_output *output)
6666{
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006667 /* Should we rename this? */
6668 output->destroying = 1;
6669
Pekka Paalanenc65df642017-03-29 15:45:46 +03006670 /* Disable is called unconditionally also for not-enabled outputs,
6671 * because at compositor start-up, if there is an output that is
6672 * already on but the compositor wants to turn it off, we have to
6673 * forward the turn-off to the backend so it knows to do it.
6674 * The backend cannot initially turn off everything, because it
6675 * would cause unnecessary mode-sets for all outputs the compositor
6676 * wants to be on.
6677 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006678 if (output->disable(output) < 0)
6679 return;
6680
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006681 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006682 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006683
6684 output->destroying = 0;
6685}
6686
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006687/** Forces a synchronous call to heads_changed hook
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006688 *
6689 * \param compositor The compositor instance
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006690 *
6691 * If there are new or changed heads, calls the heads_changed hook and
6692 * returns after the hook returns.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006693 *
6694 * \ingroup compositor
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006695 */
6696WL_EXPORT void
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006697weston_compositor_flush_heads_changed(struct weston_compositor *compositor)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006698{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03006699 if (compositor->heads_changed_source) {
6700 wl_event_source_remove(compositor->heads_changed_source);
6701 weston_compositor_call_heads_changed(compositor);
6702 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006703}
6704
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006705/** Add destroy callback for an output
6706 *
6707 * \param output The output to watch.
6708 * \param listener The listener to add. The \c notify member must be set.
6709 *
6710 * The listener callback will be called when user destroys an output. This
6711 * may be delayed by a backend in some cases. The main purpose of the
6712 * listener is to allow hooking up custom data to the output. The custom data
6713 * can be fetched via weston_output_get_destroy_listener() followed by
6714 * container_of().
6715 *
6716 * The \c data argument to the notify callback is the weston_output being
6717 * destroyed.
6718 *
6719 * @note This is for the final destruction of an output, not when it gets
6720 * disabled. If you want to keep track of enabled outputs, this is not it.
Marius Vlad55d87362019-06-11 01:15:35 +03006721 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05006722 * \ingroup output
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006723 */
6724WL_EXPORT void
6725weston_output_add_destroy_listener(struct weston_output *output,
6726 struct wl_listener *listener)
6727{
6728 wl_signal_add(&output->user_destroy_signal, listener);
6729}
6730
6731/** Look up destroy listener for an output
6732 *
6733 * \param output The output to query.
6734 * \param notify The notify function used used for the added destroy listener.
6735 * \return The listener, or NULL if not found.
6736 *
6737 * This looks up the previously added destroy listener struct based on the
6738 * notify function it has. The listener can be used to access user data
6739 * through \c container_of().
6740 *
6741 * \sa wl_signal_get() weston_output_add_destroy_listener()
Marius Vlad55d87362019-06-11 01:15:35 +03006742 * \ingroup output
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006743 */
6744WL_EXPORT struct wl_listener *
6745weston_output_get_destroy_listener(struct weston_output *output,
6746 wl_notify_func_t notify)
6747{
6748 return wl_signal_get(&output->user_destroy_signal, notify);
6749}
6750
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006751/** Uninitialize an output
6752 *
6753 * Removes the output from the list of enabled outputs if necessary, but
6754 * does not call the backend's output disable function. The output will no
6755 * longer be in the list of pending outputs either.
6756 *
6757 * All fields of weston_output become uninitialized, i.e. should not be used
6758 * anymore. The caller can free the memory after this.
6759 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05006760 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006761 * \internal
6762 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006763WL_EXPORT void
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03006764weston_output_release(struct weston_output *output)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006765{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006766 struct weston_head *head, *tmp;
6767
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006768 output->destroying = 1;
6769
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006770 wl_signal_emit(&output->user_destroy_signal, output);
6771
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03006772 if (output->idle_repaint_source)
6773 wl_event_source_remove(output->idle_repaint_source);
6774
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006775 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006776 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006777
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03006778 assert(wl_list_empty(&output->paint_node_list));
6779
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006780 pixman_region32_fini(&output->region);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006781 wl_list_remove(&output->link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006782
6783 wl_list_for_each_safe(head, tmp, &output->head_list, output_link)
6784 weston_head_detach(head);
6785
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006786 free(output->name);
6787}
6788
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006789/** Find an output by its given name
6790 *
6791 * \param compositor The compositor to search in.
6792 * \param name The output name to search for.
6793 * \return An existing output with the given name, or NULL if not found.
6794 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006795 * \ingroup compositor
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006796 */
6797WL_EXPORT struct weston_output *
6798weston_compositor_find_output_by_name(struct weston_compositor *compositor,
6799 const char *name)
6800{
6801 struct weston_output *output;
6802
6803 wl_list_for_each(output, &compositor->output_list, link)
6804 if (strcmp(output->name, name) == 0)
6805 return output;
6806
6807 wl_list_for_each(output, &compositor->pending_output_list, link)
6808 if (strcmp(output->name, name) == 0)
6809 return output;
6810
6811 return NULL;
6812}
6813
6814/** Create a named output
6815 *
6816 * \param compositor The compositor.
6817 * \param name The name for the output.
6818 * \return A new \c weston_output, or NULL on failure.
6819 *
6820 * This creates a new weston_output that starts with no heads attached.
6821 *
6822 * An output must be configured and it must have at least one head before
6823 * it can be enabled.
6824 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006825 * \ingroup compositor
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006826 */
6827WL_EXPORT struct weston_output *
6828weston_compositor_create_output(struct weston_compositor *compositor,
6829 const char *name)
6830{
6831 assert(compositor->backend->create_output);
6832
6833 if (weston_compositor_find_output_by_name(compositor, name)) {
6834 weston_log("Warning: attempted to create an output with a "
6835 "duplicate name '%s'.\n", name);
6836 return NULL;
6837 }
6838
6839 return compositor->backend->create_output(compositor, name);
6840}
6841
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006842/** Create an output for an unused head
6843 *
6844 * \param compositor The compositor.
6845 * \param head The head to attach to the output.
6846 * \return A new \c weston_output, or NULL on failure.
6847 *
6848 * This creates a new weston_output that starts with the given head attached.
6849 * The output inherits the name of the head. The head must not be already
6850 * attached to another output.
6851 *
6852 * An output must be configured before it can be enabled.
6853 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006854 * \ingroup compositor
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006855 */
6856WL_EXPORT struct weston_output *
6857weston_compositor_create_output_with_head(struct weston_compositor *compositor,
6858 struct weston_head *head)
6859{
6860 struct weston_output *output;
6861
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006862 output = weston_compositor_create_output(compositor, head->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006863 if (!output)
6864 return NULL;
6865
6866 if (weston_output_attach_head(output, head) < 0) {
Pekka Paalanen42c0e142017-10-27 12:07:49 +03006867 weston_output_destroy(output);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006868 return NULL;
6869 }
6870
6871 return output;
6872}
6873
6874/** Destroy an output
6875 *
6876 * \param output The output to destroy.
6877 *
6878 * The heads attached to the given output are detached and become unused again.
6879 *
6880 * It is not necessary to explicitly destroy all outputs at compositor exit.
Leandro Ribeiroca640d52020-01-27 19:12:01 -03006881 * weston_compositor_destroy() will automatically destroy any remaining
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006882 * outputs.
6883 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05006884 * \ingroup output
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006885 */
6886WL_EXPORT void
6887weston_output_destroy(struct weston_output *output)
6888{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006889 output->destroy(output);
6890}
6891
Pekka Paalanencf0a4762017-04-04 16:36:07 +03006892/** When you need a head...
6893 *
6894 * This function is a hack, used until all code has been converted to become
6895 * multi-head aware.
6896 *
6897 * \param output The weston_output whose head to get.
6898 * \return The first head in the output's list.
Marius Vlad55d87362019-06-11 01:15:35 +03006899 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05006900 * \ingroup output
Pekka Paalanencf0a4762017-04-04 16:36:07 +03006901 */
6902WL_EXPORT struct weston_head *
6903weston_output_get_first_head(struct weston_output *output)
6904{
6905 if (wl_list_empty(&output->head_list))
6906 return NULL;
6907
6908 return container_of(output->head_list.next,
6909 struct weston_head, output_link);
6910}
6911
Ankit Nautiyal2844f8e2019-04-03 10:14:59 +05306912/** Allow/Disallow content-protection support for an output
6913 *
6914 * This function sets the allow_protection member for an output. Setting of
6915 * this field will allow the compositor to attempt content-protection for this
6916 * output, for a backend that supports the content-protection protocol.
6917 *
6918 * \param output The weston_output for whom the content-protection is to be
6919 * allowed.
6920 * \param allow_protection The bool value which is to be set.
6921 */
6922WL_EXPORT void
6923weston_output_allow_protection(struct weston_output *output,
6924 bool allow_protection)
6925{
6926 output->allow_protection = allow_protection;
6927}
6928
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01006929static void
Roman Gilge97391c2019-03-29 13:01:06 +01006930xdg_output_unlist(struct wl_resource *resource)
6931{
6932 wl_list_remove(wl_resource_get_link(resource));
6933}
6934
6935static void
6936xdg_output_destroy(struct wl_client *client, struct wl_resource *resource)
6937{
6938 wl_resource_destroy(resource);
6939}
6940
6941static const struct zxdg_output_v1_interface xdg_output_interface = {
6942 xdg_output_destroy
6943};
6944
6945static void
6946xdg_output_manager_destroy(struct wl_client *client,
6947 struct wl_resource *resource)
6948{
6949 wl_resource_destroy(resource);
6950}
6951
6952static void
6953xdg_output_manager_get_xdg_output(struct wl_client *client,
6954 struct wl_resource *manager,
6955 uint32_t id,
6956 struct wl_resource *output_resource)
6957{
6958 int version = wl_resource_get_version(manager);
6959 struct weston_head *head = wl_resource_get_user_data(output_resource);
6960 struct weston_output *output = head->output;
6961 struct wl_resource *resource;
6962
6963 resource = wl_resource_create(client, &zxdg_output_v1_interface,
6964 version, id);
6965 if (resource == NULL) {
6966 wl_client_post_no_memory(client);
6967 return;
6968 }
6969
6970 wl_list_insert(&head->xdg_output_resource_list,
6971 wl_resource_get_link(resource));
6972
6973 wl_resource_set_implementation(resource, &xdg_output_interface,
6974 NULL, xdg_output_unlist);
6975
6976 zxdg_output_v1_send_logical_position(resource, output->x, output->y);
6977 zxdg_output_v1_send_logical_size(resource,
6978 output->width,
6979 output->height);
6980 if (version >= ZXDG_OUTPUT_V1_NAME_SINCE_VERSION)
6981 zxdg_output_v1_send_name(resource, head->name);
6982
6983 zxdg_output_v1_send_done(resource);
6984}
6985
6986static const struct zxdg_output_manager_v1_interface xdg_output_manager_interface = {
6987 xdg_output_manager_destroy,
6988 xdg_output_manager_get_xdg_output
6989};
6990
6991static void
6992bind_xdg_output_manager(struct wl_client *client,
6993 void *data, uint32_t version, uint32_t id)
6994{
6995 struct wl_resource *resource;
6996
6997 resource = wl_resource_create(client, &zxdg_output_manager_v1_interface,
6998 version, id);
6999 if (resource == NULL) {
7000 wl_client_post_no_memory(client);
7001 return;
7002 }
7003
7004 wl_resource_set_implementation(resource, &xdg_output_manager_interface,
7005 NULL, NULL);
7006}
7007
7008static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007009destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01007010{
Jonny Lamb74130762013-11-26 18:19:46 +01007011 struct weston_surface *surface =
7012 wl_resource_get_user_data(resource);
7013
Pekka Paalanen4826f872016-04-22 14:14:38 +03007014 if (!surface)
7015 return;
7016
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007017 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02007018 surface->pending.buffer_viewport.buffer.src_width =
7019 wl_fixed_from_int(-1);
7020 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02007021 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01007022}
7023
7024static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007025viewport_destroy(struct wl_client *client,
7026 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01007027{
7028 wl_resource_destroy(resource);
7029}
7030
7031static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007032viewport_set_source(struct wl_client *client,
7033 struct wl_resource *resource,
7034 wl_fixed_t src_x,
7035 wl_fixed_t src_y,
7036 wl_fixed_t src_width,
7037 wl_fixed_t src_height)
7038{
7039 struct weston_surface *surface =
7040 wl_resource_get_user_data(resource);
7041
Pekka Paalanen4826f872016-04-22 14:14:38 +03007042 if (!surface) {
7043 wl_resource_post_error(resource,
7044 WP_VIEWPORT_ERROR_NO_SURFACE,
7045 "wl_surface for this viewport is no longer exists");
7046 return;
7047 }
7048
7049 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03007050 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007051
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007052 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03007053 src_height == wl_fixed_from_int(-1) &&
7054 src_x == wl_fixed_from_int(-1) &&
7055 src_y == wl_fixed_from_int(-1)) {
7056 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007057 surface->pending.buffer_viewport.buffer.src_width =
7058 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02007059 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007060 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007061 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007062
Pekka Paalanen201769a2016-04-26 14:42:11 +03007063 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007064 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007065 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03007066 "wl_surface@%d viewport source "
7067 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
7068 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007069 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03007070 wl_fixed_to_double(src_height),
7071 wl_fixed_to_double(src_x),
7072 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007073 return;
7074 }
7075
7076 surface->pending.buffer_viewport.buffer.src_x = src_x;
7077 surface->pending.buffer_viewport.buffer.src_y = src_y;
7078 surface->pending.buffer_viewport.buffer.src_width = src_width;
7079 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02007080 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007081}
7082
7083static void
7084viewport_set_destination(struct wl_client *client,
7085 struct wl_resource *resource,
7086 int32_t dst_width,
7087 int32_t dst_height)
7088{
7089 struct weston_surface *surface =
7090 wl_resource_get_user_data(resource);
7091
Pekka Paalanen4826f872016-04-22 14:14:38 +03007092 if (!surface) {
7093 wl_resource_post_error(resource,
7094 WP_VIEWPORT_ERROR_NO_SURFACE,
7095 "wl_surface for this viewport no longer exists");
7096 return;
7097 }
7098
7099 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007100
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007101 if (dst_width == -1 && dst_height == -1) {
7102 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007103 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02007104 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007105 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007106 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007107
7108 if (dst_width <= 0 || dst_height <= 0) {
7109 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007110 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007111 "destination size must be positive (%dx%d)",
7112 dst_width, dst_height);
7113 return;
7114 }
7115
7116 surface->pending.buffer_viewport.surface.width = dst_width;
7117 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02007118 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007119}
7120
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007121static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007122 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007123 viewport_set_source,
7124 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01007125};
7126
7127static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007128viewporter_destroy(struct wl_client *client,
7129 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01007130{
7131 wl_resource_destroy(resource);
7132}
7133
7134static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007135viewporter_get_viewport(struct wl_client *client,
7136 struct wl_resource *viewporter,
7137 uint32_t id,
7138 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01007139{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007140 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007141 struct weston_surface *surface =
7142 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01007143 struct wl_resource *resource;
7144
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007145 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007146 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007147 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007148 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01007149 return;
7150 }
7151
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007152 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007153 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01007154 if (resource == NULL) {
7155 wl_client_post_no_memory(client);
7156 return;
7157 }
7158
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007159 wl_resource_set_implementation(resource, &viewport_interface,
7160 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01007161
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007162 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01007163}
7164
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007165static const struct wp_viewporter_interface viewporter_interface = {
7166 viewporter_destroy,
7167 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01007168};
7169
7170static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007171bind_viewporter(struct wl_client *client,
7172 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01007173{
7174 struct wl_resource *resource;
7175
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007176 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06007177 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01007178 if (resource == NULL) {
7179 wl_client_post_no_memory(client);
7180 return;
7181 }
7182
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007183 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01007184 NULL, NULL);
7185}
7186
7187static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04007188destroy_presentation_feedback(struct wl_resource *feedback_resource)
7189{
7190 struct weston_presentation_feedback *feedback;
7191
7192 feedback = wl_resource_get_user_data(feedback_resource);
7193
7194 wl_list_remove(&feedback->link);
7195 free(feedback);
7196}
7197
7198static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007199presentation_destroy(struct wl_client *client, struct wl_resource *resource)
7200{
7201 wl_resource_destroy(resource);
7202}
7203
7204static void
7205presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04007206 struct wl_resource *presentation_resource,
7207 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007208 uint32_t callback)
7209{
Pekka Paalanen133e4392014-09-23 22:08:46 -04007210 struct weston_surface *surface;
7211 struct weston_presentation_feedback *feedback;
7212
7213 surface = wl_resource_get_user_data(surface_resource);
7214
Bryce Harringtonde16d892014-11-20 22:21:57 -08007215 feedback = zalloc(sizeof *feedback);
7216 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04007217 goto err_calloc;
7218
7219 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007220 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04007221 1, callback);
7222 if (!feedback->resource)
7223 goto err_create;
7224
7225 wl_resource_set_implementation(feedback->resource, NULL, feedback,
7226 destroy_presentation_feedback);
7227
7228 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
7229
7230 return;
7231
7232err_create:
7233 free(feedback);
7234
7235err_calloc:
7236 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007237}
7238
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007239static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007240 presentation_destroy,
7241 presentation_feedback
7242};
7243
7244static void
7245bind_presentation(struct wl_client *client,
7246 void *data, uint32_t version, uint32_t id)
7247{
7248 struct weston_compositor *compositor = data;
7249 struct wl_resource *resource;
7250
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007251 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06007252 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007253 if (resource == NULL) {
7254 wl_client_post_no_memory(client);
7255 return;
7256 }
7257
7258 wl_resource_set_implementation(resource, &presentation_implementation,
7259 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007260 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007261}
7262
7263static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05007264compositor_bind(struct wl_client *client,
7265 void *data, uint32_t version, uint32_t id)
7266{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007267 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05007268 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05007269
Jason Ekstranda85118c2013-06-27 20:17:02 -05007270 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06007271 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07007272 if (resource == NULL) {
7273 wl_client_post_no_memory(client);
7274 return;
7275 }
7276
7277 wl_resource_set_implementation(resource, &compositor_interface,
7278 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05007279}
7280
Daniel Stonece62cb32018-07-20 09:46:24 +01007281static const char *
7282output_repaint_status_text(struct weston_output *output)
7283{
7284 switch (output->repaint_status) {
7285 case REPAINT_NOT_SCHEDULED:
7286 return "no repaint";
7287 case REPAINT_BEGIN_FROM_IDLE:
7288 return "start_repaint_loop scheduled";
7289 case REPAINT_SCHEDULED:
7290 return "repaint scheduled";
7291 case REPAINT_AWAITING_COMPLETION:
7292 return "awaiting completion";
7293 }
7294
7295 assert(!"output_repaint_status_text missing enum");
7296 return NULL;
7297}
7298
7299static void
7300debug_scene_view_print_buffer(FILE *fp, struct weston_view *view)
7301{
7302 struct weston_buffer *buffer = view->surface->buffer_ref.buffer;
7303 struct wl_shm_buffer *shm;
7304 struct linux_dmabuf_buffer *dmabuf;
Marius Vlad00a6e012018-11-20 17:52:31 +02007305 const struct pixel_format_info *pixel_info = NULL;
Daniel Stonece62cb32018-07-20 09:46:24 +01007306
7307 if (!buffer) {
7308 fprintf(fp, "\t\t[buffer not available]\n");
7309 return;
7310 }
7311
7312 shm = wl_shm_buffer_get(buffer->resource);
7313 if (shm) {
Marius Vlad00a6e012018-11-20 17:52:31 +02007314 uint32_t _format = wl_shm_buffer_get_format(shm);
7315 pixel_info = pixel_format_get_info_shm(_format);
Daniel Stonece62cb32018-07-20 09:46:24 +01007316 fprintf(fp, "\t\tSHM buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02007317 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
7318 (unsigned long) _format,
7319 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Daniel Stonece62cb32018-07-20 09:46:24 +01007320 return;
7321 }
7322
7323 dmabuf = linux_dmabuf_buffer_get(buffer->resource);
7324 if (dmabuf) {
Marius Vlad00a6e012018-11-20 17:52:31 +02007325 pixel_info = pixel_format_get_info(dmabuf->attributes.format);
Daniel Stonece62cb32018-07-20 09:46:24 +01007326 fprintf(fp, "\t\tdmabuf buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02007327 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
7328 (unsigned long) dmabuf->attributes.format,
7329 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Daniel Stonece62cb32018-07-20 09:46:24 +01007330 fprintf(fp, "\t\t\tmodifier: 0x%llx\n",
7331 (unsigned long long) dmabuf->attributes.modifier[0]);
7332 return;
7333 }
7334
Marius Vlad253ba9a2019-03-17 18:22:21 +02007335 fprintf(fp, "\t\tEGL buffer\n");
Daniel Stonece62cb32018-07-20 09:46:24 +01007336}
7337
7338static void
7339debug_scene_view_print(FILE *fp, struct weston_view *view, int view_idx)
7340{
7341 struct weston_compositor *ec = view->surface->compositor;
7342 struct weston_output *output;
7343 char desc[512];
7344 pixman_box32_t *box;
7345 uint32_t surface_id = 0;
7346 pid_t pid = 0;
7347
7348 if (view->surface->resource) {
7349 struct wl_resource *resource = view->surface->resource;
7350 wl_client_get_credentials(wl_resource_get_client(resource),
7351 &pid, NULL, NULL);
7352 surface_id = wl_resource_get_id(view->surface->resource);
7353 }
7354
7355 if (!view->surface->get_label ||
7356 view->surface->get_label(view->surface, desc, sizeof(desc)) < 0) {
7357 strcpy(desc, "[no description available]");
7358 }
7359 fprintf(fp, "\tView %d (role %s, PID %d, surface ID %u, %s, %p):\n",
7360 view_idx, view->surface->role_name, pid, surface_id,
7361 desc, view);
7362
7363 box = pixman_region32_extents(&view->transform.boundingbox);
7364 fprintf(fp, "\t\tposition: (%d, %d) -> (%d, %d)\n",
7365 box->x1, box->y1, box->x2, box->y2);
7366 box = pixman_region32_extents(&view->transform.opaque);
7367
Michael Olbrichb7e5f102020-08-11 16:33:35 +02007368 if (weston_view_is_opaque(view, &view->transform.boundingbox)) {
Daniel Stonece62cb32018-07-20 09:46:24 +01007369 fprintf(fp, "\t\t[fully opaque]\n");
7370 } else if (!pixman_region32_not_empty(&view->transform.opaque)) {
7371 fprintf(fp, "\t\t[not opaque]\n");
7372 } else {
7373 fprintf(fp, "\t\t[opaque: (%d, %d) -> (%d, %d)]\n",
7374 box->x1, box->y1, box->x2, box->y2);
7375 }
7376
7377 if (view->alpha < 1.0)
7378 fprintf(fp, "\t\talpha: %f\n", view->alpha);
7379
7380 if (view->output_mask != 0) {
7381 bool first_output = true;
7382 fprintf(fp, "\t\toutputs: ");
7383 wl_list_for_each(output, &ec->output_list, link) {
7384 if (!(view->output_mask & (1 << output->id)))
7385 continue;
7386 fprintf(fp, "%s%d (%s)%s",
7387 (first_output) ? "" : ", ",
7388 output->id, output->name,
7389 (view->output == output) ? " (primary)" : "");
7390 first_output = false;
7391 }
7392 } else {
7393 fprintf(fp, "\t\t[no outputs]");
7394 }
7395
7396 fprintf(fp, "\n");
7397
7398 debug_scene_view_print_buffer(fp, view);
7399}
7400
Marius Vlad433f4e72019-02-17 22:14:23 +02007401static void
7402debug_scene_view_print_tree(struct weston_view *view,
Marius Vlada6acfa82019-03-17 18:10:09 +02007403 FILE *fp, int *view_idx)
Marius Vlad433f4e72019-02-17 22:14:23 +02007404{
7405 struct weston_subsurface *sub;
7406 struct weston_view *ev;
7407
7408 /*
7409 * print the view first, then we recursively go on printing
7410 * sub-surfaces. We bail out once no more sub-surfaces are available.
7411 */
Marius Vlada6acfa82019-03-17 18:10:09 +02007412 debug_scene_view_print(fp, view, *view_idx);
Marius Vlad433f4e72019-02-17 22:14:23 +02007413
7414 /* no more sub-surfaces */
7415 if (wl_list_empty(&view->surface->subsurface_list))
7416 return;
7417
7418 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
7419 wl_list_for_each(ev, &sub->surface->views, surface_link) {
Michael Olbrichef5f3232020-04-29 09:03:15 +02007420 /* only print the child views of the current view */
7421 if (ev->parent_view != view)
Marius Vlad433f4e72019-02-17 22:14:23 +02007422 continue;
Marius Vlada6acfa82019-03-17 18:10:09 +02007423
7424 (*view_idx)++;
Marius Vlad433f4e72019-02-17 22:14:23 +02007425 debug_scene_view_print_tree(ev, fp, view_idx);
7426 }
7427 }
7428}
7429
Daniel Stonece62cb32018-07-20 09:46:24 +01007430/**
7431 * Output information on how libweston is currently composing the scene
7432 * graph.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007433 *
7434 * \ingroup compositor
Daniel Stonece62cb32018-07-20 09:46:24 +01007435 */
7436WL_EXPORT char *
7437weston_compositor_print_scene_graph(struct weston_compositor *ec)
7438{
7439 struct weston_output *output;
7440 struct weston_layer *layer;
7441 struct timespec now;
7442 int layer_idx = 0;
7443 FILE *fp;
7444 char *ret;
7445 size_t len;
7446 int err;
7447
7448 fp = open_memstream(&ret, &len);
7449 assert(fp);
7450
7451 weston_compositor_read_presentation_clock(ec, &now);
7452 fprintf(fp, "Weston scene graph at %ld.%09ld:\n\n",
7453 now.tv_sec, now.tv_nsec);
7454
7455 wl_list_for_each(output, &ec->output_list, link) {
7456 struct weston_head *head;
7457 int head_idx = 0;
7458
7459 fprintf(fp, "Output %d (%s):\n", output->id, output->name);
7460 assert(output->enabled);
7461
7462 fprintf(fp, "\tposition: (%d, %d) -> (%d, %d)\n",
7463 output->x, output->y,
7464 output->x + output->width,
7465 output->y + output->height);
7466 fprintf(fp, "\tmode: %dx%d@%.3fHz\n",
7467 output->current_mode->width,
7468 output->current_mode->height,
7469 output->current_mode->refresh / 1000.0);
7470 fprintf(fp, "\tscale: %d\n", output->scale);
7471
7472 fprintf(fp, "\trepaint status: %s\n",
7473 output_repaint_status_text(output));
7474 if (output->repaint_status == REPAINT_SCHEDULED)
7475 fprintf(fp, "\tnext repaint: %ld.%09ld\n",
7476 output->next_repaint.tv_sec,
7477 output->next_repaint.tv_nsec);
7478
7479 wl_list_for_each(head, &output->head_list, output_link) {
7480 fprintf(fp, "\tHead %d (%s): %sconnected\n",
7481 head_idx++, head->name,
7482 (head->connected) ? "" : "not ");
7483 }
7484 }
7485
7486 fprintf(fp, "\n");
7487
7488 wl_list_for_each(layer, &ec->layer_list, link) {
7489 struct weston_view *view;
7490 int view_idx = 0;
7491
7492 fprintf(fp, "Layer %d (pos 0x%lx):\n", layer_idx++,
7493 (unsigned long) layer->position);
7494
7495 if (!weston_layer_mask_is_infinite(layer)) {
7496 fprintf(fp, "\t[mask: (%d, %d) -> (%d,%d)]\n\n",
7497 layer->mask.x1, layer->mask.y1,
7498 layer->mask.x2, layer->mask.y2);
7499 }
7500
Marius Vlada6acfa82019-03-17 18:10:09 +02007501 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
7502 debug_scene_view_print_tree(view, fp, &view_idx);
7503 view_idx++;
7504 }
Daniel Stonece62cb32018-07-20 09:46:24 +01007505
7506 if (wl_list_empty(&layer->view_list.link))
7507 fprintf(fp, "\t[no views]\n");
7508
7509 fprintf(fp, "\n");
7510 }
7511
7512 err = fclose(fp);
7513 assert(err == 0);
7514
7515 return ret;
7516}
7517
7518/**
7519 * Called when the 'scene-graph' debug scope is bound by a client. This
7520 * one-shot weston-debug scope prints the current scene graph when bound,
7521 * and then terminates the stream.
7522 */
7523static void
Marius Vladdad882a2019-07-17 15:43:53 +03007524debug_scene_graph_cb(struct weston_log_subscription *sub, void *data)
Daniel Stonece62cb32018-07-20 09:46:24 +01007525{
7526 struct weston_compositor *ec = data;
7527 char *str = weston_compositor_print_scene_graph(ec);
7528
Marius Vladdad882a2019-07-17 15:43:53 +03007529 weston_log_subscription_printf(sub, "%s", str);
Daniel Stonece62cb32018-07-20 09:46:24 +01007530 free(str);
Marius Vladdad882a2019-07-17 15:43:53 +03007531 weston_log_subscription_complete(sub);
Daniel Stonece62cb32018-07-20 09:46:24 +01007532}
7533
Leandro Ribeiro32a5acd2020-10-19 16:06:22 -03007534/** Retrieve testsuite data from compositor
7535 *
7536 * The testsuite data can be defined by the test suite of projects that uses
7537 * libweston and given to the compositor at the moment of its creation. This
7538 * function should be used when we need to retrieve the testsuite private data
7539 * from the compositor.
7540 *
7541 * \param ec The weston compositor.
7542 * \return The testsuite data.
7543 *
7544 * \ingroup compositor
7545 * \sa weston_compositor_test_data_init
7546 */
7547WL_EXPORT void *
7548weston_compositor_get_test_data(struct weston_compositor *ec)
7549{
7550 return ec->test_data.test_private_data;
7551}
7552
Giulio Camuffo459137b2014-10-11 23:56:24 +03007553/** Create the compositor.
7554 *
7555 * This functions creates and initializes a compositor instance.
7556 *
7557 * \param display The Wayland display to be used.
7558 * \param user_data A pointer to an object that can later be retrieved
Marius Vlada2dace22019-06-12 16:05:44 +03007559 * \param log_ctx A pointer to weston_debug_compositor
Leandro Ribeirocdb72182020-11-06 11:24:22 -03007560 * \param test_data Optional testsuite data, or NULL.
Giulio Camuffo459137b2014-10-11 23:56:24 +03007561 * using the \ref weston_compositor_get_user_data function.
7562 * \return The compositor instance on success or NULL on failure.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007563 *
7564 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007565 */
7566WL_EXPORT struct weston_compositor *
Marius Vlad880b4852019-04-07 17:07:58 +03007567weston_compositor_create(struct wl_display *display,
Leandro Ribeirocdb72182020-11-06 11:24:22 -03007568 struct weston_log_context *log_ctx, void *user_data,
7569 const struct weston_testsuite_data *test_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04007570{
Giulio Camuffo459137b2014-10-11 23:56:24 +03007571 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05007572 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07007573
Leandro Ribeirobd9c0a62020-01-17 10:47:49 -03007574 if (!log_ctx)
7575 return NULL;
7576
Giulio Camuffo459137b2014-10-11 23:56:24 +03007577 ec = zalloc(sizeof *ec);
7578 if (!ec)
7579 return NULL;
7580
Leandro Ribeirocdb72182020-11-06 11:24:22 -03007581 if (test_data)
7582 ec->test_data = *test_data;
7583
Leandro Ribeirobd9c0a62020-01-17 10:47:49 -03007584 ec->weston_log_ctx = log_ctx;
Giulio Camuffo459137b2014-10-11 23:56:24 +03007585 ec->wl_display = display;
7586 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04007587 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07007588 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04007589 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03007590 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03007591 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02007592 wl_signal_init(&ec->idle_signal);
7593 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02007594 wl_signal_init(&ec->show_input_panel_signal);
7595 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02007596 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01007597 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01007598 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02007599 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02007600 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02007601 wl_signal_init(&ec->output_resized_signal);
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03007602 wl_signal_init(&ec->heads_changed_signal);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02007603 wl_signal_init(&ec->output_heads_changed_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07007604 wl_signal_init(&ec->session_signal);
Robert Beckettc569bdc2019-07-10 16:40:12 +01007605 ec->session_active = true;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04007606
Casey Dahlin58ba1372012-04-19 22:50:08 -04007607 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03007608 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04007609
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02007610 ec->activate_serial = 1;
7611
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05007612 ec->touch_mode = WESTON_TOUCH_MODE_NORMAL;
7613
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05307614 ec->content_protection = NULL;
7615
Derek Foreman152254b2015-11-26 14:17:48 -06007616 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04007617 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007618 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05007619
Giulio Camuffo954f1832014-10-11 18:27:30 +03007620 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04007621 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007622 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03007623
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007624 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007625 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007626 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01007627
Roman Gilge97391c2019-03-29 13:01:06 +01007628 if (!wl_global_create(ec->wl_display, &zxdg_output_manager_v1_interface, 2,
7629 ec, bind_xdg_output_manager))
7630 goto fail;
7631
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007632 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007633 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007634 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007635
Jonas Ådahl30d61d82014-10-22 21:21:17 +02007636 if (weston_input_init(ec) != 0)
7637 goto fail;
7638
Jason Ekstranda7af7042013-10-12 22:38:11 -05007639 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02007640 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007641 wl_list_init(&ec->layer_list);
7642 wl_list_init(&ec->seat_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007643 wl_list_init(&ec->pending_output_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007644 wl_list_init(&ec->output_list);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03007645 wl_list_init(&ec->head_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007646 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01007647 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007648 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01007649 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007650 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02007651 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007652
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007653 wl_list_init(&ec->plugin_api_list);
7654
Xiong Zhang97116532013-10-23 13:58:31 +08007655 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02007656 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04007657
Giulio Camuffo459137b2014-10-11 23:56:24 +03007658 wl_data_device_manager_init(ec->wl_display);
7659
7660 wl_display_init_shm(ec->wl_display);
7661
7662 loop = wl_display_get_event_loop(ec->wl_display);
7663 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Daniel Stone6847b852017-03-01 11:34:08 +00007664 ec->repaint_timer =
7665 wl_event_loop_add_timer(loop, output_repaint_timer_handler,
7666 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007667
Quentin Glidic82681572016-12-17 13:40:51 +01007668 weston_layer_init(&ec->fade_layer, ec);
7669 weston_layer_init(&ec->cursor_layer, ec);
7670
7671 weston_layer_set_position(&ec->fade_layer, WESTON_LAYER_POSITION_FADE);
7672 weston_layer_set_position(&ec->cursor_layer,
7673 WESTON_LAYER_POSITION_CURSOR);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007674
Daniel Stonece62cb32018-07-20 09:46:24 +01007675 ec->debug_scene =
Leandro Ribeiroce100192019-12-26 16:35:49 -03007676 weston_compositor_add_log_scope(ec, "scene-graph",
7677 "Scene graph details\n",
7678 debug_scene_graph_cb, NULL,
7679 ec);
Daniel Stonece62cb32018-07-20 09:46:24 +01007680
Marius Vladda104eb2019-09-05 14:31:01 +03007681 ec->timeline =
Leandro Ribeiroce100192019-12-26 16:35:49 -03007682 weston_compositor_add_log_scope(ec, "timeline",
7683 "Timeline event points\n",
7684 weston_timeline_create_subscription,
7685 weston_timeline_destroy_subscription,
7686 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007687 return ec;
7688
7689fail:
7690 free(ec);
7691 return NULL;
7692}
7693
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007694/** weston_compositor_shutdown
7695 * \ingroup compositor
7696 */
Benjamin Franzkeb8263022011-08-30 11:32:47 +02007697WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007698weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07007699{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007700 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07007701
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007702 wl_event_source_remove(ec->idle_source);
7703
Matt Roper361d2ad2011-08-29 13:52:23 -07007704 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02007705 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07007706 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02007707
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007708 /* Destroy all pending outputs associated with this compositor */
7709 wl_list_for_each_safe(output, next, &ec->pending_output_list, link)
7710 output->destroy(output);
7711
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02007712 if (ec->renderer)
7713 ec->renderer->destroy(ec);
7714
Daniel Stone325fc2d2012-05-30 16:31:58 +01007715 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09007716 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01007717 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01007718 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01007719 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02007720 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007721
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04007722 weston_plane_release(&ec->primary_plane);
Matt Roper361d2ad2011-08-29 13:52:23 -07007723}
7724
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007725/** weston_compositor_exit_with_code
7726 * \ingroup compositor
7727 */
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05007728WL_EXPORT void
Adam Jackson3c3f3b12019-10-16 16:02:59 -04007729weston_compositor_exit_with_code(struct weston_compositor *compositor,
Frederic Plourdec336f062014-10-29 14:44:33 -04007730 int exit_code)
7731{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02007732 if (compositor->exit_code == EXIT_SUCCESS)
7733 compositor->exit_code = exit_code;
7734
Giulio Camuffo459137b2014-10-11 23:56:24 +03007735 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04007736}
7737
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007738/** weston_compositor_set_default_pointer_grab
7739 * \ingroup compositor
7740 */
Frederic Plourdec336f062014-10-29 14:44:33 -04007741WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007742weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
7743 const struct weston_pointer_grab_interface *interface)
7744{
7745 struct weston_seat *seat;
7746
7747 ec->default_pointer_grab = interface;
7748 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05007749 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
7750
7751 if (pointer)
7752 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007753 }
7754}
7755
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007756/** weston_compositor_set_presentation_clock
7757 * \ingroup compositor
7758 */
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007759WL_EXPORT int
7760weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
7761 clockid_t clk_id)
7762{
7763 struct timespec ts;
7764
7765 if (clock_gettime(clk_id, &ts) < 0)
7766 return -1;
7767
7768 compositor->presentation_clock = clk_id;
7769
7770 return 0;
7771}
7772
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007773/** For choosing the software clock, when the display hardware or API
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007774 * does not expose a compatible presentation timestamp.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007775 *
7776 * \ingroup compositor
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007777 */
7778WL_EXPORT int
7779weston_compositor_set_presentation_clock_software(
7780 struct weston_compositor *compositor)
7781{
7782 /* In order of preference */
7783 static const clockid_t clocks[] = {
7784 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
7785 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
7786 CLOCK_MONOTONIC, /* no jumps, may crawl */
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007787 };
7788 unsigned i;
7789
7790 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
7791 if (weston_compositor_set_presentation_clock(compositor,
7792 clocks[i]) == 0)
7793 return 0;
7794
7795 weston_log("Error: no suitable presentation clock available.\n");
7796
7797 return -1;
7798}
7799
Pekka Paalanen662f3842015-03-18 12:17:26 +02007800/** Read the current time from the Presentation clock
7801 *
7802 * \param compositor
Marius Vlada2dace22019-06-12 16:05:44 +03007803 * \param[out] ts The current time.
Pekka Paalanen662f3842015-03-18 12:17:26 +02007804 *
7805 * \note Reading the current time in user space is always imprecise to some
7806 * degree.
7807 *
7808 * This function is never meant to fail. If reading the clock does fail,
7809 * an error message is logged and a zero time is returned. Callers are not
7810 * supposed to detect or react to failures.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007811 *
7812 * \ingroup compositor
Pekka Paalanen662f3842015-03-18 12:17:26 +02007813 */
7814WL_EXPORT void
7815weston_compositor_read_presentation_clock(
7816 const struct weston_compositor *compositor,
7817 struct timespec *ts)
7818{
7819 static bool warned;
7820 int ret;
7821
7822 ret = clock_gettime(compositor->presentation_clock, ts);
7823 if (ret < 0) {
7824 ts->tv_sec = 0;
7825 ts->tv_nsec = 0;
7826
7827 if (!warned)
7828 weston_log("Error: failure to read "
Antonio Borneo39578632019-04-26 23:57:31 +02007829 "the presentation clock %#x: '%s' (%d)\n",
7830 compositor->presentation_clock,
7831 strerror(errno), errno);
Pekka Paalanen662f3842015-03-18 12:17:26 +02007832 warned = true;
7833 }
7834}
7835
Pekka Paalanen230f3b12014-09-29 14:18:40 -04007836/** Import dmabuf buffer into current renderer
7837 *
7838 * \param compositor
7839 * \param buffer the dmabuf buffer to import
7840 * \return true on usable buffers, false otherwise
7841 *
7842 * This function tests that the linux_dmabuf_buffer is usable
7843 * for the current renderer. Returns false on unusable buffers. Usually
7844 * usability is tested by importing the dmabufs for composition.
7845 *
7846 * This hook is also used for detecting if the renderer supports
7847 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
7848 * supported.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007849 *
7850 * \ingroup compositor
7851 */
Pekka Paalanen230f3b12014-09-29 14:18:40 -04007852WL_EXPORT bool
7853weston_compositor_import_dmabuf(struct weston_compositor *compositor,
7854 struct linux_dmabuf_buffer *buffer)
7855{
7856 struct weston_renderer *renderer;
7857
7858 renderer = compositor->renderer;
7859
7860 if (renderer->import_dmabuf == NULL)
7861 return false;
7862
7863 return renderer->import_dmabuf(compositor, buffer);
7864}
7865
Marius Vlad5a701542019-11-16 20:26:52 +02007866WL_EXPORT bool
7867weston_compositor_dmabuf_can_scanout(struct weston_compositor *compositor,
7868 struct linux_dmabuf_buffer *buffer)
7869{
7870 struct weston_backend *backend = compositor->backend;
7871
7872 if (backend->can_scanout_dmabuf == NULL)
7873 return false;
7874
7875 return backend->can_scanout_dmabuf(compositor, buffer);
7876}
7877
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007878WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05007879weston_version(int *major, int *minor, int *micro)
7880{
7881 *major = WESTON_VERSION_MAJOR;
7882 *minor = WESTON_VERSION_MINOR;
7883 *micro = WESTON_VERSION_MICRO;
7884}
7885
Daniel Stonee03c1112016-11-24 20:45:45 +00007886/**
7887 * Attempts to find a module path from the module map specified in the
7888 * environment. If found, writes the full path into the path variable.
7889 *
7890 * The module map is a string in environment variable WESTON_MODULE_MAP, where
7891 * each entry is of the form "name=path" and entries are separated by
7892 * semicolons. Whitespace is significant.
7893 *
7894 * \param name The name to search for.
7895 * \param path Where the path is written to if found.
7896 * \param path_len Allocated bytes at \c path .
7897 * \returns The length of the string written to path on success, or 0 if the
7898 * module was not specified in the environment map or path_len was too small.
7899 */
7900WL_EXPORT size_t
7901weston_module_path_from_env(const char *name, char *path, size_t path_len)
7902{
7903 const char *mapping = getenv("WESTON_MODULE_MAP");
7904 const char *end;
7905 const int name_len = strlen(name);
7906
7907 if (!mapping)
7908 return 0;
7909
7910 end = mapping + strlen(mapping);
7911 while (mapping < end && *mapping) {
7912 const char *filename, *next;
7913
7914 /* early out: impossibly short string */
7915 if (end - mapping < name_len + 1)
7916 return 0;
7917
7918 filename = &mapping[name_len + 1];
7919 next = strchrnul(mapping, ';');
7920
7921 if (strncmp(mapping, name, name_len) == 0 &&
7922 mapping[name_len] == '=') {
7923 size_t file_len = next - filename; /* no trailing NUL */
7924 if (file_len >= path_len)
7925 return 0;
7926 strncpy(path, filename, file_len);
7927 path[file_len] = '\0';
7928 return file_len;
7929 }
7930
7931 mapping = next + 1;
7932 }
7933
7934 return 0;
7935}
7936
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03007937WL_EXPORT void *
7938weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007939{
7940 char path[PATH_MAX];
7941 void *module, *init;
Daniel Stonebeb97e52016-11-28 12:13:54 +00007942 size_t len;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007943
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08007944 if (name == NULL)
7945 return NULL;
7946
Derek Foreman3f86e502015-06-08 11:46:54 -05007947 if (name[0] != '/') {
Daniel Stonee03c1112016-11-24 20:45:45 +00007948 len = weston_module_path_from_env(name, path, sizeof path);
7949 if (len == 0)
Daniel Stonebeb97e52016-11-28 12:13:54 +00007950 len = snprintf(path, sizeof path, "%s/%s",
7951 LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05007952 } else {
Daniel Stonebeb97e52016-11-28 12:13:54 +00007953 len = snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05007954 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007955
Daniel Stonebeb97e52016-11-28 12:13:54 +00007956 /* snprintf returns the length of the string it would've written,
7957 * _excluding_ the NUL byte. So even being equal to the size of
7958 * our buffer is an error here. */
7959 if (len >= sizeof path)
7960 return NULL;
7961
Kristian Høgsberga6813d22012-09-12 12:21:01 -04007962 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
7963 if (module) {
7964 weston_log("Module '%s' already loaded\n", path);
Pekka Paalanenf696ee92019-11-04 14:07:51 +02007965 } else {
7966 weston_log("Loading module '%s'\n", path);
7967 module = dlopen(path, RTLD_NOW);
7968 if (!module) {
7969 weston_log("Failed to load module: %s\n", dlerror());
7970 return NULL;
7971 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007972 }
7973
7974 init = dlsym(module, entrypoint);
7975 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03007976 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00007977 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007978 return NULL;
7979 }
7980
7981 return init;
7982}
7983
Pekka Paalanen6ffbba32019-11-06 12:59:32 +02007984/** Add a compositor destroy listener only once
7985 *
7986 * \param compositor The compositor whose destroy to watch for.
7987 * \param listener The listener struct to initialize.
7988 * \param destroy_handler The callback when compositor is destroyed.
7989 * \return True if listener is added, or false if there already is a listener
7990 * with the given \c destroy_handler.
7991 *
7992 * This function does nothing and returns false if the given callback function
7993 * is already present in the weston_compositor destroy callbacks list.
7994 * Otherwise, this function initializes the given listener with the given
7995 * callback pointer and adds it to the compositor's destroy callbacks list.
7996 *
7997 * This can be used to ensure that plugin initialization is done only once
7998 * in case the same plugin is loaded multiple times. If this function returns
7999 * false, the plugin should be already initialized successfully.
8000 *
8001 * All plugins should register a destroy listener for cleaning up. Note, that
8002 * the plugin destruction order is not guaranteed: plugins that depend on other
8003 * plugins must be able to be torn down in arbitrary order.
8004 *
Leandro Ribeiroca640d52020-01-27 19:12:01 -03008005 * \sa weston_compositor_destroy
Pekka Paalanen6ffbba32019-11-06 12:59:32 +02008006 */
8007WL_EXPORT bool
8008weston_compositor_add_destroy_listener_once(struct weston_compositor *compositor,
8009 struct wl_listener *listener,
8010 wl_notify_func_t destroy_handler)
8011{
8012 if (wl_signal_get(&compositor->destroy_signal, destroy_handler))
8013 return false;
8014
8015 listener->notify = destroy_handler;
8016 wl_signal_add(&compositor->destroy_signal, listener);
8017 return true;
8018}
Giulio Camuffo459137b2014-10-11 23:56:24 +03008019
Leandro Ribeiroca640d52020-01-27 19:12:01 -03008020/** Destroys the compositor.
Giulio Camuffo459137b2014-10-11 23:56:24 +03008021 *
Leandro Ribeiroca640d52020-01-27 19:12:01 -03008022 * This function cleans up the compositor state and then destroys it.
Giulio Camuffo459137b2014-10-11 23:56:24 +03008023 *
Leandro Ribeiroca640d52020-01-27 19:12:01 -03008024 * @param compositor The compositor to be destroyed.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008025 *
Marius Vlad284d5342019-06-24 12:00:47 +03008026 * @ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03008027 */
8028WL_EXPORT void
Leandro Ribeiroca640d52020-01-27 19:12:01 -03008029weston_compositor_destroy(struct weston_compositor *compositor)
Giulio Camuffo459137b2014-10-11 23:56:24 +03008030{
8031 /* prevent further rendering while shutting down */
8032 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
8033
8034 wl_signal_emit(&compositor->destroy_signal, compositor);
8035
8036 weston_compositor_xkb_destroy(compositor);
8037
Giulio Camuffo2d24e642015-10-03 16:25:15 +03008038 if (compositor->backend)
8039 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02008040
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03008041 /* The backend is responsible for destroying the heads. */
8042 assert(wl_list_empty(&compositor->head_list));
8043
Pekka Paalanen827b5d22016-06-29 11:54:26 +02008044 weston_plugin_api_destroy_list(compositor);
8045
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03008046 if (compositor->heads_changed_source)
8047 wl_event_source_remove(compositor->heads_changed_source);
8048
Leandro Ribeirof0149642019-12-18 15:52:18 -03008049 weston_log_scope_destroy(compositor->debug_scene);
Daniel Stonece62cb32018-07-20 09:46:24 +01008050 compositor->debug_scene = NULL;
Marius Vladda104eb2019-09-05 14:31:01 +03008051
Leandro Ribeirof0149642019-12-18 15:52:18 -03008052 weston_log_scope_destroy(compositor->timeline);
Marius Vladda104eb2019-09-05 14:31:01 +03008053 compositor->timeline = NULL;
Pekka Paalanena5630ea2017-10-12 13:13:42 +02008054
Giulio Camuffo459137b2014-10-11 23:56:24 +03008055 free(compositor);
8056}
8057
8058/** Instruct the compositor to exit.
8059 *
8060 * This functions does not directly destroy the compositor object, it merely
8061 * command it to start the tear down process. It is not guaranteed that the
8062 * tear down will happen immediately.
8063 *
8064 * \param compositor The compositor to tear down.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008065 *
8066 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03008067 */
8068WL_EXPORT void
8069weston_compositor_exit(struct weston_compositor *compositor)
8070{
8071 compositor->exit(compositor);
8072}
8073
8074/** Return the user data stored in the compositor.
8075 *
8076 * This function returns the user data pointer set with user_data parameter
8077 * to the \ref weston_compositor_create function.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008078 *
8079 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03008080 */
8081WL_EXPORT void *
8082weston_compositor_get_user_data(struct weston_compositor *compositor)
8083{
8084 return compositor->user_data;
8085}
Pekka Paalanendd186732016-06-03 14:49:54 +03008086
Pekka Paalanen50dbf382016-06-03 15:23:46 +03008087static const char * const backend_map[] = {
8088 [WESTON_BACKEND_DRM] = "drm-backend.so",
8089 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
8090 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
8091 [WESTON_BACKEND_RDP] = "rdp-backend.so",
8092 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
8093 [WESTON_BACKEND_X11] = "x11-backend.so",
8094};
8095
Pekka Paalanendd186732016-06-03 14:49:54 +03008096/** Load a backend into a weston_compositor
8097 *
8098 * A backend must be loaded to make a weston_compositor work. A backend
8099 * provides input and output capabilities, and determines the renderer to use.
8100 *
8101 * \param compositor A compositor that has not had a backend loaded yet.
8102 * \param backend Name of the backend file.
8103 * \param config_base A pointer to a backend-specific configuration
8104 * structure's 'base' member.
8105 *
8106 * \return 0 on success, or -1 on error.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008107 *
8108 * \ingroup compositor
Pekka Paalanendd186732016-06-03 14:49:54 +03008109 */
8110WL_EXPORT int
8111weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03008112 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03008113 struct weston_backend_config *config_base)
8114{
8115 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03008116 struct weston_backend_config *config_base);
8117
Pekka Paalanend7e35112017-08-29 17:04:12 +03008118 if (compositor->backend) {
8119 weston_log("Error: attempt to load a backend when one is already loaded\n");
8120 return -1;
8121 }
8122
Quentin Glidic887c0182016-07-10 11:00:53 +02008123 if (backend >= ARRAY_LENGTH(backend_map))
Pekka Paalanen50dbf382016-06-03 15:23:46 +03008124 return -1;
8125
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01008126 backend_init = weston_load_module(backend_map[backend], "weston_backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03008127 if (!backend_init)
8128 return -1;
8129
Pekka Paalanend7e35112017-08-29 17:04:12 +03008130 if (backend_init(compositor, config_base) < 0) {
8131 compositor->backend = NULL;
8132 return -1;
8133 }
8134
8135 return 0;
Pekka Paalanendd186732016-06-03 14:49:54 +03008136}
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008137
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008138/** weston_compositor_load_xwayland
8139 * \ingroup compositor
8140 */
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008141WL_EXPORT int
8142weston_compositor_load_xwayland(struct weston_compositor *compositor)
8143{
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01008144 int (*module_init)(struct weston_compositor *ec);
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008145
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01008146 module_init = weston_load_module("xwayland.so", "weston_module_init");
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008147 if (!module_init)
8148 return -1;
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01008149 if (module_init(compositor) < 0)
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008150 return -1;
8151 return 0;
8152}
Vasilis Liaskovitis486b4632018-10-10 16:14:55 +02008153
8154/** Resolve an internal compositor error by disconnecting the client.
8155 *
8156 * This function is used in cases when the wl_buffer turns out
8157 * unusable and there is no fallback path.
8158 *
8159 * It is possible the fault is caused by a compositor bug, the underlying
8160 * graphics stack bug or normal behaviour, or perhaps a client mistake.
8161 * In any case, the options are to either composite garbage or nothing,
8162 * or disconnect the client. This is a helper function for the latter.
8163 *
8164 * The error is sent as an INVALID_OBJECT error on the client's wl_display.
8165 *
8166 * \param buffer The weston buffer that is unusable.
8167 * \param msg A custom error message attached to the protocol error.
8168 */
8169WL_EXPORT void
8170weston_buffer_send_server_error(struct weston_buffer *buffer,
8171 const char *msg)
8172{
8173 struct wl_client *client;
8174 struct wl_resource *display_resource;
8175 uint32_t id;
8176
8177 assert(buffer->resource);
8178 id = wl_resource_get_id(buffer->resource);
8179 client = wl_resource_get_client(buffer->resource);
8180 display_resource = wl_client_get_object(client, 1);
8181
8182 assert(display_resource);
8183 wl_resource_post_error(display_resource,
8184 WL_DISPLAY_ERROR_INVALID_OBJECT,
8185 "server error with "
8186 "wl_buffer@%u: %s", id, msg);
8187}
Ankit Nautiyal93dde242019-07-08 11:46:42 +05308188
8189WL_EXPORT void
8190weston_output_disable_planes_incr(struct weston_output *output)
8191{
8192 output->disable_planes++;
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05308193 /*
8194 * If disable_planes changes from 0 to non-zero, it means some type of
8195 * recording of content has started, and therefore protection level of
8196 * the protected surfaces must be updated to avoid the recording of
8197 * the protected content.
8198 */
8199 if (output->disable_planes == 1)
8200 weston_schedule_surface_protection_update(output->compositor);
Ankit Nautiyal93dde242019-07-08 11:46:42 +05308201}
8202
8203WL_EXPORT void
8204weston_output_disable_planes_decr(struct weston_output *output)
8205{
8206 output->disable_planes--;
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05308207 /*
8208 * If disable_planes changes from non-zero to 0, it means no content
8209 * recording is going on any more, and the protected and surfaces can be
8210 * shown without any apprehensions about content being recorded.
8211 */
8212 if (output->disable_planes == 0)
8213 weston_schedule_surface_protection_update(output->compositor);
8214
Ankit Nautiyal93dde242019-07-08 11:46:42 +05308215}