blob: cacded7a3861b4b70c21cf3e8105d4298f2b0663 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04002 * Copyright © 2010-2011 Intel Corporation
3 * Copyright © 2008-2011 Kristian Høgsberg
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03004 * Copyright © 2012-2018, 2021 Collabora, Ltd.
Pekka Paalanen925788f2018-04-19 14:20:01 +03005 * Copyright © 2017, 2018 General Electric Company
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05006 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -07007 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050014 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -070015 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial
17 * portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
23 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
24 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050027 */
28
Kristian Høgsberga9410222011-01-14 17:22:35 -050029#include "config.h"
30
Daniel Stoneb7452fe2012-06-01 12:14:06 +010031#include <fcntl.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040032#include <stdio.h>
33#include <string.h>
34#include <stdlib.h>
35#include <stdint.h>
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +010036#include <limits.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050037#include <stdarg.h>
Benjamin Franzke6f5fc692011-06-21 19:34:19 +020038#include <assert.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040039#include <sys/ioctl.h>
Daniel Stoneb7452fe2012-06-01 12:14:06 +010040#include <sys/mman.h>
Kristian Høgsberg27da5382011-06-21 17:32:25 -040041#include <sys/wait.h>
Pekka Paalanen409ef0a2011-12-02 15:30:21 +020042#include <sys/socket.h>
Martin Minarikf12c2872012-06-11 00:57:39 +020043#include <sys/utsname.h>
Martin Minarik37032f82012-06-18 20:15:18 +020044#include <sys/stat.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040045#include <unistd.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050046#include <math.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040047#include <linux/input.h>
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040048#include <dlfcn.h>
Kristian Høgsberg85449032011-05-02 12:11:07 -040049#include <signal.h>
Kristian Høgsberg0690da62012-01-16 11:53:54 -050050#include <setjmp.h>
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040051#include <sys/time.h>
52#include <time.h>
Pekka Paalanen23ade622014-08-27 13:31:26 +030053#include <errno.h>
Alexandros Frantzisacff29b2018-10-19 12:14:11 +030054#include <inttypes.h>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050055
Pekka Paalanenb5026542014-11-12 15:09:24 +020056#include "timeline.h"
57
Pekka Paalanen3d5d9472019-03-28 16:28:47 +020058#include <libweston/libweston.h>
Marius Vladc901e892019-06-21 22:49:18 +030059#include <libweston/weston-log.h>
Daniel Stonece62cb32018-07-20 09:46:24 +010060#include "linux-dmabuf.h"
Pekka Paalanene95ad5c2016-04-15 14:47:08 +030061#include "viewporter-server-protocol.h"
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020062#include "presentation-time-server-protocol.h"
Roman Gilge97391c2019-03-29 13:01:06 +010063#include "xdg-output-unstable-v1-server-protocol.h"
Alexandros Frantzisacff29b2018-10-19 12:14:11 +030064#include "linux-explicit-synchronization-unstable-v1-server-protocol.h"
Alexandros Frantzis67629672018-10-19 12:14:11 +030065#include "linux-explicit-synchronization.h"
Alexandros Frantzisacff29b2018-10-19 12:14:11 +030066#include "shared/fd-util.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070067#include "shared/helpers.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070068#include "shared/os-compatibility.h"
Bryce Harrington25a2bdd2016-08-03 17:40:52 -070069#include "shared/string-helpers.h"
Pekka Paalanenaa21f622015-07-03 15:44:50 +030070#include "shared/timespec-util.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040071#include "git-version.h"
Pekka Paalanencda14882019-04-04 15:41:02 +030072#include <libweston/version.h>
Pekka Paalanen27b377f2019-03-29 17:07:34 +020073#include <libweston/plugin-registry.h>
Marius Vlad00a6e012018-11-20 17:52:31 +020074#include "pixel-formats.h"
Marius Vlad63ef0782019-07-16 23:34:14 +030075#include "backend.h"
Marius Vlada72e3712019-07-10 13:46:39 +030076#include "libweston-internal.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050077
Marius Vlad4e036292019-07-09 00:36:20 +030078#include "weston-log-internal.h"
79
Marius Vlad78984ee2019-06-11 00:05:08 +030080/**
81 * \defgroup head Head
82 * \defgroup output Output
83 * \defgroup compositor Compositor
84 */
85
Pekka Paalanen0513a952014-05-21 16:17:27 +030086#define DEFAULT_REPAINT_WINDOW 7 /* milliseconds */
87
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020088static void
Pekka Paalanend72bad22017-03-29 17:01:41 +030089weston_output_update_matrix(struct weston_output *output);
90
91static void
Alexander Larsson0b135062013-05-28 16:23:36 +020092weston_output_transform_scale_init(struct weston_output *output,
93 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020094
Rob Bradford27b17932013-06-26 18:08:46 +010095static void
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +030096weston_compositor_build_view_list(struct weston_compositor *compositor,
97 struct weston_output *output);
Rob Bradford27b17932013-06-26 18:08:46 +010098
Pekka Paalanendcac3512017-12-08 14:13:34 +020099static char *
100weston_output_create_heads_string(struct weston_output *output);
101
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +0300102static struct weston_paint_node *
103weston_paint_node_create(struct weston_surface *surface,
104 struct weston_view *view,
105 struct weston_output *output)
106{
107 struct weston_paint_node *pnode;
108
109 assert(view->surface == surface);
110
111 pnode = zalloc(sizeof *pnode);
112 if (!pnode)
113 return NULL;
114
115 pnode->surface = surface;
116 wl_list_insert(&surface->paint_node_list, &pnode->surface_link);
117
118 pnode->view = view;
119 wl_list_insert(&view->paint_node_list, &pnode->view_link);
120
121 pnode->output = output;
122 wl_list_insert(&output->paint_node_list, &pnode->output_link);
123
124 return pnode;
125}
126
127static void
128weston_paint_node_destroy(struct weston_paint_node *pnode)
129{
130 assert(pnode->view->surface == pnode->surface);
131 wl_list_remove(&pnode->surface_link);
132 wl_list_remove(&pnode->view_link);
133 wl_list_remove(&pnode->output_link);
134 free(pnode);
135}
136
Pekka Paalanen6528c032017-03-28 15:27:10 +0300137/** Send wl_output events for mode and scale changes
138 *
139 * \param head Send on all resources bound to this head.
140 * \param mode_changed If true, send the current mode.
141 * \param scale_changed If true, send the current scale.
142 */
143static void
144weston_mode_switch_send_events(struct weston_head *head,
145 bool mode_changed, bool scale_changed)
146{
147 struct weston_output *output = head->output;
148 struct wl_resource *resource;
149 int version;
150
151 wl_resource_for_each(resource, &head->resource_list) {
152 if (mode_changed) {
153 wl_output_send_mode(resource,
154 output->current_mode->flags,
155 output->current_mode->width,
156 output->current_mode->height,
157 output->current_mode->refresh);
158 }
159
160 version = wl_resource_get_version(resource);
161 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION && scale_changed)
162 wl_output_send_scale(resource, output->current_scale);
163
164 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
165 wl_output_send_done(resource);
166 }
Roman Gilge97391c2019-03-29 13:01:06 +0100167 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
168 zxdg_output_v1_send_logical_position(resource,
169 output->x,
170 output->y);
171 zxdg_output_v1_send_logical_size(resource,
172 output->width,
173 output->height);
174 zxdg_output_v1_send_done(resource);
175 }
Pekka Paalanen6528c032017-03-28 15:27:10 +0300176}
177
178static void
179weston_mode_switch_finish(struct weston_output *output,
180 int mode_changed, int scale_changed)
Alex Wu2dda6042012-04-17 17:20:47 +0800181{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200182 struct weston_seat *seat;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300183 struct weston_head *head;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200184 pixman_region32_t old_output_region;
Alexander Larsson355748e2013-05-28 16:23:38 +0200185
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200186 pixman_region32_init(&old_output_region);
187 pixman_region32_copy(&old_output_region, &output->region);
188
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200189 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +0200190 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200191
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200192 pixman_region32_init_rect(&output->region, output->x, output->y,
193 output->width, output->height);
194
195 weston_output_update_matrix(output);
196
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200197 /* If a pointer falls outside the outputs new geometry, move it to its
198 * lower-right corner */
199 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500200 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200201 int32_t x, y;
202
203 if (!pointer)
204 continue;
205
206 x = wl_fixed_to_int(pointer->x);
207 y = wl_fixed_to_int(pointer->y);
208
209 if (!pixman_region32_contains_point(&old_output_region,
210 x, y, NULL) ||
211 pixman_region32_contains_point(&output->region,
212 x, y, NULL))
213 continue;
214
215 if (x >= output->x + output->width)
216 x = output->x + output->width - 1;
217 if (y >= output->y + output->height)
218 y = output->y + output->height - 1;
219
220 pointer->x = wl_fixed_from_int(x);
221 pointer->y = wl_fixed_from_int(y);
222 }
223
224 pixman_region32_fini(&old_output_region);
225
Derek Foremandd4cd332014-11-10 10:29:59 -0600226 if (!mode_changed && !scale_changed)
227 return;
228
Hardening57388e42013-09-18 23:56:36 +0200229 /* notify clients of the changes */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300230 wl_list_for_each(head, &output->head_list, output_link)
231 weston_mode_switch_send_events(head,
232 mode_changed, scale_changed);
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600233}
234
David Fort0de859e2016-05-27 23:22:57 +0200235static void
236weston_compositor_reflow_outputs(struct weston_compositor *compositor,
237 struct weston_output *resized_output, int delta_width);
238
Marius Vlad55d87362019-06-11 01:15:35 +0300239/**
240 * \ingroup output
241 */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600242WL_EXPORT int
243weston_output_mode_set_native(struct weston_output *output,
244 struct weston_mode *mode,
245 int32_t scale)
246{
247 int ret;
248 int mode_changed = 0, scale_changed = 0;
David Fort0de859e2016-05-27 23:22:57 +0200249 int32_t old_width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600250
251 if (!output->switch_mode)
252 return -1;
253
254 if (!output->original_mode) {
255 mode_changed = 1;
256 ret = output->switch_mode(output, mode);
257 if (ret < 0)
258 return ret;
259 if (output->current_scale != scale) {
260 scale_changed = 1;
261 output->current_scale = scale;
Hardening57388e42013-09-18 23:56:36 +0200262 }
263 }
264
David Fort0de859e2016-05-27 23:22:57 +0200265 old_width = output->width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600266 output->native_mode = mode;
267 output->native_scale = scale;
268
269 weston_mode_switch_finish(output, mode_changed, scale_changed);
270
David Fort0de859e2016-05-27 23:22:57 +0200271 if (mode_changed || scale_changed) {
272 weston_compositor_reflow_outputs(output->compositor, output, output->width - old_width);
273
274 wl_signal_emit(&output->compositor->output_resized_signal, output);
275 }
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600276 return 0;
277}
278
Marius Vlad55d87362019-06-11 01:15:35 +0300279/**
280 * \ingroup output
281 */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600282WL_EXPORT int
283weston_output_mode_switch_to_native(struct weston_output *output)
284{
285 int ret;
286 int mode_changed = 0, scale_changed = 0;
287
288 if (!output->switch_mode)
289 return -1;
290
291 if (!output->original_mode) {
292 weston_log("already in the native mode\n");
293 return -1;
294 }
295 /* the non fullscreen clients haven't seen a mode set since we
296 * switched into a temporary, so we need to notify them if the
297 * mode at that time is different from the native mode now.
298 */
299 mode_changed = (output->original_mode != output->native_mode);
300 scale_changed = (output->original_scale != output->native_scale);
301
302 ret = output->switch_mode(output, output->native_mode);
303 if (ret < 0)
304 return ret;
305
306 output->current_scale = output->native_scale;
307
308 output->original_mode = NULL;
309 output->original_scale = 0;
310
311 weston_mode_switch_finish(output, mode_changed, scale_changed);
312
313 return 0;
314}
315
Marius Vlad55d87362019-06-11 01:15:35 +0300316/**
317 * \ingroup output
318 */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600319WL_EXPORT int
320weston_output_mode_switch_to_temporary(struct weston_output *output,
321 struct weston_mode *mode,
322 int32_t scale)
323{
324 int ret;
325
326 if (!output->switch_mode)
327 return -1;
328
329 /* original_mode is the last mode non full screen clients have seen,
330 * so we shouldn't change it if we already have one set.
331 */
332 if (!output->original_mode) {
333 output->original_mode = output->native_mode;
334 output->original_scale = output->native_scale;
335 }
336 ret = output->switch_mode(output, mode);
337 if (ret < 0)
338 return ret;
339
340 output->current_scale = scale;
341
342 weston_mode_switch_finish(output, 0, 0);
343
344 return 0;
Alex Wu2dda6042012-04-17 17:20:47 +0800345}
346
Benjamin Franzke06286262011-05-06 19:12:33 +0200347static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300348region_init_infinite(pixman_region32_t *region)
349{
350 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
351 UINT32_MAX, UINT32_MAX);
352}
353
Pekka Paalanene67858b2013-04-25 13:57:42 +0300354static struct weston_subsurface *
355weston_surface_to_subsurface(struct weston_surface *surface);
356
Jason Ekstranda7af7042013-10-12 22:38:11 -0500357WL_EXPORT struct weston_view *
358weston_view_create(struct weston_surface *surface)
359{
360 struct weston_view *view;
361
Bryce Harringtonde16d892014-11-20 22:21:57 -0800362 view = zalloc(sizeof *view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500363 if (view == NULL)
364 return NULL;
365
366 view->surface = surface;
Daniel Stonefb4869d2016-12-09 16:27:54 +0000367 view->plane = &surface->compositor->primary_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500368
Jason Ekstranda7af7042013-10-12 22:38:11 -0500369 /* Assign to surface */
370 wl_list_insert(&surface->views, &view->surface_link);
371
372 wl_signal_init(&view->destroy_signal);
373 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300374 wl_list_init(&view->layer_link.link);
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +0300375 wl_list_init(&view->paint_node_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500376
Jason Ekstranda7af7042013-10-12 22:38:11 -0500377 pixman_region32_init(&view->clip);
378
379 view->alpha = 1.0;
380 pixman_region32_init(&view->transform.opaque);
381
382 wl_list_init(&view->geometry.transformation_list);
383 wl_list_insert(&view->geometry.transformation_list,
384 &view->transform.position.link);
385 weston_matrix_init(&view->transform.position.matrix);
386 wl_list_init(&view->geometry.child_list);
Pekka Paalanen380adf52015-02-16 14:39:11 +0200387 pixman_region32_init(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500388 pixman_region32_init(&view->transform.boundingbox);
389 view->transform.dirty = 1;
390
Jason Ekstranda7af7042013-10-12 22:38:11 -0500391 return view;
392}
393
Jason Ekstrand108865d2014-06-26 10:04:49 -0700394struct weston_frame_callback {
395 struct wl_resource *resource;
396 struct wl_list link;
397};
398
Pekka Paalanen133e4392014-09-23 22:08:46 -0400399struct weston_presentation_feedback {
400 struct wl_resource *resource;
401
402 /* XXX: could use just wl_resource_get_link() instead */
403 struct wl_list link;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200404
405 /* The per-surface feedback flags */
406 uint32_t psf_flags;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400407};
408
409static void
410weston_presentation_feedback_discard(
411 struct weston_presentation_feedback *feedback)
412{
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200413 wp_presentation_feedback_send_discarded(feedback->resource);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400414 wl_resource_destroy(feedback->resource);
415}
416
417static void
418weston_presentation_feedback_discard_list(struct wl_list *list)
419{
420 struct weston_presentation_feedback *feedback, *tmp;
421
422 wl_list_for_each_safe(feedback, tmp, list, link)
423 weston_presentation_feedback_discard(feedback);
424}
425
426static void
427weston_presentation_feedback_present(
428 struct weston_presentation_feedback *feedback,
429 struct weston_output *output,
430 uint32_t refresh_nsec,
431 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200432 uint64_t seq,
433 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400434{
435 struct wl_client *client = wl_resource_get_client(feedback->resource);
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300436 struct weston_head *head;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400437 struct wl_resource *o;
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200438 uint32_t tv_sec_hi;
439 uint32_t tv_sec_lo;
440 uint32_t tv_nsec;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300441 bool done = false;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400442
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300443 wl_list_for_each(head, &output->head_list, output_link) {
444 wl_resource_for_each(o, &head->resource_list) {
445 if (wl_resource_get_client(o) != client)
446 continue;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400447
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300448 wp_presentation_feedback_send_sync_output(feedback->resource, o);
449 done = true;
450 }
451
452 /* For clone mode, send it for just one wl_output global,
453 * they are all equivalent anyway.
454 */
455 if (done)
456 break;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400457 }
458
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200459 timespec_to_proto(ts, &tv_sec_hi, &tv_sec_lo, &tv_nsec);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200460 wp_presentation_feedback_send_presented(feedback->resource,
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200461 tv_sec_hi, tv_sec_lo, tv_nsec,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200462 refresh_nsec,
463 seq >> 32, seq & 0xffffffff,
464 flags | feedback->psf_flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400465 wl_resource_destroy(feedback->resource);
466}
467
468static void
469weston_presentation_feedback_present_list(struct wl_list *list,
470 struct weston_output *output,
471 uint32_t refresh_nsec,
472 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200473 uint64_t seq,
474 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400475{
476 struct weston_presentation_feedback *feedback, *tmp;
477
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200478 assert(!(flags & WP_PRESENTATION_FEEDBACK_INVALID) ||
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200479 wl_list_empty(list));
480
Pekka Paalanen133e4392014-09-23 22:08:46 -0400481 wl_list_for_each_safe(feedback, tmp, list, link)
482 weston_presentation_feedback_present(feedback, output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200483 refresh_nsec, ts, seq,
484 flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400485}
486
Jason Ekstrand7b982072014-05-20 14:33:03 -0500487static void
488surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
489{
490 struct weston_surface_state *state =
491 container_of(listener, struct weston_surface_state,
492 buffer_destroy_listener);
493
494 state->buffer = NULL;
495}
496
497static void
498weston_surface_state_init(struct weston_surface_state *state)
499{
500 state->newly_attached = 0;
501 state->buffer = NULL;
502 state->buffer_destroy_listener.notify =
503 surface_state_handle_buffer_destroy;
504 state->sx = 0;
505 state->sy = 0;
506
Derek Foreman152254b2015-11-26 14:17:48 -0600507 pixman_region32_init(&state->damage_surface);
508 pixman_region32_init(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500509 pixman_region32_init(&state->opaque);
510 region_init_infinite(&state->input);
511
512 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400513 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500514
515 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
516 state->buffer_viewport.buffer.scale = 1;
517 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
518 state->buffer_viewport.surface.width = -1;
519 state->buffer_viewport.changed = 0;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300520
521 state->acquire_fence_fd = -1;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +0530522
523 state->desired_protection = WESTON_HDCP_DISABLE;
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +0530524 state->protection_mode = WESTON_SURFACE_PROTECTION_MODE_RELAXED;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500525}
526
527static void
528weston_surface_state_fini(struct weston_surface_state *state)
529{
530 struct weston_frame_callback *cb, *next;
531
532 wl_list_for_each_safe(cb, next,
533 &state->frame_callback_list, link)
534 wl_resource_destroy(cb->resource);
535
Pekka Paalanen133e4392014-09-23 22:08:46 -0400536 weston_presentation_feedback_discard_list(&state->feedback_list);
537
Jason Ekstrand7b982072014-05-20 14:33:03 -0500538 pixman_region32_fini(&state->input);
539 pixman_region32_fini(&state->opaque);
Derek Foreman152254b2015-11-26 14:17:48 -0600540 pixman_region32_fini(&state->damage_surface);
541 pixman_region32_fini(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500542
543 if (state->buffer)
544 wl_list_remove(&state->buffer_destroy_listener.link);
545 state->buffer = NULL;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300546
547 fd_clear(&state->acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +0300548 weston_buffer_release_reference(&state->buffer_release_ref, NULL);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500549}
550
551static void
552weston_surface_state_set_buffer(struct weston_surface_state *state,
553 struct weston_buffer *buffer)
554{
555 if (state->buffer == buffer)
556 return;
557
558 if (state->buffer)
559 wl_list_remove(&state->buffer_destroy_listener.link);
560 state->buffer = buffer;
561 if (state->buffer)
562 wl_signal_add(&state->buffer->destroy_signal,
563 &state->buffer_destroy_listener);
564}
565
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500566WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500567weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500568{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500569 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400570
Bryce Harringtonde16d892014-11-20 22:21:57 -0800571 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400572 if (surface == NULL)
573 return NULL;
574
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500575 wl_signal_init(&surface->destroy_signal);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +0800576 wl_signal_init(&surface->commit_signal);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500577
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500578 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200579 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400580
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200581 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
582 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200583 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
584 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500585
586 weston_surface_state_init(&surface->pending);
587
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400588 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500589 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300590 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400591
Jason Ekstranda7af7042013-10-12 22:38:11 -0500592 wl_list_init(&surface->views);
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +0300593 wl_list_init(&surface->paint_node_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500594
595 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400596 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500597
Pekka Paalanene67858b2013-04-25 13:57:42 +0300598 wl_list_init(&surface->subsurface_list);
599 wl_list_init(&surface->subsurface_list_pending);
600
Jason Ekstrand1e059042014-10-16 10:55:19 -0500601 weston_matrix_init(&surface->buffer_to_surface_matrix);
602 weston_matrix_init(&surface->surface_to_buffer_matrix);
603
Jonas Ådahld3414f22016-07-22 17:56:31 +0800604 wl_list_init(&surface->pointer_constraints);
605
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300606 surface->acquire_fence_fd = -1;
607
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +0530608 surface->desired_protection = WESTON_HDCP_DISABLE;
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +0530609 surface->current_protection = WESTON_HDCP_DISABLE;
610 surface->protection_mode = WESTON_SURFACE_PROTECTION_MODE_RELAXED;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +0530611
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400612 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500613}
614
Alex Wu8811bf92012-02-28 18:07:54 +0800615WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500616weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200617 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500618{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100619 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Philipp Zabel195dade2018-09-03 19:44:59 +0200620 surface->is_opaque = !(alpha < 1.0);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500621}
622
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400623WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500624weston_view_to_global_float(struct weston_view *view,
625 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200626{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500627 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200628 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
629
Jason Ekstranda7af7042013-10-12 22:38:11 -0500630 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200631
632 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200633 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700634 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200635 v.f[3]);
636 *x = 0;
637 *y = 0;
638 return;
639 }
640
641 *x = v.f[0] / v.f[3];
642 *y = v.f[1] / v.f[3];
643 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500644 *x = sx + view->geometry.x;
645 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200646 }
647}
648
Pekka Paalanen0df44772020-02-14 14:50:03 +0200649/** Transform a point to buffer coordinates
650 *
651 * \param width Surface width.
652 * \param height Surface height.
653 * \param transform Buffer transform.
654 * \param scale Buffer scale.
655 * \param sx Surface x coordinate of a point.
656 * \param sy Surface y coordinate of a point.
657 * \param[out] bx Buffer x coordinate of the point.
658 * \param[out] by Buffer Y coordinate of the point.
659 *
660 * Converts the given surface-local coordinates to buffer coordinates
661 * according to the given buffer transform and scale.
662 * This ignores wp_viewport.
663 *
664 * The given width and height must be the result of inverse scaled and
665 * inverse transformed buffer size.
666 */
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500667WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200668weston_transformed_coord(int width, int height,
669 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200670 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200671 float sx, float sy, float *bx, float *by)
672{
673 switch (transform) {
674 case WL_OUTPUT_TRANSFORM_NORMAL:
675 default:
676 *bx = sx;
677 *by = sy;
678 break;
679 case WL_OUTPUT_TRANSFORM_FLIPPED:
680 *bx = width - sx;
681 *by = sy;
682 break;
683 case WL_OUTPUT_TRANSFORM_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200684 *bx = sy;
685 *by = width - sx;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200686 break;
687 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200688 *bx = sy;
689 *by = sx;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200690 break;
691 case WL_OUTPUT_TRANSFORM_180:
692 *bx = width - sx;
693 *by = height - sy;
694 break;
695 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
696 *bx = sx;
697 *by = height - sy;
698 break;
699 case WL_OUTPUT_TRANSFORM_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200700 *bx = height - sy;
701 *by = sx;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200702 break;
703 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200704 *bx = height - sy;
705 *by = width - sx;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200706 break;
707 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200708
709 *bx *= scale;
710 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200711}
712
Pekka Paalanen0df44772020-02-14 14:50:03 +0200713/** Transform a rectangle to buffer coordinates
714 *
715 * \param width Surface width.
716 * \param height Surface height.
717 * \param transform Buffer transform.
718 * \param scale Buffer scale.
719 * \param rect Rectangle in surface coordinates.
720 * \return Rectangle in buffer coordinates.
721 *
722 * Converts the given surface-local rectangle to buffer coordinates
723 * according to the given buffer transform and scale. The resulting
724 * rectangle is guaranteed to be well-formed.
725 * This ignores wp_viewport.
726 *
727 * The given width and height must be the result of inverse scaled and
728 * inverse transformed buffer size.
729 */
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200730WL_EXPORT pixman_box32_t
731weston_transformed_rect(int width, int height,
732 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200733 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200734 pixman_box32_t rect)
735{
736 float x1, x2, y1, y2;
737
738 pixman_box32_t ret;
739
Alexander Larsson4ea95522013-05-22 14:41:37 +0200740 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200741 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200742 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200743 rect.x2, rect.y2, &x2, &y2);
744
745 if (x1 <= x2) {
746 ret.x1 = x1;
747 ret.x2 = x2;
748 } else {
749 ret.x1 = x2;
750 ret.x2 = x1;
751 }
752
753 if (y1 <= y2) {
754 ret.y1 = y1;
755 ret.y2 = y2;
756 } else {
757 ret.y1 = y2;
758 ret.y2 = y1;
759 }
760
761 return ret;
762}
763
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600764/** Transform a region by a matrix, restricted to axis-aligned transformations
765 *
766 * Warning: This function does not work for projective, affine, or matrices
767 * that encode arbitrary rotations. Only 90-degree step rotations are
768 * supported.
769 */
770WL_EXPORT void
771weston_matrix_transform_region(pixman_region32_t *dest,
772 struct weston_matrix *matrix,
773 pixman_region32_t *src)
774{
775 pixman_box32_t *src_rects, *dest_rects;
776 int nrects, i;
777
778 src_rects = pixman_region32_rectangles(src, &nrects);
779 dest_rects = malloc(nrects * sizeof(*dest_rects));
780 if (!dest_rects)
781 return;
782
783 for (i = 0; i < nrects; i++) {
784 struct weston_vector vec1 = {{
785 src_rects[i].x1, src_rects[i].y1, 0, 1
786 }};
787 weston_matrix_transform(matrix, &vec1);
788 vec1.f[0] /= vec1.f[3];
789 vec1.f[1] /= vec1.f[3];
790
791 struct weston_vector vec2 = {{
792 src_rects[i].x2, src_rects[i].y2, 0, 1
793 }};
794 weston_matrix_transform(matrix, &vec2);
795 vec2.f[0] /= vec2.f[3];
796 vec2.f[1] /= vec2.f[3];
797
798 if (vec1.f[0] < vec2.f[0]) {
799 dest_rects[i].x1 = floor(vec1.f[0]);
800 dest_rects[i].x2 = ceil(vec2.f[0]);
801 } else {
802 dest_rects[i].x1 = floor(vec2.f[0]);
803 dest_rects[i].x2 = ceil(vec1.f[0]);
804 }
805
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600806 if (vec1.f[1] < vec2.f[1]) {
807 dest_rects[i].y1 = floor(vec1.f[1]);
808 dest_rects[i].y2 = ceil(vec2.f[1]);
809 } else {
810 dest_rects[i].y1 = floor(vec2.f[1]);
811 dest_rects[i].y2 = ceil(vec1.f[1]);
812 }
813 }
814
815 pixman_region32_clear(dest);
816 pixman_region32_init_rects(dest, dest_rects, nrects);
817 free(dest_rects);
818}
819
Pekka Paalanen0df44772020-02-14 14:50:03 +0200820/** Transform a region to buffer coordinates
821 *
822 * \param width Surface width.
823 * \param height Surface height.
824 * \param transform Buffer transform.
825 * \param scale Buffer scale.
826 * \param[in] src Region in surface coordinates.
827 * \param[out] dest Resulting region in buffer coordinates.
828 *
829 * Converts the given surface-local region to buffer coordinates
830 * according to the given buffer transform and scale.
831 * This ignores wp_viewport.
832 *
833 * The given width and height must be the result of inverse scaled and
834 * inverse transformed buffer size.
835 *
836 * src and dest are allowed to point to the same memory for in-place conversion.
837 */
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200838WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500839weston_transformed_region(int width, int height,
840 enum wl_output_transform transform,
841 int32_t scale,
842 pixman_region32_t *src, pixman_region32_t *dest)
843{
844 pixman_box32_t *src_rects, *dest_rects;
845 int nrects, i;
846
847 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
848 if (src != dest)
849 pixman_region32_copy(dest, src);
850 return;
851 }
852
853 src_rects = pixman_region32_rectangles(src, &nrects);
854 dest_rects = malloc(nrects * sizeof(*dest_rects));
855 if (!dest_rects)
856 return;
857
858 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
859 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
860 } else {
861 for (i = 0; i < nrects; i++) {
862 switch (transform) {
863 default:
864 case WL_OUTPUT_TRANSFORM_NORMAL:
865 dest_rects[i].x1 = src_rects[i].x1;
866 dest_rects[i].y1 = src_rects[i].y1;
867 dest_rects[i].x2 = src_rects[i].x2;
868 dest_rects[i].y2 = src_rects[i].y2;
869 break;
870 case WL_OUTPUT_TRANSFORM_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200871 dest_rects[i].x1 = src_rects[i].y1;
872 dest_rects[i].y1 = width - src_rects[i].x2;
873 dest_rects[i].x2 = src_rects[i].y2;
874 dest_rects[i].y2 = width - src_rects[i].x1;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500875 break;
876 case WL_OUTPUT_TRANSFORM_180:
877 dest_rects[i].x1 = width - src_rects[i].x2;
878 dest_rects[i].y1 = height - src_rects[i].y2;
879 dest_rects[i].x2 = width - src_rects[i].x1;
880 dest_rects[i].y2 = height - src_rects[i].y1;
881 break;
882 case WL_OUTPUT_TRANSFORM_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200883 dest_rects[i].x1 = height - src_rects[i].y2;
884 dest_rects[i].y1 = src_rects[i].x1;
885 dest_rects[i].x2 = height - src_rects[i].y1;
886 dest_rects[i].y2 = src_rects[i].x2;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500887 break;
888 case WL_OUTPUT_TRANSFORM_FLIPPED:
889 dest_rects[i].x1 = width - src_rects[i].x2;
890 dest_rects[i].y1 = src_rects[i].y1;
891 dest_rects[i].x2 = width - src_rects[i].x1;
892 dest_rects[i].y2 = src_rects[i].y2;
893 break;
894 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200895 dest_rects[i].x1 = src_rects[i].y1;
896 dest_rects[i].y1 = src_rects[i].x1;
897 dest_rects[i].x2 = src_rects[i].y2;
898 dest_rects[i].y2 = src_rects[i].x2;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500899 break;
900 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
901 dest_rects[i].x1 = src_rects[i].x1;
902 dest_rects[i].y1 = height - src_rects[i].y2;
903 dest_rects[i].x2 = src_rects[i].x2;
904 dest_rects[i].y2 = height - src_rects[i].y1;
905 break;
906 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200907 dest_rects[i].x1 = height - src_rects[i].y2;
908 dest_rects[i].y1 = width - src_rects[i].x2;
909 dest_rects[i].x2 = height - src_rects[i].y1;
910 dest_rects[i].y2 = width - src_rects[i].x1;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500911 break;
912 }
913 }
914 }
915
916 if (scale != 1) {
917 for (i = 0; i < nrects; i++) {
918 dest_rects[i].x1 *= scale;
919 dest_rects[i].x2 *= scale;
920 dest_rects[i].y1 *= scale;
921 dest_rects[i].y2 *= scale;
922 }
923 }
924
925 pixman_region32_clear(dest);
926 pixman_region32_init_rects(dest, dest_rects, nrects);
927 free(dest_rects);
928}
929
Jonny Lamb74130762013-11-26 18:19:46 +0100930static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300931viewport_surface_to_buffer(struct weston_surface *surface,
932 float sx, float sy, float *bx, float *by)
Jonny Lamb74130762013-11-26 18:19:46 +0100933{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200934 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200935 double src_width, src_height;
936 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200937
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200938 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
939 if (vp->surface.width == -1) {
940 *bx = sx;
941 *by = sy;
942 return;
943 }
Jonny Lamb74130762013-11-26 18:19:46 +0100944
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200945 src_x = 0.0;
946 src_y = 0.0;
947 src_width = surface->width_from_buffer;
948 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100949 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200950 src_x = wl_fixed_to_double(vp->buffer.src_x);
951 src_y = wl_fixed_to_double(vp->buffer.src_y);
952 src_width = wl_fixed_to_double(vp->buffer.src_width);
953 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100954 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200955
956 *bx = sx * src_width / surface->width + src_x;
957 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100958}
959
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500960WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200961weston_surface_to_buffer_float(struct weston_surface *surface,
962 float sx, float sy, float *bx, float *by)
963{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200964 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
965
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300966 /* first transform coordinates if the viewport is set */
967 viewport_surface_to_buffer(surface, sx, sy, bx, by);
Jonny Lamb74130762013-11-26 18:19:46 +0100968
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500969 weston_transformed_coord(surface->width_from_buffer,
970 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200971 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100972 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200973}
974
Derek Foreman99739672015-12-03 16:38:11 -0600975/** Transform a rectangle from surface coordinates to buffer coordinates
976 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300977 * \param surface The surface to fetch wp_viewport and buffer transformation
Derek Foreman99739672015-12-03 16:38:11 -0600978 * from.
979 * \param rect The rectangle to transform.
980 * \return The transformed rectangle.
981 *
982 * Viewport and buffer transformations can only do translation, scaling,
983 * and rotations in 90-degree steps. Therefore the only loss in the
984 * conversion is coordinate rounding.
985 *
986 * However, some coordinate rounding takes place as an intermediate
987 * step before the buffer scale factor is applied, so the rectangle
988 * boundary may not be exactly as expected.
989 *
990 * This is OK for damage tracking since a little extra coverage is
991 * not a problem.
992 */
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200993WL_EXPORT pixman_box32_t
994weston_surface_to_buffer_rect(struct weston_surface *surface,
995 pixman_box32_t rect)
996{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200997 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100998 float xf, yf;
999
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03001000 /* first transform box coordinates if the viewport is set */
1001 viewport_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
Jonny Lamb74130762013-11-26 18:19:46 +01001002 rect.x1 = floorf(xf);
1003 rect.y1 = floorf(yf);
1004
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03001005 viewport_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
Derek Foremane2e15ac2015-12-01 13:00:43 -06001006 rect.x2 = ceilf(xf);
1007 rect.y2 = ceilf(yf);
Jonny Lamb74130762013-11-26 18:19:46 +01001008
Jason Ekstrandd0cebc32014-04-21 20:56:46 -05001009 return weston_transformed_rect(surface->width_from_buffer,
1010 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001011 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +02001012 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +02001013}
1014
Pekka Paalanene54e31c2015-03-04 14:23:28 +02001015/** Transform a region from surface coordinates to buffer coordinates
1016 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03001017 * \param surface The surface to fetch wp_viewport and buffer transformation
Pekka Paalanene54e31c2015-03-04 14:23:28 +02001018 * from.
Marius Vlada2dace22019-06-12 16:05:44 +03001019 * \param[in] surface_region The region in surface coordinates.
1020 * \param[out] buffer_region The region converted to buffer coordinates.
Pekka Paalanene54e31c2015-03-04 14:23:28 +02001021 *
1022 * Buffer_region must be init'd, but will be completely overwritten.
1023 *
1024 * Viewport and buffer transformations can only do translation, scaling,
1025 * and rotations in 90-degree steps. Therefore the only loss in the
Derek Foreman99739672015-12-03 16:38:11 -06001026 * conversion is from the coordinate rounding that takes place in
1027 * \ref weston_surface_to_buffer_rect.
Marius Vlada2dace22019-06-12 16:05:44 +03001028 *
Pekka Paalanene54e31c2015-03-04 14:23:28 +02001029 */
1030WL_EXPORT void
1031weston_surface_to_buffer_region(struct weston_surface *surface,
1032 pixman_region32_t *surface_region,
1033 pixman_region32_t *buffer_region)
1034{
1035 pixman_box32_t *src_rects, *dest_rects;
1036 int nrects, i;
1037
1038 src_rects = pixman_region32_rectangles(surface_region, &nrects);
1039 dest_rects = malloc(nrects * sizeof(*dest_rects));
1040 if (!dest_rects)
1041 return;
1042
1043 for (i = 0; i < nrects; i++) {
1044 dest_rects[i] = weston_surface_to_buffer_rect(surface,
1045 src_rects[i]);
1046 }
1047
1048 pixman_region32_fini(buffer_region);
1049 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
1050 free(dest_rects);
1051}
1052
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +02001053WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001054weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001055 struct weston_plane *plane)
1056{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001057 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001058 return;
1059
Jason Ekstranda7af7042013-10-12 22:38:11 -05001060 weston_view_damage_below(view);
1061 view->plane = plane;
1062 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001063}
1064
Pekka Paalanen51723d52015-02-17 13:10:01 +02001065/** Inflict damage on the plane where the view is visible.
1066 *
1067 * \param view The view that causes the damage.
1068 *
1069 * If the view is currently on a plane (including the primary plane),
1070 * take the view's boundingbox, subtract all the opaque views that cover it,
1071 * and add the remaining region as damage to the plane. This corresponds
1072 * to the damage inflicted to the plane if this view disappeared.
1073 *
1074 * A repaint is scheduled for this view.
1075 *
1076 * The region of all opaque views covering this view is stored in
1077 * weston_view::clip and updated by view_accumulate_damage() during
1078 * weston_output_repaint(). Specifically, that region matches the
1079 * scenegraph as it was last painted.
1080 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001081WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001082weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001083{
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001084 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001085
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001086 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001087 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001088 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +08001089 if (view->plane)
1090 pixman_region32_union(&view->plane->damage,
1091 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001092 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -08001093 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001094}
1095
Pekka Paalanen01e00682017-03-24 16:21:06 +02001096/** Send wl_surface.enter/leave events
1097 *
1098 * \param surface The surface.
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03001099 * \param head A head of the entered/left output.
Pekka Paalanen01e00682017-03-24 16:21:06 +02001100 * \param enter True if entered.
Marius Vlada2dace22019-06-12 16:05:44 +03001101 * \param leave True if left.
Pekka Paalanen01e00682017-03-24 16:21:06 +02001102 *
1103 * Send the enter/leave events for all protocol objects bound to the given
1104 * output by the client owning the surface.
1105 */
1106static void
1107weston_surface_send_enter_leave(struct weston_surface *surface,
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03001108 struct weston_head *head,
Pekka Paalanen01e00682017-03-24 16:21:06 +02001109 bool enter,
1110 bool leave)
1111{
1112 struct wl_resource *wloutput;
1113 struct wl_client *client;
1114
1115 assert(enter != leave);
1116
1117 client = wl_resource_get_client(surface->resource);
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03001118 wl_resource_for_each(wloutput, &head->resource_list) {
Pekka Paalanen01e00682017-03-24 16:21:06 +02001119 if (wl_resource_get_client(wloutput) != client)
1120 continue;
1121
1122 if (enter)
1123 wl_surface_send_enter(surface->resource, wloutput);
1124 if (leave)
1125 wl_surface_send_leave(surface->resource, wloutput);
1126 }
1127}
1128
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301129static void
1130weston_surface_compute_protection(struct protected_surface *psurface)
1131{
1132 enum weston_hdcp_protection min_protection;
1133 bool min_protection_valid = false;
1134 struct weston_surface *surface = psurface->surface;
1135 struct weston_output *output;
1136
1137 wl_list_for_each(output, &surface->compositor->output_list, link)
1138 if (surface->output_mask & (1u << output->id)) {
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05301139 /*
1140 * If the content-protection is enabled with protection
1141 * mode as RELAXED for a surface, and if
1142 * content-recording features like: screen-shooter,
1143 * recorder, screen-sharing, etc are on, then notify the
1144 * client, that the protection is disabled.
1145 *
1146 * Note: If the protection mode is ENFORCED then there
1147 * is no need to bother the client as the renderer takes
1148 * care of censoring the visibility of the protected
1149 * content.
1150 */
1151
1152 if (output->disable_planes > 0 &&
1153 surface->protection_mode == WESTON_SURFACE_PROTECTION_MODE_RELAXED) {
1154 min_protection = WESTON_HDCP_DISABLE;
1155 min_protection_valid = true;
1156 break;
1157 }
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301158 if (!min_protection_valid) {
1159 min_protection = output->current_protection;
1160 min_protection_valid = true;
1161 }
1162 if (output->current_protection < min_protection)
1163 min_protection = output->current_protection;
1164 }
1165 if (!min_protection_valid)
1166 min_protection = WESTON_HDCP_DISABLE;
1167
1168 surface->current_protection = min_protection;
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05301169
1170 weston_protected_surface_send_event(psurface, surface->current_protection);
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301171}
1172
1173static void
1174notify_surface_protection_change(void *data)
1175{
1176 struct weston_compositor *compositor = data;
1177 struct content_protection *cp;
1178 struct protected_surface *psurface;
1179
1180 cp = compositor->content_protection;
1181 cp->surface_protection_update = NULL;
1182
1183 /* Notify the clients, whose surfaces are changed */
1184 wl_list_for_each(psurface, &cp->protected_list, link)
1185 if (psurface && psurface->surface)
1186 weston_surface_compute_protection(psurface);
1187}
1188
Bryce Harrington3f650b82015-12-23 11:01:58 -08001189/**
Ankit Nautiyalfaa5ab42019-07-08 13:23:24 +05301190 * \param compositor weston_compositor
1191 *
1192 * Schedule an idle task to notify surface about the update in protection,
1193 * if not already scheduled.
1194 */
1195static void
1196weston_schedule_surface_protection_update(struct weston_compositor *compositor)
1197{
1198 struct content_protection *cp = compositor->content_protection;
1199 struct wl_event_loop *loop;
1200
1201 if (!cp || cp->surface_protection_update)
1202 return;
1203 loop = wl_display_get_event_loop(compositor->wl_display);
1204 cp->surface_protection_update = wl_event_loop_add_idle(loop,
1205 notify_surface_protection_change,
1206 compositor);
1207}
1208
1209/**
Bryce Harrington3f650b82015-12-23 11:01:58 -08001210 * \param es The surface
1211 * \param mask The new set of outputs for the surface
1212 *
1213 * Sets the surface's set of outputs to the ones specified by
1214 * the new output mask provided. Identifies the outputs that
1215 * have changed, the posts enter and leave events for these
1216 * outputs as appropriate.
1217 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001218static void
1219weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
1220{
1221 uint32_t different = es->output_mask ^ mask;
1222 uint32_t entered = mask & different;
1223 uint32_t left = es->output_mask & different;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001224 uint32_t output_bit;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001225 struct weston_output *output;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001226 struct weston_head *head;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001227
1228 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001229 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001230 return;
1231 if (different == 0)
1232 return;
1233
1234 wl_list_for_each(output, &es->compositor->output_list, link) {
Pekka Paalanen01e00682017-03-24 16:21:06 +02001235 output_bit = 1u << output->id;
1236 if (!(output_bit & different))
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001237 continue;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001238
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001239 wl_list_for_each(head, &output->head_list, output_link) {
1240 weston_surface_send_enter_leave(es, head,
1241 output_bit & entered,
1242 output_bit & left);
1243 }
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001244 }
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301245 /*
1246 * Change in surfaces' output mask might trigger a change in its
1247 * protection.
1248 */
Ankit Nautiyalfaa5ab42019-07-08 13:23:24 +05301249 weston_schedule_surface_protection_update(es->compositor);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001250}
1251
Semi Malinene7a52fb2018-04-26 11:08:10 +02001252static void
1253notify_view_output_destroy(struct wl_listener *listener, void *data)
1254{
1255 struct weston_view *view =
1256 container_of(listener,
1257 struct weston_view, output_destroy_listener);
1258
1259 view->output = NULL;
1260 view->output_destroy_listener.notify = NULL;
1261}
1262
1263/** Set the primary output of the view
1264 *
1265 * \param view The view whose primary output to set
1266 * \param output The new primary output for the view
1267 *
1268 * Set \a output to be the primary output of the \a view.
1269 *
1270 * Notice that the assignment may be temporary; the primary output could be
1271 * automatically changed. Hence, one cannot rely on the value persisting.
1272 *
1273 * Passing NULL as /a output will set the primary output to NULL.
1274 */
1275WL_EXPORT void
1276weston_view_set_output(struct weston_view *view, struct weston_output *output)
1277{
1278 if (view->output_destroy_listener.notify) {
1279 wl_list_remove(&view->output_destroy_listener.link);
1280 view->output_destroy_listener.notify = NULL;
1281 }
1282 view->output = output;
1283 if (output) {
1284 view->output_destroy_listener.notify =
1285 notify_view_output_destroy;
1286 wl_signal_add(&output->destroy_signal,
1287 &view->output_destroy_listener);
1288 }
1289}
1290
Bryce Harrington3f650b82015-12-23 11:01:58 -08001291/** Recalculate which output(s) the surface has views displayed on
1292 *
1293 * \param es The surface to remap to outputs
1294 *
1295 * Finds the output that is showing the largest amount of one
1296 * of the surface's various views. This output becomes the
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001297 * surface's primary output for vsync and frame callback purposes.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001298 *
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001299 * Also notes all outputs of all of the surface's views
Bryce Harrington3f650b82015-12-23 11:01:58 -08001300 * in the output_mask for the surface.
1301 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001302static void
1303weston_surface_assign_output(struct weston_surface *es)
1304{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001305 struct weston_output *new_output;
1306 struct weston_view *view;
1307 pixman_region32_t region;
1308 uint32_t max, area, mask;
1309 pixman_box32_t *e;
1310
1311 new_output = NULL;
1312 max = 0;
1313 mask = 0;
1314 pixman_region32_init(&region);
1315 wl_list_for_each(view, &es->views, surface_link) {
1316 if (!view->output)
1317 continue;
1318
1319 pixman_region32_intersect(&region, &view->transform.boundingbox,
1320 &view->output->region);
1321
1322 e = pixman_region32_extents(&region);
1323 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1324
1325 mask |= view->output_mask;
1326
1327 if (area >= max) {
1328 new_output = view->output;
1329 max = area;
1330 }
1331 }
1332 pixman_region32_fini(&region);
1333
1334 es->output = new_output;
1335 weston_surface_update_output_mask(es, mask);
1336}
1337
Bryce Harrington3f650b82015-12-23 11:01:58 -08001338/** Recalculate which output(s) the view is displayed on
1339 *
1340 * \param ev The view to remap to outputs
1341 *
1342 * Identifies the set of outputs that the view is visible on,
1343 * noting them into the output_mask. The output that the view
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001344 * is most visible on is set as the view's primary output.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001345 *
1346 * Also does the same for the view's surface. See
1347 * weston_surface_assign_output().
1348 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001349static void
1350weston_view_assign_output(struct weston_view *ev)
1351{
1352 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001353 struct weston_output *output, *new_output;
1354 pixman_region32_t region;
1355 uint32_t max, area, mask;
1356 pixman_box32_t *e;
1357
1358 new_output = NULL;
1359 max = 0;
1360 mask = 0;
1361 pixman_region32_init(&region);
1362 wl_list_for_each(output, &ec->output_list, link) {
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03001363 if (output->destroying)
1364 continue;
1365
Jason Ekstranda7af7042013-10-12 22:38:11 -05001366 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001367 &output->region);
1368
1369 e = pixman_region32_extents(&region);
1370 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1371
1372 if (area > 0)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001373 mask |= 1u << output->id;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001374
1375 if (area >= max) {
1376 new_output = output;
1377 max = area;
1378 }
1379 }
1380 pixman_region32_fini(&region);
1381
Semi Malinene7a52fb2018-04-26 11:08:10 +02001382 weston_view_set_output(ev, new_output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001383 ev->output_mask = mask;
1384
1385 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001386}
1387
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001388static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001389weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1390 int from_x, int from_y, int *to_x, int *to_y)
1391{
1392 float x, y;
1393
1394 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1395 weston_view_from_global_float(to, x, y, &x, &y);
1396
1397 *to_x = round(x);
1398 *to_y = round(y);
1399}
1400
1401static void
1402weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1403{
1404 pixman_box32_t *a;
1405 pixman_box32_t b;
1406
1407 a = pixman_region32_extents(&from->geometry.scissor);
1408
1409 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1410 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1411
1412 pixman_region32_fini(&to->geometry.scissor);
1413 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1414}
1415
1416static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001417view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001418 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001419{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001420 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1421 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001422 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001423 { inbox->x1, inbox->y1 },
1424 { inbox->x1, inbox->y2 },
1425 { inbox->x2, inbox->y1 },
1426 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001427 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001428 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001429 int i;
1430
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001431 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001432 /* avoid rounding empty bbox to 1x1 */
1433 pixman_region32_init(bbox);
1434 return;
1435 }
1436
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001437 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001438 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001439 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001440 if (x < min_x)
1441 min_x = x;
1442 if (x > max_x)
1443 max_x = x;
1444 if (y < min_y)
1445 min_y = y;
1446 if (y > max_y)
1447 max_y = y;
1448 }
1449
Pekka Paalanen219b9822012-02-08 15:38:37 +02001450 int_x = floorf(min_x);
1451 int_y = floorf(min_y);
1452 pixman_region32_init_rect(bbox, int_x, int_y,
1453 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001454}
1455
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001456static void
Michael Olbrichcb04f982020-06-08 10:22:32 +02001457weston_view_update_transform_scissor(struct weston_view *view,
1458 pixman_region32_t *region)
1459{
1460 struct weston_view *parent = view->geometry.parent;
1461
1462 if (parent) {
1463 if (parent->geometry.scissor_enabled) {
1464 view->geometry.scissor_enabled = true;
1465 weston_view_transfer_scissor(parent, view);
1466 } else {
1467 view->geometry.scissor_enabled = false;
1468 }
1469 }
1470
1471 if (view->geometry.scissor_enabled)
1472 pixman_region32_intersect(region, region,
1473 &view->geometry.scissor);
1474}
1475static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001476weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001477{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001478 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001479
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001480 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001481 view->geometry.x = roundf(view->geometry.x);
1482 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001483
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001484 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001485 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1486 view->transform.position.matrix.d[12] = view->geometry.x;
1487 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001488
Jason Ekstranda7af7042013-10-12 22:38:11 -05001489 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001490
Jason Ekstranda7af7042013-10-12 22:38:11 -05001491 view->transform.inverse = view->transform.position.matrix;
1492 view->transform.inverse.d[12] = -view->geometry.x;
1493 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001494
Jason Ekstranda7af7042013-10-12 22:38:11 -05001495 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001496 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001497 view->surface->width,
1498 view->surface->height);
Michael Olbrichcb04f982020-06-08 10:22:32 +02001499
1500 weston_view_update_transform_scissor(view, &view->transform.boundingbox);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001501
1502 pixman_region32_translate(&view->transform.boundingbox,
1503 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001504
Jason Ekstranda7af7042013-10-12 22:38:11 -05001505 if (view->alpha == 1.0) {
1506 pixman_region32_copy(&view->transform.opaque,
1507 &view->surface->opaque);
Michael Olbrich0e4f0972020-08-19 11:31:03 +02001508 pixman_region32_intersect(&view->transform.opaque,
1509 &view->transform.opaque,
1510 &view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001511 pixman_region32_translate(&view->transform.opaque,
1512 view->geometry.x,
1513 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001514 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001515}
1516
1517static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001518weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001519{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001520 struct weston_view *parent = view->geometry.parent;
1521 struct weston_matrix *matrix = &view->transform.matrix;
1522 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001523 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001524 pixman_region32_t surfregion;
1525 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001526
Jason Ekstranda7af7042013-10-12 22:38:11 -05001527 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001528
1529 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001530 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1531 view->transform.position.matrix.d[12] = view->geometry.x;
1532 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001533
1534 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001535 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001536 weston_matrix_multiply(matrix, &tform->matrix);
1537
Pekka Paalanen483243f2013-03-08 14:56:50 +02001538 if (parent)
1539 weston_matrix_multiply(matrix, &parent->transform.matrix);
1540
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001541 if (weston_matrix_invert(inverse, matrix) < 0) {
1542 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001543 weston_log("error: weston_view %p"
1544 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001545 return -1;
1546 }
1547
Pekka Paalanen380adf52015-02-16 14:39:11 +02001548 pixman_region32_init_rect(&surfregion, 0, 0,
1549 view->surface->width, view->surface->height);
Michael Olbrichcb04f982020-06-08 10:22:32 +02001550
1551 weston_view_update_transform_scissor(view, &surfregion);
1552
Pekka Paalanen380adf52015-02-16 14:39:11 +02001553 surfbox = pixman_region32_extents(&surfregion);
1554
1555 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1556 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001557
Michael Olbrich0e4f0972020-08-19 11:31:03 +02001558 if (view->alpha == 1.0 &&
1559 matrix->type == WESTON_MATRIX_TRANSFORM_TRANSLATE) {
1560 pixman_region32_copy(&view->transform.opaque,
1561 &view->surface->opaque);
1562 pixman_region32_intersect(&view->transform.opaque,
1563 &view->transform.opaque,
1564 &view->geometry.scissor);
1565 pixman_region32_translate(&view->transform.opaque,
1566 matrix->d[12],
1567 matrix->d[13]);
1568 }
1569
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001570 return 0;
1571}
1572
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001573static struct weston_layer *
1574get_view_layer(struct weston_view *view)
1575{
1576 if (view->parent_view)
1577 return get_view_layer(view->parent_view);
1578 return view->layer_link.layer;
1579}
1580
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001581WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001582weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001583{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001584 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001585 struct weston_layer *layer;
1586 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001587
Jason Ekstranda7af7042013-10-12 22:38:11 -05001588 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001589 return;
1590
Pekka Paalanen483243f2013-03-08 14:56:50 +02001591 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001592 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001593
Jason Ekstranda7af7042013-10-12 22:38:11 -05001594 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001595
Jason Ekstranda7af7042013-10-12 22:38:11 -05001596 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001597
Jason Ekstranda7af7042013-10-12 22:38:11 -05001598 pixman_region32_fini(&view->transform.boundingbox);
1599 pixman_region32_fini(&view->transform.opaque);
1600 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001601
Pekka Paalanencd403622012-01-25 13:37:39 +02001602 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001603 if (view->geometry.transformation_list.next ==
1604 &view->transform.position.link &&
1605 view->geometry.transformation_list.prev ==
1606 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001607 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001608 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001609 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001610 if (weston_view_update_transform_enable(view) < 0)
1611 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001612 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001613
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001614 layer = get_view_layer(view);
1615 if (layer) {
1616 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001617 pixman_region32_intersect(&view->transform.boundingbox,
1618 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001619 pixman_region32_intersect(&view->transform.opaque,
1620 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001621 pixman_region32_fini(&mask);
1622 }
1623
Jason Ekstranda7af7042013-10-12 22:38:11 -05001624 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001625
Jason Ekstranda7af7042013-10-12 22:38:11 -05001626 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001627
Jason Ekstranda7af7042013-10-12 22:38:11 -05001628 wl_signal_emit(&view->surface->compositor->transform_signal,
1629 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001630}
1631
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001632WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001633weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001634{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001635 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001636
1637 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001638 * The invariant: if view->geometry.dirty, then all views
1639 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001640 * Corollary: if not parent->geometry.dirty, then all ancestors
1641 * are not dirty.
1642 */
1643
Jason Ekstranda7af7042013-10-12 22:38:11 -05001644 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001645 return;
1646
Jason Ekstranda7af7042013-10-12 22:38:11 -05001647 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001648
Jason Ekstranda7af7042013-10-12 22:38:11 -05001649 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001650 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001651 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001652}
1653
1654WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001655weston_view_to_global_fixed(struct weston_view *view,
1656 wl_fixed_t vx, wl_fixed_t vy,
1657 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001658{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001659 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001660
Jason Ekstranda7af7042013-10-12 22:38:11 -05001661 weston_view_to_global_float(view,
1662 wl_fixed_to_double(vx),
1663 wl_fixed_to_double(vy),
1664 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001665 *x = wl_fixed_from_double(xf);
1666 *y = wl_fixed_from_double(yf);
1667}
1668
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001669WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001670weston_view_from_global_float(struct weston_view *view,
1671 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001672{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001673 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001674 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1675
Jason Ekstranda7af7042013-10-12 22:38:11 -05001676 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001677
1678 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001679 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001680 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001681 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001682 *vx = 0;
1683 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001684 return;
1685 }
1686
Jason Ekstranda7af7042013-10-12 22:38:11 -05001687 *vx = v.f[0] / v.f[3];
1688 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001689 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001690 *vx = x - view->geometry.x;
1691 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001692 }
1693}
1694
1695WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001696weston_view_from_global_fixed(struct weston_view *view,
1697 wl_fixed_t x, wl_fixed_t y,
1698 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001699{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001700 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001701
Jason Ekstranda7af7042013-10-12 22:38:11 -05001702 weston_view_from_global_float(view,
1703 wl_fixed_to_double(x),
1704 wl_fixed_to_double(y),
1705 &vxf, &vyf);
1706 *vx = wl_fixed_from_double(vxf);
1707 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001708}
1709
1710WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001711weston_view_from_global(struct weston_view *view,
1712 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001713{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001714 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001715
Jason Ekstranda7af7042013-10-12 22:38:11 -05001716 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1717 *vx = floorf(vxf);
1718 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001719}
1720
Bryce Harrington3f650b82015-12-23 11:01:58 -08001721/**
1722 * \param surface The surface to be repainted
1723 *
1724 * Marks the output(s) that the surface is shown on as needing to be
1725 * repainted. See weston_output_schedule_repaint().
1726 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001727WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001728weston_surface_schedule_repaint(struct weston_surface *surface)
1729{
1730 struct weston_output *output;
1731
1732 wl_list_for_each(output, &surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001733 if (surface->output_mask & (1u << output->id))
Kristian Høgsberg98238702012-08-03 16:29:12 -04001734 weston_output_schedule_repaint(output);
1735}
1736
Bryce Harrington3f650b82015-12-23 11:01:58 -08001737/**
1738 * \param view The view to be repainted
1739 *
1740 * Marks the output(s) that the view is shown on as needing to be
1741 * repainted. See weston_output_schedule_repaint().
1742 */
Kristian Høgsberg98238702012-08-03 16:29:12 -04001743WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001744weston_view_schedule_repaint(struct weston_view *view)
1745{
1746 struct weston_output *output;
1747
1748 wl_list_for_each(output, &view->surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001749 if (view->output_mask & (1u << output->id))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001750 weston_output_schedule_repaint(output);
1751}
1752
Pekka Paalanene508ce62015-02-19 13:59:55 +02001753/**
1754 * XXX: This function does it the wrong way.
1755 * surface->damage is the damage from the client, and causes
1756 * surface_flush_damage() to copy pixels. No window management action can
1757 * cause damage to the client-provided content, warranting re-upload!
1758 *
1759 * Instead of surface->damage, this function should record the damage
1760 * with all the views for this surface to avoid extraneous texture
1761 * uploads.
1762 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001763WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001764weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001765{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001766 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001767 0, 0, surface->width,
1768 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001769
Kristian Høgsberg98238702012-08-03 16:29:12 -04001770 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001771}
1772
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001773WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001774weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001775{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001776 if (view->geometry.x == x && view->geometry.y == y)
1777 return;
1778
Jason Ekstranda7af7042013-10-12 22:38:11 -05001779 view->geometry.x = x;
1780 view->geometry.y = y;
1781 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001782}
1783
Pekka Paalanen483243f2013-03-08 14:56:50 +02001784static void
1785transform_parent_handle_parent_destroy(struct wl_listener *listener,
1786 void *data)
1787{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001788 struct weston_view *view =
1789 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001790 geometry.parent_destroy_listener);
1791
Jason Ekstranda7af7042013-10-12 22:38:11 -05001792 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001793}
1794
1795WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001796weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001797 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001798{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001799 if (view->geometry.parent) {
1800 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1801 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001802
1803 if (!parent)
1804 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001805 }
1806
Jason Ekstranda7af7042013-10-12 22:38:11 -05001807 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001808
Jason Ekstranda7af7042013-10-12 22:38:11 -05001809 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001810 transform_parent_handle_parent_destroy;
1811 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001812 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001813 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001814 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001815 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001816 }
1817
Jason Ekstranda7af7042013-10-12 22:38:11 -05001818 weston_view_geometry_dirty(view);
1819}
1820
Pekka Paalanen380adf52015-02-16 14:39:11 +02001821/** Set a clip mask rectangle on a view
1822 *
1823 * \param view The view to set the clip mask on.
1824 * \param x Top-left corner X coordinate of the clip rectangle.
1825 * \param y Top-left corner Y coordinate of the clip rectangle.
1826 * \param width Width of the clip rectangle, non-negative.
1827 * \param height Height of the clip rectangle, non-negative.
1828 *
1829 * A shell may set a clip mask rectangle on a view. Everything outside
1830 * the rectangle is cut away for input and output purposes: it is
1831 * not drawn and cannot be hit by hit-test based input like pointer
1832 * motion or touch-downs. Everything inside the rectangle will behave
1833 * normally. Clients are unaware of clipping.
1834 *
Yong Bakos4c72e292016-04-28 11:59:10 -05001835 * The rectangle is set in surface-local coordinates. Setting a clip
Pekka Paalanen380adf52015-02-16 14:39:11 +02001836 * mask rectangle does not affect the view position, the view is positioned
1837 * as it would be without a clip. The clip also does not change
1838 * weston_surface::width,height.
1839 *
1840 * The clip mask rectangle is part of transformation inheritance
1841 * (weston_view_set_transform_parent()). A clip set in the root of the
1842 * transformation inheritance tree will affect all views in the tree.
1843 * A clip can be set only on the root view. Attempting to set a clip
1844 * on view that has a transformation parent will fail. Assigning a parent
1845 * to a view that has a clip set will cause the clip to be forgotten.
1846 *
1847 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1848 * on the additional transformations in the child views. These transformations
1849 * may not rotate the coordinate axes, i.e., only translation and scaling
1850 * are allowed. Violating this restriction causes the clipping to malfunction.
1851 * Furthermore, using scaling may cause rounding errors in child clipping.
1852 *
1853 * The clip mask rectangle is not automatically adjusted based on
1854 * wl_surface.attach dx and dy arguments.
1855 *
1856 * A clip mask rectangle can be set only if the compositor capability
1857 * WESTON_CAP_VIEW_CLIP_MASK is present.
1858 *
1859 * This function sets the clip mask rectangle and schedules a repaint for
1860 * the view.
1861 */
1862WL_EXPORT void
1863weston_view_set_mask(struct weston_view *view,
1864 int x, int y, int width, int height)
1865{
1866 struct weston_compositor *compositor = view->surface->compositor;
1867
1868 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1869 weston_log("%s not allowed without capability!\n", __func__);
1870 return;
1871 }
1872
1873 if (view->geometry.parent) {
1874 weston_log("view %p has a parent, clip forbidden!\n", view);
1875 return;
1876 }
1877
1878 if (width < 0 || height < 0) {
1879 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1880 x, y, width, height);
1881 return;
1882 }
1883
1884 pixman_region32_fini(&view->geometry.scissor);
1885 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1886 view->geometry.scissor_enabled = true;
1887 weston_view_geometry_dirty(view);
1888 weston_view_schedule_repaint(view);
1889}
1890
1891/** Remove the clip mask from a view
1892 *
1893 * \param view The view to remove the clip mask from.
1894 *
1895 * Removed the clip mask rectangle and schedules a repaint.
1896 *
1897 * \sa weston_view_set_mask
1898 */
1899WL_EXPORT void
1900weston_view_set_mask_infinite(struct weston_view *view)
1901{
1902 view->geometry.scissor_enabled = false;
1903 weston_view_geometry_dirty(view);
1904 weston_view_schedule_repaint(view);
1905}
1906
Armin Krezović0da12b82016-06-30 06:04:33 +02001907/* Check if view should be displayed
1908 *
1909 * The indicator is set manually when assigning
1910 * a view to a surface.
1911 *
1912 * This needs reworking. See the thread starting at:
1913 *
1914 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1915 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001916WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001917weston_view_is_mapped(struct weston_view *view)
1918{
Armin Krezović0da12b82016-06-30 06:04:33 +02001919 return view->is_mapped;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001920}
1921
Philipp Zabel70decd52018-09-03 20:11:15 +02001922/* Check if view is opaque in specified region
1923 *
1924 * \param view The view to check for opacity.
1925 * \param region The region to check for opacity, in view coordinates.
1926 *
1927 * Returns true if the view is opaque in the specified region, because view
1928 * alpha is 1.0 and either the opaque region set by the client contains the
1929 * specified region, or the buffer pixel format or solid color is opaque.
1930 */
1931WL_EXPORT bool
1932weston_view_is_opaque(struct weston_view *ev, pixman_region32_t *region)
1933{
1934 pixman_region32_t r;
1935 bool ret = false;
1936
1937 if (ev->alpha < 1.0)
1938 return false;
1939
1940 if (ev->surface->is_opaque)
1941 return true;
1942
Marius Vlad67b382c2020-08-21 13:57:54 +03001943 if (ev->transform.dirty)
Philipp Zabel70decd52018-09-03 20:11:15 +02001944 return false;
Philipp Zabel70decd52018-09-03 20:11:15 +02001945
1946 pixman_region32_init(&r);
1947 pixman_region32_subtract(&r, region, &ev->transform.opaque);
1948
1949 if (!pixman_region32_not_empty(&r))
1950 ret = true;
1951
1952 pixman_region32_fini(&r);
1953
1954 return ret;
1955}
1956
Marius Vlad5f6bee42019-09-11 16:41:04 +03001957/** Check if the view has a valid buffer available
1958 *
1959 * @param ev The view to check if it has a valid buffer.
1960 *
1961 * Returns true if the view has a valid buffer or false otherwise.
1962 */
1963WL_EXPORT bool
1964weston_view_has_valid_buffer(struct weston_view *ev)
1965{
1966 return ev->surface->buffer_ref.buffer != NULL;
1967}
1968
Marius Vlad47e3d1e2019-09-11 16:53:08 +03001969/** Check if the view matches the entire output
1970 *
1971 * @param ev The view to check.
1972 * @param output The output to check against.
1973 *
1974 * Returns true if the view does indeed matches the entire output.
1975 */
1976WL_EXPORT bool
1977weston_view_matches_output_entirely(struct weston_view *ev,
1978 struct weston_output *output)
1979{
1980 pixman_box32_t *extents =
1981 pixman_region32_extents(&ev->transform.boundingbox);
1982
1983 if (extents->x1 != output->x ||
1984 extents->y1 != output->y ||
1985 extents->x2 != output->x + output->width ||
1986 extents->y2 != output->y + output->height)
1987 return false;
1988
1989 return true;
1990}
1991
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03001992/** Find paint node for the given view and output
1993 */
1994WL_EXPORT struct weston_paint_node *
1995weston_view_find_paint_node(struct weston_view *view,
1996 struct weston_output *output)
1997{
1998 struct weston_paint_node *pnode;
1999
2000 wl_list_for_each(pnode, &view->paint_node_list, view_link) {
2001 assert(pnode->surface == view->surface);
2002 if (pnode->output == output)
2003 return pnode;
2004 }
2005
2006 return NULL;
2007}
2008
Armin Krezović0da12b82016-06-30 06:04:33 +02002009/* Check if a surface has a view assigned to it
2010 *
2011 * The indicator is set manually when mapping
2012 * a surface and creating a view for it.
2013 *
2014 * This needs to go. See the thread starting at:
2015 *
2016 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
2017 *
2018 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05002019WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03002020weston_surface_is_mapped(struct weston_surface *surface)
2021{
Armin Krezović0da12b82016-06-30 06:04:33 +02002022 return surface->is_mapped;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03002023}
2024
Pekka Paalanenda75ee12013-11-26 18:19:43 +01002025static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06002026surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002027{
2028 struct weston_view *view;
2029
2030 if (surface->width == width && surface->height == height)
2031 return;
2032
2033 surface->width = width;
2034 surface->height = height;
2035
2036 wl_list_for_each(view, &surface->views, surface_link)
2037 weston_view_geometry_dirty(view);
2038}
2039
Jason Ekstrand5c11a332013-12-04 20:32:03 -06002040WL_EXPORT void
2041weston_surface_set_size(struct weston_surface *surface,
2042 int32_t width, int32_t height)
2043{
2044 assert(!surface->resource);
2045 surface_set_size(surface, width, height);
2046}
2047
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02002048static int
2049fixed_round_up_to_int(wl_fixed_t f)
2050{
2051 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
2052}
2053
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002054static void
Pekka Paalanen59987fa2016-04-26 15:50:59 +03002055convert_size_by_transform_scale(int32_t *width_out, int32_t *height_out,
2056 int32_t width, int32_t height,
2057 uint32_t transform,
2058 int32_t scale)
2059{
2060 assert(scale > 0);
2061
2062 switch (transform) {
2063 case WL_OUTPUT_TRANSFORM_NORMAL:
2064 case WL_OUTPUT_TRANSFORM_180:
2065 case WL_OUTPUT_TRANSFORM_FLIPPED:
2066 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2067 *width_out = width / scale;
2068 *height_out = height / scale;
2069 break;
2070 case WL_OUTPUT_TRANSFORM_90:
2071 case WL_OUTPUT_TRANSFORM_270:
2072 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2073 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2074 *width_out = height / scale;
2075 *height_out = width / scale;
2076 break;
2077 default:
2078 assert(0 && "invalid transform");
2079 }
2080}
2081
2082static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002083weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002084{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002085 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01002086
2087 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02002088 surface->width_from_buffer = 0;
2089 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01002090 return;
2091 }
2092
Pekka Paalanen59987fa2016-04-26 15:50:59 +03002093 convert_size_by_transform_scale(&surface->width_from_buffer,
2094 &surface->height_from_buffer,
2095 surface->buffer_ref.buffer->width,
2096 surface->buffer_ref.buffer->height,
2097 vp->buffer.transform,
2098 vp->buffer.scale);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002099}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02002100
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002101static void
2102weston_surface_update_size(struct weston_surface *surface)
2103{
2104 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
2105 int32_t width, height;
2106
2107 width = surface->width_from_buffer;
2108 height = surface->height_from_buffer;
2109
2110 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02002111 surface_set_size(surface,
2112 vp->surface.width, vp->surface.height);
2113 return;
2114 }
2115
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002116 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03002117 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
2118 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
2119
2120 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02002121 return;
2122 }
2123
Jason Ekstrand5c11a332013-12-04 20:32:03 -06002124 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002125}
2126
Marius Vlad9fdda7f2019-06-11 16:08:55 +03002127/** weston_compositor_get_time
2128 * \ingroup compositor
2129 */
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02002130WL_EXPORT void
2131weston_compositor_get_time(struct timespec *time)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05002132{
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02002133 clock_gettime(CLOCK_REALTIME, time);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05002134}
2135
Marius Vlad9fdda7f2019-06-11 16:08:55 +03002136/** weston_compositor_pick_view
2137 * \ingroup compositor
2138 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002139WL_EXPORT struct weston_view *
2140weston_compositor_pick_view(struct weston_compositor *compositor,
2141 wl_fixed_t x, wl_fixed_t y,
2142 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02002143{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002144 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02002145 wl_fixed_t view_x, view_y;
2146 int view_ix, view_iy;
2147 int ix = wl_fixed_to_int(x);
2148 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02002149
Jason Ekstranda7af7042013-10-12 22:38:11 -05002150 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02002151 if (!pixman_region32_contains_point(
2152 &view->transform.boundingbox, ix, iy, NULL))
2153 continue;
2154
2155 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
2156 view_ix = wl_fixed_to_int(view_x);
2157 view_iy = wl_fixed_to_int(view_y);
2158
2159 if (!pixman_region32_contains_point(&view->surface->input,
2160 view_ix, view_iy, NULL))
2161 continue;
2162
Pekka Paalanen380adf52015-02-16 14:39:11 +02002163 if (view->geometry.scissor_enabled &&
2164 !pixman_region32_contains_point(&view->geometry.scissor,
2165 view_ix, view_iy, NULL))
2166 continue;
2167
Pekka Paalanenfc22a522015-02-18 15:08:29 +02002168 *vx = view_x;
2169 *vy = view_y;
2170 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02002171 }
2172
Derek Foremanf9318d12015-05-11 15:40:11 -05002173 *vx = wl_fixed_from_int(-1000000);
2174 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02002175 return NULL;
2176}
2177
2178static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002179weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002180{
Daniel Stone37816df2012-05-16 18:45:18 +01002181 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002182
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07002183 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002184 return;
2185
Daniel Stone37816df2012-05-16 18:45:18 +01002186 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04002187 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002188}
2189
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002190WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002191weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05002192{
Daniel Stone4dab5db2012-05-30 16:31:53 +01002193 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002194
Jason Ekstranda7af7042013-10-12 22:38:11 -05002195 if (!weston_view_is_mapped(view))
2196 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002197
Jason Ekstranda7af7042013-10-12 22:38:11 -05002198 weston_view_damage_below(view);
Semi Malinene7a52fb2018-04-26 11:08:10 +02002199 weston_view_set_output(view, NULL);
Xiong Zhang97116532013-10-23 13:58:31 +08002200 view->plane = NULL;
Armin Krezovićf8486c32016-06-30 06:04:28 +02002201 view->is_mapped = false;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002202 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002203 wl_list_remove(&view->link);
2204 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002205 view->output_mask = 0;
2206 weston_surface_assign_output(view->surface);
2207
2208 if (weston_surface_is_mapped(view->surface))
2209 return;
2210
2211 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002212 struct weston_touch *touch = weston_seat_get_touch(seat);
2213 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2214 struct weston_keyboard *keyboard =
2215 weston_seat_get_keyboard(seat);
2216
2217 if (keyboard && keyboard->focus == view->surface)
2218 weston_keyboard_set_focus(keyboard, NULL);
2219 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05002220 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05002221 if (touch && touch->focus == view)
2222 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01002223 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002224}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002225
Jason Ekstranda7af7042013-10-12 22:38:11 -05002226WL_EXPORT void
2227weston_surface_unmap(struct weston_surface *surface)
2228{
2229 struct weston_view *view;
2230
Armin Krezovićf8486c32016-06-30 06:04:28 +02002231 surface->is_mapped = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002232 wl_list_for_each(view, &surface->views, surface_link)
2233 weston_view_unmap(view);
2234 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05002235}
2236
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002237static void
2238weston_surface_reset_pending_buffer(struct weston_surface *surface)
2239{
Jason Ekstrand7b982072014-05-20 14:33:03 -05002240 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002241 surface->pending.sx = 0;
2242 surface->pending.sy = 0;
2243 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002244 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002245}
2246
Jason Ekstranda7af7042013-10-12 22:38:11 -05002247WL_EXPORT void
2248weston_view_destroy(struct weston_view *view)
2249{
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002250 struct weston_paint_node *pnode, *pntmp;
2251
Jason Ekstranda7af7042013-10-12 22:38:11 -05002252 wl_signal_emit(&view->destroy_signal, view);
2253
2254 assert(wl_list_empty(&view->geometry.child_list));
2255
2256 if (weston_view_is_mapped(view)) {
2257 weston_view_unmap(view);
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002258 weston_compositor_build_view_list(view->surface->compositor,
2259 NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002260 }
2261
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002262 wl_list_for_each_safe(pnode, pntmp, &view->paint_node_list, view_link)
2263 weston_paint_node_destroy(pnode);
2264
Jason Ekstranda7af7042013-10-12 22:38:11 -05002265 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002266 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002267
2268 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02002269 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002270 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002271 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002272
2273 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen944fae82018-05-22 13:15:58 +03002274 weston_view_set_output(view, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002275
Jason Ekstranda7af7042013-10-12 22:38:11 -05002276 wl_list_remove(&view->surface_link);
2277
2278 free(view);
2279}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002280
2281WL_EXPORT void
2282weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05002283{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002284 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002285 struct weston_view *ev, *nv;
Jonas Ådahld3414f22016-07-22 17:56:31 +08002286 struct weston_pointer_constraint *constraint, *next_constraint;
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002287 struct weston_paint_node *pnode, *pntmp;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04002288
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02002289 if (--surface->ref_count > 0)
2290 return;
2291
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03002292 assert(surface->resource == NULL);
2293
Pekka Paalanenca790762015-04-17 14:23:38 +03002294 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02002295
Pekka Paalanene67858b2013-04-25 13:57:42 +03002296 assert(wl_list_empty(&surface->subsurface_list_pending));
2297 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02002298
Jason Ekstranda7af7042013-10-12 22:38:11 -05002299 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
2300 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002301
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002302 wl_list_for_each_safe(pnode, pntmp,
2303 &surface->paint_node_list, surface_link) {
2304 weston_paint_node_destroy(pnode);
2305 }
2306
Jason Ekstrand7b982072014-05-20 14:33:03 -05002307 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002308
Pekka Paalanende685b82012-12-04 15:58:12 +02002309 weston_buffer_reference(&surface->buffer_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002310 weston_buffer_release_reference(&surface->buffer_release_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04002311
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02002312 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002313 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002314 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02002315
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002316 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002317 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002318
Pekka Paalanen133e4392014-09-23 22:08:46 -04002319 weston_presentation_feedback_discard_list(&surface->feedback_list);
2320
Jonas Ådahld3414f22016-07-22 17:56:31 +08002321 wl_list_for_each_safe(constraint, next_constraint,
2322 &surface->pointer_constraints,
2323 link)
2324 weston_pointer_constraint_destroy(constraint);
2325
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03002326 fd_clear(&surface->acquire_fence_fd);
2327
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04002328 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05002329}
2330
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002331static void
2332destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08002333{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002334 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08002335
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03002336 assert(surface);
2337
Giulio Camuffo0d379742013-11-15 22:06:15 +01002338 /* Set the resource to NULL, since we don't want to leave a
2339 * dangling pointer if the surface was refcounted and survives
2340 * the weston_surface_destroy() call. */
2341 surface->resource = NULL;
Pekka Paalanen4826f872016-04-22 14:14:38 +03002342
2343 if (surface->viewport_resource)
2344 wl_resource_set_user_data(surface->viewport_resource, NULL);
2345
Alexandros Frantzis27d7c392018-10-19 12:14:11 +03002346 if (surface->synchronization_resource) {
2347 wl_resource_set_user_data(surface->synchronization_resource,
2348 NULL);
2349 }
2350
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002351 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08002352}
2353
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002354static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002355weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
2356{
2357 struct weston_buffer *buffer =
2358 container_of(listener, struct weston_buffer, destroy_listener);
2359
2360 wl_signal_emit(&buffer->destroy_signal, buffer);
2361 free(buffer);
2362}
2363
Giulio Camuffoe058cd12013-12-12 14:14:29 +01002364WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002365weston_buffer_from_resource(struct wl_resource *resource)
2366{
2367 struct weston_buffer *buffer;
2368 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002369
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002370 listener = wl_resource_get_destroy_listener(resource,
2371 weston_buffer_destroy_handler);
2372
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002373 if (listener)
2374 return container_of(listener, struct weston_buffer,
2375 destroy_listener);
2376
2377 buffer = zalloc(sizeof *buffer);
2378 if (buffer == NULL)
2379 return NULL;
2380
2381 buffer->resource = resource;
2382 wl_signal_init(&buffer->destroy_signal);
2383 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04002384 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002385 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002386
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002387 return buffer;
2388}
2389
2390static void
Pekka Paalanende685b82012-12-04 15:58:12 +02002391weston_buffer_reference_handle_destroy(struct wl_listener *listener,
2392 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002393{
Pekka Paalanende685b82012-12-04 15:58:12 +02002394 struct weston_buffer_reference *ref =
2395 container_of(listener, struct weston_buffer_reference,
2396 destroy_listener);
2397
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002398 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02002399 ref->buffer = NULL;
2400}
2401
2402WL_EXPORT void
2403weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002404 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002405{
2406 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05002407 ref->buffer->busy_count--;
2408 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002409 assert(wl_resource_get_client(ref->buffer->resource));
Matt Hoosier3052bc72017-09-26 08:09:40 -05002410 wl_buffer_send_release(ref->buffer->resource);
Kristian Høgsberg20347802013-03-04 12:07:46 -05002411 }
Pekka Paalanende685b82012-12-04 15:58:12 +02002412 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002413 }
2414
Pekka Paalanende685b82012-12-04 15:58:12 +02002415 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002416 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002417 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02002418 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02002419 }
2420
Pekka Paalanende685b82012-12-04 15:58:12 +02002421 ref->buffer = buffer;
2422 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
2423}
2424
2425static void
Alexandros Frantzis67629672018-10-19 12:14:11 +03002426weston_buffer_release_reference_handle_destroy(struct wl_listener *listener,
2427 void *data)
2428{
2429 struct weston_buffer_release_reference *ref =
2430 container_of(listener, struct weston_buffer_release_reference,
2431 destroy_listener);
2432
2433 assert((struct wl_resource *)data == ref->buffer_release->resource);
2434 ref->buffer_release = NULL;
2435}
2436
2437static void
2438weston_buffer_release_destroy(struct weston_buffer_release *buffer_release)
2439{
2440 struct wl_resource *resource = buffer_release->resource;
2441 int release_fence_fd = buffer_release->fence_fd;
2442
2443 if (release_fence_fd >= 0) {
2444 zwp_linux_buffer_release_v1_send_fenced_release(
2445 resource, release_fence_fd);
2446 } else {
2447 zwp_linux_buffer_release_v1_send_immediate_release(
2448 resource);
2449 }
2450
2451 wl_resource_destroy(resource);
2452}
2453
2454WL_EXPORT void
2455weston_buffer_release_reference(struct weston_buffer_release_reference *ref,
2456 struct weston_buffer_release *buffer_release)
2457{
2458 if (buffer_release == ref->buffer_release)
2459 return;
2460
2461 if (ref->buffer_release) {
2462 ref->buffer_release->ref_count--;
2463 wl_list_remove(&ref->destroy_listener.link);
2464 if (ref->buffer_release->ref_count == 0)
2465 weston_buffer_release_destroy(ref->buffer_release);
2466 }
2467
2468 if (buffer_release) {
2469 buffer_release->ref_count++;
2470 wl_resource_add_destroy_listener(buffer_release->resource,
2471 &ref->destroy_listener);
2472 }
2473
2474 ref->buffer_release = buffer_release;
2475 ref->destroy_listener.notify =
2476 weston_buffer_release_reference_handle_destroy;
2477}
2478
2479WL_EXPORT void
2480weston_buffer_release_move(struct weston_buffer_release_reference *dest,
2481 struct weston_buffer_release_reference *src)
2482{
2483 weston_buffer_release_reference(dest, src->buffer_release);
2484 weston_buffer_release_reference(src, NULL);
2485}
2486
2487static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002488weston_surface_attach(struct weston_surface *surface,
2489 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002490{
2491 weston_buffer_reference(&surface->buffer_ref, buffer);
2492
Pekka Paalanena6421c42012-12-04 15:58:10 +02002493 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002494 if (weston_surface_is_mapped(surface))
2495 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002496 }
2497
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002498 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02002499
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002500 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002501 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002502}
2503
Marius Vlad9fdda7f2019-06-11 16:08:55 +03002504/** weston_compositor_damage_all
2505 * \ingroup compositor
2506 */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002507WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002508weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002509{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002510 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002511
2512 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002513 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002514}
2515
Marius Vlad55d87362019-06-11 01:15:35 +03002516/**
2517 * \ingroup output
2518 */
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05002519WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002520weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002521{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002522 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002523
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002524 pixman_region32_union(&compositor->primary_plane.damage,
2525 &compositor->primary_plane.damage,
2526 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002527 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002528}
2529
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04002530static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002531surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002532{
Pekka Paalanende685b82012-12-04 15:58:12 +02002533 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002534 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002535 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002536
Marius Vlad2a1b7862019-09-05 13:12:18 +03002537 if (pixman_region32_not_empty(&surface->damage))
Marius Vlad3203ff62019-09-05 14:56:12 +03002538 TL_POINT(surface->compositor, "core_flush_damage", TLP_SURFACE(surface),
Pekka Paalanenb5026542014-11-12 15:09:24 +02002539 TLP_OUTPUT(surface->output), TLP_END);
2540
Jason Ekstrandef540082014-06-26 10:37:36 -07002541 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002542}
2543
2544static void
2545view_accumulate_damage(struct weston_view *view,
2546 pixman_region32_t *opaque)
2547{
2548 pixman_region32_t damage;
2549
2550 pixman_region32_init(&damage);
2551 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002552 pixman_box32_t *extents;
2553
Jason Ekstranda7af7042013-10-12 22:38:11 -05002554 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002555 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002556 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002557 pixman_region32_copy(&damage, &view->surface->damage);
2558 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002559 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002560 }
2561
Pekka Paalanen380adf52015-02-16 14:39:11 +02002562 pixman_region32_intersect(&damage, &damage,
2563 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002564 pixman_region32_subtract(&damage, &damage, opaque);
2565 pixman_region32_union(&view->plane->damage,
2566 &view->plane->damage, &damage);
2567 pixman_region32_fini(&damage);
2568 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002569 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002570}
2571
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002572static void
Michael Olbrichc5ea4952020-08-11 12:42:35 +02002573output_accumulate_damage(struct weston_output *output)
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002574{
Michael Olbrichc5ea4952020-08-11 12:42:35 +02002575 struct weston_compositor *ec = output->compositor;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002576 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002577 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002578 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002579
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002580 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002581
2582 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002583 pixman_region32_copy(&plane->clip, &clip);
2584
2585 pixman_region32_init(&opaque);
2586
Jason Ekstranda7af7042013-10-12 22:38:11 -05002587 wl_list_for_each(ev, &ec->view_list, link) {
2588 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002589 continue;
2590
Jason Ekstranda7af7042013-10-12 22:38:11 -05002591 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002592 }
2593
2594 pixman_region32_union(&clip, &clip, &opaque);
2595 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002596 }
2597
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002598 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002599
Jason Ekstranda7af7042013-10-12 22:38:11 -05002600 wl_list_for_each(ev, &ec->view_list, link)
Derek Foreman060cf112015-11-18 16:32:26 -06002601 ev->surface->touched = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002602
2603 wl_list_for_each(ev, &ec->view_list, link) {
Michael Olbrichc5ea4952020-08-11 12:42:35 +02002604 /* Ignore views not visible on the current output */
2605 if (!(ev->output_mask & (1u << output->id)))
2606 continue;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002607 if (ev->surface->touched)
2608 continue;
Derek Foreman060cf112015-11-18 16:32:26 -06002609 ev->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002610
2611 surface_flush_damage(ev->surface);
2612
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002613 /* Both the renderer and the backend have seen the buffer
2614 * by now. If renderer needs the buffer, it has its own
2615 * reference set. If the backend wants to keep the buffer
2616 * around for migrating the surface into a non-primary plane
2617 * later, keep_buffer is true. Otherwise, drop the core
2618 * reference now, and allow early buffer release. This enables
2619 * clients to use single-buffering.
2620 */
Alexandros Frantzis67629672018-10-19 12:14:11 +03002621 if (!ev->surface->keep_buffer) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002622 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002623 weston_buffer_release_reference(
2624 &ev->surface->buffer_release_ref, NULL);
2625 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002626 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002627}
2628
2629static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002630surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002631{
2632 struct weston_subsurface *sub;
2633
Pekka Paalanene67858b2013-04-25 13:57:42 +03002634 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002635 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002636 continue;
2637
Jason Ekstranda7af7042013-10-12 22:38:11 -05002638 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2639 wl_list_init(&sub->surface->views);
2640
2641 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002642 }
2643}
2644
2645static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002646surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002647{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002648 struct weston_subsurface *sub;
2649 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002650
Jason Ekstranda7af7042013-10-12 22:38:11 -05002651 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2652 if (sub->surface == surface)
2653 continue;
2654
George Kiagiadakised04d382014-06-13 18:10:26 +02002655 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2656 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002657 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002658 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002659
2660 surface_free_unused_subsurface_views(sub->surface);
2661 }
2662}
2663
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002664static struct weston_paint_node *
2665view_ensure_paint_node(struct weston_view *view, struct weston_output *output)
2666{
2667 struct weston_paint_node *pnode;
2668
2669 if (!output)
2670 return NULL;
2671
2672 pnode = weston_view_find_paint_node(view, output);
2673 if (pnode)
2674 return pnode;
2675
2676 return weston_paint_node_create(view->surface, view, output);
2677}
2678
Jason Ekstranda7af7042013-10-12 22:38:11 -05002679static void
2680view_list_add_subsurface_view(struct weston_compositor *compositor,
2681 struct weston_subsurface *sub,
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002682 struct weston_view *parent,
2683 struct weston_output *output)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002684{
2685 struct weston_subsurface *child;
2686 struct weston_view *view = NULL, *iv;
2687
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002688 if (!weston_surface_is_mapped(sub->surface))
2689 return;
2690
Jason Ekstranda7af7042013-10-12 22:38:11 -05002691 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2692 if (iv->geometry.parent == parent) {
2693 view = iv;
2694 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002695 }
2696 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002697
2698 if (view) {
2699 /* Put it back in the surface's list of views */
2700 wl_list_remove(&view->surface_link);
2701 wl_list_insert(&sub->surface->views, &view->surface_link);
2702 } else {
2703 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002704 weston_view_set_position(view,
2705 sub->position.x,
2706 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002707 weston_view_set_transform_parent(view, parent);
2708 }
2709
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002710 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002711 weston_view_update_transform(view);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002712 view->is_mapped = true;
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002713 view_ensure_paint_node(view, output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002714
Pekka Paalanenb188e912013-11-19 14:03:35 +02002715 if (wl_list_empty(&sub->surface->subsurface_list)) {
2716 wl_list_insert(compositor->view_list.prev, &view->link);
2717 return;
2718 }
2719
2720 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2721 if (child->surface == sub->surface)
2722 wl_list_insert(compositor->view_list.prev, &view->link);
2723 else
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002724 view_list_add_subsurface_view(compositor, child, view, output);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002725 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002726}
2727
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002728/* This recursively adds the sub-surfaces for a view, relying on the
2729 * sub-surface order. Thus, if a client restacks the sub-surfaces, that
2730 * change first happens to the sub-surface list, and then automatically
2731 * propagates here. See weston_surface_damage_subsurfaces() for how the
2732 * sub-surfaces receive damage when the client changes the state.
2733 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002734static void
2735view_list_add(struct weston_compositor *compositor,
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002736 struct weston_view *view,
2737 struct weston_output *output)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002738{
2739 struct weston_subsurface *sub;
2740
2741 weston_view_update_transform(view);
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002742 view_ensure_paint_node(view, output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002743
Pekka Paalanenb188e912013-11-19 14:03:35 +02002744 if (wl_list_empty(&view->surface->subsurface_list)) {
2745 wl_list_insert(compositor->view_list.prev, &view->link);
2746 return;
2747 }
2748
2749 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2750 if (sub->surface == view->surface)
2751 wl_list_insert(compositor->view_list.prev, &view->link);
2752 else
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002753 view_list_add_subsurface_view(compositor, sub, view, output);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002754 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002755}
2756
2757static void
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002758weston_compositor_build_view_list(struct weston_compositor *compositor,
2759 struct weston_output *output)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002760{
Loïc Yhuel267b16e2019-09-17 20:14:56 +02002761 struct weston_view *view, *tmp;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002762 struct weston_layer *layer;
2763
2764 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002765 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002766 surface_stash_subsurface_views(view->surface);
2767
Loïc Yhuel267b16e2019-09-17 20:14:56 +02002768 wl_list_for_each_safe(view, tmp, &compositor->view_list, link)
2769 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002770 wl_list_init(&compositor->view_list);
Loïc Yhuel267b16e2019-09-17 20:14:56 +02002771
Jason Ekstranda7af7042013-10-12 22:38:11 -05002772 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002773 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002774 view_list_add(compositor, view, output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002775 }
2776 }
2777
2778 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002779 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002780 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002781}
2782
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002783static void
2784weston_output_take_feedback_list(struct weston_output *output,
2785 struct weston_surface *surface)
2786{
2787 struct weston_view *view;
2788 struct weston_presentation_feedback *feedback;
2789 uint32_t flags = 0xffffffff;
2790
2791 if (wl_list_empty(&surface->feedback_list))
2792 return;
2793
2794 /* All views must have the flag for the flag to survive. */
2795 wl_list_for_each(view, &surface->views, surface_link) {
2796 /* ignore views that are not on this output at all */
2797 if (view->output_mask & (1u << output->id))
2798 flags &= view->psf_flags;
2799 }
2800
2801 wl_list_for_each(feedback, &surface->feedback_list, link)
2802 feedback->psf_flags = flags;
2803
2804 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2805 wl_list_init(&surface->feedback_list);
2806}
2807
David Herrmann1edf44c2013-10-22 17:11:26 +02002808static int
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002809weston_output_repaint(struct weston_output *output, void *repaint_data)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002810{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002811 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002812 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002813 struct weston_animation *animation, *next;
2814 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002815 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002816 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002817 int r;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002818 uint32_t frame_time_msec;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05302819 enum weston_hdcp_protection highest_requested = WESTON_HDCP_DISABLE;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002820
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002821 if (output->destroying)
2822 return 0;
2823
Marius Vlad3203ff62019-09-05 14:56:12 +03002824 TL_POINT(ec, "core_repaint_begin", TLP_OUTPUT(output), TLP_END);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002825
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002826 /* Rebuild the surface list and update surface transforms up front. */
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03002827 weston_compositor_build_view_list(ec, output);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002828
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05302829 /* Find the highest protection desired for an output */
2830 wl_list_for_each(ev, &ec->view_list, link) {
2831 if (ev->surface->output_mask & (1u << output->id)) {
2832 /*
2833 * The desired_protection of the output should be the
2834 * maximum of the desired_protection of the surfaces,
2835 * that are displayed on that output, to avoid
2836 * reducing the protection for existing surfaces.
2837 */
2838 if (ev->surface->desired_protection > highest_requested)
2839 highest_requested =
2840 ev->surface->desired_protection;
2841 }
2842 }
2843
2844 output->desired_protection = highest_requested;
2845
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002846 if (output->assign_planes && !output->disable_planes) {
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002847 output->assign_planes(output, repaint_data);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002848 } else {
2849 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002850 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002851 ev->psf_flags = 0;
2852 }
2853 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002854
Pekka Paalanene67858b2013-04-25 13:57:42 +03002855 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002856 wl_list_for_each(ev, &ec->view_list, link) {
2857 /* Note: This operation is safe to do multiple times on the
2858 * same surface.
2859 */
2860 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002861 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002862 &ev->surface->frame_callback_list);
2863 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002864
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002865 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002866 }
2867 }
2868
Michael Olbrichc5ea4952020-08-11 12:42:35 +02002869 output_accumulate_damage(output);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002870
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002871 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002872 pixman_region32_intersect(&output_damage,
2873 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002874 pixman_region32_subtract(&output_damage,
2875 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002876
Scott Moreauccbf29d2012-02-22 14:21:41 -07002877 if (output->dirty)
2878 weston_output_update_matrix(output);
2879
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002880 r = output->repaint(output, &output_damage, repaint_data);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002881
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002882 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002883
Daniel Stone09a97e22017-03-01 11:34:06 +00002884 output->repaint_needed = false;
Daniel Stone05df8c12017-03-03 16:59:42 +00002885 if (r == 0)
2886 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002887
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002888 weston_compositor_repick(ec);
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002889
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002890 frame_time_msec = timespec_to_msec(&output->frame_time);
2891
Jonas Ådahldb773762012-06-13 00:01:21 +02002892 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002893 wl_callback_send_done(cb->resource, frame_time_msec);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002894 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002895 }
2896
Scott Moreaud64cf212012-06-08 19:40:54 -06002897 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002898 animation->frame_counter++;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002899 animation->frame(animation, output, &output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002900 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002901
Marius Vlad3203ff62019-09-05 14:56:12 +03002902 TL_POINT(ec, "core_repaint_posted", TLP_OUTPUT(output), TLP_END);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002903
David Herrmann1edf44c2013-10-22 17:11:26 +02002904 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002905}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002906
Pekka Paalanen82919792014-05-21 13:51:49 +03002907static void
2908weston_output_schedule_repaint_reset(struct weston_output *output)
2909{
Daniel Stone05df8c12017-03-03 16:59:42 +00002910 output->repaint_status = REPAINT_NOT_SCHEDULED;
Marius Vlad3203ff62019-09-05 14:56:12 +03002911 TL_POINT(output->compositor, "core_repaint_exit_loop",
2912 TLP_OUTPUT(output), TLP_END);
Pekka Paalanen82919792014-05-21 13:51:49 +03002913}
2914
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002915static int
2916weston_output_maybe_repaint(struct weston_output *output, struct timespec *now,
2917 void *repaint_data)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002918{
Pekka Paalanen0513a952014-05-21 16:17:27 +03002919 struct weston_compositor *compositor = output->compositor;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002920 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002921 int64_t msec_to_repaint;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002922
Daniel Stone6847b852017-03-01 11:34:08 +00002923 /* We're not ready yet; come back to make a decision later. */
2924 if (output->repaint_status != REPAINT_SCHEDULED)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002925 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002926
2927 msec_to_repaint = timespec_sub_to_msec(&output->next_repaint, now);
2928 if (msec_to_repaint > 1)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002929 return ret;
Daniel Stone05df8c12017-03-03 16:59:42 +00002930
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002931 /* If we're sleeping, drop the repaint machinery entirely; we will
2932 * explicitly repaint all outputs when we come back. */
2933 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2934 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
2935 goto err;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002936
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002937 /* We don't actually need to repaint this output; drop it from
2938 * repaint until something causes damage. */
2939 if (!output->repaint_needed)
2940 goto err;
2941
2942 /* If repaint fails, we aren't going to get weston_output_finish_frame
2943 * to trigger a new repaint, so drop it from repaint and hope
Daniel Stone6847b852017-03-01 11:34:08 +00002944 * something schedules a successful repaint later. As repainting may
2945 * take some time, re-read our clock as a courtesy to the next
2946 * output. */
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002947 ret = weston_output_repaint(output, repaint_data);
Daniel Stone6847b852017-03-01 11:34:08 +00002948 weston_compositor_read_presentation_clock(compositor, now);
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002949 if (ret != 0)
2950 goto err;
2951
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09002952 output->repainted = true;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002953 return ret;
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002954
2955err:
Pekka Paalanen0513a952014-05-21 16:17:27 +03002956 weston_output_schedule_repaint_reset(output);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002957 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002958}
2959
2960static void
2961output_repaint_timer_arm(struct weston_compositor *compositor)
2962{
2963 struct weston_output *output;
2964 bool any_should_repaint = false;
2965 struct timespec now;
Sergi Granellb4c08862017-03-18 13:01:15 +01002966 int64_t msec_to_next = INT64_MAX;
Daniel Stone6847b852017-03-01 11:34:08 +00002967
2968 weston_compositor_read_presentation_clock(compositor, &now);
2969
2970 wl_list_for_each(output, &compositor->output_list, link) {
2971 int64_t msec_to_this;
2972
2973 if (output->repaint_status != REPAINT_SCHEDULED)
2974 continue;
2975
2976 msec_to_this = timespec_sub_to_msec(&output->next_repaint,
2977 &now);
2978 if (!any_should_repaint || msec_to_this < msec_to_next)
2979 msec_to_next = msec_to_this;
2980
2981 any_should_repaint = true;
2982 }
2983
2984 if (!any_should_repaint)
2985 return;
2986
2987 /* Even if we should repaint immediately, add the minimum 1 ms delay.
2988 * This is a workaround to allow coalescing multiple output repaints
2989 * particularly from weston_output_finish_frame()
2990 * into the same call, which would not happen if we called
2991 * output_repaint_timer_handler() directly.
2992 */
2993 if (msec_to_next < 1)
2994 msec_to_next = 1;
2995
2996 wl_event_source_timer_update(compositor->repaint_timer, msec_to_next);
2997}
2998
2999static int
3000output_repaint_timer_handler(void *data)
3001{
3002 struct weston_compositor *compositor = data;
3003 struct weston_output *output;
3004 struct timespec now;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003005 void *repaint_data = NULL;
Emre Ucane479ed82018-03-20 15:29:40 +01003006 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00003007
3008 weston_compositor_read_presentation_clock(compositor, &now);
Pekka Paalanen1ed2cad2021-02-10 12:33:03 +02003009 compositor->last_repaint_start = now;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003010
3011 if (compositor->backend->repaint_begin)
3012 repaint_data = compositor->backend->repaint_begin(compositor);
3013
3014 wl_list_for_each(output, &compositor->output_list, link) {
3015 ret = weston_output_maybe_repaint(output, &now, repaint_data);
3016 if (ret)
3017 break;
3018 }
3019
3020 if (ret == 0) {
Tomohito Esaki09bfcd62018-06-05 10:37:05 +09003021 if (compositor->backend->repaint_flush)
Antonio Borneoc90fccc2019-06-30 15:51:10 +02003022 ret = compositor->backend->repaint_flush(compositor,
3023 repaint_data);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003024 } else {
Antonio Borneoc90fccc2019-06-30 15:51:10 +02003025 if (compositor->backend->repaint_cancel)
3026 compositor->backend->repaint_cancel(compositor,
3027 repaint_data);
3028 }
3029
3030 if (ret != 0) {
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09003031 wl_list_for_each(output, &compositor->output_list, link) {
3032 if (output->repainted)
3033 weston_output_schedule_repaint_reset(output);
3034 }
Daniel Stoneb1f166d2017-03-01 11:34:10 +00003035 }
Daniel Stone6847b852017-03-01 11:34:08 +00003036
Tomohito Esakiddaf95c2018-07-10 11:47:15 +09003037 wl_list_for_each(output, &compositor->output_list, link)
3038 output->repainted = false;
3039
Daniel Stone6847b852017-03-01 11:34:08 +00003040 output_repaint_timer_arm(compositor);
3041
Pekka Paalanen0513a952014-05-21 16:17:27 +03003042 return 0;
3043}
3044
Pekka Paalanen50aa3a72020-05-28 11:34:04 +03003045/** Convert a presentation timestamp to another clock domain
3046 *
3047 * \param compositor The compositor defines the presentation clock domain.
3048 * \param presentation_stamp The timestamp in presentation clock domain.
3049 * \param presentation_now Current time in presentation clock domain.
3050 * \param target_clock Defines the target clock domain.
3051 *
3052 * This approximation relies on presentation_stamp to be close to current time.
3053 * The further it is from current time and the bigger the speed difference
3054 * between the two clock domains, the bigger the conversion error.
3055 *
3056 * Conversion error due to system load is biased and unbounded.
3057 */
3058static struct timespec
3059convert_presentation_time_now(struct weston_compositor *compositor,
3060 const struct timespec *presentation_stamp,
3061 const struct timespec *presentation_now,
3062 clockid_t target_clock)
3063{
3064 struct timespec target_now = {};
3065 struct timespec target_stamp;
3066 int64_t delta_ns;
3067
3068 if (compositor->presentation_clock == target_clock)
3069 return *presentation_stamp;
3070
3071 clock_gettime(target_clock, &target_now);
3072 delta_ns = timespec_sub_to_nsec(presentation_stamp, presentation_now);
3073 timespec_add_nsec(&target_stamp, &target_now, delta_ns);
3074
3075 return target_stamp;
3076}
3077
Marius Vlad55d87362019-06-11 01:15:35 +03003078/**
3079 * \ingroup output
3080 */
Kristian Høgsbergef044142011-06-21 15:02:12 -04003081WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04003082weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02003083 const struct timespec *stamp,
3084 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04003085{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003086 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03003087 int32_t refresh_nsec;
3088 struct timespec now;
Pekka Paalanen50aa3a72020-05-28 11:34:04 +03003089 struct timespec vblank_monotonic;
Daniel Stone6847b852017-03-01 11:34:08 +00003090 int64_t msec_rel;
Pekka Paalanen133e4392014-09-23 22:08:46 -04003091
Daniel Stone05df8c12017-03-03 16:59:42 +00003092 assert(output->repaint_status == REPAINT_AWAITING_COMPLETION);
Pekka Paalanen98b4e202021-05-10 11:33:23 +03003093
3094 /*
3095 * If timestamp of latest vblank is given, it must always go forwards.
3096 * If not given, INVALID flag must be set.
3097 */
3098 if (stamp)
3099 assert(timespec_sub_to_nsec(stamp, &output->frame_time) >= 0);
3100 else
3101 assert(presented_flags & WP_PRESENTATION_FEEDBACK_INVALID);
Daniel Stone3615ce12017-03-01 11:34:05 +00003102
Daniel Stone6847b852017-03-01 11:34:08 +00003103 weston_compositor_read_presentation_clock(compositor, &now);
3104
Daniel Stone3615ce12017-03-01 11:34:05 +00003105 /* If we haven't been supplied any timestamp at all, we don't have a
3106 * timebase to work against, so any delay just wastes time. Push a
3107 * repaint as soon as possible so we can get on with it. */
Daniel Stone6847b852017-03-01 11:34:08 +00003108 if (!stamp) {
3109 output->next_repaint = now;
Daniel Stone3615ce12017-03-01 11:34:05 +00003110 goto out;
Daniel Stone6847b852017-03-01 11:34:08 +00003111 }
Daniel Stone3615ce12017-03-01 11:34:05 +00003112
Pekka Paalanen50aa3a72020-05-28 11:34:04 +03003113 vblank_monotonic = convert_presentation_time_now(compositor,
3114 stamp, &now,
3115 CLOCK_MONOTONIC);
Marius Vlad3203ff62019-09-05 14:56:12 +03003116 TL_POINT(compositor, "core_repaint_finished", TLP_OUTPUT(output),
Pekka Paalanen50aa3a72020-05-28 11:34:04 +03003117 TLP_VBLANK(&vblank_monotonic), TLP_END);
Marius Vladdf9278a2018-03-06 18:56:23 +02003118
Pekka Paalanend7894d02015-07-03 15:08:53 +03003119 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003120 weston_presentation_feedback_present_list(&output->feedback_list,
3121 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02003122 output->msc,
3123 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003124
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02003125 output->frame_time = *stamp;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003126
Daniel Stone6847b852017-03-01 11:34:08 +00003127 timespec_add_nsec(&output->next_repaint, stamp, refresh_nsec);
3128 timespec_add_msec(&output->next_repaint, &output->next_repaint,
3129 -compositor->repaint_msec);
3130 msec_rel = timespec_sub_to_msec(&output->next_repaint, &now);
Daniel Stone84aff5c2017-03-01 11:34:04 +00003131
3132 if (msec_rel < -1000 || msec_rel > 1000) {
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02003133 static bool warned;
3134
3135 if (!warned)
3136 weston_log("Warning: computed repaint delay is "
Daniel Stone6847b852017-03-01 11:34:08 +00003137 "insane: %lld msec\n", (long long) msec_rel);
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02003138 warned = true;
3139
Daniel Stone6847b852017-03-01 11:34:08 +00003140 output->next_repaint = now;
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02003141 }
3142
Mario Kleinerb7df04e2015-06-21 21:25:15 +02003143 /* Called from restart_repaint_loop and restart happens already after
3144 * the deadline given by repaint_msec? In that case we delay until
3145 * the deadline of the next frame, to give clients a more predictable
3146 * timing of the repaint cycle to lock on. */
Daniel Stoneeca5cca2017-02-28 21:53:51 +00003147 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID &&
3148 msec_rel < 0) {
3149 while (timespec_sub_to_nsec(&output->next_repaint, &now) < 0) {
3150 timespec_add_nsec(&output->next_repaint,
3151 &output->next_repaint,
3152 refresh_nsec);
3153 }
3154 }
Mario Kleinerb7df04e2015-06-21 21:25:15 +02003155
Daniel Stone3615ce12017-03-01 11:34:05 +00003156out:
Daniel Stone05df8c12017-03-03 16:59:42 +00003157 output->repaint_status = REPAINT_SCHEDULED;
Daniel Stone6847b852017-03-01 11:34:08 +00003158 output_repaint_timer_arm(compositor);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003159}
3160
3161static void
3162idle_repaint(void *data)
3163{
3164 struct weston_output *output = data;
Antonio Borneoc90fccc2019-06-30 15:51:10 +02003165 int ret;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003166
Daniel Stone05df8c12017-03-03 16:59:42 +00003167 assert(output->repaint_status == REPAINT_BEGIN_FROM_IDLE);
3168 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03003169 output->idle_repaint_source = NULL;
Antonio Borneoc90fccc2019-06-30 15:51:10 +02003170 ret = output->start_repaint_loop(output);
3171 if (ret != 0)
3172 weston_output_schedule_repaint_reset(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003173}
3174
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003175WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003176weston_layer_entry_insert(struct weston_layer_entry *list,
3177 struct weston_layer_entry *entry)
3178{
3179 wl_list_insert(&list->link, &entry->link);
3180 entry->layer = list->layer;
3181}
3182
3183WL_EXPORT void
3184weston_layer_entry_remove(struct weston_layer_entry *entry)
3185{
3186 wl_list_remove(&entry->link);
3187 wl_list_init(&entry->link);
3188 entry->layer = NULL;
3189}
3190
Quentin Glidic82681572016-12-17 13:40:51 +01003191
3192/** Initialize the weston_layer struct.
3193 *
3194 * \param compositor The compositor instance
3195 * \param layer The layer to initialize
3196 */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003197WL_EXPORT void
Quentin Glidic82681572016-12-17 13:40:51 +01003198weston_layer_init(struct weston_layer *layer,
3199 struct weston_compositor *compositor)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003200{
Quentin Glidic82681572016-12-17 13:40:51 +01003201 layer->compositor = compositor;
3202 wl_list_init(&layer->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003203 wl_list_init(&layer->view_list.link);
3204 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003205 weston_layer_set_mask_infinite(layer);
Quentin Glidic82681572016-12-17 13:40:51 +01003206}
3207
3208/** Sets the position of the layer in the layer list. The layer will be placed
3209 * below any layer with the same position value, if any.
3210 * This function is safe to call if the layer is already on the list, but the
3211 * layer may be moved below other layers at the same position, if any.
3212 *
3213 * \param layer The layer to modify
3214 * \param position The position the layer will be placed at
3215 */
3216WL_EXPORT void
3217weston_layer_set_position(struct weston_layer *layer,
3218 enum weston_layer_position position)
3219{
3220 struct weston_layer *below;
3221
3222 wl_list_remove(&layer->link);
3223
3224 /* layer_list is ordered from top to bottom, the last layer being the
3225 * background with the smallest position value */
3226
3227 layer->position = position;
3228 wl_list_for_each_reverse(below, &layer->compositor->layer_list, link) {
3229 if (below->position >= layer->position) {
3230 wl_list_insert(&below->link, &layer->link);
3231 return;
3232 }
3233 }
3234 wl_list_insert(&layer->compositor->layer_list, &layer->link);
3235}
3236
3237/** Hide a layer by taking it off the layer list.
3238 * This function is safe to call if the layer is not on the list.
3239 *
3240 * \param layer The layer to hide
3241 */
3242WL_EXPORT void
3243weston_layer_unset_position(struct weston_layer *layer)
3244{
3245 wl_list_remove(&layer->link);
3246 wl_list_init(&layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003247}
3248
3249WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003250weston_layer_set_mask(struct weston_layer *layer,
3251 int x, int y, int width, int height)
3252{
3253 struct weston_view *view;
3254
3255 layer->mask.x1 = x;
3256 layer->mask.x2 = x + width;
3257 layer->mask.y1 = y;
3258 layer->mask.y2 = y + height;
3259
3260 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
3261 weston_view_geometry_dirty(view);
3262 }
3263}
3264
3265WL_EXPORT void
3266weston_layer_set_mask_infinite(struct weston_layer *layer)
3267{
Adam Jackson3c3f3b12019-10-16 16:02:59 -04003268 struct weston_view *view;
3269
3270 layer->mask.x1 = INT32_MIN;
3271 layer->mask.x2 = INT32_MAX;
3272 layer->mask.y1 = INT32_MIN;
3273 layer->mask.y2 = INT32_MAX;
3274
3275 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
3276 weston_view_geometry_dirty(view);
3277 }
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003278}
3279
Daniel Stone3b775632018-07-20 08:38:25 +01003280WL_EXPORT bool
3281weston_layer_mask_is_infinite(struct weston_layer *layer)
3282{
3283 return layer->mask.x1 == INT32_MIN &&
3284 layer->mask.y1 == INT32_MIN &&
Adam Jackson3c3f3b12019-10-16 16:02:59 -04003285 layer->mask.x2 == INT32_MAX &&
3286 layer->mask.y2 == INT32_MAX;
Daniel Stone3b775632018-07-20 08:38:25 +01003287}
3288
Marius Vlad55d87362019-06-11 01:15:35 +03003289/**
3290 * \ingroup output
3291 */
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003292WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003293weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003294{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003295 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04003296 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01003297
Bryce Harrington08976ac2016-08-30 12:05:16 -07003298 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
3299 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003300 return;
3301
Pekka Paalanenb5026542014-11-12 15:09:24 +02003302 if (!output->repaint_needed)
Marius Vlad3203ff62019-09-05 14:56:12 +03003303 TL_POINT(compositor, "core_repaint_req", TLP_OUTPUT(output), TLP_END);
Pekka Paalanenb5026542014-11-12 15:09:24 +02003304
Kristian Høgsbergef044142011-06-21 15:02:12 -04003305 loop = wl_display_get_event_loop(compositor->wl_display);
Daniel Stone09a97e22017-03-01 11:34:06 +00003306 output->repaint_needed = true;
Daniel Stone05df8c12017-03-03 16:59:42 +00003307
3308 /* If we already have a repaint scheduled for our idle handler,
3309 * no need to set it again. If the repaint has been called but
3310 * not finished, then weston_output_finish_frame() will notice
3311 * that a repaint is needed and schedule one. */
3312 if (output->repaint_status != REPAINT_NOT_SCHEDULED)
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003313 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01003314
Daniel Stone05df8c12017-03-03 16:59:42 +00003315 output->repaint_status = REPAINT_BEGIN_FROM_IDLE;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03003316 assert(!output->idle_repaint_source);
3317 output->idle_repaint_source = wl_event_loop_add_idle(loop, idle_repaint,
3318 output);
Marius Vlad3203ff62019-09-05 14:56:12 +03003319 TL_POINT(compositor, "core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003320}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05003321
Marius Vlad9fdda7f2019-06-11 16:08:55 +03003322/** weston_compositor_schedule_repaint
3323 * \ingroup compositor
3324 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003325WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003326weston_compositor_schedule_repaint(struct weston_compositor *compositor)
3327{
3328 struct weston_output *output;
3329
3330 wl_list_for_each(output, &compositor->output_list, link)
3331 weston_output_schedule_repaint(output);
3332}
3333
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003334static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003335surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003336{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003337 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003338}
3339
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003340static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003341surface_attach(struct wl_client *client,
3342 struct wl_resource *resource,
3343 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
3344{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003345 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05003346 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003347
Kristian Høgsbergab19f932013-08-20 11:30:54 -07003348 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05003349 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07003350 if (buffer == NULL) {
3351 wl_client_post_no_memory(client);
3352 return;
3353 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07003354 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04003355
Pekka Paalanende685b82012-12-04 15:58:12 +02003356 /* Attach, attach, without commit in between does not send
3357 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003358 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003359
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003360 surface->pending.sx = sx;
3361 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01003362 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003363}
3364
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003365static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003366surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003367 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003368 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05003369{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003370 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003371
Derek Foreman57e92ed2015-11-17 14:11:35 -06003372 if (width <= 0 || height <= 0)
3373 return;
3374
Derek Foreman152254b2015-11-26 14:17:48 -06003375 pixman_region32_union_rect(&surface->pending.damage_surface,
3376 &surface->pending.damage_surface,
3377 x, y, width, height);
3378}
3379
3380static void
3381surface_damage_buffer(struct wl_client *client,
3382 struct wl_resource *resource,
3383 int32_t x, int32_t y, int32_t width, int32_t height)
3384{
3385 struct weston_surface *surface = wl_resource_get_user_data(resource);
3386
3387 if (width <= 0 || height <= 0)
3388 return;
3389
3390 pixman_region32_union_rect(&surface->pending.damage_buffer,
3391 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04003392 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003393}
3394
Kristian Høgsberg33418202011-08-16 23:01:28 -04003395static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003396destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04003397{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05003398 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003399
3400 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02003401 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003402}
3403
3404static void
3405surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003406 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04003407{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003408 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003409 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003410
3411 cb = malloc(sizeof *cb);
3412 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003413 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003414 return;
3415 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03003416
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003417 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
3418 callback);
3419 if (cb->resource == NULL) {
3420 free(cb);
3421 wl_resource_post_no_memory(resource);
3422 return;
3423 }
3424
Jason Ekstranda85118c2013-06-27 20:17:02 -05003425 wl_resource_set_implementation(cb->resource, NULL, cb,
3426 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003427
Pekka Paalanenbc106382012-10-10 12:49:31 +03003428 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003429}
3430
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003431static void
3432surface_set_opaque_region(struct wl_client *client,
3433 struct wl_resource *resource,
3434 struct wl_resource *region_resource)
3435{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003436 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003437 struct weston_region *region;
3438
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003439 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003440 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003441 pixman_region32_copy(&surface->pending.opaque,
3442 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003443 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07003444 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003445 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003446}
3447
3448static void
3449surface_set_input_region(struct wl_client *client,
3450 struct wl_resource *resource,
3451 struct wl_resource *region_resource)
3452{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003453 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003454 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003455
3456 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003457 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003458 pixman_region32_copy(&surface->pending.input,
3459 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003460 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003461 pixman_region32_fini(&surface->pending.input);
3462 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003463 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003464}
3465
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003466/* Cause damage to this sub-surface and all its children.
3467 *
3468 * This is useful when there are state changes that need an implicit
3469 * damage, e.g. a z-order change.
3470 */
3471static void
3472weston_surface_damage_subsurfaces(struct weston_subsurface *sub)
3473{
3474 struct weston_subsurface *child;
3475
3476 weston_surface_damage(sub->surface);
3477 sub->reordered = false;
3478
3479 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link)
3480 if (child != sub)
3481 weston_surface_damage_subsurfaces(child);
3482}
3483
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003484static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003485weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003486{
Pekka Paalanene67858b2013-04-25 13:57:42 +03003487 struct weston_subsurface *sub;
3488
3489 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
3490 parent_link_pending) {
3491 wl_list_remove(&sub->parent_link);
3492 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003493
3494 if (sub->reordered)
3495 weston_surface_damage_subsurfaces(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003496 }
3497}
3498
3499static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03003500weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05003501 struct weston_matrix *matrix)
3502{
Pekka Paalanen04baea52016-04-26 15:50:58 +03003503 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05003504 double src_width, src_height, dest_width, dest_height;
3505
3506 weston_matrix_init(matrix);
3507
3508 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
3509 src_width = surface->width_from_buffer;
3510 src_height = surface->height_from_buffer;
3511 } else {
3512 src_width = wl_fixed_to_double(vp->buffer.src_width);
3513 src_height = wl_fixed_to_double(vp->buffer.src_height);
3514 }
3515
3516 if (vp->surface.width == -1) {
3517 dest_width = src_width;
3518 dest_height = src_height;
3519 } else {
3520 dest_width = vp->surface.width;
3521 dest_height = vp->surface.height;
3522 }
3523
3524 if (src_width != dest_width || src_height != dest_height)
3525 weston_matrix_scale(matrix,
3526 src_width / dest_width,
3527 src_height / dest_height, 1);
3528
3529 if (vp->buffer.src_width != wl_fixed_from_int(-1))
3530 weston_matrix_translate(matrix,
3531 wl_fixed_to_double(vp->buffer.src_x),
3532 wl_fixed_to_double(vp->buffer.src_y),
3533 0);
3534
3535 switch (vp->buffer.transform) {
3536 case WL_OUTPUT_TRANSFORM_FLIPPED:
3537 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3538 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3539 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3540 weston_matrix_scale(matrix, -1, 1, 1);
3541 weston_matrix_translate(matrix,
3542 surface->width_from_buffer, 0, 0);
3543 break;
3544 }
3545
3546 switch (vp->buffer.transform) {
3547 default:
3548 case WL_OUTPUT_TRANSFORM_NORMAL:
3549 case WL_OUTPUT_TRANSFORM_FLIPPED:
3550 break;
3551 case WL_OUTPUT_TRANSFORM_90:
3552 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +02003553 weston_matrix_rotate_xy(matrix, 0, -1);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003554 weston_matrix_translate(matrix,
Pekka Paalanen8060d822020-02-06 15:27:54 +02003555 0, surface->width_from_buffer, 0);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003556 break;
3557 case WL_OUTPUT_TRANSFORM_180:
3558 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3559 weston_matrix_rotate_xy(matrix, -1, 0);
3560 weston_matrix_translate(matrix,
3561 surface->width_from_buffer,
3562 surface->height_from_buffer, 0);
3563 break;
3564 case WL_OUTPUT_TRANSFORM_270:
3565 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +02003566 weston_matrix_rotate_xy(matrix, 0, 1);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003567 weston_matrix_translate(matrix,
Pekka Paalanen8060d822020-02-06 15:27:54 +02003568 surface->height_from_buffer, 0, 0);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003569 break;
3570 }
3571
3572 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
3573}
3574
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003575/**
3576 * Compute a + b > c while being safe to overflows.
3577 */
3578static bool
3579fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
3580{
3581 return (int64_t)a + (int64_t)b > (int64_t)c;
3582}
3583
3584static bool
3585weston_surface_is_pending_viewport_source_valid(
3586 const struct weston_surface *surface)
3587{
3588 const struct weston_surface_state *pend = &surface->pending;
3589 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
3590 int width_from_buffer = 0;
3591 int height_from_buffer = 0;
3592 wl_fixed_t w;
3593 wl_fixed_t h;
3594
3595 /* If viewport source rect is not set, it is always ok. */
3596 if (vp->buffer.src_width == wl_fixed_from_int(-1))
3597 return true;
3598
3599 if (pend->newly_attached) {
3600 if (pend->buffer) {
3601 convert_size_by_transform_scale(&width_from_buffer,
3602 &height_from_buffer,
3603 pend->buffer->width,
3604 pend->buffer->height,
3605 vp->buffer.transform,
3606 vp->buffer.scale);
3607 } else {
3608 /* No buffer: viewport is irrelevant. */
3609 return true;
3610 }
3611 } else {
3612 width_from_buffer = surface->width_from_buffer;
3613 height_from_buffer = surface->height_from_buffer;
3614 }
3615
3616 assert((width_from_buffer == 0) == (height_from_buffer == 0));
3617 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
3618
3619 /* No buffer: viewport is irrelevant. */
3620 if (width_from_buffer == 0 || height_from_buffer == 0)
3621 return true;
3622
3623 /* overflow checks for wl_fixed_from_int() */
3624 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
3625 return false;
3626 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
3627 return false;
3628
3629 w = wl_fixed_from_int(width_from_buffer);
3630 h = wl_fixed_from_int(height_from_buffer);
3631
3632 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
3633 return false;
3634 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
3635 return false;
3636
3637 return true;
3638}
3639
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003640static bool
3641fixed_is_integer(wl_fixed_t v)
3642{
3643 return (v & 0xff) == 0;
3644}
3645
3646static bool
3647weston_surface_is_pending_viewport_dst_size_int(
3648 const struct weston_surface *surface)
3649{
3650 const struct weston_buffer_viewport *vp =
3651 &surface->pending.buffer_viewport;
3652
3653 if (vp->surface.width != -1) {
3654 assert(vp->surface.width > 0 && vp->surface.height > 0);
3655 return true;
3656 }
3657
3658 return fixed_is_integer(vp->buffer.src_width) &&
3659 fixed_is_integer(vp->buffer.src_height);
3660}
3661
Derek Foreman152254b2015-11-26 14:17:48 -06003662/* Translate pending damage in buffer co-ordinates to surface
3663 * co-ordinates and union it with a pixman_region32_t.
3664 * This should only be called after the buffer is attached.
3665 */
3666static void
3667apply_damage_buffer(pixman_region32_t *dest,
3668 struct weston_surface *surface,
3669 struct weston_surface_state *state)
3670{
3671 struct weston_buffer *buffer = surface->buffer_ref.buffer;
3672
3673 /* wl_surface.damage_buffer needs to be clipped to the buffer,
3674 * translated into surface co-ordinates and unioned with
3675 * any other surface damage.
3676 * None of this makes sense if there is no buffer though.
3677 */
3678 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
3679 pixman_region32_t buffer_damage;
3680
3681 pixman_region32_intersect_rect(&state->damage_buffer,
3682 &state->damage_buffer,
3683 0, 0, buffer->width,
3684 buffer->height);
3685 pixman_region32_init(&buffer_damage);
3686 weston_matrix_transform_region(&buffer_damage,
3687 &surface->buffer_to_surface_matrix,
3688 &state->damage_buffer);
3689 pixman_region32_union(dest, dest, &buffer_damage);
3690 pixman_region32_fini(&buffer_damage);
3691 }
3692 /* We should clear this on commit even if there was no buffer */
3693 pixman_region32_clear(&state->damage_buffer);
3694}
3695
Jason Ekstrand1e059042014-10-16 10:55:19 -05003696static void
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303697weston_surface_set_desired_protection(struct weston_surface *surface,
3698 enum weston_hdcp_protection protection)
3699{
3700 if (surface->desired_protection == protection)
3701 return;
3702 surface->desired_protection = protection;
3703 weston_surface_damage(surface);
3704}
3705
3706static void
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303707weston_surface_set_protection_mode(struct weston_surface *surface,
3708 enum weston_surface_protection_mode p_mode)
3709{
3710 struct content_protection *cp = surface->compositor->content_protection;
3711 struct protected_surface *psurface;
3712
3713 surface->protection_mode = p_mode;
3714 wl_list_for_each(psurface, &cp->protected_list, link) {
3715 if (!psurface || psurface->surface != surface)
3716 continue;
3717 weston_protected_surface_send_event(psurface,
3718 surface->current_protection);
3719 }
3720}
3721
3722static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05003723weston_surface_commit_state(struct weston_surface *surface,
3724 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003725{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003726 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003727 pixman_region32_t opaque;
3728
Alexander Larsson4ea95522013-05-22 14:41:37 +02003729 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02003730 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03003731 /* wp_viewport.set_source */
3732 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003733 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003734
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003735 /* wl_surface.attach */
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003736 if (state->newly_attached) {
3737 /* zwp_surface_synchronization_v1.set_acquire_fence */
3738 fd_move(&surface->acquire_fence_fd,
3739 &state->acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003740 /* zwp_surface_synchronization_v1.get_release */
3741 weston_buffer_release_move(&surface->buffer_release_ref,
3742 &state->buffer_release_ref);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003743 weston_surface_attach(surface, state->buffer);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003744 }
Jason Ekstrand7b982072014-05-20 14:33:03 -05003745 weston_surface_state_set_buffer(state, NULL);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003746 assert(state->acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003747 assert(state->buffer_release_ref.buffer_release == NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01003748
Jason Ekstrand1e059042014-10-16 10:55:19 -05003749 weston_surface_build_buffer_matrix(surface,
3750 &surface->surface_to_buffer_matrix);
3751 weston_matrix_invert(&surface->buffer_to_surface_matrix,
3752 &surface->surface_to_buffer_matrix);
3753
Jason Ekstrand7b982072014-05-20 14:33:03 -05003754 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003755 weston_surface_update_size(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003756 if (surface->committed)
3757 surface->committed(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003758 }
Giulio Camuffo184df502013-02-21 11:29:21 +01003759
Jason Ekstrand7b982072014-05-20 14:33:03 -05003760 state->sx = 0;
3761 state->sy = 0;
3762 state->newly_attached = 0;
3763 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03003764
Derek Foreman152254b2015-11-26 14:17:48 -06003765 /* wl_surface.damage and wl_surface.damage_buffer */
Marius Vlad2a1b7862019-09-05 13:12:18 +03003766 if (pixman_region32_not_empty(&state->damage_surface) ||
3767 pixman_region32_not_empty(&state->damage_buffer))
Marius Vlad3203ff62019-09-05 14:56:12 +03003768 TL_POINT(surface->compositor, "core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06003769
Pekka Paalanen8e159182012-10-10 12:49:25 +03003770 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06003771 &state->damage_surface);
3772
3773 apply_damage_buffer(&surface->damage, surface, state);
3774
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05003775 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07003776 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06003777 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003778
3779 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003780 pixman_region32_init(&opaque);
3781 pixman_region32_intersect_rect(&opaque, &state->opaque,
3782 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003783
3784 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
3785 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003786 wl_list_for_each(view, &surface->views, surface_link)
3787 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003788 }
3789
3790 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003791
3792 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003793 pixman_region32_intersect_rect(&surface->input, &state->input,
3794 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003795
Pekka Paalanenbc106382012-10-10 12:49:31 +03003796 /* wl_surface.frame */
3797 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05003798 &state->frame_callback_list);
3799 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003800
3801 /* XXX:
3802 * What should happen with a feedback request, if there
3803 * is no wl_buffer attached for this commit?
3804 */
3805
3806 /* presentation.feedback */
3807 wl_list_insert_list(&surface->feedback_list,
3808 &state->feedback_list);
3809 wl_list_init(&state->feedback_list);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003810
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303811 /* weston_protected_surface.enforced/relaxed */
3812 if (surface->protection_mode != state->protection_mode)
3813 weston_surface_set_protection_mode(surface,
3814 state->protection_mode);
3815
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303816 /* weston_protected_surface.set_type */
3817 weston_surface_set_desired_protection(surface, state->desired_protection);
3818
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003819 wl_signal_emit(&surface->commit_signal, surface);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003820}
3821
3822static void
3823weston_surface_commit(struct weston_surface *surface)
3824{
3825 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003826
Pekka Paalanene67858b2013-04-25 13:57:42 +03003827 weston_surface_commit_subsurface_order(surface);
3828
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003829 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003830}
3831
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003832static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003833weston_subsurface_commit(struct weston_subsurface *sub);
3834
3835static void
3836weston_subsurface_parent_commit(struct weston_subsurface *sub,
3837 int parent_is_synchronized);
3838
3839static void
3840surface_commit(struct wl_client *client, struct wl_resource *resource)
3841{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003842 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003843 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
3844
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003845 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
3846 assert(surface->viewport_resource);
3847
3848 wl_resource_post_error(surface->viewport_resource,
3849 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
3850 "wl_surface@%d has viewport source outside buffer",
3851 wl_resource_get_id(resource));
3852 return;
3853 }
3854
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003855 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
3856 assert(surface->viewport_resource);
3857
3858 wl_resource_post_error(surface->viewport_resource,
3859 WP_VIEWPORT_ERROR_BAD_SIZE,
3860 "wl_surface@%d viewport dst size not integer",
3861 wl_resource_get_id(resource));
3862 return;
3863 }
3864
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003865 if (surface->pending.acquire_fence_fd >= 0) {
3866 assert(surface->synchronization_resource);
3867
3868 if (!surface->pending.buffer) {
3869 fd_clear(&surface->pending.acquire_fence_fd);
3870 wl_resource_post_error(surface->synchronization_resource,
3871 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3872 "wl_surface@%"PRIu32" no buffer for synchronization",
3873 wl_resource_get_id(resource));
3874 return;
3875 }
3876
3877 /* We support fences for both wp_linux_dmabuf and opaque EGL
3878 * buffers, as mandated by minor version 2 of the
3879 * zwp_linux_explicit_synchronization_v1 protocol. Since
3880 * renderers that support fences currently only support these
3881 * two buffer types plus SHM buffers, we can just check for the
3882 * SHM buffer case here.
3883 */
3884 if (wl_shm_buffer_get(surface->pending.buffer->resource)) {
3885 fd_clear(&surface->pending.acquire_fence_fd);
3886 wl_resource_post_error(surface->synchronization_resource,
3887 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_UNSUPPORTED_BUFFER,
3888 "wl_surface@%"PRIu32" unsupported buffer for synchronization",
3889 wl_resource_get_id(resource));
3890 return;
3891 }
3892 }
3893
Alexandros Frantzis67629672018-10-19 12:14:11 +03003894 if (surface->pending.buffer_release_ref.buffer_release &&
3895 !surface->pending.buffer) {
3896 assert(surface->synchronization_resource);
3897
3898 wl_resource_post_error(surface->synchronization_resource,
3899 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3900 "wl_surface@%"PRIu32" no buffer for synchronization",
3901 wl_resource_get_id(resource));
3902 return;
3903 }
3904
Pekka Paalanene67858b2013-04-25 13:57:42 +03003905 if (sub) {
3906 weston_subsurface_commit(sub);
3907 return;
3908 }
3909
3910 weston_surface_commit(surface);
3911
3912 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
3913 if (sub->surface != surface)
3914 weston_subsurface_parent_commit(sub, 0);
3915 }
3916}
3917
3918static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003919surface_set_buffer_transform(struct wl_client *client,
3920 struct wl_resource *resource, int transform)
3921{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003922 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003923
Jonny Lamba55f1392014-05-30 12:07:15 +02003924 /* if wl_output.transform grows more members this will need to be updated. */
3925 if (transform < 0 ||
3926 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
3927 wl_resource_post_error(resource,
3928 WL_SURFACE_ERROR_INVALID_TRANSFORM,
3929 "buffer transform must be a valid transform "
3930 "('%d' specified)", transform);
3931 return;
3932 }
3933
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003934 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003935 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003936}
3937
Alexander Larsson4ea95522013-05-22 14:41:37 +02003938static void
3939surface_set_buffer_scale(struct wl_client *client,
3940 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003941 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003942{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003943 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003944
Jonny Lamba55f1392014-05-30 12:07:15 +02003945 if (scale < 1) {
3946 wl_resource_post_error(resource,
3947 WL_SURFACE_ERROR_INVALID_SCALE,
3948 "buffer scale must be at least one "
3949 "('%d' specified)", scale);
3950 return;
3951 }
3952
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003953 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003954 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003955}
3956
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003957static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003958 surface_destroy,
3959 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04003960 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003961 surface_frame,
3962 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003963 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003964 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02003965 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06003966 surface_set_buffer_scale,
3967 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003968};
3969
3970static void
3971compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003972 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003973{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04003974 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003975 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003976
Kristian Høgsberg18c93002012-01-27 11:58:31 -05003977 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003978 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003979 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003980 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003981 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003982
Jason Ekstranda85118c2013-06-27 20:17:02 -05003983 surface->resource =
3984 wl_resource_create(client, &wl_surface_interface,
3985 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003986 if (surface->resource == NULL) {
3987 weston_surface_destroy(surface);
3988 wl_resource_post_no_memory(resource);
3989 return;
3990 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003991 wl_resource_set_implementation(surface->resource, &surface_interface,
3992 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003993
3994 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003995}
3996
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003997static void
3998destroy_region(struct wl_resource *resource)
3999{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05004000 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004001
4002 pixman_region32_fini(&region->region);
4003 free(region);
4004}
4005
4006static void
4007region_destroy(struct wl_client *client, struct wl_resource *resource)
4008{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004009 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004010}
4011
4012static void
4013region_add(struct wl_client *client, struct wl_resource *resource,
4014 int32_t x, int32_t y, int32_t width, int32_t height)
4015{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05004016 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004017
4018 pixman_region32_union_rect(&region->region, &region->region,
4019 x, y, width, height);
4020}
4021
4022static void
4023region_subtract(struct wl_client *client, struct wl_resource *resource,
4024 int32_t x, int32_t y, int32_t width, int32_t height)
4025{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05004026 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004027 pixman_region32_t rect;
4028
4029 pixman_region32_init_rect(&rect, x, y, width, height);
4030 pixman_region32_subtract(&region->region, &region->region, &rect);
4031 pixman_region32_fini(&rect);
4032}
4033
4034static const struct wl_region_interface region_interface = {
4035 region_destroy,
4036 region_add,
4037 region_subtract
4038};
4039
4040static void
4041compositor_create_region(struct wl_client *client,
4042 struct wl_resource *resource, uint32_t id)
4043{
4044 struct weston_region *region;
4045
4046 region = malloc(sizeof *region);
4047 if (region == NULL) {
4048 wl_resource_post_no_memory(resource);
4049 return;
4050 }
4051
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004052 pixman_region32_init(&region->region);
4053
Jason Ekstranda85118c2013-06-27 20:17:02 -05004054 region->resource =
4055 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004056 if (region->resource == NULL) {
4057 free(region);
4058 wl_resource_post_no_memory(resource);
4059 return;
4060 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05004061 wl_resource_set_implementation(region->resource, &region_interface,
4062 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004063}
4064
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04004065static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004066 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05004067 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05004068};
4069
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004070static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03004071weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
4072{
4073 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004074
Jason Ekstrand7b982072014-05-20 14:33:03 -05004075 weston_surface_commit_state(surface, &sub->cached);
4076 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004077
4078 weston_surface_commit_subsurface_order(surface);
4079
4080 weston_surface_schedule_repaint(surface);
4081
Jason Ekstrand7b982072014-05-20 14:33:03 -05004082 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004083}
4084
4085static void
4086weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
4087{
4088 struct weston_surface *surface = sub->surface;
4089
4090 /*
4091 * If this commit would cause the surface to move by the
4092 * attach(dx, dy) parameters, the old damage region must be
4093 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05004094 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03004095 */
Derek Foreman152254b2015-11-26 14:17:48 -06004096 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004097 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06004098 pixman_region32_union(&sub->cached.damage_surface,
4099 &sub->cached.damage_surface,
4100 &surface->pending.damage_surface);
4101 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004102
4103 if (surface->pending.newly_attached) {
4104 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05004105 weston_surface_state_set_buffer(&sub->cached,
4106 surface->pending.buffer);
4107 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004108 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04004109 weston_presentation_feedback_discard_list(
4110 &sub->cached.feedback_list);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03004111 /* zwp_surface_synchronization_v1.set_acquire_fence */
4112 fd_move(&sub->cached.acquire_fence_fd,
4113 &surface->pending.acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03004114 /* zwp_surface_synchronization_v1.get_release */
4115 weston_buffer_release_move(&sub->cached.buffer_release_ref,
4116 &surface->pending.buffer_release_ref);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004117 }
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05304118 sub->cached.desired_protection = surface->pending.desired_protection;
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05304119 sub->cached.protection_mode = surface->pending.protection_mode;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03004120 assert(surface->pending.acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03004121 assert(surface->pending.buffer_release_ref.buffer_release == NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004122 sub->cached.sx += surface->pending.sx;
4123 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02004124
Derek Foreman152254b2015-11-26 14:17:48 -06004125 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
4126
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004127 sub->cached.buffer_viewport.changed |=
4128 surface->pending.buffer_viewport.changed;
4129 sub->cached.buffer_viewport.buffer =
4130 surface->pending.buffer_viewport.buffer;
4131 sub->cached.buffer_viewport.surface =
4132 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004133
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004134 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004135
4136 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
4137
4138 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
4139
4140 wl_list_insert_list(&sub->cached.frame_callback_list,
4141 &surface->pending.frame_callback_list);
4142 wl_list_init(&surface->pending.frame_callback_list);
4143
Pekka Paalanen133e4392014-09-23 22:08:46 -04004144 wl_list_insert_list(&sub->cached.feedback_list,
4145 &surface->pending.feedback_list);
4146 wl_list_init(&surface->pending.feedback_list);
4147
Jason Ekstrand7b982072014-05-20 14:33:03 -05004148 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004149}
4150
Derek Foreman280e7dd2014-10-03 13:13:42 -05004151static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03004152weston_subsurface_is_synchronized(struct weston_subsurface *sub)
4153{
4154 while (sub) {
4155 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05004156 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004157
4158 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05004159 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004160
4161 sub = weston_surface_to_subsurface(sub->parent);
4162 }
4163
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01004164 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004165}
4166
4167static void
4168weston_subsurface_commit(struct weston_subsurface *sub)
4169{
4170 struct weston_surface *surface = sub->surface;
4171 struct weston_subsurface *tmp;
4172
4173 /* Recursive check for effectively synchronized. */
4174 if (weston_subsurface_is_synchronized(sub)) {
4175 weston_subsurface_commit_to_cache(sub);
4176 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05004177 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03004178 /* flush accumulated state from cache */
4179 weston_subsurface_commit_to_cache(sub);
4180 weston_subsurface_commit_from_cache(sub);
4181 } else {
4182 weston_surface_commit(surface);
4183 }
4184
4185 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
4186 if (tmp->surface != surface)
4187 weston_subsurface_parent_commit(tmp, 0);
4188 }
4189 }
4190}
4191
4192static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004193weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004194{
4195 struct weston_surface *surface = sub->surface;
4196 struct weston_subsurface *tmp;
4197
Pekka Paalanene67858b2013-04-25 13:57:42 +03004198 /* From now on, commit_from_cache the whole sub-tree, regardless of
4199 * the synchronized mode of each child. This sub-surface or some
4200 * of its ancestors were synchronized, so we are synchronized
4201 * all the way down.
4202 */
4203
Jason Ekstrand7b982072014-05-20 14:33:03 -05004204 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004205 weston_subsurface_commit_from_cache(sub);
4206
4207 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
4208 if (tmp->surface != surface)
4209 weston_subsurface_parent_commit(tmp, 1);
4210 }
4211}
4212
4213static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004214weston_subsurface_parent_commit(struct weston_subsurface *sub,
4215 int parent_is_synchronized)
4216{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004217 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004218 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004219 wl_list_for_each(view, &sub->surface->views, surface_link)
4220 weston_view_set_position(view,
4221 sub->position.x,
4222 sub->position.y);
4223
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004224 sub->position.set = 0;
4225 }
4226
4227 if (parent_is_synchronized || sub->synchronized)
4228 weston_subsurface_synchronized_commit(sub);
4229}
4230
Pekka Paalanen8274d902014-08-06 19:36:51 +03004231static int
4232subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
4233{
4234 return snprintf(buf, len, "sub-surface");
4235}
4236
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004237static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004238subsurface_committed(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004239{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004240 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004241
Jason Ekstranda7af7042013-10-12 22:38:11 -05004242 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004243 weston_view_set_position(view,
4244 view->geometry.x + dx,
4245 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004246
4247 /* No need to check parent mappedness, because if parent is not
4248 * mapped, parent is not in a visible layer, so this sub-surface
4249 * will not be drawn either.
4250 */
Armin Krezovićf8486c32016-06-30 06:04:28 +02004251
Pekka Paalanene67858b2013-04-25 13:57:42 +03004252 if (!weston_surface_is_mapped(surface)) {
Armin Krezovićf8486c32016-06-30 06:04:28 +02004253 surface->is_mapped = true;
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03004254
Derek Foreman4b1a0a12014-09-10 15:37:33 -05004255 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03004256 * because that would call it also for the parent surface,
4257 * which might not be mapped yet. That would lead to
4258 * inconsistent state, where the window could never be
4259 * mapped.
4260 *
Armin Krezovićf8486c32016-06-30 06:04:28 +02004261 * Instead just force the is_mapped flag on, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03004262 * weston_surface_is_mapped() return true, so that when the
4263 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03004264 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03004265 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004266 }
4267}
4268
4269static struct weston_subsurface *
4270weston_surface_to_subsurface(struct weston_surface *surface)
4271{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004272 if (surface->committed == subsurface_committed)
4273 return surface->committed_private;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004274
4275 return NULL;
4276}
4277
Pekka Paalanen01388e22013-04-25 13:57:44 +03004278WL_EXPORT struct weston_surface *
4279weston_surface_get_main_surface(struct weston_surface *surface)
4280{
4281 struct weston_subsurface *sub;
4282
4283 while (surface && (sub = weston_surface_to_subsurface(surface)))
4284 surface = sub->parent;
4285
4286 return surface;
4287}
4288
Pekka Paalanen50b67472014-10-01 15:02:41 +03004289WL_EXPORT int
4290weston_surface_set_role(struct weston_surface *surface,
4291 const char *role_name,
4292 struct wl_resource *error_resource,
4293 uint32_t error_code)
4294{
4295 assert(role_name);
4296
4297 if (surface->role_name == NULL ||
4298 surface->role_name == role_name ||
4299 strcmp(surface->role_name, role_name) == 0) {
4300 surface->role_name = role_name;
4301
4302 return 0;
4303 }
4304
4305 wl_resource_post_error(error_resource, error_code,
4306 "Cannot assign role %s to wl_surface@%d,"
4307 " already has role %s\n",
4308 role_name,
4309 wl_resource_get_id(surface->resource),
4310 surface->role_name);
4311 return -1;
4312}
4313
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02004314WL_EXPORT const char *
4315weston_surface_get_role(struct weston_surface *surface)
4316{
4317 return surface->role_name;
4318}
4319
Pekka Paalanen8274d902014-08-06 19:36:51 +03004320WL_EXPORT void
4321weston_surface_set_label_func(struct weston_surface *surface,
4322 int (*desc)(struct weston_surface *,
4323 char *, size_t))
4324{
4325 surface->get_label = desc;
Marius Vlad5de92972019-10-15 13:25:41 +03004326 weston_timeline_refresh_subscription_objects(surface->compositor,
4327 surface);
Pekka Paalanen8274d902014-08-06 19:36:51 +03004328}
4329
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004330/** Get the size of surface contents
4331 *
4332 * \param surface The surface to query.
4333 * \param width Returns the width of raw contents.
4334 * \param height Returns the height of raw contents.
4335 *
4336 * Retrieves the raw surface content size in pixels for the given surface.
4337 * This is the whole content size in buffer pixels. If the surface
4338 * has no content or the renderer does not implement this feature,
4339 * zeroes are returned.
4340 *
4341 * This function is used to determine the buffer size needed for
4342 * a weston_surface_copy_content() call.
4343 */
4344WL_EXPORT void
4345weston_surface_get_content_size(struct weston_surface *surface,
4346 int *width, int *height)
4347{
4348 struct weston_renderer *rer = surface->compositor->renderer;
4349
4350 if (!rer->surface_get_content_size) {
4351 *width = 0;
4352 *height = 0;
4353 return;
4354 }
4355
4356 rer->surface_get_content_size(surface, width, height);
4357}
4358
Quentin Glidic248dd102016-08-12 10:41:34 +02004359/** Get the bounding box of a surface and its subsurfaces
4360 *
4361 * \param surface The surface to query.
4362 * \return The bounding box relative to the surface origin.
4363 *
4364 */
4365WL_EXPORT struct weston_geometry
4366weston_surface_get_bounding_box(struct weston_surface *surface)
4367{
4368 pixman_region32_t region;
4369 pixman_box32_t *box;
4370 struct weston_subsurface *subsurface;
4371
4372 pixman_region32_init_rect(&region,
4373 0, 0,
4374 surface->width, surface->height);
4375
4376 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link)
4377 pixman_region32_union_rect(&region, &region,
4378 subsurface->position.x,
4379 subsurface->position.y,
4380 subsurface->surface->width,
4381 subsurface->surface->height);
4382
4383 box = pixman_region32_extents(&region);
4384 struct weston_geometry geometry = {
4385 .x = box->x1,
4386 .y = box->y1,
4387 .width = box->x2 - box->x1,
4388 .height = box->y2 - box->y1,
4389 };
4390
4391 pixman_region32_fini(&region);
4392
4393 return geometry;
4394}
4395
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004396/** Copy surface contents to system memory.
4397 *
4398 * \param surface The surface to copy from.
4399 * \param target Pointer to the target memory buffer.
4400 * \param size Size of the target buffer in bytes.
4401 * \param src_x X location on contents to copy from.
4402 * \param src_y Y location on contents to copy from.
4403 * \param width Width in pixels of the area to copy.
4404 * \param height Height in pixels of the area to copy.
4405 * \return 0 for success, -1 for failure.
4406 *
4407 * Surface contents are maintained by the renderer. They can be in a
4408 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
4409 * else.
4410 *
4411 * Surface contents are copied into memory pointed to by target,
4412 * which has size bytes of space available. The target memory
4413 * may be larger than needed, but being smaller returns an error.
4414 * The extra bytes in target may or may not be written; their content is
4415 * unspecified. Size must be large enough to hold the image.
4416 *
4417 * The image in the target memory will be arranged in rows from
4418 * top to bottom, and pixels on a row from left to right. The pixel
4419 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
4420 * width * 4.
4421 *
4422 * Parameters src_x and src_y define the upper-left corner in buffer
4423 * coordinates (pixels) to copy from. Parameters width and height
4424 * define the size of the area to copy in pixels.
4425 *
4426 * The rectangle defined by src_x, src_y, width, height must fit in
4427 * the surface contents. Otherwise an error is returned.
4428 *
Changwoo Chof97d2502017-08-05 00:30:47 +09004429 * Use weston_surface_get_content_size to determine the content size; the
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004430 * needed target buffer size and rectangle limits.
4431 *
4432 * CURRENT IMPLEMENTATION RESTRICTIONS:
4433 * - the machine must be little-endian due to Pixman formats.
4434 *
4435 * NOTE: Pixman formats are premultiplied.
4436 */
4437WL_EXPORT int
4438weston_surface_copy_content(struct weston_surface *surface,
4439 void *target, size_t size,
4440 int src_x, int src_y,
4441 int width, int height)
4442{
4443 struct weston_renderer *rer = surface->compositor->renderer;
4444 int cw, ch;
4445 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
4446
4447 if (!rer->surface_copy_content)
4448 return -1;
4449
4450 weston_surface_get_content_size(surface, &cw, &ch);
4451
4452 if (src_x < 0 || src_y < 0)
4453 return -1;
4454
4455 if (width <= 0 || height <= 0)
4456 return -1;
4457
4458 if (src_x + width > cw || src_y + height > ch)
4459 return -1;
4460
4461 if (width * bytespp * height > size)
4462 return -1;
4463
4464 return rer->surface_copy_content(surface, target, size,
4465 src_x, src_y, width, height);
4466}
4467
Pekka Paalanene67858b2013-04-25 13:57:42 +03004468static void
4469subsurface_set_position(struct wl_client *client,
4470 struct wl_resource *resource, int32_t x, int32_t y)
4471{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004472 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004473
4474 if (!sub)
4475 return;
4476
4477 sub->position.x = x;
4478 sub->position.y = y;
4479 sub->position.set = 1;
4480}
4481
4482static struct weston_subsurface *
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004483subsurface_find_sibling(struct weston_subsurface *sub,
4484 struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004485{
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004486 struct weston_surface *parent = sub->parent;
4487 struct weston_subsurface *sibling;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004488
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004489 wl_list_for_each(sibling, &parent->subsurface_list, parent_link) {
4490 if (sibling->surface == surface && sibling != sub)
4491 return sibling;
4492 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004493
4494 return NULL;
4495}
4496
4497static struct weston_subsurface *
4498subsurface_sibling_check(struct weston_subsurface *sub,
4499 struct weston_surface *surface,
4500 const char *request)
4501{
4502 struct weston_subsurface *sibling;
4503
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004504 sibling = subsurface_find_sibling(sub, surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004505 if (!sibling) {
4506 wl_resource_post_error(sub->resource,
4507 WL_SUBSURFACE_ERROR_BAD_SURFACE,
4508 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004509 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004510 return NULL;
4511 }
4512
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004513 assert(sibling->parent == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004514
4515 return sibling;
4516}
4517
4518static void
4519subsurface_place_above(struct wl_client *client,
4520 struct wl_resource *resource,
4521 struct wl_resource *sibling_resource)
4522{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004523 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004524 struct weston_surface *surface =
4525 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004526 struct weston_subsurface *sibling;
4527
4528 if (!sub)
4529 return;
4530
4531 sibling = subsurface_sibling_check(sub, surface, "place_above");
4532 if (!sibling)
4533 return;
4534
4535 wl_list_remove(&sub->parent_link_pending);
4536 wl_list_insert(sibling->parent_link_pending.prev,
4537 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004538
4539 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004540}
4541
4542static void
4543subsurface_place_below(struct wl_client *client,
4544 struct wl_resource *resource,
4545 struct wl_resource *sibling_resource)
4546{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004547 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004548 struct weston_surface *surface =
4549 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004550 struct weston_subsurface *sibling;
4551
4552 if (!sub)
4553 return;
4554
4555 sibling = subsurface_sibling_check(sub, surface, "place_below");
4556 if (!sibling)
4557 return;
4558
4559 wl_list_remove(&sub->parent_link_pending);
4560 wl_list_insert(&sibling->parent_link_pending,
4561 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004562
4563 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004564}
4565
4566static void
4567subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
4568{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004569 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004570
4571 if (sub)
4572 sub->synchronized = 1;
4573}
4574
4575static void
4576subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
4577{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004578 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004579
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004580 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03004581 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004582
4583 /* If sub became effectively desynchronized, flush. */
4584 if (!weston_subsurface_is_synchronized(sub))
4585 weston_subsurface_synchronized_commit(sub);
4586 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004587}
4588
4589static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03004590weston_subsurface_unlink_parent(struct weston_subsurface *sub)
4591{
4592 wl_list_remove(&sub->parent_link);
4593 wl_list_remove(&sub->parent_link_pending);
4594 wl_list_remove(&sub->parent_destroy_listener.link);
4595 sub->parent = NULL;
4596}
4597
4598static void
4599weston_subsurface_destroy(struct weston_subsurface *sub);
4600
4601static void
4602subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
4603{
4604 struct weston_subsurface *sub =
4605 container_of(listener, struct weston_subsurface,
4606 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004607 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004608
4609 /* The protocol object (wl_resource) is left inert. */
4610 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004611 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004612
4613 weston_subsurface_destroy(sub);
4614}
4615
4616static void
4617subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
4618{
4619 struct weston_subsurface *sub =
4620 container_of(listener, struct weston_subsurface,
4621 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004622 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004623 assert(sub->surface != sub->parent);
4624
4625 if (weston_surface_is_mapped(sub->surface))
4626 weston_surface_unmap(sub->surface);
4627
4628 weston_subsurface_unlink_parent(sub);
4629}
4630
4631static void
4632subsurface_resource_destroy(struct wl_resource *resource)
4633{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004634 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004635
4636 if (sub)
4637 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004638}
4639
4640static void
4641subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
4642{
4643 wl_resource_destroy(resource);
4644}
4645
4646static void
4647weston_subsurface_link_parent(struct weston_subsurface *sub,
4648 struct weston_surface *parent)
4649{
4650 sub->parent = parent;
4651 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004652 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004653 &sub->parent_destroy_listener);
4654
4655 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4656 wl_list_insert(&parent->subsurface_list_pending,
4657 &sub->parent_link_pending);
4658}
4659
4660static void
4661weston_subsurface_link_surface(struct weston_subsurface *sub,
4662 struct weston_surface *surface)
4663{
4664 sub->surface = surface;
4665 sub->surface_destroy_listener.notify =
4666 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004667 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004668 &sub->surface_destroy_listener);
4669}
4670
4671static void
4672weston_subsurface_destroy(struct weston_subsurface *sub)
4673{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004674 struct weston_view *view, *next;
4675
Pekka Paalanene67858b2013-04-25 13:57:42 +03004676 assert(sub->surface);
4677
4678 if (sub->resource) {
4679 assert(weston_surface_to_subsurface(sub->surface) == sub);
4680 assert(sub->parent_destroy_listener.notify ==
4681 subsurface_handle_parent_destroy);
4682
George Kiagiadakised04d382014-06-13 18:10:26 +02004683 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
4684 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004685 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02004686 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05004687
Pekka Paalanene67858b2013-04-25 13:57:42 +03004688 if (sub->parent)
4689 weston_subsurface_unlink_parent(sub);
4690
Jason Ekstrand7b982072014-05-20 14:33:03 -05004691 weston_surface_state_fini(&sub->cached);
4692 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004693
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004694 sub->surface->committed = NULL;
4695 sub->surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004696 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004697 } else {
4698 /* the dummy weston_subsurface for the parent itself */
4699 assert(sub->parent_destroy_listener.notify == NULL);
4700 wl_list_remove(&sub->parent_link);
4701 wl_list_remove(&sub->parent_link_pending);
4702 }
4703
4704 wl_list_remove(&sub->surface_destroy_listener.link);
4705 free(sub);
4706}
4707
4708static const struct wl_subsurface_interface subsurface_implementation = {
4709 subsurface_destroy,
4710 subsurface_set_position,
4711 subsurface_place_above,
4712 subsurface_place_below,
4713 subsurface_set_sync,
4714 subsurface_set_desync
4715};
4716
4717static struct weston_subsurface *
4718weston_subsurface_create(uint32_t id, struct weston_surface *surface,
4719 struct weston_surface *parent)
4720{
4721 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004722 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004723
Bryce Harringtonde16d892014-11-20 22:21:57 -08004724 sub = zalloc(sizeof *sub);
4725 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004726 return NULL;
4727
Jason Ekstranda7af7042013-10-12 22:38:11 -05004728 wl_list_init(&sub->unused_views);
4729
Jason Ekstranda85118c2013-06-27 20:17:02 -05004730 sub->resource =
4731 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004732 if (!sub->resource) {
4733 free(sub);
4734 return NULL;
4735 }
4736
Jason Ekstranda85118c2013-06-27 20:17:02 -05004737 wl_resource_set_implementation(sub->resource,
4738 &subsurface_implementation,
4739 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004740 weston_subsurface_link_surface(sub, surface);
4741 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05004742 weston_surface_state_init(&sub->cached);
4743 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004744 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004745
4746 return sub;
4747}
4748
4749/* Create a dummy subsurface for having the parent itself in its
4750 * sub-surface lists. Makes stacking order manipulation easy.
4751 */
4752static struct weston_subsurface *
4753weston_subsurface_create_for_parent(struct weston_surface *parent)
4754{
4755 struct weston_subsurface *sub;
4756
Bryce Harringtonde16d892014-11-20 22:21:57 -08004757 sub = zalloc(sizeof *sub);
4758 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004759 return NULL;
4760
4761 weston_subsurface_link_surface(sub, parent);
4762 sub->parent = parent;
4763 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4764 wl_list_insert(&parent->subsurface_list_pending,
4765 &sub->parent_link_pending);
4766
4767 return sub;
4768}
4769
4770static void
4771subcompositor_get_subsurface(struct wl_client *client,
4772 struct wl_resource *resource,
4773 uint32_t id,
4774 struct wl_resource *surface_resource,
4775 struct wl_resource *parent_resource)
4776{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004777 struct weston_surface *surface =
4778 wl_resource_get_user_data(surface_resource);
4779 struct weston_surface *parent =
4780 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004781 struct weston_subsurface *sub;
4782 static const char where[] = "get_subsurface: wl_subsurface@";
4783
4784 if (surface == parent) {
4785 wl_resource_post_error(resource,
4786 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4787 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004788 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004789 return;
4790 }
4791
4792 if (weston_surface_to_subsurface(surface)) {
4793 wl_resource_post_error(resource,
4794 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4795 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004796 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004797 return;
4798 }
4799
Pekka Paalanen50b67472014-10-01 15:02:41 +03004800 if (weston_surface_set_role(surface, "wl_subsurface", resource,
4801 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004802 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004803
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004804 if (weston_surface_get_main_surface(parent) == surface) {
4805 wl_resource_post_error(resource,
4806 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4807 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004808 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004809 return;
4810 }
4811
Pekka Paalanene67858b2013-04-25 13:57:42 +03004812 /* make sure the parent is in its own list */
4813 if (wl_list_empty(&parent->subsurface_list)) {
4814 if (!weston_subsurface_create_for_parent(parent)) {
4815 wl_resource_post_no_memory(resource);
4816 return;
4817 }
4818 }
4819
4820 sub = weston_subsurface_create(id, surface, parent);
4821 if (!sub) {
4822 wl_resource_post_no_memory(resource);
4823 return;
4824 }
4825
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004826 surface->committed = subsurface_committed;
4827 surface->committed_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004828 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004829}
4830
4831static void
4832subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
4833{
4834 wl_resource_destroy(resource);
4835}
4836
4837static const struct wl_subcompositor_interface subcompositor_interface = {
4838 subcompositor_destroy,
4839 subcompositor_get_subsurface
4840};
4841
4842static void
4843bind_subcompositor(struct wl_client *client,
4844 void *data, uint32_t version, uint32_t id)
4845{
4846 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004847 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004848
Jason Ekstranda85118c2013-06-27 20:17:02 -05004849 resource =
4850 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004851 if (resource == NULL) {
4852 wl_client_post_no_memory(client);
4853 return;
4854 }
4855 wl_resource_set_implementation(resource, &subcompositor_interface,
4856 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004857}
4858
Bryce Harrington0795ece2016-08-30 12:04:26 -07004859/** Set a DPMS mode on all of the compositor's outputs
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004860 *
4861 * \param compositor The compositor instance
4862 * \param state The DPMS state the outputs will be set to
4863 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004864static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004865weston_compositor_dpms(struct weston_compositor *compositor,
4866 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004867{
4868 struct weston_output *output;
4869
Bryce Harrington08976ac2016-08-30 12:05:16 -07004870 wl_list_for_each(output, &compositor->output_list, link)
4871 if (output->set_dpms)
4872 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004873}
4874
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004875/** Restores the compositor to active status
4876 *
4877 * \param compositor The compositor instance
4878 *
4879 * If the compositor was in a sleeping mode, all outputs are powered
4880 * back on via DPMS. Otherwise if the compositor was inactive
4881 * (idle/locked, offscreen, or sleeping) then the compositor's wake
4882 * signal will fire.
4883 *
4884 * Restarts the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004885 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004886 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004887WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004888weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004889{
Neil Roberts8b62e202013-09-30 13:14:47 +01004890 uint32_t old_state = compositor->state;
4891
4892 /* The state needs to be changed before emitting the wake
4893 * signal because that may try to schedule a repaint which
4894 * will not work if the compositor is still sleeping */
4895 compositor->state = WESTON_COMPOSITOR_ACTIVE;
4896
4897 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004898 case WESTON_COMPOSITOR_SLEEPING:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004899 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004900 case WESTON_COMPOSITOR_OFFSCREEN:
Daniel Stone893b9362016-11-08 15:47:09 +00004901 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004902 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004903 /* fall through */
4904 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004905 wl_event_source_timer_update(compositor->idle_source,
4906 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004907 }
4908}
4909
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004910/** Turns off rendering and frame events for the compositor.
4911 *
4912 * \param compositor The compositor instance
4913 *
4914 * This is used for example to prevent further rendering while the
4915 * compositor is shutting down.
4916 *
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004917 * Stops the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004918 *
4919 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004920 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004921WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004922weston_compositor_offscreen(struct weston_compositor *compositor)
4923{
4924 switch (compositor->state) {
4925 case WESTON_COMPOSITOR_OFFSCREEN:
4926 return;
4927 case WESTON_COMPOSITOR_SLEEPING:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004928 default:
4929 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4930 wl_event_source_timer_update(compositor->idle_source, 0);
4931 }
4932}
4933
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004934/** Powers down all attached output devices
4935 *
4936 * \param compositor The compositor instance
4937 *
4938 * Causes rendering to the outputs to cease, and no frame events to be
4939 * sent. Only powers down the outputs if the compositor is not already
4940 * in sleep mode.
4941 *
4942 * Stops the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004943 *
4944 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004945 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004946WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004947weston_compositor_sleep(struct weston_compositor *compositor)
4948{
4949 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
4950 return;
4951
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004952 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004953 compositor->state = WESTON_COMPOSITOR_SLEEPING;
4954 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
4955}
4956
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004957/** Sets compositor to idle mode
4958 *
4959 * \param data The compositor instance
4960 *
4961 * This is called when the idle timer fires. Once the compositor is in
4962 * idle mode it requires a wake action (e.g. via
4963 * weston_compositor_wake()) to restore it. The compositor's
4964 * idle_signal will be triggered when the idle event occurs.
4965 *
4966 * Idleness can be inhibited by setting the compositor's idle_inhibit
4967 * property.
4968 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004969static int
4970idle_handler(void *data)
4971{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004972 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004973
4974 if (compositor->idle_inhibit)
4975 return 1;
4976
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004977 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004978 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004979
4980 return 1;
4981}
4982
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004983WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08004984weston_plane_init(struct weston_plane *plane,
4985 struct weston_compositor *ec,
4986 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004987{
4988 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004989 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004990 plane->x = x;
4991 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08004992 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004993
4994 /* Init the link so that the call to wl_list_remove() when releasing
4995 * the plane without ever stacking doesn't lead to a crash */
4996 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004997}
4998
4999WL_EXPORT void
5000weston_plane_release(struct weston_plane *plane)
5001{
Xiong Zhang97116532013-10-23 13:58:31 +08005002 struct weston_view *view;
5003
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005004 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02005005 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03005006
Xiong Zhang97116532013-10-23 13:58:31 +08005007 wl_list_for_each(view, &plane->compositor->view_list, link) {
5008 if (view->plane == plane)
5009 view->plane = NULL;
5010 }
5011
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03005012 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005013}
5014
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005015/** weston_compositor_stack_plane
5016 * \ingroup compositor
5017 */
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02005018WL_EXPORT void
5019weston_compositor_stack_plane(struct weston_compositor *ec,
5020 struct weston_plane *plane,
5021 struct weston_plane *above)
5022{
5023 if (above)
5024 wl_list_insert(above->link.prev, &plane->link);
5025 else
5026 wl_list_insert(&ec->plane_list, &plane->link);
5027}
5028
Quentin Glidic4ef719c2016-07-05 20:44:33 +02005029static void
5030output_release(struct wl_client *client, struct wl_resource *resource)
5031{
5032 wl_resource_destroy(resource);
5033}
5034
5035static const struct wl_output_interface output_interface = {
5036 output_release,
5037};
5038
5039
Casey Dahlin9074db52012-04-19 22:50:09 -04005040static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04005041{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05005042 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04005043}
5044
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005045static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005046bind_output(struct wl_client *client,
5047 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05005048{
Pekka Paalanen05347622017-03-27 12:24:34 +03005049 struct weston_head *head = data;
5050 struct weston_output *output = head->output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005051 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04005052 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05005053
Jason Ekstranda85118c2013-06-27 20:17:02 -05005054 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06005055 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07005056 if (resource == NULL) {
5057 wl_client_post_no_memory(client);
5058 return;
5059 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04005060
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005061 wl_list_insert(&head->resource_list, wl_resource_get_link(resource));
Pekka Paalanen055c1132017-03-27 16:31:25 +03005062 wl_resource_set_implementation(resource, &output_interface, head,
Pekka Paalanen05347622017-03-27 12:24:34 +03005063 unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04005064
Pekka Paalanen05347622017-03-27 12:24:34 +03005065 assert(output);
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05005066 wl_output_send_geometry(resource,
5067 output->x,
5068 output->y,
Pekka Paalanen01f60212017-03-24 15:39:24 +02005069 head->mm_width,
5070 head->mm_height,
5071 head->subpixel,
5072 head->make, head->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04005073 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04005074 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02005075 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02005076 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005077
5078 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05005079 wl_output_send_mode(resource,
5080 mode->flags,
5081 mode->width,
5082 mode->height,
5083 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005084 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02005085
Jasper St. Pierre0013a292014-08-07 16:43:11 -04005086 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02005087 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05005088}
5089
Pekka Paalanendcac3512017-12-08 14:13:34 +02005090static void
5091weston_head_add_global(struct weston_head *head)
5092{
5093 head->global = wl_global_create(head->compositor->wl_display,
5094 &wl_output_interface, 3,
5095 head, bind_output);
5096}
5097
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02005098/** Remove the global wl_output protocol object
5099 *
5100 * \param head The head whose global to remove.
5101 *
5102 * Also orphans the wl_resources for this head (wl_output).
5103 */
5104static void
5105weston_head_remove_global(struct weston_head *head)
5106{
5107 struct wl_resource *resource, *tmp;
5108
5109 if (head->global)
5110 wl_global_destroy(head->global);
5111 head->global = NULL;
5112
5113 wl_resource_for_each_safe(resource, tmp, &head->resource_list) {
5114 unbind_resource(resource);
5115 wl_resource_set_destructor(resource, NULL);
5116 wl_resource_set_user_data(resource, NULL);
5117 }
Roman Gilge97391c2019-03-29 13:01:06 +01005118
5119 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
5120 /* It's sufficient to unset the destructor, then the list elements
5121 * won't be accessed.
5122 */
5123 wl_resource_set_destructor(resource, NULL);
5124 }
5125 wl_list_init(&head->xdg_output_resource_list);
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02005126}
5127
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005128/** Get the backing object of wl_output
5129 *
5130 * \param resource A wl_output protocol object.
5131 * \return The backing object (user data) of a wl_resource representing a
5132 * wl_output protocol object.
Marius Vlad78984ee2019-06-11 00:05:08 +03005133 *
5134 * \ingroup head
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005135 */
Pekka Paalanen055c1132017-03-27 16:31:25 +03005136WL_EXPORT struct weston_head *
5137weston_head_from_resource(struct wl_resource *resource)
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005138{
5139 assert(wl_resource_instance_of(resource, &wl_output_interface,
5140 &output_interface));
5141
5142 return wl_resource_get_user_data(resource);
5143}
5144
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005145/** Initialize a pre-allocated weston_head
5146 *
5147 * \param head The head to initialize.
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005148 * \param name The head name, e.g. the connector name or equivalent.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005149 *
5150 * The head will be safe to attach, detach and release.
5151 *
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005152 * The name is used in logs, and can be used by compositors as a configuration
5153 * identifier.
5154 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005155 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005156 * \internal
5157 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005158WL_EXPORT void
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005159weston_head_init(struct weston_head *head, const char *name)
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005160{
5161 /* Add some (in)sane defaults which can be used
5162 * for checking if an output was properly configured
5163 */
5164 memset(head, 0, sizeof *head);
5165
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005166 wl_list_init(&head->compositor_link);
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005167 wl_signal_init(&head->destroy_signal);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005168 wl_list_init(&head->output_link);
5169 wl_list_init(&head->resource_list);
Roman Gilge97391c2019-03-29 13:01:06 +01005170 wl_list_init(&head->xdg_output_resource_list);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005171 head->name = strdup(name);
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305172 head->current_protection = WESTON_HDCP_DISABLE;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005173}
5174
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005175/** Send output heads changed signal
5176 *
5177 * \param output The output that changed.
5178 *
5179 * Notify that the enabled output gained and/or lost heads, or that the
5180 * associated heads may have changed their connection status. This does not
5181 * include cases where the output becomes enabled or disabled. The registered
5182 * callbacks are called after the change has successfully happened.
5183 *
5184 * If connection status change causes the compositor to attach or detach a head
5185 * to an enabled output, the registered callbacks may be called multiple times.
Marius Vlad55d87362019-06-11 01:15:35 +03005186 *
5187 * \ingroup output
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005188 */
5189static void
5190weston_output_emit_heads_changed(struct weston_output *output)
5191{
5192 wl_signal_emit(&output->compositor->output_heads_changed_signal,
5193 output);
5194}
5195
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005196/** Idle task for emitting heads_changed_signal */
5197static void
5198weston_compositor_call_heads_changed(void *data)
5199{
5200 struct weston_compositor *compositor = data;
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005201 struct weston_head *head;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005202
5203 compositor->heads_changed_source = NULL;
5204
5205 wl_signal_emit(&compositor->heads_changed_signal, compositor);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005206
5207 wl_list_for_each(head, &compositor->head_list, compositor_link) {
5208 if (head->output && head->output->enabled)
5209 weston_output_emit_heads_changed(head->output);
5210 }
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005211}
5212
5213/** Schedule a call on idle to heads_changed callback
5214 *
5215 * \param compositor The Compositor.
5216 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005217 * \ingroup compositor
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005218 * \internal
5219 */
5220static void
5221weston_compositor_schedule_heads_changed(struct weston_compositor *compositor)
5222{
5223 struct wl_event_loop *loop;
5224
5225 if (compositor->heads_changed_source)
5226 return;
5227
5228 loop = wl_display_get_event_loop(compositor->wl_display);
5229 compositor->heads_changed_source = wl_event_loop_add_idle(loop,
5230 weston_compositor_call_heads_changed, compositor);
5231}
5232
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005233/** Register a new head
5234 *
5235 * \param compositor The compositor.
5236 * \param head The head to register, must not be already registered.
5237 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005238 * This signals the core that a new head has become available, leading to
5239 * heads_changed hook being called later.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005240 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005241 * \ingroup compositor
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005242 * \internal
5243 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005244WL_EXPORT void
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005245weston_compositor_add_head(struct weston_compositor *compositor,
5246 struct weston_head *head)
5247{
5248 assert(wl_list_empty(&head->compositor_link));
5249 assert(head->name);
5250
5251 wl_list_insert(compositor->head_list.prev, &head->compositor_link);
5252 head->compositor = compositor;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005253 weston_compositor_schedule_heads_changed(compositor);
5254}
5255
5256/** Adds a listener to be called when heads change
5257 *
5258 * \param compositor The compositor.
5259 * \param listener The listener to add.
5260 *
Marius Vlada2dace22019-06-12 16:05:44 +03005261 * The listener notify function argument is weston_compositor.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005262 *
5263 * The listener function will be called after heads are added or their
5264 * connection status has changed. Several changes may be accumulated into a
5265 * single call. The user is expected to iterate over the existing heads and
5266 * check their statuses to find out what changed.
5267 *
5268 * \sa weston_compositor_iterate_heads, weston_head_is_connected,
5269 * weston_head_is_enabled
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005270 * \ingroup compositor
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005271 */
5272WL_EXPORT void
5273weston_compositor_add_heads_changed_listener(struct weston_compositor *compositor,
5274 struct wl_listener *listener)
5275{
5276 wl_signal_add(&compositor->heads_changed_signal, listener);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005277}
5278
5279/** Iterate over available heads
5280 *
5281 * \param compositor The compositor.
Marius Vlada2dace22019-06-12 16:05:44 +03005282 * \param iter The iterator, or NULL for start.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005283 * \return The next available head in the list.
5284 *
5285 * Returns all available heads, regardless of being connected or enabled.
5286 *
5287 * You can iterate over all heads as follows:
5288 * \code
5289 * struct weston_head *head = NULL;
5290 *
5291 * while ((head = weston_compositor_iterate_heads(compositor, head))) {
5292 * ...
5293 * }
5294 * \endcode
5295 *
5296 * If you cause \c iter to be removed from the list, you cannot use it to
5297 * continue iterating. Removing any other item is safe.
5298 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005299 * \ingroup compositor
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005300 */
5301WL_EXPORT struct weston_head *
5302weston_compositor_iterate_heads(struct weston_compositor *compositor,
5303 struct weston_head *iter)
5304{
5305 struct wl_list *list = &compositor->head_list;
5306 struct wl_list *node;
5307
5308 assert(compositor);
5309 assert(!iter || iter->compositor == compositor);
5310
5311 if (iter)
5312 node = iter->compositor_link.next;
5313 else
5314 node = list->next;
5315
5316 assert(node);
5317 assert(!iter || node != &iter->compositor_link);
5318
5319 if (node == list)
5320 return NULL;
5321
5322 return container_of(node, struct weston_head, compositor_link);
5323}
5324
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005325/** Iterate over attached heads
5326 *
5327 * \param output The output whose heads to iterate.
Marius Vlada2dace22019-06-12 16:05:44 +03005328 * \param iter The iterator, or NULL for start.
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005329 * \return The next attached head in the list.
5330 *
5331 * Returns all heads currently attached to the output.
5332 *
5333 * You can iterate over all heads as follows:
5334 * \code
5335 * struct weston_head *head = NULL;
5336 *
5337 * while ((head = weston_output_iterate_heads(output, head))) {
5338 * ...
5339 * }
5340 * \endcode
5341 *
5342 * If you cause \c iter to be removed from the list, you cannot use it to
5343 * continue iterating. Removing any other item is safe.
5344 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05005345 * \ingroup output
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005346 */
5347WL_EXPORT struct weston_head *
5348weston_output_iterate_heads(struct weston_output *output,
5349 struct weston_head *iter)
5350{
5351 struct wl_list *list = &output->head_list;
5352 struct wl_list *node;
5353
5354 assert(output);
5355 assert(!iter || iter->output == output);
5356
5357 if (iter)
5358 node = iter->output_link.next;
5359 else
5360 node = list->next;
5361
5362 assert(node);
5363 assert(!iter || node != &iter->output_link);
5364
5365 if (node == list)
5366 return NULL;
5367
5368 return container_of(node, struct weston_head, output_link);
5369}
5370
Pekka Paalanendcac3512017-12-08 14:13:34 +02005371/** Attach a head to an output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005372 *
5373 * \param output The output to attach to.
5374 * \param head The head that is not yet attached.
5375 * \return 0 on success, -1 on failure.
5376 *
5377 * Attaches the given head to the output. All heads of an output are clones
5378 * and share the resolution and timings.
5379 *
5380 * Cloning heads this way uses less resources than creating an output for
5381 * each head, but is not always possible due to environment, driver and hardware
5382 * limitations.
5383 *
5384 * On failure, the head remains unattached. Success of this function does not
5385 * guarantee the output configuration is actually valid. The final checks are
Pekka Paalanendcac3512017-12-08 14:13:34 +02005386 * made on weston_output_enable() unless the output was already enabled.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005387 *
Marius Vlad55d87362019-06-11 01:15:35 +03005388 * \ingroup output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005389 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005390WL_EXPORT int
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005391weston_output_attach_head(struct weston_output *output,
5392 struct weston_head *head)
5393{
Pekka Paalanendcac3512017-12-08 14:13:34 +02005394 char *head_names;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005395
5396 if (!wl_list_empty(&head->output_link))
5397 return -1;
5398
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005399 if (output->attach_head) {
5400 if (output->attach_head(output, head) < 0)
5401 return -1;
5402 } else if (!wl_list_empty(&output->head_list)) {
5403 /* No support for clones in the legacy path. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005404 return -1;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005405 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005406
5407 head->output = output;
5408 wl_list_insert(output->head_list.prev, &head->output_link);
5409
Pekka Paalanendcac3512017-12-08 14:13:34 +02005410 if (output->enabled) {
5411 weston_head_add_global(head);
5412
5413 head_names = weston_output_create_heads_string(output);
5414 weston_log("Output '%s' updated to have head(s) %s\n",
5415 output->name, head_names);
5416 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005417
5418 weston_output_emit_heads_changed(output);
Pekka Paalanendcac3512017-12-08 14:13:34 +02005419 }
5420
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005421 return 0;
5422}
5423
5424/** Detach a head from its output
5425 *
5426 * \param head The head to detach.
5427 *
5428 * It is safe to detach a non-attached head.
5429 *
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005430 * If the head is attached to an enabled output and the output will be left
5431 * with no heads, the output will be disabled.
5432 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005433 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005434 * \sa weston_output_disable
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005435 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005436WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005437weston_head_detach(struct weston_head *head)
5438{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005439 struct weston_output *output = head->output;
Pekka Paalanena0106992017-12-08 16:11:17 +02005440 char *head_names;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005441
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005442 wl_list_remove(&head->output_link);
5443 wl_list_init(&head->output_link);
5444 head->output = NULL;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005445
5446 if (!output)
5447 return;
5448
5449 if (output->detach_head)
5450 output->detach_head(output, head);
5451
5452 if (output->enabled) {
5453 weston_head_remove_global(head);
5454
Pekka Paalanena0106992017-12-08 16:11:17 +02005455 if (wl_list_empty(&output->head_list)) {
5456 weston_log("Output '%s' no heads left, disabling.\n",
5457 output->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005458 weston_output_disable(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02005459 } else {
5460 head_names = weston_output_create_heads_string(output);
5461 weston_log("Output '%s' updated to have head(s) %s\n",
5462 output->name, head_names);
5463 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005464
5465 weston_output_emit_heads_changed(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02005466 }
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005467 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005468}
5469
5470/** Destroy a head
5471 *
5472 * \param head The head to be released.
5473 *
5474 * Destroys the head. The caller is responsible for freeing the memory pointed
5475 * to by \c head.
5476 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005477 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005478 * \internal
5479 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005480WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005481weston_head_release(struct weston_head *head)
5482{
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005483 wl_signal_emit(&head->destroy_signal, head);
5484
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005485 weston_head_detach(head);
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005486
5487 free(head->make);
5488 free(head->model);
5489 free(head->serial_number);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005490 free(head->name);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005491
5492 wl_list_remove(&head->compositor_link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005493}
5494
Pekka Paalanene19970f2017-08-28 14:11:02 +03005495static void
5496weston_head_set_device_changed(struct weston_head *head)
5497{
5498 head->device_changed = true;
5499
5500 if (head->compositor)
5501 weston_compositor_schedule_heads_changed(head->compositor);
5502}
5503
5504/** String equal comparison with NULLs being equal */
5505static bool
5506str_null_eq(const char *a, const char *b)
5507{
5508 if (!a && !b)
5509 return true;
5510
5511 if (!!a != !!b)
5512 return false;
5513
5514 return strcmp(a, b) == 0;
5515}
5516
Pekka Paalanen01f60212017-03-24 15:39:24 +02005517/** Store monitor make, model and serial number
5518 *
5519 * \param head The head to modify.
5520 * \param make The monitor make. If EDID is available, the PNP ID. Otherwise
5521 * any string, or NULL for none.
5522 * \param model The monitor model or name, or a made-up string, or NULL for
5523 * none.
5524 * \param serialno The monitor serial number, a made-up string, or NULL for
5525 * none.
5526 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005527 * This may set the device_changed flag.
5528 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005529 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005530 * \internal
5531 */
5532WL_EXPORT void
5533weston_head_set_monitor_strings(struct weston_head *head,
5534 const char *make,
5535 const char *model,
5536 const char *serialno)
5537{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005538 if (str_null_eq(head->make, make) &&
5539 str_null_eq(head->model, model) &&
5540 str_null_eq(head->serial_number, serialno))
5541 return;
5542
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005543 free(head->make);
5544 free(head->model);
5545 free(head->serial_number);
5546
5547 head->make = make ? strdup(make) : NULL;
5548 head->model = model ? strdup(model) : NULL;
5549 head->serial_number = serialno ? strdup(serialno) : NULL;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005550
5551 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005552}
5553
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005554/** Store display non-desktop status
5555 *
5556 * \param head The head to modify.
5557 * \param non_desktop Whether the head connects to a non-desktop display.
5558 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005559 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005560 * \internal
5561 */
5562WL_EXPORT void
5563weston_head_set_non_desktop(struct weston_head *head, bool non_desktop)
5564{
5565 if (head->non_desktop == non_desktop)
5566 return;
5567
5568 head->non_desktop = non_desktop;
5569
5570 weston_head_set_device_changed(head);
5571}
5572
Lucas Stacha69cb712019-11-25 23:29:31 +00005573/** Store display transformation
5574 *
5575 * \param head The head to modify.
5576 * \param transform The transformation to apply for this head
5577 *
5578 * This may set the device_changed flag.
5579 *
5580 * \ingroup head
5581 * \internal
5582 */
5583WL_EXPORT void
5584weston_head_set_transform(struct weston_head *head, uint32_t transform)
5585{
5586 if (head->transform == transform)
5587 return;
5588
5589 head->transform = transform;
5590
5591 weston_head_set_device_changed(head);
5592}
5593
5594
Pekka Paalanen01f60212017-03-24 15:39:24 +02005595/** Store physical image size
5596 *
5597 * \param head The head to modify.
5598 * \param mm_width Image area width in millimeters.
5599 * \param mm_height Image area height in millimeters.
5600 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005601 * This may set the device_changed flag.
5602 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005603 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005604 * \internal
5605 */
5606WL_EXPORT void
5607weston_head_set_physical_size(struct weston_head *head,
5608 int32_t mm_width, int32_t mm_height)
5609{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005610 if (head->mm_width == mm_width &&
5611 head->mm_height == mm_height)
5612 return;
5613
Pekka Paalanen01f60212017-03-24 15:39:24 +02005614 head->mm_width = mm_width;
5615 head->mm_height = mm_height;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005616
5617 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005618}
5619
5620/** Store monitor sub-pixel layout
5621 *
5622 * \param head The head to modify.
5623 * \param sp Sub-pixel layout. The possible values are:
5624 * - WL_OUTPUT_SUBPIXEL_UNKNOWN,
5625 * - WL_OUTPUT_SUBPIXEL_NONE,
5626 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB,
5627 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR,
5628 * - WL_OUTPUT_SUBPIXEL_VERTICAL_RGB,
5629 * - WL_OUTPUT_SUBPIXEL_VERTICAL_BGR
5630 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005631 * This may set the device_changed flag.
5632 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005633 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005634 * \internal
5635 */
5636WL_EXPORT void
5637weston_head_set_subpixel(struct weston_head *head,
5638 enum wl_output_subpixel sp)
5639{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005640 if (head->subpixel == sp)
5641 return;
5642
Pekka Paalanen01f60212017-03-24 15:39:24 +02005643 head->subpixel = sp;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005644
5645 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005646}
5647
5648/** Mark the monitor as internal
5649 *
5650 * This is used for embedded screens, like laptop panels.
5651 *
5652 * \param head The head to mark as internal.
5653 *
5654 * By default a head is external. The type is often inferred from the physical
5655 * connector type.
5656 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005657 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005658 * \internal
5659 */
5660WL_EXPORT void
5661weston_head_set_internal(struct weston_head *head)
5662{
5663 head->connection_internal = true;
5664}
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005665
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005666/** Store connector status
5667 *
5668 * \param head The head to modify.
5669 * \param connected Whether the head is connected.
5670 *
5671 * Connectors are created as disconnected. This function can be used to
5672 * set the connector status.
5673 *
5674 * The status should be set to true when a physical connector is connected to
5675 * a video sink device like a monitor and to false when the connector is
5676 * disconnected. For nested backends, the connection status should reflect the
5677 * connection to the parent display server.
5678 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005679 * When the connection status changes, it schedules a call to the heads_changed
Pekka Paalanene19970f2017-08-28 14:11:02 +03005680 * hook and sets the device_changed flag.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005681 *
5682 * \sa weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005683 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005684 * \internal
5685 */
5686WL_EXPORT void
5687weston_head_set_connection_status(struct weston_head *head, bool connected)
5688{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005689 if (head->connected == connected)
5690 return;
5691
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005692 head->connected = connected;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005693
Pekka Paalanene19970f2017-08-28 14:11:02 +03005694 weston_head_set_device_changed(head);
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005695}
5696
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305697static void
5698weston_output_compute_protection(struct weston_output *output)
5699{
5700 struct weston_head *head;
5701 enum weston_hdcp_protection op_protection;
5702 bool op_protection_valid = false;
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305703 struct weston_compositor *wc = output->compositor;
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305704
5705 wl_list_for_each(head, &output->head_list, output_link) {
5706 if (!op_protection_valid) {
5707 op_protection = head->current_protection;
5708 op_protection_valid = true;
5709 }
5710 if (head->current_protection < op_protection)
5711 op_protection = head->current_protection;
5712 }
5713
5714 if (!op_protection_valid)
5715 op_protection = WESTON_HDCP_DISABLE;
5716
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305717 if (output->current_protection != op_protection) {
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305718 output->current_protection = op_protection;
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305719 weston_output_damage(output);
Ankit Nautiyalfaa5ab42019-07-08 13:23:24 +05305720 weston_schedule_surface_protection_update(wc);
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305721 }
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305722}
5723
5724WL_EXPORT void
5725weston_head_set_content_protection_status(struct weston_head *head,
5726 enum weston_hdcp_protection status)
5727{
5728 head->current_protection = status;
5729 if (head->output)
5730 weston_output_compute_protection(head->output);
5731}
5732
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005733/** Is the head currently connected?
5734 *
5735 * \param head The head to query.
5736 * \return Connection status.
5737 *
5738 * Returns true if the head is physically connected to a monitor, or in
5739 * case of a nested backend returns true when there is a connection to the
5740 * parent display server.
5741 *
5742 * This is independent from the head being enabled.
5743 *
5744 * \sa weston_head_is_enabled
Marius Vlad78984ee2019-06-11 00:05:08 +03005745 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005746 */
5747WL_EXPORT bool
5748weston_head_is_connected(struct weston_head *head)
5749{
5750 return head->connected;
5751}
5752
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005753/** Is the head currently enabled?
5754 *
5755 * \param head The head to query.
5756 * \return Video status.
5757 *
5758 * Returns true if the head is currently transmitting a video stream.
5759 *
5760 * This is independent of the head being connected.
5761 *
5762 * \sa weston_head_is_connected
Marius Vlad78984ee2019-06-11 00:05:08 +03005763 * \ingroup head
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005764 */
5765WL_EXPORT bool
5766weston_head_is_enabled(struct weston_head *head)
5767{
5768 if (!head->output)
5769 return false;
5770
5771 return head->output->enabled;
5772}
5773
Pekka Paalanene19970f2017-08-28 14:11:02 +03005774/** Has the device information changed?
5775 *
5776 * \param head The head to query.
5777 * \return True if the device information has changed since last reset.
5778 *
5779 * The information about the connected display device, e.g. a monitor, may
5780 * change without being disconnected in between. Changing information
5781 * causes a call to the heads_changed hook.
5782 *
5783 * The information includes make, model, serial number, physical size,
5784 * and sub-pixel type. The connection status is also included.
5785 *
5786 * \sa weston_head_reset_device_changed, weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005787 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03005788 */
5789WL_EXPORT bool
5790weston_head_is_device_changed(struct weston_head *head)
5791{
5792 return head->device_changed;
5793}
5794
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005795/** Does the head represent a non-desktop display?
5796 *
5797 * \param head The head to query.
5798 * \return True if the device is a non-desktop display.
5799 *
5800 * Non-desktop heads are not attached to outputs by default.
5801 * This stops weston from extending the desktop onto head mounted displays.
5802 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005803 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005804 */
5805WL_EXPORT bool
5806weston_head_is_non_desktop(struct weston_head *head)
5807{
5808 return head->non_desktop;
5809}
5810
Pekka Paalanene19970f2017-08-28 14:11:02 +03005811/** Acknowledge device information change
5812 *
5813 * \param head The head to acknowledge.
5814 *
5815 * Clears the device changed flag on this head. When a compositor has processed
5816 * device information, it should call this to be able to notice further
5817 * changes.
5818 *
5819 * \sa weston_head_is_device_changed
Marius Vlad78984ee2019-06-11 00:05:08 +03005820 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03005821 */
5822WL_EXPORT void
5823weston_head_reset_device_changed(struct weston_head *head)
5824{
5825 head->device_changed = false;
5826}
5827
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005828/** Get the name of a head
5829 *
5830 * \param head The head to query.
5831 * \return The head's name, not NULL.
5832 *
5833 * The name depends on the backend. The DRM backend uses connector names,
5834 * other backends may use hardcoded names or user-given names.
Marius Vlad78984ee2019-06-11 00:05:08 +03005835 *
5836 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005837 */
5838WL_EXPORT const char *
5839weston_head_get_name(struct weston_head *head)
5840{
5841 return head->name;
5842}
5843
5844/** Get the output the head is attached to
5845 *
5846 * \param head The head to query.
5847 * \return The output the head is attached to, or NULL if detached.
Marius Vlad78984ee2019-06-11 00:05:08 +03005848 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005849 */
5850WL_EXPORT struct weston_output *
5851weston_head_get_output(struct weston_head *head)
5852{
5853 return head->output;
5854}
5855
Lucas Stacha69cb712019-11-25 23:29:31 +00005856/** Get the head's native transformation
5857 *
5858 * \param head The head to query.
5859 * \return The head's native transform, as a WL_OUTPUT_TRANSFORM_* value
5860 *
5861 * A weston_head may have a 'native' transform provided by the backend.
5862 * Examples include panels which are physically rotated, where the rotation
5863 * is recorded and described as part of the system configuration. This call
5864 * will return any known native transform for the head.
5865 *
5866 * \ingroup head
5867 */
5868WL_EXPORT uint32_t
5869weston_head_get_transform(struct weston_head *head)
5870{
5871 return head->transform;
5872}
5873
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005874/** Add destroy callback for a head
5875 *
5876 * \param head The head to watch for.
5877 * \param listener The listener to add. The \c notify member must be set.
5878 *
5879 * Heads may get destroyed for various reasons by the backends. If a head is
5880 * attached to an output, the compositor should listen for head destruction
5881 * and reconfigure or destroy the output if necessary.
5882 *
5883 * The destroy callbacks will be called on weston_head destruction before any
5884 * automatic detaching from an associated weston_output and before any
5885 * weston_head information is lost.
5886 *
5887 * The \c data argument to the notify callback is the weston_head being
5888 * destroyed.
Marius Vlad78984ee2019-06-11 00:05:08 +03005889 *
5890 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005891 */
5892WL_EXPORT void
5893weston_head_add_destroy_listener(struct weston_head *head,
5894 struct wl_listener *listener)
5895{
5896 wl_signal_add(&head->destroy_signal, listener);
5897}
5898
5899/** Look up destroy listener for a head
5900 *
5901 * \param head The head to query.
5902 * \param notify The notify function used used for the added destroy listener.
5903 * \return The listener, or NULL if not found.
5904 *
5905 * This looks up the previously added destroy listener struct based on the
5906 * notify function it has. The listener can be used to access user data
5907 * through \c container_of().
5908 *
5909 * \sa wl_signal_get()
Marius Vlad78984ee2019-06-11 00:05:08 +03005910 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005911 */
5912WL_EXPORT struct wl_listener *
5913weston_head_get_destroy_listener(struct weston_head *head,
5914 wl_notify_func_t notify)
5915{
5916 return wl_signal_get(&head->destroy_signal, notify);
5917}
5918
David Fort0de859e2016-05-27 23:22:57 +02005919/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005920static void
David Fort0de859e2016-05-27 23:22:57 +02005921weston_compositor_reflow_outputs(struct weston_compositor *compositor,
5922 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005923{
5924 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02005925 bool start_resizing = false;
5926
5927 if (!delta_width)
5928 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005929
5930 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02005931 if (output == resized_output) {
5932 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005933 continue;
5934 }
5935
David Fort0de859e2016-05-27 23:22:57 +02005936 if (start_resizing) {
5937 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005938 output->dirty = 1;
5939 }
5940 }
5941}
5942
Pekka Paalanen06920792020-12-08 15:22:36 +02005943/** Transform a region in-place from global to output coordinates
5944 *
5945 * \param output The output that defines the transformation.
5946 * \param region The region to be transformed in-place.
5947 *
5948 * This takes a region in the global coordinate system, and takes into account
5949 * output position, transform and scale, and the zoom, and converts the region
5950 * into output pixel coordinates in the framebuffer.
5951 *
5952 * Uses floating-point operations if zoom is active, which may round to expand
5953 * the region.
5954 *
5955 * \internal
5956 * \ingroup output
5957 */
5958WL_EXPORT void
5959weston_output_region_from_global(struct weston_output *output,
5960 pixman_region32_t *region)
5961{
5962 if (output->zoom.active) {
5963 weston_matrix_transform_region(region, &output->matrix, region);
5964 } else {
5965 pixman_region32_translate(region, -output->x, -output->y);
5966 weston_transformed_region(output->width, output->height,
5967 output->transform,
5968 output->current_scale,
5969 region, region);
5970 }
5971}
5972
Pekka Paalanend72bad22017-03-29 17:01:41 +03005973static void
Scott Moreauccbf29d2012-02-22 14:21:41 -07005974weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005975{
Scott Moreau850ca422012-05-21 15:21:25 -06005976 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05005977
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005978 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005979 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06005980
Scott Moreauccbf29d2012-02-22 14:21:41 -07005981 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06005982 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005983 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005984 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005985 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005986 weston_matrix_scale(&output->matrix, magnification,
5987 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005988 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005989
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005990 switch (output->transform) {
5991 case WL_OUTPUT_TRANSFORM_FLIPPED:
5992 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5993 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5994 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5995 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5996 weston_matrix_scale(&output->matrix, -1, 1, 1);
5997 break;
5998 }
5999
6000 switch (output->transform) {
6001 default:
6002 case WL_OUTPUT_TRANSFORM_NORMAL:
6003 case WL_OUTPUT_TRANSFORM_FLIPPED:
6004 break;
6005 case WL_OUTPUT_TRANSFORM_90:
6006 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +02006007 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
6008 weston_matrix_rotate_xy(&output->matrix, 0, -1);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05006009 break;
6010 case WL_OUTPUT_TRANSFORM_180:
6011 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
6012 weston_matrix_translate(&output->matrix,
6013 -output->width, -output->height, 0);
6014 weston_matrix_rotate_xy(&output->matrix, -1, 0);
6015 break;
6016 case WL_OUTPUT_TRANSFORM_270:
6017 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +02006018 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
6019 weston_matrix_rotate_xy(&output->matrix, 0, 1);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05006020 break;
6021 }
6022
6023 if (output->current_scale != 1)
6024 weston_matrix_scale(&output->matrix,
6025 output->current_scale,
6026 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01006027
Scott Moreauccbf29d2012-02-22 14:21:41 -07006028 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05006029
6030 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07006031}
6032
Scott Moreau1bad5db2012-08-18 01:04:05 -06006033static void
Alexander Larsson0b135062013-05-28 16:23:36 +02006034weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06006035{
6036 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03006037 output->native_scale = scale;
6038 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06006039
Pekka Paalanen59987fa2016-04-26 15:50:59 +03006040 convert_size_by_transform_scale(&output->width, &output->height,
6041 output->current_mode->width,
6042 output->current_mode->height,
6043 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02006044}
6045
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006046static void
6047weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07006048{
6049 output->x = x;
6050 output->y = y;
6051
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006052 pixman_region32_fini(&output->region);
Scott Moreauccbf29d2012-02-22 14:21:41 -07006053 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06006054 output->width,
6055 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01006056}
6057
Marius Vlad55d87362019-06-11 01:15:35 +03006058/**
6059 * \ingroup output
6060 */
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006061WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006062weston_output_move(struct weston_output *output, int x, int y)
6063{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006064 struct weston_head *head;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006065 struct wl_resource *resource;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006066 int ver;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006067
6068 output->move_x = x - output->x;
6069 output->move_y = y - output->y;
6070
6071 if (output->move_x == 0 && output->move_y == 0)
6072 return;
6073
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006074 weston_output_init_geometry(output, x, y);
6075
6076 output->dirty = 1;
6077
6078 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006079 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006080
6081 /* Notify clients of the change for output position. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006082 wl_list_for_each(head, &output->head_list, output_link) {
6083 wl_resource_for_each(resource, &head->resource_list) {
6084 wl_output_send_geometry(resource,
6085 output->x,
6086 output->y,
6087 head->mm_width,
6088 head->mm_height,
6089 head->subpixel,
6090 head->make,
6091 head->model,
6092 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08006093
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006094 ver = wl_resource_get_version(resource);
6095 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
6096 wl_output_send_done(resource);
6097 }
Roman Gilge97391c2019-03-29 13:01:06 +01006098
6099 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
6100 zxdg_output_v1_send_logical_position(resource,
6101 output->x,
6102 output->y);
6103 zxdg_output_v1_send_done(resource);
6104 }
Quanxian Wangb2c86362014-03-14 09:16:25 +08006105 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006106}
6107
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006108/** Signal that a pending output is taken into use.
6109 *
6110 * Removes the output from the pending list and adds it to the compositor's
6111 * list of enabled outputs. The output created signal is emitted.
Giulio Camuffob1147152015-05-06 21:41:57 +03006112 *
Pekka Paalanen2210ad02017-03-30 15:48:06 +03006113 * The output gets an internal ID assigned, and the wl_output global is
6114 * created.
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03006115 *
Giulio Camuffob1147152015-05-06 21:41:57 +03006116 * \param compositor The compositor instance.
6117 * \param output The output to be added.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006118 *
6119 * \internal
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006120 * \ingroup compositor
Giulio Camuffob1147152015-05-06 21:41:57 +03006121 */
Pekka Paalanenf9681b52017-03-29 16:58:48 +03006122static void
Giulio Camuffob1147152015-05-06 21:41:57 +03006123weston_compositor_add_output(struct weston_compositor *compositor,
6124 struct weston_output *output)
6125{
Armin Krezoviće5403842016-08-05 15:28:29 +02006126 struct weston_view *view, *next;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03006127 struct weston_head *head;
Armin Krezoviće5403842016-08-05 15:28:29 +02006128
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006129 assert(!output->enabled);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03006130
6131 /* Verify we haven't reached the limit of 32 available output IDs */
6132 assert(ffs(~compositor->output_id_pool) > 0);
6133
6134 /* Invert the output id pool and look for the lowest numbered
6135 * switch (the least significant bit). Take that bit's position
6136 * as our ID, and mark it used in the compositor's output_id_pool.
6137 */
6138 output->id = ffs(~compositor->output_id_pool) - 1;
6139 compositor->output_id_pool |= 1u << output->id;
6140
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006141 wl_list_remove(&output->link);
Giulio Camuffob1147152015-05-06 21:41:57 +03006142 wl_list_insert(compositor->output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006143 output->enabled = true;
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006144
Pekka Paalanendcac3512017-12-08 14:13:34 +02006145 wl_list_for_each(head, &output->head_list, output_link)
6146 weston_head_add_global(head);
Pekka Paalanen2210ad02017-03-30 15:48:06 +03006147
Giulio Camuffob1147152015-05-06 21:41:57 +03006148 wl_signal_emit(&compositor->output_created_signal, output);
Armin Krezoviće5403842016-08-05 15:28:29 +02006149
6150 wl_list_for_each_safe(view, next, &compositor->view_list, link)
6151 weston_view_geometry_dirty(view);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04006152}
6153
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006154/** Transform device coordinates into global coordinates
6155 *
Marius Vlada2dace22019-06-12 16:05:44 +03006156 * \param output the weston_output object
6157 * \param[in] device_x X coordinate in device units.
6158 * \param[in] device_y Y coordinate in device units.
6159 * \param[out] x X coordinate in the global space.
6160 * \param[out] y Y coordinate in the global space.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006161 *
Marius Vlada2dace22019-06-12 16:05:44 +03006162 * Transforms coordinates from the device coordinate space (physical pixel
6163 * units) to the global coordinate space (logical pixel units). This takes
6164 * into account output transform and scale.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006165 *
Marius Vlad55d87362019-06-11 01:15:35 +03006166 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006167 * \internal
6168 */
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006169WL_EXPORT void
6170weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02006171 double device_x, double device_y,
6172 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006173{
Derek Foreman0f679412014-10-02 13:41:17 -05006174 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02006175 device_x,
6176 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05006177 0.0,
6178 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006179
Derek Foreman67a18b92015-03-24 11:36:14 -05006180 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006181
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02006182 *x = p.f[0] / p.f[3];
6183 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006184}
6185
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006186/** Removes output from compositor's list of enabled outputs
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006187 *
6188 * \param output The weston_output object that is being removed.
6189 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006190 * The following happens:
6191 *
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03006192 * - Destroys all paint nodes related to the output.
6193 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006194 * - The output assignments of all views in the current scenegraph are
6195 * recomputed.
6196 *
6197 * - Presentation feedback is discarded.
6198 *
6199 * - Compositor is notified that outputs were changed and
6200 * applies the necessary changes to re-layout outputs.
6201 *
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006202 * - The output is put back in the pending outputs list.
6203 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006204 * - Signal is emitted to notify all users of the weston_output
6205 * object that the output is being destroyed.
6206 *
6207 * - wl_output protocol objects referencing this weston_output
Pekka Paalanen2210ad02017-03-30 15:48:06 +03006208 * are made inert, and the wl_output global is removed.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006209 *
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03006210 * - The output's internal ID is released.
6211 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006212 * \ingroup compositor
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006213 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006214 */
6215static void
6216weston_compositor_remove_output(struct weston_output *output)
6217{
Pekka Paalanenbccda712017-03-29 16:16:04 +03006218 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03006219 struct weston_paint_node *pnode, *pntmp;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006220 struct weston_view *view;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03006221 struct weston_head *head;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006222
6223 assert(output->destroying);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006224 assert(output->enabled);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006225
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03006226 wl_list_for_each_safe(pnode, pntmp,
6227 &output->paint_node_list, output_link) {
6228 weston_paint_node_destroy(pnode);
6229 }
6230
Pekka Paalanenbccda712017-03-29 16:16:04 +03006231 wl_list_for_each(view, &compositor->view_list, link) {
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006232 if (view->output_mask & (1u << output->id))
6233 weston_view_assign_output(view);
6234 }
6235
6236 weston_presentation_feedback_discard_list(&output->feedback_list);
6237
Pekka Paalanen9711fd92018-06-21 14:26:18 +03006238 weston_compositor_reflow_outputs(compositor, output, -output->width);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006239
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006240 wl_list_remove(&output->link);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006241 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006242 output->enabled = false;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006243
Pekka Paalanenbccda712017-03-29 16:16:04 +03006244 wl_signal_emit(&compositor->output_destroyed_signal, output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006245 wl_signal_emit(&output->destroy_signal, output);
6246
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02006247 wl_list_for_each(head, &output->head_list, output_link)
6248 weston_head_remove_global(head);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03006249
6250 compositor->output_id_pool &= ~(1u << output->id);
6251 output->id = 0xffffffff; /* invalid */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006252}
6253
6254/** Sets the output scale for a given output.
6255 *
6256 * \param output The weston_output object that the scale is set for.
6257 * \param scale Scale factor for the given output.
6258 *
6259 * It only supports setting scale for an output that
6260 * is not enabled and it can only be ran once.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006261 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05006262 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006263 */
6264WL_EXPORT void
6265weston_output_set_scale(struct weston_output *output,
6266 int32_t scale)
6267{
6268 /* We can only set scale on a disabled output */
6269 assert(!output->enabled);
6270
6271 /* We only want to set scale once */
6272 assert(!output->scale);
6273
6274 output->scale = scale;
6275}
6276
6277/** Sets the output transform for a given output.
6278 *
6279 * \param output The weston_output object that the transform is set for.
6280 * \param transform Transform value for the given output.
6281 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006282 * Refer to wl_output::transform section located at
6283 * https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_output
6284 * for list of values that can be passed to this function.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006285 *
Marius Vlad55d87362019-06-11 01:15:35 +03006286 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006287 */
6288WL_EXPORT void
6289weston_output_set_transform(struct weston_output *output,
6290 uint32_t transform)
6291{
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006292 struct weston_pointer_motion_event ev;
6293 struct wl_resource *resource;
6294 struct weston_seat *seat;
6295 pixman_region32_t old_region;
6296 int mid_x, mid_y;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006297 struct weston_head *head;
6298 int ver;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006299
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006300 if (!output->enabled && output->transform == UINT32_MAX) {
6301 output->transform = transform;
6302 return;
6303 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006304
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006305 weston_output_transform_scale_init(output, transform, output->scale);
6306
6307 pixman_region32_init(&old_region);
6308 pixman_region32_copy(&old_region, &output->region);
6309
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006310 weston_output_init_geometry(output, output->x, output->y);
6311
6312 output->dirty = 1;
6313
6314 /* Notify clients of the change for output transform. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006315 wl_list_for_each(head, &output->head_list, output_link) {
6316 wl_resource_for_each(resource, &head->resource_list) {
6317 wl_output_send_geometry(resource,
6318 output->x,
6319 output->y,
6320 head->mm_width,
6321 head->mm_height,
6322 head->subpixel,
6323 head->make,
6324 head->model,
6325 output->transform);
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006326
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006327 ver = wl_resource_get_version(resource);
6328 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
6329 wl_output_send_done(resource);
6330 }
Roman Gilge97391c2019-03-29 13:01:06 +01006331 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
6332 zxdg_output_v1_send_logical_position(resource,
6333 output->x,
6334 output->y);
6335 zxdg_output_v1_send_logical_size(resource,
6336 output->width,
6337 output->height);
6338 zxdg_output_v1_send_done(resource);
6339 }
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006340 }
6341
6342 /* we must ensure that pointers are inside output, otherwise they disappear */
6343 mid_x = output->x + output->width / 2;
6344 mid_y = output->y + output->height / 2;
6345
6346 ev.mask = WESTON_POINTER_MOTION_ABS;
6347 ev.x = wl_fixed_to_double(wl_fixed_from_int(mid_x));
6348 ev.y = wl_fixed_to_double(wl_fixed_from_int(mid_y));
6349
6350 wl_list_for_each(seat, &output->compositor->seat_list, link) {
6351 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
6352
6353 if (pointer && pixman_region32_contains_point(&old_region,
6354 wl_fixed_to_int(pointer->x),
6355 wl_fixed_to_int(pointer->y),
6356 NULL))
6357 weston_pointer_move(pointer, &ev);
6358 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006359}
6360
Pekka Paalanenb5265af2020-12-02 11:57:58 +02006361/** Make the output use renderer shadow buffer.
6362 *
6363 * \param output The weston_output object to modify.
6364 * \return True on success, false if unsupported.
6365 *
6366 * This can only be set on a disabled output object.
6367 *
6368 * This is a temporary API to demonstrate WESTON_CAP_COLOR_OPS and allow
6369 * testing related features. This will be superseded with color management
6370 * API.
6371 *
6372 * By default, a renderer is not using a shadow buffer of its own. Enabling
6373 * a shadow buffer may enable other color related features.
6374 *
6375 * Support depends on the chosen renderer and the graphics driver stack in use.
6376 *
6377 * \ingroup output
6378 */
6379WL_EXPORT bool
6380weston_output_set_renderer_shadow_buffer(struct weston_output *output)
6381{
6382 struct weston_compositor *compositor = output->compositor;
6383
6384 assert(!output->enabled);
6385
6386 if (compositor->capabilities & WESTON_CAP_COLOR_OPS) {
6387 output->use_renderer_shadow_buffer = true;
6388 return true;
6389 }
6390
6391 return false;
6392}
6393
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006394/** Initializes a weston_output object with enough data so
6395 ** an output can be configured.
6396 *
6397 * \param output The weston_output object to initialize
6398 * \param compositor The compositor instance.
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006399 * \param name Name for the output (the string is copied).
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006400 *
6401 * Sets initial values for fields that are expected to be
6402 * configured either by compositors or backends.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006403 *
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006404 * The name is used in logs, and can be used by compositors as a configuration
6405 * identifier.
6406 *
Marius Vlad55d87362019-06-11 01:15:35 +03006407 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006408 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006409 */
6410WL_EXPORT void
Armin Krezović40087402016-09-30 14:11:12 +02006411weston_output_init(struct weston_output *output,
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006412 struct weston_compositor *compositor,
6413 const char *name)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006414{
6415 output->compositor = compositor;
6416 output->destroying = 0;
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006417 output->name = strdup(name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006418 wl_list_init(&output->link);
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006419 wl_signal_init(&output->user_destroy_signal);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006420 output->enabled = false;
Ankit Nautiyal2690a772019-03-25 17:57:59 +05306421 output->desired_protection = WESTON_HDCP_DISABLE;
Ankit Nautiyal2844f8e2019-04-03 10:14:59 +05306422 output->allow_protection = true;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006423
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006424 wl_list_init(&output->head_list);
6425
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006426 /* Add some (in)sane defaults which can be used
6427 * for checking if an output was properly configured
6428 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006429 output->scale = 0;
6430 /* Can't use -1 on uint32_t and 0 is valid enum value */
6431 output->transform = UINT32_MAX;
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006432
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006433 pixman_region32_init(&output->region);
Pekka Paalanen42704142017-09-06 16:47:52 +03006434 wl_list_init(&output->mode_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006435}
6436
6437/** Adds weston_output object to pending output list.
6438 *
6439 * \param output The weston_output object to add
6440 * \param compositor The compositor instance.
6441 *
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03006442 * The opposite of this operation is built into weston_output_release().
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006443 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006444 * \ingroup compositor
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006445 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006446 */
6447WL_EXPORT void
6448weston_compositor_add_pending_output(struct weston_output *output,
6449 struct weston_compositor *compositor)
6450{
Pekka Paalanene952a012017-03-29 17:14:00 +03006451 assert(output->disable);
6452 assert(output->enable);
6453
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006454 wl_list_remove(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006455 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006456}
6457
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006458/** Create a string with the attached heads' names.
6459 *
6460 * The string must be free()'d.
Marius Vlad55d87362019-06-11 01:15:35 +03006461 *
6462 * \ingroup output
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006463 */
6464static char *
6465weston_output_create_heads_string(struct weston_output *output)
6466{
6467 FILE *fp;
6468 char *str = NULL;
6469 size_t size = 0;
6470 struct weston_head *head;
6471 const char *sep = "";
6472
6473 fp = open_memstream(&str, &size);
6474 if (!fp)
6475 return NULL;
6476
6477 wl_list_for_each(head, &output->head_list, output_link) {
6478 fprintf(fp, "%s%s", sep, head->name);
6479 sep = ", ";
6480 }
6481 fclose(fp);
6482
6483 return str;
6484}
6485
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006486/** Constructs a weston_output object that can be used by the compositor.
6487 *
Pekka Paalanencc201e42017-03-30 15:11:25 +03006488 * \param output The weston_output object that needs to be enabled. Must not
Pekka Paalanenddce54d2017-08-23 16:00:21 +03006489 * be enabled already. Must have at least one head attached.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006490 *
6491 * Output coordinates are calculated and each new output is by default
6492 * assigned to the right of previous one.
6493 *
6494 * Sets up the transformation, zoom, and geometry of the output using
6495 * the properties that need to be configured by the compositor.
6496 *
6497 * Establishes a repaint timer for the output with the relevant display
6498 * object's event loop. See output_repaint_timer_handler().
6499 *
6500 * The output is assigned an ID. Weston can support up to 32 distinct
6501 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
6502 * is referred to and used to find the first available ID number, and
6503 * then this ID is marked as used in output_id_pool.
6504 *
6505 * The output is also assigned a Wayland global with the wl_output
6506 * external interface.
6507 *
6508 * Backend specific function is called to set up the output output.
6509 *
6510 * Output is added to the compositor's output list
6511 *
6512 * If the backend specific function fails, the weston_output object
6513 * is returned to a state it was before calling this function and
6514 * is added to the compositor's pending_output_list in case it needs
6515 * to be reconfigured or just so it can be destroyed at shutdown.
6516 *
6517 * 0 is returned on success, -1 on failure.
Marius Vlad55d87362019-06-11 01:15:35 +03006518 *
6519 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006520 */
6521WL_EXPORT int
6522weston_output_enable(struct weston_output *output)
6523{
Armin Krezović782f5df2016-09-30 14:11:11 +02006524 struct weston_compositor *c = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006525 struct weston_output *iterator;
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03006526 struct weston_head *head;
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006527 char *head_names;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006528 int x = 0, y = 0;
6529
Pekka Paalanencc201e42017-03-30 15:11:25 +03006530 if (output->enabled) {
6531 weston_log("Error: attempt to enable an enabled output '%s'\n",
6532 output->name);
6533 return -1;
6534 }
6535
Pekka Paalanenddce54d2017-08-23 16:00:21 +03006536 if (wl_list_empty(&output->head_list)) {
6537 weston_log("Error: cannot enable output '%s' without heads.\n",
6538 output->name);
6539 return -1;
6540 }
6541
Pekka Paalanen586e1ac2017-09-14 16:17:59 +03006542 if (wl_list_empty(&output->mode_list) || !output->current_mode) {
6543 weston_log("Error: no video mode for output '%s'.\n",
6544 output->name);
6545 return -1;
6546 }
6547
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03006548 wl_list_for_each(head, &output->head_list, output_link) {
6549 assert(head->make);
6550 assert(head->model);
6551 }
6552
Armin Krezović782f5df2016-09-30 14:11:11 +02006553 iterator = container_of(c->output_list.prev,
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006554 struct weston_output, link);
6555
Armin Krezović782f5df2016-09-30 14:11:11 +02006556 if (!wl_list_empty(&c->output_list))
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006557 x = iterator->x + iterator->width;
6558
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006559 /* Make sure the scale is set up */
6560 assert(output->scale);
6561
6562 /* Make sure we have a transform set */
6563 assert(output->transform != UINT32_MAX);
6564
Armin Krezović782f5df2016-09-30 14:11:11 +02006565 output->x = x;
6566 output->y = y;
6567 output->dirty = 1;
6568 output->original_scale = output->scale;
6569
Marius Vlad3a2f8292019-11-04 17:53:46 +02006570 wl_signal_init(&output->frame_signal);
6571 wl_signal_init(&output->destroy_signal);
6572
Armin Krezović782f5df2016-09-30 14:11:11 +02006573 weston_output_transform_scale_init(output, output->transform, output->scale);
6574 weston_output_init_zoom(output);
6575
6576 weston_output_init_geometry(output, x, y);
6577 weston_output_damage(output);
6578
Armin Krezović782f5df2016-09-30 14:11:11 +02006579 wl_list_init(&output->animation_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02006580 wl_list_init(&output->feedback_list);
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03006581 wl_list_init(&output->paint_node_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02006582
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006583 /* Enable the output (set up the crtc or create a
6584 * window representing the output, set up the
6585 * renderer, etc)
6586 */
6587 if (output->enable(output) < 0) {
6588 weston_log("Enabling output \"%s\" failed.\n", output->name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006589 return -1;
6590 }
6591
6592 weston_compositor_add_output(output->compositor, output);
6593
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006594 head_names = weston_output_create_heads_string(output);
6595 weston_log("Output '%s' enabled with head(s) %s\n",
6596 output->name, head_names);
6597 free(head_names);
6598
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006599 return 0;
6600}
6601
6602/** Converts a weston_output object to a pending output state, so it
6603 ** can be configured again or destroyed.
6604 *
6605 * \param output The weston_output object that needs to be disabled.
6606 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006607 * Calls a backend specific function to disable an output, in case
6608 * such function exists.
6609 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03006610 * The backend specific disable function may choose to postpone the disabling
6611 * by returning a negative value, in which case this function returns early.
6612 * In that case the backend will guarantee the output will be disabled soon
6613 * by the backend calling this function again. One must not attempt to re-enable
6614 * the output until that happens.
6615 *
6616 * Otherwise, if the output is being used by the compositor, it is removed
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006617 * from weston's output_list (see weston_compositor_remove_output())
6618 * and is returned to a state it was before weston_output_enable()
6619 * was ran (see weston_output_enable_undo()).
6620 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03006621 * See weston_output_init() for more information on the
6622 * state output is returned to.
Pekka Paalanencc201e42017-03-30 15:11:25 +03006623 *
6624 * If the output has never been enabled yet, this function can still be
6625 * called to ensure that the output is actually turned off rather than left
6626 * in the state it was discovered in.
Marius Vlad55d87362019-06-11 01:15:35 +03006627 *
6628 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006629 */
6630WL_EXPORT void
6631weston_output_disable(struct weston_output *output)
6632{
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006633 /* Should we rename this? */
6634 output->destroying = 1;
6635
Pekka Paalanenc65df642017-03-29 15:45:46 +03006636 /* Disable is called unconditionally also for not-enabled outputs,
6637 * because at compositor start-up, if there is an output that is
6638 * already on but the compositor wants to turn it off, we have to
6639 * forward the turn-off to the backend so it knows to do it.
6640 * The backend cannot initially turn off everything, because it
6641 * would cause unnecessary mode-sets for all outputs the compositor
6642 * wants to be on.
6643 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006644 if (output->disable(output) < 0)
6645 return;
6646
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006647 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006648 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006649
6650 output->destroying = 0;
6651}
6652
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006653/** Forces a synchronous call to heads_changed hook
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006654 *
6655 * \param compositor The compositor instance
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006656 *
6657 * If there are new or changed heads, calls the heads_changed hook and
6658 * returns after the hook returns.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006659 *
6660 * \ingroup compositor
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006661 */
6662WL_EXPORT void
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006663weston_compositor_flush_heads_changed(struct weston_compositor *compositor)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006664{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03006665 if (compositor->heads_changed_source) {
6666 wl_event_source_remove(compositor->heads_changed_source);
6667 weston_compositor_call_heads_changed(compositor);
6668 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006669}
6670
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006671/** Add destroy callback for an output
6672 *
6673 * \param output The output to watch.
6674 * \param listener The listener to add. The \c notify member must be set.
6675 *
6676 * The listener callback will be called when user destroys an output. This
6677 * may be delayed by a backend in some cases. The main purpose of the
6678 * listener is to allow hooking up custom data to the output. The custom data
6679 * can be fetched via weston_output_get_destroy_listener() followed by
6680 * container_of().
6681 *
6682 * The \c data argument to the notify callback is the weston_output being
6683 * destroyed.
6684 *
6685 * @note This is for the final destruction of an output, not when it gets
6686 * disabled. If you want to keep track of enabled outputs, this is not it.
Marius Vlad55d87362019-06-11 01:15:35 +03006687 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05006688 * \ingroup output
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006689 */
6690WL_EXPORT void
6691weston_output_add_destroy_listener(struct weston_output *output,
6692 struct wl_listener *listener)
6693{
6694 wl_signal_add(&output->user_destroy_signal, listener);
6695}
6696
6697/** Look up destroy listener for an output
6698 *
6699 * \param output The output to query.
6700 * \param notify The notify function used used for the added destroy listener.
6701 * \return The listener, or NULL if not found.
6702 *
6703 * This looks up the previously added destroy listener struct based on the
6704 * notify function it has. The listener can be used to access user data
6705 * through \c container_of().
6706 *
6707 * \sa wl_signal_get() weston_output_add_destroy_listener()
Marius Vlad55d87362019-06-11 01:15:35 +03006708 * \ingroup output
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006709 */
6710WL_EXPORT struct wl_listener *
6711weston_output_get_destroy_listener(struct weston_output *output,
6712 wl_notify_func_t notify)
6713{
6714 return wl_signal_get(&output->user_destroy_signal, notify);
6715}
6716
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006717/** Uninitialize an output
6718 *
6719 * Removes the output from the list of enabled outputs if necessary, but
6720 * does not call the backend's output disable function. The output will no
6721 * longer be in the list of pending outputs either.
6722 *
6723 * All fields of weston_output become uninitialized, i.e. should not be used
6724 * anymore. The caller can free the memory after this.
6725 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05006726 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006727 * \internal
6728 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006729WL_EXPORT void
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03006730weston_output_release(struct weston_output *output)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006731{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006732 struct weston_head *head, *tmp;
6733
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006734 output->destroying = 1;
6735
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006736 wl_signal_emit(&output->user_destroy_signal, output);
6737
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03006738 if (output->idle_repaint_source)
6739 wl_event_source_remove(output->idle_repaint_source);
6740
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006741 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006742 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006743
Pekka Paalanen1a4f87d2021-04-30 14:48:52 +03006744 assert(wl_list_empty(&output->paint_node_list));
6745
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006746 pixman_region32_fini(&output->region);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006747 wl_list_remove(&output->link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006748
6749 wl_list_for_each_safe(head, tmp, &output->head_list, output_link)
6750 weston_head_detach(head);
6751
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006752 free(output->name);
6753}
6754
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006755/** Find an output by its given name
6756 *
6757 * \param compositor The compositor to search in.
6758 * \param name The output name to search for.
6759 * \return An existing output with the given name, or NULL if not found.
6760 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006761 * \ingroup compositor
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006762 */
6763WL_EXPORT struct weston_output *
6764weston_compositor_find_output_by_name(struct weston_compositor *compositor,
6765 const char *name)
6766{
6767 struct weston_output *output;
6768
6769 wl_list_for_each(output, &compositor->output_list, link)
6770 if (strcmp(output->name, name) == 0)
6771 return output;
6772
6773 wl_list_for_each(output, &compositor->pending_output_list, link)
6774 if (strcmp(output->name, name) == 0)
6775 return output;
6776
6777 return NULL;
6778}
6779
6780/** Create a named output
6781 *
6782 * \param compositor The compositor.
6783 * \param name The name for the output.
6784 * \return A new \c weston_output, or NULL on failure.
6785 *
6786 * This creates a new weston_output that starts with no heads attached.
6787 *
6788 * An output must be configured and it must have at least one head before
6789 * it can be enabled.
6790 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006791 * \ingroup compositor
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006792 */
6793WL_EXPORT struct weston_output *
6794weston_compositor_create_output(struct weston_compositor *compositor,
6795 const char *name)
6796{
6797 assert(compositor->backend->create_output);
6798
6799 if (weston_compositor_find_output_by_name(compositor, name)) {
6800 weston_log("Warning: attempted to create an output with a "
6801 "duplicate name '%s'.\n", name);
6802 return NULL;
6803 }
6804
6805 return compositor->backend->create_output(compositor, name);
6806}
6807
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006808/** Create an output for an unused head
6809 *
6810 * \param compositor The compositor.
6811 * \param head The head to attach to the output.
6812 * \return A new \c weston_output, or NULL on failure.
6813 *
6814 * This creates a new weston_output that starts with the given head attached.
6815 * The output inherits the name of the head. The head must not be already
6816 * attached to another output.
6817 *
6818 * An output must be configured before it can be enabled.
6819 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006820 * \ingroup compositor
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006821 */
6822WL_EXPORT struct weston_output *
6823weston_compositor_create_output_with_head(struct weston_compositor *compositor,
6824 struct weston_head *head)
6825{
6826 struct weston_output *output;
6827
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006828 output = weston_compositor_create_output(compositor, head->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006829 if (!output)
6830 return NULL;
6831
6832 if (weston_output_attach_head(output, head) < 0) {
Pekka Paalanen42c0e142017-10-27 12:07:49 +03006833 weston_output_destroy(output);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006834 return NULL;
6835 }
6836
6837 return output;
6838}
6839
6840/** Destroy an output
6841 *
6842 * \param output The output to destroy.
6843 *
6844 * The heads attached to the given output are detached and become unused again.
6845 *
6846 * It is not necessary to explicitly destroy all outputs at compositor exit.
Leandro Ribeiroca640d52020-01-27 19:12:01 -03006847 * weston_compositor_destroy() will automatically destroy any remaining
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006848 * outputs.
6849 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05006850 * \ingroup output
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006851 */
6852WL_EXPORT void
6853weston_output_destroy(struct weston_output *output)
6854{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006855 output->destroy(output);
6856}
6857
Pekka Paalanencf0a4762017-04-04 16:36:07 +03006858/** When you need a head...
6859 *
6860 * This function is a hack, used until all code has been converted to become
6861 * multi-head aware.
6862 *
6863 * \param output The weston_output whose head to get.
6864 * \return The first head in the output's list.
Marius Vlad55d87362019-06-11 01:15:35 +03006865 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05006866 * \ingroup output
Pekka Paalanencf0a4762017-04-04 16:36:07 +03006867 */
6868WL_EXPORT struct weston_head *
6869weston_output_get_first_head(struct weston_output *output)
6870{
6871 if (wl_list_empty(&output->head_list))
6872 return NULL;
6873
6874 return container_of(output->head_list.next,
6875 struct weston_head, output_link);
6876}
6877
Ankit Nautiyal2844f8e2019-04-03 10:14:59 +05306878/** Allow/Disallow content-protection support for an output
6879 *
6880 * This function sets the allow_protection member for an output. Setting of
6881 * this field will allow the compositor to attempt content-protection for this
6882 * output, for a backend that supports the content-protection protocol.
6883 *
6884 * \param output The weston_output for whom the content-protection is to be
6885 * allowed.
6886 * \param allow_protection The bool value which is to be set.
6887 */
6888WL_EXPORT void
6889weston_output_allow_protection(struct weston_output *output,
6890 bool allow_protection)
6891{
6892 output->allow_protection = allow_protection;
6893}
6894
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01006895static void
Roman Gilge97391c2019-03-29 13:01:06 +01006896xdg_output_unlist(struct wl_resource *resource)
6897{
6898 wl_list_remove(wl_resource_get_link(resource));
6899}
6900
6901static void
6902xdg_output_destroy(struct wl_client *client, struct wl_resource *resource)
6903{
6904 wl_resource_destroy(resource);
6905}
6906
6907static const struct zxdg_output_v1_interface xdg_output_interface = {
6908 xdg_output_destroy
6909};
6910
6911static void
6912xdg_output_manager_destroy(struct wl_client *client,
6913 struct wl_resource *resource)
6914{
6915 wl_resource_destroy(resource);
6916}
6917
6918static void
6919xdg_output_manager_get_xdg_output(struct wl_client *client,
6920 struct wl_resource *manager,
6921 uint32_t id,
6922 struct wl_resource *output_resource)
6923{
6924 int version = wl_resource_get_version(manager);
6925 struct weston_head *head = wl_resource_get_user_data(output_resource);
6926 struct weston_output *output = head->output;
6927 struct wl_resource *resource;
6928
6929 resource = wl_resource_create(client, &zxdg_output_v1_interface,
6930 version, id);
6931 if (resource == NULL) {
6932 wl_client_post_no_memory(client);
6933 return;
6934 }
6935
6936 wl_list_insert(&head->xdg_output_resource_list,
6937 wl_resource_get_link(resource));
6938
6939 wl_resource_set_implementation(resource, &xdg_output_interface,
6940 NULL, xdg_output_unlist);
6941
6942 zxdg_output_v1_send_logical_position(resource, output->x, output->y);
6943 zxdg_output_v1_send_logical_size(resource,
6944 output->width,
6945 output->height);
6946 if (version >= ZXDG_OUTPUT_V1_NAME_SINCE_VERSION)
6947 zxdg_output_v1_send_name(resource, head->name);
6948
6949 zxdg_output_v1_send_done(resource);
6950}
6951
6952static const struct zxdg_output_manager_v1_interface xdg_output_manager_interface = {
6953 xdg_output_manager_destroy,
6954 xdg_output_manager_get_xdg_output
6955};
6956
6957static void
6958bind_xdg_output_manager(struct wl_client *client,
6959 void *data, uint32_t version, uint32_t id)
6960{
6961 struct wl_resource *resource;
6962
6963 resource = wl_resource_create(client, &zxdg_output_manager_v1_interface,
6964 version, id);
6965 if (resource == NULL) {
6966 wl_client_post_no_memory(client);
6967 return;
6968 }
6969
6970 wl_resource_set_implementation(resource, &xdg_output_manager_interface,
6971 NULL, NULL);
6972}
6973
6974static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006975destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006976{
Jonny Lamb74130762013-11-26 18:19:46 +01006977 struct weston_surface *surface =
6978 wl_resource_get_user_data(resource);
6979
Pekka Paalanen4826f872016-04-22 14:14:38 +03006980 if (!surface)
6981 return;
6982
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006983 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02006984 surface->pending.buffer_viewport.buffer.src_width =
6985 wl_fixed_from_int(-1);
6986 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006987 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006988}
6989
6990static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006991viewport_destroy(struct wl_client *client,
6992 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006993{
6994 wl_resource_destroy(resource);
6995}
6996
6997static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006998viewport_set_source(struct wl_client *client,
6999 struct wl_resource *resource,
7000 wl_fixed_t src_x,
7001 wl_fixed_t src_y,
7002 wl_fixed_t src_width,
7003 wl_fixed_t src_height)
7004{
7005 struct weston_surface *surface =
7006 wl_resource_get_user_data(resource);
7007
Pekka Paalanen4826f872016-04-22 14:14:38 +03007008 if (!surface) {
7009 wl_resource_post_error(resource,
7010 WP_VIEWPORT_ERROR_NO_SURFACE,
7011 "wl_surface for this viewport is no longer exists");
7012 return;
7013 }
7014
7015 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03007016 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007017
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007018 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03007019 src_height == wl_fixed_from_int(-1) &&
7020 src_x == wl_fixed_from_int(-1) &&
7021 src_y == wl_fixed_from_int(-1)) {
7022 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007023 surface->pending.buffer_viewport.buffer.src_width =
7024 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02007025 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007026 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007027 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007028
Pekka Paalanen201769a2016-04-26 14:42:11 +03007029 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007030 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007031 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03007032 "wl_surface@%d viewport source "
7033 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
7034 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007035 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03007036 wl_fixed_to_double(src_height),
7037 wl_fixed_to_double(src_x),
7038 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007039 return;
7040 }
7041
7042 surface->pending.buffer_viewport.buffer.src_x = src_x;
7043 surface->pending.buffer_viewport.buffer.src_y = src_y;
7044 surface->pending.buffer_viewport.buffer.src_width = src_width;
7045 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02007046 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007047}
7048
7049static void
7050viewport_set_destination(struct wl_client *client,
7051 struct wl_resource *resource,
7052 int32_t dst_width,
7053 int32_t dst_height)
7054{
7055 struct weston_surface *surface =
7056 wl_resource_get_user_data(resource);
7057
Pekka Paalanen4826f872016-04-22 14:14:38 +03007058 if (!surface) {
7059 wl_resource_post_error(resource,
7060 WP_VIEWPORT_ERROR_NO_SURFACE,
7061 "wl_surface for this viewport no longer exists");
7062 return;
7063 }
7064
7065 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007066
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007067 if (dst_width == -1 && dst_height == -1) {
7068 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007069 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02007070 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007071 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007072 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007073
7074 if (dst_width <= 0 || dst_height <= 0) {
7075 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007076 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03007077 "destination size must be positive (%dx%d)",
7078 dst_width, dst_height);
7079 return;
7080 }
7081
7082 surface->pending.buffer_viewport.surface.width = dst_width;
7083 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02007084 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007085}
7086
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007087static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007088 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007089 viewport_set_source,
7090 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01007091};
7092
7093static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007094viewporter_destroy(struct wl_client *client,
7095 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01007096{
7097 wl_resource_destroy(resource);
7098}
7099
7100static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007101viewporter_get_viewport(struct wl_client *client,
7102 struct wl_resource *viewporter,
7103 uint32_t id,
7104 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01007105{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007106 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007107 struct weston_surface *surface =
7108 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01007109 struct wl_resource *resource;
7110
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007111 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007112 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007113 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007114 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01007115 return;
7116 }
7117
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007118 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007119 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01007120 if (resource == NULL) {
7121 wl_client_post_no_memory(client);
7122 return;
7123 }
7124
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007125 wl_resource_set_implementation(resource, &viewport_interface,
7126 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01007127
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007128 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01007129}
7130
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007131static const struct wp_viewporter_interface viewporter_interface = {
7132 viewporter_destroy,
7133 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01007134};
7135
7136static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007137bind_viewporter(struct wl_client *client,
7138 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01007139{
7140 struct wl_resource *resource;
7141
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007142 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06007143 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01007144 if (resource == NULL) {
7145 wl_client_post_no_memory(client);
7146 return;
7147 }
7148
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007149 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01007150 NULL, NULL);
7151}
7152
7153static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04007154destroy_presentation_feedback(struct wl_resource *feedback_resource)
7155{
7156 struct weston_presentation_feedback *feedback;
7157
7158 feedback = wl_resource_get_user_data(feedback_resource);
7159
7160 wl_list_remove(&feedback->link);
7161 free(feedback);
7162}
7163
7164static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007165presentation_destroy(struct wl_client *client, struct wl_resource *resource)
7166{
7167 wl_resource_destroy(resource);
7168}
7169
7170static void
7171presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04007172 struct wl_resource *presentation_resource,
7173 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007174 uint32_t callback)
7175{
Pekka Paalanen133e4392014-09-23 22:08:46 -04007176 struct weston_surface *surface;
7177 struct weston_presentation_feedback *feedback;
7178
7179 surface = wl_resource_get_user_data(surface_resource);
7180
Bryce Harringtonde16d892014-11-20 22:21:57 -08007181 feedback = zalloc(sizeof *feedback);
7182 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04007183 goto err_calloc;
7184
7185 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007186 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04007187 1, callback);
7188 if (!feedback->resource)
7189 goto err_create;
7190
7191 wl_resource_set_implementation(feedback->resource, NULL, feedback,
7192 destroy_presentation_feedback);
7193
7194 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
7195
7196 return;
7197
7198err_create:
7199 free(feedback);
7200
7201err_calloc:
7202 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007203}
7204
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007205static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007206 presentation_destroy,
7207 presentation_feedback
7208};
7209
7210static void
7211bind_presentation(struct wl_client *client,
7212 void *data, uint32_t version, uint32_t id)
7213{
7214 struct weston_compositor *compositor = data;
7215 struct wl_resource *resource;
7216
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007217 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06007218 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007219 if (resource == NULL) {
7220 wl_client_post_no_memory(client);
7221 return;
7222 }
7223
7224 wl_resource_set_implementation(resource, &presentation_implementation,
7225 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007226 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007227}
7228
7229static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05007230compositor_bind(struct wl_client *client,
7231 void *data, uint32_t version, uint32_t id)
7232{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007233 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05007234 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05007235
Jason Ekstranda85118c2013-06-27 20:17:02 -05007236 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06007237 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07007238 if (resource == NULL) {
7239 wl_client_post_no_memory(client);
7240 return;
7241 }
7242
7243 wl_resource_set_implementation(resource, &compositor_interface,
7244 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05007245}
7246
Daniel Stonece62cb32018-07-20 09:46:24 +01007247static const char *
7248output_repaint_status_text(struct weston_output *output)
7249{
7250 switch (output->repaint_status) {
7251 case REPAINT_NOT_SCHEDULED:
7252 return "no repaint";
7253 case REPAINT_BEGIN_FROM_IDLE:
7254 return "start_repaint_loop scheduled";
7255 case REPAINT_SCHEDULED:
7256 return "repaint scheduled";
7257 case REPAINT_AWAITING_COMPLETION:
7258 return "awaiting completion";
7259 }
7260
7261 assert(!"output_repaint_status_text missing enum");
7262 return NULL;
7263}
7264
7265static void
7266debug_scene_view_print_buffer(FILE *fp, struct weston_view *view)
7267{
7268 struct weston_buffer *buffer = view->surface->buffer_ref.buffer;
7269 struct wl_shm_buffer *shm;
7270 struct linux_dmabuf_buffer *dmabuf;
Marius Vlad00a6e012018-11-20 17:52:31 +02007271 const struct pixel_format_info *pixel_info = NULL;
Daniel Stonece62cb32018-07-20 09:46:24 +01007272
7273 if (!buffer) {
7274 fprintf(fp, "\t\t[buffer not available]\n");
7275 return;
7276 }
7277
7278 shm = wl_shm_buffer_get(buffer->resource);
7279 if (shm) {
Marius Vlad00a6e012018-11-20 17:52:31 +02007280 uint32_t _format = wl_shm_buffer_get_format(shm);
7281 pixel_info = pixel_format_get_info_shm(_format);
Daniel Stonece62cb32018-07-20 09:46:24 +01007282 fprintf(fp, "\t\tSHM buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02007283 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
7284 (unsigned long) _format,
7285 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Daniel Stonece62cb32018-07-20 09:46:24 +01007286 return;
7287 }
7288
7289 dmabuf = linux_dmabuf_buffer_get(buffer->resource);
7290 if (dmabuf) {
Marius Vlad00a6e012018-11-20 17:52:31 +02007291 pixel_info = pixel_format_get_info(dmabuf->attributes.format);
Daniel Stonece62cb32018-07-20 09:46:24 +01007292 fprintf(fp, "\t\tdmabuf buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02007293 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
7294 (unsigned long) dmabuf->attributes.format,
7295 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Daniel Stonece62cb32018-07-20 09:46:24 +01007296 fprintf(fp, "\t\t\tmodifier: 0x%llx\n",
7297 (unsigned long long) dmabuf->attributes.modifier[0]);
7298 return;
7299 }
7300
Marius Vlad253ba9a2019-03-17 18:22:21 +02007301 fprintf(fp, "\t\tEGL buffer\n");
Daniel Stonece62cb32018-07-20 09:46:24 +01007302}
7303
7304static void
7305debug_scene_view_print(FILE *fp, struct weston_view *view, int view_idx)
7306{
7307 struct weston_compositor *ec = view->surface->compositor;
7308 struct weston_output *output;
7309 char desc[512];
7310 pixman_box32_t *box;
7311 uint32_t surface_id = 0;
7312 pid_t pid = 0;
7313
7314 if (view->surface->resource) {
7315 struct wl_resource *resource = view->surface->resource;
7316 wl_client_get_credentials(wl_resource_get_client(resource),
7317 &pid, NULL, NULL);
7318 surface_id = wl_resource_get_id(view->surface->resource);
7319 }
7320
7321 if (!view->surface->get_label ||
7322 view->surface->get_label(view->surface, desc, sizeof(desc)) < 0) {
7323 strcpy(desc, "[no description available]");
7324 }
7325 fprintf(fp, "\tView %d (role %s, PID %d, surface ID %u, %s, %p):\n",
7326 view_idx, view->surface->role_name, pid, surface_id,
7327 desc, view);
7328
7329 box = pixman_region32_extents(&view->transform.boundingbox);
7330 fprintf(fp, "\t\tposition: (%d, %d) -> (%d, %d)\n",
7331 box->x1, box->y1, box->x2, box->y2);
7332 box = pixman_region32_extents(&view->transform.opaque);
7333
Michael Olbrichb7e5f102020-08-11 16:33:35 +02007334 if (weston_view_is_opaque(view, &view->transform.boundingbox)) {
Daniel Stonece62cb32018-07-20 09:46:24 +01007335 fprintf(fp, "\t\t[fully opaque]\n");
7336 } else if (!pixman_region32_not_empty(&view->transform.opaque)) {
7337 fprintf(fp, "\t\t[not opaque]\n");
7338 } else {
7339 fprintf(fp, "\t\t[opaque: (%d, %d) -> (%d, %d)]\n",
7340 box->x1, box->y1, box->x2, box->y2);
7341 }
7342
7343 if (view->alpha < 1.0)
7344 fprintf(fp, "\t\talpha: %f\n", view->alpha);
7345
7346 if (view->output_mask != 0) {
7347 bool first_output = true;
7348 fprintf(fp, "\t\toutputs: ");
7349 wl_list_for_each(output, &ec->output_list, link) {
7350 if (!(view->output_mask & (1 << output->id)))
7351 continue;
7352 fprintf(fp, "%s%d (%s)%s",
7353 (first_output) ? "" : ", ",
7354 output->id, output->name,
7355 (view->output == output) ? " (primary)" : "");
7356 first_output = false;
7357 }
7358 } else {
7359 fprintf(fp, "\t\t[no outputs]");
7360 }
7361
7362 fprintf(fp, "\n");
7363
7364 debug_scene_view_print_buffer(fp, view);
7365}
7366
Marius Vlad433f4e72019-02-17 22:14:23 +02007367static void
7368debug_scene_view_print_tree(struct weston_view *view,
Marius Vlada6acfa82019-03-17 18:10:09 +02007369 FILE *fp, int *view_idx)
Marius Vlad433f4e72019-02-17 22:14:23 +02007370{
7371 struct weston_subsurface *sub;
7372 struct weston_view *ev;
7373
7374 /*
7375 * print the view first, then we recursively go on printing
7376 * sub-surfaces. We bail out once no more sub-surfaces are available.
7377 */
Marius Vlada6acfa82019-03-17 18:10:09 +02007378 debug_scene_view_print(fp, view, *view_idx);
Marius Vlad433f4e72019-02-17 22:14:23 +02007379
7380 /* no more sub-surfaces */
7381 if (wl_list_empty(&view->surface->subsurface_list))
7382 return;
7383
7384 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
7385 wl_list_for_each(ev, &sub->surface->views, surface_link) {
Michael Olbrichef5f3232020-04-29 09:03:15 +02007386 /* only print the child views of the current view */
7387 if (ev->parent_view != view)
Marius Vlad433f4e72019-02-17 22:14:23 +02007388 continue;
Marius Vlada6acfa82019-03-17 18:10:09 +02007389
7390 (*view_idx)++;
Marius Vlad433f4e72019-02-17 22:14:23 +02007391 debug_scene_view_print_tree(ev, fp, view_idx);
7392 }
7393 }
7394}
7395
Daniel Stonece62cb32018-07-20 09:46:24 +01007396/**
7397 * Output information on how libweston is currently composing the scene
7398 * graph.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007399 *
7400 * \ingroup compositor
Daniel Stonece62cb32018-07-20 09:46:24 +01007401 */
7402WL_EXPORT char *
7403weston_compositor_print_scene_graph(struct weston_compositor *ec)
7404{
7405 struct weston_output *output;
7406 struct weston_layer *layer;
7407 struct timespec now;
7408 int layer_idx = 0;
7409 FILE *fp;
7410 char *ret;
7411 size_t len;
7412 int err;
7413
7414 fp = open_memstream(&ret, &len);
7415 assert(fp);
7416
7417 weston_compositor_read_presentation_clock(ec, &now);
7418 fprintf(fp, "Weston scene graph at %ld.%09ld:\n\n",
7419 now.tv_sec, now.tv_nsec);
7420
7421 wl_list_for_each(output, &ec->output_list, link) {
7422 struct weston_head *head;
7423 int head_idx = 0;
7424
7425 fprintf(fp, "Output %d (%s):\n", output->id, output->name);
7426 assert(output->enabled);
7427
7428 fprintf(fp, "\tposition: (%d, %d) -> (%d, %d)\n",
7429 output->x, output->y,
7430 output->x + output->width,
7431 output->y + output->height);
7432 fprintf(fp, "\tmode: %dx%d@%.3fHz\n",
7433 output->current_mode->width,
7434 output->current_mode->height,
7435 output->current_mode->refresh / 1000.0);
7436 fprintf(fp, "\tscale: %d\n", output->scale);
7437
7438 fprintf(fp, "\trepaint status: %s\n",
7439 output_repaint_status_text(output));
7440 if (output->repaint_status == REPAINT_SCHEDULED)
7441 fprintf(fp, "\tnext repaint: %ld.%09ld\n",
7442 output->next_repaint.tv_sec,
7443 output->next_repaint.tv_nsec);
7444
7445 wl_list_for_each(head, &output->head_list, output_link) {
7446 fprintf(fp, "\tHead %d (%s): %sconnected\n",
7447 head_idx++, head->name,
7448 (head->connected) ? "" : "not ");
7449 }
7450 }
7451
7452 fprintf(fp, "\n");
7453
7454 wl_list_for_each(layer, &ec->layer_list, link) {
7455 struct weston_view *view;
7456 int view_idx = 0;
7457
7458 fprintf(fp, "Layer %d (pos 0x%lx):\n", layer_idx++,
7459 (unsigned long) layer->position);
7460
7461 if (!weston_layer_mask_is_infinite(layer)) {
7462 fprintf(fp, "\t[mask: (%d, %d) -> (%d,%d)]\n\n",
7463 layer->mask.x1, layer->mask.y1,
7464 layer->mask.x2, layer->mask.y2);
7465 }
7466
Marius Vlada6acfa82019-03-17 18:10:09 +02007467 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
7468 debug_scene_view_print_tree(view, fp, &view_idx);
7469 view_idx++;
7470 }
Daniel Stonece62cb32018-07-20 09:46:24 +01007471
7472 if (wl_list_empty(&layer->view_list.link))
7473 fprintf(fp, "\t[no views]\n");
7474
7475 fprintf(fp, "\n");
7476 }
7477
7478 err = fclose(fp);
7479 assert(err == 0);
7480
7481 return ret;
7482}
7483
7484/**
7485 * Called when the 'scene-graph' debug scope is bound by a client. This
7486 * one-shot weston-debug scope prints the current scene graph when bound,
7487 * and then terminates the stream.
7488 */
7489static void
Marius Vladdad882a2019-07-17 15:43:53 +03007490debug_scene_graph_cb(struct weston_log_subscription *sub, void *data)
Daniel Stonece62cb32018-07-20 09:46:24 +01007491{
7492 struct weston_compositor *ec = data;
7493 char *str = weston_compositor_print_scene_graph(ec);
7494
Marius Vladdad882a2019-07-17 15:43:53 +03007495 weston_log_subscription_printf(sub, "%s", str);
Daniel Stonece62cb32018-07-20 09:46:24 +01007496 free(str);
Marius Vladdad882a2019-07-17 15:43:53 +03007497 weston_log_subscription_complete(sub);
Daniel Stonece62cb32018-07-20 09:46:24 +01007498}
7499
Leandro Ribeiro32a5acd2020-10-19 16:06:22 -03007500/** Retrieve testsuite data from compositor
7501 *
7502 * The testsuite data can be defined by the test suite of projects that uses
7503 * libweston and given to the compositor at the moment of its creation. This
7504 * function should be used when we need to retrieve the testsuite private data
7505 * from the compositor.
7506 *
7507 * \param ec The weston compositor.
7508 * \return The testsuite data.
7509 *
7510 * \ingroup compositor
7511 * \sa weston_compositor_test_data_init
7512 */
7513WL_EXPORT void *
7514weston_compositor_get_test_data(struct weston_compositor *ec)
7515{
7516 return ec->test_data.test_private_data;
7517}
7518
Giulio Camuffo459137b2014-10-11 23:56:24 +03007519/** Create the compositor.
7520 *
7521 * This functions creates and initializes a compositor instance.
7522 *
7523 * \param display The Wayland display to be used.
7524 * \param user_data A pointer to an object that can later be retrieved
Marius Vlada2dace22019-06-12 16:05:44 +03007525 * \param log_ctx A pointer to weston_debug_compositor
Leandro Ribeirocdb72182020-11-06 11:24:22 -03007526 * \param test_data Optional testsuite data, or NULL.
Giulio Camuffo459137b2014-10-11 23:56:24 +03007527 * using the \ref weston_compositor_get_user_data function.
7528 * \return The compositor instance on success or NULL on failure.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007529 *
7530 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007531 */
7532WL_EXPORT struct weston_compositor *
Marius Vlad880b4852019-04-07 17:07:58 +03007533weston_compositor_create(struct wl_display *display,
Leandro Ribeirocdb72182020-11-06 11:24:22 -03007534 struct weston_log_context *log_ctx, void *user_data,
7535 const struct weston_testsuite_data *test_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04007536{
Giulio Camuffo459137b2014-10-11 23:56:24 +03007537 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05007538 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07007539
Leandro Ribeirobd9c0a62020-01-17 10:47:49 -03007540 if (!log_ctx)
7541 return NULL;
7542
Giulio Camuffo459137b2014-10-11 23:56:24 +03007543 ec = zalloc(sizeof *ec);
7544 if (!ec)
7545 return NULL;
7546
Leandro Ribeirocdb72182020-11-06 11:24:22 -03007547 if (test_data)
7548 ec->test_data = *test_data;
7549
Leandro Ribeirobd9c0a62020-01-17 10:47:49 -03007550 ec->weston_log_ctx = log_ctx;
Giulio Camuffo459137b2014-10-11 23:56:24 +03007551 ec->wl_display = display;
7552 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04007553 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07007554 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04007555 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03007556 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03007557 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02007558 wl_signal_init(&ec->idle_signal);
7559 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02007560 wl_signal_init(&ec->show_input_panel_signal);
7561 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02007562 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01007563 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01007564 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02007565 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02007566 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02007567 wl_signal_init(&ec->output_resized_signal);
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03007568 wl_signal_init(&ec->heads_changed_signal);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02007569 wl_signal_init(&ec->output_heads_changed_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07007570 wl_signal_init(&ec->session_signal);
Robert Beckettc569bdc2019-07-10 16:40:12 +01007571 ec->session_active = true;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04007572
Casey Dahlin58ba1372012-04-19 22:50:08 -04007573 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03007574 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04007575
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02007576 ec->activate_serial = 1;
7577
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05007578 ec->touch_mode = WESTON_TOUCH_MODE_NORMAL;
7579
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05307580 ec->content_protection = NULL;
7581
Derek Foreman152254b2015-11-26 14:17:48 -06007582 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04007583 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007584 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05007585
Giulio Camuffo954f1832014-10-11 18:27:30 +03007586 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04007587 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007588 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03007589
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007590 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007591 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007592 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01007593
Roman Gilge97391c2019-03-29 13:01:06 +01007594 if (!wl_global_create(ec->wl_display, &zxdg_output_manager_v1_interface, 2,
7595 ec, bind_xdg_output_manager))
7596 goto fail;
7597
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007598 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007599 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007600 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007601
Jonas Ådahl30d61d82014-10-22 21:21:17 +02007602 if (weston_input_init(ec) != 0)
7603 goto fail;
7604
Jason Ekstranda7af7042013-10-12 22:38:11 -05007605 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02007606 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007607 wl_list_init(&ec->layer_list);
7608 wl_list_init(&ec->seat_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007609 wl_list_init(&ec->pending_output_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007610 wl_list_init(&ec->output_list);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03007611 wl_list_init(&ec->head_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007612 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01007613 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007614 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01007615 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007616 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02007617 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007618
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007619 wl_list_init(&ec->plugin_api_list);
7620
Xiong Zhang97116532013-10-23 13:58:31 +08007621 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02007622 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04007623
Giulio Camuffo459137b2014-10-11 23:56:24 +03007624 wl_data_device_manager_init(ec->wl_display);
7625
7626 wl_display_init_shm(ec->wl_display);
7627
7628 loop = wl_display_get_event_loop(ec->wl_display);
7629 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Daniel Stone6847b852017-03-01 11:34:08 +00007630 ec->repaint_timer =
7631 wl_event_loop_add_timer(loop, output_repaint_timer_handler,
7632 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007633
Quentin Glidic82681572016-12-17 13:40:51 +01007634 weston_layer_init(&ec->fade_layer, ec);
7635 weston_layer_init(&ec->cursor_layer, ec);
7636
7637 weston_layer_set_position(&ec->fade_layer, WESTON_LAYER_POSITION_FADE);
7638 weston_layer_set_position(&ec->cursor_layer,
7639 WESTON_LAYER_POSITION_CURSOR);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007640
Daniel Stonece62cb32018-07-20 09:46:24 +01007641 ec->debug_scene =
Leandro Ribeiroce100192019-12-26 16:35:49 -03007642 weston_compositor_add_log_scope(ec, "scene-graph",
7643 "Scene graph details\n",
7644 debug_scene_graph_cb, NULL,
7645 ec);
Daniel Stonece62cb32018-07-20 09:46:24 +01007646
Marius Vladda104eb2019-09-05 14:31:01 +03007647 ec->timeline =
Leandro Ribeiroce100192019-12-26 16:35:49 -03007648 weston_compositor_add_log_scope(ec, "timeline",
7649 "Timeline event points\n",
7650 weston_timeline_create_subscription,
7651 weston_timeline_destroy_subscription,
7652 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007653 return ec;
7654
7655fail:
7656 free(ec);
7657 return NULL;
7658}
7659
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007660/** weston_compositor_shutdown
7661 * \ingroup compositor
7662 */
Benjamin Franzkeb8263022011-08-30 11:32:47 +02007663WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007664weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07007665{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007666 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07007667
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007668 wl_event_source_remove(ec->idle_source);
7669
Matt Roper361d2ad2011-08-29 13:52:23 -07007670 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02007671 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07007672 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02007673
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007674 /* Destroy all pending outputs associated with this compositor */
7675 wl_list_for_each_safe(output, next, &ec->pending_output_list, link)
7676 output->destroy(output);
7677
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02007678 if (ec->renderer)
7679 ec->renderer->destroy(ec);
7680
Daniel Stone325fc2d2012-05-30 16:31:58 +01007681 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09007682 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01007683 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01007684 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01007685 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02007686 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007687
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04007688 weston_plane_release(&ec->primary_plane);
Matt Roper361d2ad2011-08-29 13:52:23 -07007689}
7690
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007691/** weston_compositor_exit_with_code
7692 * \ingroup compositor
7693 */
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05007694WL_EXPORT void
Adam Jackson3c3f3b12019-10-16 16:02:59 -04007695weston_compositor_exit_with_code(struct weston_compositor *compositor,
Frederic Plourdec336f062014-10-29 14:44:33 -04007696 int exit_code)
7697{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02007698 if (compositor->exit_code == EXIT_SUCCESS)
7699 compositor->exit_code = exit_code;
7700
Giulio Camuffo459137b2014-10-11 23:56:24 +03007701 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04007702}
7703
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007704/** weston_compositor_set_default_pointer_grab
7705 * \ingroup compositor
7706 */
Frederic Plourdec336f062014-10-29 14:44:33 -04007707WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007708weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
7709 const struct weston_pointer_grab_interface *interface)
7710{
7711 struct weston_seat *seat;
7712
7713 ec->default_pointer_grab = interface;
7714 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05007715 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
7716
7717 if (pointer)
7718 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007719 }
7720}
7721
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007722/** weston_compositor_set_presentation_clock
7723 * \ingroup compositor
7724 */
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007725WL_EXPORT int
7726weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
7727 clockid_t clk_id)
7728{
7729 struct timespec ts;
7730
7731 if (clock_gettime(clk_id, &ts) < 0)
7732 return -1;
7733
7734 compositor->presentation_clock = clk_id;
7735
7736 return 0;
7737}
7738
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007739/** For choosing the software clock, when the display hardware or API
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007740 * does not expose a compatible presentation timestamp.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007741 *
7742 * \ingroup compositor
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007743 */
7744WL_EXPORT int
7745weston_compositor_set_presentation_clock_software(
7746 struct weston_compositor *compositor)
7747{
7748 /* In order of preference */
7749 static const clockid_t clocks[] = {
7750 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
7751 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
7752 CLOCK_MONOTONIC, /* no jumps, may crawl */
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007753 };
7754 unsigned i;
7755
7756 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
7757 if (weston_compositor_set_presentation_clock(compositor,
7758 clocks[i]) == 0)
7759 return 0;
7760
7761 weston_log("Error: no suitable presentation clock available.\n");
7762
7763 return -1;
7764}
7765
Pekka Paalanen662f3842015-03-18 12:17:26 +02007766/** Read the current time from the Presentation clock
7767 *
7768 * \param compositor
Marius Vlada2dace22019-06-12 16:05:44 +03007769 * \param[out] ts The current time.
Pekka Paalanen662f3842015-03-18 12:17:26 +02007770 *
7771 * \note Reading the current time in user space is always imprecise to some
7772 * degree.
7773 *
7774 * This function is never meant to fail. If reading the clock does fail,
7775 * an error message is logged and a zero time is returned. Callers are not
7776 * supposed to detect or react to failures.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007777 *
7778 * \ingroup compositor
Pekka Paalanen662f3842015-03-18 12:17:26 +02007779 */
7780WL_EXPORT void
7781weston_compositor_read_presentation_clock(
7782 const struct weston_compositor *compositor,
7783 struct timespec *ts)
7784{
7785 static bool warned;
7786 int ret;
7787
7788 ret = clock_gettime(compositor->presentation_clock, ts);
7789 if (ret < 0) {
7790 ts->tv_sec = 0;
7791 ts->tv_nsec = 0;
7792
7793 if (!warned)
7794 weston_log("Error: failure to read "
Antonio Borneo39578632019-04-26 23:57:31 +02007795 "the presentation clock %#x: '%s' (%d)\n",
7796 compositor->presentation_clock,
7797 strerror(errno), errno);
Pekka Paalanen662f3842015-03-18 12:17:26 +02007798 warned = true;
7799 }
7800}
7801
Pekka Paalanen230f3b12014-09-29 14:18:40 -04007802/** Import dmabuf buffer into current renderer
7803 *
7804 * \param compositor
7805 * \param buffer the dmabuf buffer to import
7806 * \return true on usable buffers, false otherwise
7807 *
7808 * This function tests that the linux_dmabuf_buffer is usable
7809 * for the current renderer. Returns false on unusable buffers. Usually
7810 * usability is tested by importing the dmabufs for composition.
7811 *
7812 * This hook is also used for detecting if the renderer supports
7813 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
7814 * supported.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007815 *
7816 * \ingroup compositor
7817 */
Pekka Paalanen230f3b12014-09-29 14:18:40 -04007818WL_EXPORT bool
7819weston_compositor_import_dmabuf(struct weston_compositor *compositor,
7820 struct linux_dmabuf_buffer *buffer)
7821{
7822 struct weston_renderer *renderer;
7823
7824 renderer = compositor->renderer;
7825
7826 if (renderer->import_dmabuf == NULL)
7827 return false;
7828
7829 return renderer->import_dmabuf(compositor, buffer);
7830}
7831
Marius Vlad5a701542019-11-16 20:26:52 +02007832WL_EXPORT bool
7833weston_compositor_dmabuf_can_scanout(struct weston_compositor *compositor,
7834 struct linux_dmabuf_buffer *buffer)
7835{
7836 struct weston_backend *backend = compositor->backend;
7837
7838 if (backend->can_scanout_dmabuf == NULL)
7839 return false;
7840
7841 return backend->can_scanout_dmabuf(compositor, buffer);
7842}
7843
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007844WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05007845weston_version(int *major, int *minor, int *micro)
7846{
7847 *major = WESTON_VERSION_MAJOR;
7848 *minor = WESTON_VERSION_MINOR;
7849 *micro = WESTON_VERSION_MICRO;
7850}
7851
Daniel Stonee03c1112016-11-24 20:45:45 +00007852/**
7853 * Attempts to find a module path from the module map specified in the
7854 * environment. If found, writes the full path into the path variable.
7855 *
7856 * The module map is a string in environment variable WESTON_MODULE_MAP, where
7857 * each entry is of the form "name=path" and entries are separated by
7858 * semicolons. Whitespace is significant.
7859 *
7860 * \param name The name to search for.
7861 * \param path Where the path is written to if found.
7862 * \param path_len Allocated bytes at \c path .
7863 * \returns The length of the string written to path on success, or 0 if the
7864 * module was not specified in the environment map or path_len was too small.
7865 */
7866WL_EXPORT size_t
7867weston_module_path_from_env(const char *name, char *path, size_t path_len)
7868{
7869 const char *mapping = getenv("WESTON_MODULE_MAP");
7870 const char *end;
7871 const int name_len = strlen(name);
7872
7873 if (!mapping)
7874 return 0;
7875
7876 end = mapping + strlen(mapping);
7877 while (mapping < end && *mapping) {
7878 const char *filename, *next;
7879
7880 /* early out: impossibly short string */
7881 if (end - mapping < name_len + 1)
7882 return 0;
7883
7884 filename = &mapping[name_len + 1];
7885 next = strchrnul(mapping, ';');
7886
7887 if (strncmp(mapping, name, name_len) == 0 &&
7888 mapping[name_len] == '=') {
7889 size_t file_len = next - filename; /* no trailing NUL */
7890 if (file_len >= path_len)
7891 return 0;
7892 strncpy(path, filename, file_len);
7893 path[file_len] = '\0';
7894 return file_len;
7895 }
7896
7897 mapping = next + 1;
7898 }
7899
7900 return 0;
7901}
7902
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03007903WL_EXPORT void *
7904weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007905{
7906 char path[PATH_MAX];
7907 void *module, *init;
Daniel Stonebeb97e52016-11-28 12:13:54 +00007908 size_t len;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007909
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08007910 if (name == NULL)
7911 return NULL;
7912
Derek Foreman3f86e502015-06-08 11:46:54 -05007913 if (name[0] != '/') {
Daniel Stonee03c1112016-11-24 20:45:45 +00007914 len = weston_module_path_from_env(name, path, sizeof path);
7915 if (len == 0)
Daniel Stonebeb97e52016-11-28 12:13:54 +00007916 len = snprintf(path, sizeof path, "%s/%s",
7917 LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05007918 } else {
Daniel Stonebeb97e52016-11-28 12:13:54 +00007919 len = snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05007920 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007921
Daniel Stonebeb97e52016-11-28 12:13:54 +00007922 /* snprintf returns the length of the string it would've written,
7923 * _excluding_ the NUL byte. So even being equal to the size of
7924 * our buffer is an error here. */
7925 if (len >= sizeof path)
7926 return NULL;
7927
Kristian Høgsberga6813d22012-09-12 12:21:01 -04007928 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
7929 if (module) {
7930 weston_log("Module '%s' already loaded\n", path);
Pekka Paalanenf696ee92019-11-04 14:07:51 +02007931 } else {
7932 weston_log("Loading module '%s'\n", path);
7933 module = dlopen(path, RTLD_NOW);
7934 if (!module) {
7935 weston_log("Failed to load module: %s\n", dlerror());
7936 return NULL;
7937 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007938 }
7939
7940 init = dlsym(module, entrypoint);
7941 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03007942 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00007943 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007944 return NULL;
7945 }
7946
7947 return init;
7948}
7949
Pekka Paalanen6ffbba32019-11-06 12:59:32 +02007950/** Add a compositor destroy listener only once
7951 *
7952 * \param compositor The compositor whose destroy to watch for.
7953 * \param listener The listener struct to initialize.
7954 * \param destroy_handler The callback when compositor is destroyed.
7955 * \return True if listener is added, or false if there already is a listener
7956 * with the given \c destroy_handler.
7957 *
7958 * This function does nothing and returns false if the given callback function
7959 * is already present in the weston_compositor destroy callbacks list.
7960 * Otherwise, this function initializes the given listener with the given
7961 * callback pointer and adds it to the compositor's destroy callbacks list.
7962 *
7963 * This can be used to ensure that plugin initialization is done only once
7964 * in case the same plugin is loaded multiple times. If this function returns
7965 * false, the plugin should be already initialized successfully.
7966 *
7967 * All plugins should register a destroy listener for cleaning up. Note, that
7968 * the plugin destruction order is not guaranteed: plugins that depend on other
7969 * plugins must be able to be torn down in arbitrary order.
7970 *
Leandro Ribeiroca640d52020-01-27 19:12:01 -03007971 * \sa weston_compositor_destroy
Pekka Paalanen6ffbba32019-11-06 12:59:32 +02007972 */
7973WL_EXPORT bool
7974weston_compositor_add_destroy_listener_once(struct weston_compositor *compositor,
7975 struct wl_listener *listener,
7976 wl_notify_func_t destroy_handler)
7977{
7978 if (wl_signal_get(&compositor->destroy_signal, destroy_handler))
7979 return false;
7980
7981 listener->notify = destroy_handler;
7982 wl_signal_add(&compositor->destroy_signal, listener);
7983 return true;
7984}
Giulio Camuffo459137b2014-10-11 23:56:24 +03007985
Leandro Ribeiroca640d52020-01-27 19:12:01 -03007986/** Destroys the compositor.
Giulio Camuffo459137b2014-10-11 23:56:24 +03007987 *
Leandro Ribeiroca640d52020-01-27 19:12:01 -03007988 * This function cleans up the compositor state and then destroys it.
Giulio Camuffo459137b2014-10-11 23:56:24 +03007989 *
Leandro Ribeiroca640d52020-01-27 19:12:01 -03007990 * @param compositor The compositor to be destroyed.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007991 *
Marius Vlad284d5342019-06-24 12:00:47 +03007992 * @ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007993 */
7994WL_EXPORT void
Leandro Ribeiroca640d52020-01-27 19:12:01 -03007995weston_compositor_destroy(struct weston_compositor *compositor)
Giulio Camuffo459137b2014-10-11 23:56:24 +03007996{
7997 /* prevent further rendering while shutting down */
7998 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
7999
8000 wl_signal_emit(&compositor->destroy_signal, compositor);
8001
8002 weston_compositor_xkb_destroy(compositor);
8003
Giulio Camuffo2d24e642015-10-03 16:25:15 +03008004 if (compositor->backend)
8005 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02008006
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03008007 /* The backend is responsible for destroying the heads. */
8008 assert(wl_list_empty(&compositor->head_list));
8009
Pekka Paalanen827b5d22016-06-29 11:54:26 +02008010 weston_plugin_api_destroy_list(compositor);
8011
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03008012 if (compositor->heads_changed_source)
8013 wl_event_source_remove(compositor->heads_changed_source);
8014
Leandro Ribeirof0149642019-12-18 15:52:18 -03008015 weston_log_scope_destroy(compositor->debug_scene);
Daniel Stonece62cb32018-07-20 09:46:24 +01008016 compositor->debug_scene = NULL;
Marius Vladda104eb2019-09-05 14:31:01 +03008017
Leandro Ribeirof0149642019-12-18 15:52:18 -03008018 weston_log_scope_destroy(compositor->timeline);
Marius Vladda104eb2019-09-05 14:31:01 +03008019 compositor->timeline = NULL;
Pekka Paalanena5630ea2017-10-12 13:13:42 +02008020
Giulio Camuffo459137b2014-10-11 23:56:24 +03008021 free(compositor);
8022}
8023
8024/** Instruct the compositor to exit.
8025 *
8026 * This functions does not directly destroy the compositor object, it merely
8027 * command it to start the tear down process. It is not guaranteed that the
8028 * tear down will happen immediately.
8029 *
8030 * \param compositor The compositor to tear down.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008031 *
8032 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03008033 */
8034WL_EXPORT void
8035weston_compositor_exit(struct weston_compositor *compositor)
8036{
8037 compositor->exit(compositor);
8038}
8039
8040/** Return the user data stored in the compositor.
8041 *
8042 * This function returns the user data pointer set with user_data parameter
8043 * to the \ref weston_compositor_create function.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008044 *
8045 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03008046 */
8047WL_EXPORT void *
8048weston_compositor_get_user_data(struct weston_compositor *compositor)
8049{
8050 return compositor->user_data;
8051}
Pekka Paalanendd186732016-06-03 14:49:54 +03008052
Pekka Paalanen50dbf382016-06-03 15:23:46 +03008053static const char * const backend_map[] = {
8054 [WESTON_BACKEND_DRM] = "drm-backend.so",
8055 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
8056 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
8057 [WESTON_BACKEND_RDP] = "rdp-backend.so",
8058 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
8059 [WESTON_BACKEND_X11] = "x11-backend.so",
8060};
8061
Pekka Paalanendd186732016-06-03 14:49:54 +03008062/** Load a backend into a weston_compositor
8063 *
8064 * A backend must be loaded to make a weston_compositor work. A backend
8065 * provides input and output capabilities, and determines the renderer to use.
8066 *
8067 * \param compositor A compositor that has not had a backend loaded yet.
8068 * \param backend Name of the backend file.
8069 * \param config_base A pointer to a backend-specific configuration
8070 * structure's 'base' member.
8071 *
8072 * \return 0 on success, or -1 on error.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008073 *
8074 * \ingroup compositor
Pekka Paalanendd186732016-06-03 14:49:54 +03008075 */
8076WL_EXPORT int
8077weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03008078 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03008079 struct weston_backend_config *config_base)
8080{
8081 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03008082 struct weston_backend_config *config_base);
8083
Pekka Paalanend7e35112017-08-29 17:04:12 +03008084 if (compositor->backend) {
8085 weston_log("Error: attempt to load a backend when one is already loaded\n");
8086 return -1;
8087 }
8088
Quentin Glidic887c0182016-07-10 11:00:53 +02008089 if (backend >= ARRAY_LENGTH(backend_map))
Pekka Paalanen50dbf382016-06-03 15:23:46 +03008090 return -1;
8091
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01008092 backend_init = weston_load_module(backend_map[backend], "weston_backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03008093 if (!backend_init)
8094 return -1;
8095
Pekka Paalanend7e35112017-08-29 17:04:12 +03008096 if (backend_init(compositor, config_base) < 0) {
8097 compositor->backend = NULL;
8098 return -1;
8099 }
8100
8101 return 0;
Pekka Paalanendd186732016-06-03 14:49:54 +03008102}
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008103
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008104/** weston_compositor_load_xwayland
8105 * \ingroup compositor
8106 */
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008107WL_EXPORT int
8108weston_compositor_load_xwayland(struct weston_compositor *compositor)
8109{
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01008110 int (*module_init)(struct weston_compositor *ec);
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008111
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01008112 module_init = weston_load_module("xwayland.so", "weston_module_init");
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008113 if (!module_init)
8114 return -1;
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01008115 if (module_init(compositor) < 0)
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008116 return -1;
8117 return 0;
8118}
Vasilis Liaskovitis486b4632018-10-10 16:14:55 +02008119
8120/** Resolve an internal compositor error by disconnecting the client.
8121 *
8122 * This function is used in cases when the wl_buffer turns out
8123 * unusable and there is no fallback path.
8124 *
8125 * It is possible the fault is caused by a compositor bug, the underlying
8126 * graphics stack bug or normal behaviour, or perhaps a client mistake.
8127 * In any case, the options are to either composite garbage or nothing,
8128 * or disconnect the client. This is a helper function for the latter.
8129 *
8130 * The error is sent as an INVALID_OBJECT error on the client's wl_display.
8131 *
8132 * \param buffer The weston buffer that is unusable.
8133 * \param msg A custom error message attached to the protocol error.
8134 */
8135WL_EXPORT void
8136weston_buffer_send_server_error(struct weston_buffer *buffer,
8137 const char *msg)
8138{
8139 struct wl_client *client;
8140 struct wl_resource *display_resource;
8141 uint32_t id;
8142
8143 assert(buffer->resource);
8144 id = wl_resource_get_id(buffer->resource);
8145 client = wl_resource_get_client(buffer->resource);
8146 display_resource = wl_client_get_object(client, 1);
8147
8148 assert(display_resource);
8149 wl_resource_post_error(display_resource,
8150 WL_DISPLAY_ERROR_INVALID_OBJECT,
8151 "server error with "
8152 "wl_buffer@%u: %s", id, msg);
8153}
Ankit Nautiyal93dde242019-07-08 11:46:42 +05308154
8155WL_EXPORT void
8156weston_output_disable_planes_incr(struct weston_output *output)
8157{
8158 output->disable_planes++;
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05308159 /*
8160 * If disable_planes changes from 0 to non-zero, it means some type of
8161 * recording of content has started, and therefore protection level of
8162 * the protected surfaces must be updated to avoid the recording of
8163 * the protected content.
8164 */
8165 if (output->disable_planes == 1)
8166 weston_schedule_surface_protection_update(output->compositor);
Ankit Nautiyal93dde242019-07-08 11:46:42 +05308167}
8168
8169WL_EXPORT void
8170weston_output_disable_planes_decr(struct weston_output *output)
8171{
8172 output->disable_planes--;
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05308173 /*
8174 * If disable_planes changes from non-zero to 0, it means no content
8175 * recording is going on any more, and the protected and surfaces can be
8176 * shown without any apprehensions about content being recorded.
8177 */
8178 if (output->disable_planes == 0)
8179 weston_schedule_surface_protection_update(output->compositor);
8180
Ankit Nautiyal93dde242019-07-08 11:46:42 +05308181}