blob: d747e31dd7cfe9ef585a013f50df7fcc0046b794 [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
Jason Ekstranda7af7042013-10-12 22:38:11 -05002316 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
2317 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002318
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002319 wl_list_for_each_safe(pnode, pntmp,
2320 &surface->paint_node_list, surface_link) {
2321 weston_paint_node_destroy(pnode);
2322 }
2323
Jason Ekstrand7b982072014-05-20 14:33:03 -05002324 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002325
Pekka Paalanende685b82012-12-04 15:58:12 +02002326 weston_buffer_reference(&surface->buffer_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002327 weston_buffer_release_reference(&surface->buffer_release_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04002328
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02002329 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002330 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002331 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02002332
Vlad Zahorodniic2b97472021-07-29 11:16:51 +03002333 wl_resource_for_each_safe(cb, next, &surface->frame_callback_list)
2334 wl_resource_destroy(cb);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002335
Pekka Paalanen133e4392014-09-23 22:08:46 -04002336 weston_presentation_feedback_discard_list(&surface->feedback_list);
2337
Jonas Ådahld3414f22016-07-22 17:56:31 +08002338 wl_list_for_each_safe(constraint, next_constraint,
2339 &surface->pointer_constraints,
2340 link)
2341 weston_pointer_constraint_destroy(constraint);
2342
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03002343 fd_clear(&surface->acquire_fence_fd);
2344
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04002345 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05002346}
2347
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002348static void
2349destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08002350{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002351 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08002352
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03002353 assert(surface);
2354
Giulio Camuffo0d379742013-11-15 22:06:15 +01002355 /* Set the resource to NULL, since we don't want to leave a
2356 * dangling pointer if the surface was refcounted and survives
2357 * the weston_surface_destroy() call. */
2358 surface->resource = NULL;
Pekka Paalanen4826f872016-04-22 14:14:38 +03002359
2360 if (surface->viewport_resource)
2361 wl_resource_set_user_data(surface->viewport_resource, NULL);
2362
Alexandros Frantzis27d7c392018-10-19 12:14:11 +03002363 if (surface->synchronization_resource) {
2364 wl_resource_set_user_data(surface->synchronization_resource,
2365 NULL);
2366 }
2367
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002368 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08002369}
2370
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002371static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002372weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
2373{
2374 struct weston_buffer *buffer =
2375 container_of(listener, struct weston_buffer, destroy_listener);
2376
2377 wl_signal_emit(&buffer->destroy_signal, buffer);
2378 free(buffer);
2379}
2380
Giulio Camuffoe058cd12013-12-12 14:14:29 +01002381WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002382weston_buffer_from_resource(struct wl_resource *resource)
2383{
2384 struct weston_buffer *buffer;
2385 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002386
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002387 listener = wl_resource_get_destroy_listener(resource,
2388 weston_buffer_destroy_handler);
2389
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002390 if (listener)
2391 return container_of(listener, struct weston_buffer,
2392 destroy_listener);
2393
2394 buffer = zalloc(sizeof *buffer);
2395 if (buffer == NULL)
2396 return NULL;
2397
2398 buffer->resource = resource;
2399 wl_signal_init(&buffer->destroy_signal);
2400 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04002401 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002402 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002403
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002404 return buffer;
2405}
2406
2407static void
Pekka Paalanende685b82012-12-04 15:58:12 +02002408weston_buffer_reference_handle_destroy(struct wl_listener *listener,
2409 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002410{
Pekka Paalanende685b82012-12-04 15:58:12 +02002411 struct weston_buffer_reference *ref =
2412 container_of(listener, struct weston_buffer_reference,
2413 destroy_listener);
2414
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002415 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02002416 ref->buffer = NULL;
2417}
2418
2419WL_EXPORT void
2420weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002421 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002422{
2423 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05002424 ref->buffer->busy_count--;
2425 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002426 assert(wl_resource_get_client(ref->buffer->resource));
Matt Hoosier3052bc72017-09-26 08:09:40 -05002427 wl_buffer_send_release(ref->buffer->resource);
Kristian Høgsberg20347802013-03-04 12:07:46 -05002428 }
Pekka Paalanende685b82012-12-04 15:58:12 +02002429 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002430 }
2431
Pekka Paalanende685b82012-12-04 15:58:12 +02002432 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002433 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002434 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02002435 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02002436 }
2437
Pekka Paalanende685b82012-12-04 15:58:12 +02002438 ref->buffer = buffer;
2439 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
2440}
2441
2442static void
Alexandros Frantzis67629672018-10-19 12:14:11 +03002443weston_buffer_release_reference_handle_destroy(struct wl_listener *listener,
2444 void *data)
2445{
2446 struct weston_buffer_release_reference *ref =
2447 container_of(listener, struct weston_buffer_release_reference,
2448 destroy_listener);
2449
2450 assert((struct wl_resource *)data == ref->buffer_release->resource);
2451 ref->buffer_release = NULL;
2452}
2453
2454static void
2455weston_buffer_release_destroy(struct weston_buffer_release *buffer_release)
2456{
2457 struct wl_resource *resource = buffer_release->resource;
2458 int release_fence_fd = buffer_release->fence_fd;
2459
2460 if (release_fence_fd >= 0) {
2461 zwp_linux_buffer_release_v1_send_fenced_release(
2462 resource, release_fence_fd);
2463 } else {
2464 zwp_linux_buffer_release_v1_send_immediate_release(
2465 resource);
2466 }
2467
2468 wl_resource_destroy(resource);
2469}
2470
2471WL_EXPORT void
2472weston_buffer_release_reference(struct weston_buffer_release_reference *ref,
2473 struct weston_buffer_release *buffer_release)
2474{
2475 if (buffer_release == ref->buffer_release)
2476 return;
2477
2478 if (ref->buffer_release) {
2479 ref->buffer_release->ref_count--;
2480 wl_list_remove(&ref->destroy_listener.link);
2481 if (ref->buffer_release->ref_count == 0)
2482 weston_buffer_release_destroy(ref->buffer_release);
2483 }
2484
2485 if (buffer_release) {
2486 buffer_release->ref_count++;
2487 wl_resource_add_destroy_listener(buffer_release->resource,
2488 &ref->destroy_listener);
2489 }
2490
2491 ref->buffer_release = buffer_release;
2492 ref->destroy_listener.notify =
2493 weston_buffer_release_reference_handle_destroy;
2494}
2495
2496WL_EXPORT void
2497weston_buffer_release_move(struct weston_buffer_release_reference *dest,
2498 struct weston_buffer_release_reference *src)
2499{
2500 weston_buffer_release_reference(dest, src->buffer_release);
2501 weston_buffer_release_reference(src, NULL);
2502}
2503
2504static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002505weston_surface_attach(struct weston_surface *surface,
2506 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002507{
2508 weston_buffer_reference(&surface->buffer_ref, buffer);
2509
Pekka Paalanena6421c42012-12-04 15:58:10 +02002510 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002511 if (weston_surface_is_mapped(surface))
2512 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002513 }
2514
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002515 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02002516
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002517 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002518 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002519}
2520
Marius Vlad9fdda7f2019-06-11 16:08:55 +03002521/** weston_compositor_damage_all
2522 * \ingroup compositor
2523 */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002524WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002525weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002526{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002527 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002528
2529 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002530 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002531}
2532
Marius Vlad55d87362019-06-11 01:15:35 +03002533/**
2534 * \ingroup output
2535 */
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05002536WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002537weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002538{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002539 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002540
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002541 pixman_region32_union(&compositor->primary_plane.damage,
2542 &compositor->primary_plane.damage,
2543 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002544 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002545}
2546
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04002547static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002548surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002549{
Pekka Paalanende685b82012-12-04 15:58:12 +02002550 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002551 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002552 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002553
Marius Vlad2a1b7862019-09-05 13:12:18 +03002554 if (pixman_region32_not_empty(&surface->damage))
Marius Vlad3203ff62019-09-05 14:56:12 +03002555 TL_POINT(surface->compositor, "core_flush_damage", TLP_SURFACE(surface),
Pekka Paalanenb5026542014-11-12 15:09:24 +02002556 TLP_OUTPUT(surface->output), TLP_END);
2557
Jason Ekstrandef540082014-06-26 10:37:36 -07002558 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002559}
2560
2561static void
2562view_accumulate_damage(struct weston_view *view,
2563 pixman_region32_t *opaque)
2564{
2565 pixman_region32_t damage;
2566
2567 pixman_region32_init(&damage);
2568 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002569 pixman_box32_t *extents;
2570
Jason Ekstranda7af7042013-10-12 22:38:11 -05002571 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002572 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002573 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002574 pixman_region32_copy(&damage, &view->surface->damage);
2575 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002576 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002577 }
2578
Pekka Paalanen380adf52015-02-16 14:39:11 +02002579 pixman_region32_intersect(&damage, &damage,
2580 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002581 pixman_region32_subtract(&damage, &damage, opaque);
2582 pixman_region32_union(&view->plane->damage,
2583 &view->plane->damage, &damage);
2584 pixman_region32_fini(&damage);
2585 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002586 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002587}
2588
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002589static void
Michael Olbrichc5ea4952020-08-11 12:42:35 +02002590output_accumulate_damage(struct weston_output *output)
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002591{
Michael Olbrichc5ea4952020-08-11 12:42:35 +02002592 struct weston_compositor *ec = output->compositor;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002593 struct weston_plane *plane;
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002594 struct weston_paint_node *pnode;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002595 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002596
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002597 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002598
2599 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002600 pixman_region32_copy(&plane->clip, &clip);
2601
2602 pixman_region32_init(&opaque);
2603
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002604 wl_list_for_each(pnode, &output->paint_node_z_order_list,
2605 z_order_link) {
2606 if (pnode->view->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002607 continue;
2608
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002609 view_accumulate_damage(pnode->view, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002610 }
2611
2612 pixman_region32_union(&clip, &clip, &opaque);
2613 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002614 }
2615
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002616 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002617
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002618 wl_list_for_each(pnode, &output->paint_node_z_order_list,
2619 z_order_link) {
2620 pnode->surface->touched = false;
2621 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002622
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002623 wl_list_for_each(pnode, &output->paint_node_z_order_list,
2624 z_order_link) {
Michael Olbrichc5ea4952020-08-11 12:42:35 +02002625 /* Ignore views not visible on the current output */
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002626 /* TODO: turn this into assert once z_order_list is pruned. */
2627 if (!(pnode->view->output_mask & (1u << output->id)))
Michael Olbrichc5ea4952020-08-11 12:42:35 +02002628 continue;
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002629 if (pnode->surface->touched)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002630 continue;
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002631 pnode->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002632
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002633 surface_flush_damage(pnode->surface);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002634
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002635 /* Both the renderer and the backend have seen the buffer
2636 * by now. If renderer needs the buffer, it has its own
2637 * reference set. If the backend wants to keep the buffer
2638 * around for migrating the surface into a non-primary plane
2639 * later, keep_buffer is true. Otherwise, drop the core
2640 * reference now, and allow early buffer release. This enables
2641 * clients to use single-buffering.
2642 */
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002643 if (!pnode->surface->keep_buffer) {
2644 weston_buffer_reference(&pnode->surface->buffer_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002645 weston_buffer_release_reference(
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002646 &pnode->surface->buffer_release_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002647 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002648 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002649}
2650
2651static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002652surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002653{
2654 struct weston_subsurface *sub;
2655
Pekka Paalanene67858b2013-04-25 13:57:42 +03002656 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002657 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002658 continue;
2659
Jason Ekstranda7af7042013-10-12 22:38:11 -05002660 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2661 wl_list_init(&sub->surface->views);
2662
2663 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002664 }
2665}
2666
2667static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002668surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002669{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002670 struct weston_subsurface *sub;
2671 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002672
Jason Ekstranda7af7042013-10-12 22:38:11 -05002673 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2674 if (sub->surface == surface)
2675 continue;
2676
George Kiagiadakised04d382014-06-13 18:10:26 +02002677 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2678 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002679 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002680 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002681
2682 surface_free_unused_subsurface_views(sub->surface);
2683 }
2684}
2685
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002686static struct weston_paint_node *
2687view_ensure_paint_node(struct weston_view *view, struct weston_output *output)
2688{
2689 struct weston_paint_node *pnode;
2690
2691 if (!output)
2692 return NULL;
2693
2694 pnode = weston_view_find_paint_node(view, output);
2695 if (pnode)
2696 return pnode;
2697
2698 return weston_paint_node_create(view->surface, view, output);
2699}
2700
Jason Ekstranda7af7042013-10-12 22:38:11 -05002701static void
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002702add_to_z_order_list(struct weston_output *output,
2703 struct weston_paint_node *pnode)
2704{
2705 if (!pnode)
2706 return;
2707
2708 wl_list_remove(&pnode->z_order_link);
2709 wl_list_insert(output->paint_node_z_order_list.prev,
2710 &pnode->z_order_link);
Pekka Paalanen90a5ffa2021-02-25 12:03:28 +02002711
2712 /*
2713 * Building weston_output::paint_node_z_order_list ensures all
2714 * necessary color transform objects are installed.
2715 */
2716 weston_paint_node_ensure_color_transform(pnode);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002717}
2718
2719static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002720view_list_add_subsurface_view(struct weston_compositor *compositor,
2721 struct weston_subsurface *sub,
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002722 struct weston_view *parent,
2723 struct weston_output *output)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002724{
2725 struct weston_subsurface *child;
2726 struct weston_view *view = NULL, *iv;
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002727 struct weston_paint_node *pnode;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002728
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002729 if (!weston_surface_is_mapped(sub->surface))
2730 return;
2731
Jason Ekstranda7af7042013-10-12 22:38:11 -05002732 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2733 if (iv->geometry.parent == parent) {
2734 view = iv;
2735 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002736 }
2737 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002738
2739 if (view) {
2740 /* Put it back in the surface's list of views */
2741 wl_list_remove(&view->surface_link);
2742 wl_list_insert(&sub->surface->views, &view->surface_link);
2743 } else {
2744 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002745 weston_view_set_position(view,
2746 sub->position.x,
2747 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002748 weston_view_set_transform_parent(view, parent);
2749 }
2750
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002751 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002752 weston_view_update_transform(view);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002753 view->is_mapped = true;
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002754 pnode = view_ensure_paint_node(view, output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002755
Pekka Paalanenb188e912013-11-19 14:03:35 +02002756 if (wl_list_empty(&sub->surface->subsurface_list)) {
2757 wl_list_insert(compositor->view_list.prev, &view->link);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002758 add_to_z_order_list(output, pnode);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002759 return;
2760 }
2761
2762 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002763 if (child->surface == sub->surface) {
Pekka Paalanenb188e912013-11-19 14:03:35 +02002764 wl_list_insert(compositor->view_list.prev, &view->link);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002765 add_to_z_order_list(output, pnode);
2766 } else {
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002767 view_list_add_subsurface_view(compositor, child, view, output);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002768 }
Pekka Paalanenb188e912013-11-19 14:03:35 +02002769 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002770}
2771
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002772/* This recursively adds the sub-surfaces for a view, relying on the
2773 * sub-surface order. Thus, if a client restacks the sub-surfaces, that
2774 * change first happens to the sub-surface list, and then automatically
2775 * propagates here. See weston_surface_damage_subsurfaces() for how the
2776 * sub-surfaces receive damage when the client changes the state.
2777 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002778static void
2779view_list_add(struct weston_compositor *compositor,
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002780 struct weston_view *view,
2781 struct weston_output *output)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002782{
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002783 struct weston_paint_node *pnode;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002784 struct weston_subsurface *sub;
2785
2786 weston_view_update_transform(view);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002787 pnode = view_ensure_paint_node(view, output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002788
Pekka Paalanenb188e912013-11-19 14:03:35 +02002789 if (wl_list_empty(&view->surface->subsurface_list)) {
2790 wl_list_insert(compositor->view_list.prev, &view->link);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002791 add_to_z_order_list(output, pnode);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002792 return;
2793 }
2794
2795 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002796 if (sub->surface == view->surface) {
Pekka Paalanenb188e912013-11-19 14:03:35 +02002797 wl_list_insert(compositor->view_list.prev, &view->link);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002798 add_to_z_order_list(output, pnode);
2799 } else {
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002800 view_list_add_subsurface_view(compositor, sub, view, output);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002801 }
Pekka Paalanenb188e912013-11-19 14:03:35 +02002802 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002803}
2804
2805static void
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002806weston_compositor_build_view_list(struct weston_compositor *compositor,
2807 struct weston_output *output)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002808{
Loïc Yhuel267b16e2019-09-17 20:14:56 +02002809 struct weston_view *view, *tmp;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002810 struct weston_layer *layer;
2811
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002812 if (output) {
2813 wl_list_remove(&output->paint_node_z_order_list);
2814 wl_list_init(&output->paint_node_z_order_list);
2815 }
2816
Jason Ekstranda7af7042013-10-12 22:38:11 -05002817 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002818 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002819 surface_stash_subsurface_views(view->surface);
2820
Loïc Yhuel267b16e2019-09-17 20:14:56 +02002821 wl_list_for_each_safe(view, tmp, &compositor->view_list, link)
2822 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002823 wl_list_init(&compositor->view_list);
Loïc Yhuel267b16e2019-09-17 20:14:56 +02002824
Jason Ekstranda7af7042013-10-12 22:38:11 -05002825 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002826 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002827 view_list_add(compositor, view, output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002828 }
2829 }
2830
2831 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002832 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002833 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002834}
2835
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002836static void
2837weston_output_take_feedback_list(struct weston_output *output,
2838 struct weston_surface *surface)
2839{
2840 struct weston_view *view;
2841 struct weston_presentation_feedback *feedback;
2842 uint32_t flags = 0xffffffff;
2843
2844 if (wl_list_empty(&surface->feedback_list))
2845 return;
2846
2847 /* All views must have the flag for the flag to survive. */
2848 wl_list_for_each(view, &surface->views, surface_link) {
2849 /* ignore views that are not on this output at all */
2850 if (view->output_mask & (1u << output->id))
2851 flags &= view->psf_flags;
2852 }
2853
2854 wl_list_for_each(feedback, &surface->feedback_list, link)
2855 feedback->psf_flags = flags;
2856
2857 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2858 wl_list_init(&surface->feedback_list);
2859}
2860
David Herrmann1edf44c2013-10-22 17:11:26 +02002861static int
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002862weston_output_repaint(struct weston_output *output, void *repaint_data)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002863{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002864 struct weston_compositor *ec = output->compositor;
Pekka Paalanen6d6b3672021-05-03 14:06:55 +03002865 struct weston_paint_node *pnode;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002866 struct weston_animation *animation, *next;
Vlad Zahorodniic2b97472021-07-29 11:16:51 +03002867 struct wl_resource *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002868 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002869 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002870 int r;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002871 uint32_t frame_time_msec;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05302872 enum weston_hdcp_protection highest_requested = WESTON_HDCP_DISABLE;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002873
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002874 if (output->destroying)
2875 return 0;
2876
Marius Vlad3203ff62019-09-05 14:56:12 +03002877 TL_POINT(ec, "core_repaint_begin", TLP_OUTPUT(output), TLP_END);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002878
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002879 /* Rebuild the surface list and update surface transforms up front. */
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002880 weston_compositor_build_view_list(ec, output);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002881
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05302882 /* Find the highest protection desired for an output */
Pekka Paalanen6d6b3672021-05-03 14:06:55 +03002883 wl_list_for_each(pnode, &output->paint_node_z_order_list,
2884 z_order_link) {
2885 /* TODO: turn this into assert once z_order_list is pruned. */
2886 if ((pnode->surface->output_mask & (1u << output->id)) == 0)
2887 continue;
2888
2889 /*
2890 * The desired_protection of the output should be the
2891 * maximum of the desired_protection of the surfaces,
2892 * that are displayed on that output, to avoid
2893 * reducing the protection for existing surfaces.
2894 */
2895 if (pnode->surface->desired_protection > highest_requested)
2896 highest_requested = pnode->surface->desired_protection;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05302897 }
2898
2899 output->desired_protection = highest_requested;
2900
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002901 if (output->assign_planes && !output->disable_planes) {
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002902 output->assign_planes(output, repaint_data);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002903 } else {
Pekka Paalanen6d6b3672021-05-03 14:06:55 +03002904 wl_list_for_each(pnode, &output->paint_node_z_order_list,
2905 z_order_link) {
2906 weston_view_move_to_plane(pnode->view, &ec->primary_plane);
2907 pnode->view->psf_flags = 0;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002908 }
2909 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002910
Pekka Paalanene67858b2013-04-25 13:57:42 +03002911 wl_list_init(&frame_callback_list);
Pekka Paalanen6d6b3672021-05-03 14:06:55 +03002912 wl_list_for_each(pnode, &output->paint_node_z_order_list,
2913 z_order_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002914 /* Note: This operation is safe to do multiple times on the
2915 * same surface.
2916 */
Pekka Paalanen6d6b3672021-05-03 14:06:55 +03002917 if (pnode->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002918 wl_list_insert_list(&frame_callback_list,
Pekka Paalanen6d6b3672021-05-03 14:06:55 +03002919 &pnode->surface->frame_callback_list);
2920 wl_list_init(&pnode->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002921
Pekka Paalanen6d6b3672021-05-03 14:06:55 +03002922 weston_output_take_feedback_list(output, pnode->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002923 }
2924 }
2925
Michael Olbrichc5ea4952020-08-11 12:42:35 +02002926 output_accumulate_damage(output);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002927
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002928 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002929 pixman_region32_intersect(&output_damage,
2930 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002931 pixman_region32_subtract(&output_damage,
2932 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002933
Scott Moreauccbf29d2012-02-22 14:21:41 -07002934 if (output->dirty)
2935 weston_output_update_matrix(output);
2936
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002937 r = output->repaint(output, &output_damage, repaint_data);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002938
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002939 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002940
Daniel Stone09a97e22017-03-01 11:34:06 +00002941 output->repaint_needed = false;
Daniel Stone05df8c12017-03-03 16:59:42 +00002942 if (r == 0)
2943 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002944
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002945 weston_compositor_repick(ec);
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002946
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002947 frame_time_msec = timespec_to_msec(&output->frame_time);
2948
Vlad Zahorodniic2b97472021-07-29 11:16:51 +03002949 wl_resource_for_each_safe(cb, cnext, &frame_callback_list) {
2950 wl_callback_send_done(cb, frame_time_msec);
2951 wl_resource_destroy(cb);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002952 }
2953
Scott Moreaud64cf212012-06-08 19:40:54 -06002954 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002955 animation->frame_counter++;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002956 animation->frame(animation, output, &output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002957 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002958
Marius Vlad3203ff62019-09-05 14:56:12 +03002959 TL_POINT(ec, "core_repaint_posted", TLP_OUTPUT(output), TLP_END);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002960
David Herrmann1edf44c2013-10-22 17:11:26 +02002961 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002962}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002963
Pekka Paalanen82919792014-05-21 13:51:49 +03002964static void
2965weston_output_schedule_repaint_reset(struct weston_output *output)
2966{
Daniel Stone05df8c12017-03-03 16:59:42 +00002967 output->repaint_status = REPAINT_NOT_SCHEDULED;
Marius Vlad3203ff62019-09-05 14:56:12 +03002968 TL_POINT(output->compositor, "core_repaint_exit_loop",
2969 TLP_OUTPUT(output), TLP_END);
Pekka Paalanen82919792014-05-21 13:51:49 +03002970}
2971
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002972static int
2973weston_output_maybe_repaint(struct weston_output *output, struct timespec *now,
2974 void *repaint_data)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002975{
Pekka Paalanen0513a952014-05-21 16:17:27 +03002976 struct weston_compositor *compositor = output->compositor;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002977 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002978 int64_t msec_to_repaint;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002979
Daniel Stone6847b852017-03-01 11:34:08 +00002980 /* We're not ready yet; come back to make a decision later. */
2981 if (output->repaint_status != REPAINT_SCHEDULED)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002982 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002983
2984 msec_to_repaint = timespec_sub_to_msec(&output->next_repaint, now);
2985 if (msec_to_repaint > 1)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002986 return ret;
Daniel Stone05df8c12017-03-03 16:59:42 +00002987
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002988 /* If we're sleeping, drop the repaint machinery entirely; we will
2989 * explicitly repaint all outputs when we come back. */
2990 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2991 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
2992 goto err;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002993
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002994 /* We don't actually need to repaint this output; drop it from
2995 * repaint until something causes damage. */
2996 if (!output->repaint_needed)
2997 goto err;
2998
2999 /* If repaint fails, we aren't going to get weston_output_finish_frame
3000 * to trigger a new repaint, so drop it from repaint and hope
Daniel Stone6847b852017-03-01 11:34:08 +00003001 * something schedules a successful repaint later. As repainting may
3002 * take some time, re-read our clock as a courtesy to the next
3003 * output. */
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003004 ret = weston_output_repaint(output, repaint_data);
Daniel Stone6847b852017-03-01 11:34:08 +00003005 weston_compositor_read_presentation_clock(compositor, now);
Daniel Stonecd1a1c32017-01-16 15:38:54 +00003006 if (ret != 0)
3007 goto err;
3008
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09003009 output->repainted = true;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003010 return ret;
Daniel Stonecd1a1c32017-01-16 15:38:54 +00003011
3012err:
Pekka Paalanen0513a952014-05-21 16:17:27 +03003013 weston_output_schedule_repaint_reset(output);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003014 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00003015}
3016
3017static void
3018output_repaint_timer_arm(struct weston_compositor *compositor)
3019{
3020 struct weston_output *output;
3021 bool any_should_repaint = false;
3022 struct timespec now;
Sergi Granellb4c08862017-03-18 13:01:15 +01003023 int64_t msec_to_next = INT64_MAX;
Daniel Stone6847b852017-03-01 11:34:08 +00003024
3025 weston_compositor_read_presentation_clock(compositor, &now);
3026
3027 wl_list_for_each(output, &compositor->output_list, link) {
3028 int64_t msec_to_this;
3029
3030 if (output->repaint_status != REPAINT_SCHEDULED)
3031 continue;
3032
3033 msec_to_this = timespec_sub_to_msec(&output->next_repaint,
3034 &now);
3035 if (!any_should_repaint || msec_to_this < msec_to_next)
3036 msec_to_next = msec_to_this;
3037
3038 any_should_repaint = true;
3039 }
3040
3041 if (!any_should_repaint)
3042 return;
3043
3044 /* Even if we should repaint immediately, add the minimum 1 ms delay.
3045 * This is a workaround to allow coalescing multiple output repaints
3046 * particularly from weston_output_finish_frame()
3047 * into the same call, which would not happen if we called
3048 * output_repaint_timer_handler() directly.
3049 */
3050 if (msec_to_next < 1)
3051 msec_to_next = 1;
3052
3053 wl_event_source_timer_update(compositor->repaint_timer, msec_to_next);
3054}
3055
3056static int
3057output_repaint_timer_handler(void *data)
3058{
3059 struct weston_compositor *compositor = data;
3060 struct weston_output *output;
3061 struct timespec now;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003062 void *repaint_data = NULL;
Emre Ucane479ed82018-03-20 15:29:40 +01003063 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00003064
3065 weston_compositor_read_presentation_clock(compositor, &now);
Pekka Paalanen1ed2cad2021-02-10 12:33:03 +02003066 compositor->last_repaint_start = now;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003067
3068 if (compositor->backend->repaint_begin)
3069 repaint_data = compositor->backend->repaint_begin(compositor);
3070
3071 wl_list_for_each(output, &compositor->output_list, link) {
3072 ret = weston_output_maybe_repaint(output, &now, repaint_data);
3073 if (ret)
3074 break;
3075 }
3076
3077 if (ret == 0) {
Tomohito Esaki09bfcd62018-06-05 10:37:05 +09003078 if (compositor->backend->repaint_flush)
Antonio Borneoc90fccc2019-06-30 15:51:10 +02003079 ret = compositor->backend->repaint_flush(compositor,
3080 repaint_data);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003081 } else {
Antonio Borneoc90fccc2019-06-30 15:51:10 +02003082 if (compositor->backend->repaint_cancel)
3083 compositor->backend->repaint_cancel(compositor,
3084 repaint_data);
3085 }
3086
3087 if (ret != 0) {
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09003088 wl_list_for_each(output, &compositor->output_list, link) {
3089 if (output->repainted)
3090 weston_output_schedule_repaint_reset(output);
3091 }
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003092 }
Daniel Stone6847b852017-03-01 11:34:08 +00003093
Tomohito Esakiddaf95c2018-07-10 11:47:15 +09003094 wl_list_for_each(output, &compositor->output_list, link)
3095 output->repainted = false;
3096
Daniel Stone6847b852017-03-01 11:34:08 +00003097 output_repaint_timer_arm(compositor);
3098
Pekka Paalanen0513a952014-05-21 16:17:27 +03003099 return 0;
3100}
3101
Pekka Paalanen50aa3a72020-05-28 11:34:04 +03003102/** Convert a presentation timestamp to another clock domain
3103 *
3104 * \param compositor The compositor defines the presentation clock domain.
3105 * \param presentation_stamp The timestamp in presentation clock domain.
3106 * \param presentation_now Current time in presentation clock domain.
3107 * \param target_clock Defines the target clock domain.
3108 *
3109 * This approximation relies on presentation_stamp to be close to current time.
3110 * The further it is from current time and the bigger the speed difference
3111 * between the two clock domains, the bigger the conversion error.
3112 *
3113 * Conversion error due to system load is biased and unbounded.
3114 */
3115static struct timespec
3116convert_presentation_time_now(struct weston_compositor *compositor,
3117 const struct timespec *presentation_stamp,
3118 const struct timespec *presentation_now,
3119 clockid_t target_clock)
3120{
3121 struct timespec target_now = {};
3122 struct timespec target_stamp;
3123 int64_t delta_ns;
3124
3125 if (compositor->presentation_clock == target_clock)
3126 return *presentation_stamp;
3127
3128 clock_gettime(target_clock, &target_now);
3129 delta_ns = timespec_sub_to_nsec(presentation_stamp, presentation_now);
3130 timespec_add_nsec(&target_stamp, &target_now, delta_ns);
3131
3132 return target_stamp;
3133}
3134
Marius Vlad55d87362019-06-11 01:15:35 +03003135/**
3136 * \ingroup output
3137 */
Kristian Høgsbergef044142011-06-21 15:02:12 -04003138WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04003139weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02003140 const struct timespec *stamp,
3141 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04003142{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003143 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03003144 int32_t refresh_nsec;
3145 struct timespec now;
Pekka Paalanen50aa3a72020-05-28 11:34:04 +03003146 struct timespec vblank_monotonic;
Daniel Stone6847b852017-03-01 11:34:08 +00003147 int64_t msec_rel;
Pekka Paalanen133e4392014-09-23 22:08:46 -04003148
Daniel Stone05df8c12017-03-03 16:59:42 +00003149 assert(output->repaint_status == REPAINT_AWAITING_COMPLETION);
Pekka Paalanen98b4e202021-05-10 11:33:23 +03003150
3151 /*
3152 * If timestamp of latest vblank is given, it must always go forwards.
3153 * If not given, INVALID flag must be set.
3154 */
3155 if (stamp)
3156 assert(timespec_sub_to_nsec(stamp, &output->frame_time) >= 0);
3157 else
3158 assert(presented_flags & WP_PRESENTATION_FEEDBACK_INVALID);
Daniel Stone3615ce12017-03-01 11:34:05 +00003159
Daniel Stone6847b852017-03-01 11:34:08 +00003160 weston_compositor_read_presentation_clock(compositor, &now);
3161
Daniel Stone3615ce12017-03-01 11:34:05 +00003162 /* If we haven't been supplied any timestamp at all, we don't have a
3163 * timebase to work against, so any delay just wastes time. Push a
3164 * repaint as soon as possible so we can get on with it. */
Daniel Stone6847b852017-03-01 11:34:08 +00003165 if (!stamp) {
3166 output->next_repaint = now;
Daniel Stone3615ce12017-03-01 11:34:05 +00003167 goto out;
Daniel Stone6847b852017-03-01 11:34:08 +00003168 }
Daniel Stone3615ce12017-03-01 11:34:05 +00003169
Pekka Paalanen50aa3a72020-05-28 11:34:04 +03003170 vblank_monotonic = convert_presentation_time_now(compositor,
3171 stamp, &now,
3172 CLOCK_MONOTONIC);
Marius Vlad3203ff62019-09-05 14:56:12 +03003173 TL_POINT(compositor, "core_repaint_finished", TLP_OUTPUT(output),
Pekka Paalanen50aa3a72020-05-28 11:34:04 +03003174 TLP_VBLANK(&vblank_monotonic), TLP_END);
Marius Vladdf9278a2018-03-06 18:56:23 +02003175
Pekka Paalanend7894d02015-07-03 15:08:53 +03003176 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003177 weston_presentation_feedback_present_list(&output->feedback_list,
3178 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02003179 output->msc,
3180 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003181
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02003182 output->frame_time = *stamp;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003183
Daniel Stone6847b852017-03-01 11:34:08 +00003184 timespec_add_nsec(&output->next_repaint, stamp, refresh_nsec);
3185 timespec_add_msec(&output->next_repaint, &output->next_repaint,
3186 -compositor->repaint_msec);
3187 msec_rel = timespec_sub_to_msec(&output->next_repaint, &now);
Daniel Stone84aff5c2017-03-01 11:34:04 +00003188
3189 if (msec_rel < -1000 || msec_rel > 1000) {
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02003190 static bool warned;
3191
3192 if (!warned)
3193 weston_log("Warning: computed repaint delay is "
Daniel Stone6847b852017-03-01 11:34:08 +00003194 "insane: %lld msec\n", (long long) msec_rel);
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02003195 warned = true;
3196
Daniel Stone6847b852017-03-01 11:34:08 +00003197 output->next_repaint = now;
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02003198 }
3199
Mario Kleinerb7df04e2015-06-21 21:25:15 +02003200 /* Called from restart_repaint_loop and restart happens already after
3201 * the deadline given by repaint_msec? In that case we delay until
3202 * the deadline of the next frame, to give clients a more predictable
3203 * timing of the repaint cycle to lock on. */
Daniel Stoneeca5cca2017-02-28 21:53:51 +00003204 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID &&
3205 msec_rel < 0) {
3206 while (timespec_sub_to_nsec(&output->next_repaint, &now) < 0) {
3207 timespec_add_nsec(&output->next_repaint,
3208 &output->next_repaint,
3209 refresh_nsec);
3210 }
3211 }
Mario Kleinerb7df04e2015-06-21 21:25:15 +02003212
Daniel Stone3615ce12017-03-01 11:34:05 +00003213out:
Daniel Stone05df8c12017-03-03 16:59:42 +00003214 output->repaint_status = REPAINT_SCHEDULED;
Daniel Stone6847b852017-03-01 11:34:08 +00003215 output_repaint_timer_arm(compositor);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003216}
3217
nerdopolisd2a320d2021-08-23 21:29:42 -04003218
3219WL_EXPORT void
3220weston_output_repaint_failed(struct weston_output *output)
3221{
3222 weston_log("Clearing repaint status.\n");
3223 assert(output->repaint_status == REPAINT_AWAITING_COMPLETION);
3224 output->repaint_status = REPAINT_NOT_SCHEDULED;
3225}
3226
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003227static void
3228idle_repaint(void *data)
3229{
3230 struct weston_output *output = data;
Antonio Borneoc90fccc2019-06-30 15:51:10 +02003231 int ret;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003232
Daniel Stone05df8c12017-03-03 16:59:42 +00003233 assert(output->repaint_status == REPAINT_BEGIN_FROM_IDLE);
3234 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03003235 output->idle_repaint_source = NULL;
Antonio Borneoc90fccc2019-06-30 15:51:10 +02003236 ret = output->start_repaint_loop(output);
3237 if (ret != 0)
3238 weston_output_schedule_repaint_reset(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003239}
3240
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003241WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003242weston_layer_entry_insert(struct weston_layer_entry *list,
3243 struct weston_layer_entry *entry)
3244{
3245 wl_list_insert(&list->link, &entry->link);
3246 entry->layer = list->layer;
3247}
3248
3249WL_EXPORT void
3250weston_layer_entry_remove(struct weston_layer_entry *entry)
3251{
3252 wl_list_remove(&entry->link);
3253 wl_list_init(&entry->link);
3254 entry->layer = NULL;
3255}
3256
Quentin Glidic82681572016-12-17 13:40:51 +01003257
3258/** Initialize the weston_layer struct.
3259 *
3260 * \param compositor The compositor instance
3261 * \param layer The layer to initialize
3262 */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003263WL_EXPORT void
Quentin Glidic82681572016-12-17 13:40:51 +01003264weston_layer_init(struct weston_layer *layer,
3265 struct weston_compositor *compositor)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003266{
Quentin Glidic82681572016-12-17 13:40:51 +01003267 layer->compositor = compositor;
3268 wl_list_init(&layer->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003269 wl_list_init(&layer->view_list.link);
3270 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003271 weston_layer_set_mask_infinite(layer);
Quentin Glidic82681572016-12-17 13:40:51 +01003272}
3273
Pekka Paalanen87400372021-05-14 14:29:40 +03003274/** Finalize the weston_layer struct.
3275 *
3276 * \param layer The layer to finalize.
3277 */
3278WL_EXPORT void
3279weston_layer_fini(struct weston_layer *layer)
3280{
3281 wl_list_remove(&layer->link);
3282
3283 if (!wl_list_empty(&layer->view_list.link))
3284 weston_log("BUG: finalizing a layer with views still on it.\n");
3285
3286 wl_list_remove(&layer->view_list.link);
3287}
3288
Quentin Glidic82681572016-12-17 13:40:51 +01003289/** Sets the position of the layer in the layer list. The layer will be placed
3290 * below any layer with the same position value, if any.
3291 * This function is safe to call if the layer is already on the list, but the
3292 * layer may be moved below other layers at the same position, if any.
3293 *
3294 * \param layer The layer to modify
3295 * \param position The position the layer will be placed at
3296 */
3297WL_EXPORT void
3298weston_layer_set_position(struct weston_layer *layer,
3299 enum weston_layer_position position)
3300{
3301 struct weston_layer *below;
3302
3303 wl_list_remove(&layer->link);
3304
3305 /* layer_list is ordered from top to bottom, the last layer being the
3306 * background with the smallest position value */
3307
3308 layer->position = position;
3309 wl_list_for_each_reverse(below, &layer->compositor->layer_list, link) {
3310 if (below->position >= layer->position) {
3311 wl_list_insert(&below->link, &layer->link);
3312 return;
3313 }
3314 }
3315 wl_list_insert(&layer->compositor->layer_list, &layer->link);
3316}
3317
3318/** Hide a layer by taking it off the layer list.
3319 * This function is safe to call if the layer is not on the list.
3320 *
3321 * \param layer The layer to hide
3322 */
3323WL_EXPORT void
3324weston_layer_unset_position(struct weston_layer *layer)
3325{
3326 wl_list_remove(&layer->link);
3327 wl_list_init(&layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003328}
3329
3330WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003331weston_layer_set_mask(struct weston_layer *layer,
3332 int x, int y, int width, int height)
3333{
3334 struct weston_view *view;
3335
3336 layer->mask.x1 = x;
3337 layer->mask.x2 = x + width;
3338 layer->mask.y1 = y;
3339 layer->mask.y2 = y + height;
3340
3341 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
3342 weston_view_geometry_dirty(view);
3343 }
3344}
3345
3346WL_EXPORT void
3347weston_layer_set_mask_infinite(struct weston_layer *layer)
3348{
Adam Jackson3c3f3b12019-10-16 16:02:59 -04003349 struct weston_view *view;
3350
3351 layer->mask.x1 = INT32_MIN;
3352 layer->mask.x2 = INT32_MAX;
3353 layer->mask.y1 = INT32_MIN;
3354 layer->mask.y2 = INT32_MAX;
3355
3356 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
3357 weston_view_geometry_dirty(view);
3358 }
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003359}
3360
Daniel Stone3b775632018-07-20 08:38:25 +01003361WL_EXPORT bool
3362weston_layer_mask_is_infinite(struct weston_layer *layer)
3363{
3364 return layer->mask.x1 == INT32_MIN &&
3365 layer->mask.y1 == INT32_MIN &&
Adam Jackson3c3f3b12019-10-16 16:02:59 -04003366 layer->mask.x2 == INT32_MAX &&
3367 layer->mask.y2 == INT32_MAX;
Daniel Stone3b775632018-07-20 08:38:25 +01003368}
3369
Marius Vlad55d87362019-06-11 01:15:35 +03003370/**
3371 * \ingroup output
3372 */
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003373WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003374weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003375{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003376 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04003377 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01003378
Bryce Harrington08976ac2016-08-30 12:05:16 -07003379 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
3380 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003381 return;
3382
Pekka Paalanenb5026542014-11-12 15:09:24 +02003383 if (!output->repaint_needed)
Marius Vlad3203ff62019-09-05 14:56:12 +03003384 TL_POINT(compositor, "core_repaint_req", TLP_OUTPUT(output), TLP_END);
Pekka Paalanenb5026542014-11-12 15:09:24 +02003385
Kristian Høgsbergef044142011-06-21 15:02:12 -04003386 loop = wl_display_get_event_loop(compositor->wl_display);
Daniel Stone09a97e22017-03-01 11:34:06 +00003387 output->repaint_needed = true;
Daniel Stone05df8c12017-03-03 16:59:42 +00003388
3389 /* If we already have a repaint scheduled for our idle handler,
3390 * no need to set it again. If the repaint has been called but
3391 * not finished, then weston_output_finish_frame() will notice
3392 * that a repaint is needed and schedule one. */
3393 if (output->repaint_status != REPAINT_NOT_SCHEDULED)
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003394 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01003395
Daniel Stone05df8c12017-03-03 16:59:42 +00003396 output->repaint_status = REPAINT_BEGIN_FROM_IDLE;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03003397 assert(!output->idle_repaint_source);
3398 output->idle_repaint_source = wl_event_loop_add_idle(loop, idle_repaint,
3399 output);
Marius Vlad3203ff62019-09-05 14:56:12 +03003400 TL_POINT(compositor, "core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003401}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05003402
Marius Vlad9fdda7f2019-06-11 16:08:55 +03003403/** weston_compositor_schedule_repaint
3404 * \ingroup compositor
3405 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003406WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003407weston_compositor_schedule_repaint(struct weston_compositor *compositor)
3408{
3409 struct weston_output *output;
3410
3411 wl_list_for_each(output, &compositor->output_list, link)
3412 weston_output_schedule_repaint(output);
3413}
3414
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003415static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003416surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003417{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003418 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003419}
3420
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003421static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003422surface_attach(struct wl_client *client,
3423 struct wl_resource *resource,
3424 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
3425{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003426 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05003427 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003428
Kristian Høgsbergab19f932013-08-20 11:30:54 -07003429 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05003430 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07003431 if (buffer == NULL) {
3432 wl_client_post_no_memory(client);
3433 return;
3434 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07003435 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04003436
Pekka Paalanende685b82012-12-04 15:58:12 +02003437 /* Attach, attach, without commit in between does not send
3438 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003439 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003440
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003441 surface->pending.sx = sx;
3442 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01003443 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003444}
3445
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003446static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003447surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003448 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003449 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05003450{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003451 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003452
Derek Foreman57e92ed2015-11-17 14:11:35 -06003453 if (width <= 0 || height <= 0)
3454 return;
3455
Derek Foreman152254b2015-11-26 14:17:48 -06003456 pixman_region32_union_rect(&surface->pending.damage_surface,
3457 &surface->pending.damage_surface,
3458 x, y, width, height);
3459}
3460
3461static void
3462surface_damage_buffer(struct wl_client *client,
3463 struct wl_resource *resource,
3464 int32_t x, int32_t y, int32_t width, int32_t height)
3465{
3466 struct weston_surface *surface = wl_resource_get_user_data(resource);
3467
3468 if (width <= 0 || height <= 0)
3469 return;
3470
3471 pixman_region32_union_rect(&surface->pending.damage_buffer,
3472 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04003473 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003474}
3475
Kristian Høgsberg33418202011-08-16 23:01:28 -04003476static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003477destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04003478{
Vlad Zahorodniic2b97472021-07-29 11:16:51 +03003479 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsberg33418202011-08-16 23:01:28 -04003480}
3481
3482static void
3483surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003484 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04003485{
Vlad Zahorodniic2b97472021-07-29 11:16:51 +03003486 struct wl_resource *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003487 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003488
Vlad Zahorodniic2b97472021-07-29 11:16:51 +03003489 cb = wl_resource_create(client, &wl_callback_interface, 1, callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003490 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003491 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003492 return;
3493 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03003494
Vlad Zahorodniic2b97472021-07-29 11:16:51 +03003495 wl_resource_set_implementation(cb, NULL, NULL,
Jason Ekstranda85118c2013-06-27 20:17:02 -05003496 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003497
Vlad Zahorodniic2b97472021-07-29 11:16:51 +03003498 wl_list_insert(surface->pending.frame_callback_list.prev,
3499 wl_resource_get_link(cb));
Kristian Høgsberg33418202011-08-16 23:01:28 -04003500}
3501
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003502static void
3503surface_set_opaque_region(struct wl_client *client,
3504 struct wl_resource *resource,
3505 struct wl_resource *region_resource)
3506{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003507 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003508 struct weston_region *region;
3509
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003510 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003511 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003512 pixman_region32_copy(&surface->pending.opaque,
3513 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003514 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07003515 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003516 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003517}
3518
3519static void
3520surface_set_input_region(struct wl_client *client,
3521 struct wl_resource *resource,
3522 struct wl_resource *region_resource)
3523{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003524 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003525 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003526
3527 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003528 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003529 pixman_region32_copy(&surface->pending.input,
3530 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003531 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003532 pixman_region32_fini(&surface->pending.input);
3533 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003534 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003535}
3536
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003537/* Cause damage to this sub-surface and all its children.
3538 *
3539 * This is useful when there are state changes that need an implicit
3540 * damage, e.g. a z-order change.
3541 */
3542static void
3543weston_surface_damage_subsurfaces(struct weston_subsurface *sub)
3544{
3545 struct weston_subsurface *child;
3546
3547 weston_surface_damage(sub->surface);
3548 sub->reordered = false;
3549
3550 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link)
3551 if (child != sub)
3552 weston_surface_damage_subsurfaces(child);
3553}
3554
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003555static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003556weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003557{
Pekka Paalanene67858b2013-04-25 13:57:42 +03003558 struct weston_subsurface *sub;
3559
3560 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
3561 parent_link_pending) {
3562 wl_list_remove(&sub->parent_link);
3563 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003564
3565 if (sub->reordered)
3566 weston_surface_damage_subsurfaces(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003567 }
3568}
3569
3570static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03003571weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05003572 struct weston_matrix *matrix)
3573{
Pekka Paalanen04baea52016-04-26 15:50:58 +03003574 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05003575 double src_width, src_height, dest_width, dest_height;
3576
3577 weston_matrix_init(matrix);
3578
3579 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
3580 src_width = surface->width_from_buffer;
3581 src_height = surface->height_from_buffer;
3582 } else {
3583 src_width = wl_fixed_to_double(vp->buffer.src_width);
3584 src_height = wl_fixed_to_double(vp->buffer.src_height);
3585 }
3586
3587 if (vp->surface.width == -1) {
3588 dest_width = src_width;
3589 dest_height = src_height;
3590 } else {
3591 dest_width = vp->surface.width;
3592 dest_height = vp->surface.height;
3593 }
3594
3595 if (src_width != dest_width || src_height != dest_height)
3596 weston_matrix_scale(matrix,
3597 src_width / dest_width,
3598 src_height / dest_height, 1);
3599
3600 if (vp->buffer.src_width != wl_fixed_from_int(-1))
3601 weston_matrix_translate(matrix,
3602 wl_fixed_to_double(vp->buffer.src_x),
3603 wl_fixed_to_double(vp->buffer.src_y),
3604 0);
3605
3606 switch (vp->buffer.transform) {
3607 case WL_OUTPUT_TRANSFORM_FLIPPED:
3608 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3609 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3610 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3611 weston_matrix_scale(matrix, -1, 1, 1);
3612 weston_matrix_translate(matrix,
3613 surface->width_from_buffer, 0, 0);
3614 break;
3615 }
3616
3617 switch (vp->buffer.transform) {
3618 default:
3619 case WL_OUTPUT_TRANSFORM_NORMAL:
3620 case WL_OUTPUT_TRANSFORM_FLIPPED:
3621 break;
3622 case WL_OUTPUT_TRANSFORM_90:
3623 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +02003624 weston_matrix_rotate_xy(matrix, 0, -1);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003625 weston_matrix_translate(matrix,
Pekka Paalanen8060d822020-02-06 15:27:54 +02003626 0, surface->width_from_buffer, 0);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003627 break;
3628 case WL_OUTPUT_TRANSFORM_180:
3629 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3630 weston_matrix_rotate_xy(matrix, -1, 0);
3631 weston_matrix_translate(matrix,
3632 surface->width_from_buffer,
3633 surface->height_from_buffer, 0);
3634 break;
3635 case WL_OUTPUT_TRANSFORM_270:
3636 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +02003637 weston_matrix_rotate_xy(matrix, 0, 1);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003638 weston_matrix_translate(matrix,
Pekka Paalanen8060d822020-02-06 15:27:54 +02003639 surface->height_from_buffer, 0, 0);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003640 break;
3641 }
3642
3643 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
3644}
3645
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003646/**
3647 * Compute a + b > c while being safe to overflows.
3648 */
3649static bool
3650fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
3651{
3652 return (int64_t)a + (int64_t)b > (int64_t)c;
3653}
3654
3655static bool
3656weston_surface_is_pending_viewport_source_valid(
3657 const struct weston_surface *surface)
3658{
3659 const struct weston_surface_state *pend = &surface->pending;
3660 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
3661 int width_from_buffer = 0;
3662 int height_from_buffer = 0;
3663 wl_fixed_t w;
3664 wl_fixed_t h;
3665
3666 /* If viewport source rect is not set, it is always ok. */
3667 if (vp->buffer.src_width == wl_fixed_from_int(-1))
3668 return true;
3669
3670 if (pend->newly_attached) {
3671 if (pend->buffer) {
3672 convert_size_by_transform_scale(&width_from_buffer,
3673 &height_from_buffer,
3674 pend->buffer->width,
3675 pend->buffer->height,
3676 vp->buffer.transform,
3677 vp->buffer.scale);
3678 } else {
3679 /* No buffer: viewport is irrelevant. */
3680 return true;
3681 }
3682 } else {
3683 width_from_buffer = surface->width_from_buffer;
3684 height_from_buffer = surface->height_from_buffer;
3685 }
3686
3687 assert((width_from_buffer == 0) == (height_from_buffer == 0));
3688 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
3689
3690 /* No buffer: viewport is irrelevant. */
3691 if (width_from_buffer == 0 || height_from_buffer == 0)
3692 return true;
3693
3694 /* overflow checks for wl_fixed_from_int() */
3695 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
3696 return false;
3697 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
3698 return false;
3699
3700 w = wl_fixed_from_int(width_from_buffer);
3701 h = wl_fixed_from_int(height_from_buffer);
3702
3703 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
3704 return false;
3705 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
3706 return false;
3707
3708 return true;
3709}
3710
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003711static bool
3712fixed_is_integer(wl_fixed_t v)
3713{
3714 return (v & 0xff) == 0;
3715}
3716
3717static bool
3718weston_surface_is_pending_viewport_dst_size_int(
3719 const struct weston_surface *surface)
3720{
3721 const struct weston_buffer_viewport *vp =
3722 &surface->pending.buffer_viewport;
3723
3724 if (vp->surface.width != -1) {
3725 assert(vp->surface.width > 0 && vp->surface.height > 0);
3726 return true;
3727 }
3728
3729 return fixed_is_integer(vp->buffer.src_width) &&
3730 fixed_is_integer(vp->buffer.src_height);
3731}
3732
Derek Foreman152254b2015-11-26 14:17:48 -06003733/* Translate pending damage in buffer co-ordinates to surface
3734 * co-ordinates and union it with a pixman_region32_t.
3735 * This should only be called after the buffer is attached.
3736 */
3737static void
3738apply_damage_buffer(pixman_region32_t *dest,
3739 struct weston_surface *surface,
3740 struct weston_surface_state *state)
3741{
3742 struct weston_buffer *buffer = surface->buffer_ref.buffer;
3743
3744 /* wl_surface.damage_buffer needs to be clipped to the buffer,
3745 * translated into surface co-ordinates and unioned with
3746 * any other surface damage.
3747 * None of this makes sense if there is no buffer though.
3748 */
3749 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
3750 pixman_region32_t buffer_damage;
3751
3752 pixman_region32_intersect_rect(&state->damage_buffer,
3753 &state->damage_buffer,
3754 0, 0, buffer->width,
3755 buffer->height);
3756 pixman_region32_init(&buffer_damage);
3757 weston_matrix_transform_region(&buffer_damage,
3758 &surface->buffer_to_surface_matrix,
3759 &state->damage_buffer);
3760 pixman_region32_union(dest, dest, &buffer_damage);
3761 pixman_region32_fini(&buffer_damage);
3762 }
3763 /* We should clear this on commit even if there was no buffer */
3764 pixman_region32_clear(&state->damage_buffer);
3765}
3766
Jason Ekstrand1e059042014-10-16 10:55:19 -05003767static void
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303768weston_surface_set_desired_protection(struct weston_surface *surface,
3769 enum weston_hdcp_protection protection)
3770{
3771 if (surface->desired_protection == protection)
3772 return;
3773 surface->desired_protection = protection;
3774 weston_surface_damage(surface);
3775}
3776
3777static void
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303778weston_surface_set_protection_mode(struct weston_surface *surface,
3779 enum weston_surface_protection_mode p_mode)
3780{
3781 struct content_protection *cp = surface->compositor->content_protection;
3782 struct protected_surface *psurface;
3783
3784 surface->protection_mode = p_mode;
3785 wl_list_for_each(psurface, &cp->protected_list, link) {
3786 if (!psurface || psurface->surface != surface)
3787 continue;
3788 weston_protected_surface_send_event(psurface,
3789 surface->current_protection);
3790 }
3791}
3792
3793static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05003794weston_surface_commit_state(struct weston_surface *surface,
3795 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003796{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003797 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003798 pixman_region32_t opaque;
3799
Alexander Larsson4ea95522013-05-22 14:41:37 +02003800 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02003801 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03003802 /* wp_viewport.set_source */
3803 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003804 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003805
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003806 /* wl_surface.attach */
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003807 if (state->newly_attached) {
3808 /* zwp_surface_synchronization_v1.set_acquire_fence */
3809 fd_move(&surface->acquire_fence_fd,
3810 &state->acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003811 /* zwp_surface_synchronization_v1.get_release */
3812 weston_buffer_release_move(&surface->buffer_release_ref,
3813 &state->buffer_release_ref);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003814 weston_surface_attach(surface, state->buffer);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003815 }
Jason Ekstrand7b982072014-05-20 14:33:03 -05003816 weston_surface_state_set_buffer(state, NULL);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003817 assert(state->acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003818 assert(state->buffer_release_ref.buffer_release == NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01003819
Jason Ekstrand1e059042014-10-16 10:55:19 -05003820 weston_surface_build_buffer_matrix(surface,
3821 &surface->surface_to_buffer_matrix);
3822 weston_matrix_invert(&surface->buffer_to_surface_matrix,
3823 &surface->surface_to_buffer_matrix);
3824
Jason Ekstrand7b982072014-05-20 14:33:03 -05003825 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003826 weston_surface_update_size(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003827 if (surface->committed)
3828 surface->committed(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003829 }
Giulio Camuffo184df502013-02-21 11:29:21 +01003830
Jason Ekstrand7b982072014-05-20 14:33:03 -05003831 state->sx = 0;
3832 state->sy = 0;
3833 state->newly_attached = 0;
3834 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03003835
Derek Foreman152254b2015-11-26 14:17:48 -06003836 /* wl_surface.damage and wl_surface.damage_buffer */
Marius Vlad2a1b7862019-09-05 13:12:18 +03003837 if (pixman_region32_not_empty(&state->damage_surface) ||
3838 pixman_region32_not_empty(&state->damage_buffer))
Marius Vlad3203ff62019-09-05 14:56:12 +03003839 TL_POINT(surface->compositor, "core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06003840
Pekka Paalanen8e159182012-10-10 12:49:25 +03003841 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06003842 &state->damage_surface);
3843
3844 apply_damage_buffer(&surface->damage, surface, state);
3845
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05003846 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07003847 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06003848 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003849
3850 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003851 pixman_region32_init(&opaque);
3852 pixman_region32_intersect_rect(&opaque, &state->opaque,
3853 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003854
3855 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
3856 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003857 wl_list_for_each(view, &surface->views, surface_link)
3858 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003859 }
3860
3861 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003862
3863 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003864 pixman_region32_intersect_rect(&surface->input, &state->input,
3865 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003866
Pekka Paalanenbc106382012-10-10 12:49:31 +03003867 /* wl_surface.frame */
3868 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05003869 &state->frame_callback_list);
3870 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003871
3872 /* XXX:
3873 * What should happen with a feedback request, if there
3874 * is no wl_buffer attached for this commit?
3875 */
3876
3877 /* presentation.feedback */
3878 wl_list_insert_list(&surface->feedback_list,
3879 &state->feedback_list);
3880 wl_list_init(&state->feedback_list);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003881
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303882 /* weston_protected_surface.enforced/relaxed */
3883 if (surface->protection_mode != state->protection_mode)
3884 weston_surface_set_protection_mode(surface,
3885 state->protection_mode);
3886
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303887 /* weston_protected_surface.set_type */
3888 weston_surface_set_desired_protection(surface, state->desired_protection);
3889
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003890 wl_signal_emit(&surface->commit_signal, surface);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003891}
3892
3893static void
3894weston_surface_commit(struct weston_surface *surface)
3895{
3896 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003897
Pekka Paalanene67858b2013-04-25 13:57:42 +03003898 weston_surface_commit_subsurface_order(surface);
3899
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003900 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003901}
3902
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003903static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003904weston_subsurface_commit(struct weston_subsurface *sub);
3905
3906static void
3907weston_subsurface_parent_commit(struct weston_subsurface *sub,
3908 int parent_is_synchronized);
3909
3910static void
3911surface_commit(struct wl_client *client, struct wl_resource *resource)
3912{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003913 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003914 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
3915
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003916 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
3917 assert(surface->viewport_resource);
3918
3919 wl_resource_post_error(surface->viewport_resource,
3920 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
3921 "wl_surface@%d has viewport source outside buffer",
3922 wl_resource_get_id(resource));
3923 return;
3924 }
3925
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003926 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
3927 assert(surface->viewport_resource);
3928
3929 wl_resource_post_error(surface->viewport_resource,
3930 WP_VIEWPORT_ERROR_BAD_SIZE,
3931 "wl_surface@%d viewport dst size not integer",
3932 wl_resource_get_id(resource));
3933 return;
3934 }
3935
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003936 if (surface->pending.acquire_fence_fd >= 0) {
3937 assert(surface->synchronization_resource);
3938
3939 if (!surface->pending.buffer) {
3940 fd_clear(&surface->pending.acquire_fence_fd);
3941 wl_resource_post_error(surface->synchronization_resource,
3942 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3943 "wl_surface@%"PRIu32" no buffer for synchronization",
3944 wl_resource_get_id(resource));
3945 return;
3946 }
3947
3948 /* We support fences for both wp_linux_dmabuf and opaque EGL
3949 * buffers, as mandated by minor version 2 of the
3950 * zwp_linux_explicit_synchronization_v1 protocol. Since
3951 * renderers that support fences currently only support these
3952 * two buffer types plus SHM buffers, we can just check for the
3953 * SHM buffer case here.
3954 */
3955 if (wl_shm_buffer_get(surface->pending.buffer->resource)) {
3956 fd_clear(&surface->pending.acquire_fence_fd);
3957 wl_resource_post_error(surface->synchronization_resource,
3958 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_UNSUPPORTED_BUFFER,
3959 "wl_surface@%"PRIu32" unsupported buffer for synchronization",
3960 wl_resource_get_id(resource));
3961 return;
3962 }
3963 }
3964
Alexandros Frantzis67629672018-10-19 12:14:11 +03003965 if (surface->pending.buffer_release_ref.buffer_release &&
3966 !surface->pending.buffer) {
3967 assert(surface->synchronization_resource);
3968
3969 wl_resource_post_error(surface->synchronization_resource,
3970 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3971 "wl_surface@%"PRIu32" no buffer for synchronization",
3972 wl_resource_get_id(resource));
3973 return;
3974 }
3975
Pekka Paalanene67858b2013-04-25 13:57:42 +03003976 if (sub) {
3977 weston_subsurface_commit(sub);
3978 return;
3979 }
3980
3981 weston_surface_commit(surface);
3982
3983 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
3984 if (sub->surface != surface)
3985 weston_subsurface_parent_commit(sub, 0);
3986 }
3987}
3988
3989static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003990surface_set_buffer_transform(struct wl_client *client,
3991 struct wl_resource *resource, int transform)
3992{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003993 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003994
Jonny Lamba55f1392014-05-30 12:07:15 +02003995 /* if wl_output.transform grows more members this will need to be updated. */
3996 if (transform < 0 ||
3997 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
3998 wl_resource_post_error(resource,
3999 WL_SURFACE_ERROR_INVALID_TRANSFORM,
4000 "buffer transform must be a valid transform "
4001 "('%d' specified)", transform);
4002 return;
4003 }
4004
Pekka Paalanen952b6c82014-03-14 14:38:15 +02004005 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004006 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004007}
4008
Alexander Larsson4ea95522013-05-22 14:41:37 +02004009static void
4010surface_set_buffer_scale(struct wl_client *client,
4011 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004012 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004013{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004014 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02004015
Jonny Lamba55f1392014-05-30 12:07:15 +02004016 if (scale < 1) {
4017 wl_resource_post_error(resource,
4018 WL_SURFACE_ERROR_INVALID_SCALE,
4019 "buffer scale must be at least one "
4020 "('%d' specified)", scale);
4021 return;
4022 }
4023
Pekka Paalanen952b6c82014-03-14 14:38:15 +02004024 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004025 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02004026}
4027
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04004028static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004029 surface_destroy,
4030 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04004031 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004032 surface_frame,
4033 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03004034 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004035 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02004036 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06004037 surface_set_buffer_scale,
4038 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004039};
4040
4041static void
4042compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04004043 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004044{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04004045 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004046 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004047
Kristian Høgsberg18c93002012-01-27 11:58:31 -05004048 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04004049 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04004050 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004051 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04004052 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004053
Jason Ekstranda85118c2013-06-27 20:17:02 -05004054 surface->resource =
4055 wl_resource_create(client, &wl_surface_interface,
4056 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004057 if (surface->resource == NULL) {
4058 weston_surface_destroy(surface);
4059 wl_resource_post_no_memory(resource);
4060 return;
4061 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05004062 wl_resource_set_implementation(surface->resource, &surface_interface,
4063 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07004064
4065 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004066}
4067
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004068static void
4069destroy_region(struct wl_resource *resource)
4070{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05004071 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004072
4073 pixman_region32_fini(&region->region);
4074 free(region);
4075}
4076
4077static void
4078region_destroy(struct wl_client *client, struct wl_resource *resource)
4079{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004080 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004081}
4082
4083static void
4084region_add(struct wl_client *client, struct wl_resource *resource,
4085 int32_t x, int32_t y, int32_t width, int32_t height)
4086{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05004087 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004088
4089 pixman_region32_union_rect(&region->region, &region->region,
4090 x, y, width, height);
4091}
4092
4093static void
4094region_subtract(struct wl_client *client, struct wl_resource *resource,
4095 int32_t x, int32_t y, int32_t width, int32_t height)
4096{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05004097 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004098 pixman_region32_t rect;
4099
4100 pixman_region32_init_rect(&rect, x, y, width, height);
4101 pixman_region32_subtract(&region->region, &region->region, &rect);
4102 pixman_region32_fini(&rect);
4103}
4104
4105static const struct wl_region_interface region_interface = {
4106 region_destroy,
4107 region_add,
4108 region_subtract
4109};
4110
4111static void
4112compositor_create_region(struct wl_client *client,
4113 struct wl_resource *resource, uint32_t id)
4114{
4115 struct weston_region *region;
4116
4117 region = malloc(sizeof *region);
4118 if (region == NULL) {
4119 wl_resource_post_no_memory(resource);
4120 return;
4121 }
4122
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004123 pixman_region32_init(&region->region);
4124
Jason Ekstranda85118c2013-06-27 20:17:02 -05004125 region->resource =
4126 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004127 if (region->resource == NULL) {
4128 free(region);
4129 wl_resource_post_no_memory(resource);
4130 return;
4131 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05004132 wl_resource_set_implementation(region->resource, &region_interface,
4133 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004134}
4135
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04004136static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004137 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004138 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004139};
4140
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004141static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03004142weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
4143{
4144 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004145
Jason Ekstrand7b982072014-05-20 14:33:03 -05004146 weston_surface_commit_state(surface, &sub->cached);
4147 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004148
4149 weston_surface_commit_subsurface_order(surface);
4150
4151 weston_surface_schedule_repaint(surface);
4152
Jason Ekstrand7b982072014-05-20 14:33:03 -05004153 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004154}
4155
4156static void
4157weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
4158{
4159 struct weston_surface *surface = sub->surface;
4160
4161 /*
4162 * If this commit would cause the surface to move by the
4163 * attach(dx, dy) parameters, the old damage region must be
4164 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05004165 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03004166 */
Derek Foreman152254b2015-11-26 14:17:48 -06004167 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004168 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06004169 pixman_region32_union(&sub->cached.damage_surface,
4170 &sub->cached.damage_surface,
4171 &surface->pending.damage_surface);
4172 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004173
4174 if (surface->pending.newly_attached) {
4175 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05004176 weston_surface_state_set_buffer(&sub->cached,
4177 surface->pending.buffer);
4178 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004179 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04004180 weston_presentation_feedback_discard_list(
4181 &sub->cached.feedback_list);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03004182 /* zwp_surface_synchronization_v1.set_acquire_fence */
4183 fd_move(&sub->cached.acquire_fence_fd,
4184 &surface->pending.acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03004185 /* zwp_surface_synchronization_v1.get_release */
4186 weston_buffer_release_move(&sub->cached.buffer_release_ref,
4187 &surface->pending.buffer_release_ref);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004188 }
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05304189 sub->cached.desired_protection = surface->pending.desired_protection;
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05304190 sub->cached.protection_mode = surface->pending.protection_mode;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03004191 assert(surface->pending.acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03004192 assert(surface->pending.buffer_release_ref.buffer_release == NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004193 sub->cached.sx += surface->pending.sx;
4194 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02004195
Derek Foreman152254b2015-11-26 14:17:48 -06004196 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
4197
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004198 sub->cached.buffer_viewport.changed |=
4199 surface->pending.buffer_viewport.changed;
4200 sub->cached.buffer_viewport.buffer =
4201 surface->pending.buffer_viewport.buffer;
4202 sub->cached.buffer_viewport.surface =
4203 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004204
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004205 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004206
4207 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
4208
4209 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
4210
4211 wl_list_insert_list(&sub->cached.frame_callback_list,
4212 &surface->pending.frame_callback_list);
4213 wl_list_init(&surface->pending.frame_callback_list);
4214
Pekka Paalanen133e4392014-09-23 22:08:46 -04004215 wl_list_insert_list(&sub->cached.feedback_list,
4216 &surface->pending.feedback_list);
4217 wl_list_init(&surface->pending.feedback_list);
4218
Jason Ekstrand7b982072014-05-20 14:33:03 -05004219 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004220}
4221
Derek Foreman280e7dd2014-10-03 13:13:42 -05004222static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03004223weston_subsurface_is_synchronized(struct weston_subsurface *sub)
4224{
4225 while (sub) {
4226 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05004227 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004228
4229 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05004230 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004231
4232 sub = weston_surface_to_subsurface(sub->parent);
4233 }
4234
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01004235 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004236}
4237
4238static void
4239weston_subsurface_commit(struct weston_subsurface *sub)
4240{
4241 struct weston_surface *surface = sub->surface;
4242 struct weston_subsurface *tmp;
4243
4244 /* Recursive check for effectively synchronized. */
4245 if (weston_subsurface_is_synchronized(sub)) {
4246 weston_subsurface_commit_to_cache(sub);
4247 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05004248 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03004249 /* flush accumulated state from cache */
4250 weston_subsurface_commit_to_cache(sub);
4251 weston_subsurface_commit_from_cache(sub);
4252 } else {
4253 weston_surface_commit(surface);
4254 }
4255
4256 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
4257 if (tmp->surface != surface)
4258 weston_subsurface_parent_commit(tmp, 0);
4259 }
4260 }
4261}
4262
4263static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004264weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004265{
4266 struct weston_surface *surface = sub->surface;
4267 struct weston_subsurface *tmp;
4268
Pekka Paalanene67858b2013-04-25 13:57:42 +03004269 /* From now on, commit_from_cache the whole sub-tree, regardless of
4270 * the synchronized mode of each child. This sub-surface or some
4271 * of its ancestors were synchronized, so we are synchronized
4272 * all the way down.
4273 */
4274
Jason Ekstrand7b982072014-05-20 14:33:03 -05004275 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004276 weston_subsurface_commit_from_cache(sub);
4277
4278 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
4279 if (tmp->surface != surface)
4280 weston_subsurface_parent_commit(tmp, 1);
4281 }
4282}
4283
4284static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004285weston_subsurface_parent_commit(struct weston_subsurface *sub,
4286 int parent_is_synchronized)
4287{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004288 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004289 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004290 wl_list_for_each(view, &sub->surface->views, surface_link)
4291 weston_view_set_position(view,
4292 sub->position.x,
4293 sub->position.y);
4294
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004295 sub->position.set = 0;
4296 }
4297
4298 if (parent_is_synchronized || sub->synchronized)
4299 weston_subsurface_synchronized_commit(sub);
4300}
4301
Pekka Paalanen8274d902014-08-06 19:36:51 +03004302static int
4303subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
4304{
4305 return snprintf(buf, len, "sub-surface");
4306}
4307
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004308static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004309subsurface_committed(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004310{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004311 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004312
Jason Ekstranda7af7042013-10-12 22:38:11 -05004313 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004314 weston_view_set_position(view,
4315 view->geometry.x + dx,
4316 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004317
4318 /* No need to check parent mappedness, because if parent is not
4319 * mapped, parent is not in a visible layer, so this sub-surface
4320 * will not be drawn either.
4321 */
Armin Krezovićf8486c32016-06-30 06:04:28 +02004322
Pekka Paalanene67858b2013-04-25 13:57:42 +03004323 if (!weston_surface_is_mapped(surface)) {
Armin Krezovićf8486c32016-06-30 06:04:28 +02004324 surface->is_mapped = true;
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03004325
Derek Foreman4b1a0a12014-09-10 15:37:33 -05004326 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03004327 * because that would call it also for the parent surface,
4328 * which might not be mapped yet. That would lead to
4329 * inconsistent state, where the window could never be
4330 * mapped.
4331 *
Armin Krezovićf8486c32016-06-30 06:04:28 +02004332 * Instead just force the is_mapped flag on, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03004333 * weston_surface_is_mapped() return true, so that when the
4334 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03004335 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03004336 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004337 }
4338}
4339
4340static struct weston_subsurface *
4341weston_surface_to_subsurface(struct weston_surface *surface)
4342{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004343 if (surface->committed == subsurface_committed)
4344 return surface->committed_private;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004345
4346 return NULL;
4347}
4348
Pekka Paalanen01388e22013-04-25 13:57:44 +03004349WL_EXPORT struct weston_surface *
4350weston_surface_get_main_surface(struct weston_surface *surface)
4351{
4352 struct weston_subsurface *sub;
4353
4354 while (surface && (sub = weston_surface_to_subsurface(surface)))
4355 surface = sub->parent;
4356
4357 return surface;
4358}
4359
Pekka Paalanen50b67472014-10-01 15:02:41 +03004360WL_EXPORT int
4361weston_surface_set_role(struct weston_surface *surface,
4362 const char *role_name,
4363 struct wl_resource *error_resource,
4364 uint32_t error_code)
4365{
4366 assert(role_name);
4367
4368 if (surface->role_name == NULL ||
4369 surface->role_name == role_name ||
4370 strcmp(surface->role_name, role_name) == 0) {
4371 surface->role_name = role_name;
4372
4373 return 0;
4374 }
4375
4376 wl_resource_post_error(error_resource, error_code,
4377 "Cannot assign role %s to wl_surface@%d,"
4378 " already has role %s\n",
4379 role_name,
4380 wl_resource_get_id(surface->resource),
4381 surface->role_name);
4382 return -1;
4383}
4384
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02004385WL_EXPORT const char *
4386weston_surface_get_role(struct weston_surface *surface)
4387{
4388 return surface->role_name;
4389}
4390
Pekka Paalanen8274d902014-08-06 19:36:51 +03004391WL_EXPORT void
4392weston_surface_set_label_func(struct weston_surface *surface,
4393 int (*desc)(struct weston_surface *,
4394 char *, size_t))
4395{
4396 surface->get_label = desc;
Marius Vlad5de92972019-10-15 13:25:41 +03004397 weston_timeline_refresh_subscription_objects(surface->compositor,
4398 surface);
Pekka Paalanen8274d902014-08-06 19:36:51 +03004399}
4400
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004401/** Get the size of surface contents
4402 *
4403 * \param surface The surface to query.
4404 * \param width Returns the width of raw contents.
4405 * \param height Returns the height of raw contents.
4406 *
4407 * Retrieves the raw surface content size in pixels for the given surface.
4408 * This is the whole content size in buffer pixels. If the surface
4409 * has no content or the renderer does not implement this feature,
4410 * zeroes are returned.
4411 *
4412 * This function is used to determine the buffer size needed for
4413 * a weston_surface_copy_content() call.
4414 */
4415WL_EXPORT void
4416weston_surface_get_content_size(struct weston_surface *surface,
4417 int *width, int *height)
4418{
4419 struct weston_renderer *rer = surface->compositor->renderer;
4420
4421 if (!rer->surface_get_content_size) {
4422 *width = 0;
4423 *height = 0;
4424 return;
4425 }
4426
4427 rer->surface_get_content_size(surface, width, height);
4428}
4429
Quentin Glidic248dd102016-08-12 10:41:34 +02004430/** Get the bounding box of a surface and its subsurfaces
4431 *
4432 * \param surface The surface to query.
4433 * \return The bounding box relative to the surface origin.
4434 *
4435 */
4436WL_EXPORT struct weston_geometry
4437weston_surface_get_bounding_box(struct weston_surface *surface)
4438{
4439 pixman_region32_t region;
4440 pixman_box32_t *box;
4441 struct weston_subsurface *subsurface;
4442
4443 pixman_region32_init_rect(&region,
4444 0, 0,
4445 surface->width, surface->height);
4446
4447 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link)
4448 pixman_region32_union_rect(&region, &region,
4449 subsurface->position.x,
4450 subsurface->position.y,
4451 subsurface->surface->width,
4452 subsurface->surface->height);
4453
4454 box = pixman_region32_extents(&region);
4455 struct weston_geometry geometry = {
4456 .x = box->x1,
4457 .y = box->y1,
4458 .width = box->x2 - box->x1,
4459 .height = box->y2 - box->y1,
4460 };
4461
4462 pixman_region32_fini(&region);
4463
4464 return geometry;
4465}
4466
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004467/** Copy surface contents to system memory.
4468 *
4469 * \param surface The surface to copy from.
4470 * \param target Pointer to the target memory buffer.
4471 * \param size Size of the target buffer in bytes.
4472 * \param src_x X location on contents to copy from.
4473 * \param src_y Y location on contents to copy from.
4474 * \param width Width in pixels of the area to copy.
4475 * \param height Height in pixels of the area to copy.
4476 * \return 0 for success, -1 for failure.
4477 *
4478 * Surface contents are maintained by the renderer. They can be in a
4479 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
4480 * else.
4481 *
4482 * Surface contents are copied into memory pointed to by target,
4483 * which has size bytes of space available. The target memory
4484 * may be larger than needed, but being smaller returns an error.
4485 * The extra bytes in target may or may not be written; their content is
4486 * unspecified. Size must be large enough to hold the image.
4487 *
4488 * The image in the target memory will be arranged in rows from
4489 * top to bottom, and pixels on a row from left to right. The pixel
4490 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
4491 * width * 4.
4492 *
4493 * Parameters src_x and src_y define the upper-left corner in buffer
4494 * coordinates (pixels) to copy from. Parameters width and height
4495 * define the size of the area to copy in pixels.
4496 *
4497 * The rectangle defined by src_x, src_y, width, height must fit in
4498 * the surface contents. Otherwise an error is returned.
4499 *
Changwoo Chof97d2502017-08-05 00:30:47 +09004500 * Use weston_surface_get_content_size to determine the content size; the
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004501 * needed target buffer size and rectangle limits.
4502 *
4503 * CURRENT IMPLEMENTATION RESTRICTIONS:
4504 * - the machine must be little-endian due to Pixman formats.
4505 *
4506 * NOTE: Pixman formats are premultiplied.
4507 */
4508WL_EXPORT int
4509weston_surface_copy_content(struct weston_surface *surface,
4510 void *target, size_t size,
4511 int src_x, int src_y,
4512 int width, int height)
4513{
4514 struct weston_renderer *rer = surface->compositor->renderer;
4515 int cw, ch;
4516 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
4517
4518 if (!rer->surface_copy_content)
4519 return -1;
4520
4521 weston_surface_get_content_size(surface, &cw, &ch);
4522
4523 if (src_x < 0 || src_y < 0)
4524 return -1;
4525
4526 if (width <= 0 || height <= 0)
4527 return -1;
4528
4529 if (src_x + width > cw || src_y + height > ch)
4530 return -1;
4531
4532 if (width * bytespp * height > size)
4533 return -1;
4534
4535 return rer->surface_copy_content(surface, target, size,
4536 src_x, src_y, width, height);
4537}
4538
Pekka Paalanene67858b2013-04-25 13:57:42 +03004539static void
4540subsurface_set_position(struct wl_client *client,
4541 struct wl_resource *resource, int32_t x, int32_t y)
4542{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004543 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004544
4545 if (!sub)
4546 return;
4547
4548 sub->position.x = x;
4549 sub->position.y = y;
4550 sub->position.set = 1;
4551}
4552
4553static struct weston_subsurface *
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004554subsurface_find_sibling(struct weston_subsurface *sub,
4555 struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004556{
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004557 struct weston_surface *parent = sub->parent;
4558 struct weston_subsurface *sibling;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004559
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004560 wl_list_for_each(sibling, &parent->subsurface_list, parent_link) {
4561 if (sibling->surface == surface && sibling != sub)
4562 return sibling;
4563 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004564
4565 return NULL;
4566}
4567
4568static struct weston_subsurface *
4569subsurface_sibling_check(struct weston_subsurface *sub,
4570 struct weston_surface *surface,
4571 const char *request)
4572{
4573 struct weston_subsurface *sibling;
4574
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004575 sibling = subsurface_find_sibling(sub, surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004576 if (!sibling) {
4577 wl_resource_post_error(sub->resource,
4578 WL_SUBSURFACE_ERROR_BAD_SURFACE,
4579 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004580 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004581 return NULL;
4582 }
4583
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004584 assert(sibling->parent == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004585
4586 return sibling;
4587}
4588
4589static void
4590subsurface_place_above(struct wl_client *client,
4591 struct wl_resource *resource,
4592 struct wl_resource *sibling_resource)
4593{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004594 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004595 struct weston_surface *surface =
4596 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004597 struct weston_subsurface *sibling;
4598
4599 if (!sub)
4600 return;
4601
4602 sibling = subsurface_sibling_check(sub, surface, "place_above");
4603 if (!sibling)
4604 return;
4605
4606 wl_list_remove(&sub->parent_link_pending);
4607 wl_list_insert(sibling->parent_link_pending.prev,
4608 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004609
4610 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004611}
4612
4613static void
4614subsurface_place_below(struct wl_client *client,
4615 struct wl_resource *resource,
4616 struct wl_resource *sibling_resource)
4617{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004618 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004619 struct weston_surface *surface =
4620 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004621 struct weston_subsurface *sibling;
4622
4623 if (!sub)
4624 return;
4625
4626 sibling = subsurface_sibling_check(sub, surface, "place_below");
4627 if (!sibling)
4628 return;
4629
4630 wl_list_remove(&sub->parent_link_pending);
4631 wl_list_insert(&sibling->parent_link_pending,
4632 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004633
4634 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004635}
4636
4637static void
4638subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
4639{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004640 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004641
4642 if (sub)
4643 sub->synchronized = 1;
4644}
4645
4646static void
4647subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
4648{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004649 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004650
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004651 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03004652 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004653
4654 /* If sub became effectively desynchronized, flush. */
4655 if (!weston_subsurface_is_synchronized(sub))
4656 weston_subsurface_synchronized_commit(sub);
4657 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004658}
4659
4660static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03004661weston_subsurface_unlink_parent(struct weston_subsurface *sub)
4662{
4663 wl_list_remove(&sub->parent_link);
4664 wl_list_remove(&sub->parent_link_pending);
4665 wl_list_remove(&sub->parent_destroy_listener.link);
4666 sub->parent = NULL;
4667}
4668
4669static void
4670weston_subsurface_destroy(struct weston_subsurface *sub);
4671
4672static void
4673subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
4674{
4675 struct weston_subsurface *sub =
4676 container_of(listener, struct weston_subsurface,
4677 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004678 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004679
4680 /* The protocol object (wl_resource) is left inert. */
4681 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004682 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004683
4684 weston_subsurface_destroy(sub);
4685}
4686
4687static void
4688subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
4689{
4690 struct weston_subsurface *sub =
4691 container_of(listener, struct weston_subsurface,
4692 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004693 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004694 assert(sub->surface != sub->parent);
4695
4696 if (weston_surface_is_mapped(sub->surface))
4697 weston_surface_unmap(sub->surface);
4698
4699 weston_subsurface_unlink_parent(sub);
4700}
4701
4702static void
4703subsurface_resource_destroy(struct wl_resource *resource)
4704{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004705 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004706
4707 if (sub)
4708 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004709}
4710
4711static void
4712subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
4713{
4714 wl_resource_destroy(resource);
4715}
4716
4717static void
4718weston_subsurface_link_parent(struct weston_subsurface *sub,
4719 struct weston_surface *parent)
4720{
4721 sub->parent = parent;
4722 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004723 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004724 &sub->parent_destroy_listener);
4725
4726 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4727 wl_list_insert(&parent->subsurface_list_pending,
4728 &sub->parent_link_pending);
4729}
4730
4731static void
4732weston_subsurface_link_surface(struct weston_subsurface *sub,
4733 struct weston_surface *surface)
4734{
4735 sub->surface = surface;
4736 sub->surface_destroy_listener.notify =
4737 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004738 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004739 &sub->surface_destroy_listener);
4740}
4741
4742static void
4743weston_subsurface_destroy(struct weston_subsurface *sub)
4744{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004745 struct weston_view *view, *next;
4746
Pekka Paalanene67858b2013-04-25 13:57:42 +03004747 assert(sub->surface);
4748
4749 if (sub->resource) {
4750 assert(weston_surface_to_subsurface(sub->surface) == sub);
4751 assert(sub->parent_destroy_listener.notify ==
4752 subsurface_handle_parent_destroy);
4753
George Kiagiadakised04d382014-06-13 18:10:26 +02004754 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
4755 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004756 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02004757 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05004758
Pekka Paalanene67858b2013-04-25 13:57:42 +03004759 if (sub->parent)
4760 weston_subsurface_unlink_parent(sub);
4761
Jason Ekstrand7b982072014-05-20 14:33:03 -05004762 weston_surface_state_fini(&sub->cached);
4763 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004764
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004765 sub->surface->committed = NULL;
4766 sub->surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004767 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004768 } else {
4769 /* the dummy weston_subsurface for the parent itself */
4770 assert(sub->parent_destroy_listener.notify == NULL);
4771 wl_list_remove(&sub->parent_link);
4772 wl_list_remove(&sub->parent_link_pending);
4773 }
4774
4775 wl_list_remove(&sub->surface_destroy_listener.link);
4776 free(sub);
4777}
4778
4779static const struct wl_subsurface_interface subsurface_implementation = {
4780 subsurface_destroy,
4781 subsurface_set_position,
4782 subsurface_place_above,
4783 subsurface_place_below,
4784 subsurface_set_sync,
4785 subsurface_set_desync
4786};
4787
4788static struct weston_subsurface *
4789weston_subsurface_create(uint32_t id, struct weston_surface *surface,
4790 struct weston_surface *parent)
4791{
4792 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004793 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004794
Bryce Harringtonde16d892014-11-20 22:21:57 -08004795 sub = zalloc(sizeof *sub);
4796 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004797 return NULL;
4798
Jason Ekstranda7af7042013-10-12 22:38:11 -05004799 wl_list_init(&sub->unused_views);
4800
Jason Ekstranda85118c2013-06-27 20:17:02 -05004801 sub->resource =
4802 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004803 if (!sub->resource) {
4804 free(sub);
4805 return NULL;
4806 }
4807
Jason Ekstranda85118c2013-06-27 20:17:02 -05004808 wl_resource_set_implementation(sub->resource,
4809 &subsurface_implementation,
4810 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004811 weston_subsurface_link_surface(sub, surface);
4812 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05004813 weston_surface_state_init(&sub->cached);
4814 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004815 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004816
4817 return sub;
4818}
4819
4820/* Create a dummy subsurface for having the parent itself in its
4821 * sub-surface lists. Makes stacking order manipulation easy.
4822 */
4823static struct weston_subsurface *
4824weston_subsurface_create_for_parent(struct weston_surface *parent)
4825{
4826 struct weston_subsurface *sub;
4827
Bryce Harringtonde16d892014-11-20 22:21:57 -08004828 sub = zalloc(sizeof *sub);
4829 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004830 return NULL;
4831
4832 weston_subsurface_link_surface(sub, parent);
4833 sub->parent = parent;
4834 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4835 wl_list_insert(&parent->subsurface_list_pending,
4836 &sub->parent_link_pending);
4837
4838 return sub;
4839}
4840
4841static void
4842subcompositor_get_subsurface(struct wl_client *client,
4843 struct wl_resource *resource,
4844 uint32_t id,
4845 struct wl_resource *surface_resource,
4846 struct wl_resource *parent_resource)
4847{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004848 struct weston_surface *surface =
4849 wl_resource_get_user_data(surface_resource);
4850 struct weston_surface *parent =
4851 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004852 struct weston_subsurface *sub;
4853 static const char where[] = "get_subsurface: wl_subsurface@";
4854
4855 if (surface == parent) {
4856 wl_resource_post_error(resource,
4857 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4858 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004859 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004860 return;
4861 }
4862
4863 if (weston_surface_to_subsurface(surface)) {
4864 wl_resource_post_error(resource,
4865 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4866 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004867 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004868 return;
4869 }
4870
Pekka Paalanen50b67472014-10-01 15:02:41 +03004871 if (weston_surface_set_role(surface, "wl_subsurface", resource,
4872 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004873 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004874
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004875 if (weston_surface_get_main_surface(parent) == surface) {
4876 wl_resource_post_error(resource,
4877 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4878 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004879 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004880 return;
4881 }
4882
Pekka Paalanene67858b2013-04-25 13:57:42 +03004883 /* make sure the parent is in its own list */
4884 if (wl_list_empty(&parent->subsurface_list)) {
4885 if (!weston_subsurface_create_for_parent(parent)) {
4886 wl_resource_post_no_memory(resource);
4887 return;
4888 }
4889 }
4890
4891 sub = weston_subsurface_create(id, surface, parent);
4892 if (!sub) {
4893 wl_resource_post_no_memory(resource);
4894 return;
4895 }
4896
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004897 surface->committed = subsurface_committed;
4898 surface->committed_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004899 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004900}
4901
4902static void
4903subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
4904{
4905 wl_resource_destroy(resource);
4906}
4907
4908static const struct wl_subcompositor_interface subcompositor_interface = {
4909 subcompositor_destroy,
4910 subcompositor_get_subsurface
4911};
4912
4913static void
4914bind_subcompositor(struct wl_client *client,
4915 void *data, uint32_t version, uint32_t id)
4916{
4917 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004918 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004919
Jason Ekstranda85118c2013-06-27 20:17:02 -05004920 resource =
4921 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004922 if (resource == NULL) {
4923 wl_client_post_no_memory(client);
4924 return;
4925 }
4926 wl_resource_set_implementation(resource, &subcompositor_interface,
4927 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004928}
4929
Bryce Harrington0795ece2016-08-30 12:04:26 -07004930/** Set a DPMS mode on all of the compositor's outputs
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004931 *
4932 * \param compositor The compositor instance
4933 * \param state The DPMS state the outputs will be set to
4934 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004935static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004936weston_compositor_dpms(struct weston_compositor *compositor,
4937 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004938{
Emmanuel Gil Peyroteff793a2021-07-31 17:25:41 +02004939 struct weston_output *output;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004940
Emmanuel Gil Peyroteff793a2021-07-31 17:25:41 +02004941 wl_list_for_each(output, &compositor->output_list, link)
Bryce Harrington08976ac2016-08-30 12:05:16 -07004942 if (output->set_dpms)
4943 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004944}
4945
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004946/** Restores the compositor to active status
4947 *
4948 * \param compositor The compositor instance
4949 *
4950 * If the compositor was in a sleeping mode, all outputs are powered
4951 * back on via DPMS. Otherwise if the compositor was inactive
4952 * (idle/locked, offscreen, or sleeping) then the compositor's wake
4953 * signal will fire.
4954 *
4955 * Restarts the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004956 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004957 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004958WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004959weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004960{
Neil Roberts8b62e202013-09-30 13:14:47 +01004961 uint32_t old_state = compositor->state;
4962
4963 /* The state needs to be changed before emitting the wake
4964 * signal because that may try to schedule a repaint which
4965 * will not work if the compositor is still sleeping */
4966 compositor->state = WESTON_COMPOSITOR_ACTIVE;
4967
4968 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004969 case WESTON_COMPOSITOR_SLEEPING:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004970 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004971 case WESTON_COMPOSITOR_OFFSCREEN:
Daniel Stone893b9362016-11-08 15:47:09 +00004972 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004973 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004974 /* fall through */
4975 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004976 wl_event_source_timer_update(compositor->idle_source,
4977 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004978 }
4979}
4980
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004981/** Turns off rendering and frame events for the compositor.
4982 *
4983 * \param compositor The compositor instance
4984 *
4985 * This is used for example to prevent further rendering while the
4986 * compositor is shutting down.
4987 *
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004988 * Stops the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004989 *
4990 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004991 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004992WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004993weston_compositor_offscreen(struct weston_compositor *compositor)
4994{
4995 switch (compositor->state) {
4996 case WESTON_COMPOSITOR_OFFSCREEN:
4997 return;
4998 case WESTON_COMPOSITOR_SLEEPING:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004999 default:
5000 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
5001 wl_event_source_timer_update(compositor->idle_source, 0);
5002 }
5003}
5004
Bryce Harringtonc9626a32015-12-11 13:11:38 -08005005/** Powers down all attached output devices
5006 *
5007 * \param compositor The compositor instance
5008 *
5009 * Causes rendering to the outputs to cease, and no frame events to be
5010 * sent. Only powers down the outputs if the compositor is not already
5011 * in sleep mode.
5012 *
5013 * Stops the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005014 *
5015 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08005016 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01005017WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005018weston_compositor_sleep(struct weston_compositor *compositor)
5019{
5020 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
5021 return;
5022
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01005023 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005024 compositor->state = WESTON_COMPOSITOR_SLEEPING;
5025 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
5026}
5027
Bryce Harringtonc9626a32015-12-11 13:11:38 -08005028/** Sets compositor to idle mode
5029 *
5030 * \param data The compositor instance
5031 *
5032 * This is called when the idle timer fires. Once the compositor is in
5033 * idle mode it requires a wake action (e.g. via
5034 * weston_compositor_wake()) to restore it. The compositor's
5035 * idle_signal will be triggered when the idle event occurs.
5036 *
5037 * Idleness can be inhibited by setting the compositor's idle_inhibit
5038 * property.
5039 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04005040static int
5041idle_handler(void *data)
5042{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005043 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04005044
5045 if (compositor->idle_inhibit)
5046 return 1;
5047
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005048 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005049 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04005050
5051 return 1;
5052}
5053
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005054WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08005055weston_plane_init(struct weston_plane *plane,
5056 struct weston_compositor *ec,
5057 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005058{
5059 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02005060 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005061 plane->x = x;
5062 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08005063 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03005064
5065 /* Init the link so that the call to wl_list_remove() when releasing
5066 * the plane without ever stacking doesn't lead to a crash */
5067 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005068}
5069
5070WL_EXPORT void
5071weston_plane_release(struct weston_plane *plane)
5072{
Xiong Zhang97116532013-10-23 13:58:31 +08005073 struct weston_view *view;
5074
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005075 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02005076 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03005077
Pekka Paalanen6551c092021-05-03 16:09:45 +03005078 /*
5079 * Can't use paint node list here, weston_plane is not specific to an
5080 * output.
5081 */
Xiong Zhang97116532013-10-23 13:58:31 +08005082 wl_list_for_each(view, &plane->compositor->view_list, link) {
5083 if (view->plane == plane)
5084 view->plane = NULL;
5085 }
5086
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03005087 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005088}
5089
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005090/** weston_compositor_stack_plane
5091 * \ingroup compositor
5092 */
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02005093WL_EXPORT void
5094weston_compositor_stack_plane(struct weston_compositor *ec,
5095 struct weston_plane *plane,
5096 struct weston_plane *above)
5097{
5098 if (above)
5099 wl_list_insert(above->link.prev, &plane->link);
5100 else
5101 wl_list_insert(&ec->plane_list, &plane->link);
5102}
5103
Quentin Glidic4ef719c2016-07-05 20:44:33 +02005104static void
5105output_release(struct wl_client *client, struct wl_resource *resource)
5106{
5107 wl_resource_destroy(resource);
5108}
5109
5110static const struct wl_output_interface output_interface = {
5111 output_release,
5112};
5113
5114
Casey Dahlin9074db52012-04-19 22:50:09 -04005115static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04005116{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05005117 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04005118}
5119
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005120static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005121bind_output(struct wl_client *client,
5122 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05005123{
Pekka Paalanen05347622017-03-27 12:24:34 +03005124 struct weston_head *head = data;
5125 struct weston_output *output = head->output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005126 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04005127 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05005128
Jason Ekstranda85118c2013-06-27 20:17:02 -05005129 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06005130 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07005131 if (resource == NULL) {
5132 wl_client_post_no_memory(client);
5133 return;
5134 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04005135
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005136 wl_list_insert(&head->resource_list, wl_resource_get_link(resource));
Pekka Paalanen055c1132017-03-27 16:31:25 +03005137 wl_resource_set_implementation(resource, &output_interface, head,
Pekka Paalanen05347622017-03-27 12:24:34 +03005138 unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04005139
Pekka Paalanen05347622017-03-27 12:24:34 +03005140 assert(output);
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05005141 wl_output_send_geometry(resource,
5142 output->x,
5143 output->y,
Pekka Paalanen01f60212017-03-24 15:39:24 +02005144 head->mm_width,
5145 head->mm_height,
5146 head->subpixel,
5147 head->make, head->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04005148 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04005149 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02005150 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02005151 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005152
5153 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05005154 wl_output_send_mode(resource,
5155 mode->flags,
5156 mode->width,
5157 mode->height,
5158 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005159 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02005160
Jasper St. Pierre0013a292014-08-07 16:43:11 -04005161 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02005162 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05005163}
5164
Pekka Paalanendcac3512017-12-08 14:13:34 +02005165static void
5166weston_head_add_global(struct weston_head *head)
5167{
5168 head->global = wl_global_create(head->compositor->wl_display,
5169 &wl_output_interface, 3,
5170 head, bind_output);
5171}
5172
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02005173/** Remove the global wl_output protocol object
5174 *
5175 * \param head The head whose global to remove.
5176 *
5177 * Also orphans the wl_resources for this head (wl_output).
5178 */
5179static void
5180weston_head_remove_global(struct weston_head *head)
5181{
5182 struct wl_resource *resource, *tmp;
5183
5184 if (head->global)
5185 wl_global_destroy(head->global);
5186 head->global = NULL;
5187
5188 wl_resource_for_each_safe(resource, tmp, &head->resource_list) {
5189 unbind_resource(resource);
5190 wl_resource_set_destructor(resource, NULL);
5191 wl_resource_set_user_data(resource, NULL);
5192 }
Roman Gilge97391c2019-03-29 13:01:06 +01005193
5194 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
5195 /* It's sufficient to unset the destructor, then the list elements
5196 * won't be accessed.
5197 */
5198 wl_resource_set_destructor(resource, NULL);
5199 }
5200 wl_list_init(&head->xdg_output_resource_list);
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02005201}
5202
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005203/** Get the backing object of wl_output
5204 *
5205 * \param resource A wl_output protocol object.
5206 * \return The backing object (user data) of a wl_resource representing a
5207 * wl_output protocol object.
Marius Vlad78984ee2019-06-11 00:05:08 +03005208 *
5209 * \ingroup head
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005210 */
Pekka Paalanen055c1132017-03-27 16:31:25 +03005211WL_EXPORT struct weston_head *
5212weston_head_from_resource(struct wl_resource *resource)
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005213{
5214 assert(wl_resource_instance_of(resource, &wl_output_interface,
5215 &output_interface));
5216
5217 return wl_resource_get_user_data(resource);
5218}
5219
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005220/** Initialize a pre-allocated weston_head
5221 *
5222 * \param head The head to initialize.
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005223 * \param name The head name, e.g. the connector name or equivalent.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005224 *
5225 * The head will be safe to attach, detach and release.
5226 *
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005227 * The name is used in logs, and can be used by compositors as a configuration
5228 * identifier.
5229 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005230 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005231 * \internal
5232 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005233WL_EXPORT void
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005234weston_head_init(struct weston_head *head, const char *name)
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005235{
5236 /* Add some (in)sane defaults which can be used
5237 * for checking if an output was properly configured
5238 */
5239 memset(head, 0, sizeof *head);
5240
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005241 wl_list_init(&head->compositor_link);
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005242 wl_signal_init(&head->destroy_signal);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005243 wl_list_init(&head->output_link);
5244 wl_list_init(&head->resource_list);
Roman Gilge97391c2019-03-29 13:01:06 +01005245 wl_list_init(&head->xdg_output_resource_list);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005246 head->name = strdup(name);
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305247 head->current_protection = WESTON_HDCP_DISABLE;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005248}
5249
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005250/** Send output heads changed signal
5251 *
5252 * \param output The output that changed.
5253 *
5254 * Notify that the enabled output gained and/or lost heads, or that the
5255 * associated heads may have changed their connection status. This does not
5256 * include cases where the output becomes enabled or disabled. The registered
5257 * callbacks are called after the change has successfully happened.
5258 *
5259 * If connection status change causes the compositor to attach or detach a head
5260 * to an enabled output, the registered callbacks may be called multiple times.
Marius Vlad55d87362019-06-11 01:15:35 +03005261 *
5262 * \ingroup output
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005263 */
5264static void
5265weston_output_emit_heads_changed(struct weston_output *output)
5266{
5267 wl_signal_emit(&output->compositor->output_heads_changed_signal,
5268 output);
5269}
5270
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005271/** Idle task for emitting heads_changed_signal */
5272static void
5273weston_compositor_call_heads_changed(void *data)
5274{
5275 struct weston_compositor *compositor = data;
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005276 struct weston_head *head;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005277
5278 compositor->heads_changed_source = NULL;
5279
5280 wl_signal_emit(&compositor->heads_changed_signal, compositor);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005281
5282 wl_list_for_each(head, &compositor->head_list, compositor_link) {
5283 if (head->output && head->output->enabled)
5284 weston_output_emit_heads_changed(head->output);
5285 }
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005286}
5287
5288/** Schedule a call on idle to heads_changed callback
5289 *
5290 * \param compositor The Compositor.
5291 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005292 * \ingroup compositor
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005293 * \internal
5294 */
5295static void
5296weston_compositor_schedule_heads_changed(struct weston_compositor *compositor)
5297{
5298 struct wl_event_loop *loop;
5299
5300 if (compositor->heads_changed_source)
5301 return;
5302
5303 loop = wl_display_get_event_loop(compositor->wl_display);
5304 compositor->heads_changed_source = wl_event_loop_add_idle(loop,
5305 weston_compositor_call_heads_changed, compositor);
5306}
5307
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005308/** Register a new head
5309 *
5310 * \param compositor The compositor.
5311 * \param head The head to register, must not be already registered.
5312 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005313 * This signals the core that a new head has become available, leading to
5314 * heads_changed hook being called later.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005315 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005316 * \ingroup compositor
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005317 * \internal
5318 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005319WL_EXPORT void
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005320weston_compositor_add_head(struct weston_compositor *compositor,
5321 struct weston_head *head)
5322{
5323 assert(wl_list_empty(&head->compositor_link));
5324 assert(head->name);
5325
5326 wl_list_insert(compositor->head_list.prev, &head->compositor_link);
5327 head->compositor = compositor;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005328 weston_compositor_schedule_heads_changed(compositor);
5329}
5330
5331/** Adds a listener to be called when heads change
5332 *
5333 * \param compositor The compositor.
5334 * \param listener The listener to add.
5335 *
Marius Vlada2dace22019-06-12 16:05:44 +03005336 * The listener notify function argument is weston_compositor.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005337 *
5338 * The listener function will be called after heads are added or their
5339 * connection status has changed. Several changes may be accumulated into a
5340 * single call. The user is expected to iterate over the existing heads and
5341 * check their statuses to find out what changed.
5342 *
5343 * \sa weston_compositor_iterate_heads, weston_head_is_connected,
5344 * weston_head_is_enabled
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005345 * \ingroup compositor
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005346 */
5347WL_EXPORT void
5348weston_compositor_add_heads_changed_listener(struct weston_compositor *compositor,
5349 struct wl_listener *listener)
5350{
5351 wl_signal_add(&compositor->heads_changed_signal, listener);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005352}
5353
5354/** Iterate over available heads
5355 *
5356 * \param compositor The compositor.
Marius Vlada2dace22019-06-12 16:05:44 +03005357 * \param iter The iterator, or NULL for start.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005358 * \return The next available head in the list.
5359 *
5360 * Returns all available heads, regardless of being connected or enabled.
5361 *
5362 * You can iterate over all heads as follows:
5363 * \code
5364 * struct weston_head *head = NULL;
5365 *
5366 * while ((head = weston_compositor_iterate_heads(compositor, head))) {
5367 * ...
5368 * }
5369 * \endcode
5370 *
5371 * If you cause \c iter to be removed from the list, you cannot use it to
5372 * continue iterating. Removing any other item is safe.
5373 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005374 * \ingroup compositor
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005375 */
5376WL_EXPORT struct weston_head *
5377weston_compositor_iterate_heads(struct weston_compositor *compositor,
5378 struct weston_head *iter)
5379{
5380 struct wl_list *list = &compositor->head_list;
5381 struct wl_list *node;
5382
5383 assert(compositor);
5384 assert(!iter || iter->compositor == compositor);
5385
5386 if (iter)
5387 node = iter->compositor_link.next;
5388 else
5389 node = list->next;
5390
5391 assert(node);
5392 assert(!iter || node != &iter->compositor_link);
5393
5394 if (node == list)
5395 return NULL;
5396
5397 return container_of(node, struct weston_head, compositor_link);
5398}
5399
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005400/** Iterate over attached heads
5401 *
5402 * \param output The output whose heads to iterate.
Marius Vlada2dace22019-06-12 16:05:44 +03005403 * \param iter The iterator, or NULL for start.
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005404 * \return The next attached head in the list.
5405 *
5406 * Returns all heads currently attached to the output.
5407 *
5408 * You can iterate over all heads as follows:
5409 * \code
5410 * struct weston_head *head = NULL;
5411 *
5412 * while ((head = weston_output_iterate_heads(output, head))) {
5413 * ...
5414 * }
5415 * \endcode
5416 *
5417 * If you cause \c iter to be removed from the list, you cannot use it to
5418 * continue iterating. Removing any other item is safe.
5419 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05005420 * \ingroup output
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005421 */
5422WL_EXPORT struct weston_head *
5423weston_output_iterate_heads(struct weston_output *output,
5424 struct weston_head *iter)
5425{
5426 struct wl_list *list = &output->head_list;
5427 struct wl_list *node;
5428
5429 assert(output);
5430 assert(!iter || iter->output == output);
5431
5432 if (iter)
5433 node = iter->output_link.next;
5434 else
5435 node = list->next;
5436
5437 assert(node);
5438 assert(!iter || node != &iter->output_link);
5439
5440 if (node == list)
5441 return NULL;
5442
5443 return container_of(node, struct weston_head, output_link);
5444}
5445
Pekka Paalanendcac3512017-12-08 14:13:34 +02005446/** Attach a head to an output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005447 *
5448 * \param output The output to attach to.
5449 * \param head The head that is not yet attached.
5450 * \return 0 on success, -1 on failure.
5451 *
5452 * Attaches the given head to the output. All heads of an output are clones
5453 * and share the resolution and timings.
5454 *
5455 * Cloning heads this way uses less resources than creating an output for
5456 * each head, but is not always possible due to environment, driver and hardware
5457 * limitations.
5458 *
5459 * On failure, the head remains unattached. Success of this function does not
5460 * guarantee the output configuration is actually valid. The final checks are
Pekka Paalanendcac3512017-12-08 14:13:34 +02005461 * made on weston_output_enable() unless the output was already enabled.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005462 *
Marius Vlad55d87362019-06-11 01:15:35 +03005463 * \ingroup output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005464 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005465WL_EXPORT int
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005466weston_output_attach_head(struct weston_output *output,
5467 struct weston_head *head)
5468{
Pekka Paalanendcac3512017-12-08 14:13:34 +02005469 char *head_names;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005470
5471 if (!wl_list_empty(&head->output_link))
5472 return -1;
5473
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005474 if (output->attach_head) {
5475 if (output->attach_head(output, head) < 0)
5476 return -1;
5477 } else if (!wl_list_empty(&output->head_list)) {
5478 /* No support for clones in the legacy path. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005479 return -1;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005480 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005481
5482 head->output = output;
5483 wl_list_insert(output->head_list.prev, &head->output_link);
5484
Pekka Paalanendcac3512017-12-08 14:13:34 +02005485 if (output->enabled) {
5486 weston_head_add_global(head);
5487
5488 head_names = weston_output_create_heads_string(output);
5489 weston_log("Output '%s' updated to have head(s) %s\n",
5490 output->name, head_names);
5491 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005492
5493 weston_output_emit_heads_changed(output);
Pekka Paalanendcac3512017-12-08 14:13:34 +02005494 }
5495
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005496 return 0;
5497}
5498
5499/** Detach a head from its output
5500 *
5501 * \param head The head to detach.
5502 *
5503 * It is safe to detach a non-attached head.
5504 *
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005505 * If the head is attached to an enabled output and the output will be left
5506 * with no heads, the output will be disabled.
5507 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005508 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005509 * \sa weston_output_disable
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005510 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005511WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005512weston_head_detach(struct weston_head *head)
5513{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005514 struct weston_output *output = head->output;
Pekka Paalanena0106992017-12-08 16:11:17 +02005515 char *head_names;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005516
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005517 wl_list_remove(&head->output_link);
5518 wl_list_init(&head->output_link);
5519 head->output = NULL;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005520
5521 if (!output)
5522 return;
5523
5524 if (output->detach_head)
5525 output->detach_head(output, head);
5526
5527 if (output->enabled) {
5528 weston_head_remove_global(head);
5529
Pekka Paalanena0106992017-12-08 16:11:17 +02005530 if (wl_list_empty(&output->head_list)) {
5531 weston_log("Output '%s' no heads left, disabling.\n",
5532 output->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005533 weston_output_disable(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02005534 } else {
5535 head_names = weston_output_create_heads_string(output);
5536 weston_log("Output '%s' updated to have head(s) %s\n",
5537 output->name, head_names);
5538 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005539
5540 weston_output_emit_heads_changed(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02005541 }
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005542 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005543}
5544
5545/** Destroy a head
5546 *
5547 * \param head The head to be released.
5548 *
5549 * Destroys the head. The caller is responsible for freeing the memory pointed
5550 * to by \c head.
5551 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005552 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005553 * \internal
5554 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005555WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005556weston_head_release(struct weston_head *head)
5557{
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005558 wl_signal_emit(&head->destroy_signal, head);
5559
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005560 weston_head_detach(head);
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005561
5562 free(head->make);
5563 free(head->model);
5564 free(head->serial_number);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005565 free(head->name);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005566
5567 wl_list_remove(&head->compositor_link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005568}
5569
Pekka Paalanene19970f2017-08-28 14:11:02 +03005570static void
5571weston_head_set_device_changed(struct weston_head *head)
5572{
5573 head->device_changed = true;
5574
5575 if (head->compositor)
5576 weston_compositor_schedule_heads_changed(head->compositor);
5577}
5578
5579/** String equal comparison with NULLs being equal */
5580static bool
5581str_null_eq(const char *a, const char *b)
5582{
5583 if (!a && !b)
5584 return true;
5585
5586 if (!!a != !!b)
5587 return false;
5588
5589 return strcmp(a, b) == 0;
5590}
5591
Pekka Paalanen01f60212017-03-24 15:39:24 +02005592/** Store monitor make, model and serial number
5593 *
5594 * \param head The head to modify.
5595 * \param make The monitor make. If EDID is available, the PNP ID. Otherwise
5596 * any string, or NULL for none.
5597 * \param model The monitor model or name, or a made-up string, or NULL for
5598 * none.
5599 * \param serialno The monitor serial number, a made-up string, or NULL for
5600 * none.
5601 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005602 * This may set the device_changed flag.
5603 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005604 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005605 * \internal
5606 */
5607WL_EXPORT void
5608weston_head_set_monitor_strings(struct weston_head *head,
5609 const char *make,
5610 const char *model,
5611 const char *serialno)
5612{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005613 if (str_null_eq(head->make, make) &&
5614 str_null_eq(head->model, model) &&
5615 str_null_eq(head->serial_number, serialno))
5616 return;
5617
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005618 free(head->make);
5619 free(head->model);
5620 free(head->serial_number);
5621
5622 head->make = make ? strdup(make) : NULL;
5623 head->model = model ? strdup(model) : NULL;
5624 head->serial_number = serialno ? strdup(serialno) : NULL;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005625
5626 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005627}
5628
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005629/** Store display non-desktop status
5630 *
5631 * \param head The head to modify.
5632 * \param non_desktop Whether the head connects to a non-desktop display.
5633 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005634 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005635 * \internal
5636 */
5637WL_EXPORT void
5638weston_head_set_non_desktop(struct weston_head *head, bool non_desktop)
5639{
5640 if (head->non_desktop == non_desktop)
5641 return;
5642
5643 head->non_desktop = non_desktop;
5644
5645 weston_head_set_device_changed(head);
5646}
5647
Lucas Stacha69cb712019-11-25 23:29:31 +00005648/** Store display transformation
5649 *
5650 * \param head The head to modify.
5651 * \param transform The transformation to apply for this head
5652 *
5653 * This may set the device_changed flag.
5654 *
5655 * \ingroup head
5656 * \internal
5657 */
5658WL_EXPORT void
5659weston_head_set_transform(struct weston_head *head, uint32_t transform)
5660{
5661 if (head->transform == transform)
5662 return;
5663
5664 head->transform = transform;
5665
5666 weston_head_set_device_changed(head);
5667}
5668
5669
Pekka Paalanen01f60212017-03-24 15:39:24 +02005670/** Store physical image size
5671 *
5672 * \param head The head to modify.
5673 * \param mm_width Image area width in millimeters.
5674 * \param mm_height Image area height in millimeters.
5675 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005676 * This may set the device_changed flag.
5677 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005678 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005679 * \internal
5680 */
5681WL_EXPORT void
5682weston_head_set_physical_size(struct weston_head *head,
5683 int32_t mm_width, int32_t mm_height)
5684{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005685 if (head->mm_width == mm_width &&
5686 head->mm_height == mm_height)
5687 return;
5688
Pekka Paalanen01f60212017-03-24 15:39:24 +02005689 head->mm_width = mm_width;
5690 head->mm_height = mm_height;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005691
5692 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005693}
5694
5695/** Store monitor sub-pixel layout
5696 *
5697 * \param head The head to modify.
5698 * \param sp Sub-pixel layout. The possible values are:
5699 * - WL_OUTPUT_SUBPIXEL_UNKNOWN,
5700 * - WL_OUTPUT_SUBPIXEL_NONE,
5701 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB,
5702 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR,
5703 * - WL_OUTPUT_SUBPIXEL_VERTICAL_RGB,
5704 * - WL_OUTPUT_SUBPIXEL_VERTICAL_BGR
5705 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005706 * This may set the device_changed flag.
5707 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005708 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005709 * \internal
5710 */
5711WL_EXPORT void
5712weston_head_set_subpixel(struct weston_head *head,
5713 enum wl_output_subpixel sp)
5714{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005715 if (head->subpixel == sp)
5716 return;
5717
Pekka Paalanen01f60212017-03-24 15:39:24 +02005718 head->subpixel = sp;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005719
5720 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005721}
5722
5723/** Mark the monitor as internal
5724 *
5725 * This is used for embedded screens, like laptop panels.
5726 *
5727 * \param head The head to mark as internal.
5728 *
5729 * By default a head is external. The type is often inferred from the physical
5730 * connector type.
5731 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005732 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005733 * \internal
5734 */
5735WL_EXPORT void
5736weston_head_set_internal(struct weston_head *head)
5737{
5738 head->connection_internal = true;
5739}
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005740
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005741/** Store connector status
5742 *
5743 * \param head The head to modify.
5744 * \param connected Whether the head is connected.
5745 *
5746 * Connectors are created as disconnected. This function can be used to
5747 * set the connector status.
5748 *
5749 * The status should be set to true when a physical connector is connected to
5750 * a video sink device like a monitor and to false when the connector is
5751 * disconnected. For nested backends, the connection status should reflect the
5752 * connection to the parent display server.
5753 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005754 * When the connection status changes, it schedules a call to the heads_changed
Pekka Paalanene19970f2017-08-28 14:11:02 +03005755 * hook and sets the device_changed flag.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005756 *
5757 * \sa weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005758 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005759 * \internal
5760 */
5761WL_EXPORT void
5762weston_head_set_connection_status(struct weston_head *head, bool connected)
5763{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005764 if (head->connected == connected)
5765 return;
5766
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005767 head->connected = connected;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005768
Pekka Paalanene19970f2017-08-28 14:11:02 +03005769 weston_head_set_device_changed(head);
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005770}
5771
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305772static void
5773weston_output_compute_protection(struct weston_output *output)
5774{
5775 struct weston_head *head;
5776 enum weston_hdcp_protection op_protection;
5777 bool op_protection_valid = false;
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305778 struct weston_compositor *wc = output->compositor;
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305779
5780 wl_list_for_each(head, &output->head_list, output_link) {
5781 if (!op_protection_valid) {
5782 op_protection = head->current_protection;
5783 op_protection_valid = true;
5784 }
5785 if (head->current_protection < op_protection)
5786 op_protection = head->current_protection;
5787 }
5788
5789 if (!op_protection_valid)
5790 op_protection = WESTON_HDCP_DISABLE;
5791
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305792 if (output->current_protection != op_protection) {
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305793 output->current_protection = op_protection;
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305794 weston_output_damage(output);
Ankit Nautiyalfaa5ab42019-07-08 13:23:24 +05305795 weston_schedule_surface_protection_update(wc);
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305796 }
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305797}
5798
5799WL_EXPORT void
5800weston_head_set_content_protection_status(struct weston_head *head,
5801 enum weston_hdcp_protection status)
5802{
5803 head->current_protection = status;
5804 if (head->output)
5805 weston_output_compute_protection(head->output);
5806}
5807
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005808/** Is the head currently connected?
5809 *
5810 * \param head The head to query.
5811 * \return Connection status.
5812 *
5813 * Returns true if the head is physically connected to a monitor, or in
5814 * case of a nested backend returns true when there is a connection to the
5815 * parent display server.
5816 *
5817 * This is independent from the head being enabled.
5818 *
5819 * \sa weston_head_is_enabled
Marius Vlad78984ee2019-06-11 00:05:08 +03005820 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005821 */
5822WL_EXPORT bool
5823weston_head_is_connected(struct weston_head *head)
5824{
5825 return head->connected;
5826}
5827
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005828/** Is the head currently enabled?
5829 *
5830 * \param head The head to query.
5831 * \return Video status.
5832 *
5833 * Returns true if the head is currently transmitting a video stream.
5834 *
5835 * This is independent of the head being connected.
5836 *
5837 * \sa weston_head_is_connected
Marius Vlad78984ee2019-06-11 00:05:08 +03005838 * \ingroup head
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005839 */
5840WL_EXPORT bool
5841weston_head_is_enabled(struct weston_head *head)
5842{
5843 if (!head->output)
5844 return false;
5845
5846 return head->output->enabled;
5847}
5848
Pekka Paalanene19970f2017-08-28 14:11:02 +03005849/** Has the device information changed?
5850 *
5851 * \param head The head to query.
5852 * \return True if the device information has changed since last reset.
5853 *
5854 * The information about the connected display device, e.g. a monitor, may
5855 * change without being disconnected in between. Changing information
5856 * causes a call to the heads_changed hook.
5857 *
5858 * The information includes make, model, serial number, physical size,
5859 * and sub-pixel type. The connection status is also included.
5860 *
5861 * \sa weston_head_reset_device_changed, weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005862 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03005863 */
5864WL_EXPORT bool
5865weston_head_is_device_changed(struct weston_head *head)
5866{
5867 return head->device_changed;
5868}
5869
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005870/** Does the head represent a non-desktop display?
5871 *
5872 * \param head The head to query.
5873 * \return True if the device is a non-desktop display.
5874 *
5875 * Non-desktop heads are not attached to outputs by default.
5876 * This stops weston from extending the desktop onto head mounted displays.
5877 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005878 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005879 */
5880WL_EXPORT bool
5881weston_head_is_non_desktop(struct weston_head *head)
5882{
5883 return head->non_desktop;
5884}
5885
Pekka Paalanene19970f2017-08-28 14:11:02 +03005886/** Acknowledge device information change
5887 *
5888 * \param head The head to acknowledge.
5889 *
5890 * Clears the device changed flag on this head. When a compositor has processed
5891 * device information, it should call this to be able to notice further
5892 * changes.
5893 *
5894 * \sa weston_head_is_device_changed
Marius Vlad78984ee2019-06-11 00:05:08 +03005895 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03005896 */
5897WL_EXPORT void
5898weston_head_reset_device_changed(struct weston_head *head)
5899{
5900 head->device_changed = false;
5901}
5902
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005903/** Get the name of a head
5904 *
5905 * \param head The head to query.
5906 * \return The head's name, not NULL.
5907 *
5908 * The name depends on the backend. The DRM backend uses connector names,
5909 * other backends may use hardcoded names or user-given names.
Marius Vlad78984ee2019-06-11 00:05:08 +03005910 *
5911 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005912 */
5913WL_EXPORT const char *
5914weston_head_get_name(struct weston_head *head)
5915{
5916 return head->name;
5917}
5918
5919/** Get the output the head is attached to
5920 *
5921 * \param head The head to query.
5922 * \return The output the head is attached to, or NULL if detached.
Marius Vlad78984ee2019-06-11 00:05:08 +03005923 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005924 */
5925WL_EXPORT struct weston_output *
5926weston_head_get_output(struct weston_head *head)
5927{
5928 return head->output;
5929}
5930
Lucas Stacha69cb712019-11-25 23:29:31 +00005931/** Get the head's native transformation
5932 *
5933 * \param head The head to query.
5934 * \return The head's native transform, as a WL_OUTPUT_TRANSFORM_* value
5935 *
5936 * A weston_head may have a 'native' transform provided by the backend.
5937 * Examples include panels which are physically rotated, where the rotation
5938 * is recorded and described as part of the system configuration. This call
5939 * will return any known native transform for the head.
5940 *
5941 * \ingroup head
5942 */
5943WL_EXPORT uint32_t
5944weston_head_get_transform(struct weston_head *head)
5945{
5946 return head->transform;
5947}
5948
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005949/** Add destroy callback for a head
5950 *
5951 * \param head The head to watch for.
5952 * \param listener The listener to add. The \c notify member must be set.
5953 *
5954 * Heads may get destroyed for various reasons by the backends. If a head is
5955 * attached to an output, the compositor should listen for head destruction
5956 * and reconfigure or destroy the output if necessary.
5957 *
5958 * The destroy callbacks will be called on weston_head destruction before any
5959 * automatic detaching from an associated weston_output and before any
5960 * weston_head information is lost.
5961 *
5962 * The \c data argument to the notify callback is the weston_head being
5963 * destroyed.
Marius Vlad78984ee2019-06-11 00:05:08 +03005964 *
5965 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005966 */
5967WL_EXPORT void
5968weston_head_add_destroy_listener(struct weston_head *head,
5969 struct wl_listener *listener)
5970{
5971 wl_signal_add(&head->destroy_signal, listener);
5972}
5973
5974/** Look up destroy listener for a head
5975 *
5976 * \param head The head to query.
5977 * \param notify The notify function used used for the added destroy listener.
5978 * \return The listener, or NULL if not found.
5979 *
5980 * This looks up the previously added destroy listener struct based on the
5981 * notify function it has. The listener can be used to access user data
5982 * through \c container_of().
5983 *
5984 * \sa wl_signal_get()
Marius Vlad78984ee2019-06-11 00:05:08 +03005985 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005986 */
5987WL_EXPORT struct wl_listener *
5988weston_head_get_destroy_listener(struct weston_head *head,
5989 wl_notify_func_t notify)
5990{
5991 return wl_signal_get(&head->destroy_signal, notify);
5992}
5993
David Fort0de859e2016-05-27 23:22:57 +02005994/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005995static void
David Fort0de859e2016-05-27 23:22:57 +02005996weston_compositor_reflow_outputs(struct weston_compositor *compositor,
5997 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005998{
5999 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02006000 bool start_resizing = false;
6001
6002 if (!delta_width)
6003 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02006004
6005 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02006006 if (output == resized_output) {
6007 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02006008 continue;
6009 }
6010
David Fort0de859e2016-05-27 23:22:57 +02006011 if (start_resizing) {
6012 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02006013 output->dirty = 1;
6014 }
6015 }
6016}
6017
Pekka Paalanen06920792020-12-08 15:22:36 +02006018/** Transform a region in-place from global to output coordinates
6019 *
6020 * \param output The output that defines the transformation.
6021 * \param region The region to be transformed in-place.
6022 *
6023 * This takes a region in the global coordinate system, and takes into account
6024 * output position, transform and scale, and the zoom, and converts the region
6025 * into output pixel coordinates in the framebuffer.
6026 *
6027 * Uses floating-point operations if zoom is active, which may round to expand
6028 * the region.
6029 *
6030 * \internal
6031 * \ingroup output
6032 */
6033WL_EXPORT void
6034weston_output_region_from_global(struct weston_output *output,
6035 pixman_region32_t *region)
6036{
6037 if (output->zoom.active) {
6038 weston_matrix_transform_region(region, &output->matrix, region);
6039 } else {
6040 pixman_region32_translate(region, -output->x, -output->y);
6041 weston_transformed_region(output->width, output->height,
6042 output->transform,
6043 output->current_scale,
6044 region, region);
6045 }
6046}
6047
Pekka Paalanend72bad22017-03-29 17:01:41 +03006048static void
Scott Moreauccbf29d2012-02-22 14:21:41 -07006049weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01006050{
Scott Moreau850ca422012-05-21 15:21:25 -06006051 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05006052
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006053 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05006054 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06006055
Scott Moreauccbf29d2012-02-22 14:21:41 -07006056 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06006057 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05006058 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01006059 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05006060 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01006061 weston_matrix_scale(&output->matrix, magnification,
6062 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07006063 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04006064
Jason Ekstrandfb23df72014-10-16 10:55:21 -05006065 switch (output->transform) {
6066 case WL_OUTPUT_TRANSFORM_FLIPPED:
6067 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
6068 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
6069 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
6070 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
6071 weston_matrix_scale(&output->matrix, -1, 1, 1);
6072 break;
6073 }
6074
6075 switch (output->transform) {
6076 default:
6077 case WL_OUTPUT_TRANSFORM_NORMAL:
6078 case WL_OUTPUT_TRANSFORM_FLIPPED:
6079 break;
6080 case WL_OUTPUT_TRANSFORM_90:
6081 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +02006082 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
6083 weston_matrix_rotate_xy(&output->matrix, 0, -1);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05006084 break;
6085 case WL_OUTPUT_TRANSFORM_180:
6086 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
6087 weston_matrix_translate(&output->matrix,
6088 -output->width, -output->height, 0);
6089 weston_matrix_rotate_xy(&output->matrix, -1, 0);
6090 break;
6091 case WL_OUTPUT_TRANSFORM_270:
6092 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +02006093 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
6094 weston_matrix_rotate_xy(&output->matrix, 0, 1);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05006095 break;
6096 }
6097
6098 if (output->current_scale != 1)
6099 weston_matrix_scale(&output->matrix,
6100 output->current_scale,
6101 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01006102
Scott Moreauccbf29d2012-02-22 14:21:41 -07006103 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05006104
6105 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07006106}
6107
Scott Moreau1bad5db2012-08-18 01:04:05 -06006108static void
Alexander Larsson0b135062013-05-28 16:23:36 +02006109weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06006110{
6111 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03006112 output->native_scale = scale;
6113 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06006114
Pekka Paalanen59987fa2016-04-26 15:50:59 +03006115 convert_size_by_transform_scale(&output->width, &output->height,
6116 output->current_mode->width,
6117 output->current_mode->height,
6118 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02006119}
6120
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006121static void
6122weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07006123{
6124 output->x = x;
6125 output->y = y;
6126
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006127 pixman_region32_fini(&output->region);
Scott Moreauccbf29d2012-02-22 14:21:41 -07006128 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06006129 output->width,
6130 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01006131}
6132
Marius Vlad55d87362019-06-11 01:15:35 +03006133/**
6134 * \ingroup output
6135 */
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006136WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006137weston_output_move(struct weston_output *output, int x, int y)
6138{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006139 struct weston_head *head;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006140 struct wl_resource *resource;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006141 int ver;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006142
6143 output->move_x = x - output->x;
6144 output->move_y = y - output->y;
6145
6146 if (output->move_x == 0 && output->move_y == 0)
6147 return;
6148
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006149 weston_output_init_geometry(output, x, y);
6150
6151 output->dirty = 1;
6152
6153 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006154 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006155
6156 /* Notify clients of the change for output position. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006157 wl_list_for_each(head, &output->head_list, output_link) {
6158 wl_resource_for_each(resource, &head->resource_list) {
6159 wl_output_send_geometry(resource,
6160 output->x,
6161 output->y,
6162 head->mm_width,
6163 head->mm_height,
6164 head->subpixel,
6165 head->make,
6166 head->model,
6167 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08006168
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006169 ver = wl_resource_get_version(resource);
6170 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
6171 wl_output_send_done(resource);
6172 }
Roman Gilge97391c2019-03-29 13:01:06 +01006173
6174 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
6175 zxdg_output_v1_send_logical_position(resource,
6176 output->x,
6177 output->y);
6178 zxdg_output_v1_send_done(resource);
6179 }
Quanxian Wangb2c86362014-03-14 09:16:25 +08006180 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006181}
6182
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006183/** Signal that a pending output is taken into use.
6184 *
6185 * Removes the output from the pending list and adds it to the compositor's
6186 * list of enabled outputs. The output created signal is emitted.
Giulio Camuffob1147152015-05-06 21:41:57 +03006187 *
Pekka Paalanen2210ad02017-03-30 15:48:06 +03006188 * The output gets an internal ID assigned, and the wl_output global is
6189 * created.
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03006190 *
Giulio Camuffob1147152015-05-06 21:41:57 +03006191 * \param compositor The compositor instance.
6192 * \param output The output to be added.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006193 *
6194 * \internal
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006195 * \ingroup compositor
Giulio Camuffob1147152015-05-06 21:41:57 +03006196 */
Pekka Paalanenf9681b52017-03-29 16:58:48 +03006197static void
Giulio Camuffob1147152015-05-06 21:41:57 +03006198weston_compositor_add_output(struct weston_compositor *compositor,
6199 struct weston_output *output)
6200{
Armin Krezoviće5403842016-08-05 15:28:29 +02006201 struct weston_view *view, *next;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03006202 struct weston_head *head;
Armin Krezoviće5403842016-08-05 15:28:29 +02006203
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006204 assert(!output->enabled);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03006205
6206 /* Verify we haven't reached the limit of 32 available output IDs */
6207 assert(ffs(~compositor->output_id_pool) > 0);
6208
6209 /* Invert the output id pool and look for the lowest numbered
6210 * switch (the least significant bit). Take that bit's position
6211 * as our ID, and mark it used in the compositor's output_id_pool.
6212 */
6213 output->id = ffs(~compositor->output_id_pool) - 1;
6214 compositor->output_id_pool |= 1u << output->id;
6215
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006216 wl_list_remove(&output->link);
Giulio Camuffob1147152015-05-06 21:41:57 +03006217 wl_list_insert(compositor->output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006218 output->enabled = true;
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006219
Pekka Paalanendcac3512017-12-08 14:13:34 +02006220 wl_list_for_each(head, &output->head_list, output_link)
6221 weston_head_add_global(head);
Pekka Paalanen2210ad02017-03-30 15:48:06 +03006222
Giulio Camuffob1147152015-05-06 21:41:57 +03006223 wl_signal_emit(&compositor->output_created_signal, output);
Armin Krezoviće5403842016-08-05 15:28:29 +02006224
Pekka Paalanen6551c092021-05-03 16:09:45 +03006225 /*
6226 * Use view_list, as paint nodes have not been created for this
6227 * output yet. Any existing view might touch this new output.
6228 */
Armin Krezoviće5403842016-08-05 15:28:29 +02006229 wl_list_for_each_safe(view, next, &compositor->view_list, link)
6230 weston_view_geometry_dirty(view);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04006231}
6232
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006233/** Transform device coordinates into global coordinates
6234 *
Marius Vlada2dace22019-06-12 16:05:44 +03006235 * \param output the weston_output object
6236 * \param[in] device_x X coordinate in device units.
6237 * \param[in] device_y Y coordinate in device units.
6238 * \param[out] x X coordinate in the global space.
6239 * \param[out] y Y coordinate in the global space.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006240 *
Marius Vlada2dace22019-06-12 16:05:44 +03006241 * Transforms coordinates from the device coordinate space (physical pixel
6242 * units) to the global coordinate space (logical pixel units). This takes
6243 * into account output transform and scale.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006244 *
Marius Vlad55d87362019-06-11 01:15:35 +03006245 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006246 * \internal
6247 */
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006248WL_EXPORT void
6249weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02006250 double device_x, double device_y,
6251 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006252{
Derek Foreman0f679412014-10-02 13:41:17 -05006253 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02006254 device_x,
6255 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05006256 0.0,
6257 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006258
Derek Foreman67a18b92015-03-24 11:36:14 -05006259 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006260
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02006261 *x = p.f[0] / p.f[3];
6262 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006263}
6264
Pekka Paalanen1d2eee22021-02-25 12:03:28 +02006265static void
6266weston_output_reset_color_transforms(struct weston_output *output)
6267{
Pekka Paalanencda39512021-03-22 17:32:07 +02006268 weston_color_transform_unref(output->from_sRGB_to_output);
6269 output->from_sRGB_to_output = NULL;
Pekka Paalanen8fb23ed2021-03-23 13:40:43 +02006270 weston_color_transform_unref(output->from_sRGB_to_blend);
6271 output->from_sRGB_to_blend = NULL;
Pekka Paalanen1d2eee22021-02-25 12:03:28 +02006272 weston_color_transform_unref(output->from_blend_to_output);
6273 output->from_blend_to_output = NULL;
6274}
6275
Pekka Paalanen9a0aa372021-06-08 13:38:35 +03006276static bool
6277weston_output_set_color_transforms(struct weston_output *output)
6278{
6279 struct weston_color_manager *cm = output->compositor->color_manager;
6280 struct weston_color_transform *blend_to_output = NULL;
6281 struct weston_color_transform *sRGB_to_output = NULL;
6282 struct weston_color_transform *sRGB_to_blend = NULL;
6283 bool ok;
6284
6285 ok = cm->get_output_color_transform(cm, output, &blend_to_output);
6286 ok = ok && cm->get_sRGB_to_output_color_transform(cm, output,
6287 &sRGB_to_output);
6288 ok = ok && cm->get_sRGB_to_blend_color_transform(cm, output,
6289 &sRGB_to_blend);
6290 if (!ok) {
6291 weston_log("Creating color transformation for output \"%s\" failed.\n",
6292 output->name);
6293 weston_color_transform_unref(blend_to_output);
6294 weston_color_transform_unref(sRGB_to_output);
6295 weston_color_transform_unref(sRGB_to_blend);
6296
6297 return false;
6298 }
6299
6300 weston_output_reset_color_transforms(output);
6301 output->from_blend_to_output = blend_to_output;
6302 output->from_blend_to_output_by_backend = false;
6303 output->from_sRGB_to_output = sRGB_to_output;
6304 output->from_sRGB_to_blend = sRGB_to_blend;
6305
6306 return true;
6307}
6308
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006309/** Removes output from compositor's list of enabled outputs
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006310 *
6311 * \param output The weston_output object that is being removed.
6312 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006313 * The following happens:
6314 *
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03006315 * - Destroys all paint nodes related to the output.
6316 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006317 * - The output assignments of all views in the current scenegraph are
6318 * recomputed.
6319 *
Pekka Paalanen1d2eee22021-02-25 12:03:28 +02006320 * - Destroys output's color transforms.
6321 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006322 * - Presentation feedback is discarded.
6323 *
6324 * - Compositor is notified that outputs were changed and
6325 * applies the necessary changes to re-layout outputs.
6326 *
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006327 * - The output is put back in the pending outputs list.
6328 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006329 * - Signal is emitted to notify all users of the weston_output
6330 * object that the output is being destroyed.
6331 *
6332 * - wl_output protocol objects referencing this weston_output
Pekka Paalanen2210ad02017-03-30 15:48:06 +03006333 * are made inert, and the wl_output global is removed.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006334 *
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03006335 * - The output's internal ID is released.
6336 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006337 * \ingroup compositor
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006338 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006339 */
6340static void
6341weston_compositor_remove_output(struct weston_output *output)
6342{
Pekka Paalanenbccda712017-03-29 16:16:04 +03006343 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03006344 struct weston_paint_node *pnode, *pntmp;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006345 struct weston_view *view;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03006346 struct weston_head *head;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006347
6348 assert(output->destroying);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006349 assert(output->enabled);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006350
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03006351 wl_list_for_each_safe(pnode, pntmp,
6352 &output->paint_node_list, output_link) {
6353 weston_paint_node_destroy(pnode);
6354 }
Pekka Paalanen2fddc532021-04-30 17:41:29 +03006355 assert(wl_list_empty(&output->paint_node_z_order_list));
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03006356
Pekka Paalanen6551c092021-05-03 16:09:45 +03006357 /*
6358 * Use view_list in case the output did not go through repaint
6359 * after a view came on it, lacking a paint node. Just to be sure.
6360 */
Pekka Paalanenbccda712017-03-29 16:16:04 +03006361 wl_list_for_each(view, &compositor->view_list, link) {
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006362 if (view->output_mask & (1u << output->id))
6363 weston_view_assign_output(view);
6364 }
6365
Pekka Paalanen1d2eee22021-02-25 12:03:28 +02006366 weston_output_reset_color_transforms(output);
6367
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006368 weston_presentation_feedback_discard_list(&output->feedback_list);
6369
Pekka Paalanen9711fd92018-06-21 14:26:18 +03006370 weston_compositor_reflow_outputs(compositor, output, -output->width);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006371
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006372 wl_list_remove(&output->link);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006373 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006374 output->enabled = false;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006375
Pekka Paalanenbccda712017-03-29 16:16:04 +03006376 wl_signal_emit(&compositor->output_destroyed_signal, output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006377 wl_signal_emit(&output->destroy_signal, output);
6378
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02006379 wl_list_for_each(head, &output->head_list, output_link)
6380 weston_head_remove_global(head);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03006381
6382 compositor->output_id_pool &= ~(1u << output->id);
6383 output->id = 0xffffffff; /* invalid */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006384}
6385
6386/** Sets the output scale for a given output.
6387 *
6388 * \param output The weston_output object that the scale is set for.
6389 * \param scale Scale factor for the given output.
6390 *
6391 * It only supports setting scale for an output that
6392 * is not enabled and it can only be ran once.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006393 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05006394 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006395 */
6396WL_EXPORT void
6397weston_output_set_scale(struct weston_output *output,
6398 int32_t scale)
6399{
6400 /* We can only set scale on a disabled output */
6401 assert(!output->enabled);
6402
6403 /* We only want to set scale once */
6404 assert(!output->scale);
6405
6406 output->scale = scale;
6407}
6408
6409/** Sets the output transform for a given output.
6410 *
6411 * \param output The weston_output object that the transform is set for.
6412 * \param transform Transform value for the given output.
6413 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006414 * Refer to wl_output::transform section located at
6415 * https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_output
6416 * for list of values that can be passed to this function.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006417 *
Marius Vlad55d87362019-06-11 01:15:35 +03006418 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006419 */
6420WL_EXPORT void
6421weston_output_set_transform(struct weston_output *output,
6422 uint32_t transform)
6423{
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006424 struct weston_pointer_motion_event ev;
6425 struct wl_resource *resource;
6426 struct weston_seat *seat;
6427 pixman_region32_t old_region;
6428 int mid_x, mid_y;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006429 struct weston_head *head;
6430 int ver;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006431
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006432 if (!output->enabled && output->transform == UINT32_MAX) {
6433 output->transform = transform;
6434 return;
6435 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006436
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006437 weston_output_transform_scale_init(output, transform, output->scale);
6438
6439 pixman_region32_init(&old_region);
6440 pixman_region32_copy(&old_region, &output->region);
6441
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006442 weston_output_init_geometry(output, output->x, output->y);
6443
6444 output->dirty = 1;
6445
6446 /* Notify clients of the change for output transform. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006447 wl_list_for_each(head, &output->head_list, output_link) {
6448 wl_resource_for_each(resource, &head->resource_list) {
6449 wl_output_send_geometry(resource,
6450 output->x,
6451 output->y,
6452 head->mm_width,
6453 head->mm_height,
6454 head->subpixel,
6455 head->make,
6456 head->model,
6457 output->transform);
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006458
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006459 ver = wl_resource_get_version(resource);
6460 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
6461 wl_output_send_done(resource);
6462 }
Roman Gilge97391c2019-03-29 13:01:06 +01006463 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
6464 zxdg_output_v1_send_logical_position(resource,
6465 output->x,
6466 output->y);
6467 zxdg_output_v1_send_logical_size(resource,
6468 output->width,
6469 output->height);
6470 zxdg_output_v1_send_done(resource);
6471 }
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006472 }
6473
6474 /* we must ensure that pointers are inside output, otherwise they disappear */
6475 mid_x = output->x + output->width / 2;
6476 mid_y = output->y + output->height / 2;
6477
6478 ev.mask = WESTON_POINTER_MOTION_ABS;
6479 ev.x = wl_fixed_to_double(wl_fixed_from_int(mid_x));
6480 ev.y = wl_fixed_to_double(wl_fixed_from_int(mid_y));
6481
6482 wl_list_for_each(seat, &output->compositor->seat_list, link) {
6483 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
6484
6485 if (pointer && pixman_region32_contains_point(&old_region,
6486 wl_fixed_to_int(pointer->x),
6487 wl_fixed_to_int(pointer->y),
6488 NULL))
6489 weston_pointer_move(pointer, &ev);
6490 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006491}
6492
6493/** Initializes a weston_output object with enough data so
6494 ** an output can be configured.
6495 *
6496 * \param output The weston_output object to initialize
6497 * \param compositor The compositor instance.
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006498 * \param name Name for the output (the string is copied).
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006499 *
6500 * Sets initial values for fields that are expected to be
6501 * configured either by compositors or backends.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006502 *
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006503 * The name is used in logs, and can be used by compositors as a configuration
6504 * identifier.
6505 *
Marius Vlad55d87362019-06-11 01:15:35 +03006506 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006507 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006508 */
6509WL_EXPORT void
Armin Krezović40087402016-09-30 14:11:12 +02006510weston_output_init(struct weston_output *output,
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006511 struct weston_compositor *compositor,
6512 const char *name)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006513{
6514 output->compositor = compositor;
6515 output->destroying = 0;
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006516 output->name = strdup(name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006517 wl_list_init(&output->link);
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006518 wl_signal_init(&output->user_destroy_signal);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006519 output->enabled = false;
Ankit Nautiyal2690a772019-03-25 17:57:59 +05306520 output->desired_protection = WESTON_HDCP_DISABLE;
Ankit Nautiyal2844f8e2019-04-03 10:14:59 +05306521 output->allow_protection = true;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006522
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006523 wl_list_init(&output->head_list);
6524
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006525 /* Add some (in)sane defaults which can be used
6526 * for checking if an output was properly configured
6527 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006528 output->scale = 0;
6529 /* Can't use -1 on uint32_t and 0 is valid enum value */
6530 output->transform = UINT32_MAX;
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006531
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006532 pixman_region32_init(&output->region);
Pekka Paalanen42704142017-09-06 16:47:52 +03006533 wl_list_init(&output->mode_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006534}
6535
6536/** Adds weston_output object to pending output list.
6537 *
6538 * \param output The weston_output object to add
6539 * \param compositor The compositor instance.
6540 *
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03006541 * The opposite of this operation is built into weston_output_release().
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006542 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006543 * \ingroup compositor
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006544 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006545 */
6546WL_EXPORT void
6547weston_compositor_add_pending_output(struct weston_output *output,
6548 struct weston_compositor *compositor)
6549{
Pekka Paalanene952a012017-03-29 17:14:00 +03006550 assert(output->disable);
6551 assert(output->enable);
6552
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006553 wl_list_remove(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006554 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006555}
6556
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006557/** Create a string with the attached heads' names.
6558 *
6559 * The string must be free()'d.
Marius Vlad55d87362019-06-11 01:15:35 +03006560 *
6561 * \ingroup output
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006562 */
6563static char *
6564weston_output_create_heads_string(struct weston_output *output)
6565{
6566 FILE *fp;
6567 char *str = NULL;
6568 size_t size = 0;
6569 struct weston_head *head;
6570 const char *sep = "";
6571
6572 fp = open_memstream(&str, &size);
6573 if (!fp)
6574 return NULL;
6575
6576 wl_list_for_each(head, &output->head_list, output_link) {
6577 fprintf(fp, "%s%s", sep, head->name);
6578 sep = ", ";
6579 }
6580 fclose(fp);
6581
6582 return str;
6583}
6584
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006585/** Constructs a weston_output object that can be used by the compositor.
6586 *
Pekka Paalanencc201e42017-03-30 15:11:25 +03006587 * \param output The weston_output object that needs to be enabled. Must not
Pekka Paalanenddce54d2017-08-23 16:00:21 +03006588 * be enabled already. Must have at least one head attached.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006589 *
6590 * Output coordinates are calculated and each new output is by default
6591 * assigned to the right of previous one.
6592 *
6593 * Sets up the transformation, zoom, and geometry of the output using
6594 * the properties that need to be configured by the compositor.
6595 *
6596 * Establishes a repaint timer for the output with the relevant display
6597 * object's event loop. See output_repaint_timer_handler().
6598 *
6599 * The output is assigned an ID. Weston can support up to 32 distinct
6600 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
6601 * is referred to and used to find the first available ID number, and
6602 * then this ID is marked as used in output_id_pool.
6603 *
6604 * The output is also assigned a Wayland global with the wl_output
6605 * external interface.
6606 *
6607 * Backend specific function is called to set up the output output.
6608 *
6609 * Output is added to the compositor's output list
6610 *
6611 * If the backend specific function fails, the weston_output object
6612 * is returned to a state it was before calling this function and
6613 * is added to the compositor's pending_output_list in case it needs
6614 * to be reconfigured or just so it can be destroyed at shutdown.
6615 *
6616 * 0 is returned on success, -1 on failure.
Marius Vlad55d87362019-06-11 01:15:35 +03006617 *
6618 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006619 */
6620WL_EXPORT int
6621weston_output_enable(struct weston_output *output)
6622{
Armin Krezović782f5df2016-09-30 14:11:11 +02006623 struct weston_compositor *c = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006624 struct weston_output *iterator;
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03006625 struct weston_head *head;
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006626 char *head_names;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006627 int x = 0, y = 0;
6628
Pekka Paalanencc201e42017-03-30 15:11:25 +03006629 if (output->enabled) {
6630 weston_log("Error: attempt to enable an enabled output '%s'\n",
6631 output->name);
6632 return -1;
6633 }
6634
Pekka Paalanenddce54d2017-08-23 16:00:21 +03006635 if (wl_list_empty(&output->head_list)) {
6636 weston_log("Error: cannot enable output '%s' without heads.\n",
6637 output->name);
6638 return -1;
6639 }
6640
Pekka Paalanen586e1ac2017-09-14 16:17:59 +03006641 if (wl_list_empty(&output->mode_list) || !output->current_mode) {
6642 weston_log("Error: no video mode for output '%s'.\n",
6643 output->name);
6644 return -1;
6645 }
6646
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03006647 wl_list_for_each(head, &output->head_list, output_link) {
6648 assert(head->make);
6649 assert(head->model);
6650 }
6651
Armin Krezović782f5df2016-09-30 14:11:11 +02006652 iterator = container_of(c->output_list.prev,
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006653 struct weston_output, link);
6654
Armin Krezović782f5df2016-09-30 14:11:11 +02006655 if (!wl_list_empty(&c->output_list))
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006656 x = iterator->x + iterator->width;
6657
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006658 /* Make sure the scale is set up */
6659 assert(output->scale);
6660
6661 /* Make sure we have a transform set */
6662 assert(output->transform != UINT32_MAX);
6663
Armin Krezović782f5df2016-09-30 14:11:11 +02006664 output->x = x;
6665 output->y = y;
6666 output->dirty = 1;
6667 output->original_scale = output->scale;
6668
Marius Vlad3a2f8292019-11-04 17:53:46 +02006669 wl_signal_init(&output->frame_signal);
6670 wl_signal_init(&output->destroy_signal);
6671
Armin Krezović782f5df2016-09-30 14:11:11 +02006672 weston_output_transform_scale_init(output, output->transform, output->scale);
6673 weston_output_init_zoom(output);
6674
6675 weston_output_init_geometry(output, x, y);
6676 weston_output_damage(output);
6677
Armin Krezović782f5df2016-09-30 14:11:11 +02006678 wl_list_init(&output->animation_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02006679 wl_list_init(&output->feedback_list);
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03006680 wl_list_init(&output->paint_node_list);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03006681 wl_list_init(&output->paint_node_z_order_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02006682
Pekka Paalanen9a0aa372021-06-08 13:38:35 +03006683 if (!weston_output_set_color_transforms(output))
Pekka Paalanen1d2eee22021-02-25 12:03:28 +02006684 return -1;
Pekka Paalanen1d2eee22021-02-25 12:03:28 +02006685
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006686 /* Enable the output (set up the crtc or create a
6687 * window representing the output, set up the
6688 * renderer, etc)
6689 */
6690 if (output->enable(output) < 0) {
6691 weston_log("Enabling output \"%s\" failed.\n", output->name);
Pekka Paalanen1d2eee22021-02-25 12:03:28 +02006692 weston_output_reset_color_transforms(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006693 return -1;
6694 }
6695
6696 weston_compositor_add_output(output->compositor, output);
6697
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006698 head_names = weston_output_create_heads_string(output);
6699 weston_log("Output '%s' enabled with head(s) %s\n",
6700 output->name, head_names);
6701 free(head_names);
6702
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006703 return 0;
6704}
6705
6706/** Converts a weston_output object to a pending output state, so it
6707 ** can be configured again or destroyed.
6708 *
6709 * \param output The weston_output object that needs to be disabled.
6710 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006711 * Calls a backend specific function to disable an output, in case
6712 * such function exists.
6713 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03006714 * The backend specific disable function may choose to postpone the disabling
6715 * by returning a negative value, in which case this function returns early.
6716 * In that case the backend will guarantee the output will be disabled soon
6717 * by the backend calling this function again. One must not attempt to re-enable
6718 * the output until that happens.
6719 *
6720 * Otherwise, if the output is being used by the compositor, it is removed
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006721 * from weston's output_list (see weston_compositor_remove_output())
6722 * and is returned to a state it was before weston_output_enable()
6723 * was ran (see weston_output_enable_undo()).
6724 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03006725 * See weston_output_init() for more information on the
6726 * state output is returned to.
Pekka Paalanencc201e42017-03-30 15:11:25 +03006727 *
6728 * If the output has never been enabled yet, this function can still be
6729 * called to ensure that the output is actually turned off rather than left
6730 * in the state it was discovered in.
Marius Vlad55d87362019-06-11 01:15:35 +03006731 *
6732 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006733 */
6734WL_EXPORT void
6735weston_output_disable(struct weston_output *output)
6736{
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006737 /* Should we rename this? */
6738 output->destroying = 1;
6739
Pekka Paalanenc65df642017-03-29 15:45:46 +03006740 /* Disable is called unconditionally also for not-enabled outputs,
6741 * because at compositor start-up, if there is an output that is
6742 * already on but the compositor wants to turn it off, we have to
6743 * forward the turn-off to the backend so it knows to do it.
6744 * The backend cannot initially turn off everything, because it
6745 * would cause unnecessary mode-sets for all outputs the compositor
6746 * wants to be on.
6747 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006748 if (output->disable(output) < 0)
6749 return;
6750
Pekka Paalanen2ec15eb2021-06-09 16:14:45 +03006751 if (output->enabled) {
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006752 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006753
Pekka Paalanen2ec15eb2021-06-09 16:14:45 +03006754 assert(wl_list_empty(&output->paint_node_list));
6755 }
6756
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006757 output->destroying = 0;
6758}
6759
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006760/** Forces a synchronous call to heads_changed hook
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006761 *
6762 * \param compositor The compositor instance
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006763 *
6764 * If there are new or changed heads, calls the heads_changed hook and
6765 * returns after the hook returns.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006766 *
6767 * \ingroup compositor
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006768 */
6769WL_EXPORT void
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006770weston_compositor_flush_heads_changed(struct weston_compositor *compositor)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006771{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03006772 if (compositor->heads_changed_source) {
6773 wl_event_source_remove(compositor->heads_changed_source);
6774 weston_compositor_call_heads_changed(compositor);
6775 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006776}
6777
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006778/** Add destroy callback for an output
6779 *
6780 * \param output The output to watch.
6781 * \param listener The listener to add. The \c notify member must be set.
6782 *
6783 * The listener callback will be called when user destroys an output. This
6784 * may be delayed by a backend in some cases. The main purpose of the
6785 * listener is to allow hooking up custom data to the output. The custom data
6786 * can be fetched via weston_output_get_destroy_listener() followed by
6787 * container_of().
6788 *
6789 * The \c data argument to the notify callback is the weston_output being
6790 * destroyed.
6791 *
6792 * @note This is for the final destruction of an output, not when it gets
6793 * disabled. If you want to keep track of enabled outputs, this is not it.
Marius Vlad55d87362019-06-11 01:15:35 +03006794 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05006795 * \ingroup output
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006796 */
6797WL_EXPORT void
6798weston_output_add_destroy_listener(struct weston_output *output,
6799 struct wl_listener *listener)
6800{
6801 wl_signal_add(&output->user_destroy_signal, listener);
6802}
6803
6804/** Look up destroy listener for an output
6805 *
6806 * \param output The output to query.
6807 * \param notify The notify function used used for the added destroy listener.
6808 * \return The listener, or NULL if not found.
6809 *
6810 * This looks up the previously added destroy listener struct based on the
6811 * notify function it has. The listener can be used to access user data
6812 * through \c container_of().
6813 *
6814 * \sa wl_signal_get() weston_output_add_destroy_listener()
Marius Vlad55d87362019-06-11 01:15:35 +03006815 * \ingroup output
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006816 */
6817WL_EXPORT struct wl_listener *
6818weston_output_get_destroy_listener(struct weston_output *output,
6819 wl_notify_func_t notify)
6820{
6821 return wl_signal_get(&output->user_destroy_signal, notify);
6822}
6823
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006824/** Uninitialize an output
6825 *
6826 * Removes the output from the list of enabled outputs if necessary, but
6827 * does not call the backend's output disable function. The output will no
6828 * longer be in the list of pending outputs either.
6829 *
6830 * All fields of weston_output become uninitialized, i.e. should not be used
6831 * anymore. The caller can free the memory after this.
6832 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05006833 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006834 * \internal
6835 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006836WL_EXPORT void
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03006837weston_output_release(struct weston_output *output)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006838{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006839 struct weston_head *head, *tmp;
6840
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006841 output->destroying = 1;
6842
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006843 wl_signal_emit(&output->user_destroy_signal, output);
6844
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03006845 if (output->idle_repaint_source)
6846 wl_event_source_remove(output->idle_repaint_source);
6847
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006848 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006849 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006850
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006851 pixman_region32_fini(&output->region);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006852 wl_list_remove(&output->link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006853
6854 wl_list_for_each_safe(head, tmp, &output->head_list, output_link)
6855 weston_head_detach(head);
6856
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006857 free(output->name);
6858}
6859
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006860/** Find an output by its given name
6861 *
6862 * \param compositor The compositor to search in.
6863 * \param name The output name to search for.
6864 * \return An existing output with the given name, or NULL if not found.
6865 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006866 * \ingroup compositor
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006867 */
6868WL_EXPORT struct weston_output *
6869weston_compositor_find_output_by_name(struct weston_compositor *compositor,
6870 const char *name)
6871{
6872 struct weston_output *output;
6873
6874 wl_list_for_each(output, &compositor->output_list, link)
6875 if (strcmp(output->name, name) == 0)
6876 return output;
6877
6878 wl_list_for_each(output, &compositor->pending_output_list, link)
6879 if (strcmp(output->name, name) == 0)
6880 return output;
6881
6882 return NULL;
6883}
6884
6885/** Create a named output
6886 *
6887 * \param compositor The compositor.
6888 * \param name The name for the output.
6889 * \return A new \c weston_output, or NULL on failure.
6890 *
6891 * This creates a new weston_output that starts with no heads attached.
6892 *
6893 * An output must be configured and it must have at least one head before
6894 * it can be enabled.
6895 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006896 * \ingroup compositor
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006897 */
6898WL_EXPORT struct weston_output *
6899weston_compositor_create_output(struct weston_compositor *compositor,
6900 const char *name)
6901{
6902 assert(compositor->backend->create_output);
6903
6904 if (weston_compositor_find_output_by_name(compositor, name)) {
6905 weston_log("Warning: attempted to create an output with a "
6906 "duplicate name '%s'.\n", name);
6907 return NULL;
6908 }
6909
6910 return compositor->backend->create_output(compositor, name);
6911}
6912
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006913/** Create an output for an unused head
6914 *
6915 * \param compositor The compositor.
6916 * \param head The head to attach to the output.
6917 * \return A new \c weston_output, or NULL on failure.
6918 *
6919 * This creates a new weston_output that starts with the given head attached.
6920 * The output inherits the name of the head. The head must not be already
6921 * attached to another output.
6922 *
6923 * An output must be configured before it can be enabled.
6924 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006925 * \ingroup compositor
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006926 */
6927WL_EXPORT struct weston_output *
6928weston_compositor_create_output_with_head(struct weston_compositor *compositor,
6929 struct weston_head *head)
6930{
6931 struct weston_output *output;
6932
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006933 output = weston_compositor_create_output(compositor, head->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006934 if (!output)
6935 return NULL;
6936
6937 if (weston_output_attach_head(output, head) < 0) {
Pekka Paalanen42c0e142017-10-27 12:07:49 +03006938 weston_output_destroy(output);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006939 return NULL;
6940 }
6941
6942 return output;
6943}
6944
6945/** Destroy an output
6946 *
6947 * \param output The output to destroy.
6948 *
6949 * The heads attached to the given output are detached and become unused again.
6950 *
6951 * It is not necessary to explicitly destroy all outputs at compositor exit.
Leandro Ribeiroca640d52020-01-27 19:12:01 -03006952 * weston_compositor_destroy() will automatically destroy any remaining
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006953 * outputs.
6954 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05006955 * \ingroup output
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006956 */
6957WL_EXPORT void
6958weston_output_destroy(struct weston_output *output)
6959{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006960 output->destroy(output);
6961}
6962
Pekka Paalanencf0a4762017-04-04 16:36:07 +03006963/** When you need a head...
6964 *
6965 * This function is a hack, used until all code has been converted to become
6966 * multi-head aware.
6967 *
6968 * \param output The weston_output whose head to get.
6969 * \return The first head in the output's list.
Marius Vlad55d87362019-06-11 01:15:35 +03006970 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05006971 * \ingroup output
Pekka Paalanencf0a4762017-04-04 16:36:07 +03006972 */
6973WL_EXPORT struct weston_head *
6974weston_output_get_first_head(struct weston_output *output)
6975{
6976 if (wl_list_empty(&output->head_list))
6977 return NULL;
6978
6979 return container_of(output->head_list.next,
6980 struct weston_head, output_link);
6981}
6982
Ankit Nautiyal2844f8e2019-04-03 10:14:59 +05306983/** Allow/Disallow content-protection support for an output
6984 *
6985 * This function sets the allow_protection member for an output. Setting of
6986 * this field will allow the compositor to attempt content-protection for this
6987 * output, for a backend that supports the content-protection protocol.
6988 *
6989 * \param output The weston_output for whom the content-protection is to be
6990 * allowed.
6991 * \param allow_protection The bool value which is to be set.
6992 */
6993WL_EXPORT void
6994weston_output_allow_protection(struct weston_output *output,
6995 bool allow_protection)
6996{
6997 output->allow_protection = allow_protection;
6998}
6999
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01007000static void
Roman Gilge97391c2019-03-29 13:01:06 +01007001xdg_output_unlist(struct wl_resource *resource)
7002{
7003 wl_list_remove(wl_resource_get_link(resource));
7004}
7005
7006static void
7007xdg_output_destroy(struct wl_client *client, struct wl_resource *resource)
7008{
7009 wl_resource_destroy(resource);
7010}
7011
7012static const struct zxdg_output_v1_interface xdg_output_interface = {
7013 xdg_output_destroy
7014};
7015
7016static void
7017xdg_output_manager_destroy(struct wl_client *client,
7018 struct wl_resource *resource)
7019{
7020 wl_resource_destroy(resource);
7021}
7022
7023static void
7024xdg_output_manager_get_xdg_output(struct wl_client *client,
7025 struct wl_resource *manager,
7026 uint32_t id,
7027 struct wl_resource *output_resource)
7028{
7029 int version = wl_resource_get_version(manager);
7030 struct weston_head *head = wl_resource_get_user_data(output_resource);
7031 struct weston_output *output = head->output;
7032 struct wl_resource *resource;
7033
7034 resource = wl_resource_create(client, &zxdg_output_v1_interface,
7035 version, id);
7036 if (resource == NULL) {
7037 wl_client_post_no_memory(client);
7038 return;
7039 }
7040
7041 wl_list_insert(&head->xdg_output_resource_list,
7042 wl_resource_get_link(resource));
7043
7044 wl_resource_set_implementation(resource, &xdg_output_interface,
7045 NULL, xdg_output_unlist);
7046
7047 zxdg_output_v1_send_logical_position(resource, output->x, output->y);
7048 zxdg_output_v1_send_logical_size(resource,
7049 output->width,
7050 output->height);
7051 if (version >= ZXDG_OUTPUT_V1_NAME_SINCE_VERSION)
7052 zxdg_output_v1_send_name(resource, head->name);
7053
7054 zxdg_output_v1_send_done(resource);
7055}
7056
7057static const struct zxdg_output_manager_v1_interface xdg_output_manager_interface = {
7058 xdg_output_manager_destroy,
7059 xdg_output_manager_get_xdg_output
7060};
7061
7062static void
7063bind_xdg_output_manager(struct wl_client *client,
7064 void *data, uint32_t version, uint32_t id)
7065{
7066 struct wl_resource *resource;
7067
7068 resource = wl_resource_create(client, &zxdg_output_manager_v1_interface,
7069 version, id);
7070 if (resource == NULL) {
7071 wl_client_post_no_memory(client);
7072 return;
7073 }
7074
7075 wl_resource_set_implementation(resource, &xdg_output_manager_interface,
7076 NULL, NULL);
7077}
7078
7079static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007080destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01007081{
Jonny Lamb74130762013-11-26 18:19:46 +01007082 struct weston_surface *surface =
7083 wl_resource_get_user_data(resource);
7084
Pekka Paalanen4826f872016-04-22 14:14:38 +03007085 if (!surface)
7086 return;
7087
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007088 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02007089 surface->pending.buffer_viewport.buffer.src_width =
7090 wl_fixed_from_int(-1);
7091 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02007092 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01007093}
7094
7095static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007096viewport_destroy(struct wl_client *client,
7097 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01007098{
7099 wl_resource_destroy(resource);
7100}
7101
7102static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007103viewport_set_source(struct wl_client *client,
7104 struct wl_resource *resource,
7105 wl_fixed_t src_x,
7106 wl_fixed_t src_y,
7107 wl_fixed_t src_width,
7108 wl_fixed_t src_height)
7109{
7110 struct weston_surface *surface =
7111 wl_resource_get_user_data(resource);
7112
Pekka Paalanen4826f872016-04-22 14:14:38 +03007113 if (!surface) {
7114 wl_resource_post_error(resource,
7115 WP_VIEWPORT_ERROR_NO_SURFACE,
7116 "wl_surface for this viewport is no longer exists");
7117 return;
7118 }
7119
7120 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03007121 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007122
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007123 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03007124 src_height == wl_fixed_from_int(-1) &&
7125 src_x == wl_fixed_from_int(-1) &&
7126 src_y == wl_fixed_from_int(-1)) {
7127 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007128 surface->pending.buffer_viewport.buffer.src_width =
7129 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02007130 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007131 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007132 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007133
Pekka Paalanen201769a2016-04-26 14:42:11 +03007134 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007135 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007136 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03007137 "wl_surface@%d viewport source "
7138 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
7139 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007140 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03007141 wl_fixed_to_double(src_height),
7142 wl_fixed_to_double(src_x),
7143 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007144 return;
7145 }
7146
7147 surface->pending.buffer_viewport.buffer.src_x = src_x;
7148 surface->pending.buffer_viewport.buffer.src_y = src_y;
7149 surface->pending.buffer_viewport.buffer.src_width = src_width;
7150 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02007151 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007152}
7153
7154static void
7155viewport_set_destination(struct wl_client *client,
7156 struct wl_resource *resource,
7157 int32_t dst_width,
7158 int32_t dst_height)
7159{
7160 struct weston_surface *surface =
7161 wl_resource_get_user_data(resource);
7162
Pekka Paalanen4826f872016-04-22 14:14:38 +03007163 if (!surface) {
7164 wl_resource_post_error(resource,
7165 WP_VIEWPORT_ERROR_NO_SURFACE,
7166 "wl_surface for this viewport no longer exists");
7167 return;
7168 }
7169
7170 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007171
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007172 if (dst_width == -1 && dst_height == -1) {
7173 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007174 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02007175 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007176 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007177 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007178
7179 if (dst_width <= 0 || dst_height <= 0) {
7180 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007181 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007182 "destination size must be positive (%dx%d)",
7183 dst_width, dst_height);
7184 return;
7185 }
7186
7187 surface->pending.buffer_viewport.surface.width = dst_width;
7188 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02007189 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007190}
7191
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007192static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007193 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007194 viewport_set_source,
7195 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01007196};
7197
7198static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007199viewporter_destroy(struct wl_client *client,
7200 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01007201{
7202 wl_resource_destroy(resource);
7203}
7204
7205static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007206viewporter_get_viewport(struct wl_client *client,
7207 struct wl_resource *viewporter,
7208 uint32_t id,
7209 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01007210{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007211 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007212 struct weston_surface *surface =
7213 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01007214 struct wl_resource *resource;
7215
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007216 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007217 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007218 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007219 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01007220 return;
7221 }
7222
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007223 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007224 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01007225 if (resource == NULL) {
7226 wl_client_post_no_memory(client);
7227 return;
7228 }
7229
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007230 wl_resource_set_implementation(resource, &viewport_interface,
7231 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01007232
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007233 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01007234}
7235
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007236static const struct wp_viewporter_interface viewporter_interface = {
7237 viewporter_destroy,
7238 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01007239};
7240
7241static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007242bind_viewporter(struct wl_client *client,
7243 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01007244{
7245 struct wl_resource *resource;
7246
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007247 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06007248 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01007249 if (resource == NULL) {
7250 wl_client_post_no_memory(client);
7251 return;
7252 }
7253
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007254 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01007255 NULL, NULL);
7256}
7257
7258static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04007259destroy_presentation_feedback(struct wl_resource *feedback_resource)
7260{
7261 struct weston_presentation_feedback *feedback;
7262
7263 feedback = wl_resource_get_user_data(feedback_resource);
7264
7265 wl_list_remove(&feedback->link);
7266 free(feedback);
7267}
7268
7269static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007270presentation_destroy(struct wl_client *client, struct wl_resource *resource)
7271{
7272 wl_resource_destroy(resource);
7273}
7274
7275static void
7276presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04007277 struct wl_resource *presentation_resource,
7278 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007279 uint32_t callback)
7280{
Pekka Paalanen133e4392014-09-23 22:08:46 -04007281 struct weston_surface *surface;
7282 struct weston_presentation_feedback *feedback;
7283
7284 surface = wl_resource_get_user_data(surface_resource);
7285
Bryce Harringtonde16d892014-11-20 22:21:57 -08007286 feedback = zalloc(sizeof *feedback);
7287 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04007288 goto err_calloc;
7289
7290 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007291 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04007292 1, callback);
7293 if (!feedback->resource)
7294 goto err_create;
7295
7296 wl_resource_set_implementation(feedback->resource, NULL, feedback,
7297 destroy_presentation_feedback);
7298
7299 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
7300
7301 return;
7302
7303err_create:
7304 free(feedback);
7305
7306err_calloc:
7307 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007308}
7309
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007310static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007311 presentation_destroy,
7312 presentation_feedback
7313};
7314
7315static void
7316bind_presentation(struct wl_client *client,
7317 void *data, uint32_t version, uint32_t id)
7318{
7319 struct weston_compositor *compositor = data;
7320 struct wl_resource *resource;
7321
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007322 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06007323 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007324 if (resource == NULL) {
7325 wl_client_post_no_memory(client);
7326 return;
7327 }
7328
7329 wl_resource_set_implementation(resource, &presentation_implementation,
7330 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007331 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007332}
7333
7334static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05007335compositor_bind(struct wl_client *client,
7336 void *data, uint32_t version, uint32_t id)
7337{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007338 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05007339 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05007340
Jason Ekstranda85118c2013-06-27 20:17:02 -05007341 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06007342 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07007343 if (resource == NULL) {
7344 wl_client_post_no_memory(client);
7345 return;
7346 }
7347
7348 wl_resource_set_implementation(resource, &compositor_interface,
7349 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05007350}
7351
Daniel Stonece62cb32018-07-20 09:46:24 +01007352static const char *
7353output_repaint_status_text(struct weston_output *output)
7354{
7355 switch (output->repaint_status) {
7356 case REPAINT_NOT_SCHEDULED:
7357 return "no repaint";
7358 case REPAINT_BEGIN_FROM_IDLE:
7359 return "start_repaint_loop scheduled";
7360 case REPAINT_SCHEDULED:
7361 return "repaint scheduled";
7362 case REPAINT_AWAITING_COMPLETION:
7363 return "awaiting completion";
7364 }
7365
7366 assert(!"output_repaint_status_text missing enum");
7367 return NULL;
7368}
7369
7370static void
7371debug_scene_view_print_buffer(FILE *fp, struct weston_view *view)
7372{
7373 struct weston_buffer *buffer = view->surface->buffer_ref.buffer;
7374 struct wl_shm_buffer *shm;
7375 struct linux_dmabuf_buffer *dmabuf;
Marius Vlad00a6e012018-11-20 17:52:31 +02007376 const struct pixel_format_info *pixel_info = NULL;
Daniel Stonece62cb32018-07-20 09:46:24 +01007377
7378 if (!buffer) {
7379 fprintf(fp, "\t\t[buffer not available]\n");
7380 return;
7381 }
7382
7383 shm = wl_shm_buffer_get(buffer->resource);
7384 if (shm) {
Marius Vlad00a6e012018-11-20 17:52:31 +02007385 uint32_t _format = wl_shm_buffer_get_format(shm);
7386 pixel_info = pixel_format_get_info_shm(_format);
Daniel Stonece62cb32018-07-20 09:46:24 +01007387 fprintf(fp, "\t\tSHM buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02007388 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
7389 (unsigned long) _format,
7390 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Daniel Stonece62cb32018-07-20 09:46:24 +01007391 return;
7392 }
7393
7394 dmabuf = linux_dmabuf_buffer_get(buffer->resource);
7395 if (dmabuf) {
Marius Vlad6f6fd262021-07-12 12:58:34 +03007396 uint64_t modifier = dmabuf->attributes.modifier[0];
7397 char *modifier_name = pixel_format_get_modifier(modifier);
Marius Vlad00a6e012018-11-20 17:52:31 +02007398 pixel_info = pixel_format_get_info(dmabuf->attributes.format);
Daniel Stonece62cb32018-07-20 09:46:24 +01007399 fprintf(fp, "\t\tdmabuf buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02007400 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
7401 (unsigned long) dmabuf->attributes.format,
7402 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Marius Vlad6f6fd262021-07-12 12:58:34 +03007403
7404 fprintf(fp, "\t\t\tmodifier: %s\n", modifier_name ? modifier_name :
7405 "Failed to convert to a modifier name");
7406 free(modifier_name);
Daniel Stonece62cb32018-07-20 09:46:24 +01007407 return;
7408 }
7409
Marius Vlad253ba9a2019-03-17 18:22:21 +02007410 fprintf(fp, "\t\tEGL buffer\n");
Daniel Stonece62cb32018-07-20 09:46:24 +01007411}
7412
7413static void
7414debug_scene_view_print(FILE *fp, struct weston_view *view, int view_idx)
7415{
7416 struct weston_compositor *ec = view->surface->compositor;
7417 struct weston_output *output;
7418 char desc[512];
7419 pixman_box32_t *box;
7420 uint32_t surface_id = 0;
7421 pid_t pid = 0;
7422
7423 if (view->surface->resource) {
7424 struct wl_resource *resource = view->surface->resource;
7425 wl_client_get_credentials(wl_resource_get_client(resource),
7426 &pid, NULL, NULL);
7427 surface_id = wl_resource_get_id(view->surface->resource);
7428 }
7429
7430 if (!view->surface->get_label ||
7431 view->surface->get_label(view->surface, desc, sizeof(desc)) < 0) {
7432 strcpy(desc, "[no description available]");
7433 }
7434 fprintf(fp, "\tView %d (role %s, PID %d, surface ID %u, %s, %p):\n",
7435 view_idx, view->surface->role_name, pid, surface_id,
7436 desc, view);
7437
7438 box = pixman_region32_extents(&view->transform.boundingbox);
7439 fprintf(fp, "\t\tposition: (%d, %d) -> (%d, %d)\n",
7440 box->x1, box->y1, box->x2, box->y2);
7441 box = pixman_region32_extents(&view->transform.opaque);
7442
Michael Olbrichb7e5f102020-08-11 16:33:35 +02007443 if (weston_view_is_opaque(view, &view->transform.boundingbox)) {
Daniel Stonece62cb32018-07-20 09:46:24 +01007444 fprintf(fp, "\t\t[fully opaque]\n");
7445 } else if (!pixman_region32_not_empty(&view->transform.opaque)) {
7446 fprintf(fp, "\t\t[not opaque]\n");
7447 } else {
7448 fprintf(fp, "\t\t[opaque: (%d, %d) -> (%d, %d)]\n",
7449 box->x1, box->y1, box->x2, box->y2);
7450 }
7451
7452 if (view->alpha < 1.0)
7453 fprintf(fp, "\t\talpha: %f\n", view->alpha);
7454
7455 if (view->output_mask != 0) {
7456 bool first_output = true;
7457 fprintf(fp, "\t\toutputs: ");
7458 wl_list_for_each(output, &ec->output_list, link) {
7459 if (!(view->output_mask & (1 << output->id)))
7460 continue;
7461 fprintf(fp, "%s%d (%s)%s",
7462 (first_output) ? "" : ", ",
7463 output->id, output->name,
7464 (view->output == output) ? " (primary)" : "");
7465 first_output = false;
7466 }
7467 } else {
7468 fprintf(fp, "\t\t[no outputs]");
7469 }
7470
7471 fprintf(fp, "\n");
7472
7473 debug_scene_view_print_buffer(fp, view);
7474}
7475
Marius Vlad433f4e72019-02-17 22:14:23 +02007476static void
7477debug_scene_view_print_tree(struct weston_view *view,
Marius Vlada6acfa82019-03-17 18:10:09 +02007478 FILE *fp, int *view_idx)
Marius Vlad433f4e72019-02-17 22:14:23 +02007479{
7480 struct weston_subsurface *sub;
7481 struct weston_view *ev;
7482
7483 /*
7484 * print the view first, then we recursively go on printing
7485 * sub-surfaces. We bail out once no more sub-surfaces are available.
7486 */
Marius Vlada6acfa82019-03-17 18:10:09 +02007487 debug_scene_view_print(fp, view, *view_idx);
Marius Vlad433f4e72019-02-17 22:14:23 +02007488
7489 /* no more sub-surfaces */
7490 if (wl_list_empty(&view->surface->subsurface_list))
7491 return;
7492
7493 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
7494 wl_list_for_each(ev, &sub->surface->views, surface_link) {
Michael Olbrichef5f3232020-04-29 09:03:15 +02007495 /* only print the child views of the current view */
7496 if (ev->parent_view != view)
Marius Vlad433f4e72019-02-17 22:14:23 +02007497 continue;
Marius Vlada6acfa82019-03-17 18:10:09 +02007498
7499 (*view_idx)++;
Marius Vlad433f4e72019-02-17 22:14:23 +02007500 debug_scene_view_print_tree(ev, fp, view_idx);
7501 }
7502 }
7503}
7504
Daniel Stonece62cb32018-07-20 09:46:24 +01007505/**
7506 * Output information on how libweston is currently composing the scene
7507 * graph.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007508 *
7509 * \ingroup compositor
Daniel Stonece62cb32018-07-20 09:46:24 +01007510 */
7511WL_EXPORT char *
7512weston_compositor_print_scene_graph(struct weston_compositor *ec)
7513{
7514 struct weston_output *output;
7515 struct weston_layer *layer;
7516 struct timespec now;
7517 int layer_idx = 0;
7518 FILE *fp;
7519 char *ret;
7520 size_t len;
7521 int err;
7522
7523 fp = open_memstream(&ret, &len);
7524 assert(fp);
7525
7526 weston_compositor_read_presentation_clock(ec, &now);
7527 fprintf(fp, "Weston scene graph at %ld.%09ld:\n\n",
7528 now.tv_sec, now.tv_nsec);
7529
7530 wl_list_for_each(output, &ec->output_list, link) {
7531 struct weston_head *head;
7532 int head_idx = 0;
7533
7534 fprintf(fp, "Output %d (%s):\n", output->id, output->name);
7535 assert(output->enabled);
7536
7537 fprintf(fp, "\tposition: (%d, %d) -> (%d, %d)\n",
7538 output->x, output->y,
7539 output->x + output->width,
7540 output->y + output->height);
7541 fprintf(fp, "\tmode: %dx%d@%.3fHz\n",
7542 output->current_mode->width,
7543 output->current_mode->height,
7544 output->current_mode->refresh / 1000.0);
7545 fprintf(fp, "\tscale: %d\n", output->scale);
7546
7547 fprintf(fp, "\trepaint status: %s\n",
7548 output_repaint_status_text(output));
7549 if (output->repaint_status == REPAINT_SCHEDULED)
7550 fprintf(fp, "\tnext repaint: %ld.%09ld\n",
7551 output->next_repaint.tv_sec,
7552 output->next_repaint.tv_nsec);
7553
7554 wl_list_for_each(head, &output->head_list, output_link) {
7555 fprintf(fp, "\tHead %d (%s): %sconnected\n",
7556 head_idx++, head->name,
7557 (head->connected) ? "" : "not ");
7558 }
7559 }
7560
7561 fprintf(fp, "\n");
7562
7563 wl_list_for_each(layer, &ec->layer_list, link) {
7564 struct weston_view *view;
7565 int view_idx = 0;
7566
7567 fprintf(fp, "Layer %d (pos 0x%lx):\n", layer_idx++,
7568 (unsigned long) layer->position);
7569
7570 if (!weston_layer_mask_is_infinite(layer)) {
7571 fprintf(fp, "\t[mask: (%d, %d) -> (%d,%d)]\n\n",
7572 layer->mask.x1, layer->mask.y1,
7573 layer->mask.x2, layer->mask.y2);
7574 }
7575
Marius Vlada6acfa82019-03-17 18:10:09 +02007576 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
7577 debug_scene_view_print_tree(view, fp, &view_idx);
7578 view_idx++;
7579 }
Daniel Stonece62cb32018-07-20 09:46:24 +01007580
7581 if (wl_list_empty(&layer->view_list.link))
7582 fprintf(fp, "\t[no views]\n");
7583
7584 fprintf(fp, "\n");
7585 }
7586
7587 err = fclose(fp);
7588 assert(err == 0);
7589
7590 return ret;
7591}
7592
7593/**
7594 * Called when the 'scene-graph' debug scope is bound by a client. This
7595 * one-shot weston-debug scope prints the current scene graph when bound,
7596 * and then terminates the stream.
7597 */
7598static void
Marius Vladdad882a2019-07-17 15:43:53 +03007599debug_scene_graph_cb(struct weston_log_subscription *sub, void *data)
Daniel Stonece62cb32018-07-20 09:46:24 +01007600{
7601 struct weston_compositor *ec = data;
7602 char *str = weston_compositor_print_scene_graph(ec);
7603
Marius Vladdad882a2019-07-17 15:43:53 +03007604 weston_log_subscription_printf(sub, "%s", str);
Daniel Stonece62cb32018-07-20 09:46:24 +01007605 free(str);
Marius Vladdad882a2019-07-17 15:43:53 +03007606 weston_log_subscription_complete(sub);
Daniel Stonece62cb32018-07-20 09:46:24 +01007607}
7608
Leandro Ribeiro32a5acd2020-10-19 16:06:22 -03007609/** Retrieve testsuite data from compositor
7610 *
7611 * The testsuite data can be defined by the test suite of projects that uses
7612 * libweston and given to the compositor at the moment of its creation. This
7613 * function should be used when we need to retrieve the testsuite private data
7614 * from the compositor.
7615 *
7616 * \param ec The weston compositor.
7617 * \return The testsuite data.
7618 *
7619 * \ingroup compositor
7620 * \sa weston_compositor_test_data_init
7621 */
7622WL_EXPORT void *
7623weston_compositor_get_test_data(struct weston_compositor *ec)
7624{
7625 return ec->test_data.test_private_data;
7626}
7627
Giulio Camuffo459137b2014-10-11 23:56:24 +03007628/** Create the compositor.
7629 *
7630 * This functions creates and initializes a compositor instance.
7631 *
7632 * \param display The Wayland display to be used.
7633 * \param user_data A pointer to an object that can later be retrieved
Marius Vlada2dace22019-06-12 16:05:44 +03007634 * \param log_ctx A pointer to weston_debug_compositor
Leandro Ribeirocdb72182020-11-06 11:24:22 -03007635 * \param test_data Optional testsuite data, or NULL.
Giulio Camuffo459137b2014-10-11 23:56:24 +03007636 * using the \ref weston_compositor_get_user_data function.
7637 * \return The compositor instance on success or NULL on failure.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007638 *
7639 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007640 */
7641WL_EXPORT struct weston_compositor *
Marius Vlad880b4852019-04-07 17:07:58 +03007642weston_compositor_create(struct wl_display *display,
Leandro Ribeirocdb72182020-11-06 11:24:22 -03007643 struct weston_log_context *log_ctx, void *user_data,
7644 const struct weston_testsuite_data *test_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04007645{
Giulio Camuffo459137b2014-10-11 23:56:24 +03007646 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05007647 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07007648
Leandro Ribeirobd9c0a62020-01-17 10:47:49 -03007649 if (!log_ctx)
7650 return NULL;
7651
Giulio Camuffo459137b2014-10-11 23:56:24 +03007652 ec = zalloc(sizeof *ec);
7653 if (!ec)
7654 return NULL;
7655
Leandro Ribeirocdb72182020-11-06 11:24:22 -03007656 if (test_data)
7657 ec->test_data = *test_data;
7658
Leandro Ribeirobd9c0a62020-01-17 10:47:49 -03007659 ec->weston_log_ctx = log_ctx;
Giulio Camuffo459137b2014-10-11 23:56:24 +03007660 ec->wl_display = display;
7661 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04007662 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07007663 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04007664 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03007665 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03007666 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02007667 wl_signal_init(&ec->idle_signal);
7668 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02007669 wl_signal_init(&ec->show_input_panel_signal);
7670 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02007671 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01007672 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01007673 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02007674 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02007675 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02007676 wl_signal_init(&ec->output_resized_signal);
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03007677 wl_signal_init(&ec->heads_changed_signal);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02007678 wl_signal_init(&ec->output_heads_changed_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07007679 wl_signal_init(&ec->session_signal);
Robert Beckettc569bdc2019-07-10 16:40:12 +01007680 ec->session_active = true;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04007681
Casey Dahlin58ba1372012-04-19 22:50:08 -04007682 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03007683 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04007684
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02007685 ec->activate_serial = 1;
7686
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05007687 ec->touch_mode = WESTON_TOUCH_MODE_NORMAL;
7688
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05307689 ec->content_protection = NULL;
7690
Derek Foreman152254b2015-11-26 14:17:48 -06007691 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04007692 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007693 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05007694
Giulio Camuffo954f1832014-10-11 18:27:30 +03007695 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04007696 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007697 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03007698
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007699 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007700 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007701 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01007702
Roman Gilge97391c2019-03-29 13:01:06 +01007703 if (!wl_global_create(ec->wl_display, &zxdg_output_manager_v1_interface, 2,
7704 ec, bind_xdg_output_manager))
7705 goto fail;
7706
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007707 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007708 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007709 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007710
Jonas Ådahl30d61d82014-10-22 21:21:17 +02007711 if (weston_input_init(ec) != 0)
7712 goto fail;
7713
Jason Ekstranda7af7042013-10-12 22:38:11 -05007714 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02007715 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007716 wl_list_init(&ec->layer_list);
7717 wl_list_init(&ec->seat_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007718 wl_list_init(&ec->pending_output_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007719 wl_list_init(&ec->output_list);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03007720 wl_list_init(&ec->head_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007721 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01007722 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007723 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01007724 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007725 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02007726 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007727
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007728 wl_list_init(&ec->plugin_api_list);
7729
Xiong Zhang97116532013-10-23 13:58:31 +08007730 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02007731 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04007732
Giulio Camuffo459137b2014-10-11 23:56:24 +03007733 wl_data_device_manager_init(ec->wl_display);
7734
7735 wl_display_init_shm(ec->wl_display);
7736
7737 loop = wl_display_get_event_loop(ec->wl_display);
7738 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Daniel Stone6847b852017-03-01 11:34:08 +00007739 ec->repaint_timer =
7740 wl_event_loop_add_timer(loop, output_repaint_timer_handler,
7741 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007742
Quentin Glidic82681572016-12-17 13:40:51 +01007743 weston_layer_init(&ec->fade_layer, ec);
7744 weston_layer_init(&ec->cursor_layer, ec);
7745
7746 weston_layer_set_position(&ec->fade_layer, WESTON_LAYER_POSITION_FADE);
7747 weston_layer_set_position(&ec->cursor_layer,
7748 WESTON_LAYER_POSITION_CURSOR);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007749
Daniel Stonece62cb32018-07-20 09:46:24 +01007750 ec->debug_scene =
Leandro Ribeiroce100192019-12-26 16:35:49 -03007751 weston_compositor_add_log_scope(ec, "scene-graph",
7752 "Scene graph details\n",
7753 debug_scene_graph_cb, NULL,
7754 ec);
Daniel Stonece62cb32018-07-20 09:46:24 +01007755
Marius Vladda104eb2019-09-05 14:31:01 +03007756 ec->timeline =
Leandro Ribeiroce100192019-12-26 16:35:49 -03007757 weston_compositor_add_log_scope(ec, "timeline",
7758 "Timeline event points\n",
7759 weston_timeline_create_subscription,
7760 weston_timeline_destroy_subscription,
7761 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007762 return ec;
7763
7764fail:
7765 free(ec);
7766 return NULL;
7767}
7768
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007769/** weston_compositor_shutdown
7770 * \ingroup compositor
7771 */
Benjamin Franzkeb8263022011-08-30 11:32:47 +02007772WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007773weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07007774{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007775 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07007776
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007777 wl_event_source_remove(ec->idle_source);
Lujin Wangd4b46cd2019-08-12 12:03:29 -07007778 wl_event_source_remove(ec->repaint_timer);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007779
Matt Roper361d2ad2011-08-29 13:52:23 -07007780 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02007781 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07007782 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02007783
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007784 /* Destroy all pending outputs associated with this compositor */
7785 wl_list_for_each_safe(output, next, &ec->pending_output_list, link)
7786 output->destroy(output);
7787
Pekka Paalanen90a5ffa2021-02-25 12:03:28 +02007788 /* Color manager objects may have renderer hooks */
7789 if (ec->color_manager) {
7790 ec->color_manager->destroy(ec->color_manager);
7791 ec->color_manager = NULL;
7792 }
7793
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02007794 if (ec->renderer)
7795 ec->renderer->destroy(ec);
7796
Daniel Stone325fc2d2012-05-30 16:31:58 +01007797 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09007798 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01007799 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01007800 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01007801 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02007802 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007803
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04007804 weston_plane_release(&ec->primary_plane);
Pekka Paalanen87400372021-05-14 14:29:40 +03007805
7806 weston_layer_fini(&ec->fade_layer);
7807 weston_layer_fini(&ec->cursor_layer);
7808
7809 if (!wl_list_empty(&ec->layer_list))
7810 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 -07007811}
7812
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007813/** weston_compositor_exit_with_code
7814 * \ingroup compositor
7815 */
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05007816WL_EXPORT void
Adam Jackson3c3f3b12019-10-16 16:02:59 -04007817weston_compositor_exit_with_code(struct weston_compositor *compositor,
Frederic Plourdec336f062014-10-29 14:44:33 -04007818 int exit_code)
7819{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02007820 if (compositor->exit_code == EXIT_SUCCESS)
7821 compositor->exit_code = exit_code;
7822
Giulio Camuffo459137b2014-10-11 23:56:24 +03007823 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04007824}
7825
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007826/** weston_compositor_set_default_pointer_grab
7827 * \ingroup compositor
7828 */
Frederic Plourdec336f062014-10-29 14:44:33 -04007829WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007830weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
7831 const struct weston_pointer_grab_interface *interface)
7832{
7833 struct weston_seat *seat;
7834
7835 ec->default_pointer_grab = interface;
7836 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05007837 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
7838
7839 if (pointer)
7840 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007841 }
7842}
7843
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007844/** weston_compositor_set_presentation_clock
7845 * \ingroup compositor
7846 */
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007847WL_EXPORT int
7848weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
7849 clockid_t clk_id)
7850{
7851 struct timespec ts;
7852
7853 if (clock_gettime(clk_id, &ts) < 0)
7854 return -1;
7855
7856 compositor->presentation_clock = clk_id;
7857
7858 return 0;
7859}
7860
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007861/** For choosing the software clock, when the display hardware or API
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007862 * does not expose a compatible presentation timestamp.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007863 *
7864 * \ingroup compositor
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007865 */
7866WL_EXPORT int
7867weston_compositor_set_presentation_clock_software(
7868 struct weston_compositor *compositor)
7869{
7870 /* In order of preference */
7871 static const clockid_t clocks[] = {
7872 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
7873 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
7874 CLOCK_MONOTONIC, /* no jumps, may crawl */
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007875 };
7876 unsigned i;
7877
7878 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
7879 if (weston_compositor_set_presentation_clock(compositor,
7880 clocks[i]) == 0)
7881 return 0;
7882
7883 weston_log("Error: no suitable presentation clock available.\n");
7884
7885 return -1;
7886}
7887
Pekka Paalanen662f3842015-03-18 12:17:26 +02007888/** Read the current time from the Presentation clock
7889 *
7890 * \param compositor
Marius Vlada2dace22019-06-12 16:05:44 +03007891 * \param[out] ts The current time.
Pekka Paalanen662f3842015-03-18 12:17:26 +02007892 *
7893 * \note Reading the current time in user space is always imprecise to some
7894 * degree.
7895 *
7896 * This function is never meant to fail. If reading the clock does fail,
7897 * an error message is logged and a zero time is returned. Callers are not
7898 * supposed to detect or react to failures.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007899 *
7900 * \ingroup compositor
Pekka Paalanen662f3842015-03-18 12:17:26 +02007901 */
7902WL_EXPORT void
7903weston_compositor_read_presentation_clock(
7904 const struct weston_compositor *compositor,
7905 struct timespec *ts)
7906{
7907 static bool warned;
7908 int ret;
7909
7910 ret = clock_gettime(compositor->presentation_clock, ts);
7911 if (ret < 0) {
7912 ts->tv_sec = 0;
7913 ts->tv_nsec = 0;
7914
7915 if (!warned)
7916 weston_log("Error: failure to read "
Antonio Borneo39578632019-04-26 23:57:31 +02007917 "the presentation clock %#x: '%s' (%d)\n",
7918 compositor->presentation_clock,
7919 strerror(errno), errno);
Pekka Paalanen662f3842015-03-18 12:17:26 +02007920 warned = true;
7921 }
7922}
7923
Pekka Paalanen230f3b12014-09-29 14:18:40 -04007924/** Import dmabuf buffer into current renderer
7925 *
7926 * \param compositor
7927 * \param buffer the dmabuf buffer to import
7928 * \return true on usable buffers, false otherwise
7929 *
7930 * This function tests that the linux_dmabuf_buffer is usable
7931 * for the current renderer. Returns false on unusable buffers. Usually
7932 * usability is tested by importing the dmabufs for composition.
7933 *
7934 * This hook is also used for detecting if the renderer supports
7935 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
7936 * supported.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007937 *
7938 * \ingroup compositor
7939 */
Pekka Paalanen230f3b12014-09-29 14:18:40 -04007940WL_EXPORT bool
7941weston_compositor_import_dmabuf(struct weston_compositor *compositor,
7942 struct linux_dmabuf_buffer *buffer)
7943{
7944 struct weston_renderer *renderer;
7945
7946 renderer = compositor->renderer;
7947
7948 if (renderer->import_dmabuf == NULL)
7949 return false;
7950
7951 return renderer->import_dmabuf(compositor, buffer);
7952}
7953
Marius Vlad5a701542019-11-16 20:26:52 +02007954WL_EXPORT bool
7955weston_compositor_dmabuf_can_scanout(struct weston_compositor *compositor,
7956 struct linux_dmabuf_buffer *buffer)
7957{
7958 struct weston_backend *backend = compositor->backend;
7959
7960 if (backend->can_scanout_dmabuf == NULL)
7961 return false;
7962
7963 return backend->can_scanout_dmabuf(compositor, buffer);
7964}
7965
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007966WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05007967weston_version(int *major, int *minor, int *micro)
7968{
7969 *major = WESTON_VERSION_MAJOR;
7970 *minor = WESTON_VERSION_MINOR;
7971 *micro = WESTON_VERSION_MICRO;
7972}
7973
Daniel Stonee03c1112016-11-24 20:45:45 +00007974/**
7975 * Attempts to find a module path from the module map specified in the
7976 * environment. If found, writes the full path into the path variable.
7977 *
7978 * The module map is a string in environment variable WESTON_MODULE_MAP, where
7979 * each entry is of the form "name=path" and entries are separated by
7980 * semicolons. Whitespace is significant.
7981 *
7982 * \param name The name to search for.
7983 * \param path Where the path is written to if found.
7984 * \param path_len Allocated bytes at \c path .
7985 * \returns The length of the string written to path on success, or 0 if the
7986 * module was not specified in the environment map or path_len was too small.
7987 */
7988WL_EXPORT size_t
7989weston_module_path_from_env(const char *name, char *path, size_t path_len)
7990{
7991 const char *mapping = getenv("WESTON_MODULE_MAP");
7992 const char *end;
7993 const int name_len = strlen(name);
7994
7995 if (!mapping)
7996 return 0;
7997
7998 end = mapping + strlen(mapping);
7999 while (mapping < end && *mapping) {
8000 const char *filename, *next;
8001
8002 /* early out: impossibly short string */
8003 if (end - mapping < name_len + 1)
8004 return 0;
8005
8006 filename = &mapping[name_len + 1];
8007 next = strchrnul(mapping, ';');
8008
8009 if (strncmp(mapping, name, name_len) == 0 &&
8010 mapping[name_len] == '=') {
8011 size_t file_len = next - filename; /* no trailing NUL */
8012 if (file_len >= path_len)
8013 return 0;
8014 strncpy(path, filename, file_len);
8015 path[file_len] = '\0';
8016 return file_len;
8017 }
8018
8019 mapping = next + 1;
8020 }
8021
8022 return 0;
8023}
8024
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03008025WL_EXPORT void *
8026weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04008027{
8028 char path[PATH_MAX];
8029 void *module, *init;
Daniel Stonebeb97e52016-11-28 12:13:54 +00008030 size_t len;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04008031
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08008032 if (name == NULL)
8033 return NULL;
8034
Derek Foreman3f86e502015-06-08 11:46:54 -05008035 if (name[0] != '/') {
Daniel Stonee03c1112016-11-24 20:45:45 +00008036 len = weston_module_path_from_env(name, path, sizeof path);
8037 if (len == 0)
Daniel Stonebeb97e52016-11-28 12:13:54 +00008038 len = snprintf(path, sizeof path, "%s/%s",
8039 LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05008040 } else {
Daniel Stonebeb97e52016-11-28 12:13:54 +00008041 len = snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05008042 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04008043
Daniel Stonebeb97e52016-11-28 12:13:54 +00008044 /* snprintf returns the length of the string it would've written,
8045 * _excluding_ the NUL byte. So even being equal to the size of
8046 * our buffer is an error here. */
8047 if (len >= sizeof path)
8048 return NULL;
8049
Kristian Høgsberga6813d22012-09-12 12:21:01 -04008050 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
8051 if (module) {
8052 weston_log("Module '%s' already loaded\n", path);
Pekka Paalanenf696ee92019-11-04 14:07:51 +02008053 } else {
8054 weston_log("Loading module '%s'\n", path);
8055 module = dlopen(path, RTLD_NOW);
8056 if (!module) {
8057 weston_log("Failed to load module: %s\n", dlerror());
8058 return NULL;
8059 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04008060 }
8061
8062 init = dlsym(module, entrypoint);
8063 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03008064 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00008065 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04008066 return NULL;
8067 }
8068
8069 return init;
8070}
8071
Pekka Paalanen6ffbba32019-11-06 12:59:32 +02008072/** Add a compositor destroy listener only once
8073 *
8074 * \param compositor The compositor whose destroy to watch for.
8075 * \param listener The listener struct to initialize.
8076 * \param destroy_handler The callback when compositor is destroyed.
8077 * \return True if listener is added, or false if there already is a listener
8078 * with the given \c destroy_handler.
8079 *
8080 * This function does nothing and returns false if the given callback function
8081 * is already present in the weston_compositor destroy callbacks list.
8082 * Otherwise, this function initializes the given listener with the given
8083 * callback pointer and adds it to the compositor's destroy callbacks list.
8084 *
8085 * This can be used to ensure that plugin initialization is done only once
8086 * in case the same plugin is loaded multiple times. If this function returns
8087 * false, the plugin should be already initialized successfully.
8088 *
8089 * All plugins should register a destroy listener for cleaning up. Note, that
8090 * the plugin destruction order is not guaranteed: plugins that depend on other
8091 * plugins must be able to be torn down in arbitrary order.
8092 *
Leandro Ribeiroca640d52020-01-27 19:12:01 -03008093 * \sa weston_compositor_destroy
Pekka Paalanen6ffbba32019-11-06 12:59:32 +02008094 */
8095WL_EXPORT bool
8096weston_compositor_add_destroy_listener_once(struct weston_compositor *compositor,
8097 struct wl_listener *listener,
8098 wl_notify_func_t destroy_handler)
8099{
8100 if (wl_signal_get(&compositor->destroy_signal, destroy_handler))
8101 return false;
8102
8103 listener->notify = destroy_handler;
8104 wl_signal_add(&compositor->destroy_signal, listener);
8105 return true;
8106}
Giulio Camuffo459137b2014-10-11 23:56:24 +03008107
Leandro Ribeiroca640d52020-01-27 19:12:01 -03008108/** Destroys the compositor.
Giulio Camuffo459137b2014-10-11 23:56:24 +03008109 *
Leandro Ribeiroca640d52020-01-27 19:12:01 -03008110 * This function cleans up the compositor state and then destroys it.
Giulio Camuffo459137b2014-10-11 23:56:24 +03008111 *
Leandro Ribeiroca640d52020-01-27 19:12:01 -03008112 * @param compositor The compositor to be destroyed.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008113 *
Marius Vlad284d5342019-06-24 12:00:47 +03008114 * @ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03008115 */
8116WL_EXPORT void
Leandro Ribeiroca640d52020-01-27 19:12:01 -03008117weston_compositor_destroy(struct weston_compositor *compositor)
Giulio Camuffo459137b2014-10-11 23:56:24 +03008118{
8119 /* prevent further rendering while shutting down */
8120 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
8121
8122 wl_signal_emit(&compositor->destroy_signal, compositor);
8123
8124 weston_compositor_xkb_destroy(compositor);
8125
Giulio Camuffo2d24e642015-10-03 16:25:15 +03008126 if (compositor->backend)
8127 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02008128
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03008129 /* The backend is responsible for destroying the heads. */
8130 assert(wl_list_empty(&compositor->head_list));
8131
Pekka Paalanen827b5d22016-06-29 11:54:26 +02008132 weston_plugin_api_destroy_list(compositor);
8133
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03008134 if (compositor->heads_changed_source)
8135 wl_event_source_remove(compositor->heads_changed_source);
8136
Leandro Ribeirof0149642019-12-18 15:52:18 -03008137 weston_log_scope_destroy(compositor->debug_scene);
Daniel Stonece62cb32018-07-20 09:46:24 +01008138 compositor->debug_scene = NULL;
Marius Vladda104eb2019-09-05 14:31:01 +03008139
Leandro Ribeirof0149642019-12-18 15:52:18 -03008140 weston_log_scope_destroy(compositor->timeline);
Marius Vladda104eb2019-09-05 14:31:01 +03008141 compositor->timeline = NULL;
Pekka Paalanena5630ea2017-10-12 13:13:42 +02008142
Giulio Camuffo459137b2014-10-11 23:56:24 +03008143 free(compositor);
8144}
8145
8146/** Instruct the compositor to exit.
8147 *
8148 * This functions does not directly destroy the compositor object, it merely
8149 * command it to start the tear down process. It is not guaranteed that the
8150 * tear down will happen immediately.
8151 *
8152 * \param compositor The compositor to tear down.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008153 *
8154 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03008155 */
8156WL_EXPORT void
8157weston_compositor_exit(struct weston_compositor *compositor)
8158{
8159 compositor->exit(compositor);
8160}
8161
8162/** Return the user data stored in the compositor.
8163 *
8164 * This function returns the user data pointer set with user_data parameter
8165 * to the \ref weston_compositor_create function.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008166 *
8167 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03008168 */
8169WL_EXPORT void *
8170weston_compositor_get_user_data(struct weston_compositor *compositor)
8171{
8172 return compositor->user_data;
8173}
Pekka Paalanendd186732016-06-03 14:49:54 +03008174
Pekka Paalanen50dbf382016-06-03 15:23:46 +03008175static const char * const backend_map[] = {
8176 [WESTON_BACKEND_DRM] = "drm-backend.so",
8177 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
8178 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
8179 [WESTON_BACKEND_RDP] = "rdp-backend.so",
8180 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
8181 [WESTON_BACKEND_X11] = "x11-backend.so",
8182};
8183
Pekka Paalanendd186732016-06-03 14:49:54 +03008184/** Load a backend into a weston_compositor
8185 *
8186 * A backend must be loaded to make a weston_compositor work. A backend
8187 * provides input and output capabilities, and determines the renderer to use.
8188 *
8189 * \param compositor A compositor that has not had a backend loaded yet.
8190 * \param backend Name of the backend file.
8191 * \param config_base A pointer to a backend-specific configuration
8192 * structure's 'base' member.
8193 *
8194 * \return 0 on success, or -1 on error.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008195 *
8196 * \ingroup compositor
Pekka Paalanendd186732016-06-03 14:49:54 +03008197 */
8198WL_EXPORT int
8199weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03008200 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03008201 struct weston_backend_config *config_base)
8202{
8203 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03008204 struct weston_backend_config *config_base);
8205
Pekka Paalanend7e35112017-08-29 17:04:12 +03008206 if (compositor->backend) {
8207 weston_log("Error: attempt to load a backend when one is already loaded\n");
8208 return -1;
8209 }
8210
Quentin Glidic887c0182016-07-10 11:00:53 +02008211 if (backend >= ARRAY_LENGTH(backend_map))
Pekka Paalanen50dbf382016-06-03 15:23:46 +03008212 return -1;
8213
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01008214 backend_init = weston_load_module(backend_map[backend], "weston_backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03008215 if (!backend_init)
8216 return -1;
8217
Pekka Paalanend7e35112017-08-29 17:04:12 +03008218 if (backend_init(compositor, config_base) < 0) {
8219 compositor->backend = NULL;
8220 return -1;
8221 }
8222
Pekka Paalanen90a5ffa2021-02-25 12:03:28 +02008223 if (!compositor->color_manager) {
8224 compositor->color_manager =
8225 weston_color_manager_noop_create(compositor);
8226 }
8227
8228 if (!compositor->color_manager)
8229 return -1;
8230
8231 if (!compositor->color_manager->init(compositor->color_manager))
8232 return -1;
8233
8234 weston_log("Color manager: %s\n", compositor->color_manager->name);
8235
Pekka Paalanend7e35112017-08-29 17:04:12 +03008236 return 0;
Pekka Paalanendd186732016-06-03 14:49:54 +03008237}
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008238
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008239/** weston_compositor_load_xwayland
8240 * \ingroup compositor
8241 */
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008242WL_EXPORT int
8243weston_compositor_load_xwayland(struct weston_compositor *compositor)
8244{
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01008245 int (*module_init)(struct weston_compositor *ec);
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008246
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01008247 module_init = weston_load_module("xwayland.so", "weston_module_init");
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008248 if (!module_init)
8249 return -1;
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01008250 if (module_init(compositor) < 0)
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008251 return -1;
8252 return 0;
8253}
Vasilis Liaskovitis486b4632018-10-10 16:14:55 +02008254
Pekka Paalanen5e79dd42021-03-29 14:11:49 +03008255/** Load Little CMS color manager plugin
8256 *
8257 * Calling this function before loading any backend sets Little CMS
8258 * as the active color matching module (CMM) instead of the default no-op
8259 * color manager.
8260 *
8261 * \ingroup compositor
8262 */
8263WL_EXPORT int
8264weston_compositor_load_color_manager(struct weston_compositor *compositor)
8265{
8266 struct weston_color_manager *
8267 (*cm_create)(struct weston_compositor *compositor);
8268
8269 if (compositor->color_manager) {
8270 weston_log("Error: Color manager '%s' is loaded, cannot load another.\n",
8271 compositor->color_manager->name);
8272 return -1;
8273 }
8274
8275 cm_create = weston_load_module("color-lcms.so", "weston_color_manager_create");
8276 if (!cm_create) {
8277 weston_log("Error: Could not load color-lcms.so.\n");
8278 return -1;
8279 }
8280
8281 compositor->color_manager = cm_create(compositor);
8282 if (!compositor->color_manager) {
8283 weston_log("Error: loading color-lcms.so failed.\n");
8284 return -1;
8285 }
8286
8287 return 0;
8288}
8289
Vasilis Liaskovitis486b4632018-10-10 16:14:55 +02008290/** Resolve an internal compositor error by disconnecting the client.
8291 *
8292 * This function is used in cases when the wl_buffer turns out
8293 * unusable and there is no fallback path.
8294 *
8295 * It is possible the fault is caused by a compositor bug, the underlying
8296 * graphics stack bug or normal behaviour, or perhaps a client mistake.
8297 * In any case, the options are to either composite garbage or nothing,
8298 * or disconnect the client. This is a helper function for the latter.
8299 *
8300 * The error is sent as an INVALID_OBJECT error on the client's wl_display.
8301 *
8302 * \param buffer The weston buffer that is unusable.
8303 * \param msg A custom error message attached to the protocol error.
8304 */
8305WL_EXPORT void
8306weston_buffer_send_server_error(struct weston_buffer *buffer,
8307 const char *msg)
8308{
8309 struct wl_client *client;
8310 struct wl_resource *display_resource;
8311 uint32_t id;
8312
8313 assert(buffer->resource);
8314 id = wl_resource_get_id(buffer->resource);
8315 client = wl_resource_get_client(buffer->resource);
8316 display_resource = wl_client_get_object(client, 1);
8317
8318 assert(display_resource);
8319 wl_resource_post_error(display_resource,
8320 WL_DISPLAY_ERROR_INVALID_OBJECT,
8321 "server error with "
8322 "wl_buffer@%u: %s", id, msg);
8323}
Ankit Nautiyal93dde242019-07-08 11:46:42 +05308324
8325WL_EXPORT void
8326weston_output_disable_planes_incr(struct weston_output *output)
8327{
8328 output->disable_planes++;
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05308329 /*
8330 * If disable_planes changes from 0 to non-zero, it means some type of
8331 * recording of content has started, and therefore protection level of
8332 * the protected surfaces must be updated to avoid the recording of
8333 * the protected content.
8334 */
8335 if (output->disable_planes == 1)
8336 weston_schedule_surface_protection_update(output->compositor);
Ankit Nautiyal93dde242019-07-08 11:46:42 +05308337}
8338
8339WL_EXPORT void
8340weston_output_disable_planes_decr(struct weston_output *output)
8341{
8342 output->disable_planes--;
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05308343 /*
8344 * If disable_planes changes from non-zero to 0, it means no content
8345 * recording is going on any more, and the protected and surfaces can be
8346 * shown without any apprehensions about content being recorded.
8347 */
8348 if (output->disable_planes == 0)
8349 weston_schedule_surface_protection_update(output->compositor);
8350
Ankit Nautiyal93dde242019-07-08 11:46:42 +05308351}