blob: bcdcb0981d41d3f4634508a35973888bf5638394 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04002 * Copyright © 2010-2011 Intel Corporation
3 * Copyright © 2008-2011 Kristian Høgsberg
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03004 * Copyright © 2012-2018, 2021 Collabora, Ltd.
Pekka Paalanen925788f2018-04-19 14:20:01 +03005 * Copyright © 2017, 2018 General Electric Company
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05006 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -07007 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050014 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -070015 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial
17 * portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
23 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
24 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050027 */
28
Kristian Høgsberga9410222011-01-14 17:22:35 -050029#include "config.h"
30
Daniel Stoneb7452fe2012-06-01 12:14:06 +010031#include <fcntl.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040032#include <stdio.h>
33#include <string.h>
34#include <stdlib.h>
35#include <stdint.h>
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +010036#include <limits.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050037#include <stdarg.h>
Benjamin Franzke6f5fc692011-06-21 19:34:19 +020038#include <assert.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040039#include <sys/ioctl.h>
Daniel Stoneb7452fe2012-06-01 12:14:06 +010040#include <sys/mman.h>
Kristian Høgsberg27da5382011-06-21 17:32:25 -040041#include <sys/wait.h>
Pekka Paalanen409ef0a2011-12-02 15:30:21 +020042#include <sys/socket.h>
Martin Minarikf12c2872012-06-11 00:57:39 +020043#include <sys/utsname.h>
Martin Minarik37032f82012-06-18 20:15:18 +020044#include <sys/stat.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040045#include <unistd.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050046#include <math.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040047#include <linux/input.h>
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040048#include <dlfcn.h>
Kristian Høgsberg85449032011-05-02 12:11:07 -040049#include <signal.h>
Kristian Høgsberg0690da62012-01-16 11:53:54 -050050#include <setjmp.h>
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040051#include <sys/time.h>
52#include <time.h>
Pekka Paalanen23ade622014-08-27 13:31:26 +030053#include <errno.h>
Alexandros Frantzisacff29b2018-10-19 12:14:11 +030054#include <inttypes.h>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050055
Pekka Paalanenb5026542014-11-12 15:09:24 +020056#include "timeline.h"
57
Pekka Paalanen3d5d9472019-03-28 16:28:47 +020058#include <libweston/libweston.h>
Marius Vladc901e892019-06-21 22:49:18 +030059#include <libweston/weston-log.h>
Daniel Stonece62cb32018-07-20 09:46:24 +010060#include "linux-dmabuf.h"
Pekka Paalanene95ad5c2016-04-15 14:47:08 +030061#include "viewporter-server-protocol.h"
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020062#include "presentation-time-server-protocol.h"
Roman Gilge97391c2019-03-29 13:01:06 +010063#include "xdg-output-unstable-v1-server-protocol.h"
Alexandros Frantzisacff29b2018-10-19 12:14:11 +030064#include "linux-explicit-synchronization-unstable-v1-server-protocol.h"
Alexandros Frantzis67629672018-10-19 12:14:11 +030065#include "linux-explicit-synchronization.h"
Alexandros Frantzisacff29b2018-10-19 12:14:11 +030066#include "shared/fd-util.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070067#include "shared/helpers.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070068#include "shared/os-compatibility.h"
Bryce Harrington25a2bdd2016-08-03 17:40:52 -070069#include "shared/string-helpers.h"
Pekka Paalanenaa21f622015-07-03 15:44:50 +030070#include "shared/timespec-util.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040071#include "git-version.h"
Pekka Paalanencda14882019-04-04 15:41:02 +030072#include <libweston/version.h>
Pekka Paalanen27b377f2019-03-29 17:07:34 +020073#include <libweston/plugin-registry.h>
Marius Vlad00a6e012018-11-20 17:52:31 +020074#include "pixel-formats.h"
Marius Vlad63ef0782019-07-16 23:34:14 +030075#include "backend.h"
Marius Vlada72e3712019-07-10 13:46:39 +030076#include "libweston-internal.h"
Pekka Paalanen90a5ffa2021-02-25 12:03:28 +020077#include "color.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050078
Marius Vlad4e036292019-07-09 00:36:20 +030079#include "weston-log-internal.h"
80
Marius Vlad78984ee2019-06-11 00:05:08 +030081/**
82 * \defgroup head Head
83 * \defgroup output Output
84 * \defgroup compositor Compositor
85 */
86
Pekka Paalanen0513a952014-05-21 16:17:27 +030087#define DEFAULT_REPAINT_WINDOW 7 /* milliseconds */
88
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020089static void
Pekka Paalanend72bad22017-03-29 17:01:41 +030090weston_output_update_matrix(struct weston_output *output);
91
92static void
Alexander Larsson0b135062013-05-28 16:23:36 +020093weston_output_transform_scale_init(struct weston_output *output,
94 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020095
Rob Bradford27b17932013-06-26 18:08:46 +010096static void
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +030097weston_compositor_build_view_list(struct weston_compositor *compositor,
98 struct weston_output *output);
Rob Bradford27b17932013-06-26 18:08:46 +010099
Pekka Paalanendcac3512017-12-08 14:13:34 +0200100static char *
101weston_output_create_heads_string(struct weston_output *output);
102
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +0300103static struct weston_paint_node *
104weston_paint_node_create(struct weston_surface *surface,
105 struct weston_view *view,
106 struct weston_output *output)
107{
108 struct weston_paint_node *pnode;
Pekka Paalanen90a5ffa2021-02-25 12:03:28 +0200109 struct weston_paint_node *existing_node;
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +0300110
111 assert(view->surface == surface);
112
113 pnode = zalloc(sizeof *pnode);
114 if (!pnode)
115 return NULL;
116
Pekka Paalanen90a5ffa2021-02-25 12:03:28 +0200117 /*
118 * Invariant: all paint nodes with the same surface+output have the
119 * same surf_xform state.
120 */
121 wl_list_for_each(existing_node, &view->paint_node_list, view_link) {
122 assert(existing_node->surface == surface);
123 if (existing_node->output != output)
124 continue;
125
126 weston_surface_color_transform_copy(&pnode->surf_xform,
127 &existing_node->surf_xform);
128 pnode->surf_xform_valid = existing_node->surf_xform_valid;
129 break;
130 }
131
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +0300132 pnode->surface = surface;
133 wl_list_insert(&surface->paint_node_list, &pnode->surface_link);
134
135 pnode->view = view;
136 wl_list_insert(&view->paint_node_list, &pnode->view_link);
137
138 pnode->output = output;
139 wl_list_insert(&output->paint_node_list, &pnode->output_link);
140
Pekka Paalanen2fddc532021-04-30 17:41:29 +0300141 wl_list_init(&pnode->z_order_link);
142
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +0300143 return pnode;
144}
145
146static void
147weston_paint_node_destroy(struct weston_paint_node *pnode)
148{
149 assert(pnode->view->surface == pnode->surface);
150 wl_list_remove(&pnode->surface_link);
151 wl_list_remove(&pnode->view_link);
152 wl_list_remove(&pnode->output_link);
Pekka Paalanen2fddc532021-04-30 17:41:29 +0300153 wl_list_remove(&pnode->z_order_link);
Pekka Paalanen90a5ffa2021-02-25 12:03:28 +0200154 assert(pnode->surf_xform_valid || !pnode->surf_xform.transform);
155 weston_surface_color_transform_fini(&pnode->surf_xform);
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +0300156 free(pnode);
157}
158
Pekka Paalanen6528c032017-03-28 15:27:10 +0300159/** Send wl_output events for mode and scale changes
160 *
161 * \param head Send on all resources bound to this head.
162 * \param mode_changed If true, send the current mode.
163 * \param scale_changed If true, send the current scale.
164 */
165static void
166weston_mode_switch_send_events(struct weston_head *head,
167 bool mode_changed, bool scale_changed)
168{
169 struct weston_output *output = head->output;
170 struct wl_resource *resource;
171 int version;
172
173 wl_resource_for_each(resource, &head->resource_list) {
174 if (mode_changed) {
175 wl_output_send_mode(resource,
176 output->current_mode->flags,
177 output->current_mode->width,
178 output->current_mode->height,
179 output->current_mode->refresh);
180 }
181
182 version = wl_resource_get_version(resource);
183 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION && scale_changed)
184 wl_output_send_scale(resource, output->current_scale);
185
186 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
187 wl_output_send_done(resource);
188 }
Roman Gilge97391c2019-03-29 13:01:06 +0100189 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
190 zxdg_output_v1_send_logical_position(resource,
191 output->x,
192 output->y);
193 zxdg_output_v1_send_logical_size(resource,
194 output->width,
195 output->height);
196 zxdg_output_v1_send_done(resource);
197 }
Pekka Paalanen6528c032017-03-28 15:27:10 +0300198}
199
200static void
201weston_mode_switch_finish(struct weston_output *output,
202 int mode_changed, int scale_changed)
Alex Wu2dda6042012-04-17 17:20:47 +0800203{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200204 struct weston_seat *seat;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300205 struct weston_head *head;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200206 pixman_region32_t old_output_region;
Alexander Larsson355748e2013-05-28 16:23:38 +0200207
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200208 pixman_region32_init(&old_output_region);
209 pixman_region32_copy(&old_output_region, &output->region);
210
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200211 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +0200212 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200213
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200214 pixman_region32_init_rect(&output->region, output->x, output->y,
215 output->width, output->height);
216
217 weston_output_update_matrix(output);
218
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200219 /* If a pointer falls outside the outputs new geometry, move it to its
220 * lower-right corner */
221 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500222 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200223 int32_t x, y;
224
225 if (!pointer)
226 continue;
227
228 x = wl_fixed_to_int(pointer->x);
229 y = wl_fixed_to_int(pointer->y);
230
231 if (!pixman_region32_contains_point(&old_output_region,
232 x, y, NULL) ||
233 pixman_region32_contains_point(&output->region,
234 x, y, NULL))
235 continue;
236
237 if (x >= output->x + output->width)
238 x = output->x + output->width - 1;
239 if (y >= output->y + output->height)
240 y = output->y + output->height - 1;
241
242 pointer->x = wl_fixed_from_int(x);
243 pointer->y = wl_fixed_from_int(y);
244 }
245
246 pixman_region32_fini(&old_output_region);
247
Derek Foremandd4cd332014-11-10 10:29:59 -0600248 if (!mode_changed && !scale_changed)
249 return;
250
Hardening57388e42013-09-18 23:56:36 +0200251 /* notify clients of the changes */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300252 wl_list_for_each(head, &output->head_list, output_link)
253 weston_mode_switch_send_events(head,
254 mode_changed, scale_changed);
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600255}
256
David Fort0de859e2016-05-27 23:22:57 +0200257static void
258weston_compositor_reflow_outputs(struct weston_compositor *compositor,
259 struct weston_output *resized_output, int delta_width);
260
Marius Vlad55d87362019-06-11 01:15:35 +0300261/**
262 * \ingroup output
263 */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600264WL_EXPORT int
265weston_output_mode_set_native(struct weston_output *output,
266 struct weston_mode *mode,
267 int32_t scale)
268{
269 int ret;
270 int mode_changed = 0, scale_changed = 0;
David Fort0de859e2016-05-27 23:22:57 +0200271 int32_t old_width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600272
273 if (!output->switch_mode)
274 return -1;
275
276 if (!output->original_mode) {
277 mode_changed = 1;
278 ret = output->switch_mode(output, mode);
279 if (ret < 0)
280 return ret;
281 if (output->current_scale != scale) {
282 scale_changed = 1;
283 output->current_scale = scale;
Hardening57388e42013-09-18 23:56:36 +0200284 }
285 }
286
David Fort0de859e2016-05-27 23:22:57 +0200287 old_width = output->width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600288 output->native_mode = mode;
289 output->native_scale = scale;
290
291 weston_mode_switch_finish(output, mode_changed, scale_changed);
292
David Fort0de859e2016-05-27 23:22:57 +0200293 if (mode_changed || scale_changed) {
294 weston_compositor_reflow_outputs(output->compositor, output, output->width - old_width);
295
296 wl_signal_emit(&output->compositor->output_resized_signal, output);
297 }
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600298 return 0;
299}
300
Marius Vlad55d87362019-06-11 01:15:35 +0300301/**
302 * \ingroup output
303 */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600304WL_EXPORT int
305weston_output_mode_switch_to_native(struct weston_output *output)
306{
307 int ret;
308 int mode_changed = 0, scale_changed = 0;
309
310 if (!output->switch_mode)
311 return -1;
312
313 if (!output->original_mode) {
314 weston_log("already in the native mode\n");
315 return -1;
316 }
317 /* the non fullscreen clients haven't seen a mode set since we
318 * switched into a temporary, so we need to notify them if the
319 * mode at that time is different from the native mode now.
320 */
321 mode_changed = (output->original_mode != output->native_mode);
322 scale_changed = (output->original_scale != output->native_scale);
323
324 ret = output->switch_mode(output, output->native_mode);
325 if (ret < 0)
326 return ret;
327
328 output->current_scale = output->native_scale;
329
330 output->original_mode = NULL;
331 output->original_scale = 0;
332
333 weston_mode_switch_finish(output, mode_changed, scale_changed);
334
335 return 0;
336}
337
Marius Vlad55d87362019-06-11 01:15:35 +0300338/**
339 * \ingroup output
340 */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600341WL_EXPORT int
342weston_output_mode_switch_to_temporary(struct weston_output *output,
343 struct weston_mode *mode,
344 int32_t scale)
345{
346 int ret;
347
348 if (!output->switch_mode)
349 return -1;
350
351 /* original_mode is the last mode non full screen clients have seen,
352 * so we shouldn't change it if we already have one set.
353 */
354 if (!output->original_mode) {
355 output->original_mode = output->native_mode;
356 output->original_scale = output->native_scale;
357 }
358 ret = output->switch_mode(output, mode);
359 if (ret < 0)
360 return ret;
361
362 output->current_scale = scale;
363
364 weston_mode_switch_finish(output, 0, 0);
365
366 return 0;
Alex Wu2dda6042012-04-17 17:20:47 +0800367}
368
Benjamin Franzke06286262011-05-06 19:12:33 +0200369static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300370region_init_infinite(pixman_region32_t *region)
371{
372 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
373 UINT32_MAX, UINT32_MAX);
374}
375
Pekka Paalanene67858b2013-04-25 13:57:42 +0300376static struct weston_subsurface *
377weston_surface_to_subsurface(struct weston_surface *surface);
378
Jason Ekstranda7af7042013-10-12 22:38:11 -0500379WL_EXPORT struct weston_view *
380weston_view_create(struct weston_surface *surface)
381{
382 struct weston_view *view;
383
Bryce Harringtonde16d892014-11-20 22:21:57 -0800384 view = zalloc(sizeof *view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500385 if (view == NULL)
386 return NULL;
387
388 view->surface = surface;
Daniel Stonefb4869d2016-12-09 16:27:54 +0000389 view->plane = &surface->compositor->primary_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500390
Jason Ekstranda7af7042013-10-12 22:38:11 -0500391 /* Assign to surface */
392 wl_list_insert(&surface->views, &view->surface_link);
393
394 wl_signal_init(&view->destroy_signal);
395 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300396 wl_list_init(&view->layer_link.link);
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +0300397 wl_list_init(&view->paint_node_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500398
Jason Ekstranda7af7042013-10-12 22:38:11 -0500399 pixman_region32_init(&view->clip);
400
401 view->alpha = 1.0;
402 pixman_region32_init(&view->transform.opaque);
403
404 wl_list_init(&view->geometry.transformation_list);
405 wl_list_insert(&view->geometry.transformation_list,
406 &view->transform.position.link);
407 weston_matrix_init(&view->transform.position.matrix);
408 wl_list_init(&view->geometry.child_list);
Pekka Paalanen380adf52015-02-16 14:39:11 +0200409 pixman_region32_init(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500410 pixman_region32_init(&view->transform.boundingbox);
411 view->transform.dirty = 1;
412
Jason Ekstranda7af7042013-10-12 22:38:11 -0500413 return view;
414}
415
Pekka Paalanen133e4392014-09-23 22:08:46 -0400416struct weston_presentation_feedback {
417 struct wl_resource *resource;
418
419 /* XXX: could use just wl_resource_get_link() instead */
420 struct wl_list link;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200421
422 /* The per-surface feedback flags */
423 uint32_t psf_flags;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400424};
425
426static void
427weston_presentation_feedback_discard(
428 struct weston_presentation_feedback *feedback)
429{
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200430 wp_presentation_feedback_send_discarded(feedback->resource);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400431 wl_resource_destroy(feedback->resource);
432}
433
434static void
435weston_presentation_feedback_discard_list(struct wl_list *list)
436{
437 struct weston_presentation_feedback *feedback, *tmp;
438
439 wl_list_for_each_safe(feedback, tmp, list, link)
440 weston_presentation_feedback_discard(feedback);
441}
442
443static void
444weston_presentation_feedback_present(
445 struct weston_presentation_feedback *feedback,
446 struct weston_output *output,
447 uint32_t refresh_nsec,
448 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200449 uint64_t seq,
450 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400451{
452 struct wl_client *client = wl_resource_get_client(feedback->resource);
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300453 struct weston_head *head;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400454 struct wl_resource *o;
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200455 uint32_t tv_sec_hi;
456 uint32_t tv_sec_lo;
457 uint32_t tv_nsec;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300458 bool done = false;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400459
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300460 wl_list_for_each(head, &output->head_list, output_link) {
461 wl_resource_for_each(o, &head->resource_list) {
462 if (wl_resource_get_client(o) != client)
463 continue;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400464
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300465 wp_presentation_feedback_send_sync_output(feedback->resource, o);
466 done = true;
467 }
468
469 /* For clone mode, send it for just one wl_output global,
470 * they are all equivalent anyway.
471 */
472 if (done)
473 break;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400474 }
475
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200476 timespec_to_proto(ts, &tv_sec_hi, &tv_sec_lo, &tv_nsec);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200477 wp_presentation_feedback_send_presented(feedback->resource,
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200478 tv_sec_hi, tv_sec_lo, tv_nsec,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200479 refresh_nsec,
480 seq >> 32, seq & 0xffffffff,
481 flags | feedback->psf_flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400482 wl_resource_destroy(feedback->resource);
483}
484
485static void
486weston_presentation_feedback_present_list(struct wl_list *list,
487 struct weston_output *output,
488 uint32_t refresh_nsec,
489 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200490 uint64_t seq,
491 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400492{
493 struct weston_presentation_feedback *feedback, *tmp;
494
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200495 assert(!(flags & WP_PRESENTATION_FEEDBACK_INVALID) ||
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200496 wl_list_empty(list));
497
Pekka Paalanen133e4392014-09-23 22:08:46 -0400498 wl_list_for_each_safe(feedback, tmp, list, link)
499 weston_presentation_feedback_present(feedback, output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200500 refresh_nsec, ts, seq,
501 flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400502}
503
Jason Ekstrand7b982072014-05-20 14:33:03 -0500504static void
505surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
506{
507 struct weston_surface_state *state =
508 container_of(listener, struct weston_surface_state,
509 buffer_destroy_listener);
510
511 state->buffer = NULL;
512}
513
514static void
515weston_surface_state_init(struct weston_surface_state *state)
516{
517 state->newly_attached = 0;
518 state->buffer = NULL;
519 state->buffer_destroy_listener.notify =
520 surface_state_handle_buffer_destroy;
521 state->sx = 0;
522 state->sy = 0;
523
Derek Foreman152254b2015-11-26 14:17:48 -0600524 pixman_region32_init(&state->damage_surface);
525 pixman_region32_init(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500526 pixman_region32_init(&state->opaque);
527 region_init_infinite(&state->input);
528
529 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400530 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500531
532 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
533 state->buffer_viewport.buffer.scale = 1;
534 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
535 state->buffer_viewport.surface.width = -1;
536 state->buffer_viewport.changed = 0;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300537
538 state->acquire_fence_fd = -1;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +0530539
540 state->desired_protection = WESTON_HDCP_DISABLE;
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +0530541 state->protection_mode = WESTON_SURFACE_PROTECTION_MODE_RELAXED;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500542}
543
544static void
545weston_surface_state_fini(struct weston_surface_state *state)
546{
Vlad Zahorodniic2b97472021-07-29 11:16:51 +0300547 struct wl_resource *cb, *next;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500548
Vlad Zahorodniic2b97472021-07-29 11:16:51 +0300549 wl_resource_for_each_safe(cb, next, &state->frame_callback_list)
550 wl_resource_destroy(cb);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500551
Pekka Paalanen133e4392014-09-23 22:08:46 -0400552 weston_presentation_feedback_discard_list(&state->feedback_list);
553
Jason Ekstrand7b982072014-05-20 14:33:03 -0500554 pixman_region32_fini(&state->input);
555 pixman_region32_fini(&state->opaque);
Derek Foreman152254b2015-11-26 14:17:48 -0600556 pixman_region32_fini(&state->damage_surface);
557 pixman_region32_fini(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500558
559 if (state->buffer)
560 wl_list_remove(&state->buffer_destroy_listener.link);
561 state->buffer = NULL;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300562
563 fd_clear(&state->acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +0300564 weston_buffer_release_reference(&state->buffer_release_ref, NULL);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500565}
566
567static void
568weston_surface_state_set_buffer(struct weston_surface_state *state,
569 struct weston_buffer *buffer)
570{
571 if (state->buffer == buffer)
572 return;
573
574 if (state->buffer)
575 wl_list_remove(&state->buffer_destroy_listener.link);
576 state->buffer = buffer;
577 if (state->buffer)
578 wl_signal_add(&state->buffer->destroy_signal,
579 &state->buffer_destroy_listener);
580}
581
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500582WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500583weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500584{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500585 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400586
Bryce Harringtonde16d892014-11-20 22:21:57 -0800587 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400588 if (surface == NULL)
589 return NULL;
590
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500591 wl_signal_init(&surface->destroy_signal);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +0800592 wl_signal_init(&surface->commit_signal);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500593
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500594 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200595 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400596
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200597 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
598 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200599 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
600 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500601
602 weston_surface_state_init(&surface->pending);
603
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400604 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500605 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300606 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400607
Jason Ekstranda7af7042013-10-12 22:38:11 -0500608 wl_list_init(&surface->views);
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +0300609 wl_list_init(&surface->paint_node_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500610
611 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400612 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500613
Pekka Paalanene67858b2013-04-25 13:57:42 +0300614 wl_list_init(&surface->subsurface_list);
615 wl_list_init(&surface->subsurface_list_pending);
616
Jason Ekstrand1e059042014-10-16 10:55:19 -0500617 weston_matrix_init(&surface->buffer_to_surface_matrix);
618 weston_matrix_init(&surface->surface_to_buffer_matrix);
619
Jonas Ådahld3414f22016-07-22 17:56:31 +0800620 wl_list_init(&surface->pointer_constraints);
621
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300622 surface->acquire_fence_fd = -1;
623
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +0530624 surface->desired_protection = WESTON_HDCP_DISABLE;
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +0530625 surface->current_protection = WESTON_HDCP_DISABLE;
626 surface->protection_mode = WESTON_SURFACE_PROTECTION_MODE_RELAXED;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +0530627
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400628 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500629}
630
Alex Wu8811bf92012-02-28 18:07:54 +0800631WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500632weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200633 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500634{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100635 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Philipp Zabel195dade2018-09-03 19:44:59 +0200636 surface->is_opaque = !(alpha < 1.0);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500637}
638
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400639WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500640weston_view_to_global_float(struct weston_view *view,
641 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200642{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500643 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200644 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
645
Jason Ekstranda7af7042013-10-12 22:38:11 -0500646 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200647
648 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200649 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700650 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200651 v.f[3]);
652 *x = 0;
653 *y = 0;
654 return;
655 }
656
657 *x = v.f[0] / v.f[3];
658 *y = v.f[1] / v.f[3];
659 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500660 *x = sx + view->geometry.x;
661 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200662 }
663}
664
Pekka Paalanen0df44772020-02-14 14:50:03 +0200665/** Transform a point to buffer coordinates
666 *
667 * \param width Surface width.
668 * \param height Surface height.
669 * \param transform Buffer transform.
670 * \param scale Buffer scale.
671 * \param sx Surface x coordinate of a point.
672 * \param sy Surface y coordinate of a point.
673 * \param[out] bx Buffer x coordinate of the point.
674 * \param[out] by Buffer Y coordinate of the point.
675 *
676 * Converts the given surface-local coordinates to buffer coordinates
677 * according to the given buffer transform and scale.
678 * This ignores wp_viewport.
679 *
680 * The given width and height must be the result of inverse scaled and
681 * inverse transformed buffer size.
682 */
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500683WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200684weston_transformed_coord(int width, int height,
685 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200686 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200687 float sx, float sy, float *bx, float *by)
688{
689 switch (transform) {
690 case WL_OUTPUT_TRANSFORM_NORMAL:
691 default:
692 *bx = sx;
693 *by = sy;
694 break;
695 case WL_OUTPUT_TRANSFORM_FLIPPED:
696 *bx = width - sx;
697 *by = sy;
698 break;
699 case WL_OUTPUT_TRANSFORM_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200700 *bx = sy;
701 *by = width - sx;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200702 break;
703 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200704 *bx = sy;
705 *by = sx;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200706 break;
707 case WL_OUTPUT_TRANSFORM_180:
708 *bx = width - sx;
709 *by = height - sy;
710 break;
711 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
712 *bx = sx;
713 *by = height - sy;
714 break;
715 case WL_OUTPUT_TRANSFORM_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200716 *bx = height - sy;
717 *by = sx;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200718 break;
719 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200720 *bx = height - sy;
721 *by = width - sx;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200722 break;
723 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200724
725 *bx *= scale;
726 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200727}
728
Pekka Paalanen0df44772020-02-14 14:50:03 +0200729/** Transform a rectangle to buffer coordinates
730 *
731 * \param width Surface width.
732 * \param height Surface height.
733 * \param transform Buffer transform.
734 * \param scale Buffer scale.
735 * \param rect Rectangle in surface coordinates.
736 * \return Rectangle in buffer coordinates.
737 *
738 * Converts the given surface-local rectangle to buffer coordinates
739 * according to the given buffer transform and scale. The resulting
740 * rectangle is guaranteed to be well-formed.
741 * This ignores wp_viewport.
742 *
743 * The given width and height must be the result of inverse scaled and
744 * inverse transformed buffer size.
745 */
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200746WL_EXPORT pixman_box32_t
747weston_transformed_rect(int width, int height,
748 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200749 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200750 pixman_box32_t rect)
751{
752 float x1, x2, y1, y2;
753
754 pixman_box32_t ret;
755
Alexander Larsson4ea95522013-05-22 14:41:37 +0200756 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200757 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200758 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200759 rect.x2, rect.y2, &x2, &y2);
760
761 if (x1 <= x2) {
762 ret.x1 = x1;
763 ret.x2 = x2;
764 } else {
765 ret.x1 = x2;
766 ret.x2 = x1;
767 }
768
769 if (y1 <= y2) {
770 ret.y1 = y1;
771 ret.y2 = y2;
772 } else {
773 ret.y1 = y2;
774 ret.y2 = y1;
775 }
776
777 return ret;
778}
779
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600780/** Transform a region by a matrix, restricted to axis-aligned transformations
781 *
782 * Warning: This function does not work for projective, affine, or matrices
783 * that encode arbitrary rotations. Only 90-degree step rotations are
784 * supported.
785 */
786WL_EXPORT void
787weston_matrix_transform_region(pixman_region32_t *dest,
788 struct weston_matrix *matrix,
789 pixman_region32_t *src)
790{
791 pixman_box32_t *src_rects, *dest_rects;
792 int nrects, i;
793
794 src_rects = pixman_region32_rectangles(src, &nrects);
795 dest_rects = malloc(nrects * sizeof(*dest_rects));
796 if (!dest_rects)
797 return;
798
799 for (i = 0; i < nrects; i++) {
800 struct weston_vector vec1 = {{
801 src_rects[i].x1, src_rects[i].y1, 0, 1
802 }};
803 weston_matrix_transform(matrix, &vec1);
804 vec1.f[0] /= vec1.f[3];
805 vec1.f[1] /= vec1.f[3];
806
807 struct weston_vector vec2 = {{
808 src_rects[i].x2, src_rects[i].y2, 0, 1
809 }};
810 weston_matrix_transform(matrix, &vec2);
811 vec2.f[0] /= vec2.f[3];
812 vec2.f[1] /= vec2.f[3];
813
814 if (vec1.f[0] < vec2.f[0]) {
815 dest_rects[i].x1 = floor(vec1.f[0]);
816 dest_rects[i].x2 = ceil(vec2.f[0]);
817 } else {
818 dest_rects[i].x1 = floor(vec2.f[0]);
819 dest_rects[i].x2 = ceil(vec1.f[0]);
820 }
821
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600822 if (vec1.f[1] < vec2.f[1]) {
823 dest_rects[i].y1 = floor(vec1.f[1]);
824 dest_rects[i].y2 = ceil(vec2.f[1]);
825 } else {
826 dest_rects[i].y1 = floor(vec2.f[1]);
827 dest_rects[i].y2 = ceil(vec1.f[1]);
828 }
829 }
830
831 pixman_region32_clear(dest);
832 pixman_region32_init_rects(dest, dest_rects, nrects);
833 free(dest_rects);
834}
835
Pekka Paalanen0df44772020-02-14 14:50:03 +0200836/** Transform a region to buffer coordinates
837 *
838 * \param width Surface width.
839 * \param height Surface height.
840 * \param transform Buffer transform.
841 * \param scale Buffer scale.
842 * \param[in] src Region in surface coordinates.
843 * \param[out] dest Resulting region in buffer coordinates.
844 *
845 * Converts the given surface-local region to buffer coordinates
846 * according to the given buffer transform and scale.
847 * This ignores wp_viewport.
848 *
849 * The given width and height must be the result of inverse scaled and
850 * inverse transformed buffer size.
851 *
852 * src and dest are allowed to point to the same memory for in-place conversion.
853 */
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200854WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500855weston_transformed_region(int width, int height,
856 enum wl_output_transform transform,
857 int32_t scale,
858 pixman_region32_t *src, pixman_region32_t *dest)
859{
860 pixman_box32_t *src_rects, *dest_rects;
861 int nrects, i;
862
863 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
864 if (src != dest)
865 pixman_region32_copy(dest, src);
866 return;
867 }
868
869 src_rects = pixman_region32_rectangles(src, &nrects);
870 dest_rects = malloc(nrects * sizeof(*dest_rects));
871 if (!dest_rects)
872 return;
873
874 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
875 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
876 } else {
877 for (i = 0; i < nrects; i++) {
878 switch (transform) {
879 default:
880 case WL_OUTPUT_TRANSFORM_NORMAL:
881 dest_rects[i].x1 = src_rects[i].x1;
882 dest_rects[i].y1 = src_rects[i].y1;
883 dest_rects[i].x2 = src_rects[i].x2;
884 dest_rects[i].y2 = src_rects[i].y2;
885 break;
886 case WL_OUTPUT_TRANSFORM_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200887 dest_rects[i].x1 = src_rects[i].y1;
888 dest_rects[i].y1 = width - src_rects[i].x2;
889 dest_rects[i].x2 = src_rects[i].y2;
890 dest_rects[i].y2 = width - src_rects[i].x1;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500891 break;
892 case WL_OUTPUT_TRANSFORM_180:
893 dest_rects[i].x1 = width - src_rects[i].x2;
894 dest_rects[i].y1 = height - src_rects[i].y2;
895 dest_rects[i].x2 = width - src_rects[i].x1;
896 dest_rects[i].y2 = height - src_rects[i].y1;
897 break;
898 case WL_OUTPUT_TRANSFORM_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200899 dest_rects[i].x1 = height - src_rects[i].y2;
900 dest_rects[i].y1 = src_rects[i].x1;
901 dest_rects[i].x2 = height - src_rects[i].y1;
902 dest_rects[i].y2 = src_rects[i].x2;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500903 break;
904 case WL_OUTPUT_TRANSFORM_FLIPPED:
905 dest_rects[i].x1 = width - src_rects[i].x2;
906 dest_rects[i].y1 = src_rects[i].y1;
907 dest_rects[i].x2 = width - src_rects[i].x1;
908 dest_rects[i].y2 = src_rects[i].y2;
909 break;
910 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200911 dest_rects[i].x1 = src_rects[i].y1;
912 dest_rects[i].y1 = src_rects[i].x1;
913 dest_rects[i].x2 = src_rects[i].y2;
914 dest_rects[i].y2 = src_rects[i].x2;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500915 break;
916 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
917 dest_rects[i].x1 = src_rects[i].x1;
918 dest_rects[i].y1 = height - src_rects[i].y2;
919 dest_rects[i].x2 = src_rects[i].x2;
920 dest_rects[i].y2 = height - src_rects[i].y1;
921 break;
922 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200923 dest_rects[i].x1 = height - src_rects[i].y2;
924 dest_rects[i].y1 = width - src_rects[i].x2;
925 dest_rects[i].x2 = height - src_rects[i].y1;
926 dest_rects[i].y2 = width - src_rects[i].x1;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500927 break;
928 }
929 }
930 }
931
932 if (scale != 1) {
933 for (i = 0; i < nrects; i++) {
934 dest_rects[i].x1 *= scale;
935 dest_rects[i].x2 *= scale;
936 dest_rects[i].y1 *= scale;
937 dest_rects[i].y2 *= scale;
938 }
939 }
940
941 pixman_region32_clear(dest);
942 pixman_region32_init_rects(dest, dest_rects, nrects);
943 free(dest_rects);
944}
945
Jonny Lamb74130762013-11-26 18:19:46 +0100946static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300947viewport_surface_to_buffer(struct weston_surface *surface,
948 float sx, float sy, float *bx, float *by)
Jonny Lamb74130762013-11-26 18:19:46 +0100949{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200950 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200951 double src_width, src_height;
952 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200953
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200954 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
955 if (vp->surface.width == -1) {
956 *bx = sx;
957 *by = sy;
958 return;
959 }
Jonny Lamb74130762013-11-26 18:19:46 +0100960
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200961 src_x = 0.0;
962 src_y = 0.0;
963 src_width = surface->width_from_buffer;
964 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100965 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200966 src_x = wl_fixed_to_double(vp->buffer.src_x);
967 src_y = wl_fixed_to_double(vp->buffer.src_y);
968 src_width = wl_fixed_to_double(vp->buffer.src_width);
969 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100970 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200971
972 *bx = sx * src_width / surface->width + src_x;
973 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100974}
975
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500976WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200977weston_surface_to_buffer_float(struct weston_surface *surface,
978 float sx, float sy, float *bx, float *by)
979{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200980 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
981
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300982 /* first transform coordinates if the viewport is set */
983 viewport_surface_to_buffer(surface, sx, sy, bx, by);
Jonny Lamb74130762013-11-26 18:19:46 +0100984
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500985 weston_transformed_coord(surface->width_from_buffer,
986 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200987 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100988 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200989}
990
Derek Foreman99739672015-12-03 16:38:11 -0600991/** Transform a rectangle from surface coordinates to buffer coordinates
992 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300993 * \param surface The surface to fetch wp_viewport and buffer transformation
Derek Foreman99739672015-12-03 16:38:11 -0600994 * from.
995 * \param rect The rectangle to transform.
996 * \return The transformed rectangle.
997 *
998 * Viewport and buffer transformations can only do translation, scaling,
999 * and rotations in 90-degree steps. Therefore the only loss in the
1000 * conversion is coordinate rounding.
1001 *
1002 * However, some coordinate rounding takes place as an intermediate
1003 * step before the buffer scale factor is applied, so the rectangle
1004 * boundary may not be exactly as expected.
1005 *
1006 * This is OK for damage tracking since a little extra coverage is
1007 * not a problem.
1008 */
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +02001009WL_EXPORT pixman_box32_t
1010weston_surface_to_buffer_rect(struct weston_surface *surface,
1011 pixman_box32_t rect)
1012{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001013 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +01001014 float xf, yf;
1015
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03001016 /* first transform box coordinates if the viewport is set */
1017 viewport_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
Jonny Lamb74130762013-11-26 18:19:46 +01001018 rect.x1 = floorf(xf);
1019 rect.y1 = floorf(yf);
1020
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03001021 viewport_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
Derek Foremane2e15ac2015-12-01 13:00:43 -06001022 rect.x2 = ceilf(xf);
1023 rect.y2 = ceilf(yf);
Jonny Lamb74130762013-11-26 18:19:46 +01001024
Jason Ekstrandd0cebc32014-04-21 20:56:46 -05001025 return weston_transformed_rect(surface->width_from_buffer,
1026 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001027 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +02001028 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +02001029}
1030
Pekka Paalanene54e31c2015-03-04 14:23:28 +02001031/** Transform a region from surface coordinates to buffer coordinates
1032 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03001033 * \param surface The surface to fetch wp_viewport and buffer transformation
Pekka Paalanene54e31c2015-03-04 14:23:28 +02001034 * from.
Marius Vlada2dace22019-06-12 16:05:44 +03001035 * \param[in] surface_region The region in surface coordinates.
1036 * \param[out] buffer_region The region converted to buffer coordinates.
Pekka Paalanene54e31c2015-03-04 14:23:28 +02001037 *
1038 * Buffer_region must be init'd, but will be completely overwritten.
1039 *
1040 * Viewport and buffer transformations can only do translation, scaling,
1041 * and rotations in 90-degree steps. Therefore the only loss in the
Derek Foreman99739672015-12-03 16:38:11 -06001042 * conversion is from the coordinate rounding that takes place in
1043 * \ref weston_surface_to_buffer_rect.
Marius Vlada2dace22019-06-12 16:05:44 +03001044 *
Pekka Paalanene54e31c2015-03-04 14:23:28 +02001045 */
1046WL_EXPORT void
1047weston_surface_to_buffer_region(struct weston_surface *surface,
1048 pixman_region32_t *surface_region,
1049 pixman_region32_t *buffer_region)
1050{
1051 pixman_box32_t *src_rects, *dest_rects;
1052 int nrects, i;
1053
1054 src_rects = pixman_region32_rectangles(surface_region, &nrects);
1055 dest_rects = malloc(nrects * sizeof(*dest_rects));
1056 if (!dest_rects)
1057 return;
1058
1059 for (i = 0; i < nrects; i++) {
1060 dest_rects[i] = weston_surface_to_buffer_rect(surface,
1061 src_rects[i]);
1062 }
1063
1064 pixman_region32_fini(buffer_region);
1065 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
1066 free(dest_rects);
1067}
1068
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +02001069WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001070weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001071 struct weston_plane *plane)
1072{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001073 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001074 return;
1075
Jason Ekstranda7af7042013-10-12 22:38:11 -05001076 weston_view_damage_below(view);
1077 view->plane = plane;
1078 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001079}
1080
Pekka Paalanen51723d52015-02-17 13:10:01 +02001081/** Inflict damage on the plane where the view is visible.
1082 *
1083 * \param view The view that causes the damage.
1084 *
1085 * If the view is currently on a plane (including the primary plane),
1086 * take the view's boundingbox, subtract all the opaque views that cover it,
1087 * and add the remaining region as damage to the plane. This corresponds
1088 * to the damage inflicted to the plane if this view disappeared.
1089 *
1090 * A repaint is scheduled for this view.
1091 *
1092 * The region of all opaque views covering this view is stored in
1093 * weston_view::clip and updated by view_accumulate_damage() during
1094 * weston_output_repaint(). Specifically, that region matches the
1095 * scenegraph as it was last painted.
1096 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001097WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001098weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001099{
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001100 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001101
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001102 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001103 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001104 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +08001105 if (view->plane)
1106 pixman_region32_union(&view->plane->damage,
1107 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001108 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -08001109 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001110}
1111
Pekka Paalanen01e00682017-03-24 16:21:06 +02001112/** Send wl_surface.enter/leave events
1113 *
1114 * \param surface The surface.
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03001115 * \param head A head of the entered/left output.
Pekka Paalanen01e00682017-03-24 16:21:06 +02001116 * \param enter True if entered.
Marius Vlada2dace22019-06-12 16:05:44 +03001117 * \param leave True if left.
Pekka Paalanen01e00682017-03-24 16:21:06 +02001118 *
1119 * Send the enter/leave events for all protocol objects bound to the given
1120 * output by the client owning the surface.
1121 */
1122static void
1123weston_surface_send_enter_leave(struct weston_surface *surface,
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03001124 struct weston_head *head,
Pekka Paalanen01e00682017-03-24 16:21:06 +02001125 bool enter,
1126 bool leave)
1127{
1128 struct wl_resource *wloutput;
1129 struct wl_client *client;
1130
1131 assert(enter != leave);
1132
1133 client = wl_resource_get_client(surface->resource);
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03001134 wl_resource_for_each(wloutput, &head->resource_list) {
Pekka Paalanen01e00682017-03-24 16:21:06 +02001135 if (wl_resource_get_client(wloutput) != client)
1136 continue;
1137
1138 if (enter)
1139 wl_surface_send_enter(surface->resource, wloutput);
1140 if (leave)
1141 wl_surface_send_leave(surface->resource, wloutput);
1142 }
1143}
1144
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301145static void
1146weston_surface_compute_protection(struct protected_surface *psurface)
1147{
1148 enum weston_hdcp_protection min_protection;
1149 bool min_protection_valid = false;
1150 struct weston_surface *surface = psurface->surface;
1151 struct weston_output *output;
1152
1153 wl_list_for_each(output, &surface->compositor->output_list, link)
1154 if (surface->output_mask & (1u << output->id)) {
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05301155 /*
1156 * If the content-protection is enabled with protection
1157 * mode as RELAXED for a surface, and if
1158 * content-recording features like: screen-shooter,
1159 * recorder, screen-sharing, etc are on, then notify the
1160 * client, that the protection is disabled.
1161 *
1162 * Note: If the protection mode is ENFORCED then there
1163 * is no need to bother the client as the renderer takes
1164 * care of censoring the visibility of the protected
1165 * content.
1166 */
1167
1168 if (output->disable_planes > 0 &&
1169 surface->protection_mode == WESTON_SURFACE_PROTECTION_MODE_RELAXED) {
1170 min_protection = WESTON_HDCP_DISABLE;
1171 min_protection_valid = true;
1172 break;
1173 }
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301174 if (!min_protection_valid) {
1175 min_protection = output->current_protection;
1176 min_protection_valid = true;
1177 }
1178 if (output->current_protection < min_protection)
1179 min_protection = output->current_protection;
1180 }
1181 if (!min_protection_valid)
1182 min_protection = WESTON_HDCP_DISABLE;
1183
1184 surface->current_protection = min_protection;
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05301185
1186 weston_protected_surface_send_event(psurface, surface->current_protection);
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301187}
1188
1189static void
1190notify_surface_protection_change(void *data)
1191{
1192 struct weston_compositor *compositor = data;
1193 struct content_protection *cp;
1194 struct protected_surface *psurface;
1195
1196 cp = compositor->content_protection;
1197 cp->surface_protection_update = NULL;
1198
1199 /* Notify the clients, whose surfaces are changed */
1200 wl_list_for_each(psurface, &cp->protected_list, link)
1201 if (psurface && psurface->surface)
1202 weston_surface_compute_protection(psurface);
1203}
1204
Bryce Harrington3f650b82015-12-23 11:01:58 -08001205/**
Ankit Nautiyalfaa5ab42019-07-08 13:23:24 +05301206 * \param compositor weston_compositor
1207 *
1208 * Schedule an idle task to notify surface about the update in protection,
1209 * if not already scheduled.
1210 */
1211static void
1212weston_schedule_surface_protection_update(struct weston_compositor *compositor)
1213{
1214 struct content_protection *cp = compositor->content_protection;
1215 struct wl_event_loop *loop;
1216
1217 if (!cp || cp->surface_protection_update)
1218 return;
1219 loop = wl_display_get_event_loop(compositor->wl_display);
1220 cp->surface_protection_update = wl_event_loop_add_idle(loop,
1221 notify_surface_protection_change,
1222 compositor);
1223}
1224
1225/**
Bryce Harrington3f650b82015-12-23 11:01:58 -08001226 * \param es The surface
1227 * \param mask The new set of outputs for the surface
1228 *
1229 * Sets the surface's set of outputs to the ones specified by
1230 * the new output mask provided. Identifies the outputs that
1231 * have changed, the posts enter and leave events for these
1232 * outputs as appropriate.
1233 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001234static void
1235weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
1236{
1237 uint32_t different = es->output_mask ^ mask;
1238 uint32_t entered = mask & different;
1239 uint32_t left = es->output_mask & different;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001240 uint32_t output_bit;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001241 struct weston_output *output;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001242 struct weston_head *head;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001243
1244 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001245 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001246 return;
1247 if (different == 0)
1248 return;
1249
1250 wl_list_for_each(output, &es->compositor->output_list, link) {
Pekka Paalanen01e00682017-03-24 16:21:06 +02001251 output_bit = 1u << output->id;
1252 if (!(output_bit & different))
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001253 continue;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001254
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001255 wl_list_for_each(head, &output->head_list, output_link) {
1256 weston_surface_send_enter_leave(es, head,
1257 output_bit & entered,
1258 output_bit & left);
1259 }
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001260 }
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301261 /*
1262 * Change in surfaces' output mask might trigger a change in its
1263 * protection.
1264 */
Ankit Nautiyalfaa5ab42019-07-08 13:23:24 +05301265 weston_schedule_surface_protection_update(es->compositor);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001266}
1267
Semi Malinene7a52fb2018-04-26 11:08:10 +02001268static void
1269notify_view_output_destroy(struct wl_listener *listener, void *data)
1270{
1271 struct weston_view *view =
1272 container_of(listener,
1273 struct weston_view, output_destroy_listener);
1274
1275 view->output = NULL;
1276 view->output_destroy_listener.notify = NULL;
1277}
1278
1279/** Set the primary output of the view
1280 *
1281 * \param view The view whose primary output to set
1282 * \param output The new primary output for the view
1283 *
1284 * Set \a output to be the primary output of the \a view.
1285 *
1286 * Notice that the assignment may be temporary; the primary output could be
1287 * automatically changed. Hence, one cannot rely on the value persisting.
1288 *
1289 * Passing NULL as /a output will set the primary output to NULL.
1290 */
1291WL_EXPORT void
1292weston_view_set_output(struct weston_view *view, struct weston_output *output)
1293{
1294 if (view->output_destroy_listener.notify) {
1295 wl_list_remove(&view->output_destroy_listener.link);
1296 view->output_destroy_listener.notify = NULL;
1297 }
1298 view->output = output;
1299 if (output) {
1300 view->output_destroy_listener.notify =
1301 notify_view_output_destroy;
1302 wl_signal_add(&output->destroy_signal,
1303 &view->output_destroy_listener);
1304 }
1305}
1306
Bryce Harrington3f650b82015-12-23 11:01:58 -08001307/** Recalculate which output(s) the surface has views displayed on
1308 *
1309 * \param es The surface to remap to outputs
1310 *
1311 * Finds the output that is showing the largest amount of one
1312 * of the surface's various views. This output becomes the
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001313 * surface's primary output for vsync and frame callback purposes.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001314 *
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001315 * Also notes all outputs of all of the surface's views
Bryce Harrington3f650b82015-12-23 11:01:58 -08001316 * in the output_mask for the surface.
1317 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001318static void
1319weston_surface_assign_output(struct weston_surface *es)
1320{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001321 struct weston_output *new_output;
1322 struct weston_view *view;
1323 pixman_region32_t region;
1324 uint32_t max, area, mask;
1325 pixman_box32_t *e;
1326
1327 new_output = NULL;
1328 max = 0;
1329 mask = 0;
1330 pixman_region32_init(&region);
1331 wl_list_for_each(view, &es->views, surface_link) {
1332 if (!view->output)
1333 continue;
1334
1335 pixman_region32_intersect(&region, &view->transform.boundingbox,
1336 &view->output->region);
1337
1338 e = pixman_region32_extents(&region);
1339 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1340
1341 mask |= view->output_mask;
1342
1343 if (area >= max) {
1344 new_output = view->output;
1345 max = area;
1346 }
1347 }
1348 pixman_region32_fini(&region);
1349
1350 es->output = new_output;
1351 weston_surface_update_output_mask(es, mask);
1352}
1353
Bryce Harrington3f650b82015-12-23 11:01:58 -08001354/** Recalculate which output(s) the view is displayed on
1355 *
1356 * \param ev The view to remap to outputs
1357 *
1358 * Identifies the set of outputs that the view is visible on,
1359 * noting them into the output_mask. The output that the view
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001360 * is most visible on is set as the view's primary output.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001361 *
1362 * Also does the same for the view's surface. See
1363 * weston_surface_assign_output().
1364 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001365static void
1366weston_view_assign_output(struct weston_view *ev)
1367{
1368 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001369 struct weston_output *output, *new_output;
1370 pixman_region32_t region;
1371 uint32_t max, area, mask;
1372 pixman_box32_t *e;
1373
1374 new_output = NULL;
1375 max = 0;
1376 mask = 0;
1377 pixman_region32_init(&region);
1378 wl_list_for_each(output, &ec->output_list, link) {
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03001379 if (output->destroying)
1380 continue;
1381
Jason Ekstranda7af7042013-10-12 22:38:11 -05001382 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001383 &output->region);
1384
1385 e = pixman_region32_extents(&region);
1386 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1387
1388 if (area > 0)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001389 mask |= 1u << output->id;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001390
1391 if (area >= max) {
1392 new_output = output;
1393 max = area;
1394 }
1395 }
1396 pixman_region32_fini(&region);
1397
Semi Malinene7a52fb2018-04-26 11:08:10 +02001398 weston_view_set_output(ev, new_output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001399 ev->output_mask = mask;
1400
1401 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001402}
1403
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001404static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001405weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1406 int from_x, int from_y, int *to_x, int *to_y)
1407{
1408 float x, y;
1409
1410 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1411 weston_view_from_global_float(to, x, y, &x, &y);
1412
1413 *to_x = round(x);
1414 *to_y = round(y);
1415}
1416
1417static void
1418weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1419{
1420 pixman_box32_t *a;
1421 pixman_box32_t b;
1422
1423 a = pixman_region32_extents(&from->geometry.scissor);
1424
1425 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1426 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1427
1428 pixman_region32_fini(&to->geometry.scissor);
1429 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1430}
1431
1432static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001433view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001434 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001435{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001436 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1437 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001438 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001439 { inbox->x1, inbox->y1 },
1440 { inbox->x1, inbox->y2 },
1441 { inbox->x2, inbox->y1 },
1442 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001443 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001444 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001445 int i;
1446
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001447 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001448 /* avoid rounding empty bbox to 1x1 */
1449 pixman_region32_init(bbox);
1450 return;
1451 }
1452
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001453 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001454 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001455 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001456 if (x < min_x)
1457 min_x = x;
1458 if (x > max_x)
1459 max_x = x;
1460 if (y < min_y)
1461 min_y = y;
1462 if (y > max_y)
1463 max_y = y;
1464 }
1465
Pekka Paalanen219b9822012-02-08 15:38:37 +02001466 int_x = floorf(min_x);
1467 int_y = floorf(min_y);
1468 pixman_region32_init_rect(bbox, int_x, int_y,
1469 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001470}
1471
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001472static void
Michael Olbrichcb04f982020-06-08 10:22:32 +02001473weston_view_update_transform_scissor(struct weston_view *view,
1474 pixman_region32_t *region)
1475{
1476 struct weston_view *parent = view->geometry.parent;
1477
1478 if (parent) {
1479 if (parent->geometry.scissor_enabled) {
1480 view->geometry.scissor_enabled = true;
1481 weston_view_transfer_scissor(parent, view);
1482 } else {
1483 view->geometry.scissor_enabled = false;
1484 }
1485 }
1486
1487 if (view->geometry.scissor_enabled)
1488 pixman_region32_intersect(region, region,
1489 &view->geometry.scissor);
1490}
1491static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001492weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001493{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001494 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001495
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001496 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001497 view->geometry.x = roundf(view->geometry.x);
1498 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001499
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001500 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001501 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1502 view->transform.position.matrix.d[12] = view->geometry.x;
1503 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001504
Jason Ekstranda7af7042013-10-12 22:38:11 -05001505 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001506
Jason Ekstranda7af7042013-10-12 22:38:11 -05001507 view->transform.inverse = view->transform.position.matrix;
1508 view->transform.inverse.d[12] = -view->geometry.x;
1509 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001510
Jason Ekstranda7af7042013-10-12 22:38:11 -05001511 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001512 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001513 view->surface->width,
1514 view->surface->height);
Michael Olbrichcb04f982020-06-08 10:22:32 +02001515
1516 weston_view_update_transform_scissor(view, &view->transform.boundingbox);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001517
1518 pixman_region32_translate(&view->transform.boundingbox,
1519 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001520
Jason Ekstranda7af7042013-10-12 22:38:11 -05001521 if (view->alpha == 1.0) {
1522 pixman_region32_copy(&view->transform.opaque,
1523 &view->surface->opaque);
Michael Olbrich0e4f0972020-08-19 11:31:03 +02001524 pixman_region32_intersect(&view->transform.opaque,
1525 &view->transform.opaque,
1526 &view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001527 pixman_region32_translate(&view->transform.opaque,
1528 view->geometry.x,
1529 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001530 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001531}
1532
1533static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001534weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001535{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001536 struct weston_view *parent = view->geometry.parent;
1537 struct weston_matrix *matrix = &view->transform.matrix;
1538 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001539 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001540 pixman_region32_t surfregion;
1541 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001542
Jason Ekstranda7af7042013-10-12 22:38:11 -05001543 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001544
1545 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001546 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1547 view->transform.position.matrix.d[12] = view->geometry.x;
1548 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001549
1550 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001551 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001552 weston_matrix_multiply(matrix, &tform->matrix);
1553
Pekka Paalanen483243f2013-03-08 14:56:50 +02001554 if (parent)
1555 weston_matrix_multiply(matrix, &parent->transform.matrix);
1556
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001557 if (weston_matrix_invert(inverse, matrix) < 0) {
1558 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001559 weston_log("error: weston_view %p"
1560 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001561 return -1;
1562 }
1563
Pekka Paalanen380adf52015-02-16 14:39:11 +02001564 pixman_region32_init_rect(&surfregion, 0, 0,
1565 view->surface->width, view->surface->height);
Michael Olbrichcb04f982020-06-08 10:22:32 +02001566
1567 weston_view_update_transform_scissor(view, &surfregion);
1568
Pekka Paalanen380adf52015-02-16 14:39:11 +02001569 surfbox = pixman_region32_extents(&surfregion);
1570
1571 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1572 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001573
Michael Olbrich0e4f0972020-08-19 11:31:03 +02001574 if (view->alpha == 1.0 &&
1575 matrix->type == WESTON_MATRIX_TRANSFORM_TRANSLATE) {
1576 pixman_region32_copy(&view->transform.opaque,
1577 &view->surface->opaque);
1578 pixman_region32_intersect(&view->transform.opaque,
1579 &view->transform.opaque,
1580 &view->geometry.scissor);
1581 pixman_region32_translate(&view->transform.opaque,
1582 matrix->d[12],
1583 matrix->d[13]);
1584 }
1585
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001586 return 0;
1587}
1588
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001589static struct weston_layer *
1590get_view_layer(struct weston_view *view)
1591{
1592 if (view->parent_view)
1593 return get_view_layer(view->parent_view);
1594 return view->layer_link.layer;
1595}
1596
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001597WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001598weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001599{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001600 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001601 struct weston_layer *layer;
1602 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001603
Jason Ekstranda7af7042013-10-12 22:38:11 -05001604 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001605 return;
1606
Pekka Paalanen483243f2013-03-08 14:56:50 +02001607 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001608 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001609
Jason Ekstranda7af7042013-10-12 22:38:11 -05001610 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001611
Jason Ekstranda7af7042013-10-12 22:38:11 -05001612 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001613
Jason Ekstranda7af7042013-10-12 22:38:11 -05001614 pixman_region32_fini(&view->transform.boundingbox);
1615 pixman_region32_fini(&view->transform.opaque);
1616 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001617
Pekka Paalanencd403622012-01-25 13:37:39 +02001618 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001619 if (view->geometry.transformation_list.next ==
1620 &view->transform.position.link &&
1621 view->geometry.transformation_list.prev ==
1622 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001623 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001624 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001625 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001626 if (weston_view_update_transform_enable(view) < 0)
1627 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001628 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001629
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001630 layer = get_view_layer(view);
1631 if (layer) {
1632 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001633 pixman_region32_intersect(&view->transform.boundingbox,
1634 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001635 pixman_region32_intersect(&view->transform.opaque,
1636 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001637 pixman_region32_fini(&mask);
1638 }
1639
Jason Ekstranda7af7042013-10-12 22:38:11 -05001640 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001641
Jason Ekstranda7af7042013-10-12 22:38:11 -05001642 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001643
Jason Ekstranda7af7042013-10-12 22:38:11 -05001644 wl_signal_emit(&view->surface->compositor->transform_signal,
1645 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001646}
1647
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001648WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001649weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001650{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001651 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001652
1653 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001654 * The invariant: if view->geometry.dirty, then all views
1655 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001656 * Corollary: if not parent->geometry.dirty, then all ancestors
1657 * are not dirty.
1658 */
1659
Jason Ekstranda7af7042013-10-12 22:38:11 -05001660 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001661 return;
1662
Jason Ekstranda7af7042013-10-12 22:38:11 -05001663 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001664
Jason Ekstranda7af7042013-10-12 22:38:11 -05001665 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001666 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001667 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001668}
1669
1670WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001671weston_view_to_global_fixed(struct weston_view *view,
1672 wl_fixed_t vx, wl_fixed_t vy,
1673 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001674{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001675 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001676
Jason Ekstranda7af7042013-10-12 22:38:11 -05001677 weston_view_to_global_float(view,
1678 wl_fixed_to_double(vx),
1679 wl_fixed_to_double(vy),
1680 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001681 *x = wl_fixed_from_double(xf);
1682 *y = wl_fixed_from_double(yf);
1683}
1684
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001685WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001686weston_view_from_global_float(struct weston_view *view,
1687 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001688{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001689 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001690 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1691
Jason Ekstranda7af7042013-10-12 22:38:11 -05001692 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001693
1694 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001695 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001696 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001697 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001698 *vx = 0;
1699 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001700 return;
1701 }
1702
Jason Ekstranda7af7042013-10-12 22:38:11 -05001703 *vx = v.f[0] / v.f[3];
1704 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001705 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001706 *vx = x - view->geometry.x;
1707 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001708 }
1709}
1710
1711WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001712weston_view_from_global_fixed(struct weston_view *view,
1713 wl_fixed_t x, wl_fixed_t y,
1714 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001715{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001716 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001717
Jason Ekstranda7af7042013-10-12 22:38:11 -05001718 weston_view_from_global_float(view,
1719 wl_fixed_to_double(x),
1720 wl_fixed_to_double(y),
1721 &vxf, &vyf);
1722 *vx = wl_fixed_from_double(vxf);
1723 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001724}
1725
1726WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001727weston_view_from_global(struct weston_view *view,
1728 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001729{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001730 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001731
Jason Ekstranda7af7042013-10-12 22:38:11 -05001732 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1733 *vx = floorf(vxf);
1734 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001735}
1736
Bryce Harrington3f650b82015-12-23 11:01:58 -08001737/**
1738 * \param surface The surface to be repainted
1739 *
1740 * Marks the output(s) that the surface is shown on as needing to be
1741 * repainted. See weston_output_schedule_repaint().
1742 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001743WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001744weston_surface_schedule_repaint(struct weston_surface *surface)
1745{
1746 struct weston_output *output;
1747
1748 wl_list_for_each(output, &surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001749 if (surface->output_mask & (1u << output->id))
Kristian Høgsberg98238702012-08-03 16:29:12 -04001750 weston_output_schedule_repaint(output);
1751}
1752
Bryce Harrington3f650b82015-12-23 11:01:58 -08001753/**
1754 * \param view The view to be repainted
1755 *
1756 * Marks the output(s) that the view is shown on as needing to be
1757 * repainted. See weston_output_schedule_repaint().
1758 */
Kristian Høgsberg98238702012-08-03 16:29:12 -04001759WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001760weston_view_schedule_repaint(struct weston_view *view)
1761{
1762 struct weston_output *output;
1763
1764 wl_list_for_each(output, &view->surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001765 if (view->output_mask & (1u << output->id))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001766 weston_output_schedule_repaint(output);
1767}
1768
Pekka Paalanene508ce62015-02-19 13:59:55 +02001769/**
1770 * XXX: This function does it the wrong way.
1771 * surface->damage is the damage from the client, and causes
1772 * surface_flush_damage() to copy pixels. No window management action can
1773 * cause damage to the client-provided content, warranting re-upload!
1774 *
1775 * Instead of surface->damage, this function should record the damage
1776 * with all the views for this surface to avoid extraneous texture
1777 * uploads.
1778 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001779WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001780weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001781{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001782 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001783 0, 0, surface->width,
1784 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001785
Kristian Høgsberg98238702012-08-03 16:29:12 -04001786 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001787}
1788
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001789WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001790weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001791{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001792 if (view->geometry.x == x && view->geometry.y == y)
1793 return;
1794
Jason Ekstranda7af7042013-10-12 22:38:11 -05001795 view->geometry.x = x;
1796 view->geometry.y = y;
1797 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001798}
1799
Pekka Paalanen483243f2013-03-08 14:56:50 +02001800static void
1801transform_parent_handle_parent_destroy(struct wl_listener *listener,
1802 void *data)
1803{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001804 struct weston_view *view =
1805 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001806 geometry.parent_destroy_listener);
1807
Jason Ekstranda7af7042013-10-12 22:38:11 -05001808 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001809}
1810
1811WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001812weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001813 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001814{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001815 if (view->geometry.parent) {
1816 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1817 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001818
1819 if (!parent)
1820 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001821 }
1822
Jason Ekstranda7af7042013-10-12 22:38:11 -05001823 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001824
Jason Ekstranda7af7042013-10-12 22:38:11 -05001825 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001826 transform_parent_handle_parent_destroy;
1827 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001828 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001829 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001830 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001831 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001832 }
1833
Jason Ekstranda7af7042013-10-12 22:38:11 -05001834 weston_view_geometry_dirty(view);
1835}
1836
Pekka Paalanen380adf52015-02-16 14:39:11 +02001837/** Set a clip mask rectangle on a view
1838 *
1839 * \param view The view to set the clip mask on.
1840 * \param x Top-left corner X coordinate of the clip rectangle.
1841 * \param y Top-left corner Y coordinate of the clip rectangle.
1842 * \param width Width of the clip rectangle, non-negative.
1843 * \param height Height of the clip rectangle, non-negative.
1844 *
1845 * A shell may set a clip mask rectangle on a view. Everything outside
1846 * the rectangle is cut away for input and output purposes: it is
1847 * not drawn and cannot be hit by hit-test based input like pointer
1848 * motion or touch-downs. Everything inside the rectangle will behave
1849 * normally. Clients are unaware of clipping.
1850 *
Yong Bakos4c72e292016-04-28 11:59:10 -05001851 * The rectangle is set in surface-local coordinates. Setting a clip
Pekka Paalanen380adf52015-02-16 14:39:11 +02001852 * mask rectangle does not affect the view position, the view is positioned
1853 * as it would be without a clip. The clip also does not change
1854 * weston_surface::width,height.
1855 *
1856 * The clip mask rectangle is part of transformation inheritance
1857 * (weston_view_set_transform_parent()). A clip set in the root of the
1858 * transformation inheritance tree will affect all views in the tree.
1859 * A clip can be set only on the root view. Attempting to set a clip
1860 * on view that has a transformation parent will fail. Assigning a parent
1861 * to a view that has a clip set will cause the clip to be forgotten.
1862 *
1863 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1864 * on the additional transformations in the child views. These transformations
1865 * may not rotate the coordinate axes, i.e., only translation and scaling
1866 * are allowed. Violating this restriction causes the clipping to malfunction.
1867 * Furthermore, using scaling may cause rounding errors in child clipping.
1868 *
1869 * The clip mask rectangle is not automatically adjusted based on
1870 * wl_surface.attach dx and dy arguments.
1871 *
1872 * A clip mask rectangle can be set only if the compositor capability
1873 * WESTON_CAP_VIEW_CLIP_MASK is present.
1874 *
1875 * This function sets the clip mask rectangle and schedules a repaint for
1876 * the view.
1877 */
1878WL_EXPORT void
1879weston_view_set_mask(struct weston_view *view,
1880 int x, int y, int width, int height)
1881{
1882 struct weston_compositor *compositor = view->surface->compositor;
1883
1884 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1885 weston_log("%s not allowed without capability!\n", __func__);
1886 return;
1887 }
1888
1889 if (view->geometry.parent) {
1890 weston_log("view %p has a parent, clip forbidden!\n", view);
1891 return;
1892 }
1893
1894 if (width < 0 || height < 0) {
1895 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1896 x, y, width, height);
1897 return;
1898 }
1899
1900 pixman_region32_fini(&view->geometry.scissor);
1901 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1902 view->geometry.scissor_enabled = true;
1903 weston_view_geometry_dirty(view);
1904 weston_view_schedule_repaint(view);
1905}
1906
1907/** Remove the clip mask from a view
1908 *
1909 * \param view The view to remove the clip mask from.
1910 *
1911 * Removed the clip mask rectangle and schedules a repaint.
1912 *
1913 * \sa weston_view_set_mask
1914 */
1915WL_EXPORT void
1916weston_view_set_mask_infinite(struct weston_view *view)
1917{
1918 view->geometry.scissor_enabled = false;
1919 weston_view_geometry_dirty(view);
1920 weston_view_schedule_repaint(view);
1921}
1922
Armin Krezović0da12b82016-06-30 06:04:33 +02001923/* Check if view should be displayed
1924 *
1925 * The indicator is set manually when assigning
1926 * a view to a surface.
1927 *
1928 * This needs reworking. See the thread starting at:
1929 *
1930 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1931 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001932WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001933weston_view_is_mapped(struct weston_view *view)
1934{
Armin Krezović0da12b82016-06-30 06:04:33 +02001935 return view->is_mapped;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001936}
1937
Philipp Zabel70decd52018-09-03 20:11:15 +02001938/* Check if view is opaque in specified region
1939 *
1940 * \param view The view to check for opacity.
1941 * \param region The region to check for opacity, in view coordinates.
1942 *
1943 * Returns true if the view is opaque in the specified region, because view
1944 * alpha is 1.0 and either the opaque region set by the client contains the
1945 * specified region, or the buffer pixel format or solid color is opaque.
1946 */
1947WL_EXPORT bool
1948weston_view_is_opaque(struct weston_view *ev, pixman_region32_t *region)
1949{
1950 pixman_region32_t r;
1951 bool ret = false;
1952
1953 if (ev->alpha < 1.0)
1954 return false;
1955
1956 if (ev->surface->is_opaque)
1957 return true;
1958
Marius Vlad67b382c2020-08-21 13:57:54 +03001959 if (ev->transform.dirty)
Philipp Zabel70decd52018-09-03 20:11:15 +02001960 return false;
Philipp Zabel70decd52018-09-03 20:11:15 +02001961
1962 pixman_region32_init(&r);
1963 pixman_region32_subtract(&r, region, &ev->transform.opaque);
1964
1965 if (!pixman_region32_not_empty(&r))
1966 ret = true;
1967
1968 pixman_region32_fini(&r);
1969
1970 return ret;
1971}
1972
Marius Vlad5f6bee42019-09-11 16:41:04 +03001973/** Check if the view has a valid buffer available
1974 *
1975 * @param ev The view to check if it has a valid buffer.
1976 *
1977 * Returns true if the view has a valid buffer or false otherwise.
1978 */
1979WL_EXPORT bool
1980weston_view_has_valid_buffer(struct weston_view *ev)
1981{
1982 return ev->surface->buffer_ref.buffer != NULL;
1983}
1984
Marius Vlad47e3d1e2019-09-11 16:53:08 +03001985/** Check if the view matches the entire output
1986 *
1987 * @param ev The view to check.
1988 * @param output The output to check against.
1989 *
1990 * Returns true if the view does indeed matches the entire output.
1991 */
1992WL_EXPORT bool
1993weston_view_matches_output_entirely(struct weston_view *ev,
1994 struct weston_output *output)
1995{
1996 pixman_box32_t *extents =
1997 pixman_region32_extents(&ev->transform.boundingbox);
1998
1999 if (extents->x1 != output->x ||
2000 extents->y1 != output->y ||
2001 extents->x2 != output->x + output->width ||
2002 extents->y2 != output->y + output->height)
2003 return false;
2004
2005 return true;
2006}
2007
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002008/** Find paint node for the given view and output
2009 */
2010WL_EXPORT struct weston_paint_node *
2011weston_view_find_paint_node(struct weston_view *view,
2012 struct weston_output *output)
2013{
2014 struct weston_paint_node *pnode;
2015
2016 wl_list_for_each(pnode, &view->paint_node_list, view_link) {
2017 assert(pnode->surface == view->surface);
2018 if (pnode->output == output)
2019 return pnode;
2020 }
2021
2022 return NULL;
2023}
2024
Armin Krezović0da12b82016-06-30 06:04:33 +02002025/* Check if a surface has a view assigned to it
2026 *
2027 * The indicator is set manually when mapping
2028 * a surface and creating a view for it.
2029 *
2030 * This needs to go. See the thread starting at:
2031 *
2032 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
2033 *
2034 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05002035WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03002036weston_surface_is_mapped(struct weston_surface *surface)
2037{
Armin Krezović0da12b82016-06-30 06:04:33 +02002038 return surface->is_mapped;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03002039}
2040
Pekka Paalanenda75ee12013-11-26 18:19:43 +01002041static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06002042surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002043{
2044 struct weston_view *view;
2045
2046 if (surface->width == width && surface->height == height)
2047 return;
2048
2049 surface->width = width;
2050 surface->height = height;
2051
2052 wl_list_for_each(view, &surface->views, surface_link)
2053 weston_view_geometry_dirty(view);
2054}
2055
Jason Ekstrand5c11a332013-12-04 20:32:03 -06002056WL_EXPORT void
2057weston_surface_set_size(struct weston_surface *surface,
2058 int32_t width, int32_t height)
2059{
2060 assert(!surface->resource);
2061 surface_set_size(surface, width, height);
2062}
2063
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02002064static int
2065fixed_round_up_to_int(wl_fixed_t f)
2066{
2067 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
2068}
2069
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002070static void
Pekka Paalanen59987fa2016-04-26 15:50:59 +03002071convert_size_by_transform_scale(int32_t *width_out, int32_t *height_out,
2072 int32_t width, int32_t height,
2073 uint32_t transform,
2074 int32_t scale)
2075{
2076 assert(scale > 0);
2077
2078 switch (transform) {
2079 case WL_OUTPUT_TRANSFORM_NORMAL:
2080 case WL_OUTPUT_TRANSFORM_180:
2081 case WL_OUTPUT_TRANSFORM_FLIPPED:
2082 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2083 *width_out = width / scale;
2084 *height_out = height / scale;
2085 break;
2086 case WL_OUTPUT_TRANSFORM_90:
2087 case WL_OUTPUT_TRANSFORM_270:
2088 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2089 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2090 *width_out = height / scale;
2091 *height_out = width / scale;
2092 break;
2093 default:
2094 assert(0 && "invalid transform");
2095 }
2096}
2097
2098static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002099weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002100{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002101 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01002102
2103 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02002104 surface->width_from_buffer = 0;
2105 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01002106 return;
2107 }
2108
Pekka Paalanen59987fa2016-04-26 15:50:59 +03002109 convert_size_by_transform_scale(&surface->width_from_buffer,
2110 &surface->height_from_buffer,
2111 surface->buffer_ref.buffer->width,
2112 surface->buffer_ref.buffer->height,
2113 vp->buffer.transform,
2114 vp->buffer.scale);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002115}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02002116
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002117static void
2118weston_surface_update_size(struct weston_surface *surface)
2119{
2120 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
2121 int32_t width, height;
2122
2123 width = surface->width_from_buffer;
2124 height = surface->height_from_buffer;
2125
2126 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02002127 surface_set_size(surface,
2128 vp->surface.width, vp->surface.height);
2129 return;
2130 }
2131
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002132 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03002133 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
2134 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
2135
2136 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02002137 return;
2138 }
2139
Jason Ekstrand5c11a332013-12-04 20:32:03 -06002140 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002141}
2142
Marius Vlad9fdda7f2019-06-11 16:08:55 +03002143/** weston_compositor_get_time
2144 * \ingroup compositor
2145 */
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02002146WL_EXPORT void
2147weston_compositor_get_time(struct timespec *time)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05002148{
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02002149 clock_gettime(CLOCK_REALTIME, time);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05002150}
2151
Marius Vlad9fdda7f2019-06-11 16:08:55 +03002152/** weston_compositor_pick_view
2153 * \ingroup compositor
2154 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002155WL_EXPORT struct weston_view *
2156weston_compositor_pick_view(struct weston_compositor *compositor,
2157 wl_fixed_t x, wl_fixed_t y,
2158 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02002159{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002160 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02002161 wl_fixed_t view_x, view_y;
2162 int view_ix, view_iy;
2163 int ix = wl_fixed_to_int(x);
2164 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02002165
Pekka Paalanen6551c092021-05-03 16:09:45 +03002166 /* Can't use paint node list: occlusion by input regions, not opaque. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002167 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02002168 if (!pixman_region32_contains_point(
2169 &view->transform.boundingbox, ix, iy, NULL))
2170 continue;
2171
2172 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
2173 view_ix = wl_fixed_to_int(view_x);
2174 view_iy = wl_fixed_to_int(view_y);
2175
2176 if (!pixman_region32_contains_point(&view->surface->input,
2177 view_ix, view_iy, NULL))
2178 continue;
2179
Pekka Paalanen380adf52015-02-16 14:39:11 +02002180 if (view->geometry.scissor_enabled &&
2181 !pixman_region32_contains_point(&view->geometry.scissor,
2182 view_ix, view_iy, NULL))
2183 continue;
2184
Pekka Paalanenfc22a522015-02-18 15:08:29 +02002185 *vx = view_x;
2186 *vy = view_y;
2187 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02002188 }
2189
Derek Foremanf9318d12015-05-11 15:40:11 -05002190 *vx = wl_fixed_from_int(-1000000);
2191 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02002192 return NULL;
2193}
2194
2195static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002196weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002197{
Daniel Stone37816df2012-05-16 18:45:18 +01002198 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002199
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07002200 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002201 return;
2202
Daniel Stone37816df2012-05-16 18:45:18 +01002203 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04002204 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002205}
2206
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002207WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002208weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05002209{
Daniel Stone4dab5db2012-05-30 16:31:53 +01002210 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002211
Jason Ekstranda7af7042013-10-12 22:38:11 -05002212 if (!weston_view_is_mapped(view))
2213 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002214
Jason Ekstranda7af7042013-10-12 22:38:11 -05002215 weston_view_damage_below(view);
Semi Malinene7a52fb2018-04-26 11:08:10 +02002216 weston_view_set_output(view, NULL);
Xiong Zhang97116532013-10-23 13:58:31 +08002217 view->plane = NULL;
Armin Krezovićf8486c32016-06-30 06:04:28 +02002218 view->is_mapped = false;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002219 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002220 wl_list_remove(&view->link);
2221 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002222 view->output_mask = 0;
2223 weston_surface_assign_output(view->surface);
2224
2225 if (weston_surface_is_mapped(view->surface))
2226 return;
2227
2228 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002229 struct weston_touch *touch = weston_seat_get_touch(seat);
2230 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2231 struct weston_keyboard *keyboard =
2232 weston_seat_get_keyboard(seat);
2233
2234 if (keyboard && keyboard->focus == view->surface)
2235 weston_keyboard_set_focus(keyboard, NULL);
2236 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05002237 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05002238 if (touch && touch->focus == view)
2239 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01002240 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002241}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002242
Jason Ekstranda7af7042013-10-12 22:38:11 -05002243WL_EXPORT void
2244weston_surface_unmap(struct weston_surface *surface)
2245{
2246 struct weston_view *view;
2247
Armin Krezovićf8486c32016-06-30 06:04:28 +02002248 surface->is_mapped = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002249 wl_list_for_each(view, &surface->views, surface_link)
2250 weston_view_unmap(view);
2251 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05002252}
2253
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002254static void
2255weston_surface_reset_pending_buffer(struct weston_surface *surface)
2256{
Jason Ekstrand7b982072014-05-20 14:33:03 -05002257 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002258 surface->pending.sx = 0;
2259 surface->pending.sy = 0;
2260 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002261 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002262}
2263
Jason Ekstranda7af7042013-10-12 22:38:11 -05002264WL_EXPORT void
2265weston_view_destroy(struct weston_view *view)
2266{
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002267 struct weston_paint_node *pnode, *pntmp;
2268
Jason Ekstranda7af7042013-10-12 22:38:11 -05002269 wl_signal_emit(&view->destroy_signal, view);
2270
2271 assert(wl_list_empty(&view->geometry.child_list));
2272
2273 if (weston_view_is_mapped(view)) {
2274 weston_view_unmap(view);
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002275 weston_compositor_build_view_list(view->surface->compositor,
2276 NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002277 }
2278
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002279 wl_list_for_each_safe(pnode, pntmp, &view->paint_node_list, view_link)
2280 weston_paint_node_destroy(pnode);
2281
Jason Ekstranda7af7042013-10-12 22:38:11 -05002282 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002283 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002284
2285 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02002286 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002287 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002288 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002289
2290 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen944fae82018-05-22 13:15:58 +03002291 weston_view_set_output(view, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002292
Jason Ekstranda7af7042013-10-12 22:38:11 -05002293 wl_list_remove(&view->surface_link);
2294
2295 free(view);
2296}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002297
2298WL_EXPORT void
2299weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05002300{
Vlad Zahorodniic2b97472021-07-29 11:16:51 +03002301 struct wl_resource *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002302 struct weston_view *ev, *nv;
Jonas Ådahld3414f22016-07-22 17:56:31 +08002303 struct weston_pointer_constraint *constraint, *next_constraint;
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002304 struct weston_paint_node *pnode, *pntmp;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04002305
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02002306 if (--surface->ref_count > 0)
2307 return;
2308
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03002309 assert(surface->resource == NULL);
2310
Pekka Paalanenca790762015-04-17 14:23:38 +03002311 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02002312
Pekka Paalanene67858b2013-04-25 13:57:42 +03002313 assert(wl_list_empty(&surface->subsurface_list_pending));
2314 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02002315
Leandro Ribeiro8eb84142021-01-18 19:36:48 -03002316 if (surface->dmabuf_feedback)
2317 weston_dmabuf_feedback_destroy(surface->dmabuf_feedback);
2318
Jason Ekstranda7af7042013-10-12 22:38:11 -05002319 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
2320 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002321
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002322 wl_list_for_each_safe(pnode, pntmp,
2323 &surface->paint_node_list, surface_link) {
2324 weston_paint_node_destroy(pnode);
2325 }
2326
Jason Ekstrand7b982072014-05-20 14:33:03 -05002327 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002328
Pekka Paalanende685b82012-12-04 15:58:12 +02002329 weston_buffer_reference(&surface->buffer_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002330 weston_buffer_release_reference(&surface->buffer_release_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04002331
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02002332 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002333 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002334 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02002335
Vlad Zahorodniic2b97472021-07-29 11:16:51 +03002336 wl_resource_for_each_safe(cb, next, &surface->frame_callback_list)
2337 wl_resource_destroy(cb);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002338
Pekka Paalanen133e4392014-09-23 22:08:46 -04002339 weston_presentation_feedback_discard_list(&surface->feedback_list);
2340
Jonas Ådahld3414f22016-07-22 17:56:31 +08002341 wl_list_for_each_safe(constraint, next_constraint,
2342 &surface->pointer_constraints,
2343 link)
2344 weston_pointer_constraint_destroy(constraint);
2345
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03002346 fd_clear(&surface->acquire_fence_fd);
2347
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04002348 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05002349}
2350
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002351static void
2352destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08002353{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002354 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08002355
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03002356 assert(surface);
2357
Giulio Camuffo0d379742013-11-15 22:06:15 +01002358 /* Set the resource to NULL, since we don't want to leave a
2359 * dangling pointer if the surface was refcounted and survives
2360 * the weston_surface_destroy() call. */
2361 surface->resource = NULL;
Pekka Paalanen4826f872016-04-22 14:14:38 +03002362
2363 if (surface->viewport_resource)
2364 wl_resource_set_user_data(surface->viewport_resource, NULL);
2365
Alexandros Frantzis27d7c392018-10-19 12:14:11 +03002366 if (surface->synchronization_resource) {
2367 wl_resource_set_user_data(surface->synchronization_resource,
2368 NULL);
2369 }
2370
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002371 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08002372}
2373
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002374static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002375weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
2376{
2377 struct weston_buffer *buffer =
2378 container_of(listener, struct weston_buffer, destroy_listener);
2379
2380 wl_signal_emit(&buffer->destroy_signal, buffer);
2381 free(buffer);
2382}
2383
Giulio Camuffoe058cd12013-12-12 14:14:29 +01002384WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002385weston_buffer_from_resource(struct wl_resource *resource)
2386{
2387 struct weston_buffer *buffer;
2388 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002389
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002390 listener = wl_resource_get_destroy_listener(resource,
2391 weston_buffer_destroy_handler);
2392
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002393 if (listener)
2394 return container_of(listener, struct weston_buffer,
2395 destroy_listener);
2396
2397 buffer = zalloc(sizeof *buffer);
2398 if (buffer == NULL)
2399 return NULL;
2400
2401 buffer->resource = resource;
2402 wl_signal_init(&buffer->destroy_signal);
2403 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04002404 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002405 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002406
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002407 return buffer;
2408}
2409
2410static void
Pekka Paalanende685b82012-12-04 15:58:12 +02002411weston_buffer_reference_handle_destroy(struct wl_listener *listener,
2412 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002413{
Pekka Paalanende685b82012-12-04 15:58:12 +02002414 struct weston_buffer_reference *ref =
2415 container_of(listener, struct weston_buffer_reference,
2416 destroy_listener);
2417
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002418 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02002419 ref->buffer = NULL;
2420}
2421
2422WL_EXPORT void
2423weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002424 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002425{
2426 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05002427 ref->buffer->busy_count--;
2428 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002429 assert(wl_resource_get_client(ref->buffer->resource));
Matt Hoosier3052bc72017-09-26 08:09:40 -05002430 wl_buffer_send_release(ref->buffer->resource);
Kristian Høgsberg20347802013-03-04 12:07:46 -05002431 }
Pekka Paalanende685b82012-12-04 15:58:12 +02002432 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002433 }
2434
Pekka Paalanende685b82012-12-04 15:58:12 +02002435 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002436 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002437 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02002438 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02002439 }
2440
Pekka Paalanende685b82012-12-04 15:58:12 +02002441 ref->buffer = buffer;
2442 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
2443}
2444
2445static void
Alexandros Frantzis67629672018-10-19 12:14:11 +03002446weston_buffer_release_reference_handle_destroy(struct wl_listener *listener,
2447 void *data)
2448{
2449 struct weston_buffer_release_reference *ref =
2450 container_of(listener, struct weston_buffer_release_reference,
2451 destroy_listener);
2452
2453 assert((struct wl_resource *)data == ref->buffer_release->resource);
2454 ref->buffer_release = NULL;
2455}
2456
2457static void
2458weston_buffer_release_destroy(struct weston_buffer_release *buffer_release)
2459{
2460 struct wl_resource *resource = buffer_release->resource;
2461 int release_fence_fd = buffer_release->fence_fd;
2462
2463 if (release_fence_fd >= 0) {
2464 zwp_linux_buffer_release_v1_send_fenced_release(
2465 resource, release_fence_fd);
2466 } else {
2467 zwp_linux_buffer_release_v1_send_immediate_release(
2468 resource);
2469 }
2470
2471 wl_resource_destroy(resource);
2472}
2473
2474WL_EXPORT void
2475weston_buffer_release_reference(struct weston_buffer_release_reference *ref,
2476 struct weston_buffer_release *buffer_release)
2477{
2478 if (buffer_release == ref->buffer_release)
2479 return;
2480
2481 if (ref->buffer_release) {
2482 ref->buffer_release->ref_count--;
2483 wl_list_remove(&ref->destroy_listener.link);
2484 if (ref->buffer_release->ref_count == 0)
2485 weston_buffer_release_destroy(ref->buffer_release);
2486 }
2487
2488 if (buffer_release) {
2489 buffer_release->ref_count++;
2490 wl_resource_add_destroy_listener(buffer_release->resource,
2491 &ref->destroy_listener);
2492 }
2493
2494 ref->buffer_release = buffer_release;
2495 ref->destroy_listener.notify =
2496 weston_buffer_release_reference_handle_destroy;
2497}
2498
2499WL_EXPORT void
2500weston_buffer_release_move(struct weston_buffer_release_reference *dest,
2501 struct weston_buffer_release_reference *src)
2502{
2503 weston_buffer_release_reference(dest, src->buffer_release);
2504 weston_buffer_release_reference(src, NULL);
2505}
2506
2507static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002508weston_surface_attach(struct weston_surface *surface,
2509 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002510{
2511 weston_buffer_reference(&surface->buffer_ref, buffer);
2512
Pekka Paalanena6421c42012-12-04 15:58:10 +02002513 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002514 if (weston_surface_is_mapped(surface))
2515 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002516 }
2517
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002518 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02002519
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002520 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002521 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002522}
2523
Marius Vlad9fdda7f2019-06-11 16:08:55 +03002524/** weston_compositor_damage_all
2525 * \ingroup compositor
2526 */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002527WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002528weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002529{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002530 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002531
2532 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002533 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002534}
2535
Marius Vlad55d87362019-06-11 01:15:35 +03002536/**
2537 * \ingroup output
2538 */
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05002539WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002540weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002541{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002542 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002543
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002544 pixman_region32_union(&compositor->primary_plane.damage,
2545 &compositor->primary_plane.damage,
2546 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002547 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002548}
2549
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04002550static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002551surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002552{
Pekka Paalanende685b82012-12-04 15:58:12 +02002553 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002554 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002555 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002556
Marius Vlad2a1b7862019-09-05 13:12:18 +03002557 if (pixman_region32_not_empty(&surface->damage))
Marius Vlad3203ff62019-09-05 14:56:12 +03002558 TL_POINT(surface->compositor, "core_flush_damage", TLP_SURFACE(surface),
Pekka Paalanenb5026542014-11-12 15:09:24 +02002559 TLP_OUTPUT(surface->output), TLP_END);
2560
Jason Ekstrandef540082014-06-26 10:37:36 -07002561 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002562}
2563
2564static void
2565view_accumulate_damage(struct weston_view *view,
2566 pixman_region32_t *opaque)
2567{
2568 pixman_region32_t damage;
2569
2570 pixman_region32_init(&damage);
2571 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002572 pixman_box32_t *extents;
2573
Jason Ekstranda7af7042013-10-12 22:38:11 -05002574 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002575 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002576 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002577 pixman_region32_copy(&damage, &view->surface->damage);
2578 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002579 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002580 }
2581
Pekka Paalanen380adf52015-02-16 14:39:11 +02002582 pixman_region32_intersect(&damage, &damage,
2583 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002584 pixman_region32_subtract(&damage, &damage, opaque);
2585 pixman_region32_union(&view->plane->damage,
2586 &view->plane->damage, &damage);
2587 pixman_region32_fini(&damage);
2588 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002589 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002590}
2591
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002592static void
Michael Olbrichc5ea4952020-08-11 12:42:35 +02002593output_accumulate_damage(struct weston_output *output)
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002594{
Michael Olbrichc5ea4952020-08-11 12:42:35 +02002595 struct weston_compositor *ec = output->compositor;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002596 struct weston_plane *plane;
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002597 struct weston_paint_node *pnode;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002598 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002599
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002600 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002601
2602 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002603 pixman_region32_copy(&plane->clip, &clip);
2604
2605 pixman_region32_init(&opaque);
2606
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002607 wl_list_for_each(pnode, &output->paint_node_z_order_list,
2608 z_order_link) {
2609 if (pnode->view->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002610 continue;
2611
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002612 view_accumulate_damage(pnode->view, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002613 }
2614
2615 pixman_region32_union(&clip, &clip, &opaque);
2616 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002617 }
2618
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002619 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002620
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002621 wl_list_for_each(pnode, &output->paint_node_z_order_list,
2622 z_order_link) {
2623 pnode->surface->touched = false;
2624 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002625
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002626 wl_list_for_each(pnode, &output->paint_node_z_order_list,
2627 z_order_link) {
Michael Olbrichc5ea4952020-08-11 12:42:35 +02002628 /* Ignore views not visible on the current output */
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002629 /* TODO: turn this into assert once z_order_list is pruned. */
2630 if (!(pnode->view->output_mask & (1u << output->id)))
Michael Olbrichc5ea4952020-08-11 12:42:35 +02002631 continue;
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002632 if (pnode->surface->touched)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002633 continue;
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002634 pnode->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002635
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002636 surface_flush_damage(pnode->surface);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002637
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002638 /* Both the renderer and the backend have seen the buffer
2639 * by now. If renderer needs the buffer, it has its own
2640 * reference set. If the backend wants to keep the buffer
2641 * around for migrating the surface into a non-primary plane
2642 * later, keep_buffer is true. Otherwise, drop the core
2643 * reference now, and allow early buffer release. This enables
2644 * clients to use single-buffering.
2645 */
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002646 if (!pnode->surface->keep_buffer) {
2647 weston_buffer_reference(&pnode->surface->buffer_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002648 weston_buffer_release_reference(
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002649 &pnode->surface->buffer_release_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002650 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002651 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002652}
2653
2654static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002655surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002656{
2657 struct weston_subsurface *sub;
2658
Pekka Paalanene67858b2013-04-25 13:57:42 +03002659 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002660 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002661 continue;
2662
Jason Ekstranda7af7042013-10-12 22:38:11 -05002663 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2664 wl_list_init(&sub->surface->views);
2665
2666 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002667 }
2668}
2669
2670static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002671surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002672{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002673 struct weston_subsurface *sub;
2674 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002675
Jason Ekstranda7af7042013-10-12 22:38:11 -05002676 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2677 if (sub->surface == surface)
2678 continue;
2679
George Kiagiadakised04d382014-06-13 18:10:26 +02002680 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2681 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002682 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002683 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002684
2685 surface_free_unused_subsurface_views(sub->surface);
2686 }
2687}
2688
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002689static struct weston_paint_node *
2690view_ensure_paint_node(struct weston_view *view, struct weston_output *output)
2691{
2692 struct weston_paint_node *pnode;
2693
2694 if (!output)
2695 return NULL;
2696
2697 pnode = weston_view_find_paint_node(view, output);
2698 if (pnode)
2699 return pnode;
2700
2701 return weston_paint_node_create(view->surface, view, output);
2702}
2703
Jason Ekstranda7af7042013-10-12 22:38:11 -05002704static void
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002705add_to_z_order_list(struct weston_output *output,
2706 struct weston_paint_node *pnode)
2707{
2708 if (!pnode)
2709 return;
2710
2711 wl_list_remove(&pnode->z_order_link);
2712 wl_list_insert(output->paint_node_z_order_list.prev,
2713 &pnode->z_order_link);
Pekka Paalanen90a5ffa2021-02-25 12:03:28 +02002714
2715 /*
2716 * Building weston_output::paint_node_z_order_list ensures all
2717 * necessary color transform objects are installed.
2718 */
2719 weston_paint_node_ensure_color_transform(pnode);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002720}
2721
2722static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002723view_list_add_subsurface_view(struct weston_compositor *compositor,
2724 struct weston_subsurface *sub,
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002725 struct weston_view *parent,
2726 struct weston_output *output)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002727{
2728 struct weston_subsurface *child;
2729 struct weston_view *view = NULL, *iv;
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002730 struct weston_paint_node *pnode;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002731
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002732 if (!weston_surface_is_mapped(sub->surface))
2733 return;
2734
Jason Ekstranda7af7042013-10-12 22:38:11 -05002735 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2736 if (iv->geometry.parent == parent) {
2737 view = iv;
2738 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002739 }
2740 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002741
2742 if (view) {
2743 /* Put it back in the surface's list of views */
2744 wl_list_remove(&view->surface_link);
2745 wl_list_insert(&sub->surface->views, &view->surface_link);
2746 } else {
2747 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002748 weston_view_set_position(view,
2749 sub->position.x,
2750 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002751 weston_view_set_transform_parent(view, parent);
2752 }
2753
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002754 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002755 weston_view_update_transform(view);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002756 view->is_mapped = true;
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002757 pnode = view_ensure_paint_node(view, output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002758
Pekka Paalanenb188e912013-11-19 14:03:35 +02002759 if (wl_list_empty(&sub->surface->subsurface_list)) {
2760 wl_list_insert(compositor->view_list.prev, &view->link);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002761 add_to_z_order_list(output, pnode);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002762 return;
2763 }
2764
2765 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002766 if (child->surface == sub->surface) {
Pekka Paalanenb188e912013-11-19 14:03:35 +02002767 wl_list_insert(compositor->view_list.prev, &view->link);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002768 add_to_z_order_list(output, pnode);
2769 } else {
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002770 view_list_add_subsurface_view(compositor, child, view, output);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002771 }
Pekka Paalanenb188e912013-11-19 14:03:35 +02002772 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002773}
2774
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002775/* This recursively adds the sub-surfaces for a view, relying on the
2776 * sub-surface order. Thus, if a client restacks the sub-surfaces, that
2777 * change first happens to the sub-surface list, and then automatically
2778 * propagates here. See weston_surface_damage_subsurfaces() for how the
2779 * sub-surfaces receive damage when the client changes the state.
2780 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002781static void
2782view_list_add(struct weston_compositor *compositor,
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002783 struct weston_view *view,
2784 struct weston_output *output)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002785{
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002786 struct weston_paint_node *pnode;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002787 struct weston_subsurface *sub;
2788
2789 weston_view_update_transform(view);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002790 pnode = view_ensure_paint_node(view, output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002791
Pekka Paalanenb188e912013-11-19 14:03:35 +02002792 if (wl_list_empty(&view->surface->subsurface_list)) {
2793 wl_list_insert(compositor->view_list.prev, &view->link);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002794 add_to_z_order_list(output, pnode);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002795 return;
2796 }
2797
2798 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002799 if (sub->surface == view->surface) {
Pekka Paalanenb188e912013-11-19 14:03:35 +02002800 wl_list_insert(compositor->view_list.prev, &view->link);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002801 add_to_z_order_list(output, pnode);
2802 } else {
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002803 view_list_add_subsurface_view(compositor, sub, view, output);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002804 }
Pekka Paalanenb188e912013-11-19 14:03:35 +02002805 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002806}
2807
2808static void
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002809weston_compositor_build_view_list(struct weston_compositor *compositor,
2810 struct weston_output *output)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002811{
Loïc Yhuel267b16e2019-09-17 20:14:56 +02002812 struct weston_view *view, *tmp;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002813 struct weston_layer *layer;
2814
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002815 if (output) {
2816 wl_list_remove(&output->paint_node_z_order_list);
2817 wl_list_init(&output->paint_node_z_order_list);
2818 }
2819
Jason Ekstranda7af7042013-10-12 22:38:11 -05002820 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002821 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002822 surface_stash_subsurface_views(view->surface);
2823
Loïc Yhuel267b16e2019-09-17 20:14:56 +02002824 wl_list_for_each_safe(view, tmp, &compositor->view_list, link)
2825 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002826 wl_list_init(&compositor->view_list);
Loïc Yhuel267b16e2019-09-17 20:14:56 +02002827
Jason Ekstranda7af7042013-10-12 22:38:11 -05002828 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002829 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002830 view_list_add(compositor, view, output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002831 }
2832 }
2833
2834 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002835 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002836 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002837}
2838
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002839static void
2840weston_output_take_feedback_list(struct weston_output *output,
2841 struct weston_surface *surface)
2842{
2843 struct weston_view *view;
2844 struct weston_presentation_feedback *feedback;
2845 uint32_t flags = 0xffffffff;
2846
2847 if (wl_list_empty(&surface->feedback_list))
2848 return;
2849
2850 /* All views must have the flag for the flag to survive. */
2851 wl_list_for_each(view, &surface->views, surface_link) {
2852 /* ignore views that are not on this output at all */
2853 if (view->output_mask & (1u << output->id))
2854 flags &= view->psf_flags;
2855 }
2856
2857 wl_list_for_each(feedback, &surface->feedback_list, link)
2858 feedback->psf_flags = flags;
2859
2860 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2861 wl_list_init(&surface->feedback_list);
2862}
2863
David Herrmann1edf44c2013-10-22 17:11:26 +02002864static int
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002865weston_output_repaint(struct weston_output *output, void *repaint_data)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002866{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002867 struct weston_compositor *ec = output->compositor;
Pekka Paalanen6d6b3672021-05-03 14:06:55 +03002868 struct weston_paint_node *pnode;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002869 struct weston_animation *animation, *next;
Vlad Zahorodniic2b97472021-07-29 11:16:51 +03002870 struct wl_resource *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002871 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002872 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002873 int r;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002874 uint32_t frame_time_msec;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05302875 enum weston_hdcp_protection highest_requested = WESTON_HDCP_DISABLE;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002876
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002877 if (output->destroying)
2878 return 0;
2879
Marius Vlad3203ff62019-09-05 14:56:12 +03002880 TL_POINT(ec, "core_repaint_begin", TLP_OUTPUT(output), TLP_END);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002881
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002882 /* Rebuild the surface list and update surface transforms up front. */
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002883 weston_compositor_build_view_list(ec, output);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002884
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05302885 /* Find the highest protection desired for an output */
Pekka Paalanen6d6b3672021-05-03 14:06:55 +03002886 wl_list_for_each(pnode, &output->paint_node_z_order_list,
2887 z_order_link) {
2888 /* TODO: turn this into assert once z_order_list is pruned. */
2889 if ((pnode->surface->output_mask & (1u << output->id)) == 0)
2890 continue;
2891
2892 /*
2893 * The desired_protection of the output should be the
2894 * maximum of the desired_protection of the surfaces,
2895 * that are displayed on that output, to avoid
2896 * reducing the protection for existing surfaces.
2897 */
2898 if (pnode->surface->desired_protection > highest_requested)
2899 highest_requested = pnode->surface->desired_protection;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05302900 }
2901
2902 output->desired_protection = highest_requested;
2903
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002904 if (output->assign_planes && !output->disable_planes) {
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002905 output->assign_planes(output, repaint_data);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002906 } else {
Pekka Paalanen6d6b3672021-05-03 14:06:55 +03002907 wl_list_for_each(pnode, &output->paint_node_z_order_list,
2908 z_order_link) {
2909 weston_view_move_to_plane(pnode->view, &ec->primary_plane);
2910 pnode->view->psf_flags = 0;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002911 }
2912 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002913
Pekka Paalanene67858b2013-04-25 13:57:42 +03002914 wl_list_init(&frame_callback_list);
Pekka Paalanen6d6b3672021-05-03 14:06:55 +03002915 wl_list_for_each(pnode, &output->paint_node_z_order_list,
2916 z_order_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002917 /* Note: This operation is safe to do multiple times on the
2918 * same surface.
2919 */
Pekka Paalanen6d6b3672021-05-03 14:06:55 +03002920 if (pnode->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002921 wl_list_insert_list(&frame_callback_list,
Pekka Paalanen6d6b3672021-05-03 14:06:55 +03002922 &pnode->surface->frame_callback_list);
2923 wl_list_init(&pnode->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002924
Pekka Paalanen6d6b3672021-05-03 14:06:55 +03002925 weston_output_take_feedback_list(output, pnode->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002926 }
2927 }
2928
Michael Olbrichc5ea4952020-08-11 12:42:35 +02002929 output_accumulate_damage(output);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002930
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002931 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002932 pixman_region32_intersect(&output_damage,
2933 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002934 pixman_region32_subtract(&output_damage,
2935 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002936
Scott Moreauccbf29d2012-02-22 14:21:41 -07002937 if (output->dirty)
2938 weston_output_update_matrix(output);
2939
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002940 r = output->repaint(output, &output_damage, repaint_data);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002941
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002942 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002943
Daniel Stone09a97e22017-03-01 11:34:06 +00002944 output->repaint_needed = false;
Daniel Stone05df8c12017-03-03 16:59:42 +00002945 if (r == 0)
2946 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002947
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002948 weston_compositor_repick(ec);
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002949
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002950 frame_time_msec = timespec_to_msec(&output->frame_time);
2951
Vlad Zahorodniic2b97472021-07-29 11:16:51 +03002952 wl_resource_for_each_safe(cb, cnext, &frame_callback_list) {
2953 wl_callback_send_done(cb, frame_time_msec);
2954 wl_resource_destroy(cb);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002955 }
2956
Scott Moreaud64cf212012-06-08 19:40:54 -06002957 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002958 animation->frame_counter++;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002959 animation->frame(animation, output, &output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002960 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002961
Marius Vlad3203ff62019-09-05 14:56:12 +03002962 TL_POINT(ec, "core_repaint_posted", TLP_OUTPUT(output), TLP_END);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002963
David Herrmann1edf44c2013-10-22 17:11:26 +02002964 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002965}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002966
Pekka Paalanen82919792014-05-21 13:51:49 +03002967static void
2968weston_output_schedule_repaint_reset(struct weston_output *output)
2969{
Daniel Stone05df8c12017-03-03 16:59:42 +00002970 output->repaint_status = REPAINT_NOT_SCHEDULED;
Marius Vlad3203ff62019-09-05 14:56:12 +03002971 TL_POINT(output->compositor, "core_repaint_exit_loop",
2972 TLP_OUTPUT(output), TLP_END);
Pekka Paalanen82919792014-05-21 13:51:49 +03002973}
2974
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002975static int
2976weston_output_maybe_repaint(struct weston_output *output, struct timespec *now,
2977 void *repaint_data)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002978{
Pekka Paalanen0513a952014-05-21 16:17:27 +03002979 struct weston_compositor *compositor = output->compositor;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002980 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002981 int64_t msec_to_repaint;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002982
Daniel Stone6847b852017-03-01 11:34:08 +00002983 /* We're not ready yet; come back to make a decision later. */
2984 if (output->repaint_status != REPAINT_SCHEDULED)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002985 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002986
2987 msec_to_repaint = timespec_sub_to_msec(&output->next_repaint, now);
2988 if (msec_to_repaint > 1)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002989 return ret;
Daniel Stone05df8c12017-03-03 16:59:42 +00002990
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002991 /* If we're sleeping, drop the repaint machinery entirely; we will
2992 * explicitly repaint all outputs when we come back. */
2993 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2994 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
2995 goto err;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002996
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002997 /* We don't actually need to repaint this output; drop it from
2998 * repaint until something causes damage. */
2999 if (!output->repaint_needed)
3000 goto err;
3001
3002 /* If repaint fails, we aren't going to get weston_output_finish_frame
3003 * to trigger a new repaint, so drop it from repaint and hope
Daniel Stone6847b852017-03-01 11:34:08 +00003004 * something schedules a successful repaint later. As repainting may
3005 * take some time, re-read our clock as a courtesy to the next
3006 * output. */
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003007 ret = weston_output_repaint(output, repaint_data);
Daniel Stone6847b852017-03-01 11:34:08 +00003008 weston_compositor_read_presentation_clock(compositor, now);
Daniel Stonecd1a1c32017-01-16 15:38:54 +00003009 if (ret != 0)
3010 goto err;
3011
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09003012 output->repainted = true;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003013 return ret;
Daniel Stonecd1a1c32017-01-16 15:38:54 +00003014
3015err:
Pekka Paalanen0513a952014-05-21 16:17:27 +03003016 weston_output_schedule_repaint_reset(output);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003017 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00003018}
3019
3020static void
3021output_repaint_timer_arm(struct weston_compositor *compositor)
3022{
3023 struct weston_output *output;
3024 bool any_should_repaint = false;
3025 struct timespec now;
Sergi Granellb4c08862017-03-18 13:01:15 +01003026 int64_t msec_to_next = INT64_MAX;
Daniel Stone6847b852017-03-01 11:34:08 +00003027
3028 weston_compositor_read_presentation_clock(compositor, &now);
3029
3030 wl_list_for_each(output, &compositor->output_list, link) {
3031 int64_t msec_to_this;
3032
3033 if (output->repaint_status != REPAINT_SCHEDULED)
3034 continue;
3035
3036 msec_to_this = timespec_sub_to_msec(&output->next_repaint,
3037 &now);
3038 if (!any_should_repaint || msec_to_this < msec_to_next)
3039 msec_to_next = msec_to_this;
3040
3041 any_should_repaint = true;
3042 }
3043
3044 if (!any_should_repaint)
3045 return;
3046
3047 /* Even if we should repaint immediately, add the minimum 1 ms delay.
3048 * This is a workaround to allow coalescing multiple output repaints
3049 * particularly from weston_output_finish_frame()
3050 * into the same call, which would not happen if we called
3051 * output_repaint_timer_handler() directly.
3052 */
3053 if (msec_to_next < 1)
3054 msec_to_next = 1;
3055
3056 wl_event_source_timer_update(compositor->repaint_timer, msec_to_next);
3057}
3058
3059static int
3060output_repaint_timer_handler(void *data)
3061{
3062 struct weston_compositor *compositor = data;
3063 struct weston_output *output;
3064 struct timespec now;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003065 void *repaint_data = NULL;
Emre Ucane479ed82018-03-20 15:29:40 +01003066 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00003067
3068 weston_compositor_read_presentation_clock(compositor, &now);
Pekka Paalanen1ed2cad2021-02-10 12:33:03 +02003069 compositor->last_repaint_start = now;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003070
3071 if (compositor->backend->repaint_begin)
3072 repaint_data = compositor->backend->repaint_begin(compositor);
3073
3074 wl_list_for_each(output, &compositor->output_list, link) {
3075 ret = weston_output_maybe_repaint(output, &now, repaint_data);
3076 if (ret)
3077 break;
3078 }
3079
3080 if (ret == 0) {
Tomohito Esaki09bfcd62018-06-05 10:37:05 +09003081 if (compositor->backend->repaint_flush)
Antonio Borneoc90fccc2019-06-30 15:51:10 +02003082 ret = compositor->backend->repaint_flush(compositor,
3083 repaint_data);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003084 } else {
Antonio Borneoc90fccc2019-06-30 15:51:10 +02003085 if (compositor->backend->repaint_cancel)
3086 compositor->backend->repaint_cancel(compositor,
3087 repaint_data);
3088 }
3089
3090 if (ret != 0) {
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09003091 wl_list_for_each(output, &compositor->output_list, link) {
3092 if (output->repainted)
3093 weston_output_schedule_repaint_reset(output);
3094 }
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003095 }
Daniel Stone6847b852017-03-01 11:34:08 +00003096
Tomohito Esakiddaf95c2018-07-10 11:47:15 +09003097 wl_list_for_each(output, &compositor->output_list, link)
3098 output->repainted = false;
3099
Daniel Stone6847b852017-03-01 11:34:08 +00003100 output_repaint_timer_arm(compositor);
3101
Pekka Paalanen0513a952014-05-21 16:17:27 +03003102 return 0;
3103}
3104
Pekka Paalanen50aa3a72020-05-28 11:34:04 +03003105/** Convert a presentation timestamp to another clock domain
3106 *
3107 * \param compositor The compositor defines the presentation clock domain.
3108 * \param presentation_stamp The timestamp in presentation clock domain.
3109 * \param presentation_now Current time in presentation clock domain.
3110 * \param target_clock Defines the target clock domain.
3111 *
3112 * This approximation relies on presentation_stamp to be close to current time.
3113 * The further it is from current time and the bigger the speed difference
3114 * between the two clock domains, the bigger the conversion error.
3115 *
3116 * Conversion error due to system load is biased and unbounded.
3117 */
3118static struct timespec
3119convert_presentation_time_now(struct weston_compositor *compositor,
3120 const struct timespec *presentation_stamp,
3121 const struct timespec *presentation_now,
3122 clockid_t target_clock)
3123{
3124 struct timespec target_now = {};
3125 struct timespec target_stamp;
3126 int64_t delta_ns;
3127
3128 if (compositor->presentation_clock == target_clock)
3129 return *presentation_stamp;
3130
3131 clock_gettime(target_clock, &target_now);
3132 delta_ns = timespec_sub_to_nsec(presentation_stamp, presentation_now);
3133 timespec_add_nsec(&target_stamp, &target_now, delta_ns);
3134
3135 return target_stamp;
3136}
3137
Marius Vlad55d87362019-06-11 01:15:35 +03003138/**
3139 * \ingroup output
3140 */
Kristian Høgsbergef044142011-06-21 15:02:12 -04003141WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04003142weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02003143 const struct timespec *stamp,
3144 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04003145{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003146 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03003147 int32_t refresh_nsec;
3148 struct timespec now;
Pekka Paalanen50aa3a72020-05-28 11:34:04 +03003149 struct timespec vblank_monotonic;
Daniel Stone6847b852017-03-01 11:34:08 +00003150 int64_t msec_rel;
Pekka Paalanen133e4392014-09-23 22:08:46 -04003151
Daniel Stone05df8c12017-03-03 16:59:42 +00003152 assert(output->repaint_status == REPAINT_AWAITING_COMPLETION);
Pekka Paalanen98b4e202021-05-10 11:33:23 +03003153
3154 /*
3155 * If timestamp of latest vblank is given, it must always go forwards.
3156 * If not given, INVALID flag must be set.
3157 */
3158 if (stamp)
3159 assert(timespec_sub_to_nsec(stamp, &output->frame_time) >= 0);
3160 else
3161 assert(presented_flags & WP_PRESENTATION_FEEDBACK_INVALID);
Daniel Stone3615ce12017-03-01 11:34:05 +00003162
Daniel Stone6847b852017-03-01 11:34:08 +00003163 weston_compositor_read_presentation_clock(compositor, &now);
3164
Daniel Stone3615ce12017-03-01 11:34:05 +00003165 /* If we haven't been supplied any timestamp at all, we don't have a
3166 * timebase to work against, so any delay just wastes time. Push a
3167 * repaint as soon as possible so we can get on with it. */
Daniel Stone6847b852017-03-01 11:34:08 +00003168 if (!stamp) {
3169 output->next_repaint = now;
Daniel Stone3615ce12017-03-01 11:34:05 +00003170 goto out;
Daniel Stone6847b852017-03-01 11:34:08 +00003171 }
Daniel Stone3615ce12017-03-01 11:34:05 +00003172
Pekka Paalanen50aa3a72020-05-28 11:34:04 +03003173 vblank_monotonic = convert_presentation_time_now(compositor,
3174 stamp, &now,
3175 CLOCK_MONOTONIC);
Marius Vlad3203ff62019-09-05 14:56:12 +03003176 TL_POINT(compositor, "core_repaint_finished", TLP_OUTPUT(output),
Pekka Paalanen50aa3a72020-05-28 11:34:04 +03003177 TLP_VBLANK(&vblank_monotonic), TLP_END);
Marius Vladdf9278a2018-03-06 18:56:23 +02003178
Pekka Paalanend7894d02015-07-03 15:08:53 +03003179 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003180 weston_presentation_feedback_present_list(&output->feedback_list,
3181 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02003182 output->msc,
3183 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003184
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02003185 output->frame_time = *stamp;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003186
Daniel Stone6847b852017-03-01 11:34:08 +00003187 timespec_add_nsec(&output->next_repaint, stamp, refresh_nsec);
3188 timespec_add_msec(&output->next_repaint, &output->next_repaint,
3189 -compositor->repaint_msec);
3190 msec_rel = timespec_sub_to_msec(&output->next_repaint, &now);
Daniel Stone84aff5c2017-03-01 11:34:04 +00003191
3192 if (msec_rel < -1000 || msec_rel > 1000) {
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02003193 static bool warned;
3194
3195 if (!warned)
3196 weston_log("Warning: computed repaint delay is "
Daniel Stone6847b852017-03-01 11:34:08 +00003197 "insane: %lld msec\n", (long long) msec_rel);
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02003198 warned = true;
3199
Daniel Stone6847b852017-03-01 11:34:08 +00003200 output->next_repaint = now;
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02003201 }
3202
Mario Kleinerb7df04e2015-06-21 21:25:15 +02003203 /* Called from restart_repaint_loop and restart happens already after
3204 * the deadline given by repaint_msec? In that case we delay until
3205 * the deadline of the next frame, to give clients a more predictable
3206 * timing of the repaint cycle to lock on. */
Daniel Stoneeca5cca2017-02-28 21:53:51 +00003207 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID &&
3208 msec_rel < 0) {
3209 while (timespec_sub_to_nsec(&output->next_repaint, &now) < 0) {
3210 timespec_add_nsec(&output->next_repaint,
3211 &output->next_repaint,
3212 refresh_nsec);
3213 }
3214 }
Mario Kleinerb7df04e2015-06-21 21:25:15 +02003215
Daniel Stone3615ce12017-03-01 11:34:05 +00003216out:
Daniel Stone05df8c12017-03-03 16:59:42 +00003217 output->repaint_status = REPAINT_SCHEDULED;
Daniel Stone6847b852017-03-01 11:34:08 +00003218 output_repaint_timer_arm(compositor);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003219}
3220
nerdopolisd2a320d2021-08-23 21:29:42 -04003221
3222WL_EXPORT void
3223weston_output_repaint_failed(struct weston_output *output)
3224{
3225 weston_log("Clearing repaint status.\n");
3226 assert(output->repaint_status == REPAINT_AWAITING_COMPLETION);
3227 output->repaint_status = REPAINT_NOT_SCHEDULED;
3228}
3229
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003230static void
3231idle_repaint(void *data)
3232{
3233 struct weston_output *output = data;
Antonio Borneoc90fccc2019-06-30 15:51:10 +02003234 int ret;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003235
Daniel Stone05df8c12017-03-03 16:59:42 +00003236 assert(output->repaint_status == REPAINT_BEGIN_FROM_IDLE);
3237 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03003238 output->idle_repaint_source = NULL;
Antonio Borneoc90fccc2019-06-30 15:51:10 +02003239 ret = output->start_repaint_loop(output);
3240 if (ret != 0)
3241 weston_output_schedule_repaint_reset(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003242}
3243
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003244WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003245weston_layer_entry_insert(struct weston_layer_entry *list,
3246 struct weston_layer_entry *entry)
3247{
3248 wl_list_insert(&list->link, &entry->link);
3249 entry->layer = list->layer;
3250}
3251
3252WL_EXPORT void
3253weston_layer_entry_remove(struct weston_layer_entry *entry)
3254{
3255 wl_list_remove(&entry->link);
3256 wl_list_init(&entry->link);
3257 entry->layer = NULL;
3258}
3259
Quentin Glidic82681572016-12-17 13:40:51 +01003260
3261/** Initialize the weston_layer struct.
3262 *
3263 * \param compositor The compositor instance
3264 * \param layer The layer to initialize
3265 */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003266WL_EXPORT void
Quentin Glidic82681572016-12-17 13:40:51 +01003267weston_layer_init(struct weston_layer *layer,
3268 struct weston_compositor *compositor)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003269{
Quentin Glidic82681572016-12-17 13:40:51 +01003270 layer->compositor = compositor;
3271 wl_list_init(&layer->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003272 wl_list_init(&layer->view_list.link);
3273 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003274 weston_layer_set_mask_infinite(layer);
Quentin Glidic82681572016-12-17 13:40:51 +01003275}
3276
Pekka Paalanen87400372021-05-14 14:29:40 +03003277/** Finalize the weston_layer struct.
3278 *
3279 * \param layer The layer to finalize.
3280 */
3281WL_EXPORT void
3282weston_layer_fini(struct weston_layer *layer)
3283{
3284 wl_list_remove(&layer->link);
3285
3286 if (!wl_list_empty(&layer->view_list.link))
3287 weston_log("BUG: finalizing a layer with views still on it.\n");
3288
3289 wl_list_remove(&layer->view_list.link);
3290}
3291
Quentin Glidic82681572016-12-17 13:40:51 +01003292/** Sets the position of the layer in the layer list. The layer will be placed
3293 * below any layer with the same position value, if any.
3294 * This function is safe to call if the layer is already on the list, but the
3295 * layer may be moved below other layers at the same position, if any.
3296 *
3297 * \param layer The layer to modify
3298 * \param position The position the layer will be placed at
3299 */
3300WL_EXPORT void
3301weston_layer_set_position(struct weston_layer *layer,
3302 enum weston_layer_position position)
3303{
3304 struct weston_layer *below;
3305
3306 wl_list_remove(&layer->link);
3307
3308 /* layer_list is ordered from top to bottom, the last layer being the
3309 * background with the smallest position value */
3310
3311 layer->position = position;
3312 wl_list_for_each_reverse(below, &layer->compositor->layer_list, link) {
3313 if (below->position >= layer->position) {
3314 wl_list_insert(&below->link, &layer->link);
3315 return;
3316 }
3317 }
3318 wl_list_insert(&layer->compositor->layer_list, &layer->link);
3319}
3320
3321/** Hide a layer by taking it off the layer list.
3322 * This function is safe to call if the layer is not on the list.
3323 *
3324 * \param layer The layer to hide
3325 */
3326WL_EXPORT void
3327weston_layer_unset_position(struct weston_layer *layer)
3328{
3329 wl_list_remove(&layer->link);
3330 wl_list_init(&layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003331}
3332
3333WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003334weston_layer_set_mask(struct weston_layer *layer,
3335 int x, int y, int width, int height)
3336{
3337 struct weston_view *view;
3338
3339 layer->mask.x1 = x;
3340 layer->mask.x2 = x + width;
3341 layer->mask.y1 = y;
3342 layer->mask.y2 = y + height;
3343
3344 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
3345 weston_view_geometry_dirty(view);
3346 }
3347}
3348
3349WL_EXPORT void
3350weston_layer_set_mask_infinite(struct weston_layer *layer)
3351{
Adam Jackson3c3f3b12019-10-16 16:02:59 -04003352 struct weston_view *view;
3353
3354 layer->mask.x1 = INT32_MIN;
3355 layer->mask.x2 = INT32_MAX;
3356 layer->mask.y1 = INT32_MIN;
3357 layer->mask.y2 = INT32_MAX;
3358
3359 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
3360 weston_view_geometry_dirty(view);
3361 }
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003362}
3363
Daniel Stone3b775632018-07-20 08:38:25 +01003364WL_EXPORT bool
3365weston_layer_mask_is_infinite(struct weston_layer *layer)
3366{
3367 return layer->mask.x1 == INT32_MIN &&
3368 layer->mask.y1 == INT32_MIN &&
Adam Jackson3c3f3b12019-10-16 16:02:59 -04003369 layer->mask.x2 == INT32_MAX &&
3370 layer->mask.y2 == INT32_MAX;
Daniel Stone3b775632018-07-20 08:38:25 +01003371}
3372
Marius Vlad55d87362019-06-11 01:15:35 +03003373/**
3374 * \ingroup output
3375 */
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003376WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003377weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003378{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003379 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04003380 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01003381
Bryce Harrington08976ac2016-08-30 12:05:16 -07003382 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
3383 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003384 return;
3385
Pekka Paalanenb5026542014-11-12 15:09:24 +02003386 if (!output->repaint_needed)
Marius Vlad3203ff62019-09-05 14:56:12 +03003387 TL_POINT(compositor, "core_repaint_req", TLP_OUTPUT(output), TLP_END);
Pekka Paalanenb5026542014-11-12 15:09:24 +02003388
Kristian Høgsbergef044142011-06-21 15:02:12 -04003389 loop = wl_display_get_event_loop(compositor->wl_display);
Daniel Stone09a97e22017-03-01 11:34:06 +00003390 output->repaint_needed = true;
Daniel Stone05df8c12017-03-03 16:59:42 +00003391
3392 /* If we already have a repaint scheduled for our idle handler,
3393 * no need to set it again. If the repaint has been called but
3394 * not finished, then weston_output_finish_frame() will notice
3395 * that a repaint is needed and schedule one. */
3396 if (output->repaint_status != REPAINT_NOT_SCHEDULED)
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003397 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01003398
Daniel Stone05df8c12017-03-03 16:59:42 +00003399 output->repaint_status = REPAINT_BEGIN_FROM_IDLE;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03003400 assert(!output->idle_repaint_source);
3401 output->idle_repaint_source = wl_event_loop_add_idle(loop, idle_repaint,
3402 output);
Marius Vlad3203ff62019-09-05 14:56:12 +03003403 TL_POINT(compositor, "core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003404}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05003405
Marius Vlad9fdda7f2019-06-11 16:08:55 +03003406/** weston_compositor_schedule_repaint
3407 * \ingroup compositor
3408 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003409WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003410weston_compositor_schedule_repaint(struct weston_compositor *compositor)
3411{
3412 struct weston_output *output;
3413
3414 wl_list_for_each(output, &compositor->output_list, link)
3415 weston_output_schedule_repaint(output);
3416}
3417
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003418static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003419surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003420{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003421 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003422}
3423
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003424static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003425surface_attach(struct wl_client *client,
3426 struct wl_resource *resource,
3427 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
3428{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003429 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05003430 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003431
Kristian Høgsbergab19f932013-08-20 11:30:54 -07003432 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05003433 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07003434 if (buffer == NULL) {
3435 wl_client_post_no_memory(client);
3436 return;
3437 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07003438 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04003439
Pekka Paalanende685b82012-12-04 15:58:12 +02003440 /* Attach, attach, without commit in between does not send
3441 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003442 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003443
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003444 surface->pending.sx = sx;
3445 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01003446 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003447}
3448
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003449static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003450surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003451 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003452 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05003453{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003454 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003455
Derek Foreman57e92ed2015-11-17 14:11:35 -06003456 if (width <= 0 || height <= 0)
3457 return;
3458
Derek Foreman152254b2015-11-26 14:17:48 -06003459 pixman_region32_union_rect(&surface->pending.damage_surface,
3460 &surface->pending.damage_surface,
3461 x, y, width, height);
3462}
3463
3464static void
3465surface_damage_buffer(struct wl_client *client,
3466 struct wl_resource *resource,
3467 int32_t x, int32_t y, int32_t width, int32_t height)
3468{
3469 struct weston_surface *surface = wl_resource_get_user_data(resource);
3470
3471 if (width <= 0 || height <= 0)
3472 return;
3473
3474 pixman_region32_union_rect(&surface->pending.damage_buffer,
3475 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04003476 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003477}
3478
Kristian Høgsberg33418202011-08-16 23:01:28 -04003479static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003480destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04003481{
Vlad Zahorodniic2b97472021-07-29 11:16:51 +03003482 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsberg33418202011-08-16 23:01:28 -04003483}
3484
3485static void
3486surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003487 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04003488{
Vlad Zahorodniic2b97472021-07-29 11:16:51 +03003489 struct wl_resource *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003490 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003491
Vlad Zahorodniic2b97472021-07-29 11:16:51 +03003492 cb = wl_resource_create(client, &wl_callback_interface, 1, callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003493 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003494 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003495 return;
3496 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03003497
Vlad Zahorodniic2b97472021-07-29 11:16:51 +03003498 wl_resource_set_implementation(cb, NULL, NULL,
Jason Ekstranda85118c2013-06-27 20:17:02 -05003499 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003500
Vlad Zahorodniic2b97472021-07-29 11:16:51 +03003501 wl_list_insert(surface->pending.frame_callback_list.prev,
3502 wl_resource_get_link(cb));
Kristian Høgsberg33418202011-08-16 23:01:28 -04003503}
3504
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003505static void
3506surface_set_opaque_region(struct wl_client *client,
3507 struct wl_resource *resource,
3508 struct wl_resource *region_resource)
3509{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003510 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003511 struct weston_region *region;
3512
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003513 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003514 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003515 pixman_region32_copy(&surface->pending.opaque,
3516 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003517 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07003518 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003519 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003520}
3521
3522static void
3523surface_set_input_region(struct wl_client *client,
3524 struct wl_resource *resource,
3525 struct wl_resource *region_resource)
3526{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003527 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003528 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003529
3530 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003531 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003532 pixman_region32_copy(&surface->pending.input,
3533 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003534 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003535 pixman_region32_fini(&surface->pending.input);
3536 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003537 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003538}
3539
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003540/* Cause damage to this sub-surface and all its children.
3541 *
3542 * This is useful when there are state changes that need an implicit
3543 * damage, e.g. a z-order change.
3544 */
3545static void
3546weston_surface_damage_subsurfaces(struct weston_subsurface *sub)
3547{
3548 struct weston_subsurface *child;
3549
3550 weston_surface_damage(sub->surface);
3551 sub->reordered = false;
3552
3553 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link)
3554 if (child != sub)
3555 weston_surface_damage_subsurfaces(child);
3556}
3557
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003558static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003559weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003560{
Pekka Paalanene67858b2013-04-25 13:57:42 +03003561 struct weston_subsurface *sub;
3562
3563 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
3564 parent_link_pending) {
3565 wl_list_remove(&sub->parent_link);
3566 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003567
3568 if (sub->reordered)
3569 weston_surface_damage_subsurfaces(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003570 }
3571}
3572
3573static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03003574weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05003575 struct weston_matrix *matrix)
3576{
Pekka Paalanen04baea52016-04-26 15:50:58 +03003577 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05003578 double src_width, src_height, dest_width, dest_height;
3579
3580 weston_matrix_init(matrix);
3581
3582 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
3583 src_width = surface->width_from_buffer;
3584 src_height = surface->height_from_buffer;
3585 } else {
3586 src_width = wl_fixed_to_double(vp->buffer.src_width);
3587 src_height = wl_fixed_to_double(vp->buffer.src_height);
3588 }
3589
3590 if (vp->surface.width == -1) {
3591 dest_width = src_width;
3592 dest_height = src_height;
3593 } else {
3594 dest_width = vp->surface.width;
3595 dest_height = vp->surface.height;
3596 }
3597
3598 if (src_width != dest_width || src_height != dest_height)
3599 weston_matrix_scale(matrix,
3600 src_width / dest_width,
3601 src_height / dest_height, 1);
3602
3603 if (vp->buffer.src_width != wl_fixed_from_int(-1))
3604 weston_matrix_translate(matrix,
3605 wl_fixed_to_double(vp->buffer.src_x),
3606 wl_fixed_to_double(vp->buffer.src_y),
3607 0);
3608
3609 switch (vp->buffer.transform) {
3610 case WL_OUTPUT_TRANSFORM_FLIPPED:
3611 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3612 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3613 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3614 weston_matrix_scale(matrix, -1, 1, 1);
3615 weston_matrix_translate(matrix,
3616 surface->width_from_buffer, 0, 0);
3617 break;
3618 }
3619
3620 switch (vp->buffer.transform) {
3621 default:
3622 case WL_OUTPUT_TRANSFORM_NORMAL:
3623 case WL_OUTPUT_TRANSFORM_FLIPPED:
3624 break;
3625 case WL_OUTPUT_TRANSFORM_90:
3626 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +02003627 weston_matrix_rotate_xy(matrix, 0, -1);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003628 weston_matrix_translate(matrix,
Pekka Paalanen8060d822020-02-06 15:27:54 +02003629 0, surface->width_from_buffer, 0);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003630 break;
3631 case WL_OUTPUT_TRANSFORM_180:
3632 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3633 weston_matrix_rotate_xy(matrix, -1, 0);
3634 weston_matrix_translate(matrix,
3635 surface->width_from_buffer,
3636 surface->height_from_buffer, 0);
3637 break;
3638 case WL_OUTPUT_TRANSFORM_270:
3639 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +02003640 weston_matrix_rotate_xy(matrix, 0, 1);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003641 weston_matrix_translate(matrix,
Pekka Paalanen8060d822020-02-06 15:27:54 +02003642 surface->height_from_buffer, 0, 0);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003643 break;
3644 }
3645
3646 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
3647}
3648
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003649/**
3650 * Compute a + b > c while being safe to overflows.
3651 */
3652static bool
3653fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
3654{
3655 return (int64_t)a + (int64_t)b > (int64_t)c;
3656}
3657
3658static bool
3659weston_surface_is_pending_viewport_source_valid(
3660 const struct weston_surface *surface)
3661{
3662 const struct weston_surface_state *pend = &surface->pending;
3663 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
3664 int width_from_buffer = 0;
3665 int height_from_buffer = 0;
3666 wl_fixed_t w;
3667 wl_fixed_t h;
3668
3669 /* If viewport source rect is not set, it is always ok. */
3670 if (vp->buffer.src_width == wl_fixed_from_int(-1))
3671 return true;
3672
3673 if (pend->newly_attached) {
3674 if (pend->buffer) {
3675 convert_size_by_transform_scale(&width_from_buffer,
3676 &height_from_buffer,
3677 pend->buffer->width,
3678 pend->buffer->height,
3679 vp->buffer.transform,
3680 vp->buffer.scale);
3681 } else {
3682 /* No buffer: viewport is irrelevant. */
3683 return true;
3684 }
3685 } else {
3686 width_from_buffer = surface->width_from_buffer;
3687 height_from_buffer = surface->height_from_buffer;
3688 }
3689
3690 assert((width_from_buffer == 0) == (height_from_buffer == 0));
3691 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
3692
3693 /* No buffer: viewport is irrelevant. */
3694 if (width_from_buffer == 0 || height_from_buffer == 0)
3695 return true;
3696
3697 /* overflow checks for wl_fixed_from_int() */
3698 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
3699 return false;
3700 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
3701 return false;
3702
3703 w = wl_fixed_from_int(width_from_buffer);
3704 h = wl_fixed_from_int(height_from_buffer);
3705
3706 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
3707 return false;
3708 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
3709 return false;
3710
3711 return true;
3712}
3713
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003714static bool
3715fixed_is_integer(wl_fixed_t v)
3716{
3717 return (v & 0xff) == 0;
3718}
3719
3720static bool
3721weston_surface_is_pending_viewport_dst_size_int(
3722 const struct weston_surface *surface)
3723{
3724 const struct weston_buffer_viewport *vp =
3725 &surface->pending.buffer_viewport;
3726
3727 if (vp->surface.width != -1) {
3728 assert(vp->surface.width > 0 && vp->surface.height > 0);
3729 return true;
3730 }
3731
3732 return fixed_is_integer(vp->buffer.src_width) &&
3733 fixed_is_integer(vp->buffer.src_height);
3734}
3735
Derek Foreman152254b2015-11-26 14:17:48 -06003736/* Translate pending damage in buffer co-ordinates to surface
3737 * co-ordinates and union it with a pixman_region32_t.
3738 * This should only be called after the buffer is attached.
3739 */
3740static void
3741apply_damage_buffer(pixman_region32_t *dest,
3742 struct weston_surface *surface,
3743 struct weston_surface_state *state)
3744{
3745 struct weston_buffer *buffer = surface->buffer_ref.buffer;
3746
3747 /* wl_surface.damage_buffer needs to be clipped to the buffer,
3748 * translated into surface co-ordinates and unioned with
3749 * any other surface damage.
3750 * None of this makes sense if there is no buffer though.
3751 */
3752 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
3753 pixman_region32_t buffer_damage;
3754
3755 pixman_region32_intersect_rect(&state->damage_buffer,
3756 &state->damage_buffer,
3757 0, 0, buffer->width,
3758 buffer->height);
3759 pixman_region32_init(&buffer_damage);
3760 weston_matrix_transform_region(&buffer_damage,
3761 &surface->buffer_to_surface_matrix,
3762 &state->damage_buffer);
3763 pixman_region32_union(dest, dest, &buffer_damage);
3764 pixman_region32_fini(&buffer_damage);
3765 }
3766 /* We should clear this on commit even if there was no buffer */
3767 pixman_region32_clear(&state->damage_buffer);
3768}
3769
Jason Ekstrand1e059042014-10-16 10:55:19 -05003770static void
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303771weston_surface_set_desired_protection(struct weston_surface *surface,
3772 enum weston_hdcp_protection protection)
3773{
3774 if (surface->desired_protection == protection)
3775 return;
3776 surface->desired_protection = protection;
3777 weston_surface_damage(surface);
3778}
3779
3780static void
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303781weston_surface_set_protection_mode(struct weston_surface *surface,
3782 enum weston_surface_protection_mode p_mode)
3783{
3784 struct content_protection *cp = surface->compositor->content_protection;
3785 struct protected_surface *psurface;
3786
3787 surface->protection_mode = p_mode;
3788 wl_list_for_each(psurface, &cp->protected_list, link) {
3789 if (!psurface || psurface->surface != surface)
3790 continue;
3791 weston_protected_surface_send_event(psurface,
3792 surface->current_protection);
3793 }
3794}
3795
3796static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05003797weston_surface_commit_state(struct weston_surface *surface,
3798 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003799{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003800 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003801 pixman_region32_t opaque;
3802
Alexander Larsson4ea95522013-05-22 14:41:37 +02003803 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02003804 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03003805 /* wp_viewport.set_source */
3806 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003807 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003808
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003809 /* wl_surface.attach */
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003810 if (state->newly_attached) {
3811 /* zwp_surface_synchronization_v1.set_acquire_fence */
3812 fd_move(&surface->acquire_fence_fd,
3813 &state->acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003814 /* zwp_surface_synchronization_v1.get_release */
3815 weston_buffer_release_move(&surface->buffer_release_ref,
3816 &state->buffer_release_ref);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003817 weston_surface_attach(surface, state->buffer);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003818 }
Jason Ekstrand7b982072014-05-20 14:33:03 -05003819 weston_surface_state_set_buffer(state, NULL);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003820 assert(state->acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003821 assert(state->buffer_release_ref.buffer_release == NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01003822
Jason Ekstrand1e059042014-10-16 10:55:19 -05003823 weston_surface_build_buffer_matrix(surface,
3824 &surface->surface_to_buffer_matrix);
3825 weston_matrix_invert(&surface->buffer_to_surface_matrix,
3826 &surface->surface_to_buffer_matrix);
3827
Jason Ekstrand7b982072014-05-20 14:33:03 -05003828 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003829 weston_surface_update_size(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003830 if (surface->committed)
3831 surface->committed(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003832 }
Giulio Camuffo184df502013-02-21 11:29:21 +01003833
Jason Ekstrand7b982072014-05-20 14:33:03 -05003834 state->sx = 0;
3835 state->sy = 0;
3836 state->newly_attached = 0;
3837 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03003838
Derek Foreman152254b2015-11-26 14:17:48 -06003839 /* wl_surface.damage and wl_surface.damage_buffer */
Marius Vlad2a1b7862019-09-05 13:12:18 +03003840 if (pixman_region32_not_empty(&state->damage_surface) ||
3841 pixman_region32_not_empty(&state->damage_buffer))
Marius Vlad3203ff62019-09-05 14:56:12 +03003842 TL_POINT(surface->compositor, "core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06003843
Pekka Paalanen8e159182012-10-10 12:49:25 +03003844 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06003845 &state->damage_surface);
3846
3847 apply_damage_buffer(&surface->damage, surface, state);
3848
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05003849 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07003850 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06003851 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003852
3853 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003854 pixman_region32_init(&opaque);
3855 pixman_region32_intersect_rect(&opaque, &state->opaque,
3856 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003857
3858 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
3859 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003860 wl_list_for_each(view, &surface->views, surface_link)
3861 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003862 }
3863
3864 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003865
3866 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003867 pixman_region32_intersect_rect(&surface->input, &state->input,
3868 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003869
Pekka Paalanenbc106382012-10-10 12:49:31 +03003870 /* wl_surface.frame */
3871 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05003872 &state->frame_callback_list);
3873 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003874
3875 /* XXX:
3876 * What should happen with a feedback request, if there
3877 * is no wl_buffer attached for this commit?
3878 */
3879
3880 /* presentation.feedback */
3881 wl_list_insert_list(&surface->feedback_list,
3882 &state->feedback_list);
3883 wl_list_init(&state->feedback_list);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003884
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303885 /* weston_protected_surface.enforced/relaxed */
3886 if (surface->protection_mode != state->protection_mode)
3887 weston_surface_set_protection_mode(surface,
3888 state->protection_mode);
3889
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303890 /* weston_protected_surface.set_type */
3891 weston_surface_set_desired_protection(surface, state->desired_protection);
3892
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003893 wl_signal_emit(&surface->commit_signal, surface);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003894}
3895
3896static void
3897weston_surface_commit(struct weston_surface *surface)
3898{
3899 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003900
Pekka Paalanene67858b2013-04-25 13:57:42 +03003901 weston_surface_commit_subsurface_order(surface);
3902
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003903 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003904}
3905
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003906static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003907weston_subsurface_commit(struct weston_subsurface *sub);
3908
3909static void
3910weston_subsurface_parent_commit(struct weston_subsurface *sub,
3911 int parent_is_synchronized);
3912
3913static void
3914surface_commit(struct wl_client *client, struct wl_resource *resource)
3915{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003916 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003917 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
3918
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003919 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
3920 assert(surface->viewport_resource);
3921
3922 wl_resource_post_error(surface->viewport_resource,
3923 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
3924 "wl_surface@%d has viewport source outside buffer",
3925 wl_resource_get_id(resource));
3926 return;
3927 }
3928
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003929 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
3930 assert(surface->viewport_resource);
3931
3932 wl_resource_post_error(surface->viewport_resource,
3933 WP_VIEWPORT_ERROR_BAD_SIZE,
3934 "wl_surface@%d viewport dst size not integer",
3935 wl_resource_get_id(resource));
3936 return;
3937 }
3938
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003939 if (surface->pending.acquire_fence_fd >= 0) {
3940 assert(surface->synchronization_resource);
3941
3942 if (!surface->pending.buffer) {
3943 fd_clear(&surface->pending.acquire_fence_fd);
3944 wl_resource_post_error(surface->synchronization_resource,
3945 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3946 "wl_surface@%"PRIu32" no buffer for synchronization",
3947 wl_resource_get_id(resource));
3948 return;
3949 }
3950
3951 /* We support fences for both wp_linux_dmabuf and opaque EGL
3952 * buffers, as mandated by minor version 2 of the
3953 * zwp_linux_explicit_synchronization_v1 protocol. Since
3954 * renderers that support fences currently only support these
3955 * two buffer types plus SHM buffers, we can just check for the
3956 * SHM buffer case here.
3957 */
3958 if (wl_shm_buffer_get(surface->pending.buffer->resource)) {
3959 fd_clear(&surface->pending.acquire_fence_fd);
3960 wl_resource_post_error(surface->synchronization_resource,
3961 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_UNSUPPORTED_BUFFER,
3962 "wl_surface@%"PRIu32" unsupported buffer for synchronization",
3963 wl_resource_get_id(resource));
3964 return;
3965 }
3966 }
3967
Alexandros Frantzis67629672018-10-19 12:14:11 +03003968 if (surface->pending.buffer_release_ref.buffer_release &&
3969 !surface->pending.buffer) {
3970 assert(surface->synchronization_resource);
3971
3972 wl_resource_post_error(surface->synchronization_resource,
3973 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3974 "wl_surface@%"PRIu32" no buffer for synchronization",
3975 wl_resource_get_id(resource));
3976 return;
3977 }
3978
Pekka Paalanene67858b2013-04-25 13:57:42 +03003979 if (sub) {
3980 weston_subsurface_commit(sub);
3981 return;
3982 }
3983
3984 weston_surface_commit(surface);
3985
3986 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
3987 if (sub->surface != surface)
3988 weston_subsurface_parent_commit(sub, 0);
3989 }
3990}
3991
3992static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003993surface_set_buffer_transform(struct wl_client *client,
3994 struct wl_resource *resource, int transform)
3995{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003996 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003997
Jonny Lamba55f1392014-05-30 12:07:15 +02003998 /* if wl_output.transform grows more members this will need to be updated. */
3999 if (transform < 0 ||
4000 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
4001 wl_resource_post_error(resource,
4002 WL_SURFACE_ERROR_INVALID_TRANSFORM,
4003 "buffer transform must be a valid transform "
4004 "('%d' specified)", transform);
4005 return;
4006 }
4007
Pekka Paalanen952b6c82014-03-14 14:38:15 +02004008 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004009 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004010}
4011
Alexander Larsson4ea95522013-05-22 14:41:37 +02004012static void
4013surface_set_buffer_scale(struct wl_client *client,
4014 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004015 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004016{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004017 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02004018
Jonny Lamba55f1392014-05-30 12:07:15 +02004019 if (scale < 1) {
4020 wl_resource_post_error(resource,
4021 WL_SURFACE_ERROR_INVALID_SCALE,
4022 "buffer scale must be at least one "
4023 "('%d' specified)", scale);
4024 return;
4025 }
4026
Pekka Paalanen952b6c82014-03-14 14:38:15 +02004027 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004028 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02004029}
4030
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04004031static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004032 surface_destroy,
4033 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04004034 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004035 surface_frame,
4036 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03004037 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004038 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02004039 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06004040 surface_set_buffer_scale,
4041 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004042};
4043
Leandro Ribeiro8eb84142021-01-18 19:36:48 -03004044static int
4045create_surface_dmabuf_feedback(struct weston_compositor *ec,
4046 struct weston_surface *surface)
4047{
4048 struct weston_dmabuf_feedback_tranche *tranche;
4049 dev_t main_device = ec->default_dmabuf_feedback->main_device;
4050 uint32_t flags = 0;
4051
4052 surface->dmabuf_feedback = weston_dmabuf_feedback_create(main_device);
4053 if (!surface->dmabuf_feedback)
4054 return -1;
4055
4056 tranche = weston_dmabuf_feedback_tranche_create(surface->dmabuf_feedback,
4057 ec->dmabuf_feedback_format_table,
4058 main_device, flags,
4059 RENDERER_PREF);
4060 if (!tranche) {
4061 weston_dmabuf_feedback_destroy(surface->dmabuf_feedback);
4062 surface->dmabuf_feedback = NULL;
4063 return -1;
4064 }
4065
4066 return 0;
4067}
4068
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004069static void
4070compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04004071 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004072{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04004073 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004074 struct weston_surface *surface;
Leandro Ribeiro8eb84142021-01-18 19:36:48 -03004075 int ret;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004076
Kristian Høgsberg18c93002012-01-27 11:58:31 -05004077 surface = weston_surface_create(ec);
Leandro Ribeiro8eb84142021-01-18 19:36:48 -03004078 if (surface == NULL)
4079 goto err;
4080
4081 if (ec->default_dmabuf_feedback) {
4082 ret = create_surface_dmabuf_feedback(ec, surface);
4083 if (ret < 0)
4084 goto err_dmabuf_feedback;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04004085 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004086
Jason Ekstranda85118c2013-06-27 20:17:02 -05004087 surface->resource =
4088 wl_resource_create(client, &wl_surface_interface,
4089 wl_resource_get_version(resource), id);
Leandro Ribeiro8eb84142021-01-18 19:36:48 -03004090 if (surface->resource == NULL)
4091 goto err_dmabuf_feedback;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004092 wl_resource_set_implementation(surface->resource, &surface_interface,
4093 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07004094
4095 wl_signal_emit(&ec->create_surface_signal, surface);
Leandro Ribeiro8eb84142021-01-18 19:36:48 -03004096
4097 return;
4098
4099err_dmabuf_feedback:
4100 weston_surface_destroy(surface);
4101err:
4102 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004103}
4104
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004105static void
4106destroy_region(struct wl_resource *resource)
4107{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05004108 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004109
4110 pixman_region32_fini(&region->region);
4111 free(region);
4112}
4113
4114static void
4115region_destroy(struct wl_client *client, struct wl_resource *resource)
4116{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004117 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004118}
4119
4120static void
4121region_add(struct wl_client *client, struct wl_resource *resource,
4122 int32_t x, int32_t y, int32_t width, int32_t height)
4123{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05004124 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004125
4126 pixman_region32_union_rect(&region->region, &region->region,
4127 x, y, width, height);
4128}
4129
4130static void
4131region_subtract(struct wl_client *client, struct wl_resource *resource,
4132 int32_t x, int32_t y, int32_t width, int32_t height)
4133{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05004134 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004135 pixman_region32_t rect;
4136
4137 pixman_region32_init_rect(&rect, x, y, width, height);
4138 pixman_region32_subtract(&region->region, &region->region, &rect);
4139 pixman_region32_fini(&rect);
4140}
4141
4142static const struct wl_region_interface region_interface = {
4143 region_destroy,
4144 region_add,
4145 region_subtract
4146};
4147
4148static void
4149compositor_create_region(struct wl_client *client,
4150 struct wl_resource *resource, uint32_t id)
4151{
4152 struct weston_region *region;
4153
4154 region = malloc(sizeof *region);
4155 if (region == NULL) {
4156 wl_resource_post_no_memory(resource);
4157 return;
4158 }
4159
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004160 pixman_region32_init(&region->region);
4161
Jason Ekstranda85118c2013-06-27 20:17:02 -05004162 region->resource =
4163 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004164 if (region->resource == NULL) {
4165 free(region);
4166 wl_resource_post_no_memory(resource);
4167 return;
4168 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05004169 wl_resource_set_implementation(region->resource, &region_interface,
4170 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004171}
4172
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04004173static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004174 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004175 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004176};
4177
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004178static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03004179weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
4180{
4181 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004182
Jason Ekstrand7b982072014-05-20 14:33:03 -05004183 weston_surface_commit_state(surface, &sub->cached);
4184 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004185
4186 weston_surface_commit_subsurface_order(surface);
4187
4188 weston_surface_schedule_repaint(surface);
4189
Jason Ekstrand7b982072014-05-20 14:33:03 -05004190 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004191}
4192
4193static void
4194weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
4195{
4196 struct weston_surface *surface = sub->surface;
4197
4198 /*
4199 * If this commit would cause the surface to move by the
4200 * attach(dx, dy) parameters, the old damage region must be
4201 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05004202 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03004203 */
Derek Foreman152254b2015-11-26 14:17:48 -06004204 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004205 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06004206 pixman_region32_union(&sub->cached.damage_surface,
4207 &sub->cached.damage_surface,
4208 &surface->pending.damage_surface);
4209 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004210
4211 if (surface->pending.newly_attached) {
4212 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05004213 weston_surface_state_set_buffer(&sub->cached,
4214 surface->pending.buffer);
4215 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004216 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04004217 weston_presentation_feedback_discard_list(
4218 &sub->cached.feedback_list);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03004219 /* zwp_surface_synchronization_v1.set_acquire_fence */
4220 fd_move(&sub->cached.acquire_fence_fd,
4221 &surface->pending.acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03004222 /* zwp_surface_synchronization_v1.get_release */
4223 weston_buffer_release_move(&sub->cached.buffer_release_ref,
4224 &surface->pending.buffer_release_ref);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004225 }
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05304226 sub->cached.desired_protection = surface->pending.desired_protection;
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05304227 sub->cached.protection_mode = surface->pending.protection_mode;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03004228 assert(surface->pending.acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03004229 assert(surface->pending.buffer_release_ref.buffer_release == NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004230 sub->cached.sx += surface->pending.sx;
4231 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02004232
Derek Foreman152254b2015-11-26 14:17:48 -06004233 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
4234
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004235 sub->cached.buffer_viewport.changed |=
4236 surface->pending.buffer_viewport.changed;
4237 sub->cached.buffer_viewport.buffer =
4238 surface->pending.buffer_viewport.buffer;
4239 sub->cached.buffer_viewport.surface =
4240 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004241
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004242 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004243
4244 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
4245
4246 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
4247
4248 wl_list_insert_list(&sub->cached.frame_callback_list,
4249 &surface->pending.frame_callback_list);
4250 wl_list_init(&surface->pending.frame_callback_list);
4251
Pekka Paalanen133e4392014-09-23 22:08:46 -04004252 wl_list_insert_list(&sub->cached.feedback_list,
4253 &surface->pending.feedback_list);
4254 wl_list_init(&surface->pending.feedback_list);
4255
Jason Ekstrand7b982072014-05-20 14:33:03 -05004256 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004257}
4258
Derek Foreman280e7dd2014-10-03 13:13:42 -05004259static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03004260weston_subsurface_is_synchronized(struct weston_subsurface *sub)
4261{
4262 while (sub) {
4263 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05004264 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004265
4266 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05004267 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004268
4269 sub = weston_surface_to_subsurface(sub->parent);
4270 }
4271
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01004272 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004273}
4274
4275static void
4276weston_subsurface_commit(struct weston_subsurface *sub)
4277{
4278 struct weston_surface *surface = sub->surface;
4279 struct weston_subsurface *tmp;
4280
4281 /* Recursive check for effectively synchronized. */
4282 if (weston_subsurface_is_synchronized(sub)) {
4283 weston_subsurface_commit_to_cache(sub);
4284 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05004285 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03004286 /* flush accumulated state from cache */
4287 weston_subsurface_commit_to_cache(sub);
4288 weston_subsurface_commit_from_cache(sub);
4289 } else {
4290 weston_surface_commit(surface);
4291 }
4292
4293 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
4294 if (tmp->surface != surface)
4295 weston_subsurface_parent_commit(tmp, 0);
4296 }
4297 }
4298}
4299
4300static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004301weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004302{
4303 struct weston_surface *surface = sub->surface;
4304 struct weston_subsurface *tmp;
4305
Pekka Paalanene67858b2013-04-25 13:57:42 +03004306 /* From now on, commit_from_cache the whole sub-tree, regardless of
4307 * the synchronized mode of each child. This sub-surface or some
4308 * of its ancestors were synchronized, so we are synchronized
4309 * all the way down.
4310 */
4311
Jason Ekstrand7b982072014-05-20 14:33:03 -05004312 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004313 weston_subsurface_commit_from_cache(sub);
4314
4315 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
4316 if (tmp->surface != surface)
4317 weston_subsurface_parent_commit(tmp, 1);
4318 }
4319}
4320
4321static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004322weston_subsurface_parent_commit(struct weston_subsurface *sub,
4323 int parent_is_synchronized)
4324{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004325 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004326 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004327 wl_list_for_each(view, &sub->surface->views, surface_link)
4328 weston_view_set_position(view,
4329 sub->position.x,
4330 sub->position.y);
4331
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004332 sub->position.set = 0;
4333 }
4334
4335 if (parent_is_synchronized || sub->synchronized)
4336 weston_subsurface_synchronized_commit(sub);
4337}
4338
Pekka Paalanen8274d902014-08-06 19:36:51 +03004339static int
4340subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
4341{
4342 return snprintf(buf, len, "sub-surface");
4343}
4344
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004345static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004346subsurface_committed(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004347{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004348 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004349
Jason Ekstranda7af7042013-10-12 22:38:11 -05004350 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004351 weston_view_set_position(view,
4352 view->geometry.x + dx,
4353 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004354
4355 /* No need to check parent mappedness, because if parent is not
4356 * mapped, parent is not in a visible layer, so this sub-surface
4357 * will not be drawn either.
4358 */
Armin Krezovićf8486c32016-06-30 06:04:28 +02004359
Pekka Paalanene67858b2013-04-25 13:57:42 +03004360 if (!weston_surface_is_mapped(surface)) {
Armin Krezovićf8486c32016-06-30 06:04:28 +02004361 surface->is_mapped = true;
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03004362
Derek Foreman4b1a0a12014-09-10 15:37:33 -05004363 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03004364 * because that would call it also for the parent surface,
4365 * which might not be mapped yet. That would lead to
4366 * inconsistent state, where the window could never be
4367 * mapped.
4368 *
Armin Krezovićf8486c32016-06-30 06:04:28 +02004369 * Instead just force the is_mapped flag on, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03004370 * weston_surface_is_mapped() return true, so that when the
4371 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03004372 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03004373 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004374 }
4375}
4376
4377static struct weston_subsurface *
4378weston_surface_to_subsurface(struct weston_surface *surface)
4379{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004380 if (surface->committed == subsurface_committed)
4381 return surface->committed_private;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004382
4383 return NULL;
4384}
4385
Pekka Paalanen01388e22013-04-25 13:57:44 +03004386WL_EXPORT struct weston_surface *
4387weston_surface_get_main_surface(struct weston_surface *surface)
4388{
4389 struct weston_subsurface *sub;
4390
4391 while (surface && (sub = weston_surface_to_subsurface(surface)))
4392 surface = sub->parent;
4393
4394 return surface;
4395}
4396
Pekka Paalanen50b67472014-10-01 15:02:41 +03004397WL_EXPORT int
4398weston_surface_set_role(struct weston_surface *surface,
4399 const char *role_name,
4400 struct wl_resource *error_resource,
4401 uint32_t error_code)
4402{
4403 assert(role_name);
4404
4405 if (surface->role_name == NULL ||
4406 surface->role_name == role_name ||
4407 strcmp(surface->role_name, role_name) == 0) {
4408 surface->role_name = role_name;
4409
4410 return 0;
4411 }
4412
4413 wl_resource_post_error(error_resource, error_code,
4414 "Cannot assign role %s to wl_surface@%d,"
4415 " already has role %s\n",
4416 role_name,
4417 wl_resource_get_id(surface->resource),
4418 surface->role_name);
4419 return -1;
4420}
4421
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02004422WL_EXPORT const char *
4423weston_surface_get_role(struct weston_surface *surface)
4424{
4425 return surface->role_name;
4426}
4427
Pekka Paalanen8274d902014-08-06 19:36:51 +03004428WL_EXPORT void
4429weston_surface_set_label_func(struct weston_surface *surface,
4430 int (*desc)(struct weston_surface *,
4431 char *, size_t))
4432{
4433 surface->get_label = desc;
Marius Vlad5de92972019-10-15 13:25:41 +03004434 weston_timeline_refresh_subscription_objects(surface->compositor,
4435 surface);
Pekka Paalanen8274d902014-08-06 19:36:51 +03004436}
4437
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004438/** Get the size of surface contents
4439 *
4440 * \param surface The surface to query.
4441 * \param width Returns the width of raw contents.
4442 * \param height Returns the height of raw contents.
4443 *
4444 * Retrieves the raw surface content size in pixels for the given surface.
4445 * This is the whole content size in buffer pixels. If the surface
4446 * has no content or the renderer does not implement this feature,
4447 * zeroes are returned.
4448 *
4449 * This function is used to determine the buffer size needed for
4450 * a weston_surface_copy_content() call.
4451 */
4452WL_EXPORT void
4453weston_surface_get_content_size(struct weston_surface *surface,
4454 int *width, int *height)
4455{
4456 struct weston_renderer *rer = surface->compositor->renderer;
4457
4458 if (!rer->surface_get_content_size) {
4459 *width = 0;
4460 *height = 0;
4461 return;
4462 }
4463
4464 rer->surface_get_content_size(surface, width, height);
4465}
4466
Quentin Glidic248dd102016-08-12 10:41:34 +02004467/** Get the bounding box of a surface and its subsurfaces
4468 *
4469 * \param surface The surface to query.
4470 * \return The bounding box relative to the surface origin.
4471 *
4472 */
4473WL_EXPORT struct weston_geometry
4474weston_surface_get_bounding_box(struct weston_surface *surface)
4475{
4476 pixman_region32_t region;
4477 pixman_box32_t *box;
4478 struct weston_subsurface *subsurface;
4479
4480 pixman_region32_init_rect(&region,
4481 0, 0,
4482 surface->width, surface->height);
4483
4484 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link)
4485 pixman_region32_union_rect(&region, &region,
4486 subsurface->position.x,
4487 subsurface->position.y,
4488 subsurface->surface->width,
4489 subsurface->surface->height);
4490
4491 box = pixman_region32_extents(&region);
4492 struct weston_geometry geometry = {
4493 .x = box->x1,
4494 .y = box->y1,
4495 .width = box->x2 - box->x1,
4496 .height = box->y2 - box->y1,
4497 };
4498
4499 pixman_region32_fini(&region);
4500
4501 return geometry;
4502}
4503
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004504/** Copy surface contents to system memory.
4505 *
4506 * \param surface The surface to copy from.
4507 * \param target Pointer to the target memory buffer.
4508 * \param size Size of the target buffer in bytes.
4509 * \param src_x X location on contents to copy from.
4510 * \param src_y Y location on contents to copy from.
4511 * \param width Width in pixels of the area to copy.
4512 * \param height Height in pixels of the area to copy.
4513 * \return 0 for success, -1 for failure.
4514 *
4515 * Surface contents are maintained by the renderer. They can be in a
4516 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
4517 * else.
4518 *
4519 * Surface contents are copied into memory pointed to by target,
4520 * which has size bytes of space available. The target memory
4521 * may be larger than needed, but being smaller returns an error.
4522 * The extra bytes in target may or may not be written; their content is
4523 * unspecified. Size must be large enough to hold the image.
4524 *
4525 * The image in the target memory will be arranged in rows from
4526 * top to bottom, and pixels on a row from left to right. The pixel
4527 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
4528 * width * 4.
4529 *
4530 * Parameters src_x and src_y define the upper-left corner in buffer
4531 * coordinates (pixels) to copy from. Parameters width and height
4532 * define the size of the area to copy in pixels.
4533 *
4534 * The rectangle defined by src_x, src_y, width, height must fit in
4535 * the surface contents. Otherwise an error is returned.
4536 *
Changwoo Chof97d2502017-08-05 00:30:47 +09004537 * Use weston_surface_get_content_size to determine the content size; the
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004538 * needed target buffer size and rectangle limits.
4539 *
4540 * CURRENT IMPLEMENTATION RESTRICTIONS:
4541 * - the machine must be little-endian due to Pixman formats.
4542 *
4543 * NOTE: Pixman formats are premultiplied.
4544 */
4545WL_EXPORT int
4546weston_surface_copy_content(struct weston_surface *surface,
4547 void *target, size_t size,
4548 int src_x, int src_y,
4549 int width, int height)
4550{
4551 struct weston_renderer *rer = surface->compositor->renderer;
4552 int cw, ch;
4553 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
4554
4555 if (!rer->surface_copy_content)
4556 return -1;
4557
4558 weston_surface_get_content_size(surface, &cw, &ch);
4559
4560 if (src_x < 0 || src_y < 0)
4561 return -1;
4562
4563 if (width <= 0 || height <= 0)
4564 return -1;
4565
4566 if (src_x + width > cw || src_y + height > ch)
4567 return -1;
4568
4569 if (width * bytespp * height > size)
4570 return -1;
4571
4572 return rer->surface_copy_content(surface, target, size,
4573 src_x, src_y, width, height);
4574}
4575
Pekka Paalanene67858b2013-04-25 13:57:42 +03004576static void
4577subsurface_set_position(struct wl_client *client,
4578 struct wl_resource *resource, int32_t x, int32_t y)
4579{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004580 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004581
4582 if (!sub)
4583 return;
4584
4585 sub->position.x = x;
4586 sub->position.y = y;
4587 sub->position.set = 1;
4588}
4589
4590static struct weston_subsurface *
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004591subsurface_find_sibling(struct weston_subsurface *sub,
4592 struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004593{
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004594 struct weston_surface *parent = sub->parent;
4595 struct weston_subsurface *sibling;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004596
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004597 wl_list_for_each(sibling, &parent->subsurface_list, parent_link) {
4598 if (sibling->surface == surface && sibling != sub)
4599 return sibling;
4600 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004601
4602 return NULL;
4603}
4604
4605static struct weston_subsurface *
4606subsurface_sibling_check(struct weston_subsurface *sub,
4607 struct weston_surface *surface,
4608 const char *request)
4609{
4610 struct weston_subsurface *sibling;
4611
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004612 sibling = subsurface_find_sibling(sub, surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004613 if (!sibling) {
4614 wl_resource_post_error(sub->resource,
4615 WL_SUBSURFACE_ERROR_BAD_SURFACE,
4616 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004617 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004618 return NULL;
4619 }
4620
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004621 assert(sibling->parent == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004622
4623 return sibling;
4624}
4625
4626static void
4627subsurface_place_above(struct wl_client *client,
4628 struct wl_resource *resource,
4629 struct wl_resource *sibling_resource)
4630{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004631 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004632 struct weston_surface *surface =
4633 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004634 struct weston_subsurface *sibling;
4635
4636 if (!sub)
4637 return;
4638
4639 sibling = subsurface_sibling_check(sub, surface, "place_above");
4640 if (!sibling)
4641 return;
4642
4643 wl_list_remove(&sub->parent_link_pending);
4644 wl_list_insert(sibling->parent_link_pending.prev,
4645 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004646
4647 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004648}
4649
4650static void
4651subsurface_place_below(struct wl_client *client,
4652 struct wl_resource *resource,
4653 struct wl_resource *sibling_resource)
4654{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004655 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004656 struct weston_surface *surface =
4657 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004658 struct weston_subsurface *sibling;
4659
4660 if (!sub)
4661 return;
4662
4663 sibling = subsurface_sibling_check(sub, surface, "place_below");
4664 if (!sibling)
4665 return;
4666
4667 wl_list_remove(&sub->parent_link_pending);
4668 wl_list_insert(&sibling->parent_link_pending,
4669 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004670
4671 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004672}
4673
4674static void
4675subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
4676{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004677 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004678
4679 if (sub)
4680 sub->synchronized = 1;
4681}
4682
4683static void
4684subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
4685{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004686 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004687
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004688 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03004689 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004690
4691 /* If sub became effectively desynchronized, flush. */
4692 if (!weston_subsurface_is_synchronized(sub))
4693 weston_subsurface_synchronized_commit(sub);
4694 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004695}
4696
4697static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03004698weston_subsurface_unlink_parent(struct weston_subsurface *sub)
4699{
4700 wl_list_remove(&sub->parent_link);
4701 wl_list_remove(&sub->parent_link_pending);
4702 wl_list_remove(&sub->parent_destroy_listener.link);
4703 sub->parent = NULL;
4704}
4705
4706static void
4707weston_subsurface_destroy(struct weston_subsurface *sub);
4708
4709static void
4710subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
4711{
4712 struct weston_subsurface *sub =
4713 container_of(listener, struct weston_subsurface,
4714 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004715 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004716
4717 /* The protocol object (wl_resource) is left inert. */
4718 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004719 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004720
4721 weston_subsurface_destroy(sub);
4722}
4723
4724static void
4725subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
4726{
4727 struct weston_subsurface *sub =
4728 container_of(listener, struct weston_subsurface,
4729 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004730 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004731 assert(sub->surface != sub->parent);
4732
4733 if (weston_surface_is_mapped(sub->surface))
4734 weston_surface_unmap(sub->surface);
4735
4736 weston_subsurface_unlink_parent(sub);
4737}
4738
4739static void
4740subsurface_resource_destroy(struct wl_resource *resource)
4741{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004742 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004743
4744 if (sub)
4745 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004746}
4747
4748static void
4749subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
4750{
4751 wl_resource_destroy(resource);
4752}
4753
4754static void
4755weston_subsurface_link_parent(struct weston_subsurface *sub,
4756 struct weston_surface *parent)
4757{
4758 sub->parent = parent;
4759 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004760 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004761 &sub->parent_destroy_listener);
4762
4763 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4764 wl_list_insert(&parent->subsurface_list_pending,
4765 &sub->parent_link_pending);
4766}
4767
4768static void
4769weston_subsurface_link_surface(struct weston_subsurface *sub,
4770 struct weston_surface *surface)
4771{
4772 sub->surface = surface;
4773 sub->surface_destroy_listener.notify =
4774 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004775 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004776 &sub->surface_destroy_listener);
4777}
4778
4779static void
4780weston_subsurface_destroy(struct weston_subsurface *sub)
4781{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004782 struct weston_view *view, *next;
4783
Pekka Paalanene67858b2013-04-25 13:57:42 +03004784 assert(sub->surface);
4785
4786 if (sub->resource) {
4787 assert(weston_surface_to_subsurface(sub->surface) == sub);
4788 assert(sub->parent_destroy_listener.notify ==
4789 subsurface_handle_parent_destroy);
4790
George Kiagiadakised04d382014-06-13 18:10:26 +02004791 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
4792 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004793 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02004794 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05004795
Pekka Paalanene67858b2013-04-25 13:57:42 +03004796 if (sub->parent)
4797 weston_subsurface_unlink_parent(sub);
4798
Jason Ekstrand7b982072014-05-20 14:33:03 -05004799 weston_surface_state_fini(&sub->cached);
4800 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004801
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004802 sub->surface->committed = NULL;
4803 sub->surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004804 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004805 } else {
4806 /* the dummy weston_subsurface for the parent itself */
4807 assert(sub->parent_destroy_listener.notify == NULL);
4808 wl_list_remove(&sub->parent_link);
4809 wl_list_remove(&sub->parent_link_pending);
4810 }
4811
4812 wl_list_remove(&sub->surface_destroy_listener.link);
4813 free(sub);
4814}
4815
4816static const struct wl_subsurface_interface subsurface_implementation = {
4817 subsurface_destroy,
4818 subsurface_set_position,
4819 subsurface_place_above,
4820 subsurface_place_below,
4821 subsurface_set_sync,
4822 subsurface_set_desync
4823};
4824
4825static struct weston_subsurface *
4826weston_subsurface_create(uint32_t id, struct weston_surface *surface,
4827 struct weston_surface *parent)
4828{
4829 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004830 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004831
Bryce Harringtonde16d892014-11-20 22:21:57 -08004832 sub = zalloc(sizeof *sub);
4833 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004834 return NULL;
4835
Jason Ekstranda7af7042013-10-12 22:38:11 -05004836 wl_list_init(&sub->unused_views);
4837
Jason Ekstranda85118c2013-06-27 20:17:02 -05004838 sub->resource =
4839 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004840 if (!sub->resource) {
4841 free(sub);
4842 return NULL;
4843 }
4844
Jason Ekstranda85118c2013-06-27 20:17:02 -05004845 wl_resource_set_implementation(sub->resource,
4846 &subsurface_implementation,
4847 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004848 weston_subsurface_link_surface(sub, surface);
4849 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05004850 weston_surface_state_init(&sub->cached);
4851 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004852 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004853
4854 return sub;
4855}
4856
4857/* Create a dummy subsurface for having the parent itself in its
4858 * sub-surface lists. Makes stacking order manipulation easy.
4859 */
4860static struct weston_subsurface *
4861weston_subsurface_create_for_parent(struct weston_surface *parent)
4862{
4863 struct weston_subsurface *sub;
4864
Bryce Harringtonde16d892014-11-20 22:21:57 -08004865 sub = zalloc(sizeof *sub);
4866 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004867 return NULL;
4868
4869 weston_subsurface_link_surface(sub, parent);
4870 sub->parent = parent;
4871 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4872 wl_list_insert(&parent->subsurface_list_pending,
4873 &sub->parent_link_pending);
4874
4875 return sub;
4876}
4877
4878static void
4879subcompositor_get_subsurface(struct wl_client *client,
4880 struct wl_resource *resource,
4881 uint32_t id,
4882 struct wl_resource *surface_resource,
4883 struct wl_resource *parent_resource)
4884{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004885 struct weston_surface *surface =
4886 wl_resource_get_user_data(surface_resource);
4887 struct weston_surface *parent =
4888 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004889 struct weston_subsurface *sub;
4890 static const char where[] = "get_subsurface: wl_subsurface@";
4891
4892 if (surface == parent) {
4893 wl_resource_post_error(resource,
4894 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4895 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004896 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004897 return;
4898 }
4899
4900 if (weston_surface_to_subsurface(surface)) {
4901 wl_resource_post_error(resource,
4902 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4903 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004904 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004905 return;
4906 }
4907
Pekka Paalanen50b67472014-10-01 15:02:41 +03004908 if (weston_surface_set_role(surface, "wl_subsurface", resource,
4909 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004910 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004911
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004912 if (weston_surface_get_main_surface(parent) == surface) {
4913 wl_resource_post_error(resource,
4914 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4915 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004916 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004917 return;
4918 }
4919
Pekka Paalanene67858b2013-04-25 13:57:42 +03004920 /* make sure the parent is in its own list */
4921 if (wl_list_empty(&parent->subsurface_list)) {
4922 if (!weston_subsurface_create_for_parent(parent)) {
4923 wl_resource_post_no_memory(resource);
4924 return;
4925 }
4926 }
4927
4928 sub = weston_subsurface_create(id, surface, parent);
4929 if (!sub) {
4930 wl_resource_post_no_memory(resource);
4931 return;
4932 }
4933
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004934 surface->committed = subsurface_committed;
4935 surface->committed_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004936 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004937}
4938
4939static void
4940subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
4941{
4942 wl_resource_destroy(resource);
4943}
4944
4945static const struct wl_subcompositor_interface subcompositor_interface = {
4946 subcompositor_destroy,
4947 subcompositor_get_subsurface
4948};
4949
4950static void
4951bind_subcompositor(struct wl_client *client,
4952 void *data, uint32_t version, uint32_t id)
4953{
4954 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004955 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004956
Jason Ekstranda85118c2013-06-27 20:17:02 -05004957 resource =
4958 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004959 if (resource == NULL) {
4960 wl_client_post_no_memory(client);
4961 return;
4962 }
4963 wl_resource_set_implementation(resource, &subcompositor_interface,
4964 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004965}
4966
Bryce Harrington0795ece2016-08-30 12:04:26 -07004967/** Set a DPMS mode on all of the compositor's outputs
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004968 *
4969 * \param compositor The compositor instance
4970 * \param state The DPMS state the outputs will be set to
4971 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004972static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004973weston_compositor_dpms(struct weston_compositor *compositor,
4974 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004975{
Emmanuel Gil Peyroteff793a2021-07-31 17:25:41 +02004976 struct weston_output *output;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004977
Emmanuel Gil Peyroteff793a2021-07-31 17:25:41 +02004978 wl_list_for_each(output, &compositor->output_list, link)
Bryce Harrington08976ac2016-08-30 12:05:16 -07004979 if (output->set_dpms)
4980 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004981}
4982
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004983/** Restores the compositor to active status
4984 *
4985 * \param compositor The compositor instance
4986 *
4987 * If the compositor was in a sleeping mode, all outputs are powered
4988 * back on via DPMS. Otherwise if the compositor was inactive
4989 * (idle/locked, offscreen, or sleeping) then the compositor's wake
4990 * signal will fire.
4991 *
4992 * Restarts the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004993 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004994 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004995WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004996weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004997{
Neil Roberts8b62e202013-09-30 13:14:47 +01004998 uint32_t old_state = compositor->state;
4999
5000 /* The state needs to be changed before emitting the wake
5001 * signal because that may try to schedule a repaint which
5002 * will not work if the compositor is still sleeping */
5003 compositor->state = WESTON_COMPOSITOR_ACTIVE;
5004
5005 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005006 case WESTON_COMPOSITOR_SLEEPING:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005007 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01005008 case WESTON_COMPOSITOR_OFFSCREEN:
Daniel Stone893b9362016-11-08 15:47:09 +00005009 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005010 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005011 /* fall through */
5012 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005013 wl_event_source_timer_update(compositor->idle_source,
5014 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02005015 }
5016}
5017
Bryce Harringtonc9626a32015-12-11 13:11:38 -08005018/** Turns off rendering and frame events for the compositor.
5019 *
5020 * \param compositor The compositor instance
5021 *
5022 * This is used for example to prevent further rendering while the
5023 * compositor is shutting down.
5024 *
Bryce Harringtonc9626a32015-12-11 13:11:38 -08005025 * Stops the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005026 *
5027 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08005028 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005029WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01005030weston_compositor_offscreen(struct weston_compositor *compositor)
5031{
5032 switch (compositor->state) {
5033 case WESTON_COMPOSITOR_OFFSCREEN:
5034 return;
5035 case WESTON_COMPOSITOR_SLEEPING:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01005036 default:
5037 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
5038 wl_event_source_timer_update(compositor->idle_source, 0);
5039 }
5040}
5041
Bryce Harringtonc9626a32015-12-11 13:11:38 -08005042/** Powers down all attached output devices
5043 *
5044 * \param compositor The compositor instance
5045 *
5046 * Causes rendering to the outputs to cease, and no frame events to be
5047 * sent. Only powers down the outputs if the compositor is not already
5048 * in sleep mode.
5049 *
5050 * Stops the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005051 *
5052 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08005053 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01005054WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005055weston_compositor_sleep(struct weston_compositor *compositor)
5056{
5057 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
5058 return;
5059
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01005060 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005061 compositor->state = WESTON_COMPOSITOR_SLEEPING;
5062 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
5063}
5064
Bryce Harringtonc9626a32015-12-11 13:11:38 -08005065/** Sets compositor to idle mode
5066 *
5067 * \param data The compositor instance
5068 *
5069 * This is called when the idle timer fires. Once the compositor is in
5070 * idle mode it requires a wake action (e.g. via
5071 * weston_compositor_wake()) to restore it. The compositor's
5072 * idle_signal will be triggered when the idle event occurs.
5073 *
5074 * Idleness can be inhibited by setting the compositor's idle_inhibit
5075 * property.
5076 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04005077static int
5078idle_handler(void *data)
5079{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005080 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04005081
5082 if (compositor->idle_inhibit)
5083 return 1;
5084
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005085 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005086 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04005087
5088 return 1;
5089}
5090
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005091WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08005092weston_plane_init(struct weston_plane *plane,
5093 struct weston_compositor *ec,
5094 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005095{
5096 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02005097 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005098 plane->x = x;
5099 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08005100 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03005101
5102 /* Init the link so that the call to wl_list_remove() when releasing
5103 * the plane without ever stacking doesn't lead to a crash */
5104 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005105}
5106
5107WL_EXPORT void
5108weston_plane_release(struct weston_plane *plane)
5109{
Xiong Zhang97116532013-10-23 13:58:31 +08005110 struct weston_view *view;
5111
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005112 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02005113 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03005114
Pekka Paalanen6551c092021-05-03 16:09:45 +03005115 /*
5116 * Can't use paint node list here, weston_plane is not specific to an
5117 * output.
5118 */
Xiong Zhang97116532013-10-23 13:58:31 +08005119 wl_list_for_each(view, &plane->compositor->view_list, link) {
5120 if (view->plane == plane)
5121 view->plane = NULL;
5122 }
5123
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03005124 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005125}
5126
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005127/** weston_compositor_stack_plane
5128 * \ingroup compositor
5129 */
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02005130WL_EXPORT void
5131weston_compositor_stack_plane(struct weston_compositor *ec,
5132 struct weston_plane *plane,
5133 struct weston_plane *above)
5134{
5135 if (above)
5136 wl_list_insert(above->link.prev, &plane->link);
5137 else
5138 wl_list_insert(&ec->plane_list, &plane->link);
5139}
5140
Quentin Glidic4ef719c2016-07-05 20:44:33 +02005141static void
5142output_release(struct wl_client *client, struct wl_resource *resource)
5143{
5144 wl_resource_destroy(resource);
5145}
5146
5147static const struct wl_output_interface output_interface = {
5148 output_release,
5149};
5150
5151
Casey Dahlin9074db52012-04-19 22:50:09 -04005152static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04005153{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05005154 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04005155}
5156
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005157static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005158bind_output(struct wl_client *client,
5159 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05005160{
Pekka Paalanen05347622017-03-27 12:24:34 +03005161 struct weston_head *head = data;
5162 struct weston_output *output = head->output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005163 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04005164 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05005165
Jason Ekstranda85118c2013-06-27 20:17:02 -05005166 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06005167 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07005168 if (resource == NULL) {
5169 wl_client_post_no_memory(client);
5170 return;
5171 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04005172
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005173 wl_list_insert(&head->resource_list, wl_resource_get_link(resource));
Pekka Paalanen055c1132017-03-27 16:31:25 +03005174 wl_resource_set_implementation(resource, &output_interface, head,
Pekka Paalanen05347622017-03-27 12:24:34 +03005175 unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04005176
Pekka Paalanen05347622017-03-27 12:24:34 +03005177 assert(output);
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05005178 wl_output_send_geometry(resource,
5179 output->x,
5180 output->y,
Pekka Paalanen01f60212017-03-24 15:39:24 +02005181 head->mm_width,
5182 head->mm_height,
5183 head->subpixel,
5184 head->make, head->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04005185 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04005186 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02005187 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02005188 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005189
5190 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05005191 wl_output_send_mode(resource,
5192 mode->flags,
5193 mode->width,
5194 mode->height,
5195 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005196 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02005197
Jasper St. Pierre0013a292014-08-07 16:43:11 -04005198 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02005199 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05005200}
5201
Pekka Paalanendcac3512017-12-08 14:13:34 +02005202static void
5203weston_head_add_global(struct weston_head *head)
5204{
5205 head->global = wl_global_create(head->compositor->wl_display,
5206 &wl_output_interface, 3,
5207 head, bind_output);
5208}
5209
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02005210/** Remove the global wl_output protocol object
5211 *
5212 * \param head The head whose global to remove.
5213 *
5214 * Also orphans the wl_resources for this head (wl_output).
5215 */
5216static void
5217weston_head_remove_global(struct weston_head *head)
5218{
5219 struct wl_resource *resource, *tmp;
5220
5221 if (head->global)
5222 wl_global_destroy(head->global);
5223 head->global = NULL;
5224
5225 wl_resource_for_each_safe(resource, tmp, &head->resource_list) {
5226 unbind_resource(resource);
5227 wl_resource_set_destructor(resource, NULL);
5228 wl_resource_set_user_data(resource, NULL);
5229 }
Roman Gilge97391c2019-03-29 13:01:06 +01005230
5231 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
5232 /* It's sufficient to unset the destructor, then the list elements
5233 * won't be accessed.
5234 */
5235 wl_resource_set_destructor(resource, NULL);
5236 }
5237 wl_list_init(&head->xdg_output_resource_list);
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02005238}
5239
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005240/** Get the backing object of wl_output
5241 *
5242 * \param resource A wl_output protocol object.
5243 * \return The backing object (user data) of a wl_resource representing a
5244 * wl_output protocol object.
Marius Vlad78984ee2019-06-11 00:05:08 +03005245 *
5246 * \ingroup head
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005247 */
Pekka Paalanen055c1132017-03-27 16:31:25 +03005248WL_EXPORT struct weston_head *
5249weston_head_from_resource(struct wl_resource *resource)
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005250{
5251 assert(wl_resource_instance_of(resource, &wl_output_interface,
5252 &output_interface));
5253
5254 return wl_resource_get_user_data(resource);
5255}
5256
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005257/** Initialize a pre-allocated weston_head
5258 *
5259 * \param head The head to initialize.
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005260 * \param name The head name, e.g. the connector name or equivalent.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005261 *
5262 * The head will be safe to attach, detach and release.
5263 *
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005264 * The name is used in logs, and can be used by compositors as a configuration
5265 * identifier.
5266 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005267 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005268 * \internal
5269 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005270WL_EXPORT void
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005271weston_head_init(struct weston_head *head, const char *name)
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005272{
5273 /* Add some (in)sane defaults which can be used
5274 * for checking if an output was properly configured
5275 */
5276 memset(head, 0, sizeof *head);
5277
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005278 wl_list_init(&head->compositor_link);
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005279 wl_signal_init(&head->destroy_signal);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005280 wl_list_init(&head->output_link);
5281 wl_list_init(&head->resource_list);
Roman Gilge97391c2019-03-29 13:01:06 +01005282 wl_list_init(&head->xdg_output_resource_list);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005283 head->name = strdup(name);
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305284 head->current_protection = WESTON_HDCP_DISABLE;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005285}
5286
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005287/** Send output heads changed signal
5288 *
5289 * \param output The output that changed.
5290 *
5291 * Notify that the enabled output gained and/or lost heads, or that the
5292 * associated heads may have changed their connection status. This does not
5293 * include cases where the output becomes enabled or disabled. The registered
5294 * callbacks are called after the change has successfully happened.
5295 *
5296 * If connection status change causes the compositor to attach or detach a head
5297 * to an enabled output, the registered callbacks may be called multiple times.
Marius Vlad55d87362019-06-11 01:15:35 +03005298 *
5299 * \ingroup output
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005300 */
5301static void
5302weston_output_emit_heads_changed(struct weston_output *output)
5303{
5304 wl_signal_emit(&output->compositor->output_heads_changed_signal,
5305 output);
5306}
5307
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005308/** Idle task for emitting heads_changed_signal */
5309static void
5310weston_compositor_call_heads_changed(void *data)
5311{
5312 struct weston_compositor *compositor = data;
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005313 struct weston_head *head;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005314
5315 compositor->heads_changed_source = NULL;
5316
5317 wl_signal_emit(&compositor->heads_changed_signal, compositor);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005318
5319 wl_list_for_each(head, &compositor->head_list, compositor_link) {
5320 if (head->output && head->output->enabled)
5321 weston_output_emit_heads_changed(head->output);
5322 }
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005323}
5324
5325/** Schedule a call on idle to heads_changed callback
5326 *
5327 * \param compositor The Compositor.
5328 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005329 * \ingroup compositor
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005330 * \internal
5331 */
5332static void
5333weston_compositor_schedule_heads_changed(struct weston_compositor *compositor)
5334{
5335 struct wl_event_loop *loop;
5336
5337 if (compositor->heads_changed_source)
5338 return;
5339
5340 loop = wl_display_get_event_loop(compositor->wl_display);
5341 compositor->heads_changed_source = wl_event_loop_add_idle(loop,
5342 weston_compositor_call_heads_changed, compositor);
5343}
5344
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005345/** Register a new head
5346 *
5347 * \param compositor The compositor.
5348 * \param head The head to register, must not be already registered.
5349 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005350 * This signals the core that a new head has become available, leading to
5351 * heads_changed hook being called later.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005352 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005353 * \ingroup compositor
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005354 * \internal
5355 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005356WL_EXPORT void
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005357weston_compositor_add_head(struct weston_compositor *compositor,
5358 struct weston_head *head)
5359{
5360 assert(wl_list_empty(&head->compositor_link));
5361 assert(head->name);
5362
5363 wl_list_insert(compositor->head_list.prev, &head->compositor_link);
5364 head->compositor = compositor;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005365 weston_compositor_schedule_heads_changed(compositor);
5366}
5367
5368/** Adds a listener to be called when heads change
5369 *
5370 * \param compositor The compositor.
5371 * \param listener The listener to add.
5372 *
Marius Vlada2dace22019-06-12 16:05:44 +03005373 * The listener notify function argument is weston_compositor.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005374 *
5375 * The listener function will be called after heads are added or their
5376 * connection status has changed. Several changes may be accumulated into a
5377 * single call. The user is expected to iterate over the existing heads and
5378 * check their statuses to find out what changed.
5379 *
5380 * \sa weston_compositor_iterate_heads, weston_head_is_connected,
5381 * weston_head_is_enabled
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005382 * \ingroup compositor
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005383 */
5384WL_EXPORT void
5385weston_compositor_add_heads_changed_listener(struct weston_compositor *compositor,
5386 struct wl_listener *listener)
5387{
5388 wl_signal_add(&compositor->heads_changed_signal, listener);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005389}
5390
5391/** Iterate over available heads
5392 *
5393 * \param compositor The compositor.
Marius Vlada2dace22019-06-12 16:05:44 +03005394 * \param iter The iterator, or NULL for start.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005395 * \return The next available head in the list.
5396 *
5397 * Returns all available heads, regardless of being connected or enabled.
5398 *
5399 * You can iterate over all heads as follows:
5400 * \code
5401 * struct weston_head *head = NULL;
5402 *
5403 * while ((head = weston_compositor_iterate_heads(compositor, head))) {
5404 * ...
5405 * }
5406 * \endcode
5407 *
5408 * If you cause \c iter to be removed from the list, you cannot use it to
5409 * continue iterating. Removing any other item is safe.
5410 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005411 * \ingroup compositor
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005412 */
5413WL_EXPORT struct weston_head *
5414weston_compositor_iterate_heads(struct weston_compositor *compositor,
5415 struct weston_head *iter)
5416{
5417 struct wl_list *list = &compositor->head_list;
5418 struct wl_list *node;
5419
5420 assert(compositor);
5421 assert(!iter || iter->compositor == compositor);
5422
5423 if (iter)
5424 node = iter->compositor_link.next;
5425 else
5426 node = list->next;
5427
5428 assert(node);
5429 assert(!iter || node != &iter->compositor_link);
5430
5431 if (node == list)
5432 return NULL;
5433
5434 return container_of(node, struct weston_head, compositor_link);
5435}
5436
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005437/** Iterate over attached heads
5438 *
5439 * \param output The output whose heads to iterate.
Marius Vlada2dace22019-06-12 16:05:44 +03005440 * \param iter The iterator, or NULL for start.
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005441 * \return The next attached head in the list.
5442 *
5443 * Returns all heads currently attached to the output.
5444 *
5445 * You can iterate over all heads as follows:
5446 * \code
5447 * struct weston_head *head = NULL;
5448 *
5449 * while ((head = weston_output_iterate_heads(output, head))) {
5450 * ...
5451 * }
5452 * \endcode
5453 *
5454 * If you cause \c iter to be removed from the list, you cannot use it to
5455 * continue iterating. Removing any other item is safe.
5456 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05005457 * \ingroup output
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005458 */
5459WL_EXPORT struct weston_head *
5460weston_output_iterate_heads(struct weston_output *output,
5461 struct weston_head *iter)
5462{
5463 struct wl_list *list = &output->head_list;
5464 struct wl_list *node;
5465
5466 assert(output);
5467 assert(!iter || iter->output == output);
5468
5469 if (iter)
5470 node = iter->output_link.next;
5471 else
5472 node = list->next;
5473
5474 assert(node);
5475 assert(!iter || node != &iter->output_link);
5476
5477 if (node == list)
5478 return NULL;
5479
5480 return container_of(node, struct weston_head, output_link);
5481}
5482
Pekka Paalanendcac3512017-12-08 14:13:34 +02005483/** Attach a head to an output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005484 *
5485 * \param output The output to attach to.
5486 * \param head The head that is not yet attached.
5487 * \return 0 on success, -1 on failure.
5488 *
5489 * Attaches the given head to the output. All heads of an output are clones
5490 * and share the resolution and timings.
5491 *
5492 * Cloning heads this way uses less resources than creating an output for
5493 * each head, but is not always possible due to environment, driver and hardware
5494 * limitations.
5495 *
5496 * On failure, the head remains unattached. Success of this function does not
5497 * guarantee the output configuration is actually valid. The final checks are
Pekka Paalanendcac3512017-12-08 14:13:34 +02005498 * made on weston_output_enable() unless the output was already enabled.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005499 *
Marius Vlad55d87362019-06-11 01:15:35 +03005500 * \ingroup output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005501 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005502WL_EXPORT int
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005503weston_output_attach_head(struct weston_output *output,
5504 struct weston_head *head)
5505{
Pekka Paalanendcac3512017-12-08 14:13:34 +02005506 char *head_names;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005507
5508 if (!wl_list_empty(&head->output_link))
5509 return -1;
5510
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005511 if (output->attach_head) {
5512 if (output->attach_head(output, head) < 0)
5513 return -1;
5514 } else if (!wl_list_empty(&output->head_list)) {
5515 /* No support for clones in the legacy path. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005516 return -1;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005517 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005518
5519 head->output = output;
5520 wl_list_insert(output->head_list.prev, &head->output_link);
5521
Pekka Paalanendcac3512017-12-08 14:13:34 +02005522 if (output->enabled) {
5523 weston_head_add_global(head);
5524
5525 head_names = weston_output_create_heads_string(output);
5526 weston_log("Output '%s' updated to have head(s) %s\n",
5527 output->name, head_names);
5528 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005529
5530 weston_output_emit_heads_changed(output);
Pekka Paalanendcac3512017-12-08 14:13:34 +02005531 }
5532
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005533 return 0;
5534}
5535
5536/** Detach a head from its output
5537 *
5538 * \param head The head to detach.
5539 *
5540 * It is safe to detach a non-attached head.
5541 *
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005542 * If the head is attached to an enabled output and the output will be left
5543 * with no heads, the output will be disabled.
5544 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005545 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005546 * \sa weston_output_disable
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005547 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005548WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005549weston_head_detach(struct weston_head *head)
5550{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005551 struct weston_output *output = head->output;
Pekka Paalanena0106992017-12-08 16:11:17 +02005552 char *head_names;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005553
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005554 wl_list_remove(&head->output_link);
5555 wl_list_init(&head->output_link);
5556 head->output = NULL;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005557
5558 if (!output)
5559 return;
5560
5561 if (output->detach_head)
5562 output->detach_head(output, head);
5563
5564 if (output->enabled) {
5565 weston_head_remove_global(head);
5566
Pekka Paalanena0106992017-12-08 16:11:17 +02005567 if (wl_list_empty(&output->head_list)) {
5568 weston_log("Output '%s' no heads left, disabling.\n",
5569 output->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005570 weston_output_disable(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02005571 } else {
5572 head_names = weston_output_create_heads_string(output);
5573 weston_log("Output '%s' updated to have head(s) %s\n",
5574 output->name, head_names);
5575 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005576
5577 weston_output_emit_heads_changed(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02005578 }
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005579 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005580}
5581
5582/** Destroy a head
5583 *
5584 * \param head The head to be released.
5585 *
5586 * Destroys the head. The caller is responsible for freeing the memory pointed
5587 * to by \c head.
5588 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005589 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005590 * \internal
5591 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005592WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005593weston_head_release(struct weston_head *head)
5594{
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005595 wl_signal_emit(&head->destroy_signal, head);
5596
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005597 weston_head_detach(head);
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005598
5599 free(head->make);
5600 free(head->model);
5601 free(head->serial_number);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005602 free(head->name);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005603
5604 wl_list_remove(&head->compositor_link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005605}
5606
Pekka Paalanene19970f2017-08-28 14:11:02 +03005607static void
5608weston_head_set_device_changed(struct weston_head *head)
5609{
5610 head->device_changed = true;
5611
5612 if (head->compositor)
5613 weston_compositor_schedule_heads_changed(head->compositor);
5614}
5615
5616/** String equal comparison with NULLs being equal */
5617static bool
5618str_null_eq(const char *a, const char *b)
5619{
5620 if (!a && !b)
5621 return true;
5622
5623 if (!!a != !!b)
5624 return false;
5625
5626 return strcmp(a, b) == 0;
5627}
5628
Pekka Paalanen01f60212017-03-24 15:39:24 +02005629/** Store monitor make, model and serial number
5630 *
5631 * \param head The head to modify.
5632 * \param make The monitor make. If EDID is available, the PNP ID. Otherwise
5633 * any string, or NULL for none.
5634 * \param model The monitor model or name, or a made-up string, or NULL for
5635 * none.
5636 * \param serialno The monitor serial number, a made-up string, or NULL for
5637 * none.
5638 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005639 * This may set the device_changed flag.
5640 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005641 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005642 * \internal
5643 */
5644WL_EXPORT void
5645weston_head_set_monitor_strings(struct weston_head *head,
5646 const char *make,
5647 const char *model,
5648 const char *serialno)
5649{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005650 if (str_null_eq(head->make, make) &&
5651 str_null_eq(head->model, model) &&
5652 str_null_eq(head->serial_number, serialno))
5653 return;
5654
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005655 free(head->make);
5656 free(head->model);
5657 free(head->serial_number);
5658
5659 head->make = make ? strdup(make) : NULL;
5660 head->model = model ? strdup(model) : NULL;
5661 head->serial_number = serialno ? strdup(serialno) : NULL;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005662
5663 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005664}
5665
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005666/** Store display non-desktop status
5667 *
5668 * \param head The head to modify.
5669 * \param non_desktop Whether the head connects to a non-desktop display.
5670 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005671 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005672 * \internal
5673 */
5674WL_EXPORT void
5675weston_head_set_non_desktop(struct weston_head *head, bool non_desktop)
5676{
5677 if (head->non_desktop == non_desktop)
5678 return;
5679
5680 head->non_desktop = non_desktop;
5681
5682 weston_head_set_device_changed(head);
5683}
5684
Lucas Stacha69cb712019-11-25 23:29:31 +00005685/** Store display transformation
5686 *
5687 * \param head The head to modify.
5688 * \param transform The transformation to apply for this head
5689 *
5690 * This may set the device_changed flag.
5691 *
5692 * \ingroup head
5693 * \internal
5694 */
5695WL_EXPORT void
5696weston_head_set_transform(struct weston_head *head, uint32_t transform)
5697{
5698 if (head->transform == transform)
5699 return;
5700
5701 head->transform = transform;
5702
5703 weston_head_set_device_changed(head);
5704}
5705
5706
Pekka Paalanen01f60212017-03-24 15:39:24 +02005707/** Store physical image size
5708 *
5709 * \param head The head to modify.
5710 * \param mm_width Image area width in millimeters.
5711 * \param mm_height Image area height in millimeters.
5712 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005713 * This may set the device_changed flag.
5714 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005715 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005716 * \internal
5717 */
5718WL_EXPORT void
5719weston_head_set_physical_size(struct weston_head *head,
5720 int32_t mm_width, int32_t mm_height)
5721{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005722 if (head->mm_width == mm_width &&
5723 head->mm_height == mm_height)
5724 return;
5725
Pekka Paalanen01f60212017-03-24 15:39:24 +02005726 head->mm_width = mm_width;
5727 head->mm_height = mm_height;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005728
5729 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005730}
5731
5732/** Store monitor sub-pixel layout
5733 *
5734 * \param head The head to modify.
5735 * \param sp Sub-pixel layout. The possible values are:
5736 * - WL_OUTPUT_SUBPIXEL_UNKNOWN,
5737 * - WL_OUTPUT_SUBPIXEL_NONE,
5738 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB,
5739 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR,
5740 * - WL_OUTPUT_SUBPIXEL_VERTICAL_RGB,
5741 * - WL_OUTPUT_SUBPIXEL_VERTICAL_BGR
5742 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005743 * This may set the device_changed flag.
5744 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005745 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005746 * \internal
5747 */
5748WL_EXPORT void
5749weston_head_set_subpixel(struct weston_head *head,
5750 enum wl_output_subpixel sp)
5751{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005752 if (head->subpixel == sp)
5753 return;
5754
Pekka Paalanen01f60212017-03-24 15:39:24 +02005755 head->subpixel = sp;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005756
5757 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005758}
5759
5760/** Mark the monitor as internal
5761 *
5762 * This is used for embedded screens, like laptop panels.
5763 *
5764 * \param head The head to mark as internal.
5765 *
5766 * By default a head is external. The type is often inferred from the physical
5767 * connector type.
5768 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005769 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005770 * \internal
5771 */
5772WL_EXPORT void
5773weston_head_set_internal(struct weston_head *head)
5774{
5775 head->connection_internal = true;
5776}
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005777
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005778/** Store connector status
5779 *
5780 * \param head The head to modify.
5781 * \param connected Whether the head is connected.
5782 *
5783 * Connectors are created as disconnected. This function can be used to
5784 * set the connector status.
5785 *
5786 * The status should be set to true when a physical connector is connected to
5787 * a video sink device like a monitor and to false when the connector is
5788 * disconnected. For nested backends, the connection status should reflect the
5789 * connection to the parent display server.
5790 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005791 * When the connection status changes, it schedules a call to the heads_changed
Pekka Paalanene19970f2017-08-28 14:11:02 +03005792 * hook and sets the device_changed flag.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005793 *
5794 * \sa weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005795 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005796 * \internal
5797 */
5798WL_EXPORT void
5799weston_head_set_connection_status(struct weston_head *head, bool connected)
5800{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005801 if (head->connected == connected)
5802 return;
5803
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005804 head->connected = connected;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005805
Pekka Paalanene19970f2017-08-28 14:11:02 +03005806 weston_head_set_device_changed(head);
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005807}
5808
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305809static void
5810weston_output_compute_protection(struct weston_output *output)
5811{
5812 struct weston_head *head;
5813 enum weston_hdcp_protection op_protection;
5814 bool op_protection_valid = false;
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305815 struct weston_compositor *wc = output->compositor;
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305816
5817 wl_list_for_each(head, &output->head_list, output_link) {
5818 if (!op_protection_valid) {
5819 op_protection = head->current_protection;
5820 op_protection_valid = true;
5821 }
5822 if (head->current_protection < op_protection)
5823 op_protection = head->current_protection;
5824 }
5825
5826 if (!op_protection_valid)
5827 op_protection = WESTON_HDCP_DISABLE;
5828
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305829 if (output->current_protection != op_protection) {
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305830 output->current_protection = op_protection;
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305831 weston_output_damage(output);
Ankit Nautiyalfaa5ab42019-07-08 13:23:24 +05305832 weston_schedule_surface_protection_update(wc);
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305833 }
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305834}
5835
5836WL_EXPORT void
5837weston_head_set_content_protection_status(struct weston_head *head,
5838 enum weston_hdcp_protection status)
5839{
5840 head->current_protection = status;
5841 if (head->output)
5842 weston_output_compute_protection(head->output);
5843}
5844
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005845/** Is the head currently connected?
5846 *
5847 * \param head The head to query.
5848 * \return Connection status.
5849 *
5850 * Returns true if the head is physically connected to a monitor, or in
5851 * case of a nested backend returns true when there is a connection to the
5852 * parent display server.
5853 *
5854 * This is independent from the head being enabled.
5855 *
5856 * \sa weston_head_is_enabled
Marius Vlad78984ee2019-06-11 00:05:08 +03005857 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005858 */
5859WL_EXPORT bool
5860weston_head_is_connected(struct weston_head *head)
5861{
5862 return head->connected;
5863}
5864
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005865/** Is the head currently enabled?
5866 *
5867 * \param head The head to query.
5868 * \return Video status.
5869 *
5870 * Returns true if the head is currently transmitting a video stream.
5871 *
5872 * This is independent of the head being connected.
5873 *
5874 * \sa weston_head_is_connected
Marius Vlad78984ee2019-06-11 00:05:08 +03005875 * \ingroup head
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005876 */
5877WL_EXPORT bool
5878weston_head_is_enabled(struct weston_head *head)
5879{
5880 if (!head->output)
5881 return false;
5882
5883 return head->output->enabled;
5884}
5885
Pekka Paalanene19970f2017-08-28 14:11:02 +03005886/** Has the device information changed?
5887 *
5888 * \param head The head to query.
5889 * \return True if the device information has changed since last reset.
5890 *
5891 * The information about the connected display device, e.g. a monitor, may
5892 * change without being disconnected in between. Changing information
5893 * causes a call to the heads_changed hook.
5894 *
5895 * The information includes make, model, serial number, physical size,
5896 * and sub-pixel type. The connection status is also included.
5897 *
5898 * \sa weston_head_reset_device_changed, weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005899 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03005900 */
5901WL_EXPORT bool
5902weston_head_is_device_changed(struct weston_head *head)
5903{
5904 return head->device_changed;
5905}
5906
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005907/** Does the head represent a non-desktop display?
5908 *
5909 * \param head The head to query.
5910 * \return True if the device is a non-desktop display.
5911 *
5912 * Non-desktop heads are not attached to outputs by default.
5913 * This stops weston from extending the desktop onto head mounted displays.
5914 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005915 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005916 */
5917WL_EXPORT bool
5918weston_head_is_non_desktop(struct weston_head *head)
5919{
5920 return head->non_desktop;
5921}
5922
Pekka Paalanene19970f2017-08-28 14:11:02 +03005923/** Acknowledge device information change
5924 *
5925 * \param head The head to acknowledge.
5926 *
5927 * Clears the device changed flag on this head. When a compositor has processed
5928 * device information, it should call this to be able to notice further
5929 * changes.
5930 *
5931 * \sa weston_head_is_device_changed
Marius Vlad78984ee2019-06-11 00:05:08 +03005932 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03005933 */
5934WL_EXPORT void
5935weston_head_reset_device_changed(struct weston_head *head)
5936{
5937 head->device_changed = false;
5938}
5939
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005940/** Get the name of a head
5941 *
5942 * \param head The head to query.
5943 * \return The head's name, not NULL.
5944 *
5945 * The name depends on the backend. The DRM backend uses connector names,
5946 * other backends may use hardcoded names or user-given names.
Marius Vlad78984ee2019-06-11 00:05:08 +03005947 *
5948 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005949 */
5950WL_EXPORT const char *
5951weston_head_get_name(struct weston_head *head)
5952{
5953 return head->name;
5954}
5955
5956/** Get the output the head is attached to
5957 *
5958 * \param head The head to query.
5959 * \return The output the head is attached to, or NULL if detached.
Marius Vlad78984ee2019-06-11 00:05:08 +03005960 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005961 */
5962WL_EXPORT struct weston_output *
5963weston_head_get_output(struct weston_head *head)
5964{
5965 return head->output;
5966}
5967
Lucas Stacha69cb712019-11-25 23:29:31 +00005968/** Get the head's native transformation
5969 *
5970 * \param head The head to query.
5971 * \return The head's native transform, as a WL_OUTPUT_TRANSFORM_* value
5972 *
5973 * A weston_head may have a 'native' transform provided by the backend.
5974 * Examples include panels which are physically rotated, where the rotation
5975 * is recorded and described as part of the system configuration. This call
5976 * will return any known native transform for the head.
5977 *
5978 * \ingroup head
5979 */
5980WL_EXPORT uint32_t
5981weston_head_get_transform(struct weston_head *head)
5982{
5983 return head->transform;
5984}
5985
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005986/** Add destroy callback for a head
5987 *
5988 * \param head The head to watch for.
5989 * \param listener The listener to add. The \c notify member must be set.
5990 *
5991 * Heads may get destroyed for various reasons by the backends. If a head is
5992 * attached to an output, the compositor should listen for head destruction
5993 * and reconfigure or destroy the output if necessary.
5994 *
5995 * The destroy callbacks will be called on weston_head destruction before any
5996 * automatic detaching from an associated weston_output and before any
5997 * weston_head information is lost.
5998 *
5999 * The \c data argument to the notify callback is the weston_head being
6000 * destroyed.
Marius Vlad78984ee2019-06-11 00:05:08 +03006001 *
6002 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03006003 */
6004WL_EXPORT void
6005weston_head_add_destroy_listener(struct weston_head *head,
6006 struct wl_listener *listener)
6007{
6008 wl_signal_add(&head->destroy_signal, listener);
6009}
6010
6011/** Look up destroy listener for a head
6012 *
6013 * \param head The head to query.
6014 * \param notify The notify function used used for the added destroy listener.
6015 * \return The listener, or NULL if not found.
6016 *
6017 * This looks up the previously added destroy listener struct based on the
6018 * notify function it has. The listener can be used to access user data
6019 * through \c container_of().
6020 *
6021 * \sa wl_signal_get()
Marius Vlad78984ee2019-06-11 00:05:08 +03006022 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03006023 */
6024WL_EXPORT struct wl_listener *
6025weston_head_get_destroy_listener(struct weston_head *head,
6026 wl_notify_func_t notify)
6027{
6028 return wl_signal_get(&head->destroy_signal, notify);
6029}
6030
David Fort0de859e2016-05-27 23:22:57 +02006031/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02006032static void
David Fort0de859e2016-05-27 23:22:57 +02006033weston_compositor_reflow_outputs(struct weston_compositor *compositor,
6034 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02006035{
6036 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02006037 bool start_resizing = false;
6038
6039 if (!delta_width)
6040 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02006041
6042 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02006043 if (output == resized_output) {
6044 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02006045 continue;
6046 }
6047
David Fort0de859e2016-05-27 23:22:57 +02006048 if (start_resizing) {
6049 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02006050 output->dirty = 1;
6051 }
6052 }
6053}
6054
Pekka Paalanen06920792020-12-08 15:22:36 +02006055/** Transform a region in-place from global to output coordinates
6056 *
6057 * \param output The output that defines the transformation.
6058 * \param region The region to be transformed in-place.
6059 *
6060 * This takes a region in the global coordinate system, and takes into account
6061 * output position, transform and scale, and the zoom, and converts the region
6062 * into output pixel coordinates in the framebuffer.
6063 *
6064 * Uses floating-point operations if zoom is active, which may round to expand
6065 * the region.
6066 *
6067 * \internal
6068 * \ingroup output
6069 */
6070WL_EXPORT void
6071weston_output_region_from_global(struct weston_output *output,
6072 pixman_region32_t *region)
6073{
6074 if (output->zoom.active) {
6075 weston_matrix_transform_region(region, &output->matrix, region);
6076 } else {
6077 pixman_region32_translate(region, -output->x, -output->y);
6078 weston_transformed_region(output->width, output->height,
6079 output->transform,
6080 output->current_scale,
6081 region, region);
6082 }
6083}
6084
Pekka Paalanend72bad22017-03-29 17:01:41 +03006085static void
Scott Moreauccbf29d2012-02-22 14:21:41 -07006086weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01006087{
Scott Moreau850ca422012-05-21 15:21:25 -06006088 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05006089
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006090 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05006091 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06006092
Scott Moreauccbf29d2012-02-22 14:21:41 -07006093 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06006094 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05006095 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01006096 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05006097 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01006098 weston_matrix_scale(&output->matrix, magnification,
6099 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07006100 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04006101
Jason Ekstrandfb23df72014-10-16 10:55:21 -05006102 switch (output->transform) {
6103 case WL_OUTPUT_TRANSFORM_FLIPPED:
6104 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
6105 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
6106 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
6107 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
6108 weston_matrix_scale(&output->matrix, -1, 1, 1);
6109 break;
6110 }
6111
6112 switch (output->transform) {
6113 default:
6114 case WL_OUTPUT_TRANSFORM_NORMAL:
6115 case WL_OUTPUT_TRANSFORM_FLIPPED:
6116 break;
6117 case WL_OUTPUT_TRANSFORM_90:
6118 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +02006119 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
6120 weston_matrix_rotate_xy(&output->matrix, 0, -1);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05006121 break;
6122 case WL_OUTPUT_TRANSFORM_180:
6123 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
6124 weston_matrix_translate(&output->matrix,
6125 -output->width, -output->height, 0);
6126 weston_matrix_rotate_xy(&output->matrix, -1, 0);
6127 break;
6128 case WL_OUTPUT_TRANSFORM_270:
6129 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +02006130 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
6131 weston_matrix_rotate_xy(&output->matrix, 0, 1);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05006132 break;
6133 }
6134
6135 if (output->current_scale != 1)
6136 weston_matrix_scale(&output->matrix,
6137 output->current_scale,
6138 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01006139
Scott Moreauccbf29d2012-02-22 14:21:41 -07006140 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05006141
6142 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07006143}
6144
Scott Moreau1bad5db2012-08-18 01:04:05 -06006145static void
Alexander Larsson0b135062013-05-28 16:23:36 +02006146weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06006147{
6148 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03006149 output->native_scale = scale;
6150 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06006151
Pekka Paalanen59987fa2016-04-26 15:50:59 +03006152 convert_size_by_transform_scale(&output->width, &output->height,
6153 output->current_mode->width,
6154 output->current_mode->height,
6155 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02006156}
6157
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006158static void
6159weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07006160{
6161 output->x = x;
6162 output->y = y;
6163
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006164 pixman_region32_fini(&output->region);
Scott Moreauccbf29d2012-02-22 14:21:41 -07006165 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06006166 output->width,
6167 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01006168}
6169
Marius Vlad55d87362019-06-11 01:15:35 +03006170/**
6171 * \ingroup output
6172 */
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006173WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006174weston_output_move(struct weston_output *output, int x, int y)
6175{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006176 struct weston_head *head;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006177 struct wl_resource *resource;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006178 int ver;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006179
6180 output->move_x = x - output->x;
6181 output->move_y = y - output->y;
6182
6183 if (output->move_x == 0 && output->move_y == 0)
6184 return;
6185
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006186 weston_output_init_geometry(output, x, y);
6187
6188 output->dirty = 1;
6189
6190 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006191 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006192
6193 /* Notify clients of the change for output position. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006194 wl_list_for_each(head, &output->head_list, output_link) {
6195 wl_resource_for_each(resource, &head->resource_list) {
6196 wl_output_send_geometry(resource,
6197 output->x,
6198 output->y,
6199 head->mm_width,
6200 head->mm_height,
6201 head->subpixel,
6202 head->make,
6203 head->model,
6204 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08006205
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006206 ver = wl_resource_get_version(resource);
6207 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
6208 wl_output_send_done(resource);
6209 }
Roman Gilge97391c2019-03-29 13:01:06 +01006210
6211 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
6212 zxdg_output_v1_send_logical_position(resource,
6213 output->x,
6214 output->y);
6215 zxdg_output_v1_send_done(resource);
6216 }
Quanxian Wangb2c86362014-03-14 09:16:25 +08006217 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006218}
6219
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006220/** Signal that a pending output is taken into use.
6221 *
6222 * Removes the output from the pending list and adds it to the compositor's
6223 * list of enabled outputs. The output created signal is emitted.
Giulio Camuffob1147152015-05-06 21:41:57 +03006224 *
Pekka Paalanen2210ad02017-03-30 15:48:06 +03006225 * The output gets an internal ID assigned, and the wl_output global is
6226 * created.
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03006227 *
Giulio Camuffob1147152015-05-06 21:41:57 +03006228 * \param compositor The compositor instance.
6229 * \param output The output to be added.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006230 *
6231 * \internal
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006232 * \ingroup compositor
Giulio Camuffob1147152015-05-06 21:41:57 +03006233 */
Pekka Paalanenf9681b52017-03-29 16:58:48 +03006234static void
Giulio Camuffob1147152015-05-06 21:41:57 +03006235weston_compositor_add_output(struct weston_compositor *compositor,
6236 struct weston_output *output)
6237{
Armin Krezoviće5403842016-08-05 15:28:29 +02006238 struct weston_view *view, *next;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03006239 struct weston_head *head;
Armin Krezoviće5403842016-08-05 15:28:29 +02006240
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006241 assert(!output->enabled);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03006242
6243 /* Verify we haven't reached the limit of 32 available output IDs */
6244 assert(ffs(~compositor->output_id_pool) > 0);
6245
6246 /* Invert the output id pool and look for the lowest numbered
6247 * switch (the least significant bit). Take that bit's position
6248 * as our ID, and mark it used in the compositor's output_id_pool.
6249 */
6250 output->id = ffs(~compositor->output_id_pool) - 1;
6251 compositor->output_id_pool |= 1u << output->id;
6252
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006253 wl_list_remove(&output->link);
Giulio Camuffob1147152015-05-06 21:41:57 +03006254 wl_list_insert(compositor->output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006255 output->enabled = true;
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006256
Pekka Paalanendcac3512017-12-08 14:13:34 +02006257 wl_list_for_each(head, &output->head_list, output_link)
6258 weston_head_add_global(head);
Pekka Paalanen2210ad02017-03-30 15:48:06 +03006259
Giulio Camuffob1147152015-05-06 21:41:57 +03006260 wl_signal_emit(&compositor->output_created_signal, output);
Armin Krezoviće5403842016-08-05 15:28:29 +02006261
Pekka Paalanen6551c092021-05-03 16:09:45 +03006262 /*
6263 * Use view_list, as paint nodes have not been created for this
6264 * output yet. Any existing view might touch this new output.
6265 */
Armin Krezoviće5403842016-08-05 15:28:29 +02006266 wl_list_for_each_safe(view, next, &compositor->view_list, link)
6267 weston_view_geometry_dirty(view);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04006268}
6269
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006270/** Transform device coordinates into global coordinates
6271 *
Marius Vlada2dace22019-06-12 16:05:44 +03006272 * \param output the weston_output object
6273 * \param[in] device_x X coordinate in device units.
6274 * \param[in] device_y Y coordinate in device units.
6275 * \param[out] x X coordinate in the global space.
6276 * \param[out] y Y coordinate in the global space.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006277 *
Marius Vlada2dace22019-06-12 16:05:44 +03006278 * Transforms coordinates from the device coordinate space (physical pixel
6279 * units) to the global coordinate space (logical pixel units). This takes
6280 * into account output transform and scale.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006281 *
Marius Vlad55d87362019-06-11 01:15:35 +03006282 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006283 * \internal
6284 */
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006285WL_EXPORT void
6286weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02006287 double device_x, double device_y,
6288 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006289{
Derek Foreman0f679412014-10-02 13:41:17 -05006290 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02006291 device_x,
6292 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05006293 0.0,
6294 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006295
Derek Foreman67a18b92015-03-24 11:36:14 -05006296 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006297
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02006298 *x = p.f[0] / p.f[3];
6299 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006300}
6301
Pekka Paalanen1d2eee22021-02-25 12:03:28 +02006302static void
6303weston_output_reset_color_transforms(struct weston_output *output)
6304{
Pekka Paalanencda39512021-03-22 17:32:07 +02006305 weston_color_transform_unref(output->from_sRGB_to_output);
6306 output->from_sRGB_to_output = NULL;
Pekka Paalanen8fb23ed2021-03-23 13:40:43 +02006307 weston_color_transform_unref(output->from_sRGB_to_blend);
6308 output->from_sRGB_to_blend = NULL;
Pekka Paalanen1d2eee22021-02-25 12:03:28 +02006309 weston_color_transform_unref(output->from_blend_to_output);
6310 output->from_blend_to_output = NULL;
6311}
6312
Pekka Paalanen9a0aa372021-06-08 13:38:35 +03006313static bool
6314weston_output_set_color_transforms(struct weston_output *output)
6315{
6316 struct weston_color_manager *cm = output->compositor->color_manager;
6317 struct weston_color_transform *blend_to_output = NULL;
6318 struct weston_color_transform *sRGB_to_output = NULL;
6319 struct weston_color_transform *sRGB_to_blend = NULL;
6320 bool ok;
6321
6322 ok = cm->get_output_color_transform(cm, output, &blend_to_output);
6323 ok = ok && cm->get_sRGB_to_output_color_transform(cm, output,
6324 &sRGB_to_output);
6325 ok = ok && cm->get_sRGB_to_blend_color_transform(cm, output,
6326 &sRGB_to_blend);
6327 if (!ok) {
6328 weston_log("Creating color transformation for output \"%s\" failed.\n",
6329 output->name);
6330 weston_color_transform_unref(blend_to_output);
6331 weston_color_transform_unref(sRGB_to_output);
6332 weston_color_transform_unref(sRGB_to_blend);
6333
6334 return false;
6335 }
6336
6337 weston_output_reset_color_transforms(output);
6338 output->from_blend_to_output = blend_to_output;
6339 output->from_blend_to_output_by_backend = false;
6340 output->from_sRGB_to_output = sRGB_to_output;
6341 output->from_sRGB_to_blend = sRGB_to_blend;
6342
Pekka Paalanen9a9e6ce2021-06-08 14:17:44 +03006343 weston_log("Output '%s' using color profile: %s\n", output->name,
6344 weston_color_profile_get_description(output->color_profile));
6345
Pekka Paalanen9a0aa372021-06-08 13:38:35 +03006346 return true;
6347}
6348
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006349/** Removes output from compositor's list of enabled outputs
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006350 *
6351 * \param output The weston_output object that is being removed.
6352 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006353 * The following happens:
6354 *
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03006355 * - Destroys all paint nodes related to the output.
6356 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006357 * - The output assignments of all views in the current scenegraph are
6358 * recomputed.
6359 *
Pekka Paalanen1d2eee22021-02-25 12:03:28 +02006360 * - Destroys output's color transforms.
6361 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006362 * - Presentation feedback is discarded.
6363 *
6364 * - Compositor is notified that outputs were changed and
6365 * applies the necessary changes to re-layout outputs.
6366 *
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006367 * - The output is put back in the pending outputs list.
6368 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006369 * - Signal is emitted to notify all users of the weston_output
6370 * object that the output is being destroyed.
6371 *
6372 * - wl_output protocol objects referencing this weston_output
Pekka Paalanen2210ad02017-03-30 15:48:06 +03006373 * are made inert, and the wl_output global is removed.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006374 *
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03006375 * - The output's internal ID is released.
6376 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006377 * \ingroup compositor
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006378 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006379 */
6380static void
6381weston_compositor_remove_output(struct weston_output *output)
6382{
Pekka Paalanenbccda712017-03-29 16:16:04 +03006383 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03006384 struct weston_paint_node *pnode, *pntmp;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006385 struct weston_view *view;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03006386 struct weston_head *head;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006387
6388 assert(output->destroying);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006389 assert(output->enabled);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006390
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03006391 wl_list_for_each_safe(pnode, pntmp,
6392 &output->paint_node_list, output_link) {
6393 weston_paint_node_destroy(pnode);
6394 }
Pekka Paalanen2fddc532021-04-30 17:41:29 +03006395 assert(wl_list_empty(&output->paint_node_z_order_list));
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03006396
Pekka Paalanen6551c092021-05-03 16:09:45 +03006397 /*
6398 * Use view_list in case the output did not go through repaint
6399 * after a view came on it, lacking a paint node. Just to be sure.
6400 */
Pekka Paalanenbccda712017-03-29 16:16:04 +03006401 wl_list_for_each(view, &compositor->view_list, link) {
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006402 if (view->output_mask & (1u << output->id))
6403 weston_view_assign_output(view);
6404 }
6405
Pekka Paalanen1d2eee22021-02-25 12:03:28 +02006406 weston_output_reset_color_transforms(output);
6407
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006408 weston_presentation_feedback_discard_list(&output->feedback_list);
6409
Pekka Paalanen9711fd92018-06-21 14:26:18 +03006410 weston_compositor_reflow_outputs(compositor, output, -output->width);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006411
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006412 wl_list_remove(&output->link);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006413 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006414 output->enabled = false;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006415
Pekka Paalanenbccda712017-03-29 16:16:04 +03006416 wl_signal_emit(&compositor->output_destroyed_signal, output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006417 wl_signal_emit(&output->destroy_signal, output);
6418
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02006419 wl_list_for_each(head, &output->head_list, output_link)
6420 weston_head_remove_global(head);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03006421
6422 compositor->output_id_pool &= ~(1u << output->id);
6423 output->id = 0xffffffff; /* invalid */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006424}
6425
6426/** Sets the output scale for a given output.
6427 *
6428 * \param output The weston_output object that the scale is set for.
6429 * \param scale Scale factor for the given output.
6430 *
6431 * It only supports setting scale for an output that
6432 * is not enabled and it can only be ran once.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006433 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05006434 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006435 */
6436WL_EXPORT void
6437weston_output_set_scale(struct weston_output *output,
6438 int32_t scale)
6439{
6440 /* We can only set scale on a disabled output */
6441 assert(!output->enabled);
6442
6443 /* We only want to set scale once */
6444 assert(!output->scale);
6445
6446 output->scale = scale;
6447}
6448
6449/** Sets the output transform for a given output.
6450 *
6451 * \param output The weston_output object that the transform is set for.
6452 * \param transform Transform value for the given output.
6453 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006454 * Refer to wl_output::transform section located at
6455 * https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_output
6456 * for list of values that can be passed to this function.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006457 *
Marius Vlad55d87362019-06-11 01:15:35 +03006458 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006459 */
6460WL_EXPORT void
6461weston_output_set_transform(struct weston_output *output,
6462 uint32_t transform)
6463{
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006464 struct weston_pointer_motion_event ev;
6465 struct wl_resource *resource;
6466 struct weston_seat *seat;
6467 pixman_region32_t old_region;
6468 int mid_x, mid_y;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006469 struct weston_head *head;
6470 int ver;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006471
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006472 if (!output->enabled && output->transform == UINT32_MAX) {
6473 output->transform = transform;
6474 return;
6475 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006476
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006477 weston_output_transform_scale_init(output, transform, output->scale);
6478
6479 pixman_region32_init(&old_region);
6480 pixman_region32_copy(&old_region, &output->region);
6481
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006482 weston_output_init_geometry(output, output->x, output->y);
6483
6484 output->dirty = 1;
6485
6486 /* Notify clients of the change for output transform. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006487 wl_list_for_each(head, &output->head_list, output_link) {
6488 wl_resource_for_each(resource, &head->resource_list) {
6489 wl_output_send_geometry(resource,
6490 output->x,
6491 output->y,
6492 head->mm_width,
6493 head->mm_height,
6494 head->subpixel,
6495 head->make,
6496 head->model,
6497 output->transform);
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006498
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006499 ver = wl_resource_get_version(resource);
6500 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
6501 wl_output_send_done(resource);
6502 }
Roman Gilge97391c2019-03-29 13:01:06 +01006503 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
6504 zxdg_output_v1_send_logical_position(resource,
6505 output->x,
6506 output->y);
6507 zxdg_output_v1_send_logical_size(resource,
6508 output->width,
6509 output->height);
6510 zxdg_output_v1_send_done(resource);
6511 }
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006512 }
6513
6514 /* we must ensure that pointers are inside output, otherwise they disappear */
6515 mid_x = output->x + output->width / 2;
6516 mid_y = output->y + output->height / 2;
6517
6518 ev.mask = WESTON_POINTER_MOTION_ABS;
6519 ev.x = wl_fixed_to_double(wl_fixed_from_int(mid_x));
6520 ev.y = wl_fixed_to_double(wl_fixed_from_int(mid_y));
6521
6522 wl_list_for_each(seat, &output->compositor->seat_list, link) {
6523 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
6524
6525 if (pointer && pixman_region32_contains_point(&old_region,
6526 wl_fixed_to_int(pointer->x),
6527 wl_fixed_to_int(pointer->y),
6528 NULL))
6529 weston_pointer_move(pointer, &ev);
6530 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006531}
6532
Pekka Paalanen9a9e6ce2021-06-08 14:17:44 +03006533/** Set output's color profile
6534 *
6535 * \param output The output to change.
6536 * \param cprof The color profile to set. Can be NULL for default sRGB profile.
6537 * \return True on success, or false on failure.
6538 *
6539 * Calling this function changes the color profile of the output. This causes
6540 * all existing weston_color_transform objects related to this output via
6541 * paint nodes to be unreferenced and later re-created on demand.
6542 *
6543 * This function may not be called from within weston_output_repaint().
6544 *
6545 * On failure, nothing is changed.
6546 *
6547 * \ingroup output
6548 */
6549WL_EXPORT bool
6550weston_output_set_color_profile(struct weston_output *output,
6551 struct weston_color_profile *cprof)
6552{
6553 struct weston_color_profile *old;
6554 struct weston_paint_node *pnode;
6555
6556 old = output->color_profile;
6557 output->color_profile = weston_color_profile_ref(cprof);
6558
6559 if (output->enabled) {
6560 if (!weston_output_set_color_transforms(output)) {
6561 /* Failed, roll back */
6562 weston_color_profile_unref(output->color_profile);
6563 output->color_profile = old;
6564 return false;
6565 }
6566
6567 /* Remove outdated cached color transformations */
6568 wl_list_for_each(pnode, &output->paint_node_list, output_link) {
6569 weston_surface_color_transform_fini(&pnode->surf_xform);
6570 pnode->surf_xform_valid = false;
6571 }
6572 }
6573
6574 weston_color_profile_unref(old);
6575
6576 return true;
6577}
6578
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006579/** Initializes a weston_output object with enough data so
6580 ** an output can be configured.
6581 *
6582 * \param output The weston_output object to initialize
6583 * \param compositor The compositor instance.
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006584 * \param name Name for the output (the string is copied).
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006585 *
6586 * Sets initial values for fields that are expected to be
6587 * configured either by compositors or backends.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006588 *
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006589 * The name is used in logs, and can be used by compositors as a configuration
6590 * identifier.
6591 *
Marius Vlad55d87362019-06-11 01:15:35 +03006592 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006593 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006594 */
6595WL_EXPORT void
Armin Krezović40087402016-09-30 14:11:12 +02006596weston_output_init(struct weston_output *output,
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006597 struct weston_compositor *compositor,
6598 const char *name)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006599{
6600 output->compositor = compositor;
6601 output->destroying = 0;
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006602 output->name = strdup(name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006603 wl_list_init(&output->link);
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006604 wl_signal_init(&output->user_destroy_signal);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006605 output->enabled = false;
Ankit Nautiyal2690a772019-03-25 17:57:59 +05306606 output->desired_protection = WESTON_HDCP_DISABLE;
Ankit Nautiyal2844f8e2019-04-03 10:14:59 +05306607 output->allow_protection = true;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006608
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006609 wl_list_init(&output->head_list);
6610
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006611 /* Add some (in)sane defaults which can be used
6612 * for checking if an output was properly configured
6613 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006614 output->scale = 0;
6615 /* Can't use -1 on uint32_t and 0 is valid enum value */
6616 output->transform = UINT32_MAX;
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006617
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006618 pixman_region32_init(&output->region);
Pekka Paalanen42704142017-09-06 16:47:52 +03006619 wl_list_init(&output->mode_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006620}
6621
6622/** Adds weston_output object to pending output list.
6623 *
6624 * \param output The weston_output object to add
6625 * \param compositor The compositor instance.
6626 *
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03006627 * The opposite of this operation is built into weston_output_release().
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006628 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006629 * \ingroup compositor
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006630 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006631 */
6632WL_EXPORT void
6633weston_compositor_add_pending_output(struct weston_output *output,
6634 struct weston_compositor *compositor)
6635{
Pekka Paalanene952a012017-03-29 17:14:00 +03006636 assert(output->disable);
6637 assert(output->enable);
6638
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006639 wl_list_remove(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006640 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006641}
6642
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006643/** Create a string with the attached heads' names.
6644 *
6645 * The string must be free()'d.
Marius Vlad55d87362019-06-11 01:15:35 +03006646 *
6647 * \ingroup output
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006648 */
6649static char *
6650weston_output_create_heads_string(struct weston_output *output)
6651{
6652 FILE *fp;
6653 char *str = NULL;
6654 size_t size = 0;
6655 struct weston_head *head;
6656 const char *sep = "";
6657
6658 fp = open_memstream(&str, &size);
6659 if (!fp)
6660 return NULL;
6661
6662 wl_list_for_each(head, &output->head_list, output_link) {
6663 fprintf(fp, "%s%s", sep, head->name);
6664 sep = ", ";
6665 }
6666 fclose(fp);
6667
6668 return str;
6669}
6670
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006671/** Constructs a weston_output object that can be used by the compositor.
6672 *
Pekka Paalanencc201e42017-03-30 15:11:25 +03006673 * \param output The weston_output object that needs to be enabled. Must not
Pekka Paalanenddce54d2017-08-23 16:00:21 +03006674 * be enabled already. Must have at least one head attached.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006675 *
6676 * Output coordinates are calculated and each new output is by default
6677 * assigned to the right of previous one.
6678 *
6679 * Sets up the transformation, zoom, and geometry of the output using
6680 * the properties that need to be configured by the compositor.
6681 *
6682 * Establishes a repaint timer for the output with the relevant display
6683 * object's event loop. See output_repaint_timer_handler().
6684 *
6685 * The output is assigned an ID. Weston can support up to 32 distinct
6686 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
6687 * is referred to and used to find the first available ID number, and
6688 * then this ID is marked as used in output_id_pool.
6689 *
6690 * The output is also assigned a Wayland global with the wl_output
6691 * external interface.
6692 *
6693 * Backend specific function is called to set up the output output.
6694 *
6695 * Output is added to the compositor's output list
6696 *
6697 * If the backend specific function fails, the weston_output object
6698 * is returned to a state it was before calling this function and
6699 * is added to the compositor's pending_output_list in case it needs
6700 * to be reconfigured or just so it can be destroyed at shutdown.
6701 *
6702 * 0 is returned on success, -1 on failure.
Marius Vlad55d87362019-06-11 01:15:35 +03006703 *
6704 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006705 */
6706WL_EXPORT int
6707weston_output_enable(struct weston_output *output)
6708{
Armin Krezović782f5df2016-09-30 14:11:11 +02006709 struct weston_compositor *c = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006710 struct weston_output *iterator;
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03006711 struct weston_head *head;
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006712 char *head_names;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006713 int x = 0, y = 0;
6714
Pekka Paalanencc201e42017-03-30 15:11:25 +03006715 if (output->enabled) {
6716 weston_log("Error: attempt to enable an enabled output '%s'\n",
6717 output->name);
6718 return -1;
6719 }
6720
Pekka Paalanenddce54d2017-08-23 16:00:21 +03006721 if (wl_list_empty(&output->head_list)) {
6722 weston_log("Error: cannot enable output '%s' without heads.\n",
6723 output->name);
6724 return -1;
6725 }
6726
Pekka Paalanen586e1ac2017-09-14 16:17:59 +03006727 if (wl_list_empty(&output->mode_list) || !output->current_mode) {
6728 weston_log("Error: no video mode for output '%s'.\n",
6729 output->name);
6730 return -1;
6731 }
6732
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03006733 wl_list_for_each(head, &output->head_list, output_link) {
6734 assert(head->make);
6735 assert(head->model);
6736 }
6737
Armin Krezović782f5df2016-09-30 14:11:11 +02006738 iterator = container_of(c->output_list.prev,
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006739 struct weston_output, link);
6740
Armin Krezović782f5df2016-09-30 14:11:11 +02006741 if (!wl_list_empty(&c->output_list))
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006742 x = iterator->x + iterator->width;
6743
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006744 /* Make sure the scale is set up */
6745 assert(output->scale);
6746
6747 /* Make sure we have a transform set */
6748 assert(output->transform != UINT32_MAX);
6749
Armin Krezović782f5df2016-09-30 14:11:11 +02006750 output->x = x;
6751 output->y = y;
6752 output->dirty = 1;
6753 output->original_scale = output->scale;
6754
Marius Vlad3a2f8292019-11-04 17:53:46 +02006755 wl_signal_init(&output->frame_signal);
6756 wl_signal_init(&output->destroy_signal);
6757
Armin Krezović782f5df2016-09-30 14:11:11 +02006758 weston_output_transform_scale_init(output, output->transform, output->scale);
6759 weston_output_init_zoom(output);
6760
6761 weston_output_init_geometry(output, x, y);
6762 weston_output_damage(output);
6763
Armin Krezović782f5df2016-09-30 14:11:11 +02006764 wl_list_init(&output->animation_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02006765 wl_list_init(&output->feedback_list);
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03006766 wl_list_init(&output->paint_node_list);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03006767 wl_list_init(&output->paint_node_z_order_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02006768
Pekka Paalanen9a0aa372021-06-08 13:38:35 +03006769 if (!weston_output_set_color_transforms(output))
Pekka Paalanen1d2eee22021-02-25 12:03:28 +02006770 return -1;
Pekka Paalanen1d2eee22021-02-25 12:03:28 +02006771
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006772 /* Enable the output (set up the crtc or create a
6773 * window representing the output, set up the
6774 * renderer, etc)
6775 */
6776 if (output->enable(output) < 0) {
6777 weston_log("Enabling output \"%s\" failed.\n", output->name);
Pekka Paalanen1d2eee22021-02-25 12:03:28 +02006778 weston_output_reset_color_transforms(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006779 return -1;
6780 }
6781
6782 weston_compositor_add_output(output->compositor, output);
6783
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006784 head_names = weston_output_create_heads_string(output);
6785 weston_log("Output '%s' enabled with head(s) %s\n",
6786 output->name, head_names);
6787 free(head_names);
6788
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006789 return 0;
6790}
6791
6792/** Converts a weston_output object to a pending output state, so it
6793 ** can be configured again or destroyed.
6794 *
6795 * \param output The weston_output object that needs to be disabled.
6796 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006797 * Calls a backend specific function to disable an output, in case
6798 * such function exists.
6799 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03006800 * The backend specific disable function may choose to postpone the disabling
6801 * by returning a negative value, in which case this function returns early.
6802 * In that case the backend will guarantee the output will be disabled soon
6803 * by the backend calling this function again. One must not attempt to re-enable
6804 * the output until that happens.
6805 *
6806 * Otherwise, if the output is being used by the compositor, it is removed
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006807 * from weston's output_list (see weston_compositor_remove_output())
6808 * and is returned to a state it was before weston_output_enable()
6809 * was ran (see weston_output_enable_undo()).
6810 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03006811 * See weston_output_init() for more information on the
6812 * state output is returned to.
Pekka Paalanencc201e42017-03-30 15:11:25 +03006813 *
6814 * If the output has never been enabled yet, this function can still be
6815 * called to ensure that the output is actually turned off rather than left
6816 * in the state it was discovered in.
Marius Vlad55d87362019-06-11 01:15:35 +03006817 *
6818 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006819 */
6820WL_EXPORT void
6821weston_output_disable(struct weston_output *output)
6822{
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006823 /* Should we rename this? */
6824 output->destroying = 1;
6825
Pekka Paalanenc65df642017-03-29 15:45:46 +03006826 /* Disable is called unconditionally also for not-enabled outputs,
6827 * because at compositor start-up, if there is an output that is
6828 * already on but the compositor wants to turn it off, we have to
6829 * forward the turn-off to the backend so it knows to do it.
6830 * The backend cannot initially turn off everything, because it
6831 * would cause unnecessary mode-sets for all outputs the compositor
6832 * wants to be on.
6833 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006834 if (output->disable(output) < 0)
6835 return;
6836
Pekka Paalanen2ec15eb2021-06-09 16:14:45 +03006837 if (output->enabled) {
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006838 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006839
Pekka Paalanen2ec15eb2021-06-09 16:14:45 +03006840 assert(wl_list_empty(&output->paint_node_list));
6841 }
6842
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006843 output->destroying = 0;
6844}
6845
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006846/** Forces a synchronous call to heads_changed hook
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006847 *
6848 * \param compositor The compositor instance
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006849 *
6850 * If there are new or changed heads, calls the heads_changed hook and
6851 * returns after the hook returns.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006852 *
6853 * \ingroup compositor
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006854 */
6855WL_EXPORT void
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006856weston_compositor_flush_heads_changed(struct weston_compositor *compositor)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006857{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03006858 if (compositor->heads_changed_source) {
6859 wl_event_source_remove(compositor->heads_changed_source);
6860 weston_compositor_call_heads_changed(compositor);
6861 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006862}
6863
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006864/** Add destroy callback for an output
6865 *
6866 * \param output The output to watch.
6867 * \param listener The listener to add. The \c notify member must be set.
6868 *
6869 * The listener callback will be called when user destroys an output. This
6870 * may be delayed by a backend in some cases. The main purpose of the
6871 * listener is to allow hooking up custom data to the output. The custom data
6872 * can be fetched via weston_output_get_destroy_listener() followed by
6873 * container_of().
6874 *
6875 * The \c data argument to the notify callback is the weston_output being
6876 * destroyed.
6877 *
6878 * @note This is for the final destruction of an output, not when it gets
6879 * disabled. If you want to keep track of enabled outputs, this is not it.
Marius Vlad55d87362019-06-11 01:15:35 +03006880 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05006881 * \ingroup output
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006882 */
6883WL_EXPORT void
6884weston_output_add_destroy_listener(struct weston_output *output,
6885 struct wl_listener *listener)
6886{
6887 wl_signal_add(&output->user_destroy_signal, listener);
6888}
6889
6890/** Look up destroy listener for an output
6891 *
6892 * \param output The output to query.
6893 * \param notify The notify function used used for the added destroy listener.
6894 * \return The listener, or NULL if not found.
6895 *
6896 * This looks up the previously added destroy listener struct based on the
6897 * notify function it has. The listener can be used to access user data
6898 * through \c container_of().
6899 *
6900 * \sa wl_signal_get() weston_output_add_destroy_listener()
Marius Vlad55d87362019-06-11 01:15:35 +03006901 * \ingroup output
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006902 */
6903WL_EXPORT struct wl_listener *
6904weston_output_get_destroy_listener(struct weston_output *output,
6905 wl_notify_func_t notify)
6906{
6907 return wl_signal_get(&output->user_destroy_signal, notify);
6908}
6909
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006910/** Uninitialize an output
6911 *
6912 * Removes the output from the list of enabled outputs if necessary, but
6913 * does not call the backend's output disable function. The output will no
6914 * longer be in the list of pending outputs either.
6915 *
6916 * All fields of weston_output become uninitialized, i.e. should not be used
6917 * anymore. The caller can free the memory after this.
6918 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05006919 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006920 * \internal
6921 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006922WL_EXPORT void
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03006923weston_output_release(struct weston_output *output)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006924{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006925 struct weston_head *head, *tmp;
6926
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006927 output->destroying = 1;
6928
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006929 wl_signal_emit(&output->user_destroy_signal, output);
6930
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03006931 if (output->idle_repaint_source)
6932 wl_event_source_remove(output->idle_repaint_source);
6933
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006934 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006935 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006936
Pekka Paalanen9a9e6ce2021-06-08 14:17:44 +03006937 weston_color_profile_unref(output->color_profile);
6938
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006939 pixman_region32_fini(&output->region);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006940 wl_list_remove(&output->link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006941
6942 wl_list_for_each_safe(head, tmp, &output->head_list, output_link)
6943 weston_head_detach(head);
6944
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006945 free(output->name);
6946}
6947
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006948/** Find an output by its given name
6949 *
6950 * \param compositor The compositor to search in.
6951 * \param name The output name to search for.
6952 * \return An existing output with the given name, or NULL if not found.
6953 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006954 * \ingroup compositor
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006955 */
6956WL_EXPORT struct weston_output *
6957weston_compositor_find_output_by_name(struct weston_compositor *compositor,
6958 const char *name)
6959{
6960 struct weston_output *output;
6961
6962 wl_list_for_each(output, &compositor->output_list, link)
6963 if (strcmp(output->name, name) == 0)
6964 return output;
6965
6966 wl_list_for_each(output, &compositor->pending_output_list, link)
6967 if (strcmp(output->name, name) == 0)
6968 return output;
6969
6970 return NULL;
6971}
6972
6973/** Create a named output
6974 *
6975 * \param compositor The compositor.
6976 * \param name The name for the output.
6977 * \return A new \c weston_output, or NULL on failure.
6978 *
6979 * This creates a new weston_output that starts with no heads attached.
6980 *
6981 * An output must be configured and it must have at least one head before
6982 * it can be enabled.
6983 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006984 * \ingroup compositor
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006985 */
6986WL_EXPORT struct weston_output *
6987weston_compositor_create_output(struct weston_compositor *compositor,
6988 const char *name)
6989{
6990 assert(compositor->backend->create_output);
6991
6992 if (weston_compositor_find_output_by_name(compositor, name)) {
6993 weston_log("Warning: attempted to create an output with a "
6994 "duplicate name '%s'.\n", name);
6995 return NULL;
6996 }
6997
6998 return compositor->backend->create_output(compositor, name);
6999}
7000
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03007001/** Create an output for an unused head
7002 *
7003 * \param compositor The compositor.
7004 * \param head The head to attach to the output.
7005 * \return A new \c weston_output, or NULL on failure.
7006 *
7007 * This creates a new weston_output that starts with the given head attached.
7008 * The output inherits the name of the head. The head must not be already
7009 * attached to another output.
7010 *
7011 * An output must be configured before it can be enabled.
7012 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007013 * \ingroup compositor
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03007014 */
7015WL_EXPORT struct weston_output *
7016weston_compositor_create_output_with_head(struct weston_compositor *compositor,
7017 struct weston_head *head)
7018{
7019 struct weston_output *output;
7020
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02007021 output = weston_compositor_create_output(compositor, head->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03007022 if (!output)
7023 return NULL;
7024
7025 if (weston_output_attach_head(output, head) < 0) {
Pekka Paalanen42c0e142017-10-27 12:07:49 +03007026 weston_output_destroy(output);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03007027 return NULL;
7028 }
7029
7030 return output;
7031}
7032
7033/** Destroy an output
7034 *
7035 * \param output The output to destroy.
7036 *
7037 * The heads attached to the given output are detached and become unused again.
7038 *
7039 * It is not necessary to explicitly destroy all outputs at compositor exit.
Leandro Ribeiroca640d52020-01-27 19:12:01 -03007040 * weston_compositor_destroy() will automatically destroy any remaining
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03007041 * outputs.
7042 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05007043 * \ingroup output
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03007044 */
7045WL_EXPORT void
7046weston_output_destroy(struct weston_output *output)
7047{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03007048 output->destroy(output);
7049}
7050
Pekka Paalanencf0a4762017-04-04 16:36:07 +03007051/** When you need a head...
7052 *
7053 * This function is a hack, used until all code has been converted to become
7054 * multi-head aware.
7055 *
7056 * \param output The weston_output whose head to get.
7057 * \return The first head in the output's list.
Marius Vlad55d87362019-06-11 01:15:35 +03007058 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05007059 * \ingroup output
Pekka Paalanencf0a4762017-04-04 16:36:07 +03007060 */
7061WL_EXPORT struct weston_head *
7062weston_output_get_first_head(struct weston_output *output)
7063{
7064 if (wl_list_empty(&output->head_list))
7065 return NULL;
7066
7067 return container_of(output->head_list.next,
7068 struct weston_head, output_link);
7069}
7070
Ankit Nautiyal2844f8e2019-04-03 10:14:59 +05307071/** Allow/Disallow content-protection support for an output
7072 *
7073 * This function sets the allow_protection member for an output. Setting of
7074 * this field will allow the compositor to attempt content-protection for this
7075 * output, for a backend that supports the content-protection protocol.
7076 *
7077 * \param output The weston_output for whom the content-protection is to be
7078 * allowed.
7079 * \param allow_protection The bool value which is to be set.
7080 */
7081WL_EXPORT void
7082weston_output_allow_protection(struct weston_output *output,
7083 bool allow_protection)
7084{
7085 output->allow_protection = allow_protection;
7086}
7087
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01007088static void
Roman Gilge97391c2019-03-29 13:01:06 +01007089xdg_output_unlist(struct wl_resource *resource)
7090{
7091 wl_list_remove(wl_resource_get_link(resource));
7092}
7093
7094static void
7095xdg_output_destroy(struct wl_client *client, struct wl_resource *resource)
7096{
7097 wl_resource_destroy(resource);
7098}
7099
7100static const struct zxdg_output_v1_interface xdg_output_interface = {
7101 xdg_output_destroy
7102};
7103
7104static void
7105xdg_output_manager_destroy(struct wl_client *client,
7106 struct wl_resource *resource)
7107{
7108 wl_resource_destroy(resource);
7109}
7110
7111static void
7112xdg_output_manager_get_xdg_output(struct wl_client *client,
7113 struct wl_resource *manager,
7114 uint32_t id,
7115 struct wl_resource *output_resource)
7116{
7117 int version = wl_resource_get_version(manager);
7118 struct weston_head *head = wl_resource_get_user_data(output_resource);
7119 struct weston_output *output = head->output;
7120 struct wl_resource *resource;
7121
7122 resource = wl_resource_create(client, &zxdg_output_v1_interface,
7123 version, id);
7124 if (resource == NULL) {
7125 wl_client_post_no_memory(client);
7126 return;
7127 }
7128
7129 wl_list_insert(&head->xdg_output_resource_list,
7130 wl_resource_get_link(resource));
7131
7132 wl_resource_set_implementation(resource, &xdg_output_interface,
7133 NULL, xdg_output_unlist);
7134
7135 zxdg_output_v1_send_logical_position(resource, output->x, output->y);
7136 zxdg_output_v1_send_logical_size(resource,
7137 output->width,
7138 output->height);
7139 if (version >= ZXDG_OUTPUT_V1_NAME_SINCE_VERSION)
7140 zxdg_output_v1_send_name(resource, head->name);
7141
7142 zxdg_output_v1_send_done(resource);
7143}
7144
7145static const struct zxdg_output_manager_v1_interface xdg_output_manager_interface = {
7146 xdg_output_manager_destroy,
7147 xdg_output_manager_get_xdg_output
7148};
7149
7150static void
7151bind_xdg_output_manager(struct wl_client *client,
7152 void *data, uint32_t version, uint32_t id)
7153{
7154 struct wl_resource *resource;
7155
7156 resource = wl_resource_create(client, &zxdg_output_manager_v1_interface,
7157 version, id);
7158 if (resource == NULL) {
7159 wl_client_post_no_memory(client);
7160 return;
7161 }
7162
7163 wl_resource_set_implementation(resource, &xdg_output_manager_interface,
7164 NULL, NULL);
7165}
7166
7167static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007168destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01007169{
Jonny Lamb74130762013-11-26 18:19:46 +01007170 struct weston_surface *surface =
7171 wl_resource_get_user_data(resource);
7172
Pekka Paalanen4826f872016-04-22 14:14:38 +03007173 if (!surface)
7174 return;
7175
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007176 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02007177 surface->pending.buffer_viewport.buffer.src_width =
7178 wl_fixed_from_int(-1);
7179 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02007180 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01007181}
7182
7183static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007184viewport_destroy(struct wl_client *client,
7185 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01007186{
7187 wl_resource_destroy(resource);
7188}
7189
7190static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007191viewport_set_source(struct wl_client *client,
7192 struct wl_resource *resource,
7193 wl_fixed_t src_x,
7194 wl_fixed_t src_y,
7195 wl_fixed_t src_width,
7196 wl_fixed_t src_height)
7197{
7198 struct weston_surface *surface =
7199 wl_resource_get_user_data(resource);
7200
Pekka Paalanen4826f872016-04-22 14:14:38 +03007201 if (!surface) {
7202 wl_resource_post_error(resource,
7203 WP_VIEWPORT_ERROR_NO_SURFACE,
7204 "wl_surface for this viewport is no longer exists");
7205 return;
7206 }
7207
7208 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03007209 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007210
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007211 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03007212 src_height == wl_fixed_from_int(-1) &&
7213 src_x == wl_fixed_from_int(-1) &&
7214 src_y == wl_fixed_from_int(-1)) {
7215 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007216 surface->pending.buffer_viewport.buffer.src_width =
7217 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02007218 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007219 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007220 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007221
Pekka Paalanen201769a2016-04-26 14:42:11 +03007222 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007223 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007224 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03007225 "wl_surface@%d viewport source "
7226 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
7227 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007228 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03007229 wl_fixed_to_double(src_height),
7230 wl_fixed_to_double(src_x),
7231 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007232 return;
7233 }
7234
7235 surface->pending.buffer_viewport.buffer.src_x = src_x;
7236 surface->pending.buffer_viewport.buffer.src_y = src_y;
7237 surface->pending.buffer_viewport.buffer.src_width = src_width;
7238 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02007239 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007240}
7241
7242static void
7243viewport_set_destination(struct wl_client *client,
7244 struct wl_resource *resource,
7245 int32_t dst_width,
7246 int32_t dst_height)
7247{
7248 struct weston_surface *surface =
7249 wl_resource_get_user_data(resource);
7250
Pekka Paalanen4826f872016-04-22 14:14:38 +03007251 if (!surface) {
7252 wl_resource_post_error(resource,
7253 WP_VIEWPORT_ERROR_NO_SURFACE,
7254 "wl_surface for this viewport no longer exists");
7255 return;
7256 }
7257
7258 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007259
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007260 if (dst_width == -1 && dst_height == -1) {
7261 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007262 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02007263 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007264 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007265 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007266
7267 if (dst_width <= 0 || dst_height <= 0) {
7268 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007269 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007270 "destination size must be positive (%dx%d)",
7271 dst_width, dst_height);
7272 return;
7273 }
7274
7275 surface->pending.buffer_viewport.surface.width = dst_width;
7276 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02007277 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007278}
7279
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007280static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007281 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007282 viewport_set_source,
7283 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01007284};
7285
7286static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007287viewporter_destroy(struct wl_client *client,
7288 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01007289{
7290 wl_resource_destroy(resource);
7291}
7292
7293static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007294viewporter_get_viewport(struct wl_client *client,
7295 struct wl_resource *viewporter,
7296 uint32_t id,
7297 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01007298{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007299 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007300 struct weston_surface *surface =
7301 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01007302 struct wl_resource *resource;
7303
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007304 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007305 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007306 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007307 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01007308 return;
7309 }
7310
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007311 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007312 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01007313 if (resource == NULL) {
7314 wl_client_post_no_memory(client);
7315 return;
7316 }
7317
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007318 wl_resource_set_implementation(resource, &viewport_interface,
7319 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01007320
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007321 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01007322}
7323
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007324static const struct wp_viewporter_interface viewporter_interface = {
7325 viewporter_destroy,
7326 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01007327};
7328
7329static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007330bind_viewporter(struct wl_client *client,
7331 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01007332{
7333 struct wl_resource *resource;
7334
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007335 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06007336 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01007337 if (resource == NULL) {
7338 wl_client_post_no_memory(client);
7339 return;
7340 }
7341
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007342 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01007343 NULL, NULL);
7344}
7345
7346static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04007347destroy_presentation_feedback(struct wl_resource *feedback_resource)
7348{
7349 struct weston_presentation_feedback *feedback;
7350
7351 feedback = wl_resource_get_user_data(feedback_resource);
7352
7353 wl_list_remove(&feedback->link);
7354 free(feedback);
7355}
7356
7357static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007358presentation_destroy(struct wl_client *client, struct wl_resource *resource)
7359{
7360 wl_resource_destroy(resource);
7361}
7362
7363static void
7364presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04007365 struct wl_resource *presentation_resource,
7366 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007367 uint32_t callback)
7368{
Pekka Paalanen133e4392014-09-23 22:08:46 -04007369 struct weston_surface *surface;
7370 struct weston_presentation_feedback *feedback;
7371
7372 surface = wl_resource_get_user_data(surface_resource);
7373
Bryce Harringtonde16d892014-11-20 22:21:57 -08007374 feedback = zalloc(sizeof *feedback);
7375 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04007376 goto err_calloc;
7377
7378 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007379 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04007380 1, callback);
7381 if (!feedback->resource)
7382 goto err_create;
7383
7384 wl_resource_set_implementation(feedback->resource, NULL, feedback,
7385 destroy_presentation_feedback);
7386
7387 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
7388
7389 return;
7390
7391err_create:
7392 free(feedback);
7393
7394err_calloc:
7395 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007396}
7397
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007398static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007399 presentation_destroy,
7400 presentation_feedback
7401};
7402
7403static void
7404bind_presentation(struct wl_client *client,
7405 void *data, uint32_t version, uint32_t id)
7406{
7407 struct weston_compositor *compositor = data;
7408 struct wl_resource *resource;
7409
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007410 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06007411 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007412 if (resource == NULL) {
7413 wl_client_post_no_memory(client);
7414 return;
7415 }
7416
7417 wl_resource_set_implementation(resource, &presentation_implementation,
7418 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007419 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007420}
7421
7422static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05007423compositor_bind(struct wl_client *client,
7424 void *data, uint32_t version, uint32_t id)
7425{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007426 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05007427 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05007428
Jason Ekstranda85118c2013-06-27 20:17:02 -05007429 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06007430 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07007431 if (resource == NULL) {
7432 wl_client_post_no_memory(client);
7433 return;
7434 }
7435
7436 wl_resource_set_implementation(resource, &compositor_interface,
7437 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05007438}
7439
Daniel Stonece62cb32018-07-20 09:46:24 +01007440static const char *
7441output_repaint_status_text(struct weston_output *output)
7442{
7443 switch (output->repaint_status) {
7444 case REPAINT_NOT_SCHEDULED:
7445 return "no repaint";
7446 case REPAINT_BEGIN_FROM_IDLE:
7447 return "start_repaint_loop scheduled";
7448 case REPAINT_SCHEDULED:
7449 return "repaint scheduled";
7450 case REPAINT_AWAITING_COMPLETION:
7451 return "awaiting completion";
7452 }
7453
7454 assert(!"output_repaint_status_text missing enum");
7455 return NULL;
7456}
7457
7458static void
7459debug_scene_view_print_buffer(FILE *fp, struct weston_view *view)
7460{
7461 struct weston_buffer *buffer = view->surface->buffer_ref.buffer;
7462 struct wl_shm_buffer *shm;
7463 struct linux_dmabuf_buffer *dmabuf;
Marius Vlad00a6e012018-11-20 17:52:31 +02007464 const struct pixel_format_info *pixel_info = NULL;
Daniel Stonece62cb32018-07-20 09:46:24 +01007465
7466 if (!buffer) {
7467 fprintf(fp, "\t\t[buffer not available]\n");
7468 return;
7469 }
7470
7471 shm = wl_shm_buffer_get(buffer->resource);
7472 if (shm) {
Marius Vlad00a6e012018-11-20 17:52:31 +02007473 uint32_t _format = wl_shm_buffer_get_format(shm);
7474 pixel_info = pixel_format_get_info_shm(_format);
Daniel Stonece62cb32018-07-20 09:46:24 +01007475 fprintf(fp, "\t\tSHM buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02007476 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
7477 (unsigned long) _format,
7478 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Daniel Stonece62cb32018-07-20 09:46:24 +01007479 return;
7480 }
7481
7482 dmabuf = linux_dmabuf_buffer_get(buffer->resource);
7483 if (dmabuf) {
Marius Vlad6f6fd262021-07-12 12:58:34 +03007484 uint64_t modifier = dmabuf->attributes.modifier[0];
7485 char *modifier_name = pixel_format_get_modifier(modifier);
Marius Vlad00a6e012018-11-20 17:52:31 +02007486 pixel_info = pixel_format_get_info(dmabuf->attributes.format);
Daniel Stonece62cb32018-07-20 09:46:24 +01007487 fprintf(fp, "\t\tdmabuf buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02007488 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
7489 (unsigned long) dmabuf->attributes.format,
7490 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Marius Vlad6f6fd262021-07-12 12:58:34 +03007491
7492 fprintf(fp, "\t\t\tmodifier: %s\n", modifier_name ? modifier_name :
7493 "Failed to convert to a modifier name");
7494 free(modifier_name);
Daniel Stonece62cb32018-07-20 09:46:24 +01007495 return;
7496 }
7497
Marius Vlad253ba9a2019-03-17 18:22:21 +02007498 fprintf(fp, "\t\tEGL buffer\n");
Daniel Stonece62cb32018-07-20 09:46:24 +01007499}
7500
7501static void
7502debug_scene_view_print(FILE *fp, struct weston_view *view, int view_idx)
7503{
7504 struct weston_compositor *ec = view->surface->compositor;
7505 struct weston_output *output;
7506 char desc[512];
7507 pixman_box32_t *box;
7508 uint32_t surface_id = 0;
7509 pid_t pid = 0;
7510
7511 if (view->surface->resource) {
7512 struct wl_resource *resource = view->surface->resource;
7513 wl_client_get_credentials(wl_resource_get_client(resource),
7514 &pid, NULL, NULL);
7515 surface_id = wl_resource_get_id(view->surface->resource);
7516 }
7517
7518 if (!view->surface->get_label ||
7519 view->surface->get_label(view->surface, desc, sizeof(desc)) < 0) {
7520 strcpy(desc, "[no description available]");
7521 }
7522 fprintf(fp, "\tView %d (role %s, PID %d, surface ID %u, %s, %p):\n",
7523 view_idx, view->surface->role_name, pid, surface_id,
7524 desc, view);
7525
7526 box = pixman_region32_extents(&view->transform.boundingbox);
7527 fprintf(fp, "\t\tposition: (%d, %d) -> (%d, %d)\n",
7528 box->x1, box->y1, box->x2, box->y2);
7529 box = pixman_region32_extents(&view->transform.opaque);
7530
Michael Olbrichb7e5f102020-08-11 16:33:35 +02007531 if (weston_view_is_opaque(view, &view->transform.boundingbox)) {
Daniel Stonece62cb32018-07-20 09:46:24 +01007532 fprintf(fp, "\t\t[fully opaque]\n");
7533 } else if (!pixman_region32_not_empty(&view->transform.opaque)) {
7534 fprintf(fp, "\t\t[not opaque]\n");
7535 } else {
7536 fprintf(fp, "\t\t[opaque: (%d, %d) -> (%d, %d)]\n",
7537 box->x1, box->y1, box->x2, box->y2);
7538 }
7539
7540 if (view->alpha < 1.0)
7541 fprintf(fp, "\t\talpha: %f\n", view->alpha);
7542
7543 if (view->output_mask != 0) {
7544 bool first_output = true;
7545 fprintf(fp, "\t\toutputs: ");
7546 wl_list_for_each(output, &ec->output_list, link) {
7547 if (!(view->output_mask & (1 << output->id)))
7548 continue;
7549 fprintf(fp, "%s%d (%s)%s",
7550 (first_output) ? "" : ", ",
7551 output->id, output->name,
7552 (view->output == output) ? " (primary)" : "");
7553 first_output = false;
7554 }
7555 } else {
7556 fprintf(fp, "\t\t[no outputs]");
7557 }
7558
7559 fprintf(fp, "\n");
7560
7561 debug_scene_view_print_buffer(fp, view);
7562}
7563
Marius Vlad433f4e72019-02-17 22:14:23 +02007564static void
7565debug_scene_view_print_tree(struct weston_view *view,
Marius Vlada6acfa82019-03-17 18:10:09 +02007566 FILE *fp, int *view_idx)
Marius Vlad433f4e72019-02-17 22:14:23 +02007567{
7568 struct weston_subsurface *sub;
7569 struct weston_view *ev;
7570
7571 /*
7572 * print the view first, then we recursively go on printing
7573 * sub-surfaces. We bail out once no more sub-surfaces are available.
7574 */
Marius Vlada6acfa82019-03-17 18:10:09 +02007575 debug_scene_view_print(fp, view, *view_idx);
Marius Vlad433f4e72019-02-17 22:14:23 +02007576
7577 /* no more sub-surfaces */
7578 if (wl_list_empty(&view->surface->subsurface_list))
7579 return;
7580
7581 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
7582 wl_list_for_each(ev, &sub->surface->views, surface_link) {
Michael Olbrichef5f3232020-04-29 09:03:15 +02007583 /* only print the child views of the current view */
7584 if (ev->parent_view != view)
Marius Vlad433f4e72019-02-17 22:14:23 +02007585 continue;
Marius Vlada6acfa82019-03-17 18:10:09 +02007586
7587 (*view_idx)++;
Marius Vlad433f4e72019-02-17 22:14:23 +02007588 debug_scene_view_print_tree(ev, fp, view_idx);
7589 }
7590 }
7591}
7592
Daniel Stonece62cb32018-07-20 09:46:24 +01007593/**
7594 * Output information on how libweston is currently composing the scene
7595 * graph.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007596 *
7597 * \ingroup compositor
Daniel Stonece62cb32018-07-20 09:46:24 +01007598 */
7599WL_EXPORT char *
7600weston_compositor_print_scene_graph(struct weston_compositor *ec)
7601{
7602 struct weston_output *output;
7603 struct weston_layer *layer;
7604 struct timespec now;
7605 int layer_idx = 0;
7606 FILE *fp;
7607 char *ret;
7608 size_t len;
7609 int err;
7610
7611 fp = open_memstream(&ret, &len);
7612 assert(fp);
7613
7614 weston_compositor_read_presentation_clock(ec, &now);
7615 fprintf(fp, "Weston scene graph at %ld.%09ld:\n\n",
7616 now.tv_sec, now.tv_nsec);
7617
7618 wl_list_for_each(output, &ec->output_list, link) {
7619 struct weston_head *head;
7620 int head_idx = 0;
7621
7622 fprintf(fp, "Output %d (%s):\n", output->id, output->name);
7623 assert(output->enabled);
7624
7625 fprintf(fp, "\tposition: (%d, %d) -> (%d, %d)\n",
7626 output->x, output->y,
7627 output->x + output->width,
7628 output->y + output->height);
7629 fprintf(fp, "\tmode: %dx%d@%.3fHz\n",
7630 output->current_mode->width,
7631 output->current_mode->height,
7632 output->current_mode->refresh / 1000.0);
7633 fprintf(fp, "\tscale: %d\n", output->scale);
7634
7635 fprintf(fp, "\trepaint status: %s\n",
7636 output_repaint_status_text(output));
7637 if (output->repaint_status == REPAINT_SCHEDULED)
7638 fprintf(fp, "\tnext repaint: %ld.%09ld\n",
7639 output->next_repaint.tv_sec,
7640 output->next_repaint.tv_nsec);
7641
7642 wl_list_for_each(head, &output->head_list, output_link) {
7643 fprintf(fp, "\tHead %d (%s): %sconnected\n",
7644 head_idx++, head->name,
7645 (head->connected) ? "" : "not ");
7646 }
7647 }
7648
7649 fprintf(fp, "\n");
7650
7651 wl_list_for_each(layer, &ec->layer_list, link) {
7652 struct weston_view *view;
7653 int view_idx = 0;
7654
7655 fprintf(fp, "Layer %d (pos 0x%lx):\n", layer_idx++,
7656 (unsigned long) layer->position);
7657
7658 if (!weston_layer_mask_is_infinite(layer)) {
7659 fprintf(fp, "\t[mask: (%d, %d) -> (%d,%d)]\n\n",
7660 layer->mask.x1, layer->mask.y1,
7661 layer->mask.x2, layer->mask.y2);
7662 }
7663
Marius Vlada6acfa82019-03-17 18:10:09 +02007664 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
7665 debug_scene_view_print_tree(view, fp, &view_idx);
7666 view_idx++;
7667 }
Daniel Stonece62cb32018-07-20 09:46:24 +01007668
7669 if (wl_list_empty(&layer->view_list.link))
7670 fprintf(fp, "\t[no views]\n");
7671
7672 fprintf(fp, "\n");
7673 }
7674
7675 err = fclose(fp);
7676 assert(err == 0);
7677
7678 return ret;
7679}
7680
7681/**
7682 * Called when the 'scene-graph' debug scope is bound by a client. This
7683 * one-shot weston-debug scope prints the current scene graph when bound,
7684 * and then terminates the stream.
7685 */
7686static void
Marius Vladdad882a2019-07-17 15:43:53 +03007687debug_scene_graph_cb(struct weston_log_subscription *sub, void *data)
Daniel Stonece62cb32018-07-20 09:46:24 +01007688{
7689 struct weston_compositor *ec = data;
7690 char *str = weston_compositor_print_scene_graph(ec);
7691
Marius Vladdad882a2019-07-17 15:43:53 +03007692 weston_log_subscription_printf(sub, "%s", str);
Daniel Stonece62cb32018-07-20 09:46:24 +01007693 free(str);
Marius Vladdad882a2019-07-17 15:43:53 +03007694 weston_log_subscription_complete(sub);
Daniel Stonece62cb32018-07-20 09:46:24 +01007695}
7696
Leandro Ribeiro32a5acd2020-10-19 16:06:22 -03007697/** Retrieve testsuite data from compositor
7698 *
7699 * The testsuite data can be defined by the test suite of projects that uses
7700 * libweston and given to the compositor at the moment of its creation. This
7701 * function should be used when we need to retrieve the testsuite private data
7702 * from the compositor.
7703 *
7704 * \param ec The weston compositor.
7705 * \return The testsuite data.
7706 *
7707 * \ingroup compositor
7708 * \sa weston_compositor_test_data_init
7709 */
7710WL_EXPORT void *
7711weston_compositor_get_test_data(struct weston_compositor *ec)
7712{
7713 return ec->test_data.test_private_data;
7714}
7715
Giulio Camuffo459137b2014-10-11 23:56:24 +03007716/** Create the compositor.
7717 *
7718 * This functions creates and initializes a compositor instance.
7719 *
7720 * \param display The Wayland display to be used.
7721 * \param user_data A pointer to an object that can later be retrieved
Marius Vlada2dace22019-06-12 16:05:44 +03007722 * \param log_ctx A pointer to weston_debug_compositor
Leandro Ribeirocdb72182020-11-06 11:24:22 -03007723 * \param test_data Optional testsuite data, or NULL.
Giulio Camuffo459137b2014-10-11 23:56:24 +03007724 * using the \ref weston_compositor_get_user_data function.
7725 * \return The compositor instance on success or NULL on failure.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007726 *
7727 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007728 */
7729WL_EXPORT struct weston_compositor *
Marius Vlad880b4852019-04-07 17:07:58 +03007730weston_compositor_create(struct wl_display *display,
Leandro Ribeirocdb72182020-11-06 11:24:22 -03007731 struct weston_log_context *log_ctx, void *user_data,
7732 const struct weston_testsuite_data *test_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04007733{
Giulio Camuffo459137b2014-10-11 23:56:24 +03007734 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05007735 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07007736
Leandro Ribeirobd9c0a62020-01-17 10:47:49 -03007737 if (!log_ctx)
7738 return NULL;
7739
Giulio Camuffo459137b2014-10-11 23:56:24 +03007740 ec = zalloc(sizeof *ec);
7741 if (!ec)
7742 return NULL;
7743
Leandro Ribeirocdb72182020-11-06 11:24:22 -03007744 if (test_data)
7745 ec->test_data = *test_data;
7746
Leandro Ribeirobd9c0a62020-01-17 10:47:49 -03007747 ec->weston_log_ctx = log_ctx;
Giulio Camuffo459137b2014-10-11 23:56:24 +03007748 ec->wl_display = display;
7749 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04007750 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07007751 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04007752 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03007753 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03007754 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02007755 wl_signal_init(&ec->idle_signal);
7756 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02007757 wl_signal_init(&ec->show_input_panel_signal);
7758 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02007759 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01007760 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01007761 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02007762 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02007763 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02007764 wl_signal_init(&ec->output_resized_signal);
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03007765 wl_signal_init(&ec->heads_changed_signal);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02007766 wl_signal_init(&ec->output_heads_changed_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07007767 wl_signal_init(&ec->session_signal);
Robert Beckettc569bdc2019-07-10 16:40:12 +01007768 ec->session_active = true;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04007769
Casey Dahlin58ba1372012-04-19 22:50:08 -04007770 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03007771 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04007772
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02007773 ec->activate_serial = 1;
7774
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05007775 ec->touch_mode = WESTON_TOUCH_MODE_NORMAL;
7776
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05307777 ec->content_protection = NULL;
7778
Derek Foreman152254b2015-11-26 14:17:48 -06007779 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04007780 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007781 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05007782
Giulio Camuffo954f1832014-10-11 18:27:30 +03007783 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04007784 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007785 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03007786
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007787 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007788 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007789 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01007790
Roman Gilge97391c2019-03-29 13:01:06 +01007791 if (!wl_global_create(ec->wl_display, &zxdg_output_manager_v1_interface, 2,
7792 ec, bind_xdg_output_manager))
7793 goto fail;
7794
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007795 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007796 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007797 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007798
Jonas Ådahl30d61d82014-10-22 21:21:17 +02007799 if (weston_input_init(ec) != 0)
7800 goto fail;
7801
Jason Ekstranda7af7042013-10-12 22:38:11 -05007802 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02007803 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007804 wl_list_init(&ec->layer_list);
7805 wl_list_init(&ec->seat_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007806 wl_list_init(&ec->pending_output_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007807 wl_list_init(&ec->output_list);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03007808 wl_list_init(&ec->head_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007809 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01007810 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007811 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01007812 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007813 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02007814 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007815
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007816 wl_list_init(&ec->plugin_api_list);
7817
Xiong Zhang97116532013-10-23 13:58:31 +08007818 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02007819 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04007820
Giulio Camuffo459137b2014-10-11 23:56:24 +03007821 wl_data_device_manager_init(ec->wl_display);
7822
7823 wl_display_init_shm(ec->wl_display);
7824
7825 loop = wl_display_get_event_loop(ec->wl_display);
7826 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Daniel Stone6847b852017-03-01 11:34:08 +00007827 ec->repaint_timer =
7828 wl_event_loop_add_timer(loop, output_repaint_timer_handler,
7829 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007830
Quentin Glidic82681572016-12-17 13:40:51 +01007831 weston_layer_init(&ec->fade_layer, ec);
7832 weston_layer_init(&ec->cursor_layer, ec);
7833
7834 weston_layer_set_position(&ec->fade_layer, WESTON_LAYER_POSITION_FADE);
7835 weston_layer_set_position(&ec->cursor_layer,
7836 WESTON_LAYER_POSITION_CURSOR);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007837
Daniel Stonece62cb32018-07-20 09:46:24 +01007838 ec->debug_scene =
Leandro Ribeiroce100192019-12-26 16:35:49 -03007839 weston_compositor_add_log_scope(ec, "scene-graph",
7840 "Scene graph details\n",
7841 debug_scene_graph_cb, NULL,
7842 ec);
Daniel Stonece62cb32018-07-20 09:46:24 +01007843
Marius Vladda104eb2019-09-05 14:31:01 +03007844 ec->timeline =
Leandro Ribeiroce100192019-12-26 16:35:49 -03007845 weston_compositor_add_log_scope(ec, "timeline",
7846 "Timeline event points\n",
7847 weston_timeline_create_subscription,
7848 weston_timeline_destroy_subscription,
7849 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007850 return ec;
7851
7852fail:
7853 free(ec);
7854 return NULL;
7855}
7856
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007857/** weston_compositor_shutdown
7858 * \ingroup compositor
7859 */
Benjamin Franzkeb8263022011-08-30 11:32:47 +02007860WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007861weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07007862{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007863 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07007864
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007865 wl_event_source_remove(ec->idle_source);
Lujin Wangd4b46cd2019-08-12 12:03:29 -07007866 wl_event_source_remove(ec->repaint_timer);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007867
Matt Roper361d2ad2011-08-29 13:52:23 -07007868 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02007869 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07007870 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02007871
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007872 /* Destroy all pending outputs associated with this compositor */
7873 wl_list_for_each_safe(output, next, &ec->pending_output_list, link)
7874 output->destroy(output);
7875
Pekka Paalanen90a5ffa2021-02-25 12:03:28 +02007876 /* Color manager objects may have renderer hooks */
7877 if (ec->color_manager) {
7878 ec->color_manager->destroy(ec->color_manager);
7879 ec->color_manager = NULL;
7880 }
7881
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02007882 if (ec->renderer)
7883 ec->renderer->destroy(ec);
7884
Daniel Stone325fc2d2012-05-30 16:31:58 +01007885 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09007886 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01007887 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01007888 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01007889 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02007890 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007891
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04007892 weston_plane_release(&ec->primary_plane);
Pekka Paalanen87400372021-05-14 14:29:40 +03007893
7894 weston_layer_fini(&ec->fade_layer);
7895 weston_layer_fini(&ec->cursor_layer);
7896
7897 if (!wl_list_empty(&ec->layer_list))
7898 weston_log("BUG: layer_list is not empty after shutdown. Calls to weston_layer_fini() are missing somwhere.\n");
Matt Roper361d2ad2011-08-29 13:52:23 -07007899}
7900
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007901/** weston_compositor_exit_with_code
7902 * \ingroup compositor
7903 */
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05007904WL_EXPORT void
Adam Jackson3c3f3b12019-10-16 16:02:59 -04007905weston_compositor_exit_with_code(struct weston_compositor *compositor,
Frederic Plourdec336f062014-10-29 14:44:33 -04007906 int exit_code)
7907{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02007908 if (compositor->exit_code == EXIT_SUCCESS)
7909 compositor->exit_code = exit_code;
7910
Giulio Camuffo459137b2014-10-11 23:56:24 +03007911 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04007912}
7913
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007914/** weston_compositor_set_default_pointer_grab
7915 * \ingroup compositor
7916 */
Frederic Plourdec336f062014-10-29 14:44:33 -04007917WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007918weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
7919 const struct weston_pointer_grab_interface *interface)
7920{
7921 struct weston_seat *seat;
7922
7923 ec->default_pointer_grab = interface;
7924 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05007925 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
7926
7927 if (pointer)
7928 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007929 }
7930}
7931
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007932/** weston_compositor_set_presentation_clock
7933 * \ingroup compositor
7934 */
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007935WL_EXPORT int
7936weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
7937 clockid_t clk_id)
7938{
7939 struct timespec ts;
7940
7941 if (clock_gettime(clk_id, &ts) < 0)
7942 return -1;
7943
7944 compositor->presentation_clock = clk_id;
7945
7946 return 0;
7947}
7948
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007949/** For choosing the software clock, when the display hardware or API
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007950 * does not expose a compatible presentation timestamp.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007951 *
7952 * \ingroup compositor
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007953 */
7954WL_EXPORT int
7955weston_compositor_set_presentation_clock_software(
7956 struct weston_compositor *compositor)
7957{
7958 /* In order of preference */
7959 static const clockid_t clocks[] = {
7960 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
7961 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
7962 CLOCK_MONOTONIC, /* no jumps, may crawl */
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007963 };
7964 unsigned i;
7965
7966 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
7967 if (weston_compositor_set_presentation_clock(compositor,
7968 clocks[i]) == 0)
7969 return 0;
7970
7971 weston_log("Error: no suitable presentation clock available.\n");
7972
7973 return -1;
7974}
7975
Pekka Paalanen662f3842015-03-18 12:17:26 +02007976/** Read the current time from the Presentation clock
7977 *
7978 * \param compositor
Marius Vlada2dace22019-06-12 16:05:44 +03007979 * \param[out] ts The current time.
Pekka Paalanen662f3842015-03-18 12:17:26 +02007980 *
7981 * \note Reading the current time in user space is always imprecise to some
7982 * degree.
7983 *
7984 * This function is never meant to fail. If reading the clock does fail,
7985 * an error message is logged and a zero time is returned. Callers are not
7986 * supposed to detect or react to failures.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007987 *
7988 * \ingroup compositor
Pekka Paalanen662f3842015-03-18 12:17:26 +02007989 */
7990WL_EXPORT void
7991weston_compositor_read_presentation_clock(
7992 const struct weston_compositor *compositor,
7993 struct timespec *ts)
7994{
7995 static bool warned;
7996 int ret;
7997
7998 ret = clock_gettime(compositor->presentation_clock, ts);
7999 if (ret < 0) {
8000 ts->tv_sec = 0;
8001 ts->tv_nsec = 0;
8002
8003 if (!warned)
8004 weston_log("Error: failure to read "
Antonio Borneo39578632019-04-26 23:57:31 +02008005 "the presentation clock %#x: '%s' (%d)\n",
8006 compositor->presentation_clock,
8007 strerror(errno), errno);
Pekka Paalanen662f3842015-03-18 12:17:26 +02008008 warned = true;
8009 }
8010}
8011
Pekka Paalanen230f3b12014-09-29 14:18:40 -04008012/** Import dmabuf buffer into current renderer
8013 *
8014 * \param compositor
8015 * \param buffer the dmabuf buffer to import
8016 * \return true on usable buffers, false otherwise
8017 *
8018 * This function tests that the linux_dmabuf_buffer is usable
8019 * for the current renderer. Returns false on unusable buffers. Usually
8020 * usability is tested by importing the dmabufs for composition.
8021 *
8022 * This hook is also used for detecting if the renderer supports
8023 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
8024 * supported.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008025 *
8026 * \ingroup compositor
8027 */
Pekka Paalanen230f3b12014-09-29 14:18:40 -04008028WL_EXPORT bool
8029weston_compositor_import_dmabuf(struct weston_compositor *compositor,
8030 struct linux_dmabuf_buffer *buffer)
8031{
8032 struct weston_renderer *renderer;
8033
8034 renderer = compositor->renderer;
8035
8036 if (renderer->import_dmabuf == NULL)
8037 return false;
8038
8039 return renderer->import_dmabuf(compositor, buffer);
8040}
8041
Marius Vlad5a701542019-11-16 20:26:52 +02008042WL_EXPORT bool
8043weston_compositor_dmabuf_can_scanout(struct weston_compositor *compositor,
8044 struct linux_dmabuf_buffer *buffer)
8045{
8046 struct weston_backend *backend = compositor->backend;
8047
8048 if (backend->can_scanout_dmabuf == NULL)
8049 return false;
8050
8051 return backend->can_scanout_dmabuf(compositor, buffer);
8052}
8053
Giulio Camuffocdb4d292013-11-14 23:42:53 +01008054WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05008055weston_version(int *major, int *minor, int *micro)
8056{
8057 *major = WESTON_VERSION_MAJOR;
8058 *minor = WESTON_VERSION_MINOR;
8059 *micro = WESTON_VERSION_MICRO;
8060}
8061
Daniel Stonee03c1112016-11-24 20:45:45 +00008062/**
8063 * Attempts to find a module path from the module map specified in the
8064 * environment. If found, writes the full path into the path variable.
8065 *
8066 * The module map is a string in environment variable WESTON_MODULE_MAP, where
8067 * each entry is of the form "name=path" and entries are separated by
8068 * semicolons. Whitespace is significant.
8069 *
8070 * \param name The name to search for.
8071 * \param path Where the path is written to if found.
8072 * \param path_len Allocated bytes at \c path .
8073 * \returns The length of the string written to path on success, or 0 if the
8074 * module was not specified in the environment map or path_len was too small.
8075 */
8076WL_EXPORT size_t
8077weston_module_path_from_env(const char *name, char *path, size_t path_len)
8078{
8079 const char *mapping = getenv("WESTON_MODULE_MAP");
8080 const char *end;
8081 const int name_len = strlen(name);
8082
8083 if (!mapping)
8084 return 0;
8085
8086 end = mapping + strlen(mapping);
8087 while (mapping < end && *mapping) {
8088 const char *filename, *next;
8089
8090 /* early out: impossibly short string */
8091 if (end - mapping < name_len + 1)
8092 return 0;
8093
8094 filename = &mapping[name_len + 1];
8095 next = strchrnul(mapping, ';');
8096
8097 if (strncmp(mapping, name, name_len) == 0 &&
8098 mapping[name_len] == '=') {
8099 size_t file_len = next - filename; /* no trailing NUL */
8100 if (file_len >= path_len)
8101 return 0;
8102 strncpy(path, filename, file_len);
8103 path[file_len] = '\0';
8104 return file_len;
8105 }
8106
8107 mapping = next + 1;
8108 }
8109
8110 return 0;
8111}
8112
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03008113WL_EXPORT void *
8114weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04008115{
8116 char path[PATH_MAX];
8117 void *module, *init;
Daniel Stonebeb97e52016-11-28 12:13:54 +00008118 size_t len;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04008119
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08008120 if (name == NULL)
8121 return NULL;
8122
Derek Foreman3f86e502015-06-08 11:46:54 -05008123 if (name[0] != '/') {
Daniel Stonee03c1112016-11-24 20:45:45 +00008124 len = weston_module_path_from_env(name, path, sizeof path);
8125 if (len == 0)
Daniel Stonebeb97e52016-11-28 12:13:54 +00008126 len = snprintf(path, sizeof path, "%s/%s",
8127 LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05008128 } else {
Daniel Stonebeb97e52016-11-28 12:13:54 +00008129 len = snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05008130 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04008131
Daniel Stonebeb97e52016-11-28 12:13:54 +00008132 /* snprintf returns the length of the string it would've written,
8133 * _excluding_ the NUL byte. So even being equal to the size of
8134 * our buffer is an error here. */
8135 if (len >= sizeof path)
8136 return NULL;
8137
Kristian Høgsberga6813d22012-09-12 12:21:01 -04008138 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
8139 if (module) {
8140 weston_log("Module '%s' already loaded\n", path);
Pekka Paalanenf696ee92019-11-04 14:07:51 +02008141 } else {
8142 weston_log("Loading module '%s'\n", path);
8143 module = dlopen(path, RTLD_NOW);
8144 if (!module) {
8145 weston_log("Failed to load module: %s\n", dlerror());
8146 return NULL;
8147 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04008148 }
8149
8150 init = dlsym(module, entrypoint);
8151 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03008152 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00008153 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04008154 return NULL;
8155 }
8156
8157 return init;
8158}
8159
Pekka Paalanen6ffbba32019-11-06 12:59:32 +02008160/** Add a compositor destroy listener only once
8161 *
8162 * \param compositor The compositor whose destroy to watch for.
8163 * \param listener The listener struct to initialize.
8164 * \param destroy_handler The callback when compositor is destroyed.
8165 * \return True if listener is added, or false if there already is a listener
8166 * with the given \c destroy_handler.
8167 *
8168 * This function does nothing and returns false if the given callback function
8169 * is already present in the weston_compositor destroy callbacks list.
8170 * Otherwise, this function initializes the given listener with the given
8171 * callback pointer and adds it to the compositor's destroy callbacks list.
8172 *
8173 * This can be used to ensure that plugin initialization is done only once
8174 * in case the same plugin is loaded multiple times. If this function returns
8175 * false, the plugin should be already initialized successfully.
8176 *
8177 * All plugins should register a destroy listener for cleaning up. Note, that
8178 * the plugin destruction order is not guaranteed: plugins that depend on other
8179 * plugins must be able to be torn down in arbitrary order.
8180 *
Leandro Ribeiroca640d52020-01-27 19:12:01 -03008181 * \sa weston_compositor_destroy
Pekka Paalanen6ffbba32019-11-06 12:59:32 +02008182 */
8183WL_EXPORT bool
8184weston_compositor_add_destroy_listener_once(struct weston_compositor *compositor,
8185 struct wl_listener *listener,
8186 wl_notify_func_t destroy_handler)
8187{
8188 if (wl_signal_get(&compositor->destroy_signal, destroy_handler))
8189 return false;
8190
8191 listener->notify = destroy_handler;
8192 wl_signal_add(&compositor->destroy_signal, listener);
8193 return true;
8194}
Giulio Camuffo459137b2014-10-11 23:56:24 +03008195
Leandro Ribeiroca640d52020-01-27 19:12:01 -03008196/** Destroys the compositor.
Giulio Camuffo459137b2014-10-11 23:56:24 +03008197 *
Leandro Ribeiroca640d52020-01-27 19:12:01 -03008198 * This function cleans up the compositor state and then destroys it.
Giulio Camuffo459137b2014-10-11 23:56:24 +03008199 *
Leandro Ribeiroca640d52020-01-27 19:12:01 -03008200 * @param compositor The compositor to be destroyed.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008201 *
Marius Vlad284d5342019-06-24 12:00:47 +03008202 * @ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03008203 */
8204WL_EXPORT void
Leandro Ribeiroca640d52020-01-27 19:12:01 -03008205weston_compositor_destroy(struct weston_compositor *compositor)
Giulio Camuffo459137b2014-10-11 23:56:24 +03008206{
8207 /* prevent further rendering while shutting down */
8208 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
8209
8210 wl_signal_emit(&compositor->destroy_signal, compositor);
8211
8212 weston_compositor_xkb_destroy(compositor);
8213
Giulio Camuffo2d24e642015-10-03 16:25:15 +03008214 if (compositor->backend)
8215 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02008216
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03008217 /* The backend is responsible for destroying the heads. */
8218 assert(wl_list_empty(&compositor->head_list));
8219
Pekka Paalanen827b5d22016-06-29 11:54:26 +02008220 weston_plugin_api_destroy_list(compositor);
8221
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03008222 if (compositor->heads_changed_source)
8223 wl_event_source_remove(compositor->heads_changed_source);
8224
Leandro Ribeirof0149642019-12-18 15:52:18 -03008225 weston_log_scope_destroy(compositor->debug_scene);
Daniel Stonece62cb32018-07-20 09:46:24 +01008226 compositor->debug_scene = NULL;
Marius Vladda104eb2019-09-05 14:31:01 +03008227
Leandro Ribeirof0149642019-12-18 15:52:18 -03008228 weston_log_scope_destroy(compositor->timeline);
Marius Vladda104eb2019-09-05 14:31:01 +03008229 compositor->timeline = NULL;
Pekka Paalanena5630ea2017-10-12 13:13:42 +02008230
Leandro Ribeiro8eb84142021-01-18 19:36:48 -03008231 if (compositor->default_dmabuf_feedback) {
8232 weston_dmabuf_feedback_destroy(compositor->default_dmabuf_feedback);
8233 weston_dmabuf_feedback_format_table_destroy(compositor->dmabuf_feedback_format_table);
8234 }
8235
Giulio Camuffo459137b2014-10-11 23:56:24 +03008236 free(compositor);
8237}
8238
8239/** Instruct the compositor to exit.
8240 *
8241 * This functions does not directly destroy the compositor object, it merely
8242 * command it to start the tear down process. It is not guaranteed that the
8243 * tear down will happen immediately.
8244 *
8245 * \param compositor The compositor to tear down.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008246 *
8247 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03008248 */
8249WL_EXPORT void
8250weston_compositor_exit(struct weston_compositor *compositor)
8251{
8252 compositor->exit(compositor);
8253}
8254
8255/** Return the user data stored in the compositor.
8256 *
8257 * This function returns the user data pointer set with user_data parameter
8258 * to the \ref weston_compositor_create function.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008259 *
8260 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03008261 */
8262WL_EXPORT void *
8263weston_compositor_get_user_data(struct weston_compositor *compositor)
8264{
8265 return compositor->user_data;
8266}
Pekka Paalanendd186732016-06-03 14:49:54 +03008267
Pekka Paalanen50dbf382016-06-03 15:23:46 +03008268static const char * const backend_map[] = {
8269 [WESTON_BACKEND_DRM] = "drm-backend.so",
8270 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
8271 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
8272 [WESTON_BACKEND_RDP] = "rdp-backend.so",
8273 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
8274 [WESTON_BACKEND_X11] = "x11-backend.so",
8275};
8276
Pekka Paalanendd186732016-06-03 14:49:54 +03008277/** Load a backend into a weston_compositor
8278 *
8279 * A backend must be loaded to make a weston_compositor work. A backend
8280 * provides input and output capabilities, and determines the renderer to use.
8281 *
8282 * \param compositor A compositor that has not had a backend loaded yet.
8283 * \param backend Name of the backend file.
8284 * \param config_base A pointer to a backend-specific configuration
8285 * structure's 'base' member.
8286 *
8287 * \return 0 on success, or -1 on error.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008288 *
8289 * \ingroup compositor
Pekka Paalanendd186732016-06-03 14:49:54 +03008290 */
8291WL_EXPORT int
8292weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03008293 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03008294 struct weston_backend_config *config_base)
8295{
8296 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03008297 struct weston_backend_config *config_base);
8298
Pekka Paalanend7e35112017-08-29 17:04:12 +03008299 if (compositor->backend) {
8300 weston_log("Error: attempt to load a backend when one is already loaded\n");
8301 return -1;
8302 }
8303
Quentin Glidic887c0182016-07-10 11:00:53 +02008304 if (backend >= ARRAY_LENGTH(backend_map))
Pekka Paalanen50dbf382016-06-03 15:23:46 +03008305 return -1;
8306
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01008307 backend_init = weston_load_module(backend_map[backend], "weston_backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03008308 if (!backend_init)
8309 return -1;
8310
Pekka Paalanend7e35112017-08-29 17:04:12 +03008311 if (backend_init(compositor, config_base) < 0) {
8312 compositor->backend = NULL;
8313 return -1;
8314 }
8315
Pekka Paalanen90a5ffa2021-02-25 12:03:28 +02008316 if (!compositor->color_manager) {
8317 compositor->color_manager =
8318 weston_color_manager_noop_create(compositor);
8319 }
8320
8321 if (!compositor->color_manager)
8322 return -1;
8323
8324 if (!compositor->color_manager->init(compositor->color_manager))
8325 return -1;
8326
8327 weston_log("Color manager: %s\n", compositor->color_manager->name);
8328
Pekka Paalanend7e35112017-08-29 17:04:12 +03008329 return 0;
Pekka Paalanendd186732016-06-03 14:49:54 +03008330}
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008331
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008332/** weston_compositor_load_xwayland
8333 * \ingroup compositor
8334 */
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008335WL_EXPORT int
8336weston_compositor_load_xwayland(struct weston_compositor *compositor)
8337{
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01008338 int (*module_init)(struct weston_compositor *ec);
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008339
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01008340 module_init = weston_load_module("xwayland.so", "weston_module_init");
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008341 if (!module_init)
8342 return -1;
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01008343 if (module_init(compositor) < 0)
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008344 return -1;
8345 return 0;
8346}
Vasilis Liaskovitis486b4632018-10-10 16:14:55 +02008347
Pekka Paalanen5e79dd42021-03-29 14:11:49 +03008348/** Load Little CMS color manager plugin
8349 *
8350 * Calling this function before loading any backend sets Little CMS
8351 * as the active color matching module (CMM) instead of the default no-op
8352 * color manager.
8353 *
8354 * \ingroup compositor
8355 */
8356WL_EXPORT int
8357weston_compositor_load_color_manager(struct weston_compositor *compositor)
8358{
8359 struct weston_color_manager *
8360 (*cm_create)(struct weston_compositor *compositor);
8361
8362 if (compositor->color_manager) {
8363 weston_log("Error: Color manager '%s' is loaded, cannot load another.\n",
8364 compositor->color_manager->name);
8365 return -1;
8366 }
8367
8368 cm_create = weston_load_module("color-lcms.so", "weston_color_manager_create");
8369 if (!cm_create) {
8370 weston_log("Error: Could not load color-lcms.so.\n");
8371 return -1;
8372 }
8373
8374 compositor->color_manager = cm_create(compositor);
8375 if (!compositor->color_manager) {
8376 weston_log("Error: loading color-lcms.so failed.\n");
8377 return -1;
8378 }
8379
8380 return 0;
8381}
8382
Vasilis Liaskovitis486b4632018-10-10 16:14:55 +02008383/** Resolve an internal compositor error by disconnecting the client.
8384 *
8385 * This function is used in cases when the wl_buffer turns out
8386 * unusable and there is no fallback path.
8387 *
8388 * It is possible the fault is caused by a compositor bug, the underlying
8389 * graphics stack bug or normal behaviour, or perhaps a client mistake.
8390 * In any case, the options are to either composite garbage or nothing,
8391 * or disconnect the client. This is a helper function for the latter.
8392 *
8393 * The error is sent as an INVALID_OBJECT error on the client's wl_display.
8394 *
8395 * \param buffer The weston buffer that is unusable.
8396 * \param msg A custom error message attached to the protocol error.
8397 */
8398WL_EXPORT void
8399weston_buffer_send_server_error(struct weston_buffer *buffer,
8400 const char *msg)
8401{
8402 struct wl_client *client;
8403 struct wl_resource *display_resource;
8404 uint32_t id;
8405
8406 assert(buffer->resource);
8407 id = wl_resource_get_id(buffer->resource);
8408 client = wl_resource_get_client(buffer->resource);
8409 display_resource = wl_client_get_object(client, 1);
8410
8411 assert(display_resource);
8412 wl_resource_post_error(display_resource,
8413 WL_DISPLAY_ERROR_INVALID_OBJECT,
8414 "server error with "
8415 "wl_buffer@%u: %s", id, msg);
8416}
Ankit Nautiyal93dde242019-07-08 11:46:42 +05308417
8418WL_EXPORT void
8419weston_output_disable_planes_incr(struct weston_output *output)
8420{
8421 output->disable_planes++;
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05308422 /*
8423 * If disable_planes changes from 0 to non-zero, it means some type of
8424 * recording of content has started, and therefore protection level of
8425 * the protected surfaces must be updated to avoid the recording of
8426 * the protected content.
8427 */
8428 if (output->disable_planes == 1)
8429 weston_schedule_surface_protection_update(output->compositor);
Ankit Nautiyal93dde242019-07-08 11:46:42 +05308430}
8431
8432WL_EXPORT void
8433weston_output_disable_planes_decr(struct weston_output *output)
8434{
8435 output->disable_planes--;
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05308436 /*
8437 * If disable_planes changes from non-zero to 0, it means no content
8438 * recording is going on any more, and the protected and surfaces can be
8439 * shown without any apprehensions about content being recorded.
8440 */
8441 if (output->disable_planes == 0)
8442 weston_schedule_surface_protection_update(output->compositor);
8443
Ankit Nautiyal93dde242019-07-08 11:46:42 +05308444}