blob: 2c87f2da79c819a65f9227b2dd4a3a034b265415 [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);
limin.tian531dd232025-01-07 12:09:27 +0000634 WESTON_LOGI(COMMON_LOG, "%p \n", surface);
leng.fang32af9fc2024-06-13 11:22:15 +0800635
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400636 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500637}
638
Alex Wu8811bf92012-02-28 18:07:54 +0800639WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500640weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200641 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500642{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100643 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Philipp Zabel195dade2018-09-03 19:44:59 +0200644 surface->is_opaque = !(alpha < 1.0);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500645}
646
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400647WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500648weston_view_to_global_float(struct weston_view *view,
649 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200650{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500651 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200652 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
653
Jason Ekstranda7af7042013-10-12 22:38:11 -0500654 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200655
656 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200657 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700658 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200659 v.f[3]);
660 *x = 0;
661 *y = 0;
662 return;
663 }
664
665 *x = v.f[0] / v.f[3];
666 *y = v.f[1] / v.f[3];
667 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500668 *x = sx + view->geometry.x;
669 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200670 }
671}
672
Pekka Paalanen0df44772020-02-14 14:50:03 +0200673/** Transform a point to buffer coordinates
674 *
675 * \param width Surface width.
676 * \param height Surface height.
677 * \param transform Buffer transform.
678 * \param scale Buffer scale.
679 * \param sx Surface x coordinate of a point.
680 * \param sy Surface y coordinate of a point.
681 * \param[out] bx Buffer x coordinate of the point.
682 * \param[out] by Buffer Y coordinate of the point.
683 *
684 * Converts the given surface-local coordinates to buffer coordinates
685 * according to the given buffer transform and scale.
686 * This ignores wp_viewport.
687 *
688 * The given width and height must be the result of inverse scaled and
689 * inverse transformed buffer size.
690 */
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500691WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200692weston_transformed_coord(int width, int height,
693 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200694 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200695 float sx, float sy, float *bx, float *by)
696{
697 switch (transform) {
698 case WL_OUTPUT_TRANSFORM_NORMAL:
699 default:
700 *bx = sx;
701 *by = sy;
702 break;
703 case WL_OUTPUT_TRANSFORM_FLIPPED:
704 *bx = width - sx;
705 *by = sy;
706 break;
707 case WL_OUTPUT_TRANSFORM_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200708 *bx = sy;
709 *by = width - sx;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200710 break;
711 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200712 *bx = sy;
713 *by = sx;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200714 break;
715 case WL_OUTPUT_TRANSFORM_180:
716 *bx = width - sx;
717 *by = height - sy;
718 break;
719 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
720 *bx = sx;
721 *by = height - sy;
722 break;
723 case WL_OUTPUT_TRANSFORM_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200724 *bx = height - sy;
725 *by = sx;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200726 break;
727 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200728 *bx = height - sy;
729 *by = width - sx;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200730 break;
731 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200732
733 *bx *= scale;
734 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200735}
736
Pekka Paalanen0df44772020-02-14 14:50:03 +0200737/** Transform a rectangle to buffer coordinates
738 *
739 * \param width Surface width.
740 * \param height Surface height.
741 * \param transform Buffer transform.
742 * \param scale Buffer scale.
743 * \param rect Rectangle in surface coordinates.
744 * \return Rectangle in buffer coordinates.
745 *
746 * Converts the given surface-local rectangle to buffer coordinates
747 * according to the given buffer transform and scale. The resulting
748 * rectangle is guaranteed to be well-formed.
749 * This ignores wp_viewport.
750 *
751 * The given width and height must be the result of inverse scaled and
752 * inverse transformed buffer size.
753 */
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200754WL_EXPORT pixman_box32_t
755weston_transformed_rect(int width, int height,
756 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200757 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200758 pixman_box32_t rect)
759{
760 float x1, x2, y1, y2;
761
762 pixman_box32_t ret;
763
Alexander Larsson4ea95522013-05-22 14:41:37 +0200764 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200765 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200766 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200767 rect.x2, rect.y2, &x2, &y2);
768
769 if (x1 <= x2) {
770 ret.x1 = x1;
771 ret.x2 = x2;
772 } else {
773 ret.x1 = x2;
774 ret.x2 = x1;
775 }
776
777 if (y1 <= y2) {
778 ret.y1 = y1;
779 ret.y2 = y2;
780 } else {
781 ret.y1 = y2;
782 ret.y2 = y1;
783 }
784
785 return ret;
786}
787
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600788/** Transform a region by a matrix, restricted to axis-aligned transformations
789 *
790 * Warning: This function does not work for projective, affine, or matrices
791 * that encode arbitrary rotations. Only 90-degree step rotations are
792 * supported.
793 */
794WL_EXPORT void
795weston_matrix_transform_region(pixman_region32_t *dest,
796 struct weston_matrix *matrix,
797 pixman_region32_t *src)
798{
799 pixman_box32_t *src_rects, *dest_rects;
800 int nrects, i;
801
802 src_rects = pixman_region32_rectangles(src, &nrects);
803 dest_rects = malloc(nrects * sizeof(*dest_rects));
804 if (!dest_rects)
805 return;
806
807 for (i = 0; i < nrects; i++) {
808 struct weston_vector vec1 = {{
809 src_rects[i].x1, src_rects[i].y1, 0, 1
810 }};
811 weston_matrix_transform(matrix, &vec1);
812 vec1.f[0] /= vec1.f[3];
813 vec1.f[1] /= vec1.f[3];
814
815 struct weston_vector vec2 = {{
816 src_rects[i].x2, src_rects[i].y2, 0, 1
817 }};
818 weston_matrix_transform(matrix, &vec2);
819 vec2.f[0] /= vec2.f[3];
820 vec2.f[1] /= vec2.f[3];
821
822 if (vec1.f[0] < vec2.f[0]) {
823 dest_rects[i].x1 = floor(vec1.f[0]);
824 dest_rects[i].x2 = ceil(vec2.f[0]);
825 } else {
826 dest_rects[i].x1 = floor(vec2.f[0]);
827 dest_rects[i].x2 = ceil(vec1.f[0]);
828 }
829
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600830 if (vec1.f[1] < vec2.f[1]) {
831 dest_rects[i].y1 = floor(vec1.f[1]);
832 dest_rects[i].y2 = ceil(vec2.f[1]);
833 } else {
834 dest_rects[i].y1 = floor(vec2.f[1]);
835 dest_rects[i].y2 = ceil(vec1.f[1]);
836 }
837 }
838
839 pixman_region32_clear(dest);
840 pixman_region32_init_rects(dest, dest_rects, nrects);
841 free(dest_rects);
842}
843
Pekka Paalanen0df44772020-02-14 14:50:03 +0200844/** Transform a region to buffer coordinates
845 *
846 * \param width Surface width.
847 * \param height Surface height.
848 * \param transform Buffer transform.
849 * \param scale Buffer scale.
850 * \param[in] src Region in surface coordinates.
851 * \param[out] dest Resulting region in buffer coordinates.
852 *
853 * Converts the given surface-local region to buffer coordinates
854 * according to the given buffer transform and scale.
855 * This ignores wp_viewport.
856 *
857 * The given width and height must be the result of inverse scaled and
858 * inverse transformed buffer size.
859 *
860 * src and dest are allowed to point to the same memory for in-place conversion.
861 */
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200862WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500863weston_transformed_region(int width, int height,
864 enum wl_output_transform transform,
865 int32_t scale,
866 pixman_region32_t *src, pixman_region32_t *dest)
867{
868 pixman_box32_t *src_rects, *dest_rects;
869 int nrects, i;
870
871 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
872 if (src != dest)
873 pixman_region32_copy(dest, src);
874 return;
875 }
876
877 src_rects = pixman_region32_rectangles(src, &nrects);
878 dest_rects = malloc(nrects * sizeof(*dest_rects));
879 if (!dest_rects)
880 return;
881
882 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
883 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
884 } else {
885 for (i = 0; i < nrects; i++) {
886 switch (transform) {
887 default:
888 case WL_OUTPUT_TRANSFORM_NORMAL:
889 dest_rects[i].x1 = src_rects[i].x1;
890 dest_rects[i].y1 = src_rects[i].y1;
891 dest_rects[i].x2 = src_rects[i].x2;
892 dest_rects[i].y2 = src_rects[i].y2;
893 break;
894 case WL_OUTPUT_TRANSFORM_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200895 dest_rects[i].x1 = src_rects[i].y1;
896 dest_rects[i].y1 = width - src_rects[i].x2;
897 dest_rects[i].x2 = src_rects[i].y2;
898 dest_rects[i].y2 = width - src_rects[i].x1;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500899 break;
900 case WL_OUTPUT_TRANSFORM_180:
901 dest_rects[i].x1 = width - src_rects[i].x2;
902 dest_rects[i].y1 = height - src_rects[i].y2;
903 dest_rects[i].x2 = width - src_rects[i].x1;
904 dest_rects[i].y2 = height - src_rects[i].y1;
905 break;
906 case WL_OUTPUT_TRANSFORM_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200907 dest_rects[i].x1 = height - src_rects[i].y2;
908 dest_rects[i].y1 = src_rects[i].x1;
909 dest_rects[i].x2 = height - src_rects[i].y1;
910 dest_rects[i].y2 = src_rects[i].x2;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500911 break;
912 case WL_OUTPUT_TRANSFORM_FLIPPED:
913 dest_rects[i].x1 = width - src_rects[i].x2;
914 dest_rects[i].y1 = src_rects[i].y1;
915 dest_rects[i].x2 = width - src_rects[i].x1;
916 dest_rects[i].y2 = src_rects[i].y2;
917 break;
918 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200919 dest_rects[i].x1 = src_rects[i].y1;
920 dest_rects[i].y1 = src_rects[i].x1;
921 dest_rects[i].x2 = src_rects[i].y2;
922 dest_rects[i].y2 = src_rects[i].x2;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500923 break;
924 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
925 dest_rects[i].x1 = src_rects[i].x1;
926 dest_rects[i].y1 = height - src_rects[i].y2;
927 dest_rects[i].x2 = src_rects[i].x2;
928 dest_rects[i].y2 = height - src_rects[i].y1;
929 break;
930 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200931 dest_rects[i].x1 = height - src_rects[i].y2;
932 dest_rects[i].y1 = width - src_rects[i].x2;
933 dest_rects[i].x2 = height - src_rects[i].y1;
934 dest_rects[i].y2 = width - src_rects[i].x1;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500935 break;
936 }
937 }
938 }
939
940 if (scale != 1) {
941 for (i = 0; i < nrects; i++) {
942 dest_rects[i].x1 *= scale;
943 dest_rects[i].x2 *= scale;
944 dest_rects[i].y1 *= scale;
945 dest_rects[i].y2 *= scale;
946 }
947 }
948
949 pixman_region32_clear(dest);
950 pixman_region32_init_rects(dest, dest_rects, nrects);
951 free(dest_rects);
952}
953
Jonny Lamb74130762013-11-26 18:19:46 +0100954static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300955viewport_surface_to_buffer(struct weston_surface *surface,
956 float sx, float sy, float *bx, float *by)
Jonny Lamb74130762013-11-26 18:19:46 +0100957{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200958 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200959 double src_width, src_height;
960 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200961
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200962 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
963 if (vp->surface.width == -1) {
964 *bx = sx;
965 *by = sy;
966 return;
967 }
Jonny Lamb74130762013-11-26 18:19:46 +0100968
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200969 src_x = 0.0;
970 src_y = 0.0;
971 src_width = surface->width_from_buffer;
972 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100973 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200974 src_x = wl_fixed_to_double(vp->buffer.src_x);
975 src_y = wl_fixed_to_double(vp->buffer.src_y);
976 src_width = wl_fixed_to_double(vp->buffer.src_width);
977 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100978 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200979
980 *bx = sx * src_width / surface->width + src_x;
981 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100982}
983
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500984WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200985weston_surface_to_buffer_float(struct weston_surface *surface,
986 float sx, float sy, float *bx, float *by)
987{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200988 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
989
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300990 /* first transform coordinates if the viewport is set */
991 viewport_surface_to_buffer(surface, sx, sy, bx, by);
Jonny Lamb74130762013-11-26 18:19:46 +0100992
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500993 weston_transformed_coord(surface->width_from_buffer,
994 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200995 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100996 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200997}
998
Derek Foreman99739672015-12-03 16:38:11 -0600999/** Transform a rectangle from surface coordinates to buffer coordinates
1000 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03001001 * \param surface The surface to fetch wp_viewport and buffer transformation
Derek Foreman99739672015-12-03 16:38:11 -06001002 * from.
1003 * \param rect The rectangle to transform.
1004 * \return The transformed rectangle.
1005 *
1006 * Viewport and buffer transformations can only do translation, scaling,
1007 * and rotations in 90-degree steps. Therefore the only loss in the
1008 * conversion is coordinate rounding.
1009 *
1010 * However, some coordinate rounding takes place as an intermediate
1011 * step before the buffer scale factor is applied, so the rectangle
1012 * boundary may not be exactly as expected.
1013 *
1014 * This is OK for damage tracking since a little extra coverage is
1015 * not a problem.
1016 */
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +02001017WL_EXPORT pixman_box32_t
1018weston_surface_to_buffer_rect(struct weston_surface *surface,
1019 pixman_box32_t rect)
1020{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001021 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +01001022 float xf, yf;
1023
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03001024 /* first transform box coordinates if the viewport is set */
1025 viewport_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
Jonny Lamb74130762013-11-26 18:19:46 +01001026 rect.x1 = floorf(xf);
1027 rect.y1 = floorf(yf);
1028
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03001029 viewport_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
Derek Foremane2e15ac2015-12-01 13:00:43 -06001030 rect.x2 = ceilf(xf);
1031 rect.y2 = ceilf(yf);
Jonny Lamb74130762013-11-26 18:19:46 +01001032
Jason Ekstrandd0cebc32014-04-21 20:56:46 -05001033 return weston_transformed_rect(surface->width_from_buffer,
1034 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001035 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +02001036 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +02001037}
1038
Pekka Paalanene54e31c2015-03-04 14:23:28 +02001039/** Transform a region from surface coordinates to buffer coordinates
1040 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03001041 * \param surface The surface to fetch wp_viewport and buffer transformation
Pekka Paalanene54e31c2015-03-04 14:23:28 +02001042 * from.
Marius Vlada2dace22019-06-12 16:05:44 +03001043 * \param[in] surface_region The region in surface coordinates.
1044 * \param[out] buffer_region The region converted to buffer coordinates.
Pekka Paalanene54e31c2015-03-04 14:23:28 +02001045 *
1046 * Buffer_region must be init'd, but will be completely overwritten.
1047 *
1048 * Viewport and buffer transformations can only do translation, scaling,
1049 * and rotations in 90-degree steps. Therefore the only loss in the
Derek Foreman99739672015-12-03 16:38:11 -06001050 * conversion is from the coordinate rounding that takes place in
1051 * \ref weston_surface_to_buffer_rect.
Marius Vlada2dace22019-06-12 16:05:44 +03001052 *
Pekka Paalanene54e31c2015-03-04 14:23:28 +02001053 */
1054WL_EXPORT void
1055weston_surface_to_buffer_region(struct weston_surface *surface,
1056 pixman_region32_t *surface_region,
1057 pixman_region32_t *buffer_region)
1058{
1059 pixman_box32_t *src_rects, *dest_rects;
1060 int nrects, i;
1061
1062 src_rects = pixman_region32_rectangles(surface_region, &nrects);
1063 dest_rects = malloc(nrects * sizeof(*dest_rects));
1064 if (!dest_rects)
1065 return;
1066
1067 for (i = 0; i < nrects; i++) {
1068 dest_rects[i] = weston_surface_to_buffer_rect(surface,
1069 src_rects[i]);
1070 }
1071
1072 pixman_region32_fini(buffer_region);
1073 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
1074 free(dest_rects);
1075}
1076
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +02001077WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001078weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001079 struct weston_plane *plane)
1080{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001081 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001082 return;
1083
Jason Ekstranda7af7042013-10-12 22:38:11 -05001084 weston_view_damage_below(view);
1085 view->plane = plane;
1086 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001087}
1088
Pekka Paalanen51723d52015-02-17 13:10:01 +02001089/** Inflict damage on the plane where the view is visible.
1090 *
1091 * \param view The view that causes the damage.
1092 *
1093 * If the view is currently on a plane (including the primary plane),
1094 * take the view's boundingbox, subtract all the opaque views that cover it,
1095 * and add the remaining region as damage to the plane. This corresponds
1096 * to the damage inflicted to the plane if this view disappeared.
1097 *
1098 * A repaint is scheduled for this view.
1099 *
1100 * The region of all opaque views covering this view is stored in
1101 * weston_view::clip and updated by view_accumulate_damage() during
1102 * weston_output_repaint(). Specifically, that region matches the
1103 * scenegraph as it was last painted.
1104 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001105WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001106weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001107{
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001108 pixman_region32_t damage;
limin.tian230ec582024-07-30 09:35:17 +00001109 if (!view->surface->is_video_surface && !(view->alpha == 0.0)) {
1110 pixman_region32_init(&damage);
1111 pixman_region32_subtract(&damage, &view->transform.boundingbox,
1112 &view->clip);
1113 if (view->plane)
1114 pixman_region32_union(&view->plane->damage,
1115 &view->plane->damage, &damage);
1116 pixman_region32_fini(&damage);
1117 }
Kristian Høgsberga3a784a2013-11-13 21:33:43 -08001118 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001119}
1120
Pekka Paalanen01e00682017-03-24 16:21:06 +02001121/** Send wl_surface.enter/leave events
1122 *
1123 * \param surface The surface.
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03001124 * \param head A head of the entered/left output.
Pekka Paalanen01e00682017-03-24 16:21:06 +02001125 * \param enter True if entered.
Marius Vlada2dace22019-06-12 16:05:44 +03001126 * \param leave True if left.
Pekka Paalanen01e00682017-03-24 16:21:06 +02001127 *
1128 * Send the enter/leave events for all protocol objects bound to the given
1129 * output by the client owning the surface.
1130 */
1131static void
1132weston_surface_send_enter_leave(struct weston_surface *surface,
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03001133 struct weston_head *head,
Pekka Paalanen01e00682017-03-24 16:21:06 +02001134 bool enter,
1135 bool leave)
1136{
1137 struct wl_resource *wloutput;
1138 struct wl_client *client;
1139
1140 assert(enter != leave);
1141
1142 client = wl_resource_get_client(surface->resource);
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03001143 wl_resource_for_each(wloutput, &head->resource_list) {
Pekka Paalanen01e00682017-03-24 16:21:06 +02001144 if (wl_resource_get_client(wloutput) != client)
1145 continue;
1146
1147 if (enter)
1148 wl_surface_send_enter(surface->resource, wloutput);
1149 if (leave)
1150 wl_surface_send_leave(surface->resource, wloutput);
1151 }
1152}
1153
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301154static void
1155weston_surface_compute_protection(struct protected_surface *psurface)
1156{
1157 enum weston_hdcp_protection min_protection;
1158 bool min_protection_valid = false;
1159 struct weston_surface *surface = psurface->surface;
1160 struct weston_output *output;
1161
1162 wl_list_for_each(output, &surface->compositor->output_list, link)
1163 if (surface->output_mask & (1u << output->id)) {
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05301164 /*
1165 * If the content-protection is enabled with protection
1166 * mode as RELAXED for a surface, and if
1167 * content-recording features like: screen-shooter,
1168 * recorder, screen-sharing, etc are on, then notify the
1169 * client, that the protection is disabled.
1170 *
1171 * Note: If the protection mode is ENFORCED then there
1172 * is no need to bother the client as the renderer takes
1173 * care of censoring the visibility of the protected
1174 * content.
1175 */
1176
1177 if (output->disable_planes > 0 &&
1178 surface->protection_mode == WESTON_SURFACE_PROTECTION_MODE_RELAXED) {
1179 min_protection = WESTON_HDCP_DISABLE;
1180 min_protection_valid = true;
1181 break;
1182 }
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301183 if (!min_protection_valid) {
1184 min_protection = output->current_protection;
1185 min_protection_valid = true;
1186 }
1187 if (output->current_protection < min_protection)
1188 min_protection = output->current_protection;
1189 }
1190 if (!min_protection_valid)
1191 min_protection = WESTON_HDCP_DISABLE;
1192
1193 surface->current_protection = min_protection;
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05301194
1195 weston_protected_surface_send_event(psurface, surface->current_protection);
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301196}
1197
1198static void
1199notify_surface_protection_change(void *data)
1200{
1201 struct weston_compositor *compositor = data;
1202 struct content_protection *cp;
1203 struct protected_surface *psurface;
1204
1205 cp = compositor->content_protection;
1206 cp->surface_protection_update = NULL;
1207
1208 /* Notify the clients, whose surfaces are changed */
1209 wl_list_for_each(psurface, &cp->protected_list, link)
1210 if (psurface && psurface->surface)
1211 weston_surface_compute_protection(psurface);
1212}
1213
Bryce Harrington3f650b82015-12-23 11:01:58 -08001214/**
Ankit Nautiyalfaa5ab42019-07-08 13:23:24 +05301215 * \param compositor weston_compositor
1216 *
1217 * Schedule an idle task to notify surface about the update in protection,
1218 * if not already scheduled.
1219 */
1220static void
1221weston_schedule_surface_protection_update(struct weston_compositor *compositor)
1222{
1223 struct content_protection *cp = compositor->content_protection;
1224 struct wl_event_loop *loop;
1225
1226 if (!cp || cp->surface_protection_update)
1227 return;
1228 loop = wl_display_get_event_loop(compositor->wl_display);
1229 cp->surface_protection_update = wl_event_loop_add_idle(loop,
1230 notify_surface_protection_change,
1231 compositor);
1232}
1233
1234/**
Bryce Harrington3f650b82015-12-23 11:01:58 -08001235 * \param es The surface
1236 * \param mask The new set of outputs for the surface
1237 *
1238 * Sets the surface's set of outputs to the ones specified by
1239 * the new output mask provided. Identifies the outputs that
1240 * have changed, the posts enter and leave events for these
1241 * outputs as appropriate.
1242 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001243static void
1244weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
1245{
1246 uint32_t different = es->output_mask ^ mask;
1247 uint32_t entered = mask & different;
1248 uint32_t left = es->output_mask & different;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001249 uint32_t output_bit;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001250 struct weston_output *output;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001251 struct weston_head *head;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001252
1253 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001254 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001255 return;
1256 if (different == 0)
1257 return;
1258
1259 wl_list_for_each(output, &es->compositor->output_list, link) {
Pekka Paalanen01e00682017-03-24 16:21:06 +02001260 output_bit = 1u << output->id;
1261 if (!(output_bit & different))
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001262 continue;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001263
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001264 wl_list_for_each(head, &output->head_list, output_link) {
1265 weston_surface_send_enter_leave(es, head,
1266 output_bit & entered,
1267 output_bit & left);
1268 }
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001269 }
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301270 /*
1271 * Change in surfaces' output mask might trigger a change in its
1272 * protection.
1273 */
Ankit Nautiyalfaa5ab42019-07-08 13:23:24 +05301274 weston_schedule_surface_protection_update(es->compositor);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001275}
1276
Semi Malinene7a52fb2018-04-26 11:08:10 +02001277static void
1278notify_view_output_destroy(struct wl_listener *listener, void *data)
1279{
1280 struct weston_view *view =
1281 container_of(listener,
1282 struct weston_view, output_destroy_listener);
1283
1284 view->output = NULL;
1285 view->output_destroy_listener.notify = NULL;
1286}
1287
1288/** Set the primary output of the view
1289 *
1290 * \param view The view whose primary output to set
1291 * \param output The new primary output for the view
1292 *
1293 * Set \a output to be the primary output of the \a view.
1294 *
1295 * Notice that the assignment may be temporary; the primary output could be
1296 * automatically changed. Hence, one cannot rely on the value persisting.
1297 *
1298 * Passing NULL as /a output will set the primary output to NULL.
1299 */
1300WL_EXPORT void
1301weston_view_set_output(struct weston_view *view, struct weston_output *output)
1302{
1303 if (view->output_destroy_listener.notify) {
1304 wl_list_remove(&view->output_destroy_listener.link);
1305 view->output_destroy_listener.notify = NULL;
1306 }
1307 view->output = output;
1308 if (output) {
1309 view->output_destroy_listener.notify =
1310 notify_view_output_destroy;
1311 wl_signal_add(&output->destroy_signal,
1312 &view->output_destroy_listener);
1313 }
1314}
1315
Bryce Harrington3f650b82015-12-23 11:01:58 -08001316/** Recalculate which output(s) the surface has views displayed on
1317 *
1318 * \param es The surface to remap to outputs
1319 *
1320 * Finds the output that is showing the largest amount of one
1321 * of the surface's various views. This output becomes the
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001322 * surface's primary output for vsync and frame callback purposes.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001323 *
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001324 * Also notes all outputs of all of the surface's views
Bryce Harrington3f650b82015-12-23 11:01:58 -08001325 * in the output_mask for the surface.
1326 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001327static void
1328weston_surface_assign_output(struct weston_surface *es)
1329{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001330 struct weston_output *new_output;
1331 struct weston_view *view;
1332 pixman_region32_t region;
1333 uint32_t max, area, mask;
1334 pixman_box32_t *e;
1335
1336 new_output = NULL;
1337 max = 0;
1338 mask = 0;
1339 pixman_region32_init(&region);
1340 wl_list_for_each(view, &es->views, surface_link) {
1341 if (!view->output)
1342 continue;
1343
1344 pixman_region32_intersect(&region, &view->transform.boundingbox,
1345 &view->output->region);
1346
1347 e = pixman_region32_extents(&region);
1348 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1349
1350 mask |= view->output_mask;
1351
1352 if (area >= max) {
1353 new_output = view->output;
1354 max = area;
1355 }
1356 }
1357 pixman_region32_fini(&region);
1358
1359 es->output = new_output;
1360 weston_surface_update_output_mask(es, mask);
1361}
1362
Bryce Harrington3f650b82015-12-23 11:01:58 -08001363/** Recalculate which output(s) the view is displayed on
1364 *
1365 * \param ev The view to remap to outputs
1366 *
1367 * Identifies the set of outputs that the view is visible on,
1368 * noting them into the output_mask. The output that the view
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001369 * is most visible on is set as the view's primary output.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001370 *
1371 * Also does the same for the view's surface. See
1372 * weston_surface_assign_output().
1373 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001374static void
1375weston_view_assign_output(struct weston_view *ev)
1376{
1377 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001378 struct weston_output *output, *new_output;
1379 pixman_region32_t region;
1380 uint32_t max, area, mask;
1381 pixman_box32_t *e;
1382
1383 new_output = NULL;
1384 max = 0;
1385 mask = 0;
1386 pixman_region32_init(&region);
1387 wl_list_for_each(output, &ec->output_list, link) {
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03001388 if (output->destroying)
1389 continue;
1390
Jason Ekstranda7af7042013-10-12 22:38:11 -05001391 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001392 &output->region);
1393
1394 e = pixman_region32_extents(&region);
1395 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1396
1397 if (area > 0)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001398 mask |= 1u << output->id;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001399
1400 if (area >= max) {
1401 new_output = output;
1402 max = area;
1403 }
1404 }
1405 pixman_region32_fini(&region);
1406
Semi Malinene7a52fb2018-04-26 11:08:10 +02001407 weston_view_set_output(ev, new_output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001408 ev->output_mask = mask;
1409
1410 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001411}
1412
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001413static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001414weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1415 int from_x, int from_y, int *to_x, int *to_y)
1416{
1417 float x, y;
1418
1419 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1420 weston_view_from_global_float(to, x, y, &x, &y);
1421
1422 *to_x = round(x);
1423 *to_y = round(y);
1424}
1425
1426static void
1427weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1428{
1429 pixman_box32_t *a;
1430 pixman_box32_t b;
1431
1432 a = pixman_region32_extents(&from->geometry.scissor);
1433
1434 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1435 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1436
1437 pixman_region32_fini(&to->geometry.scissor);
1438 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1439}
1440
1441static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001442view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001443 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001444{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001445 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1446 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001447 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001448 { inbox->x1, inbox->y1 },
1449 { inbox->x1, inbox->y2 },
1450 { inbox->x2, inbox->y1 },
1451 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001452 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001453 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001454 int i;
1455
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001456 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001457 /* avoid rounding empty bbox to 1x1 */
1458 pixman_region32_init(bbox);
1459 return;
1460 }
1461
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001462 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001463 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001464 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001465 if (x < min_x)
1466 min_x = x;
1467 if (x > max_x)
1468 max_x = x;
1469 if (y < min_y)
1470 min_y = y;
1471 if (y > max_y)
1472 max_y = y;
1473 }
1474
Pekka Paalanen219b9822012-02-08 15:38:37 +02001475 int_x = floorf(min_x);
1476 int_y = floorf(min_y);
1477 pixman_region32_init_rect(bbox, int_x, int_y,
1478 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001479}
1480
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001481static void
Michael Olbrichcb04f982020-06-08 10:22:32 +02001482weston_view_update_transform_scissor(struct weston_view *view,
1483 pixman_region32_t *region)
1484{
1485 struct weston_view *parent = view->geometry.parent;
1486
1487 if (parent) {
1488 if (parent->geometry.scissor_enabled) {
1489 view->geometry.scissor_enabled = true;
1490 weston_view_transfer_scissor(parent, view);
1491 } else {
1492 view->geometry.scissor_enabled = false;
1493 }
1494 }
1495
1496 if (view->geometry.scissor_enabled)
1497 pixman_region32_intersect(region, region,
1498 &view->geometry.scissor);
1499}
1500static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001501weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001502{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001503 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001504
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001505 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001506 view->geometry.x = roundf(view->geometry.x);
1507 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001508
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001509 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001510 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1511 view->transform.position.matrix.d[12] = view->geometry.x;
1512 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001513
Jason Ekstranda7af7042013-10-12 22:38:11 -05001514 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001515
Jason Ekstranda7af7042013-10-12 22:38:11 -05001516 view->transform.inverse = view->transform.position.matrix;
1517 view->transform.inverse.d[12] = -view->geometry.x;
1518 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001519
Jason Ekstranda7af7042013-10-12 22:38:11 -05001520 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001521 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001522 view->surface->width,
1523 view->surface->height);
Michael Olbrichcb04f982020-06-08 10:22:32 +02001524
1525 weston_view_update_transform_scissor(view, &view->transform.boundingbox);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001526
1527 pixman_region32_translate(&view->transform.boundingbox,
1528 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001529
Jason Ekstranda7af7042013-10-12 22:38:11 -05001530 if (view->alpha == 1.0) {
1531 pixman_region32_copy(&view->transform.opaque,
1532 &view->surface->opaque);
Derek Foreman713f0232022-01-12 16:30:26 -06001533 if (view->geometry.scissor_enabled)
1534 pixman_region32_intersect(&view->transform.opaque,
1535 &view->transform.opaque,
1536 &view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001537 pixman_region32_translate(&view->transform.opaque,
1538 view->geometry.x,
1539 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001540 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001541}
1542
1543static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001544weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001545{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001546 struct weston_view *parent = view->geometry.parent;
1547 struct weston_matrix *matrix = &view->transform.matrix;
1548 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001549 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001550 pixman_region32_t surfregion;
1551 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001552
Jason Ekstranda7af7042013-10-12 22:38:11 -05001553 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001554
1555 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001556 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1557 view->transform.position.matrix.d[12] = view->geometry.x;
1558 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001559
1560 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001561 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001562 weston_matrix_multiply(matrix, &tform->matrix);
1563
Pekka Paalanen483243f2013-03-08 14:56:50 +02001564 if (parent)
1565 weston_matrix_multiply(matrix, &parent->transform.matrix);
1566
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001567 if (weston_matrix_invert(inverse, matrix) < 0) {
1568 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001569 weston_log("error: weston_view %p"
1570 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001571 return -1;
1572 }
1573
Pekka Paalanen380adf52015-02-16 14:39:11 +02001574 pixman_region32_init_rect(&surfregion, 0, 0,
1575 view->surface->width, view->surface->height);
Michael Olbrichcb04f982020-06-08 10:22:32 +02001576
1577 weston_view_update_transform_scissor(view, &surfregion);
1578
Pekka Paalanen380adf52015-02-16 14:39:11 +02001579 surfbox = pixman_region32_extents(&surfregion);
1580
1581 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1582 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001583
Michael Olbrich0e4f0972020-08-19 11:31:03 +02001584 if (view->alpha == 1.0 &&
1585 matrix->type == WESTON_MATRIX_TRANSFORM_TRANSLATE) {
1586 pixman_region32_copy(&view->transform.opaque,
1587 &view->surface->opaque);
Derek Foreman713f0232022-01-12 16:30:26 -06001588 if (view->geometry.scissor_enabled)
1589 pixman_region32_intersect(&view->transform.opaque,
1590 &view->transform.opaque,
1591 &view->geometry.scissor);
Michael Olbrich0e4f0972020-08-19 11:31:03 +02001592 pixman_region32_translate(&view->transform.opaque,
1593 matrix->d[12],
1594 matrix->d[13]);
1595 }
1596
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001597 return 0;
1598}
1599
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001600static struct weston_layer *
1601get_view_layer(struct weston_view *view)
1602{
1603 if (view->parent_view)
1604 return get_view_layer(view->parent_view);
1605 return view->layer_link.layer;
1606}
1607
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001608WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001609weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001610{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001611 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001612 struct weston_layer *layer;
1613 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001614
Jason Ekstranda7af7042013-10-12 22:38:11 -05001615 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001616 return;
1617
Pekka Paalanen483243f2013-03-08 14:56:50 +02001618 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001619 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001620
Jason Ekstranda7af7042013-10-12 22:38:11 -05001621 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001622
Jason Ekstranda7af7042013-10-12 22:38:11 -05001623 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001624
Jason Ekstranda7af7042013-10-12 22:38:11 -05001625 pixman_region32_fini(&view->transform.boundingbox);
1626 pixman_region32_fini(&view->transform.opaque);
1627 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001628
Pekka Paalanencd403622012-01-25 13:37:39 +02001629 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001630 if (view->geometry.transformation_list.next ==
1631 &view->transform.position.link &&
1632 view->geometry.transformation_list.prev ==
1633 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001634 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001635 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001636 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001637 if (weston_view_update_transform_enable(view) < 0)
1638 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001639 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001640
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001641 layer = get_view_layer(view);
1642 if (layer) {
1643 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001644 pixman_region32_intersect(&view->transform.boundingbox,
1645 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001646 pixman_region32_intersect(&view->transform.opaque,
1647 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001648 pixman_region32_fini(&mask);
1649 }
1650
Jason Ekstranda7af7042013-10-12 22:38:11 -05001651 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001652
Jason Ekstranda7af7042013-10-12 22:38:11 -05001653 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001654
Jason Ekstranda7af7042013-10-12 22:38:11 -05001655 wl_signal_emit(&view->surface->compositor->transform_signal,
1656 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001657}
1658
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001659WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001660weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001661{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001662 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001663
1664 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001665 * The invariant: if view->geometry.dirty, then all views
1666 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001667 * Corollary: if not parent->geometry.dirty, then all ancestors
1668 * are not dirty.
1669 */
1670
Jason Ekstranda7af7042013-10-12 22:38:11 -05001671 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001672 return;
1673
Jason Ekstranda7af7042013-10-12 22:38:11 -05001674 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001675
Jason Ekstranda7af7042013-10-12 22:38:11 -05001676 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001677 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001678 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001679}
1680
1681WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001682weston_view_to_global_fixed(struct weston_view *view,
1683 wl_fixed_t vx, wl_fixed_t vy,
1684 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001685{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001686 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001687
Jason Ekstranda7af7042013-10-12 22:38:11 -05001688 weston_view_to_global_float(view,
1689 wl_fixed_to_double(vx),
1690 wl_fixed_to_double(vy),
1691 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001692 *x = wl_fixed_from_double(xf);
1693 *y = wl_fixed_from_double(yf);
1694}
1695
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001696WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001697weston_view_from_global_float(struct weston_view *view,
1698 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001699{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001700 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001701 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1702
Jason Ekstranda7af7042013-10-12 22:38:11 -05001703 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001704
1705 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001706 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001707 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001708 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001709 *vx = 0;
1710 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001711 return;
1712 }
1713
Jason Ekstranda7af7042013-10-12 22:38:11 -05001714 *vx = v.f[0] / v.f[3];
1715 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001716 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001717 *vx = x - view->geometry.x;
1718 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001719 }
1720}
1721
1722WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001723weston_view_from_global_fixed(struct weston_view *view,
1724 wl_fixed_t x, wl_fixed_t y,
1725 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001726{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001727 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001728
Jason Ekstranda7af7042013-10-12 22:38:11 -05001729 weston_view_from_global_float(view,
1730 wl_fixed_to_double(x),
1731 wl_fixed_to_double(y),
1732 &vxf, &vyf);
1733 *vx = wl_fixed_from_double(vxf);
1734 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001735}
1736
1737WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001738weston_view_from_global(struct weston_view *view,
1739 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001740{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001741 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001742
Jason Ekstranda7af7042013-10-12 22:38:11 -05001743 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1744 *vx = floorf(vxf);
1745 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001746}
1747
Bryce Harrington3f650b82015-12-23 11:01:58 -08001748/**
1749 * \param surface The surface to be repainted
1750 *
1751 * Marks the output(s) that the surface is shown on as needing to be
1752 * repainted. See weston_output_schedule_repaint().
1753 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001754WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001755weston_surface_schedule_repaint(struct weston_surface *surface)
1756{
1757 struct weston_output *output;
1758
1759 wl_list_for_each(output, &surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001760 if (surface->output_mask & (1u << output->id))
Kristian Høgsberg98238702012-08-03 16:29:12 -04001761 weston_output_schedule_repaint(output);
1762}
1763
Bryce Harrington3f650b82015-12-23 11:01:58 -08001764/**
1765 * \param view The view to be repainted
1766 *
1767 * Marks the output(s) that the view is shown on as needing to be
1768 * repainted. See weston_output_schedule_repaint().
1769 */
Kristian Høgsberg98238702012-08-03 16:29:12 -04001770WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001771weston_view_schedule_repaint(struct weston_view *view)
1772{
1773 struct weston_output *output;
1774
1775 wl_list_for_each(output, &view->surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001776 if (view->output_mask & (1u << output->id))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001777 weston_output_schedule_repaint(output);
1778}
1779
Pekka Paalanene508ce62015-02-19 13:59:55 +02001780/**
1781 * XXX: This function does it the wrong way.
1782 * surface->damage is the damage from the client, and causes
1783 * surface_flush_damage() to copy pixels. No window management action can
1784 * cause damage to the client-provided content, warranting re-upload!
1785 *
1786 * Instead of surface->damage, this function should record the damage
1787 * with all the views for this surface to avoid extraneous texture
1788 * uploads.
1789 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001790WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001791weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001792{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001793 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001794 0, 0, surface->width,
1795 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001796
Kristian Høgsberg98238702012-08-03 16:29:12 -04001797 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001798}
1799
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001800WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001801weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001802{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001803 if (view->geometry.x == x && view->geometry.y == y)
1804 return;
1805
Jason Ekstranda7af7042013-10-12 22:38:11 -05001806 view->geometry.x = x;
1807 view->geometry.y = y;
1808 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001809}
1810
Pekka Paalanen483243f2013-03-08 14:56:50 +02001811static void
1812transform_parent_handle_parent_destroy(struct wl_listener *listener,
1813 void *data)
1814{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001815 struct weston_view *view =
1816 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001817 geometry.parent_destroy_listener);
1818
Jason Ekstranda7af7042013-10-12 22:38:11 -05001819 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001820}
1821
1822WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001823weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001824 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001825{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001826 if (view->geometry.parent) {
1827 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1828 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001829
1830 if (!parent)
1831 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001832 }
1833
Jason Ekstranda7af7042013-10-12 22:38:11 -05001834 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001835
Jason Ekstranda7af7042013-10-12 22:38:11 -05001836 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001837 transform_parent_handle_parent_destroy;
1838 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001839 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001840 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001841 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001842 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001843 }
1844
Jason Ekstranda7af7042013-10-12 22:38:11 -05001845 weston_view_geometry_dirty(view);
1846}
1847
Pekka Paalanen380adf52015-02-16 14:39:11 +02001848/** Set a clip mask rectangle on a view
1849 *
1850 * \param view The view to set the clip mask on.
1851 * \param x Top-left corner X coordinate of the clip rectangle.
1852 * \param y Top-left corner Y coordinate of the clip rectangle.
1853 * \param width Width of the clip rectangle, non-negative.
1854 * \param height Height of the clip rectangle, non-negative.
1855 *
1856 * A shell may set a clip mask rectangle on a view. Everything outside
1857 * the rectangle is cut away for input and output purposes: it is
1858 * not drawn and cannot be hit by hit-test based input like pointer
1859 * motion or touch-downs. Everything inside the rectangle will behave
1860 * normally. Clients are unaware of clipping.
1861 *
Yong Bakos4c72e292016-04-28 11:59:10 -05001862 * The rectangle is set in surface-local coordinates. Setting a clip
Pekka Paalanen380adf52015-02-16 14:39:11 +02001863 * mask rectangle does not affect the view position, the view is positioned
1864 * as it would be without a clip. The clip also does not change
1865 * weston_surface::width,height.
1866 *
1867 * The clip mask rectangle is part of transformation inheritance
1868 * (weston_view_set_transform_parent()). A clip set in the root of the
1869 * transformation inheritance tree will affect all views in the tree.
1870 * A clip can be set only on the root view. Attempting to set a clip
1871 * on view that has a transformation parent will fail. Assigning a parent
1872 * to a view that has a clip set will cause the clip to be forgotten.
1873 *
1874 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1875 * on the additional transformations in the child views. These transformations
1876 * may not rotate the coordinate axes, i.e., only translation and scaling
1877 * are allowed. Violating this restriction causes the clipping to malfunction.
1878 * Furthermore, using scaling may cause rounding errors in child clipping.
1879 *
1880 * The clip mask rectangle is not automatically adjusted based on
1881 * wl_surface.attach dx and dy arguments.
1882 *
1883 * A clip mask rectangle can be set only if the compositor capability
1884 * WESTON_CAP_VIEW_CLIP_MASK is present.
1885 *
1886 * This function sets the clip mask rectangle and schedules a repaint for
1887 * the view.
1888 */
1889WL_EXPORT void
1890weston_view_set_mask(struct weston_view *view,
1891 int x, int y, int width, int height)
1892{
1893 struct weston_compositor *compositor = view->surface->compositor;
1894
1895 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1896 weston_log("%s not allowed without capability!\n", __func__);
1897 return;
1898 }
1899
1900 if (view->geometry.parent) {
1901 weston_log("view %p has a parent, clip forbidden!\n", view);
1902 return;
1903 }
1904
1905 if (width < 0 || height < 0) {
1906 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1907 x, y, width, height);
1908 return;
1909 }
1910
1911 pixman_region32_fini(&view->geometry.scissor);
1912 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1913 view->geometry.scissor_enabled = true;
1914 weston_view_geometry_dirty(view);
1915 weston_view_schedule_repaint(view);
1916}
1917
1918/** Remove the clip mask from a view
1919 *
1920 * \param view The view to remove the clip mask from.
1921 *
1922 * Removed the clip mask rectangle and schedules a repaint.
1923 *
1924 * \sa weston_view_set_mask
1925 */
1926WL_EXPORT void
1927weston_view_set_mask_infinite(struct weston_view *view)
1928{
1929 view->geometry.scissor_enabled = false;
1930 weston_view_geometry_dirty(view);
1931 weston_view_schedule_repaint(view);
1932}
1933
Armin Krezović0da12b82016-06-30 06:04:33 +02001934/* Check if view should be displayed
1935 *
1936 * The indicator is set manually when assigning
1937 * a view to a surface.
1938 *
1939 * This needs reworking. See the thread starting at:
1940 *
1941 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1942 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001943WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001944weston_view_is_mapped(struct weston_view *view)
1945{
Armin Krezović0da12b82016-06-30 06:04:33 +02001946 return view->is_mapped;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001947}
1948
Philipp Zabel70decd52018-09-03 20:11:15 +02001949/* Check if view is opaque in specified region
1950 *
1951 * \param view The view to check for opacity.
1952 * \param region The region to check for opacity, in view coordinates.
1953 *
1954 * Returns true if the view is opaque in the specified region, because view
1955 * alpha is 1.0 and either the opaque region set by the client contains the
1956 * specified region, or the buffer pixel format or solid color is opaque.
1957 */
1958WL_EXPORT bool
1959weston_view_is_opaque(struct weston_view *ev, pixman_region32_t *region)
1960{
1961 pixman_region32_t r;
1962 bool ret = false;
1963
1964 if (ev->alpha < 1.0)
1965 return false;
1966
1967 if (ev->surface->is_opaque)
1968 return true;
1969
Marius Vlad67b382c2020-08-21 13:57:54 +03001970 if (ev->transform.dirty)
Philipp Zabel70decd52018-09-03 20:11:15 +02001971 return false;
Philipp Zabel70decd52018-09-03 20:11:15 +02001972
1973 pixman_region32_init(&r);
1974 pixman_region32_subtract(&r, region, &ev->transform.opaque);
1975
1976 if (!pixman_region32_not_empty(&r))
1977 ret = true;
1978
1979 pixman_region32_fini(&r);
1980
1981 return ret;
1982}
1983
Marius Vlad5f6bee42019-09-11 16:41:04 +03001984/** Check if the view has a valid buffer available
1985 *
1986 * @param ev The view to check if it has a valid buffer.
1987 *
1988 * Returns true if the view has a valid buffer or false otherwise.
1989 */
1990WL_EXPORT bool
1991weston_view_has_valid_buffer(struct weston_view *ev)
1992{
1993 return ev->surface->buffer_ref.buffer != NULL;
1994}
1995
Marius Vlad47e3d1e2019-09-11 16:53:08 +03001996/** Check if the view matches the entire output
1997 *
1998 * @param ev The view to check.
1999 * @param output The output to check against.
2000 *
2001 * Returns true if the view does indeed matches the entire output.
2002 */
2003WL_EXPORT bool
2004weston_view_matches_output_entirely(struct weston_view *ev,
2005 struct weston_output *output)
2006{
2007 pixman_box32_t *extents =
2008 pixman_region32_extents(&ev->transform.boundingbox);
2009
2010 if (extents->x1 != output->x ||
2011 extents->y1 != output->y ||
2012 extents->x2 != output->x + output->width ||
2013 extents->y2 != output->y + output->height)
2014 return false;
2015
2016 return true;
2017}
2018
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002019/** Find paint node for the given view and output
2020 */
2021WL_EXPORT struct weston_paint_node *
2022weston_view_find_paint_node(struct weston_view *view,
2023 struct weston_output *output)
2024{
2025 struct weston_paint_node *pnode;
2026
2027 wl_list_for_each(pnode, &view->paint_node_list, view_link) {
2028 assert(pnode->surface == view->surface);
2029 if (pnode->output == output)
2030 return pnode;
2031 }
2032
2033 return NULL;
2034}
2035
Armin Krezović0da12b82016-06-30 06:04:33 +02002036/* Check if a surface has a view assigned to it
2037 *
2038 * The indicator is set manually when mapping
2039 * a surface and creating a view for it.
2040 *
2041 * This needs to go. See the thread starting at:
2042 *
2043 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
2044 *
2045 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05002046WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03002047weston_surface_is_mapped(struct weston_surface *surface)
2048{
Armin Krezović0da12b82016-06-30 06:04:33 +02002049 return surface->is_mapped;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03002050}
2051
Pekka Paalanenda75ee12013-11-26 18:19:43 +01002052static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06002053surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002054{
2055 struct weston_view *view;
2056
2057 if (surface->width == width && surface->height == height)
2058 return;
2059
2060 surface->width = width;
2061 surface->height = height;
2062
2063 wl_list_for_each(view, &surface->views, surface_link)
2064 weston_view_geometry_dirty(view);
2065}
2066
Jason Ekstrand5c11a332013-12-04 20:32:03 -06002067WL_EXPORT void
2068weston_surface_set_size(struct weston_surface *surface,
2069 int32_t width, int32_t height)
2070{
2071 assert(!surface->resource);
2072 surface_set_size(surface, width, height);
2073}
2074
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02002075static int
2076fixed_round_up_to_int(wl_fixed_t f)
2077{
2078 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
2079}
2080
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002081static void
Pekka Paalanen59987fa2016-04-26 15:50:59 +03002082convert_size_by_transform_scale(int32_t *width_out, int32_t *height_out,
2083 int32_t width, int32_t height,
2084 uint32_t transform,
2085 int32_t scale)
2086{
2087 assert(scale > 0);
2088
2089 switch (transform) {
2090 case WL_OUTPUT_TRANSFORM_NORMAL:
2091 case WL_OUTPUT_TRANSFORM_180:
2092 case WL_OUTPUT_TRANSFORM_FLIPPED:
2093 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2094 *width_out = width / scale;
2095 *height_out = height / scale;
2096 break;
2097 case WL_OUTPUT_TRANSFORM_90:
2098 case WL_OUTPUT_TRANSFORM_270:
2099 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2100 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2101 *width_out = height / scale;
2102 *height_out = width / scale;
2103 break;
2104 default:
2105 assert(0 && "invalid transform");
2106 }
2107}
2108
2109static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002110weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002111{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002112 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01002113
2114 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02002115 surface->width_from_buffer = 0;
2116 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01002117 return;
2118 }
2119
Pekka Paalanen59987fa2016-04-26 15:50:59 +03002120 convert_size_by_transform_scale(&surface->width_from_buffer,
2121 &surface->height_from_buffer,
2122 surface->buffer_ref.buffer->width,
2123 surface->buffer_ref.buffer->height,
2124 vp->buffer.transform,
2125 vp->buffer.scale);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002126}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02002127
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002128static void
2129weston_surface_update_size(struct weston_surface *surface)
2130{
2131 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
2132 int32_t width, height;
2133
2134 width = surface->width_from_buffer;
2135 height = surface->height_from_buffer;
2136
2137 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02002138 surface_set_size(surface,
2139 vp->surface.width, vp->surface.height);
2140 return;
2141 }
2142
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002143 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03002144 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
2145 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
2146
2147 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02002148 return;
2149 }
2150
Jason Ekstrand5c11a332013-12-04 20:32:03 -06002151 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002152}
2153
Marius Vlad9fdda7f2019-06-11 16:08:55 +03002154/** weston_compositor_get_time
2155 * \ingroup compositor
2156 */
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02002157WL_EXPORT void
2158weston_compositor_get_time(struct timespec *time)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05002159{
limin.tianc616dad2024-07-15 11:35:38 +00002160 clock_gettime(CLOCK_MONOTONIC, time);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05002161}
2162
Marius Vlad9fdda7f2019-06-11 16:08:55 +03002163/** weston_compositor_pick_view
2164 * \ingroup compositor
2165 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002166WL_EXPORT struct weston_view *
2167weston_compositor_pick_view(struct weston_compositor *compositor,
2168 wl_fixed_t x, wl_fixed_t y,
2169 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02002170{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002171 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02002172 wl_fixed_t view_x, view_y;
2173 int view_ix, view_iy;
2174 int ix = wl_fixed_to_int(x);
2175 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02002176
Pekka Paalanen6551c092021-05-03 16:09:45 +03002177 /* Can't use paint node list: occlusion by input regions, not opaque. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002178 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02002179 if (!pixman_region32_contains_point(
2180 &view->transform.boundingbox, ix, iy, NULL))
2181 continue;
2182
2183 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
2184 view_ix = wl_fixed_to_int(view_x);
2185 view_iy = wl_fixed_to_int(view_y);
2186
2187 if (!pixman_region32_contains_point(&view->surface->input,
2188 view_ix, view_iy, NULL))
2189 continue;
2190
Pekka Paalanen380adf52015-02-16 14:39:11 +02002191 if (view->geometry.scissor_enabled &&
2192 !pixman_region32_contains_point(&view->geometry.scissor,
2193 view_ix, view_iy, NULL))
2194 continue;
2195
Pekka Paalanenfc22a522015-02-18 15:08:29 +02002196 *vx = view_x;
2197 *vy = view_y;
2198 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02002199 }
2200
Derek Foremanf9318d12015-05-11 15:40:11 -05002201 *vx = wl_fixed_from_int(-1000000);
2202 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02002203 return NULL;
2204}
2205
2206static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002207weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002208{
Daniel Stone37816df2012-05-16 18:45:18 +01002209 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002210
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07002211 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002212 return;
2213
Daniel Stone37816df2012-05-16 18:45:18 +01002214 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04002215 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002216}
2217
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002218WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002219weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05002220{
Daniel Stone4dab5db2012-05-30 16:31:53 +01002221 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002222
Jason Ekstranda7af7042013-10-12 22:38:11 -05002223 if (!weston_view_is_mapped(view))
2224 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002225
Jason Ekstranda7af7042013-10-12 22:38:11 -05002226 weston_view_damage_below(view);
Semi Malinene7a52fb2018-04-26 11:08:10 +02002227 weston_view_set_output(view, NULL);
Xiong Zhang97116532013-10-23 13:58:31 +08002228 view->plane = NULL;
Armin Krezovićf8486c32016-06-30 06:04:28 +02002229 view->is_mapped = false;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002230 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002231 wl_list_remove(&view->link);
2232 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002233 view->output_mask = 0;
2234 weston_surface_assign_output(view->surface);
2235
2236 if (weston_surface_is_mapped(view->surface))
2237 return;
2238
2239 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002240 struct weston_touch *touch = weston_seat_get_touch(seat);
2241 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2242 struct weston_keyboard *keyboard =
2243 weston_seat_get_keyboard(seat);
2244
2245 if (keyboard && keyboard->focus == view->surface)
2246 weston_keyboard_set_focus(keyboard, NULL);
2247 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05002248 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05002249 if (touch && touch->focus == view)
2250 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01002251 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002252}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002253
Jason Ekstranda7af7042013-10-12 22:38:11 -05002254WL_EXPORT void
2255weston_surface_unmap(struct weston_surface *surface)
2256{
2257 struct weston_view *view;
2258
Armin Krezovićf8486c32016-06-30 06:04:28 +02002259 surface->is_mapped = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002260 wl_list_for_each(view, &surface->views, surface_link)
2261 weston_view_unmap(view);
2262 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05002263}
2264
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002265static void
2266weston_surface_reset_pending_buffer(struct weston_surface *surface)
2267{
Jason Ekstrand7b982072014-05-20 14:33:03 -05002268 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002269 surface->pending.sx = 0;
2270 surface->pending.sy = 0;
2271 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002272 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002273}
2274
Jason Ekstranda7af7042013-10-12 22:38:11 -05002275WL_EXPORT void
2276weston_view_destroy(struct weston_view *view)
2277{
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002278 struct weston_paint_node *pnode, *pntmp;
Marius Vladf2452d62021-12-09 14:07:21 +02002279 weston_signal_emit_mutable(&view->destroy_signal, view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002280
2281 assert(wl_list_empty(&view->geometry.child_list));
2282
2283 if (weston_view_is_mapped(view)) {
2284 weston_view_unmap(view);
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002285 weston_compositor_build_view_list(view->surface->compositor,
2286 NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002287 }
2288
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002289 wl_list_for_each_safe(pnode, pntmp, &view->paint_node_list, view_link)
2290 weston_paint_node_destroy(pnode);
2291
Jason Ekstranda7af7042013-10-12 22:38:11 -05002292 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002293 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002294
2295 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02002296 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002297 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002298 pixman_region32_fini(&view->transform.opaque);
leng.fang32af9fc2024-06-13 11:22:15 +08002299#ifdef MESON_VIDEO_PLAN_SUPPORT
2300 pixman_region32_fini(&view->transform.transparent);
2301#endif
Jason Ekstranda7af7042013-10-12 22:38:11 -05002302
2303 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen944fae82018-05-22 13:15:58 +03002304 weston_view_set_output(view, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002305
Jason Ekstranda7af7042013-10-12 22:38:11 -05002306 wl_list_remove(&view->surface_link);
2307
2308 free(view);
2309}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002310
2311WL_EXPORT void
2312weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05002313{
limin.tiana0140a72024-10-12 07:28:49 +00002314 weston_log("\n weston_surface_destroy :%p output:%p name :%s \n", surface, surface->output, surface->name);
Vlad Zahorodniic2b97472021-07-29 11:16:51 +03002315 struct wl_resource *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002316 struct weston_view *ev, *nv;
Jonas Ådahld3414f22016-07-22 17:56:31 +08002317 struct weston_pointer_constraint *constraint, *next_constraint;
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002318 struct weston_paint_node *pnode, *pntmp;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04002319
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02002320 if (--surface->ref_count > 0)
2321 return;
2322
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03002323 assert(surface->resource == NULL);
2324
Alexandros Frantzisfa48c5e2020-07-09 17:31:34 +03002325 weston_signal_emit_mutable(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02002326
Pekka Paalanene67858b2013-04-25 13:57:42 +03002327 assert(wl_list_empty(&surface->subsurface_list_pending));
2328 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02002329
Leandro Ribeiro8eb84142021-01-18 19:36:48 -03002330 if (surface->dmabuf_feedback)
2331 weston_dmabuf_feedback_destroy(surface->dmabuf_feedback);
2332
Jason Ekstranda7af7042013-10-12 22:38:11 -05002333 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
2334 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002335
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002336 wl_list_for_each_safe(pnode, pntmp,
2337 &surface->paint_node_list, surface_link) {
2338 weston_paint_node_destroy(pnode);
2339 }
2340
Jason Ekstrand7b982072014-05-20 14:33:03 -05002341 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002342
Pekka Paalanende685b82012-12-04 15:58:12 +02002343 weston_buffer_reference(&surface->buffer_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002344 weston_buffer_release_reference(&surface->buffer_release_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04002345
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02002346 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002347 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002348 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02002349
Vlad Zahorodniic2b97472021-07-29 11:16:51 +03002350 wl_resource_for_each_safe(cb, next, &surface->frame_callback_list)
2351 wl_resource_destroy(cb);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002352
Pekka Paalanen133e4392014-09-23 22:08:46 -04002353 weston_presentation_feedback_discard_list(&surface->feedback_list);
2354
Jonas Ådahld3414f22016-07-22 17:56:31 +08002355 wl_list_for_each_safe(constraint, next_constraint,
2356 &surface->pointer_constraints,
2357 link)
2358 weston_pointer_constraint_destroy(constraint);
2359
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03002360 fd_clear(&surface->acquire_fence_fd);
2361
limin.tiana0140a72024-10-12 07:28:49 +00002362 weston_set_appstate_by_appname(surface->compositor, surface->name, 0);
leng.fang32af9fc2024-06-13 11:22:15 +08002363 weston_clear_video_surface_info(surface);
leng.fangdbaf6fa2024-06-20 19:31:04 +08002364 weston_surface_destroy_info_for_surface(surface);
limin.tian531dd232025-01-07 12:09:27 +00002365 weston_log("weston_surface_destroy :%p output:%p name :%s \n", surface, surface->output, surface->name);
leng.fangdbaf6fa2024-06-20 19:31:04 +08002366
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04002367 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05002368}
2369
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002370static void
2371destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08002372{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002373 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08002374
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03002375 assert(surface);
2376
Giulio Camuffo0d379742013-11-15 22:06:15 +01002377 /* Set the resource to NULL, since we don't want to leave a
2378 * dangling pointer if the surface was refcounted and survives
2379 * the weston_surface_destroy() call. */
2380 surface->resource = NULL;
Pekka Paalanen4826f872016-04-22 14:14:38 +03002381
2382 if (surface->viewport_resource)
2383 wl_resource_set_user_data(surface->viewport_resource, NULL);
2384
Alexandros Frantzis27d7c392018-10-19 12:14:11 +03002385 if (surface->synchronization_resource) {
2386 wl_resource_set_user_data(surface->synchronization_resource,
2387 NULL);
2388 }
2389
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002390 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08002391}
2392
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002393static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002394weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
2395{
2396 struct weston_buffer *buffer =
2397 container_of(listener, struct weston_buffer, destroy_listener);
2398
limin.tian531dd232025-01-07 12:09:27 +00002399 weston_log("weston_buffer_destroy_handler buffer:%p\n",buffer);
Marius Vladf2452d62021-12-09 14:07:21 +02002400 weston_signal_emit_mutable(&buffer->destroy_signal, buffer);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002401 free(buffer);
2402}
2403
Giulio Camuffoe058cd12013-12-12 14:14:29 +01002404WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002405weston_buffer_from_resource(struct wl_resource *resource)
2406{
2407 struct weston_buffer *buffer;
2408 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002409
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002410 listener = wl_resource_get_destroy_listener(resource,
2411 weston_buffer_destroy_handler);
2412
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002413 if (listener)
2414 return container_of(listener, struct weston_buffer,
2415 destroy_listener);
2416
2417 buffer = zalloc(sizeof *buffer);
2418 if (buffer == NULL)
2419 return NULL;
2420
2421 buffer->resource = resource;
2422 wl_signal_init(&buffer->destroy_signal);
2423 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04002424 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002425 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002426
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002427 return buffer;
2428}
2429
2430static void
Pekka Paalanende685b82012-12-04 15:58:12 +02002431weston_buffer_reference_handle_destroy(struct wl_listener *listener,
2432 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002433{
Pekka Paalanende685b82012-12-04 15:58:12 +02002434 struct weston_buffer_reference *ref =
2435 container_of(listener, struct weston_buffer_reference,
2436 destroy_listener);
2437
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002438 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02002439 ref->buffer = NULL;
2440}
2441
2442WL_EXPORT void
2443weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002444 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002445{
2446 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05002447 ref->buffer->busy_count--;
2448 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002449 assert(wl_resource_get_client(ref->buffer->resource));
leng.fang32af9fc2024-06-13 11:22:15 +08002450 buffer_send_release(ref);
Kristian Høgsberg20347802013-03-04 12:07:46 -05002451 }
Pekka Paalanende685b82012-12-04 15:58:12 +02002452 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002453 }
2454
Pekka Paalanende685b82012-12-04 15:58:12 +02002455 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002456 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002457 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02002458 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02002459 }
2460
Pekka Paalanende685b82012-12-04 15:58:12 +02002461 ref->buffer = buffer;
2462 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
2463}
2464
2465static void
Alexandros Frantzis67629672018-10-19 12:14:11 +03002466weston_buffer_release_reference_handle_destroy(struct wl_listener *listener,
2467 void *data)
2468{
2469 struct weston_buffer_release_reference *ref =
2470 container_of(listener, struct weston_buffer_release_reference,
2471 destroy_listener);
2472
2473 assert((struct wl_resource *)data == ref->buffer_release->resource);
2474 ref->buffer_release = NULL;
2475}
2476
2477static void
2478weston_buffer_release_destroy(struct weston_buffer_release *buffer_release)
2479{
2480 struct wl_resource *resource = buffer_release->resource;
2481 int release_fence_fd = buffer_release->fence_fd;
2482
2483 if (release_fence_fd >= 0) {
2484 zwp_linux_buffer_release_v1_send_fenced_release(
2485 resource, release_fence_fd);
2486 } else {
2487 zwp_linux_buffer_release_v1_send_immediate_release(
2488 resource);
2489 }
2490
2491 wl_resource_destroy(resource);
2492}
2493
2494WL_EXPORT void
2495weston_buffer_release_reference(struct weston_buffer_release_reference *ref,
2496 struct weston_buffer_release *buffer_release)
2497{
2498 if (buffer_release == ref->buffer_release)
2499 return;
2500
2501 if (ref->buffer_release) {
2502 ref->buffer_release->ref_count--;
2503 wl_list_remove(&ref->destroy_listener.link);
2504 if (ref->buffer_release->ref_count == 0)
2505 weston_buffer_release_destroy(ref->buffer_release);
2506 }
2507
2508 if (buffer_release) {
2509 buffer_release->ref_count++;
2510 wl_resource_add_destroy_listener(buffer_release->resource,
2511 &ref->destroy_listener);
2512 }
2513
2514 ref->buffer_release = buffer_release;
2515 ref->destroy_listener.notify =
2516 weston_buffer_release_reference_handle_destroy;
2517}
2518
2519WL_EXPORT void
2520weston_buffer_release_move(struct weston_buffer_release_reference *dest,
2521 struct weston_buffer_release_reference *src)
2522{
2523 weston_buffer_release_reference(dest, src->buffer_release);
2524 weston_buffer_release_reference(src, NULL);
2525}
2526
2527static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002528weston_surface_attach(struct weston_surface *surface,
2529 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002530{
2531 weston_buffer_reference(&surface->buffer_ref, buffer);
2532
Pekka Paalanena6421c42012-12-04 15:58:10 +02002533 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002534 if (weston_surface_is_mapped(surface))
2535 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002536 }
2537
leng.fang32af9fc2024-06-13 11:22:15 +08002538 weston_video_surface_attach(surface, buffer);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002539 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02002540
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002541 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002542 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002543}
2544
Marius Vlad9fdda7f2019-06-11 16:08:55 +03002545/** weston_compositor_damage_all
2546 * \ingroup compositor
2547 */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002548WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002549weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002550{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002551 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002552
2553 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002554 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002555}
2556
Marius Vlad55d87362019-06-11 01:15:35 +03002557/**
2558 * \ingroup output
2559 */
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05002560WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002561weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002562{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002563 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002564
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002565 pixman_region32_union(&compositor->primary_plane.damage,
2566 &compositor->primary_plane.damage,
2567 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002568 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002569}
2570
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04002571static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002572surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002573{
Pekka Paalanende685b82012-12-04 15:58:12 +02002574 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002575 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002576 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002577
Marius Vlad2a1b7862019-09-05 13:12:18 +03002578 if (pixman_region32_not_empty(&surface->damage))
Marius Vlad3203ff62019-09-05 14:56:12 +03002579 TL_POINT(surface->compositor, "core_flush_damage", TLP_SURFACE(surface),
Pekka Paalanenb5026542014-11-12 15:09:24 +02002580 TLP_OUTPUT(surface->output), TLP_END);
2581
Jason Ekstrandef540082014-06-26 10:37:36 -07002582 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002583}
2584
2585static void
2586view_accumulate_damage(struct weston_view *view,
2587 pixman_region32_t *opaque)
2588{
2589 pixman_region32_t damage;
2590
2591 pixman_region32_init(&damage);
2592 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002593 pixman_box32_t *extents;
2594
Jason Ekstranda7af7042013-10-12 22:38:11 -05002595 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002596 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002597 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002598 pixman_region32_copy(&damage, &view->surface->damage);
2599 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002600 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002601 }
2602
Pekka Paalanen380adf52015-02-16 14:39:11 +02002603 pixman_region32_intersect(&damage, &damage,
2604 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002605 pixman_region32_subtract(&damage, &damage, opaque);
2606 pixman_region32_union(&view->plane->damage,
2607 &view->plane->damage, &damage);
2608 pixman_region32_fini(&damage);
leng.fang32af9fc2024-06-13 11:22:15 +08002609#if MESON_VIDEO_PLAN_SUPPORT
2610 pixman_region32_union(&view->clip, &view->clip, opaque);
2611#else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002612 pixman_region32_copy(&view->clip, opaque);
leng.fang32af9fc2024-06-13 11:22:15 +08002613#endif
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002614 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002615}
2616
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002617static void
Michael Olbrichc5ea4952020-08-11 12:42:35 +02002618output_accumulate_damage(struct weston_output *output)
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002619{
Michael Olbrichc5ea4952020-08-11 12:42:35 +02002620 struct weston_compositor *ec = output->compositor;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002621 struct weston_plane *plane;
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002622 struct weston_paint_node *pnode;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002623 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002624
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002625 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002626
2627 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002628 pixman_region32_copy(&plane->clip, &clip);
2629
2630 pixman_region32_init(&opaque);
2631
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002632 wl_list_for_each(pnode, &output->paint_node_z_order_list,
2633 z_order_link) {
2634 if (pnode->view->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002635 continue;
leng.fang32af9fc2024-06-13 11:22:15 +08002636#if MESON_VIDEO_PLAN_SUPPORT
2637 // meson platform video plane is below primary plane.(but the zpos may hight then primary plane.
2638 // Avoid video plane region make the primary plane be occlude.
2639
2640 // We assume the overlay plane only have one surface, on weston just primary plane support glrender compositor.
2641 if (!pnode->view->surface->is_video_surface)
2642#endif
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002643
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002644 view_accumulate_damage(pnode->view, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002645 }
2646
2647 pixman_region32_union(&clip, &clip, &opaque);
2648 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002649 }
2650
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002651 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002652
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002653 wl_list_for_each(pnode, &output->paint_node_z_order_list,
2654 z_order_link) {
2655 pnode->surface->touched = false;
2656 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002657
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002658 wl_list_for_each(pnode, &output->paint_node_z_order_list,
2659 z_order_link) {
Michael Olbrichc5ea4952020-08-11 12:42:35 +02002660 /* Ignore views not visible on the current output */
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002661 /* TODO: turn this into assert once z_order_list is pruned. */
2662 if (!(pnode->view->output_mask & (1u << output->id)))
Michael Olbrichc5ea4952020-08-11 12:42:35 +02002663 continue;
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002664 if (pnode->surface->touched)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002665 continue;
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002666 pnode->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002667
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002668 surface_flush_damage(pnode->surface);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002669
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002670 /* Both the renderer and the backend have seen the buffer
2671 * by now. If renderer needs the buffer, it has its own
2672 * reference set. If the backend wants to keep the buffer
2673 * around for migrating the surface into a non-primary plane
2674 * later, keep_buffer is true. Otherwise, drop the core
2675 * reference now, and allow early buffer release. This enables
2676 * clients to use single-buffering.
2677 */
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002678 if (!pnode->surface->keep_buffer) {
2679 weston_buffer_reference(&pnode->surface->buffer_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002680 weston_buffer_release_reference(
Pekka Paalanen20ab6f82021-05-03 14:06:55 +03002681 &pnode->surface->buffer_release_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002682 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002683 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002684}
2685
2686static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002687surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002688{
2689 struct weston_subsurface *sub;
2690
Pekka Paalanene67858b2013-04-25 13:57:42 +03002691 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002692 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002693 continue;
2694
Jason Ekstranda7af7042013-10-12 22:38:11 -05002695 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2696 wl_list_init(&sub->surface->views);
2697
2698 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002699 }
2700}
2701
2702static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002703surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002704{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002705 struct weston_subsurface *sub;
2706 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002707
Jason Ekstranda7af7042013-10-12 22:38:11 -05002708 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2709 if (sub->surface == surface)
2710 continue;
2711
George Kiagiadakised04d382014-06-13 18:10:26 +02002712 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2713 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002714 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002715 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002716
2717 surface_free_unused_subsurface_views(sub->surface);
2718 }
2719}
2720
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002721static struct weston_paint_node *
2722view_ensure_paint_node(struct weston_view *view, struct weston_output *output)
2723{
2724 struct weston_paint_node *pnode;
2725
2726 if (!output)
2727 return NULL;
2728
2729 pnode = weston_view_find_paint_node(view, output);
2730 if (pnode)
2731 return pnode;
2732
2733 return weston_paint_node_create(view->surface, view, output);
2734}
2735
Jason Ekstranda7af7042013-10-12 22:38:11 -05002736static void
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002737add_to_z_order_list(struct weston_output *output,
2738 struct weston_paint_node *pnode)
2739{
2740 if (!pnode)
2741 return;
limin.tian230ec582024-07-30 09:35:17 +00002742 if (pnode->view && pnode->view->surface && pnode->view->surface->invisible)
2743 return;
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002744 wl_list_remove(&pnode->z_order_link);
2745 wl_list_insert(output->paint_node_z_order_list.prev,
2746 &pnode->z_order_link);
Pekka Paalanen90a5ffa2021-02-25 12:03:28 +02002747
2748 /*
2749 * Building weston_output::paint_node_z_order_list ensures all
2750 * necessary color transform objects are installed.
2751 */
2752 weston_paint_node_ensure_color_transform(pnode);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002753}
2754
2755static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002756view_list_add_subsurface_view(struct weston_compositor *compositor,
2757 struct weston_subsurface *sub,
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002758 struct weston_view *parent,
2759 struct weston_output *output)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002760{
2761 struct weston_subsurface *child;
2762 struct weston_view *view = NULL, *iv;
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002763 struct weston_paint_node *pnode;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002764
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002765 if (!weston_surface_is_mapped(sub->surface))
2766 return;
2767
Jason Ekstranda7af7042013-10-12 22:38:11 -05002768 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2769 if (iv->geometry.parent == parent) {
2770 view = iv;
2771 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002772 }
2773 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002774
2775 if (view) {
2776 /* Put it back in the surface's list of views */
2777 wl_list_remove(&view->surface_link);
2778 wl_list_insert(&sub->surface->views, &view->surface_link);
2779 } else {
2780 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002781 weston_view_set_position(view,
2782 sub->position.x,
2783 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002784 weston_view_set_transform_parent(view, parent);
2785 }
2786
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002787 view->parent_view = parent;
limin.tiance0f9d72024-08-06 06:30:57 +00002788 weston_update_subsurface_position(sub, view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002789 weston_view_update_transform(view);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002790 view->is_mapped = true;
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002791 pnode = view_ensure_paint_node(view, output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002792
Pekka Paalanenb188e912013-11-19 14:03:35 +02002793 if (wl_list_empty(&sub->surface->subsurface_list)) {
2794 wl_list_insert(compositor->view_list.prev, &view->link);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002795 add_to_z_order_list(output, pnode);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002796 return;
2797 }
2798
2799 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002800 if (child->surface == sub->surface) {
Pekka Paalanenb188e912013-11-19 14:03:35 +02002801 wl_list_insert(compositor->view_list.prev, &view->link);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002802 add_to_z_order_list(output, pnode);
2803 } else {
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002804 view_list_add_subsurface_view(compositor, child, view, output);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002805 }
Pekka Paalanenb188e912013-11-19 14:03:35 +02002806 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002807}
2808
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002809/* This recursively adds the sub-surfaces for a view, relying on the
2810 * sub-surface order. Thus, if a client restacks the sub-surfaces, that
2811 * change first happens to the sub-surface list, and then automatically
2812 * propagates here. See weston_surface_damage_subsurfaces() for how the
2813 * sub-surfaces receive damage when the client changes the state.
2814 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002815static void
2816view_list_add(struct weston_compositor *compositor,
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002817 struct weston_view *view,
2818 struct weston_output *output)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002819{
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002820 struct weston_paint_node *pnode;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002821 struct weston_subsurface *sub;
2822
leng.fanga0f20922024-10-11 17:23:59 +08002823 if (!weston_compositor_get_view_visible(view))
leng.fangdbaf6fa2024-06-20 19:31:04 +08002824 return;
2825
Jason Ekstranda7af7042013-10-12 22:38:11 -05002826 weston_view_update_transform(view);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002827 pnode = view_ensure_paint_node(view, output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002828
Pekka Paalanenb188e912013-11-19 14:03:35 +02002829 if (wl_list_empty(&view->surface->subsurface_list)) {
2830 wl_list_insert(compositor->view_list.prev, &view->link);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002831 add_to_z_order_list(output, pnode);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002832 return;
2833 }
2834
2835 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002836 if (sub->surface == view->surface) {
Pekka Paalanenb188e912013-11-19 14:03:35 +02002837 wl_list_insert(compositor->view_list.prev, &view->link);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002838 add_to_z_order_list(output, pnode);
2839 } else {
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002840 view_list_add_subsurface_view(compositor, sub, view, output);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002841 }
Pekka Paalanenb188e912013-11-19 14:03:35 +02002842 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002843}
2844
2845static void
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002846weston_compositor_build_view_list(struct weston_compositor *compositor,
2847 struct weston_output *output)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002848{
Loïc Yhuel267b16e2019-09-17 20:14:56 +02002849 struct weston_view *view, *tmp;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002850 struct weston_layer *layer;
2851
Pekka Paalanen2fddc532021-04-30 17:41:29 +03002852 if (output) {
2853 wl_list_remove(&output->paint_node_z_order_list);
2854 wl_list_init(&output->paint_node_z_order_list);
2855 }
2856
Jason Ekstranda7af7042013-10-12 22:38:11 -05002857 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002858 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002859 surface_stash_subsurface_views(view->surface);
2860
leng.fangdbaf6fa2024-06-20 19:31:04 +08002861 weston_compositor_build_view_list_by_zorder(compositor);
Loïc Yhuel267b16e2019-09-17 20:14:56 +02002862 wl_list_for_each_safe(view, tmp, &compositor->view_list, link)
2863 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002864 wl_list_init(&compositor->view_list);
Loïc Yhuel267b16e2019-09-17 20:14:56 +02002865
Jason Ekstranda7af7042013-10-12 22:38:11 -05002866 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002867 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002868 view_list_add(compositor, view, output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002869 }
2870 }
2871
2872 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002873 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002874 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002875}
2876
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002877static void
2878weston_output_take_feedback_list(struct weston_output *output,
2879 struct weston_surface *surface)
2880{
2881 struct weston_view *view;
2882 struct weston_presentation_feedback *feedback;
2883 uint32_t flags = 0xffffffff;
2884
2885 if (wl_list_empty(&surface->feedback_list))
2886 return;
2887
2888 /* All views must have the flag for the flag to survive. */
2889 wl_list_for_each(view, &surface->views, surface_link) {
2890 /* ignore views that are not on this output at all */
2891 if (view->output_mask & (1u << output->id))
2892 flags &= view->psf_flags;
2893 }
2894
2895 wl_list_for_each(feedback, &surface->feedback_list, link)
2896 feedback->psf_flags = flags;
2897
2898 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2899 wl_list_init(&surface->feedback_list);
2900}
2901
David Herrmann1edf44c2013-10-22 17:11:26 +02002902static int
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002903weston_output_repaint(struct weston_output *output, void *repaint_data)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002904{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002905 struct weston_compositor *ec = output->compositor;
Pekka Paalanen6d6b3672021-05-03 14:06:55 +03002906 struct weston_paint_node *pnode;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002907 struct weston_animation *animation, *next;
Vlad Zahorodniic2b97472021-07-29 11:16:51 +03002908 struct wl_resource *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002909 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002910 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002911 int r;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002912 uint32_t frame_time_msec;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05302913 enum weston_hdcp_protection highest_requested = WESTON_HDCP_DISABLE;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002914
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002915 if (output->destroying)
2916 return 0;
2917
Marius Vlad3203ff62019-09-05 14:56:12 +03002918 TL_POINT(ec, "core_repaint_begin", TLP_OUTPUT(output), TLP_END);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002919
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002920 /* Rebuild the surface list and update surface transforms up front. */
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002921 weston_compositor_build_view_list(ec, output);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002922
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05302923 /* Find the highest protection desired for an output */
Pekka Paalanen6d6b3672021-05-03 14:06:55 +03002924 wl_list_for_each(pnode, &output->paint_node_z_order_list,
2925 z_order_link) {
2926 /* TODO: turn this into assert once z_order_list is pruned. */
2927 if ((pnode->surface->output_mask & (1u << output->id)) == 0)
2928 continue;
2929
2930 /*
2931 * The desired_protection of the output should be the
2932 * maximum of the desired_protection of the surfaces,
2933 * that are displayed on that output, to avoid
2934 * reducing the protection for existing surfaces.
2935 */
2936 if (pnode->surface->desired_protection > highest_requested)
2937 highest_requested = pnode->surface->desired_protection;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05302938 }
2939
2940 output->desired_protection = highest_requested;
2941
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002942 if (output->assign_planes && !output->disable_planes) {
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002943 output->assign_planes(output, repaint_data);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002944 } else {
Pekka Paalanen6d6b3672021-05-03 14:06:55 +03002945 wl_list_for_each(pnode, &output->paint_node_z_order_list,
2946 z_order_link) {
2947 weston_view_move_to_plane(pnode->view, &ec->primary_plane);
2948 pnode->view->psf_flags = 0;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002949 }
2950 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002951
Pekka Paalanene67858b2013-04-25 13:57:42 +03002952 wl_list_init(&frame_callback_list);
Pekka Paalanen6d6b3672021-05-03 14:06:55 +03002953 wl_list_for_each(pnode, &output->paint_node_z_order_list,
2954 z_order_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002955 /* Note: This operation is safe to do multiple times on the
2956 * same surface.
2957 */
Pekka Paalanen6d6b3672021-05-03 14:06:55 +03002958 if (pnode->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002959 wl_list_insert_list(&frame_callback_list,
Pekka Paalanen6d6b3672021-05-03 14:06:55 +03002960 &pnode->surface->frame_callback_list);
2961 wl_list_init(&pnode->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002962
Pekka Paalanen6d6b3672021-05-03 14:06:55 +03002963 weston_output_take_feedback_list(output, pnode->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002964 }
2965 }
2966
Michael Olbrichc5ea4952020-08-11 12:42:35 +02002967 output_accumulate_damage(output);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002968
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002969 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002970 pixman_region32_intersect(&output_damage,
2971 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002972 pixman_region32_subtract(&output_damage,
2973 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002974
Scott Moreauccbf29d2012-02-22 14:21:41 -07002975 if (output->dirty)
2976 weston_output_update_matrix(output);
2977
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002978 r = output->repaint(output, &output_damage, repaint_data);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002979
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002980 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002981
Daniel Stone09a97e22017-03-01 11:34:06 +00002982 output->repaint_needed = false;
Daniel Stone05df8c12017-03-03 16:59:42 +00002983 if (r == 0)
2984 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002985
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002986 weston_compositor_repick(ec);
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002987
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002988 frame_time_msec = timespec_to_msec(&output->frame_time);
limin.tian531dd232025-01-07 12:09:27 +00002989 WESTON_LOGD(COMMON_LOG, "send callback\n");
Vlad Zahorodniic2b97472021-07-29 11:16:51 +03002990 wl_resource_for_each_safe(cb, cnext, &frame_callback_list) {
2991 wl_callback_send_done(cb, frame_time_msec);
2992 wl_resource_destroy(cb);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002993 }
2994
Scott Moreaud64cf212012-06-08 19:40:54 -06002995 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002996 animation->frame_counter++;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002997 animation->frame(animation, output, &output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002998 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002999
Marius Vlad3203ff62019-09-05 14:56:12 +03003000 TL_POINT(ec, "core_repaint_posted", TLP_OUTPUT(output), TLP_END);
Pekka Paalanenb5026542014-11-12 15:09:24 +02003001
David Herrmann1edf44c2013-10-22 17:11:26 +02003002 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04003003}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003004
Pekka Paalanen82919792014-05-21 13:51:49 +03003005static void
3006weston_output_schedule_repaint_reset(struct weston_output *output)
3007{
Daniel Stone05df8c12017-03-03 16:59:42 +00003008 output->repaint_status = REPAINT_NOT_SCHEDULED;
Marius Vlad3203ff62019-09-05 14:56:12 +03003009 TL_POINT(output->compositor, "core_repaint_exit_loop",
3010 TLP_OUTPUT(output), TLP_END);
Pekka Paalanen82919792014-05-21 13:51:49 +03003011}
3012
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003013static int
3014weston_output_maybe_repaint(struct weston_output *output, struct timespec *now,
3015 void *repaint_data)
Pekka Paalanen0513a952014-05-21 16:17:27 +03003016{
Pekka Paalanen0513a952014-05-21 16:17:27 +03003017 struct weston_compositor *compositor = output->compositor;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003018 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00003019 int64_t msec_to_repaint;
Pekka Paalanen0513a952014-05-21 16:17:27 +03003020
Daniel Stone6847b852017-03-01 11:34:08 +00003021 /* We're not ready yet; come back to make a decision later. */
3022 if (output->repaint_status != REPAINT_SCHEDULED)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003023 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00003024
3025 msec_to_repaint = timespec_sub_to_msec(&output->next_repaint, now);
leng.fang32af9fc2024-06-13 11:22:15 +08003026 if (msec_to_repaint > 1) {
limin.tian531dd232025-01-07 12:09:27 +00003027 WESTON_LOGD(COMMON_LOG, "msec_to_repaint > 1\n");
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003028 return ret;
leng.fang32af9fc2024-06-13 11:22:15 +08003029 }
Daniel Stone05df8c12017-03-03 16:59:42 +00003030
Daniel Stonecd1a1c32017-01-16 15:38:54 +00003031 /* If we're sleeping, drop the repaint machinery entirely; we will
3032 * explicitly repaint all outputs when we come back. */
3033 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
leng.fang32af9fc2024-06-13 11:22:15 +08003034 compositor->state == WESTON_COMPOSITOR_OFFSCREEN) {
limin.tian531dd232025-01-07 12:09:27 +00003035 WESTON_LOGD(COMMON_LOG, "state:%d\n", compositor->state);
Daniel Stonecd1a1c32017-01-16 15:38:54 +00003036 goto err;
leng.fang32af9fc2024-06-13 11:22:15 +08003037 }
Daniel Stonecd1a1c32017-01-16 15:38:54 +00003038 /* We don't actually need to repaint this output; drop it from
3039 * repaint until something causes damage. */
leng.fang32af9fc2024-06-13 11:22:15 +08003040 if (!output->repaint_needed) {
limin.tian531dd232025-01-07 12:09:27 +00003041 WESTON_LOGD(COMMON_LOG, "repaint not needed\n");
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
3045 /* If repaint fails, we aren't going to get weston_output_finish_frame
3046 * to trigger a new repaint, so drop it from repaint and hope
Daniel Stone6847b852017-03-01 11:34:08 +00003047 * something schedules a successful repaint later. As repainting may
3048 * take some time, re-read our clock as a courtesy to the next
3049 * output. */
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003050 ret = weston_output_repaint(output, repaint_data);
Daniel Stone6847b852017-03-01 11:34:08 +00003051 weston_compositor_read_presentation_clock(compositor, now);
leng.fang32af9fc2024-06-13 11:22:15 +08003052 if (ret != 0) {
limin.tian531dd232025-01-07 12:09:27 +00003053 WESTON_LOGD(COMMON_LOG, "fail\n");
Daniel Stonecd1a1c32017-01-16 15:38:54 +00003054 goto err;
leng.fang32af9fc2024-06-13 11:22:15 +08003055 }
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09003056 output->repainted = true;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003057 return ret;
Daniel Stonecd1a1c32017-01-16 15:38:54 +00003058
3059err:
Pekka Paalanen0513a952014-05-21 16:17:27 +03003060 weston_output_schedule_repaint_reset(output);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003061 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00003062}
3063
3064static void
3065output_repaint_timer_arm(struct weston_compositor *compositor)
3066{
3067 struct weston_output *output;
3068 bool any_should_repaint = false;
3069 struct timespec now;
Sergi Granellb4c08862017-03-18 13:01:15 +01003070 int64_t msec_to_next = INT64_MAX;
Daniel Stone6847b852017-03-01 11:34:08 +00003071
3072 weston_compositor_read_presentation_clock(compositor, &now);
3073
3074 wl_list_for_each(output, &compositor->output_list, link) {
3075 int64_t msec_to_this;
3076
3077 if (output->repaint_status != REPAINT_SCHEDULED)
3078 continue;
3079
3080 msec_to_this = timespec_sub_to_msec(&output->next_repaint,
3081 &now);
3082 if (!any_should_repaint || msec_to_this < msec_to_next)
3083 msec_to_next = msec_to_this;
3084
3085 any_should_repaint = true;
3086 }
3087
3088 if (!any_should_repaint)
3089 return;
3090
3091 /* Even if we should repaint immediately, add the minimum 1 ms delay.
3092 * This is a workaround to allow coalescing multiple output repaints
3093 * particularly from weston_output_finish_frame()
3094 * into the same call, which would not happen if we called
3095 * output_repaint_timer_handler() directly.
3096 */
3097 if (msec_to_next < 1)
3098 msec_to_next = 1;
3099
3100 wl_event_source_timer_update(compositor->repaint_timer, msec_to_next);
3101}
3102
3103static int
3104output_repaint_timer_handler(void *data)
3105{
3106 struct weston_compositor *compositor = data;
3107 struct weston_output *output;
3108 struct timespec now;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003109 void *repaint_data = NULL;
Emre Ucane479ed82018-03-20 15:29:40 +01003110 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00003111
3112 weston_compositor_read_presentation_clock(compositor, &now);
Pekka Paalanen1ed2cad2021-02-10 12:33:03 +02003113 compositor->last_repaint_start = now;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003114
3115 if (compositor->backend->repaint_begin)
3116 repaint_data = compositor->backend->repaint_begin(compositor);
3117
3118 wl_list_for_each(output, &compositor->output_list, link) {
3119 ret = weston_output_maybe_repaint(output, &now, repaint_data);
3120 if (ret)
3121 break;
3122 }
3123
3124 if (ret == 0) {
Tomohito Esaki09bfcd62018-06-05 10:37:05 +09003125 if (compositor->backend->repaint_flush)
Antonio Borneoc90fccc2019-06-30 15:51:10 +02003126 ret = compositor->backend->repaint_flush(compositor,
3127 repaint_data);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003128 } else {
Antonio Borneoc90fccc2019-06-30 15:51:10 +02003129 if (compositor->backend->repaint_cancel)
3130 compositor->backend->repaint_cancel(compositor,
3131 repaint_data);
3132 }
3133
3134 if (ret != 0) {
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09003135 wl_list_for_each(output, &compositor->output_list, link) {
3136 if (output->repainted)
3137 weston_output_schedule_repaint_reset(output);
3138 }
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003139 }
Daniel Stone6847b852017-03-01 11:34:08 +00003140
Tomohito Esakiddaf95c2018-07-10 11:47:15 +09003141 wl_list_for_each(output, &compositor->output_list, link)
3142 output->repainted = false;
3143
Daniel Stone6847b852017-03-01 11:34:08 +00003144 output_repaint_timer_arm(compositor);
3145
Pekka Paalanen0513a952014-05-21 16:17:27 +03003146 return 0;
3147}
3148
Pekka Paalanen50aa3a72020-05-28 11:34:04 +03003149/** Convert a presentation timestamp to another clock domain
3150 *
3151 * \param compositor The compositor defines the presentation clock domain.
3152 * \param presentation_stamp The timestamp in presentation clock domain.
3153 * \param presentation_now Current time in presentation clock domain.
3154 * \param target_clock Defines the target clock domain.
3155 *
3156 * This approximation relies on presentation_stamp to be close to current time.
3157 * The further it is from current time and the bigger the speed difference
3158 * between the two clock domains, the bigger the conversion error.
3159 *
3160 * Conversion error due to system load is biased and unbounded.
3161 */
3162static struct timespec
3163convert_presentation_time_now(struct weston_compositor *compositor,
3164 const struct timespec *presentation_stamp,
3165 const struct timespec *presentation_now,
3166 clockid_t target_clock)
3167{
3168 struct timespec target_now = {};
3169 struct timespec target_stamp;
3170 int64_t delta_ns;
3171
3172 if (compositor->presentation_clock == target_clock)
3173 return *presentation_stamp;
3174
3175 clock_gettime(target_clock, &target_now);
3176 delta_ns = timespec_sub_to_nsec(presentation_stamp, presentation_now);
3177 timespec_add_nsec(&target_stamp, &target_now, delta_ns);
3178
3179 return target_stamp;
3180}
3181
Marius Vlad55d87362019-06-11 01:15:35 +03003182/**
3183 * \ingroup output
3184 */
Kristian Høgsbergef044142011-06-21 15:02:12 -04003185WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04003186weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02003187 const struct timespec *stamp,
3188 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04003189{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003190 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03003191 int32_t refresh_nsec;
3192 struct timespec now;
Pekka Paalanen50aa3a72020-05-28 11:34:04 +03003193 struct timespec vblank_monotonic;
Daniel Stone6847b852017-03-01 11:34:08 +00003194 int64_t msec_rel;
Pekka Paalanen133e4392014-09-23 22:08:46 -04003195
Daniel Stone05df8c12017-03-03 16:59:42 +00003196 assert(output->repaint_status == REPAINT_AWAITING_COMPLETION);
Pekka Paalanen98b4e202021-05-10 11:33:23 +03003197
3198 /*
3199 * If timestamp of latest vblank is given, it must always go forwards.
3200 * If not given, INVALID flag must be set.
3201 */
3202 if (stamp)
3203 assert(timespec_sub_to_nsec(stamp, &output->frame_time) >= 0);
3204 else
3205 assert(presented_flags & WP_PRESENTATION_FEEDBACK_INVALID);
Daniel Stone3615ce12017-03-01 11:34:05 +00003206
Daniel Stone6847b852017-03-01 11:34:08 +00003207 weston_compositor_read_presentation_clock(compositor, &now);
3208
Daniel Stone3615ce12017-03-01 11:34:05 +00003209 /* If we haven't been supplied any timestamp at all, we don't have a
3210 * timebase to work against, so any delay just wastes time. Push a
3211 * repaint as soon as possible so we can get on with it. */
Daniel Stone6847b852017-03-01 11:34:08 +00003212 if (!stamp) {
3213 output->next_repaint = now;
Daniel Stone3615ce12017-03-01 11:34:05 +00003214 goto out;
Daniel Stone6847b852017-03-01 11:34:08 +00003215 }
Daniel Stone3615ce12017-03-01 11:34:05 +00003216
Pekka Paalanen50aa3a72020-05-28 11:34:04 +03003217 vblank_monotonic = convert_presentation_time_now(compositor,
3218 stamp, &now,
3219 CLOCK_MONOTONIC);
Marius Vlad3203ff62019-09-05 14:56:12 +03003220 TL_POINT(compositor, "core_repaint_finished", TLP_OUTPUT(output),
Pekka Paalanen50aa3a72020-05-28 11:34:04 +03003221 TLP_VBLANK(&vblank_monotonic), TLP_END);
Marius Vladdf9278a2018-03-06 18:56:23 +02003222
Pekka Paalanend7894d02015-07-03 15:08:53 +03003223 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003224 weston_presentation_feedback_present_list(&output->feedback_list,
3225 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02003226 output->msc,
3227 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003228
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02003229 output->frame_time = *stamp;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003230
Daniel Stone6847b852017-03-01 11:34:08 +00003231 timespec_add_nsec(&output->next_repaint, stamp, refresh_nsec);
3232 timespec_add_msec(&output->next_repaint, &output->next_repaint,
3233 -compositor->repaint_msec);
3234 msec_rel = timespec_sub_to_msec(&output->next_repaint, &now);
Daniel Stone84aff5c2017-03-01 11:34:04 +00003235
3236 if (msec_rel < -1000 || msec_rel > 1000) {
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02003237 static bool warned;
3238
3239 if (!warned)
3240 weston_log("Warning: computed repaint delay is "
Daniel Stone6847b852017-03-01 11:34:08 +00003241 "insane: %lld msec\n", (long long) msec_rel);
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02003242 warned = true;
3243
Daniel Stone6847b852017-03-01 11:34:08 +00003244 output->next_repaint = now;
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02003245 }
3246
Mario Kleinerb7df04e2015-06-21 21:25:15 +02003247 /* Called from restart_repaint_loop and restart happens already after
3248 * the deadline given by repaint_msec? In that case we delay until
3249 * the deadline of the next frame, to give clients a more predictable
3250 * timing of the repaint cycle to lock on. */
Daniel Stoneeca5cca2017-02-28 21:53:51 +00003251 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID &&
3252 msec_rel < 0) {
3253 while (timespec_sub_to_nsec(&output->next_repaint, &now) < 0) {
3254 timespec_add_nsec(&output->next_repaint,
3255 &output->next_repaint,
3256 refresh_nsec);
3257 }
3258 }
Mario Kleinerb7df04e2015-06-21 21:25:15 +02003259
Daniel Stone3615ce12017-03-01 11:34:05 +00003260out:
Daniel Stone05df8c12017-03-03 16:59:42 +00003261 output->repaint_status = REPAINT_SCHEDULED;
Daniel Stone6847b852017-03-01 11:34:08 +00003262 output_repaint_timer_arm(compositor);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003263}
3264
nerdopolisd2a320d2021-08-23 21:29:42 -04003265
3266WL_EXPORT void
3267weston_output_repaint_failed(struct weston_output *output)
3268{
3269 weston_log("Clearing repaint status.\n");
3270 assert(output->repaint_status == REPAINT_AWAITING_COMPLETION);
3271 output->repaint_status = REPAINT_NOT_SCHEDULED;
3272}
3273
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003274static void
3275idle_repaint(void *data)
3276{
3277 struct weston_output *output = data;
Antonio Borneoc90fccc2019-06-30 15:51:10 +02003278 int ret;
Daniel Stone05df8c12017-03-03 16:59:42 +00003279 assert(output->repaint_status == REPAINT_BEGIN_FROM_IDLE);
3280 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03003281 output->idle_repaint_source = NULL;
Antonio Borneoc90fccc2019-06-30 15:51:10 +02003282 ret = output->start_repaint_loop(output);
3283 if (ret != 0)
3284 weston_output_schedule_repaint_reset(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003285}
3286
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003287WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003288weston_layer_entry_insert(struct weston_layer_entry *list,
3289 struct weston_layer_entry *entry)
3290{
3291 wl_list_insert(&list->link, &entry->link);
3292 entry->layer = list->layer;
3293}
3294
3295WL_EXPORT void
3296weston_layer_entry_remove(struct weston_layer_entry *entry)
3297{
3298 wl_list_remove(&entry->link);
3299 wl_list_init(&entry->link);
3300 entry->layer = NULL;
3301}
3302
Quentin Glidic82681572016-12-17 13:40:51 +01003303
3304/** Initialize the weston_layer struct.
3305 *
3306 * \param compositor The compositor instance
3307 * \param layer The layer to initialize
3308 */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003309WL_EXPORT void
Quentin Glidic82681572016-12-17 13:40:51 +01003310weston_layer_init(struct weston_layer *layer,
3311 struct weston_compositor *compositor)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003312{
Quentin Glidic82681572016-12-17 13:40:51 +01003313 layer->compositor = compositor;
3314 wl_list_init(&layer->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003315 wl_list_init(&layer->view_list.link);
3316 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003317 weston_layer_set_mask_infinite(layer);
Quentin Glidic82681572016-12-17 13:40:51 +01003318}
3319
Pekka Paalanen87400372021-05-14 14:29:40 +03003320/** Finalize the weston_layer struct.
3321 *
3322 * \param layer The layer to finalize.
3323 */
3324WL_EXPORT void
3325weston_layer_fini(struct weston_layer *layer)
3326{
3327 wl_list_remove(&layer->link);
3328
3329 if (!wl_list_empty(&layer->view_list.link))
3330 weston_log("BUG: finalizing a layer with views still on it.\n");
3331
3332 wl_list_remove(&layer->view_list.link);
3333}
3334
Quentin Glidic82681572016-12-17 13:40:51 +01003335/** Sets the position of the layer in the layer list. The layer will be placed
3336 * below any layer with the same position value, if any.
3337 * This function is safe to call if the layer is already on the list, but the
3338 * layer may be moved below other layers at the same position, if any.
3339 *
3340 * \param layer The layer to modify
3341 * \param position The position the layer will be placed at
3342 */
3343WL_EXPORT void
3344weston_layer_set_position(struct weston_layer *layer,
3345 enum weston_layer_position position)
3346{
3347 struct weston_layer *below;
3348
3349 wl_list_remove(&layer->link);
3350
3351 /* layer_list is ordered from top to bottom, the last layer being the
3352 * background with the smallest position value */
3353
3354 layer->position = position;
3355 wl_list_for_each_reverse(below, &layer->compositor->layer_list, link) {
3356 if (below->position >= layer->position) {
3357 wl_list_insert(&below->link, &layer->link);
3358 return;
3359 }
3360 }
3361 wl_list_insert(&layer->compositor->layer_list, &layer->link);
3362}
3363
3364/** Hide a layer by taking it off the layer list.
3365 * This function is safe to call if the layer is not on the list.
3366 *
3367 * \param layer The layer to hide
3368 */
3369WL_EXPORT void
3370weston_layer_unset_position(struct weston_layer *layer)
3371{
3372 wl_list_remove(&layer->link);
3373 wl_list_init(&layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003374}
3375
3376WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003377weston_layer_set_mask(struct weston_layer *layer,
3378 int x, int y, int width, int height)
3379{
3380 struct weston_view *view;
3381
3382 layer->mask.x1 = x;
3383 layer->mask.x2 = x + width;
3384 layer->mask.y1 = y;
3385 layer->mask.y2 = y + height;
3386
3387 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
3388 weston_view_geometry_dirty(view);
3389 }
3390}
3391
3392WL_EXPORT void
3393weston_layer_set_mask_infinite(struct weston_layer *layer)
3394{
Adam Jackson3c3f3b12019-10-16 16:02:59 -04003395 struct weston_view *view;
3396
3397 layer->mask.x1 = INT32_MIN;
3398 layer->mask.x2 = INT32_MAX;
3399 layer->mask.y1 = INT32_MIN;
3400 layer->mask.y2 = INT32_MAX;
3401
3402 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
3403 weston_view_geometry_dirty(view);
3404 }
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003405}
3406
Daniel Stone3b775632018-07-20 08:38:25 +01003407WL_EXPORT bool
3408weston_layer_mask_is_infinite(struct weston_layer *layer)
3409{
3410 return layer->mask.x1 == INT32_MIN &&
3411 layer->mask.y1 == INT32_MIN &&
Adam Jackson3c3f3b12019-10-16 16:02:59 -04003412 layer->mask.x2 == INT32_MAX &&
3413 layer->mask.y2 == INT32_MAX;
Daniel Stone3b775632018-07-20 08:38:25 +01003414}
3415
Marius Vlad55d87362019-06-11 01:15:35 +03003416/**
3417 * \ingroup output
3418 */
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003419WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003420weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003421{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003422 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04003423 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01003424
Bryce Harrington08976ac2016-08-30 12:05:16 -07003425 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
3426 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003427 return;
3428
Pekka Paalanenb5026542014-11-12 15:09:24 +02003429 if (!output->repaint_needed)
Marius Vlad3203ff62019-09-05 14:56:12 +03003430 TL_POINT(compositor, "core_repaint_req", TLP_OUTPUT(output), TLP_END);
Pekka Paalanenb5026542014-11-12 15:09:24 +02003431
Kristian Høgsbergef044142011-06-21 15:02:12 -04003432 loop = wl_display_get_event_loop(compositor->wl_display);
Daniel Stone09a97e22017-03-01 11:34:06 +00003433 output->repaint_needed = true;
Daniel Stone05df8c12017-03-03 16:59:42 +00003434
3435 /* If we already have a repaint scheduled for our idle handler,
3436 * no need to set it again. If the repaint has been called but
3437 * not finished, then weston_output_finish_frame() will notice
3438 * that a repaint is needed and schedule one. */
3439 if (output->repaint_status != REPAINT_NOT_SCHEDULED)
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003440 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01003441
Daniel Stone05df8c12017-03-03 16:59:42 +00003442 output->repaint_status = REPAINT_BEGIN_FROM_IDLE;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03003443 assert(!output->idle_repaint_source);
3444 output->idle_repaint_source = wl_event_loop_add_idle(loop, idle_repaint,
3445 output);
Marius Vlad3203ff62019-09-05 14:56:12 +03003446 TL_POINT(compositor, "core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003447}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05003448
Marius Vlad9fdda7f2019-06-11 16:08:55 +03003449/** weston_compositor_schedule_repaint
3450 * \ingroup compositor
3451 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003452WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003453weston_compositor_schedule_repaint(struct weston_compositor *compositor)
3454{
3455 struct weston_output *output;
3456
3457 wl_list_for_each(output, &compositor->output_list, link)
3458 weston_output_schedule_repaint(output);
3459}
3460
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003461static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003462surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003463{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003464 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003465}
3466
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003467static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003468surface_attach(struct wl_client *client,
3469 struct wl_resource *resource,
3470 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
3471{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003472 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05003473 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003474
Kristian Høgsbergab19f932013-08-20 11:30:54 -07003475 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05003476 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07003477 if (buffer == NULL) {
3478 wl_client_post_no_memory(client);
3479 return;
3480 }
limin.tian4733ccf2024-08-14 08:48:55 +00003481 buffer->surface = surface;
3482 buffer->attach = buffer->attach+1;
limin.tian70492762024-08-23 02:02:12 +00003483 buffer->ec = surface->compositor;
limin.tianbf71b4c2024-08-27 09:09:34 +00003484 if (surface) {
3485 buffer->enable_display_time = surface->enable_display_time;
3486 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07003487 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04003488
Pekka Paalanende685b82012-12-04 15:58:12 +02003489 /* Attach, attach, without commit in between does not send
3490 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003491 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003492
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003493 surface->pending.sx = sx;
3494 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01003495 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003496}
3497
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003498static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003499surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003500 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003501 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05003502{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003503 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003504
Derek Foreman57e92ed2015-11-17 14:11:35 -06003505 if (width <= 0 || height <= 0)
3506 return;
3507
Derek Foreman152254b2015-11-26 14:17:48 -06003508 pixman_region32_union_rect(&surface->pending.damage_surface,
3509 &surface->pending.damage_surface,
3510 x, y, width, height);
3511}
3512
3513static void
3514surface_damage_buffer(struct wl_client *client,
3515 struct wl_resource *resource,
3516 int32_t x, int32_t y, int32_t width, int32_t height)
3517{
3518 struct weston_surface *surface = wl_resource_get_user_data(resource);
3519
3520 if (width <= 0 || height <= 0)
3521 return;
3522
3523 pixman_region32_union_rect(&surface->pending.damage_buffer,
3524 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04003525 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003526}
3527
Kristian Høgsberg33418202011-08-16 23:01:28 -04003528static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003529destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04003530{
Vlad Zahorodniic2b97472021-07-29 11:16:51 +03003531 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsberg33418202011-08-16 23:01:28 -04003532}
3533
3534static void
3535surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003536 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04003537{
Vlad Zahorodniic2b97472021-07-29 11:16:51 +03003538 struct wl_resource *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003539 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003540
Vlad Zahorodniic2b97472021-07-29 11:16:51 +03003541 cb = wl_resource_create(client, &wl_callback_interface, 1, callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003542 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003543 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003544 return;
3545 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03003546
Vlad Zahorodniic2b97472021-07-29 11:16:51 +03003547 wl_resource_set_implementation(cb, NULL, NULL,
Jason Ekstranda85118c2013-06-27 20:17:02 -05003548 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003549
leng.fang32af9fc2024-06-13 11:22:15 +08003550 wl_list_init(&surface->pending.frame_callback_list);
Vlad Zahorodniic2b97472021-07-29 11:16:51 +03003551 wl_list_insert(surface->pending.frame_callback_list.prev,
3552 wl_resource_get_link(cb));
Kristian Høgsberg33418202011-08-16 23:01:28 -04003553}
3554
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003555static void
3556surface_set_opaque_region(struct wl_client *client,
3557 struct wl_resource *resource,
3558 struct wl_resource *region_resource)
3559{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003560 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003561 struct weston_region *region;
3562
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003563 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003564 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003565 pixman_region32_copy(&surface->pending.opaque,
3566 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003567 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07003568 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003569 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003570}
3571
3572static void
3573surface_set_input_region(struct wl_client *client,
3574 struct wl_resource *resource,
3575 struct wl_resource *region_resource)
3576{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003577 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003578 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003579
3580 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003581 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003582 pixman_region32_copy(&surface->pending.input,
3583 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003584 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003585 pixman_region32_fini(&surface->pending.input);
3586 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003587 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003588}
3589
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003590/* Cause damage to this sub-surface and all its children.
3591 *
3592 * This is useful when there are state changes that need an implicit
3593 * damage, e.g. a z-order change.
3594 */
3595static void
3596weston_surface_damage_subsurfaces(struct weston_subsurface *sub)
3597{
3598 struct weston_subsurface *child;
3599
3600 weston_surface_damage(sub->surface);
3601 sub->reordered = false;
3602
3603 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link)
3604 if (child != sub)
3605 weston_surface_damage_subsurfaces(child);
3606}
3607
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003608static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003609weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003610{
Pekka Paalanene67858b2013-04-25 13:57:42 +03003611 struct weston_subsurface *sub;
3612
3613 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
3614 parent_link_pending) {
3615 wl_list_remove(&sub->parent_link);
3616 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003617
3618 if (sub->reordered)
3619 weston_surface_damage_subsurfaces(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003620 }
3621}
3622
3623static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03003624weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05003625 struct weston_matrix *matrix)
3626{
Pekka Paalanen04baea52016-04-26 15:50:58 +03003627 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05003628 double src_width, src_height, dest_width, dest_height;
3629
3630 weston_matrix_init(matrix);
3631
3632 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
3633 src_width = surface->width_from_buffer;
3634 src_height = surface->height_from_buffer;
3635 } else {
3636 src_width = wl_fixed_to_double(vp->buffer.src_width);
3637 src_height = wl_fixed_to_double(vp->buffer.src_height);
3638 }
3639
3640 if (vp->surface.width == -1) {
3641 dest_width = src_width;
3642 dest_height = src_height;
3643 } else {
3644 dest_width = vp->surface.width;
3645 dest_height = vp->surface.height;
3646 }
3647
3648 if (src_width != dest_width || src_height != dest_height)
3649 weston_matrix_scale(matrix,
3650 src_width / dest_width,
3651 src_height / dest_height, 1);
3652
3653 if (vp->buffer.src_width != wl_fixed_from_int(-1))
3654 weston_matrix_translate(matrix,
3655 wl_fixed_to_double(vp->buffer.src_x),
3656 wl_fixed_to_double(vp->buffer.src_y),
3657 0);
3658
3659 switch (vp->buffer.transform) {
3660 case WL_OUTPUT_TRANSFORM_FLIPPED:
3661 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3662 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3663 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3664 weston_matrix_scale(matrix, -1, 1, 1);
3665 weston_matrix_translate(matrix,
3666 surface->width_from_buffer, 0, 0);
3667 break;
3668 }
3669
3670 switch (vp->buffer.transform) {
3671 default:
3672 case WL_OUTPUT_TRANSFORM_NORMAL:
3673 case WL_OUTPUT_TRANSFORM_FLIPPED:
3674 break;
3675 case WL_OUTPUT_TRANSFORM_90:
3676 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +02003677 weston_matrix_rotate_xy(matrix, 0, -1);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003678 weston_matrix_translate(matrix,
Pekka Paalanen8060d822020-02-06 15:27:54 +02003679 0, surface->width_from_buffer, 0);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003680 break;
3681 case WL_OUTPUT_TRANSFORM_180:
3682 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3683 weston_matrix_rotate_xy(matrix, -1, 0);
3684 weston_matrix_translate(matrix,
3685 surface->width_from_buffer,
3686 surface->height_from_buffer, 0);
3687 break;
3688 case WL_OUTPUT_TRANSFORM_270:
3689 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +02003690 weston_matrix_rotate_xy(matrix, 0, 1);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003691 weston_matrix_translate(matrix,
Pekka Paalanen8060d822020-02-06 15:27:54 +02003692 surface->height_from_buffer, 0, 0);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003693 break;
3694 }
3695
3696 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
3697}
3698
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003699/**
3700 * Compute a + b > c while being safe to overflows.
3701 */
3702static bool
3703fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
3704{
3705 return (int64_t)a + (int64_t)b > (int64_t)c;
3706}
3707
3708static bool
3709weston_surface_is_pending_viewport_source_valid(
3710 const struct weston_surface *surface)
3711{
3712 const struct weston_surface_state *pend = &surface->pending;
3713 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
3714 int width_from_buffer = 0;
3715 int height_from_buffer = 0;
3716 wl_fixed_t w;
3717 wl_fixed_t h;
3718
3719 /* If viewport source rect is not set, it is always ok. */
3720 if (vp->buffer.src_width == wl_fixed_from_int(-1))
3721 return true;
3722
3723 if (pend->newly_attached) {
3724 if (pend->buffer) {
3725 convert_size_by_transform_scale(&width_from_buffer,
3726 &height_from_buffer,
3727 pend->buffer->width,
3728 pend->buffer->height,
3729 vp->buffer.transform,
3730 vp->buffer.scale);
3731 } else {
3732 /* No buffer: viewport is irrelevant. */
3733 return true;
3734 }
3735 } else {
3736 width_from_buffer = surface->width_from_buffer;
3737 height_from_buffer = surface->height_from_buffer;
3738 }
3739
3740 assert((width_from_buffer == 0) == (height_from_buffer == 0));
3741 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
3742
3743 /* No buffer: viewport is irrelevant. */
3744 if (width_from_buffer == 0 || height_from_buffer == 0)
3745 return true;
3746
3747 /* overflow checks for wl_fixed_from_int() */
3748 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
3749 return false;
3750 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
3751 return false;
3752
3753 w = wl_fixed_from_int(width_from_buffer);
3754 h = wl_fixed_from_int(height_from_buffer);
3755
3756 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
3757 return false;
3758 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
3759 return false;
3760
3761 return true;
3762}
3763
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003764static bool
3765fixed_is_integer(wl_fixed_t v)
3766{
3767 return (v & 0xff) == 0;
3768}
3769
3770static bool
3771weston_surface_is_pending_viewport_dst_size_int(
3772 const struct weston_surface *surface)
3773{
3774 const struct weston_buffer_viewport *vp =
3775 &surface->pending.buffer_viewport;
3776
3777 if (vp->surface.width != -1) {
3778 assert(vp->surface.width > 0 && vp->surface.height > 0);
3779 return true;
3780 }
3781
3782 return fixed_is_integer(vp->buffer.src_width) &&
3783 fixed_is_integer(vp->buffer.src_height);
3784}
3785
Derek Foreman152254b2015-11-26 14:17:48 -06003786/* Translate pending damage in buffer co-ordinates to surface
3787 * co-ordinates and union it with a pixman_region32_t.
3788 * This should only be called after the buffer is attached.
3789 */
3790static void
3791apply_damage_buffer(pixman_region32_t *dest,
3792 struct weston_surface *surface,
3793 struct weston_surface_state *state)
3794{
3795 struct weston_buffer *buffer = surface->buffer_ref.buffer;
3796
3797 /* wl_surface.damage_buffer needs to be clipped to the buffer,
3798 * translated into surface co-ordinates and unioned with
3799 * any other surface damage.
3800 * None of this makes sense if there is no buffer though.
3801 */
3802 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
3803 pixman_region32_t buffer_damage;
3804
3805 pixman_region32_intersect_rect(&state->damage_buffer,
3806 &state->damage_buffer,
3807 0, 0, buffer->width,
3808 buffer->height);
3809 pixman_region32_init(&buffer_damage);
3810 weston_matrix_transform_region(&buffer_damage,
3811 &surface->buffer_to_surface_matrix,
3812 &state->damage_buffer);
3813 pixman_region32_union(dest, dest, &buffer_damage);
3814 pixman_region32_fini(&buffer_damage);
3815 }
3816 /* We should clear this on commit even if there was no buffer */
3817 pixman_region32_clear(&state->damage_buffer);
3818}
3819
Jason Ekstrand1e059042014-10-16 10:55:19 -05003820static void
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303821weston_surface_set_desired_protection(struct weston_surface *surface,
3822 enum weston_hdcp_protection protection)
3823{
3824 if (surface->desired_protection == protection)
3825 return;
3826 surface->desired_protection = protection;
3827 weston_surface_damage(surface);
3828}
3829
3830static void
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303831weston_surface_set_protection_mode(struct weston_surface *surface,
3832 enum weston_surface_protection_mode p_mode)
3833{
3834 struct content_protection *cp = surface->compositor->content_protection;
3835 struct protected_surface *psurface;
3836
3837 surface->protection_mode = p_mode;
3838 wl_list_for_each(psurface, &cp->protected_list, link) {
3839 if (!psurface || psurface->surface != surface)
3840 continue;
3841 weston_protected_surface_send_event(psurface,
3842 surface->current_protection);
3843 }
3844}
3845
3846static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05003847weston_surface_commit_state(struct weston_surface *surface,
3848 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003849{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003850 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003851 pixman_region32_t opaque;
3852
Alexander Larsson4ea95522013-05-22 14:41:37 +02003853 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02003854 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03003855 /* wp_viewport.set_source */
3856 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003857 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003858
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003859 /* wl_surface.attach */
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003860 if (state->newly_attached) {
3861 /* zwp_surface_synchronization_v1.set_acquire_fence */
3862 fd_move(&surface->acquire_fence_fd,
3863 &state->acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003864 /* zwp_surface_synchronization_v1.get_release */
3865 weston_buffer_release_move(&surface->buffer_release_ref,
3866 &state->buffer_release_ref);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003867 weston_surface_attach(surface, state->buffer);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003868 }
Jason Ekstrand7b982072014-05-20 14:33:03 -05003869 weston_surface_state_set_buffer(state, NULL);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003870 assert(state->acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003871 assert(state->buffer_release_ref.buffer_release == NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01003872
Jason Ekstrand1e059042014-10-16 10:55:19 -05003873 weston_surface_build_buffer_matrix(surface,
3874 &surface->surface_to_buffer_matrix);
3875 weston_matrix_invert(&surface->buffer_to_surface_matrix,
3876 &surface->surface_to_buffer_matrix);
3877
Jason Ekstrand7b982072014-05-20 14:33:03 -05003878 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003879 weston_surface_update_size(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003880 if (surface->committed)
3881 surface->committed(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003882 }
Giulio Camuffo184df502013-02-21 11:29:21 +01003883
Jason Ekstrand7b982072014-05-20 14:33:03 -05003884 state->sx = 0;
3885 state->sy = 0;
3886 state->newly_attached = 0;
3887 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03003888
Derek Foreman152254b2015-11-26 14:17:48 -06003889 /* wl_surface.damage and wl_surface.damage_buffer */
Marius Vlad2a1b7862019-09-05 13:12:18 +03003890 if (pixman_region32_not_empty(&state->damage_surface) ||
3891 pixman_region32_not_empty(&state->damage_buffer))
Marius Vlad3203ff62019-09-05 14:56:12 +03003892 TL_POINT(surface->compositor, "core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06003893
Pekka Paalanen8e159182012-10-10 12:49:25 +03003894 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06003895 &state->damage_surface);
3896
3897 apply_damage_buffer(&surface->damage, surface, state);
3898
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05003899 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07003900 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06003901 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003902
3903 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003904 pixman_region32_init(&opaque);
3905 pixman_region32_intersect_rect(&opaque, &state->opaque,
3906 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003907
3908 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
3909 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003910 wl_list_for_each(view, &surface->views, surface_link)
3911 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003912 }
3913
3914 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003915
3916 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003917 pixman_region32_intersect_rect(&surface->input, &state->input,
3918 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003919
Pekka Paalanenbc106382012-10-10 12:49:31 +03003920 /* wl_surface.frame */
3921 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05003922 &state->frame_callback_list);
3923 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003924
3925 /* XXX:
3926 * What should happen with a feedback request, if there
3927 * is no wl_buffer attached for this commit?
3928 */
3929
3930 /* presentation.feedback */
3931 wl_list_insert_list(&surface->feedback_list,
3932 &state->feedback_list);
3933 wl_list_init(&state->feedback_list);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003934
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303935 /* weston_protected_surface.enforced/relaxed */
3936 if (surface->protection_mode != state->protection_mode)
3937 weston_surface_set_protection_mode(surface,
3938 state->protection_mode);
3939
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303940 /* weston_protected_surface.set_type */
3941 weston_surface_set_desired_protection(surface, state->desired_protection);
3942
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003943 wl_signal_emit(&surface->commit_signal, surface);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003944}
3945
3946static void
3947weston_surface_commit(struct weston_surface *surface)
3948{
3949 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003950
Pekka Paalanene67858b2013-04-25 13:57:42 +03003951 weston_surface_commit_subsurface_order(surface);
3952
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003953 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003954}
3955
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003956static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003957weston_subsurface_commit(struct weston_subsurface *sub);
3958
3959static void
3960weston_subsurface_parent_commit(struct weston_subsurface *sub,
3961 int parent_is_synchronized);
3962
3963static void
3964surface_commit(struct wl_client *client, struct wl_resource *resource)
3965{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003966 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003967 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
3968
leng.fang32af9fc2024-06-13 11:22:15 +08003969 weston_surface_commit_time(surface);
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003970 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
3971 assert(surface->viewport_resource);
3972
3973 wl_resource_post_error(surface->viewport_resource,
3974 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
3975 "wl_surface@%d has viewport source outside buffer",
3976 wl_resource_get_id(resource));
3977 return;
3978 }
3979
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003980 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
3981 assert(surface->viewport_resource);
3982
3983 wl_resource_post_error(surface->viewport_resource,
3984 WP_VIEWPORT_ERROR_BAD_SIZE,
3985 "wl_surface@%d viewport dst size not integer",
3986 wl_resource_get_id(resource));
3987 return;
3988 }
3989
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003990 if (surface->pending.acquire_fence_fd >= 0) {
3991 assert(surface->synchronization_resource);
3992
3993 if (!surface->pending.buffer) {
3994 fd_clear(&surface->pending.acquire_fence_fd);
3995 wl_resource_post_error(surface->synchronization_resource,
3996 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3997 "wl_surface@%"PRIu32" no buffer for synchronization",
3998 wl_resource_get_id(resource));
3999 return;
4000 }
4001
4002 /* We support fences for both wp_linux_dmabuf and opaque EGL
4003 * buffers, as mandated by minor version 2 of the
4004 * zwp_linux_explicit_synchronization_v1 protocol. Since
4005 * renderers that support fences currently only support these
4006 * two buffer types plus SHM buffers, we can just check for the
4007 * SHM buffer case here.
4008 */
4009 if (wl_shm_buffer_get(surface->pending.buffer->resource)) {
4010 fd_clear(&surface->pending.acquire_fence_fd);
4011 wl_resource_post_error(surface->synchronization_resource,
4012 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_UNSUPPORTED_BUFFER,
4013 "wl_surface@%"PRIu32" unsupported buffer for synchronization",
4014 wl_resource_get_id(resource));
4015 return;
4016 }
4017 }
4018
Alexandros Frantzis67629672018-10-19 12:14:11 +03004019 if (surface->pending.buffer_release_ref.buffer_release &&
4020 !surface->pending.buffer) {
4021 assert(surface->synchronization_resource);
4022
4023 wl_resource_post_error(surface->synchronization_resource,
4024 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
4025 "wl_surface@%"PRIu32" no buffer for synchronization",
4026 wl_resource_get_id(resource));
4027 return;
4028 }
4029
Pekka Paalanene67858b2013-04-25 13:57:42 +03004030 if (sub) {
leng.fang32af9fc2024-06-13 11:22:15 +08004031 weston_video_subsurface_commit(surface, sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004032 return;
4033 }
4034
4035 weston_surface_commit(surface);
4036
4037 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
4038 if (sub->surface != surface)
4039 weston_subsurface_parent_commit(sub, 0);
4040 }
4041}
4042
4043static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004044surface_set_buffer_transform(struct wl_client *client,
4045 struct wl_resource *resource, int transform)
4046{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004047 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004048
Jonny Lamba55f1392014-05-30 12:07:15 +02004049 /* if wl_output.transform grows more members this will need to be updated. */
4050 if (transform < 0 ||
4051 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
4052 wl_resource_post_error(resource,
4053 WL_SURFACE_ERROR_INVALID_TRANSFORM,
4054 "buffer transform must be a valid transform "
4055 "('%d' specified)", transform);
4056 return;
4057 }
4058
Pekka Paalanen952b6c82014-03-14 14:38:15 +02004059 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004060 surface->pending.buffer_viewport.changed = 1;
limin.tian531dd232025-01-07 12:09:27 +00004061 weston_log("%s %d transform:%d\n", __FUNCTION__,__LINE__,transform);
chen.wang1f06854b2024-09-26 11:26:04 +00004062 surface->video_transform = transform;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004063}
4064
Alexander Larsson4ea95522013-05-22 14:41:37 +02004065static void
4066surface_set_buffer_scale(struct wl_client *client,
4067 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004068 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004069{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004070 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02004071
Jonny Lamba55f1392014-05-30 12:07:15 +02004072 if (scale < 1) {
4073 wl_resource_post_error(resource,
4074 WL_SURFACE_ERROR_INVALID_SCALE,
4075 "buffer scale must be at least one "
4076 "('%d' specified)", scale);
4077 return;
4078 }
4079
Pekka Paalanen952b6c82014-03-14 14:38:15 +02004080 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004081 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02004082}
4083
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04004084static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004085 surface_destroy,
4086 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04004087 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004088 surface_frame,
4089 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03004090 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004091 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02004092 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06004093 surface_set_buffer_scale,
leng.fang32af9fc2024-06-13 11:22:15 +08004094 surface_damage_buffer,
4095 surface_set_video_plane,
4096 surface_set_pts,
4097 surface_keep_last_frame,
limin.tianc616dad2024-07-15 11:35:38 +00004098 surface_set_display_rate,
4099 surface_destroy_callback,
limin.tian230ec582024-07-30 09:35:17 +00004100 surface_enable_ll_mode,
chen.wang1d9c36b12024-08-05 11:45:27 +00004101 surface_set_invisible,
limin.tianbf71b4c2024-08-27 09:09:34 +00004102 surface_set_plane_mute,
chen.wang1fdc0f562024-09-04 07:57:16 +00004103 surface_enable_display_time_callback,
4104 surface_set_video_zorder
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004105};
4106
Leandro Ribeiro8eb84142021-01-18 19:36:48 -03004107static int
4108create_surface_dmabuf_feedback(struct weston_compositor *ec,
4109 struct weston_surface *surface)
4110{
4111 struct weston_dmabuf_feedback_tranche *tranche;
4112 dev_t main_device = ec->default_dmabuf_feedback->main_device;
4113 uint32_t flags = 0;
4114
4115 surface->dmabuf_feedback = weston_dmabuf_feedback_create(main_device);
4116 if (!surface->dmabuf_feedback)
4117 return -1;
4118
4119 tranche = weston_dmabuf_feedback_tranche_create(surface->dmabuf_feedback,
4120 ec->dmabuf_feedback_format_table,
4121 main_device, flags,
4122 RENDERER_PREF);
4123 if (!tranche) {
4124 weston_dmabuf_feedback_destroy(surface->dmabuf_feedback);
4125 surface->dmabuf_feedback = NULL;
4126 return -1;
4127 }
4128
4129 return 0;
4130}
4131
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004132static void
4133compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04004134 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004135{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04004136 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004137 struct weston_surface *surface;
Leandro Ribeiro8eb84142021-01-18 19:36:48 -03004138 int ret;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004139
Kristian Høgsberg18c93002012-01-27 11:58:31 -05004140 surface = weston_surface_create(ec);
Leandro Ribeiro8eb84142021-01-18 19:36:48 -03004141 if (surface == NULL)
4142 goto err;
4143
leng.fangdbaf6fa2024-06-20 19:31:04 +08004144 weston_compositor_add_surface_id(ec, surface);
4145
Leandro Ribeiro8eb84142021-01-18 19:36:48 -03004146 if (ec->default_dmabuf_feedback) {
4147 ret = create_surface_dmabuf_feedback(ec, surface);
4148 if (ret < 0)
4149 goto err_dmabuf_feedback;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04004150 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004151
Jason Ekstranda85118c2013-06-27 20:17:02 -05004152 surface->resource =
4153 wl_resource_create(client, &wl_surface_interface,
4154 wl_resource_get_version(resource), id);
Leandro Ribeiro8eb84142021-01-18 19:36:48 -03004155 if (surface->resource == NULL)
4156 goto err_dmabuf_feedback;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004157 wl_resource_set_implementation(surface->resource, &surface_interface,
4158 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07004159
4160 wl_signal_emit(&ec->create_surface_signal, surface);
Leandro Ribeiro8eb84142021-01-18 19:36:48 -03004161
4162 return;
4163
4164err_dmabuf_feedback:
4165 weston_surface_destroy(surface);
4166err:
4167 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004168}
4169
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004170static void
4171destroy_region(struct wl_resource *resource)
4172{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05004173 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004174
4175 pixman_region32_fini(&region->region);
4176 free(region);
4177}
4178
4179static void
4180region_destroy(struct wl_client *client, struct wl_resource *resource)
4181{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004182 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004183}
4184
4185static void
4186region_add(struct wl_client *client, struct wl_resource *resource,
4187 int32_t x, int32_t y, int32_t width, int32_t height)
4188{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05004189 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004190
4191 pixman_region32_union_rect(&region->region, &region->region,
4192 x, y, width, height);
4193}
4194
4195static void
4196region_subtract(struct wl_client *client, struct wl_resource *resource,
4197 int32_t x, int32_t y, int32_t width, int32_t height)
4198{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05004199 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004200 pixman_region32_t rect;
4201
4202 pixman_region32_init_rect(&rect, x, y, width, height);
4203 pixman_region32_subtract(&region->region, &region->region, &rect);
4204 pixman_region32_fini(&rect);
4205}
4206
4207static const struct wl_region_interface region_interface = {
4208 region_destroy,
4209 region_add,
4210 region_subtract
4211};
4212
4213static void
4214compositor_create_region(struct wl_client *client,
4215 struct wl_resource *resource, uint32_t id)
4216{
4217 struct weston_region *region;
4218
4219 region = malloc(sizeof *region);
4220 if (region == NULL) {
4221 wl_resource_post_no_memory(resource);
4222 return;
4223 }
4224
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004225 pixman_region32_init(&region->region);
4226
Jason Ekstranda85118c2013-06-27 20:17:02 -05004227 region->resource =
4228 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004229 if (region->resource == NULL) {
4230 free(region);
4231 wl_resource_post_no_memory(resource);
4232 return;
4233 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05004234 wl_resource_set_implementation(region->resource, &region_interface,
4235 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004236}
4237
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04004238static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004239 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004240 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004241};
4242
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004243static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03004244weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
4245{
4246 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004247
Jason Ekstrand7b982072014-05-20 14:33:03 -05004248 weston_surface_commit_state(surface, &sub->cached);
4249 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004250
4251 weston_surface_commit_subsurface_order(surface);
4252
4253 weston_surface_schedule_repaint(surface);
4254
Jason Ekstrand7b982072014-05-20 14:33:03 -05004255 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004256}
4257
4258static void
4259weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
4260{
4261 struct weston_surface *surface = sub->surface;
4262
4263 /*
4264 * If this commit would cause the surface to move by the
4265 * attach(dx, dy) parameters, the old damage region must be
4266 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05004267 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03004268 */
Derek Foreman152254b2015-11-26 14:17:48 -06004269 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004270 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06004271 pixman_region32_union(&sub->cached.damage_surface,
4272 &sub->cached.damage_surface,
4273 &surface->pending.damage_surface);
4274 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004275
4276 if (surface->pending.newly_attached) {
4277 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05004278 weston_surface_state_set_buffer(&sub->cached,
4279 surface->pending.buffer);
4280 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004281 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04004282 weston_presentation_feedback_discard_list(
4283 &sub->cached.feedback_list);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03004284 /* zwp_surface_synchronization_v1.set_acquire_fence */
4285 fd_move(&sub->cached.acquire_fence_fd,
4286 &surface->pending.acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03004287 /* zwp_surface_synchronization_v1.get_release */
4288 weston_buffer_release_move(&sub->cached.buffer_release_ref,
4289 &surface->pending.buffer_release_ref);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004290 }
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05304291 sub->cached.desired_protection = surface->pending.desired_protection;
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05304292 sub->cached.protection_mode = surface->pending.protection_mode;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03004293 assert(surface->pending.acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03004294 assert(surface->pending.buffer_release_ref.buffer_release == NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004295 sub->cached.sx += surface->pending.sx;
4296 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02004297
Derek Foreman152254b2015-11-26 14:17:48 -06004298 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
4299
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004300 sub->cached.buffer_viewport.changed |=
4301 surface->pending.buffer_viewport.changed;
4302 sub->cached.buffer_viewport.buffer =
4303 surface->pending.buffer_viewport.buffer;
4304 sub->cached.buffer_viewport.surface =
4305 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004306
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004307 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004308
4309 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
4310
4311 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
4312
4313 wl_list_insert_list(&sub->cached.frame_callback_list,
4314 &surface->pending.frame_callback_list);
4315 wl_list_init(&surface->pending.frame_callback_list);
4316
Pekka Paalanen133e4392014-09-23 22:08:46 -04004317 wl_list_insert_list(&sub->cached.feedback_list,
4318 &surface->pending.feedback_list);
4319 wl_list_init(&surface->pending.feedback_list);
4320
Jason Ekstrand7b982072014-05-20 14:33:03 -05004321 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004322}
4323
Derek Foreman280e7dd2014-10-03 13:13:42 -05004324static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03004325weston_subsurface_is_synchronized(struct weston_subsurface *sub)
4326{
4327 while (sub) {
4328 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05004329 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004330
4331 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05004332 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004333
4334 sub = weston_surface_to_subsurface(sub->parent);
4335 }
4336
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01004337 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004338}
4339
4340static void
4341weston_subsurface_commit(struct weston_subsurface *sub)
4342{
4343 struct weston_surface *surface = sub->surface;
4344 struct weston_subsurface *tmp;
4345
4346 /* Recursive check for effectively synchronized. */
4347 if (weston_subsurface_is_synchronized(sub)) {
4348 weston_subsurface_commit_to_cache(sub);
4349 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05004350 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03004351 /* flush accumulated state from cache */
4352 weston_subsurface_commit_to_cache(sub);
4353 weston_subsurface_commit_from_cache(sub);
4354 } else {
4355 weston_surface_commit(surface);
4356 }
4357
4358 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
4359 if (tmp->surface != surface)
4360 weston_subsurface_parent_commit(tmp, 0);
4361 }
4362 }
4363}
4364
4365static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004366weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004367{
4368 struct weston_surface *surface = sub->surface;
4369 struct weston_subsurface *tmp;
4370
Pekka Paalanene67858b2013-04-25 13:57:42 +03004371 /* From now on, commit_from_cache the whole sub-tree, regardless of
4372 * the synchronized mode of each child. This sub-surface or some
4373 * of its ancestors were synchronized, so we are synchronized
4374 * all the way down.
4375 */
4376
Jason Ekstrand7b982072014-05-20 14:33:03 -05004377 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004378 weston_subsurface_commit_from_cache(sub);
4379
4380 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
4381 if (tmp->surface != surface)
4382 weston_subsurface_parent_commit(tmp, 1);
4383 }
4384}
4385
4386static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004387weston_subsurface_parent_commit(struct weston_subsurface *sub,
4388 int parent_is_synchronized)
4389{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004390 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004391 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004392 wl_list_for_each(view, &sub->surface->views, surface_link)
4393 weston_view_set_position(view,
4394 sub->position.x,
4395 sub->position.y);
4396
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004397 sub->position.set = 0;
4398 }
4399
4400 if (parent_is_synchronized || sub->synchronized)
4401 weston_subsurface_synchronized_commit(sub);
4402}
4403
Pekka Paalanen8274d902014-08-06 19:36:51 +03004404static int
4405subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
4406{
4407 return snprintf(buf, len, "sub-surface");
4408}
4409
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004410static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004411subsurface_committed(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004412{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004413 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004414
Jason Ekstranda7af7042013-10-12 22:38:11 -05004415 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004416 weston_view_set_position(view,
4417 view->geometry.x + dx,
4418 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004419
4420 /* No need to check parent mappedness, because if parent is not
4421 * mapped, parent is not in a visible layer, so this sub-surface
4422 * will not be drawn either.
4423 */
Armin Krezovićf8486c32016-06-30 06:04:28 +02004424
Pekka Paalanene67858b2013-04-25 13:57:42 +03004425 if (!weston_surface_is_mapped(surface)) {
Armin Krezovićf8486c32016-06-30 06:04:28 +02004426 surface->is_mapped = true;
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03004427
Derek Foreman4b1a0a12014-09-10 15:37:33 -05004428 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03004429 * because that would call it also for the parent surface,
4430 * which might not be mapped yet. That would lead to
4431 * inconsistent state, where the window could never be
4432 * mapped.
4433 *
Armin Krezovićf8486c32016-06-30 06:04:28 +02004434 * Instead just force the is_mapped flag on, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03004435 * weston_surface_is_mapped() return true, so that when the
4436 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03004437 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03004438 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004439 }
4440}
4441
4442static struct weston_subsurface *
4443weston_surface_to_subsurface(struct weston_surface *surface)
4444{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004445 if (surface->committed == subsurface_committed)
4446 return surface->committed_private;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004447
4448 return NULL;
4449}
4450
Pekka Paalanen01388e22013-04-25 13:57:44 +03004451WL_EXPORT struct weston_surface *
4452weston_surface_get_main_surface(struct weston_surface *surface)
4453{
4454 struct weston_subsurface *sub;
4455
4456 while (surface && (sub = weston_surface_to_subsurface(surface)))
4457 surface = sub->parent;
4458
4459 return surface;
4460}
4461
Pekka Paalanen50b67472014-10-01 15:02:41 +03004462WL_EXPORT int
4463weston_surface_set_role(struct weston_surface *surface,
4464 const char *role_name,
4465 struct wl_resource *error_resource,
4466 uint32_t error_code)
4467{
4468 assert(role_name);
4469
4470 if (surface->role_name == NULL ||
4471 surface->role_name == role_name ||
4472 strcmp(surface->role_name, role_name) == 0) {
4473 surface->role_name = role_name;
4474
4475 return 0;
4476 }
4477
4478 wl_resource_post_error(error_resource, error_code,
4479 "Cannot assign role %s to wl_surface@%d,"
4480 " already has role %s\n",
4481 role_name,
4482 wl_resource_get_id(surface->resource),
4483 surface->role_name);
4484 return -1;
4485}
4486
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02004487WL_EXPORT const char *
4488weston_surface_get_role(struct weston_surface *surface)
4489{
4490 return surface->role_name;
4491}
4492
Pekka Paalanen8274d902014-08-06 19:36:51 +03004493WL_EXPORT void
4494weston_surface_set_label_func(struct weston_surface *surface,
4495 int (*desc)(struct weston_surface *,
4496 char *, size_t))
4497{
4498 surface->get_label = desc;
Marius Vlad5de92972019-10-15 13:25:41 +03004499 weston_timeline_refresh_subscription_objects(surface->compositor,
4500 surface);
Pekka Paalanen8274d902014-08-06 19:36:51 +03004501}
4502
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004503/** Get the size of surface contents
4504 *
4505 * \param surface The surface to query.
4506 * \param width Returns the width of raw contents.
4507 * \param height Returns the height of raw contents.
4508 *
4509 * Retrieves the raw surface content size in pixels for the given surface.
4510 * This is the whole content size in buffer pixels. If the surface
4511 * has no content or the renderer does not implement this feature,
4512 * zeroes are returned.
4513 *
4514 * This function is used to determine the buffer size needed for
4515 * a weston_surface_copy_content() call.
4516 */
4517WL_EXPORT void
4518weston_surface_get_content_size(struct weston_surface *surface,
4519 int *width, int *height)
4520{
4521 struct weston_renderer *rer = surface->compositor->renderer;
4522
4523 if (!rer->surface_get_content_size) {
4524 *width = 0;
4525 *height = 0;
4526 return;
4527 }
4528
4529 rer->surface_get_content_size(surface, width, height);
4530}
4531
Quentin Glidic248dd102016-08-12 10:41:34 +02004532/** Get the bounding box of a surface and its subsurfaces
4533 *
4534 * \param surface The surface to query.
4535 * \return The bounding box relative to the surface origin.
4536 *
4537 */
4538WL_EXPORT struct weston_geometry
4539weston_surface_get_bounding_box(struct weston_surface *surface)
4540{
4541 pixman_region32_t region;
4542 pixman_box32_t *box;
4543 struct weston_subsurface *subsurface;
4544
4545 pixman_region32_init_rect(&region,
4546 0, 0,
4547 surface->width, surface->height);
4548
4549 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link)
4550 pixman_region32_union_rect(&region, &region,
4551 subsurface->position.x,
4552 subsurface->position.y,
4553 subsurface->surface->width,
4554 subsurface->surface->height);
4555
4556 box = pixman_region32_extents(&region);
4557 struct weston_geometry geometry = {
4558 .x = box->x1,
4559 .y = box->y1,
4560 .width = box->x2 - box->x1,
4561 .height = box->y2 - box->y1,
4562 };
4563
4564 pixman_region32_fini(&region);
4565
4566 return geometry;
4567}
4568
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004569/** Copy surface contents to system memory.
4570 *
4571 * \param surface The surface to copy from.
4572 * \param target Pointer to the target memory buffer.
4573 * \param size Size of the target buffer in bytes.
4574 * \param src_x X location on contents to copy from.
4575 * \param src_y Y location on contents to copy from.
4576 * \param width Width in pixels of the area to copy.
4577 * \param height Height in pixels of the area to copy.
4578 * \return 0 for success, -1 for failure.
4579 *
4580 * Surface contents are maintained by the renderer. They can be in a
4581 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
4582 * else.
4583 *
4584 * Surface contents are copied into memory pointed to by target,
4585 * which has size bytes of space available. The target memory
4586 * may be larger than needed, but being smaller returns an error.
4587 * The extra bytes in target may or may not be written; their content is
4588 * unspecified. Size must be large enough to hold the image.
4589 *
4590 * The image in the target memory will be arranged in rows from
4591 * top to bottom, and pixels on a row from left to right. The pixel
4592 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
4593 * width * 4.
4594 *
4595 * Parameters src_x and src_y define the upper-left corner in buffer
4596 * coordinates (pixels) to copy from. Parameters width and height
4597 * define the size of the area to copy in pixels.
4598 *
4599 * The rectangle defined by src_x, src_y, width, height must fit in
4600 * the surface contents. Otherwise an error is returned.
4601 *
Changwoo Chof97d2502017-08-05 00:30:47 +09004602 * Use weston_surface_get_content_size to determine the content size; the
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004603 * needed target buffer size and rectangle limits.
4604 *
4605 * CURRENT IMPLEMENTATION RESTRICTIONS:
4606 * - the machine must be little-endian due to Pixman formats.
4607 *
4608 * NOTE: Pixman formats are premultiplied.
4609 */
4610WL_EXPORT int
4611weston_surface_copy_content(struct weston_surface *surface,
4612 void *target, size_t size,
4613 int src_x, int src_y,
4614 int width, int height)
4615{
4616 struct weston_renderer *rer = surface->compositor->renderer;
4617 int cw, ch;
4618 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
4619
4620 if (!rer->surface_copy_content)
4621 return -1;
4622
4623 weston_surface_get_content_size(surface, &cw, &ch);
4624
4625 if (src_x < 0 || src_y < 0)
4626 return -1;
4627
4628 if (width <= 0 || height <= 0)
4629 return -1;
4630
4631 if (src_x + width > cw || src_y + height > ch)
4632 return -1;
4633
4634 if (width * bytespp * height > size)
4635 return -1;
4636
4637 return rer->surface_copy_content(surface, target, size,
4638 src_x, src_y, width, height);
4639}
4640
Pekka Paalanene67858b2013-04-25 13:57:42 +03004641static void
4642subsurface_set_position(struct wl_client *client,
4643 struct wl_resource *resource, int32_t x, int32_t y)
4644{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004645 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004646
4647 if (!sub)
4648 return;
limin.tian531dd232025-01-07 12:09:27 +00004649 WESTON_LOGI(VIDEO_LOG, "x:%d, y:%d\n", x,y);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004650 sub->position.x = x;
4651 sub->position.y = y;
4652 sub->position.set = 1;
4653}
4654
4655static struct weston_subsurface *
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004656subsurface_find_sibling(struct weston_subsurface *sub,
4657 struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004658{
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004659 struct weston_surface *parent = sub->parent;
4660 struct weston_subsurface *sibling;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004661
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004662 wl_list_for_each(sibling, &parent->subsurface_list, parent_link) {
4663 if (sibling->surface == surface && sibling != sub)
4664 return sibling;
4665 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004666
4667 return NULL;
4668}
4669
4670static struct weston_subsurface *
4671subsurface_sibling_check(struct weston_subsurface *sub,
4672 struct weston_surface *surface,
4673 const char *request)
4674{
4675 struct weston_subsurface *sibling;
4676
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004677 sibling = subsurface_find_sibling(sub, surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004678 if (!sibling) {
4679 wl_resource_post_error(sub->resource,
4680 WL_SUBSURFACE_ERROR_BAD_SURFACE,
4681 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004682 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004683 return NULL;
4684 }
4685
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004686 assert(sibling->parent == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004687
4688 return sibling;
4689}
4690
4691static void
4692subsurface_place_above(struct wl_client *client,
4693 struct wl_resource *resource,
4694 struct wl_resource *sibling_resource)
4695{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004696 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004697 struct weston_surface *surface =
4698 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004699 struct weston_subsurface *sibling;
4700
4701 if (!sub)
4702 return;
4703
4704 sibling = subsurface_sibling_check(sub, surface, "place_above");
4705 if (!sibling)
4706 return;
4707
4708 wl_list_remove(&sub->parent_link_pending);
4709 wl_list_insert(sibling->parent_link_pending.prev,
4710 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004711
4712 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004713}
4714
4715static void
4716subsurface_place_below(struct wl_client *client,
4717 struct wl_resource *resource,
4718 struct wl_resource *sibling_resource)
4719{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004720 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004721 struct weston_surface *surface =
4722 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004723 struct weston_subsurface *sibling;
4724
4725 if (!sub)
4726 return;
4727
4728 sibling = subsurface_sibling_check(sub, surface, "place_below");
4729 if (!sibling)
4730 return;
4731
4732 wl_list_remove(&sub->parent_link_pending);
4733 wl_list_insert(&sibling->parent_link_pending,
4734 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004735
4736 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004737}
4738
4739static void
4740subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
4741{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004742 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004743
4744 if (sub)
4745 sub->synchronized = 1;
4746}
4747
4748static void
4749subsurface_set_desync(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
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004753 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03004754 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004755
4756 /* If sub became effectively desynchronized, flush. */
4757 if (!weston_subsurface_is_synchronized(sub))
4758 weston_subsurface_synchronized_commit(sub);
4759 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004760}
4761
4762static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03004763weston_subsurface_unlink_parent(struct weston_subsurface *sub)
4764{
4765 wl_list_remove(&sub->parent_link);
4766 wl_list_remove(&sub->parent_link_pending);
4767 wl_list_remove(&sub->parent_destroy_listener.link);
4768 sub->parent = NULL;
4769}
4770
4771static void
4772weston_subsurface_destroy(struct weston_subsurface *sub);
4773
4774static void
4775subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
4776{
4777 struct weston_subsurface *sub =
4778 container_of(listener, struct weston_subsurface,
4779 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004780 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004781
4782 /* The protocol object (wl_resource) is left inert. */
4783 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004784 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004785
4786 weston_subsurface_destroy(sub);
4787}
4788
4789static void
4790subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
4791{
4792 struct weston_subsurface *sub =
4793 container_of(listener, struct weston_subsurface,
4794 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004795 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004796 assert(sub->surface != sub->parent);
4797
4798 if (weston_surface_is_mapped(sub->surface))
4799 weston_surface_unmap(sub->surface);
4800
4801 weston_subsurface_unlink_parent(sub);
4802}
4803
4804static void
4805subsurface_resource_destroy(struct wl_resource *resource)
4806{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004807 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004808
4809 if (sub)
4810 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004811}
4812
4813static void
4814subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
4815{
4816 wl_resource_destroy(resource);
4817}
4818
4819static void
4820weston_subsurface_link_parent(struct weston_subsurface *sub,
4821 struct weston_surface *parent)
4822{
4823 sub->parent = parent;
4824 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004825 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004826 &sub->parent_destroy_listener);
4827
4828 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4829 wl_list_insert(&parent->subsurface_list_pending,
4830 &sub->parent_link_pending);
4831}
4832
4833static void
4834weston_subsurface_link_surface(struct weston_subsurface *sub,
4835 struct weston_surface *surface)
4836{
4837 sub->surface = surface;
4838 sub->surface_destroy_listener.notify =
4839 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004840 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004841 &sub->surface_destroy_listener);
4842}
4843
4844static void
4845weston_subsurface_destroy(struct weston_subsurface *sub)
4846{
leng.fang32af9fc2024-06-13 11:22:15 +08004847 weston_log("\nweston_subsurface_destroy sub:%p ->%p\n", sub, sub->surface);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004848 struct weston_view *view, *next;
4849
Pekka Paalanene67858b2013-04-25 13:57:42 +03004850 assert(sub->surface);
4851
4852 if (sub->resource) {
4853 assert(weston_surface_to_subsurface(sub->surface) == sub);
4854 assert(sub->parent_destroy_listener.notify ==
4855 subsurface_handle_parent_destroy);
4856
George Kiagiadakised04d382014-06-13 18:10:26 +02004857 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
4858 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004859 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02004860 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05004861
Pekka Paalanene67858b2013-04-25 13:57:42 +03004862 if (sub->parent)
4863 weston_subsurface_unlink_parent(sub);
4864
Jason Ekstrand7b982072014-05-20 14:33:03 -05004865 weston_surface_state_fini(&sub->cached);
4866 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004867
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004868 sub->surface->committed = NULL;
4869 sub->surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004870 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004871 } else {
4872 /* the dummy weston_subsurface for the parent itself */
4873 assert(sub->parent_destroy_listener.notify == NULL);
4874 wl_list_remove(&sub->parent_link);
4875 wl_list_remove(&sub->parent_link_pending);
4876 }
4877
4878 wl_list_remove(&sub->surface_destroy_listener.link);
4879 free(sub);
4880}
4881
4882static const struct wl_subsurface_interface subsurface_implementation = {
4883 subsurface_destroy,
4884 subsurface_set_position,
4885 subsurface_place_above,
4886 subsurface_place_below,
4887 subsurface_set_sync,
4888 subsurface_set_desync
4889};
4890
4891static struct weston_subsurface *
4892weston_subsurface_create(uint32_t id, struct weston_surface *surface,
4893 struct weston_surface *parent)
4894{
4895 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004896 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004897
Bryce Harringtonde16d892014-11-20 22:21:57 -08004898 sub = zalloc(sizeof *sub);
4899 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004900 return NULL;
4901
Jason Ekstranda7af7042013-10-12 22:38:11 -05004902 wl_list_init(&sub->unused_views);
4903
Jason Ekstranda85118c2013-06-27 20:17:02 -05004904 sub->resource =
4905 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004906 if (!sub->resource) {
4907 free(sub);
4908 return NULL;
4909 }
4910
Jason Ekstranda85118c2013-06-27 20:17:02 -05004911 wl_resource_set_implementation(sub->resource,
4912 &subsurface_implementation,
4913 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004914 weston_subsurface_link_surface(sub, surface);
4915 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05004916 weston_surface_state_init(&sub->cached);
4917 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004918 sub->synchronized = 1;
leng.fang32af9fc2024-06-13 11:22:15 +08004919 weston_log("\n weston_subsurface_create :%p sub:%p\n",surface, sub );
Pekka Paalanene67858b2013-04-25 13:57:42 +03004920
4921 return sub;
4922}
4923
4924/* Create a dummy subsurface for having the parent itself in its
4925 * sub-surface lists. Makes stacking order manipulation easy.
4926 */
4927static struct weston_subsurface *
4928weston_subsurface_create_for_parent(struct weston_surface *parent)
4929{
4930 struct weston_subsurface *sub;
4931
Bryce Harringtonde16d892014-11-20 22:21:57 -08004932 sub = zalloc(sizeof *sub);
4933 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004934 return NULL;
4935
4936 weston_subsurface_link_surface(sub, parent);
4937 sub->parent = parent;
4938 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4939 wl_list_insert(&parent->subsurface_list_pending,
4940 &sub->parent_link_pending);
4941
4942 return sub;
4943}
4944
4945static void
4946subcompositor_get_subsurface(struct wl_client *client,
4947 struct wl_resource *resource,
4948 uint32_t id,
4949 struct wl_resource *surface_resource,
4950 struct wl_resource *parent_resource)
4951{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004952 struct weston_surface *surface =
4953 wl_resource_get_user_data(surface_resource);
4954 struct weston_surface *parent =
4955 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004956 struct weston_subsurface *sub;
4957 static const char where[] = "get_subsurface: wl_subsurface@";
4958
4959 if (surface == parent) {
4960 wl_resource_post_error(resource,
4961 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4962 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004963 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004964 return;
4965 }
4966
4967 if (weston_surface_to_subsurface(surface)) {
4968 wl_resource_post_error(resource,
4969 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4970 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004971 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004972 return;
4973 }
4974
Pekka Paalanen50b67472014-10-01 15:02:41 +03004975 if (weston_surface_set_role(surface, "wl_subsurface", resource,
4976 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004977 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004978
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004979 if (weston_surface_get_main_surface(parent) == surface) {
4980 wl_resource_post_error(resource,
4981 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4982 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004983 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004984 return;
4985 }
4986
Pekka Paalanene67858b2013-04-25 13:57:42 +03004987 /* make sure the parent is in its own list */
4988 if (wl_list_empty(&parent->subsurface_list)) {
4989 if (!weston_subsurface_create_for_parent(parent)) {
4990 wl_resource_post_no_memory(resource);
4991 return;
4992 }
4993 }
4994
4995 sub = weston_subsurface_create(id, surface, parent);
4996 if (!sub) {
4997 wl_resource_post_no_memory(resource);
4998 return;
4999 }
5000
Quentin Glidic2edc3d52016-08-12 10:41:33 +02005001 surface->committed = subsurface_committed;
5002 surface->committed_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03005003 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03005004}
5005
5006static void
5007subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
5008{
5009 wl_resource_destroy(resource);
5010}
5011
5012static const struct wl_subcompositor_interface subcompositor_interface = {
5013 subcompositor_destroy,
5014 subcompositor_get_subsurface
5015};
5016
5017static void
5018bind_subcompositor(struct wl_client *client,
5019 void *data, uint32_t version, uint32_t id)
5020{
5021 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05005022 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03005023
Jason Ekstranda85118c2013-06-27 20:17:02 -05005024 resource =
5025 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07005026 if (resource == NULL) {
5027 wl_client_post_no_memory(client);
5028 return;
5029 }
5030 wl_resource_set_implementation(resource, &subcompositor_interface,
5031 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03005032}
5033
Bryce Harrington0795ece2016-08-30 12:04:26 -07005034/** Set a DPMS mode on all of the compositor's outputs
Bryce Harringtonc9626a32015-12-11 13:11:38 -08005035 *
5036 * \param compositor The compositor instance
5037 * \param state The DPMS state the outputs will be set to
5038 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03005039static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005040weston_compositor_dpms(struct weston_compositor *compositor,
5041 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005042{
Emmanuel Gil Peyroteff793a2021-07-31 17:25:41 +02005043 struct weston_output *output;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005044
Emmanuel Gil Peyroteff793a2021-07-31 17:25:41 +02005045 wl_list_for_each(output, &compositor->output_list, link)
Bryce Harrington08976ac2016-08-30 12:05:16 -07005046 if (output->set_dpms)
5047 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005048}
5049
Bryce Harringtonc9626a32015-12-11 13:11:38 -08005050/** Restores the compositor to active status
5051 *
5052 * \param compositor The compositor instance
5053 *
5054 * If the compositor was in a sleeping mode, all outputs are powered
5055 * back on via DPMS. Otherwise if the compositor was inactive
5056 * (idle/locked, offscreen, or sleeping) then the compositor's wake
5057 * signal will fire.
5058 *
5059 * Restarts the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005060 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08005061 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02005062WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005063weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02005064{
Neil Roberts8b62e202013-09-30 13:14:47 +01005065 uint32_t old_state = compositor->state;
5066
5067 /* The state needs to be changed before emitting the wake
5068 * signal because that may try to schedule a repaint which
5069 * will not work if the compositor is still sleeping */
5070 compositor->state = WESTON_COMPOSITOR_ACTIVE;
5071
5072 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005073 case WESTON_COMPOSITOR_SLEEPING:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005074 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01005075 case WESTON_COMPOSITOR_OFFSCREEN:
Daniel Stone893b9362016-11-08 15:47:09 +00005076 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005077 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005078 /* fall through */
5079 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005080 wl_event_source_timer_update(compositor->idle_source,
5081 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02005082 }
5083}
5084
Bryce Harringtonc9626a32015-12-11 13:11:38 -08005085/** Turns off rendering and frame events for the compositor.
5086 *
5087 * \param compositor The compositor instance
5088 *
5089 * This is used for example to prevent further rendering while the
5090 * compositor is shutting down.
5091 *
Bryce Harringtonc9626a32015-12-11 13:11:38 -08005092 * Stops the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005093 *
5094 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08005095 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005096WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01005097weston_compositor_offscreen(struct weston_compositor *compositor)
5098{
5099 switch (compositor->state) {
5100 case WESTON_COMPOSITOR_OFFSCREEN:
5101 return;
5102 case WESTON_COMPOSITOR_SLEEPING:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01005103 default:
5104 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
5105 wl_event_source_timer_update(compositor->idle_source, 0);
5106 }
5107}
5108
Bryce Harringtonc9626a32015-12-11 13:11:38 -08005109/** Powers down all attached output devices
5110 *
5111 * \param compositor The compositor instance
5112 *
5113 * Causes rendering to the outputs to cease, and no frame events to be
5114 * sent. Only powers down the outputs if the compositor is not already
5115 * in sleep mode.
5116 *
5117 * Stops the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005118 *
5119 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08005120 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01005121WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005122weston_compositor_sleep(struct weston_compositor *compositor)
5123{
5124 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
5125 return;
5126
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01005127 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02005128 compositor->state = WESTON_COMPOSITOR_SLEEPING;
5129 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
5130}
5131
Bryce Harringtonc9626a32015-12-11 13:11:38 -08005132/** Sets compositor to idle mode
5133 *
5134 * \param data The compositor instance
5135 *
5136 * This is called when the idle timer fires. Once the compositor is in
5137 * idle mode it requires a wake action (e.g. via
5138 * weston_compositor_wake()) to restore it. The compositor's
5139 * idle_signal will be triggered when the idle event occurs.
5140 *
5141 * Idleness can be inhibited by setting the compositor's idle_inhibit
5142 * property.
5143 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04005144static int
5145idle_handler(void *data)
5146{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005147 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04005148
5149 if (compositor->idle_inhibit)
5150 return 1;
5151
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005152 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005153 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04005154
5155 return 1;
5156}
5157
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005158WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08005159weston_plane_init(struct weston_plane *plane,
5160 struct weston_compositor *ec,
5161 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005162{
5163 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02005164 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005165 plane->x = x;
5166 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08005167 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03005168
5169 /* Init the link so that the call to wl_list_remove() when releasing
5170 * the plane without ever stacking doesn't lead to a crash */
5171 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005172}
5173
5174WL_EXPORT void
5175weston_plane_release(struct weston_plane *plane)
5176{
Xiong Zhang97116532013-10-23 13:58:31 +08005177 struct weston_view *view;
5178
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005179 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02005180 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03005181
Pekka Paalanen6551c092021-05-03 16:09:45 +03005182 /*
5183 * Can't use paint node list here, weston_plane is not specific to an
5184 * output.
5185 */
Xiong Zhang97116532013-10-23 13:58:31 +08005186 wl_list_for_each(view, &plane->compositor->view_list, link) {
5187 if (view->plane == plane)
5188 view->plane = NULL;
5189 }
5190
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03005191 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005192}
5193
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005194/** weston_compositor_stack_plane
5195 * \ingroup compositor
5196 */
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02005197WL_EXPORT void
5198weston_compositor_stack_plane(struct weston_compositor *ec,
5199 struct weston_plane *plane,
5200 struct weston_plane *above)
5201{
5202 if (above)
5203 wl_list_insert(above->link.prev, &plane->link);
5204 else
5205 wl_list_insert(&ec->plane_list, &plane->link);
5206}
5207
Quentin Glidic4ef719c2016-07-05 20:44:33 +02005208static void
5209output_release(struct wl_client *client, struct wl_resource *resource)
5210{
5211 wl_resource_destroy(resource);
5212}
5213
5214static const struct wl_output_interface output_interface = {
5215 output_release,
5216};
5217
5218
Casey Dahlin9074db52012-04-19 22:50:09 -04005219static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04005220{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05005221 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04005222}
5223
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005224static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005225bind_output(struct wl_client *client,
5226 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05005227{
Pekka Paalanen05347622017-03-27 12:24:34 +03005228 struct weston_head *head = data;
5229 struct weston_output *output = head->output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005230 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04005231 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05005232
Jason Ekstranda85118c2013-06-27 20:17:02 -05005233 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06005234 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07005235 if (resource == NULL) {
5236 wl_client_post_no_memory(client);
5237 return;
5238 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04005239
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005240 wl_list_insert(&head->resource_list, wl_resource_get_link(resource));
Pekka Paalanen055c1132017-03-27 16:31:25 +03005241 wl_resource_set_implementation(resource, &output_interface, head,
Pekka Paalanen05347622017-03-27 12:24:34 +03005242 unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04005243
Pekka Paalanen05347622017-03-27 12:24:34 +03005244 assert(output);
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05005245 wl_output_send_geometry(resource,
5246 output->x,
5247 output->y,
Pekka Paalanen01f60212017-03-24 15:39:24 +02005248 head->mm_width,
5249 head->mm_height,
5250 head->subpixel,
5251 head->make, head->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04005252 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04005253 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02005254 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02005255 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005256
5257 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05005258 wl_output_send_mode(resource,
5259 mode->flags,
5260 mode->width,
5261 mode->height,
5262 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005263 }
leng.fang32af9fc2024-06-13 11:22:15 +08005264 struct drm_output *drm_output = to_drm_output(output);
5265 int crtc_index = drm_output->crtc->pipe;
5266 if (version >= WL_OUTPUT_CRTC_INDEX_SINCE_VERSION)
5267 wl_output_send_crtc_index(resource, crtc_index);
Alexander Larsson4ea95522013-05-22 14:41:37 +02005268
Jasper St. Pierre0013a292014-08-07 16:43:11 -04005269 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02005270 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05005271}
5272
Pekka Paalanendcac3512017-12-08 14:13:34 +02005273static void
5274weston_head_add_global(struct weston_head *head)
5275{
5276 head->global = wl_global_create(head->compositor->wl_display,
5277 &wl_output_interface, 3,
5278 head, bind_output);
5279}
5280
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02005281/** Remove the global wl_output protocol object
5282 *
5283 * \param head The head whose global to remove.
5284 *
5285 * Also orphans the wl_resources for this head (wl_output).
5286 */
5287static void
5288weston_head_remove_global(struct weston_head *head)
5289{
5290 struct wl_resource *resource, *tmp;
5291
5292 if (head->global)
5293 wl_global_destroy(head->global);
5294 head->global = NULL;
5295
5296 wl_resource_for_each_safe(resource, tmp, &head->resource_list) {
5297 unbind_resource(resource);
5298 wl_resource_set_destructor(resource, NULL);
5299 wl_resource_set_user_data(resource, NULL);
5300 }
Roman Gilge97391c2019-03-29 13:01:06 +01005301
5302 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
5303 /* It's sufficient to unset the destructor, then the list elements
5304 * won't be accessed.
5305 */
5306 wl_resource_set_destructor(resource, NULL);
5307 }
5308 wl_list_init(&head->xdg_output_resource_list);
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02005309}
5310
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005311/** Get the backing object of wl_output
5312 *
5313 * \param resource A wl_output protocol object.
5314 * \return The backing object (user data) of a wl_resource representing a
5315 * wl_output protocol object.
Marius Vlad78984ee2019-06-11 00:05:08 +03005316 *
5317 * \ingroup head
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005318 */
Pekka Paalanen055c1132017-03-27 16:31:25 +03005319WL_EXPORT struct weston_head *
5320weston_head_from_resource(struct wl_resource *resource)
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005321{
5322 assert(wl_resource_instance_of(resource, &wl_output_interface,
5323 &output_interface));
5324
5325 return wl_resource_get_user_data(resource);
5326}
5327
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005328/** Initialize a pre-allocated weston_head
5329 *
5330 * \param head The head to initialize.
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005331 * \param name The head name, e.g. the connector name or equivalent.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005332 *
5333 * The head will be safe to attach, detach and release.
5334 *
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005335 * The name is used in logs, and can be used by compositors as a configuration
5336 * identifier.
5337 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005338 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005339 * \internal
5340 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005341WL_EXPORT void
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005342weston_head_init(struct weston_head *head, const char *name)
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005343{
5344 /* Add some (in)sane defaults which can be used
5345 * for checking if an output was properly configured
5346 */
5347 memset(head, 0, sizeof *head);
5348
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005349 wl_list_init(&head->compositor_link);
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005350 wl_signal_init(&head->destroy_signal);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005351 wl_list_init(&head->output_link);
5352 wl_list_init(&head->resource_list);
Roman Gilge97391c2019-03-29 13:01:06 +01005353 wl_list_init(&head->xdg_output_resource_list);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005354 head->name = strdup(name);
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305355 head->current_protection = WESTON_HDCP_DISABLE;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005356}
5357
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005358/** Send output heads changed signal
5359 *
5360 * \param output The output that changed.
5361 *
5362 * Notify that the enabled output gained and/or lost heads, or that the
5363 * associated heads may have changed their connection status. This does not
5364 * include cases where the output becomes enabled or disabled. The registered
5365 * callbacks are called after the change has successfully happened.
5366 *
5367 * If connection status change causes the compositor to attach or detach a head
5368 * to an enabled output, the registered callbacks may be called multiple times.
Marius Vlad55d87362019-06-11 01:15:35 +03005369 *
5370 * \ingroup output
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005371 */
5372static void
5373weston_output_emit_heads_changed(struct weston_output *output)
5374{
5375 wl_signal_emit(&output->compositor->output_heads_changed_signal,
5376 output);
5377}
5378
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005379/** Idle task for emitting heads_changed_signal */
5380static void
5381weston_compositor_call_heads_changed(void *data)
5382{
5383 struct weston_compositor *compositor = data;
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005384 struct weston_head *head;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005385
5386 compositor->heads_changed_source = NULL;
5387
5388 wl_signal_emit(&compositor->heads_changed_signal, compositor);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005389
5390 wl_list_for_each(head, &compositor->head_list, compositor_link) {
5391 if (head->output && head->output->enabled)
5392 weston_output_emit_heads_changed(head->output);
5393 }
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005394}
5395
5396/** Schedule a call on idle to heads_changed callback
5397 *
5398 * \param compositor The Compositor.
5399 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005400 * \ingroup compositor
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005401 * \internal
5402 */
5403static void
5404weston_compositor_schedule_heads_changed(struct weston_compositor *compositor)
5405{
5406 struct wl_event_loop *loop;
5407
5408 if (compositor->heads_changed_source)
5409 return;
5410
5411 loop = wl_display_get_event_loop(compositor->wl_display);
5412 compositor->heads_changed_source = wl_event_loop_add_idle(loop,
5413 weston_compositor_call_heads_changed, compositor);
5414}
5415
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005416/** Register a new head
5417 *
5418 * \param compositor The compositor.
5419 * \param head The head to register, must not be already registered.
5420 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005421 * This signals the core that a new head has become available, leading to
5422 * heads_changed hook being called later.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005423 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005424 * \ingroup compositor
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005425 * \internal
5426 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005427WL_EXPORT void
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005428weston_compositor_add_head(struct weston_compositor *compositor,
5429 struct weston_head *head)
5430{
5431 assert(wl_list_empty(&head->compositor_link));
5432 assert(head->name);
5433
5434 wl_list_insert(compositor->head_list.prev, &head->compositor_link);
5435 head->compositor = compositor;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005436 weston_compositor_schedule_heads_changed(compositor);
5437}
5438
5439/** Adds a listener to be called when heads change
5440 *
5441 * \param compositor The compositor.
5442 * \param listener The listener to add.
5443 *
Marius Vlada2dace22019-06-12 16:05:44 +03005444 * The listener notify function argument is weston_compositor.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005445 *
5446 * The listener function will be called after heads are added or their
5447 * connection status has changed. Several changes may be accumulated into a
5448 * single call. The user is expected to iterate over the existing heads and
5449 * check their statuses to find out what changed.
5450 *
5451 * \sa weston_compositor_iterate_heads, weston_head_is_connected,
5452 * weston_head_is_enabled
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005453 * \ingroup compositor
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005454 */
5455WL_EXPORT void
5456weston_compositor_add_heads_changed_listener(struct weston_compositor *compositor,
5457 struct wl_listener *listener)
5458{
5459 wl_signal_add(&compositor->heads_changed_signal, listener);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005460}
5461
5462/** Iterate over available heads
5463 *
5464 * \param compositor The compositor.
Marius Vlada2dace22019-06-12 16:05:44 +03005465 * \param iter The iterator, or NULL for start.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005466 * \return The next available head in the list.
5467 *
5468 * Returns all available heads, regardless of being connected or enabled.
5469 *
5470 * You can iterate over all heads as follows:
5471 * \code
5472 * struct weston_head *head = NULL;
5473 *
5474 * while ((head = weston_compositor_iterate_heads(compositor, head))) {
5475 * ...
5476 * }
5477 * \endcode
5478 *
5479 * If you cause \c iter to be removed from the list, you cannot use it to
5480 * continue iterating. Removing any other item is safe.
5481 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005482 * \ingroup compositor
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005483 */
5484WL_EXPORT struct weston_head *
5485weston_compositor_iterate_heads(struct weston_compositor *compositor,
5486 struct weston_head *iter)
5487{
5488 struct wl_list *list = &compositor->head_list;
5489 struct wl_list *node;
5490
5491 assert(compositor);
5492 assert(!iter || iter->compositor == compositor);
5493
5494 if (iter)
5495 node = iter->compositor_link.next;
5496 else
5497 node = list->next;
5498
5499 assert(node);
5500 assert(!iter || node != &iter->compositor_link);
5501
5502 if (node == list)
5503 return NULL;
5504
5505 return container_of(node, struct weston_head, compositor_link);
5506}
5507
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005508/** Iterate over attached heads
5509 *
5510 * \param output The output whose heads to iterate.
Marius Vlada2dace22019-06-12 16:05:44 +03005511 * \param iter The iterator, or NULL for start.
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005512 * \return The next attached head in the list.
5513 *
5514 * Returns all heads currently attached to the output.
5515 *
5516 * You can iterate over all heads as follows:
5517 * \code
5518 * struct weston_head *head = NULL;
5519 *
5520 * while ((head = weston_output_iterate_heads(output, head))) {
5521 * ...
5522 * }
5523 * \endcode
5524 *
5525 * If you cause \c iter to be removed from the list, you cannot use it to
5526 * continue iterating. Removing any other item is safe.
5527 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05005528 * \ingroup output
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005529 */
5530WL_EXPORT struct weston_head *
5531weston_output_iterate_heads(struct weston_output *output,
5532 struct weston_head *iter)
5533{
5534 struct wl_list *list = &output->head_list;
5535 struct wl_list *node;
5536
5537 assert(output);
5538 assert(!iter || iter->output == output);
5539
5540 if (iter)
5541 node = iter->output_link.next;
5542 else
5543 node = list->next;
5544
5545 assert(node);
5546 assert(!iter || node != &iter->output_link);
5547
5548 if (node == list)
5549 return NULL;
5550
5551 return container_of(node, struct weston_head, output_link);
5552}
5553
Pekka Paalanendcac3512017-12-08 14:13:34 +02005554/** Attach a head to an output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005555 *
5556 * \param output The output to attach to.
5557 * \param head The head that is not yet attached.
5558 * \return 0 on success, -1 on failure.
5559 *
5560 * Attaches the given head to the output. All heads of an output are clones
5561 * and share the resolution and timings.
5562 *
5563 * Cloning heads this way uses less resources than creating an output for
5564 * each head, but is not always possible due to environment, driver and hardware
5565 * limitations.
5566 *
5567 * On failure, the head remains unattached. Success of this function does not
5568 * guarantee the output configuration is actually valid. The final checks are
Pekka Paalanendcac3512017-12-08 14:13:34 +02005569 * made on weston_output_enable() unless the output was already enabled.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005570 *
Marius Vlad55d87362019-06-11 01:15:35 +03005571 * \ingroup output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005572 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005573WL_EXPORT int
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005574weston_output_attach_head(struct weston_output *output,
5575 struct weston_head *head)
5576{
Pekka Paalanendcac3512017-12-08 14:13:34 +02005577 char *head_names;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005578
5579 if (!wl_list_empty(&head->output_link))
5580 return -1;
5581
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005582 if (output->attach_head) {
5583 if (output->attach_head(output, head) < 0)
5584 return -1;
5585 } else if (!wl_list_empty(&output->head_list)) {
5586 /* No support for clones in the legacy path. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005587 return -1;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005588 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005589
5590 head->output = output;
5591 wl_list_insert(output->head_list.prev, &head->output_link);
5592
Pekka Paalanendcac3512017-12-08 14:13:34 +02005593 if (output->enabled) {
5594 weston_head_add_global(head);
5595
5596 head_names = weston_output_create_heads_string(output);
5597 weston_log("Output '%s' updated to have head(s) %s\n",
5598 output->name, head_names);
5599 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005600
5601 weston_output_emit_heads_changed(output);
Pekka Paalanendcac3512017-12-08 14:13:34 +02005602 }
5603
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005604 return 0;
5605}
5606
5607/** Detach a head from its output
5608 *
5609 * \param head The head to detach.
5610 *
5611 * It is safe to detach a non-attached head.
5612 *
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005613 * If the head is attached to an enabled output and the output will be left
5614 * with no heads, the output will be disabled.
5615 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005616 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005617 * \sa weston_output_disable
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005618 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005619WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005620weston_head_detach(struct weston_head *head)
5621{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005622 struct weston_output *output = head->output;
Pekka Paalanena0106992017-12-08 16:11:17 +02005623 char *head_names;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005624
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005625 wl_list_remove(&head->output_link);
5626 wl_list_init(&head->output_link);
5627 head->output = NULL;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005628
5629 if (!output)
5630 return;
5631
5632 if (output->detach_head)
5633 output->detach_head(output, head);
5634
5635 if (output->enabled) {
5636 weston_head_remove_global(head);
5637
Pekka Paalanena0106992017-12-08 16:11:17 +02005638 if (wl_list_empty(&output->head_list)) {
5639 weston_log("Output '%s' no heads left, disabling.\n",
5640 output->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005641 weston_output_disable(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02005642 } else {
5643 head_names = weston_output_create_heads_string(output);
5644 weston_log("Output '%s' updated to have head(s) %s\n",
5645 output->name, head_names);
5646 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005647
5648 weston_output_emit_heads_changed(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02005649 }
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005650 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005651}
5652
5653/** Destroy a head
5654 *
5655 * \param head The head to be released.
5656 *
5657 * Destroys the head. The caller is responsible for freeing the memory pointed
5658 * to by \c head.
5659 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005660 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005661 * \internal
5662 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005663WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005664weston_head_release(struct weston_head *head)
5665{
Marius Vladf2452d62021-12-09 14:07:21 +02005666 weston_signal_emit_mutable(&head->destroy_signal, head);
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005667
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005668 weston_head_detach(head);
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005669
5670 free(head->make);
5671 free(head->model);
5672 free(head->serial_number);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005673 free(head->name);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005674
5675 wl_list_remove(&head->compositor_link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005676}
5677
Pekka Paalanene19970f2017-08-28 14:11:02 +03005678static void
5679weston_head_set_device_changed(struct weston_head *head)
5680{
5681 head->device_changed = true;
5682
5683 if (head->compositor)
5684 weston_compositor_schedule_heads_changed(head->compositor);
5685}
5686
5687/** String equal comparison with NULLs being equal */
5688static bool
5689str_null_eq(const char *a, const char *b)
5690{
5691 if (!a && !b)
5692 return true;
5693
5694 if (!!a != !!b)
5695 return false;
5696
5697 return strcmp(a, b) == 0;
5698}
5699
Pekka Paalanen01f60212017-03-24 15:39:24 +02005700/** Store monitor make, model and serial number
5701 *
5702 * \param head The head to modify.
5703 * \param make The monitor make. If EDID is available, the PNP ID. Otherwise
5704 * any string, or NULL for none.
5705 * \param model The monitor model or name, or a made-up string, or NULL for
5706 * none.
5707 * \param serialno The monitor serial number, a made-up string, or NULL for
5708 * none.
5709 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005710 * This may set the device_changed flag.
5711 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005712 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005713 * \internal
5714 */
5715WL_EXPORT void
5716weston_head_set_monitor_strings(struct weston_head *head,
5717 const char *make,
5718 const char *model,
5719 const char *serialno)
5720{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005721 if (str_null_eq(head->make, make) &&
5722 str_null_eq(head->model, model) &&
5723 str_null_eq(head->serial_number, serialno))
5724 return;
5725
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005726 free(head->make);
5727 free(head->model);
5728 free(head->serial_number);
5729
5730 head->make = make ? strdup(make) : NULL;
5731 head->model = model ? strdup(model) : NULL;
5732 head->serial_number = serialno ? strdup(serialno) : NULL;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005733
5734 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005735}
5736
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005737/** Store display non-desktop status
5738 *
5739 * \param head The head to modify.
5740 * \param non_desktop Whether the head connects to a non-desktop display.
5741 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005742 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005743 * \internal
5744 */
5745WL_EXPORT void
5746weston_head_set_non_desktop(struct weston_head *head, bool non_desktop)
5747{
5748 if (head->non_desktop == non_desktop)
5749 return;
5750
5751 head->non_desktop = non_desktop;
5752
5753 weston_head_set_device_changed(head);
5754}
5755
Lucas Stacha69cb712019-11-25 23:29:31 +00005756/** Store display transformation
5757 *
5758 * \param head The head to modify.
5759 * \param transform The transformation to apply for this head
5760 *
5761 * This may set the device_changed flag.
5762 *
5763 * \ingroup head
5764 * \internal
5765 */
5766WL_EXPORT void
5767weston_head_set_transform(struct weston_head *head, uint32_t transform)
5768{
5769 if (head->transform == transform)
5770 return;
5771
5772 head->transform = transform;
5773
5774 weston_head_set_device_changed(head);
5775}
5776
5777
Pekka Paalanen01f60212017-03-24 15:39:24 +02005778/** Store physical image size
5779 *
5780 * \param head The head to modify.
5781 * \param mm_width Image area width in millimeters.
5782 * \param mm_height Image area height in millimeters.
5783 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005784 * This may set the device_changed flag.
5785 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005786 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005787 * \internal
5788 */
5789WL_EXPORT void
5790weston_head_set_physical_size(struct weston_head *head,
5791 int32_t mm_width, int32_t mm_height)
5792{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005793 if (head->mm_width == mm_width &&
5794 head->mm_height == mm_height)
5795 return;
5796
Pekka Paalanen01f60212017-03-24 15:39:24 +02005797 head->mm_width = mm_width;
5798 head->mm_height = mm_height;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005799
5800 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005801}
5802
5803/** Store monitor sub-pixel layout
5804 *
5805 * \param head The head to modify.
5806 * \param sp Sub-pixel layout. The possible values are:
5807 * - WL_OUTPUT_SUBPIXEL_UNKNOWN,
5808 * - WL_OUTPUT_SUBPIXEL_NONE,
5809 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB,
5810 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR,
5811 * - WL_OUTPUT_SUBPIXEL_VERTICAL_RGB,
5812 * - WL_OUTPUT_SUBPIXEL_VERTICAL_BGR
5813 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005814 * This may set the device_changed flag.
5815 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005816 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005817 * \internal
5818 */
5819WL_EXPORT void
5820weston_head_set_subpixel(struct weston_head *head,
5821 enum wl_output_subpixel sp)
5822{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005823 if (head->subpixel == sp)
5824 return;
5825
Pekka Paalanen01f60212017-03-24 15:39:24 +02005826 head->subpixel = sp;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005827
5828 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005829}
5830
5831/** Mark the monitor as internal
5832 *
5833 * This is used for embedded screens, like laptop panels.
5834 *
5835 * \param head The head to mark as internal.
5836 *
5837 * By default a head is external. The type is often inferred from the physical
5838 * connector type.
5839 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005840 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005841 * \internal
5842 */
5843WL_EXPORT void
5844weston_head_set_internal(struct weston_head *head)
5845{
5846 head->connection_internal = true;
5847}
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005848
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005849/** Store connector status
5850 *
5851 * \param head The head to modify.
5852 * \param connected Whether the head is connected.
5853 *
5854 * Connectors are created as disconnected. This function can be used to
5855 * set the connector status.
5856 *
5857 * The status should be set to true when a physical connector is connected to
5858 * a video sink device like a monitor and to false when the connector is
5859 * disconnected. For nested backends, the connection status should reflect the
5860 * connection to the parent display server.
5861 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005862 * When the connection status changes, it schedules a call to the heads_changed
Pekka Paalanene19970f2017-08-28 14:11:02 +03005863 * hook and sets the device_changed flag.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005864 *
5865 * \sa weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005866 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005867 * \internal
5868 */
5869WL_EXPORT void
5870weston_head_set_connection_status(struct weston_head *head, bool connected)
5871{
limin.tianaaaff782024-12-20 09:49:25 +00005872 if (head->connected == connected && !head->need_change)
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005873 return;
5874
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005875 head->connected = connected;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005876
Pekka Paalanene19970f2017-08-28 14:11:02 +03005877 weston_head_set_device_changed(head);
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005878}
5879
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305880static void
5881weston_output_compute_protection(struct weston_output *output)
5882{
5883 struct weston_head *head;
5884 enum weston_hdcp_protection op_protection;
5885 bool op_protection_valid = false;
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305886 struct weston_compositor *wc = output->compositor;
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305887
5888 wl_list_for_each(head, &output->head_list, output_link) {
5889 if (!op_protection_valid) {
5890 op_protection = head->current_protection;
5891 op_protection_valid = true;
5892 }
5893 if (head->current_protection < op_protection)
5894 op_protection = head->current_protection;
5895 }
5896
5897 if (!op_protection_valid)
5898 op_protection = WESTON_HDCP_DISABLE;
5899
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305900 if (output->current_protection != op_protection) {
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305901 output->current_protection = op_protection;
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305902 weston_output_damage(output);
Ankit Nautiyalfaa5ab42019-07-08 13:23:24 +05305903 weston_schedule_surface_protection_update(wc);
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305904 }
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305905}
5906
5907WL_EXPORT void
5908weston_head_set_content_protection_status(struct weston_head *head,
5909 enum weston_hdcp_protection status)
5910{
5911 head->current_protection = status;
5912 if (head->output)
5913 weston_output_compute_protection(head->output);
5914}
5915
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005916/** Is the head currently connected?
5917 *
5918 * \param head The head to query.
5919 * \return Connection status.
5920 *
5921 * Returns true if the head is physically connected to a monitor, or in
5922 * case of a nested backend returns true when there is a connection to the
5923 * parent display server.
5924 *
5925 * This is independent from the head being enabled.
5926 *
5927 * \sa weston_head_is_enabled
Marius Vlad78984ee2019-06-11 00:05:08 +03005928 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005929 */
5930WL_EXPORT bool
5931weston_head_is_connected(struct weston_head *head)
5932{
5933 return head->connected;
5934}
5935
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005936/** Is the head currently enabled?
5937 *
5938 * \param head The head to query.
5939 * \return Video status.
5940 *
5941 * Returns true if the head is currently transmitting a video stream.
5942 *
5943 * This is independent of the head being connected.
5944 *
5945 * \sa weston_head_is_connected
Marius Vlad78984ee2019-06-11 00:05:08 +03005946 * \ingroup head
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005947 */
5948WL_EXPORT bool
5949weston_head_is_enabled(struct weston_head *head)
5950{
5951 if (!head->output)
5952 return false;
5953
5954 return head->output->enabled;
5955}
5956
Pekka Paalanene19970f2017-08-28 14:11:02 +03005957/** Has the device information changed?
5958 *
5959 * \param head The head to query.
5960 * \return True if the device information has changed since last reset.
5961 *
5962 * The information about the connected display device, e.g. a monitor, may
5963 * change without being disconnected in between. Changing information
5964 * causes a call to the heads_changed hook.
5965 *
5966 * The information includes make, model, serial number, physical size,
5967 * and sub-pixel type. The connection status is also included.
5968 *
5969 * \sa weston_head_reset_device_changed, weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005970 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03005971 */
5972WL_EXPORT bool
5973weston_head_is_device_changed(struct weston_head *head)
5974{
5975 return head->device_changed;
5976}
5977
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005978/** Does the head represent a non-desktop display?
5979 *
5980 * \param head The head to query.
5981 * \return True if the device is a non-desktop display.
5982 *
5983 * Non-desktop heads are not attached to outputs by default.
5984 * This stops weston from extending the desktop onto head mounted displays.
5985 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005986 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005987 */
5988WL_EXPORT bool
5989weston_head_is_non_desktop(struct weston_head *head)
5990{
5991 return head->non_desktop;
5992}
5993
Pekka Paalanene19970f2017-08-28 14:11:02 +03005994/** Acknowledge device information change
5995 *
5996 * \param head The head to acknowledge.
5997 *
5998 * Clears the device changed flag on this head. When a compositor has processed
5999 * device information, it should call this to be able to notice further
6000 * changes.
6001 *
6002 * \sa weston_head_is_device_changed
Marius Vlad78984ee2019-06-11 00:05:08 +03006003 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03006004 */
6005WL_EXPORT void
6006weston_head_reset_device_changed(struct weston_head *head)
6007{
6008 head->device_changed = false;
6009}
6010
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006011/** Get the name of a head
6012 *
6013 * \param head The head to query.
6014 * \return The head's name, not NULL.
6015 *
6016 * The name depends on the backend. The DRM backend uses connector names,
6017 * other backends may use hardcoded names or user-given names.
Marius Vlad78984ee2019-06-11 00:05:08 +03006018 *
6019 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006020 */
6021WL_EXPORT const char *
6022weston_head_get_name(struct weston_head *head)
6023{
6024 return head->name;
6025}
6026
6027/** Get the output the head is attached to
6028 *
6029 * \param head The head to query.
6030 * \return The output the head is attached to, or NULL if detached.
Marius Vlad78984ee2019-06-11 00:05:08 +03006031 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006032 */
6033WL_EXPORT struct weston_output *
6034weston_head_get_output(struct weston_head *head)
6035{
6036 return head->output;
6037}
6038
Lucas Stacha69cb712019-11-25 23:29:31 +00006039/** Get the head's native transformation
6040 *
6041 * \param head The head to query.
6042 * \return The head's native transform, as a WL_OUTPUT_TRANSFORM_* value
6043 *
6044 * A weston_head may have a 'native' transform provided by the backend.
6045 * Examples include panels which are physically rotated, where the rotation
6046 * is recorded and described as part of the system configuration. This call
6047 * will return any known native transform for the head.
6048 *
6049 * \ingroup head
6050 */
6051WL_EXPORT uint32_t
6052weston_head_get_transform(struct weston_head *head)
6053{
6054 return head->transform;
6055}
6056
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03006057/** Add destroy callback for a head
6058 *
6059 * \param head The head to watch for.
6060 * \param listener The listener to add. The \c notify member must be set.
6061 *
6062 * Heads may get destroyed for various reasons by the backends. If a head is
6063 * attached to an output, the compositor should listen for head destruction
6064 * and reconfigure or destroy the output if necessary.
6065 *
6066 * The destroy callbacks will be called on weston_head destruction before any
6067 * automatic detaching from an associated weston_output and before any
6068 * weston_head information is lost.
6069 *
6070 * The \c data argument to the notify callback is the weston_head being
6071 * destroyed.
Marius Vlad78984ee2019-06-11 00:05:08 +03006072 *
6073 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03006074 */
6075WL_EXPORT void
6076weston_head_add_destroy_listener(struct weston_head *head,
6077 struct wl_listener *listener)
6078{
6079 wl_signal_add(&head->destroy_signal, listener);
6080}
6081
6082/** Look up destroy listener for a head
6083 *
6084 * \param head The head to query.
6085 * \param notify The notify function used used for the added destroy listener.
6086 * \return The listener, or NULL if not found.
6087 *
6088 * This looks up the previously added destroy listener struct based on the
6089 * notify function it has. The listener can be used to access user data
6090 * through \c container_of().
6091 *
6092 * \sa wl_signal_get()
Marius Vlad78984ee2019-06-11 00:05:08 +03006093 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03006094 */
6095WL_EXPORT struct wl_listener *
6096weston_head_get_destroy_listener(struct weston_head *head,
6097 wl_notify_func_t notify)
6098{
6099 return wl_signal_get(&head->destroy_signal, notify);
6100}
6101
David Fort0de859e2016-05-27 23:22:57 +02006102/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02006103static void
David Fort0de859e2016-05-27 23:22:57 +02006104weston_compositor_reflow_outputs(struct weston_compositor *compositor,
6105 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02006106{
6107 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02006108 bool start_resizing = false;
6109
6110 if (!delta_width)
6111 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02006112
6113 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02006114 if (output == resized_output) {
6115 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02006116 continue;
6117 }
6118
David Fort0de859e2016-05-27 23:22:57 +02006119 if (start_resizing) {
6120 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02006121 output->dirty = 1;
6122 }
6123 }
6124}
6125
Pekka Paalanen06920792020-12-08 15:22:36 +02006126/** Transform a region in-place from global to output coordinates
6127 *
6128 * \param output The output that defines the transformation.
6129 * \param region The region to be transformed in-place.
6130 *
6131 * This takes a region in the global coordinate system, and takes into account
6132 * output position, transform and scale, and the zoom, and converts the region
6133 * into output pixel coordinates in the framebuffer.
6134 *
6135 * Uses floating-point operations if zoom is active, which may round to expand
6136 * the region.
6137 *
6138 * \internal
6139 * \ingroup output
6140 */
6141WL_EXPORT void
6142weston_output_region_from_global(struct weston_output *output,
6143 pixman_region32_t *region)
6144{
6145 if (output->zoom.active) {
6146 weston_matrix_transform_region(region, &output->matrix, region);
6147 } else {
6148 pixman_region32_translate(region, -output->x, -output->y);
6149 weston_transformed_region(output->width, output->height,
6150 output->transform,
6151 output->current_scale,
6152 region, region);
6153 }
6154}
6155
Pekka Paalanend72bad22017-03-29 17:01:41 +03006156static void
Scott Moreauccbf29d2012-02-22 14:21:41 -07006157weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01006158{
Scott Moreau850ca422012-05-21 15:21:25 -06006159 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05006160
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006161 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05006162 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06006163
Scott Moreauccbf29d2012-02-22 14:21:41 -07006164 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06006165 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05006166 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01006167 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05006168 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01006169 weston_matrix_scale(&output->matrix, magnification,
6170 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07006171 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04006172
Jason Ekstrandfb23df72014-10-16 10:55:21 -05006173 switch (output->transform) {
6174 case WL_OUTPUT_TRANSFORM_FLIPPED:
6175 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
6176 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
6177 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
6178 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
6179 weston_matrix_scale(&output->matrix, -1, 1, 1);
6180 break;
6181 }
6182
6183 switch (output->transform) {
6184 default:
6185 case WL_OUTPUT_TRANSFORM_NORMAL:
6186 case WL_OUTPUT_TRANSFORM_FLIPPED:
6187 break;
6188 case WL_OUTPUT_TRANSFORM_90:
6189 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +02006190 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
6191 weston_matrix_rotate_xy(&output->matrix, 0, -1);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05006192 break;
6193 case WL_OUTPUT_TRANSFORM_180:
6194 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
6195 weston_matrix_translate(&output->matrix,
6196 -output->width, -output->height, 0);
6197 weston_matrix_rotate_xy(&output->matrix, -1, 0);
6198 break;
6199 case WL_OUTPUT_TRANSFORM_270:
6200 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +02006201 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
6202 weston_matrix_rotate_xy(&output->matrix, 0, 1);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05006203 break;
6204 }
6205
6206 if (output->current_scale != 1)
6207 weston_matrix_scale(&output->matrix,
6208 output->current_scale,
6209 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01006210
Scott Moreauccbf29d2012-02-22 14:21:41 -07006211 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05006212
6213 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07006214}
6215
Scott Moreau1bad5db2012-08-18 01:04:05 -06006216static void
Alexander Larsson0b135062013-05-28 16:23:36 +02006217weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06006218{
6219 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03006220 output->native_scale = scale;
6221 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06006222
Pekka Paalanen59987fa2016-04-26 15:50:59 +03006223 convert_size_by_transform_scale(&output->width, &output->height,
6224 output->current_mode->width,
6225 output->current_mode->height,
6226 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02006227}
6228
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006229static void
6230weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07006231{
6232 output->x = x;
6233 output->y = y;
6234
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006235 pixman_region32_fini(&output->region);
Scott Moreauccbf29d2012-02-22 14:21:41 -07006236 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06006237 output->width,
6238 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01006239}
6240
Marius Vlad55d87362019-06-11 01:15:35 +03006241/**
6242 * \ingroup output
6243 */
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006244WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006245weston_output_move(struct weston_output *output, int x, int y)
6246{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006247 struct weston_head *head;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006248 struct wl_resource *resource;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006249 int ver;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006250
6251 output->move_x = x - output->x;
6252 output->move_y = y - output->y;
6253
6254 if (output->move_x == 0 && output->move_y == 0)
6255 return;
6256
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006257 weston_output_init_geometry(output, x, y);
6258
6259 output->dirty = 1;
6260
6261 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006262 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006263
6264 /* Notify clients of the change for output position. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006265 wl_list_for_each(head, &output->head_list, output_link) {
6266 wl_resource_for_each(resource, &head->resource_list) {
6267 wl_output_send_geometry(resource,
6268 output->x,
6269 output->y,
6270 head->mm_width,
6271 head->mm_height,
6272 head->subpixel,
6273 head->make,
6274 head->model,
6275 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08006276
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006277 ver = wl_resource_get_version(resource);
6278 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
6279 wl_output_send_done(resource);
6280 }
Roman Gilge97391c2019-03-29 13:01:06 +01006281
6282 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
6283 zxdg_output_v1_send_logical_position(resource,
6284 output->x,
6285 output->y);
6286 zxdg_output_v1_send_done(resource);
6287 }
Quanxian Wangb2c86362014-03-14 09:16:25 +08006288 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006289}
6290
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006291/** Signal that a pending output is taken into use.
6292 *
6293 * Removes the output from the pending list and adds it to the compositor's
6294 * list of enabled outputs. The output created signal is emitted.
Giulio Camuffob1147152015-05-06 21:41:57 +03006295 *
Pekka Paalanen2210ad02017-03-30 15:48:06 +03006296 * The output gets an internal ID assigned, and the wl_output global is
6297 * created.
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03006298 *
Giulio Camuffob1147152015-05-06 21:41:57 +03006299 * \param compositor The compositor instance.
6300 * \param output The output to be added.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006301 *
6302 * \internal
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006303 * \ingroup compositor
Giulio Camuffob1147152015-05-06 21:41:57 +03006304 */
Pekka Paalanenf9681b52017-03-29 16:58:48 +03006305static void
Giulio Camuffob1147152015-05-06 21:41:57 +03006306weston_compositor_add_output(struct weston_compositor *compositor,
6307 struct weston_output *output)
6308{
Armin Krezoviće5403842016-08-05 15:28:29 +02006309 struct weston_view *view, *next;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03006310 struct weston_head *head;
Armin Krezoviće5403842016-08-05 15:28:29 +02006311
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006312 assert(!output->enabled);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03006313
6314 /* Verify we haven't reached the limit of 32 available output IDs */
6315 assert(ffs(~compositor->output_id_pool) > 0);
6316
6317 /* Invert the output id pool and look for the lowest numbered
6318 * switch (the least significant bit). Take that bit's position
6319 * as our ID, and mark it used in the compositor's output_id_pool.
6320 */
6321 output->id = ffs(~compositor->output_id_pool) - 1;
6322 compositor->output_id_pool |= 1u << output->id;
6323
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006324 wl_list_remove(&output->link);
Giulio Camuffob1147152015-05-06 21:41:57 +03006325 wl_list_insert(compositor->output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006326 output->enabled = true;
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006327
Pekka Paalanendcac3512017-12-08 14:13:34 +02006328 wl_list_for_each(head, &output->head_list, output_link)
6329 weston_head_add_global(head);
Pekka Paalanen2210ad02017-03-30 15:48:06 +03006330
Giulio Camuffob1147152015-05-06 21:41:57 +03006331 wl_signal_emit(&compositor->output_created_signal, output);
Armin Krezoviće5403842016-08-05 15:28:29 +02006332
Pekka Paalanen6551c092021-05-03 16:09:45 +03006333 /*
6334 * Use view_list, as paint nodes have not been created for this
6335 * output yet. Any existing view might touch this new output.
6336 */
Armin Krezoviće5403842016-08-05 15:28:29 +02006337 wl_list_for_each_safe(view, next, &compositor->view_list, link)
6338 weston_view_geometry_dirty(view);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04006339}
6340
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006341/** Transform device coordinates into global coordinates
6342 *
Marius Vlada2dace22019-06-12 16:05:44 +03006343 * \param output the weston_output object
6344 * \param[in] device_x X coordinate in device units.
6345 * \param[in] device_y Y coordinate in device units.
6346 * \param[out] x X coordinate in the global space.
6347 * \param[out] y Y coordinate in the global space.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006348 *
Marius Vlada2dace22019-06-12 16:05:44 +03006349 * Transforms coordinates from the device coordinate space (physical pixel
6350 * units) to the global coordinate space (logical pixel units). This takes
6351 * into account output transform and scale.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006352 *
Marius Vlad55d87362019-06-11 01:15:35 +03006353 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006354 * \internal
6355 */
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006356WL_EXPORT void
6357weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02006358 double device_x, double device_y,
6359 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006360{
Derek Foreman0f679412014-10-02 13:41:17 -05006361 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02006362 device_x,
6363 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05006364 0.0,
6365 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006366
Derek Foreman67a18b92015-03-24 11:36:14 -05006367 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006368
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02006369 *x = p.f[0] / p.f[3];
6370 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006371}
6372
Pekka Paalanen1d2eee22021-02-25 12:03:28 +02006373static void
6374weston_output_reset_color_transforms(struct weston_output *output)
6375{
Pekka Paalanencda39512021-03-22 17:32:07 +02006376 weston_color_transform_unref(output->from_sRGB_to_output);
6377 output->from_sRGB_to_output = NULL;
Pekka Paalanen8fb23ed2021-03-23 13:40:43 +02006378 weston_color_transform_unref(output->from_sRGB_to_blend);
6379 output->from_sRGB_to_blend = NULL;
Pekka Paalanen1d2eee22021-02-25 12:03:28 +02006380 weston_color_transform_unref(output->from_blend_to_output);
6381 output->from_blend_to_output = NULL;
6382}
6383
Pekka Paalanen9a0aa372021-06-08 13:38:35 +03006384static bool
6385weston_output_set_color_transforms(struct weston_output *output)
6386{
6387 struct weston_color_manager *cm = output->compositor->color_manager;
6388 struct weston_color_transform *blend_to_output = NULL;
6389 struct weston_color_transform *sRGB_to_output = NULL;
6390 struct weston_color_transform *sRGB_to_blend = NULL;
6391 bool ok;
6392
6393 ok = cm->get_output_color_transform(cm, output, &blend_to_output);
6394 ok = ok && cm->get_sRGB_to_output_color_transform(cm, output,
6395 &sRGB_to_output);
6396 ok = ok && cm->get_sRGB_to_blend_color_transform(cm, output,
6397 &sRGB_to_blend);
6398 if (!ok) {
6399 weston_log("Creating color transformation for output \"%s\" failed.\n",
6400 output->name);
6401 weston_color_transform_unref(blend_to_output);
6402 weston_color_transform_unref(sRGB_to_output);
6403 weston_color_transform_unref(sRGB_to_blend);
6404
6405 return false;
6406 }
6407
6408 weston_output_reset_color_transforms(output);
6409 output->from_blend_to_output = blend_to_output;
6410 output->from_blend_to_output_by_backend = false;
6411 output->from_sRGB_to_output = sRGB_to_output;
6412 output->from_sRGB_to_blend = sRGB_to_blend;
6413
Pekka Paalanen9a9e6ce2021-06-08 14:17:44 +03006414 weston_log("Output '%s' using color profile: %s\n", output->name,
6415 weston_color_profile_get_description(output->color_profile));
6416
Pekka Paalanen9a0aa372021-06-08 13:38:35 +03006417 return true;
6418}
6419
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006420/** Removes output from compositor's list of enabled outputs
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006421 *
6422 * \param output The weston_output object that is being removed.
6423 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006424 * The following happens:
6425 *
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03006426 * - Destroys all paint nodes related to the output.
6427 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006428 * - The output assignments of all views in the current scenegraph are
6429 * recomputed.
6430 *
Pekka Paalanen1d2eee22021-02-25 12:03:28 +02006431 * - Destroys output's color transforms.
6432 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006433 * - Presentation feedback is discarded.
6434 *
6435 * - Compositor is notified that outputs were changed and
6436 * applies the necessary changes to re-layout outputs.
6437 *
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006438 * - The output is put back in the pending outputs list.
6439 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006440 * - Signal is emitted to notify all users of the weston_output
6441 * object that the output is being destroyed.
6442 *
6443 * - wl_output protocol objects referencing this weston_output
Pekka Paalanen2210ad02017-03-30 15:48:06 +03006444 * are made inert, and the wl_output global is removed.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006445 *
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03006446 * - The output's internal ID is released.
6447 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006448 * \ingroup compositor
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006449 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006450 */
6451static void
6452weston_compositor_remove_output(struct weston_output *output)
6453{
Pekka Paalanenbccda712017-03-29 16:16:04 +03006454 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03006455 struct weston_paint_node *pnode, *pntmp;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006456 struct weston_view *view;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03006457 struct weston_head *head;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006458
6459 assert(output->destroying);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006460 assert(output->enabled);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006461
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03006462 wl_list_for_each_safe(pnode, pntmp,
6463 &output->paint_node_list, output_link) {
6464 weston_paint_node_destroy(pnode);
6465 }
Pekka Paalanen2fddc532021-04-30 17:41:29 +03006466 assert(wl_list_empty(&output->paint_node_z_order_list));
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03006467
Pekka Paalanen6551c092021-05-03 16:09:45 +03006468 /*
6469 * Use view_list in case the output did not go through repaint
6470 * after a view came on it, lacking a paint node. Just to be sure.
6471 */
Pekka Paalanenbccda712017-03-29 16:16:04 +03006472 wl_list_for_each(view, &compositor->view_list, link) {
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006473 if (view->output_mask & (1u << output->id))
6474 weston_view_assign_output(view);
6475 }
6476
Pekka Paalanen1d2eee22021-02-25 12:03:28 +02006477 weston_output_reset_color_transforms(output);
6478
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006479 weston_presentation_feedback_discard_list(&output->feedback_list);
6480
Pekka Paalanen9711fd92018-06-21 14:26:18 +03006481 weston_compositor_reflow_outputs(compositor, output, -output->width);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006482
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006483 wl_list_remove(&output->link);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006484 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006485 output->enabled = false;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006486
Marius Vlad2015dfc2021-12-02 11:15:15 +02006487 weston_signal_emit_mutable(&compositor->output_destroyed_signal, output);
6488 weston_signal_emit_mutable(&output->destroy_signal, output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006489
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02006490 wl_list_for_each(head, &output->head_list, output_link)
6491 weston_head_remove_global(head);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03006492
6493 compositor->output_id_pool &= ~(1u << output->id);
6494 output->id = 0xffffffff; /* invalid */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006495}
6496
6497/** Sets the output scale for a given output.
6498 *
6499 * \param output The weston_output object that the scale is set for.
6500 * \param scale Scale factor for the given output.
6501 *
6502 * It only supports setting scale for an output that
6503 * is not enabled and it can only be ran once.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006504 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05006505 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006506 */
6507WL_EXPORT void
6508weston_output_set_scale(struct weston_output *output,
6509 int32_t scale)
6510{
6511 /* We can only set scale on a disabled output */
6512 assert(!output->enabled);
6513
6514 /* We only want to set scale once */
6515 assert(!output->scale);
6516
6517 output->scale = scale;
6518}
6519
6520/** Sets the output transform for a given output.
6521 *
6522 * \param output The weston_output object that the transform is set for.
6523 * \param transform Transform value for the given output.
6524 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006525 * Refer to wl_output::transform section located at
6526 * https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_output
6527 * for list of values that can be passed to this function.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006528 *
Marius Vlad55d87362019-06-11 01:15:35 +03006529 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006530 */
6531WL_EXPORT void
6532weston_output_set_transform(struct weston_output *output,
6533 uint32_t transform)
6534{
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006535 struct weston_pointer_motion_event ev;
6536 struct wl_resource *resource;
6537 struct weston_seat *seat;
6538 pixman_region32_t old_region;
6539 int mid_x, mid_y;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006540 struct weston_head *head;
6541 int ver;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006542
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006543 if (!output->enabled && output->transform == UINT32_MAX) {
6544 output->transform = transform;
6545 return;
6546 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006547
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006548 weston_output_transform_scale_init(output, transform, output->scale);
6549
6550 pixman_region32_init(&old_region);
6551 pixman_region32_copy(&old_region, &output->region);
6552
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006553 weston_output_init_geometry(output, output->x, output->y);
6554
6555 output->dirty = 1;
6556
6557 /* Notify clients of the change for output transform. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006558 wl_list_for_each(head, &output->head_list, output_link) {
6559 wl_resource_for_each(resource, &head->resource_list) {
6560 wl_output_send_geometry(resource,
6561 output->x,
6562 output->y,
6563 head->mm_width,
6564 head->mm_height,
6565 head->subpixel,
6566 head->make,
6567 head->model,
6568 output->transform);
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006569
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006570 ver = wl_resource_get_version(resource);
6571 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
6572 wl_output_send_done(resource);
6573 }
Roman Gilge97391c2019-03-29 13:01:06 +01006574 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
6575 zxdg_output_v1_send_logical_position(resource,
6576 output->x,
6577 output->y);
6578 zxdg_output_v1_send_logical_size(resource,
6579 output->width,
6580 output->height);
6581 zxdg_output_v1_send_done(resource);
6582 }
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006583 }
6584
6585 /* we must ensure that pointers are inside output, otherwise they disappear */
6586 mid_x = output->x + output->width / 2;
6587 mid_y = output->y + output->height / 2;
6588
6589 ev.mask = WESTON_POINTER_MOTION_ABS;
6590 ev.x = wl_fixed_to_double(wl_fixed_from_int(mid_x));
6591 ev.y = wl_fixed_to_double(wl_fixed_from_int(mid_y));
6592
6593 wl_list_for_each(seat, &output->compositor->seat_list, link) {
6594 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
6595
6596 if (pointer && pixman_region32_contains_point(&old_region,
6597 wl_fixed_to_int(pointer->x),
6598 wl_fixed_to_int(pointer->y),
6599 NULL))
6600 weston_pointer_move(pointer, &ev);
6601 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006602}
6603
Pekka Paalanen9a9e6ce2021-06-08 14:17:44 +03006604/** Set output's color profile
6605 *
6606 * \param output The output to change.
6607 * \param cprof The color profile to set. Can be NULL for default sRGB profile.
6608 * \return True on success, or false on failure.
6609 *
6610 * Calling this function changes the color profile of the output. This causes
6611 * all existing weston_color_transform objects related to this output via
6612 * paint nodes to be unreferenced and later re-created on demand.
6613 *
6614 * This function may not be called from within weston_output_repaint().
6615 *
6616 * On failure, nothing is changed.
6617 *
6618 * \ingroup output
6619 */
6620WL_EXPORT bool
6621weston_output_set_color_profile(struct weston_output *output,
6622 struct weston_color_profile *cprof)
6623{
6624 struct weston_color_profile *old;
6625 struct weston_paint_node *pnode;
6626
6627 old = output->color_profile;
6628 output->color_profile = weston_color_profile_ref(cprof);
6629
6630 if (output->enabled) {
6631 if (!weston_output_set_color_transforms(output)) {
6632 /* Failed, roll back */
6633 weston_color_profile_unref(output->color_profile);
6634 output->color_profile = old;
6635 return false;
6636 }
6637
6638 /* Remove outdated cached color transformations */
6639 wl_list_for_each(pnode, &output->paint_node_list, output_link) {
6640 weston_surface_color_transform_fini(&pnode->surf_xform);
6641 pnode->surf_xform_valid = false;
6642 }
6643 }
6644
6645 weston_color_profile_unref(old);
6646
6647 return true;
6648}
6649
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006650/** Initializes a weston_output object with enough data so
6651 ** an output can be configured.
6652 *
6653 * \param output The weston_output object to initialize
6654 * \param compositor The compositor instance.
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006655 * \param name Name for the output (the string is copied).
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006656 *
6657 * Sets initial values for fields that are expected to be
6658 * configured either by compositors or backends.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006659 *
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006660 * The name is used in logs, and can be used by compositors as a configuration
6661 * identifier.
6662 *
Marius Vlad55d87362019-06-11 01:15:35 +03006663 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006664 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006665 */
6666WL_EXPORT void
Armin Krezović40087402016-09-30 14:11:12 +02006667weston_output_init(struct weston_output *output,
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006668 struct weston_compositor *compositor,
6669 const char *name)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006670{
6671 output->compositor = compositor;
6672 output->destroying = 0;
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006673 output->name = strdup(name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006674 wl_list_init(&output->link);
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006675 wl_signal_init(&output->user_destroy_signal);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006676 output->enabled = false;
Ankit Nautiyal2690a772019-03-25 17:57:59 +05306677 output->desired_protection = WESTON_HDCP_DISABLE;
Ankit Nautiyal2844f8e2019-04-03 10:14:59 +05306678 output->allow_protection = true;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006679
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006680 wl_list_init(&output->head_list);
6681
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006682 /* Add some (in)sane defaults which can be used
6683 * for checking if an output was properly configured
6684 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006685 output->scale = 0;
6686 /* Can't use -1 on uint32_t and 0 is valid enum value */
6687 output->transform = UINT32_MAX;
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006688
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006689 pixman_region32_init(&output->region);
Pekka Paalanen42704142017-09-06 16:47:52 +03006690 wl_list_init(&output->mode_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006691}
6692
6693/** Adds weston_output object to pending output list.
6694 *
6695 * \param output The weston_output object to add
6696 * \param compositor The compositor instance.
6697 *
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03006698 * The opposite of this operation is built into weston_output_release().
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006699 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006700 * \ingroup compositor
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006701 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006702 */
6703WL_EXPORT void
6704weston_compositor_add_pending_output(struct weston_output *output,
6705 struct weston_compositor *compositor)
6706{
Pekka Paalanene952a012017-03-29 17:14:00 +03006707 assert(output->disable);
6708 assert(output->enable);
6709
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006710 wl_list_remove(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006711 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006712}
6713
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006714/** Create a string with the attached heads' names.
6715 *
6716 * The string must be free()'d.
Marius Vlad55d87362019-06-11 01:15:35 +03006717 *
6718 * \ingroup output
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006719 */
6720static char *
6721weston_output_create_heads_string(struct weston_output *output)
6722{
6723 FILE *fp;
6724 char *str = NULL;
6725 size_t size = 0;
6726 struct weston_head *head;
6727 const char *sep = "";
6728
6729 fp = open_memstream(&str, &size);
6730 if (!fp)
6731 return NULL;
6732
6733 wl_list_for_each(head, &output->head_list, output_link) {
6734 fprintf(fp, "%s%s", sep, head->name);
6735 sep = ", ";
6736 }
6737 fclose(fp);
6738
6739 return str;
6740}
6741
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006742/** Constructs a weston_output object that can be used by the compositor.
6743 *
Pekka Paalanencc201e42017-03-30 15:11:25 +03006744 * \param output The weston_output object that needs to be enabled. Must not
Pekka Paalanenddce54d2017-08-23 16:00:21 +03006745 * be enabled already. Must have at least one head attached.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006746 *
6747 * Output coordinates are calculated and each new output is by default
6748 * assigned to the right of previous one.
6749 *
6750 * Sets up the transformation, zoom, and geometry of the output using
6751 * the properties that need to be configured by the compositor.
6752 *
6753 * Establishes a repaint timer for the output with the relevant display
6754 * object's event loop. See output_repaint_timer_handler().
6755 *
6756 * The output is assigned an ID. Weston can support up to 32 distinct
6757 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
6758 * is referred to and used to find the first available ID number, and
6759 * then this ID is marked as used in output_id_pool.
6760 *
6761 * The output is also assigned a Wayland global with the wl_output
6762 * external interface.
6763 *
6764 * Backend specific function is called to set up the output output.
6765 *
6766 * Output is added to the compositor's output list
6767 *
6768 * If the backend specific function fails, the weston_output object
6769 * is returned to a state it was before calling this function and
6770 * is added to the compositor's pending_output_list in case it needs
6771 * to be reconfigured or just so it can be destroyed at shutdown.
6772 *
6773 * 0 is returned on success, -1 on failure.
Marius Vlad55d87362019-06-11 01:15:35 +03006774 *
6775 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006776 */
6777WL_EXPORT int
6778weston_output_enable(struct weston_output *output)
6779{
Armin Krezović782f5df2016-09-30 14:11:11 +02006780 struct weston_compositor *c = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006781 struct weston_output *iterator;
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03006782 struct weston_head *head;
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006783 char *head_names;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006784 int x = 0, y = 0;
6785
Pekka Paalanencc201e42017-03-30 15:11:25 +03006786 if (output->enabled) {
6787 weston_log("Error: attempt to enable an enabled output '%s'\n",
6788 output->name);
6789 return -1;
6790 }
6791
Pekka Paalanenddce54d2017-08-23 16:00:21 +03006792 if (wl_list_empty(&output->head_list)) {
6793 weston_log("Error: cannot enable output '%s' without heads.\n",
6794 output->name);
6795 return -1;
6796 }
6797
Pekka Paalanen586e1ac2017-09-14 16:17:59 +03006798 if (wl_list_empty(&output->mode_list) || !output->current_mode) {
6799 weston_log("Error: no video mode for output '%s'.\n",
6800 output->name);
6801 return -1;
6802 }
6803
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03006804 wl_list_for_each(head, &output->head_list, output_link) {
6805 assert(head->make);
6806 assert(head->model);
6807 }
6808
Armin Krezović782f5df2016-09-30 14:11:11 +02006809 iterator = container_of(c->output_list.prev,
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006810 struct weston_output, link);
6811
Armin Krezović782f5df2016-09-30 14:11:11 +02006812 if (!wl_list_empty(&c->output_list))
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006813 x = iterator->x + iterator->width;
6814
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006815 /* Make sure the scale is set up */
6816 assert(output->scale);
6817
6818 /* Make sure we have a transform set */
6819 assert(output->transform != UINT32_MAX);
6820
Armin Krezović782f5df2016-09-30 14:11:11 +02006821 output->x = x;
6822 output->y = y;
6823 output->dirty = 1;
6824 output->original_scale = output->scale;
6825
Marius Vlad3a2f8292019-11-04 17:53:46 +02006826 wl_signal_init(&output->frame_signal);
6827 wl_signal_init(&output->destroy_signal);
6828
Armin Krezović782f5df2016-09-30 14:11:11 +02006829 weston_output_transform_scale_init(output, output->transform, output->scale);
6830 weston_output_init_zoom(output);
6831
6832 weston_output_init_geometry(output, x, y);
6833 weston_output_damage(output);
6834
Armin Krezović782f5df2016-09-30 14:11:11 +02006835 wl_list_init(&output->animation_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02006836 wl_list_init(&output->feedback_list);
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03006837 wl_list_init(&output->paint_node_list);
Pekka Paalanen2fddc532021-04-30 17:41:29 +03006838 wl_list_init(&output->paint_node_z_order_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02006839
Pekka Paalanen9a0aa372021-06-08 13:38:35 +03006840 if (!weston_output_set_color_transforms(output))
Pekka Paalanen1d2eee22021-02-25 12:03:28 +02006841 return -1;
Pekka Paalanen1d2eee22021-02-25 12:03:28 +02006842
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006843 /* Enable the output (set up the crtc or create a
6844 * window representing the output, set up the
6845 * renderer, etc)
6846 */
6847 if (output->enable(output) < 0) {
6848 weston_log("Enabling output \"%s\" failed.\n", output->name);
Pekka Paalanen1d2eee22021-02-25 12:03:28 +02006849 weston_output_reset_color_transforms(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006850 return -1;
6851 }
6852
6853 weston_compositor_add_output(output->compositor, output);
6854
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006855 head_names = weston_output_create_heads_string(output);
6856 weston_log("Output '%s' enabled with head(s) %s\n",
6857 output->name, head_names);
6858 free(head_names);
6859
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006860 return 0;
6861}
6862
6863/** Converts a weston_output object to a pending output state, so it
6864 ** can be configured again or destroyed.
6865 *
6866 * \param output The weston_output object that needs to be disabled.
6867 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006868 * Calls a backend specific function to disable an output, in case
6869 * such function exists.
6870 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03006871 * The backend specific disable function may choose to postpone the disabling
6872 * by returning a negative value, in which case this function returns early.
6873 * In that case the backend will guarantee the output will be disabled soon
6874 * by the backend calling this function again. One must not attempt to re-enable
6875 * the output until that happens.
6876 *
6877 * Otherwise, if the output is being used by the compositor, it is removed
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006878 * from weston's output_list (see weston_compositor_remove_output())
6879 * and is returned to a state it was before weston_output_enable()
6880 * was ran (see weston_output_enable_undo()).
6881 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03006882 * See weston_output_init() for more information on the
6883 * state output is returned to.
Pekka Paalanencc201e42017-03-30 15:11:25 +03006884 *
6885 * If the output has never been enabled yet, this function can still be
6886 * called to ensure that the output is actually turned off rather than left
6887 * in the state it was discovered in.
Marius Vlad55d87362019-06-11 01:15:35 +03006888 *
6889 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006890 */
6891WL_EXPORT void
6892weston_output_disable(struct weston_output *output)
6893{
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006894 /* Should we rename this? */
6895 output->destroying = 1;
6896
Pekka Paalanenc65df642017-03-29 15:45:46 +03006897 /* Disable is called unconditionally also for not-enabled outputs,
6898 * because at compositor start-up, if there is an output that is
6899 * already on but the compositor wants to turn it off, we have to
6900 * forward the turn-off to the backend so it knows to do it.
6901 * The backend cannot initially turn off everything, because it
6902 * would cause unnecessary mode-sets for all outputs the compositor
6903 * wants to be on.
6904 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006905 if (output->disable(output) < 0)
6906 return;
6907
Pekka Paalanen2ec15eb2021-06-09 16:14:45 +03006908 if (output->enabled) {
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006909 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006910
Pekka Paalanen2ec15eb2021-06-09 16:14:45 +03006911 assert(wl_list_empty(&output->paint_node_list));
6912 }
6913
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006914 output->destroying = 0;
6915}
6916
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006917/** Forces a synchronous call to heads_changed hook
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006918 *
6919 * \param compositor The compositor instance
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006920 *
6921 * If there are new or changed heads, calls the heads_changed hook and
6922 * returns after the hook returns.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006923 *
6924 * \ingroup compositor
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006925 */
6926WL_EXPORT void
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006927weston_compositor_flush_heads_changed(struct weston_compositor *compositor)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006928{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03006929 if (compositor->heads_changed_source) {
6930 wl_event_source_remove(compositor->heads_changed_source);
6931 weston_compositor_call_heads_changed(compositor);
6932 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006933}
6934
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006935/** Add destroy callback for an output
6936 *
6937 * \param output The output to watch.
6938 * \param listener The listener to add. The \c notify member must be set.
6939 *
6940 * The listener callback will be called when user destroys an output. This
6941 * may be delayed by a backend in some cases. The main purpose of the
6942 * listener is to allow hooking up custom data to the output. The custom data
6943 * can be fetched via weston_output_get_destroy_listener() followed by
6944 * container_of().
6945 *
6946 * The \c data argument to the notify callback is the weston_output being
6947 * destroyed.
6948 *
6949 * @note This is for the final destruction of an output, not when it gets
6950 * disabled. If you want to keep track of enabled outputs, this is not it.
Marius Vlad55d87362019-06-11 01:15:35 +03006951 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05006952 * \ingroup output
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006953 */
6954WL_EXPORT void
6955weston_output_add_destroy_listener(struct weston_output *output,
6956 struct wl_listener *listener)
6957{
6958 wl_signal_add(&output->user_destroy_signal, listener);
6959}
6960
6961/** Look up destroy listener for an output
6962 *
6963 * \param output The output to query.
6964 * \param notify The notify function used used for the added destroy listener.
6965 * \return The listener, or NULL if not found.
6966 *
6967 * This looks up the previously added destroy listener struct based on the
6968 * notify function it has. The listener can be used to access user data
6969 * through \c container_of().
6970 *
6971 * \sa wl_signal_get() weston_output_add_destroy_listener()
Marius Vlad55d87362019-06-11 01:15:35 +03006972 * \ingroup output
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006973 */
6974WL_EXPORT struct wl_listener *
6975weston_output_get_destroy_listener(struct weston_output *output,
6976 wl_notify_func_t notify)
6977{
6978 return wl_signal_get(&output->user_destroy_signal, notify);
6979}
6980
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006981/** Uninitialize an output
6982 *
6983 * Removes the output from the list of enabled outputs if necessary, but
6984 * does not call the backend's output disable function. The output will no
6985 * longer be in the list of pending outputs either.
6986 *
6987 * All fields of weston_output become uninitialized, i.e. should not be used
6988 * anymore. The caller can free the memory after this.
6989 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05006990 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006991 * \internal
6992 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006993WL_EXPORT void
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03006994weston_output_release(struct weston_output *output)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006995{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006996 struct weston_head *head, *tmp;
6997
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006998 output->destroying = 1;
6999
Marius Vladf2452d62021-12-09 14:07:21 +02007000 weston_signal_emit_mutable(&output->user_destroy_signal, output);
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02007001
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03007002 if (output->idle_repaint_source)
7003 wl_event_source_remove(output->idle_repaint_source);
7004
Pekka Paalanen4b582c72017-03-30 16:04:58 +03007005 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007006 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007007
Pekka Paalanen9a9e6ce2021-06-08 14:17:44 +03007008 weston_color_profile_unref(output->color_profile);
7009
Pekka Paalanen4b582c72017-03-30 16:04:58 +03007010 pixman_region32_fini(&output->region);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03007011 wl_list_remove(&output->link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03007012
7013 wl_list_for_each_safe(head, tmp, &output->head_list, output_link)
7014 weston_head_detach(head);
7015
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007016 free(output->name);
7017}
7018
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02007019/** Find an output by its given name
7020 *
7021 * \param compositor The compositor to search in.
7022 * \param name The output name to search for.
7023 * \return An existing output with the given name, or NULL if not found.
7024 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007025 * \ingroup compositor
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02007026 */
7027WL_EXPORT struct weston_output *
7028weston_compositor_find_output_by_name(struct weston_compositor *compositor,
7029 const char *name)
7030{
7031 struct weston_output *output;
7032
7033 wl_list_for_each(output, &compositor->output_list, link)
7034 if (strcmp(output->name, name) == 0)
7035 return output;
7036
7037 wl_list_for_each(output, &compositor->pending_output_list, link)
7038 if (strcmp(output->name, name) == 0)
7039 return output;
7040
7041 return NULL;
7042}
7043
7044/** Create a named output
7045 *
7046 * \param compositor The compositor.
7047 * \param name The name for the output.
7048 * \return A new \c weston_output, or NULL on failure.
7049 *
7050 * This creates a new weston_output that starts with no heads attached.
7051 *
7052 * An output must be configured and it must have at least one head before
7053 * it can be enabled.
7054 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007055 * \ingroup compositor
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02007056 */
7057WL_EXPORT struct weston_output *
7058weston_compositor_create_output(struct weston_compositor *compositor,
7059 const char *name)
7060{
7061 assert(compositor->backend->create_output);
7062
7063 if (weston_compositor_find_output_by_name(compositor, name)) {
7064 weston_log("Warning: attempted to create an output with a "
7065 "duplicate name '%s'.\n", name);
7066 return NULL;
7067 }
7068
7069 return compositor->backend->create_output(compositor, name);
7070}
7071
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03007072/** Create an output for an unused head
7073 *
7074 * \param compositor The compositor.
7075 * \param head The head to attach to the output.
7076 * \return A new \c weston_output, or NULL on failure.
7077 *
7078 * This creates a new weston_output that starts with the given head attached.
7079 * The output inherits the name of the head. The head must not be already
7080 * attached to another output.
7081 *
7082 * An output must be configured before it can be enabled.
7083 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007084 * \ingroup compositor
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03007085 */
7086WL_EXPORT struct weston_output *
7087weston_compositor_create_output_with_head(struct weston_compositor *compositor,
7088 struct weston_head *head)
7089{
7090 struct weston_output *output;
7091
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02007092 output = weston_compositor_create_output(compositor, head->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03007093 if (!output)
7094 return NULL;
7095
7096 if (weston_output_attach_head(output, head) < 0) {
Pekka Paalanen42c0e142017-10-27 12:07:49 +03007097 weston_output_destroy(output);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03007098 return NULL;
7099 }
7100
7101 return output;
7102}
7103
7104/** Destroy an output
7105 *
7106 * \param output The output to destroy.
7107 *
7108 * The heads attached to the given output are detached and become unused again.
7109 *
7110 * It is not necessary to explicitly destroy all outputs at compositor exit.
Leandro Ribeiroca640d52020-01-27 19:12:01 -03007111 * weston_compositor_destroy() will automatically destroy any remaining
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03007112 * outputs.
7113 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05007114 * \ingroup output
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03007115 */
7116WL_EXPORT void
7117weston_output_destroy(struct weston_output *output)
7118{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03007119 output->destroy(output);
7120}
7121
Pekka Paalanencf0a4762017-04-04 16:36:07 +03007122/** When you need a head...
7123 *
7124 * This function is a hack, used until all code has been converted to become
7125 * multi-head aware.
7126 *
7127 * \param output The weston_output whose head to get.
7128 * \return The first head in the output's list.
Marius Vlad55d87362019-06-11 01:15:35 +03007129 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05007130 * \ingroup output
Pekka Paalanencf0a4762017-04-04 16:36:07 +03007131 */
7132WL_EXPORT struct weston_head *
7133weston_output_get_first_head(struct weston_output *output)
7134{
7135 if (wl_list_empty(&output->head_list))
7136 return NULL;
7137
7138 return container_of(output->head_list.next,
7139 struct weston_head, output_link);
7140}
7141
Ankit Nautiyal2844f8e2019-04-03 10:14:59 +05307142/** Allow/Disallow content-protection support for an output
7143 *
7144 * This function sets the allow_protection member for an output. Setting of
7145 * this field will allow the compositor to attempt content-protection for this
7146 * output, for a backend that supports the content-protection protocol.
7147 *
7148 * \param output The weston_output for whom the content-protection is to be
7149 * allowed.
7150 * \param allow_protection The bool value which is to be set.
7151 */
7152WL_EXPORT void
7153weston_output_allow_protection(struct weston_output *output,
7154 bool allow_protection)
7155{
7156 output->allow_protection = allow_protection;
7157}
7158
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01007159static void
Roman Gilge97391c2019-03-29 13:01:06 +01007160xdg_output_unlist(struct wl_resource *resource)
7161{
7162 wl_list_remove(wl_resource_get_link(resource));
7163}
7164
7165static void
7166xdg_output_destroy(struct wl_client *client, struct wl_resource *resource)
7167{
7168 wl_resource_destroy(resource);
7169}
7170
7171static const struct zxdg_output_v1_interface xdg_output_interface = {
7172 xdg_output_destroy
7173};
7174
7175static void
7176xdg_output_manager_destroy(struct wl_client *client,
7177 struct wl_resource *resource)
7178{
7179 wl_resource_destroy(resource);
7180}
7181
7182static void
7183xdg_output_manager_get_xdg_output(struct wl_client *client,
7184 struct wl_resource *manager,
7185 uint32_t id,
7186 struct wl_resource *output_resource)
7187{
7188 int version = wl_resource_get_version(manager);
7189 struct weston_head *head = wl_resource_get_user_data(output_resource);
7190 struct weston_output *output = head->output;
7191 struct wl_resource *resource;
7192
7193 resource = wl_resource_create(client, &zxdg_output_v1_interface,
7194 version, id);
7195 if (resource == NULL) {
7196 wl_client_post_no_memory(client);
7197 return;
7198 }
7199
7200 wl_list_insert(&head->xdg_output_resource_list,
7201 wl_resource_get_link(resource));
7202
7203 wl_resource_set_implementation(resource, &xdg_output_interface,
7204 NULL, xdg_output_unlist);
7205
7206 zxdg_output_v1_send_logical_position(resource, output->x, output->y);
7207 zxdg_output_v1_send_logical_size(resource,
7208 output->width,
7209 output->height);
7210 if (version >= ZXDG_OUTPUT_V1_NAME_SINCE_VERSION)
7211 zxdg_output_v1_send_name(resource, head->name);
7212
7213 zxdg_output_v1_send_done(resource);
7214}
7215
7216static const struct zxdg_output_manager_v1_interface xdg_output_manager_interface = {
7217 xdg_output_manager_destroy,
7218 xdg_output_manager_get_xdg_output
7219};
7220
7221static void
7222bind_xdg_output_manager(struct wl_client *client,
7223 void *data, uint32_t version, uint32_t id)
7224{
7225 struct wl_resource *resource;
7226
7227 resource = wl_resource_create(client, &zxdg_output_manager_v1_interface,
7228 version, id);
7229 if (resource == NULL) {
7230 wl_client_post_no_memory(client);
7231 return;
7232 }
7233
7234 wl_resource_set_implementation(resource, &xdg_output_manager_interface,
7235 NULL, NULL);
7236}
7237
7238static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007239destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01007240{
Jonny Lamb74130762013-11-26 18:19:46 +01007241 struct weston_surface *surface =
7242 wl_resource_get_user_data(resource);
7243
Pekka Paalanen4826f872016-04-22 14:14:38 +03007244 if (!surface)
7245 return;
7246
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007247 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02007248 surface->pending.buffer_viewport.buffer.src_width =
7249 wl_fixed_from_int(-1);
7250 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02007251 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01007252}
7253
7254static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007255viewport_destroy(struct wl_client *client,
7256 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01007257{
7258 wl_resource_destroy(resource);
7259}
7260
7261static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007262viewport_set_source(struct wl_client *client,
7263 struct wl_resource *resource,
7264 wl_fixed_t src_x,
7265 wl_fixed_t src_y,
7266 wl_fixed_t src_width,
7267 wl_fixed_t src_height)
7268{
7269 struct weston_surface *surface =
7270 wl_resource_get_user_data(resource);
7271
Pekka Paalanen4826f872016-04-22 14:14:38 +03007272 if (!surface) {
7273 wl_resource_post_error(resource,
7274 WP_VIEWPORT_ERROR_NO_SURFACE,
7275 "wl_surface for this viewport is no longer exists");
7276 return;
7277 }
7278
7279 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03007280 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007281
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007282 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03007283 src_height == wl_fixed_from_int(-1) &&
7284 src_x == wl_fixed_from_int(-1) &&
7285 src_y == wl_fixed_from_int(-1)) {
7286 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007287 surface->pending.buffer_viewport.buffer.src_width =
7288 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02007289 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007290 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007291 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007292
Pekka Paalanen201769a2016-04-26 14:42:11 +03007293 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007294 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007295 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03007296 "wl_surface@%d viewport source "
7297 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
7298 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007299 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03007300 wl_fixed_to_double(src_height),
7301 wl_fixed_to_double(src_x),
7302 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007303 return;
7304 }
7305
7306 surface->pending.buffer_viewport.buffer.src_x = src_x;
7307 surface->pending.buffer_viewport.buffer.src_y = src_y;
7308 surface->pending.buffer_viewport.buffer.src_width = src_width;
7309 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02007310 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007311}
7312
7313static void
7314viewport_set_destination(struct wl_client *client,
7315 struct wl_resource *resource,
7316 int32_t dst_width,
7317 int32_t dst_height)
7318{
7319 struct weston_surface *surface =
7320 wl_resource_get_user_data(resource);
7321
Pekka Paalanen4826f872016-04-22 14:14:38 +03007322 if (!surface) {
7323 wl_resource_post_error(resource,
7324 WP_VIEWPORT_ERROR_NO_SURFACE,
7325 "wl_surface for this viewport no longer exists");
7326 return;
7327 }
limin.tian531dd232025-01-07 12:09:27 +00007328 WESTON_LOGI(COMMON_LOG, "w:%d h:%d\n", dst_width, dst_height);
Pekka Paalanen4826f872016-04-22 14:14:38 +03007329 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007330
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007331 if (dst_width == -1 && dst_height == -1) {
7332 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007333 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02007334 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007335 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007336 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007337
7338 if (dst_width <= 0 || dst_height <= 0) {
7339 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007340 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007341 "destination size must be positive (%dx%d)",
7342 dst_width, dst_height);
7343 return;
7344 }
7345
7346 surface->pending.buffer_viewport.surface.width = dst_width;
7347 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02007348 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007349}
7350
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007351static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007352 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007353 viewport_set_source,
7354 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01007355};
7356
7357static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007358viewporter_destroy(struct wl_client *client,
7359 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01007360{
7361 wl_resource_destroy(resource);
7362}
7363
7364static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007365viewporter_get_viewport(struct wl_client *client,
7366 struct wl_resource *viewporter,
7367 uint32_t id,
7368 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01007369{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007370 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007371 struct weston_surface *surface =
7372 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01007373 struct wl_resource *resource;
7374
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007375 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007376 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007377 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007378 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01007379 return;
7380 }
7381
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007382 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007383 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01007384 if (resource == NULL) {
7385 wl_client_post_no_memory(client);
7386 return;
7387 }
7388
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007389 wl_resource_set_implementation(resource, &viewport_interface,
7390 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01007391
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007392 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01007393}
7394
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007395static const struct wp_viewporter_interface viewporter_interface = {
7396 viewporter_destroy,
7397 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01007398};
7399
7400static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007401bind_viewporter(struct wl_client *client,
7402 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01007403{
7404 struct wl_resource *resource;
7405
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007406 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06007407 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01007408 if (resource == NULL) {
7409 wl_client_post_no_memory(client);
7410 return;
7411 }
7412
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007413 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01007414 NULL, NULL);
7415}
7416
7417static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04007418destroy_presentation_feedback(struct wl_resource *feedback_resource)
7419{
7420 struct weston_presentation_feedback *feedback;
7421
7422 feedback = wl_resource_get_user_data(feedback_resource);
7423
7424 wl_list_remove(&feedback->link);
7425 free(feedback);
7426}
7427
7428static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007429presentation_destroy(struct wl_client *client, struct wl_resource *resource)
7430{
7431 wl_resource_destroy(resource);
7432}
7433
7434static void
7435presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04007436 struct wl_resource *presentation_resource,
7437 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007438 uint32_t callback)
7439{
Pekka Paalanen133e4392014-09-23 22:08:46 -04007440 struct weston_surface *surface;
7441 struct weston_presentation_feedback *feedback;
7442
7443 surface = wl_resource_get_user_data(surface_resource);
7444
Bryce Harringtonde16d892014-11-20 22:21:57 -08007445 feedback = zalloc(sizeof *feedback);
7446 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04007447 goto err_calloc;
7448
7449 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007450 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04007451 1, callback);
7452 if (!feedback->resource)
7453 goto err_create;
7454
7455 wl_resource_set_implementation(feedback->resource, NULL, feedback,
7456 destroy_presentation_feedback);
7457
7458 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
7459
7460 return;
7461
7462err_create:
7463 free(feedback);
7464
7465err_calloc:
7466 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007467}
7468
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007469static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007470 presentation_destroy,
7471 presentation_feedback
7472};
7473
7474static void
7475bind_presentation(struct wl_client *client,
7476 void *data, uint32_t version, uint32_t id)
7477{
7478 struct weston_compositor *compositor = data;
7479 struct wl_resource *resource;
7480
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007481 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06007482 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007483 if (resource == NULL) {
7484 wl_client_post_no_memory(client);
7485 return;
7486 }
7487
7488 wl_resource_set_implementation(resource, &presentation_implementation,
7489 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007490 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007491}
7492
7493static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05007494compositor_bind(struct wl_client *client,
7495 void *data, uint32_t version, uint32_t id)
7496{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007497 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05007498 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05007499
Jason Ekstranda85118c2013-06-27 20:17:02 -05007500 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06007501 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07007502 if (resource == NULL) {
7503 wl_client_post_no_memory(client);
7504 return;
7505 }
7506
7507 wl_resource_set_implementation(resource, &compositor_interface,
7508 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05007509}
7510
Daniel Stonece62cb32018-07-20 09:46:24 +01007511static const char *
7512output_repaint_status_text(struct weston_output *output)
7513{
7514 switch (output->repaint_status) {
7515 case REPAINT_NOT_SCHEDULED:
7516 return "no repaint";
7517 case REPAINT_BEGIN_FROM_IDLE:
7518 return "start_repaint_loop scheduled";
7519 case REPAINT_SCHEDULED:
7520 return "repaint scheduled";
7521 case REPAINT_AWAITING_COMPLETION:
7522 return "awaiting completion";
7523 }
7524
7525 assert(!"output_repaint_status_text missing enum");
7526 return NULL;
7527}
7528
7529static void
7530debug_scene_view_print_buffer(FILE *fp, struct weston_view *view)
7531{
7532 struct weston_buffer *buffer = view->surface->buffer_ref.buffer;
7533 struct wl_shm_buffer *shm;
7534 struct linux_dmabuf_buffer *dmabuf;
Marius Vlad00a6e012018-11-20 17:52:31 +02007535 const struct pixel_format_info *pixel_info = NULL;
Daniel Stonece62cb32018-07-20 09:46:24 +01007536
7537 if (!buffer) {
7538 fprintf(fp, "\t\t[buffer not available]\n");
7539 return;
7540 }
7541
7542 shm = wl_shm_buffer_get(buffer->resource);
7543 if (shm) {
Marius Vlad00a6e012018-11-20 17:52:31 +02007544 uint32_t _format = wl_shm_buffer_get_format(shm);
7545 pixel_info = pixel_format_get_info_shm(_format);
Daniel Stonece62cb32018-07-20 09:46:24 +01007546 fprintf(fp, "\t\tSHM buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02007547 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
7548 (unsigned long) _format,
7549 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
leng.fang32af9fc2024-06-13 11:22:15 +08007550#ifdef ENABLE_DRM_HELP
7551 debug_write_shm_buffer_to_file(view, shm);
7552#endif
Daniel Stonece62cb32018-07-20 09:46:24 +01007553 return;
7554 }
7555
7556 dmabuf = linux_dmabuf_buffer_get(buffer->resource);
7557 if (dmabuf) {
Marius Vlad6f6fd262021-07-12 12:58:34 +03007558 uint64_t modifier = dmabuf->attributes.modifier[0];
7559 char *modifier_name = pixel_format_get_modifier(modifier);
Marius Vlad00a6e012018-11-20 17:52:31 +02007560 pixel_info = pixel_format_get_info(dmabuf->attributes.format);
Daniel Stonece62cb32018-07-20 09:46:24 +01007561 fprintf(fp, "\t\tdmabuf buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02007562 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
7563 (unsigned long) dmabuf->attributes.format,
7564 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Marius Vlad6f6fd262021-07-12 12:58:34 +03007565
7566 fprintf(fp, "\t\t\tmodifier: %s\n", modifier_name ? modifier_name :
7567 "Failed to convert to a modifier name");
7568 free(modifier_name);
leng.fang32af9fc2024-06-13 11:22:15 +08007569#ifdef ENABLE_DRM_HELP
7570 debug_write_dma_buffer_to_file(view, dmabuf);
7571#endif
Daniel Stonece62cb32018-07-20 09:46:24 +01007572 return;
7573 }
7574
Marius Vlad253ba9a2019-03-17 18:22:21 +02007575 fprintf(fp, "\t\tEGL buffer\n");
Daniel Stonece62cb32018-07-20 09:46:24 +01007576}
7577
7578static void
7579debug_scene_view_print(FILE *fp, struct weston_view *view, int view_idx)
7580{
7581 struct weston_compositor *ec = view->surface->compositor;
7582 struct weston_output *output;
7583 char desc[512];
7584 pixman_box32_t *box;
leng.fang32af9fc2024-06-13 11:22:15 +08007585 pixman_box32_t *opaque_surface_box;
Daniel Stonece62cb32018-07-20 09:46:24 +01007586 uint32_t surface_id = 0;
7587 pid_t pid = 0;
7588
7589 if (view->surface->resource) {
7590 struct wl_resource *resource = view->surface->resource;
7591 wl_client_get_credentials(wl_resource_get_client(resource),
Dongjin Kimba89f002024-09-30 17:49:36 +09007592 &pid, NULL, NULL);
Daniel Stonece62cb32018-07-20 09:46:24 +01007593 surface_id = wl_resource_get_id(view->surface->resource);
7594 }
7595
7596 if (!view->surface->get_label ||
7597 view->surface->get_label(view->surface, desc, sizeof(desc)) < 0) {
7598 strcpy(desc, "[no description available]");
7599 }
leng.fang32af9fc2024-06-13 11:22:15 +08007600 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 +01007601 view_idx, view->surface->role_name, pid, surface_id,
leng.fang32af9fc2024-06-13 11:22:15 +08007602 desc, view, view->surface, view->surface->is_video_surface);
Daniel Stonece62cb32018-07-20 09:46:24 +01007603
7604 box = pixman_region32_extents(&view->transform.boundingbox);
7605 fprintf(fp, "\t\tposition: (%d, %d) -> (%d, %d)\n",
7606 box->x1, box->y1, box->x2, box->y2);
7607 box = pixman_region32_extents(&view->transform.opaque);
7608
Michael Olbrichb7e5f102020-08-11 16:33:35 +02007609 if (weston_view_is_opaque(view, &view->transform.boundingbox)) {
Daniel Stonece62cb32018-07-20 09:46:24 +01007610 fprintf(fp, "\t\t[fully opaque]\n");
7611 } else if (!pixman_region32_not_empty(&view->transform.opaque)) {
7612 fprintf(fp, "\t\t[not opaque]\n");
7613 } else {
7614 fprintf(fp, "\t\t[opaque: (%d, %d) -> (%d, %d)]\n",
7615 box->x1, box->y1, box->x2, box->y2);
7616 }
leng.fang32af9fc2024-06-13 11:22:15 +08007617 opaque_surface_box = pixman_region32_extents(&view->surface->opaque);
7618 fprintf(fp, "\t\t[surface:%p opaque: (%d, %d) -> (%d, %d)]\n",
7619 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 +01007620
7621 if (view->alpha < 1.0)
7622 fprintf(fp, "\t\talpha: %f\n", view->alpha);
7623
7624 if (view->output_mask != 0) {
7625 bool first_output = true;
7626 fprintf(fp, "\t\toutputs: ");
7627 wl_list_for_each(output, &ec->output_list, link) {
7628 if (!(view->output_mask & (1 << output->id)))
7629 continue;
7630 fprintf(fp, "%s%d (%s)%s",
7631 (first_output) ? "" : ", ",
7632 output->id, output->name,
7633 (view->output == output) ? " (primary)" : "");
7634 first_output = false;
7635 }
7636 } else {
7637 fprintf(fp, "\t\t[no outputs]");
7638 }
7639
7640 fprintf(fp, "\n");
7641
7642 debug_scene_view_print_buffer(fp, view);
7643}
7644
Marius Vlad433f4e72019-02-17 22:14:23 +02007645static void
7646debug_scene_view_print_tree(struct weston_view *view,
Marius Vlada6acfa82019-03-17 18:10:09 +02007647 FILE *fp, int *view_idx)
Marius Vlad433f4e72019-02-17 22:14:23 +02007648{
7649 struct weston_subsurface *sub;
7650 struct weston_view *ev;
7651
7652 /*
7653 * print the view first, then we recursively go on printing
7654 * sub-surfaces. We bail out once no more sub-surfaces are available.
7655 */
Marius Vlada6acfa82019-03-17 18:10:09 +02007656 debug_scene_view_print(fp, view, *view_idx);
Marius Vlad433f4e72019-02-17 22:14:23 +02007657
7658 /* no more sub-surfaces */
7659 if (wl_list_empty(&view->surface->subsurface_list))
7660 return;
7661
7662 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
7663 wl_list_for_each(ev, &sub->surface->views, surface_link) {
Michael Olbrichef5f3232020-04-29 09:03:15 +02007664 /* only print the child views of the current view */
7665 if (ev->parent_view != view)
Marius Vlad433f4e72019-02-17 22:14:23 +02007666 continue;
Marius Vlada6acfa82019-03-17 18:10:09 +02007667
7668 (*view_idx)++;
Marius Vlad433f4e72019-02-17 22:14:23 +02007669 debug_scene_view_print_tree(ev, fp, view_idx);
7670 }
7671 }
7672}
7673
Daniel Stonece62cb32018-07-20 09:46:24 +01007674/**
7675 * Output information on how libweston is currently composing the scene
7676 * graph.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007677 *
7678 * \ingroup compositor
Daniel Stonece62cb32018-07-20 09:46:24 +01007679 */
7680WL_EXPORT char *
7681weston_compositor_print_scene_graph(struct weston_compositor *ec)
7682{
7683 struct weston_output *output;
7684 struct weston_layer *layer;
7685 struct timespec now;
7686 int layer_idx = 0;
7687 FILE *fp;
7688 char *ret;
7689 size_t len;
7690 int err;
7691
7692 fp = open_memstream(&ret, &len);
7693 assert(fp);
7694
7695 weston_compositor_read_presentation_clock(ec, &now);
7696 fprintf(fp, "Weston scene graph at %ld.%09ld:\n\n",
7697 now.tv_sec, now.tv_nsec);
7698
7699 wl_list_for_each(output, &ec->output_list, link) {
7700 struct weston_head *head;
7701 int head_idx = 0;
7702
7703 fprintf(fp, "Output %d (%s):\n", output->id, output->name);
7704 assert(output->enabled);
7705
7706 fprintf(fp, "\tposition: (%d, %d) -> (%d, %d)\n",
7707 output->x, output->y,
7708 output->x + output->width,
7709 output->y + output->height);
7710 fprintf(fp, "\tmode: %dx%d@%.3fHz\n",
7711 output->current_mode->width,
7712 output->current_mode->height,
7713 output->current_mode->refresh / 1000.0);
7714 fprintf(fp, "\tscale: %d\n", output->scale);
7715
7716 fprintf(fp, "\trepaint status: %s\n",
7717 output_repaint_status_text(output));
7718 if (output->repaint_status == REPAINT_SCHEDULED)
7719 fprintf(fp, "\tnext repaint: %ld.%09ld\n",
7720 output->next_repaint.tv_sec,
7721 output->next_repaint.tv_nsec);
7722
7723 wl_list_for_each(head, &output->head_list, output_link) {
7724 fprintf(fp, "\tHead %d (%s): %sconnected\n",
7725 head_idx++, head->name,
7726 (head->connected) ? "" : "not ");
7727 }
7728 }
7729
7730 fprintf(fp, "\n");
7731
7732 wl_list_for_each(layer, &ec->layer_list, link) {
7733 struct weston_view *view;
7734 int view_idx = 0;
7735
7736 fprintf(fp, "Layer %d (pos 0x%lx):\n", layer_idx++,
7737 (unsigned long) layer->position);
7738
7739 if (!weston_layer_mask_is_infinite(layer)) {
7740 fprintf(fp, "\t[mask: (%d, %d) -> (%d,%d)]\n\n",
7741 layer->mask.x1, layer->mask.y1,
7742 layer->mask.x2, layer->mask.y2);
7743 }
7744
Marius Vlada6acfa82019-03-17 18:10:09 +02007745 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
7746 debug_scene_view_print_tree(view, fp, &view_idx);
7747 view_idx++;
7748 }
Daniel Stonece62cb32018-07-20 09:46:24 +01007749
7750 if (wl_list_empty(&layer->view_list.link))
7751 fprintf(fp, "\t[no views]\n");
7752
7753 fprintf(fp, "\n");
7754 }
7755
7756 err = fclose(fp);
7757 assert(err == 0);
7758
7759 return ret;
7760}
7761
7762/**
7763 * Called when the 'scene-graph' debug scope is bound by a client. This
7764 * one-shot weston-debug scope prints the current scene graph when bound,
7765 * and then terminates the stream.
7766 */
7767static void
Marius Vladdad882a2019-07-17 15:43:53 +03007768debug_scene_graph_cb(struct weston_log_subscription *sub, void *data)
Daniel Stonece62cb32018-07-20 09:46:24 +01007769{
7770 struct weston_compositor *ec = data;
7771 char *str = weston_compositor_print_scene_graph(ec);
7772
Marius Vladdad882a2019-07-17 15:43:53 +03007773 weston_log_subscription_printf(sub, "%s", str);
Daniel Stonece62cb32018-07-20 09:46:24 +01007774 free(str);
Marius Vladdad882a2019-07-17 15:43:53 +03007775 weston_log_subscription_complete(sub);
Daniel Stonece62cb32018-07-20 09:46:24 +01007776}
7777
Leandro Ribeiro32a5acd2020-10-19 16:06:22 -03007778/** Retrieve testsuite data from compositor
7779 *
7780 * The testsuite data can be defined by the test suite of projects that uses
7781 * libweston and given to the compositor at the moment of its creation. This
7782 * function should be used when we need to retrieve the testsuite private data
7783 * from the compositor.
7784 *
7785 * \param ec The weston compositor.
7786 * \return The testsuite data.
7787 *
7788 * \ingroup compositor
7789 * \sa weston_compositor_test_data_init
7790 */
7791WL_EXPORT void *
7792weston_compositor_get_test_data(struct weston_compositor *ec)
7793{
7794 return ec->test_data.test_private_data;
7795}
7796
Giulio Camuffo459137b2014-10-11 23:56:24 +03007797/** Create the compositor.
7798 *
7799 * This functions creates and initializes a compositor instance.
7800 *
7801 * \param display The Wayland display to be used.
7802 * \param user_data A pointer to an object that can later be retrieved
Marius Vlada2dace22019-06-12 16:05:44 +03007803 * \param log_ctx A pointer to weston_debug_compositor
Leandro Ribeirocdb72182020-11-06 11:24:22 -03007804 * \param test_data Optional testsuite data, or NULL.
Giulio Camuffo459137b2014-10-11 23:56:24 +03007805 * using the \ref weston_compositor_get_user_data function.
7806 * \return The compositor instance on success or NULL on failure.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007807 *
7808 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007809 */
7810WL_EXPORT struct weston_compositor *
Marius Vlad880b4852019-04-07 17:07:58 +03007811weston_compositor_create(struct wl_display *display,
Leandro Ribeirocdb72182020-11-06 11:24:22 -03007812 struct weston_log_context *log_ctx, void *user_data,
7813 const struct weston_testsuite_data *test_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04007814{
Giulio Camuffo459137b2014-10-11 23:56:24 +03007815 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05007816 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07007817
Leandro Ribeirobd9c0a62020-01-17 10:47:49 -03007818 if (!log_ctx)
7819 return NULL;
7820
Giulio Camuffo459137b2014-10-11 23:56:24 +03007821 ec = zalloc(sizeof *ec);
7822 if (!ec)
7823 return NULL;
7824
Leandro Ribeirocdb72182020-11-06 11:24:22 -03007825 if (test_data)
7826 ec->test_data = *test_data;
7827
Leandro Ribeirobd9c0a62020-01-17 10:47:49 -03007828 ec->weston_log_ctx = log_ctx;
Giulio Camuffo459137b2014-10-11 23:56:24 +03007829 ec->wl_display = display;
7830 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04007831 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07007832 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04007833 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03007834 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03007835 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02007836 wl_signal_init(&ec->idle_signal);
7837 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02007838 wl_signal_init(&ec->show_input_panel_signal);
7839 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02007840 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01007841 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01007842 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02007843 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02007844 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02007845 wl_signal_init(&ec->output_resized_signal);
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03007846 wl_signal_init(&ec->heads_changed_signal);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02007847 wl_signal_init(&ec->output_heads_changed_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07007848 wl_signal_init(&ec->session_signal);
Robert Beckettc569bdc2019-07-10 16:40:12 +01007849 ec->session_active = true;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04007850
Casey Dahlin58ba1372012-04-19 22:50:08 -04007851 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03007852 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04007853
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02007854 ec->activate_serial = 1;
7855
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05007856 ec->touch_mode = WESTON_TOUCH_MODE_NORMAL;
7857
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05307858 ec->content_protection = NULL;
7859
Derek Foreman152254b2015-11-26 14:17:48 -06007860 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04007861 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007862 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05007863
Giulio Camuffo954f1832014-10-11 18:27:30 +03007864 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04007865 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007866 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03007867
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007868 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007869 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007870 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01007871
Roman Gilge97391c2019-03-29 13:01:06 +01007872 if (!wl_global_create(ec->wl_display, &zxdg_output_manager_v1_interface, 2,
7873 ec, bind_xdg_output_manager))
7874 goto fail;
7875
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007876 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007877 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007878 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007879
Jonas Ådahl30d61d82014-10-22 21:21:17 +02007880 if (weston_input_init(ec) != 0)
7881 goto fail;
7882
leng.fang32af9fc2024-06-13 11:22:15 +08007883 if (aml_config_setup(ec) != 0 ) {
7884 weston_log("\n aml config fail\n");
7885 }
7886
limin.tianff789062024-08-14 02:24:40 +00007887 if (aml_drm_config_setup(ec) != 0 ) {
7888 weston_log("\n aml drm config fail\n");
7889 }
7890
Jason Ekstranda7af7042013-10-12 22:38:11 -05007891 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02007892 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007893 wl_list_init(&ec->layer_list);
7894 wl_list_init(&ec->seat_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007895 wl_list_init(&ec->pending_output_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007896 wl_list_init(&ec->output_list);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03007897 wl_list_init(&ec->head_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007898 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01007899 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007900 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01007901 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007902 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02007903 wl_list_init(&ec->debug_binding_list);
leng.fang32af9fc2024-06-13 11:22:15 +08007904 wl_list_init(&ec->video_surface_list);
leng.fangdbaf6fa2024-06-20 19:31:04 +08007905 wl_list_init(&ec->surface_id_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007906
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007907 wl_list_init(&ec->plugin_api_list);
7908
Xiong Zhang97116532013-10-23 13:58:31 +08007909 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02007910 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04007911
Giulio Camuffo459137b2014-10-11 23:56:24 +03007912 wl_data_device_manager_init(ec->wl_display);
7913
7914 wl_display_init_shm(ec->wl_display);
7915
7916 loop = wl_display_get_event_loop(ec->wl_display);
7917 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Daniel Stone6847b852017-03-01 11:34:08 +00007918 ec->repaint_timer =
leng.fang32af9fc2024-06-13 11:22:15 +08007919 wl_event_loop_add_timer(loop, output_repaint_timer_handler_aml,
Daniel Stone6847b852017-03-01 11:34:08 +00007920 ec);
leng.fang32af9fc2024-06-13 11:22:15 +08007921 wl_event_source_timer_update(ec->repaint_timer, 16);
leng.fangbd0b8832024-08-09 18:36:50 +08007922 weston_compositor_init_keytimer(ec, loop);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007923
Quentin Glidic82681572016-12-17 13:40:51 +01007924 weston_layer_init(&ec->fade_layer, ec);
7925 weston_layer_init(&ec->cursor_layer, ec);
7926
7927 weston_layer_set_position(&ec->fade_layer, WESTON_LAYER_POSITION_FADE);
7928 weston_layer_set_position(&ec->cursor_layer,
7929 WESTON_LAYER_POSITION_CURSOR);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007930
Daniel Stonece62cb32018-07-20 09:46:24 +01007931 ec->debug_scene =
Leandro Ribeiroce100192019-12-26 16:35:49 -03007932 weston_compositor_add_log_scope(ec, "scene-graph",
7933 "Scene graph details\n",
7934 debug_scene_graph_cb, NULL,
7935 ec);
Daniel Stonece62cb32018-07-20 09:46:24 +01007936
Marius Vladda104eb2019-09-05 14:31:01 +03007937 ec->timeline =
Leandro Ribeiroce100192019-12-26 16:35:49 -03007938 weston_compositor_add_log_scope(ec, "timeline",
7939 "Timeline event points\n",
7940 weston_timeline_create_subscription,
7941 weston_timeline_destroy_subscription,
7942 ec);
leng.fang32af9fc2024-06-13 11:22:15 +08007943 weston_compositor_aml_init(ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007944 return ec;
7945
7946fail:
7947 free(ec);
7948 return NULL;
7949}
7950
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007951/** weston_compositor_shutdown
7952 * \ingroup compositor
7953 */
Benjamin Franzkeb8263022011-08-30 11:32:47 +02007954WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007955weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07007956{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007957 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07007958
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007959 wl_event_source_remove(ec->idle_source);
Lujin Wangd4b46cd2019-08-12 12:03:29 -07007960 wl_event_source_remove(ec->repaint_timer);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007961
Matt Roper361d2ad2011-08-29 13:52:23 -07007962 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02007963 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07007964 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02007965
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007966 /* Destroy all pending outputs associated with this compositor */
7967 wl_list_for_each_safe(output, next, &ec->pending_output_list, link)
7968 output->destroy(output);
7969
Pekka Paalanen90a5ffa2021-02-25 12:03:28 +02007970 /* Color manager objects may have renderer hooks */
7971 if (ec->color_manager) {
7972 ec->color_manager->destroy(ec->color_manager);
7973 ec->color_manager = NULL;
7974 }
7975
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02007976 if (ec->renderer)
7977 ec->renderer->destroy(ec);
7978
Daniel Stone325fc2d2012-05-30 16:31:58 +01007979 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09007980 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01007981 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01007982 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01007983 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02007984 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007985
leng.fangdbaf6fa2024-06-20 19:31:04 +08007986 weston_compositor_clear_surface_id(ec);
7987
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04007988 weston_plane_release(&ec->primary_plane);
Pekka Paalanen87400372021-05-14 14:29:40 +03007989
7990 weston_layer_fini(&ec->fade_layer);
7991 weston_layer_fini(&ec->cursor_layer);
7992
7993 if (!wl_list_empty(&ec->layer_list))
Dongjin Kimba89f002024-09-30 17:49:36 +09007994 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 -07007995}
7996
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007997/** weston_compositor_exit_with_code
7998 * \ingroup compositor
7999 */
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05008000WL_EXPORT void
Adam Jackson3c3f3b12019-10-16 16:02:59 -04008001weston_compositor_exit_with_code(struct weston_compositor *compositor,
Frederic Plourdec336f062014-10-29 14:44:33 -04008002 int exit_code)
8003{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02008004 if (compositor->exit_code == EXIT_SUCCESS)
8005 compositor->exit_code = exit_code;
8006
Giulio Camuffo459137b2014-10-11 23:56:24 +03008007 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04008008}
8009
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008010/** weston_compositor_set_default_pointer_grab
8011 * \ingroup compositor
8012 */
Frederic Plourdec336f062014-10-29 14:44:33 -04008013WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01008014weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
8015 const struct weston_pointer_grab_interface *interface)
8016{
8017 struct weston_seat *seat;
8018
8019 ec->default_pointer_grab = interface;
8020 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05008021 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
8022
8023 if (pointer)
8024 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01008025 }
8026}
8027
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008028/** weston_compositor_set_presentation_clock
8029 * \ingroup compositor
8030 */
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04008031WL_EXPORT int
8032weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
8033 clockid_t clk_id)
8034{
8035 struct timespec ts;
8036
8037 if (clock_gettime(clk_id, &ts) < 0)
8038 return -1;
8039
8040 compositor->presentation_clock = clk_id;
8041
8042 return 0;
8043}
8044
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008045/** For choosing the software clock, when the display hardware or API
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04008046 * does not expose a compatible presentation timestamp.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008047 *
8048 * \ingroup compositor
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04008049 */
8050WL_EXPORT int
8051weston_compositor_set_presentation_clock_software(
8052 struct weston_compositor *compositor)
8053{
8054 /* In order of preference */
8055 static const clockid_t clocks[] = {
8056 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
8057 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
8058 CLOCK_MONOTONIC, /* no jumps, may crawl */
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04008059 };
8060 unsigned i;
8061
8062 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
8063 if (weston_compositor_set_presentation_clock(compositor,
8064 clocks[i]) == 0)
8065 return 0;
8066
8067 weston_log("Error: no suitable presentation clock available.\n");
8068
8069 return -1;
8070}
8071
Pekka Paalanen662f3842015-03-18 12:17:26 +02008072/** Read the current time from the Presentation clock
8073 *
8074 * \param compositor
Marius Vlada2dace22019-06-12 16:05:44 +03008075 * \param[out] ts The current time.
Pekka Paalanen662f3842015-03-18 12:17:26 +02008076 *
8077 * \note Reading the current time in user space is always imprecise to some
8078 * degree.
8079 *
8080 * This function is never meant to fail. If reading the clock does fail,
8081 * an error message is logged and a zero time is returned. Callers are not
8082 * supposed to detect or react to failures.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008083 *
8084 * \ingroup compositor
Pekka Paalanen662f3842015-03-18 12:17:26 +02008085 */
8086WL_EXPORT void
8087weston_compositor_read_presentation_clock(
8088 const struct weston_compositor *compositor,
8089 struct timespec *ts)
8090{
8091 static bool warned;
8092 int ret;
8093
8094 ret = clock_gettime(compositor->presentation_clock, ts);
8095 if (ret < 0) {
8096 ts->tv_sec = 0;
8097 ts->tv_nsec = 0;
8098
8099 if (!warned)
8100 weston_log("Error: failure to read "
Antonio Borneo39578632019-04-26 23:57:31 +02008101 "the presentation clock %#x: '%s' (%d)\n",
8102 compositor->presentation_clock,
8103 strerror(errno), errno);
Pekka Paalanen662f3842015-03-18 12:17:26 +02008104 warned = true;
8105 }
8106}
8107
Pekka Paalanen230f3b12014-09-29 14:18:40 -04008108/** Import dmabuf buffer into current renderer
8109 *
8110 * \param compositor
8111 * \param buffer the dmabuf buffer to import
8112 * \return true on usable buffers, false otherwise
8113 *
8114 * This function tests that the linux_dmabuf_buffer is usable
8115 * for the current renderer. Returns false on unusable buffers. Usually
8116 * usability is tested by importing the dmabufs for composition.
8117 *
8118 * This hook is also used for detecting if the renderer supports
8119 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
8120 * supported.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008121 *
8122 * \ingroup compositor
8123 */
Pekka Paalanen230f3b12014-09-29 14:18:40 -04008124WL_EXPORT bool
8125weston_compositor_import_dmabuf(struct weston_compositor *compositor,
8126 struct linux_dmabuf_buffer *buffer)
8127{
8128 struct weston_renderer *renderer;
8129
8130 renderer = compositor->renderer;
8131
8132 if (renderer->import_dmabuf == NULL)
8133 return false;
8134
8135 return renderer->import_dmabuf(compositor, buffer);
8136}
8137
Marius Vlad5a701542019-11-16 20:26:52 +02008138WL_EXPORT bool
8139weston_compositor_dmabuf_can_scanout(struct weston_compositor *compositor,
8140 struct linux_dmabuf_buffer *buffer)
8141{
8142 struct weston_backend *backend = compositor->backend;
8143
8144 if (backend->can_scanout_dmabuf == NULL)
8145 return false;
8146
8147 return backend->can_scanout_dmabuf(compositor, buffer);
8148}
8149
Giulio Camuffocdb4d292013-11-14 23:42:53 +01008150WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05008151weston_version(int *major, int *minor, int *micro)
8152{
8153 *major = WESTON_VERSION_MAJOR;
8154 *minor = WESTON_VERSION_MINOR;
8155 *micro = WESTON_VERSION_MICRO;
8156}
8157
Daniel Stonee03c1112016-11-24 20:45:45 +00008158/**
8159 * Attempts to find a module path from the module map specified in the
8160 * environment. If found, writes the full path into the path variable.
8161 *
8162 * The module map is a string in environment variable WESTON_MODULE_MAP, where
8163 * each entry is of the form "name=path" and entries are separated by
8164 * semicolons. Whitespace is significant.
8165 *
8166 * \param name The name to search for.
8167 * \param path Where the path is written to if found.
8168 * \param path_len Allocated bytes at \c path .
8169 * \returns The length of the string written to path on success, or 0 if the
8170 * module was not specified in the environment map or path_len was too small.
8171 */
8172WL_EXPORT size_t
8173weston_module_path_from_env(const char *name, char *path, size_t path_len)
8174{
8175 const char *mapping = getenv("WESTON_MODULE_MAP");
8176 const char *end;
8177 const int name_len = strlen(name);
8178
8179 if (!mapping)
8180 return 0;
8181
8182 end = mapping + strlen(mapping);
8183 while (mapping < end && *mapping) {
8184 const char *filename, *next;
8185
8186 /* early out: impossibly short string */
8187 if (end - mapping < name_len + 1)
8188 return 0;
8189
8190 filename = &mapping[name_len + 1];
8191 next = strchrnul(mapping, ';');
8192
8193 if (strncmp(mapping, name, name_len) == 0 &&
8194 mapping[name_len] == '=') {
8195 size_t file_len = next - filename; /* no trailing NUL */
8196 if (file_len >= path_len)
8197 return 0;
8198 strncpy(path, filename, file_len);
8199 path[file_len] = '\0';
8200 return file_len;
8201 }
8202
8203 mapping = next + 1;
8204 }
8205
8206 return 0;
8207}
8208
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03008209WL_EXPORT void *
8210weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04008211{
8212 char path[PATH_MAX];
8213 void *module, *init;
Daniel Stonebeb97e52016-11-28 12:13:54 +00008214 size_t len;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04008215
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08008216 if (name == NULL)
8217 return NULL;
8218
Derek Foreman3f86e502015-06-08 11:46:54 -05008219 if (name[0] != '/') {
Daniel Stonee03c1112016-11-24 20:45:45 +00008220 len = weston_module_path_from_env(name, path, sizeof path);
8221 if (len == 0)
Daniel Stonebeb97e52016-11-28 12:13:54 +00008222 len = snprintf(path, sizeof path, "%s/%s",
8223 LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05008224 } else {
Daniel Stonebeb97e52016-11-28 12:13:54 +00008225 len = snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05008226 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04008227
Daniel Stonebeb97e52016-11-28 12:13:54 +00008228 /* snprintf returns the length of the string it would've written,
8229 * _excluding_ the NUL byte. So even being equal to the size of
8230 * our buffer is an error here. */
8231 if (len >= sizeof path)
8232 return NULL;
8233
Kristian Høgsberga6813d22012-09-12 12:21:01 -04008234 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
8235 if (module) {
8236 weston_log("Module '%s' already loaded\n", path);
Pekka Paalanenf696ee92019-11-04 14:07:51 +02008237 } else {
8238 weston_log("Loading module '%s'\n", path);
8239 module = dlopen(path, RTLD_NOW);
8240 if (!module) {
8241 weston_log("Failed to load module: %s\n", dlerror());
8242 return NULL;
8243 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04008244 }
8245
8246 init = dlsym(module, entrypoint);
8247 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03008248 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00008249 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04008250 return NULL;
8251 }
8252
8253 return init;
8254}
8255
Pekka Paalanen6ffbba32019-11-06 12:59:32 +02008256/** Add a compositor destroy listener only once
8257 *
8258 * \param compositor The compositor whose destroy to watch for.
8259 * \param listener The listener struct to initialize.
8260 * \param destroy_handler The callback when compositor is destroyed.
8261 * \return True if listener is added, or false if there already is a listener
8262 * with the given \c destroy_handler.
8263 *
8264 * This function does nothing and returns false if the given callback function
8265 * is already present in the weston_compositor destroy callbacks list.
8266 * Otherwise, this function initializes the given listener with the given
8267 * callback pointer and adds it to the compositor's destroy callbacks list.
8268 *
8269 * This can be used to ensure that plugin initialization is done only once
8270 * in case the same plugin is loaded multiple times. If this function returns
8271 * false, the plugin should be already initialized successfully.
8272 *
8273 * All plugins should register a destroy listener for cleaning up. Note, that
8274 * the plugin destruction order is not guaranteed: plugins that depend on other
8275 * plugins must be able to be torn down in arbitrary order.
8276 *
Leandro Ribeiroca640d52020-01-27 19:12:01 -03008277 * \sa weston_compositor_destroy
Pekka Paalanen6ffbba32019-11-06 12:59:32 +02008278 */
8279WL_EXPORT bool
8280weston_compositor_add_destroy_listener_once(struct weston_compositor *compositor,
8281 struct wl_listener *listener,
8282 wl_notify_func_t destroy_handler)
8283{
8284 if (wl_signal_get(&compositor->destroy_signal, destroy_handler))
8285 return false;
8286
8287 listener->notify = destroy_handler;
8288 wl_signal_add(&compositor->destroy_signal, listener);
8289 return true;
8290}
Giulio Camuffo459137b2014-10-11 23:56:24 +03008291
Leandro Ribeiroca640d52020-01-27 19:12:01 -03008292/** Destroys the compositor.
Giulio Camuffo459137b2014-10-11 23:56:24 +03008293 *
Leandro Ribeiroca640d52020-01-27 19:12:01 -03008294 * This function cleans up the compositor state and then destroys it.
Giulio Camuffo459137b2014-10-11 23:56:24 +03008295 *
Leandro Ribeiroca640d52020-01-27 19:12:01 -03008296 * @param compositor The compositor to be destroyed.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008297 *
Marius Vlad284d5342019-06-24 12:00:47 +03008298 * @ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03008299 */
8300WL_EXPORT void
Leandro Ribeiroca640d52020-01-27 19:12:01 -03008301weston_compositor_destroy(struct weston_compositor *compositor)
Giulio Camuffo459137b2014-10-11 23:56:24 +03008302{
8303 /* prevent further rendering while shutting down */
8304 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
8305
Marius Vladf2452d62021-12-09 14:07:21 +02008306 weston_signal_emit_mutable(&compositor->destroy_signal, compositor);
Giulio Camuffo459137b2014-10-11 23:56:24 +03008307
8308 weston_compositor_xkb_destroy(compositor);
8309
Giulio Camuffo2d24e642015-10-03 16:25:15 +03008310 if (compositor->backend)
8311 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02008312
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03008313 /* The backend is responsible for destroying the heads. */
8314 assert(wl_list_empty(&compositor->head_list));
8315
Pekka Paalanen827b5d22016-06-29 11:54:26 +02008316 weston_plugin_api_destroy_list(compositor);
8317
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03008318 if (compositor->heads_changed_source)
8319 wl_event_source_remove(compositor->heads_changed_source);
8320
Leandro Ribeirof0149642019-12-18 15:52:18 -03008321 weston_log_scope_destroy(compositor->debug_scene);
Daniel Stonece62cb32018-07-20 09:46:24 +01008322 compositor->debug_scene = NULL;
Marius Vladda104eb2019-09-05 14:31:01 +03008323
Leandro Ribeirof0149642019-12-18 15:52:18 -03008324 weston_log_scope_destroy(compositor->timeline);
Marius Vladda104eb2019-09-05 14:31:01 +03008325 compositor->timeline = NULL;
Pekka Paalanena5630ea2017-10-12 13:13:42 +02008326
Leandro Ribeiro8eb84142021-01-18 19:36:48 -03008327 if (compositor->default_dmabuf_feedback) {
8328 weston_dmabuf_feedback_destroy(compositor->default_dmabuf_feedback);
8329 weston_dmabuf_feedback_format_table_destroy(compositor->dmabuf_feedback_format_table);
8330 }
8331
Giulio Camuffo459137b2014-10-11 23:56:24 +03008332 free(compositor);
8333}
8334
8335/** Instruct the compositor to exit.
8336 *
8337 * This functions does not directly destroy the compositor object, it merely
8338 * command it to start the tear down process. It is not guaranteed that the
8339 * tear down will happen immediately.
8340 *
8341 * \param compositor The compositor to tear down.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008342 *
8343 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03008344 */
8345WL_EXPORT void
8346weston_compositor_exit(struct weston_compositor *compositor)
8347{
8348 compositor->exit(compositor);
8349}
8350
8351/** Return the user data stored in the compositor.
8352 *
8353 * This function returns the user data pointer set with user_data parameter
8354 * to the \ref weston_compositor_create function.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008355 *
8356 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03008357 */
8358WL_EXPORT void *
8359weston_compositor_get_user_data(struct weston_compositor *compositor)
8360{
8361 return compositor->user_data;
8362}
Pekka Paalanendd186732016-06-03 14:49:54 +03008363
Pekka Paalanen50dbf382016-06-03 15:23:46 +03008364static const char * const backend_map[] = {
8365 [WESTON_BACKEND_DRM] = "drm-backend.so",
8366 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
8367 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
8368 [WESTON_BACKEND_RDP] = "rdp-backend.so",
8369 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
8370 [WESTON_BACKEND_X11] = "x11-backend.so",
8371};
8372
Pekka Paalanendd186732016-06-03 14:49:54 +03008373/** Load a backend into a weston_compositor
8374 *
8375 * A backend must be loaded to make a weston_compositor work. A backend
8376 * provides input and output capabilities, and determines the renderer to use.
8377 *
8378 * \param compositor A compositor that has not had a backend loaded yet.
8379 * \param backend Name of the backend file.
8380 * \param config_base A pointer to a backend-specific configuration
8381 * structure's 'base' member.
8382 *
8383 * \return 0 on success, or -1 on error.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008384 *
8385 * \ingroup compositor
Pekka Paalanendd186732016-06-03 14:49:54 +03008386 */
8387WL_EXPORT int
8388weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03008389 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03008390 struct weston_backend_config *config_base)
8391{
8392 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03008393 struct weston_backend_config *config_base);
8394
Pekka Paalanend7e35112017-08-29 17:04:12 +03008395 if (compositor->backend) {
8396 weston_log("Error: attempt to load a backend when one is already loaded\n");
8397 return -1;
8398 }
8399
Quentin Glidic887c0182016-07-10 11:00:53 +02008400 if (backend >= ARRAY_LENGTH(backend_map))
Pekka Paalanen50dbf382016-06-03 15:23:46 +03008401 return -1;
8402
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01008403 backend_init = weston_load_module(backend_map[backend], "weston_backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03008404 if (!backend_init)
8405 return -1;
8406
Pekka Paalanend7e35112017-08-29 17:04:12 +03008407 if (backend_init(compositor, config_base) < 0) {
8408 compositor->backend = NULL;
8409 return -1;
8410 }
8411
Pekka Paalanen90a5ffa2021-02-25 12:03:28 +02008412 if (!compositor->color_manager) {
8413 compositor->color_manager =
8414 weston_color_manager_noop_create(compositor);
8415 }
8416
8417 if (!compositor->color_manager)
8418 return -1;
8419
8420 if (!compositor->color_manager->init(compositor->color_manager))
8421 return -1;
8422
8423 weston_log("Color manager: %s\n", compositor->color_manager->name);
8424
Pekka Paalanend7e35112017-08-29 17:04:12 +03008425 return 0;
Pekka Paalanendd186732016-06-03 14:49:54 +03008426}
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008427
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008428/** weston_compositor_load_xwayland
8429 * \ingroup compositor
8430 */
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008431WL_EXPORT int
8432weston_compositor_load_xwayland(struct weston_compositor *compositor)
8433{
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01008434 int (*module_init)(struct weston_compositor *ec);
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008435
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01008436 module_init = weston_load_module("xwayland.so", "weston_module_init");
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008437 if (!module_init)
8438 return -1;
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01008439 if (module_init(compositor) < 0)
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008440 return -1;
8441 return 0;
8442}
Vasilis Liaskovitis486b4632018-10-10 16:14:55 +02008443
Pekka Paalanen5e79dd42021-03-29 14:11:49 +03008444/** Load Little CMS color manager plugin
8445 *
8446 * Calling this function before loading any backend sets Little CMS
8447 * as the active color matching module (CMM) instead of the default no-op
8448 * color manager.
8449 *
8450 * \ingroup compositor
8451 */
8452WL_EXPORT int
8453weston_compositor_load_color_manager(struct weston_compositor *compositor)
8454{
8455 struct weston_color_manager *
8456 (*cm_create)(struct weston_compositor *compositor);
8457
8458 if (compositor->color_manager) {
8459 weston_log("Error: Color manager '%s' is loaded, cannot load another.\n",
8460 compositor->color_manager->name);
8461 return -1;
8462 }
8463
8464 cm_create = weston_load_module("color-lcms.so", "weston_color_manager_create");
8465 if (!cm_create) {
8466 weston_log("Error: Could not load color-lcms.so.\n");
8467 return -1;
8468 }
8469
8470 compositor->color_manager = cm_create(compositor);
8471 if (!compositor->color_manager) {
8472 weston_log("Error: loading color-lcms.so failed.\n");
8473 return -1;
8474 }
8475
8476 return 0;
8477}
8478
Vasilis Liaskovitis486b4632018-10-10 16:14:55 +02008479/** Resolve an internal compositor error by disconnecting the client.
8480 *
8481 * This function is used in cases when the wl_buffer turns out
8482 * unusable and there is no fallback path.
8483 *
8484 * It is possible the fault is caused by a compositor bug, the underlying
8485 * graphics stack bug or normal behaviour, or perhaps a client mistake.
8486 * In any case, the options are to either composite garbage or nothing,
8487 * or disconnect the client. This is a helper function for the latter.
8488 *
8489 * The error is sent as an INVALID_OBJECT error on the client's wl_display.
8490 *
8491 * \param buffer The weston buffer that is unusable.
8492 * \param msg A custom error message attached to the protocol error.
8493 */
8494WL_EXPORT void
8495weston_buffer_send_server_error(struct weston_buffer *buffer,
8496 const char *msg)
8497{
8498 struct wl_client *client;
8499 struct wl_resource *display_resource;
8500 uint32_t id;
8501
8502 assert(buffer->resource);
8503 id = wl_resource_get_id(buffer->resource);
8504 client = wl_resource_get_client(buffer->resource);
8505 display_resource = wl_client_get_object(client, 1);
8506
8507 assert(display_resource);
8508 wl_resource_post_error(display_resource,
8509 WL_DISPLAY_ERROR_INVALID_OBJECT,
8510 "server error with "
8511 "wl_buffer@%u: %s", id, msg);
8512}
Ankit Nautiyal93dde242019-07-08 11:46:42 +05308513
8514WL_EXPORT void
8515weston_output_disable_planes_incr(struct weston_output *output)
8516{
8517 output->disable_planes++;
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05308518 /*
8519 * If disable_planes changes from 0 to non-zero, it means some type of
8520 * recording of content has started, and therefore protection level of
8521 * the protected surfaces must be updated to avoid the recording of
8522 * the protected content.
8523 */
8524 if (output->disable_planes == 1)
8525 weston_schedule_surface_protection_update(output->compositor);
Ankit Nautiyal93dde242019-07-08 11:46:42 +05308526}
8527
8528WL_EXPORT void
8529weston_output_disable_planes_decr(struct weston_output *output)
8530{
8531 output->disable_planes--;
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05308532 /*
8533 * If disable_planes changes from non-zero to 0, it means no content
8534 * recording is going on any more, and the protected and surfaces can be
8535 * shown without any apprehensions about content being recorded.
8536 */
8537 if (output->disable_planes == 0)
8538 weston_schedule_surface_protection_update(output->compositor);
8539
Ankit Nautiyal93dde242019-07-08 11:46:42 +05308540}