blob: 44b79423a6620778e3c1ac532fbb5808ab604968 [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"
Alexandros Frantzisfa48c5e2020-07-09 17:31:34 +030071#include "shared/signal.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040072#include "git-version.h"
Pekka Paalanencda14882019-04-04 15:41:02 +030073#include <libweston/version.h>
Pekka Paalanen27b377f2019-03-29 17:07:34 +020074#include <libweston/plugin-registry.h>
Marius Vlad00a6e012018-11-20 17:52:31 +020075#include "pixel-formats.h"
Marius Vlad63ef0782019-07-16 23:34:14 +030076#include "backend.h"
Marius Vlada72e3712019-07-10 13:46:39 +030077#include "libweston-internal.h"
Pekka Paalanen90a5ffa2021-02-25 12:03:28 +020078#include "color.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050079
Marius Vlad4e036292019-07-09 00:36:20 +030080#include "weston-log-internal.h"
81
leng.fang32af9fc2024-06-13 11:22:15 +080082#include "../aml-weston/aml-compositor.c"
Marius Vlad78984ee2019-06-11 00:05:08 +030083/**
84 * \defgroup head Head
85 * \defgroup output Output
86 * \defgroup compositor Compositor
87 */
88
Pekka Paalanen0513a952014-05-21 16:17:27 +030089#define DEFAULT_REPAINT_WINDOW 7 /* milliseconds */
90
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020091static void
Pekka Paalanend72bad22017-03-29 17:01:41 +030092weston_output_update_matrix(struct weston_output *output);
93
94static void
Alexander Larsson0b135062013-05-28 16:23:36 +020095weston_output_transform_scale_init(struct weston_output *output,
96 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020097
Rob Bradford27b17932013-06-26 18:08:46 +010098static void
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +030099weston_compositor_build_view_list(struct weston_compositor *compositor,
100 struct weston_output *output);
Rob Bradford27b17932013-06-26 18:08:46 +0100101
Pekka Paalanendcac3512017-12-08 14:13:34 +0200102static char *
103weston_output_create_heads_string(struct weston_output *output);
104
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +0300105static struct weston_paint_node *
106weston_paint_node_create(struct weston_surface *surface,
107 struct weston_view *view,
108 struct weston_output *output)
109{
110 struct weston_paint_node *pnode;
Pekka Paalanen90a5ffa2021-02-25 12:03:28 +0200111 struct weston_paint_node *existing_node;
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +0300112
113 assert(view->surface == surface);
114
115 pnode = zalloc(sizeof *pnode);
116 if (!pnode)
117 return NULL;
118
Pekka Paalanen90a5ffa2021-02-25 12:03:28 +0200119 /*
120 * Invariant: all paint nodes with the same surface+output have the
121 * same surf_xform state.
122 */
Pekka Paalanen26b04f02022-01-24 13:09:01 +0200123 wl_list_for_each(existing_node, &surface->paint_node_list, surface_link) {
Pekka Paalanen90a5ffa2021-02-25 12:03:28 +0200124 assert(existing_node->surface == surface);
125 if (existing_node->output != output)
126 continue;
127
128 weston_surface_color_transform_copy(&pnode->surf_xform,
129 &existing_node->surf_xform);
130 pnode->surf_xform_valid = existing_node->surf_xform_valid;
131 break;
132 }
133
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +0300134 pnode->surface = surface;
135 wl_list_insert(&surface->paint_node_list, &pnode->surface_link);
136
137 pnode->view = view;
138 wl_list_insert(&view->paint_node_list, &pnode->view_link);
139
140 pnode->output = output;
141 wl_list_insert(&output->paint_node_list, &pnode->output_link);
142
Pekka Paalanen2fddc532021-04-30 17:41:29 +0300143 wl_list_init(&pnode->z_order_link);
144
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +0300145 return pnode;
146}
147
148static void
149weston_paint_node_destroy(struct weston_paint_node *pnode)
150{
151 assert(pnode->view->surface == pnode->surface);
152 wl_list_remove(&pnode->surface_link);
153 wl_list_remove(&pnode->view_link);
154 wl_list_remove(&pnode->output_link);
Pekka Paalanen2fddc532021-04-30 17:41:29 +0300155 wl_list_remove(&pnode->z_order_link);
Pekka Paalanen90a5ffa2021-02-25 12:03:28 +0200156 assert(pnode->surf_xform_valid || !pnode->surf_xform.transform);
157 weston_surface_color_transform_fini(&pnode->surf_xform);
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +0300158 free(pnode);
159}
160
Pekka Paalanen6528c032017-03-28 15:27:10 +0300161/** Send wl_output events for mode and scale changes
162 *
163 * \param head Send on all resources bound to this head.
164 * \param mode_changed If true, send the current mode.
165 * \param scale_changed If true, send the current scale.
166 */
167static void
168weston_mode_switch_send_events(struct weston_head *head,
169 bool mode_changed, bool scale_changed)
170{
171 struct weston_output *output = head->output;
172 struct wl_resource *resource;
173 int version;
174
175 wl_resource_for_each(resource, &head->resource_list) {
176 if (mode_changed) {
177 wl_output_send_mode(resource,
178 output->current_mode->flags,
179 output->current_mode->width,
180 output->current_mode->height,
181 output->current_mode->refresh);
182 }
183
184 version = wl_resource_get_version(resource);
185 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION && scale_changed)
186 wl_output_send_scale(resource, output->current_scale);
187
188 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
189 wl_output_send_done(resource);
190 }
Roman Gilge97391c2019-03-29 13:01:06 +0100191 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
192 zxdg_output_v1_send_logical_position(resource,
193 output->x,
194 output->y);
195 zxdg_output_v1_send_logical_size(resource,
196 output->width,
197 output->height);
198 zxdg_output_v1_send_done(resource);
199 }
Pekka Paalanen6528c032017-03-28 15:27:10 +0300200}
201
202static void
203weston_mode_switch_finish(struct weston_output *output,
204 int mode_changed, int scale_changed)
Alex Wu2dda6042012-04-17 17:20:47 +0800205{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200206 struct weston_seat *seat;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300207 struct weston_head *head;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200208 pixman_region32_t old_output_region;
Alexander Larsson355748e2013-05-28 16:23:38 +0200209
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200210 pixman_region32_init(&old_output_region);
211 pixman_region32_copy(&old_output_region, &output->region);
212
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200213 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +0200214 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200215
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200216 pixman_region32_init_rect(&output->region, output->x, output->y,
217 output->width, output->height);
218
219 weston_output_update_matrix(output);
220
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200221 /* If a pointer falls outside the outputs new geometry, move it to its
222 * lower-right corner */
223 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500224 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200225 int32_t x, y;
226
227 if (!pointer)
228 continue;
229
230 x = wl_fixed_to_int(pointer->x);
231 y = wl_fixed_to_int(pointer->y);
232
233 if (!pixman_region32_contains_point(&old_output_region,
234 x, y, NULL) ||
235 pixman_region32_contains_point(&output->region,
236 x, y, NULL))
237 continue;
238
239 if (x >= output->x + output->width)
240 x = output->x + output->width - 1;
241 if (y >= output->y + output->height)
242 y = output->y + output->height - 1;
243
244 pointer->x = wl_fixed_from_int(x);
245 pointer->y = wl_fixed_from_int(y);
246 }
247
248 pixman_region32_fini(&old_output_region);
249
Derek Foremandd4cd332014-11-10 10:29:59 -0600250 if (!mode_changed && !scale_changed)
251 return;
252
Hardening57388e42013-09-18 23:56:36 +0200253 /* notify clients of the changes */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300254 wl_list_for_each(head, &output->head_list, output_link)
255 weston_mode_switch_send_events(head,
256 mode_changed, scale_changed);
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600257}
258
David Fort0de859e2016-05-27 23:22:57 +0200259static void
260weston_compositor_reflow_outputs(struct weston_compositor *compositor,
261 struct weston_output *resized_output, int delta_width);
262
Marius Vlad55d87362019-06-11 01:15:35 +0300263/**
264 * \ingroup output
265 */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600266WL_EXPORT int
267weston_output_mode_set_native(struct weston_output *output,
268 struct weston_mode *mode,
269 int32_t scale)
270{
271 int ret;
272 int mode_changed = 0, scale_changed = 0;
David Fort0de859e2016-05-27 23:22:57 +0200273 int32_t old_width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600274
275 if (!output->switch_mode)
276 return -1;
277
278 if (!output->original_mode) {
279 mode_changed = 1;
280 ret = output->switch_mode(output, mode);
281 if (ret < 0)
282 return ret;
283 if (output->current_scale != scale) {
284 scale_changed = 1;
285 output->current_scale = scale;
Hardening57388e42013-09-18 23:56:36 +0200286 }
287 }
288
David Fort0de859e2016-05-27 23:22:57 +0200289 old_width = output->width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600290 output->native_mode = mode;
291 output->native_scale = scale;
292
293 weston_mode_switch_finish(output, mode_changed, scale_changed);
294
David Fort0de859e2016-05-27 23:22:57 +0200295 if (mode_changed || scale_changed) {
296 weston_compositor_reflow_outputs(output->compositor, output, output->width - old_width);
297
298 wl_signal_emit(&output->compositor->output_resized_signal, output);
299 }
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600300 return 0;
301}
302
Marius Vlad55d87362019-06-11 01:15:35 +0300303/**
304 * \ingroup output
305 */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600306WL_EXPORT int
307weston_output_mode_switch_to_native(struct weston_output *output)
308{
309 int ret;
310 int mode_changed = 0, scale_changed = 0;
311
312 if (!output->switch_mode)
313 return -1;
314
315 if (!output->original_mode) {
316 weston_log("already in the native mode\n");
317 return -1;
318 }
319 /* the non fullscreen clients haven't seen a mode set since we
320 * switched into a temporary, so we need to notify them if the
321 * mode at that time is different from the native mode now.
322 */
323 mode_changed = (output->original_mode != output->native_mode);
324 scale_changed = (output->original_scale != output->native_scale);
325
326 ret = output->switch_mode(output, output->native_mode);
327 if (ret < 0)
328 return ret;
329
330 output->current_scale = output->native_scale;
331
332 output->original_mode = NULL;
333 output->original_scale = 0;
334
335 weston_mode_switch_finish(output, mode_changed, scale_changed);
336
337 return 0;
338}
339
Marius Vlad55d87362019-06-11 01:15:35 +0300340/**
341 * \ingroup output
342 */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600343WL_EXPORT int
344weston_output_mode_switch_to_temporary(struct weston_output *output,
345 struct weston_mode *mode,
346 int32_t scale)
347{
348 int ret;
349
350 if (!output->switch_mode)
351 return -1;
352
353 /* original_mode is the last mode non full screen clients have seen,
354 * so we shouldn't change it if we already have one set.
355 */
356 if (!output->original_mode) {
357 output->original_mode = output->native_mode;
358 output->original_scale = output->native_scale;
359 }
360 ret = output->switch_mode(output, mode);
361 if (ret < 0)
362 return ret;
363
364 output->current_scale = scale;
365
366 weston_mode_switch_finish(output, 0, 0);
367
368 return 0;
Alex Wu2dda6042012-04-17 17:20:47 +0800369}
370
Benjamin Franzke06286262011-05-06 19:12:33 +0200371static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300372region_init_infinite(pixman_region32_t *region)
373{
374 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
375 UINT32_MAX, UINT32_MAX);
376}
377
Pekka Paalanene67858b2013-04-25 13:57:42 +0300378static struct weston_subsurface *
379weston_surface_to_subsurface(struct weston_surface *surface);
380
Jason Ekstranda7af7042013-10-12 22:38:11 -0500381WL_EXPORT struct weston_view *
382weston_view_create(struct weston_surface *surface)
383{
384 struct weston_view *view;
385
Bryce Harringtonde16d892014-11-20 22:21:57 -0800386 view = zalloc(sizeof *view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500387 if (view == NULL)
388 return NULL;
389
390 view->surface = surface;
Daniel Stonefb4869d2016-12-09 16:27:54 +0000391 view->plane = &surface->compositor->primary_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500392
Jason Ekstranda7af7042013-10-12 22:38:11 -0500393 /* Assign to surface */
394 wl_list_insert(&surface->views, &view->surface_link);
395
396 wl_signal_init(&view->destroy_signal);
397 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300398 wl_list_init(&view->layer_link.link);
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +0300399 wl_list_init(&view->paint_node_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500400
Jason Ekstranda7af7042013-10-12 22:38:11 -0500401 pixman_region32_init(&view->clip);
402
403 view->alpha = 1.0;
leng.fangdbaf6fa2024-06-20 19:31:04 +0800404 view->visible = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500405 pixman_region32_init(&view->transform.opaque);
406
407 wl_list_init(&view->geometry.transformation_list);
408 wl_list_insert(&view->geometry.transformation_list,
409 &view->transform.position.link);
410 weston_matrix_init(&view->transform.position.matrix);
411 wl_list_init(&view->geometry.child_list);
Pekka Paalanen380adf52015-02-16 14:39:11 +0200412 pixman_region32_init(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500413 pixman_region32_init(&view->transform.boundingbox);
414 view->transform.dirty = 1;
leng.fang32af9fc2024-06-13 11:22:15 +0800415#ifdef MESON_VIDEO_PLAN_SUPPORT
416 pixman_region32_init(&view->transform.transparent);
417#endif
Jason Ekstranda7af7042013-10-12 22:38:11 -0500418
Jason Ekstranda7af7042013-10-12 22:38:11 -0500419 return view;
420}
421
Pekka Paalanen133e4392014-09-23 22:08:46 -0400422struct weston_presentation_feedback {
423 struct wl_resource *resource;
424
425 /* XXX: could use just wl_resource_get_link() instead */
426 struct wl_list link;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200427
428 /* The per-surface feedback flags */
429 uint32_t psf_flags;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400430};
431
432static void
433weston_presentation_feedback_discard(
434 struct weston_presentation_feedback *feedback)
435{
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200436 wp_presentation_feedback_send_discarded(feedback->resource);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400437 wl_resource_destroy(feedback->resource);
438}
439
440static void
441weston_presentation_feedback_discard_list(struct wl_list *list)
442{
443 struct weston_presentation_feedback *feedback, *tmp;
444
445 wl_list_for_each_safe(feedback, tmp, list, link)
446 weston_presentation_feedback_discard(feedback);
447}
448
449static void
450weston_presentation_feedback_present(
451 struct weston_presentation_feedback *feedback,
452 struct weston_output *output,
453 uint32_t refresh_nsec,
454 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200455 uint64_t seq,
456 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400457{
458 struct wl_client *client = wl_resource_get_client(feedback->resource);
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300459 struct weston_head *head;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400460 struct wl_resource *o;
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200461 uint32_t tv_sec_hi;
462 uint32_t tv_sec_lo;
463 uint32_t tv_nsec;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300464 bool done = false;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400465
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300466 wl_list_for_each(head, &output->head_list, output_link) {
467 wl_resource_for_each(o, &head->resource_list) {
468 if (wl_resource_get_client(o) != client)
469 continue;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400470
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300471 wp_presentation_feedback_send_sync_output(feedback->resource, o);
472 done = true;
473 }
474
475 /* For clone mode, send it for just one wl_output global,
476 * they are all equivalent anyway.
477 */
478 if (done)
479 break;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400480 }
481
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200482 timespec_to_proto(ts, &tv_sec_hi, &tv_sec_lo, &tv_nsec);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200483 wp_presentation_feedback_send_presented(feedback->resource,
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200484 tv_sec_hi, tv_sec_lo, tv_nsec,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200485 refresh_nsec,
486 seq >> 32, seq & 0xffffffff,
487 flags | feedback->psf_flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400488 wl_resource_destroy(feedback->resource);
489}
490
491static void
492weston_presentation_feedback_present_list(struct wl_list *list,
493 struct weston_output *output,
494 uint32_t refresh_nsec,
495 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200496 uint64_t seq,
497 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400498{
499 struct weston_presentation_feedback *feedback, *tmp;
500
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200501 assert(!(flags & WP_PRESENTATION_FEEDBACK_INVALID) ||
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200502 wl_list_empty(list));
503
Pekka Paalanen133e4392014-09-23 22:08:46 -0400504 wl_list_for_each_safe(feedback, tmp, list, link)
505 weston_presentation_feedback_present(feedback, output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200506 refresh_nsec, ts, seq,
507 flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400508}
509
Jason Ekstrand7b982072014-05-20 14:33:03 -0500510static void
511surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
512{
513 struct weston_surface_state *state =
514 container_of(listener, struct weston_surface_state,
515 buffer_destroy_listener);
516
517 state->buffer = NULL;
518}
519
520static void
521weston_surface_state_init(struct weston_surface_state *state)
522{
523 state->newly_attached = 0;
524 state->buffer = NULL;
525 state->buffer_destroy_listener.notify =
526 surface_state_handle_buffer_destroy;
527 state->sx = 0;
528 state->sy = 0;
529
Derek Foreman152254b2015-11-26 14:17:48 -0600530 pixman_region32_init(&state->damage_surface);
531 pixman_region32_init(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500532 pixman_region32_init(&state->opaque);
533 region_init_infinite(&state->input);
534
535 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400536 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500537
538 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
539 state->buffer_viewport.buffer.scale = 1;
540 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
541 state->buffer_viewport.surface.width = -1;
542 state->buffer_viewport.changed = 0;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300543
544 state->acquire_fence_fd = -1;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +0530545
546 state->desired_protection = WESTON_HDCP_DISABLE;
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +0530547 state->protection_mode = WESTON_SURFACE_PROTECTION_MODE_RELAXED;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500548}
549
550static void
551weston_surface_state_fini(struct weston_surface_state *state)
552{
Vlad Zahorodniic2b97472021-07-29 11:16:51 +0300553 struct wl_resource *cb, *next;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500554
Vlad Zahorodniic2b97472021-07-29 11:16:51 +0300555 wl_resource_for_each_safe(cb, next, &state->frame_callback_list)
556 wl_resource_destroy(cb);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500557
Pekka Paalanen133e4392014-09-23 22:08:46 -0400558 weston_presentation_feedback_discard_list(&state->feedback_list);
559
Jason Ekstrand7b982072014-05-20 14:33:03 -0500560 pixman_region32_fini(&state->input);
561 pixman_region32_fini(&state->opaque);
Derek Foreman152254b2015-11-26 14:17:48 -0600562 pixman_region32_fini(&state->damage_surface);
563 pixman_region32_fini(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500564
Jason Ekstrand7b982072014-05-20 14:33:03 -0500565 state->buffer = NULL;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300566
567 fd_clear(&state->acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +0300568 weston_buffer_release_reference(&state->buffer_release_ref, NULL);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500569}
570
571static void
572weston_surface_state_set_buffer(struct weston_surface_state *state,
573 struct weston_buffer *buffer)
574{
575 if (state->buffer == buffer)
576 return;
577
leng.fang32af9fc2024-06-13 11:22:15 +0800578 /*if (state->buffer)
579 wl_list_remove(&state->buffer_destroy_listener.link);*/
Jason Ekstrand7b982072014-05-20 14:33:03 -0500580 state->buffer = buffer;
leng.fang32af9fc2024-06-13 11:22:15 +0800581 /*if (state->buffer)
Jason Ekstrand7b982072014-05-20 14:33:03 -0500582 wl_signal_add(&state->buffer->destroy_signal,
leng.fang32af9fc2024-06-13 11:22:15 +0800583 &state->buffer_destroy_listener);*/
Jason Ekstrand7b982072014-05-20 14:33:03 -0500584}
585
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500586WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500587weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500588{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500589 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400590
Bryce Harringtonde16d892014-11-20 22:21:57 -0800591 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400592 if (surface == NULL)
593 return NULL;
594
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500595 wl_signal_init(&surface->destroy_signal);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +0800596 wl_signal_init(&surface->commit_signal);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500597
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500598 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200599 surface->ref_count = 1;
leng.fangdbaf6fa2024-06-20 19:31:04 +0800600 surface->surface_id = compositor->next_surface_id++;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400601
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200602 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
603 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200604 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
605 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500606
607 weston_surface_state_init(&surface->pending);
608
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400609 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500610 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300611 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400612
Jason Ekstranda7af7042013-10-12 22:38:11 -0500613 wl_list_init(&surface->views);
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +0300614 wl_list_init(&surface->paint_node_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500615
616 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400617 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500618
Pekka Paalanene67858b2013-04-25 13:57:42 +0300619 wl_list_init(&surface->subsurface_list);
620 wl_list_init(&surface->subsurface_list_pending);
621
Jason Ekstrand1e059042014-10-16 10:55:19 -0500622 weston_matrix_init(&surface->buffer_to_surface_matrix);
623 weston_matrix_init(&surface->surface_to_buffer_matrix);
624
Jonas Ådahld3414f22016-07-22 17:56:31 +0800625 wl_list_init(&surface->pointer_constraints);
626
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300627 surface->acquire_fence_fd = -1;
628
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +0530629 surface->desired_protection = WESTON_HDCP_DISABLE;
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +0530630 surface->current_protection = WESTON_HDCP_DISABLE;
631 surface->protection_mode = WESTON_SURFACE_PROTECTION_MODE_RELAXED;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +0530632
leng.fang32af9fc2024-06-13 11:22:15 +0800633 weston_init_video_surface_info(surface);
634
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400635 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500636}
637
Alex Wu8811bf92012-02-28 18:07:54 +0800638WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500639weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200640 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500641{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100642 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Philipp Zabel195dade2018-09-03 19:44:59 +0200643 surface->is_opaque = !(alpha < 1.0);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500644}
645
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400646WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500647weston_view_to_global_float(struct weston_view *view,
648 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200649{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500650 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200651 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
652
Jason Ekstranda7af7042013-10-12 22:38:11 -0500653 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200654
655 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200656 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700657 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200658 v.f[3]);
659 *x = 0;
660 *y = 0;
661 return;
662 }
663
664 *x = v.f[0] / v.f[3];
665 *y = v.f[1] / v.f[3];
666 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500667 *x = sx + view->geometry.x;
668 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200669 }
670}
671
Pekka Paalanen0df44772020-02-14 14:50:03 +0200672/** Transform a point to buffer coordinates
673 *
674 * \param width Surface width.
675 * \param height Surface height.
676 * \param transform Buffer transform.
677 * \param scale Buffer scale.
678 * \param sx Surface x coordinate of a point.
679 * \param sy Surface y coordinate of a point.
680 * \param[out] bx Buffer x coordinate of the point.
681 * \param[out] by Buffer Y coordinate of the point.
682 *
683 * Converts the given surface-local coordinates to buffer coordinates
684 * according to the given buffer transform and scale.
685 * This ignores wp_viewport.
686 *
687 * The given width and height must be the result of inverse scaled and
688 * inverse transformed buffer size.
689 */
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500690WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200691weston_transformed_coord(int width, int height,
692 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200693 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200694 float sx, float sy, float *bx, float *by)
695{
696 switch (transform) {
697 case WL_OUTPUT_TRANSFORM_NORMAL:
698 default:
699 *bx = sx;
700 *by = sy;
701 break;
702 case WL_OUTPUT_TRANSFORM_FLIPPED:
703 *bx = width - sx;
704 *by = sy;
705 break;
706 case WL_OUTPUT_TRANSFORM_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200707 *bx = sy;
708 *by = width - sx;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200709 break;
710 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200711 *bx = sy;
712 *by = sx;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200713 break;
714 case WL_OUTPUT_TRANSFORM_180:
715 *bx = width - sx;
716 *by = height - sy;
717 break;
718 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
719 *bx = sx;
720 *by = height - sy;
721 break;
722 case WL_OUTPUT_TRANSFORM_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200723 *bx = height - sy;
724 *by = sx;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200725 break;
726 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200727 *bx = height - sy;
728 *by = width - sx;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200729 break;
730 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200731
732 *bx *= scale;
733 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200734}
735
Pekka Paalanen0df44772020-02-14 14:50:03 +0200736/** Transform a rectangle to buffer coordinates
737 *
738 * \param width Surface width.
739 * \param height Surface height.
740 * \param transform Buffer transform.
741 * \param scale Buffer scale.
742 * \param rect Rectangle in surface coordinates.
743 * \return Rectangle in buffer coordinates.
744 *
745 * Converts the given surface-local rectangle to buffer coordinates
746 * according to the given buffer transform and scale. The resulting
747 * rectangle is guaranteed to be well-formed.
748 * This ignores wp_viewport.
749 *
750 * The given width and height must be the result of inverse scaled and
751 * inverse transformed buffer size.
752 */
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200753WL_EXPORT pixman_box32_t
754weston_transformed_rect(int width, int height,
755 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200756 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200757 pixman_box32_t rect)
758{
759 float x1, x2, y1, y2;
760
761 pixman_box32_t ret;
762
Alexander Larsson4ea95522013-05-22 14:41:37 +0200763 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200764 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200765 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200766 rect.x2, rect.y2, &x2, &y2);
767
768 if (x1 <= x2) {
769 ret.x1 = x1;
770 ret.x2 = x2;
771 } else {
772 ret.x1 = x2;
773 ret.x2 = x1;
774 }
775
776 if (y1 <= y2) {
777 ret.y1 = y1;
778 ret.y2 = y2;
779 } else {
780 ret.y1 = y2;
781 ret.y2 = y1;
782 }
783
784 return ret;
785}
786
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600787/** Transform a region by a matrix, restricted to axis-aligned transformations
788 *
789 * Warning: This function does not work for projective, affine, or matrices
790 * that encode arbitrary rotations. Only 90-degree step rotations are
791 * supported.
792 */
793WL_EXPORT void
794weston_matrix_transform_region(pixman_region32_t *dest,
795 struct weston_matrix *matrix,
796 pixman_region32_t *src)
797{
798 pixman_box32_t *src_rects, *dest_rects;
799 int nrects, i;
800
801 src_rects = pixman_region32_rectangles(src, &nrects);
802 dest_rects = malloc(nrects * sizeof(*dest_rects));
803 if (!dest_rects)
804 return;
805
806 for (i = 0; i < nrects; i++) {
807 struct weston_vector vec1 = {{
808 src_rects[i].x1, src_rects[i].y1, 0, 1
809 }};
810 weston_matrix_transform(matrix, &vec1);
811 vec1.f[0] /= vec1.f[3];
812 vec1.f[1] /= vec1.f[3];
813
814 struct weston_vector vec2 = {{
815 src_rects[i].x2, src_rects[i].y2, 0, 1
816 }};
817 weston_matrix_transform(matrix, &vec2);
818 vec2.f[0] /= vec2.f[3];
819 vec2.f[1] /= vec2.f[3];
820
821 if (vec1.f[0] < vec2.f[0]) {
822 dest_rects[i].x1 = floor(vec1.f[0]);
823 dest_rects[i].x2 = ceil(vec2.f[0]);
824 } else {
825 dest_rects[i].x1 = floor(vec2.f[0]);
826 dest_rects[i].x2 = ceil(vec1.f[0]);
827 }
828
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600829 if (vec1.f[1] < vec2.f[1]) {
830 dest_rects[i].y1 = floor(vec1.f[1]);
831 dest_rects[i].y2 = ceil(vec2.f[1]);
832 } else {
833 dest_rects[i].y1 = floor(vec2.f[1]);
834 dest_rects[i].y2 = ceil(vec1.f[1]);
835 }
836 }
837
838 pixman_region32_clear(dest);
839 pixman_region32_init_rects(dest, dest_rects, nrects);
840 free(dest_rects);
841}
842
Pekka Paalanen0df44772020-02-14 14:50:03 +0200843/** Transform a region to buffer coordinates
844 *
845 * \param width Surface width.
846 * \param height Surface height.
847 * \param transform Buffer transform.
848 * \param scale Buffer scale.
849 * \param[in] src Region in surface coordinates.
850 * \param[out] dest Resulting region in buffer coordinates.
851 *
852 * Converts the given surface-local region to buffer coordinates
853 * according to the given buffer transform and scale.
854 * This ignores wp_viewport.
855 *
856 * The given width and height must be the result of inverse scaled and
857 * inverse transformed buffer size.
858 *
859 * src and dest are allowed to point to the same memory for in-place conversion.
860 */
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200861WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500862weston_transformed_region(int width, int height,
863 enum wl_output_transform transform,
864 int32_t scale,
865 pixman_region32_t *src, pixman_region32_t *dest)
866{
867 pixman_box32_t *src_rects, *dest_rects;
868 int nrects, i;
869
870 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
871 if (src != dest)
872 pixman_region32_copy(dest, src);
873 return;
874 }
875
876 src_rects = pixman_region32_rectangles(src, &nrects);
877 dest_rects = malloc(nrects * sizeof(*dest_rects));
878 if (!dest_rects)
879 return;
880
881 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
882 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
883 } else {
884 for (i = 0; i < nrects; i++) {
885 switch (transform) {
886 default:
887 case WL_OUTPUT_TRANSFORM_NORMAL:
888 dest_rects[i].x1 = src_rects[i].x1;
889 dest_rects[i].y1 = src_rects[i].y1;
890 dest_rects[i].x2 = src_rects[i].x2;
891 dest_rects[i].y2 = src_rects[i].y2;
892 break;
893 case WL_OUTPUT_TRANSFORM_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200894 dest_rects[i].x1 = src_rects[i].y1;
895 dest_rects[i].y1 = width - src_rects[i].x2;
896 dest_rects[i].x2 = src_rects[i].y2;
897 dest_rects[i].y2 = width - src_rects[i].x1;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500898 break;
899 case WL_OUTPUT_TRANSFORM_180:
900 dest_rects[i].x1 = width - src_rects[i].x2;
901 dest_rects[i].y1 = height - src_rects[i].y2;
902 dest_rects[i].x2 = width - src_rects[i].x1;
903 dest_rects[i].y2 = height - src_rects[i].y1;
904 break;
905 case WL_OUTPUT_TRANSFORM_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200906 dest_rects[i].x1 = height - src_rects[i].y2;
907 dest_rects[i].y1 = src_rects[i].x1;
908 dest_rects[i].x2 = height - src_rects[i].y1;
909 dest_rects[i].y2 = src_rects[i].x2;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500910 break;
911 case WL_OUTPUT_TRANSFORM_FLIPPED:
912 dest_rects[i].x1 = width - src_rects[i].x2;
913 dest_rects[i].y1 = src_rects[i].y1;
914 dest_rects[i].x2 = width - src_rects[i].x1;
915 dest_rects[i].y2 = src_rects[i].y2;
916 break;
917 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200918 dest_rects[i].x1 = src_rects[i].y1;
919 dest_rects[i].y1 = src_rects[i].x1;
920 dest_rects[i].x2 = src_rects[i].y2;
921 dest_rects[i].y2 = src_rects[i].x2;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500922 break;
923 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
924 dest_rects[i].x1 = src_rects[i].x1;
925 dest_rects[i].y1 = height - src_rects[i].y2;
926 dest_rects[i].x2 = src_rects[i].x2;
927 dest_rects[i].y2 = height - src_rects[i].y1;
928 break;
929 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200930 dest_rects[i].x1 = height - src_rects[i].y2;
931 dest_rects[i].y1 = width - src_rects[i].x2;
932 dest_rects[i].x2 = height - src_rects[i].y1;
933 dest_rects[i].y2 = width - src_rects[i].x1;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500934 break;
935 }
936 }
937 }
938
939 if (scale != 1) {
940 for (i = 0; i < nrects; i++) {
941 dest_rects[i].x1 *= scale;
942 dest_rects[i].x2 *= scale;
943 dest_rects[i].y1 *= scale;
944 dest_rects[i].y2 *= scale;
945 }
946 }
947
948 pixman_region32_clear(dest);
949 pixman_region32_init_rects(dest, dest_rects, nrects);
950 free(dest_rects);
951}
952
Jonny Lamb74130762013-11-26 18:19:46 +0100953static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300954viewport_surface_to_buffer(struct weston_surface *surface,
955 float sx, float sy, float *bx, float *by)
Jonny Lamb74130762013-11-26 18:19:46 +0100956{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200957 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200958 double src_width, src_height;
959 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200960
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200961 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
962 if (vp->surface.width == -1) {
963 *bx = sx;
964 *by = sy;
965 return;
966 }
Jonny Lamb74130762013-11-26 18:19:46 +0100967
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200968 src_x = 0.0;
969 src_y = 0.0;
970 src_width = surface->width_from_buffer;
971 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100972 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200973 src_x = wl_fixed_to_double(vp->buffer.src_x);
974 src_y = wl_fixed_to_double(vp->buffer.src_y);
975 src_width = wl_fixed_to_double(vp->buffer.src_width);
976 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100977 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200978
979 *bx = sx * src_width / surface->width + src_x;
980 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100981}
982
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500983WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200984weston_surface_to_buffer_float(struct weston_surface *surface,
985 float sx, float sy, float *bx, float *by)
986{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200987 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
988
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300989 /* first transform coordinates if the viewport is set */
990 viewport_surface_to_buffer(surface, sx, sy, bx, by);
Jonny Lamb74130762013-11-26 18:19:46 +0100991
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500992 weston_transformed_coord(surface->width_from_buffer,
993 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200994 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100995 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200996}
997
Derek Foreman99739672015-12-03 16:38:11 -0600998/** Transform a rectangle from surface coordinates to buffer coordinates
999 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03001000 * \param surface The surface to fetch wp_viewport and buffer transformation
Derek Foreman99739672015-12-03 16:38:11 -06001001 * from.
1002 * \param rect The rectangle to transform.
1003 * \return The transformed rectangle.
1004 *
1005 * Viewport and buffer transformations can only do translation, scaling,
1006 * and rotations in 90-degree steps. Therefore the only loss in the
1007 * conversion is coordinate rounding.
1008 *
1009 * However, some coordinate rounding takes place as an intermediate
1010 * step before the buffer scale factor is applied, so the rectangle
1011 * boundary may not be exactly as expected.
1012 *
1013 * This is OK for damage tracking since a little extra coverage is
1014 * not a problem.
1015 */
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +02001016WL_EXPORT pixman_box32_t
1017weston_surface_to_buffer_rect(struct weston_surface *surface,
1018 pixman_box32_t rect)
1019{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001020 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +01001021 float xf, yf;
1022
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03001023 /* first transform box coordinates if the viewport is set */
1024 viewport_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
Jonny Lamb74130762013-11-26 18:19:46 +01001025 rect.x1 = floorf(xf);
1026 rect.y1 = floorf(yf);
1027
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03001028 viewport_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
Derek Foremane2e15ac2015-12-01 13:00:43 -06001029 rect.x2 = ceilf(xf);
1030 rect.y2 = ceilf(yf);
Jonny Lamb74130762013-11-26 18:19:46 +01001031
Jason Ekstrandd0cebc32014-04-21 20:56:46 -05001032 return weston_transformed_rect(surface->width_from_buffer,
1033 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001034 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +02001035 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +02001036}
1037
Pekka Paalanene54e31c2015-03-04 14:23:28 +02001038/** Transform a region from surface coordinates to buffer coordinates
1039 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03001040 * \param surface The surface to fetch wp_viewport and buffer transformation
Pekka Paalanene54e31c2015-03-04 14:23:28 +02001041 * from.
Marius Vlada2dace22019-06-12 16:05:44 +03001042 * \param[in] surface_region The region in surface coordinates.
1043 * \param[out] buffer_region The region converted to buffer coordinates.
Pekka Paalanene54e31c2015-03-04 14:23:28 +02001044 *
1045 * Buffer_region must be init'd, but will be completely overwritten.
1046 *
1047 * Viewport and buffer transformations can only do translation, scaling,
1048 * and rotations in 90-degree steps. Therefore the only loss in the
Derek Foreman99739672015-12-03 16:38:11 -06001049 * conversion is from the coordinate rounding that takes place in
1050 * \ref weston_surface_to_buffer_rect.
Marius Vlada2dace22019-06-12 16:05:44 +03001051 *
Pekka Paalanene54e31c2015-03-04 14:23:28 +02001052 */
1053WL_EXPORT void
1054weston_surface_to_buffer_region(struct weston_surface *surface,
1055 pixman_region32_t *surface_region,
1056 pixman_region32_t *buffer_region)
1057{
1058 pixman_box32_t *src_rects, *dest_rects;
1059 int nrects, i;
1060
1061 src_rects = pixman_region32_rectangles(surface_region, &nrects);
1062 dest_rects = malloc(nrects * sizeof(*dest_rects));
1063 if (!dest_rects)
1064 return;
1065
1066 for (i = 0; i < nrects; i++) {
1067 dest_rects[i] = weston_surface_to_buffer_rect(surface,
1068 src_rects[i]);
1069 }
1070
1071 pixman_region32_fini(buffer_region);
1072 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
1073 free(dest_rects);
1074}
1075
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +02001076WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001077weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001078 struct weston_plane *plane)
1079{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001080 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001081 return;
1082
Jason Ekstranda7af7042013-10-12 22:38:11 -05001083 weston_view_damage_below(view);
1084 view->plane = plane;
1085 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001086}
1087
Pekka Paalanen51723d52015-02-17 13:10:01 +02001088/** Inflict damage on the plane where the view is visible.
1089 *
1090 * \param view The view that causes the damage.
1091 *
1092 * If the view is currently on a plane (including the primary plane),
1093 * take the view's boundingbox, subtract all the opaque views that cover it,
1094 * and add the remaining region as damage to the plane. This corresponds
1095 * to the damage inflicted to the plane if this view disappeared.
1096 *
1097 * A repaint is scheduled for this view.
1098 *
1099 * The region of all opaque views covering this view is stored in
1100 * weston_view::clip and updated by view_accumulate_damage() during
1101 * weston_output_repaint(). Specifically, that region matches the
1102 * scenegraph as it was last painted.
1103 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001104WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001105weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001106{
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001107 pixman_region32_t damage;
limin.tian230ec582024-07-30 09:35:17 +00001108 if (!view->surface->is_video_surface && !(view->alpha == 0.0)) {
1109 pixman_region32_init(&damage);
1110 pixman_region32_subtract(&damage, &view->transform.boundingbox,
1111 &view->clip);
1112 if (view->plane)
1113 pixman_region32_union(&view->plane->damage,
1114 &view->plane->damage, &damage);
1115 pixman_region32_fini(&damage);
1116 }
Kristian Høgsberga3a784a2013-11-13 21:33:43 -08001117 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001118}
1119
Pekka Paalanen01e00682017-03-24 16:21:06 +02001120/** Send wl_surface.enter/leave events
1121 *
1122 * \param surface The surface.
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03001123 * \param head A head of the entered/left output.
Pekka Paalanen01e00682017-03-24 16:21:06 +02001124 * \param enter True if entered.
Marius Vlada2dace22019-06-12 16:05:44 +03001125 * \param leave True if left.
Pekka Paalanen01e00682017-03-24 16:21:06 +02001126 *
1127 * Send the enter/leave events for all protocol objects bound to the given
1128 * output by the client owning the surface.
1129 */
1130static void
1131weston_surface_send_enter_leave(struct weston_surface *surface,
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03001132 struct weston_head *head,
Pekka Paalanen01e00682017-03-24 16:21:06 +02001133 bool enter,
1134 bool leave)
1135{
1136 struct wl_resource *wloutput;
1137 struct wl_client *client;
1138
1139 assert(enter != leave);
1140
1141 client = wl_resource_get_client(surface->resource);
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03001142 wl_resource_for_each(wloutput, &head->resource_list) {
Pekka Paalanen01e00682017-03-24 16:21:06 +02001143 if (wl_resource_get_client(wloutput) != client)
1144 continue;
1145
1146 if (enter)
1147 wl_surface_send_enter(surface->resource, wloutput);
1148 if (leave)
1149 wl_surface_send_leave(surface->resource, wloutput);
1150 }
1151}
1152
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301153static void
1154weston_surface_compute_protection(struct protected_surface *psurface)
1155{
1156 enum weston_hdcp_protection min_protection;
1157 bool min_protection_valid = false;
1158 struct weston_surface *surface = psurface->surface;
1159 struct weston_output *output;
1160
1161 wl_list_for_each(output, &surface->compositor->output_list, link)
1162 if (surface->output_mask & (1u << output->id)) {
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05301163 /*
1164 * If the content-protection is enabled with protection
1165 * mode as RELAXED for a surface, and if
1166 * content-recording features like: screen-shooter,
1167 * recorder, screen-sharing, etc are on, then notify the
1168 * client, that the protection is disabled.
1169 *
1170 * Note: If the protection mode is ENFORCED then there
1171 * is no need to bother the client as the renderer takes
1172 * care of censoring the visibility of the protected
1173 * content.
1174 */
1175
1176 if (output->disable_planes > 0 &&
1177 surface->protection_mode == WESTON_SURFACE_PROTECTION_MODE_RELAXED) {
1178 min_protection = WESTON_HDCP_DISABLE;
1179 min_protection_valid = true;
1180 break;
1181 }
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301182 if (!min_protection_valid) {
1183 min_protection = output->current_protection;
1184 min_protection_valid = true;
1185 }
1186 if (output->current_protection < min_protection)
1187 min_protection = output->current_protection;
1188 }
1189 if (!min_protection_valid)
1190 min_protection = WESTON_HDCP_DISABLE;
1191
1192 surface->current_protection = min_protection;
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05301193
1194 weston_protected_surface_send_event(psurface, surface->current_protection);
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301195}
1196
1197static void
1198notify_surface_protection_change(void *data)
1199{
1200 struct weston_compositor *compositor = data;
1201 struct content_protection *cp;
1202 struct protected_surface *psurface;
1203
1204 cp = compositor->content_protection;
1205 cp->surface_protection_update = NULL;
1206
1207 /* Notify the clients, whose surfaces are changed */
1208 wl_list_for_each(psurface, &cp->protected_list, link)
1209 if (psurface && psurface->surface)
1210 weston_surface_compute_protection(psurface);
1211}
1212
Bryce Harrington3f650b82015-12-23 11:01:58 -08001213/**
Ankit Nautiyalfaa5ab42019-07-08 13:23:24 +05301214 * \param compositor weston_compositor
1215 *
1216 * Schedule an idle task to notify surface about the update in protection,
1217 * if not already scheduled.
1218 */
1219static void
1220weston_schedule_surface_protection_update(struct weston_compositor *compositor)
1221{
1222 struct content_protection *cp = compositor->content_protection;
1223 struct wl_event_loop *loop;
1224
1225 if (!cp || cp->surface_protection_update)
1226 return;
1227 loop = wl_display_get_event_loop(compositor->wl_display);
1228 cp->surface_protection_update = wl_event_loop_add_idle(loop,
1229 notify_surface_protection_change,
1230 compositor);
1231}
1232
1233/**
Bryce Harrington3f650b82015-12-23 11:01:58 -08001234 * \param es The surface
1235 * \param mask The new set of outputs for the surface
1236 *
1237 * Sets the surface's set of outputs to the ones specified by
1238 * the new output mask provided. Identifies the outputs that
1239 * have changed, the posts enter and leave events for these
1240 * outputs as appropriate.
1241 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001242static void
1243weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
1244{
1245 uint32_t different = es->output_mask ^ mask;
1246 uint32_t entered = mask & different;
1247 uint32_t left = es->output_mask & different;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001248 uint32_t output_bit;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001249 struct weston_output *output;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001250 struct weston_head *head;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001251
1252 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001253 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001254 return;
1255 if (different == 0)
1256 return;
1257
1258 wl_list_for_each(output, &es->compositor->output_list, link) {
Pekka Paalanen01e00682017-03-24 16:21:06 +02001259 output_bit = 1u << output->id;
1260 if (!(output_bit & different))
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001261 continue;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001262
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001263 wl_list_for_each(head, &output->head_list, output_link) {
1264 weston_surface_send_enter_leave(es, head,
1265 output_bit & entered,
1266 output_bit & left);
1267 }
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001268 }
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301269 /*
1270 * Change in surfaces' output mask might trigger a change in its
1271 * protection.
1272 */
Ankit Nautiyalfaa5ab42019-07-08 13:23:24 +05301273 weston_schedule_surface_protection_update(es->compositor);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001274}
1275
Semi Malinene7a52fb2018-04-26 11:08:10 +02001276static void
1277notify_view_output_destroy(struct wl_listener *listener, void *data)
1278{
1279 struct weston_view *view =
1280 container_of(listener,
1281 struct weston_view, output_destroy_listener);
1282
1283 view->output = NULL;
1284 view->output_destroy_listener.notify = NULL;
1285}
1286
1287/** Set the primary output of the view
1288 *
1289 * \param view The view whose primary output to set
1290 * \param output The new primary output for the view
1291 *
1292 * Set \a output to be the primary output of the \a view.
1293 *
1294 * Notice that the assignment may be temporary; the primary output could be
1295 * automatically changed. Hence, one cannot rely on the value persisting.
1296 *
1297 * Passing NULL as /a output will set the primary output to NULL.
1298 */
1299WL_EXPORT void
1300weston_view_set_output(struct weston_view *view, struct weston_output *output)
1301{
1302 if (view->output_destroy_listener.notify) {
1303 wl_list_remove(&view->output_destroy_listener.link);
1304 view->output_destroy_listener.notify = NULL;
1305 }
1306 view->output = output;
1307 if (output) {
1308 view->output_destroy_listener.notify =
1309 notify_view_output_destroy;
1310 wl_signal_add(&output->destroy_signal,
1311 &view->output_destroy_listener);
1312 }
1313}
1314
Bryce Harrington3f650b82015-12-23 11:01:58 -08001315/** Recalculate which output(s) the surface has views displayed on
1316 *
1317 * \param es The surface to remap to outputs
1318 *
1319 * Finds the output that is showing the largest amount of one
1320 * of the surface's various views. This output becomes the
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001321 * surface's primary output for vsync and frame callback purposes.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001322 *
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001323 * Also notes all outputs of all of the surface's views
Bryce Harrington3f650b82015-12-23 11:01:58 -08001324 * in the output_mask for the surface.
1325 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001326static void
1327weston_surface_assign_output(struct weston_surface *es)
1328{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001329 struct weston_output *new_output;
1330 struct weston_view *view;
1331 pixman_region32_t region;
1332 uint32_t max, area, mask;
1333 pixman_box32_t *e;
1334
1335 new_output = NULL;
1336 max = 0;
1337 mask = 0;
1338 pixman_region32_init(&region);
1339 wl_list_for_each(view, &es->views, surface_link) {
1340 if (!view->output)
1341 continue;
1342
1343 pixman_region32_intersect(&region, &view->transform.boundingbox,
1344 &view->output->region);
1345
1346 e = pixman_region32_extents(&region);
1347 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1348
1349 mask |= view->output_mask;
1350
1351 if (area >= max) {
1352 new_output = view->output;
1353 max = area;
1354 }
1355 }
1356 pixman_region32_fini(&region);
1357
1358 es->output = new_output;
1359 weston_surface_update_output_mask(es, mask);
1360}
1361
Bryce Harrington3f650b82015-12-23 11:01:58 -08001362/** Recalculate which output(s) the view is displayed on
1363 *
1364 * \param ev The view to remap to outputs
1365 *
1366 * Identifies the set of outputs that the view is visible on,
1367 * noting them into the output_mask. The output that the view
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001368 * is most visible on is set as the view's primary output.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001369 *
1370 * Also does the same for the view's surface. See
1371 * weston_surface_assign_output().
1372 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001373static void
1374weston_view_assign_output(struct weston_view *ev)
1375{
1376 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001377 struct weston_output *output, *new_output;
1378 pixman_region32_t region;
1379 uint32_t max, area, mask;
1380 pixman_box32_t *e;
1381
1382 new_output = NULL;
1383 max = 0;
1384 mask = 0;
1385 pixman_region32_init(&region);
1386 wl_list_for_each(output, &ec->output_list, link) {
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03001387 if (output->destroying)
1388 continue;
1389
Jason Ekstranda7af7042013-10-12 22:38:11 -05001390 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001391 &output->region);
1392
1393 e = pixman_region32_extents(&region);
1394 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1395
1396 if (area > 0)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001397 mask |= 1u << output->id;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001398
1399 if (area >= max) {
1400 new_output = output;
1401 max = area;
1402 }
1403 }
1404 pixman_region32_fini(&region);
1405
Semi Malinene7a52fb2018-04-26 11:08:10 +02001406 weston_view_set_output(ev, new_output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001407 ev->output_mask = mask;
1408
1409 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001410}
1411
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001412static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001413weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1414 int from_x, int from_y, int *to_x, int *to_y)
1415{
1416 float x, y;
1417
1418 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1419 weston_view_from_global_float(to, x, y, &x, &y);
1420
1421 *to_x = round(x);
1422 *to_y = round(y);
1423}
1424
1425static void
1426weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1427{
1428 pixman_box32_t *a;
1429 pixman_box32_t b;
1430
1431 a = pixman_region32_extents(&from->geometry.scissor);
1432
1433 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1434 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1435
1436 pixman_region32_fini(&to->geometry.scissor);
1437 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1438}
1439
1440static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001441view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001442 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001443{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001444 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1445 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001446 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001447 { inbox->x1, inbox->y1 },
1448 { inbox->x1, inbox->y2 },
1449 { inbox->x2, inbox->y1 },
1450 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001451 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001452 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001453 int i;
1454
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001455 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001456 /* avoid rounding empty bbox to 1x1 */
1457 pixman_region32_init(bbox);
1458 return;
1459 }
1460
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001461 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001462 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001463 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001464 if (x < min_x)
1465 min_x = x;
1466 if (x > max_x)
1467 max_x = x;
1468 if (y < min_y)
1469 min_y = y;
1470 if (y > max_y)
1471 max_y = y;
1472 }
1473
Pekka Paalanen219b9822012-02-08 15:38:37 +02001474 int_x = floorf(min_x);
1475 int_y = floorf(min_y);
1476 pixman_region32_init_rect(bbox, int_x, int_y,
1477 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001478}
1479
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001480static void
Michael Olbrichcb04f982020-06-08 10:22:32 +02001481weston_view_update_transform_scissor(struct weston_view *view,
1482 pixman_region32_t *region)
1483{
1484 struct weston_view *parent = view->geometry.parent;
1485
1486 if (parent) {
1487 if (parent->geometry.scissor_enabled) {
1488 view->geometry.scissor_enabled = true;
1489 weston_view_transfer_scissor(parent, view);
1490 } else {
1491 view->geometry.scissor_enabled = false;
1492 }
1493 }
1494
1495 if (view->geometry.scissor_enabled)
1496 pixman_region32_intersect(region, region,
1497 &view->geometry.scissor);
1498}
1499static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001500weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001501{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001502 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001503
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001504 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001505 view->geometry.x = roundf(view->geometry.x);
1506 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001507
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001508 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001509 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1510 view->transform.position.matrix.d[12] = view->geometry.x;
1511 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001512
Jason Ekstranda7af7042013-10-12 22:38:11 -05001513 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001514
Jason Ekstranda7af7042013-10-12 22:38:11 -05001515 view->transform.inverse = view->transform.position.matrix;
1516 view->transform.inverse.d[12] = -view->geometry.x;
1517 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001518
Jason Ekstranda7af7042013-10-12 22:38:11 -05001519 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001520 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001521 view->surface->width,
1522 view->surface->height);
Michael Olbrichcb04f982020-06-08 10:22:32 +02001523
1524 weston_view_update_transform_scissor(view, &view->transform.boundingbox);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001525
1526 pixman_region32_translate(&view->transform.boundingbox,
1527 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001528
Jason Ekstranda7af7042013-10-12 22:38:11 -05001529 if (view->alpha == 1.0) {
1530 pixman_region32_copy(&view->transform.opaque,
1531 &view->surface->opaque);
Derek Foreman713f0232022-01-12 16:30:26 -06001532 if (view->geometry.scissor_enabled)
1533 pixman_region32_intersect(&view->transform.opaque,
1534 &view->transform.opaque,
1535 &view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001536 pixman_region32_translate(&view->transform.opaque,
1537 view->geometry.x,
1538 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001539 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001540}
1541
1542static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001543weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001544{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001545 struct weston_view *parent = view->geometry.parent;
1546 struct weston_matrix *matrix = &view->transform.matrix;
1547 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001548 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001549 pixman_region32_t surfregion;
1550 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001551
Jason Ekstranda7af7042013-10-12 22:38:11 -05001552 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001553
1554 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001555 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1556 view->transform.position.matrix.d[12] = view->geometry.x;
1557 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001558
1559 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001560 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001561 weston_matrix_multiply(matrix, &tform->matrix);
1562
Pekka Paalanen483243f2013-03-08 14:56:50 +02001563 if (parent)
1564 weston_matrix_multiply(matrix, &parent->transform.matrix);
1565
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001566 if (weston_matrix_invert(inverse, matrix) < 0) {
1567 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001568 weston_log("error: weston_view %p"
1569 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001570 return -1;
1571 }
1572
Pekka Paalanen380adf52015-02-16 14:39:11 +02001573 pixman_region32_init_rect(&surfregion, 0, 0,
1574 view->surface->width, view->surface->height);
Michael Olbrichcb04f982020-06-08 10:22:32 +02001575
1576 weston_view_update_transform_scissor(view, &surfregion);
1577
Pekka Paalanen380adf52015-02-16 14:39:11 +02001578 surfbox = pixman_region32_extents(&surfregion);
1579
1580 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1581 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001582
Michael Olbrich0e4f0972020-08-19 11:31:03 +02001583 if (view->alpha == 1.0 &&
1584 matrix->type == WESTON_MATRIX_TRANSFORM_TRANSLATE) {
1585 pixman_region32_copy(&view->transform.opaque,
1586 &view->surface->opaque);
Derek Foreman713f0232022-01-12 16:30:26 -06001587 if (view->geometry.scissor_enabled)
1588 pixman_region32_intersect(&view->transform.opaque,
1589 &view->transform.opaque,
1590 &view->geometry.scissor);
Michael Olbrich0e4f0972020-08-19 11:31:03 +02001591 pixman_region32_translate(&view->transform.opaque,
1592 matrix->d[12],
1593 matrix->d[13]);
1594 }
1595
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001596 return 0;
1597}
1598
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001599static struct weston_layer *
1600get_view_layer(struct weston_view *view)
1601{
1602 if (view->parent_view)
1603 return get_view_layer(view->parent_view);
1604 return view->layer_link.layer;
1605}
1606
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001607WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001608weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001609{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001610 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001611 struct weston_layer *layer;
1612 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001613
Jason Ekstranda7af7042013-10-12 22:38:11 -05001614 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001615 return;
1616
Pekka Paalanen483243f2013-03-08 14:56:50 +02001617 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001618 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001619
Jason Ekstranda7af7042013-10-12 22:38:11 -05001620 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001621
Jason Ekstranda7af7042013-10-12 22:38:11 -05001622 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001623
Jason Ekstranda7af7042013-10-12 22:38:11 -05001624 pixman_region32_fini(&view->transform.boundingbox);
1625 pixman_region32_fini(&view->transform.opaque);
1626 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001627
Pekka Paalanencd403622012-01-25 13:37:39 +02001628 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001629 if (view->geometry.transformation_list.next ==
1630 &view->transform.position.link &&
1631 view->geometry.transformation_list.prev ==
1632 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001633 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001634 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001635 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001636 if (weston_view_update_transform_enable(view) < 0)
1637 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001638 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001639
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001640 layer = get_view_layer(view);
1641 if (layer) {
1642 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001643 pixman_region32_intersect(&view->transform.boundingbox,
1644 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001645 pixman_region32_intersect(&view->transform.opaque,
1646 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001647 pixman_region32_fini(&mask);
1648 }
1649
Jason Ekstranda7af7042013-10-12 22:38:11 -05001650 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001651
Jason Ekstranda7af7042013-10-12 22:38:11 -05001652 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001653
Jason Ekstranda7af7042013-10-12 22:38:11 -05001654 wl_signal_emit(&view->surface->compositor->transform_signal,
1655 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001656}
1657
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001658WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001659weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001660{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001661 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001662
1663 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001664 * The invariant: if view->geometry.dirty, then all views
1665 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001666 * Corollary: if not parent->geometry.dirty, then all ancestors
1667 * are not dirty.
1668 */
1669
Jason Ekstranda7af7042013-10-12 22:38:11 -05001670 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001671 return;
1672
Jason Ekstranda7af7042013-10-12 22:38:11 -05001673 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001674
Jason Ekstranda7af7042013-10-12 22:38:11 -05001675 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001676 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001677 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001678}
1679
1680WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001681weston_view_to_global_fixed(struct weston_view *view,
1682 wl_fixed_t vx, wl_fixed_t vy,
1683 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001684{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001685 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001686
Jason Ekstranda7af7042013-10-12 22:38:11 -05001687 weston_view_to_global_float(view,
1688 wl_fixed_to_double(vx),
1689 wl_fixed_to_double(vy),
1690 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001691 *x = wl_fixed_from_double(xf);
1692 *y = wl_fixed_from_double(yf);
1693}
1694
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001695WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001696weston_view_from_global_float(struct weston_view *view,
1697 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001698{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001699 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001700 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1701
Jason Ekstranda7af7042013-10-12 22:38:11 -05001702 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001703
1704 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001705 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001706 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001707 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001708 *vx = 0;
1709 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001710 return;
1711 }
1712
Jason Ekstranda7af7042013-10-12 22:38:11 -05001713 *vx = v.f[0] / v.f[3];
1714 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001715 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001716 *vx = x - view->geometry.x;
1717 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001718 }
1719}
1720
1721WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001722weston_view_from_global_fixed(struct weston_view *view,
1723 wl_fixed_t x, wl_fixed_t y,
1724 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001725{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001726 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001727
Jason Ekstranda7af7042013-10-12 22:38:11 -05001728 weston_view_from_global_float(view,
1729 wl_fixed_to_double(x),
1730 wl_fixed_to_double(y),
1731 &vxf, &vyf);
1732 *vx = wl_fixed_from_double(vxf);
1733 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001734}
1735
1736WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001737weston_view_from_global(struct weston_view *view,
1738 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001739{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001740 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001741
Jason Ekstranda7af7042013-10-12 22:38:11 -05001742 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1743 *vx = floorf(vxf);
1744 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001745}
1746
Bryce Harrington3f650b82015-12-23 11:01:58 -08001747/**
1748 * \param surface The surface to be repainted
1749 *
1750 * Marks the output(s) that the surface is shown on as needing to be
1751 * repainted. See weston_output_schedule_repaint().
1752 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001753WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001754weston_surface_schedule_repaint(struct weston_surface *surface)
1755{
1756 struct weston_output *output;
1757
1758 wl_list_for_each(output, &surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001759 if (surface->output_mask & (1u << output->id))
Kristian Høgsberg98238702012-08-03 16:29:12 -04001760 weston_output_schedule_repaint(output);
1761}
1762
Bryce Harrington3f650b82015-12-23 11:01:58 -08001763/**
1764 * \param view The view to be repainted
1765 *
1766 * Marks the output(s) that the view is shown on as needing to be
1767 * repainted. See weston_output_schedule_repaint().
1768 */
Kristian Høgsberg98238702012-08-03 16:29:12 -04001769WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001770weston_view_schedule_repaint(struct weston_view *view)
1771{
1772 struct weston_output *output;
1773
1774 wl_list_for_each(output, &view->surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001775 if (view->output_mask & (1u << output->id))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001776 weston_output_schedule_repaint(output);
1777}
1778
Pekka Paalanene508ce62015-02-19 13:59:55 +02001779/**
1780 * XXX: This function does it the wrong way.
1781 * surface->damage is the damage from the client, and causes
1782 * surface_flush_damage() to copy pixels. No window management action can
1783 * cause damage to the client-provided content, warranting re-upload!
1784 *
1785 * Instead of surface->damage, this function should record the damage
1786 * with all the views for this surface to avoid extraneous texture
1787 * uploads.
1788 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001789WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001790weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001791{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001792 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001793 0, 0, surface->width,
1794 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001795
Kristian Høgsberg98238702012-08-03 16:29:12 -04001796 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001797}
1798
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001799WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001800weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001801{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001802 if (view->geometry.x == x && view->geometry.y == y)
1803 return;
1804
Jason Ekstranda7af7042013-10-12 22:38:11 -05001805 view->geometry.x = x;
1806 view->geometry.y = y;
1807 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001808}
1809
Pekka Paalanen483243f2013-03-08 14:56:50 +02001810static void
1811transform_parent_handle_parent_destroy(struct wl_listener *listener,
1812 void *data)
1813{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001814 struct weston_view *view =
1815 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001816 geometry.parent_destroy_listener);
1817
Jason Ekstranda7af7042013-10-12 22:38:11 -05001818 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001819}
1820
1821WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001822weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001823 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001824{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001825 if (view->geometry.parent) {
1826 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1827 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001828
1829 if (!parent)
1830 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001831 }
1832
Jason Ekstranda7af7042013-10-12 22:38:11 -05001833 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001834
Jason Ekstranda7af7042013-10-12 22:38:11 -05001835 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001836 transform_parent_handle_parent_destroy;
1837 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001838 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001839 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001840 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001841 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001842 }
1843
Jason Ekstranda7af7042013-10-12 22:38:11 -05001844 weston_view_geometry_dirty(view);
1845}
1846
Pekka Paalanen380adf52015-02-16 14:39:11 +02001847/** Set a clip mask rectangle on a view
1848 *
1849 * \param view The view to set the clip mask on.
1850 * \param x Top-left corner X coordinate of the clip rectangle.
1851 * \param y Top-left corner Y coordinate of the clip rectangle.
1852 * \param width Width of the clip rectangle, non-negative.
1853 * \param height Height of the clip rectangle, non-negative.
1854 *
1855 * A shell may set a clip mask rectangle on a view. Everything outside
1856 * the rectangle is cut away for input and output purposes: it is
1857 * not drawn and cannot be hit by hit-test based input like pointer
1858 * motion or touch-downs. Everything inside the rectangle will behave
1859 * normally. Clients are unaware of clipping.
1860 *
Yong Bakos4c72e292016-04-28 11:59:10 -05001861 * The rectangle is set in surface-local coordinates. Setting a clip
Pekka Paalanen380adf52015-02-16 14:39:11 +02001862 * mask rectangle does not affect the view position, the view is positioned
1863 * as it would be without a clip. The clip also does not change
1864 * weston_surface::width,height.
1865 *
1866 * The clip mask rectangle is part of transformation inheritance
1867 * (weston_view_set_transform_parent()). A clip set in the root of the
1868 * transformation inheritance tree will affect all views in the tree.
1869 * A clip can be set only on the root view. Attempting to set a clip
1870 * on view that has a transformation parent will fail. Assigning a parent
1871 * to a view that has a clip set will cause the clip to be forgotten.
1872 *
1873 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1874 * on the additional transformations in the child views. These transformations
1875 * may not rotate the coordinate axes, i.e., only translation and scaling
1876 * are allowed. Violating this restriction causes the clipping to malfunction.
1877 * Furthermore, using scaling may cause rounding errors in child clipping.
1878 *
1879 * The clip mask rectangle is not automatically adjusted based on
1880 * wl_surface.attach dx and dy arguments.
1881 *
1882 * A clip mask rectangle can be set only if the compositor capability
1883 * WESTON_CAP_VIEW_CLIP_MASK is present.
1884 *
1885 * This function sets the clip mask rectangle and schedules a repaint for
1886 * the view.
1887 */
1888WL_EXPORT void
1889weston_view_set_mask(struct weston_view *view,
1890 int x, int y, int width, int height)
1891{
1892 struct weston_compositor *compositor = view->surface->compositor;
1893
1894 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1895 weston_log("%s not allowed without capability!\n", __func__);
1896 return;
1897 }
1898
1899 if (view->geometry.parent) {
1900 weston_log("view %p has a parent, clip forbidden!\n", view);
1901 return;
1902 }
1903
1904 if (width < 0 || height < 0) {
1905 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1906 x, y, width, height);
1907 return;
1908 }
1909
1910 pixman_region32_fini(&view->geometry.scissor);
1911 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1912 view->geometry.scissor_enabled = true;
1913 weston_view_geometry_dirty(view);
1914 weston_view_schedule_repaint(view);
1915}
1916
1917/** Remove the clip mask from a view
1918 *
1919 * \param view The view to remove the clip mask from.
1920 *
1921 * Removed the clip mask rectangle and schedules a repaint.
1922 *
1923 * \sa weston_view_set_mask
1924 */
1925WL_EXPORT void
1926weston_view_set_mask_infinite(struct weston_view *view)
1927{
1928 view->geometry.scissor_enabled = false;
1929 weston_view_geometry_dirty(view);
1930 weston_view_schedule_repaint(view);
1931}
1932
Armin Krezović0da12b82016-06-30 06:04:33 +02001933/* Check if view should be displayed
1934 *
1935 * The indicator is set manually when assigning
1936 * a view to a surface.
1937 *
1938 * This needs reworking. See the thread starting at:
1939 *
1940 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1941 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001942WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001943weston_view_is_mapped(struct weston_view *view)
1944{
Armin Krezović0da12b82016-06-30 06:04:33 +02001945 return view->is_mapped;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001946}
1947
Philipp Zabel70decd52018-09-03 20:11:15 +02001948/* Check if view is opaque in specified region
1949 *
1950 * \param view The view to check for opacity.
1951 * \param region The region to check for opacity, in view coordinates.
1952 *
1953 * Returns true if the view is opaque in the specified region, because view
1954 * alpha is 1.0 and either the opaque region set by the client contains the
1955 * specified region, or the buffer pixel format or solid color is opaque.
1956 */
1957WL_EXPORT bool
1958weston_view_is_opaque(struct weston_view *ev, pixman_region32_t *region)
1959{
1960 pixman_region32_t r;
1961 bool ret = false;
1962
1963 if (ev->alpha < 1.0)
1964 return false;
1965
1966 if (ev->surface->is_opaque)
1967 return true;
1968
Marius Vlad67b382c2020-08-21 13:57:54 +03001969 if (ev->transform.dirty)
Philipp Zabel70decd52018-09-03 20:11:15 +02001970 return false;
Philipp Zabel70decd52018-09-03 20:11:15 +02001971
1972 pixman_region32_init(&r);
1973 pixman_region32_subtract(&r, region, &ev->transform.opaque);
1974
1975 if (!pixman_region32_not_empty(&r))
1976 ret = true;
1977
1978 pixman_region32_fini(&r);
1979
1980 return ret;
1981}
1982
Marius Vlad5f6bee42019-09-11 16:41:04 +03001983/** Check if the view has a valid buffer available
1984 *
1985 * @param ev The view to check if it has a valid buffer.
1986 *
1987 * Returns true if the view has a valid buffer or false otherwise.
1988 */
1989WL_EXPORT bool
1990weston_view_has_valid_buffer(struct weston_view *ev)
1991{
1992 return ev->surface->buffer_ref.buffer != NULL;
1993}
1994
Marius Vlad47e3d1e2019-09-11 16:53:08 +03001995/** Check if the view matches the entire output
1996 *
1997 * @param ev The view to check.
1998 * @param output The output to check against.
1999 *
2000 * Returns true if the view does indeed matches the entire output.
2001 */
2002WL_EXPORT bool
2003weston_view_matches_output_entirely(struct weston_view *ev,
2004 struct weston_output *output)
2005{
2006 pixman_box32_t *extents =
2007 pixman_region32_extents(&ev->transform.boundingbox);
2008
2009 if (extents->x1 != output->x ||
2010 extents->y1 != output->y ||
2011 extents->x2 != output->x + output->width ||
2012 extents->y2 != output->y + output->height)
2013 return false;
2014
2015 return true;
2016}
2017
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002018/** Find paint node for the given view and output
2019 */
2020WL_EXPORT struct weston_paint_node *
2021weston_view_find_paint_node(struct weston_view *view,
2022 struct weston_output *output)
2023{
2024 struct weston_paint_node *pnode;
2025
2026 wl_list_for_each(pnode, &view->paint_node_list, view_link) {
2027 assert(pnode->surface == view->surface);
2028 if (pnode->output == output)
2029 return pnode;
2030 }
2031
2032 return NULL;
2033}
2034
Armin Krezović0da12b82016-06-30 06:04:33 +02002035/* Check if a surface has a view assigned to it
2036 *
2037 * The indicator is set manually when mapping
2038 * a surface and creating a view for it.
2039 *
2040 * This needs to go. See the thread starting at:
2041 *
2042 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
2043 *
2044 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05002045WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03002046weston_surface_is_mapped(struct weston_surface *surface)
2047{
Armin Krezović0da12b82016-06-30 06:04:33 +02002048 return surface->is_mapped;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03002049}
2050
Pekka Paalanenda75ee12013-11-26 18:19:43 +01002051static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06002052surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002053{
2054 struct weston_view *view;
2055
2056 if (surface->width == width && surface->height == height)
2057 return;
2058
2059 surface->width = width;
2060 surface->height = height;
2061
2062 wl_list_for_each(view, &surface->views, surface_link)
2063 weston_view_geometry_dirty(view);
2064}
2065
Jason Ekstrand5c11a332013-12-04 20:32:03 -06002066WL_EXPORT void
2067weston_surface_set_size(struct weston_surface *surface,
2068 int32_t width, int32_t height)
2069{
2070 assert(!surface->resource);
2071 surface_set_size(surface, width, height);
2072}
2073
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02002074static int
2075fixed_round_up_to_int(wl_fixed_t f)
2076{
2077 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
2078}
2079
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002080static void
Pekka Paalanen59987fa2016-04-26 15:50:59 +03002081convert_size_by_transform_scale(int32_t *width_out, int32_t *height_out,
2082 int32_t width, int32_t height,
2083 uint32_t transform,
2084 int32_t scale)
2085{
2086 assert(scale > 0);
2087
2088 switch (transform) {
2089 case WL_OUTPUT_TRANSFORM_NORMAL:
2090 case WL_OUTPUT_TRANSFORM_180:
2091 case WL_OUTPUT_TRANSFORM_FLIPPED:
2092 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2093 *width_out = width / scale;
2094 *height_out = height / scale;
2095 break;
2096 case WL_OUTPUT_TRANSFORM_90:
2097 case WL_OUTPUT_TRANSFORM_270:
2098 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2099 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2100 *width_out = height / scale;
2101 *height_out = width / scale;
2102 break;
2103 default:
2104 assert(0 && "invalid transform");
2105 }
2106}
2107
2108static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002109weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002110{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002111 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01002112
2113 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02002114 surface->width_from_buffer = 0;
2115 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01002116 return;
2117 }
2118
Pekka Paalanen59987fa2016-04-26 15:50:59 +03002119 convert_size_by_transform_scale(&surface->width_from_buffer,
2120 &surface->height_from_buffer,
2121 surface->buffer_ref.buffer->width,
2122 surface->buffer_ref.buffer->height,
2123 vp->buffer.transform,
2124 vp->buffer.scale);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002125}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02002126
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002127static void
2128weston_surface_update_size(struct weston_surface *surface)
2129{
2130 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
2131 int32_t width, height;
2132
2133 width = surface->width_from_buffer;
2134 height = surface->height_from_buffer;
2135
2136 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02002137 surface_set_size(surface,
2138 vp->surface.width, vp->surface.height);
2139 return;
2140 }
2141
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002142 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03002143 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
2144 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
2145
2146 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02002147 return;
2148 }
2149
Jason Ekstrand5c11a332013-12-04 20:32:03 -06002150 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002151}
2152
Marius Vlad9fdda7f2019-06-11 16:08:55 +03002153/** weston_compositor_get_time
2154 * \ingroup compositor
2155 */
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02002156WL_EXPORT void
2157weston_compositor_get_time(struct timespec *time)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05002158{
limin.tianc616dad2024-07-15 11:35:38 +00002159 clock_gettime(CLOCK_MONOTONIC, time);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05002160}
2161
Marius Vlad9fdda7f2019-06-11 16:08:55 +03002162/** weston_compositor_pick_view
2163 * \ingroup compositor
2164 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002165WL_EXPORT struct weston_view *
2166weston_compositor_pick_view(struct weston_compositor *compositor,
2167 wl_fixed_t x, wl_fixed_t y,
2168 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02002169{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002170 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02002171 wl_fixed_t view_x, view_y;
2172 int view_ix, view_iy;
2173 int ix = wl_fixed_to_int(x);
2174 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02002175
Pekka Paalanen6551c092021-05-03 16:09:45 +03002176 /* Can't use paint node list: occlusion by input regions, not opaque. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002177 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02002178 if (!pixman_region32_contains_point(
2179 &view->transform.boundingbox, ix, iy, NULL))
2180 continue;
2181
2182 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
2183 view_ix = wl_fixed_to_int(view_x);
2184 view_iy = wl_fixed_to_int(view_y);
2185
2186 if (!pixman_region32_contains_point(&view->surface->input,
2187 view_ix, view_iy, NULL))
2188 continue;
2189
Pekka Paalanen380adf52015-02-16 14:39:11 +02002190 if (view->geometry.scissor_enabled &&
2191 !pixman_region32_contains_point(&view->geometry.scissor,
2192 view_ix, view_iy, NULL))
2193 continue;
2194
Pekka Paalanenfc22a522015-02-18 15:08:29 +02002195 *vx = view_x;
2196 *vy = view_y;
2197 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02002198 }
2199
Derek Foremanf9318d12015-05-11 15:40:11 -05002200 *vx = wl_fixed_from_int(-1000000);
2201 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02002202 return NULL;
2203}
2204
2205static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002206weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002207{
Daniel Stone37816df2012-05-16 18:45:18 +01002208 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002209
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07002210 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002211 return;
2212
Daniel Stone37816df2012-05-16 18:45:18 +01002213 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04002214 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002215}
2216
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002217WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002218weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05002219{
Daniel Stone4dab5db2012-05-30 16:31:53 +01002220 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002221
Jason Ekstranda7af7042013-10-12 22:38:11 -05002222 if (!weston_view_is_mapped(view))
2223 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002224
Jason Ekstranda7af7042013-10-12 22:38:11 -05002225 weston_view_damage_below(view);
Semi Malinene7a52fb2018-04-26 11:08:10 +02002226 weston_view_set_output(view, NULL);
Xiong Zhang97116532013-10-23 13:58:31 +08002227 view->plane = NULL;
Armin Krezovićf8486c32016-06-30 06:04:28 +02002228 view->is_mapped = false;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002229 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002230 wl_list_remove(&view->link);
2231 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002232 view->output_mask = 0;
2233 weston_surface_assign_output(view->surface);
2234
2235 if (weston_surface_is_mapped(view->surface))
2236 return;
2237
2238 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002239 struct weston_touch *touch = weston_seat_get_touch(seat);
2240 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2241 struct weston_keyboard *keyboard =
2242 weston_seat_get_keyboard(seat);
2243
2244 if (keyboard && keyboard->focus == view->surface)
2245 weston_keyboard_set_focus(keyboard, NULL);
2246 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05002247 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05002248 if (touch && touch->focus == view)
2249 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01002250 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002251}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002252
Jason Ekstranda7af7042013-10-12 22:38:11 -05002253WL_EXPORT void
2254weston_surface_unmap(struct weston_surface *surface)
2255{
2256 struct weston_view *view;
2257
Armin Krezovićf8486c32016-06-30 06:04:28 +02002258 surface->is_mapped = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002259 wl_list_for_each(view, &surface->views, surface_link)
2260 weston_view_unmap(view);
2261 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05002262}
2263
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002264static void
2265weston_surface_reset_pending_buffer(struct weston_surface *surface)
2266{
Jason Ekstrand7b982072014-05-20 14:33:03 -05002267 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002268 surface->pending.sx = 0;
2269 surface->pending.sy = 0;
2270 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002271 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002272}
2273
Jason Ekstranda7af7042013-10-12 22:38:11 -05002274WL_EXPORT void
2275weston_view_destroy(struct weston_view *view)
2276{
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002277 struct weston_paint_node *pnode, *pntmp;
Marius Vladf2452d62021-12-09 14:07:21 +02002278 weston_signal_emit_mutable(&view->destroy_signal, view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002279
2280 assert(wl_list_empty(&view->geometry.child_list));
2281
2282 if (weston_view_is_mapped(view)) {
2283 weston_view_unmap(view);
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002284 weston_compositor_build_view_list(view->surface->compositor,
2285 NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002286 }
2287
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002288 wl_list_for_each_safe(pnode, pntmp, &view->paint_node_list, view_link)
2289 weston_paint_node_destroy(pnode);
2290
Jason Ekstranda7af7042013-10-12 22:38:11 -05002291 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002292 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002293
2294 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02002295 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002296 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002297 pixman_region32_fini(&view->transform.opaque);
leng.fang32af9fc2024-06-13 11:22:15 +08002298#ifdef MESON_VIDEO_PLAN_SUPPORT
2299 pixman_region32_fini(&view->transform.transparent);
2300#endif
Jason Ekstranda7af7042013-10-12 22:38:11 -05002301
2302 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen944fae82018-05-22 13:15:58 +03002303 weston_view_set_output(view, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002304
Jason Ekstranda7af7042013-10-12 22:38:11 -05002305 wl_list_remove(&view->surface_link);
2306
2307 free(view);
2308}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002309
2310WL_EXPORT void
2311weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05002312{
limin.tiana0140a72024-10-12 07:28:49 +00002313 weston_log("\n weston_surface_destroy :%p output:%p name :%s \n", surface, surface->output, surface->name);
Vlad Zahorodniic2b97472021-07-29 11:16:51 +03002314 struct wl_resource *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002315 struct weston_view *ev, *nv;
Jonas Ådahld3414f22016-07-22 17:56:31 +08002316 struct weston_pointer_constraint *constraint, *next_constraint;
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002317 struct weston_paint_node *pnode, *pntmp;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04002318
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02002319 if (--surface->ref_count > 0)
2320 return;
2321
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03002322 assert(surface->resource == NULL);
2323
Alexandros Frantzisfa48c5e2020-07-09 17:31:34 +03002324 weston_signal_emit_mutable(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02002325
Pekka Paalanene67858b2013-04-25 13:57:42 +03002326 assert(wl_list_empty(&surface->subsurface_list_pending));
2327 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02002328
Leandro Ribeiro8eb84142021-01-18 19:36:48 -03002329 if (surface->dmabuf_feedback)
2330 weston_dmabuf_feedback_destroy(surface->dmabuf_feedback);
2331
Jason Ekstranda7af7042013-10-12 22:38:11 -05002332 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
2333 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002334
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002335 wl_list_for_each_safe(pnode, pntmp,
2336 &surface->paint_node_list, surface_link) {
2337 weston_paint_node_destroy(pnode);
2338 }
2339
Jason Ekstrand7b982072014-05-20 14:33:03 -05002340 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002341
Pekka Paalanende685b82012-12-04 15:58:12 +02002342 weston_buffer_reference(&surface->buffer_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002343 weston_buffer_release_reference(&surface->buffer_release_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04002344
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02002345 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002346 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002347 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02002348
Vlad Zahorodniic2b97472021-07-29 11:16:51 +03002349 wl_resource_for_each_safe(cb, next, &surface->frame_callback_list)
2350 wl_resource_destroy(cb);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002351
Pekka Paalanen133e4392014-09-23 22:08:46 -04002352 weston_presentation_feedback_discard_list(&surface->feedback_list);
2353
Jonas Ådahld3414f22016-07-22 17:56:31 +08002354 wl_list_for_each_safe(constraint, next_constraint,
2355 &surface->pointer_constraints,
2356 link)
2357 weston_pointer_constraint_destroy(constraint);
2358
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03002359 fd_clear(&surface->acquire_fence_fd);
2360
limin.tiana0140a72024-10-12 07:28:49 +00002361 weston_set_appstate_by_appname(surface->compositor, surface->name, 0);
leng.fang32af9fc2024-06-13 11:22:15 +08002362 weston_clear_video_surface_info(surface);
leng.fangdbaf6fa2024-06-20 19:31:04 +08002363 weston_surface_destroy_info_for_surface(surface);
limin.tiana0140a72024-10-12 07:28:49 +00002364 weston_log("\n weston_surface_destroy :%p output:%p name :%s \n", surface, surface->output, surface->name);
leng.fangdbaf6fa2024-06-20 19:31:04 +08002365
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04002366 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05002367}
2368
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002369static void
2370destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08002371{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002372 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08002373
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03002374 assert(surface);
2375
Giulio Camuffo0d379742013-11-15 22:06:15 +01002376 /* Set the resource to NULL, since we don't want to leave a
2377 * dangling pointer if the surface was refcounted and survives
2378 * the weston_surface_destroy() call. */
2379 surface->resource = NULL;
Pekka Paalanen4826f872016-04-22 14:14:38 +03002380
2381 if (surface->viewport_resource)
2382 wl_resource_set_user_data(surface->viewport_resource, NULL);
2383
Alexandros Frantzis27d7c392018-10-19 12:14:11 +03002384 if (surface->synchronization_resource) {
2385 wl_resource_set_user_data(surface->synchronization_resource,
2386 NULL);
2387 }
2388
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002389 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08002390}
2391
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002392static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002393weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
2394{
2395 struct weston_buffer *buffer =
2396 container_of(listener, struct weston_buffer, destroy_listener);
2397
leng.fang32af9fc2024-06-13 11:22:15 +08002398 weston_log("\n weston_buffer_destroy_handler buffer:%p\n",buffer);
Marius Vladf2452d62021-12-09 14:07:21 +02002399 weston_signal_emit_mutable(&buffer->destroy_signal, buffer);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002400 free(buffer);
2401}
2402
Giulio Camuffoe058cd12013-12-12 14:14:29 +01002403WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002404weston_buffer_from_resource(struct wl_resource *resource)
2405{
2406 struct weston_buffer *buffer;
2407 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002408
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002409 listener = wl_resource_get_destroy_listener(resource,
2410 weston_buffer_destroy_handler);
2411
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002412 if (listener)
2413 return container_of(listener, struct weston_buffer,
2414 destroy_listener);
2415
2416 buffer = zalloc(sizeof *buffer);
2417 if (buffer == NULL)
2418 return NULL;
2419
2420 buffer->resource = resource;
2421 wl_signal_init(&buffer->destroy_signal);
2422 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04002423 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002424 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002425
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002426 return buffer;
2427}
2428
2429static void
Pekka Paalanende685b82012-12-04 15:58:12 +02002430weston_buffer_reference_handle_destroy(struct wl_listener *listener,
2431 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002432{
Pekka Paalanende685b82012-12-04 15:58:12 +02002433 struct weston_buffer_reference *ref =
2434 container_of(listener, struct weston_buffer_reference,
2435 destroy_listener);
2436
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002437 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02002438 ref->buffer = NULL;
2439}
2440
2441WL_EXPORT void
2442weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002443 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002444{
2445 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05002446 ref->buffer->busy_count--;
2447 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002448 assert(wl_resource_get_client(ref->buffer->resource));
leng.fang32af9fc2024-06-13 11:22:15 +08002449 buffer_send_release(ref);
Kristian Høgsberg20347802013-03-04 12:07:46 -05002450 }
Pekka Paalanende685b82012-12-04 15:58:12 +02002451 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002452 }
2453
Pekka Paalanende685b82012-12-04 15:58:12 +02002454 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002455 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002456 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02002457 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02002458 }
2459
Pekka Paalanende685b82012-12-04 15:58:12 +02002460 ref->buffer = buffer;
2461 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
2462}
2463
2464static void
Alexandros Frantzis67629672018-10-19 12:14:11 +03002465weston_buffer_release_reference_handle_destroy(struct wl_listener *listener,
2466 void *data)
2467{
2468 struct weston_buffer_release_reference *ref =
2469 container_of(listener, struct weston_buffer_release_reference,
2470 destroy_listener);
2471
2472 assert((struct wl_resource *)data == ref->buffer_release->resource);
2473 ref->buffer_release = NULL;
2474}
2475
2476static void
2477weston_buffer_release_destroy(struct weston_buffer_release *buffer_release)
2478{
2479 struct wl_resource *resource = buffer_release->resource;
2480 int release_fence_fd = buffer_release->fence_fd;
2481
2482 if (release_fence_fd >= 0) {
2483 zwp_linux_buffer_release_v1_send_fenced_release(
2484 resource, release_fence_fd);
2485 } else {
2486 zwp_linux_buffer_release_v1_send_immediate_release(
2487 resource);
2488 }
2489
2490 wl_resource_destroy(resource);
2491}
2492
2493WL_EXPORT void
2494weston_buffer_release_reference(struct weston_buffer_release_reference *ref,
2495 struct weston_buffer_release *buffer_release)
2496{
2497 if (buffer_release == ref->buffer_release)
2498 return;
2499
2500 if (ref->buffer_release) {
2501 ref->buffer_release->ref_count--;
2502 wl_list_remove(&ref->destroy_listener.link);
2503 if (ref->buffer_release->ref_count == 0)
2504 weston_buffer_release_destroy(ref->buffer_release);
2505 }
2506
2507 if (buffer_release) {
2508 buffer_release->ref_count++;
2509 wl_resource_add_destroy_listener(buffer_release->resource,
2510 &ref->destroy_listener);
2511 }
2512
2513 ref->buffer_release = buffer_release;
2514 ref->destroy_listener.notify =
2515 weston_buffer_release_reference_handle_destroy;
2516}
2517
2518WL_EXPORT void
2519weston_buffer_release_move(struct weston_buffer_release_reference *dest,
2520 struct weston_buffer_release_reference *src)
2521{
2522 weston_buffer_release_reference(dest, src->buffer_release);
2523 weston_buffer_release_reference(src, NULL);
2524}
2525
2526static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002527weston_surface_attach(struct weston_surface *surface,
2528 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002529{
2530 weston_buffer_reference(&surface->buffer_ref, buffer);
2531
Pekka Paalanena6421c42012-12-04 15:58:10 +02002532 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002533 if (weston_surface_is_mapped(surface))
2534 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002535 }
2536
leng.fang32af9fc2024-06-13 11:22:15 +08002537 weston_video_surface_attach(surface, buffer);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002538 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02002539
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002540 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002541 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002542}
2543
Marius Vlad9fdda7f2019-06-11 16:08:55 +03002544/** weston_compositor_damage_all
2545 * \ingroup compositor
2546 */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002547WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002548weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002549{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002550 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002551
2552 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002553 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002554}
2555
Marius Vlad55d87362019-06-11 01:15:35 +03002556/**
2557 * \ingroup output
2558 */
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05002559WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002560weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002561{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002562 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002563
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002564 pixman_region32_union(&compositor->primary_plane.damage,
2565 &compositor->primary_plane.damage,
2566 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002567 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002568}
2569
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04002570static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002571surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002572{
Pekka Paalanende685b82012-12-04 15:58:12 +02002573 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002574 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002575 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002576
Marius Vlad2a1b7862019-09-05 13:12:18 +03002577 if (pixman_region32_not_empty(&surface->damage))
Marius Vlad3203ff62019-09-05 14:56:12 +03002578 TL_POINT(surface->compositor, "core_flush_damage", TLP_SURFACE(surface),
Pekka Paalanenb5026542014-11-12 15:09:24 +02002579 TLP_OUTPUT(surface->output), TLP_END);
2580
Jason Ekstrandef540082014-06-26 10:37:36 -07002581 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002582}
2583
2584static void
2585view_accumulate_damage(struct weston_view *view,
2586 pixman_region32_t *opaque)
2587{
2588 pixman_region32_t damage;
2589
2590 pixman_region32_init(&damage);
2591 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002592 pixman_box32_t *extents;
2593
Jason Ekstranda7af7042013-10-12 22:38:11 -05002594 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002595 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002596 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002597 pixman_region32_copy(&damage, &view->surface->damage);
2598 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002599 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002600 }
2601
Pekka Paalanen380adf52015-02-16 14:39:11 +02002602 pixman_region32_intersect(&damage, &damage,
2603 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002604 pixman_region32_subtract(&damage, &damage, opaque);
2605 pixman_region32_union(&view->plane->damage,
2606 &view->plane->damage, &damage);
2607 pixman_region32_fini(&damage);
leng.fang32af9fc2024-06-13 11:22:15 +08002608#if MESON_VIDEO_PLAN_SUPPORT
2609 pixman_region32_union(&view->clip, &view->clip, opaque);
2610#else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002611 pixman_region32_copy(&view->clip, opaque);
leng.fang32af9fc2024-06-13 11:22:15 +08002612#endif
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002613 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002614}
2615
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002616static void
Michael Olbrichc5ea4952020-08-11 12:42:35 +02002617output_accumulate_damage(struct weston_output *output)
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002618{
Michael Olbrichc5ea4952020-08-11 12:42:35 +02002619 struct weston_compositor *ec = output->compositor;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002620 struct weston_plane *plane;
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002621 struct weston_paint_node *pnode;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002622 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002623
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002624 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002625
2626 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002627 pixman_region32_copy(&plane->clip, &clip);
2628
2629 pixman_region32_init(&opaque);
2630
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002631 wl_list_for_each(pnode, &output->paint_node_z_order_list,
2632 z_order_link) {
2633 if (pnode->view->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002634 continue;
leng.fang32af9fc2024-06-13 11:22:15 +08002635#if MESON_VIDEO_PLAN_SUPPORT
2636 // meson platform video plane is below primary plane.(but the zpos may hight then primary plane.
2637 // Avoid video plane region make the primary plane be occlude.
2638
2639 // We assume the overlay plane only have one surface, on weston just primary plane support glrender compositor.
2640 if (!pnode->view->surface->is_video_surface)
2641#endif
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002642
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002643 view_accumulate_damage(pnode->view, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002644 }
2645
2646 pixman_region32_union(&clip, &clip, &opaque);
2647 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002648 }
2649
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002650 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002651
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002652 wl_list_for_each(pnode, &output->paint_node_z_order_list,
2653 z_order_link) {
2654 pnode->surface->touched = false;
2655 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002656
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002657 wl_list_for_each(pnode, &output->paint_node_z_order_list,
2658 z_order_link) {
Michael Olbrichc5ea4952020-08-11 12:42:35 +02002659 /* Ignore views not visible on the current output */
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002660 /* TODO: turn this into assert once z_order_list is pruned. */
2661 if (!(pnode->view->output_mask & (1u << output->id)))
Michael Olbrichc5ea4952020-08-11 12:42:35 +02002662 continue;
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002663 if (pnode->surface->touched)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002664 continue;
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002665 pnode->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002666
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002667 surface_flush_damage(pnode->surface);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002668
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002669 /* Both the renderer and the backend have seen the buffer
2670 * by now. If renderer needs the buffer, it has its own
2671 * reference set. If the backend wants to keep the buffer
2672 * around for migrating the surface into a non-primary plane
2673 * later, keep_buffer is true. Otherwise, drop the core
2674 * reference now, and allow early buffer release. This enables
2675 * clients to use single-buffering.
2676 */
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002677 if (!pnode->surface->keep_buffer) {
2678 weston_buffer_reference(&pnode->surface->buffer_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002679 weston_buffer_release_reference(
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002680 &pnode->surface->buffer_release_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002681 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002682 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002683}
2684
2685static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002686surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002687{
2688 struct weston_subsurface *sub;
2689
Pekka Paalanene67858b2013-04-25 13:57:42 +03002690 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002691 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002692 continue;
2693
Jason Ekstranda7af7042013-10-12 22:38:11 -05002694 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2695 wl_list_init(&sub->surface->views);
2696
2697 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002698 }
2699}
2700
2701static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002702surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002703{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002704 struct weston_subsurface *sub;
2705 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002706
Jason Ekstranda7af7042013-10-12 22:38:11 -05002707 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2708 if (sub->surface == surface)
2709 continue;
2710
George Kiagiadakised04d382014-06-13 18:10:26 +02002711 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2712 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002713 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002714 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002715
2716 surface_free_unused_subsurface_views(sub->surface);
2717 }
2718}
2719
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002720static struct weston_paint_node *
2721view_ensure_paint_node(struct weston_view *view, struct weston_output *output)
2722{
2723 struct weston_paint_node *pnode;
2724
2725 if (!output)
2726 return NULL;
2727
2728 pnode = weston_view_find_paint_node(view, output);
2729 if (pnode)
2730 return pnode;
2731
2732 return weston_paint_node_create(view->surface, view, output);
2733}
2734
Jason Ekstranda7af7042013-10-12 22:38:11 -05002735static void
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002736add_to_z_order_list(struct weston_output *output,
2737 struct weston_paint_node *pnode)
2738{
2739 if (!pnode)
2740 return;
limin.tian230ec582024-07-30 09:35:17 +00002741 if (pnode->view && pnode->view->surface && pnode->view->surface->invisible)
2742 return;
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002743 wl_list_remove(&pnode->z_order_link);
2744 wl_list_insert(output->paint_node_z_order_list.prev,
2745 &pnode->z_order_link);
Pekka Paalanen90a5ffa2021-02-25 12:03:28 +02002746
2747 /*
2748 * Building weston_output::paint_node_z_order_list ensures all
2749 * necessary color transform objects are installed.
2750 */
2751 weston_paint_node_ensure_color_transform(pnode);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002752}
2753
2754static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002755view_list_add_subsurface_view(struct weston_compositor *compositor,
2756 struct weston_subsurface *sub,
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002757 struct weston_view *parent,
2758 struct weston_output *output)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002759{
2760 struct weston_subsurface *child;
2761 struct weston_view *view = NULL, *iv;
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002762 struct weston_paint_node *pnode;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002763
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002764 if (!weston_surface_is_mapped(sub->surface))
2765 return;
2766
Jason Ekstranda7af7042013-10-12 22:38:11 -05002767 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2768 if (iv->geometry.parent == parent) {
2769 view = iv;
2770 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002771 }
2772 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002773
2774 if (view) {
2775 /* Put it back in the surface's list of views */
2776 wl_list_remove(&view->surface_link);
2777 wl_list_insert(&sub->surface->views, &view->surface_link);
2778 } else {
2779 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002780 weston_view_set_position(view,
2781 sub->position.x,
2782 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002783 weston_view_set_transform_parent(view, parent);
2784 }
2785
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002786 view->parent_view = parent;
limin.tiance0f9d72024-08-06 06:30:57 +00002787 weston_update_subsurface_position(sub, view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002788 weston_view_update_transform(view);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002789 view->is_mapped = true;
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002790 pnode = view_ensure_paint_node(view, output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002791
Pekka Paalanenb188e912013-11-19 14:03:35 +02002792 if (wl_list_empty(&sub->surface->subsurface_list)) {
2793 wl_list_insert(compositor->view_list.prev, &view->link);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002794 add_to_z_order_list(output, pnode);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002795 return;
2796 }
2797
2798 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002799 if (child->surface == sub->surface) {
Pekka Paalanenb188e912013-11-19 14:03:35 +02002800 wl_list_insert(compositor->view_list.prev, &view->link);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002801 add_to_z_order_list(output, pnode);
2802 } else {
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002803 view_list_add_subsurface_view(compositor, child, view, output);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002804 }
Pekka Paalanenb188e912013-11-19 14:03:35 +02002805 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002806}
2807
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002808/* This recursively adds the sub-surfaces for a view, relying on the
2809 * sub-surface order. Thus, if a client restacks the sub-surfaces, that
2810 * change first happens to the sub-surface list, and then automatically
2811 * propagates here. See weston_surface_damage_subsurfaces() for how the
2812 * sub-surfaces receive damage when the client changes the state.
2813 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002814static void
2815view_list_add(struct weston_compositor *compositor,
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002816 struct weston_view *view,
2817 struct weston_output *output)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002818{
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002819 struct weston_paint_node *pnode;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002820 struct weston_subsurface *sub;
2821
leng.fanga0f20922024-10-11 17:23:59 +08002822 if (!weston_compositor_get_view_visible(view))
leng.fangdbaf6fa2024-06-20 19:31:04 +08002823 return;
2824
Jason Ekstranda7af7042013-10-12 22:38:11 -05002825 weston_view_update_transform(view);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002826 pnode = view_ensure_paint_node(view, output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002827
Pekka Paalanenb188e912013-11-19 14:03:35 +02002828 if (wl_list_empty(&view->surface->subsurface_list)) {
2829 wl_list_insert(compositor->view_list.prev, &view->link);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002830 add_to_z_order_list(output, pnode);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002831 return;
2832 }
2833
2834 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002835 if (sub->surface == view->surface) {
Pekka Paalanenb188e912013-11-19 14:03:35 +02002836 wl_list_insert(compositor->view_list.prev, &view->link);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002837 add_to_z_order_list(output, pnode);
2838 } else {
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002839 view_list_add_subsurface_view(compositor, sub, view, output);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002840 }
Pekka Paalanenb188e912013-11-19 14:03:35 +02002841 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002842}
2843
2844static void
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002845weston_compositor_build_view_list(struct weston_compositor *compositor,
2846 struct weston_output *output)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002847{
Loïc Yhuel267b16e2019-09-17 20:14:56 +02002848 struct weston_view *view, *tmp;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002849 struct weston_layer *layer;
2850
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002851 if (output) {
2852 wl_list_remove(&output->paint_node_z_order_list);
2853 wl_list_init(&output->paint_node_z_order_list);
2854 }
2855
Jason Ekstranda7af7042013-10-12 22:38:11 -05002856 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002857 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002858 surface_stash_subsurface_views(view->surface);
2859
leng.fangdbaf6fa2024-06-20 19:31:04 +08002860 weston_compositor_build_view_list_by_zorder(compositor);
Loïc Yhuel267b16e2019-09-17 20:14:56 +02002861 wl_list_for_each_safe(view, tmp, &compositor->view_list, link)
2862 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002863 wl_list_init(&compositor->view_list);
Loïc Yhuel267b16e2019-09-17 20:14:56 +02002864
Jason Ekstranda7af7042013-10-12 22:38:11 -05002865 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002866 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002867 view_list_add(compositor, view, output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002868 }
2869 }
2870
2871 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002872 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002873 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002874}
2875
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002876static void
2877weston_output_take_feedback_list(struct weston_output *output,
2878 struct weston_surface *surface)
2879{
2880 struct weston_view *view;
2881 struct weston_presentation_feedback *feedback;
2882 uint32_t flags = 0xffffffff;
2883
2884 if (wl_list_empty(&surface->feedback_list))
2885 return;
2886
2887 /* All views must have the flag for the flag to survive. */
2888 wl_list_for_each(view, &surface->views, surface_link) {
2889 /* ignore views that are not on this output at all */
2890 if (view->output_mask & (1u << output->id))
2891 flags &= view->psf_flags;
2892 }
2893
2894 wl_list_for_each(feedback, &surface->feedback_list, link)
2895 feedback->psf_flags = flags;
2896
2897 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2898 wl_list_init(&surface->feedback_list);
2899}
2900
David Herrmann1edf44c2013-10-22 17:11:26 +02002901static int
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002902weston_output_repaint(struct weston_output *output, void *repaint_data)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002903{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002904 struct weston_compositor *ec = output->compositor;
Pekka Paalanen6d6b3672021-05-03 14:06:55 +03002905 struct weston_paint_node *pnode;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002906 struct weston_animation *animation, *next;
Vlad Zahorodniic2b97472021-07-29 11:16:51 +03002907 struct wl_resource *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002908 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002909 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002910 int r;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002911 uint32_t frame_time_msec;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05302912 enum weston_hdcp_protection highest_requested = WESTON_HDCP_DISABLE;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002913
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002914 if (output->destroying)
2915 return 0;
2916
Marius Vlad3203ff62019-09-05 14:56:12 +03002917 TL_POINT(ec, "core_repaint_begin", TLP_OUTPUT(output), TLP_END);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002918
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002919 /* Rebuild the surface list and update surface transforms up front. */
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002920 weston_compositor_build_view_list(ec, output);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002921
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05302922 /* Find the highest protection desired for an output */
Pekka Paalanen6d6b3672021-05-03 14:06:55 +03002923 wl_list_for_each(pnode, &output->paint_node_z_order_list,
2924 z_order_link) {
2925 /* TODO: turn this into assert once z_order_list is pruned. */
2926 if ((pnode->surface->output_mask & (1u << output->id)) == 0)
2927 continue;
2928
2929 /*
2930 * The desired_protection of the output should be the
2931 * maximum of the desired_protection of the surfaces,
2932 * that are displayed on that output, to avoid
2933 * reducing the protection for existing surfaces.
2934 */
2935 if (pnode->surface->desired_protection > highest_requested)
2936 highest_requested = pnode->surface->desired_protection;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05302937 }
2938
2939 output->desired_protection = highest_requested;
2940
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002941 if (output->assign_planes && !output->disable_planes) {
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002942 output->assign_planes(output, repaint_data);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002943 } else {
Pekka Paalanen6d6b3672021-05-03 14:06:55 +03002944 wl_list_for_each(pnode, &output->paint_node_z_order_list,
2945 z_order_link) {
2946 weston_view_move_to_plane(pnode->view, &ec->primary_plane);
2947 pnode->view->psf_flags = 0;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002948 }
2949 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002950
Pekka Paalanene67858b2013-04-25 13:57:42 +03002951 wl_list_init(&frame_callback_list);
Pekka Paalanen6d6b3672021-05-03 14:06:55 +03002952 wl_list_for_each(pnode, &output->paint_node_z_order_list,
2953 z_order_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002954 /* Note: This operation is safe to do multiple times on the
2955 * same surface.
2956 */
Pekka Paalanen6d6b3672021-05-03 14:06:55 +03002957 if (pnode->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002958 wl_list_insert_list(&frame_callback_list,
Pekka Paalanen6d6b3672021-05-03 14:06:55 +03002959 &pnode->surface->frame_callback_list);
2960 wl_list_init(&pnode->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002961
Pekka Paalanen6d6b3672021-05-03 14:06:55 +03002962 weston_output_take_feedback_list(output, pnode->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002963 }
2964 }
2965
Michael Olbrichc5ea4952020-08-11 12:42:35 +02002966 output_accumulate_damage(output);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002967
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002968 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002969 pixman_region32_intersect(&output_damage,
2970 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002971 pixman_region32_subtract(&output_damage,
2972 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002973
Scott Moreauccbf29d2012-02-22 14:21:41 -07002974 if (output->dirty)
2975 weston_output_update_matrix(output);
2976
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002977 r = output->repaint(output, &output_damage, repaint_data);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002978
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002979 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002980
Daniel Stone09a97e22017-03-01 11:34:06 +00002981 output->repaint_needed = false;
Daniel Stone05df8c12017-03-03 16:59:42 +00002982 if (r == 0)
2983 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002984
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002985 weston_compositor_repick(ec);
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002986
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002987 frame_time_msec = timespec_to_msec(&output->frame_time);
leng.fang32af9fc2024-06-13 11:22:15 +08002988 if (ec->enable_video_debug) {
2989 struct timespec time;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002990
leng.fang32af9fc2024-06-13 11:22:15 +08002991 weston_compositor_get_time(&time);
2992 weston_log("%llu repaint send done\n", timespec_to_usec(&time));
2993 }
Vlad Zahorodniic2b97472021-07-29 11:16:51 +03002994 wl_resource_for_each_safe(cb, cnext, &frame_callback_list) {
2995 wl_callback_send_done(cb, frame_time_msec);
2996 wl_resource_destroy(cb);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002997 }
2998
Scott Moreaud64cf212012-06-08 19:40:54 -06002999 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06003000 animation->frame_counter++;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02003001 animation->frame(animation, output, &output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06003002 }
David Herrmann1edf44c2013-10-22 17:11:26 +02003003
Marius Vlad3203ff62019-09-05 14:56:12 +03003004 TL_POINT(ec, "core_repaint_posted", TLP_OUTPUT(output), TLP_END);
Pekka Paalanenb5026542014-11-12 15:09:24 +02003005
David Herrmann1edf44c2013-10-22 17:11:26 +02003006 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04003007}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003008
Pekka Paalanen82919792014-05-21 13:51:49 +03003009static void
3010weston_output_schedule_repaint_reset(struct weston_output *output)
3011{
Daniel Stone05df8c12017-03-03 16:59:42 +00003012 output->repaint_status = REPAINT_NOT_SCHEDULED;
Marius Vlad3203ff62019-09-05 14:56:12 +03003013 TL_POINT(output->compositor, "core_repaint_exit_loop",
3014 TLP_OUTPUT(output), TLP_END);
Pekka Paalanen82919792014-05-21 13:51:49 +03003015}
3016
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003017static int
3018weston_output_maybe_repaint(struct weston_output *output, struct timespec *now,
3019 void *repaint_data)
Pekka Paalanen0513a952014-05-21 16:17:27 +03003020{
Pekka Paalanen0513a952014-05-21 16:17:27 +03003021 struct weston_compositor *compositor = output->compositor;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003022 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00003023 int64_t msec_to_repaint;
Pekka Paalanen0513a952014-05-21 16:17:27 +03003024
Daniel Stone6847b852017-03-01 11:34:08 +00003025 /* We're not ready yet; come back to make a decision later. */
3026 if (output->repaint_status != REPAINT_SCHEDULED)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003027 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00003028
3029 msec_to_repaint = timespec_sub_to_msec(&output->next_repaint, now);
leng.fang32af9fc2024-06-13 11:22:15 +08003030 if (msec_to_repaint > 1) {
limin.tianc616dad2024-07-15 11:35:38 +00003031 if (compositor->enable_video_debug)
3032 weston_log("\n %s %d return ret:%d\n", __FUNCTION__,__LINE__,ret);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003033 return ret;
leng.fang32af9fc2024-06-13 11:22:15 +08003034 }
Daniel Stone05df8c12017-03-03 16:59:42 +00003035
Daniel Stonecd1a1c32017-01-16 15:38:54 +00003036 /* If we're sleeping, drop the repaint machinery entirely; we will
3037 * explicitly repaint all outputs when we come back. */
3038 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
leng.fang32af9fc2024-06-13 11:22:15 +08003039 compositor->state == WESTON_COMPOSITOR_OFFSCREEN) {
limin.tianc616dad2024-07-15 11:35:38 +00003040 if (compositor->enable_video_debug)
3041 weston_log("\n %s %d return ret:%d\n", __FUNCTION__,__LINE__,ret);
Daniel Stonecd1a1c32017-01-16 15:38:54 +00003042 goto err;
leng.fang32af9fc2024-06-13 11:22:15 +08003043 }
Daniel Stonecd1a1c32017-01-16 15:38:54 +00003044 /* We don't actually need to repaint this output; drop it from
3045 * repaint until something causes damage. */
leng.fang32af9fc2024-06-13 11:22:15 +08003046 if (!output->repaint_needed) {
limin.tianc616dad2024-07-15 11:35:38 +00003047 if (compositor->enable_video_debug)
3048 weston_log("\n %s %d return ret:%d\n", __FUNCTION__,__LINE__,ret);
Daniel Stonecd1a1c32017-01-16 15:38:54 +00003049 goto err;
leng.fang32af9fc2024-06-13 11:22:15 +08003050 }
Daniel Stonecd1a1c32017-01-16 15:38:54 +00003051
3052 /* If repaint fails, we aren't going to get weston_output_finish_frame
3053 * to trigger a new repaint, so drop it from repaint and hope
Daniel Stone6847b852017-03-01 11:34:08 +00003054 * something schedules a successful repaint later. As repainting may
3055 * take some time, re-read our clock as a courtesy to the next
3056 * output. */
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003057 ret = weston_output_repaint(output, repaint_data);
Daniel Stone6847b852017-03-01 11:34:08 +00003058 weston_compositor_read_presentation_clock(compositor, now);
leng.fang32af9fc2024-06-13 11:22:15 +08003059 if (ret != 0) {
limin.tianc616dad2024-07-15 11:35:38 +00003060 if (compositor->enable_video_debug)
3061 weston_log("\n %s %d return ret:%d\n", __FUNCTION__,__LINE__,ret);
Daniel Stonecd1a1c32017-01-16 15:38:54 +00003062 goto err;
leng.fang32af9fc2024-06-13 11:22:15 +08003063 }
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09003064 output->repainted = true;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003065 return ret;
Daniel Stonecd1a1c32017-01-16 15:38:54 +00003066
3067err:
Pekka Paalanen0513a952014-05-21 16:17:27 +03003068 weston_output_schedule_repaint_reset(output);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003069 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00003070}
3071
3072static void
3073output_repaint_timer_arm(struct weston_compositor *compositor)
3074{
3075 struct weston_output *output;
3076 bool any_should_repaint = false;
3077 struct timespec now;
Sergi Granellb4c08862017-03-18 13:01:15 +01003078 int64_t msec_to_next = INT64_MAX;
Daniel Stone6847b852017-03-01 11:34:08 +00003079
3080 weston_compositor_read_presentation_clock(compositor, &now);
3081
3082 wl_list_for_each(output, &compositor->output_list, link) {
3083 int64_t msec_to_this;
3084
3085 if (output->repaint_status != REPAINT_SCHEDULED)
3086 continue;
3087
3088 msec_to_this = timespec_sub_to_msec(&output->next_repaint,
3089 &now);
3090 if (!any_should_repaint || msec_to_this < msec_to_next)
3091 msec_to_next = msec_to_this;
3092
3093 any_should_repaint = true;
3094 }
3095
3096 if (!any_should_repaint)
3097 return;
3098
3099 /* Even if we should repaint immediately, add the minimum 1 ms delay.
3100 * This is a workaround to allow coalescing multiple output repaints
3101 * particularly from weston_output_finish_frame()
3102 * into the same call, which would not happen if we called
3103 * output_repaint_timer_handler() directly.
3104 */
3105 if (msec_to_next < 1)
3106 msec_to_next = 1;
3107
3108 wl_event_source_timer_update(compositor->repaint_timer, msec_to_next);
3109}
3110
3111static int
3112output_repaint_timer_handler(void *data)
3113{
3114 struct weston_compositor *compositor = data;
3115 struct weston_output *output;
3116 struct timespec now;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003117 void *repaint_data = NULL;
Emre Ucane479ed82018-03-20 15:29:40 +01003118 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00003119
3120 weston_compositor_read_presentation_clock(compositor, &now);
Pekka Paalanen1ed2cad2021-02-10 12:33:03 +02003121 compositor->last_repaint_start = now;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003122
3123 if (compositor->backend->repaint_begin)
3124 repaint_data = compositor->backend->repaint_begin(compositor);
3125
3126 wl_list_for_each(output, &compositor->output_list, link) {
3127 ret = weston_output_maybe_repaint(output, &now, repaint_data);
3128 if (ret)
3129 break;
3130 }
3131
3132 if (ret == 0) {
Tomohito Esaki09bfcd62018-06-05 10:37:05 +09003133 if (compositor->backend->repaint_flush)
Antonio Borneoc90fccc2019-06-30 15:51:10 +02003134 ret = compositor->backend->repaint_flush(compositor,
3135 repaint_data);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003136 } else {
Antonio Borneoc90fccc2019-06-30 15:51:10 +02003137 if (compositor->backend->repaint_cancel)
3138 compositor->backend->repaint_cancel(compositor,
3139 repaint_data);
3140 }
3141
3142 if (ret != 0) {
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09003143 wl_list_for_each(output, &compositor->output_list, link) {
3144 if (output->repainted)
3145 weston_output_schedule_repaint_reset(output);
3146 }
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003147 }
Daniel Stone6847b852017-03-01 11:34:08 +00003148
Tomohito Esakiddaf95c2018-07-10 11:47:15 +09003149 wl_list_for_each(output, &compositor->output_list, link)
3150 output->repainted = false;
3151
Daniel Stone6847b852017-03-01 11:34:08 +00003152 output_repaint_timer_arm(compositor);
3153
Pekka Paalanen0513a952014-05-21 16:17:27 +03003154 return 0;
3155}
3156
Pekka Paalanen50aa3a72020-05-28 11:34:04 +03003157/** Convert a presentation timestamp to another clock domain
3158 *
3159 * \param compositor The compositor defines the presentation clock domain.
3160 * \param presentation_stamp The timestamp in presentation clock domain.
3161 * \param presentation_now Current time in presentation clock domain.
3162 * \param target_clock Defines the target clock domain.
3163 *
3164 * This approximation relies on presentation_stamp to be close to current time.
3165 * The further it is from current time and the bigger the speed difference
3166 * between the two clock domains, the bigger the conversion error.
3167 *
3168 * Conversion error due to system load is biased and unbounded.
3169 */
3170static struct timespec
3171convert_presentation_time_now(struct weston_compositor *compositor,
3172 const struct timespec *presentation_stamp,
3173 const struct timespec *presentation_now,
3174 clockid_t target_clock)
3175{
3176 struct timespec target_now = {};
3177 struct timespec target_stamp;
3178 int64_t delta_ns;
3179
3180 if (compositor->presentation_clock == target_clock)
3181 return *presentation_stamp;
3182
3183 clock_gettime(target_clock, &target_now);
3184 delta_ns = timespec_sub_to_nsec(presentation_stamp, presentation_now);
3185 timespec_add_nsec(&target_stamp, &target_now, delta_ns);
3186
3187 return target_stamp;
3188}
3189
Marius Vlad55d87362019-06-11 01:15:35 +03003190/**
3191 * \ingroup output
3192 */
Kristian Høgsbergef044142011-06-21 15:02:12 -04003193WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04003194weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02003195 const struct timespec *stamp,
3196 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04003197{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003198 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03003199 int32_t refresh_nsec;
3200 struct timespec now;
Pekka Paalanen50aa3a72020-05-28 11:34:04 +03003201 struct timespec vblank_monotonic;
Daniel Stone6847b852017-03-01 11:34:08 +00003202 int64_t msec_rel;
Pekka Paalanen133e4392014-09-23 22:08:46 -04003203
Daniel Stone05df8c12017-03-03 16:59:42 +00003204 assert(output->repaint_status == REPAINT_AWAITING_COMPLETION);
Pekka Paalanen98b4e202021-05-10 11:33:23 +03003205
3206 /*
3207 * If timestamp of latest vblank is given, it must always go forwards.
3208 * If not given, INVALID flag must be set.
3209 */
3210 if (stamp)
3211 assert(timespec_sub_to_nsec(stamp, &output->frame_time) >= 0);
3212 else
3213 assert(presented_flags & WP_PRESENTATION_FEEDBACK_INVALID);
Daniel Stone3615ce12017-03-01 11:34:05 +00003214
Daniel Stone6847b852017-03-01 11:34:08 +00003215 weston_compositor_read_presentation_clock(compositor, &now);
3216
Daniel Stone3615ce12017-03-01 11:34:05 +00003217 /* If we haven't been supplied any timestamp at all, we don't have a
3218 * timebase to work against, so any delay just wastes time. Push a
3219 * repaint as soon as possible so we can get on with it. */
Daniel Stone6847b852017-03-01 11:34:08 +00003220 if (!stamp) {
3221 output->next_repaint = now;
Daniel Stone3615ce12017-03-01 11:34:05 +00003222 goto out;
Daniel Stone6847b852017-03-01 11:34:08 +00003223 }
Daniel Stone3615ce12017-03-01 11:34:05 +00003224
Pekka Paalanen50aa3a72020-05-28 11:34:04 +03003225 vblank_monotonic = convert_presentation_time_now(compositor,
3226 stamp, &now,
3227 CLOCK_MONOTONIC);
Marius Vlad3203ff62019-09-05 14:56:12 +03003228 TL_POINT(compositor, "core_repaint_finished", TLP_OUTPUT(output),
Pekka Paalanen50aa3a72020-05-28 11:34:04 +03003229 TLP_VBLANK(&vblank_monotonic), TLP_END);
Marius Vladdf9278a2018-03-06 18:56:23 +02003230
Pekka Paalanend7894d02015-07-03 15:08:53 +03003231 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003232 weston_presentation_feedback_present_list(&output->feedback_list,
3233 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02003234 output->msc,
3235 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003236
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02003237 output->frame_time = *stamp;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003238
Daniel Stone6847b852017-03-01 11:34:08 +00003239 timespec_add_nsec(&output->next_repaint, stamp, refresh_nsec);
3240 timespec_add_msec(&output->next_repaint, &output->next_repaint,
3241 -compositor->repaint_msec);
3242 msec_rel = timespec_sub_to_msec(&output->next_repaint, &now);
Daniel Stone84aff5c2017-03-01 11:34:04 +00003243
3244 if (msec_rel < -1000 || msec_rel > 1000) {
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02003245 static bool warned;
3246
3247 if (!warned)
3248 weston_log("Warning: computed repaint delay is "
Daniel Stone6847b852017-03-01 11:34:08 +00003249 "insane: %lld msec\n", (long long) msec_rel);
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02003250 warned = true;
3251
Daniel Stone6847b852017-03-01 11:34:08 +00003252 output->next_repaint = now;
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02003253 }
3254
Mario Kleinerb7df04e2015-06-21 21:25:15 +02003255 /* Called from restart_repaint_loop and restart happens already after
3256 * the deadline given by repaint_msec? In that case we delay until
3257 * the deadline of the next frame, to give clients a more predictable
3258 * timing of the repaint cycle to lock on. */
Daniel Stoneeca5cca2017-02-28 21:53:51 +00003259 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID &&
3260 msec_rel < 0) {
3261 while (timespec_sub_to_nsec(&output->next_repaint, &now) < 0) {
3262 timespec_add_nsec(&output->next_repaint,
3263 &output->next_repaint,
3264 refresh_nsec);
3265 }
3266 }
Mario Kleinerb7df04e2015-06-21 21:25:15 +02003267
Daniel Stone3615ce12017-03-01 11:34:05 +00003268out:
Daniel Stone05df8c12017-03-03 16:59:42 +00003269 output->repaint_status = REPAINT_SCHEDULED;
Daniel Stone6847b852017-03-01 11:34:08 +00003270 output_repaint_timer_arm(compositor);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003271}
3272
nerdopolisd2a320d2021-08-23 21:29:42 -04003273
3274WL_EXPORT void
3275weston_output_repaint_failed(struct weston_output *output)
3276{
3277 weston_log("Clearing repaint status.\n");
3278 assert(output->repaint_status == REPAINT_AWAITING_COMPLETION);
3279 output->repaint_status = REPAINT_NOT_SCHEDULED;
3280}
3281
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003282static void
3283idle_repaint(void *data)
3284{
3285 struct weston_output *output = data;
Antonio Borneoc90fccc2019-06-30 15:51:10 +02003286 int ret;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003287
Daniel Stone05df8c12017-03-03 16:59:42 +00003288 assert(output->repaint_status == REPAINT_BEGIN_FROM_IDLE);
3289 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03003290 output->idle_repaint_source = NULL;
Antonio Borneoc90fccc2019-06-30 15:51:10 +02003291 ret = output->start_repaint_loop(output);
3292 if (ret != 0)
3293 weston_output_schedule_repaint_reset(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003294}
3295
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003296WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003297weston_layer_entry_insert(struct weston_layer_entry *list,
3298 struct weston_layer_entry *entry)
3299{
3300 wl_list_insert(&list->link, &entry->link);
3301 entry->layer = list->layer;
3302}
3303
3304WL_EXPORT void
3305weston_layer_entry_remove(struct weston_layer_entry *entry)
3306{
3307 wl_list_remove(&entry->link);
3308 wl_list_init(&entry->link);
3309 entry->layer = NULL;
3310}
3311
Quentin Glidic82681572016-12-17 13:40:51 +01003312
3313/** Initialize the weston_layer struct.
3314 *
3315 * \param compositor The compositor instance
3316 * \param layer The layer to initialize
3317 */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003318WL_EXPORT void
Quentin Glidic82681572016-12-17 13:40:51 +01003319weston_layer_init(struct weston_layer *layer,
3320 struct weston_compositor *compositor)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003321{
Quentin Glidic82681572016-12-17 13:40:51 +01003322 layer->compositor = compositor;
3323 wl_list_init(&layer->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003324 wl_list_init(&layer->view_list.link);
3325 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003326 weston_layer_set_mask_infinite(layer);
Quentin Glidic82681572016-12-17 13:40:51 +01003327}
3328
Pekka Paalanen87400372021-05-14 14:29:40 +03003329/** Finalize the weston_layer struct.
3330 *
3331 * \param layer The layer to finalize.
3332 */
3333WL_EXPORT void
3334weston_layer_fini(struct weston_layer *layer)
3335{
3336 wl_list_remove(&layer->link);
3337
3338 if (!wl_list_empty(&layer->view_list.link))
3339 weston_log("BUG: finalizing a layer with views still on it.\n");
3340
3341 wl_list_remove(&layer->view_list.link);
3342}
3343
Quentin Glidic82681572016-12-17 13:40:51 +01003344/** Sets the position of the layer in the layer list. The layer will be placed
3345 * below any layer with the same position value, if any.
3346 * This function is safe to call if the layer is already on the list, but the
3347 * layer may be moved below other layers at the same position, if any.
3348 *
3349 * \param layer The layer to modify
3350 * \param position The position the layer will be placed at
3351 */
3352WL_EXPORT void
3353weston_layer_set_position(struct weston_layer *layer,
3354 enum weston_layer_position position)
3355{
3356 struct weston_layer *below;
3357
3358 wl_list_remove(&layer->link);
3359
3360 /* layer_list is ordered from top to bottom, the last layer being the
3361 * background with the smallest position value */
3362
3363 layer->position = position;
3364 wl_list_for_each_reverse(below, &layer->compositor->layer_list, link) {
3365 if (below->position >= layer->position) {
3366 wl_list_insert(&below->link, &layer->link);
3367 return;
3368 }
3369 }
3370 wl_list_insert(&layer->compositor->layer_list, &layer->link);
3371}
3372
3373/** Hide a layer by taking it off the layer list.
3374 * This function is safe to call if the layer is not on the list.
3375 *
3376 * \param layer The layer to hide
3377 */
3378WL_EXPORT void
3379weston_layer_unset_position(struct weston_layer *layer)
3380{
3381 wl_list_remove(&layer->link);
3382 wl_list_init(&layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003383}
3384
3385WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003386weston_layer_set_mask(struct weston_layer *layer,
3387 int x, int y, int width, int height)
3388{
3389 struct weston_view *view;
3390
3391 layer->mask.x1 = x;
3392 layer->mask.x2 = x + width;
3393 layer->mask.y1 = y;
3394 layer->mask.y2 = y + height;
3395
3396 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
3397 weston_view_geometry_dirty(view);
3398 }
3399}
3400
3401WL_EXPORT void
3402weston_layer_set_mask_infinite(struct weston_layer *layer)
3403{
Adam Jackson3c3f3b12019-10-16 16:02:59 -04003404 struct weston_view *view;
3405
3406 layer->mask.x1 = INT32_MIN;
3407 layer->mask.x2 = INT32_MAX;
3408 layer->mask.y1 = INT32_MIN;
3409 layer->mask.y2 = INT32_MAX;
3410
3411 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
3412 weston_view_geometry_dirty(view);
3413 }
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003414}
3415
Daniel Stone3b775632018-07-20 08:38:25 +01003416WL_EXPORT bool
3417weston_layer_mask_is_infinite(struct weston_layer *layer)
3418{
3419 return layer->mask.x1 == INT32_MIN &&
3420 layer->mask.y1 == INT32_MIN &&
Adam Jackson3c3f3b12019-10-16 16:02:59 -04003421 layer->mask.x2 == INT32_MAX &&
3422 layer->mask.y2 == INT32_MAX;
Daniel Stone3b775632018-07-20 08:38:25 +01003423}
3424
Marius Vlad55d87362019-06-11 01:15:35 +03003425/**
3426 * \ingroup output
3427 */
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003428WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003429weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003430{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003431 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04003432 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01003433
Bryce Harrington08976ac2016-08-30 12:05:16 -07003434 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
3435 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003436 return;
3437
Pekka Paalanenb5026542014-11-12 15:09:24 +02003438 if (!output->repaint_needed)
Marius Vlad3203ff62019-09-05 14:56:12 +03003439 TL_POINT(compositor, "core_repaint_req", TLP_OUTPUT(output), TLP_END);
Pekka Paalanenb5026542014-11-12 15:09:24 +02003440
Kristian Høgsbergef044142011-06-21 15:02:12 -04003441 loop = wl_display_get_event_loop(compositor->wl_display);
Daniel Stone09a97e22017-03-01 11:34:06 +00003442 output->repaint_needed = true;
Daniel Stone05df8c12017-03-03 16:59:42 +00003443
3444 /* If we already have a repaint scheduled for our idle handler,
3445 * no need to set it again. If the repaint has been called but
3446 * not finished, then weston_output_finish_frame() will notice
3447 * that a repaint is needed and schedule one. */
3448 if (output->repaint_status != REPAINT_NOT_SCHEDULED)
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003449 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01003450
Daniel Stone05df8c12017-03-03 16:59:42 +00003451 output->repaint_status = REPAINT_BEGIN_FROM_IDLE;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03003452 assert(!output->idle_repaint_source);
3453 output->idle_repaint_source = wl_event_loop_add_idle(loop, idle_repaint,
3454 output);
Marius Vlad3203ff62019-09-05 14:56:12 +03003455 TL_POINT(compositor, "core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003456}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05003457
Marius Vlad9fdda7f2019-06-11 16:08:55 +03003458/** weston_compositor_schedule_repaint
3459 * \ingroup compositor
3460 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003461WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003462weston_compositor_schedule_repaint(struct weston_compositor *compositor)
3463{
3464 struct weston_output *output;
3465
3466 wl_list_for_each(output, &compositor->output_list, link)
3467 weston_output_schedule_repaint(output);
3468}
3469
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003470static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003471surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003472{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003473 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003474}
3475
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003476static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003477surface_attach(struct wl_client *client,
3478 struct wl_resource *resource,
3479 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
3480{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003481 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05003482 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003483
Kristian Høgsbergab19f932013-08-20 11:30:54 -07003484 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05003485 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07003486 if (buffer == NULL) {
3487 wl_client_post_no_memory(client);
3488 return;
3489 }
limin.tian4733ccf2024-08-14 08:48:55 +00003490 buffer->surface = surface;
3491 buffer->attach = buffer->attach+1;
limin.tian70492762024-08-23 02:02:12 +00003492 buffer->ec = surface->compositor;
limin.tianbf71b4c2024-08-27 09:09:34 +00003493 if (surface) {
3494 buffer->enable_display_time = surface->enable_display_time;
3495 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07003496 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04003497
Pekka Paalanende685b82012-12-04 15:58:12 +02003498 /* Attach, attach, without commit in between does not send
3499 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003500 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003501
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003502 surface->pending.sx = sx;
3503 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01003504 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003505}
3506
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003507static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003508surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003509 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003510 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05003511{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003512 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003513
Derek Foreman57e92ed2015-11-17 14:11:35 -06003514 if (width <= 0 || height <= 0)
3515 return;
3516
Derek Foreman152254b2015-11-26 14:17:48 -06003517 pixman_region32_union_rect(&surface->pending.damage_surface,
3518 &surface->pending.damage_surface,
3519 x, y, width, height);
3520}
3521
3522static void
3523surface_damage_buffer(struct wl_client *client,
3524 struct wl_resource *resource,
3525 int32_t x, int32_t y, int32_t width, int32_t height)
3526{
3527 struct weston_surface *surface = wl_resource_get_user_data(resource);
3528
3529 if (width <= 0 || height <= 0)
3530 return;
3531
3532 pixman_region32_union_rect(&surface->pending.damage_buffer,
3533 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04003534 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003535}
3536
Kristian Høgsberg33418202011-08-16 23:01:28 -04003537static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003538destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04003539{
Vlad Zahorodniic2b97472021-07-29 11:16:51 +03003540 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsberg33418202011-08-16 23:01:28 -04003541}
3542
3543static void
3544surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003545 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04003546{
Vlad Zahorodniic2b97472021-07-29 11:16:51 +03003547 struct wl_resource *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003548 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003549
Vlad Zahorodniic2b97472021-07-29 11:16:51 +03003550 cb = wl_resource_create(client, &wl_callback_interface, 1, callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003551 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003552 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003553 return;
3554 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03003555
Vlad Zahorodniic2b97472021-07-29 11:16:51 +03003556 wl_resource_set_implementation(cb, NULL, NULL,
Jason Ekstranda85118c2013-06-27 20:17:02 -05003557 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003558
leng.fang32af9fc2024-06-13 11:22:15 +08003559 wl_list_init(&surface->pending.frame_callback_list);
Vlad Zahorodniic2b97472021-07-29 11:16:51 +03003560 wl_list_insert(surface->pending.frame_callback_list.prev,
3561 wl_resource_get_link(cb));
Kristian Høgsberg33418202011-08-16 23:01:28 -04003562}
3563
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003564static void
3565surface_set_opaque_region(struct wl_client *client,
3566 struct wl_resource *resource,
3567 struct wl_resource *region_resource)
3568{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003569 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003570 struct weston_region *region;
3571
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003572 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003573 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003574 pixman_region32_copy(&surface->pending.opaque,
3575 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003576 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07003577 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003578 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003579}
3580
3581static void
3582surface_set_input_region(struct wl_client *client,
3583 struct wl_resource *resource,
3584 struct wl_resource *region_resource)
3585{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003586 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003587 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003588
3589 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003590 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003591 pixman_region32_copy(&surface->pending.input,
3592 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003593 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003594 pixman_region32_fini(&surface->pending.input);
3595 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003596 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003597}
3598
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003599/* Cause damage to this sub-surface and all its children.
3600 *
3601 * This is useful when there are state changes that need an implicit
3602 * damage, e.g. a z-order change.
3603 */
3604static void
3605weston_surface_damage_subsurfaces(struct weston_subsurface *sub)
3606{
3607 struct weston_subsurface *child;
3608
3609 weston_surface_damage(sub->surface);
3610 sub->reordered = false;
3611
3612 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link)
3613 if (child != sub)
3614 weston_surface_damage_subsurfaces(child);
3615}
3616
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003617static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003618weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003619{
Pekka Paalanene67858b2013-04-25 13:57:42 +03003620 struct weston_subsurface *sub;
3621
3622 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
3623 parent_link_pending) {
3624 wl_list_remove(&sub->parent_link);
3625 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003626
3627 if (sub->reordered)
3628 weston_surface_damage_subsurfaces(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003629 }
3630}
3631
3632static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03003633weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05003634 struct weston_matrix *matrix)
3635{
Pekka Paalanen04baea52016-04-26 15:50:58 +03003636 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05003637 double src_width, src_height, dest_width, dest_height;
3638
3639 weston_matrix_init(matrix);
3640
3641 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
3642 src_width = surface->width_from_buffer;
3643 src_height = surface->height_from_buffer;
3644 } else {
3645 src_width = wl_fixed_to_double(vp->buffer.src_width);
3646 src_height = wl_fixed_to_double(vp->buffer.src_height);
3647 }
3648
3649 if (vp->surface.width == -1) {
3650 dest_width = src_width;
3651 dest_height = src_height;
3652 } else {
3653 dest_width = vp->surface.width;
3654 dest_height = vp->surface.height;
3655 }
3656
3657 if (src_width != dest_width || src_height != dest_height)
3658 weston_matrix_scale(matrix,
3659 src_width / dest_width,
3660 src_height / dest_height, 1);
3661
3662 if (vp->buffer.src_width != wl_fixed_from_int(-1))
3663 weston_matrix_translate(matrix,
3664 wl_fixed_to_double(vp->buffer.src_x),
3665 wl_fixed_to_double(vp->buffer.src_y),
3666 0);
3667
3668 switch (vp->buffer.transform) {
3669 case WL_OUTPUT_TRANSFORM_FLIPPED:
3670 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3671 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3672 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3673 weston_matrix_scale(matrix, -1, 1, 1);
3674 weston_matrix_translate(matrix,
3675 surface->width_from_buffer, 0, 0);
3676 break;
3677 }
3678
3679 switch (vp->buffer.transform) {
3680 default:
3681 case WL_OUTPUT_TRANSFORM_NORMAL:
3682 case WL_OUTPUT_TRANSFORM_FLIPPED:
3683 break;
3684 case WL_OUTPUT_TRANSFORM_90:
3685 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +02003686 weston_matrix_rotate_xy(matrix, 0, -1);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003687 weston_matrix_translate(matrix,
Pekka Paalanen8060d822020-02-06 15:27:54 +02003688 0, surface->width_from_buffer, 0);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003689 break;
3690 case WL_OUTPUT_TRANSFORM_180:
3691 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3692 weston_matrix_rotate_xy(matrix, -1, 0);
3693 weston_matrix_translate(matrix,
3694 surface->width_from_buffer,
3695 surface->height_from_buffer, 0);
3696 break;
3697 case WL_OUTPUT_TRANSFORM_270:
3698 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +02003699 weston_matrix_rotate_xy(matrix, 0, 1);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003700 weston_matrix_translate(matrix,
Pekka Paalanen8060d822020-02-06 15:27:54 +02003701 surface->height_from_buffer, 0, 0);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003702 break;
3703 }
3704
3705 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
3706}
3707
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003708/**
3709 * Compute a + b > c while being safe to overflows.
3710 */
3711static bool
3712fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
3713{
3714 return (int64_t)a + (int64_t)b > (int64_t)c;
3715}
3716
3717static bool
3718weston_surface_is_pending_viewport_source_valid(
3719 const struct weston_surface *surface)
3720{
3721 const struct weston_surface_state *pend = &surface->pending;
3722 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
3723 int width_from_buffer = 0;
3724 int height_from_buffer = 0;
3725 wl_fixed_t w;
3726 wl_fixed_t h;
3727
3728 /* If viewport source rect is not set, it is always ok. */
3729 if (vp->buffer.src_width == wl_fixed_from_int(-1))
3730 return true;
3731
3732 if (pend->newly_attached) {
3733 if (pend->buffer) {
3734 convert_size_by_transform_scale(&width_from_buffer,
3735 &height_from_buffer,
3736 pend->buffer->width,
3737 pend->buffer->height,
3738 vp->buffer.transform,
3739 vp->buffer.scale);
3740 } else {
3741 /* No buffer: viewport is irrelevant. */
3742 return true;
3743 }
3744 } else {
3745 width_from_buffer = surface->width_from_buffer;
3746 height_from_buffer = surface->height_from_buffer;
3747 }
3748
3749 assert((width_from_buffer == 0) == (height_from_buffer == 0));
3750 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
3751
3752 /* No buffer: viewport is irrelevant. */
3753 if (width_from_buffer == 0 || height_from_buffer == 0)
3754 return true;
3755
3756 /* overflow checks for wl_fixed_from_int() */
3757 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
3758 return false;
3759 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
3760 return false;
3761
3762 w = wl_fixed_from_int(width_from_buffer);
3763 h = wl_fixed_from_int(height_from_buffer);
3764
3765 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
3766 return false;
3767 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
3768 return false;
3769
3770 return true;
3771}
3772
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003773static bool
3774fixed_is_integer(wl_fixed_t v)
3775{
3776 return (v & 0xff) == 0;
3777}
3778
3779static bool
3780weston_surface_is_pending_viewport_dst_size_int(
3781 const struct weston_surface *surface)
3782{
3783 const struct weston_buffer_viewport *vp =
3784 &surface->pending.buffer_viewport;
3785
3786 if (vp->surface.width != -1) {
3787 assert(vp->surface.width > 0 && vp->surface.height > 0);
3788 return true;
3789 }
3790
3791 return fixed_is_integer(vp->buffer.src_width) &&
3792 fixed_is_integer(vp->buffer.src_height);
3793}
3794
Derek Foreman152254b2015-11-26 14:17:48 -06003795/* Translate pending damage in buffer co-ordinates to surface
3796 * co-ordinates and union it with a pixman_region32_t.
3797 * This should only be called after the buffer is attached.
3798 */
3799static void
3800apply_damage_buffer(pixman_region32_t *dest,
3801 struct weston_surface *surface,
3802 struct weston_surface_state *state)
3803{
3804 struct weston_buffer *buffer = surface->buffer_ref.buffer;
3805
3806 /* wl_surface.damage_buffer needs to be clipped to the buffer,
3807 * translated into surface co-ordinates and unioned with
3808 * any other surface damage.
3809 * None of this makes sense if there is no buffer though.
3810 */
3811 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
3812 pixman_region32_t buffer_damage;
3813
3814 pixman_region32_intersect_rect(&state->damage_buffer,
3815 &state->damage_buffer,
3816 0, 0, buffer->width,
3817 buffer->height);
3818 pixman_region32_init(&buffer_damage);
3819 weston_matrix_transform_region(&buffer_damage,
3820 &surface->buffer_to_surface_matrix,
3821 &state->damage_buffer);
3822 pixman_region32_union(dest, dest, &buffer_damage);
3823 pixman_region32_fini(&buffer_damage);
3824 }
3825 /* We should clear this on commit even if there was no buffer */
3826 pixman_region32_clear(&state->damage_buffer);
3827}
3828
Jason Ekstrand1e059042014-10-16 10:55:19 -05003829static void
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303830weston_surface_set_desired_protection(struct weston_surface *surface,
3831 enum weston_hdcp_protection protection)
3832{
3833 if (surface->desired_protection == protection)
3834 return;
3835 surface->desired_protection = protection;
3836 weston_surface_damage(surface);
3837}
3838
3839static void
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303840weston_surface_set_protection_mode(struct weston_surface *surface,
3841 enum weston_surface_protection_mode p_mode)
3842{
3843 struct content_protection *cp = surface->compositor->content_protection;
3844 struct protected_surface *psurface;
3845
3846 surface->protection_mode = p_mode;
3847 wl_list_for_each(psurface, &cp->protected_list, link) {
3848 if (!psurface || psurface->surface != surface)
3849 continue;
3850 weston_protected_surface_send_event(psurface,
3851 surface->current_protection);
3852 }
3853}
3854
3855static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05003856weston_surface_commit_state(struct weston_surface *surface,
3857 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003858{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003859 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003860 pixman_region32_t opaque;
3861
Alexander Larsson4ea95522013-05-22 14:41:37 +02003862 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02003863 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03003864 /* wp_viewport.set_source */
3865 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003866 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003867
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003868 /* wl_surface.attach */
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003869 if (state->newly_attached) {
3870 /* zwp_surface_synchronization_v1.set_acquire_fence */
3871 fd_move(&surface->acquire_fence_fd,
3872 &state->acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003873 /* zwp_surface_synchronization_v1.get_release */
3874 weston_buffer_release_move(&surface->buffer_release_ref,
3875 &state->buffer_release_ref);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003876 weston_surface_attach(surface, state->buffer);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003877 }
Jason Ekstrand7b982072014-05-20 14:33:03 -05003878 weston_surface_state_set_buffer(state, NULL);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003879 assert(state->acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003880 assert(state->buffer_release_ref.buffer_release == NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01003881
Jason Ekstrand1e059042014-10-16 10:55:19 -05003882 weston_surface_build_buffer_matrix(surface,
3883 &surface->surface_to_buffer_matrix);
3884 weston_matrix_invert(&surface->buffer_to_surface_matrix,
3885 &surface->surface_to_buffer_matrix);
3886
Jason Ekstrand7b982072014-05-20 14:33:03 -05003887 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003888 weston_surface_update_size(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003889 if (surface->committed)
3890 surface->committed(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003891 }
Giulio Camuffo184df502013-02-21 11:29:21 +01003892
Jason Ekstrand7b982072014-05-20 14:33:03 -05003893 state->sx = 0;
3894 state->sy = 0;
3895 state->newly_attached = 0;
3896 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03003897
Derek Foreman152254b2015-11-26 14:17:48 -06003898 /* wl_surface.damage and wl_surface.damage_buffer */
Marius Vlad2a1b7862019-09-05 13:12:18 +03003899 if (pixman_region32_not_empty(&state->damage_surface) ||
3900 pixman_region32_not_empty(&state->damage_buffer))
Marius Vlad3203ff62019-09-05 14:56:12 +03003901 TL_POINT(surface->compositor, "core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06003902
Pekka Paalanen8e159182012-10-10 12:49:25 +03003903 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06003904 &state->damage_surface);
3905
3906 apply_damage_buffer(&surface->damage, surface, state);
3907
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05003908 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07003909 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06003910 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003911
3912 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003913 pixman_region32_init(&opaque);
3914 pixman_region32_intersect_rect(&opaque, &state->opaque,
3915 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003916
3917 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
3918 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003919 wl_list_for_each(view, &surface->views, surface_link)
3920 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003921 }
3922
3923 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003924
3925 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003926 pixman_region32_intersect_rect(&surface->input, &state->input,
3927 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003928
Pekka Paalanenbc106382012-10-10 12:49:31 +03003929 /* wl_surface.frame */
3930 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05003931 &state->frame_callback_list);
3932 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003933
3934 /* XXX:
3935 * What should happen with a feedback request, if there
3936 * is no wl_buffer attached for this commit?
3937 */
3938
3939 /* presentation.feedback */
3940 wl_list_insert_list(&surface->feedback_list,
3941 &state->feedback_list);
3942 wl_list_init(&state->feedback_list);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003943
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303944 /* weston_protected_surface.enforced/relaxed */
3945 if (surface->protection_mode != state->protection_mode)
3946 weston_surface_set_protection_mode(surface,
3947 state->protection_mode);
3948
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303949 /* weston_protected_surface.set_type */
3950 weston_surface_set_desired_protection(surface, state->desired_protection);
3951
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003952 wl_signal_emit(&surface->commit_signal, surface);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003953}
3954
3955static void
3956weston_surface_commit(struct weston_surface *surface)
3957{
3958 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003959
Pekka Paalanene67858b2013-04-25 13:57:42 +03003960 weston_surface_commit_subsurface_order(surface);
3961
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003962 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003963}
3964
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003965static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003966weston_subsurface_commit(struct weston_subsurface *sub);
3967
3968static void
3969weston_subsurface_parent_commit(struct weston_subsurface *sub,
3970 int parent_is_synchronized);
3971
3972static void
3973surface_commit(struct wl_client *client, struct wl_resource *resource)
3974{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003975 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003976 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
3977
leng.fang32af9fc2024-06-13 11:22:15 +08003978 weston_surface_commit_time(surface);
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003979 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
3980 assert(surface->viewport_resource);
3981
3982 wl_resource_post_error(surface->viewport_resource,
3983 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
3984 "wl_surface@%d has viewport source outside buffer",
3985 wl_resource_get_id(resource));
3986 return;
3987 }
3988
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003989 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
3990 assert(surface->viewport_resource);
3991
3992 wl_resource_post_error(surface->viewport_resource,
3993 WP_VIEWPORT_ERROR_BAD_SIZE,
3994 "wl_surface@%d viewport dst size not integer",
3995 wl_resource_get_id(resource));
3996 return;
3997 }
3998
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003999 if (surface->pending.acquire_fence_fd >= 0) {
4000 assert(surface->synchronization_resource);
4001
4002 if (!surface->pending.buffer) {
4003 fd_clear(&surface->pending.acquire_fence_fd);
4004 wl_resource_post_error(surface->synchronization_resource,
4005 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
4006 "wl_surface@%"PRIu32" no buffer for synchronization",
4007 wl_resource_get_id(resource));
4008 return;
4009 }
4010
4011 /* We support fences for both wp_linux_dmabuf and opaque EGL
4012 * buffers, as mandated by minor version 2 of the
4013 * zwp_linux_explicit_synchronization_v1 protocol. Since
4014 * renderers that support fences currently only support these
4015 * two buffer types plus SHM buffers, we can just check for the
4016 * SHM buffer case here.
4017 */
4018 if (wl_shm_buffer_get(surface->pending.buffer->resource)) {
4019 fd_clear(&surface->pending.acquire_fence_fd);
4020 wl_resource_post_error(surface->synchronization_resource,
4021 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_UNSUPPORTED_BUFFER,
4022 "wl_surface@%"PRIu32" unsupported buffer for synchronization",
4023 wl_resource_get_id(resource));
4024 return;
4025 }
4026 }
4027
Alexandros Frantzis67629672018-10-19 12:14:11 +03004028 if (surface->pending.buffer_release_ref.buffer_release &&
4029 !surface->pending.buffer) {
4030 assert(surface->synchronization_resource);
4031
4032 wl_resource_post_error(surface->synchronization_resource,
4033 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
4034 "wl_surface@%"PRIu32" no buffer for synchronization",
4035 wl_resource_get_id(resource));
4036 return;
4037 }
4038
Pekka Paalanene67858b2013-04-25 13:57:42 +03004039 if (sub) {
leng.fang32af9fc2024-06-13 11:22:15 +08004040 weston_video_subsurface_commit(surface, sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004041 return;
4042 }
4043
4044 weston_surface_commit(surface);
4045
4046 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
4047 if (sub->surface != surface)
4048 weston_subsurface_parent_commit(sub, 0);
4049 }
4050}
4051
4052static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004053surface_set_buffer_transform(struct wl_client *client,
4054 struct wl_resource *resource, int transform)
4055{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004056 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004057
Jonny Lamba55f1392014-05-30 12:07:15 +02004058 /* if wl_output.transform grows more members this will need to be updated. */
4059 if (transform < 0 ||
4060 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
4061 wl_resource_post_error(resource,
4062 WL_SURFACE_ERROR_INVALID_TRANSFORM,
4063 "buffer transform must be a valid transform "
4064 "('%d' specified)", transform);
4065 return;
4066 }
4067
Pekka Paalanen952b6c82014-03-14 14:38:15 +02004068 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004069 surface->pending.buffer_viewport.changed = 1;
chen.wang1f06854b2024-09-26 11:26:04 +00004070 weston_log("\n %s %d transform:%d\n", __FUNCTION__,__LINE__,transform);
4071 surface->video_transform = transform;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004072}
4073
Alexander Larsson4ea95522013-05-22 14:41:37 +02004074static void
4075surface_set_buffer_scale(struct wl_client *client,
4076 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004077 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004078{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004079 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02004080
Jonny Lamba55f1392014-05-30 12:07:15 +02004081 if (scale < 1) {
4082 wl_resource_post_error(resource,
4083 WL_SURFACE_ERROR_INVALID_SCALE,
4084 "buffer scale must be at least one "
4085 "('%d' specified)", scale);
4086 return;
4087 }
4088
Pekka Paalanen952b6c82014-03-14 14:38:15 +02004089 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004090 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02004091}
4092
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04004093static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004094 surface_destroy,
4095 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04004096 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004097 surface_frame,
4098 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03004099 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004100 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02004101 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06004102 surface_set_buffer_scale,
leng.fang32af9fc2024-06-13 11:22:15 +08004103 surface_damage_buffer,
4104 surface_set_video_plane,
4105 surface_set_pts,
4106 surface_keep_last_frame,
limin.tianc616dad2024-07-15 11:35:38 +00004107 surface_set_display_rate,
4108 surface_destroy_callback,
limin.tian230ec582024-07-30 09:35:17 +00004109 surface_enable_ll_mode,
chen.wang1d9c36b12024-08-05 11:45:27 +00004110 surface_set_invisible,
limin.tianbf71b4c2024-08-27 09:09:34 +00004111 surface_set_plane_mute,
chen.wang1fdc0f562024-09-04 07:57:16 +00004112 surface_enable_display_time_callback,
4113 surface_set_video_zorder
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004114};
4115
Leandro Ribeiro8eb84142021-01-18 19:36:48 -03004116static int
4117create_surface_dmabuf_feedback(struct weston_compositor *ec,
4118 struct weston_surface *surface)
4119{
4120 struct weston_dmabuf_feedback_tranche *tranche;
4121 dev_t main_device = ec->default_dmabuf_feedback->main_device;
4122 uint32_t flags = 0;
4123
4124 surface->dmabuf_feedback = weston_dmabuf_feedback_create(main_device);
4125 if (!surface->dmabuf_feedback)
4126 return -1;
4127
4128 tranche = weston_dmabuf_feedback_tranche_create(surface->dmabuf_feedback,
4129 ec->dmabuf_feedback_format_table,
4130 main_device, flags,
4131 RENDERER_PREF);
4132 if (!tranche) {
4133 weston_dmabuf_feedback_destroy(surface->dmabuf_feedback);
4134 surface->dmabuf_feedback = NULL;
4135 return -1;
4136 }
4137
4138 return 0;
4139}
4140
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004141static void
4142compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04004143 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004144{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04004145 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004146 struct weston_surface *surface;
Leandro Ribeiro8eb84142021-01-18 19:36:48 -03004147 int ret;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004148
Kristian Høgsberg18c93002012-01-27 11:58:31 -05004149 surface = weston_surface_create(ec);
Leandro Ribeiro8eb84142021-01-18 19:36:48 -03004150 if (surface == NULL)
4151 goto err;
4152
leng.fangdbaf6fa2024-06-20 19:31:04 +08004153 weston_compositor_add_surface_id(ec, surface);
4154
Leandro Ribeiro8eb84142021-01-18 19:36:48 -03004155 if (ec->default_dmabuf_feedback) {
4156 ret = create_surface_dmabuf_feedback(ec, surface);
4157 if (ret < 0)
4158 goto err_dmabuf_feedback;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04004159 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004160
Jason Ekstranda85118c2013-06-27 20:17:02 -05004161 surface->resource =
4162 wl_resource_create(client, &wl_surface_interface,
4163 wl_resource_get_version(resource), id);
Leandro Ribeiro8eb84142021-01-18 19:36:48 -03004164 if (surface->resource == NULL)
4165 goto err_dmabuf_feedback;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004166 wl_resource_set_implementation(surface->resource, &surface_interface,
4167 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07004168
4169 wl_signal_emit(&ec->create_surface_signal, surface);
Leandro Ribeiro8eb84142021-01-18 19:36:48 -03004170
4171 return;
4172
4173err_dmabuf_feedback:
4174 weston_surface_destroy(surface);
4175err:
4176 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004177}
4178
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004179static void
4180destroy_region(struct wl_resource *resource)
4181{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05004182 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004183
4184 pixman_region32_fini(&region->region);
4185 free(region);
4186}
4187
4188static void
4189region_destroy(struct wl_client *client, struct wl_resource *resource)
4190{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004191 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004192}
4193
4194static void
4195region_add(struct wl_client *client, struct wl_resource *resource,
4196 int32_t x, int32_t y, int32_t width, int32_t height)
4197{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05004198 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004199
4200 pixman_region32_union_rect(&region->region, &region->region,
4201 x, y, width, height);
4202}
4203
4204static void
4205region_subtract(struct wl_client *client, struct wl_resource *resource,
4206 int32_t x, int32_t y, int32_t width, int32_t height)
4207{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05004208 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004209 pixman_region32_t rect;
4210
4211 pixman_region32_init_rect(&rect, x, y, width, height);
4212 pixman_region32_subtract(&region->region, &region->region, &rect);
4213 pixman_region32_fini(&rect);
4214}
4215
4216static const struct wl_region_interface region_interface = {
4217 region_destroy,
4218 region_add,
4219 region_subtract
4220};
4221
4222static void
4223compositor_create_region(struct wl_client *client,
4224 struct wl_resource *resource, uint32_t id)
4225{
4226 struct weston_region *region;
4227
4228 region = malloc(sizeof *region);
4229 if (region == NULL) {
4230 wl_resource_post_no_memory(resource);
4231 return;
4232 }
4233
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004234 pixman_region32_init(&region->region);
4235
Jason Ekstranda85118c2013-06-27 20:17:02 -05004236 region->resource =
4237 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004238 if (region->resource == NULL) {
4239 free(region);
4240 wl_resource_post_no_memory(resource);
4241 return;
4242 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05004243 wl_resource_set_implementation(region->resource, &region_interface,
4244 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004245}
4246
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04004247static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004248 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004249 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004250};
4251
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004252static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03004253weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
4254{
4255 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004256
Jason Ekstrand7b982072014-05-20 14:33:03 -05004257 weston_surface_commit_state(surface, &sub->cached);
4258 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004259
4260 weston_surface_commit_subsurface_order(surface);
4261
4262 weston_surface_schedule_repaint(surface);
4263
Jason Ekstrand7b982072014-05-20 14:33:03 -05004264 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004265}
4266
4267static void
4268weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
4269{
4270 struct weston_surface *surface = sub->surface;
4271
4272 /*
4273 * If this commit would cause the surface to move by the
4274 * attach(dx, dy) parameters, the old damage region must be
4275 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05004276 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03004277 */
Derek Foreman152254b2015-11-26 14:17:48 -06004278 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004279 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06004280 pixman_region32_union(&sub->cached.damage_surface,
4281 &sub->cached.damage_surface,
4282 &surface->pending.damage_surface);
4283 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004284
4285 if (surface->pending.newly_attached) {
4286 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05004287 weston_surface_state_set_buffer(&sub->cached,
4288 surface->pending.buffer);
4289 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004290 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04004291 weston_presentation_feedback_discard_list(
4292 &sub->cached.feedback_list);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03004293 /* zwp_surface_synchronization_v1.set_acquire_fence */
4294 fd_move(&sub->cached.acquire_fence_fd,
4295 &surface->pending.acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03004296 /* zwp_surface_synchronization_v1.get_release */
4297 weston_buffer_release_move(&sub->cached.buffer_release_ref,
4298 &surface->pending.buffer_release_ref);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004299 }
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05304300 sub->cached.desired_protection = surface->pending.desired_protection;
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05304301 sub->cached.protection_mode = surface->pending.protection_mode;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03004302 assert(surface->pending.acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03004303 assert(surface->pending.buffer_release_ref.buffer_release == NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004304 sub->cached.sx += surface->pending.sx;
4305 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02004306
Derek Foreman152254b2015-11-26 14:17:48 -06004307 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
4308
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004309 sub->cached.buffer_viewport.changed |=
4310 surface->pending.buffer_viewport.changed;
4311 sub->cached.buffer_viewport.buffer =
4312 surface->pending.buffer_viewport.buffer;
4313 sub->cached.buffer_viewport.surface =
4314 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004315
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004316 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004317
4318 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
4319
4320 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
4321
4322 wl_list_insert_list(&sub->cached.frame_callback_list,
4323 &surface->pending.frame_callback_list);
4324 wl_list_init(&surface->pending.frame_callback_list);
4325
Pekka Paalanen133e4392014-09-23 22:08:46 -04004326 wl_list_insert_list(&sub->cached.feedback_list,
4327 &surface->pending.feedback_list);
4328 wl_list_init(&surface->pending.feedback_list);
4329
Jason Ekstrand7b982072014-05-20 14:33:03 -05004330 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004331}
4332
Derek Foreman280e7dd2014-10-03 13:13:42 -05004333static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03004334weston_subsurface_is_synchronized(struct weston_subsurface *sub)
4335{
4336 while (sub) {
4337 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05004338 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004339
4340 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05004341 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004342
4343 sub = weston_surface_to_subsurface(sub->parent);
4344 }
4345
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01004346 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004347}
4348
4349static void
4350weston_subsurface_commit(struct weston_subsurface *sub)
4351{
4352 struct weston_surface *surface = sub->surface;
4353 struct weston_subsurface *tmp;
4354
4355 /* Recursive check for effectively synchronized. */
4356 if (weston_subsurface_is_synchronized(sub)) {
4357 weston_subsurface_commit_to_cache(sub);
4358 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05004359 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03004360 /* flush accumulated state from cache */
4361 weston_subsurface_commit_to_cache(sub);
4362 weston_subsurface_commit_from_cache(sub);
4363 } else {
4364 weston_surface_commit(surface);
4365 }
4366
4367 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
4368 if (tmp->surface != surface)
4369 weston_subsurface_parent_commit(tmp, 0);
4370 }
4371 }
4372}
4373
4374static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004375weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004376{
4377 struct weston_surface *surface = sub->surface;
4378 struct weston_subsurface *tmp;
4379
Pekka Paalanene67858b2013-04-25 13:57:42 +03004380 /* From now on, commit_from_cache the whole sub-tree, regardless of
4381 * the synchronized mode of each child. This sub-surface or some
4382 * of its ancestors were synchronized, so we are synchronized
4383 * all the way down.
4384 */
4385
Jason Ekstrand7b982072014-05-20 14:33:03 -05004386 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004387 weston_subsurface_commit_from_cache(sub);
4388
4389 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
4390 if (tmp->surface != surface)
4391 weston_subsurface_parent_commit(tmp, 1);
4392 }
4393}
4394
4395static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004396weston_subsurface_parent_commit(struct weston_subsurface *sub,
4397 int parent_is_synchronized)
4398{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004399 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004400 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004401 wl_list_for_each(view, &sub->surface->views, surface_link)
4402 weston_view_set_position(view,
4403 sub->position.x,
4404 sub->position.y);
4405
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004406 sub->position.set = 0;
4407 }
4408
4409 if (parent_is_synchronized || sub->synchronized)
4410 weston_subsurface_synchronized_commit(sub);
4411}
4412
Pekka Paalanen8274d902014-08-06 19:36:51 +03004413static int
4414subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
4415{
4416 return snprintf(buf, len, "sub-surface");
4417}
4418
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004419static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004420subsurface_committed(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004421{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004422 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004423
Jason Ekstranda7af7042013-10-12 22:38:11 -05004424 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004425 weston_view_set_position(view,
4426 view->geometry.x + dx,
4427 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004428
4429 /* No need to check parent mappedness, because if parent is not
4430 * mapped, parent is not in a visible layer, so this sub-surface
4431 * will not be drawn either.
4432 */
Armin Krezovićf8486c32016-06-30 06:04:28 +02004433
Pekka Paalanene67858b2013-04-25 13:57:42 +03004434 if (!weston_surface_is_mapped(surface)) {
Armin Krezovićf8486c32016-06-30 06:04:28 +02004435 surface->is_mapped = true;
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03004436
Derek Foreman4b1a0a12014-09-10 15:37:33 -05004437 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03004438 * because that would call it also for the parent surface,
4439 * which might not be mapped yet. That would lead to
4440 * inconsistent state, where the window could never be
4441 * mapped.
4442 *
Armin Krezovićf8486c32016-06-30 06:04:28 +02004443 * Instead just force the is_mapped flag on, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03004444 * weston_surface_is_mapped() return true, so that when the
4445 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03004446 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03004447 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004448 }
4449}
4450
4451static struct weston_subsurface *
4452weston_surface_to_subsurface(struct weston_surface *surface)
4453{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004454 if (surface->committed == subsurface_committed)
4455 return surface->committed_private;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004456
4457 return NULL;
4458}
4459
Pekka Paalanen01388e22013-04-25 13:57:44 +03004460WL_EXPORT struct weston_surface *
4461weston_surface_get_main_surface(struct weston_surface *surface)
4462{
4463 struct weston_subsurface *sub;
4464
4465 while (surface && (sub = weston_surface_to_subsurface(surface)))
4466 surface = sub->parent;
4467
4468 return surface;
4469}
4470
Pekka Paalanen50b67472014-10-01 15:02:41 +03004471WL_EXPORT int
4472weston_surface_set_role(struct weston_surface *surface,
4473 const char *role_name,
4474 struct wl_resource *error_resource,
4475 uint32_t error_code)
4476{
4477 assert(role_name);
4478
4479 if (surface->role_name == NULL ||
4480 surface->role_name == role_name ||
4481 strcmp(surface->role_name, role_name) == 0) {
4482 surface->role_name = role_name;
4483
4484 return 0;
4485 }
4486
4487 wl_resource_post_error(error_resource, error_code,
4488 "Cannot assign role %s to wl_surface@%d,"
4489 " already has role %s\n",
4490 role_name,
4491 wl_resource_get_id(surface->resource),
4492 surface->role_name);
4493 return -1;
4494}
4495
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02004496WL_EXPORT const char *
4497weston_surface_get_role(struct weston_surface *surface)
4498{
4499 return surface->role_name;
4500}
4501
Pekka Paalanen8274d902014-08-06 19:36:51 +03004502WL_EXPORT void
4503weston_surface_set_label_func(struct weston_surface *surface,
4504 int (*desc)(struct weston_surface *,
4505 char *, size_t))
4506{
4507 surface->get_label = desc;
Marius Vlad5de92972019-10-15 13:25:41 +03004508 weston_timeline_refresh_subscription_objects(surface->compositor,
4509 surface);
Pekka Paalanen8274d902014-08-06 19:36:51 +03004510}
4511
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004512/** Get the size of surface contents
4513 *
4514 * \param surface The surface to query.
4515 * \param width Returns the width of raw contents.
4516 * \param height Returns the height of raw contents.
4517 *
4518 * Retrieves the raw surface content size in pixels for the given surface.
4519 * This is the whole content size in buffer pixels. If the surface
4520 * has no content or the renderer does not implement this feature,
4521 * zeroes are returned.
4522 *
4523 * This function is used to determine the buffer size needed for
4524 * a weston_surface_copy_content() call.
4525 */
4526WL_EXPORT void
4527weston_surface_get_content_size(struct weston_surface *surface,
4528 int *width, int *height)
4529{
4530 struct weston_renderer *rer = surface->compositor->renderer;
4531
4532 if (!rer->surface_get_content_size) {
4533 *width = 0;
4534 *height = 0;
4535 return;
4536 }
4537
4538 rer->surface_get_content_size(surface, width, height);
4539}
4540
Quentin Glidic248dd102016-08-12 10:41:34 +02004541/** Get the bounding box of a surface and its subsurfaces
4542 *
4543 * \param surface The surface to query.
4544 * \return The bounding box relative to the surface origin.
4545 *
4546 */
4547WL_EXPORT struct weston_geometry
4548weston_surface_get_bounding_box(struct weston_surface *surface)
4549{
4550 pixman_region32_t region;
4551 pixman_box32_t *box;
4552 struct weston_subsurface *subsurface;
4553
4554 pixman_region32_init_rect(&region,
4555 0, 0,
4556 surface->width, surface->height);
4557
4558 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link)
4559 pixman_region32_union_rect(&region, &region,
4560 subsurface->position.x,
4561 subsurface->position.y,
4562 subsurface->surface->width,
4563 subsurface->surface->height);
4564
4565 box = pixman_region32_extents(&region);
4566 struct weston_geometry geometry = {
4567 .x = box->x1,
4568 .y = box->y1,
4569 .width = box->x2 - box->x1,
4570 .height = box->y2 - box->y1,
4571 };
4572
4573 pixman_region32_fini(&region);
4574
4575 return geometry;
4576}
4577
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004578/** Copy surface contents to system memory.
4579 *
4580 * \param surface The surface to copy from.
4581 * \param target Pointer to the target memory buffer.
4582 * \param size Size of the target buffer in bytes.
4583 * \param src_x X location on contents to copy from.
4584 * \param src_y Y location on contents to copy from.
4585 * \param width Width in pixels of the area to copy.
4586 * \param height Height in pixels of the area to copy.
4587 * \return 0 for success, -1 for failure.
4588 *
4589 * Surface contents are maintained by the renderer. They can be in a
4590 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
4591 * else.
4592 *
4593 * Surface contents are copied into memory pointed to by target,
4594 * which has size bytes of space available. The target memory
4595 * may be larger than needed, but being smaller returns an error.
4596 * The extra bytes in target may or may not be written; their content is
4597 * unspecified. Size must be large enough to hold the image.
4598 *
4599 * The image in the target memory will be arranged in rows from
4600 * top to bottom, and pixels on a row from left to right. The pixel
4601 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
4602 * width * 4.
4603 *
4604 * Parameters src_x and src_y define the upper-left corner in buffer
4605 * coordinates (pixels) to copy from. Parameters width and height
4606 * define the size of the area to copy in pixels.
4607 *
4608 * The rectangle defined by src_x, src_y, width, height must fit in
4609 * the surface contents. Otherwise an error is returned.
4610 *
Changwoo Chof97d2502017-08-05 00:30:47 +09004611 * Use weston_surface_get_content_size to determine the content size; the
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004612 * needed target buffer size and rectangle limits.
4613 *
4614 * CURRENT IMPLEMENTATION RESTRICTIONS:
4615 * - the machine must be little-endian due to Pixman formats.
4616 *
4617 * NOTE: Pixman formats are premultiplied.
4618 */
4619WL_EXPORT int
4620weston_surface_copy_content(struct weston_surface *surface,
4621 void *target, size_t size,
4622 int src_x, int src_y,
4623 int width, int height)
4624{
4625 struct weston_renderer *rer = surface->compositor->renderer;
4626 int cw, ch;
4627 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
4628
4629 if (!rer->surface_copy_content)
4630 return -1;
4631
4632 weston_surface_get_content_size(surface, &cw, &ch);
4633
4634 if (src_x < 0 || src_y < 0)
4635 return -1;
4636
4637 if (width <= 0 || height <= 0)
4638 return -1;
4639
4640 if (src_x + width > cw || src_y + height > ch)
4641 return -1;
4642
4643 if (width * bytespp * height > size)
4644 return -1;
4645
4646 return rer->surface_copy_content(surface, target, size,
4647 src_x, src_y, width, height);
4648}
4649
Pekka Paalanene67858b2013-04-25 13:57:42 +03004650static void
4651subsurface_set_position(struct wl_client *client,
4652 struct wl_resource *resource, int32_t x, int32_t y)
4653{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004654 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004655
4656 if (!sub)
4657 return;
4658
4659 sub->position.x = x;
4660 sub->position.y = y;
4661 sub->position.set = 1;
4662}
4663
4664static struct weston_subsurface *
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004665subsurface_find_sibling(struct weston_subsurface *sub,
4666 struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004667{
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004668 struct weston_surface *parent = sub->parent;
4669 struct weston_subsurface *sibling;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004670
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004671 wl_list_for_each(sibling, &parent->subsurface_list, parent_link) {
4672 if (sibling->surface == surface && sibling != sub)
4673 return sibling;
4674 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004675
4676 return NULL;
4677}
4678
4679static struct weston_subsurface *
4680subsurface_sibling_check(struct weston_subsurface *sub,
4681 struct weston_surface *surface,
4682 const char *request)
4683{
4684 struct weston_subsurface *sibling;
4685
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004686 sibling = subsurface_find_sibling(sub, surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004687 if (!sibling) {
4688 wl_resource_post_error(sub->resource,
4689 WL_SUBSURFACE_ERROR_BAD_SURFACE,
4690 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004691 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004692 return NULL;
4693 }
4694
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004695 assert(sibling->parent == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004696
4697 return sibling;
4698}
4699
4700static void
4701subsurface_place_above(struct wl_client *client,
4702 struct wl_resource *resource,
4703 struct wl_resource *sibling_resource)
4704{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004705 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004706 struct weston_surface *surface =
4707 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004708 struct weston_subsurface *sibling;
4709
4710 if (!sub)
4711 return;
4712
4713 sibling = subsurface_sibling_check(sub, surface, "place_above");
4714 if (!sibling)
4715 return;
4716
4717 wl_list_remove(&sub->parent_link_pending);
4718 wl_list_insert(sibling->parent_link_pending.prev,
4719 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004720
4721 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004722}
4723
4724static void
4725subsurface_place_below(struct wl_client *client,
4726 struct wl_resource *resource,
4727 struct wl_resource *sibling_resource)
4728{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004729 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004730 struct weston_surface *surface =
4731 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004732 struct weston_subsurface *sibling;
4733
4734 if (!sub)
4735 return;
4736
4737 sibling = subsurface_sibling_check(sub, surface, "place_below");
4738 if (!sibling)
4739 return;
4740
4741 wl_list_remove(&sub->parent_link_pending);
4742 wl_list_insert(&sibling->parent_link_pending,
4743 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004744
4745 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004746}
4747
4748static void
4749subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
4750{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004751 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004752
4753 if (sub)
4754 sub->synchronized = 1;
4755}
4756
4757static void
4758subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
4759{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004760 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004761
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004762 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03004763 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004764
4765 /* If sub became effectively desynchronized, flush. */
4766 if (!weston_subsurface_is_synchronized(sub))
4767 weston_subsurface_synchronized_commit(sub);
4768 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004769}
4770
4771static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03004772weston_subsurface_unlink_parent(struct weston_subsurface *sub)
4773{
4774 wl_list_remove(&sub->parent_link);
4775 wl_list_remove(&sub->parent_link_pending);
4776 wl_list_remove(&sub->parent_destroy_listener.link);
4777 sub->parent = NULL;
4778}
4779
4780static void
4781weston_subsurface_destroy(struct weston_subsurface *sub);
4782
4783static void
4784subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
4785{
4786 struct weston_subsurface *sub =
4787 container_of(listener, struct weston_subsurface,
4788 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004789 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004790
4791 /* The protocol object (wl_resource) is left inert. */
4792 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004793 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004794
4795 weston_subsurface_destroy(sub);
4796}
4797
4798static void
4799subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
4800{
4801 struct weston_subsurface *sub =
4802 container_of(listener, struct weston_subsurface,
4803 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004804 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004805 assert(sub->surface != sub->parent);
4806
4807 if (weston_surface_is_mapped(sub->surface))
4808 weston_surface_unmap(sub->surface);
4809
4810 weston_subsurface_unlink_parent(sub);
4811}
4812
4813static void
4814subsurface_resource_destroy(struct wl_resource *resource)
4815{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004816 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004817
4818 if (sub)
4819 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004820}
4821
4822static void
4823subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
4824{
4825 wl_resource_destroy(resource);
4826}
4827
4828static void
4829weston_subsurface_link_parent(struct weston_subsurface *sub,
4830 struct weston_surface *parent)
4831{
4832 sub->parent = parent;
4833 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004834 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004835 &sub->parent_destroy_listener);
4836
4837 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4838 wl_list_insert(&parent->subsurface_list_pending,
4839 &sub->parent_link_pending);
4840}
4841
4842static void
4843weston_subsurface_link_surface(struct weston_subsurface *sub,
4844 struct weston_surface *surface)
4845{
4846 sub->surface = surface;
4847 sub->surface_destroy_listener.notify =
4848 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004849 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004850 &sub->surface_destroy_listener);
4851}
4852
4853static void
4854weston_subsurface_destroy(struct weston_subsurface *sub)
4855{
leng.fang32af9fc2024-06-13 11:22:15 +08004856 weston_log("\nweston_subsurface_destroy sub:%p ->%p\n", sub, sub->surface);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004857 struct weston_view *view, *next;
4858
Pekka Paalanene67858b2013-04-25 13:57:42 +03004859 assert(sub->surface);
4860
4861 if (sub->resource) {
4862 assert(weston_surface_to_subsurface(sub->surface) == sub);
4863 assert(sub->parent_destroy_listener.notify ==
4864 subsurface_handle_parent_destroy);
4865
George Kiagiadakised04d382014-06-13 18:10:26 +02004866 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
4867 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004868 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02004869 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05004870
Pekka Paalanene67858b2013-04-25 13:57:42 +03004871 if (sub->parent)
4872 weston_subsurface_unlink_parent(sub);
4873
Jason Ekstrand7b982072014-05-20 14:33:03 -05004874 weston_surface_state_fini(&sub->cached);
4875 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004876
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004877 sub->surface->committed = NULL;
4878 sub->surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004879 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004880 } else {
4881 /* the dummy weston_subsurface for the parent itself */
4882 assert(sub->parent_destroy_listener.notify == NULL);
4883 wl_list_remove(&sub->parent_link);
4884 wl_list_remove(&sub->parent_link_pending);
4885 }
4886
4887 wl_list_remove(&sub->surface_destroy_listener.link);
4888 free(sub);
4889}
4890
4891static const struct wl_subsurface_interface subsurface_implementation = {
4892 subsurface_destroy,
4893 subsurface_set_position,
4894 subsurface_place_above,
4895 subsurface_place_below,
4896 subsurface_set_sync,
4897 subsurface_set_desync
4898};
4899
4900static struct weston_subsurface *
4901weston_subsurface_create(uint32_t id, struct weston_surface *surface,
4902 struct weston_surface *parent)
4903{
4904 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004905 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004906
Bryce Harringtonde16d892014-11-20 22:21:57 -08004907 sub = zalloc(sizeof *sub);
4908 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004909 return NULL;
4910
Jason Ekstranda7af7042013-10-12 22:38:11 -05004911 wl_list_init(&sub->unused_views);
4912
Jason Ekstranda85118c2013-06-27 20:17:02 -05004913 sub->resource =
4914 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004915 if (!sub->resource) {
4916 free(sub);
4917 return NULL;
4918 }
4919
Jason Ekstranda85118c2013-06-27 20:17:02 -05004920 wl_resource_set_implementation(sub->resource,
4921 &subsurface_implementation,
4922 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004923 weston_subsurface_link_surface(sub, surface);
4924 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05004925 weston_surface_state_init(&sub->cached);
4926 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004927 sub->synchronized = 1;
leng.fang32af9fc2024-06-13 11:22:15 +08004928 weston_log("\n weston_subsurface_create :%p sub:%p\n",surface, sub );
Pekka Paalanene67858b2013-04-25 13:57:42 +03004929
4930 return sub;
4931}
4932
4933/* Create a dummy subsurface for having the parent itself in its
4934 * sub-surface lists. Makes stacking order manipulation easy.
4935 */
4936static struct weston_subsurface *
4937weston_subsurface_create_for_parent(struct weston_surface *parent)
4938{
4939 struct weston_subsurface *sub;
4940
Bryce Harringtonde16d892014-11-20 22:21:57 -08004941 sub = zalloc(sizeof *sub);
4942 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004943 return NULL;
4944
4945 weston_subsurface_link_surface(sub, parent);
4946 sub->parent = parent;
4947 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4948 wl_list_insert(&parent->subsurface_list_pending,
4949 &sub->parent_link_pending);
4950
4951 return sub;
4952}
4953
4954static void
4955subcompositor_get_subsurface(struct wl_client *client,
4956 struct wl_resource *resource,
4957 uint32_t id,
4958 struct wl_resource *surface_resource,
4959 struct wl_resource *parent_resource)
4960{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004961 struct weston_surface *surface =
4962 wl_resource_get_user_data(surface_resource);
4963 struct weston_surface *parent =
4964 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004965 struct weston_subsurface *sub;
4966 static const char where[] = "get_subsurface: wl_subsurface@";
4967
4968 if (surface == parent) {
4969 wl_resource_post_error(resource,
4970 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4971 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004972 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004973 return;
4974 }
4975
4976 if (weston_surface_to_subsurface(surface)) {
4977 wl_resource_post_error(resource,
4978 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4979 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004980 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004981 return;
4982 }
4983
Pekka Paalanen50b67472014-10-01 15:02:41 +03004984 if (weston_surface_set_role(surface, "wl_subsurface", resource,
4985 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004986 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004987
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004988 if (weston_surface_get_main_surface(parent) == surface) {
4989 wl_resource_post_error(resource,
4990 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4991 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004992 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004993 return;
4994 }
4995
Pekka Paalanene67858b2013-04-25 13:57:42 +03004996 /* make sure the parent is in its own list */
4997 if (wl_list_empty(&parent->subsurface_list)) {
4998 if (!weston_subsurface_create_for_parent(parent)) {
4999 wl_resource_post_no_memory(resource);
5000 return;
5001 }
5002 }
5003
5004 sub = weston_subsurface_create(id, surface, parent);
5005 if (!sub) {
5006 wl_resource_post_no_memory(resource);
5007 return;
5008 }
5009
Quentin Glidic2edc3d52016-08-12 10:41:33 +02005010 surface->committed = subsurface_committed;
5011 surface->committed_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03005012 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03005013}
5014
5015static void
5016subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
5017{
5018 wl_resource_destroy(resource);
5019}
5020
5021static const struct wl_subcompositor_interface subcompositor_interface = {
5022 subcompositor_destroy,
5023 subcompositor_get_subsurface
5024};
5025
5026static void
5027bind_subcompositor(struct wl_client *client,
5028 void *data, uint32_t version, uint32_t id)
5029{
5030 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05005031 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03005032
Jason Ekstranda85118c2013-06-27 20:17:02 -05005033 resource =
5034 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07005035 if (resource == NULL) {
5036 wl_client_post_no_memory(client);
5037 return;
5038 }
5039 wl_resource_set_implementation(resource, &subcompositor_interface,
5040 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03005041}
5042
Bryce Harrington0795ece2016-08-30 12:04:26 -07005043/** Set a DPMS mode on all of the compositor's outputs
Bryce Harringtonc9626a32015-12-11 13:11:38 -08005044 *
5045 * \param compositor The compositor instance
5046 * \param state The DPMS state the outputs will be set to
5047 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03005048static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005049weston_compositor_dpms(struct weston_compositor *compositor,
5050 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005051{
Emmanuel Gil Peyroteff793a2021-07-31 17:25:41 +02005052 struct weston_output *output;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005053
Emmanuel Gil Peyroteff793a2021-07-31 17:25:41 +02005054 wl_list_for_each(output, &compositor->output_list, link)
Bryce Harrington08976ac2016-08-30 12:05:16 -07005055 if (output->set_dpms)
5056 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005057}
5058
Bryce Harringtonc9626a32015-12-11 13:11:38 -08005059/** Restores the compositor to active status
5060 *
5061 * \param compositor The compositor instance
5062 *
5063 * If the compositor was in a sleeping mode, all outputs are powered
5064 * back on via DPMS. Otherwise if the compositor was inactive
5065 * (idle/locked, offscreen, or sleeping) then the compositor's wake
5066 * signal will fire.
5067 *
5068 * Restarts the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005069 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08005070 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02005071WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005072weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02005073{
Neil Roberts8b62e202013-09-30 13:14:47 +01005074 uint32_t old_state = compositor->state;
5075
5076 /* The state needs to be changed before emitting the wake
5077 * signal because that may try to schedule a repaint which
5078 * will not work if the compositor is still sleeping */
5079 compositor->state = WESTON_COMPOSITOR_ACTIVE;
5080
5081 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005082 case WESTON_COMPOSITOR_SLEEPING:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005083 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01005084 case WESTON_COMPOSITOR_OFFSCREEN:
Daniel Stone893b9362016-11-08 15:47:09 +00005085 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005086 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005087 /* fall through */
5088 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005089 wl_event_source_timer_update(compositor->idle_source,
5090 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02005091 }
5092}
5093
Bryce Harringtonc9626a32015-12-11 13:11:38 -08005094/** Turns off rendering and frame events for the compositor.
5095 *
5096 * \param compositor The compositor instance
5097 *
5098 * This is used for example to prevent further rendering while the
5099 * compositor is shutting down.
5100 *
Bryce Harringtonc9626a32015-12-11 13:11:38 -08005101 * Stops the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005102 *
5103 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08005104 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005105WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01005106weston_compositor_offscreen(struct weston_compositor *compositor)
5107{
5108 switch (compositor->state) {
5109 case WESTON_COMPOSITOR_OFFSCREEN:
5110 return;
5111 case WESTON_COMPOSITOR_SLEEPING:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01005112 default:
5113 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
5114 wl_event_source_timer_update(compositor->idle_source, 0);
5115 }
5116}
5117
Bryce Harringtonc9626a32015-12-11 13:11:38 -08005118/** Powers down all attached output devices
5119 *
5120 * \param compositor The compositor instance
5121 *
5122 * Causes rendering to the outputs to cease, and no frame events to be
5123 * sent. Only powers down the outputs if the compositor is not already
5124 * in sleep mode.
5125 *
5126 * Stops the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005127 *
5128 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08005129 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01005130WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005131weston_compositor_sleep(struct weston_compositor *compositor)
5132{
5133 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
5134 return;
5135
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01005136 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005137 compositor->state = WESTON_COMPOSITOR_SLEEPING;
5138 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
5139}
5140
Bryce Harringtonc9626a32015-12-11 13:11:38 -08005141/** Sets compositor to idle mode
5142 *
5143 * \param data The compositor instance
5144 *
5145 * This is called when the idle timer fires. Once the compositor is in
5146 * idle mode it requires a wake action (e.g. via
5147 * weston_compositor_wake()) to restore it. The compositor's
5148 * idle_signal will be triggered when the idle event occurs.
5149 *
5150 * Idleness can be inhibited by setting the compositor's idle_inhibit
5151 * property.
5152 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04005153static int
5154idle_handler(void *data)
5155{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005156 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04005157
5158 if (compositor->idle_inhibit)
5159 return 1;
5160
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005161 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005162 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04005163
5164 return 1;
5165}
5166
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005167WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08005168weston_plane_init(struct weston_plane *plane,
5169 struct weston_compositor *ec,
5170 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005171{
5172 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02005173 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005174 plane->x = x;
5175 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08005176 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03005177
5178 /* Init the link so that the call to wl_list_remove() when releasing
5179 * the plane without ever stacking doesn't lead to a crash */
5180 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005181}
5182
5183WL_EXPORT void
5184weston_plane_release(struct weston_plane *plane)
5185{
Xiong Zhang97116532013-10-23 13:58:31 +08005186 struct weston_view *view;
5187
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005188 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02005189 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03005190
Pekka Paalanen6551c092021-05-03 16:09:45 +03005191 /*
5192 * Can't use paint node list here, weston_plane is not specific to an
5193 * output.
5194 */
Xiong Zhang97116532013-10-23 13:58:31 +08005195 wl_list_for_each(view, &plane->compositor->view_list, link) {
5196 if (view->plane == plane)
5197 view->plane = NULL;
5198 }
5199
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03005200 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005201}
5202
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005203/** weston_compositor_stack_plane
5204 * \ingroup compositor
5205 */
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02005206WL_EXPORT void
5207weston_compositor_stack_plane(struct weston_compositor *ec,
5208 struct weston_plane *plane,
5209 struct weston_plane *above)
5210{
5211 if (above)
5212 wl_list_insert(above->link.prev, &plane->link);
5213 else
5214 wl_list_insert(&ec->plane_list, &plane->link);
5215}
5216
Quentin Glidic4ef719c2016-07-05 20:44:33 +02005217static void
5218output_release(struct wl_client *client, struct wl_resource *resource)
5219{
5220 wl_resource_destroy(resource);
5221}
5222
5223static const struct wl_output_interface output_interface = {
5224 output_release,
5225};
5226
5227
Casey Dahlin9074db52012-04-19 22:50:09 -04005228static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04005229{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05005230 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04005231}
5232
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005233static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005234bind_output(struct wl_client *client,
5235 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05005236{
Pekka Paalanen05347622017-03-27 12:24:34 +03005237 struct weston_head *head = data;
5238 struct weston_output *output = head->output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005239 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04005240 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05005241
Jason Ekstranda85118c2013-06-27 20:17:02 -05005242 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06005243 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07005244 if (resource == NULL) {
5245 wl_client_post_no_memory(client);
5246 return;
5247 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04005248
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005249 wl_list_insert(&head->resource_list, wl_resource_get_link(resource));
Pekka Paalanen055c1132017-03-27 16:31:25 +03005250 wl_resource_set_implementation(resource, &output_interface, head,
Pekka Paalanen05347622017-03-27 12:24:34 +03005251 unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04005252
Pekka Paalanen05347622017-03-27 12:24:34 +03005253 assert(output);
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05005254 wl_output_send_geometry(resource,
5255 output->x,
5256 output->y,
Pekka Paalanen01f60212017-03-24 15:39:24 +02005257 head->mm_width,
5258 head->mm_height,
5259 head->subpixel,
5260 head->make, head->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04005261 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04005262 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02005263 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02005264 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005265
5266 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05005267 wl_output_send_mode(resource,
5268 mode->flags,
5269 mode->width,
5270 mode->height,
5271 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005272 }
leng.fang32af9fc2024-06-13 11:22:15 +08005273 struct drm_output *drm_output = to_drm_output(output);
5274 int crtc_index = drm_output->crtc->pipe;
5275 if (version >= WL_OUTPUT_CRTC_INDEX_SINCE_VERSION)
5276 wl_output_send_crtc_index(resource, crtc_index);
Alexander Larsson4ea95522013-05-22 14:41:37 +02005277
Jasper St. Pierre0013a292014-08-07 16:43:11 -04005278 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02005279 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05005280}
5281
Pekka Paalanendcac3512017-12-08 14:13:34 +02005282static void
5283weston_head_add_global(struct weston_head *head)
5284{
5285 head->global = wl_global_create(head->compositor->wl_display,
5286 &wl_output_interface, 3,
5287 head, bind_output);
5288}
5289
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02005290/** Remove the global wl_output protocol object
5291 *
5292 * \param head The head whose global to remove.
5293 *
5294 * Also orphans the wl_resources for this head (wl_output).
5295 */
5296static void
5297weston_head_remove_global(struct weston_head *head)
5298{
5299 struct wl_resource *resource, *tmp;
5300
5301 if (head->global)
5302 wl_global_destroy(head->global);
5303 head->global = NULL;
5304
5305 wl_resource_for_each_safe(resource, tmp, &head->resource_list) {
5306 unbind_resource(resource);
5307 wl_resource_set_destructor(resource, NULL);
5308 wl_resource_set_user_data(resource, NULL);
5309 }
Roman Gilge97391c2019-03-29 13:01:06 +01005310
5311 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
5312 /* It's sufficient to unset the destructor, then the list elements
5313 * won't be accessed.
5314 */
5315 wl_resource_set_destructor(resource, NULL);
5316 }
5317 wl_list_init(&head->xdg_output_resource_list);
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02005318}
5319
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005320/** Get the backing object of wl_output
5321 *
5322 * \param resource A wl_output protocol object.
5323 * \return The backing object (user data) of a wl_resource representing a
5324 * wl_output protocol object.
Marius Vlad78984ee2019-06-11 00:05:08 +03005325 *
5326 * \ingroup head
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005327 */
Pekka Paalanen055c1132017-03-27 16:31:25 +03005328WL_EXPORT struct weston_head *
5329weston_head_from_resource(struct wl_resource *resource)
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005330{
5331 assert(wl_resource_instance_of(resource, &wl_output_interface,
5332 &output_interface));
5333
5334 return wl_resource_get_user_data(resource);
5335}
5336
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005337/** Initialize a pre-allocated weston_head
5338 *
5339 * \param head The head to initialize.
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005340 * \param name The head name, e.g. the connector name or equivalent.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005341 *
5342 * The head will be safe to attach, detach and release.
5343 *
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005344 * The name is used in logs, and can be used by compositors as a configuration
5345 * identifier.
5346 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005347 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005348 * \internal
5349 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005350WL_EXPORT void
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005351weston_head_init(struct weston_head *head, const char *name)
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005352{
5353 /* Add some (in)sane defaults which can be used
5354 * for checking if an output was properly configured
5355 */
5356 memset(head, 0, sizeof *head);
5357
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005358 wl_list_init(&head->compositor_link);
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005359 wl_signal_init(&head->destroy_signal);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005360 wl_list_init(&head->output_link);
5361 wl_list_init(&head->resource_list);
Roman Gilge97391c2019-03-29 13:01:06 +01005362 wl_list_init(&head->xdg_output_resource_list);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005363 head->name = strdup(name);
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305364 head->current_protection = WESTON_HDCP_DISABLE;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005365}
5366
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005367/** Send output heads changed signal
5368 *
5369 * \param output The output that changed.
5370 *
5371 * Notify that the enabled output gained and/or lost heads, or that the
5372 * associated heads may have changed their connection status. This does not
5373 * include cases where the output becomes enabled or disabled. The registered
5374 * callbacks are called after the change has successfully happened.
5375 *
5376 * If connection status change causes the compositor to attach or detach a head
5377 * to an enabled output, the registered callbacks may be called multiple times.
Marius Vlad55d87362019-06-11 01:15:35 +03005378 *
5379 * \ingroup output
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005380 */
5381static void
5382weston_output_emit_heads_changed(struct weston_output *output)
5383{
5384 wl_signal_emit(&output->compositor->output_heads_changed_signal,
5385 output);
5386}
5387
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005388/** Idle task for emitting heads_changed_signal */
5389static void
5390weston_compositor_call_heads_changed(void *data)
5391{
5392 struct weston_compositor *compositor = data;
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005393 struct weston_head *head;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005394
5395 compositor->heads_changed_source = NULL;
5396
5397 wl_signal_emit(&compositor->heads_changed_signal, compositor);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005398
5399 wl_list_for_each(head, &compositor->head_list, compositor_link) {
5400 if (head->output && head->output->enabled)
5401 weston_output_emit_heads_changed(head->output);
5402 }
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005403}
5404
5405/** Schedule a call on idle to heads_changed callback
5406 *
5407 * \param compositor The Compositor.
5408 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005409 * \ingroup compositor
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005410 * \internal
5411 */
5412static void
5413weston_compositor_schedule_heads_changed(struct weston_compositor *compositor)
5414{
5415 struct wl_event_loop *loop;
5416
5417 if (compositor->heads_changed_source)
5418 return;
5419
5420 loop = wl_display_get_event_loop(compositor->wl_display);
5421 compositor->heads_changed_source = wl_event_loop_add_idle(loop,
5422 weston_compositor_call_heads_changed, compositor);
5423}
5424
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005425/** Register a new head
5426 *
5427 * \param compositor The compositor.
5428 * \param head The head to register, must not be already registered.
5429 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005430 * This signals the core that a new head has become available, leading to
5431 * heads_changed hook being called later.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005432 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005433 * \ingroup compositor
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005434 * \internal
5435 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005436WL_EXPORT void
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005437weston_compositor_add_head(struct weston_compositor *compositor,
5438 struct weston_head *head)
5439{
5440 assert(wl_list_empty(&head->compositor_link));
5441 assert(head->name);
5442
5443 wl_list_insert(compositor->head_list.prev, &head->compositor_link);
5444 head->compositor = compositor;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005445 weston_compositor_schedule_heads_changed(compositor);
5446}
5447
5448/** Adds a listener to be called when heads change
5449 *
5450 * \param compositor The compositor.
5451 * \param listener The listener to add.
5452 *
Marius Vlada2dace22019-06-12 16:05:44 +03005453 * The listener notify function argument is weston_compositor.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005454 *
5455 * The listener function will be called after heads are added or their
5456 * connection status has changed. Several changes may be accumulated into a
5457 * single call. The user is expected to iterate over the existing heads and
5458 * check their statuses to find out what changed.
5459 *
5460 * \sa weston_compositor_iterate_heads, weston_head_is_connected,
5461 * weston_head_is_enabled
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005462 * \ingroup compositor
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005463 */
5464WL_EXPORT void
5465weston_compositor_add_heads_changed_listener(struct weston_compositor *compositor,
5466 struct wl_listener *listener)
5467{
5468 wl_signal_add(&compositor->heads_changed_signal, listener);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005469}
5470
5471/** Iterate over available heads
5472 *
5473 * \param compositor The compositor.
Marius Vlada2dace22019-06-12 16:05:44 +03005474 * \param iter The iterator, or NULL for start.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005475 * \return The next available head in the list.
5476 *
5477 * Returns all available heads, regardless of being connected or enabled.
5478 *
5479 * You can iterate over all heads as follows:
5480 * \code
5481 * struct weston_head *head = NULL;
5482 *
5483 * while ((head = weston_compositor_iterate_heads(compositor, head))) {
5484 * ...
5485 * }
5486 * \endcode
5487 *
5488 * If you cause \c iter to be removed from the list, you cannot use it to
5489 * continue iterating. Removing any other item is safe.
5490 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005491 * \ingroup compositor
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005492 */
5493WL_EXPORT struct weston_head *
5494weston_compositor_iterate_heads(struct weston_compositor *compositor,
5495 struct weston_head *iter)
5496{
5497 struct wl_list *list = &compositor->head_list;
5498 struct wl_list *node;
5499
5500 assert(compositor);
5501 assert(!iter || iter->compositor == compositor);
5502
5503 if (iter)
5504 node = iter->compositor_link.next;
5505 else
5506 node = list->next;
5507
5508 assert(node);
5509 assert(!iter || node != &iter->compositor_link);
5510
5511 if (node == list)
5512 return NULL;
5513
5514 return container_of(node, struct weston_head, compositor_link);
5515}
5516
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005517/** Iterate over attached heads
5518 *
5519 * \param output The output whose heads to iterate.
Marius Vlada2dace22019-06-12 16:05:44 +03005520 * \param iter The iterator, or NULL for start.
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005521 * \return The next attached head in the list.
5522 *
5523 * Returns all heads currently attached to the output.
5524 *
5525 * You can iterate over all heads as follows:
5526 * \code
5527 * struct weston_head *head = NULL;
5528 *
5529 * while ((head = weston_output_iterate_heads(output, head))) {
5530 * ...
5531 * }
5532 * \endcode
5533 *
5534 * If you cause \c iter to be removed from the list, you cannot use it to
5535 * continue iterating. Removing any other item is safe.
5536 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05005537 * \ingroup output
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005538 */
5539WL_EXPORT struct weston_head *
5540weston_output_iterate_heads(struct weston_output *output,
5541 struct weston_head *iter)
5542{
5543 struct wl_list *list = &output->head_list;
5544 struct wl_list *node;
5545
5546 assert(output);
5547 assert(!iter || iter->output == output);
5548
5549 if (iter)
5550 node = iter->output_link.next;
5551 else
5552 node = list->next;
5553
5554 assert(node);
5555 assert(!iter || node != &iter->output_link);
5556
5557 if (node == list)
5558 return NULL;
5559
5560 return container_of(node, struct weston_head, output_link);
5561}
5562
Pekka Paalanendcac3512017-12-08 14:13:34 +02005563/** Attach a head to an output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005564 *
5565 * \param output The output to attach to.
5566 * \param head The head that is not yet attached.
5567 * \return 0 on success, -1 on failure.
5568 *
5569 * Attaches the given head to the output. All heads of an output are clones
5570 * and share the resolution and timings.
5571 *
5572 * Cloning heads this way uses less resources than creating an output for
5573 * each head, but is not always possible due to environment, driver and hardware
5574 * limitations.
5575 *
5576 * On failure, the head remains unattached. Success of this function does not
5577 * guarantee the output configuration is actually valid. The final checks are
Pekka Paalanendcac3512017-12-08 14:13:34 +02005578 * made on weston_output_enable() unless the output was already enabled.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005579 *
Marius Vlad55d87362019-06-11 01:15:35 +03005580 * \ingroup output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005581 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005582WL_EXPORT int
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005583weston_output_attach_head(struct weston_output *output,
5584 struct weston_head *head)
5585{
Pekka Paalanendcac3512017-12-08 14:13:34 +02005586 char *head_names;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005587
5588 if (!wl_list_empty(&head->output_link))
5589 return -1;
5590
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005591 if (output->attach_head) {
5592 if (output->attach_head(output, head) < 0)
5593 return -1;
5594 } else if (!wl_list_empty(&output->head_list)) {
5595 /* No support for clones in the legacy path. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005596 return -1;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005597 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005598
5599 head->output = output;
5600 wl_list_insert(output->head_list.prev, &head->output_link);
5601
Pekka Paalanendcac3512017-12-08 14:13:34 +02005602 if (output->enabled) {
5603 weston_head_add_global(head);
5604
5605 head_names = weston_output_create_heads_string(output);
5606 weston_log("Output '%s' updated to have head(s) %s\n",
5607 output->name, head_names);
5608 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005609
5610 weston_output_emit_heads_changed(output);
Pekka Paalanendcac3512017-12-08 14:13:34 +02005611 }
5612
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005613 return 0;
5614}
5615
5616/** Detach a head from its output
5617 *
5618 * \param head The head to detach.
5619 *
5620 * It is safe to detach a non-attached head.
5621 *
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005622 * If the head is attached to an enabled output and the output will be left
5623 * with no heads, the output will be disabled.
5624 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005625 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005626 * \sa weston_output_disable
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005627 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005628WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005629weston_head_detach(struct weston_head *head)
5630{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005631 struct weston_output *output = head->output;
Pekka Paalanena0106992017-12-08 16:11:17 +02005632 char *head_names;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005633
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005634 wl_list_remove(&head->output_link);
5635 wl_list_init(&head->output_link);
5636 head->output = NULL;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005637
5638 if (!output)
5639 return;
5640
5641 if (output->detach_head)
5642 output->detach_head(output, head);
5643
5644 if (output->enabled) {
5645 weston_head_remove_global(head);
5646
Pekka Paalanena0106992017-12-08 16:11:17 +02005647 if (wl_list_empty(&output->head_list)) {
5648 weston_log("Output '%s' no heads left, disabling.\n",
5649 output->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005650 weston_output_disable(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02005651 } else {
5652 head_names = weston_output_create_heads_string(output);
5653 weston_log("Output '%s' updated to have head(s) %s\n",
5654 output->name, head_names);
5655 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005656
5657 weston_output_emit_heads_changed(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02005658 }
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005659 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005660}
5661
5662/** Destroy a head
5663 *
5664 * \param head The head to be released.
5665 *
5666 * Destroys the head. The caller is responsible for freeing the memory pointed
5667 * to by \c head.
5668 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005669 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005670 * \internal
5671 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005672WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005673weston_head_release(struct weston_head *head)
5674{
Marius Vladf2452d62021-12-09 14:07:21 +02005675 weston_signal_emit_mutable(&head->destroy_signal, head);
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005676
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005677 weston_head_detach(head);
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005678
5679 free(head->make);
5680 free(head->model);
5681 free(head->serial_number);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005682 free(head->name);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005683
5684 wl_list_remove(&head->compositor_link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005685}
5686
Pekka Paalanene19970f2017-08-28 14:11:02 +03005687static void
5688weston_head_set_device_changed(struct weston_head *head)
5689{
5690 head->device_changed = true;
5691
5692 if (head->compositor)
5693 weston_compositor_schedule_heads_changed(head->compositor);
5694}
5695
5696/** String equal comparison with NULLs being equal */
5697static bool
5698str_null_eq(const char *a, const char *b)
5699{
5700 if (!a && !b)
5701 return true;
5702
5703 if (!!a != !!b)
5704 return false;
5705
5706 return strcmp(a, b) == 0;
5707}
5708
Pekka Paalanen01f60212017-03-24 15:39:24 +02005709/** Store monitor make, model and serial number
5710 *
5711 * \param head The head to modify.
5712 * \param make The monitor make. If EDID is available, the PNP ID. Otherwise
5713 * any string, or NULL for none.
5714 * \param model The monitor model or name, or a made-up string, or NULL for
5715 * none.
5716 * \param serialno The monitor serial number, a made-up string, or NULL for
5717 * none.
5718 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005719 * This may set the device_changed flag.
5720 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005721 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005722 * \internal
5723 */
5724WL_EXPORT void
5725weston_head_set_monitor_strings(struct weston_head *head,
5726 const char *make,
5727 const char *model,
5728 const char *serialno)
5729{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005730 if (str_null_eq(head->make, make) &&
5731 str_null_eq(head->model, model) &&
5732 str_null_eq(head->serial_number, serialno))
5733 return;
5734
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005735 free(head->make);
5736 free(head->model);
5737 free(head->serial_number);
5738
5739 head->make = make ? strdup(make) : NULL;
5740 head->model = model ? strdup(model) : NULL;
5741 head->serial_number = serialno ? strdup(serialno) : NULL;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005742
5743 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005744}
5745
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005746/** Store display non-desktop status
5747 *
5748 * \param head The head to modify.
5749 * \param non_desktop Whether the head connects to a non-desktop display.
5750 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005751 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005752 * \internal
5753 */
5754WL_EXPORT void
5755weston_head_set_non_desktop(struct weston_head *head, bool non_desktop)
5756{
5757 if (head->non_desktop == non_desktop)
5758 return;
5759
5760 head->non_desktop = non_desktop;
5761
5762 weston_head_set_device_changed(head);
5763}
5764
Lucas Stacha69cb712019-11-25 23:29:31 +00005765/** Store display transformation
5766 *
5767 * \param head The head to modify.
5768 * \param transform The transformation to apply for this head
5769 *
5770 * This may set the device_changed flag.
5771 *
5772 * \ingroup head
5773 * \internal
5774 */
5775WL_EXPORT void
5776weston_head_set_transform(struct weston_head *head, uint32_t transform)
5777{
5778 if (head->transform == transform)
5779 return;
5780
5781 head->transform = transform;
5782
5783 weston_head_set_device_changed(head);
5784}
5785
5786
Pekka Paalanen01f60212017-03-24 15:39:24 +02005787/** Store physical image size
5788 *
5789 * \param head The head to modify.
5790 * \param mm_width Image area width in millimeters.
5791 * \param mm_height Image area height in millimeters.
5792 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005793 * This may set the device_changed flag.
5794 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005795 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005796 * \internal
5797 */
5798WL_EXPORT void
5799weston_head_set_physical_size(struct weston_head *head,
5800 int32_t mm_width, int32_t mm_height)
5801{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005802 if (head->mm_width == mm_width &&
5803 head->mm_height == mm_height)
5804 return;
5805
Pekka Paalanen01f60212017-03-24 15:39:24 +02005806 head->mm_width = mm_width;
5807 head->mm_height = mm_height;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005808
5809 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005810}
5811
5812/** Store monitor sub-pixel layout
5813 *
5814 * \param head The head to modify.
5815 * \param sp Sub-pixel layout. The possible values are:
5816 * - WL_OUTPUT_SUBPIXEL_UNKNOWN,
5817 * - WL_OUTPUT_SUBPIXEL_NONE,
5818 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB,
5819 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR,
5820 * - WL_OUTPUT_SUBPIXEL_VERTICAL_RGB,
5821 * - WL_OUTPUT_SUBPIXEL_VERTICAL_BGR
5822 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005823 * This may set the device_changed flag.
5824 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005825 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005826 * \internal
5827 */
5828WL_EXPORT void
5829weston_head_set_subpixel(struct weston_head *head,
5830 enum wl_output_subpixel sp)
5831{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005832 if (head->subpixel == sp)
5833 return;
5834
Pekka Paalanen01f60212017-03-24 15:39:24 +02005835 head->subpixel = sp;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005836
5837 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005838}
5839
5840/** Mark the monitor as internal
5841 *
5842 * This is used for embedded screens, like laptop panels.
5843 *
5844 * \param head The head to mark as internal.
5845 *
5846 * By default a head is external. The type is often inferred from the physical
5847 * connector type.
5848 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005849 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005850 * \internal
5851 */
5852WL_EXPORT void
5853weston_head_set_internal(struct weston_head *head)
5854{
5855 head->connection_internal = true;
5856}
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005857
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005858/** Store connector status
5859 *
5860 * \param head The head to modify.
5861 * \param connected Whether the head is connected.
5862 *
5863 * Connectors are created as disconnected. This function can be used to
5864 * set the connector status.
5865 *
5866 * The status should be set to true when a physical connector is connected to
5867 * a video sink device like a monitor and to false when the connector is
5868 * disconnected. For nested backends, the connection status should reflect the
5869 * connection to the parent display server.
5870 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005871 * When the connection status changes, it schedules a call to the heads_changed
Pekka Paalanene19970f2017-08-28 14:11:02 +03005872 * hook and sets the device_changed flag.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005873 *
5874 * \sa weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005875 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005876 * \internal
5877 */
5878WL_EXPORT void
5879weston_head_set_connection_status(struct weston_head *head, bool connected)
5880{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005881 if (head->connected == connected)
5882 return;
5883
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005884 head->connected = connected;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005885
Pekka Paalanene19970f2017-08-28 14:11:02 +03005886 weston_head_set_device_changed(head);
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005887}
5888
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305889static void
5890weston_output_compute_protection(struct weston_output *output)
5891{
5892 struct weston_head *head;
5893 enum weston_hdcp_protection op_protection;
5894 bool op_protection_valid = false;
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305895 struct weston_compositor *wc = output->compositor;
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305896
5897 wl_list_for_each(head, &output->head_list, output_link) {
5898 if (!op_protection_valid) {
5899 op_protection = head->current_protection;
5900 op_protection_valid = true;
5901 }
5902 if (head->current_protection < op_protection)
5903 op_protection = head->current_protection;
5904 }
5905
5906 if (!op_protection_valid)
5907 op_protection = WESTON_HDCP_DISABLE;
5908
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305909 if (output->current_protection != op_protection) {
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305910 output->current_protection = op_protection;
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305911 weston_output_damage(output);
Ankit Nautiyalfaa5ab42019-07-08 13:23:24 +05305912 weston_schedule_surface_protection_update(wc);
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305913 }
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305914}
5915
5916WL_EXPORT void
5917weston_head_set_content_protection_status(struct weston_head *head,
5918 enum weston_hdcp_protection status)
5919{
5920 head->current_protection = status;
5921 if (head->output)
5922 weston_output_compute_protection(head->output);
5923}
5924
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005925/** Is the head currently connected?
5926 *
5927 * \param head The head to query.
5928 * \return Connection status.
5929 *
5930 * Returns true if the head is physically connected to a monitor, or in
5931 * case of a nested backend returns true when there is a connection to the
5932 * parent display server.
5933 *
5934 * This is independent from the head being enabled.
5935 *
5936 * \sa weston_head_is_enabled
Marius Vlad78984ee2019-06-11 00:05:08 +03005937 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005938 */
5939WL_EXPORT bool
5940weston_head_is_connected(struct weston_head *head)
5941{
5942 return head->connected;
5943}
5944
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005945/** Is the head currently enabled?
5946 *
5947 * \param head The head to query.
5948 * \return Video status.
5949 *
5950 * Returns true if the head is currently transmitting a video stream.
5951 *
5952 * This is independent of the head being connected.
5953 *
5954 * \sa weston_head_is_connected
Marius Vlad78984ee2019-06-11 00:05:08 +03005955 * \ingroup head
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005956 */
5957WL_EXPORT bool
5958weston_head_is_enabled(struct weston_head *head)
5959{
5960 if (!head->output)
5961 return false;
5962
5963 return head->output->enabled;
5964}
5965
Pekka Paalanene19970f2017-08-28 14:11:02 +03005966/** Has the device information changed?
5967 *
5968 * \param head The head to query.
5969 * \return True if the device information has changed since last reset.
5970 *
5971 * The information about the connected display device, e.g. a monitor, may
5972 * change without being disconnected in between. Changing information
5973 * causes a call to the heads_changed hook.
5974 *
5975 * The information includes make, model, serial number, physical size,
5976 * and sub-pixel type. The connection status is also included.
5977 *
5978 * \sa weston_head_reset_device_changed, weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005979 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03005980 */
5981WL_EXPORT bool
5982weston_head_is_device_changed(struct weston_head *head)
5983{
5984 return head->device_changed;
5985}
5986
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005987/** Does the head represent a non-desktop display?
5988 *
5989 * \param head The head to query.
5990 * \return True if the device is a non-desktop display.
5991 *
5992 * Non-desktop heads are not attached to outputs by default.
5993 * This stops weston from extending the desktop onto head mounted displays.
5994 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005995 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005996 */
5997WL_EXPORT bool
5998weston_head_is_non_desktop(struct weston_head *head)
5999{
6000 return head->non_desktop;
6001}
6002
Pekka Paalanene19970f2017-08-28 14:11:02 +03006003/** Acknowledge device information change
6004 *
6005 * \param head The head to acknowledge.
6006 *
6007 * Clears the device changed flag on this head. When a compositor has processed
6008 * device information, it should call this to be able to notice further
6009 * changes.
6010 *
6011 * \sa weston_head_is_device_changed
Marius Vlad78984ee2019-06-11 00:05:08 +03006012 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03006013 */
6014WL_EXPORT void
6015weston_head_reset_device_changed(struct weston_head *head)
6016{
6017 head->device_changed = false;
6018}
6019
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006020/** Get the name of a head
6021 *
6022 * \param head The head to query.
6023 * \return The head's name, not NULL.
6024 *
6025 * The name depends on the backend. The DRM backend uses connector names,
6026 * other backends may use hardcoded names or user-given names.
Marius Vlad78984ee2019-06-11 00:05:08 +03006027 *
6028 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006029 */
6030WL_EXPORT const char *
6031weston_head_get_name(struct weston_head *head)
6032{
6033 return head->name;
6034}
6035
6036/** Get the output the head is attached to
6037 *
6038 * \param head The head to query.
6039 * \return The output the head is attached to, or NULL if detached.
Marius Vlad78984ee2019-06-11 00:05:08 +03006040 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006041 */
6042WL_EXPORT struct weston_output *
6043weston_head_get_output(struct weston_head *head)
6044{
6045 return head->output;
6046}
6047
Lucas Stacha69cb712019-11-25 23:29:31 +00006048/** Get the head's native transformation
6049 *
6050 * \param head The head to query.
6051 * \return The head's native transform, as a WL_OUTPUT_TRANSFORM_* value
6052 *
6053 * A weston_head may have a 'native' transform provided by the backend.
6054 * Examples include panels which are physically rotated, where the rotation
6055 * is recorded and described as part of the system configuration. This call
6056 * will return any known native transform for the head.
6057 *
6058 * \ingroup head
6059 */
6060WL_EXPORT uint32_t
6061weston_head_get_transform(struct weston_head *head)
6062{
6063 return head->transform;
6064}
6065
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03006066/** Add destroy callback for a head
6067 *
6068 * \param head The head to watch for.
6069 * \param listener The listener to add. The \c notify member must be set.
6070 *
6071 * Heads may get destroyed for various reasons by the backends. If a head is
6072 * attached to an output, the compositor should listen for head destruction
6073 * and reconfigure or destroy the output if necessary.
6074 *
6075 * The destroy callbacks will be called on weston_head destruction before any
6076 * automatic detaching from an associated weston_output and before any
6077 * weston_head information is lost.
6078 *
6079 * The \c data argument to the notify callback is the weston_head being
6080 * destroyed.
Marius Vlad78984ee2019-06-11 00:05:08 +03006081 *
6082 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03006083 */
6084WL_EXPORT void
6085weston_head_add_destroy_listener(struct weston_head *head,
6086 struct wl_listener *listener)
6087{
6088 wl_signal_add(&head->destroy_signal, listener);
6089}
6090
6091/** Look up destroy listener for a head
6092 *
6093 * \param head The head to query.
6094 * \param notify The notify function used used for the added destroy listener.
6095 * \return The listener, or NULL if not found.
6096 *
6097 * This looks up the previously added destroy listener struct based on the
6098 * notify function it has. The listener can be used to access user data
6099 * through \c container_of().
6100 *
6101 * \sa wl_signal_get()
Marius Vlad78984ee2019-06-11 00:05:08 +03006102 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03006103 */
6104WL_EXPORT struct wl_listener *
6105weston_head_get_destroy_listener(struct weston_head *head,
6106 wl_notify_func_t notify)
6107{
6108 return wl_signal_get(&head->destroy_signal, notify);
6109}
6110
David Fort0de859e2016-05-27 23:22:57 +02006111/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02006112static void
David Fort0de859e2016-05-27 23:22:57 +02006113weston_compositor_reflow_outputs(struct weston_compositor *compositor,
6114 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02006115{
6116 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02006117 bool start_resizing = false;
6118
6119 if (!delta_width)
6120 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02006121
6122 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02006123 if (output == resized_output) {
6124 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02006125 continue;
6126 }
6127
David Fort0de859e2016-05-27 23:22:57 +02006128 if (start_resizing) {
6129 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02006130 output->dirty = 1;
6131 }
6132 }
6133}
6134
Pekka Paalanen06920792020-12-08 15:22:36 +02006135/** Transform a region in-place from global to output coordinates
6136 *
6137 * \param output The output that defines the transformation.
6138 * \param region The region to be transformed in-place.
6139 *
6140 * This takes a region in the global coordinate system, and takes into account
6141 * output position, transform and scale, and the zoom, and converts the region
6142 * into output pixel coordinates in the framebuffer.
6143 *
6144 * Uses floating-point operations if zoom is active, which may round to expand
6145 * the region.
6146 *
6147 * \internal
6148 * \ingroup output
6149 */
6150WL_EXPORT void
6151weston_output_region_from_global(struct weston_output *output,
6152 pixman_region32_t *region)
6153{
6154 if (output->zoom.active) {
6155 weston_matrix_transform_region(region, &output->matrix, region);
6156 } else {
6157 pixman_region32_translate(region, -output->x, -output->y);
6158 weston_transformed_region(output->width, output->height,
6159 output->transform,
6160 output->current_scale,
6161 region, region);
6162 }
6163}
6164
Pekka Paalanend72bad22017-03-29 17:01:41 +03006165static void
Scott Moreauccbf29d2012-02-22 14:21:41 -07006166weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01006167{
Scott Moreau850ca422012-05-21 15:21:25 -06006168 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05006169
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006170 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05006171 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06006172
Scott Moreauccbf29d2012-02-22 14:21:41 -07006173 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06006174 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05006175 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01006176 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05006177 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01006178 weston_matrix_scale(&output->matrix, magnification,
6179 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07006180 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04006181
Jason Ekstrandfb23df72014-10-16 10:55:21 -05006182 switch (output->transform) {
6183 case WL_OUTPUT_TRANSFORM_FLIPPED:
6184 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
6185 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
6186 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
6187 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
6188 weston_matrix_scale(&output->matrix, -1, 1, 1);
6189 break;
6190 }
6191
6192 switch (output->transform) {
6193 default:
6194 case WL_OUTPUT_TRANSFORM_NORMAL:
6195 case WL_OUTPUT_TRANSFORM_FLIPPED:
6196 break;
6197 case WL_OUTPUT_TRANSFORM_90:
6198 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +02006199 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
6200 weston_matrix_rotate_xy(&output->matrix, 0, -1);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05006201 break;
6202 case WL_OUTPUT_TRANSFORM_180:
6203 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
6204 weston_matrix_translate(&output->matrix,
6205 -output->width, -output->height, 0);
6206 weston_matrix_rotate_xy(&output->matrix, -1, 0);
6207 break;
6208 case WL_OUTPUT_TRANSFORM_270:
6209 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +02006210 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
6211 weston_matrix_rotate_xy(&output->matrix, 0, 1);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05006212 break;
6213 }
6214
6215 if (output->current_scale != 1)
6216 weston_matrix_scale(&output->matrix,
6217 output->current_scale,
6218 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01006219
Scott Moreauccbf29d2012-02-22 14:21:41 -07006220 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05006221
6222 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07006223}
6224
Scott Moreau1bad5db2012-08-18 01:04:05 -06006225static void
Alexander Larsson0b135062013-05-28 16:23:36 +02006226weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06006227{
6228 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03006229 output->native_scale = scale;
6230 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06006231
Pekka Paalanen59987fa2016-04-26 15:50:59 +03006232 convert_size_by_transform_scale(&output->width, &output->height,
6233 output->current_mode->width,
6234 output->current_mode->height,
6235 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02006236}
6237
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006238static void
6239weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07006240{
6241 output->x = x;
6242 output->y = y;
6243
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006244 pixman_region32_fini(&output->region);
Scott Moreauccbf29d2012-02-22 14:21:41 -07006245 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06006246 output->width,
6247 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01006248}
6249
Marius Vlad55d87362019-06-11 01:15:35 +03006250/**
6251 * \ingroup output
6252 */
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006253WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006254weston_output_move(struct weston_output *output, int x, int y)
6255{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006256 struct weston_head *head;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006257 struct wl_resource *resource;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006258 int ver;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006259
6260 output->move_x = x - output->x;
6261 output->move_y = y - output->y;
6262
6263 if (output->move_x == 0 && output->move_y == 0)
6264 return;
6265
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006266 weston_output_init_geometry(output, x, y);
6267
6268 output->dirty = 1;
6269
6270 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006271 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006272
6273 /* Notify clients of the change for output position. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006274 wl_list_for_each(head, &output->head_list, output_link) {
6275 wl_resource_for_each(resource, &head->resource_list) {
6276 wl_output_send_geometry(resource,
6277 output->x,
6278 output->y,
6279 head->mm_width,
6280 head->mm_height,
6281 head->subpixel,
6282 head->make,
6283 head->model,
6284 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08006285
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006286 ver = wl_resource_get_version(resource);
6287 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
6288 wl_output_send_done(resource);
6289 }
Roman Gilge97391c2019-03-29 13:01:06 +01006290
6291 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
6292 zxdg_output_v1_send_logical_position(resource,
6293 output->x,
6294 output->y);
6295 zxdg_output_v1_send_done(resource);
6296 }
Quanxian Wangb2c86362014-03-14 09:16:25 +08006297 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006298}
6299
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006300/** Signal that a pending output is taken into use.
6301 *
6302 * Removes the output from the pending list and adds it to the compositor's
6303 * list of enabled outputs. The output created signal is emitted.
Giulio Camuffob1147152015-05-06 21:41:57 +03006304 *
Pekka Paalanen2210ad02017-03-30 15:48:06 +03006305 * The output gets an internal ID assigned, and the wl_output global is
6306 * created.
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03006307 *
Giulio Camuffob1147152015-05-06 21:41:57 +03006308 * \param compositor The compositor instance.
6309 * \param output The output to be added.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006310 *
6311 * \internal
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006312 * \ingroup compositor
Giulio Camuffob1147152015-05-06 21:41:57 +03006313 */
Pekka Paalanenf9681b52017-03-29 16:58:48 +03006314static void
Giulio Camuffob1147152015-05-06 21:41:57 +03006315weston_compositor_add_output(struct weston_compositor *compositor,
6316 struct weston_output *output)
6317{
Armin Krezoviće5403842016-08-05 15:28:29 +02006318 struct weston_view *view, *next;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03006319 struct weston_head *head;
Armin Krezoviće5403842016-08-05 15:28:29 +02006320
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006321 assert(!output->enabled);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03006322
6323 /* Verify we haven't reached the limit of 32 available output IDs */
6324 assert(ffs(~compositor->output_id_pool) > 0);
6325
6326 /* Invert the output id pool and look for the lowest numbered
6327 * switch (the least significant bit). Take that bit's position
6328 * as our ID, and mark it used in the compositor's output_id_pool.
6329 */
6330 output->id = ffs(~compositor->output_id_pool) - 1;
6331 compositor->output_id_pool |= 1u << output->id;
6332
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006333 wl_list_remove(&output->link);
Giulio Camuffob1147152015-05-06 21:41:57 +03006334 wl_list_insert(compositor->output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006335 output->enabled = true;
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006336
Pekka Paalanendcac3512017-12-08 14:13:34 +02006337 wl_list_for_each(head, &output->head_list, output_link)
6338 weston_head_add_global(head);
Pekka Paalanen2210ad02017-03-30 15:48:06 +03006339
Giulio Camuffob1147152015-05-06 21:41:57 +03006340 wl_signal_emit(&compositor->output_created_signal, output);
Armin Krezoviće5403842016-08-05 15:28:29 +02006341
Pekka Paalanen6551c092021-05-03 16:09:45 +03006342 /*
6343 * Use view_list, as paint nodes have not been created for this
6344 * output yet. Any existing view might touch this new output.
6345 */
Armin Krezoviće5403842016-08-05 15:28:29 +02006346 wl_list_for_each_safe(view, next, &compositor->view_list, link)
6347 weston_view_geometry_dirty(view);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04006348}
6349
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006350/** Transform device coordinates into global coordinates
6351 *
Marius Vlada2dace22019-06-12 16:05:44 +03006352 * \param output the weston_output object
6353 * \param[in] device_x X coordinate in device units.
6354 * \param[in] device_y Y coordinate in device units.
6355 * \param[out] x X coordinate in the global space.
6356 * \param[out] y Y coordinate in the global space.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006357 *
Marius Vlada2dace22019-06-12 16:05:44 +03006358 * Transforms coordinates from the device coordinate space (physical pixel
6359 * units) to the global coordinate space (logical pixel units). This takes
6360 * into account output transform and scale.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006361 *
Marius Vlad55d87362019-06-11 01:15:35 +03006362 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006363 * \internal
6364 */
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006365WL_EXPORT void
6366weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02006367 double device_x, double device_y,
6368 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006369{
Derek Foreman0f679412014-10-02 13:41:17 -05006370 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02006371 device_x,
6372 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05006373 0.0,
6374 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006375
Derek Foreman67a18b92015-03-24 11:36:14 -05006376 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006377
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02006378 *x = p.f[0] / p.f[3];
6379 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006380}
6381
Pekka Paalanen1d2eee22021-02-25 12:03:28 +02006382static void
6383weston_output_reset_color_transforms(struct weston_output *output)
6384{
Pekka Paalanencda39512021-03-22 17:32:07 +02006385 weston_color_transform_unref(output->from_sRGB_to_output);
6386 output->from_sRGB_to_output = NULL;
Pekka Paalanen8fb23ed2021-03-23 13:40:43 +02006387 weston_color_transform_unref(output->from_sRGB_to_blend);
6388 output->from_sRGB_to_blend = NULL;
Pekka Paalanen1d2eee22021-02-25 12:03:28 +02006389 weston_color_transform_unref(output->from_blend_to_output);
6390 output->from_blend_to_output = NULL;
6391}
6392
Pekka Paalanen9a0aa372021-06-08 13:38:35 +03006393static bool
6394weston_output_set_color_transforms(struct weston_output *output)
6395{
6396 struct weston_color_manager *cm = output->compositor->color_manager;
6397 struct weston_color_transform *blend_to_output = NULL;
6398 struct weston_color_transform *sRGB_to_output = NULL;
6399 struct weston_color_transform *sRGB_to_blend = NULL;
6400 bool ok;
6401
6402 ok = cm->get_output_color_transform(cm, output, &blend_to_output);
6403 ok = ok && cm->get_sRGB_to_output_color_transform(cm, output,
6404 &sRGB_to_output);
6405 ok = ok && cm->get_sRGB_to_blend_color_transform(cm, output,
6406 &sRGB_to_blend);
6407 if (!ok) {
6408 weston_log("Creating color transformation for output \"%s\" failed.\n",
6409 output->name);
6410 weston_color_transform_unref(blend_to_output);
6411 weston_color_transform_unref(sRGB_to_output);
6412 weston_color_transform_unref(sRGB_to_blend);
6413
6414 return false;
6415 }
6416
6417 weston_output_reset_color_transforms(output);
6418 output->from_blend_to_output = blend_to_output;
6419 output->from_blend_to_output_by_backend = false;
6420 output->from_sRGB_to_output = sRGB_to_output;
6421 output->from_sRGB_to_blend = sRGB_to_blend;
6422
Pekka Paalanen9a9e6ce2021-06-08 14:17:44 +03006423 weston_log("Output '%s' using color profile: %s\n", output->name,
6424 weston_color_profile_get_description(output->color_profile));
6425
Pekka Paalanen9a0aa372021-06-08 13:38:35 +03006426 return true;
6427}
6428
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006429/** Removes output from compositor's list of enabled outputs
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006430 *
6431 * \param output The weston_output object that is being removed.
6432 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006433 * The following happens:
6434 *
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03006435 * - Destroys all paint nodes related to the output.
6436 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006437 * - The output assignments of all views in the current scenegraph are
6438 * recomputed.
6439 *
Pekka Paalanen1d2eee22021-02-25 12:03:28 +02006440 * - Destroys output's color transforms.
6441 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006442 * - Presentation feedback is discarded.
6443 *
6444 * - Compositor is notified that outputs were changed and
6445 * applies the necessary changes to re-layout outputs.
6446 *
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006447 * - The output is put back in the pending outputs list.
6448 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006449 * - Signal is emitted to notify all users of the weston_output
6450 * object that the output is being destroyed.
6451 *
6452 * - wl_output protocol objects referencing this weston_output
Pekka Paalanen2210ad02017-03-30 15:48:06 +03006453 * are made inert, and the wl_output global is removed.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006454 *
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03006455 * - The output's internal ID is released.
6456 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006457 * \ingroup compositor
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006458 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006459 */
6460static void
6461weston_compositor_remove_output(struct weston_output *output)
6462{
Pekka Paalanenbccda712017-03-29 16:16:04 +03006463 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03006464 struct weston_paint_node *pnode, *pntmp;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006465 struct weston_view *view;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03006466 struct weston_head *head;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006467
6468 assert(output->destroying);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006469 assert(output->enabled);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006470
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03006471 wl_list_for_each_safe(pnode, pntmp,
6472 &output->paint_node_list, output_link) {
6473 weston_paint_node_destroy(pnode);
6474 }
Pekka Paalanen2fddc532021-04-30 17:41:29 +03006475 assert(wl_list_empty(&output->paint_node_z_order_list));
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03006476
Pekka Paalanen6551c092021-05-03 16:09:45 +03006477 /*
6478 * Use view_list in case the output did not go through repaint
6479 * after a view came on it, lacking a paint node. Just to be sure.
6480 */
Pekka Paalanenbccda712017-03-29 16:16:04 +03006481 wl_list_for_each(view, &compositor->view_list, link) {
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006482 if (view->output_mask & (1u << output->id))
6483 weston_view_assign_output(view);
6484 }
6485
Pekka Paalanen1d2eee22021-02-25 12:03:28 +02006486 weston_output_reset_color_transforms(output);
6487
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006488 weston_presentation_feedback_discard_list(&output->feedback_list);
6489
Pekka Paalanen9711fd92018-06-21 14:26:18 +03006490 weston_compositor_reflow_outputs(compositor, output, -output->width);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006491
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006492 wl_list_remove(&output->link);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006493 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006494 output->enabled = false;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006495
Marius Vlad2015dfc2021-12-02 11:15:15 +02006496 weston_signal_emit_mutable(&compositor->output_destroyed_signal, output);
6497 weston_signal_emit_mutable(&output->destroy_signal, output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006498
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02006499 wl_list_for_each(head, &output->head_list, output_link)
6500 weston_head_remove_global(head);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03006501
6502 compositor->output_id_pool &= ~(1u << output->id);
6503 output->id = 0xffffffff; /* invalid */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006504}
6505
6506/** Sets the output scale for a given output.
6507 *
6508 * \param output The weston_output object that the scale is set for.
6509 * \param scale Scale factor for the given output.
6510 *
6511 * It only supports setting scale for an output that
6512 * is not enabled and it can only be ran once.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006513 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05006514 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006515 */
6516WL_EXPORT void
6517weston_output_set_scale(struct weston_output *output,
6518 int32_t scale)
6519{
6520 /* We can only set scale on a disabled output */
6521 assert(!output->enabled);
6522
6523 /* We only want to set scale once */
6524 assert(!output->scale);
6525
6526 output->scale = scale;
6527}
6528
6529/** Sets the output transform for a given output.
6530 *
6531 * \param output The weston_output object that the transform is set for.
6532 * \param transform Transform value for the given output.
6533 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006534 * Refer to wl_output::transform section located at
6535 * https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_output
6536 * for list of values that can be passed to this function.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006537 *
Marius Vlad55d87362019-06-11 01:15:35 +03006538 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006539 */
6540WL_EXPORT void
6541weston_output_set_transform(struct weston_output *output,
6542 uint32_t transform)
6543{
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006544 struct weston_pointer_motion_event ev;
6545 struct wl_resource *resource;
6546 struct weston_seat *seat;
6547 pixman_region32_t old_region;
6548 int mid_x, mid_y;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006549 struct weston_head *head;
6550 int ver;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006551
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006552 if (!output->enabled && output->transform == UINT32_MAX) {
6553 output->transform = transform;
6554 return;
6555 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006556
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006557 weston_output_transform_scale_init(output, transform, output->scale);
6558
6559 pixman_region32_init(&old_region);
6560 pixman_region32_copy(&old_region, &output->region);
6561
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006562 weston_output_init_geometry(output, output->x, output->y);
6563
6564 output->dirty = 1;
6565
6566 /* Notify clients of the change for output transform. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006567 wl_list_for_each(head, &output->head_list, output_link) {
6568 wl_resource_for_each(resource, &head->resource_list) {
6569 wl_output_send_geometry(resource,
6570 output->x,
6571 output->y,
6572 head->mm_width,
6573 head->mm_height,
6574 head->subpixel,
6575 head->make,
6576 head->model,
6577 output->transform);
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006578
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006579 ver = wl_resource_get_version(resource);
6580 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
6581 wl_output_send_done(resource);
6582 }
Roman Gilge97391c2019-03-29 13:01:06 +01006583 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
6584 zxdg_output_v1_send_logical_position(resource,
6585 output->x,
6586 output->y);
6587 zxdg_output_v1_send_logical_size(resource,
6588 output->width,
6589 output->height);
6590 zxdg_output_v1_send_done(resource);
6591 }
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006592 }
6593
6594 /* we must ensure that pointers are inside output, otherwise they disappear */
6595 mid_x = output->x + output->width / 2;
6596 mid_y = output->y + output->height / 2;
6597
6598 ev.mask = WESTON_POINTER_MOTION_ABS;
6599 ev.x = wl_fixed_to_double(wl_fixed_from_int(mid_x));
6600 ev.y = wl_fixed_to_double(wl_fixed_from_int(mid_y));
6601
6602 wl_list_for_each(seat, &output->compositor->seat_list, link) {
6603 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
6604
6605 if (pointer && pixman_region32_contains_point(&old_region,
6606 wl_fixed_to_int(pointer->x),
6607 wl_fixed_to_int(pointer->y),
6608 NULL))
6609 weston_pointer_move(pointer, &ev);
6610 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006611}
6612
Pekka Paalanen9a9e6ce2021-06-08 14:17:44 +03006613/** Set output's color profile
6614 *
6615 * \param output The output to change.
6616 * \param cprof The color profile to set. Can be NULL for default sRGB profile.
6617 * \return True on success, or false on failure.
6618 *
6619 * Calling this function changes the color profile of the output. This causes
6620 * all existing weston_color_transform objects related to this output via
6621 * paint nodes to be unreferenced and later re-created on demand.
6622 *
6623 * This function may not be called from within weston_output_repaint().
6624 *
6625 * On failure, nothing is changed.
6626 *
6627 * \ingroup output
6628 */
6629WL_EXPORT bool
6630weston_output_set_color_profile(struct weston_output *output,
6631 struct weston_color_profile *cprof)
6632{
6633 struct weston_color_profile *old;
6634 struct weston_paint_node *pnode;
6635
6636 old = output->color_profile;
6637 output->color_profile = weston_color_profile_ref(cprof);
6638
6639 if (output->enabled) {
6640 if (!weston_output_set_color_transforms(output)) {
6641 /* Failed, roll back */
6642 weston_color_profile_unref(output->color_profile);
6643 output->color_profile = old;
6644 return false;
6645 }
6646
6647 /* Remove outdated cached color transformations */
6648 wl_list_for_each(pnode, &output->paint_node_list, output_link) {
6649 weston_surface_color_transform_fini(&pnode->surf_xform);
6650 pnode->surf_xform_valid = false;
6651 }
6652 }
6653
6654 weston_color_profile_unref(old);
6655
6656 return true;
6657}
6658
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006659/** Initializes a weston_output object with enough data so
6660 ** an output can be configured.
6661 *
6662 * \param output The weston_output object to initialize
6663 * \param compositor The compositor instance.
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006664 * \param name Name for the output (the string is copied).
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006665 *
6666 * Sets initial values for fields that are expected to be
6667 * configured either by compositors or backends.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006668 *
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006669 * The name is used in logs, and can be used by compositors as a configuration
6670 * identifier.
6671 *
Marius Vlad55d87362019-06-11 01:15:35 +03006672 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006673 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006674 */
6675WL_EXPORT void
Armin Krezović40087402016-09-30 14:11:12 +02006676weston_output_init(struct weston_output *output,
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006677 struct weston_compositor *compositor,
6678 const char *name)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006679{
6680 output->compositor = compositor;
6681 output->destroying = 0;
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006682 output->name = strdup(name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006683 wl_list_init(&output->link);
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006684 wl_signal_init(&output->user_destroy_signal);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006685 output->enabled = false;
Ankit Nautiyal2690a772019-03-25 17:57:59 +05306686 output->desired_protection = WESTON_HDCP_DISABLE;
Ankit Nautiyal2844f8e2019-04-03 10:14:59 +05306687 output->allow_protection = true;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006688
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006689 wl_list_init(&output->head_list);
6690
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006691 /* Add some (in)sane defaults which can be used
6692 * for checking if an output was properly configured
6693 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006694 output->scale = 0;
6695 /* Can't use -1 on uint32_t and 0 is valid enum value */
6696 output->transform = UINT32_MAX;
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006697
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006698 pixman_region32_init(&output->region);
Pekka Paalanen42704142017-09-06 16:47:52 +03006699 wl_list_init(&output->mode_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006700}
6701
6702/** Adds weston_output object to pending output list.
6703 *
6704 * \param output The weston_output object to add
6705 * \param compositor The compositor instance.
6706 *
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03006707 * The opposite of this operation is built into weston_output_release().
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006708 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006709 * \ingroup compositor
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006710 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006711 */
6712WL_EXPORT void
6713weston_compositor_add_pending_output(struct weston_output *output,
6714 struct weston_compositor *compositor)
6715{
Pekka Paalanene952a012017-03-29 17:14:00 +03006716 assert(output->disable);
6717 assert(output->enable);
6718
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006719 wl_list_remove(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006720 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006721}
6722
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006723/** Create a string with the attached heads' names.
6724 *
6725 * The string must be free()'d.
Marius Vlad55d87362019-06-11 01:15:35 +03006726 *
6727 * \ingroup output
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006728 */
6729static char *
6730weston_output_create_heads_string(struct weston_output *output)
6731{
6732 FILE *fp;
6733 char *str = NULL;
6734 size_t size = 0;
6735 struct weston_head *head;
6736 const char *sep = "";
6737
6738 fp = open_memstream(&str, &size);
6739 if (!fp)
6740 return NULL;
6741
6742 wl_list_for_each(head, &output->head_list, output_link) {
6743 fprintf(fp, "%s%s", sep, head->name);
6744 sep = ", ";
6745 }
6746 fclose(fp);
6747
6748 return str;
6749}
6750
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006751/** Constructs a weston_output object that can be used by the compositor.
6752 *
Pekka Paalanencc201e42017-03-30 15:11:25 +03006753 * \param output The weston_output object that needs to be enabled. Must not
Pekka Paalanenddce54d2017-08-23 16:00:21 +03006754 * be enabled already. Must have at least one head attached.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006755 *
6756 * Output coordinates are calculated and each new output is by default
6757 * assigned to the right of previous one.
6758 *
6759 * Sets up the transformation, zoom, and geometry of the output using
6760 * the properties that need to be configured by the compositor.
6761 *
6762 * Establishes a repaint timer for the output with the relevant display
6763 * object's event loop. See output_repaint_timer_handler().
6764 *
6765 * The output is assigned an ID. Weston can support up to 32 distinct
6766 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
6767 * is referred to and used to find the first available ID number, and
6768 * then this ID is marked as used in output_id_pool.
6769 *
6770 * The output is also assigned a Wayland global with the wl_output
6771 * external interface.
6772 *
6773 * Backend specific function is called to set up the output output.
6774 *
6775 * Output is added to the compositor's output list
6776 *
6777 * If the backend specific function fails, the weston_output object
6778 * is returned to a state it was before calling this function and
6779 * is added to the compositor's pending_output_list in case it needs
6780 * to be reconfigured or just so it can be destroyed at shutdown.
6781 *
6782 * 0 is returned on success, -1 on failure.
Marius Vlad55d87362019-06-11 01:15:35 +03006783 *
6784 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006785 */
6786WL_EXPORT int
6787weston_output_enable(struct weston_output *output)
6788{
Armin Krezović782f5df2016-09-30 14:11:11 +02006789 struct weston_compositor *c = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006790 struct weston_output *iterator;
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03006791 struct weston_head *head;
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006792 char *head_names;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006793 int x = 0, y = 0;
6794
Pekka Paalanencc201e42017-03-30 15:11:25 +03006795 if (output->enabled) {
6796 weston_log("Error: attempt to enable an enabled output '%s'\n",
6797 output->name);
6798 return -1;
6799 }
6800
Pekka Paalanenddce54d2017-08-23 16:00:21 +03006801 if (wl_list_empty(&output->head_list)) {
6802 weston_log("Error: cannot enable output '%s' without heads.\n",
6803 output->name);
6804 return -1;
6805 }
6806
Pekka Paalanen586e1ac2017-09-14 16:17:59 +03006807 if (wl_list_empty(&output->mode_list) || !output->current_mode) {
6808 weston_log("Error: no video mode for output '%s'.\n",
6809 output->name);
6810 return -1;
6811 }
6812
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03006813 wl_list_for_each(head, &output->head_list, output_link) {
6814 assert(head->make);
6815 assert(head->model);
6816 }
6817
Armin Krezović782f5df2016-09-30 14:11:11 +02006818 iterator = container_of(c->output_list.prev,
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006819 struct weston_output, link);
6820
Armin Krezović782f5df2016-09-30 14:11:11 +02006821 if (!wl_list_empty(&c->output_list))
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006822 x = iterator->x + iterator->width;
6823
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006824 /* Make sure the scale is set up */
6825 assert(output->scale);
6826
6827 /* Make sure we have a transform set */
6828 assert(output->transform != UINT32_MAX);
6829
Armin Krezović782f5df2016-09-30 14:11:11 +02006830 output->x = x;
6831 output->y = y;
6832 output->dirty = 1;
6833 output->original_scale = output->scale;
6834
Marius Vlad3a2f8292019-11-04 17:53:46 +02006835 wl_signal_init(&output->frame_signal);
6836 wl_signal_init(&output->destroy_signal);
6837
Armin Krezović782f5df2016-09-30 14:11:11 +02006838 weston_output_transform_scale_init(output, output->transform, output->scale);
6839 weston_output_init_zoom(output);
6840
6841 weston_output_init_geometry(output, x, y);
6842 weston_output_damage(output);
6843
Armin Krezović782f5df2016-09-30 14:11:11 +02006844 wl_list_init(&output->animation_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02006845 wl_list_init(&output->feedback_list);
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03006846 wl_list_init(&output->paint_node_list);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03006847 wl_list_init(&output->paint_node_z_order_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02006848
Pekka Paalanen9a0aa372021-06-08 13:38:35 +03006849 if (!weston_output_set_color_transforms(output))
Pekka Paalanen1d2eee22021-02-25 12:03:28 +02006850 return -1;
Pekka Paalanen1d2eee22021-02-25 12:03:28 +02006851
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006852 /* Enable the output (set up the crtc or create a
6853 * window representing the output, set up the
6854 * renderer, etc)
6855 */
6856 if (output->enable(output) < 0) {
6857 weston_log("Enabling output \"%s\" failed.\n", output->name);
Pekka Paalanen1d2eee22021-02-25 12:03:28 +02006858 weston_output_reset_color_transforms(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006859 return -1;
6860 }
6861
6862 weston_compositor_add_output(output->compositor, output);
6863
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006864 head_names = weston_output_create_heads_string(output);
6865 weston_log("Output '%s' enabled with head(s) %s\n",
6866 output->name, head_names);
6867 free(head_names);
6868
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006869 return 0;
6870}
6871
6872/** Converts a weston_output object to a pending output state, so it
6873 ** can be configured again or destroyed.
6874 *
6875 * \param output The weston_output object that needs to be disabled.
6876 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006877 * Calls a backend specific function to disable an output, in case
6878 * such function exists.
6879 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03006880 * The backend specific disable function may choose to postpone the disabling
6881 * by returning a negative value, in which case this function returns early.
6882 * In that case the backend will guarantee the output will be disabled soon
6883 * by the backend calling this function again. One must not attempt to re-enable
6884 * the output until that happens.
6885 *
6886 * Otherwise, if the output is being used by the compositor, it is removed
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006887 * from weston's output_list (see weston_compositor_remove_output())
6888 * and is returned to a state it was before weston_output_enable()
6889 * was ran (see weston_output_enable_undo()).
6890 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03006891 * See weston_output_init() for more information on the
6892 * state output is returned to.
Pekka Paalanencc201e42017-03-30 15:11:25 +03006893 *
6894 * If the output has never been enabled yet, this function can still be
6895 * called to ensure that the output is actually turned off rather than left
6896 * in the state it was discovered in.
Marius Vlad55d87362019-06-11 01:15:35 +03006897 *
6898 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006899 */
6900WL_EXPORT void
6901weston_output_disable(struct weston_output *output)
6902{
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006903 /* Should we rename this? */
6904 output->destroying = 1;
6905
Pekka Paalanenc65df642017-03-29 15:45:46 +03006906 /* Disable is called unconditionally also for not-enabled outputs,
6907 * because at compositor start-up, if there is an output that is
6908 * already on but the compositor wants to turn it off, we have to
6909 * forward the turn-off to the backend so it knows to do it.
6910 * The backend cannot initially turn off everything, because it
6911 * would cause unnecessary mode-sets for all outputs the compositor
6912 * wants to be on.
6913 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006914 if (output->disable(output) < 0)
6915 return;
6916
Pekka Paalanen2ec15eb2021-06-09 16:14:45 +03006917 if (output->enabled) {
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006918 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006919
Pekka Paalanen2ec15eb2021-06-09 16:14:45 +03006920 assert(wl_list_empty(&output->paint_node_list));
6921 }
6922
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006923 output->destroying = 0;
6924}
6925
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006926/** Forces a synchronous call to heads_changed hook
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006927 *
6928 * \param compositor The compositor instance
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006929 *
6930 * If there are new or changed heads, calls the heads_changed hook and
6931 * returns after the hook returns.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006932 *
6933 * \ingroup compositor
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006934 */
6935WL_EXPORT void
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006936weston_compositor_flush_heads_changed(struct weston_compositor *compositor)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006937{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03006938 if (compositor->heads_changed_source) {
6939 wl_event_source_remove(compositor->heads_changed_source);
6940 weston_compositor_call_heads_changed(compositor);
6941 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006942}
6943
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006944/** Add destroy callback for an output
6945 *
6946 * \param output The output to watch.
6947 * \param listener The listener to add. The \c notify member must be set.
6948 *
6949 * The listener callback will be called when user destroys an output. This
6950 * may be delayed by a backend in some cases. The main purpose of the
6951 * listener is to allow hooking up custom data to the output. The custom data
6952 * can be fetched via weston_output_get_destroy_listener() followed by
6953 * container_of().
6954 *
6955 * The \c data argument to the notify callback is the weston_output being
6956 * destroyed.
6957 *
6958 * @note This is for the final destruction of an output, not when it gets
6959 * disabled. If you want to keep track of enabled outputs, this is not it.
Marius Vlad55d87362019-06-11 01:15:35 +03006960 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05006961 * \ingroup output
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006962 */
6963WL_EXPORT void
6964weston_output_add_destroy_listener(struct weston_output *output,
6965 struct wl_listener *listener)
6966{
6967 wl_signal_add(&output->user_destroy_signal, listener);
6968}
6969
6970/** Look up destroy listener for an output
6971 *
6972 * \param output The output to query.
6973 * \param notify The notify function used used for the added destroy listener.
6974 * \return The listener, or NULL if not found.
6975 *
6976 * This looks up the previously added destroy listener struct based on the
6977 * notify function it has. The listener can be used to access user data
6978 * through \c container_of().
6979 *
6980 * \sa wl_signal_get() weston_output_add_destroy_listener()
Marius Vlad55d87362019-06-11 01:15:35 +03006981 * \ingroup output
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006982 */
6983WL_EXPORT struct wl_listener *
6984weston_output_get_destroy_listener(struct weston_output *output,
6985 wl_notify_func_t notify)
6986{
6987 return wl_signal_get(&output->user_destroy_signal, notify);
6988}
6989
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006990/** Uninitialize an output
6991 *
6992 * Removes the output from the list of enabled outputs if necessary, but
6993 * does not call the backend's output disable function. The output will no
6994 * longer be in the list of pending outputs either.
6995 *
6996 * All fields of weston_output become uninitialized, i.e. should not be used
6997 * anymore. The caller can free the memory after this.
6998 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05006999 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03007000 * \internal
7001 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007002WL_EXPORT void
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03007003weston_output_release(struct weston_output *output)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007004{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03007005 struct weston_head *head, *tmp;
7006
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007007 output->destroying = 1;
7008
Marius Vladf2452d62021-12-09 14:07:21 +02007009 weston_signal_emit_mutable(&output->user_destroy_signal, output);
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02007010
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03007011 if (output->idle_repaint_source)
7012 wl_event_source_remove(output->idle_repaint_source);
7013
Pekka Paalanen4b582c72017-03-30 16:04:58 +03007014 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007015 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007016
Pekka Paalanen9a9e6ce2021-06-08 14:17:44 +03007017 weston_color_profile_unref(output->color_profile);
7018
Pekka Paalanen4b582c72017-03-30 16:04:58 +03007019 pixman_region32_fini(&output->region);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03007020 wl_list_remove(&output->link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03007021
7022 wl_list_for_each_safe(head, tmp, &output->head_list, output_link)
7023 weston_head_detach(head);
7024
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007025 free(output->name);
7026}
7027
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02007028/** Find an output by its given name
7029 *
7030 * \param compositor The compositor to search in.
7031 * \param name The output name to search for.
7032 * \return An existing output with the given name, or NULL if not found.
7033 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007034 * \ingroup compositor
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02007035 */
7036WL_EXPORT struct weston_output *
7037weston_compositor_find_output_by_name(struct weston_compositor *compositor,
7038 const char *name)
7039{
7040 struct weston_output *output;
7041
7042 wl_list_for_each(output, &compositor->output_list, link)
7043 if (strcmp(output->name, name) == 0)
7044 return output;
7045
7046 wl_list_for_each(output, &compositor->pending_output_list, link)
7047 if (strcmp(output->name, name) == 0)
7048 return output;
7049
7050 return NULL;
7051}
7052
7053/** Create a named output
7054 *
7055 * \param compositor The compositor.
7056 * \param name The name for the output.
7057 * \return A new \c weston_output, or NULL on failure.
7058 *
7059 * This creates a new weston_output that starts with no heads attached.
7060 *
7061 * An output must be configured and it must have at least one head before
7062 * it can be enabled.
7063 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007064 * \ingroup compositor
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02007065 */
7066WL_EXPORT struct weston_output *
7067weston_compositor_create_output(struct weston_compositor *compositor,
7068 const char *name)
7069{
7070 assert(compositor->backend->create_output);
7071
7072 if (weston_compositor_find_output_by_name(compositor, name)) {
7073 weston_log("Warning: attempted to create an output with a "
7074 "duplicate name '%s'.\n", name);
7075 return NULL;
7076 }
7077
7078 return compositor->backend->create_output(compositor, name);
7079}
7080
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03007081/** Create an output for an unused head
7082 *
7083 * \param compositor The compositor.
7084 * \param head The head to attach to the output.
7085 * \return A new \c weston_output, or NULL on failure.
7086 *
7087 * This creates a new weston_output that starts with the given head attached.
7088 * The output inherits the name of the head. The head must not be already
7089 * attached to another output.
7090 *
7091 * An output must be configured before it can be enabled.
7092 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007093 * \ingroup compositor
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03007094 */
7095WL_EXPORT struct weston_output *
7096weston_compositor_create_output_with_head(struct weston_compositor *compositor,
7097 struct weston_head *head)
7098{
7099 struct weston_output *output;
7100
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02007101 output = weston_compositor_create_output(compositor, head->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03007102 if (!output)
7103 return NULL;
7104
7105 if (weston_output_attach_head(output, head) < 0) {
Pekka Paalanen42c0e142017-10-27 12:07:49 +03007106 weston_output_destroy(output);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03007107 return NULL;
7108 }
7109
7110 return output;
7111}
7112
7113/** Destroy an output
7114 *
7115 * \param output The output to destroy.
7116 *
7117 * The heads attached to the given output are detached and become unused again.
7118 *
7119 * It is not necessary to explicitly destroy all outputs at compositor exit.
Leandro Ribeiroca640d52020-01-27 19:12:01 -03007120 * weston_compositor_destroy() will automatically destroy any remaining
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03007121 * outputs.
7122 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05007123 * \ingroup output
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03007124 */
7125WL_EXPORT void
7126weston_output_destroy(struct weston_output *output)
7127{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03007128 output->destroy(output);
7129}
7130
Pekka Paalanencf0a4762017-04-04 16:36:07 +03007131/** When you need a head...
7132 *
7133 * This function is a hack, used until all code has been converted to become
7134 * multi-head aware.
7135 *
7136 * \param output The weston_output whose head to get.
7137 * \return The first head in the output's list.
Marius Vlad55d87362019-06-11 01:15:35 +03007138 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05007139 * \ingroup output
Pekka Paalanencf0a4762017-04-04 16:36:07 +03007140 */
7141WL_EXPORT struct weston_head *
7142weston_output_get_first_head(struct weston_output *output)
7143{
7144 if (wl_list_empty(&output->head_list))
7145 return NULL;
7146
7147 return container_of(output->head_list.next,
7148 struct weston_head, output_link);
7149}
7150
Ankit Nautiyal2844f8e2019-04-03 10:14:59 +05307151/** Allow/Disallow content-protection support for an output
7152 *
7153 * This function sets the allow_protection member for an output. Setting of
7154 * this field will allow the compositor to attempt content-protection for this
7155 * output, for a backend that supports the content-protection protocol.
7156 *
7157 * \param output The weston_output for whom the content-protection is to be
7158 * allowed.
7159 * \param allow_protection The bool value which is to be set.
7160 */
7161WL_EXPORT void
7162weston_output_allow_protection(struct weston_output *output,
7163 bool allow_protection)
7164{
7165 output->allow_protection = allow_protection;
7166}
7167
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01007168static void
Roman Gilge97391c2019-03-29 13:01:06 +01007169xdg_output_unlist(struct wl_resource *resource)
7170{
7171 wl_list_remove(wl_resource_get_link(resource));
7172}
7173
7174static void
7175xdg_output_destroy(struct wl_client *client, struct wl_resource *resource)
7176{
7177 wl_resource_destroy(resource);
7178}
7179
7180static const struct zxdg_output_v1_interface xdg_output_interface = {
7181 xdg_output_destroy
7182};
7183
7184static void
7185xdg_output_manager_destroy(struct wl_client *client,
7186 struct wl_resource *resource)
7187{
7188 wl_resource_destroy(resource);
7189}
7190
7191static void
7192xdg_output_manager_get_xdg_output(struct wl_client *client,
7193 struct wl_resource *manager,
7194 uint32_t id,
7195 struct wl_resource *output_resource)
7196{
7197 int version = wl_resource_get_version(manager);
7198 struct weston_head *head = wl_resource_get_user_data(output_resource);
7199 struct weston_output *output = head->output;
7200 struct wl_resource *resource;
7201
7202 resource = wl_resource_create(client, &zxdg_output_v1_interface,
7203 version, id);
7204 if (resource == NULL) {
7205 wl_client_post_no_memory(client);
7206 return;
7207 }
7208
7209 wl_list_insert(&head->xdg_output_resource_list,
7210 wl_resource_get_link(resource));
7211
7212 wl_resource_set_implementation(resource, &xdg_output_interface,
7213 NULL, xdg_output_unlist);
7214
7215 zxdg_output_v1_send_logical_position(resource, output->x, output->y);
7216 zxdg_output_v1_send_logical_size(resource,
7217 output->width,
7218 output->height);
7219 if (version >= ZXDG_OUTPUT_V1_NAME_SINCE_VERSION)
7220 zxdg_output_v1_send_name(resource, head->name);
7221
7222 zxdg_output_v1_send_done(resource);
7223}
7224
7225static const struct zxdg_output_manager_v1_interface xdg_output_manager_interface = {
7226 xdg_output_manager_destroy,
7227 xdg_output_manager_get_xdg_output
7228};
7229
7230static void
7231bind_xdg_output_manager(struct wl_client *client,
7232 void *data, uint32_t version, uint32_t id)
7233{
7234 struct wl_resource *resource;
7235
7236 resource = wl_resource_create(client, &zxdg_output_manager_v1_interface,
7237 version, id);
7238 if (resource == NULL) {
7239 wl_client_post_no_memory(client);
7240 return;
7241 }
7242
7243 wl_resource_set_implementation(resource, &xdg_output_manager_interface,
7244 NULL, NULL);
7245}
7246
7247static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007248destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01007249{
Jonny Lamb74130762013-11-26 18:19:46 +01007250 struct weston_surface *surface =
7251 wl_resource_get_user_data(resource);
7252
Pekka Paalanen4826f872016-04-22 14:14:38 +03007253 if (!surface)
7254 return;
7255
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007256 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02007257 surface->pending.buffer_viewport.buffer.src_width =
7258 wl_fixed_from_int(-1);
7259 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02007260 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01007261}
7262
7263static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007264viewport_destroy(struct wl_client *client,
7265 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01007266{
7267 wl_resource_destroy(resource);
7268}
7269
7270static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007271viewport_set_source(struct wl_client *client,
7272 struct wl_resource *resource,
7273 wl_fixed_t src_x,
7274 wl_fixed_t src_y,
7275 wl_fixed_t src_width,
7276 wl_fixed_t src_height)
7277{
7278 struct weston_surface *surface =
7279 wl_resource_get_user_data(resource);
7280
Pekka Paalanen4826f872016-04-22 14:14:38 +03007281 if (!surface) {
7282 wl_resource_post_error(resource,
7283 WP_VIEWPORT_ERROR_NO_SURFACE,
7284 "wl_surface for this viewport is no longer exists");
7285 return;
7286 }
7287
7288 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03007289 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007290
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007291 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03007292 src_height == wl_fixed_from_int(-1) &&
7293 src_x == wl_fixed_from_int(-1) &&
7294 src_y == wl_fixed_from_int(-1)) {
7295 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007296 surface->pending.buffer_viewport.buffer.src_width =
7297 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02007298 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007299 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007300 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007301
Pekka Paalanen201769a2016-04-26 14:42:11 +03007302 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007303 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007304 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03007305 "wl_surface@%d viewport source "
7306 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
7307 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007308 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03007309 wl_fixed_to_double(src_height),
7310 wl_fixed_to_double(src_x),
7311 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007312 return;
7313 }
7314
7315 surface->pending.buffer_viewport.buffer.src_x = src_x;
7316 surface->pending.buffer_viewport.buffer.src_y = src_y;
7317 surface->pending.buffer_viewport.buffer.src_width = src_width;
7318 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02007319 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007320}
7321
7322static void
7323viewport_set_destination(struct wl_client *client,
7324 struct wl_resource *resource,
7325 int32_t dst_width,
7326 int32_t dst_height)
7327{
7328 struct weston_surface *surface =
7329 wl_resource_get_user_data(resource);
7330
Pekka Paalanen4826f872016-04-22 14:14:38 +03007331 if (!surface) {
7332 wl_resource_post_error(resource,
7333 WP_VIEWPORT_ERROR_NO_SURFACE,
7334 "wl_surface for this viewport no longer exists");
7335 return;
7336 }
7337
7338 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007339
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007340 if (dst_width == -1 && dst_height == -1) {
7341 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007342 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02007343 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007344 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007345 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007346
7347 if (dst_width <= 0 || dst_height <= 0) {
7348 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007349 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007350 "destination size must be positive (%dx%d)",
7351 dst_width, dst_height);
7352 return;
7353 }
7354
7355 surface->pending.buffer_viewport.surface.width = dst_width;
7356 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02007357 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007358}
7359
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007360static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007361 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007362 viewport_set_source,
7363 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01007364};
7365
7366static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007367viewporter_destroy(struct wl_client *client,
7368 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01007369{
7370 wl_resource_destroy(resource);
7371}
7372
7373static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007374viewporter_get_viewport(struct wl_client *client,
7375 struct wl_resource *viewporter,
7376 uint32_t id,
7377 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01007378{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007379 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007380 struct weston_surface *surface =
7381 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01007382 struct wl_resource *resource;
7383
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007384 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007385 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007386 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007387 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01007388 return;
7389 }
7390
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007391 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007392 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01007393 if (resource == NULL) {
7394 wl_client_post_no_memory(client);
7395 return;
7396 }
7397
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007398 wl_resource_set_implementation(resource, &viewport_interface,
7399 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01007400
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007401 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01007402}
7403
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007404static const struct wp_viewporter_interface viewporter_interface = {
7405 viewporter_destroy,
7406 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01007407};
7408
7409static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007410bind_viewporter(struct wl_client *client,
7411 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01007412{
7413 struct wl_resource *resource;
7414
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007415 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06007416 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01007417 if (resource == NULL) {
7418 wl_client_post_no_memory(client);
7419 return;
7420 }
7421
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007422 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01007423 NULL, NULL);
7424}
7425
7426static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04007427destroy_presentation_feedback(struct wl_resource *feedback_resource)
7428{
7429 struct weston_presentation_feedback *feedback;
7430
7431 feedback = wl_resource_get_user_data(feedback_resource);
7432
7433 wl_list_remove(&feedback->link);
7434 free(feedback);
7435}
7436
7437static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007438presentation_destroy(struct wl_client *client, struct wl_resource *resource)
7439{
7440 wl_resource_destroy(resource);
7441}
7442
7443static void
7444presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04007445 struct wl_resource *presentation_resource,
7446 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007447 uint32_t callback)
7448{
Pekka Paalanen133e4392014-09-23 22:08:46 -04007449 struct weston_surface *surface;
7450 struct weston_presentation_feedback *feedback;
7451
7452 surface = wl_resource_get_user_data(surface_resource);
7453
Bryce Harringtonde16d892014-11-20 22:21:57 -08007454 feedback = zalloc(sizeof *feedback);
7455 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04007456 goto err_calloc;
7457
7458 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007459 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04007460 1, callback);
7461 if (!feedback->resource)
7462 goto err_create;
7463
7464 wl_resource_set_implementation(feedback->resource, NULL, feedback,
7465 destroy_presentation_feedback);
7466
7467 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
7468
7469 return;
7470
7471err_create:
7472 free(feedback);
7473
7474err_calloc:
7475 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007476}
7477
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007478static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007479 presentation_destroy,
7480 presentation_feedback
7481};
7482
7483static void
7484bind_presentation(struct wl_client *client,
7485 void *data, uint32_t version, uint32_t id)
7486{
7487 struct weston_compositor *compositor = data;
7488 struct wl_resource *resource;
7489
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007490 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06007491 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007492 if (resource == NULL) {
7493 wl_client_post_no_memory(client);
7494 return;
7495 }
7496
7497 wl_resource_set_implementation(resource, &presentation_implementation,
7498 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007499 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007500}
7501
7502static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05007503compositor_bind(struct wl_client *client,
7504 void *data, uint32_t version, uint32_t id)
7505{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007506 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05007507 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05007508
Jason Ekstranda85118c2013-06-27 20:17:02 -05007509 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06007510 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07007511 if (resource == NULL) {
7512 wl_client_post_no_memory(client);
7513 return;
7514 }
7515
7516 wl_resource_set_implementation(resource, &compositor_interface,
7517 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05007518}
7519
Daniel Stonece62cb32018-07-20 09:46:24 +01007520static const char *
7521output_repaint_status_text(struct weston_output *output)
7522{
7523 switch (output->repaint_status) {
7524 case REPAINT_NOT_SCHEDULED:
7525 return "no repaint";
7526 case REPAINT_BEGIN_FROM_IDLE:
7527 return "start_repaint_loop scheduled";
7528 case REPAINT_SCHEDULED:
7529 return "repaint scheduled";
7530 case REPAINT_AWAITING_COMPLETION:
7531 return "awaiting completion";
7532 }
7533
7534 assert(!"output_repaint_status_text missing enum");
7535 return NULL;
7536}
7537
7538static void
7539debug_scene_view_print_buffer(FILE *fp, struct weston_view *view)
7540{
7541 struct weston_buffer *buffer = view->surface->buffer_ref.buffer;
7542 struct wl_shm_buffer *shm;
7543 struct linux_dmabuf_buffer *dmabuf;
Marius Vlad00a6e012018-11-20 17:52:31 +02007544 const struct pixel_format_info *pixel_info = NULL;
Daniel Stonece62cb32018-07-20 09:46:24 +01007545
7546 if (!buffer) {
7547 fprintf(fp, "\t\t[buffer not available]\n");
7548 return;
7549 }
7550
7551 shm = wl_shm_buffer_get(buffer->resource);
7552 if (shm) {
Marius Vlad00a6e012018-11-20 17:52:31 +02007553 uint32_t _format = wl_shm_buffer_get_format(shm);
7554 pixel_info = pixel_format_get_info_shm(_format);
Daniel Stonece62cb32018-07-20 09:46:24 +01007555 fprintf(fp, "\t\tSHM buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02007556 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
7557 (unsigned long) _format,
7558 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
leng.fang32af9fc2024-06-13 11:22:15 +08007559#ifdef ENABLE_DRM_HELP
7560 debug_write_shm_buffer_to_file(view, shm);
7561#endif
Daniel Stonece62cb32018-07-20 09:46:24 +01007562 return;
7563 }
7564
7565 dmabuf = linux_dmabuf_buffer_get(buffer->resource);
7566 if (dmabuf) {
Marius Vlad6f6fd262021-07-12 12:58:34 +03007567 uint64_t modifier = dmabuf->attributes.modifier[0];
7568 char *modifier_name = pixel_format_get_modifier(modifier);
Marius Vlad00a6e012018-11-20 17:52:31 +02007569 pixel_info = pixel_format_get_info(dmabuf->attributes.format);
Daniel Stonece62cb32018-07-20 09:46:24 +01007570 fprintf(fp, "\t\tdmabuf buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02007571 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
7572 (unsigned long) dmabuf->attributes.format,
7573 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Marius Vlad6f6fd262021-07-12 12:58:34 +03007574
7575 fprintf(fp, "\t\t\tmodifier: %s\n", modifier_name ? modifier_name :
7576 "Failed to convert to a modifier name");
7577 free(modifier_name);
leng.fang32af9fc2024-06-13 11:22:15 +08007578#ifdef ENABLE_DRM_HELP
7579 debug_write_dma_buffer_to_file(view, dmabuf);
7580#endif
Daniel Stonece62cb32018-07-20 09:46:24 +01007581 return;
7582 }
7583
Marius Vlad253ba9a2019-03-17 18:22:21 +02007584 fprintf(fp, "\t\tEGL buffer\n");
Daniel Stonece62cb32018-07-20 09:46:24 +01007585}
7586
7587static void
7588debug_scene_view_print(FILE *fp, struct weston_view *view, int view_idx)
7589{
7590 struct weston_compositor *ec = view->surface->compositor;
7591 struct weston_output *output;
7592 char desc[512];
7593 pixman_box32_t *box;
leng.fang32af9fc2024-06-13 11:22:15 +08007594 pixman_box32_t *opaque_surface_box;
Daniel Stonece62cb32018-07-20 09:46:24 +01007595 uint32_t surface_id = 0;
7596 pid_t pid = 0;
7597
7598 if (view->surface->resource) {
7599 struct wl_resource *resource = view->surface->resource;
7600 wl_client_get_credentials(wl_resource_get_client(resource),
Dongjin Kimba89f002024-09-30 17:49:36 +09007601 &pid, NULL, NULL);
Daniel Stonece62cb32018-07-20 09:46:24 +01007602 surface_id = wl_resource_get_id(view->surface->resource);
7603 }
7604
7605 if (!view->surface->get_label ||
7606 view->surface->get_label(view->surface, desc, sizeof(desc)) < 0) {
7607 strcpy(desc, "[no description available]");
7608 }
leng.fang32af9fc2024-06-13 11:22:15 +08007609 fprintf(fp, "\tView %d (role %s, PID %d, surface ID %u, %s, %p %p video surface:%d):\n",
Daniel Stonece62cb32018-07-20 09:46:24 +01007610 view_idx, view->surface->role_name, pid, surface_id,
leng.fang32af9fc2024-06-13 11:22:15 +08007611 desc, view, view->surface, view->surface->is_video_surface);
Daniel Stonece62cb32018-07-20 09:46:24 +01007612
7613 box = pixman_region32_extents(&view->transform.boundingbox);
7614 fprintf(fp, "\t\tposition: (%d, %d) -> (%d, %d)\n",
7615 box->x1, box->y1, box->x2, box->y2);
7616 box = pixman_region32_extents(&view->transform.opaque);
7617
Michael Olbrichb7e5f102020-08-11 16:33:35 +02007618 if (weston_view_is_opaque(view, &view->transform.boundingbox)) {
Daniel Stonece62cb32018-07-20 09:46:24 +01007619 fprintf(fp, "\t\t[fully opaque]\n");
7620 } else if (!pixman_region32_not_empty(&view->transform.opaque)) {
7621 fprintf(fp, "\t\t[not opaque]\n");
7622 } else {
7623 fprintf(fp, "\t\t[opaque: (%d, %d) -> (%d, %d)]\n",
7624 box->x1, box->y1, box->x2, box->y2);
7625 }
leng.fang32af9fc2024-06-13 11:22:15 +08007626 opaque_surface_box = pixman_region32_extents(&view->surface->opaque);
7627 fprintf(fp, "\t\t[surface:%p opaque: (%d, %d) -> (%d, %d)]\n",
7628 view->surface, opaque_surface_box->x1, opaque_surface_box->y1, opaque_surface_box->x2, opaque_surface_box->y2);
Daniel Stonece62cb32018-07-20 09:46:24 +01007629
7630 if (view->alpha < 1.0)
7631 fprintf(fp, "\t\talpha: %f\n", view->alpha);
7632
7633 if (view->output_mask != 0) {
7634 bool first_output = true;
7635 fprintf(fp, "\t\toutputs: ");
7636 wl_list_for_each(output, &ec->output_list, link) {
7637 if (!(view->output_mask & (1 << output->id)))
7638 continue;
7639 fprintf(fp, "%s%d (%s)%s",
7640 (first_output) ? "" : ", ",
7641 output->id, output->name,
7642 (view->output == output) ? " (primary)" : "");
7643 first_output = false;
7644 }
7645 } else {
7646 fprintf(fp, "\t\t[no outputs]");
7647 }
7648
7649 fprintf(fp, "\n");
7650
7651 debug_scene_view_print_buffer(fp, view);
7652}
7653
Marius Vlad433f4e72019-02-17 22:14:23 +02007654static void
7655debug_scene_view_print_tree(struct weston_view *view,
Marius Vlada6acfa82019-03-17 18:10:09 +02007656 FILE *fp, int *view_idx)
Marius Vlad433f4e72019-02-17 22:14:23 +02007657{
7658 struct weston_subsurface *sub;
7659 struct weston_view *ev;
7660
7661 /*
7662 * print the view first, then we recursively go on printing
7663 * sub-surfaces. We bail out once no more sub-surfaces are available.
7664 */
Marius Vlada6acfa82019-03-17 18:10:09 +02007665 debug_scene_view_print(fp, view, *view_idx);
Marius Vlad433f4e72019-02-17 22:14:23 +02007666
7667 /* no more sub-surfaces */
7668 if (wl_list_empty(&view->surface->subsurface_list))
7669 return;
7670
7671 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
7672 wl_list_for_each(ev, &sub->surface->views, surface_link) {
Michael Olbrichef5f3232020-04-29 09:03:15 +02007673 /* only print the child views of the current view */
7674 if (ev->parent_view != view)
Marius Vlad433f4e72019-02-17 22:14:23 +02007675 continue;
Marius Vlada6acfa82019-03-17 18:10:09 +02007676
7677 (*view_idx)++;
Marius Vlad433f4e72019-02-17 22:14:23 +02007678 debug_scene_view_print_tree(ev, fp, view_idx);
7679 }
7680 }
7681}
7682
Daniel Stonece62cb32018-07-20 09:46:24 +01007683/**
7684 * Output information on how libweston is currently composing the scene
7685 * graph.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007686 *
7687 * \ingroup compositor
Daniel Stonece62cb32018-07-20 09:46:24 +01007688 */
7689WL_EXPORT char *
7690weston_compositor_print_scene_graph(struct weston_compositor *ec)
7691{
7692 struct weston_output *output;
7693 struct weston_layer *layer;
7694 struct timespec now;
7695 int layer_idx = 0;
7696 FILE *fp;
7697 char *ret;
7698 size_t len;
7699 int err;
7700
7701 fp = open_memstream(&ret, &len);
7702 assert(fp);
7703
7704 weston_compositor_read_presentation_clock(ec, &now);
7705 fprintf(fp, "Weston scene graph at %ld.%09ld:\n\n",
7706 now.tv_sec, now.tv_nsec);
7707
7708 wl_list_for_each(output, &ec->output_list, link) {
7709 struct weston_head *head;
7710 int head_idx = 0;
7711
7712 fprintf(fp, "Output %d (%s):\n", output->id, output->name);
7713 assert(output->enabled);
7714
7715 fprintf(fp, "\tposition: (%d, %d) -> (%d, %d)\n",
7716 output->x, output->y,
7717 output->x + output->width,
7718 output->y + output->height);
7719 fprintf(fp, "\tmode: %dx%d@%.3fHz\n",
7720 output->current_mode->width,
7721 output->current_mode->height,
7722 output->current_mode->refresh / 1000.0);
7723 fprintf(fp, "\tscale: %d\n", output->scale);
7724
7725 fprintf(fp, "\trepaint status: %s\n",
7726 output_repaint_status_text(output));
7727 if (output->repaint_status == REPAINT_SCHEDULED)
7728 fprintf(fp, "\tnext repaint: %ld.%09ld\n",
7729 output->next_repaint.tv_sec,
7730 output->next_repaint.tv_nsec);
7731
7732 wl_list_for_each(head, &output->head_list, output_link) {
7733 fprintf(fp, "\tHead %d (%s): %sconnected\n",
7734 head_idx++, head->name,
7735 (head->connected) ? "" : "not ");
7736 }
7737 }
7738
7739 fprintf(fp, "\n");
7740
7741 wl_list_for_each(layer, &ec->layer_list, link) {
7742 struct weston_view *view;
7743 int view_idx = 0;
7744
7745 fprintf(fp, "Layer %d (pos 0x%lx):\n", layer_idx++,
7746 (unsigned long) layer->position);
7747
7748 if (!weston_layer_mask_is_infinite(layer)) {
7749 fprintf(fp, "\t[mask: (%d, %d) -> (%d,%d)]\n\n",
7750 layer->mask.x1, layer->mask.y1,
7751 layer->mask.x2, layer->mask.y2);
7752 }
7753
Marius Vlada6acfa82019-03-17 18:10:09 +02007754 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
7755 debug_scene_view_print_tree(view, fp, &view_idx);
7756 view_idx++;
7757 }
Daniel Stonece62cb32018-07-20 09:46:24 +01007758
7759 if (wl_list_empty(&layer->view_list.link))
7760 fprintf(fp, "\t[no views]\n");
7761
7762 fprintf(fp, "\n");
7763 }
7764
7765 err = fclose(fp);
7766 assert(err == 0);
7767
7768 return ret;
7769}
7770
7771/**
7772 * Called when the 'scene-graph' debug scope is bound by a client. This
7773 * one-shot weston-debug scope prints the current scene graph when bound,
7774 * and then terminates the stream.
7775 */
7776static void
Marius Vladdad882a2019-07-17 15:43:53 +03007777debug_scene_graph_cb(struct weston_log_subscription *sub, void *data)
Daniel Stonece62cb32018-07-20 09:46:24 +01007778{
7779 struct weston_compositor *ec = data;
7780 char *str = weston_compositor_print_scene_graph(ec);
7781
Marius Vladdad882a2019-07-17 15:43:53 +03007782 weston_log_subscription_printf(sub, "%s", str);
Daniel Stonece62cb32018-07-20 09:46:24 +01007783 free(str);
Marius Vladdad882a2019-07-17 15:43:53 +03007784 weston_log_subscription_complete(sub);
Daniel Stonece62cb32018-07-20 09:46:24 +01007785}
7786
Leandro Ribeiro32a5acd2020-10-19 16:06:22 -03007787/** Retrieve testsuite data from compositor
7788 *
7789 * The testsuite data can be defined by the test suite of projects that uses
7790 * libweston and given to the compositor at the moment of its creation. This
7791 * function should be used when we need to retrieve the testsuite private data
7792 * from the compositor.
7793 *
7794 * \param ec The weston compositor.
7795 * \return The testsuite data.
7796 *
7797 * \ingroup compositor
7798 * \sa weston_compositor_test_data_init
7799 */
7800WL_EXPORT void *
7801weston_compositor_get_test_data(struct weston_compositor *ec)
7802{
7803 return ec->test_data.test_private_data;
7804}
7805
Giulio Camuffo459137b2014-10-11 23:56:24 +03007806/** Create the compositor.
7807 *
7808 * This functions creates and initializes a compositor instance.
7809 *
7810 * \param display The Wayland display to be used.
7811 * \param user_data A pointer to an object that can later be retrieved
Marius Vlada2dace22019-06-12 16:05:44 +03007812 * \param log_ctx A pointer to weston_debug_compositor
Leandro Ribeirocdb72182020-11-06 11:24:22 -03007813 * \param test_data Optional testsuite data, or NULL.
Giulio Camuffo459137b2014-10-11 23:56:24 +03007814 * using the \ref weston_compositor_get_user_data function.
7815 * \return The compositor instance on success or NULL on failure.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007816 *
7817 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007818 */
7819WL_EXPORT struct weston_compositor *
Marius Vlad880b4852019-04-07 17:07:58 +03007820weston_compositor_create(struct wl_display *display,
Leandro Ribeirocdb72182020-11-06 11:24:22 -03007821 struct weston_log_context *log_ctx, void *user_data,
7822 const struct weston_testsuite_data *test_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04007823{
Giulio Camuffo459137b2014-10-11 23:56:24 +03007824 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05007825 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07007826
Leandro Ribeirobd9c0a62020-01-17 10:47:49 -03007827 if (!log_ctx)
7828 return NULL;
7829
Giulio Camuffo459137b2014-10-11 23:56:24 +03007830 ec = zalloc(sizeof *ec);
7831 if (!ec)
7832 return NULL;
7833
Leandro Ribeirocdb72182020-11-06 11:24:22 -03007834 if (test_data)
7835 ec->test_data = *test_data;
7836
Leandro Ribeirobd9c0a62020-01-17 10:47:49 -03007837 ec->weston_log_ctx = log_ctx;
Giulio Camuffo459137b2014-10-11 23:56:24 +03007838 ec->wl_display = display;
7839 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04007840 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07007841 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04007842 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03007843 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03007844 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02007845 wl_signal_init(&ec->idle_signal);
7846 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02007847 wl_signal_init(&ec->show_input_panel_signal);
7848 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02007849 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01007850 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01007851 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02007852 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02007853 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02007854 wl_signal_init(&ec->output_resized_signal);
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03007855 wl_signal_init(&ec->heads_changed_signal);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02007856 wl_signal_init(&ec->output_heads_changed_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07007857 wl_signal_init(&ec->session_signal);
Robert Beckettc569bdc2019-07-10 16:40:12 +01007858 ec->session_active = true;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04007859
Casey Dahlin58ba1372012-04-19 22:50:08 -04007860 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03007861 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04007862
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02007863 ec->activate_serial = 1;
7864
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05007865 ec->touch_mode = WESTON_TOUCH_MODE_NORMAL;
7866
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05307867 ec->content_protection = NULL;
7868
Derek Foreman152254b2015-11-26 14:17:48 -06007869 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04007870 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007871 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05007872
Giulio Camuffo954f1832014-10-11 18:27:30 +03007873 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04007874 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007875 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03007876
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007877 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007878 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007879 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01007880
Roman Gilge97391c2019-03-29 13:01:06 +01007881 if (!wl_global_create(ec->wl_display, &zxdg_output_manager_v1_interface, 2,
7882 ec, bind_xdg_output_manager))
7883 goto fail;
7884
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007885 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007886 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007887 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007888
Jonas Ådahl30d61d82014-10-22 21:21:17 +02007889 if (weston_input_init(ec) != 0)
7890 goto fail;
7891
leng.fang32af9fc2024-06-13 11:22:15 +08007892 if (aml_config_setup(ec) != 0 ) {
7893 weston_log("\n aml config fail\n");
7894 }
7895
limin.tianff789062024-08-14 02:24:40 +00007896 if (aml_drm_config_setup(ec) != 0 ) {
7897 weston_log("\n aml drm config fail\n");
7898 }
7899
Jason Ekstranda7af7042013-10-12 22:38:11 -05007900 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02007901 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007902 wl_list_init(&ec->layer_list);
7903 wl_list_init(&ec->seat_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007904 wl_list_init(&ec->pending_output_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007905 wl_list_init(&ec->output_list);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03007906 wl_list_init(&ec->head_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007907 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01007908 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007909 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01007910 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007911 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02007912 wl_list_init(&ec->debug_binding_list);
leng.fang32af9fc2024-06-13 11:22:15 +08007913 wl_list_init(&ec->video_surface_list);
leng.fangdbaf6fa2024-06-20 19:31:04 +08007914 wl_list_init(&ec->surface_id_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007915
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007916 wl_list_init(&ec->plugin_api_list);
7917
Xiong Zhang97116532013-10-23 13:58:31 +08007918 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02007919 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04007920
Giulio Camuffo459137b2014-10-11 23:56:24 +03007921 wl_data_device_manager_init(ec->wl_display);
7922
7923 wl_display_init_shm(ec->wl_display);
7924
7925 loop = wl_display_get_event_loop(ec->wl_display);
7926 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Daniel Stone6847b852017-03-01 11:34:08 +00007927 ec->repaint_timer =
leng.fang32af9fc2024-06-13 11:22:15 +08007928 wl_event_loop_add_timer(loop, output_repaint_timer_handler_aml,
Daniel Stone6847b852017-03-01 11:34:08 +00007929 ec);
leng.fang32af9fc2024-06-13 11:22:15 +08007930 wl_event_source_timer_update(ec->repaint_timer, 16);
leng.fangbd0b8832024-08-09 18:36:50 +08007931 weston_compositor_init_keytimer(ec, loop);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007932
Quentin Glidic82681572016-12-17 13:40:51 +01007933 weston_layer_init(&ec->fade_layer, ec);
7934 weston_layer_init(&ec->cursor_layer, ec);
7935
7936 weston_layer_set_position(&ec->fade_layer, WESTON_LAYER_POSITION_FADE);
7937 weston_layer_set_position(&ec->cursor_layer,
7938 WESTON_LAYER_POSITION_CURSOR);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007939
Daniel Stonece62cb32018-07-20 09:46:24 +01007940 ec->debug_scene =
Leandro Ribeiroce100192019-12-26 16:35:49 -03007941 weston_compositor_add_log_scope(ec, "scene-graph",
7942 "Scene graph details\n",
7943 debug_scene_graph_cb, NULL,
7944 ec);
Daniel Stonece62cb32018-07-20 09:46:24 +01007945
Marius Vladda104eb2019-09-05 14:31:01 +03007946 ec->timeline =
Leandro Ribeiroce100192019-12-26 16:35:49 -03007947 weston_compositor_add_log_scope(ec, "timeline",
7948 "Timeline event points\n",
7949 weston_timeline_create_subscription,
7950 weston_timeline_destroy_subscription,
7951 ec);
leng.fang32af9fc2024-06-13 11:22:15 +08007952 weston_compositor_aml_init(ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007953 return ec;
7954
7955fail:
7956 free(ec);
7957 return NULL;
7958}
7959
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007960/** weston_compositor_shutdown
7961 * \ingroup compositor
7962 */
Benjamin Franzkeb8263022011-08-30 11:32:47 +02007963WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007964weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07007965{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007966 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07007967
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007968 wl_event_source_remove(ec->idle_source);
Lujin Wangd4b46cd2019-08-12 12:03:29 -07007969 wl_event_source_remove(ec->repaint_timer);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007970
Matt Roper361d2ad2011-08-29 13:52:23 -07007971 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02007972 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07007973 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02007974
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007975 /* Destroy all pending outputs associated with this compositor */
7976 wl_list_for_each_safe(output, next, &ec->pending_output_list, link)
7977 output->destroy(output);
7978
Pekka Paalanen90a5ffa2021-02-25 12:03:28 +02007979 /* Color manager objects may have renderer hooks */
7980 if (ec->color_manager) {
7981 ec->color_manager->destroy(ec->color_manager);
7982 ec->color_manager = NULL;
7983 }
7984
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02007985 if (ec->renderer)
7986 ec->renderer->destroy(ec);
7987
Daniel Stone325fc2d2012-05-30 16:31:58 +01007988 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09007989 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01007990 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01007991 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01007992 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02007993 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007994
leng.fangdbaf6fa2024-06-20 19:31:04 +08007995 weston_compositor_clear_surface_id(ec);
7996
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04007997 weston_plane_release(&ec->primary_plane);
Pekka Paalanen87400372021-05-14 14:29:40 +03007998
7999 weston_layer_fini(&ec->fade_layer);
8000 weston_layer_fini(&ec->cursor_layer);
8001
8002 if (!wl_list_empty(&ec->layer_list))
Dongjin Kimba89f002024-09-30 17:49:36 +09008003 weston_log("BUG: layer_list is not empty after shutdown. Calls to weston_layer_fini() are missing somewhere.\n");
Matt Roper361d2ad2011-08-29 13:52:23 -07008004}
8005
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008006/** weston_compositor_exit_with_code
8007 * \ingroup compositor
8008 */
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05008009WL_EXPORT void
Adam Jackson3c3f3b12019-10-16 16:02:59 -04008010weston_compositor_exit_with_code(struct weston_compositor *compositor,
Frederic Plourdec336f062014-10-29 14:44:33 -04008011 int exit_code)
8012{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02008013 if (compositor->exit_code == EXIT_SUCCESS)
8014 compositor->exit_code = exit_code;
8015
Giulio Camuffo459137b2014-10-11 23:56:24 +03008016 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04008017}
8018
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008019/** weston_compositor_set_default_pointer_grab
8020 * \ingroup compositor
8021 */
Frederic Plourdec336f062014-10-29 14:44:33 -04008022WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01008023weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
8024 const struct weston_pointer_grab_interface *interface)
8025{
8026 struct weston_seat *seat;
8027
8028 ec->default_pointer_grab = interface;
8029 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05008030 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
8031
8032 if (pointer)
8033 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01008034 }
8035}
8036
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008037/** weston_compositor_set_presentation_clock
8038 * \ingroup compositor
8039 */
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04008040WL_EXPORT int
8041weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
8042 clockid_t clk_id)
8043{
8044 struct timespec ts;
8045
8046 if (clock_gettime(clk_id, &ts) < 0)
8047 return -1;
8048
8049 compositor->presentation_clock = clk_id;
8050
8051 return 0;
8052}
8053
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008054/** For choosing the software clock, when the display hardware or API
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04008055 * does not expose a compatible presentation timestamp.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008056 *
8057 * \ingroup compositor
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04008058 */
8059WL_EXPORT int
8060weston_compositor_set_presentation_clock_software(
8061 struct weston_compositor *compositor)
8062{
8063 /* In order of preference */
8064 static const clockid_t clocks[] = {
8065 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
8066 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
8067 CLOCK_MONOTONIC, /* no jumps, may crawl */
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04008068 };
8069 unsigned i;
8070
8071 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
8072 if (weston_compositor_set_presentation_clock(compositor,
8073 clocks[i]) == 0)
8074 return 0;
8075
8076 weston_log("Error: no suitable presentation clock available.\n");
8077
8078 return -1;
8079}
8080
Pekka Paalanen662f3842015-03-18 12:17:26 +02008081/** Read the current time from the Presentation clock
8082 *
8083 * \param compositor
Marius Vlada2dace22019-06-12 16:05:44 +03008084 * \param[out] ts The current time.
Pekka Paalanen662f3842015-03-18 12:17:26 +02008085 *
8086 * \note Reading the current time in user space is always imprecise to some
8087 * degree.
8088 *
8089 * This function is never meant to fail. If reading the clock does fail,
8090 * an error message is logged and a zero time is returned. Callers are not
8091 * supposed to detect or react to failures.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008092 *
8093 * \ingroup compositor
Pekka Paalanen662f3842015-03-18 12:17:26 +02008094 */
8095WL_EXPORT void
8096weston_compositor_read_presentation_clock(
8097 const struct weston_compositor *compositor,
8098 struct timespec *ts)
8099{
8100 static bool warned;
8101 int ret;
8102
8103 ret = clock_gettime(compositor->presentation_clock, ts);
8104 if (ret < 0) {
8105 ts->tv_sec = 0;
8106 ts->tv_nsec = 0;
8107
8108 if (!warned)
8109 weston_log("Error: failure to read "
Antonio Borneo39578632019-04-26 23:57:31 +02008110 "the presentation clock %#x: '%s' (%d)\n",
8111 compositor->presentation_clock,
8112 strerror(errno), errno);
Pekka Paalanen662f3842015-03-18 12:17:26 +02008113 warned = true;
8114 }
8115}
8116
Pekka Paalanen230f3b12014-09-29 14:18:40 -04008117/** Import dmabuf buffer into current renderer
8118 *
8119 * \param compositor
8120 * \param buffer the dmabuf buffer to import
8121 * \return true on usable buffers, false otherwise
8122 *
8123 * This function tests that the linux_dmabuf_buffer is usable
8124 * for the current renderer. Returns false on unusable buffers. Usually
8125 * usability is tested by importing the dmabufs for composition.
8126 *
8127 * This hook is also used for detecting if the renderer supports
8128 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
8129 * supported.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008130 *
8131 * \ingroup compositor
8132 */
Pekka Paalanen230f3b12014-09-29 14:18:40 -04008133WL_EXPORT bool
8134weston_compositor_import_dmabuf(struct weston_compositor *compositor,
8135 struct linux_dmabuf_buffer *buffer)
8136{
8137 struct weston_renderer *renderer;
8138
8139 renderer = compositor->renderer;
8140
8141 if (renderer->import_dmabuf == NULL)
8142 return false;
8143
8144 return renderer->import_dmabuf(compositor, buffer);
8145}
8146
Marius Vlad5a701542019-11-16 20:26:52 +02008147WL_EXPORT bool
8148weston_compositor_dmabuf_can_scanout(struct weston_compositor *compositor,
8149 struct linux_dmabuf_buffer *buffer)
8150{
8151 struct weston_backend *backend = compositor->backend;
8152
8153 if (backend->can_scanout_dmabuf == NULL)
8154 return false;
8155
8156 return backend->can_scanout_dmabuf(compositor, buffer);
8157}
8158
Giulio Camuffocdb4d292013-11-14 23:42:53 +01008159WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05008160weston_version(int *major, int *minor, int *micro)
8161{
8162 *major = WESTON_VERSION_MAJOR;
8163 *minor = WESTON_VERSION_MINOR;
8164 *micro = WESTON_VERSION_MICRO;
8165}
8166
Daniel Stonee03c1112016-11-24 20:45:45 +00008167/**
8168 * Attempts to find a module path from the module map specified in the
8169 * environment. If found, writes the full path into the path variable.
8170 *
8171 * The module map is a string in environment variable WESTON_MODULE_MAP, where
8172 * each entry is of the form "name=path" and entries are separated by
8173 * semicolons. Whitespace is significant.
8174 *
8175 * \param name The name to search for.
8176 * \param path Where the path is written to if found.
8177 * \param path_len Allocated bytes at \c path .
8178 * \returns The length of the string written to path on success, or 0 if the
8179 * module was not specified in the environment map or path_len was too small.
8180 */
8181WL_EXPORT size_t
8182weston_module_path_from_env(const char *name, char *path, size_t path_len)
8183{
8184 const char *mapping = getenv("WESTON_MODULE_MAP");
8185 const char *end;
8186 const int name_len = strlen(name);
8187
8188 if (!mapping)
8189 return 0;
8190
8191 end = mapping + strlen(mapping);
8192 while (mapping < end && *mapping) {
8193 const char *filename, *next;
8194
8195 /* early out: impossibly short string */
8196 if (end - mapping < name_len + 1)
8197 return 0;
8198
8199 filename = &mapping[name_len + 1];
8200 next = strchrnul(mapping, ';');
8201
8202 if (strncmp(mapping, name, name_len) == 0 &&
8203 mapping[name_len] == '=') {
8204 size_t file_len = next - filename; /* no trailing NUL */
8205 if (file_len >= path_len)
8206 return 0;
8207 strncpy(path, filename, file_len);
8208 path[file_len] = '\0';
8209 return file_len;
8210 }
8211
8212 mapping = next + 1;
8213 }
8214
8215 return 0;
8216}
8217
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03008218WL_EXPORT void *
8219weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04008220{
8221 char path[PATH_MAX];
8222 void *module, *init;
Daniel Stonebeb97e52016-11-28 12:13:54 +00008223 size_t len;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04008224
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08008225 if (name == NULL)
8226 return NULL;
8227
Derek Foreman3f86e502015-06-08 11:46:54 -05008228 if (name[0] != '/') {
Daniel Stonee03c1112016-11-24 20:45:45 +00008229 len = weston_module_path_from_env(name, path, sizeof path);
8230 if (len == 0)
Daniel Stonebeb97e52016-11-28 12:13:54 +00008231 len = snprintf(path, sizeof path, "%s/%s",
8232 LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05008233 } else {
Daniel Stonebeb97e52016-11-28 12:13:54 +00008234 len = snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05008235 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04008236
Daniel Stonebeb97e52016-11-28 12:13:54 +00008237 /* snprintf returns the length of the string it would've written,
8238 * _excluding_ the NUL byte. So even being equal to the size of
8239 * our buffer is an error here. */
8240 if (len >= sizeof path)
8241 return NULL;
8242
Kristian Høgsberga6813d22012-09-12 12:21:01 -04008243 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
8244 if (module) {
8245 weston_log("Module '%s' already loaded\n", path);
Pekka Paalanenf696ee92019-11-04 14:07:51 +02008246 } else {
8247 weston_log("Loading module '%s'\n", path);
8248 module = dlopen(path, RTLD_NOW);
8249 if (!module) {
8250 weston_log("Failed to load module: %s\n", dlerror());
8251 return NULL;
8252 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04008253 }
8254
8255 init = dlsym(module, entrypoint);
8256 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03008257 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00008258 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04008259 return NULL;
8260 }
8261
8262 return init;
8263}
8264
Pekka Paalanen6ffbba32019-11-06 12:59:32 +02008265/** Add a compositor destroy listener only once
8266 *
8267 * \param compositor The compositor whose destroy to watch for.
8268 * \param listener The listener struct to initialize.
8269 * \param destroy_handler The callback when compositor is destroyed.
8270 * \return True if listener is added, or false if there already is a listener
8271 * with the given \c destroy_handler.
8272 *
8273 * This function does nothing and returns false if the given callback function
8274 * is already present in the weston_compositor destroy callbacks list.
8275 * Otherwise, this function initializes the given listener with the given
8276 * callback pointer and adds it to the compositor's destroy callbacks list.
8277 *
8278 * This can be used to ensure that plugin initialization is done only once
8279 * in case the same plugin is loaded multiple times. If this function returns
8280 * false, the plugin should be already initialized successfully.
8281 *
8282 * All plugins should register a destroy listener for cleaning up. Note, that
8283 * the plugin destruction order is not guaranteed: plugins that depend on other
8284 * plugins must be able to be torn down in arbitrary order.
8285 *
Leandro Ribeiroca640d52020-01-27 19:12:01 -03008286 * \sa weston_compositor_destroy
Pekka Paalanen6ffbba32019-11-06 12:59:32 +02008287 */
8288WL_EXPORT bool
8289weston_compositor_add_destroy_listener_once(struct weston_compositor *compositor,
8290 struct wl_listener *listener,
8291 wl_notify_func_t destroy_handler)
8292{
8293 if (wl_signal_get(&compositor->destroy_signal, destroy_handler))
8294 return false;
8295
8296 listener->notify = destroy_handler;
8297 wl_signal_add(&compositor->destroy_signal, listener);
8298 return true;
8299}
Giulio Camuffo459137b2014-10-11 23:56:24 +03008300
Leandro Ribeiroca640d52020-01-27 19:12:01 -03008301/** Destroys the compositor.
Giulio Camuffo459137b2014-10-11 23:56:24 +03008302 *
Leandro Ribeiroca640d52020-01-27 19:12:01 -03008303 * This function cleans up the compositor state and then destroys it.
Giulio Camuffo459137b2014-10-11 23:56:24 +03008304 *
Leandro Ribeiroca640d52020-01-27 19:12:01 -03008305 * @param compositor The compositor to be destroyed.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008306 *
Marius Vlad284d5342019-06-24 12:00:47 +03008307 * @ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03008308 */
8309WL_EXPORT void
Leandro Ribeiroca640d52020-01-27 19:12:01 -03008310weston_compositor_destroy(struct weston_compositor *compositor)
Giulio Camuffo459137b2014-10-11 23:56:24 +03008311{
8312 /* prevent further rendering while shutting down */
8313 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
8314
Marius Vladf2452d62021-12-09 14:07:21 +02008315 weston_signal_emit_mutable(&compositor->destroy_signal, compositor);
Giulio Camuffo459137b2014-10-11 23:56:24 +03008316
8317 weston_compositor_xkb_destroy(compositor);
8318
Giulio Camuffo2d24e642015-10-03 16:25:15 +03008319 if (compositor->backend)
8320 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02008321
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03008322 /* The backend is responsible for destroying the heads. */
8323 assert(wl_list_empty(&compositor->head_list));
8324
Pekka Paalanen827b5d22016-06-29 11:54:26 +02008325 weston_plugin_api_destroy_list(compositor);
8326
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03008327 if (compositor->heads_changed_source)
8328 wl_event_source_remove(compositor->heads_changed_source);
8329
Leandro Ribeirof0149642019-12-18 15:52:18 -03008330 weston_log_scope_destroy(compositor->debug_scene);
Daniel Stonece62cb32018-07-20 09:46:24 +01008331 compositor->debug_scene = NULL;
Marius Vladda104eb2019-09-05 14:31:01 +03008332
Leandro Ribeirof0149642019-12-18 15:52:18 -03008333 weston_log_scope_destroy(compositor->timeline);
Marius Vladda104eb2019-09-05 14:31:01 +03008334 compositor->timeline = NULL;
Pekka Paalanena5630ea2017-10-12 13:13:42 +02008335
Leandro Ribeiro8eb84142021-01-18 19:36:48 -03008336 if (compositor->default_dmabuf_feedback) {
8337 weston_dmabuf_feedback_destroy(compositor->default_dmabuf_feedback);
8338 weston_dmabuf_feedback_format_table_destroy(compositor->dmabuf_feedback_format_table);
8339 }
8340
Giulio Camuffo459137b2014-10-11 23:56:24 +03008341 free(compositor);
8342}
8343
8344/** Instruct the compositor to exit.
8345 *
8346 * This functions does not directly destroy the compositor object, it merely
8347 * command it to start the tear down process. It is not guaranteed that the
8348 * tear down will happen immediately.
8349 *
8350 * \param compositor The compositor to tear down.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008351 *
8352 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03008353 */
8354WL_EXPORT void
8355weston_compositor_exit(struct weston_compositor *compositor)
8356{
8357 compositor->exit(compositor);
8358}
8359
8360/** Return the user data stored in the compositor.
8361 *
8362 * This function returns the user data pointer set with user_data parameter
8363 * to the \ref weston_compositor_create function.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008364 *
8365 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03008366 */
8367WL_EXPORT void *
8368weston_compositor_get_user_data(struct weston_compositor *compositor)
8369{
8370 return compositor->user_data;
8371}
Pekka Paalanendd186732016-06-03 14:49:54 +03008372
Pekka Paalanen50dbf382016-06-03 15:23:46 +03008373static const char * const backend_map[] = {
8374 [WESTON_BACKEND_DRM] = "drm-backend.so",
8375 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
8376 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
8377 [WESTON_BACKEND_RDP] = "rdp-backend.so",
8378 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
8379 [WESTON_BACKEND_X11] = "x11-backend.so",
8380};
8381
Pekka Paalanendd186732016-06-03 14:49:54 +03008382/** Load a backend into a weston_compositor
8383 *
8384 * A backend must be loaded to make a weston_compositor work. A backend
8385 * provides input and output capabilities, and determines the renderer to use.
8386 *
8387 * \param compositor A compositor that has not had a backend loaded yet.
8388 * \param backend Name of the backend file.
8389 * \param config_base A pointer to a backend-specific configuration
8390 * structure's 'base' member.
8391 *
8392 * \return 0 on success, or -1 on error.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008393 *
8394 * \ingroup compositor
Pekka Paalanendd186732016-06-03 14:49:54 +03008395 */
8396WL_EXPORT int
8397weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03008398 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03008399 struct weston_backend_config *config_base)
8400{
8401 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03008402 struct weston_backend_config *config_base);
8403
Pekka Paalanend7e35112017-08-29 17:04:12 +03008404 if (compositor->backend) {
8405 weston_log("Error: attempt to load a backend when one is already loaded\n");
8406 return -1;
8407 }
8408
Quentin Glidic887c0182016-07-10 11:00:53 +02008409 if (backend >= ARRAY_LENGTH(backend_map))
Pekka Paalanen50dbf382016-06-03 15:23:46 +03008410 return -1;
8411
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01008412 backend_init = weston_load_module(backend_map[backend], "weston_backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03008413 if (!backend_init)
8414 return -1;
8415
Pekka Paalanend7e35112017-08-29 17:04:12 +03008416 if (backend_init(compositor, config_base) < 0) {
8417 compositor->backend = NULL;
8418 return -1;
8419 }
8420
Pekka Paalanen90a5ffa2021-02-25 12:03:28 +02008421 if (!compositor->color_manager) {
8422 compositor->color_manager =
8423 weston_color_manager_noop_create(compositor);
8424 }
8425
8426 if (!compositor->color_manager)
8427 return -1;
8428
8429 if (!compositor->color_manager->init(compositor->color_manager))
8430 return -1;
8431
8432 weston_log("Color manager: %s\n", compositor->color_manager->name);
8433
Pekka Paalanend7e35112017-08-29 17:04:12 +03008434 return 0;
Pekka Paalanendd186732016-06-03 14:49:54 +03008435}
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008436
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008437/** weston_compositor_load_xwayland
8438 * \ingroup compositor
8439 */
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008440WL_EXPORT int
8441weston_compositor_load_xwayland(struct weston_compositor *compositor)
8442{
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01008443 int (*module_init)(struct weston_compositor *ec);
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008444
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01008445 module_init = weston_load_module("xwayland.so", "weston_module_init");
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008446 if (!module_init)
8447 return -1;
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01008448 if (module_init(compositor) < 0)
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008449 return -1;
8450 return 0;
8451}
Vasilis Liaskovitis486b4632018-10-10 16:14:55 +02008452
Pekka Paalanen5e79dd42021-03-29 14:11:49 +03008453/** Load Little CMS color manager plugin
8454 *
8455 * Calling this function before loading any backend sets Little CMS
8456 * as the active color matching module (CMM) instead of the default no-op
8457 * color manager.
8458 *
8459 * \ingroup compositor
8460 */
8461WL_EXPORT int
8462weston_compositor_load_color_manager(struct weston_compositor *compositor)
8463{
8464 struct weston_color_manager *
8465 (*cm_create)(struct weston_compositor *compositor);
8466
8467 if (compositor->color_manager) {
8468 weston_log("Error: Color manager '%s' is loaded, cannot load another.\n",
8469 compositor->color_manager->name);
8470 return -1;
8471 }
8472
8473 cm_create = weston_load_module("color-lcms.so", "weston_color_manager_create");
8474 if (!cm_create) {
8475 weston_log("Error: Could not load color-lcms.so.\n");
8476 return -1;
8477 }
8478
8479 compositor->color_manager = cm_create(compositor);
8480 if (!compositor->color_manager) {
8481 weston_log("Error: loading color-lcms.so failed.\n");
8482 return -1;
8483 }
8484
8485 return 0;
8486}
8487
Vasilis Liaskovitis486b4632018-10-10 16:14:55 +02008488/** Resolve an internal compositor error by disconnecting the client.
8489 *
8490 * This function is used in cases when the wl_buffer turns out
8491 * unusable and there is no fallback path.
8492 *
8493 * It is possible the fault is caused by a compositor bug, the underlying
8494 * graphics stack bug or normal behaviour, or perhaps a client mistake.
8495 * In any case, the options are to either composite garbage or nothing,
8496 * or disconnect the client. This is a helper function for the latter.
8497 *
8498 * The error is sent as an INVALID_OBJECT error on the client's wl_display.
8499 *
8500 * \param buffer The weston buffer that is unusable.
8501 * \param msg A custom error message attached to the protocol error.
8502 */
8503WL_EXPORT void
8504weston_buffer_send_server_error(struct weston_buffer *buffer,
8505 const char *msg)
8506{
8507 struct wl_client *client;
8508 struct wl_resource *display_resource;
8509 uint32_t id;
8510
8511 assert(buffer->resource);
8512 id = wl_resource_get_id(buffer->resource);
8513 client = wl_resource_get_client(buffer->resource);
8514 display_resource = wl_client_get_object(client, 1);
8515
8516 assert(display_resource);
8517 wl_resource_post_error(display_resource,
8518 WL_DISPLAY_ERROR_INVALID_OBJECT,
8519 "server error with "
8520 "wl_buffer@%u: %s", id, msg);
8521}
Ankit Nautiyal93dde242019-07-08 11:46:42 +05308522
8523WL_EXPORT void
8524weston_output_disable_planes_incr(struct weston_output *output)
8525{
8526 output->disable_planes++;
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05308527 /*
8528 * If disable_planes changes from 0 to non-zero, it means some type of
8529 * recording of content has started, and therefore protection level of
8530 * the protected surfaces must be updated to avoid the recording of
8531 * the protected content.
8532 */
8533 if (output->disable_planes == 1)
8534 weston_schedule_surface_protection_update(output->compositor);
Ankit Nautiyal93dde242019-07-08 11:46:42 +05308535}
8536
8537WL_EXPORT void
8538weston_output_disable_planes_decr(struct weston_output *output)
8539{
8540 output->disable_planes--;
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05308541 /*
8542 * If disable_planes changes from non-zero to 0, it means no content
8543 * recording is going on any more, and the protected and surfaces can be
8544 * shown without any apprehensions about content being recorded.
8545 */
8546 if (output->disable_planes == 0)
8547 weston_schedule_surface_protection_update(output->compositor);
8548
Ankit Nautiyal93dde242019-07-08 11:46:42 +05308549}