blob: 89f438a27f270d6c39189598f951a758208a7fda [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 Paalanen925788f2018-04-19 14:20:01 +03004 * Copyright © 2012-2018 Collabora, Ltd.
5 * 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
Jason Ekstranda7af7042013-10-12 22:38:11 -050096weston_compositor_build_view_list(struct weston_compositor *compositor);
Rob Bradford27b17932013-06-26 18:08:46 +010097
Pekka Paalanendcac3512017-12-08 14:13:34 +020098static char *
99weston_output_create_heads_string(struct weston_output *output);
100
Pekka Paalanen6528c032017-03-28 15:27:10 +0300101/** Send wl_output events for mode and scale changes
102 *
103 * \param head Send on all resources bound to this head.
104 * \param mode_changed If true, send the current mode.
105 * \param scale_changed If true, send the current scale.
106 */
107static void
108weston_mode_switch_send_events(struct weston_head *head,
109 bool mode_changed, bool scale_changed)
110{
111 struct weston_output *output = head->output;
112 struct wl_resource *resource;
113 int version;
114
115 wl_resource_for_each(resource, &head->resource_list) {
116 if (mode_changed) {
117 wl_output_send_mode(resource,
118 output->current_mode->flags,
119 output->current_mode->width,
120 output->current_mode->height,
121 output->current_mode->refresh);
122 }
123
124 version = wl_resource_get_version(resource);
125 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION && scale_changed)
126 wl_output_send_scale(resource, output->current_scale);
127
128 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
129 wl_output_send_done(resource);
130 }
Roman Gilge97391c2019-03-29 13:01:06 +0100131 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
132 zxdg_output_v1_send_logical_position(resource,
133 output->x,
134 output->y);
135 zxdg_output_v1_send_logical_size(resource,
136 output->width,
137 output->height);
138 zxdg_output_v1_send_done(resource);
139 }
Pekka Paalanen6528c032017-03-28 15:27:10 +0300140}
141
142static void
143weston_mode_switch_finish(struct weston_output *output,
144 int mode_changed, int scale_changed)
Alex Wu2dda6042012-04-17 17:20:47 +0800145{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200146 struct weston_seat *seat;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300147 struct weston_head *head;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200148 pixman_region32_t old_output_region;
Alexander Larsson355748e2013-05-28 16:23:38 +0200149
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200150 pixman_region32_init(&old_output_region);
151 pixman_region32_copy(&old_output_region, &output->region);
152
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200153 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +0200154 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200155
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200156 pixman_region32_init_rect(&output->region, output->x, output->y,
157 output->width, output->height);
158
159 weston_output_update_matrix(output);
160
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200161 /* If a pointer falls outside the outputs new geometry, move it to its
162 * lower-right corner */
163 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500164 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200165 int32_t x, y;
166
167 if (!pointer)
168 continue;
169
170 x = wl_fixed_to_int(pointer->x);
171 y = wl_fixed_to_int(pointer->y);
172
173 if (!pixman_region32_contains_point(&old_output_region,
174 x, y, NULL) ||
175 pixman_region32_contains_point(&output->region,
176 x, y, NULL))
177 continue;
178
179 if (x >= output->x + output->width)
180 x = output->x + output->width - 1;
181 if (y >= output->y + output->height)
182 y = output->y + output->height - 1;
183
184 pointer->x = wl_fixed_from_int(x);
185 pointer->y = wl_fixed_from_int(y);
186 }
187
188 pixman_region32_fini(&old_output_region);
189
Derek Foremandd4cd332014-11-10 10:29:59 -0600190 if (!mode_changed && !scale_changed)
191 return;
192
Hardening57388e42013-09-18 23:56:36 +0200193 /* notify clients of the changes */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300194 wl_list_for_each(head, &output->head_list, output_link)
195 weston_mode_switch_send_events(head,
196 mode_changed, scale_changed);
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600197}
198
David Fort0de859e2016-05-27 23:22:57 +0200199static void
200weston_compositor_reflow_outputs(struct weston_compositor *compositor,
201 struct weston_output *resized_output, int delta_width);
202
Marius Vlad55d87362019-06-11 01:15:35 +0300203/**
204 * \ingroup output
205 */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600206WL_EXPORT int
207weston_output_mode_set_native(struct weston_output *output,
208 struct weston_mode *mode,
209 int32_t scale)
210{
211 int ret;
212 int mode_changed = 0, scale_changed = 0;
David Fort0de859e2016-05-27 23:22:57 +0200213 int32_t old_width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600214
215 if (!output->switch_mode)
216 return -1;
217
218 if (!output->original_mode) {
219 mode_changed = 1;
220 ret = output->switch_mode(output, mode);
221 if (ret < 0)
222 return ret;
223 if (output->current_scale != scale) {
224 scale_changed = 1;
225 output->current_scale = scale;
Hardening57388e42013-09-18 23:56:36 +0200226 }
227 }
228
David Fort0de859e2016-05-27 23:22:57 +0200229 old_width = output->width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600230 output->native_mode = mode;
231 output->native_scale = scale;
232
233 weston_mode_switch_finish(output, mode_changed, scale_changed);
234
David Fort0de859e2016-05-27 23:22:57 +0200235 if (mode_changed || scale_changed) {
236 weston_compositor_reflow_outputs(output->compositor, output, output->width - old_width);
237
238 wl_signal_emit(&output->compositor->output_resized_signal, output);
239 }
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600240 return 0;
241}
242
Marius Vlad55d87362019-06-11 01:15:35 +0300243/**
244 * \ingroup output
245 */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600246WL_EXPORT int
247weston_output_mode_switch_to_native(struct weston_output *output)
248{
249 int ret;
250 int mode_changed = 0, scale_changed = 0;
251
252 if (!output->switch_mode)
253 return -1;
254
255 if (!output->original_mode) {
256 weston_log("already in the native mode\n");
257 return -1;
258 }
259 /* the non fullscreen clients haven't seen a mode set since we
260 * switched into a temporary, so we need to notify them if the
261 * mode at that time is different from the native mode now.
262 */
263 mode_changed = (output->original_mode != output->native_mode);
264 scale_changed = (output->original_scale != output->native_scale);
265
266 ret = output->switch_mode(output, output->native_mode);
267 if (ret < 0)
268 return ret;
269
270 output->current_scale = output->native_scale;
271
272 output->original_mode = NULL;
273 output->original_scale = 0;
274
275 weston_mode_switch_finish(output, mode_changed, scale_changed);
276
277 return 0;
278}
279
Marius Vlad55d87362019-06-11 01:15:35 +0300280/**
281 * \ingroup output
282 */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600283WL_EXPORT int
284weston_output_mode_switch_to_temporary(struct weston_output *output,
285 struct weston_mode *mode,
286 int32_t scale)
287{
288 int ret;
289
290 if (!output->switch_mode)
291 return -1;
292
293 /* original_mode is the last mode non full screen clients have seen,
294 * so we shouldn't change it if we already have one set.
295 */
296 if (!output->original_mode) {
297 output->original_mode = output->native_mode;
298 output->original_scale = output->native_scale;
299 }
300 ret = output->switch_mode(output, mode);
301 if (ret < 0)
302 return ret;
303
304 output->current_scale = scale;
305
306 weston_mode_switch_finish(output, 0, 0);
307
308 return 0;
Alex Wu2dda6042012-04-17 17:20:47 +0800309}
310
Benjamin Franzke06286262011-05-06 19:12:33 +0200311static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300312region_init_infinite(pixman_region32_t *region)
313{
314 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
315 UINT32_MAX, UINT32_MAX);
316}
317
Pekka Paalanene67858b2013-04-25 13:57:42 +0300318static struct weston_subsurface *
319weston_surface_to_subsurface(struct weston_surface *surface);
320
Jason Ekstranda7af7042013-10-12 22:38:11 -0500321WL_EXPORT struct weston_view *
322weston_view_create(struct weston_surface *surface)
323{
324 struct weston_view *view;
325
Bryce Harringtonde16d892014-11-20 22:21:57 -0800326 view = zalloc(sizeof *view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500327 if (view == NULL)
328 return NULL;
329
330 view->surface = surface;
Daniel Stonefb4869d2016-12-09 16:27:54 +0000331 view->plane = &surface->compositor->primary_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500332
Jason Ekstranda7af7042013-10-12 22:38:11 -0500333 /* Assign to surface */
334 wl_list_insert(&surface->views, &view->surface_link);
335
336 wl_signal_init(&view->destroy_signal);
337 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300338 wl_list_init(&view->layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500339
Jason Ekstranda7af7042013-10-12 22:38:11 -0500340 pixman_region32_init(&view->clip);
341
342 view->alpha = 1.0;
343 pixman_region32_init(&view->transform.opaque);
344
345 wl_list_init(&view->geometry.transformation_list);
346 wl_list_insert(&view->geometry.transformation_list,
347 &view->transform.position.link);
348 weston_matrix_init(&view->transform.position.matrix);
349 wl_list_init(&view->geometry.child_list);
Pekka Paalanen380adf52015-02-16 14:39:11 +0200350 pixman_region32_init(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500351 pixman_region32_init(&view->transform.boundingbox);
352 view->transform.dirty = 1;
353
Jason Ekstranda7af7042013-10-12 22:38:11 -0500354 return view;
355}
356
Jason Ekstrand108865d2014-06-26 10:04:49 -0700357struct weston_frame_callback {
358 struct wl_resource *resource;
359 struct wl_list link;
360};
361
Pekka Paalanen133e4392014-09-23 22:08:46 -0400362struct weston_presentation_feedback {
363 struct wl_resource *resource;
364
365 /* XXX: could use just wl_resource_get_link() instead */
366 struct wl_list link;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200367
368 /* The per-surface feedback flags */
369 uint32_t psf_flags;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400370};
371
372static void
373weston_presentation_feedback_discard(
374 struct weston_presentation_feedback *feedback)
375{
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200376 wp_presentation_feedback_send_discarded(feedback->resource);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400377 wl_resource_destroy(feedback->resource);
378}
379
380static void
381weston_presentation_feedback_discard_list(struct wl_list *list)
382{
383 struct weston_presentation_feedback *feedback, *tmp;
384
385 wl_list_for_each_safe(feedback, tmp, list, link)
386 weston_presentation_feedback_discard(feedback);
387}
388
389static void
390weston_presentation_feedback_present(
391 struct weston_presentation_feedback *feedback,
392 struct weston_output *output,
393 uint32_t refresh_nsec,
394 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200395 uint64_t seq,
396 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400397{
398 struct wl_client *client = wl_resource_get_client(feedback->resource);
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300399 struct weston_head *head;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400400 struct wl_resource *o;
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200401 uint32_t tv_sec_hi;
402 uint32_t tv_sec_lo;
403 uint32_t tv_nsec;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300404 bool done = false;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400405
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300406 wl_list_for_each(head, &output->head_list, output_link) {
407 wl_resource_for_each(o, &head->resource_list) {
408 if (wl_resource_get_client(o) != client)
409 continue;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400410
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300411 wp_presentation_feedback_send_sync_output(feedback->resource, o);
412 done = true;
413 }
414
415 /* For clone mode, send it for just one wl_output global,
416 * they are all equivalent anyway.
417 */
418 if (done)
419 break;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400420 }
421
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200422 timespec_to_proto(ts, &tv_sec_hi, &tv_sec_lo, &tv_nsec);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200423 wp_presentation_feedback_send_presented(feedback->resource,
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200424 tv_sec_hi, tv_sec_lo, tv_nsec,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200425 refresh_nsec,
426 seq >> 32, seq & 0xffffffff,
427 flags | feedback->psf_flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400428 wl_resource_destroy(feedback->resource);
429}
430
431static void
432weston_presentation_feedback_present_list(struct wl_list *list,
433 struct weston_output *output,
434 uint32_t refresh_nsec,
435 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200436 uint64_t seq,
437 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400438{
439 struct weston_presentation_feedback *feedback, *tmp;
440
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200441 assert(!(flags & WP_PRESENTATION_FEEDBACK_INVALID) ||
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200442 wl_list_empty(list));
443
Pekka Paalanen133e4392014-09-23 22:08:46 -0400444 wl_list_for_each_safe(feedback, tmp, list, link)
445 weston_presentation_feedback_present(feedback, output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200446 refresh_nsec, ts, seq,
447 flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400448}
449
Jason Ekstrand7b982072014-05-20 14:33:03 -0500450static void
451surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
452{
453 struct weston_surface_state *state =
454 container_of(listener, struct weston_surface_state,
455 buffer_destroy_listener);
456
457 state->buffer = NULL;
458}
459
460static void
461weston_surface_state_init(struct weston_surface_state *state)
462{
463 state->newly_attached = 0;
464 state->buffer = NULL;
465 state->buffer_destroy_listener.notify =
466 surface_state_handle_buffer_destroy;
467 state->sx = 0;
468 state->sy = 0;
469
Derek Foreman152254b2015-11-26 14:17:48 -0600470 pixman_region32_init(&state->damage_surface);
471 pixman_region32_init(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500472 pixman_region32_init(&state->opaque);
473 region_init_infinite(&state->input);
474
475 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400476 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500477
478 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
479 state->buffer_viewport.buffer.scale = 1;
480 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
481 state->buffer_viewport.surface.width = -1;
482 state->buffer_viewport.changed = 0;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300483
484 state->acquire_fence_fd = -1;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +0530485
486 state->desired_protection = WESTON_HDCP_DISABLE;
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +0530487 state->protection_mode = WESTON_SURFACE_PROTECTION_MODE_RELAXED;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500488}
489
490static void
491weston_surface_state_fini(struct weston_surface_state *state)
492{
493 struct weston_frame_callback *cb, *next;
494
495 wl_list_for_each_safe(cb, next,
496 &state->frame_callback_list, link)
497 wl_resource_destroy(cb->resource);
498
Pekka Paalanen133e4392014-09-23 22:08:46 -0400499 weston_presentation_feedback_discard_list(&state->feedback_list);
500
Jason Ekstrand7b982072014-05-20 14:33:03 -0500501 pixman_region32_fini(&state->input);
502 pixman_region32_fini(&state->opaque);
Derek Foreman152254b2015-11-26 14:17:48 -0600503 pixman_region32_fini(&state->damage_surface);
504 pixman_region32_fini(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500505
506 if (state->buffer)
507 wl_list_remove(&state->buffer_destroy_listener.link);
508 state->buffer = NULL;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300509
510 fd_clear(&state->acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +0300511 weston_buffer_release_reference(&state->buffer_release_ref, NULL);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500512}
513
514static void
515weston_surface_state_set_buffer(struct weston_surface_state *state,
516 struct weston_buffer *buffer)
517{
518 if (state->buffer == buffer)
519 return;
520
521 if (state->buffer)
522 wl_list_remove(&state->buffer_destroy_listener.link);
523 state->buffer = buffer;
524 if (state->buffer)
525 wl_signal_add(&state->buffer->destroy_signal,
526 &state->buffer_destroy_listener);
527}
528
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500529WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500530weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500531{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500532 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400533
Bryce Harringtonde16d892014-11-20 22:21:57 -0800534 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400535 if (surface == NULL)
536 return NULL;
537
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500538 wl_signal_init(&surface->destroy_signal);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +0800539 wl_signal_init(&surface->commit_signal);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500540
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500541 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200542 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400543
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200544 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
545 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200546 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
547 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500548
549 weston_surface_state_init(&surface->pending);
550
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400551 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500552 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300553 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400554
Jason Ekstranda7af7042013-10-12 22:38:11 -0500555 wl_list_init(&surface->views);
556
557 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400558 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500559
Pekka Paalanene67858b2013-04-25 13:57:42 +0300560 wl_list_init(&surface->subsurface_list);
561 wl_list_init(&surface->subsurface_list_pending);
562
Jason Ekstrand1e059042014-10-16 10:55:19 -0500563 weston_matrix_init(&surface->buffer_to_surface_matrix);
564 weston_matrix_init(&surface->surface_to_buffer_matrix);
565
Jonas Ådahld3414f22016-07-22 17:56:31 +0800566 wl_list_init(&surface->pointer_constraints);
567
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300568 surface->acquire_fence_fd = -1;
569
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +0530570 surface->desired_protection = WESTON_HDCP_DISABLE;
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +0530571 surface->current_protection = WESTON_HDCP_DISABLE;
572 surface->protection_mode = WESTON_SURFACE_PROTECTION_MODE_RELAXED;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +0530573
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400574 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500575}
576
Alex Wu8811bf92012-02-28 18:07:54 +0800577WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500578weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200579 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500580{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100581 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Philipp Zabel195dade2018-09-03 19:44:59 +0200582 surface->is_opaque = !(alpha < 1.0);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500583}
584
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400585WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500586weston_view_to_global_float(struct weston_view *view,
587 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200588{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500589 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200590 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
591
Jason Ekstranda7af7042013-10-12 22:38:11 -0500592 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200593
594 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200595 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700596 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200597 v.f[3]);
598 *x = 0;
599 *y = 0;
600 return;
601 }
602
603 *x = v.f[0] / v.f[3];
604 *y = v.f[1] / v.f[3];
605 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500606 *x = sx + view->geometry.x;
607 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200608 }
609}
610
Pekka Paalanen0df44772020-02-14 14:50:03 +0200611/** Transform a point to buffer coordinates
612 *
613 * \param width Surface width.
614 * \param height Surface height.
615 * \param transform Buffer transform.
616 * \param scale Buffer scale.
617 * \param sx Surface x coordinate of a point.
618 * \param sy Surface y coordinate of a point.
619 * \param[out] bx Buffer x coordinate of the point.
620 * \param[out] by Buffer Y coordinate of the point.
621 *
622 * Converts the given surface-local coordinates to buffer coordinates
623 * according to the given buffer transform and scale.
624 * This ignores wp_viewport.
625 *
626 * The given width and height must be the result of inverse scaled and
627 * inverse transformed buffer size.
628 */
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500629WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200630weston_transformed_coord(int width, int height,
631 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200632 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200633 float sx, float sy, float *bx, float *by)
634{
635 switch (transform) {
636 case WL_OUTPUT_TRANSFORM_NORMAL:
637 default:
638 *bx = sx;
639 *by = sy;
640 break;
641 case WL_OUTPUT_TRANSFORM_FLIPPED:
642 *bx = width - sx;
643 *by = sy;
644 break;
645 case WL_OUTPUT_TRANSFORM_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200646 *bx = sy;
647 *by = width - sx;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200648 break;
649 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200650 *bx = sy;
651 *by = sx;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200652 break;
653 case WL_OUTPUT_TRANSFORM_180:
654 *bx = width - sx;
655 *by = height - sy;
656 break;
657 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
658 *bx = sx;
659 *by = height - sy;
660 break;
661 case WL_OUTPUT_TRANSFORM_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200662 *bx = height - sy;
663 *by = sx;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200664 break;
665 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200666 *bx = height - sy;
667 *by = width - sx;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200668 break;
669 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200670
671 *bx *= scale;
672 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200673}
674
Pekka Paalanen0df44772020-02-14 14:50:03 +0200675/** Transform a rectangle to buffer coordinates
676 *
677 * \param width Surface width.
678 * \param height Surface height.
679 * \param transform Buffer transform.
680 * \param scale Buffer scale.
681 * \param rect Rectangle in surface coordinates.
682 * \return Rectangle in buffer coordinates.
683 *
684 * Converts the given surface-local rectangle to buffer coordinates
685 * according to the given buffer transform and scale. The resulting
686 * rectangle is guaranteed to be well-formed.
687 * This ignores wp_viewport.
688 *
689 * The given width and height must be the result of inverse scaled and
690 * inverse transformed buffer size.
691 */
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200692WL_EXPORT pixman_box32_t
693weston_transformed_rect(int width, int height,
694 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200695 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200696 pixman_box32_t rect)
697{
698 float x1, x2, y1, y2;
699
700 pixman_box32_t ret;
701
Alexander Larsson4ea95522013-05-22 14:41:37 +0200702 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200703 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200704 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200705 rect.x2, rect.y2, &x2, &y2);
706
707 if (x1 <= x2) {
708 ret.x1 = x1;
709 ret.x2 = x2;
710 } else {
711 ret.x1 = x2;
712 ret.x2 = x1;
713 }
714
715 if (y1 <= y2) {
716 ret.y1 = y1;
717 ret.y2 = y2;
718 } else {
719 ret.y1 = y2;
720 ret.y2 = y1;
721 }
722
723 return ret;
724}
725
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600726/** Transform a region by a matrix, restricted to axis-aligned transformations
727 *
728 * Warning: This function does not work for projective, affine, or matrices
729 * that encode arbitrary rotations. Only 90-degree step rotations are
730 * supported.
731 */
732WL_EXPORT void
733weston_matrix_transform_region(pixman_region32_t *dest,
734 struct weston_matrix *matrix,
735 pixman_region32_t *src)
736{
737 pixman_box32_t *src_rects, *dest_rects;
738 int nrects, i;
739
740 src_rects = pixman_region32_rectangles(src, &nrects);
741 dest_rects = malloc(nrects * sizeof(*dest_rects));
742 if (!dest_rects)
743 return;
744
745 for (i = 0; i < nrects; i++) {
746 struct weston_vector vec1 = {{
747 src_rects[i].x1, src_rects[i].y1, 0, 1
748 }};
749 weston_matrix_transform(matrix, &vec1);
750 vec1.f[0] /= vec1.f[3];
751 vec1.f[1] /= vec1.f[3];
752
753 struct weston_vector vec2 = {{
754 src_rects[i].x2, src_rects[i].y2, 0, 1
755 }};
756 weston_matrix_transform(matrix, &vec2);
757 vec2.f[0] /= vec2.f[3];
758 vec2.f[1] /= vec2.f[3];
759
760 if (vec1.f[0] < vec2.f[0]) {
761 dest_rects[i].x1 = floor(vec1.f[0]);
762 dest_rects[i].x2 = ceil(vec2.f[0]);
763 } else {
764 dest_rects[i].x1 = floor(vec2.f[0]);
765 dest_rects[i].x2 = ceil(vec1.f[0]);
766 }
767
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600768 if (vec1.f[1] < vec2.f[1]) {
769 dest_rects[i].y1 = floor(vec1.f[1]);
770 dest_rects[i].y2 = ceil(vec2.f[1]);
771 } else {
772 dest_rects[i].y1 = floor(vec2.f[1]);
773 dest_rects[i].y2 = ceil(vec1.f[1]);
774 }
775 }
776
777 pixman_region32_clear(dest);
778 pixman_region32_init_rects(dest, dest_rects, nrects);
779 free(dest_rects);
780}
781
Pekka Paalanen0df44772020-02-14 14:50:03 +0200782/** Transform a region to buffer coordinates
783 *
784 * \param width Surface width.
785 * \param height Surface height.
786 * \param transform Buffer transform.
787 * \param scale Buffer scale.
788 * \param[in] src Region in surface coordinates.
789 * \param[out] dest Resulting region in buffer coordinates.
790 *
791 * Converts the given surface-local region to buffer coordinates
792 * according to the given buffer transform and scale.
793 * This ignores wp_viewport.
794 *
795 * The given width and height must be the result of inverse scaled and
796 * inverse transformed buffer size.
797 *
798 * src and dest are allowed to point to the same memory for in-place conversion.
799 */
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200800WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500801weston_transformed_region(int width, int height,
802 enum wl_output_transform transform,
803 int32_t scale,
804 pixman_region32_t *src, pixman_region32_t *dest)
805{
806 pixman_box32_t *src_rects, *dest_rects;
807 int nrects, i;
808
809 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
810 if (src != dest)
811 pixman_region32_copy(dest, src);
812 return;
813 }
814
815 src_rects = pixman_region32_rectangles(src, &nrects);
816 dest_rects = malloc(nrects * sizeof(*dest_rects));
817 if (!dest_rects)
818 return;
819
820 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
821 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
822 } else {
823 for (i = 0; i < nrects; i++) {
824 switch (transform) {
825 default:
826 case WL_OUTPUT_TRANSFORM_NORMAL:
827 dest_rects[i].x1 = src_rects[i].x1;
828 dest_rects[i].y1 = src_rects[i].y1;
829 dest_rects[i].x2 = src_rects[i].x2;
830 dest_rects[i].y2 = src_rects[i].y2;
831 break;
832 case WL_OUTPUT_TRANSFORM_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200833 dest_rects[i].x1 = src_rects[i].y1;
834 dest_rects[i].y1 = width - src_rects[i].x2;
835 dest_rects[i].x2 = src_rects[i].y2;
836 dest_rects[i].y2 = width - src_rects[i].x1;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500837 break;
838 case WL_OUTPUT_TRANSFORM_180:
839 dest_rects[i].x1 = width - src_rects[i].x2;
840 dest_rects[i].y1 = height - src_rects[i].y2;
841 dest_rects[i].x2 = width - src_rects[i].x1;
842 dest_rects[i].y2 = height - src_rects[i].y1;
843 break;
844 case WL_OUTPUT_TRANSFORM_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200845 dest_rects[i].x1 = height - src_rects[i].y2;
846 dest_rects[i].y1 = src_rects[i].x1;
847 dest_rects[i].x2 = height - src_rects[i].y1;
848 dest_rects[i].y2 = src_rects[i].x2;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500849 break;
850 case WL_OUTPUT_TRANSFORM_FLIPPED:
851 dest_rects[i].x1 = width - src_rects[i].x2;
852 dest_rects[i].y1 = src_rects[i].y1;
853 dest_rects[i].x2 = width - src_rects[i].x1;
854 dest_rects[i].y2 = src_rects[i].y2;
855 break;
856 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200857 dest_rects[i].x1 = src_rects[i].y1;
858 dest_rects[i].y1 = src_rects[i].x1;
859 dest_rects[i].x2 = src_rects[i].y2;
860 dest_rects[i].y2 = src_rects[i].x2;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500861 break;
862 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
863 dest_rects[i].x1 = src_rects[i].x1;
864 dest_rects[i].y1 = height - src_rects[i].y2;
865 dest_rects[i].x2 = src_rects[i].x2;
866 dest_rects[i].y2 = height - src_rects[i].y1;
867 break;
868 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200869 dest_rects[i].x1 = height - src_rects[i].y2;
870 dest_rects[i].y1 = width - src_rects[i].x2;
871 dest_rects[i].x2 = height - src_rects[i].y1;
872 dest_rects[i].y2 = width - src_rects[i].x1;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500873 break;
874 }
875 }
876 }
877
878 if (scale != 1) {
879 for (i = 0; i < nrects; i++) {
880 dest_rects[i].x1 *= scale;
881 dest_rects[i].x2 *= scale;
882 dest_rects[i].y1 *= scale;
883 dest_rects[i].y2 *= scale;
884 }
885 }
886
887 pixman_region32_clear(dest);
888 pixman_region32_init_rects(dest, dest_rects, nrects);
889 free(dest_rects);
890}
891
Jonny Lamb74130762013-11-26 18:19:46 +0100892static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300893viewport_surface_to_buffer(struct weston_surface *surface,
894 float sx, float sy, float *bx, float *by)
Jonny Lamb74130762013-11-26 18:19:46 +0100895{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200896 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200897 double src_width, src_height;
898 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200899
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200900 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
901 if (vp->surface.width == -1) {
902 *bx = sx;
903 *by = sy;
904 return;
905 }
Jonny Lamb74130762013-11-26 18:19:46 +0100906
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200907 src_x = 0.0;
908 src_y = 0.0;
909 src_width = surface->width_from_buffer;
910 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100911 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200912 src_x = wl_fixed_to_double(vp->buffer.src_x);
913 src_y = wl_fixed_to_double(vp->buffer.src_y);
914 src_width = wl_fixed_to_double(vp->buffer.src_width);
915 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100916 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200917
918 *bx = sx * src_width / surface->width + src_x;
919 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100920}
921
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500922WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200923weston_surface_to_buffer_float(struct weston_surface *surface,
924 float sx, float sy, float *bx, float *by)
925{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200926 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
927
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300928 /* first transform coordinates if the viewport is set */
929 viewport_surface_to_buffer(surface, sx, sy, bx, by);
Jonny Lamb74130762013-11-26 18:19:46 +0100930
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500931 weston_transformed_coord(surface->width_from_buffer,
932 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200933 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100934 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200935}
936
Derek Foreman99739672015-12-03 16:38:11 -0600937/** Transform a rectangle from surface coordinates to buffer coordinates
938 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300939 * \param surface The surface to fetch wp_viewport and buffer transformation
Derek Foreman99739672015-12-03 16:38:11 -0600940 * from.
941 * \param rect The rectangle to transform.
942 * \return The transformed rectangle.
943 *
944 * Viewport and buffer transformations can only do translation, scaling,
945 * and rotations in 90-degree steps. Therefore the only loss in the
946 * conversion is coordinate rounding.
947 *
948 * However, some coordinate rounding takes place as an intermediate
949 * step before the buffer scale factor is applied, so the rectangle
950 * boundary may not be exactly as expected.
951 *
952 * This is OK for damage tracking since a little extra coverage is
953 * not a problem.
954 */
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200955WL_EXPORT pixman_box32_t
956weston_surface_to_buffer_rect(struct weston_surface *surface,
957 pixman_box32_t rect)
958{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200959 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100960 float xf, yf;
961
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300962 /* first transform box coordinates if the viewport is set */
963 viewport_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100964 rect.x1 = floorf(xf);
965 rect.y1 = floorf(yf);
966
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300967 viewport_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
Derek Foremane2e15ac2015-12-01 13:00:43 -0600968 rect.x2 = ceilf(xf);
969 rect.y2 = ceilf(yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100970
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500971 return weston_transformed_rect(surface->width_from_buffer,
972 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200973 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200974 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200975}
976
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200977/** Transform a region from surface coordinates to buffer coordinates
978 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300979 * \param surface The surface to fetch wp_viewport and buffer transformation
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200980 * from.
Marius Vlada2dace22019-06-12 16:05:44 +0300981 * \param[in] surface_region The region in surface coordinates.
982 * \param[out] buffer_region The region converted to buffer coordinates.
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200983 *
984 * Buffer_region must be init'd, but will be completely overwritten.
985 *
986 * Viewport and buffer transformations can only do translation, scaling,
987 * and rotations in 90-degree steps. Therefore the only loss in the
Derek Foreman99739672015-12-03 16:38:11 -0600988 * conversion is from the coordinate rounding that takes place in
989 * \ref weston_surface_to_buffer_rect.
Marius Vlada2dace22019-06-12 16:05:44 +0300990 *
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200991 */
992WL_EXPORT void
993weston_surface_to_buffer_region(struct weston_surface *surface,
994 pixman_region32_t *surface_region,
995 pixman_region32_t *buffer_region)
996{
997 pixman_box32_t *src_rects, *dest_rects;
998 int nrects, i;
999
1000 src_rects = pixman_region32_rectangles(surface_region, &nrects);
1001 dest_rects = malloc(nrects * sizeof(*dest_rects));
1002 if (!dest_rects)
1003 return;
1004
1005 for (i = 0; i < nrects; i++) {
1006 dest_rects[i] = weston_surface_to_buffer_rect(surface,
1007 src_rects[i]);
1008 }
1009
1010 pixman_region32_fini(buffer_region);
1011 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
1012 free(dest_rects);
1013}
1014
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +02001015WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001016weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001017 struct weston_plane *plane)
1018{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001019 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001020 return;
1021
Jason Ekstranda7af7042013-10-12 22:38:11 -05001022 weston_view_damage_below(view);
1023 view->plane = plane;
1024 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001025}
1026
Pekka Paalanen51723d52015-02-17 13:10:01 +02001027/** Inflict damage on the plane where the view is visible.
1028 *
1029 * \param view The view that causes the damage.
1030 *
1031 * If the view is currently on a plane (including the primary plane),
1032 * take the view's boundingbox, subtract all the opaque views that cover it,
1033 * and add the remaining region as damage to the plane. This corresponds
1034 * to the damage inflicted to the plane if this view disappeared.
1035 *
1036 * A repaint is scheduled for this view.
1037 *
1038 * The region of all opaque views covering this view is stored in
1039 * weston_view::clip and updated by view_accumulate_damage() during
1040 * weston_output_repaint(). Specifically, that region matches the
1041 * scenegraph as it was last painted.
1042 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001043WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001044weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001045{
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001046 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001047
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001048 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001049 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001050 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +08001051 if (view->plane)
1052 pixman_region32_union(&view->plane->damage,
1053 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001054 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -08001055 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001056}
1057
Pekka Paalanen01e00682017-03-24 16:21:06 +02001058/** Send wl_surface.enter/leave events
1059 *
1060 * \param surface The surface.
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03001061 * \param head A head of the entered/left output.
Pekka Paalanen01e00682017-03-24 16:21:06 +02001062 * \param enter True if entered.
Marius Vlada2dace22019-06-12 16:05:44 +03001063 * \param leave True if left.
Pekka Paalanen01e00682017-03-24 16:21:06 +02001064 *
1065 * Send the enter/leave events for all protocol objects bound to the given
1066 * output by the client owning the surface.
1067 */
1068static void
1069weston_surface_send_enter_leave(struct weston_surface *surface,
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03001070 struct weston_head *head,
Pekka Paalanen01e00682017-03-24 16:21:06 +02001071 bool enter,
1072 bool leave)
1073{
1074 struct wl_resource *wloutput;
1075 struct wl_client *client;
1076
1077 assert(enter != leave);
1078
1079 client = wl_resource_get_client(surface->resource);
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03001080 wl_resource_for_each(wloutput, &head->resource_list) {
Pekka Paalanen01e00682017-03-24 16:21:06 +02001081 if (wl_resource_get_client(wloutput) != client)
1082 continue;
1083
1084 if (enter)
1085 wl_surface_send_enter(surface->resource, wloutput);
1086 if (leave)
1087 wl_surface_send_leave(surface->resource, wloutput);
1088 }
1089}
1090
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301091static void
1092weston_surface_compute_protection(struct protected_surface *psurface)
1093{
1094 enum weston_hdcp_protection min_protection;
1095 bool min_protection_valid = false;
1096 struct weston_surface *surface = psurface->surface;
1097 struct weston_output *output;
1098
1099 wl_list_for_each(output, &surface->compositor->output_list, link)
1100 if (surface->output_mask & (1u << output->id)) {
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05301101 /*
1102 * If the content-protection is enabled with protection
1103 * mode as RELAXED for a surface, and if
1104 * content-recording features like: screen-shooter,
1105 * recorder, screen-sharing, etc are on, then notify the
1106 * client, that the protection is disabled.
1107 *
1108 * Note: If the protection mode is ENFORCED then there
1109 * is no need to bother the client as the renderer takes
1110 * care of censoring the visibility of the protected
1111 * content.
1112 */
1113
1114 if (output->disable_planes > 0 &&
1115 surface->protection_mode == WESTON_SURFACE_PROTECTION_MODE_RELAXED) {
1116 min_protection = WESTON_HDCP_DISABLE;
1117 min_protection_valid = true;
1118 break;
1119 }
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301120 if (!min_protection_valid) {
1121 min_protection = output->current_protection;
1122 min_protection_valid = true;
1123 }
1124 if (output->current_protection < min_protection)
1125 min_protection = output->current_protection;
1126 }
1127 if (!min_protection_valid)
1128 min_protection = WESTON_HDCP_DISABLE;
1129
1130 surface->current_protection = min_protection;
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05301131
1132 weston_protected_surface_send_event(psurface, surface->current_protection);
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301133}
1134
1135static void
1136notify_surface_protection_change(void *data)
1137{
1138 struct weston_compositor *compositor = data;
1139 struct content_protection *cp;
1140 struct protected_surface *psurface;
1141
1142 cp = compositor->content_protection;
1143 cp->surface_protection_update = NULL;
1144
1145 /* Notify the clients, whose surfaces are changed */
1146 wl_list_for_each(psurface, &cp->protected_list, link)
1147 if (psurface && psurface->surface)
1148 weston_surface_compute_protection(psurface);
1149}
1150
Bryce Harrington3f650b82015-12-23 11:01:58 -08001151/**
Ankit Nautiyalfaa5ab42019-07-08 13:23:24 +05301152 * \param compositor weston_compositor
1153 *
1154 * Schedule an idle task to notify surface about the update in protection,
1155 * if not already scheduled.
1156 */
1157static void
1158weston_schedule_surface_protection_update(struct weston_compositor *compositor)
1159{
1160 struct content_protection *cp = compositor->content_protection;
1161 struct wl_event_loop *loop;
1162
1163 if (!cp || cp->surface_protection_update)
1164 return;
1165 loop = wl_display_get_event_loop(compositor->wl_display);
1166 cp->surface_protection_update = wl_event_loop_add_idle(loop,
1167 notify_surface_protection_change,
1168 compositor);
1169}
1170
1171/**
Bryce Harrington3f650b82015-12-23 11:01:58 -08001172 * \param es The surface
1173 * \param mask The new set of outputs for the surface
1174 *
1175 * Sets the surface's set of outputs to the ones specified by
1176 * the new output mask provided. Identifies the outputs that
1177 * have changed, the posts enter and leave events for these
1178 * outputs as appropriate.
1179 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001180static void
1181weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
1182{
1183 uint32_t different = es->output_mask ^ mask;
1184 uint32_t entered = mask & different;
1185 uint32_t left = es->output_mask & different;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001186 uint32_t output_bit;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001187 struct weston_output *output;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001188 struct weston_head *head;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001189
1190 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001191 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001192 return;
1193 if (different == 0)
1194 return;
1195
1196 wl_list_for_each(output, &es->compositor->output_list, link) {
Pekka Paalanen01e00682017-03-24 16:21:06 +02001197 output_bit = 1u << output->id;
1198 if (!(output_bit & different))
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001199 continue;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001200
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001201 wl_list_for_each(head, &output->head_list, output_link) {
1202 weston_surface_send_enter_leave(es, head,
1203 output_bit & entered,
1204 output_bit & left);
1205 }
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001206 }
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301207 /*
1208 * Change in surfaces' output mask might trigger a change in its
1209 * protection.
1210 */
Ankit Nautiyalfaa5ab42019-07-08 13:23:24 +05301211 weston_schedule_surface_protection_update(es->compositor);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001212}
1213
Semi Malinene7a52fb2018-04-26 11:08:10 +02001214static void
1215notify_view_output_destroy(struct wl_listener *listener, void *data)
1216{
1217 struct weston_view *view =
1218 container_of(listener,
1219 struct weston_view, output_destroy_listener);
1220
1221 view->output = NULL;
1222 view->output_destroy_listener.notify = NULL;
1223}
1224
1225/** Set the primary output of the view
1226 *
1227 * \param view The view whose primary output to set
1228 * \param output The new primary output for the view
1229 *
1230 * Set \a output to be the primary output of the \a view.
1231 *
1232 * Notice that the assignment may be temporary; the primary output could be
1233 * automatically changed. Hence, one cannot rely on the value persisting.
1234 *
1235 * Passing NULL as /a output will set the primary output to NULL.
1236 */
1237WL_EXPORT void
1238weston_view_set_output(struct weston_view *view, struct weston_output *output)
1239{
1240 if (view->output_destroy_listener.notify) {
1241 wl_list_remove(&view->output_destroy_listener.link);
1242 view->output_destroy_listener.notify = NULL;
1243 }
1244 view->output = output;
1245 if (output) {
1246 view->output_destroy_listener.notify =
1247 notify_view_output_destroy;
1248 wl_signal_add(&output->destroy_signal,
1249 &view->output_destroy_listener);
1250 }
1251}
1252
Bryce Harrington3f650b82015-12-23 11:01:58 -08001253/** Recalculate which output(s) the surface has views displayed on
1254 *
1255 * \param es The surface to remap to outputs
1256 *
1257 * Finds the output that is showing the largest amount of one
1258 * of the surface's various views. This output becomes the
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001259 * surface's primary output for vsync and frame callback purposes.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001260 *
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001261 * Also notes all outputs of all of the surface's views
Bryce Harrington3f650b82015-12-23 11:01:58 -08001262 * in the output_mask for the surface.
1263 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001264static void
1265weston_surface_assign_output(struct weston_surface *es)
1266{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001267 struct weston_output *new_output;
1268 struct weston_view *view;
1269 pixman_region32_t region;
1270 uint32_t max, area, mask;
1271 pixman_box32_t *e;
1272
1273 new_output = NULL;
1274 max = 0;
1275 mask = 0;
1276 pixman_region32_init(&region);
1277 wl_list_for_each(view, &es->views, surface_link) {
1278 if (!view->output)
1279 continue;
1280
1281 pixman_region32_intersect(&region, &view->transform.boundingbox,
1282 &view->output->region);
1283
1284 e = pixman_region32_extents(&region);
1285 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1286
1287 mask |= view->output_mask;
1288
1289 if (area >= max) {
1290 new_output = view->output;
1291 max = area;
1292 }
1293 }
1294 pixman_region32_fini(&region);
1295
1296 es->output = new_output;
1297 weston_surface_update_output_mask(es, mask);
1298}
1299
Bryce Harrington3f650b82015-12-23 11:01:58 -08001300/** Recalculate which output(s) the view is displayed on
1301 *
1302 * \param ev The view to remap to outputs
1303 *
1304 * Identifies the set of outputs that the view is visible on,
1305 * noting them into the output_mask. The output that the view
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001306 * is most visible on is set as the view's primary output.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001307 *
1308 * Also does the same for the view's surface. See
1309 * weston_surface_assign_output().
1310 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001311static void
1312weston_view_assign_output(struct weston_view *ev)
1313{
1314 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001315 struct weston_output *output, *new_output;
1316 pixman_region32_t region;
1317 uint32_t max, area, mask;
1318 pixman_box32_t *e;
1319
1320 new_output = NULL;
1321 max = 0;
1322 mask = 0;
1323 pixman_region32_init(&region);
1324 wl_list_for_each(output, &ec->output_list, link) {
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03001325 if (output->destroying)
1326 continue;
1327
Jason Ekstranda7af7042013-10-12 22:38:11 -05001328 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001329 &output->region);
1330
1331 e = pixman_region32_extents(&region);
1332 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1333
1334 if (area > 0)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001335 mask |= 1u << output->id;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001336
1337 if (area >= max) {
1338 new_output = output;
1339 max = area;
1340 }
1341 }
1342 pixman_region32_fini(&region);
1343
Semi Malinene7a52fb2018-04-26 11:08:10 +02001344 weston_view_set_output(ev, new_output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001345 ev->output_mask = mask;
1346
1347 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001348}
1349
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001350static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001351weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1352 int from_x, int from_y, int *to_x, int *to_y)
1353{
1354 float x, y;
1355
1356 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1357 weston_view_from_global_float(to, x, y, &x, &y);
1358
1359 *to_x = round(x);
1360 *to_y = round(y);
1361}
1362
1363static void
1364weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1365{
1366 pixman_box32_t *a;
1367 pixman_box32_t b;
1368
1369 a = pixman_region32_extents(&from->geometry.scissor);
1370
1371 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1372 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1373
1374 pixman_region32_fini(&to->geometry.scissor);
1375 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1376}
1377
1378static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001379view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001380 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001381{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001382 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1383 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001384 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001385 { inbox->x1, inbox->y1 },
1386 { inbox->x1, inbox->y2 },
1387 { inbox->x2, inbox->y1 },
1388 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001389 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001390 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001391 int i;
1392
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001393 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001394 /* avoid rounding empty bbox to 1x1 */
1395 pixman_region32_init(bbox);
1396 return;
1397 }
1398
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001399 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001400 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001401 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001402 if (x < min_x)
1403 min_x = x;
1404 if (x > max_x)
1405 max_x = x;
1406 if (y < min_y)
1407 min_y = y;
1408 if (y > max_y)
1409 max_y = y;
1410 }
1411
Pekka Paalanen219b9822012-02-08 15:38:37 +02001412 int_x = floorf(min_x);
1413 int_y = floorf(min_y);
1414 pixman_region32_init_rect(bbox, int_x, int_y,
1415 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001416}
1417
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001418static void
Michael Olbrichcb04f982020-06-08 10:22:32 +02001419weston_view_update_transform_scissor(struct weston_view *view,
1420 pixman_region32_t *region)
1421{
1422 struct weston_view *parent = view->geometry.parent;
1423
1424 if (parent) {
1425 if (parent->geometry.scissor_enabled) {
1426 view->geometry.scissor_enabled = true;
1427 weston_view_transfer_scissor(parent, view);
1428 } else {
1429 view->geometry.scissor_enabled = false;
1430 }
1431 }
1432
1433 if (view->geometry.scissor_enabled)
1434 pixman_region32_intersect(region, region,
1435 &view->geometry.scissor);
1436}
1437static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001438weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001439{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001440 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001441
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001442 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001443 view->geometry.x = roundf(view->geometry.x);
1444 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001445
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001446 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001447 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1448 view->transform.position.matrix.d[12] = view->geometry.x;
1449 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001450
Jason Ekstranda7af7042013-10-12 22:38:11 -05001451 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001452
Jason Ekstranda7af7042013-10-12 22:38:11 -05001453 view->transform.inverse = view->transform.position.matrix;
1454 view->transform.inverse.d[12] = -view->geometry.x;
1455 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001456
Jason Ekstranda7af7042013-10-12 22:38:11 -05001457 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001458 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001459 view->surface->width,
1460 view->surface->height);
Michael Olbrichcb04f982020-06-08 10:22:32 +02001461
1462 weston_view_update_transform_scissor(view, &view->transform.boundingbox);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001463
1464 pixman_region32_translate(&view->transform.boundingbox,
1465 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001466
Jason Ekstranda7af7042013-10-12 22:38:11 -05001467 if (view->alpha == 1.0) {
1468 pixman_region32_copy(&view->transform.opaque,
1469 &view->surface->opaque);
Michael Olbrich0e4f0972020-08-19 11:31:03 +02001470 pixman_region32_intersect(&view->transform.opaque,
1471 &view->transform.opaque,
1472 &view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001473 pixman_region32_translate(&view->transform.opaque,
1474 view->geometry.x,
1475 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001476 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001477}
1478
1479static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001480weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001481{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001482 struct weston_view *parent = view->geometry.parent;
1483 struct weston_matrix *matrix = &view->transform.matrix;
1484 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001485 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001486 pixman_region32_t surfregion;
1487 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001488
Jason Ekstranda7af7042013-10-12 22:38:11 -05001489 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001490
1491 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001492 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1493 view->transform.position.matrix.d[12] = view->geometry.x;
1494 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001495
1496 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001497 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001498 weston_matrix_multiply(matrix, &tform->matrix);
1499
Pekka Paalanen483243f2013-03-08 14:56:50 +02001500 if (parent)
1501 weston_matrix_multiply(matrix, &parent->transform.matrix);
1502
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001503 if (weston_matrix_invert(inverse, matrix) < 0) {
1504 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001505 weston_log("error: weston_view %p"
1506 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001507 return -1;
1508 }
1509
Pekka Paalanen380adf52015-02-16 14:39:11 +02001510 pixman_region32_init_rect(&surfregion, 0, 0,
1511 view->surface->width, view->surface->height);
Michael Olbrichcb04f982020-06-08 10:22:32 +02001512
1513 weston_view_update_transform_scissor(view, &surfregion);
1514
Pekka Paalanen380adf52015-02-16 14:39:11 +02001515 surfbox = pixman_region32_extents(&surfregion);
1516
1517 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1518 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001519
Michael Olbrich0e4f0972020-08-19 11:31:03 +02001520 if (view->alpha == 1.0 &&
1521 matrix->type == WESTON_MATRIX_TRANSFORM_TRANSLATE) {
1522 pixman_region32_copy(&view->transform.opaque,
1523 &view->surface->opaque);
1524 pixman_region32_intersect(&view->transform.opaque,
1525 &view->transform.opaque,
1526 &view->geometry.scissor);
1527 pixman_region32_translate(&view->transform.opaque,
1528 matrix->d[12],
1529 matrix->d[13]);
1530 }
1531
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001532 return 0;
1533}
1534
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001535static struct weston_layer *
1536get_view_layer(struct weston_view *view)
1537{
1538 if (view->parent_view)
1539 return get_view_layer(view->parent_view);
1540 return view->layer_link.layer;
1541}
1542
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001543WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001544weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001545{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001546 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001547 struct weston_layer *layer;
1548 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001549
Jason Ekstranda7af7042013-10-12 22:38:11 -05001550 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001551 return;
1552
Pekka Paalanen483243f2013-03-08 14:56:50 +02001553 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001554 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001555
Jason Ekstranda7af7042013-10-12 22:38:11 -05001556 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001557
Jason Ekstranda7af7042013-10-12 22:38:11 -05001558 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001559
Jason Ekstranda7af7042013-10-12 22:38:11 -05001560 pixman_region32_fini(&view->transform.boundingbox);
1561 pixman_region32_fini(&view->transform.opaque);
1562 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001563
Pekka Paalanencd403622012-01-25 13:37:39 +02001564 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001565 if (view->geometry.transformation_list.next ==
1566 &view->transform.position.link &&
1567 view->geometry.transformation_list.prev ==
1568 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001569 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001570 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001571 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001572 if (weston_view_update_transform_enable(view) < 0)
1573 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001574 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001575
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001576 layer = get_view_layer(view);
1577 if (layer) {
1578 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001579 pixman_region32_intersect(&view->transform.boundingbox,
1580 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001581 pixman_region32_intersect(&view->transform.opaque,
1582 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001583 pixman_region32_fini(&mask);
1584 }
1585
Jason Ekstranda7af7042013-10-12 22:38:11 -05001586 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001587
Jason Ekstranda7af7042013-10-12 22:38:11 -05001588 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001589
Jason Ekstranda7af7042013-10-12 22:38:11 -05001590 wl_signal_emit(&view->surface->compositor->transform_signal,
1591 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001592}
1593
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001594WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001595weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001596{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001597 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001598
1599 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001600 * The invariant: if view->geometry.dirty, then all views
1601 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001602 * Corollary: if not parent->geometry.dirty, then all ancestors
1603 * are not dirty.
1604 */
1605
Jason Ekstranda7af7042013-10-12 22:38:11 -05001606 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001607 return;
1608
Jason Ekstranda7af7042013-10-12 22:38:11 -05001609 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001610
Jason Ekstranda7af7042013-10-12 22:38:11 -05001611 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001612 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001613 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001614}
1615
1616WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001617weston_view_to_global_fixed(struct weston_view *view,
1618 wl_fixed_t vx, wl_fixed_t vy,
1619 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001620{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001621 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001622
Jason Ekstranda7af7042013-10-12 22:38:11 -05001623 weston_view_to_global_float(view,
1624 wl_fixed_to_double(vx),
1625 wl_fixed_to_double(vy),
1626 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001627 *x = wl_fixed_from_double(xf);
1628 *y = wl_fixed_from_double(yf);
1629}
1630
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001631WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001632weston_view_from_global_float(struct weston_view *view,
1633 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001634{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001635 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001636 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1637
Jason Ekstranda7af7042013-10-12 22:38:11 -05001638 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001639
1640 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001641 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001642 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001643 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001644 *vx = 0;
1645 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001646 return;
1647 }
1648
Jason Ekstranda7af7042013-10-12 22:38:11 -05001649 *vx = v.f[0] / v.f[3];
1650 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001651 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001652 *vx = x - view->geometry.x;
1653 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001654 }
1655}
1656
1657WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001658weston_view_from_global_fixed(struct weston_view *view,
1659 wl_fixed_t x, wl_fixed_t y,
1660 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001661{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001662 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001663
Jason Ekstranda7af7042013-10-12 22:38:11 -05001664 weston_view_from_global_float(view,
1665 wl_fixed_to_double(x),
1666 wl_fixed_to_double(y),
1667 &vxf, &vyf);
1668 *vx = wl_fixed_from_double(vxf);
1669 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001670}
1671
1672WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001673weston_view_from_global(struct weston_view *view,
1674 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001675{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001676 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001677
Jason Ekstranda7af7042013-10-12 22:38:11 -05001678 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1679 *vx = floorf(vxf);
1680 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001681}
1682
Bryce Harrington3f650b82015-12-23 11:01:58 -08001683/**
1684 * \param surface The surface to be repainted
1685 *
1686 * Marks the output(s) that the surface is shown on as needing to be
1687 * repainted. See weston_output_schedule_repaint().
1688 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001689WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001690weston_surface_schedule_repaint(struct weston_surface *surface)
1691{
1692 struct weston_output *output;
1693
1694 wl_list_for_each(output, &surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001695 if (surface->output_mask & (1u << output->id))
Kristian Høgsberg98238702012-08-03 16:29:12 -04001696 weston_output_schedule_repaint(output);
1697}
1698
Bryce Harrington3f650b82015-12-23 11:01:58 -08001699/**
1700 * \param view The view to be repainted
1701 *
1702 * Marks the output(s) that the view is shown on as needing to be
1703 * repainted. See weston_output_schedule_repaint().
1704 */
Kristian Høgsberg98238702012-08-03 16:29:12 -04001705WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001706weston_view_schedule_repaint(struct weston_view *view)
1707{
1708 struct weston_output *output;
1709
1710 wl_list_for_each(output, &view->surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001711 if (view->output_mask & (1u << output->id))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001712 weston_output_schedule_repaint(output);
1713}
1714
Pekka Paalanene508ce62015-02-19 13:59:55 +02001715/**
1716 * XXX: This function does it the wrong way.
1717 * surface->damage is the damage from the client, and causes
1718 * surface_flush_damage() to copy pixels. No window management action can
1719 * cause damage to the client-provided content, warranting re-upload!
1720 *
1721 * Instead of surface->damage, this function should record the damage
1722 * with all the views for this surface to avoid extraneous texture
1723 * uploads.
1724 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001725WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001726weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001727{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001728 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001729 0, 0, surface->width,
1730 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001731
Kristian Høgsberg98238702012-08-03 16:29:12 -04001732 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001733}
1734
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001735WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001736weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001737{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001738 if (view->geometry.x == x && view->geometry.y == y)
1739 return;
1740
Jason Ekstranda7af7042013-10-12 22:38:11 -05001741 view->geometry.x = x;
1742 view->geometry.y = y;
1743 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001744}
1745
Pekka Paalanen483243f2013-03-08 14:56:50 +02001746static void
1747transform_parent_handle_parent_destroy(struct wl_listener *listener,
1748 void *data)
1749{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001750 struct weston_view *view =
1751 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001752 geometry.parent_destroy_listener);
1753
Jason Ekstranda7af7042013-10-12 22:38:11 -05001754 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001755}
1756
1757WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001758weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001759 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001760{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001761 if (view->geometry.parent) {
1762 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1763 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001764
1765 if (!parent)
1766 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001767 }
1768
Jason Ekstranda7af7042013-10-12 22:38:11 -05001769 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001770
Jason Ekstranda7af7042013-10-12 22:38:11 -05001771 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001772 transform_parent_handle_parent_destroy;
1773 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001774 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001775 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001776 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001777 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001778 }
1779
Jason Ekstranda7af7042013-10-12 22:38:11 -05001780 weston_view_geometry_dirty(view);
1781}
1782
Pekka Paalanen380adf52015-02-16 14:39:11 +02001783/** Set a clip mask rectangle on a view
1784 *
1785 * \param view The view to set the clip mask on.
1786 * \param x Top-left corner X coordinate of the clip rectangle.
1787 * \param y Top-left corner Y coordinate of the clip rectangle.
1788 * \param width Width of the clip rectangle, non-negative.
1789 * \param height Height of the clip rectangle, non-negative.
1790 *
1791 * A shell may set a clip mask rectangle on a view. Everything outside
1792 * the rectangle is cut away for input and output purposes: it is
1793 * not drawn and cannot be hit by hit-test based input like pointer
1794 * motion or touch-downs. Everything inside the rectangle will behave
1795 * normally. Clients are unaware of clipping.
1796 *
Yong Bakos4c72e292016-04-28 11:59:10 -05001797 * The rectangle is set in surface-local coordinates. Setting a clip
Pekka Paalanen380adf52015-02-16 14:39:11 +02001798 * mask rectangle does not affect the view position, the view is positioned
1799 * as it would be without a clip. The clip also does not change
1800 * weston_surface::width,height.
1801 *
1802 * The clip mask rectangle is part of transformation inheritance
1803 * (weston_view_set_transform_parent()). A clip set in the root of the
1804 * transformation inheritance tree will affect all views in the tree.
1805 * A clip can be set only on the root view. Attempting to set a clip
1806 * on view that has a transformation parent will fail. Assigning a parent
1807 * to a view that has a clip set will cause the clip to be forgotten.
1808 *
1809 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1810 * on the additional transformations in the child views. These transformations
1811 * may not rotate the coordinate axes, i.e., only translation and scaling
1812 * are allowed. Violating this restriction causes the clipping to malfunction.
1813 * Furthermore, using scaling may cause rounding errors in child clipping.
1814 *
1815 * The clip mask rectangle is not automatically adjusted based on
1816 * wl_surface.attach dx and dy arguments.
1817 *
1818 * A clip mask rectangle can be set only if the compositor capability
1819 * WESTON_CAP_VIEW_CLIP_MASK is present.
1820 *
1821 * This function sets the clip mask rectangle and schedules a repaint for
1822 * the view.
1823 */
1824WL_EXPORT void
1825weston_view_set_mask(struct weston_view *view,
1826 int x, int y, int width, int height)
1827{
1828 struct weston_compositor *compositor = view->surface->compositor;
1829
1830 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1831 weston_log("%s not allowed without capability!\n", __func__);
1832 return;
1833 }
1834
1835 if (view->geometry.parent) {
1836 weston_log("view %p has a parent, clip forbidden!\n", view);
1837 return;
1838 }
1839
1840 if (width < 0 || height < 0) {
1841 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1842 x, y, width, height);
1843 return;
1844 }
1845
1846 pixman_region32_fini(&view->geometry.scissor);
1847 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1848 view->geometry.scissor_enabled = true;
1849 weston_view_geometry_dirty(view);
1850 weston_view_schedule_repaint(view);
1851}
1852
1853/** Remove the clip mask from a view
1854 *
1855 * \param view The view to remove the clip mask from.
1856 *
1857 * Removed the clip mask rectangle and schedules a repaint.
1858 *
1859 * \sa weston_view_set_mask
1860 */
1861WL_EXPORT void
1862weston_view_set_mask_infinite(struct weston_view *view)
1863{
1864 view->geometry.scissor_enabled = false;
1865 weston_view_geometry_dirty(view);
1866 weston_view_schedule_repaint(view);
1867}
1868
Armin Krezović0da12b82016-06-30 06:04:33 +02001869/* Check if view should be displayed
1870 *
1871 * The indicator is set manually when assigning
1872 * a view to a surface.
1873 *
1874 * This needs reworking. See the thread starting at:
1875 *
1876 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1877 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001878WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001879weston_view_is_mapped(struct weston_view *view)
1880{
Armin Krezović0da12b82016-06-30 06:04:33 +02001881 return view->is_mapped;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001882}
1883
Philipp Zabel70decd52018-09-03 20:11:15 +02001884/* Check if view is opaque in specified region
1885 *
1886 * \param view The view to check for opacity.
1887 * \param region The region to check for opacity, in view coordinates.
1888 *
1889 * Returns true if the view is opaque in the specified region, because view
1890 * alpha is 1.0 and either the opaque region set by the client contains the
1891 * specified region, or the buffer pixel format or solid color is opaque.
1892 */
1893WL_EXPORT bool
1894weston_view_is_opaque(struct weston_view *ev, pixman_region32_t *region)
1895{
1896 pixman_region32_t r;
1897 bool ret = false;
1898
1899 if (ev->alpha < 1.0)
1900 return false;
1901
1902 if (ev->surface->is_opaque)
1903 return true;
1904
Marius Vlad67b382c2020-08-21 13:57:54 +03001905 if (ev->transform.dirty)
Philipp Zabel70decd52018-09-03 20:11:15 +02001906 return false;
Philipp Zabel70decd52018-09-03 20:11:15 +02001907
1908 pixman_region32_init(&r);
1909 pixman_region32_subtract(&r, region, &ev->transform.opaque);
1910
1911 if (!pixman_region32_not_empty(&r))
1912 ret = true;
1913
1914 pixman_region32_fini(&r);
1915
1916 return ret;
1917}
1918
Marius Vlad5f6bee42019-09-11 16:41:04 +03001919/** Check if the view has a valid buffer available
1920 *
1921 * @param ev The view to check if it has a valid buffer.
1922 *
1923 * Returns true if the view has a valid buffer or false otherwise.
1924 */
1925WL_EXPORT bool
1926weston_view_has_valid_buffer(struct weston_view *ev)
1927{
1928 return ev->surface->buffer_ref.buffer != NULL;
1929}
1930
Marius Vlad47e3d1e2019-09-11 16:53:08 +03001931/** Check if the view matches the entire output
1932 *
1933 * @param ev The view to check.
1934 * @param output The output to check against.
1935 *
1936 * Returns true if the view does indeed matches the entire output.
1937 */
1938WL_EXPORT bool
1939weston_view_matches_output_entirely(struct weston_view *ev,
1940 struct weston_output *output)
1941{
1942 pixman_box32_t *extents =
1943 pixman_region32_extents(&ev->transform.boundingbox);
1944
1945 if (extents->x1 != output->x ||
1946 extents->y1 != output->y ||
1947 extents->x2 != output->x + output->width ||
1948 extents->y2 != output->y + output->height)
1949 return false;
1950
1951 return true;
1952}
1953
Armin Krezović0da12b82016-06-30 06:04:33 +02001954/* Check if a surface has a view assigned to it
1955 *
1956 * The indicator is set manually when mapping
1957 * a surface and creating a view for it.
1958 *
1959 * This needs to go. See the thread starting at:
1960 *
1961 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1962 *
1963 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001964WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001965weston_surface_is_mapped(struct weston_surface *surface)
1966{
Armin Krezović0da12b82016-06-30 06:04:33 +02001967 return surface->is_mapped;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001968}
1969
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001970static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001971surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001972{
1973 struct weston_view *view;
1974
1975 if (surface->width == width && surface->height == height)
1976 return;
1977
1978 surface->width = width;
1979 surface->height = height;
1980
1981 wl_list_for_each(view, &surface->views, surface_link)
1982 weston_view_geometry_dirty(view);
1983}
1984
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001985WL_EXPORT void
1986weston_surface_set_size(struct weston_surface *surface,
1987 int32_t width, int32_t height)
1988{
1989 assert(!surface->resource);
1990 surface_set_size(surface, width, height);
1991}
1992
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001993static int
1994fixed_round_up_to_int(wl_fixed_t f)
1995{
1996 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1997}
1998
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001999static void
Pekka Paalanen59987fa2016-04-26 15:50:59 +03002000convert_size_by_transform_scale(int32_t *width_out, int32_t *height_out,
2001 int32_t width, int32_t height,
2002 uint32_t transform,
2003 int32_t scale)
2004{
2005 assert(scale > 0);
2006
2007 switch (transform) {
2008 case WL_OUTPUT_TRANSFORM_NORMAL:
2009 case WL_OUTPUT_TRANSFORM_180:
2010 case WL_OUTPUT_TRANSFORM_FLIPPED:
2011 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2012 *width_out = width / scale;
2013 *height_out = height / scale;
2014 break;
2015 case WL_OUTPUT_TRANSFORM_90:
2016 case WL_OUTPUT_TRANSFORM_270:
2017 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2018 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2019 *width_out = height / scale;
2020 *height_out = width / scale;
2021 break;
2022 default:
2023 assert(0 && "invalid transform");
2024 }
2025}
2026
2027static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002028weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002029{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002030 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01002031
2032 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02002033 surface->width_from_buffer = 0;
2034 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01002035 return;
2036 }
2037
Pekka Paalanen59987fa2016-04-26 15:50:59 +03002038 convert_size_by_transform_scale(&surface->width_from_buffer,
2039 &surface->height_from_buffer,
2040 surface->buffer_ref.buffer->width,
2041 surface->buffer_ref.buffer->height,
2042 vp->buffer.transform,
2043 vp->buffer.scale);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002044}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02002045
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002046static void
2047weston_surface_update_size(struct weston_surface *surface)
2048{
2049 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
2050 int32_t width, height;
2051
2052 width = surface->width_from_buffer;
2053 height = surface->height_from_buffer;
2054
2055 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02002056 surface_set_size(surface,
2057 vp->surface.width, vp->surface.height);
2058 return;
2059 }
2060
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002061 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03002062 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
2063 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
2064
2065 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02002066 return;
2067 }
2068
Jason Ekstrand5c11a332013-12-04 20:32:03 -06002069 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002070}
2071
Marius Vlad9fdda7f2019-06-11 16:08:55 +03002072/** weston_compositor_get_time
2073 * \ingroup compositor
2074 */
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02002075WL_EXPORT void
2076weston_compositor_get_time(struct timespec *time)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05002077{
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02002078 clock_gettime(CLOCK_REALTIME, time);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05002079}
2080
Marius Vlad9fdda7f2019-06-11 16:08:55 +03002081/** weston_compositor_pick_view
2082 * \ingroup compositor
2083 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002084WL_EXPORT struct weston_view *
2085weston_compositor_pick_view(struct weston_compositor *compositor,
2086 wl_fixed_t x, wl_fixed_t y,
2087 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02002088{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002089 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02002090 wl_fixed_t view_x, view_y;
2091 int view_ix, view_iy;
2092 int ix = wl_fixed_to_int(x);
2093 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02002094
Jason Ekstranda7af7042013-10-12 22:38:11 -05002095 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02002096 if (!pixman_region32_contains_point(
2097 &view->transform.boundingbox, ix, iy, NULL))
2098 continue;
2099
2100 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
2101 view_ix = wl_fixed_to_int(view_x);
2102 view_iy = wl_fixed_to_int(view_y);
2103
2104 if (!pixman_region32_contains_point(&view->surface->input,
2105 view_ix, view_iy, NULL))
2106 continue;
2107
Pekka Paalanen380adf52015-02-16 14:39:11 +02002108 if (view->geometry.scissor_enabled &&
2109 !pixman_region32_contains_point(&view->geometry.scissor,
2110 view_ix, view_iy, NULL))
2111 continue;
2112
Pekka Paalanenfc22a522015-02-18 15:08:29 +02002113 *vx = view_x;
2114 *vy = view_y;
2115 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02002116 }
2117
Derek Foremanf9318d12015-05-11 15:40:11 -05002118 *vx = wl_fixed_from_int(-1000000);
2119 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02002120 return NULL;
2121}
2122
2123static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002124weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002125{
Daniel Stone37816df2012-05-16 18:45:18 +01002126 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002127
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07002128 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002129 return;
2130
Daniel Stone37816df2012-05-16 18:45:18 +01002131 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04002132 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002133}
2134
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002135WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002136weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05002137{
Daniel Stone4dab5db2012-05-30 16:31:53 +01002138 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002139
Jason Ekstranda7af7042013-10-12 22:38:11 -05002140 if (!weston_view_is_mapped(view))
2141 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002142
Jason Ekstranda7af7042013-10-12 22:38:11 -05002143 weston_view_damage_below(view);
Semi Malinene7a52fb2018-04-26 11:08:10 +02002144 weston_view_set_output(view, NULL);
Xiong Zhang97116532013-10-23 13:58:31 +08002145 view->plane = NULL;
Armin Krezovićf8486c32016-06-30 06:04:28 +02002146 view->is_mapped = false;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002147 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002148 wl_list_remove(&view->link);
2149 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002150 view->output_mask = 0;
2151 weston_surface_assign_output(view->surface);
2152
2153 if (weston_surface_is_mapped(view->surface))
2154 return;
2155
2156 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002157 struct weston_touch *touch = weston_seat_get_touch(seat);
2158 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2159 struct weston_keyboard *keyboard =
2160 weston_seat_get_keyboard(seat);
2161
2162 if (keyboard && keyboard->focus == view->surface)
2163 weston_keyboard_set_focus(keyboard, NULL);
2164 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05002165 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05002166 if (touch && touch->focus == view)
2167 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01002168 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002169}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002170
Jason Ekstranda7af7042013-10-12 22:38:11 -05002171WL_EXPORT void
2172weston_surface_unmap(struct weston_surface *surface)
2173{
2174 struct weston_view *view;
2175
Armin Krezovićf8486c32016-06-30 06:04:28 +02002176 surface->is_mapped = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002177 wl_list_for_each(view, &surface->views, surface_link)
2178 weston_view_unmap(view);
2179 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05002180}
2181
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002182static void
2183weston_surface_reset_pending_buffer(struct weston_surface *surface)
2184{
Jason Ekstrand7b982072014-05-20 14:33:03 -05002185 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002186 surface->pending.sx = 0;
2187 surface->pending.sy = 0;
2188 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002189 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002190}
2191
Jason Ekstranda7af7042013-10-12 22:38:11 -05002192WL_EXPORT void
2193weston_view_destroy(struct weston_view *view)
2194{
2195 wl_signal_emit(&view->destroy_signal, view);
2196
2197 assert(wl_list_empty(&view->geometry.child_list));
2198
2199 if (weston_view_is_mapped(view)) {
2200 weston_view_unmap(view);
2201 weston_compositor_build_view_list(view->surface->compositor);
2202 }
2203
2204 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002205 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002206
2207 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02002208 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002209 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002210 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002211
2212 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen944fae82018-05-22 13:15:58 +03002213 weston_view_set_output(view, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002214
Jason Ekstranda7af7042013-10-12 22:38:11 -05002215 wl_list_remove(&view->surface_link);
2216
2217 free(view);
2218}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002219
2220WL_EXPORT void
2221weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05002222{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002223 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002224 struct weston_view *ev, *nv;
Jonas Ådahld3414f22016-07-22 17:56:31 +08002225 struct weston_pointer_constraint *constraint, *next_constraint;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04002226
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02002227 if (--surface->ref_count > 0)
2228 return;
2229
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03002230 assert(surface->resource == NULL);
2231
Pekka Paalanenca790762015-04-17 14:23:38 +03002232 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02002233
Pekka Paalanene67858b2013-04-25 13:57:42 +03002234 assert(wl_list_empty(&surface->subsurface_list_pending));
2235 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02002236
Jason Ekstranda7af7042013-10-12 22:38:11 -05002237 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
2238 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002239
Jason Ekstrand7b982072014-05-20 14:33:03 -05002240 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002241
Pekka Paalanende685b82012-12-04 15:58:12 +02002242 weston_buffer_reference(&surface->buffer_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002243 weston_buffer_release_reference(&surface->buffer_release_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04002244
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02002245 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002246 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002247 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02002248
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002249 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002250 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002251
Pekka Paalanen133e4392014-09-23 22:08:46 -04002252 weston_presentation_feedback_discard_list(&surface->feedback_list);
2253
Jonas Ådahld3414f22016-07-22 17:56:31 +08002254 wl_list_for_each_safe(constraint, next_constraint,
2255 &surface->pointer_constraints,
2256 link)
2257 weston_pointer_constraint_destroy(constraint);
2258
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03002259 fd_clear(&surface->acquire_fence_fd);
2260
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04002261 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05002262}
2263
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002264static void
2265destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08002266{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002267 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08002268
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03002269 assert(surface);
2270
Giulio Camuffo0d379742013-11-15 22:06:15 +01002271 /* Set the resource to NULL, since we don't want to leave a
2272 * dangling pointer if the surface was refcounted and survives
2273 * the weston_surface_destroy() call. */
2274 surface->resource = NULL;
Pekka Paalanen4826f872016-04-22 14:14:38 +03002275
2276 if (surface->viewport_resource)
2277 wl_resource_set_user_data(surface->viewport_resource, NULL);
2278
Alexandros Frantzis27d7c392018-10-19 12:14:11 +03002279 if (surface->synchronization_resource) {
2280 wl_resource_set_user_data(surface->synchronization_resource,
2281 NULL);
2282 }
2283
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002284 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08002285}
2286
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002287static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002288weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
2289{
2290 struct weston_buffer *buffer =
2291 container_of(listener, struct weston_buffer, destroy_listener);
2292
2293 wl_signal_emit(&buffer->destroy_signal, buffer);
2294 free(buffer);
2295}
2296
Giulio Camuffoe058cd12013-12-12 14:14:29 +01002297WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002298weston_buffer_from_resource(struct wl_resource *resource)
2299{
2300 struct weston_buffer *buffer;
2301 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002302
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002303 listener = wl_resource_get_destroy_listener(resource,
2304 weston_buffer_destroy_handler);
2305
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002306 if (listener)
2307 return container_of(listener, struct weston_buffer,
2308 destroy_listener);
2309
2310 buffer = zalloc(sizeof *buffer);
2311 if (buffer == NULL)
2312 return NULL;
2313
2314 buffer->resource = resource;
2315 wl_signal_init(&buffer->destroy_signal);
2316 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04002317 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002318 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002319
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002320 return buffer;
2321}
2322
2323static void
Pekka Paalanende685b82012-12-04 15:58:12 +02002324weston_buffer_reference_handle_destroy(struct wl_listener *listener,
2325 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002326{
Pekka Paalanende685b82012-12-04 15:58:12 +02002327 struct weston_buffer_reference *ref =
2328 container_of(listener, struct weston_buffer_reference,
2329 destroy_listener);
2330
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002331 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02002332 ref->buffer = NULL;
2333}
2334
2335WL_EXPORT void
2336weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002337 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002338{
2339 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05002340 ref->buffer->busy_count--;
2341 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002342 assert(wl_resource_get_client(ref->buffer->resource));
Matt Hoosier3052bc72017-09-26 08:09:40 -05002343 wl_buffer_send_release(ref->buffer->resource);
Kristian Høgsberg20347802013-03-04 12:07:46 -05002344 }
Pekka Paalanende685b82012-12-04 15:58:12 +02002345 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002346 }
2347
Pekka Paalanende685b82012-12-04 15:58:12 +02002348 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002349 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002350 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02002351 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02002352 }
2353
Pekka Paalanende685b82012-12-04 15:58:12 +02002354 ref->buffer = buffer;
2355 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
2356}
2357
2358static void
Alexandros Frantzis67629672018-10-19 12:14:11 +03002359weston_buffer_release_reference_handle_destroy(struct wl_listener *listener,
2360 void *data)
2361{
2362 struct weston_buffer_release_reference *ref =
2363 container_of(listener, struct weston_buffer_release_reference,
2364 destroy_listener);
2365
2366 assert((struct wl_resource *)data == ref->buffer_release->resource);
2367 ref->buffer_release = NULL;
2368}
2369
2370static void
2371weston_buffer_release_destroy(struct weston_buffer_release *buffer_release)
2372{
2373 struct wl_resource *resource = buffer_release->resource;
2374 int release_fence_fd = buffer_release->fence_fd;
2375
2376 if (release_fence_fd >= 0) {
2377 zwp_linux_buffer_release_v1_send_fenced_release(
2378 resource, release_fence_fd);
2379 } else {
2380 zwp_linux_buffer_release_v1_send_immediate_release(
2381 resource);
2382 }
2383
2384 wl_resource_destroy(resource);
2385}
2386
2387WL_EXPORT void
2388weston_buffer_release_reference(struct weston_buffer_release_reference *ref,
2389 struct weston_buffer_release *buffer_release)
2390{
2391 if (buffer_release == ref->buffer_release)
2392 return;
2393
2394 if (ref->buffer_release) {
2395 ref->buffer_release->ref_count--;
2396 wl_list_remove(&ref->destroy_listener.link);
2397 if (ref->buffer_release->ref_count == 0)
2398 weston_buffer_release_destroy(ref->buffer_release);
2399 }
2400
2401 if (buffer_release) {
2402 buffer_release->ref_count++;
2403 wl_resource_add_destroy_listener(buffer_release->resource,
2404 &ref->destroy_listener);
2405 }
2406
2407 ref->buffer_release = buffer_release;
2408 ref->destroy_listener.notify =
2409 weston_buffer_release_reference_handle_destroy;
2410}
2411
2412WL_EXPORT void
2413weston_buffer_release_move(struct weston_buffer_release_reference *dest,
2414 struct weston_buffer_release_reference *src)
2415{
2416 weston_buffer_release_reference(dest, src->buffer_release);
2417 weston_buffer_release_reference(src, NULL);
2418}
2419
2420static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002421weston_surface_attach(struct weston_surface *surface,
2422 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002423{
2424 weston_buffer_reference(&surface->buffer_ref, buffer);
2425
Pekka Paalanena6421c42012-12-04 15:58:10 +02002426 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002427 if (weston_surface_is_mapped(surface))
2428 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002429 }
2430
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002431 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02002432
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002433 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002434 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002435}
2436
Marius Vlad9fdda7f2019-06-11 16:08:55 +03002437/** weston_compositor_damage_all
2438 * \ingroup compositor
2439 */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002440WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002441weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002442{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002443 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002444
2445 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002446 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002447}
2448
Marius Vlad55d87362019-06-11 01:15:35 +03002449/**
2450 * \ingroup output
2451 */
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05002452WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002453weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002454{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002455 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002456
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002457 pixman_region32_union(&compositor->primary_plane.damage,
2458 &compositor->primary_plane.damage,
2459 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002460 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002461}
2462
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04002463static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002464surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002465{
Pekka Paalanende685b82012-12-04 15:58:12 +02002466 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002467 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002468 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002469
Marius Vlad2a1b7862019-09-05 13:12:18 +03002470 if (pixman_region32_not_empty(&surface->damage))
Marius Vlad3203ff62019-09-05 14:56:12 +03002471 TL_POINT(surface->compositor, "core_flush_damage", TLP_SURFACE(surface),
Pekka Paalanenb5026542014-11-12 15:09:24 +02002472 TLP_OUTPUT(surface->output), TLP_END);
2473
Jason Ekstrandef540082014-06-26 10:37:36 -07002474 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002475}
2476
2477static void
2478view_accumulate_damage(struct weston_view *view,
2479 pixman_region32_t *opaque)
2480{
2481 pixman_region32_t damage;
2482
2483 pixman_region32_init(&damage);
2484 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002485 pixman_box32_t *extents;
2486
Jason Ekstranda7af7042013-10-12 22:38:11 -05002487 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002488 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002489 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002490 pixman_region32_copy(&damage, &view->surface->damage);
2491 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002492 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002493 }
2494
Pekka Paalanen380adf52015-02-16 14:39:11 +02002495 pixman_region32_intersect(&damage, &damage,
2496 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002497 pixman_region32_subtract(&damage, &damage, opaque);
2498 pixman_region32_union(&view->plane->damage,
2499 &view->plane->damage, &damage);
2500 pixman_region32_fini(&damage);
2501 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002502 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002503}
2504
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002505static void
Michael Olbrichc5ea4952020-08-11 12:42:35 +02002506output_accumulate_damage(struct weston_output *output)
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002507{
Michael Olbrichc5ea4952020-08-11 12:42:35 +02002508 struct weston_compositor *ec = output->compositor;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002509 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002510 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002511 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002512
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002513 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002514
2515 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002516 pixman_region32_copy(&plane->clip, &clip);
2517
2518 pixman_region32_init(&opaque);
2519
Jason Ekstranda7af7042013-10-12 22:38:11 -05002520 wl_list_for_each(ev, &ec->view_list, link) {
2521 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002522 continue;
2523
Jason Ekstranda7af7042013-10-12 22:38:11 -05002524 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002525 }
2526
2527 pixman_region32_union(&clip, &clip, &opaque);
2528 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002529 }
2530
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002531 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002532
Jason Ekstranda7af7042013-10-12 22:38:11 -05002533 wl_list_for_each(ev, &ec->view_list, link)
Derek Foreman060cf112015-11-18 16:32:26 -06002534 ev->surface->touched = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002535
2536 wl_list_for_each(ev, &ec->view_list, link) {
Michael Olbrichc5ea4952020-08-11 12:42:35 +02002537 /* Ignore views not visible on the current output */
2538 if (!(ev->output_mask & (1u << output->id)))
2539 continue;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002540 if (ev->surface->touched)
2541 continue;
Derek Foreman060cf112015-11-18 16:32:26 -06002542 ev->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002543
2544 surface_flush_damage(ev->surface);
2545
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002546 /* Both the renderer and the backend have seen the buffer
2547 * by now. If renderer needs the buffer, it has its own
2548 * reference set. If the backend wants to keep the buffer
2549 * around for migrating the surface into a non-primary plane
2550 * later, keep_buffer is true. Otherwise, drop the core
2551 * reference now, and allow early buffer release. This enables
2552 * clients to use single-buffering.
2553 */
Alexandros Frantzis67629672018-10-19 12:14:11 +03002554 if (!ev->surface->keep_buffer) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002555 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002556 weston_buffer_release_reference(
2557 &ev->surface->buffer_release_ref, NULL);
2558 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002559 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002560}
2561
2562static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002563surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002564{
2565 struct weston_subsurface *sub;
2566
Pekka Paalanene67858b2013-04-25 13:57:42 +03002567 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002568 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002569 continue;
2570
Jason Ekstranda7af7042013-10-12 22:38:11 -05002571 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2572 wl_list_init(&sub->surface->views);
2573
2574 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002575 }
2576}
2577
2578static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002579surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002580{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002581 struct weston_subsurface *sub;
2582 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002583
Jason Ekstranda7af7042013-10-12 22:38:11 -05002584 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2585 if (sub->surface == surface)
2586 continue;
2587
George Kiagiadakised04d382014-06-13 18:10:26 +02002588 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2589 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002590 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002591 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002592
2593 surface_free_unused_subsurface_views(sub->surface);
2594 }
2595}
2596
2597static void
2598view_list_add_subsurface_view(struct weston_compositor *compositor,
2599 struct weston_subsurface *sub,
2600 struct weston_view *parent)
2601{
2602 struct weston_subsurface *child;
2603 struct weston_view *view = NULL, *iv;
2604
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002605 if (!weston_surface_is_mapped(sub->surface))
2606 return;
2607
Jason Ekstranda7af7042013-10-12 22:38:11 -05002608 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2609 if (iv->geometry.parent == parent) {
2610 view = iv;
2611 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002612 }
2613 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002614
2615 if (view) {
2616 /* Put it back in the surface's list of views */
2617 wl_list_remove(&view->surface_link);
2618 wl_list_insert(&sub->surface->views, &view->surface_link);
2619 } else {
2620 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002621 weston_view_set_position(view,
2622 sub->position.x,
2623 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002624 weston_view_set_transform_parent(view, parent);
2625 }
2626
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002627 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002628 weston_view_update_transform(view);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002629 view->is_mapped = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002630
Pekka Paalanenb188e912013-11-19 14:03:35 +02002631 if (wl_list_empty(&sub->surface->subsurface_list)) {
2632 wl_list_insert(compositor->view_list.prev, &view->link);
2633 return;
2634 }
2635
2636 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2637 if (child->surface == sub->surface)
2638 wl_list_insert(compositor->view_list.prev, &view->link);
2639 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002640 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002641 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002642}
2643
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002644/* This recursively adds the sub-surfaces for a view, relying on the
2645 * sub-surface order. Thus, if a client restacks the sub-surfaces, that
2646 * change first happens to the sub-surface list, and then automatically
2647 * propagates here. See weston_surface_damage_subsurfaces() for how the
2648 * sub-surfaces receive damage when the client changes the state.
2649 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002650static void
2651view_list_add(struct weston_compositor *compositor,
2652 struct weston_view *view)
2653{
2654 struct weston_subsurface *sub;
2655
2656 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002657
Pekka Paalanenb188e912013-11-19 14:03:35 +02002658 if (wl_list_empty(&view->surface->subsurface_list)) {
2659 wl_list_insert(compositor->view_list.prev, &view->link);
2660 return;
2661 }
2662
2663 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2664 if (sub->surface == view->surface)
2665 wl_list_insert(compositor->view_list.prev, &view->link);
2666 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002667 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002668 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002669}
2670
2671static void
2672weston_compositor_build_view_list(struct weston_compositor *compositor)
2673{
Loïc Yhuel267b16e2019-09-17 20:14:56 +02002674 struct weston_view *view, *tmp;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002675 struct weston_layer *layer;
2676
2677 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002678 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002679 surface_stash_subsurface_views(view->surface);
2680
Loïc Yhuel267b16e2019-09-17 20:14:56 +02002681 wl_list_for_each_safe(view, tmp, &compositor->view_list, link)
2682 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002683 wl_list_init(&compositor->view_list);
Loïc Yhuel267b16e2019-09-17 20:14:56 +02002684
Jason Ekstranda7af7042013-10-12 22:38:11 -05002685 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002686 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002687 view_list_add(compositor, view);
2688 }
2689 }
2690
2691 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002692 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002693 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002694}
2695
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002696static void
2697weston_output_take_feedback_list(struct weston_output *output,
2698 struct weston_surface *surface)
2699{
2700 struct weston_view *view;
2701 struct weston_presentation_feedback *feedback;
2702 uint32_t flags = 0xffffffff;
2703
2704 if (wl_list_empty(&surface->feedback_list))
2705 return;
2706
2707 /* All views must have the flag for the flag to survive. */
2708 wl_list_for_each(view, &surface->views, surface_link) {
2709 /* ignore views that are not on this output at all */
2710 if (view->output_mask & (1u << output->id))
2711 flags &= view->psf_flags;
2712 }
2713
2714 wl_list_for_each(feedback, &surface->feedback_list, link)
2715 feedback->psf_flags = flags;
2716
2717 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2718 wl_list_init(&surface->feedback_list);
2719}
2720
David Herrmann1edf44c2013-10-22 17:11:26 +02002721static int
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002722weston_output_repaint(struct weston_output *output, void *repaint_data)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002723{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002724 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002725 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002726 struct weston_animation *animation, *next;
2727 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002728 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002729 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002730 int r;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002731 uint32_t frame_time_msec;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05302732 enum weston_hdcp_protection highest_requested = WESTON_HDCP_DISABLE;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002733
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002734 if (output->destroying)
2735 return 0;
2736
Marius Vlad3203ff62019-09-05 14:56:12 +03002737 TL_POINT(ec, "core_repaint_begin", TLP_OUTPUT(output), TLP_END);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002738
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002739 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002740 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002741
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05302742 /* Find the highest protection desired for an output */
2743 wl_list_for_each(ev, &ec->view_list, link) {
2744 if (ev->surface->output_mask & (1u << output->id)) {
2745 /*
2746 * The desired_protection of the output should be the
2747 * maximum of the desired_protection of the surfaces,
2748 * that are displayed on that output, to avoid
2749 * reducing the protection for existing surfaces.
2750 */
2751 if (ev->surface->desired_protection > highest_requested)
2752 highest_requested =
2753 ev->surface->desired_protection;
2754 }
2755 }
2756
2757 output->desired_protection = highest_requested;
2758
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002759 if (output->assign_planes && !output->disable_planes) {
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002760 output->assign_planes(output, repaint_data);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002761 } else {
2762 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002763 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002764 ev->psf_flags = 0;
2765 }
2766 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002767
Pekka Paalanene67858b2013-04-25 13:57:42 +03002768 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002769 wl_list_for_each(ev, &ec->view_list, link) {
2770 /* Note: This operation is safe to do multiple times on the
2771 * same surface.
2772 */
2773 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002774 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002775 &ev->surface->frame_callback_list);
2776 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002777
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002778 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002779 }
2780 }
2781
Michael Olbrichc5ea4952020-08-11 12:42:35 +02002782 output_accumulate_damage(output);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002783
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002784 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002785 pixman_region32_intersect(&output_damage,
2786 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002787 pixman_region32_subtract(&output_damage,
2788 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002789
Scott Moreauccbf29d2012-02-22 14:21:41 -07002790 if (output->dirty)
2791 weston_output_update_matrix(output);
2792
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002793 r = output->repaint(output, &output_damage, repaint_data);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002794
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002795 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002796
Daniel Stone09a97e22017-03-01 11:34:06 +00002797 output->repaint_needed = false;
Daniel Stone05df8c12017-03-03 16:59:42 +00002798 if (r == 0)
2799 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002800
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002801 weston_compositor_repick(ec);
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002802
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002803 frame_time_msec = timespec_to_msec(&output->frame_time);
2804
Jonas Ådahldb773762012-06-13 00:01:21 +02002805 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002806 wl_callback_send_done(cb->resource, frame_time_msec);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002807 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002808 }
2809
Scott Moreaud64cf212012-06-08 19:40:54 -06002810 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002811 animation->frame_counter++;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002812 animation->frame(animation, output, &output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002813 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002814
Marius Vlad3203ff62019-09-05 14:56:12 +03002815 TL_POINT(ec, "core_repaint_posted", TLP_OUTPUT(output), TLP_END);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002816
David Herrmann1edf44c2013-10-22 17:11:26 +02002817 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002818}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002819
Pekka Paalanen82919792014-05-21 13:51:49 +03002820static void
2821weston_output_schedule_repaint_reset(struct weston_output *output)
2822{
Daniel Stone05df8c12017-03-03 16:59:42 +00002823 output->repaint_status = REPAINT_NOT_SCHEDULED;
Marius Vlad3203ff62019-09-05 14:56:12 +03002824 TL_POINT(output->compositor, "core_repaint_exit_loop",
2825 TLP_OUTPUT(output), TLP_END);
Pekka Paalanen82919792014-05-21 13:51:49 +03002826}
2827
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002828static int
2829weston_output_maybe_repaint(struct weston_output *output, struct timespec *now,
2830 void *repaint_data)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002831{
Pekka Paalanen0513a952014-05-21 16:17:27 +03002832 struct weston_compositor *compositor = output->compositor;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002833 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002834 int64_t msec_to_repaint;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002835
Daniel Stone6847b852017-03-01 11:34:08 +00002836 /* We're not ready yet; come back to make a decision later. */
2837 if (output->repaint_status != REPAINT_SCHEDULED)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002838 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002839
2840 msec_to_repaint = timespec_sub_to_msec(&output->next_repaint, now);
2841 if (msec_to_repaint > 1)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002842 return ret;
Daniel Stone05df8c12017-03-03 16:59:42 +00002843
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002844 /* If we're sleeping, drop the repaint machinery entirely; we will
2845 * explicitly repaint all outputs when we come back. */
2846 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2847 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
2848 goto err;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002849
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002850 /* We don't actually need to repaint this output; drop it from
2851 * repaint until something causes damage. */
2852 if (!output->repaint_needed)
2853 goto err;
2854
2855 /* If repaint fails, we aren't going to get weston_output_finish_frame
2856 * to trigger a new repaint, so drop it from repaint and hope
Daniel Stone6847b852017-03-01 11:34:08 +00002857 * something schedules a successful repaint later. As repainting may
2858 * take some time, re-read our clock as a courtesy to the next
2859 * output. */
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002860 ret = weston_output_repaint(output, repaint_data);
Daniel Stone6847b852017-03-01 11:34:08 +00002861 weston_compositor_read_presentation_clock(compositor, now);
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002862 if (ret != 0)
2863 goto err;
2864
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09002865 output->repainted = true;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002866 return ret;
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002867
2868err:
Pekka Paalanen0513a952014-05-21 16:17:27 +03002869 weston_output_schedule_repaint_reset(output);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002870 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002871}
2872
2873static void
2874output_repaint_timer_arm(struct weston_compositor *compositor)
2875{
2876 struct weston_output *output;
2877 bool any_should_repaint = false;
2878 struct timespec now;
Sergi Granellb4c08862017-03-18 13:01:15 +01002879 int64_t msec_to_next = INT64_MAX;
Daniel Stone6847b852017-03-01 11:34:08 +00002880
2881 weston_compositor_read_presentation_clock(compositor, &now);
2882
2883 wl_list_for_each(output, &compositor->output_list, link) {
2884 int64_t msec_to_this;
2885
2886 if (output->repaint_status != REPAINT_SCHEDULED)
2887 continue;
2888
2889 msec_to_this = timespec_sub_to_msec(&output->next_repaint,
2890 &now);
2891 if (!any_should_repaint || msec_to_this < msec_to_next)
2892 msec_to_next = msec_to_this;
2893
2894 any_should_repaint = true;
2895 }
2896
2897 if (!any_should_repaint)
2898 return;
2899
2900 /* Even if we should repaint immediately, add the minimum 1 ms delay.
2901 * This is a workaround to allow coalescing multiple output repaints
2902 * particularly from weston_output_finish_frame()
2903 * into the same call, which would not happen if we called
2904 * output_repaint_timer_handler() directly.
2905 */
2906 if (msec_to_next < 1)
2907 msec_to_next = 1;
2908
2909 wl_event_source_timer_update(compositor->repaint_timer, msec_to_next);
2910}
2911
2912static int
2913output_repaint_timer_handler(void *data)
2914{
2915 struct weston_compositor *compositor = data;
2916 struct weston_output *output;
2917 struct timespec now;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002918 void *repaint_data = NULL;
Emre Ucane479ed82018-03-20 15:29:40 +01002919 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002920
2921 weston_compositor_read_presentation_clock(compositor, &now);
Pekka Paalanen1ed2cad2021-02-10 12:33:03 +02002922 compositor->last_repaint_start = now;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002923
2924 if (compositor->backend->repaint_begin)
2925 repaint_data = compositor->backend->repaint_begin(compositor);
2926
2927 wl_list_for_each(output, &compositor->output_list, link) {
2928 ret = weston_output_maybe_repaint(output, &now, repaint_data);
2929 if (ret)
2930 break;
2931 }
2932
2933 if (ret == 0) {
Tomohito Esaki09bfcd62018-06-05 10:37:05 +09002934 if (compositor->backend->repaint_flush)
Antonio Borneoc90fccc2019-06-30 15:51:10 +02002935 ret = compositor->backend->repaint_flush(compositor,
2936 repaint_data);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002937 } else {
Antonio Borneoc90fccc2019-06-30 15:51:10 +02002938 if (compositor->backend->repaint_cancel)
2939 compositor->backend->repaint_cancel(compositor,
2940 repaint_data);
2941 }
2942
2943 if (ret != 0) {
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09002944 wl_list_for_each(output, &compositor->output_list, link) {
2945 if (output->repainted)
2946 weston_output_schedule_repaint_reset(output);
2947 }
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002948 }
Daniel Stone6847b852017-03-01 11:34:08 +00002949
Tomohito Esakiddaf95c2018-07-10 11:47:15 +09002950 wl_list_for_each(output, &compositor->output_list, link)
2951 output->repainted = false;
2952
Daniel Stone6847b852017-03-01 11:34:08 +00002953 output_repaint_timer_arm(compositor);
2954
Pekka Paalanen0513a952014-05-21 16:17:27 +03002955 return 0;
2956}
2957
Pekka Paalanen50aa3a72020-05-28 11:34:04 +03002958/** Convert a presentation timestamp to another clock domain
2959 *
2960 * \param compositor The compositor defines the presentation clock domain.
2961 * \param presentation_stamp The timestamp in presentation clock domain.
2962 * \param presentation_now Current time in presentation clock domain.
2963 * \param target_clock Defines the target clock domain.
2964 *
2965 * This approximation relies on presentation_stamp to be close to current time.
2966 * The further it is from current time and the bigger the speed difference
2967 * between the two clock domains, the bigger the conversion error.
2968 *
2969 * Conversion error due to system load is biased and unbounded.
2970 */
2971static struct timespec
2972convert_presentation_time_now(struct weston_compositor *compositor,
2973 const struct timespec *presentation_stamp,
2974 const struct timespec *presentation_now,
2975 clockid_t target_clock)
2976{
2977 struct timespec target_now = {};
2978 struct timespec target_stamp;
2979 int64_t delta_ns;
2980
2981 if (compositor->presentation_clock == target_clock)
2982 return *presentation_stamp;
2983
2984 clock_gettime(target_clock, &target_now);
2985 delta_ns = timespec_sub_to_nsec(presentation_stamp, presentation_now);
2986 timespec_add_nsec(&target_stamp, &target_now, delta_ns);
2987
2988 return target_stamp;
2989}
2990
Marius Vlad55d87362019-06-11 01:15:35 +03002991/**
2992 * \ingroup output
2993 */
Kristian Høgsbergef044142011-06-21 15:02:12 -04002994WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002995weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002996 const struct timespec *stamp,
2997 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002998{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002999 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03003000 int32_t refresh_nsec;
3001 struct timespec now;
Pekka Paalanen50aa3a72020-05-28 11:34:04 +03003002 struct timespec vblank_monotonic;
Daniel Stone6847b852017-03-01 11:34:08 +00003003 int64_t msec_rel;
Pekka Paalanen133e4392014-09-23 22:08:46 -04003004
Daniel Stone05df8c12017-03-03 16:59:42 +00003005 assert(output->repaint_status == REPAINT_AWAITING_COMPLETION);
Pekka Paalanen98b4e202021-05-10 11:33:23 +03003006
3007 /*
3008 * If timestamp of latest vblank is given, it must always go forwards.
3009 * If not given, INVALID flag must be set.
3010 */
3011 if (stamp)
3012 assert(timespec_sub_to_nsec(stamp, &output->frame_time) >= 0);
3013 else
3014 assert(presented_flags & WP_PRESENTATION_FEEDBACK_INVALID);
Daniel Stone3615ce12017-03-01 11:34:05 +00003015
Daniel Stone6847b852017-03-01 11:34:08 +00003016 weston_compositor_read_presentation_clock(compositor, &now);
3017
Daniel Stone3615ce12017-03-01 11:34:05 +00003018 /* If we haven't been supplied any timestamp at all, we don't have a
3019 * timebase to work against, so any delay just wastes time. Push a
3020 * repaint as soon as possible so we can get on with it. */
Daniel Stone6847b852017-03-01 11:34:08 +00003021 if (!stamp) {
3022 output->next_repaint = now;
Daniel Stone3615ce12017-03-01 11:34:05 +00003023 goto out;
Daniel Stone6847b852017-03-01 11:34:08 +00003024 }
Daniel Stone3615ce12017-03-01 11:34:05 +00003025
Pekka Paalanen50aa3a72020-05-28 11:34:04 +03003026 vblank_monotonic = convert_presentation_time_now(compositor,
3027 stamp, &now,
3028 CLOCK_MONOTONIC);
Marius Vlad3203ff62019-09-05 14:56:12 +03003029 TL_POINT(compositor, "core_repaint_finished", TLP_OUTPUT(output),
Pekka Paalanen50aa3a72020-05-28 11:34:04 +03003030 TLP_VBLANK(&vblank_monotonic), TLP_END);
Marius Vladdf9278a2018-03-06 18:56:23 +02003031
Pekka Paalanend7894d02015-07-03 15:08:53 +03003032 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003033 weston_presentation_feedback_present_list(&output->feedback_list,
3034 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02003035 output->msc,
3036 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003037
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02003038 output->frame_time = *stamp;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003039
Daniel Stone6847b852017-03-01 11:34:08 +00003040 timespec_add_nsec(&output->next_repaint, stamp, refresh_nsec);
3041 timespec_add_msec(&output->next_repaint, &output->next_repaint,
3042 -compositor->repaint_msec);
3043 msec_rel = timespec_sub_to_msec(&output->next_repaint, &now);
Daniel Stone84aff5c2017-03-01 11:34:04 +00003044
3045 if (msec_rel < -1000 || msec_rel > 1000) {
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02003046 static bool warned;
3047
3048 if (!warned)
3049 weston_log("Warning: computed repaint delay is "
Daniel Stone6847b852017-03-01 11:34:08 +00003050 "insane: %lld msec\n", (long long) msec_rel);
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02003051 warned = true;
3052
Daniel Stone6847b852017-03-01 11:34:08 +00003053 output->next_repaint = now;
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02003054 }
3055
Mario Kleinerb7df04e2015-06-21 21:25:15 +02003056 /* Called from restart_repaint_loop and restart happens already after
3057 * the deadline given by repaint_msec? In that case we delay until
3058 * the deadline of the next frame, to give clients a more predictable
3059 * timing of the repaint cycle to lock on. */
Daniel Stoneeca5cca2017-02-28 21:53:51 +00003060 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID &&
3061 msec_rel < 0) {
3062 while (timespec_sub_to_nsec(&output->next_repaint, &now) < 0) {
3063 timespec_add_nsec(&output->next_repaint,
3064 &output->next_repaint,
3065 refresh_nsec);
3066 }
3067 }
Mario Kleinerb7df04e2015-06-21 21:25:15 +02003068
Daniel Stone3615ce12017-03-01 11:34:05 +00003069out:
Daniel Stone05df8c12017-03-03 16:59:42 +00003070 output->repaint_status = REPAINT_SCHEDULED;
Daniel Stone6847b852017-03-01 11:34:08 +00003071 output_repaint_timer_arm(compositor);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003072}
3073
3074static void
3075idle_repaint(void *data)
3076{
3077 struct weston_output *output = data;
Antonio Borneoc90fccc2019-06-30 15:51:10 +02003078 int ret;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003079
Daniel Stone05df8c12017-03-03 16:59:42 +00003080 assert(output->repaint_status == REPAINT_BEGIN_FROM_IDLE);
3081 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03003082 output->idle_repaint_source = NULL;
Antonio Borneoc90fccc2019-06-30 15:51:10 +02003083 ret = output->start_repaint_loop(output);
3084 if (ret != 0)
3085 weston_output_schedule_repaint_reset(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003086}
3087
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003088WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003089weston_layer_entry_insert(struct weston_layer_entry *list,
3090 struct weston_layer_entry *entry)
3091{
3092 wl_list_insert(&list->link, &entry->link);
3093 entry->layer = list->layer;
3094}
3095
3096WL_EXPORT void
3097weston_layer_entry_remove(struct weston_layer_entry *entry)
3098{
3099 wl_list_remove(&entry->link);
3100 wl_list_init(&entry->link);
3101 entry->layer = NULL;
3102}
3103
Quentin Glidic82681572016-12-17 13:40:51 +01003104
3105/** Initialize the weston_layer struct.
3106 *
3107 * \param compositor The compositor instance
3108 * \param layer The layer to initialize
3109 */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003110WL_EXPORT void
Quentin Glidic82681572016-12-17 13:40:51 +01003111weston_layer_init(struct weston_layer *layer,
3112 struct weston_compositor *compositor)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003113{
Quentin Glidic82681572016-12-17 13:40:51 +01003114 layer->compositor = compositor;
3115 wl_list_init(&layer->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003116 wl_list_init(&layer->view_list.link);
3117 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003118 weston_layer_set_mask_infinite(layer);
Quentin Glidic82681572016-12-17 13:40:51 +01003119}
3120
3121/** Sets the position of the layer in the layer list. The layer will be placed
3122 * below any layer with the same position value, if any.
3123 * This function is safe to call if the layer is already on the list, but the
3124 * layer may be moved below other layers at the same position, if any.
3125 *
3126 * \param layer The layer to modify
3127 * \param position The position the layer will be placed at
3128 */
3129WL_EXPORT void
3130weston_layer_set_position(struct weston_layer *layer,
3131 enum weston_layer_position position)
3132{
3133 struct weston_layer *below;
3134
3135 wl_list_remove(&layer->link);
3136
3137 /* layer_list is ordered from top to bottom, the last layer being the
3138 * background with the smallest position value */
3139
3140 layer->position = position;
3141 wl_list_for_each_reverse(below, &layer->compositor->layer_list, link) {
3142 if (below->position >= layer->position) {
3143 wl_list_insert(&below->link, &layer->link);
3144 return;
3145 }
3146 }
3147 wl_list_insert(&layer->compositor->layer_list, &layer->link);
3148}
3149
3150/** Hide a layer by taking it off the layer list.
3151 * This function is safe to call if the layer is not on the list.
3152 *
3153 * \param layer The layer to hide
3154 */
3155WL_EXPORT void
3156weston_layer_unset_position(struct weston_layer *layer)
3157{
3158 wl_list_remove(&layer->link);
3159 wl_list_init(&layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003160}
3161
3162WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003163weston_layer_set_mask(struct weston_layer *layer,
3164 int x, int y, int width, int height)
3165{
3166 struct weston_view *view;
3167
3168 layer->mask.x1 = x;
3169 layer->mask.x2 = x + width;
3170 layer->mask.y1 = y;
3171 layer->mask.y2 = y + height;
3172
3173 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
3174 weston_view_geometry_dirty(view);
3175 }
3176}
3177
3178WL_EXPORT void
3179weston_layer_set_mask_infinite(struct weston_layer *layer)
3180{
Adam Jackson3c3f3b12019-10-16 16:02:59 -04003181 struct weston_view *view;
3182
3183 layer->mask.x1 = INT32_MIN;
3184 layer->mask.x2 = INT32_MAX;
3185 layer->mask.y1 = INT32_MIN;
3186 layer->mask.y2 = INT32_MAX;
3187
3188 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
3189 weston_view_geometry_dirty(view);
3190 }
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003191}
3192
Daniel Stone3b775632018-07-20 08:38:25 +01003193WL_EXPORT bool
3194weston_layer_mask_is_infinite(struct weston_layer *layer)
3195{
3196 return layer->mask.x1 == INT32_MIN &&
3197 layer->mask.y1 == INT32_MIN &&
Adam Jackson3c3f3b12019-10-16 16:02:59 -04003198 layer->mask.x2 == INT32_MAX &&
3199 layer->mask.y2 == INT32_MAX;
Daniel Stone3b775632018-07-20 08:38:25 +01003200}
3201
Marius Vlad55d87362019-06-11 01:15:35 +03003202/**
3203 * \ingroup output
3204 */
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003205WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003206weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003207{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003208 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04003209 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01003210
Bryce Harrington08976ac2016-08-30 12:05:16 -07003211 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
3212 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003213 return;
3214
Pekka Paalanenb5026542014-11-12 15:09:24 +02003215 if (!output->repaint_needed)
Marius Vlad3203ff62019-09-05 14:56:12 +03003216 TL_POINT(compositor, "core_repaint_req", TLP_OUTPUT(output), TLP_END);
Pekka Paalanenb5026542014-11-12 15:09:24 +02003217
Kristian Høgsbergef044142011-06-21 15:02:12 -04003218 loop = wl_display_get_event_loop(compositor->wl_display);
Daniel Stone09a97e22017-03-01 11:34:06 +00003219 output->repaint_needed = true;
Daniel Stone05df8c12017-03-03 16:59:42 +00003220
3221 /* If we already have a repaint scheduled for our idle handler,
3222 * no need to set it again. If the repaint has been called but
3223 * not finished, then weston_output_finish_frame() will notice
3224 * that a repaint is needed and schedule one. */
3225 if (output->repaint_status != REPAINT_NOT_SCHEDULED)
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003226 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01003227
Daniel Stone05df8c12017-03-03 16:59:42 +00003228 output->repaint_status = REPAINT_BEGIN_FROM_IDLE;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03003229 assert(!output->idle_repaint_source);
3230 output->idle_repaint_source = wl_event_loop_add_idle(loop, idle_repaint,
3231 output);
Marius Vlad3203ff62019-09-05 14:56:12 +03003232 TL_POINT(compositor, "core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003233}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05003234
Marius Vlad9fdda7f2019-06-11 16:08:55 +03003235/** weston_compositor_schedule_repaint
3236 * \ingroup compositor
3237 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003238WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003239weston_compositor_schedule_repaint(struct weston_compositor *compositor)
3240{
3241 struct weston_output *output;
3242
3243 wl_list_for_each(output, &compositor->output_list, link)
3244 weston_output_schedule_repaint(output);
3245}
3246
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003247static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003248surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003249{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003250 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003251}
3252
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003253static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003254surface_attach(struct wl_client *client,
3255 struct wl_resource *resource,
3256 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
3257{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003258 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05003259 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003260
Kristian Høgsbergab19f932013-08-20 11:30:54 -07003261 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05003262 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07003263 if (buffer == NULL) {
3264 wl_client_post_no_memory(client);
3265 return;
3266 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07003267 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04003268
Pekka Paalanende685b82012-12-04 15:58:12 +02003269 /* Attach, attach, without commit in between does not send
3270 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003271 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003272
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003273 surface->pending.sx = sx;
3274 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01003275 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003276}
3277
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003278static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003279surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003280 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003281 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05003282{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003283 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003284
Derek Foreman57e92ed2015-11-17 14:11:35 -06003285 if (width <= 0 || height <= 0)
3286 return;
3287
Derek Foreman152254b2015-11-26 14:17:48 -06003288 pixman_region32_union_rect(&surface->pending.damage_surface,
3289 &surface->pending.damage_surface,
3290 x, y, width, height);
3291}
3292
3293static void
3294surface_damage_buffer(struct wl_client *client,
3295 struct wl_resource *resource,
3296 int32_t x, int32_t y, int32_t width, int32_t height)
3297{
3298 struct weston_surface *surface = wl_resource_get_user_data(resource);
3299
3300 if (width <= 0 || height <= 0)
3301 return;
3302
3303 pixman_region32_union_rect(&surface->pending.damage_buffer,
3304 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04003305 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003306}
3307
Kristian Høgsberg33418202011-08-16 23:01:28 -04003308static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003309destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04003310{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05003311 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003312
3313 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02003314 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003315}
3316
3317static void
3318surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003319 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04003320{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003321 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003322 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003323
3324 cb = malloc(sizeof *cb);
3325 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003326 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003327 return;
3328 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03003329
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003330 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
3331 callback);
3332 if (cb->resource == NULL) {
3333 free(cb);
3334 wl_resource_post_no_memory(resource);
3335 return;
3336 }
3337
Jason Ekstranda85118c2013-06-27 20:17:02 -05003338 wl_resource_set_implementation(cb->resource, NULL, cb,
3339 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003340
Pekka Paalanenbc106382012-10-10 12:49:31 +03003341 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003342}
3343
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003344static void
3345surface_set_opaque_region(struct wl_client *client,
3346 struct wl_resource *resource,
3347 struct wl_resource *region_resource)
3348{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003349 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003350 struct weston_region *region;
3351
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003352 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003353 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003354 pixman_region32_copy(&surface->pending.opaque,
3355 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003356 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07003357 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003358 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003359}
3360
3361static void
3362surface_set_input_region(struct wl_client *client,
3363 struct wl_resource *resource,
3364 struct wl_resource *region_resource)
3365{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003366 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003367 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003368
3369 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003370 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003371 pixman_region32_copy(&surface->pending.input,
3372 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003373 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003374 pixman_region32_fini(&surface->pending.input);
3375 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003376 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003377}
3378
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003379/* Cause damage to this sub-surface and all its children.
3380 *
3381 * This is useful when there are state changes that need an implicit
3382 * damage, e.g. a z-order change.
3383 */
3384static void
3385weston_surface_damage_subsurfaces(struct weston_subsurface *sub)
3386{
3387 struct weston_subsurface *child;
3388
3389 weston_surface_damage(sub->surface);
3390 sub->reordered = false;
3391
3392 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link)
3393 if (child != sub)
3394 weston_surface_damage_subsurfaces(child);
3395}
3396
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003397static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003398weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003399{
Pekka Paalanene67858b2013-04-25 13:57:42 +03003400 struct weston_subsurface *sub;
3401
3402 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
3403 parent_link_pending) {
3404 wl_list_remove(&sub->parent_link);
3405 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003406
3407 if (sub->reordered)
3408 weston_surface_damage_subsurfaces(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003409 }
3410}
3411
3412static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03003413weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05003414 struct weston_matrix *matrix)
3415{
Pekka Paalanen04baea52016-04-26 15:50:58 +03003416 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05003417 double src_width, src_height, dest_width, dest_height;
3418
3419 weston_matrix_init(matrix);
3420
3421 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
3422 src_width = surface->width_from_buffer;
3423 src_height = surface->height_from_buffer;
3424 } else {
3425 src_width = wl_fixed_to_double(vp->buffer.src_width);
3426 src_height = wl_fixed_to_double(vp->buffer.src_height);
3427 }
3428
3429 if (vp->surface.width == -1) {
3430 dest_width = src_width;
3431 dest_height = src_height;
3432 } else {
3433 dest_width = vp->surface.width;
3434 dest_height = vp->surface.height;
3435 }
3436
3437 if (src_width != dest_width || src_height != dest_height)
3438 weston_matrix_scale(matrix,
3439 src_width / dest_width,
3440 src_height / dest_height, 1);
3441
3442 if (vp->buffer.src_width != wl_fixed_from_int(-1))
3443 weston_matrix_translate(matrix,
3444 wl_fixed_to_double(vp->buffer.src_x),
3445 wl_fixed_to_double(vp->buffer.src_y),
3446 0);
3447
3448 switch (vp->buffer.transform) {
3449 case WL_OUTPUT_TRANSFORM_FLIPPED:
3450 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3451 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3452 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3453 weston_matrix_scale(matrix, -1, 1, 1);
3454 weston_matrix_translate(matrix,
3455 surface->width_from_buffer, 0, 0);
3456 break;
3457 }
3458
3459 switch (vp->buffer.transform) {
3460 default:
3461 case WL_OUTPUT_TRANSFORM_NORMAL:
3462 case WL_OUTPUT_TRANSFORM_FLIPPED:
3463 break;
3464 case WL_OUTPUT_TRANSFORM_90:
3465 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +02003466 weston_matrix_rotate_xy(matrix, 0, -1);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003467 weston_matrix_translate(matrix,
Pekka Paalanen8060d822020-02-06 15:27:54 +02003468 0, surface->width_from_buffer, 0);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003469 break;
3470 case WL_OUTPUT_TRANSFORM_180:
3471 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3472 weston_matrix_rotate_xy(matrix, -1, 0);
3473 weston_matrix_translate(matrix,
3474 surface->width_from_buffer,
3475 surface->height_from_buffer, 0);
3476 break;
3477 case WL_OUTPUT_TRANSFORM_270:
3478 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +02003479 weston_matrix_rotate_xy(matrix, 0, 1);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003480 weston_matrix_translate(matrix,
Pekka Paalanen8060d822020-02-06 15:27:54 +02003481 surface->height_from_buffer, 0, 0);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003482 break;
3483 }
3484
3485 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
3486}
3487
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003488/**
3489 * Compute a + b > c while being safe to overflows.
3490 */
3491static bool
3492fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
3493{
3494 return (int64_t)a + (int64_t)b > (int64_t)c;
3495}
3496
3497static bool
3498weston_surface_is_pending_viewport_source_valid(
3499 const struct weston_surface *surface)
3500{
3501 const struct weston_surface_state *pend = &surface->pending;
3502 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
3503 int width_from_buffer = 0;
3504 int height_from_buffer = 0;
3505 wl_fixed_t w;
3506 wl_fixed_t h;
3507
3508 /* If viewport source rect is not set, it is always ok. */
3509 if (vp->buffer.src_width == wl_fixed_from_int(-1))
3510 return true;
3511
3512 if (pend->newly_attached) {
3513 if (pend->buffer) {
3514 convert_size_by_transform_scale(&width_from_buffer,
3515 &height_from_buffer,
3516 pend->buffer->width,
3517 pend->buffer->height,
3518 vp->buffer.transform,
3519 vp->buffer.scale);
3520 } else {
3521 /* No buffer: viewport is irrelevant. */
3522 return true;
3523 }
3524 } else {
3525 width_from_buffer = surface->width_from_buffer;
3526 height_from_buffer = surface->height_from_buffer;
3527 }
3528
3529 assert((width_from_buffer == 0) == (height_from_buffer == 0));
3530 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
3531
3532 /* No buffer: viewport is irrelevant. */
3533 if (width_from_buffer == 0 || height_from_buffer == 0)
3534 return true;
3535
3536 /* overflow checks for wl_fixed_from_int() */
3537 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
3538 return false;
3539 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
3540 return false;
3541
3542 w = wl_fixed_from_int(width_from_buffer);
3543 h = wl_fixed_from_int(height_from_buffer);
3544
3545 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
3546 return false;
3547 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
3548 return false;
3549
3550 return true;
3551}
3552
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003553static bool
3554fixed_is_integer(wl_fixed_t v)
3555{
3556 return (v & 0xff) == 0;
3557}
3558
3559static bool
3560weston_surface_is_pending_viewport_dst_size_int(
3561 const struct weston_surface *surface)
3562{
3563 const struct weston_buffer_viewport *vp =
3564 &surface->pending.buffer_viewport;
3565
3566 if (vp->surface.width != -1) {
3567 assert(vp->surface.width > 0 && vp->surface.height > 0);
3568 return true;
3569 }
3570
3571 return fixed_is_integer(vp->buffer.src_width) &&
3572 fixed_is_integer(vp->buffer.src_height);
3573}
3574
Derek Foreman152254b2015-11-26 14:17:48 -06003575/* Translate pending damage in buffer co-ordinates to surface
3576 * co-ordinates and union it with a pixman_region32_t.
3577 * This should only be called after the buffer is attached.
3578 */
3579static void
3580apply_damage_buffer(pixman_region32_t *dest,
3581 struct weston_surface *surface,
3582 struct weston_surface_state *state)
3583{
3584 struct weston_buffer *buffer = surface->buffer_ref.buffer;
3585
3586 /* wl_surface.damage_buffer needs to be clipped to the buffer,
3587 * translated into surface co-ordinates and unioned with
3588 * any other surface damage.
3589 * None of this makes sense if there is no buffer though.
3590 */
3591 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
3592 pixman_region32_t buffer_damage;
3593
3594 pixman_region32_intersect_rect(&state->damage_buffer,
3595 &state->damage_buffer,
3596 0, 0, buffer->width,
3597 buffer->height);
3598 pixman_region32_init(&buffer_damage);
3599 weston_matrix_transform_region(&buffer_damage,
3600 &surface->buffer_to_surface_matrix,
3601 &state->damage_buffer);
3602 pixman_region32_union(dest, dest, &buffer_damage);
3603 pixman_region32_fini(&buffer_damage);
3604 }
3605 /* We should clear this on commit even if there was no buffer */
3606 pixman_region32_clear(&state->damage_buffer);
3607}
3608
Jason Ekstrand1e059042014-10-16 10:55:19 -05003609static void
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303610weston_surface_set_desired_protection(struct weston_surface *surface,
3611 enum weston_hdcp_protection protection)
3612{
3613 if (surface->desired_protection == protection)
3614 return;
3615 surface->desired_protection = protection;
3616 weston_surface_damage(surface);
3617}
3618
3619static void
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303620weston_surface_set_protection_mode(struct weston_surface *surface,
3621 enum weston_surface_protection_mode p_mode)
3622{
3623 struct content_protection *cp = surface->compositor->content_protection;
3624 struct protected_surface *psurface;
3625
3626 surface->protection_mode = p_mode;
3627 wl_list_for_each(psurface, &cp->protected_list, link) {
3628 if (!psurface || psurface->surface != surface)
3629 continue;
3630 weston_protected_surface_send_event(psurface,
3631 surface->current_protection);
3632 }
3633}
3634
3635static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05003636weston_surface_commit_state(struct weston_surface *surface,
3637 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003638{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003639 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003640 pixman_region32_t opaque;
3641
Alexander Larsson4ea95522013-05-22 14:41:37 +02003642 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02003643 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03003644 /* wp_viewport.set_source */
3645 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003646 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003647
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003648 /* wl_surface.attach */
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003649 if (state->newly_attached) {
3650 /* zwp_surface_synchronization_v1.set_acquire_fence */
3651 fd_move(&surface->acquire_fence_fd,
3652 &state->acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003653 /* zwp_surface_synchronization_v1.get_release */
3654 weston_buffer_release_move(&surface->buffer_release_ref,
3655 &state->buffer_release_ref);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003656 weston_surface_attach(surface, state->buffer);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003657 }
Jason Ekstrand7b982072014-05-20 14:33:03 -05003658 weston_surface_state_set_buffer(state, NULL);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003659 assert(state->acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003660 assert(state->buffer_release_ref.buffer_release == NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01003661
Jason Ekstrand1e059042014-10-16 10:55:19 -05003662 weston_surface_build_buffer_matrix(surface,
3663 &surface->surface_to_buffer_matrix);
3664 weston_matrix_invert(&surface->buffer_to_surface_matrix,
3665 &surface->surface_to_buffer_matrix);
3666
Jason Ekstrand7b982072014-05-20 14:33:03 -05003667 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003668 weston_surface_update_size(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003669 if (surface->committed)
3670 surface->committed(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003671 }
Giulio Camuffo184df502013-02-21 11:29:21 +01003672
Jason Ekstrand7b982072014-05-20 14:33:03 -05003673 state->sx = 0;
3674 state->sy = 0;
3675 state->newly_attached = 0;
3676 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03003677
Derek Foreman152254b2015-11-26 14:17:48 -06003678 /* wl_surface.damage and wl_surface.damage_buffer */
Marius Vlad2a1b7862019-09-05 13:12:18 +03003679 if (pixman_region32_not_empty(&state->damage_surface) ||
3680 pixman_region32_not_empty(&state->damage_buffer))
Marius Vlad3203ff62019-09-05 14:56:12 +03003681 TL_POINT(surface->compositor, "core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06003682
Pekka Paalanen8e159182012-10-10 12:49:25 +03003683 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06003684 &state->damage_surface);
3685
3686 apply_damage_buffer(&surface->damage, surface, state);
3687
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05003688 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07003689 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06003690 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003691
3692 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003693 pixman_region32_init(&opaque);
3694 pixman_region32_intersect_rect(&opaque, &state->opaque,
3695 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003696
3697 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
3698 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003699 wl_list_for_each(view, &surface->views, surface_link)
3700 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003701 }
3702
3703 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003704
3705 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003706 pixman_region32_intersect_rect(&surface->input, &state->input,
3707 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003708
Pekka Paalanenbc106382012-10-10 12:49:31 +03003709 /* wl_surface.frame */
3710 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05003711 &state->frame_callback_list);
3712 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003713
3714 /* XXX:
3715 * What should happen with a feedback request, if there
3716 * is no wl_buffer attached for this commit?
3717 */
3718
3719 /* presentation.feedback */
3720 wl_list_insert_list(&surface->feedback_list,
3721 &state->feedback_list);
3722 wl_list_init(&state->feedback_list);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003723
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303724 /* weston_protected_surface.enforced/relaxed */
3725 if (surface->protection_mode != state->protection_mode)
3726 weston_surface_set_protection_mode(surface,
3727 state->protection_mode);
3728
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303729 /* weston_protected_surface.set_type */
3730 weston_surface_set_desired_protection(surface, state->desired_protection);
3731
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003732 wl_signal_emit(&surface->commit_signal, surface);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003733}
3734
3735static void
3736weston_surface_commit(struct weston_surface *surface)
3737{
3738 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003739
Pekka Paalanene67858b2013-04-25 13:57:42 +03003740 weston_surface_commit_subsurface_order(surface);
3741
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003742 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003743}
3744
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003745static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003746weston_subsurface_commit(struct weston_subsurface *sub);
3747
3748static void
3749weston_subsurface_parent_commit(struct weston_subsurface *sub,
3750 int parent_is_synchronized);
3751
3752static void
3753surface_commit(struct wl_client *client, struct wl_resource *resource)
3754{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003755 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003756 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
3757
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003758 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
3759 assert(surface->viewport_resource);
3760
3761 wl_resource_post_error(surface->viewport_resource,
3762 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
3763 "wl_surface@%d has viewport source outside buffer",
3764 wl_resource_get_id(resource));
3765 return;
3766 }
3767
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003768 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
3769 assert(surface->viewport_resource);
3770
3771 wl_resource_post_error(surface->viewport_resource,
3772 WP_VIEWPORT_ERROR_BAD_SIZE,
3773 "wl_surface@%d viewport dst size not integer",
3774 wl_resource_get_id(resource));
3775 return;
3776 }
3777
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003778 if (surface->pending.acquire_fence_fd >= 0) {
3779 assert(surface->synchronization_resource);
3780
3781 if (!surface->pending.buffer) {
3782 fd_clear(&surface->pending.acquire_fence_fd);
3783 wl_resource_post_error(surface->synchronization_resource,
3784 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3785 "wl_surface@%"PRIu32" no buffer for synchronization",
3786 wl_resource_get_id(resource));
3787 return;
3788 }
3789
3790 /* We support fences for both wp_linux_dmabuf and opaque EGL
3791 * buffers, as mandated by minor version 2 of the
3792 * zwp_linux_explicit_synchronization_v1 protocol. Since
3793 * renderers that support fences currently only support these
3794 * two buffer types plus SHM buffers, we can just check for the
3795 * SHM buffer case here.
3796 */
3797 if (wl_shm_buffer_get(surface->pending.buffer->resource)) {
3798 fd_clear(&surface->pending.acquire_fence_fd);
3799 wl_resource_post_error(surface->synchronization_resource,
3800 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_UNSUPPORTED_BUFFER,
3801 "wl_surface@%"PRIu32" unsupported buffer for synchronization",
3802 wl_resource_get_id(resource));
3803 return;
3804 }
3805 }
3806
Alexandros Frantzis67629672018-10-19 12:14:11 +03003807 if (surface->pending.buffer_release_ref.buffer_release &&
3808 !surface->pending.buffer) {
3809 assert(surface->synchronization_resource);
3810
3811 wl_resource_post_error(surface->synchronization_resource,
3812 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3813 "wl_surface@%"PRIu32" no buffer for synchronization",
3814 wl_resource_get_id(resource));
3815 return;
3816 }
3817
Pekka Paalanene67858b2013-04-25 13:57:42 +03003818 if (sub) {
3819 weston_subsurface_commit(sub);
3820 return;
3821 }
3822
3823 weston_surface_commit(surface);
3824
3825 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
3826 if (sub->surface != surface)
3827 weston_subsurface_parent_commit(sub, 0);
3828 }
3829}
3830
3831static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003832surface_set_buffer_transform(struct wl_client *client,
3833 struct wl_resource *resource, int transform)
3834{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003835 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003836
Jonny Lamba55f1392014-05-30 12:07:15 +02003837 /* if wl_output.transform grows more members this will need to be updated. */
3838 if (transform < 0 ||
3839 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
3840 wl_resource_post_error(resource,
3841 WL_SURFACE_ERROR_INVALID_TRANSFORM,
3842 "buffer transform must be a valid transform "
3843 "('%d' specified)", transform);
3844 return;
3845 }
3846
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003847 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003848 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003849}
3850
Alexander Larsson4ea95522013-05-22 14:41:37 +02003851static void
3852surface_set_buffer_scale(struct wl_client *client,
3853 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003854 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003855{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003856 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003857
Jonny Lamba55f1392014-05-30 12:07:15 +02003858 if (scale < 1) {
3859 wl_resource_post_error(resource,
3860 WL_SURFACE_ERROR_INVALID_SCALE,
3861 "buffer scale must be at least one "
3862 "('%d' specified)", scale);
3863 return;
3864 }
3865
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003866 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003867 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003868}
3869
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003870static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003871 surface_destroy,
3872 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04003873 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003874 surface_frame,
3875 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003876 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003877 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02003878 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06003879 surface_set_buffer_scale,
3880 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003881};
3882
3883static void
3884compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003885 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003886{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04003887 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003888 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003889
Kristian Høgsberg18c93002012-01-27 11:58:31 -05003890 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003891 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003892 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003893 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003894 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003895
Jason Ekstranda85118c2013-06-27 20:17:02 -05003896 surface->resource =
3897 wl_resource_create(client, &wl_surface_interface,
3898 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003899 if (surface->resource == NULL) {
3900 weston_surface_destroy(surface);
3901 wl_resource_post_no_memory(resource);
3902 return;
3903 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003904 wl_resource_set_implementation(surface->resource, &surface_interface,
3905 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003906
3907 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003908}
3909
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003910static void
3911destroy_region(struct wl_resource *resource)
3912{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003913 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003914
3915 pixman_region32_fini(&region->region);
3916 free(region);
3917}
3918
3919static void
3920region_destroy(struct wl_client *client, struct wl_resource *resource)
3921{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003922 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003923}
3924
3925static void
3926region_add(struct wl_client *client, struct wl_resource *resource,
3927 int32_t x, int32_t y, int32_t width, int32_t height)
3928{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003929 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003930
3931 pixman_region32_union_rect(&region->region, &region->region,
3932 x, y, width, height);
3933}
3934
3935static void
3936region_subtract(struct wl_client *client, struct wl_resource *resource,
3937 int32_t x, int32_t y, int32_t width, int32_t height)
3938{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003939 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003940 pixman_region32_t rect;
3941
3942 pixman_region32_init_rect(&rect, x, y, width, height);
3943 pixman_region32_subtract(&region->region, &region->region, &rect);
3944 pixman_region32_fini(&rect);
3945}
3946
3947static const struct wl_region_interface region_interface = {
3948 region_destroy,
3949 region_add,
3950 region_subtract
3951};
3952
3953static void
3954compositor_create_region(struct wl_client *client,
3955 struct wl_resource *resource, uint32_t id)
3956{
3957 struct weston_region *region;
3958
3959 region = malloc(sizeof *region);
3960 if (region == NULL) {
3961 wl_resource_post_no_memory(resource);
3962 return;
3963 }
3964
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003965 pixman_region32_init(&region->region);
3966
Jason Ekstranda85118c2013-06-27 20:17:02 -05003967 region->resource =
3968 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003969 if (region->resource == NULL) {
3970 free(region);
3971 wl_resource_post_no_memory(resource);
3972 return;
3973 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003974 wl_resource_set_implementation(region->resource, &region_interface,
3975 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003976}
3977
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003978static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003979 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003980 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003981};
3982
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003983static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003984weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3985{
3986 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003987
Jason Ekstrand7b982072014-05-20 14:33:03 -05003988 weston_surface_commit_state(surface, &sub->cached);
3989 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003990
3991 weston_surface_commit_subsurface_order(surface);
3992
3993 weston_surface_schedule_repaint(surface);
3994
Jason Ekstrand7b982072014-05-20 14:33:03 -05003995 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003996}
3997
3998static void
3999weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
4000{
4001 struct weston_surface *surface = sub->surface;
4002
4003 /*
4004 * If this commit would cause the surface to move by the
4005 * attach(dx, dy) parameters, the old damage region must be
4006 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05004007 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03004008 */
Derek Foreman152254b2015-11-26 14:17:48 -06004009 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004010 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06004011 pixman_region32_union(&sub->cached.damage_surface,
4012 &sub->cached.damage_surface,
4013 &surface->pending.damage_surface);
4014 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004015
4016 if (surface->pending.newly_attached) {
4017 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05004018 weston_surface_state_set_buffer(&sub->cached,
4019 surface->pending.buffer);
4020 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004021 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04004022 weston_presentation_feedback_discard_list(
4023 &sub->cached.feedback_list);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03004024 /* zwp_surface_synchronization_v1.set_acquire_fence */
4025 fd_move(&sub->cached.acquire_fence_fd,
4026 &surface->pending.acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03004027 /* zwp_surface_synchronization_v1.get_release */
4028 weston_buffer_release_move(&sub->cached.buffer_release_ref,
4029 &surface->pending.buffer_release_ref);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004030 }
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05304031 sub->cached.desired_protection = surface->pending.desired_protection;
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05304032 sub->cached.protection_mode = surface->pending.protection_mode;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03004033 assert(surface->pending.acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03004034 assert(surface->pending.buffer_release_ref.buffer_release == NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004035 sub->cached.sx += surface->pending.sx;
4036 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02004037
Derek Foreman152254b2015-11-26 14:17:48 -06004038 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
4039
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004040 sub->cached.buffer_viewport.changed |=
4041 surface->pending.buffer_viewport.changed;
4042 sub->cached.buffer_viewport.buffer =
4043 surface->pending.buffer_viewport.buffer;
4044 sub->cached.buffer_viewport.surface =
4045 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004046
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004047 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004048
4049 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
4050
4051 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
4052
4053 wl_list_insert_list(&sub->cached.frame_callback_list,
4054 &surface->pending.frame_callback_list);
4055 wl_list_init(&surface->pending.frame_callback_list);
4056
Pekka Paalanen133e4392014-09-23 22:08:46 -04004057 wl_list_insert_list(&sub->cached.feedback_list,
4058 &surface->pending.feedback_list);
4059 wl_list_init(&surface->pending.feedback_list);
4060
Jason Ekstrand7b982072014-05-20 14:33:03 -05004061 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004062}
4063
Derek Foreman280e7dd2014-10-03 13:13:42 -05004064static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03004065weston_subsurface_is_synchronized(struct weston_subsurface *sub)
4066{
4067 while (sub) {
4068 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05004069 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004070
4071 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05004072 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004073
4074 sub = weston_surface_to_subsurface(sub->parent);
4075 }
4076
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01004077 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004078}
4079
4080static void
4081weston_subsurface_commit(struct weston_subsurface *sub)
4082{
4083 struct weston_surface *surface = sub->surface;
4084 struct weston_subsurface *tmp;
4085
4086 /* Recursive check for effectively synchronized. */
4087 if (weston_subsurface_is_synchronized(sub)) {
4088 weston_subsurface_commit_to_cache(sub);
4089 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05004090 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03004091 /* flush accumulated state from cache */
4092 weston_subsurface_commit_to_cache(sub);
4093 weston_subsurface_commit_from_cache(sub);
4094 } else {
4095 weston_surface_commit(surface);
4096 }
4097
4098 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
4099 if (tmp->surface != surface)
4100 weston_subsurface_parent_commit(tmp, 0);
4101 }
4102 }
4103}
4104
4105static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004106weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004107{
4108 struct weston_surface *surface = sub->surface;
4109 struct weston_subsurface *tmp;
4110
Pekka Paalanene67858b2013-04-25 13:57:42 +03004111 /* From now on, commit_from_cache the whole sub-tree, regardless of
4112 * the synchronized mode of each child. This sub-surface or some
4113 * of its ancestors were synchronized, so we are synchronized
4114 * all the way down.
4115 */
4116
Jason Ekstrand7b982072014-05-20 14:33:03 -05004117 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004118 weston_subsurface_commit_from_cache(sub);
4119
4120 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
4121 if (tmp->surface != surface)
4122 weston_subsurface_parent_commit(tmp, 1);
4123 }
4124}
4125
4126static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004127weston_subsurface_parent_commit(struct weston_subsurface *sub,
4128 int parent_is_synchronized)
4129{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004130 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004131 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004132 wl_list_for_each(view, &sub->surface->views, surface_link)
4133 weston_view_set_position(view,
4134 sub->position.x,
4135 sub->position.y);
4136
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004137 sub->position.set = 0;
4138 }
4139
4140 if (parent_is_synchronized || sub->synchronized)
4141 weston_subsurface_synchronized_commit(sub);
4142}
4143
Pekka Paalanen8274d902014-08-06 19:36:51 +03004144static int
4145subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
4146{
4147 return snprintf(buf, len, "sub-surface");
4148}
4149
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004150static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004151subsurface_committed(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004152{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004153 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004154
Jason Ekstranda7af7042013-10-12 22:38:11 -05004155 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004156 weston_view_set_position(view,
4157 view->geometry.x + dx,
4158 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004159
4160 /* No need to check parent mappedness, because if parent is not
4161 * mapped, parent is not in a visible layer, so this sub-surface
4162 * will not be drawn either.
4163 */
Armin Krezovićf8486c32016-06-30 06:04:28 +02004164
Pekka Paalanene67858b2013-04-25 13:57:42 +03004165 if (!weston_surface_is_mapped(surface)) {
Armin Krezovićf8486c32016-06-30 06:04:28 +02004166 surface->is_mapped = true;
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03004167
Derek Foreman4b1a0a12014-09-10 15:37:33 -05004168 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03004169 * because that would call it also for the parent surface,
4170 * which might not be mapped yet. That would lead to
4171 * inconsistent state, where the window could never be
4172 * mapped.
4173 *
Armin Krezovićf8486c32016-06-30 06:04:28 +02004174 * Instead just force the is_mapped flag on, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03004175 * weston_surface_is_mapped() return true, so that when the
4176 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03004177 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03004178 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004179 }
4180}
4181
4182static struct weston_subsurface *
4183weston_surface_to_subsurface(struct weston_surface *surface)
4184{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004185 if (surface->committed == subsurface_committed)
4186 return surface->committed_private;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004187
4188 return NULL;
4189}
4190
Pekka Paalanen01388e22013-04-25 13:57:44 +03004191WL_EXPORT struct weston_surface *
4192weston_surface_get_main_surface(struct weston_surface *surface)
4193{
4194 struct weston_subsurface *sub;
4195
4196 while (surface && (sub = weston_surface_to_subsurface(surface)))
4197 surface = sub->parent;
4198
4199 return surface;
4200}
4201
Pekka Paalanen50b67472014-10-01 15:02:41 +03004202WL_EXPORT int
4203weston_surface_set_role(struct weston_surface *surface,
4204 const char *role_name,
4205 struct wl_resource *error_resource,
4206 uint32_t error_code)
4207{
4208 assert(role_name);
4209
4210 if (surface->role_name == NULL ||
4211 surface->role_name == role_name ||
4212 strcmp(surface->role_name, role_name) == 0) {
4213 surface->role_name = role_name;
4214
4215 return 0;
4216 }
4217
4218 wl_resource_post_error(error_resource, error_code,
4219 "Cannot assign role %s to wl_surface@%d,"
4220 " already has role %s\n",
4221 role_name,
4222 wl_resource_get_id(surface->resource),
4223 surface->role_name);
4224 return -1;
4225}
4226
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02004227WL_EXPORT const char *
4228weston_surface_get_role(struct weston_surface *surface)
4229{
4230 return surface->role_name;
4231}
4232
Pekka Paalanen8274d902014-08-06 19:36:51 +03004233WL_EXPORT void
4234weston_surface_set_label_func(struct weston_surface *surface,
4235 int (*desc)(struct weston_surface *,
4236 char *, size_t))
4237{
4238 surface->get_label = desc;
Marius Vlad5de92972019-10-15 13:25:41 +03004239 weston_timeline_refresh_subscription_objects(surface->compositor,
4240 surface);
Pekka Paalanen8274d902014-08-06 19:36:51 +03004241}
4242
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004243/** Get the size of surface contents
4244 *
4245 * \param surface The surface to query.
4246 * \param width Returns the width of raw contents.
4247 * \param height Returns the height of raw contents.
4248 *
4249 * Retrieves the raw surface content size in pixels for the given surface.
4250 * This is the whole content size in buffer pixels. If the surface
4251 * has no content or the renderer does not implement this feature,
4252 * zeroes are returned.
4253 *
4254 * This function is used to determine the buffer size needed for
4255 * a weston_surface_copy_content() call.
4256 */
4257WL_EXPORT void
4258weston_surface_get_content_size(struct weston_surface *surface,
4259 int *width, int *height)
4260{
4261 struct weston_renderer *rer = surface->compositor->renderer;
4262
4263 if (!rer->surface_get_content_size) {
4264 *width = 0;
4265 *height = 0;
4266 return;
4267 }
4268
4269 rer->surface_get_content_size(surface, width, height);
4270}
4271
Quentin Glidic248dd102016-08-12 10:41:34 +02004272/** Get the bounding box of a surface and its subsurfaces
4273 *
4274 * \param surface The surface to query.
4275 * \return The bounding box relative to the surface origin.
4276 *
4277 */
4278WL_EXPORT struct weston_geometry
4279weston_surface_get_bounding_box(struct weston_surface *surface)
4280{
4281 pixman_region32_t region;
4282 pixman_box32_t *box;
4283 struct weston_subsurface *subsurface;
4284
4285 pixman_region32_init_rect(&region,
4286 0, 0,
4287 surface->width, surface->height);
4288
4289 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link)
4290 pixman_region32_union_rect(&region, &region,
4291 subsurface->position.x,
4292 subsurface->position.y,
4293 subsurface->surface->width,
4294 subsurface->surface->height);
4295
4296 box = pixman_region32_extents(&region);
4297 struct weston_geometry geometry = {
4298 .x = box->x1,
4299 .y = box->y1,
4300 .width = box->x2 - box->x1,
4301 .height = box->y2 - box->y1,
4302 };
4303
4304 pixman_region32_fini(&region);
4305
4306 return geometry;
4307}
4308
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004309/** Copy surface contents to system memory.
4310 *
4311 * \param surface The surface to copy from.
4312 * \param target Pointer to the target memory buffer.
4313 * \param size Size of the target buffer in bytes.
4314 * \param src_x X location on contents to copy from.
4315 * \param src_y Y location on contents to copy from.
4316 * \param width Width in pixels of the area to copy.
4317 * \param height Height in pixels of the area to copy.
4318 * \return 0 for success, -1 for failure.
4319 *
4320 * Surface contents are maintained by the renderer. They can be in a
4321 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
4322 * else.
4323 *
4324 * Surface contents are copied into memory pointed to by target,
4325 * which has size bytes of space available. The target memory
4326 * may be larger than needed, but being smaller returns an error.
4327 * The extra bytes in target may or may not be written; their content is
4328 * unspecified. Size must be large enough to hold the image.
4329 *
4330 * The image in the target memory will be arranged in rows from
4331 * top to bottom, and pixels on a row from left to right. The pixel
4332 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
4333 * width * 4.
4334 *
4335 * Parameters src_x and src_y define the upper-left corner in buffer
4336 * coordinates (pixels) to copy from. Parameters width and height
4337 * define the size of the area to copy in pixels.
4338 *
4339 * The rectangle defined by src_x, src_y, width, height must fit in
4340 * the surface contents. Otherwise an error is returned.
4341 *
Changwoo Chof97d2502017-08-05 00:30:47 +09004342 * Use weston_surface_get_content_size to determine the content size; the
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004343 * needed target buffer size and rectangle limits.
4344 *
4345 * CURRENT IMPLEMENTATION RESTRICTIONS:
4346 * - the machine must be little-endian due to Pixman formats.
4347 *
4348 * NOTE: Pixman formats are premultiplied.
4349 */
4350WL_EXPORT int
4351weston_surface_copy_content(struct weston_surface *surface,
4352 void *target, size_t size,
4353 int src_x, int src_y,
4354 int width, int height)
4355{
4356 struct weston_renderer *rer = surface->compositor->renderer;
4357 int cw, ch;
4358 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
4359
4360 if (!rer->surface_copy_content)
4361 return -1;
4362
4363 weston_surface_get_content_size(surface, &cw, &ch);
4364
4365 if (src_x < 0 || src_y < 0)
4366 return -1;
4367
4368 if (width <= 0 || height <= 0)
4369 return -1;
4370
4371 if (src_x + width > cw || src_y + height > ch)
4372 return -1;
4373
4374 if (width * bytespp * height > size)
4375 return -1;
4376
4377 return rer->surface_copy_content(surface, target, size,
4378 src_x, src_y, width, height);
4379}
4380
Pekka Paalanene67858b2013-04-25 13:57:42 +03004381static void
4382subsurface_set_position(struct wl_client *client,
4383 struct wl_resource *resource, int32_t x, int32_t y)
4384{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004385 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004386
4387 if (!sub)
4388 return;
4389
4390 sub->position.x = x;
4391 sub->position.y = y;
4392 sub->position.set = 1;
4393}
4394
4395static struct weston_subsurface *
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004396subsurface_find_sibling(struct weston_subsurface *sub,
4397 struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004398{
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004399 struct weston_surface *parent = sub->parent;
4400 struct weston_subsurface *sibling;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004401
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004402 wl_list_for_each(sibling, &parent->subsurface_list, parent_link) {
4403 if (sibling->surface == surface && sibling != sub)
4404 return sibling;
4405 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004406
4407 return NULL;
4408}
4409
4410static struct weston_subsurface *
4411subsurface_sibling_check(struct weston_subsurface *sub,
4412 struct weston_surface *surface,
4413 const char *request)
4414{
4415 struct weston_subsurface *sibling;
4416
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004417 sibling = subsurface_find_sibling(sub, surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004418 if (!sibling) {
4419 wl_resource_post_error(sub->resource,
4420 WL_SUBSURFACE_ERROR_BAD_SURFACE,
4421 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004422 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004423 return NULL;
4424 }
4425
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004426 assert(sibling->parent == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004427
4428 return sibling;
4429}
4430
4431static void
4432subsurface_place_above(struct wl_client *client,
4433 struct wl_resource *resource,
4434 struct wl_resource *sibling_resource)
4435{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004436 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004437 struct weston_surface *surface =
4438 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004439 struct weston_subsurface *sibling;
4440
4441 if (!sub)
4442 return;
4443
4444 sibling = subsurface_sibling_check(sub, surface, "place_above");
4445 if (!sibling)
4446 return;
4447
4448 wl_list_remove(&sub->parent_link_pending);
4449 wl_list_insert(sibling->parent_link_pending.prev,
4450 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004451
4452 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004453}
4454
4455static void
4456subsurface_place_below(struct wl_client *client,
4457 struct wl_resource *resource,
4458 struct wl_resource *sibling_resource)
4459{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004460 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004461 struct weston_surface *surface =
4462 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004463 struct weston_subsurface *sibling;
4464
4465 if (!sub)
4466 return;
4467
4468 sibling = subsurface_sibling_check(sub, surface, "place_below");
4469 if (!sibling)
4470 return;
4471
4472 wl_list_remove(&sub->parent_link_pending);
4473 wl_list_insert(&sibling->parent_link_pending,
4474 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004475
4476 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004477}
4478
4479static void
4480subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
4481{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004482 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004483
4484 if (sub)
4485 sub->synchronized = 1;
4486}
4487
4488static void
4489subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
4490{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004491 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004492
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004493 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03004494 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004495
4496 /* If sub became effectively desynchronized, flush. */
4497 if (!weston_subsurface_is_synchronized(sub))
4498 weston_subsurface_synchronized_commit(sub);
4499 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004500}
4501
4502static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03004503weston_subsurface_unlink_parent(struct weston_subsurface *sub)
4504{
4505 wl_list_remove(&sub->parent_link);
4506 wl_list_remove(&sub->parent_link_pending);
4507 wl_list_remove(&sub->parent_destroy_listener.link);
4508 sub->parent = NULL;
4509}
4510
4511static void
4512weston_subsurface_destroy(struct weston_subsurface *sub);
4513
4514static void
4515subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
4516{
4517 struct weston_subsurface *sub =
4518 container_of(listener, struct weston_subsurface,
4519 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004520 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004521
4522 /* The protocol object (wl_resource) is left inert. */
4523 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004524 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004525
4526 weston_subsurface_destroy(sub);
4527}
4528
4529static void
4530subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
4531{
4532 struct weston_subsurface *sub =
4533 container_of(listener, struct weston_subsurface,
4534 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004535 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004536 assert(sub->surface != sub->parent);
4537
4538 if (weston_surface_is_mapped(sub->surface))
4539 weston_surface_unmap(sub->surface);
4540
4541 weston_subsurface_unlink_parent(sub);
4542}
4543
4544static void
4545subsurface_resource_destroy(struct wl_resource *resource)
4546{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004547 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004548
4549 if (sub)
4550 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004551}
4552
4553static void
4554subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
4555{
4556 wl_resource_destroy(resource);
4557}
4558
4559static void
4560weston_subsurface_link_parent(struct weston_subsurface *sub,
4561 struct weston_surface *parent)
4562{
4563 sub->parent = parent;
4564 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004565 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004566 &sub->parent_destroy_listener);
4567
4568 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4569 wl_list_insert(&parent->subsurface_list_pending,
4570 &sub->parent_link_pending);
4571}
4572
4573static void
4574weston_subsurface_link_surface(struct weston_subsurface *sub,
4575 struct weston_surface *surface)
4576{
4577 sub->surface = surface;
4578 sub->surface_destroy_listener.notify =
4579 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004580 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004581 &sub->surface_destroy_listener);
4582}
4583
4584static void
4585weston_subsurface_destroy(struct weston_subsurface *sub)
4586{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004587 struct weston_view *view, *next;
4588
Pekka Paalanene67858b2013-04-25 13:57:42 +03004589 assert(sub->surface);
4590
4591 if (sub->resource) {
4592 assert(weston_surface_to_subsurface(sub->surface) == sub);
4593 assert(sub->parent_destroy_listener.notify ==
4594 subsurface_handle_parent_destroy);
4595
George Kiagiadakised04d382014-06-13 18:10:26 +02004596 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
4597 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004598 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02004599 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05004600
Pekka Paalanene67858b2013-04-25 13:57:42 +03004601 if (sub->parent)
4602 weston_subsurface_unlink_parent(sub);
4603
Jason Ekstrand7b982072014-05-20 14:33:03 -05004604 weston_surface_state_fini(&sub->cached);
4605 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004606
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004607 sub->surface->committed = NULL;
4608 sub->surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004609 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004610 } else {
4611 /* the dummy weston_subsurface for the parent itself */
4612 assert(sub->parent_destroy_listener.notify == NULL);
4613 wl_list_remove(&sub->parent_link);
4614 wl_list_remove(&sub->parent_link_pending);
4615 }
4616
4617 wl_list_remove(&sub->surface_destroy_listener.link);
4618 free(sub);
4619}
4620
4621static const struct wl_subsurface_interface subsurface_implementation = {
4622 subsurface_destroy,
4623 subsurface_set_position,
4624 subsurface_place_above,
4625 subsurface_place_below,
4626 subsurface_set_sync,
4627 subsurface_set_desync
4628};
4629
4630static struct weston_subsurface *
4631weston_subsurface_create(uint32_t id, struct weston_surface *surface,
4632 struct weston_surface *parent)
4633{
4634 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004635 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004636
Bryce Harringtonde16d892014-11-20 22:21:57 -08004637 sub = zalloc(sizeof *sub);
4638 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004639 return NULL;
4640
Jason Ekstranda7af7042013-10-12 22:38:11 -05004641 wl_list_init(&sub->unused_views);
4642
Jason Ekstranda85118c2013-06-27 20:17:02 -05004643 sub->resource =
4644 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004645 if (!sub->resource) {
4646 free(sub);
4647 return NULL;
4648 }
4649
Jason Ekstranda85118c2013-06-27 20:17:02 -05004650 wl_resource_set_implementation(sub->resource,
4651 &subsurface_implementation,
4652 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004653 weston_subsurface_link_surface(sub, surface);
4654 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05004655 weston_surface_state_init(&sub->cached);
4656 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004657 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004658
4659 return sub;
4660}
4661
4662/* Create a dummy subsurface for having the parent itself in its
4663 * sub-surface lists. Makes stacking order manipulation easy.
4664 */
4665static struct weston_subsurface *
4666weston_subsurface_create_for_parent(struct weston_surface *parent)
4667{
4668 struct weston_subsurface *sub;
4669
Bryce Harringtonde16d892014-11-20 22:21:57 -08004670 sub = zalloc(sizeof *sub);
4671 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004672 return NULL;
4673
4674 weston_subsurface_link_surface(sub, parent);
4675 sub->parent = parent;
4676 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4677 wl_list_insert(&parent->subsurface_list_pending,
4678 &sub->parent_link_pending);
4679
4680 return sub;
4681}
4682
4683static void
4684subcompositor_get_subsurface(struct wl_client *client,
4685 struct wl_resource *resource,
4686 uint32_t id,
4687 struct wl_resource *surface_resource,
4688 struct wl_resource *parent_resource)
4689{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004690 struct weston_surface *surface =
4691 wl_resource_get_user_data(surface_resource);
4692 struct weston_surface *parent =
4693 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004694 struct weston_subsurface *sub;
4695 static const char where[] = "get_subsurface: wl_subsurface@";
4696
4697 if (surface == parent) {
4698 wl_resource_post_error(resource,
4699 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4700 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004701 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004702 return;
4703 }
4704
4705 if (weston_surface_to_subsurface(surface)) {
4706 wl_resource_post_error(resource,
4707 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4708 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004709 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004710 return;
4711 }
4712
Pekka Paalanen50b67472014-10-01 15:02:41 +03004713 if (weston_surface_set_role(surface, "wl_subsurface", resource,
4714 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004715 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004716
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004717 if (weston_surface_get_main_surface(parent) == surface) {
4718 wl_resource_post_error(resource,
4719 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4720 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004721 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004722 return;
4723 }
4724
Pekka Paalanene67858b2013-04-25 13:57:42 +03004725 /* make sure the parent is in its own list */
4726 if (wl_list_empty(&parent->subsurface_list)) {
4727 if (!weston_subsurface_create_for_parent(parent)) {
4728 wl_resource_post_no_memory(resource);
4729 return;
4730 }
4731 }
4732
4733 sub = weston_subsurface_create(id, surface, parent);
4734 if (!sub) {
4735 wl_resource_post_no_memory(resource);
4736 return;
4737 }
4738
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004739 surface->committed = subsurface_committed;
4740 surface->committed_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004741 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004742}
4743
4744static void
4745subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
4746{
4747 wl_resource_destroy(resource);
4748}
4749
4750static const struct wl_subcompositor_interface subcompositor_interface = {
4751 subcompositor_destroy,
4752 subcompositor_get_subsurface
4753};
4754
4755static void
4756bind_subcompositor(struct wl_client *client,
4757 void *data, uint32_t version, uint32_t id)
4758{
4759 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004760 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004761
Jason Ekstranda85118c2013-06-27 20:17:02 -05004762 resource =
4763 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004764 if (resource == NULL) {
4765 wl_client_post_no_memory(client);
4766 return;
4767 }
4768 wl_resource_set_implementation(resource, &subcompositor_interface,
4769 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004770}
4771
Bryce Harrington0795ece2016-08-30 12:04:26 -07004772/** Set a DPMS mode on all of the compositor's outputs
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004773 *
4774 * \param compositor The compositor instance
4775 * \param state The DPMS state the outputs will be set to
4776 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004777static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004778weston_compositor_dpms(struct weston_compositor *compositor,
4779 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004780{
4781 struct weston_output *output;
4782
Bryce Harrington08976ac2016-08-30 12:05:16 -07004783 wl_list_for_each(output, &compositor->output_list, link)
4784 if (output->set_dpms)
4785 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004786}
4787
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004788/** Restores the compositor to active status
4789 *
4790 * \param compositor The compositor instance
4791 *
4792 * If the compositor was in a sleeping mode, all outputs are powered
4793 * back on via DPMS. Otherwise if the compositor was inactive
4794 * (idle/locked, offscreen, or sleeping) then the compositor's wake
4795 * signal will fire.
4796 *
4797 * Restarts the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004798 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004799 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004800WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004801weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004802{
Neil Roberts8b62e202013-09-30 13:14:47 +01004803 uint32_t old_state = compositor->state;
4804
4805 /* The state needs to be changed before emitting the wake
4806 * signal because that may try to schedule a repaint which
4807 * will not work if the compositor is still sleeping */
4808 compositor->state = WESTON_COMPOSITOR_ACTIVE;
4809
4810 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004811 case WESTON_COMPOSITOR_SLEEPING:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004812 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004813 case WESTON_COMPOSITOR_OFFSCREEN:
Daniel Stone893b9362016-11-08 15:47:09 +00004814 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004815 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004816 /* fall through */
4817 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004818 wl_event_source_timer_update(compositor->idle_source,
4819 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004820 }
4821}
4822
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004823/** Turns off rendering and frame events for the compositor.
4824 *
4825 * \param compositor The compositor instance
4826 *
4827 * This is used for example to prevent further rendering while the
4828 * compositor is shutting down.
4829 *
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004830 * Stops the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004831 *
4832 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004833 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004834WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004835weston_compositor_offscreen(struct weston_compositor *compositor)
4836{
4837 switch (compositor->state) {
4838 case WESTON_COMPOSITOR_OFFSCREEN:
4839 return;
4840 case WESTON_COMPOSITOR_SLEEPING:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004841 default:
4842 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4843 wl_event_source_timer_update(compositor->idle_source, 0);
4844 }
4845}
4846
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004847/** Powers down all attached output devices
4848 *
4849 * \param compositor The compositor instance
4850 *
4851 * Causes rendering to the outputs to cease, and no frame events to be
4852 * sent. Only powers down the outputs if the compositor is not already
4853 * in sleep mode.
4854 *
4855 * Stops the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004856 *
4857 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004858 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004859WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004860weston_compositor_sleep(struct weston_compositor *compositor)
4861{
4862 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
4863 return;
4864
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004865 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004866 compositor->state = WESTON_COMPOSITOR_SLEEPING;
4867 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
4868}
4869
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004870/** Sets compositor to idle mode
4871 *
4872 * \param data The compositor instance
4873 *
4874 * This is called when the idle timer fires. Once the compositor is in
4875 * idle mode it requires a wake action (e.g. via
4876 * weston_compositor_wake()) to restore it. The compositor's
4877 * idle_signal will be triggered when the idle event occurs.
4878 *
4879 * Idleness can be inhibited by setting the compositor's idle_inhibit
4880 * property.
4881 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004882static int
4883idle_handler(void *data)
4884{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004885 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004886
4887 if (compositor->idle_inhibit)
4888 return 1;
4889
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004890 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004891 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004892
4893 return 1;
4894}
4895
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004896WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08004897weston_plane_init(struct weston_plane *plane,
4898 struct weston_compositor *ec,
4899 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004900{
4901 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004902 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004903 plane->x = x;
4904 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08004905 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004906
4907 /* Init the link so that the call to wl_list_remove() when releasing
4908 * the plane without ever stacking doesn't lead to a crash */
4909 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004910}
4911
4912WL_EXPORT void
4913weston_plane_release(struct weston_plane *plane)
4914{
Xiong Zhang97116532013-10-23 13:58:31 +08004915 struct weston_view *view;
4916
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004917 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004918 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004919
Xiong Zhang97116532013-10-23 13:58:31 +08004920 wl_list_for_each(view, &plane->compositor->view_list, link) {
4921 if (view->plane == plane)
4922 view->plane = NULL;
4923 }
4924
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004925 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004926}
4927
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004928/** weston_compositor_stack_plane
4929 * \ingroup compositor
4930 */
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004931WL_EXPORT void
4932weston_compositor_stack_plane(struct weston_compositor *ec,
4933 struct weston_plane *plane,
4934 struct weston_plane *above)
4935{
4936 if (above)
4937 wl_list_insert(above->link.prev, &plane->link);
4938 else
4939 wl_list_insert(&ec->plane_list, &plane->link);
4940}
4941
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004942static void
4943output_release(struct wl_client *client, struct wl_resource *resource)
4944{
4945 wl_resource_destroy(resource);
4946}
4947
4948static const struct wl_output_interface output_interface = {
4949 output_release,
4950};
4951
4952
Casey Dahlin9074db52012-04-19 22:50:09 -04004953static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004954{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004955 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004956}
4957
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004958static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004959bind_output(struct wl_client *client,
4960 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05004961{
Pekka Paalanen05347622017-03-27 12:24:34 +03004962 struct weston_head *head = data;
4963 struct weston_output *output = head->output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004964 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004965 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004966
Jason Ekstranda85118c2013-06-27 20:17:02 -05004967 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004968 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004969 if (resource == NULL) {
4970 wl_client_post_no_memory(client);
4971 return;
4972 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004973
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03004974 wl_list_insert(&head->resource_list, wl_resource_get_link(resource));
Pekka Paalanen055c1132017-03-27 16:31:25 +03004975 wl_resource_set_implementation(resource, &output_interface, head,
Pekka Paalanen05347622017-03-27 12:24:34 +03004976 unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04004977
Pekka Paalanen05347622017-03-27 12:24:34 +03004978 assert(output);
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004979 wl_output_send_geometry(resource,
4980 output->x,
4981 output->y,
Pekka Paalanen01f60212017-03-24 15:39:24 +02004982 head->mm_width,
4983 head->mm_height,
4984 head->subpixel,
4985 head->make, head->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04004986 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004987 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004988 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02004989 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004990
4991 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004992 wl_output_send_mode(resource,
4993 mode->flags,
4994 mode->width,
4995 mode->height,
4996 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004997 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02004998
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004999 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02005000 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05005001}
5002
Pekka Paalanendcac3512017-12-08 14:13:34 +02005003static void
5004weston_head_add_global(struct weston_head *head)
5005{
5006 head->global = wl_global_create(head->compositor->wl_display,
5007 &wl_output_interface, 3,
5008 head, bind_output);
5009}
5010
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02005011/** Remove the global wl_output protocol object
5012 *
5013 * \param head The head whose global to remove.
5014 *
5015 * Also orphans the wl_resources for this head (wl_output).
5016 */
5017static void
5018weston_head_remove_global(struct weston_head *head)
5019{
5020 struct wl_resource *resource, *tmp;
5021
5022 if (head->global)
5023 wl_global_destroy(head->global);
5024 head->global = NULL;
5025
5026 wl_resource_for_each_safe(resource, tmp, &head->resource_list) {
5027 unbind_resource(resource);
5028 wl_resource_set_destructor(resource, NULL);
5029 wl_resource_set_user_data(resource, NULL);
5030 }
Roman Gilge97391c2019-03-29 13:01:06 +01005031
5032 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
5033 /* It's sufficient to unset the destructor, then the list elements
5034 * won't be accessed.
5035 */
5036 wl_resource_set_destructor(resource, NULL);
5037 }
5038 wl_list_init(&head->xdg_output_resource_list);
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02005039}
5040
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005041/** Get the backing object of wl_output
5042 *
5043 * \param resource A wl_output protocol object.
5044 * \return The backing object (user data) of a wl_resource representing a
5045 * wl_output protocol object.
Marius Vlad78984ee2019-06-11 00:05:08 +03005046 *
5047 * \ingroup head
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005048 */
Pekka Paalanen055c1132017-03-27 16:31:25 +03005049WL_EXPORT struct weston_head *
5050weston_head_from_resource(struct wl_resource *resource)
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005051{
5052 assert(wl_resource_instance_of(resource, &wl_output_interface,
5053 &output_interface));
5054
5055 return wl_resource_get_user_data(resource);
5056}
5057
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005058/** Initialize a pre-allocated weston_head
5059 *
5060 * \param head The head to initialize.
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005061 * \param name The head name, e.g. the connector name or equivalent.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005062 *
5063 * The head will be safe to attach, detach and release.
5064 *
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005065 * The name is used in logs, and can be used by compositors as a configuration
5066 * identifier.
5067 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005068 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005069 * \internal
5070 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005071WL_EXPORT void
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005072weston_head_init(struct weston_head *head, const char *name)
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005073{
5074 /* Add some (in)sane defaults which can be used
5075 * for checking if an output was properly configured
5076 */
5077 memset(head, 0, sizeof *head);
5078
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005079 wl_list_init(&head->compositor_link);
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005080 wl_signal_init(&head->destroy_signal);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005081 wl_list_init(&head->output_link);
5082 wl_list_init(&head->resource_list);
Roman Gilge97391c2019-03-29 13:01:06 +01005083 wl_list_init(&head->xdg_output_resource_list);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005084 head->name = strdup(name);
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305085 head->current_protection = WESTON_HDCP_DISABLE;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005086}
5087
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005088/** Send output heads changed signal
5089 *
5090 * \param output The output that changed.
5091 *
5092 * Notify that the enabled output gained and/or lost heads, or that the
5093 * associated heads may have changed their connection status. This does not
5094 * include cases where the output becomes enabled or disabled. The registered
5095 * callbacks are called after the change has successfully happened.
5096 *
5097 * If connection status change causes the compositor to attach or detach a head
5098 * to an enabled output, the registered callbacks may be called multiple times.
Marius Vlad55d87362019-06-11 01:15:35 +03005099 *
5100 * \ingroup output
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005101 */
5102static void
5103weston_output_emit_heads_changed(struct weston_output *output)
5104{
5105 wl_signal_emit(&output->compositor->output_heads_changed_signal,
5106 output);
5107}
5108
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005109/** Idle task for emitting heads_changed_signal */
5110static void
5111weston_compositor_call_heads_changed(void *data)
5112{
5113 struct weston_compositor *compositor = data;
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005114 struct weston_head *head;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005115
5116 compositor->heads_changed_source = NULL;
5117
5118 wl_signal_emit(&compositor->heads_changed_signal, compositor);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005119
5120 wl_list_for_each(head, &compositor->head_list, compositor_link) {
5121 if (head->output && head->output->enabled)
5122 weston_output_emit_heads_changed(head->output);
5123 }
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005124}
5125
5126/** Schedule a call on idle to heads_changed callback
5127 *
5128 * \param compositor The Compositor.
5129 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005130 * \ingroup compositor
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005131 * \internal
5132 */
5133static void
5134weston_compositor_schedule_heads_changed(struct weston_compositor *compositor)
5135{
5136 struct wl_event_loop *loop;
5137
5138 if (compositor->heads_changed_source)
5139 return;
5140
5141 loop = wl_display_get_event_loop(compositor->wl_display);
5142 compositor->heads_changed_source = wl_event_loop_add_idle(loop,
5143 weston_compositor_call_heads_changed, compositor);
5144}
5145
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005146/** Register a new head
5147 *
5148 * \param compositor The compositor.
5149 * \param head The head to register, must not be already registered.
5150 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005151 * This signals the core that a new head has become available, leading to
5152 * heads_changed hook being called later.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005153 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005154 * \ingroup compositor
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005155 * \internal
5156 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005157WL_EXPORT void
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005158weston_compositor_add_head(struct weston_compositor *compositor,
5159 struct weston_head *head)
5160{
5161 assert(wl_list_empty(&head->compositor_link));
5162 assert(head->name);
5163
5164 wl_list_insert(compositor->head_list.prev, &head->compositor_link);
5165 head->compositor = compositor;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005166 weston_compositor_schedule_heads_changed(compositor);
5167}
5168
5169/** Adds a listener to be called when heads change
5170 *
5171 * \param compositor The compositor.
5172 * \param listener The listener to add.
5173 *
Marius Vlada2dace22019-06-12 16:05:44 +03005174 * The listener notify function argument is weston_compositor.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005175 *
5176 * The listener function will be called after heads are added or their
5177 * connection status has changed. Several changes may be accumulated into a
5178 * single call. The user is expected to iterate over the existing heads and
5179 * check their statuses to find out what changed.
5180 *
5181 * \sa weston_compositor_iterate_heads, weston_head_is_connected,
5182 * weston_head_is_enabled
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005183 * \ingroup compositor
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005184 */
5185WL_EXPORT void
5186weston_compositor_add_heads_changed_listener(struct weston_compositor *compositor,
5187 struct wl_listener *listener)
5188{
5189 wl_signal_add(&compositor->heads_changed_signal, listener);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005190}
5191
5192/** Iterate over available heads
5193 *
5194 * \param compositor The compositor.
Marius Vlada2dace22019-06-12 16:05:44 +03005195 * \param iter The iterator, or NULL for start.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005196 * \return The next available head in the list.
5197 *
5198 * Returns all available heads, regardless of being connected or enabled.
5199 *
5200 * You can iterate over all heads as follows:
5201 * \code
5202 * struct weston_head *head = NULL;
5203 *
5204 * while ((head = weston_compositor_iterate_heads(compositor, head))) {
5205 * ...
5206 * }
5207 * \endcode
5208 *
5209 * If you cause \c iter to be removed from the list, you cannot use it to
5210 * continue iterating. Removing any other item is safe.
5211 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005212 * \ingroup compositor
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005213 */
5214WL_EXPORT struct weston_head *
5215weston_compositor_iterate_heads(struct weston_compositor *compositor,
5216 struct weston_head *iter)
5217{
5218 struct wl_list *list = &compositor->head_list;
5219 struct wl_list *node;
5220
5221 assert(compositor);
5222 assert(!iter || iter->compositor == compositor);
5223
5224 if (iter)
5225 node = iter->compositor_link.next;
5226 else
5227 node = list->next;
5228
5229 assert(node);
5230 assert(!iter || node != &iter->compositor_link);
5231
5232 if (node == list)
5233 return NULL;
5234
5235 return container_of(node, struct weston_head, compositor_link);
5236}
5237
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005238/** Iterate over attached heads
5239 *
5240 * \param output The output whose heads to iterate.
Marius Vlada2dace22019-06-12 16:05:44 +03005241 * \param iter The iterator, or NULL for start.
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005242 * \return The next attached head in the list.
5243 *
5244 * Returns all heads currently attached to the output.
5245 *
5246 * You can iterate over all heads as follows:
5247 * \code
5248 * struct weston_head *head = NULL;
5249 *
5250 * while ((head = weston_output_iterate_heads(output, head))) {
5251 * ...
5252 * }
5253 * \endcode
5254 *
5255 * If you cause \c iter to be removed from the list, you cannot use it to
5256 * continue iterating. Removing any other item is safe.
5257 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05005258 * \ingroup output
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005259 */
5260WL_EXPORT struct weston_head *
5261weston_output_iterate_heads(struct weston_output *output,
5262 struct weston_head *iter)
5263{
5264 struct wl_list *list = &output->head_list;
5265 struct wl_list *node;
5266
5267 assert(output);
5268 assert(!iter || iter->output == output);
5269
5270 if (iter)
5271 node = iter->output_link.next;
5272 else
5273 node = list->next;
5274
5275 assert(node);
5276 assert(!iter || node != &iter->output_link);
5277
5278 if (node == list)
5279 return NULL;
5280
5281 return container_of(node, struct weston_head, output_link);
5282}
5283
Pekka Paalanendcac3512017-12-08 14:13:34 +02005284/** Attach a head to an output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005285 *
5286 * \param output The output to attach to.
5287 * \param head The head that is not yet attached.
5288 * \return 0 on success, -1 on failure.
5289 *
5290 * Attaches the given head to the output. All heads of an output are clones
5291 * and share the resolution and timings.
5292 *
5293 * Cloning heads this way uses less resources than creating an output for
5294 * each head, but is not always possible due to environment, driver and hardware
5295 * limitations.
5296 *
5297 * On failure, the head remains unattached. Success of this function does not
5298 * guarantee the output configuration is actually valid. The final checks are
Pekka Paalanendcac3512017-12-08 14:13:34 +02005299 * made on weston_output_enable() unless the output was already enabled.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005300 *
Marius Vlad55d87362019-06-11 01:15:35 +03005301 * \ingroup output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005302 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005303WL_EXPORT int
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005304weston_output_attach_head(struct weston_output *output,
5305 struct weston_head *head)
5306{
Pekka Paalanendcac3512017-12-08 14:13:34 +02005307 char *head_names;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005308
5309 if (!wl_list_empty(&head->output_link))
5310 return -1;
5311
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005312 if (output->attach_head) {
5313 if (output->attach_head(output, head) < 0)
5314 return -1;
5315 } else if (!wl_list_empty(&output->head_list)) {
5316 /* No support for clones in the legacy path. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005317 return -1;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005318 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005319
5320 head->output = output;
5321 wl_list_insert(output->head_list.prev, &head->output_link);
5322
Pekka Paalanendcac3512017-12-08 14:13:34 +02005323 if (output->enabled) {
5324 weston_head_add_global(head);
5325
5326 head_names = weston_output_create_heads_string(output);
5327 weston_log("Output '%s' updated to have head(s) %s\n",
5328 output->name, head_names);
5329 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005330
5331 weston_output_emit_heads_changed(output);
Pekka Paalanendcac3512017-12-08 14:13:34 +02005332 }
5333
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005334 return 0;
5335}
5336
5337/** Detach a head from its output
5338 *
5339 * \param head The head to detach.
5340 *
5341 * It is safe to detach a non-attached head.
5342 *
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005343 * If the head is attached to an enabled output and the output will be left
5344 * with no heads, the output will be disabled.
5345 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005346 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005347 * \sa weston_output_disable
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005348 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005349WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005350weston_head_detach(struct weston_head *head)
5351{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005352 struct weston_output *output = head->output;
Pekka Paalanena0106992017-12-08 16:11:17 +02005353 char *head_names;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005354
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005355 wl_list_remove(&head->output_link);
5356 wl_list_init(&head->output_link);
5357 head->output = NULL;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005358
5359 if (!output)
5360 return;
5361
5362 if (output->detach_head)
5363 output->detach_head(output, head);
5364
5365 if (output->enabled) {
5366 weston_head_remove_global(head);
5367
Pekka Paalanena0106992017-12-08 16:11:17 +02005368 if (wl_list_empty(&output->head_list)) {
5369 weston_log("Output '%s' no heads left, disabling.\n",
5370 output->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005371 weston_output_disable(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02005372 } else {
5373 head_names = weston_output_create_heads_string(output);
5374 weston_log("Output '%s' updated to have head(s) %s\n",
5375 output->name, head_names);
5376 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005377
5378 weston_output_emit_heads_changed(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02005379 }
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005380 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005381}
5382
5383/** Destroy a head
5384 *
5385 * \param head The head to be released.
5386 *
5387 * Destroys the head. The caller is responsible for freeing the memory pointed
5388 * to by \c head.
5389 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005390 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005391 * \internal
5392 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005393WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005394weston_head_release(struct weston_head *head)
5395{
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005396 wl_signal_emit(&head->destroy_signal, head);
5397
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005398 weston_head_detach(head);
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005399
5400 free(head->make);
5401 free(head->model);
5402 free(head->serial_number);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005403 free(head->name);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005404
5405 wl_list_remove(&head->compositor_link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005406}
5407
Pekka Paalanene19970f2017-08-28 14:11:02 +03005408static void
5409weston_head_set_device_changed(struct weston_head *head)
5410{
5411 head->device_changed = true;
5412
5413 if (head->compositor)
5414 weston_compositor_schedule_heads_changed(head->compositor);
5415}
5416
5417/** String equal comparison with NULLs being equal */
5418static bool
5419str_null_eq(const char *a, const char *b)
5420{
5421 if (!a && !b)
5422 return true;
5423
5424 if (!!a != !!b)
5425 return false;
5426
5427 return strcmp(a, b) == 0;
5428}
5429
Pekka Paalanen01f60212017-03-24 15:39:24 +02005430/** Store monitor make, model and serial number
5431 *
5432 * \param head The head to modify.
5433 * \param make The monitor make. If EDID is available, the PNP ID. Otherwise
5434 * any string, or NULL for none.
5435 * \param model The monitor model or name, or a made-up string, or NULL for
5436 * none.
5437 * \param serialno The monitor serial number, a made-up string, or NULL for
5438 * none.
5439 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005440 * This may set the device_changed flag.
5441 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005442 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005443 * \internal
5444 */
5445WL_EXPORT void
5446weston_head_set_monitor_strings(struct weston_head *head,
5447 const char *make,
5448 const char *model,
5449 const char *serialno)
5450{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005451 if (str_null_eq(head->make, make) &&
5452 str_null_eq(head->model, model) &&
5453 str_null_eq(head->serial_number, serialno))
5454 return;
5455
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005456 free(head->make);
5457 free(head->model);
5458 free(head->serial_number);
5459
5460 head->make = make ? strdup(make) : NULL;
5461 head->model = model ? strdup(model) : NULL;
5462 head->serial_number = serialno ? strdup(serialno) : NULL;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005463
5464 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005465}
5466
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005467/** Store display non-desktop status
5468 *
5469 * \param head The head to modify.
5470 * \param non_desktop Whether the head connects to a non-desktop display.
5471 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005472 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005473 * \internal
5474 */
5475WL_EXPORT void
5476weston_head_set_non_desktop(struct weston_head *head, bool non_desktop)
5477{
5478 if (head->non_desktop == non_desktop)
5479 return;
5480
5481 head->non_desktop = non_desktop;
5482
5483 weston_head_set_device_changed(head);
5484}
5485
Lucas Stacha69cb712019-11-25 23:29:31 +00005486/** Store display transformation
5487 *
5488 * \param head The head to modify.
5489 * \param transform The transformation to apply for this head
5490 *
5491 * This may set the device_changed flag.
5492 *
5493 * \ingroup head
5494 * \internal
5495 */
5496WL_EXPORT void
5497weston_head_set_transform(struct weston_head *head, uint32_t transform)
5498{
5499 if (head->transform == transform)
5500 return;
5501
5502 head->transform = transform;
5503
5504 weston_head_set_device_changed(head);
5505}
5506
5507
Pekka Paalanen01f60212017-03-24 15:39:24 +02005508/** Store physical image size
5509 *
5510 * \param head The head to modify.
5511 * \param mm_width Image area width in millimeters.
5512 * \param mm_height Image area height in millimeters.
5513 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005514 * This may set the device_changed flag.
5515 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005516 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005517 * \internal
5518 */
5519WL_EXPORT void
5520weston_head_set_physical_size(struct weston_head *head,
5521 int32_t mm_width, int32_t mm_height)
5522{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005523 if (head->mm_width == mm_width &&
5524 head->mm_height == mm_height)
5525 return;
5526
Pekka Paalanen01f60212017-03-24 15:39:24 +02005527 head->mm_width = mm_width;
5528 head->mm_height = mm_height;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005529
5530 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005531}
5532
5533/** Store monitor sub-pixel layout
5534 *
5535 * \param head The head to modify.
5536 * \param sp Sub-pixel layout. The possible values are:
5537 * - WL_OUTPUT_SUBPIXEL_UNKNOWN,
5538 * - WL_OUTPUT_SUBPIXEL_NONE,
5539 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB,
5540 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR,
5541 * - WL_OUTPUT_SUBPIXEL_VERTICAL_RGB,
5542 * - WL_OUTPUT_SUBPIXEL_VERTICAL_BGR
5543 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005544 * This may set the device_changed flag.
5545 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005546 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005547 * \internal
5548 */
5549WL_EXPORT void
5550weston_head_set_subpixel(struct weston_head *head,
5551 enum wl_output_subpixel sp)
5552{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005553 if (head->subpixel == sp)
5554 return;
5555
Pekka Paalanen01f60212017-03-24 15:39:24 +02005556 head->subpixel = sp;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005557
5558 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005559}
5560
5561/** Mark the monitor as internal
5562 *
5563 * This is used for embedded screens, like laptop panels.
5564 *
5565 * \param head The head to mark as internal.
5566 *
5567 * By default a head is external. The type is often inferred from the physical
5568 * connector type.
5569 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005570 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005571 * \internal
5572 */
5573WL_EXPORT void
5574weston_head_set_internal(struct weston_head *head)
5575{
5576 head->connection_internal = true;
5577}
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005578
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005579/** Store connector status
5580 *
5581 * \param head The head to modify.
5582 * \param connected Whether the head is connected.
5583 *
5584 * Connectors are created as disconnected. This function can be used to
5585 * set the connector status.
5586 *
5587 * The status should be set to true when a physical connector is connected to
5588 * a video sink device like a monitor and to false when the connector is
5589 * disconnected. For nested backends, the connection status should reflect the
5590 * connection to the parent display server.
5591 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005592 * When the connection status changes, it schedules a call to the heads_changed
Pekka Paalanene19970f2017-08-28 14:11:02 +03005593 * hook and sets the device_changed flag.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005594 *
5595 * \sa weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005596 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005597 * \internal
5598 */
5599WL_EXPORT void
5600weston_head_set_connection_status(struct weston_head *head, bool connected)
5601{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005602 if (head->connected == connected)
5603 return;
5604
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005605 head->connected = connected;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005606
Pekka Paalanene19970f2017-08-28 14:11:02 +03005607 weston_head_set_device_changed(head);
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005608}
5609
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305610static void
5611weston_output_compute_protection(struct weston_output *output)
5612{
5613 struct weston_head *head;
5614 enum weston_hdcp_protection op_protection;
5615 bool op_protection_valid = false;
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305616 struct weston_compositor *wc = output->compositor;
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305617
5618 wl_list_for_each(head, &output->head_list, output_link) {
5619 if (!op_protection_valid) {
5620 op_protection = head->current_protection;
5621 op_protection_valid = true;
5622 }
5623 if (head->current_protection < op_protection)
5624 op_protection = head->current_protection;
5625 }
5626
5627 if (!op_protection_valid)
5628 op_protection = WESTON_HDCP_DISABLE;
5629
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305630 if (output->current_protection != op_protection) {
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305631 output->current_protection = op_protection;
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305632 weston_output_damage(output);
Ankit Nautiyalfaa5ab42019-07-08 13:23:24 +05305633 weston_schedule_surface_protection_update(wc);
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305634 }
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305635}
5636
5637WL_EXPORT void
5638weston_head_set_content_protection_status(struct weston_head *head,
5639 enum weston_hdcp_protection status)
5640{
5641 head->current_protection = status;
5642 if (head->output)
5643 weston_output_compute_protection(head->output);
5644}
5645
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005646/** Is the head currently connected?
5647 *
5648 * \param head The head to query.
5649 * \return Connection status.
5650 *
5651 * Returns true if the head is physically connected to a monitor, or in
5652 * case of a nested backend returns true when there is a connection to the
5653 * parent display server.
5654 *
5655 * This is independent from the head being enabled.
5656 *
5657 * \sa weston_head_is_enabled
Marius Vlad78984ee2019-06-11 00:05:08 +03005658 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005659 */
5660WL_EXPORT bool
5661weston_head_is_connected(struct weston_head *head)
5662{
5663 return head->connected;
5664}
5665
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005666/** Is the head currently enabled?
5667 *
5668 * \param head The head to query.
5669 * \return Video status.
5670 *
5671 * Returns true if the head is currently transmitting a video stream.
5672 *
5673 * This is independent of the head being connected.
5674 *
5675 * \sa weston_head_is_connected
Marius Vlad78984ee2019-06-11 00:05:08 +03005676 * \ingroup head
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005677 */
5678WL_EXPORT bool
5679weston_head_is_enabled(struct weston_head *head)
5680{
5681 if (!head->output)
5682 return false;
5683
5684 return head->output->enabled;
5685}
5686
Pekka Paalanene19970f2017-08-28 14:11:02 +03005687/** Has the device information changed?
5688 *
5689 * \param head The head to query.
5690 * \return True if the device information has changed since last reset.
5691 *
5692 * The information about the connected display device, e.g. a monitor, may
5693 * change without being disconnected in between. Changing information
5694 * causes a call to the heads_changed hook.
5695 *
5696 * The information includes make, model, serial number, physical size,
5697 * and sub-pixel type. The connection status is also included.
5698 *
5699 * \sa weston_head_reset_device_changed, weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005700 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03005701 */
5702WL_EXPORT bool
5703weston_head_is_device_changed(struct weston_head *head)
5704{
5705 return head->device_changed;
5706}
5707
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005708/** Does the head represent a non-desktop display?
5709 *
5710 * \param head The head to query.
5711 * \return True if the device is a non-desktop display.
5712 *
5713 * Non-desktop heads are not attached to outputs by default.
5714 * This stops weston from extending the desktop onto head mounted displays.
5715 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005716 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005717 */
5718WL_EXPORT bool
5719weston_head_is_non_desktop(struct weston_head *head)
5720{
5721 return head->non_desktop;
5722}
5723
Pekka Paalanene19970f2017-08-28 14:11:02 +03005724/** Acknowledge device information change
5725 *
5726 * \param head The head to acknowledge.
5727 *
5728 * Clears the device changed flag on this head. When a compositor has processed
5729 * device information, it should call this to be able to notice further
5730 * changes.
5731 *
5732 * \sa weston_head_is_device_changed
Marius Vlad78984ee2019-06-11 00:05:08 +03005733 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03005734 */
5735WL_EXPORT void
5736weston_head_reset_device_changed(struct weston_head *head)
5737{
5738 head->device_changed = false;
5739}
5740
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005741/** Get the name of a head
5742 *
5743 * \param head The head to query.
5744 * \return The head's name, not NULL.
5745 *
5746 * The name depends on the backend. The DRM backend uses connector names,
5747 * other backends may use hardcoded names or user-given names.
Marius Vlad78984ee2019-06-11 00:05:08 +03005748 *
5749 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005750 */
5751WL_EXPORT const char *
5752weston_head_get_name(struct weston_head *head)
5753{
5754 return head->name;
5755}
5756
5757/** Get the output the head is attached to
5758 *
5759 * \param head The head to query.
5760 * \return The output the head is attached to, or NULL if detached.
Marius Vlad78984ee2019-06-11 00:05:08 +03005761 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005762 */
5763WL_EXPORT struct weston_output *
5764weston_head_get_output(struct weston_head *head)
5765{
5766 return head->output;
5767}
5768
Lucas Stacha69cb712019-11-25 23:29:31 +00005769/** Get the head's native transformation
5770 *
5771 * \param head The head to query.
5772 * \return The head's native transform, as a WL_OUTPUT_TRANSFORM_* value
5773 *
5774 * A weston_head may have a 'native' transform provided by the backend.
5775 * Examples include panels which are physically rotated, where the rotation
5776 * is recorded and described as part of the system configuration. This call
5777 * will return any known native transform for the head.
5778 *
5779 * \ingroup head
5780 */
5781WL_EXPORT uint32_t
5782weston_head_get_transform(struct weston_head *head)
5783{
5784 return head->transform;
5785}
5786
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005787/** Add destroy callback for a head
5788 *
5789 * \param head The head to watch for.
5790 * \param listener The listener to add. The \c notify member must be set.
5791 *
5792 * Heads may get destroyed for various reasons by the backends. If a head is
5793 * attached to an output, the compositor should listen for head destruction
5794 * and reconfigure or destroy the output if necessary.
5795 *
5796 * The destroy callbacks will be called on weston_head destruction before any
5797 * automatic detaching from an associated weston_output and before any
5798 * weston_head information is lost.
5799 *
5800 * The \c data argument to the notify callback is the weston_head being
5801 * destroyed.
Marius Vlad78984ee2019-06-11 00:05:08 +03005802 *
5803 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005804 */
5805WL_EXPORT void
5806weston_head_add_destroy_listener(struct weston_head *head,
5807 struct wl_listener *listener)
5808{
5809 wl_signal_add(&head->destroy_signal, listener);
5810}
5811
5812/** Look up destroy listener for a head
5813 *
5814 * \param head The head to query.
5815 * \param notify The notify function used used for the added destroy listener.
5816 * \return The listener, or NULL if not found.
5817 *
5818 * This looks up the previously added destroy listener struct based on the
5819 * notify function it has. The listener can be used to access user data
5820 * through \c container_of().
5821 *
5822 * \sa wl_signal_get()
Marius Vlad78984ee2019-06-11 00:05:08 +03005823 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005824 */
5825WL_EXPORT struct wl_listener *
5826weston_head_get_destroy_listener(struct weston_head *head,
5827 wl_notify_func_t notify)
5828{
5829 return wl_signal_get(&head->destroy_signal, notify);
5830}
5831
David Fort0de859e2016-05-27 23:22:57 +02005832/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005833static void
David Fort0de859e2016-05-27 23:22:57 +02005834weston_compositor_reflow_outputs(struct weston_compositor *compositor,
5835 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005836{
5837 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02005838 bool start_resizing = false;
5839
5840 if (!delta_width)
5841 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005842
5843 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02005844 if (output == resized_output) {
5845 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005846 continue;
5847 }
5848
David Fort0de859e2016-05-27 23:22:57 +02005849 if (start_resizing) {
5850 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005851 output->dirty = 1;
5852 }
5853 }
5854}
5855
Pekka Paalanen06920792020-12-08 15:22:36 +02005856/** Transform a region in-place from global to output coordinates
5857 *
5858 * \param output The output that defines the transformation.
5859 * \param region The region to be transformed in-place.
5860 *
5861 * This takes a region in the global coordinate system, and takes into account
5862 * output position, transform and scale, and the zoom, and converts the region
5863 * into output pixel coordinates in the framebuffer.
5864 *
5865 * Uses floating-point operations if zoom is active, which may round to expand
5866 * the region.
5867 *
5868 * \internal
5869 * \ingroup output
5870 */
5871WL_EXPORT void
5872weston_output_region_from_global(struct weston_output *output,
5873 pixman_region32_t *region)
5874{
5875 if (output->zoom.active) {
5876 weston_matrix_transform_region(region, &output->matrix, region);
5877 } else {
5878 pixman_region32_translate(region, -output->x, -output->y);
5879 weston_transformed_region(output->width, output->height,
5880 output->transform,
5881 output->current_scale,
5882 region, region);
5883 }
5884}
5885
Pekka Paalanend72bad22017-03-29 17:01:41 +03005886static void
Scott Moreauccbf29d2012-02-22 14:21:41 -07005887weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005888{
Scott Moreau850ca422012-05-21 15:21:25 -06005889 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05005890
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005891 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005892 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06005893
Scott Moreauccbf29d2012-02-22 14:21:41 -07005894 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06005895 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005896 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005897 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005898 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005899 weston_matrix_scale(&output->matrix, magnification,
5900 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005901 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005902
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005903 switch (output->transform) {
5904 case WL_OUTPUT_TRANSFORM_FLIPPED:
5905 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5906 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5907 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5908 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5909 weston_matrix_scale(&output->matrix, -1, 1, 1);
5910 break;
5911 }
5912
5913 switch (output->transform) {
5914 default:
5915 case WL_OUTPUT_TRANSFORM_NORMAL:
5916 case WL_OUTPUT_TRANSFORM_FLIPPED:
5917 break;
5918 case WL_OUTPUT_TRANSFORM_90:
5919 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +02005920 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5921 weston_matrix_rotate_xy(&output->matrix, 0, -1);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005922 break;
5923 case WL_OUTPUT_TRANSFORM_180:
5924 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5925 weston_matrix_translate(&output->matrix,
5926 -output->width, -output->height, 0);
5927 weston_matrix_rotate_xy(&output->matrix, -1, 0);
5928 break;
5929 case WL_OUTPUT_TRANSFORM_270:
5930 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +02005931 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
5932 weston_matrix_rotate_xy(&output->matrix, 0, 1);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005933 break;
5934 }
5935
5936 if (output->current_scale != 1)
5937 weston_matrix_scale(&output->matrix,
5938 output->current_scale,
5939 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01005940
Scott Moreauccbf29d2012-02-22 14:21:41 -07005941 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05005942
5943 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005944}
5945
Scott Moreau1bad5db2012-08-18 01:04:05 -06005946static void
Alexander Larsson0b135062013-05-28 16:23:36 +02005947weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06005948{
5949 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005950 output->native_scale = scale;
5951 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06005952
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005953 convert_size_by_transform_scale(&output->width, &output->height,
5954 output->current_mode->width,
5955 output->current_mode->height,
5956 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02005957}
5958
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005959static void
5960weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07005961{
5962 output->x = x;
5963 output->y = y;
5964
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005965 pixman_region32_fini(&output->region);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005966 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06005967 output->width,
5968 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005969}
5970
Marius Vlad55d87362019-06-11 01:15:35 +03005971/**
5972 * \ingroup output
5973 */
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005974WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005975weston_output_move(struct weston_output *output, int x, int y)
5976{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005977 struct weston_head *head;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005978 struct wl_resource *resource;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005979 int ver;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005980
5981 output->move_x = x - output->x;
5982 output->move_y = y - output->y;
5983
5984 if (output->move_x == 0 && output->move_y == 0)
5985 return;
5986
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005987 weston_output_init_geometry(output, x, y);
5988
5989 output->dirty = 1;
5990
5991 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005992 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005993
5994 /* Notify clients of the change for output position. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005995 wl_list_for_each(head, &output->head_list, output_link) {
5996 wl_resource_for_each(resource, &head->resource_list) {
5997 wl_output_send_geometry(resource,
5998 output->x,
5999 output->y,
6000 head->mm_width,
6001 head->mm_height,
6002 head->subpixel,
6003 head->make,
6004 head->model,
6005 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08006006
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006007 ver = wl_resource_get_version(resource);
6008 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
6009 wl_output_send_done(resource);
6010 }
Roman Gilge97391c2019-03-29 13:01:06 +01006011
6012 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
6013 zxdg_output_v1_send_logical_position(resource,
6014 output->x,
6015 output->y);
6016 zxdg_output_v1_send_done(resource);
6017 }
Quanxian Wangb2c86362014-03-14 09:16:25 +08006018 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02006019}
6020
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006021/** Signal that a pending output is taken into use.
6022 *
6023 * Removes the output from the pending list and adds it to the compositor's
6024 * list of enabled outputs. The output created signal is emitted.
Giulio Camuffob1147152015-05-06 21:41:57 +03006025 *
Pekka Paalanen2210ad02017-03-30 15:48:06 +03006026 * The output gets an internal ID assigned, and the wl_output global is
6027 * created.
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03006028 *
Giulio Camuffob1147152015-05-06 21:41:57 +03006029 * \param compositor The compositor instance.
6030 * \param output The output to be added.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006031 *
6032 * \internal
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006033 * \ingroup compositor
Giulio Camuffob1147152015-05-06 21:41:57 +03006034 */
Pekka Paalanenf9681b52017-03-29 16:58:48 +03006035static void
Giulio Camuffob1147152015-05-06 21:41:57 +03006036weston_compositor_add_output(struct weston_compositor *compositor,
6037 struct weston_output *output)
6038{
Armin Krezoviće5403842016-08-05 15:28:29 +02006039 struct weston_view *view, *next;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03006040 struct weston_head *head;
Armin Krezoviće5403842016-08-05 15:28:29 +02006041
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006042 assert(!output->enabled);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03006043
6044 /* Verify we haven't reached the limit of 32 available output IDs */
6045 assert(ffs(~compositor->output_id_pool) > 0);
6046
6047 /* Invert the output id pool and look for the lowest numbered
6048 * switch (the least significant bit). Take that bit's position
6049 * as our ID, and mark it used in the compositor's output_id_pool.
6050 */
6051 output->id = ffs(~compositor->output_id_pool) - 1;
6052 compositor->output_id_pool |= 1u << output->id;
6053
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006054 wl_list_remove(&output->link);
Giulio Camuffob1147152015-05-06 21:41:57 +03006055 wl_list_insert(compositor->output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006056 output->enabled = true;
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006057
Pekka Paalanendcac3512017-12-08 14:13:34 +02006058 wl_list_for_each(head, &output->head_list, output_link)
6059 weston_head_add_global(head);
Pekka Paalanen2210ad02017-03-30 15:48:06 +03006060
Giulio Camuffob1147152015-05-06 21:41:57 +03006061 wl_signal_emit(&compositor->output_created_signal, output);
Armin Krezoviće5403842016-08-05 15:28:29 +02006062
6063 wl_list_for_each_safe(view, next, &compositor->view_list, link)
6064 weston_view_geometry_dirty(view);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04006065}
6066
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006067/** Transform device coordinates into global coordinates
6068 *
Marius Vlada2dace22019-06-12 16:05:44 +03006069 * \param output the weston_output object
6070 * \param[in] device_x X coordinate in device units.
6071 * \param[in] device_y Y coordinate in device units.
6072 * \param[out] x X coordinate in the global space.
6073 * \param[out] y Y coordinate in the global space.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006074 *
Marius Vlada2dace22019-06-12 16:05:44 +03006075 * Transforms coordinates from the device coordinate space (physical pixel
6076 * units) to the global coordinate space (logical pixel units). This takes
6077 * into account output transform and scale.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006078 *
Marius Vlad55d87362019-06-11 01:15:35 +03006079 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006080 * \internal
6081 */
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006082WL_EXPORT void
6083weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02006084 double device_x, double device_y,
6085 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006086{
Derek Foreman0f679412014-10-02 13:41:17 -05006087 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02006088 device_x,
6089 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05006090 0.0,
6091 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006092
Derek Foreman67a18b92015-03-24 11:36:14 -05006093 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006094
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02006095 *x = p.f[0] / p.f[3];
6096 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006097}
6098
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006099/** Removes output from compositor's list of enabled outputs
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006100 *
6101 * \param output The weston_output object that is being removed.
6102 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006103 * The following happens:
6104 *
6105 * - The output assignments of all views in the current scenegraph are
6106 * recomputed.
6107 *
6108 * - Presentation feedback is discarded.
6109 *
6110 * - Compositor is notified that outputs were changed and
6111 * applies the necessary changes to re-layout outputs.
6112 *
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006113 * - The output is put back in the pending outputs list.
6114 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006115 * - Signal is emitted to notify all users of the weston_output
6116 * object that the output is being destroyed.
6117 *
6118 * - wl_output protocol objects referencing this weston_output
Pekka Paalanen2210ad02017-03-30 15:48:06 +03006119 * are made inert, and the wl_output global is removed.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006120 *
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03006121 * - The output's internal ID is released.
6122 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006123 * \ingroup compositor
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006124 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006125 */
6126static void
6127weston_compositor_remove_output(struct weston_output *output)
6128{
Pekka Paalanenbccda712017-03-29 16:16:04 +03006129 struct weston_compositor *compositor = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006130 struct weston_view *view;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03006131 struct weston_head *head;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006132
6133 assert(output->destroying);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006134 assert(output->enabled);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006135
Pekka Paalanenbccda712017-03-29 16:16:04 +03006136 wl_list_for_each(view, &compositor->view_list, link) {
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006137 if (view->output_mask & (1u << output->id))
6138 weston_view_assign_output(view);
6139 }
6140
6141 weston_presentation_feedback_discard_list(&output->feedback_list);
6142
Pekka Paalanen9711fd92018-06-21 14:26:18 +03006143 weston_compositor_reflow_outputs(compositor, output, -output->width);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006144
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006145 wl_list_remove(&output->link);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006146 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006147 output->enabled = false;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006148
Pekka Paalanenbccda712017-03-29 16:16:04 +03006149 wl_signal_emit(&compositor->output_destroyed_signal, output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006150 wl_signal_emit(&output->destroy_signal, output);
6151
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02006152 wl_list_for_each(head, &output->head_list, output_link)
6153 weston_head_remove_global(head);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03006154
6155 compositor->output_id_pool &= ~(1u << output->id);
6156 output->id = 0xffffffff; /* invalid */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006157}
6158
6159/** Sets the output scale for a given output.
6160 *
6161 * \param output The weston_output object that the scale is set for.
6162 * \param scale Scale factor for the given output.
6163 *
6164 * It only supports setting scale for an output that
6165 * is not enabled and it can only be ran once.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006166 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05006167 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006168 */
6169WL_EXPORT void
6170weston_output_set_scale(struct weston_output *output,
6171 int32_t scale)
6172{
6173 /* We can only set scale on a disabled output */
6174 assert(!output->enabled);
6175
6176 /* We only want to set scale once */
6177 assert(!output->scale);
6178
6179 output->scale = scale;
6180}
6181
6182/** Sets the output transform for a given output.
6183 *
6184 * \param output The weston_output object that the transform is set for.
6185 * \param transform Transform value for the given output.
6186 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006187 * Refer to wl_output::transform section located at
6188 * https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_output
6189 * for list of values that can be passed to this function.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006190 *
Marius Vlad55d87362019-06-11 01:15:35 +03006191 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006192 */
6193WL_EXPORT void
6194weston_output_set_transform(struct weston_output *output,
6195 uint32_t transform)
6196{
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006197 struct weston_pointer_motion_event ev;
6198 struct wl_resource *resource;
6199 struct weston_seat *seat;
6200 pixman_region32_t old_region;
6201 int mid_x, mid_y;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006202 struct weston_head *head;
6203 int ver;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006204
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006205 if (!output->enabled && output->transform == UINT32_MAX) {
6206 output->transform = transform;
6207 return;
6208 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006209
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006210 weston_output_transform_scale_init(output, transform, output->scale);
6211
6212 pixman_region32_init(&old_region);
6213 pixman_region32_copy(&old_region, &output->region);
6214
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006215 weston_output_init_geometry(output, output->x, output->y);
6216
6217 output->dirty = 1;
6218
6219 /* Notify clients of the change for output transform. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006220 wl_list_for_each(head, &output->head_list, output_link) {
6221 wl_resource_for_each(resource, &head->resource_list) {
6222 wl_output_send_geometry(resource,
6223 output->x,
6224 output->y,
6225 head->mm_width,
6226 head->mm_height,
6227 head->subpixel,
6228 head->make,
6229 head->model,
6230 output->transform);
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006231
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006232 ver = wl_resource_get_version(resource);
6233 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
6234 wl_output_send_done(resource);
6235 }
Roman Gilge97391c2019-03-29 13:01:06 +01006236 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
6237 zxdg_output_v1_send_logical_position(resource,
6238 output->x,
6239 output->y);
6240 zxdg_output_v1_send_logical_size(resource,
6241 output->width,
6242 output->height);
6243 zxdg_output_v1_send_done(resource);
6244 }
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006245 }
6246
6247 /* we must ensure that pointers are inside output, otherwise they disappear */
6248 mid_x = output->x + output->width / 2;
6249 mid_y = output->y + output->height / 2;
6250
6251 ev.mask = WESTON_POINTER_MOTION_ABS;
6252 ev.x = wl_fixed_to_double(wl_fixed_from_int(mid_x));
6253 ev.y = wl_fixed_to_double(wl_fixed_from_int(mid_y));
6254
6255 wl_list_for_each(seat, &output->compositor->seat_list, link) {
6256 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
6257
6258 if (pointer && pixman_region32_contains_point(&old_region,
6259 wl_fixed_to_int(pointer->x),
6260 wl_fixed_to_int(pointer->y),
6261 NULL))
6262 weston_pointer_move(pointer, &ev);
6263 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006264}
6265
Pekka Paalanenb5265af2020-12-02 11:57:58 +02006266/** Make the output use renderer shadow buffer.
6267 *
6268 * \param output The weston_output object to modify.
6269 * \return True on success, false if unsupported.
6270 *
6271 * This can only be set on a disabled output object.
6272 *
6273 * This is a temporary API to demonstrate WESTON_CAP_COLOR_OPS and allow
6274 * testing related features. This will be superseded with color management
6275 * API.
6276 *
6277 * By default, a renderer is not using a shadow buffer of its own. Enabling
6278 * a shadow buffer may enable other color related features.
6279 *
6280 * Support depends on the chosen renderer and the graphics driver stack in use.
6281 *
6282 * \ingroup output
6283 */
6284WL_EXPORT bool
6285weston_output_set_renderer_shadow_buffer(struct weston_output *output)
6286{
6287 struct weston_compositor *compositor = output->compositor;
6288
6289 assert(!output->enabled);
6290
6291 if (compositor->capabilities & WESTON_CAP_COLOR_OPS) {
6292 output->use_renderer_shadow_buffer = true;
6293 return true;
6294 }
6295
6296 return false;
6297}
6298
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006299/** Initializes a weston_output object with enough data so
6300 ** an output can be configured.
6301 *
6302 * \param output The weston_output object to initialize
6303 * \param compositor The compositor instance.
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006304 * \param name Name for the output (the string is copied).
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006305 *
6306 * Sets initial values for fields that are expected to be
6307 * configured either by compositors or backends.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006308 *
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006309 * The name is used in logs, and can be used by compositors as a configuration
6310 * identifier.
6311 *
Marius Vlad55d87362019-06-11 01:15:35 +03006312 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006313 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006314 */
6315WL_EXPORT void
Armin Krezović40087402016-09-30 14:11:12 +02006316weston_output_init(struct weston_output *output,
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006317 struct weston_compositor *compositor,
6318 const char *name)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006319{
6320 output->compositor = compositor;
6321 output->destroying = 0;
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006322 output->name = strdup(name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006323 wl_list_init(&output->link);
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006324 wl_signal_init(&output->user_destroy_signal);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006325 output->enabled = false;
Ankit Nautiyal2690a772019-03-25 17:57:59 +05306326 output->desired_protection = WESTON_HDCP_DISABLE;
Ankit Nautiyal2844f8e2019-04-03 10:14:59 +05306327 output->allow_protection = true;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006328
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006329 wl_list_init(&output->head_list);
6330
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006331 /* Add some (in)sane defaults which can be used
6332 * for checking if an output was properly configured
6333 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006334 output->scale = 0;
6335 /* Can't use -1 on uint32_t and 0 is valid enum value */
6336 output->transform = UINT32_MAX;
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006337
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006338 pixman_region32_init(&output->region);
Pekka Paalanen42704142017-09-06 16:47:52 +03006339 wl_list_init(&output->mode_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006340}
6341
6342/** Adds weston_output object to pending output list.
6343 *
6344 * \param output The weston_output object to add
6345 * \param compositor The compositor instance.
6346 *
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03006347 * The opposite of this operation is built into weston_output_release().
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006348 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006349 * \ingroup compositor
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006350 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006351 */
6352WL_EXPORT void
6353weston_compositor_add_pending_output(struct weston_output *output,
6354 struct weston_compositor *compositor)
6355{
Pekka Paalanene952a012017-03-29 17:14:00 +03006356 assert(output->disable);
6357 assert(output->enable);
6358
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006359 wl_list_remove(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006360 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006361}
6362
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006363/** Create a string with the attached heads' names.
6364 *
6365 * The string must be free()'d.
Marius Vlad55d87362019-06-11 01:15:35 +03006366 *
6367 * \ingroup output
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006368 */
6369static char *
6370weston_output_create_heads_string(struct weston_output *output)
6371{
6372 FILE *fp;
6373 char *str = NULL;
6374 size_t size = 0;
6375 struct weston_head *head;
6376 const char *sep = "";
6377
6378 fp = open_memstream(&str, &size);
6379 if (!fp)
6380 return NULL;
6381
6382 wl_list_for_each(head, &output->head_list, output_link) {
6383 fprintf(fp, "%s%s", sep, head->name);
6384 sep = ", ";
6385 }
6386 fclose(fp);
6387
6388 return str;
6389}
6390
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006391/** Constructs a weston_output object that can be used by the compositor.
6392 *
Pekka Paalanencc201e42017-03-30 15:11:25 +03006393 * \param output The weston_output object that needs to be enabled. Must not
Pekka Paalanenddce54d2017-08-23 16:00:21 +03006394 * be enabled already. Must have at least one head attached.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006395 *
6396 * Output coordinates are calculated and each new output is by default
6397 * assigned to the right of previous one.
6398 *
6399 * Sets up the transformation, zoom, and geometry of the output using
6400 * the properties that need to be configured by the compositor.
6401 *
6402 * Establishes a repaint timer for the output with the relevant display
6403 * object's event loop. See output_repaint_timer_handler().
6404 *
6405 * The output is assigned an ID. Weston can support up to 32 distinct
6406 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
6407 * is referred to and used to find the first available ID number, and
6408 * then this ID is marked as used in output_id_pool.
6409 *
6410 * The output is also assigned a Wayland global with the wl_output
6411 * external interface.
6412 *
6413 * Backend specific function is called to set up the output output.
6414 *
6415 * Output is added to the compositor's output list
6416 *
6417 * If the backend specific function fails, the weston_output object
6418 * is returned to a state it was before calling this function and
6419 * is added to the compositor's pending_output_list in case it needs
6420 * to be reconfigured or just so it can be destroyed at shutdown.
6421 *
6422 * 0 is returned on success, -1 on failure.
Marius Vlad55d87362019-06-11 01:15:35 +03006423 *
6424 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006425 */
6426WL_EXPORT int
6427weston_output_enable(struct weston_output *output)
6428{
Armin Krezović782f5df2016-09-30 14:11:11 +02006429 struct weston_compositor *c = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006430 struct weston_output *iterator;
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03006431 struct weston_head *head;
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006432 char *head_names;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006433 int x = 0, y = 0;
6434
Pekka Paalanencc201e42017-03-30 15:11:25 +03006435 if (output->enabled) {
6436 weston_log("Error: attempt to enable an enabled output '%s'\n",
6437 output->name);
6438 return -1;
6439 }
6440
Pekka Paalanenddce54d2017-08-23 16:00:21 +03006441 if (wl_list_empty(&output->head_list)) {
6442 weston_log("Error: cannot enable output '%s' without heads.\n",
6443 output->name);
6444 return -1;
6445 }
6446
Pekka Paalanen586e1ac2017-09-14 16:17:59 +03006447 if (wl_list_empty(&output->mode_list) || !output->current_mode) {
6448 weston_log("Error: no video mode for output '%s'.\n",
6449 output->name);
6450 return -1;
6451 }
6452
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03006453 wl_list_for_each(head, &output->head_list, output_link) {
6454 assert(head->make);
6455 assert(head->model);
6456 }
6457
Armin Krezović782f5df2016-09-30 14:11:11 +02006458 iterator = container_of(c->output_list.prev,
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006459 struct weston_output, link);
6460
Armin Krezović782f5df2016-09-30 14:11:11 +02006461 if (!wl_list_empty(&c->output_list))
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006462 x = iterator->x + iterator->width;
6463
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006464 /* Make sure the scale is set up */
6465 assert(output->scale);
6466
6467 /* Make sure we have a transform set */
6468 assert(output->transform != UINT32_MAX);
6469
Armin Krezović782f5df2016-09-30 14:11:11 +02006470 output->x = x;
6471 output->y = y;
6472 output->dirty = 1;
6473 output->original_scale = output->scale;
6474
Marius Vlad3a2f8292019-11-04 17:53:46 +02006475 wl_signal_init(&output->frame_signal);
6476 wl_signal_init(&output->destroy_signal);
6477
Armin Krezović782f5df2016-09-30 14:11:11 +02006478 weston_output_transform_scale_init(output, output->transform, output->scale);
6479 weston_output_init_zoom(output);
6480
6481 weston_output_init_geometry(output, x, y);
6482 weston_output_damage(output);
6483
Armin Krezović782f5df2016-09-30 14:11:11 +02006484 wl_list_init(&output->animation_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02006485 wl_list_init(&output->feedback_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02006486
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006487 /* Enable the output (set up the crtc or create a
6488 * window representing the output, set up the
6489 * renderer, etc)
6490 */
6491 if (output->enable(output) < 0) {
6492 weston_log("Enabling output \"%s\" failed.\n", output->name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006493 return -1;
6494 }
6495
6496 weston_compositor_add_output(output->compositor, output);
6497
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006498 head_names = weston_output_create_heads_string(output);
6499 weston_log("Output '%s' enabled with head(s) %s\n",
6500 output->name, head_names);
6501 free(head_names);
6502
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006503 return 0;
6504}
6505
6506/** Converts a weston_output object to a pending output state, so it
6507 ** can be configured again or destroyed.
6508 *
6509 * \param output The weston_output object that needs to be disabled.
6510 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006511 * Calls a backend specific function to disable an output, in case
6512 * such function exists.
6513 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03006514 * The backend specific disable function may choose to postpone the disabling
6515 * by returning a negative value, in which case this function returns early.
6516 * In that case the backend will guarantee the output will be disabled soon
6517 * by the backend calling this function again. One must not attempt to re-enable
6518 * the output until that happens.
6519 *
6520 * Otherwise, if the output is being used by the compositor, it is removed
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006521 * from weston's output_list (see weston_compositor_remove_output())
6522 * and is returned to a state it was before weston_output_enable()
6523 * was ran (see weston_output_enable_undo()).
6524 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03006525 * See weston_output_init() for more information on the
6526 * state output is returned to.
Pekka Paalanencc201e42017-03-30 15:11:25 +03006527 *
6528 * If the output has never been enabled yet, this function can still be
6529 * called to ensure that the output is actually turned off rather than left
6530 * in the state it was discovered in.
Marius Vlad55d87362019-06-11 01:15:35 +03006531 *
6532 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006533 */
6534WL_EXPORT void
6535weston_output_disable(struct weston_output *output)
6536{
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006537 /* Should we rename this? */
6538 output->destroying = 1;
6539
Pekka Paalanenc65df642017-03-29 15:45:46 +03006540 /* Disable is called unconditionally also for not-enabled outputs,
6541 * because at compositor start-up, if there is an output that is
6542 * already on but the compositor wants to turn it off, we have to
6543 * forward the turn-off to the backend so it knows to do it.
6544 * The backend cannot initially turn off everything, because it
6545 * would cause unnecessary mode-sets for all outputs the compositor
6546 * wants to be on.
6547 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006548 if (output->disable(output) < 0)
6549 return;
6550
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006551 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006552 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006553
6554 output->destroying = 0;
6555}
6556
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006557/** Forces a synchronous call to heads_changed hook
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006558 *
6559 * \param compositor The compositor instance
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006560 *
6561 * If there are new or changed heads, calls the heads_changed hook and
6562 * returns after the hook returns.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006563 *
6564 * \ingroup compositor
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006565 */
6566WL_EXPORT void
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006567weston_compositor_flush_heads_changed(struct weston_compositor *compositor)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006568{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03006569 if (compositor->heads_changed_source) {
6570 wl_event_source_remove(compositor->heads_changed_source);
6571 weston_compositor_call_heads_changed(compositor);
6572 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006573}
6574
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006575/** Add destroy callback for an output
6576 *
6577 * \param output The output to watch.
6578 * \param listener The listener to add. The \c notify member must be set.
6579 *
6580 * The listener callback will be called when user destroys an output. This
6581 * may be delayed by a backend in some cases. The main purpose of the
6582 * listener is to allow hooking up custom data to the output. The custom data
6583 * can be fetched via weston_output_get_destroy_listener() followed by
6584 * container_of().
6585 *
6586 * The \c data argument to the notify callback is the weston_output being
6587 * destroyed.
6588 *
6589 * @note This is for the final destruction of an output, not when it gets
6590 * disabled. If you want to keep track of enabled outputs, this is not it.
Marius Vlad55d87362019-06-11 01:15:35 +03006591 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05006592 * \ingroup output
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006593 */
6594WL_EXPORT void
6595weston_output_add_destroy_listener(struct weston_output *output,
6596 struct wl_listener *listener)
6597{
6598 wl_signal_add(&output->user_destroy_signal, listener);
6599}
6600
6601/** Look up destroy listener for an output
6602 *
6603 * \param output The output to query.
6604 * \param notify The notify function used used for the added destroy listener.
6605 * \return The listener, or NULL if not found.
6606 *
6607 * This looks up the previously added destroy listener struct based on the
6608 * notify function it has. The listener can be used to access user data
6609 * through \c container_of().
6610 *
6611 * \sa wl_signal_get() weston_output_add_destroy_listener()
Marius Vlad55d87362019-06-11 01:15:35 +03006612 * \ingroup output
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006613 */
6614WL_EXPORT struct wl_listener *
6615weston_output_get_destroy_listener(struct weston_output *output,
6616 wl_notify_func_t notify)
6617{
6618 return wl_signal_get(&output->user_destroy_signal, notify);
6619}
6620
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006621/** Uninitialize an output
6622 *
6623 * Removes the output from the list of enabled outputs if necessary, but
6624 * does not call the backend's output disable function. The output will no
6625 * longer be in the list of pending outputs either.
6626 *
6627 * All fields of weston_output become uninitialized, i.e. should not be used
6628 * anymore. The caller can free the memory after this.
6629 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05006630 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006631 * \internal
6632 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006633WL_EXPORT void
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03006634weston_output_release(struct weston_output *output)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006635{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006636 struct weston_head *head, *tmp;
6637
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006638 output->destroying = 1;
6639
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006640 wl_signal_emit(&output->user_destroy_signal, output);
6641
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03006642 if (output->idle_repaint_source)
6643 wl_event_source_remove(output->idle_repaint_source);
6644
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006645 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006646 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006647
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006648 pixman_region32_fini(&output->region);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006649 wl_list_remove(&output->link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006650
6651 wl_list_for_each_safe(head, tmp, &output->head_list, output_link)
6652 weston_head_detach(head);
6653
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006654 free(output->name);
6655}
6656
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006657/** Find an output by its given name
6658 *
6659 * \param compositor The compositor to search in.
6660 * \param name The output name to search for.
6661 * \return An existing output with the given name, or NULL if not found.
6662 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006663 * \ingroup compositor
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006664 */
6665WL_EXPORT struct weston_output *
6666weston_compositor_find_output_by_name(struct weston_compositor *compositor,
6667 const char *name)
6668{
6669 struct weston_output *output;
6670
6671 wl_list_for_each(output, &compositor->output_list, link)
6672 if (strcmp(output->name, name) == 0)
6673 return output;
6674
6675 wl_list_for_each(output, &compositor->pending_output_list, link)
6676 if (strcmp(output->name, name) == 0)
6677 return output;
6678
6679 return NULL;
6680}
6681
6682/** Create a named output
6683 *
6684 * \param compositor The compositor.
6685 * \param name The name for the output.
6686 * \return A new \c weston_output, or NULL on failure.
6687 *
6688 * This creates a new weston_output that starts with no heads attached.
6689 *
6690 * An output must be configured and it must have at least one head before
6691 * it can be enabled.
6692 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006693 * \ingroup compositor
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006694 */
6695WL_EXPORT struct weston_output *
6696weston_compositor_create_output(struct weston_compositor *compositor,
6697 const char *name)
6698{
6699 assert(compositor->backend->create_output);
6700
6701 if (weston_compositor_find_output_by_name(compositor, name)) {
6702 weston_log("Warning: attempted to create an output with a "
6703 "duplicate name '%s'.\n", name);
6704 return NULL;
6705 }
6706
6707 return compositor->backend->create_output(compositor, name);
6708}
6709
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006710/** Create an output for an unused head
6711 *
6712 * \param compositor The compositor.
6713 * \param head The head to attach to the output.
6714 * \return A new \c weston_output, or NULL on failure.
6715 *
6716 * This creates a new weston_output that starts with the given head attached.
6717 * The output inherits the name of the head. The head must not be already
6718 * attached to another output.
6719 *
6720 * An output must be configured before it can be enabled.
6721 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006722 * \ingroup compositor
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006723 */
6724WL_EXPORT struct weston_output *
6725weston_compositor_create_output_with_head(struct weston_compositor *compositor,
6726 struct weston_head *head)
6727{
6728 struct weston_output *output;
6729
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006730 output = weston_compositor_create_output(compositor, head->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006731 if (!output)
6732 return NULL;
6733
6734 if (weston_output_attach_head(output, head) < 0) {
Pekka Paalanen42c0e142017-10-27 12:07:49 +03006735 weston_output_destroy(output);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006736 return NULL;
6737 }
6738
6739 return output;
6740}
6741
6742/** Destroy an output
6743 *
6744 * \param output The output to destroy.
6745 *
6746 * The heads attached to the given output are detached and become unused again.
6747 *
6748 * It is not necessary to explicitly destroy all outputs at compositor exit.
Leandro Ribeiroca640d52020-01-27 19:12:01 -03006749 * weston_compositor_destroy() will automatically destroy any remaining
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006750 * outputs.
6751 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05006752 * \ingroup output
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006753 */
6754WL_EXPORT void
6755weston_output_destroy(struct weston_output *output)
6756{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006757 output->destroy(output);
6758}
6759
Pekka Paalanencf0a4762017-04-04 16:36:07 +03006760/** When you need a head...
6761 *
6762 * This function is a hack, used until all code has been converted to become
6763 * multi-head aware.
6764 *
6765 * \param output The weston_output whose head to get.
6766 * \return The first head in the output's list.
Marius Vlad55d87362019-06-11 01:15:35 +03006767 *
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -05006768 * \ingroup output
Pekka Paalanencf0a4762017-04-04 16:36:07 +03006769 */
6770WL_EXPORT struct weston_head *
6771weston_output_get_first_head(struct weston_output *output)
6772{
6773 if (wl_list_empty(&output->head_list))
6774 return NULL;
6775
6776 return container_of(output->head_list.next,
6777 struct weston_head, output_link);
6778}
6779
Ankit Nautiyal2844f8e2019-04-03 10:14:59 +05306780/** Allow/Disallow content-protection support for an output
6781 *
6782 * This function sets the allow_protection member for an output. Setting of
6783 * this field will allow the compositor to attempt content-protection for this
6784 * output, for a backend that supports the content-protection protocol.
6785 *
6786 * \param output The weston_output for whom the content-protection is to be
6787 * allowed.
6788 * \param allow_protection The bool value which is to be set.
6789 */
6790WL_EXPORT void
6791weston_output_allow_protection(struct weston_output *output,
6792 bool allow_protection)
6793{
6794 output->allow_protection = allow_protection;
6795}
6796
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01006797static void
Roman Gilge97391c2019-03-29 13:01:06 +01006798xdg_output_unlist(struct wl_resource *resource)
6799{
6800 wl_list_remove(wl_resource_get_link(resource));
6801}
6802
6803static void
6804xdg_output_destroy(struct wl_client *client, struct wl_resource *resource)
6805{
6806 wl_resource_destroy(resource);
6807}
6808
6809static const struct zxdg_output_v1_interface xdg_output_interface = {
6810 xdg_output_destroy
6811};
6812
6813static void
6814xdg_output_manager_destroy(struct wl_client *client,
6815 struct wl_resource *resource)
6816{
6817 wl_resource_destroy(resource);
6818}
6819
6820static void
6821xdg_output_manager_get_xdg_output(struct wl_client *client,
6822 struct wl_resource *manager,
6823 uint32_t id,
6824 struct wl_resource *output_resource)
6825{
6826 int version = wl_resource_get_version(manager);
6827 struct weston_head *head = wl_resource_get_user_data(output_resource);
6828 struct weston_output *output = head->output;
6829 struct wl_resource *resource;
6830
6831 resource = wl_resource_create(client, &zxdg_output_v1_interface,
6832 version, id);
6833 if (resource == NULL) {
6834 wl_client_post_no_memory(client);
6835 return;
6836 }
6837
6838 wl_list_insert(&head->xdg_output_resource_list,
6839 wl_resource_get_link(resource));
6840
6841 wl_resource_set_implementation(resource, &xdg_output_interface,
6842 NULL, xdg_output_unlist);
6843
6844 zxdg_output_v1_send_logical_position(resource, output->x, output->y);
6845 zxdg_output_v1_send_logical_size(resource,
6846 output->width,
6847 output->height);
6848 if (version >= ZXDG_OUTPUT_V1_NAME_SINCE_VERSION)
6849 zxdg_output_v1_send_name(resource, head->name);
6850
6851 zxdg_output_v1_send_done(resource);
6852}
6853
6854static const struct zxdg_output_manager_v1_interface xdg_output_manager_interface = {
6855 xdg_output_manager_destroy,
6856 xdg_output_manager_get_xdg_output
6857};
6858
6859static void
6860bind_xdg_output_manager(struct wl_client *client,
6861 void *data, uint32_t version, uint32_t id)
6862{
6863 struct wl_resource *resource;
6864
6865 resource = wl_resource_create(client, &zxdg_output_manager_v1_interface,
6866 version, id);
6867 if (resource == NULL) {
6868 wl_client_post_no_memory(client);
6869 return;
6870 }
6871
6872 wl_resource_set_implementation(resource, &xdg_output_manager_interface,
6873 NULL, NULL);
6874}
6875
6876static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006877destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006878{
Jonny Lamb74130762013-11-26 18:19:46 +01006879 struct weston_surface *surface =
6880 wl_resource_get_user_data(resource);
6881
Pekka Paalanen4826f872016-04-22 14:14:38 +03006882 if (!surface)
6883 return;
6884
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006885 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02006886 surface->pending.buffer_viewport.buffer.src_width =
6887 wl_fixed_from_int(-1);
6888 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006889 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006890}
6891
6892static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006893viewport_destroy(struct wl_client *client,
6894 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006895{
6896 wl_resource_destroy(resource);
6897}
6898
6899static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006900viewport_set_source(struct wl_client *client,
6901 struct wl_resource *resource,
6902 wl_fixed_t src_x,
6903 wl_fixed_t src_y,
6904 wl_fixed_t src_width,
6905 wl_fixed_t src_height)
6906{
6907 struct weston_surface *surface =
6908 wl_resource_get_user_data(resource);
6909
Pekka Paalanen4826f872016-04-22 14:14:38 +03006910 if (!surface) {
6911 wl_resource_post_error(resource,
6912 WP_VIEWPORT_ERROR_NO_SURFACE,
6913 "wl_surface for this viewport is no longer exists");
6914 return;
6915 }
6916
6917 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03006918 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006919
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006920 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03006921 src_height == wl_fixed_from_int(-1) &&
6922 src_x == wl_fixed_from_int(-1) &&
6923 src_y == wl_fixed_from_int(-1)) {
6924 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006925 surface->pending.buffer_viewport.buffer.src_width =
6926 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006927 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006928 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006929 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006930
Pekka Paalanen201769a2016-04-26 14:42:11 +03006931 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006932 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006933 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03006934 "wl_surface@%d viewport source "
6935 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
6936 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006937 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03006938 wl_fixed_to_double(src_height),
6939 wl_fixed_to_double(src_x),
6940 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006941 return;
6942 }
6943
6944 surface->pending.buffer_viewport.buffer.src_x = src_x;
6945 surface->pending.buffer_viewport.buffer.src_y = src_y;
6946 surface->pending.buffer_viewport.buffer.src_width = src_width;
6947 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006948 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006949}
6950
6951static void
6952viewport_set_destination(struct wl_client *client,
6953 struct wl_resource *resource,
6954 int32_t dst_width,
6955 int32_t dst_height)
6956{
6957 struct weston_surface *surface =
6958 wl_resource_get_user_data(resource);
6959
Pekka Paalanen4826f872016-04-22 14:14:38 +03006960 if (!surface) {
6961 wl_resource_post_error(resource,
6962 WP_VIEWPORT_ERROR_NO_SURFACE,
6963 "wl_surface for this viewport no longer exists");
6964 return;
6965 }
6966
6967 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006968
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006969 if (dst_width == -1 && dst_height == -1) {
6970 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006971 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006972 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006973 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006974 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006975
6976 if (dst_width <= 0 || dst_height <= 0) {
6977 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006978 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006979 "destination size must be positive (%dx%d)",
6980 dst_width, dst_height);
6981 return;
6982 }
6983
6984 surface->pending.buffer_viewport.surface.width = dst_width;
6985 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006986 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006987}
6988
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006989static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006990 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006991 viewport_set_source,
6992 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01006993};
6994
6995static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006996viewporter_destroy(struct wl_client *client,
6997 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006998{
6999 wl_resource_destroy(resource);
7000}
7001
7002static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007003viewporter_get_viewport(struct wl_client *client,
7004 struct wl_resource *viewporter,
7005 uint32_t id,
7006 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01007007{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007008 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007009 struct weston_surface *surface =
7010 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01007011 struct wl_resource *resource;
7012
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007013 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007014 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007015 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007016 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01007017 return;
7018 }
7019
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007020 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02007021 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01007022 if (resource == NULL) {
7023 wl_client_post_no_memory(client);
7024 return;
7025 }
7026
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007027 wl_resource_set_implementation(resource, &viewport_interface,
7028 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01007029
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02007030 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01007031}
7032
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007033static const struct wp_viewporter_interface viewporter_interface = {
7034 viewporter_destroy,
7035 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01007036};
7037
7038static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007039bind_viewporter(struct wl_client *client,
7040 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01007041{
7042 struct wl_resource *resource;
7043
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007044 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06007045 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01007046 if (resource == NULL) {
7047 wl_client_post_no_memory(client);
7048 return;
7049 }
7050
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007051 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01007052 NULL, NULL);
7053}
7054
7055static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04007056destroy_presentation_feedback(struct wl_resource *feedback_resource)
7057{
7058 struct weston_presentation_feedback *feedback;
7059
7060 feedback = wl_resource_get_user_data(feedback_resource);
7061
7062 wl_list_remove(&feedback->link);
7063 free(feedback);
7064}
7065
7066static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007067presentation_destroy(struct wl_client *client, struct wl_resource *resource)
7068{
7069 wl_resource_destroy(resource);
7070}
7071
7072static void
7073presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04007074 struct wl_resource *presentation_resource,
7075 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007076 uint32_t callback)
7077{
Pekka Paalanen133e4392014-09-23 22:08:46 -04007078 struct weston_surface *surface;
7079 struct weston_presentation_feedback *feedback;
7080
7081 surface = wl_resource_get_user_data(surface_resource);
7082
Bryce Harringtonde16d892014-11-20 22:21:57 -08007083 feedback = zalloc(sizeof *feedback);
7084 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04007085 goto err_calloc;
7086
7087 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007088 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04007089 1, callback);
7090 if (!feedback->resource)
7091 goto err_create;
7092
7093 wl_resource_set_implementation(feedback->resource, NULL, feedback,
7094 destroy_presentation_feedback);
7095
7096 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
7097
7098 return;
7099
7100err_create:
7101 free(feedback);
7102
7103err_calloc:
7104 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007105}
7106
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007107static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007108 presentation_destroy,
7109 presentation_feedback
7110};
7111
7112static void
7113bind_presentation(struct wl_client *client,
7114 void *data, uint32_t version, uint32_t id)
7115{
7116 struct weston_compositor *compositor = data;
7117 struct wl_resource *resource;
7118
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007119 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06007120 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007121 if (resource == NULL) {
7122 wl_client_post_no_memory(client);
7123 return;
7124 }
7125
7126 wl_resource_set_implementation(resource, &presentation_implementation,
7127 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007128 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007129}
7130
7131static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05007132compositor_bind(struct wl_client *client,
7133 void *data, uint32_t version, uint32_t id)
7134{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007135 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05007136 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05007137
Jason Ekstranda85118c2013-06-27 20:17:02 -05007138 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06007139 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07007140 if (resource == NULL) {
7141 wl_client_post_no_memory(client);
7142 return;
7143 }
7144
7145 wl_resource_set_implementation(resource, &compositor_interface,
7146 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05007147}
7148
Daniel Stonece62cb32018-07-20 09:46:24 +01007149static const char *
7150output_repaint_status_text(struct weston_output *output)
7151{
7152 switch (output->repaint_status) {
7153 case REPAINT_NOT_SCHEDULED:
7154 return "no repaint";
7155 case REPAINT_BEGIN_FROM_IDLE:
7156 return "start_repaint_loop scheduled";
7157 case REPAINT_SCHEDULED:
7158 return "repaint scheduled";
7159 case REPAINT_AWAITING_COMPLETION:
7160 return "awaiting completion";
7161 }
7162
7163 assert(!"output_repaint_status_text missing enum");
7164 return NULL;
7165}
7166
7167static void
7168debug_scene_view_print_buffer(FILE *fp, struct weston_view *view)
7169{
7170 struct weston_buffer *buffer = view->surface->buffer_ref.buffer;
7171 struct wl_shm_buffer *shm;
7172 struct linux_dmabuf_buffer *dmabuf;
Marius Vlad00a6e012018-11-20 17:52:31 +02007173 const struct pixel_format_info *pixel_info = NULL;
Daniel Stonece62cb32018-07-20 09:46:24 +01007174
7175 if (!buffer) {
7176 fprintf(fp, "\t\t[buffer not available]\n");
7177 return;
7178 }
7179
7180 shm = wl_shm_buffer_get(buffer->resource);
7181 if (shm) {
Marius Vlad00a6e012018-11-20 17:52:31 +02007182 uint32_t _format = wl_shm_buffer_get_format(shm);
7183 pixel_info = pixel_format_get_info_shm(_format);
Daniel Stonece62cb32018-07-20 09:46:24 +01007184 fprintf(fp, "\t\tSHM buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02007185 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
7186 (unsigned long) _format,
7187 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Daniel Stonece62cb32018-07-20 09:46:24 +01007188 return;
7189 }
7190
7191 dmabuf = linux_dmabuf_buffer_get(buffer->resource);
7192 if (dmabuf) {
Marius Vlad00a6e012018-11-20 17:52:31 +02007193 pixel_info = pixel_format_get_info(dmabuf->attributes.format);
Daniel Stonece62cb32018-07-20 09:46:24 +01007194 fprintf(fp, "\t\tdmabuf buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02007195 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
7196 (unsigned long) dmabuf->attributes.format,
7197 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Daniel Stonece62cb32018-07-20 09:46:24 +01007198 fprintf(fp, "\t\t\tmodifier: 0x%llx\n",
7199 (unsigned long long) dmabuf->attributes.modifier[0]);
7200 return;
7201 }
7202
Marius Vlad253ba9a2019-03-17 18:22:21 +02007203 fprintf(fp, "\t\tEGL buffer\n");
Daniel Stonece62cb32018-07-20 09:46:24 +01007204}
7205
7206static void
7207debug_scene_view_print(FILE *fp, struct weston_view *view, int view_idx)
7208{
7209 struct weston_compositor *ec = view->surface->compositor;
7210 struct weston_output *output;
7211 char desc[512];
7212 pixman_box32_t *box;
7213 uint32_t surface_id = 0;
7214 pid_t pid = 0;
7215
7216 if (view->surface->resource) {
7217 struct wl_resource *resource = view->surface->resource;
7218 wl_client_get_credentials(wl_resource_get_client(resource),
7219 &pid, NULL, NULL);
7220 surface_id = wl_resource_get_id(view->surface->resource);
7221 }
7222
7223 if (!view->surface->get_label ||
7224 view->surface->get_label(view->surface, desc, sizeof(desc)) < 0) {
7225 strcpy(desc, "[no description available]");
7226 }
7227 fprintf(fp, "\tView %d (role %s, PID %d, surface ID %u, %s, %p):\n",
7228 view_idx, view->surface->role_name, pid, surface_id,
7229 desc, view);
7230
7231 box = pixman_region32_extents(&view->transform.boundingbox);
7232 fprintf(fp, "\t\tposition: (%d, %d) -> (%d, %d)\n",
7233 box->x1, box->y1, box->x2, box->y2);
7234 box = pixman_region32_extents(&view->transform.opaque);
7235
Michael Olbrichb7e5f102020-08-11 16:33:35 +02007236 if (weston_view_is_opaque(view, &view->transform.boundingbox)) {
Daniel Stonece62cb32018-07-20 09:46:24 +01007237 fprintf(fp, "\t\t[fully opaque]\n");
7238 } else if (!pixman_region32_not_empty(&view->transform.opaque)) {
7239 fprintf(fp, "\t\t[not opaque]\n");
7240 } else {
7241 fprintf(fp, "\t\t[opaque: (%d, %d) -> (%d, %d)]\n",
7242 box->x1, box->y1, box->x2, box->y2);
7243 }
7244
7245 if (view->alpha < 1.0)
7246 fprintf(fp, "\t\talpha: %f\n", view->alpha);
7247
7248 if (view->output_mask != 0) {
7249 bool first_output = true;
7250 fprintf(fp, "\t\toutputs: ");
7251 wl_list_for_each(output, &ec->output_list, link) {
7252 if (!(view->output_mask & (1 << output->id)))
7253 continue;
7254 fprintf(fp, "%s%d (%s)%s",
7255 (first_output) ? "" : ", ",
7256 output->id, output->name,
7257 (view->output == output) ? " (primary)" : "");
7258 first_output = false;
7259 }
7260 } else {
7261 fprintf(fp, "\t\t[no outputs]");
7262 }
7263
7264 fprintf(fp, "\n");
7265
7266 debug_scene_view_print_buffer(fp, view);
7267}
7268
Marius Vlad433f4e72019-02-17 22:14:23 +02007269static void
7270debug_scene_view_print_tree(struct weston_view *view,
Marius Vlada6acfa82019-03-17 18:10:09 +02007271 FILE *fp, int *view_idx)
Marius Vlad433f4e72019-02-17 22:14:23 +02007272{
7273 struct weston_subsurface *sub;
7274 struct weston_view *ev;
7275
7276 /*
7277 * print the view first, then we recursively go on printing
7278 * sub-surfaces. We bail out once no more sub-surfaces are available.
7279 */
Marius Vlada6acfa82019-03-17 18:10:09 +02007280 debug_scene_view_print(fp, view, *view_idx);
Marius Vlad433f4e72019-02-17 22:14:23 +02007281
7282 /* no more sub-surfaces */
7283 if (wl_list_empty(&view->surface->subsurface_list))
7284 return;
7285
7286 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
7287 wl_list_for_each(ev, &sub->surface->views, surface_link) {
Michael Olbrichef5f3232020-04-29 09:03:15 +02007288 /* only print the child views of the current view */
7289 if (ev->parent_view != view)
Marius Vlad433f4e72019-02-17 22:14:23 +02007290 continue;
Marius Vlada6acfa82019-03-17 18:10:09 +02007291
7292 (*view_idx)++;
Marius Vlad433f4e72019-02-17 22:14:23 +02007293 debug_scene_view_print_tree(ev, fp, view_idx);
7294 }
7295 }
7296}
7297
Daniel Stonece62cb32018-07-20 09:46:24 +01007298/**
7299 * Output information on how libweston is currently composing the scene
7300 * graph.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007301 *
7302 * \ingroup compositor
Daniel Stonece62cb32018-07-20 09:46:24 +01007303 */
7304WL_EXPORT char *
7305weston_compositor_print_scene_graph(struct weston_compositor *ec)
7306{
7307 struct weston_output *output;
7308 struct weston_layer *layer;
7309 struct timespec now;
7310 int layer_idx = 0;
7311 FILE *fp;
7312 char *ret;
7313 size_t len;
7314 int err;
7315
7316 fp = open_memstream(&ret, &len);
7317 assert(fp);
7318
7319 weston_compositor_read_presentation_clock(ec, &now);
7320 fprintf(fp, "Weston scene graph at %ld.%09ld:\n\n",
7321 now.tv_sec, now.tv_nsec);
7322
7323 wl_list_for_each(output, &ec->output_list, link) {
7324 struct weston_head *head;
7325 int head_idx = 0;
7326
7327 fprintf(fp, "Output %d (%s):\n", output->id, output->name);
7328 assert(output->enabled);
7329
7330 fprintf(fp, "\tposition: (%d, %d) -> (%d, %d)\n",
7331 output->x, output->y,
7332 output->x + output->width,
7333 output->y + output->height);
7334 fprintf(fp, "\tmode: %dx%d@%.3fHz\n",
7335 output->current_mode->width,
7336 output->current_mode->height,
7337 output->current_mode->refresh / 1000.0);
7338 fprintf(fp, "\tscale: %d\n", output->scale);
7339
7340 fprintf(fp, "\trepaint status: %s\n",
7341 output_repaint_status_text(output));
7342 if (output->repaint_status == REPAINT_SCHEDULED)
7343 fprintf(fp, "\tnext repaint: %ld.%09ld\n",
7344 output->next_repaint.tv_sec,
7345 output->next_repaint.tv_nsec);
7346
7347 wl_list_for_each(head, &output->head_list, output_link) {
7348 fprintf(fp, "\tHead %d (%s): %sconnected\n",
7349 head_idx++, head->name,
7350 (head->connected) ? "" : "not ");
7351 }
7352 }
7353
7354 fprintf(fp, "\n");
7355
7356 wl_list_for_each(layer, &ec->layer_list, link) {
7357 struct weston_view *view;
7358 int view_idx = 0;
7359
7360 fprintf(fp, "Layer %d (pos 0x%lx):\n", layer_idx++,
7361 (unsigned long) layer->position);
7362
7363 if (!weston_layer_mask_is_infinite(layer)) {
7364 fprintf(fp, "\t[mask: (%d, %d) -> (%d,%d)]\n\n",
7365 layer->mask.x1, layer->mask.y1,
7366 layer->mask.x2, layer->mask.y2);
7367 }
7368
Marius Vlada6acfa82019-03-17 18:10:09 +02007369 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
7370 debug_scene_view_print_tree(view, fp, &view_idx);
7371 view_idx++;
7372 }
Daniel Stonece62cb32018-07-20 09:46:24 +01007373
7374 if (wl_list_empty(&layer->view_list.link))
7375 fprintf(fp, "\t[no views]\n");
7376
7377 fprintf(fp, "\n");
7378 }
7379
7380 err = fclose(fp);
7381 assert(err == 0);
7382
7383 return ret;
7384}
7385
7386/**
7387 * Called when the 'scene-graph' debug scope is bound by a client. This
7388 * one-shot weston-debug scope prints the current scene graph when bound,
7389 * and then terminates the stream.
7390 */
7391static void
Marius Vladdad882a2019-07-17 15:43:53 +03007392debug_scene_graph_cb(struct weston_log_subscription *sub, void *data)
Daniel Stonece62cb32018-07-20 09:46:24 +01007393{
7394 struct weston_compositor *ec = data;
7395 char *str = weston_compositor_print_scene_graph(ec);
7396
Marius Vladdad882a2019-07-17 15:43:53 +03007397 weston_log_subscription_printf(sub, "%s", str);
Daniel Stonece62cb32018-07-20 09:46:24 +01007398 free(str);
Marius Vladdad882a2019-07-17 15:43:53 +03007399 weston_log_subscription_complete(sub);
Daniel Stonece62cb32018-07-20 09:46:24 +01007400}
7401
Leandro Ribeiro32a5acd2020-10-19 16:06:22 -03007402/** Retrieve testsuite data from compositor
7403 *
7404 * The testsuite data can be defined by the test suite of projects that uses
7405 * libweston and given to the compositor at the moment of its creation. This
7406 * function should be used when we need to retrieve the testsuite private data
7407 * from the compositor.
7408 *
7409 * \param ec The weston compositor.
7410 * \return The testsuite data.
7411 *
7412 * \ingroup compositor
7413 * \sa weston_compositor_test_data_init
7414 */
7415WL_EXPORT void *
7416weston_compositor_get_test_data(struct weston_compositor *ec)
7417{
7418 return ec->test_data.test_private_data;
7419}
7420
Giulio Camuffo459137b2014-10-11 23:56:24 +03007421/** Create the compositor.
7422 *
7423 * This functions creates and initializes a compositor instance.
7424 *
7425 * \param display The Wayland display to be used.
7426 * \param user_data A pointer to an object that can later be retrieved
Marius Vlada2dace22019-06-12 16:05:44 +03007427 * \param log_ctx A pointer to weston_debug_compositor
Leandro Ribeirocdb72182020-11-06 11:24:22 -03007428 * \param test_data Optional testsuite data, or NULL.
Giulio Camuffo459137b2014-10-11 23:56:24 +03007429 * using the \ref weston_compositor_get_user_data function.
7430 * \return The compositor instance on success or NULL on failure.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007431 *
7432 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007433 */
7434WL_EXPORT struct weston_compositor *
Marius Vlad880b4852019-04-07 17:07:58 +03007435weston_compositor_create(struct wl_display *display,
Leandro Ribeirocdb72182020-11-06 11:24:22 -03007436 struct weston_log_context *log_ctx, void *user_data,
7437 const struct weston_testsuite_data *test_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04007438{
Giulio Camuffo459137b2014-10-11 23:56:24 +03007439 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05007440 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07007441
Leandro Ribeirobd9c0a62020-01-17 10:47:49 -03007442 if (!log_ctx)
7443 return NULL;
7444
Giulio Camuffo459137b2014-10-11 23:56:24 +03007445 ec = zalloc(sizeof *ec);
7446 if (!ec)
7447 return NULL;
7448
Leandro Ribeirocdb72182020-11-06 11:24:22 -03007449 if (test_data)
7450 ec->test_data = *test_data;
7451
Leandro Ribeirobd9c0a62020-01-17 10:47:49 -03007452 ec->weston_log_ctx = log_ctx;
Giulio Camuffo459137b2014-10-11 23:56:24 +03007453 ec->wl_display = display;
7454 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04007455 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07007456 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04007457 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03007458 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03007459 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02007460 wl_signal_init(&ec->idle_signal);
7461 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02007462 wl_signal_init(&ec->show_input_panel_signal);
7463 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02007464 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01007465 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01007466 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02007467 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02007468 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02007469 wl_signal_init(&ec->output_resized_signal);
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03007470 wl_signal_init(&ec->heads_changed_signal);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02007471 wl_signal_init(&ec->output_heads_changed_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07007472 wl_signal_init(&ec->session_signal);
Robert Beckettc569bdc2019-07-10 16:40:12 +01007473 ec->session_active = true;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04007474
Casey Dahlin58ba1372012-04-19 22:50:08 -04007475 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03007476 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04007477
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02007478 ec->activate_serial = 1;
7479
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05007480 ec->touch_mode = WESTON_TOUCH_MODE_NORMAL;
7481
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05307482 ec->content_protection = NULL;
7483
Derek Foreman152254b2015-11-26 14:17:48 -06007484 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04007485 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007486 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05007487
Giulio Camuffo954f1832014-10-11 18:27:30 +03007488 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04007489 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007490 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03007491
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007492 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007493 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007494 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01007495
Roman Gilge97391c2019-03-29 13:01:06 +01007496 if (!wl_global_create(ec->wl_display, &zxdg_output_manager_v1_interface, 2,
7497 ec, bind_xdg_output_manager))
7498 goto fail;
7499
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007500 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007501 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007502 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007503
Jonas Ådahl30d61d82014-10-22 21:21:17 +02007504 if (weston_input_init(ec) != 0)
7505 goto fail;
7506
Jason Ekstranda7af7042013-10-12 22:38:11 -05007507 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02007508 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007509 wl_list_init(&ec->layer_list);
7510 wl_list_init(&ec->seat_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007511 wl_list_init(&ec->pending_output_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007512 wl_list_init(&ec->output_list);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03007513 wl_list_init(&ec->head_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007514 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01007515 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007516 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01007517 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007518 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02007519 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007520
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007521 wl_list_init(&ec->plugin_api_list);
7522
Xiong Zhang97116532013-10-23 13:58:31 +08007523 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02007524 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04007525
Giulio Camuffo459137b2014-10-11 23:56:24 +03007526 wl_data_device_manager_init(ec->wl_display);
7527
7528 wl_display_init_shm(ec->wl_display);
7529
7530 loop = wl_display_get_event_loop(ec->wl_display);
7531 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Daniel Stone6847b852017-03-01 11:34:08 +00007532 ec->repaint_timer =
7533 wl_event_loop_add_timer(loop, output_repaint_timer_handler,
7534 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007535
Quentin Glidic82681572016-12-17 13:40:51 +01007536 weston_layer_init(&ec->fade_layer, ec);
7537 weston_layer_init(&ec->cursor_layer, ec);
7538
7539 weston_layer_set_position(&ec->fade_layer, WESTON_LAYER_POSITION_FADE);
7540 weston_layer_set_position(&ec->cursor_layer,
7541 WESTON_LAYER_POSITION_CURSOR);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007542
Daniel Stonece62cb32018-07-20 09:46:24 +01007543 ec->debug_scene =
Leandro Ribeiroce100192019-12-26 16:35:49 -03007544 weston_compositor_add_log_scope(ec, "scene-graph",
7545 "Scene graph details\n",
7546 debug_scene_graph_cb, NULL,
7547 ec);
Daniel Stonece62cb32018-07-20 09:46:24 +01007548
Marius Vladda104eb2019-09-05 14:31:01 +03007549 ec->timeline =
Leandro Ribeiroce100192019-12-26 16:35:49 -03007550 weston_compositor_add_log_scope(ec, "timeline",
7551 "Timeline event points\n",
7552 weston_timeline_create_subscription,
7553 weston_timeline_destroy_subscription,
7554 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007555 return ec;
7556
7557fail:
7558 free(ec);
7559 return NULL;
7560}
7561
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007562/** weston_compositor_shutdown
7563 * \ingroup compositor
7564 */
Benjamin Franzkeb8263022011-08-30 11:32:47 +02007565WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007566weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07007567{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007568 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07007569
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007570 wl_event_source_remove(ec->idle_source);
7571
Matt Roper361d2ad2011-08-29 13:52:23 -07007572 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02007573 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07007574 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02007575
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007576 /* Destroy all pending outputs associated with this compositor */
7577 wl_list_for_each_safe(output, next, &ec->pending_output_list, link)
7578 output->destroy(output);
7579
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02007580 if (ec->renderer)
7581 ec->renderer->destroy(ec);
7582
Daniel Stone325fc2d2012-05-30 16:31:58 +01007583 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09007584 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01007585 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01007586 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01007587 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02007588 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007589
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04007590 weston_plane_release(&ec->primary_plane);
Matt Roper361d2ad2011-08-29 13:52:23 -07007591}
7592
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007593/** weston_compositor_exit_with_code
7594 * \ingroup compositor
7595 */
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05007596WL_EXPORT void
Adam Jackson3c3f3b12019-10-16 16:02:59 -04007597weston_compositor_exit_with_code(struct weston_compositor *compositor,
Frederic Plourdec336f062014-10-29 14:44:33 -04007598 int exit_code)
7599{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02007600 if (compositor->exit_code == EXIT_SUCCESS)
7601 compositor->exit_code = exit_code;
7602
Giulio Camuffo459137b2014-10-11 23:56:24 +03007603 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04007604}
7605
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007606/** weston_compositor_set_default_pointer_grab
7607 * \ingroup compositor
7608 */
Frederic Plourdec336f062014-10-29 14:44:33 -04007609WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007610weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
7611 const struct weston_pointer_grab_interface *interface)
7612{
7613 struct weston_seat *seat;
7614
7615 ec->default_pointer_grab = interface;
7616 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05007617 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
7618
7619 if (pointer)
7620 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007621 }
7622}
7623
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007624/** weston_compositor_set_presentation_clock
7625 * \ingroup compositor
7626 */
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007627WL_EXPORT int
7628weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
7629 clockid_t clk_id)
7630{
7631 struct timespec ts;
7632
7633 if (clock_gettime(clk_id, &ts) < 0)
7634 return -1;
7635
7636 compositor->presentation_clock = clk_id;
7637
7638 return 0;
7639}
7640
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007641/** For choosing the software clock, when the display hardware or API
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007642 * does not expose a compatible presentation timestamp.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007643 *
7644 * \ingroup compositor
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007645 */
7646WL_EXPORT int
7647weston_compositor_set_presentation_clock_software(
7648 struct weston_compositor *compositor)
7649{
7650 /* In order of preference */
7651 static const clockid_t clocks[] = {
7652 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
7653 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
7654 CLOCK_MONOTONIC, /* no jumps, may crawl */
7655 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
7656 CLOCK_REALTIME /* may jump and crawl */
7657 };
7658 unsigned i;
7659
7660 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
7661 if (weston_compositor_set_presentation_clock(compositor,
7662 clocks[i]) == 0)
7663 return 0;
7664
7665 weston_log("Error: no suitable presentation clock available.\n");
7666
7667 return -1;
7668}
7669
Pekka Paalanen662f3842015-03-18 12:17:26 +02007670/** Read the current time from the Presentation clock
7671 *
7672 * \param compositor
Marius Vlada2dace22019-06-12 16:05:44 +03007673 * \param[out] ts The current time.
Pekka Paalanen662f3842015-03-18 12:17:26 +02007674 *
7675 * \note Reading the current time in user space is always imprecise to some
7676 * degree.
7677 *
7678 * This function is never meant to fail. If reading the clock does fail,
7679 * an error message is logged and a zero time is returned. Callers are not
7680 * supposed to detect or react to failures.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007681 *
7682 * \ingroup compositor
Pekka Paalanen662f3842015-03-18 12:17:26 +02007683 */
7684WL_EXPORT void
7685weston_compositor_read_presentation_clock(
7686 const struct weston_compositor *compositor,
7687 struct timespec *ts)
7688{
7689 static bool warned;
7690 int ret;
7691
7692 ret = clock_gettime(compositor->presentation_clock, ts);
7693 if (ret < 0) {
7694 ts->tv_sec = 0;
7695 ts->tv_nsec = 0;
7696
7697 if (!warned)
7698 weston_log("Error: failure to read "
Antonio Borneo39578632019-04-26 23:57:31 +02007699 "the presentation clock %#x: '%s' (%d)\n",
7700 compositor->presentation_clock,
7701 strerror(errno), errno);
Pekka Paalanen662f3842015-03-18 12:17:26 +02007702 warned = true;
7703 }
7704}
7705
Pekka Paalanen230f3b12014-09-29 14:18:40 -04007706/** Import dmabuf buffer into current renderer
7707 *
7708 * \param compositor
7709 * \param buffer the dmabuf buffer to import
7710 * \return true on usable buffers, false otherwise
7711 *
7712 * This function tests that the linux_dmabuf_buffer is usable
7713 * for the current renderer. Returns false on unusable buffers. Usually
7714 * usability is tested by importing the dmabufs for composition.
7715 *
7716 * This hook is also used for detecting if the renderer supports
7717 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
7718 * supported.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007719 *
7720 * \ingroup compositor
7721 */
Pekka Paalanen230f3b12014-09-29 14:18:40 -04007722WL_EXPORT bool
7723weston_compositor_import_dmabuf(struct weston_compositor *compositor,
7724 struct linux_dmabuf_buffer *buffer)
7725{
7726 struct weston_renderer *renderer;
7727
7728 renderer = compositor->renderer;
7729
7730 if (renderer->import_dmabuf == NULL)
7731 return false;
7732
7733 return renderer->import_dmabuf(compositor, buffer);
7734}
7735
Marius Vlad5a701542019-11-16 20:26:52 +02007736WL_EXPORT bool
7737weston_compositor_dmabuf_can_scanout(struct weston_compositor *compositor,
7738 struct linux_dmabuf_buffer *buffer)
7739{
7740 struct weston_backend *backend = compositor->backend;
7741
7742 if (backend->can_scanout_dmabuf == NULL)
7743 return false;
7744
7745 return backend->can_scanout_dmabuf(compositor, buffer);
7746}
7747
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007748WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05007749weston_version(int *major, int *minor, int *micro)
7750{
7751 *major = WESTON_VERSION_MAJOR;
7752 *minor = WESTON_VERSION_MINOR;
7753 *micro = WESTON_VERSION_MICRO;
7754}
7755
Daniel Stonee03c1112016-11-24 20:45:45 +00007756/**
7757 * Attempts to find a module path from the module map specified in the
7758 * environment. If found, writes the full path into the path variable.
7759 *
7760 * The module map is a string in environment variable WESTON_MODULE_MAP, where
7761 * each entry is of the form "name=path" and entries are separated by
7762 * semicolons. Whitespace is significant.
7763 *
7764 * \param name The name to search for.
7765 * \param path Where the path is written to if found.
7766 * \param path_len Allocated bytes at \c path .
7767 * \returns The length of the string written to path on success, or 0 if the
7768 * module was not specified in the environment map or path_len was too small.
7769 */
7770WL_EXPORT size_t
7771weston_module_path_from_env(const char *name, char *path, size_t path_len)
7772{
7773 const char *mapping = getenv("WESTON_MODULE_MAP");
7774 const char *end;
7775 const int name_len = strlen(name);
7776
7777 if (!mapping)
7778 return 0;
7779
7780 end = mapping + strlen(mapping);
7781 while (mapping < end && *mapping) {
7782 const char *filename, *next;
7783
7784 /* early out: impossibly short string */
7785 if (end - mapping < name_len + 1)
7786 return 0;
7787
7788 filename = &mapping[name_len + 1];
7789 next = strchrnul(mapping, ';');
7790
7791 if (strncmp(mapping, name, name_len) == 0 &&
7792 mapping[name_len] == '=') {
7793 size_t file_len = next - filename; /* no trailing NUL */
7794 if (file_len >= path_len)
7795 return 0;
7796 strncpy(path, filename, file_len);
7797 path[file_len] = '\0';
7798 return file_len;
7799 }
7800
7801 mapping = next + 1;
7802 }
7803
7804 return 0;
7805}
7806
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03007807WL_EXPORT void *
7808weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007809{
7810 char path[PATH_MAX];
7811 void *module, *init;
Daniel Stonebeb97e52016-11-28 12:13:54 +00007812 size_t len;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007813
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08007814 if (name == NULL)
7815 return NULL;
7816
Derek Foreman3f86e502015-06-08 11:46:54 -05007817 if (name[0] != '/') {
Daniel Stonee03c1112016-11-24 20:45:45 +00007818 len = weston_module_path_from_env(name, path, sizeof path);
7819 if (len == 0)
Daniel Stonebeb97e52016-11-28 12:13:54 +00007820 len = snprintf(path, sizeof path, "%s/%s",
7821 LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05007822 } else {
Daniel Stonebeb97e52016-11-28 12:13:54 +00007823 len = snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05007824 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007825
Daniel Stonebeb97e52016-11-28 12:13:54 +00007826 /* snprintf returns the length of the string it would've written,
7827 * _excluding_ the NUL byte. So even being equal to the size of
7828 * our buffer is an error here. */
7829 if (len >= sizeof path)
7830 return NULL;
7831
Kristian Høgsberga6813d22012-09-12 12:21:01 -04007832 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
7833 if (module) {
7834 weston_log("Module '%s' already loaded\n", path);
Pekka Paalanenf696ee92019-11-04 14:07:51 +02007835 } else {
7836 weston_log("Loading module '%s'\n", path);
7837 module = dlopen(path, RTLD_NOW);
7838 if (!module) {
7839 weston_log("Failed to load module: %s\n", dlerror());
7840 return NULL;
7841 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007842 }
7843
7844 init = dlsym(module, entrypoint);
7845 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03007846 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00007847 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007848 return NULL;
7849 }
7850
7851 return init;
7852}
7853
Pekka Paalanen6ffbba32019-11-06 12:59:32 +02007854/** Add a compositor destroy listener only once
7855 *
7856 * \param compositor The compositor whose destroy to watch for.
7857 * \param listener The listener struct to initialize.
7858 * \param destroy_handler The callback when compositor is destroyed.
7859 * \return True if listener is added, or false if there already is a listener
7860 * with the given \c destroy_handler.
7861 *
7862 * This function does nothing and returns false if the given callback function
7863 * is already present in the weston_compositor destroy callbacks list.
7864 * Otherwise, this function initializes the given listener with the given
7865 * callback pointer and adds it to the compositor's destroy callbacks list.
7866 *
7867 * This can be used to ensure that plugin initialization is done only once
7868 * in case the same plugin is loaded multiple times. If this function returns
7869 * false, the plugin should be already initialized successfully.
7870 *
7871 * All plugins should register a destroy listener for cleaning up. Note, that
7872 * the plugin destruction order is not guaranteed: plugins that depend on other
7873 * plugins must be able to be torn down in arbitrary order.
7874 *
Leandro Ribeiroca640d52020-01-27 19:12:01 -03007875 * \sa weston_compositor_destroy
Pekka Paalanen6ffbba32019-11-06 12:59:32 +02007876 */
7877WL_EXPORT bool
7878weston_compositor_add_destroy_listener_once(struct weston_compositor *compositor,
7879 struct wl_listener *listener,
7880 wl_notify_func_t destroy_handler)
7881{
7882 if (wl_signal_get(&compositor->destroy_signal, destroy_handler))
7883 return false;
7884
7885 listener->notify = destroy_handler;
7886 wl_signal_add(&compositor->destroy_signal, listener);
7887 return true;
7888}
Giulio Camuffo459137b2014-10-11 23:56:24 +03007889
Leandro Ribeiroca640d52020-01-27 19:12:01 -03007890/** Destroys the compositor.
Giulio Camuffo459137b2014-10-11 23:56:24 +03007891 *
Leandro Ribeiroca640d52020-01-27 19:12:01 -03007892 * This function cleans up the compositor state and then destroys it.
Giulio Camuffo459137b2014-10-11 23:56:24 +03007893 *
Leandro Ribeiroca640d52020-01-27 19:12:01 -03007894 * @param compositor The compositor to be destroyed.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007895 *
Marius Vlad284d5342019-06-24 12:00:47 +03007896 * @ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007897 */
7898WL_EXPORT void
Leandro Ribeiroca640d52020-01-27 19:12:01 -03007899weston_compositor_destroy(struct weston_compositor *compositor)
Giulio Camuffo459137b2014-10-11 23:56:24 +03007900{
7901 /* prevent further rendering while shutting down */
7902 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
7903
7904 wl_signal_emit(&compositor->destroy_signal, compositor);
7905
7906 weston_compositor_xkb_destroy(compositor);
7907
Giulio Camuffo2d24e642015-10-03 16:25:15 +03007908 if (compositor->backend)
7909 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007910
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03007911 /* The backend is responsible for destroying the heads. */
7912 assert(wl_list_empty(&compositor->head_list));
7913
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007914 weston_plugin_api_destroy_list(compositor);
7915
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03007916 if (compositor->heads_changed_source)
7917 wl_event_source_remove(compositor->heads_changed_source);
7918
Leandro Ribeirof0149642019-12-18 15:52:18 -03007919 weston_log_scope_destroy(compositor->debug_scene);
Daniel Stonece62cb32018-07-20 09:46:24 +01007920 compositor->debug_scene = NULL;
Marius Vladda104eb2019-09-05 14:31:01 +03007921
Leandro Ribeirof0149642019-12-18 15:52:18 -03007922 weston_log_scope_destroy(compositor->timeline);
Marius Vladda104eb2019-09-05 14:31:01 +03007923 compositor->timeline = NULL;
Pekka Paalanena5630ea2017-10-12 13:13:42 +02007924
Giulio Camuffo459137b2014-10-11 23:56:24 +03007925 free(compositor);
7926}
7927
7928/** Instruct the compositor to exit.
7929 *
7930 * This functions does not directly destroy the compositor object, it merely
7931 * command it to start the tear down process. It is not guaranteed that the
7932 * tear down will happen immediately.
7933 *
7934 * \param compositor The compositor to tear down.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007935 *
7936 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007937 */
7938WL_EXPORT void
7939weston_compositor_exit(struct weston_compositor *compositor)
7940{
7941 compositor->exit(compositor);
7942}
7943
7944/** Return the user data stored in the compositor.
7945 *
7946 * This function returns the user data pointer set with user_data parameter
7947 * to the \ref weston_compositor_create function.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007948 *
7949 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007950 */
7951WL_EXPORT void *
7952weston_compositor_get_user_data(struct weston_compositor *compositor)
7953{
7954 return compositor->user_data;
7955}
Pekka Paalanendd186732016-06-03 14:49:54 +03007956
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007957static const char * const backend_map[] = {
7958 [WESTON_BACKEND_DRM] = "drm-backend.so",
7959 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
7960 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
7961 [WESTON_BACKEND_RDP] = "rdp-backend.so",
7962 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
7963 [WESTON_BACKEND_X11] = "x11-backend.so",
7964};
7965
Pekka Paalanendd186732016-06-03 14:49:54 +03007966/** Load a backend into a weston_compositor
7967 *
7968 * A backend must be loaded to make a weston_compositor work. A backend
7969 * provides input and output capabilities, and determines the renderer to use.
7970 *
7971 * \param compositor A compositor that has not had a backend loaded yet.
7972 * \param backend Name of the backend file.
7973 * \param config_base A pointer to a backend-specific configuration
7974 * structure's 'base' member.
7975 *
7976 * \return 0 on success, or -1 on error.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007977 *
7978 * \ingroup compositor
Pekka Paalanendd186732016-06-03 14:49:54 +03007979 */
7980WL_EXPORT int
7981weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007982 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03007983 struct weston_backend_config *config_base)
7984{
7985 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03007986 struct weston_backend_config *config_base);
7987
Pekka Paalanend7e35112017-08-29 17:04:12 +03007988 if (compositor->backend) {
7989 weston_log("Error: attempt to load a backend when one is already loaded\n");
7990 return -1;
7991 }
7992
Quentin Glidic887c0182016-07-10 11:00:53 +02007993 if (backend >= ARRAY_LENGTH(backend_map))
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007994 return -1;
7995
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01007996 backend_init = weston_load_module(backend_map[backend], "weston_backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03007997 if (!backend_init)
7998 return -1;
7999
Pekka Paalanend7e35112017-08-29 17:04:12 +03008000 if (backend_init(compositor, config_base) < 0) {
8001 compositor->backend = NULL;
8002 return -1;
8003 }
8004
8005 return 0;
Pekka Paalanendd186732016-06-03 14:49:54 +03008006}
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008007
Marius Vlad9fdda7f2019-06-11 16:08:55 +03008008/** weston_compositor_load_xwayland
8009 * \ingroup compositor
8010 */
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008011WL_EXPORT int
8012weston_compositor_load_xwayland(struct weston_compositor *compositor)
8013{
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01008014 int (*module_init)(struct weston_compositor *ec);
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008015
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01008016 module_init = weston_load_module("xwayland.so", "weston_module_init");
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008017 if (!module_init)
8018 return -1;
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01008019 if (module_init(compositor) < 0)
Giulio Camuffo9c764df2016-06-29 11:54:27 +02008020 return -1;
8021 return 0;
8022}
Vasilis Liaskovitis486b4632018-10-10 16:14:55 +02008023
8024/** Resolve an internal compositor error by disconnecting the client.
8025 *
8026 * This function is used in cases when the wl_buffer turns out
8027 * unusable and there is no fallback path.
8028 *
8029 * It is possible the fault is caused by a compositor bug, the underlying
8030 * graphics stack bug or normal behaviour, or perhaps a client mistake.
8031 * In any case, the options are to either composite garbage or nothing,
8032 * or disconnect the client. This is a helper function for the latter.
8033 *
8034 * The error is sent as an INVALID_OBJECT error on the client's wl_display.
8035 *
8036 * \param buffer The weston buffer that is unusable.
8037 * \param msg A custom error message attached to the protocol error.
8038 */
8039WL_EXPORT void
8040weston_buffer_send_server_error(struct weston_buffer *buffer,
8041 const char *msg)
8042{
8043 struct wl_client *client;
8044 struct wl_resource *display_resource;
8045 uint32_t id;
8046
8047 assert(buffer->resource);
8048 id = wl_resource_get_id(buffer->resource);
8049 client = wl_resource_get_client(buffer->resource);
8050 display_resource = wl_client_get_object(client, 1);
8051
8052 assert(display_resource);
8053 wl_resource_post_error(display_resource,
8054 WL_DISPLAY_ERROR_INVALID_OBJECT,
8055 "server error with "
8056 "wl_buffer@%u: %s", id, msg);
8057}
Ankit Nautiyal93dde242019-07-08 11:46:42 +05308058
8059WL_EXPORT void
8060weston_output_disable_planes_incr(struct weston_output *output)
8061{
8062 output->disable_planes++;
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05308063 /*
8064 * If disable_planes changes from 0 to non-zero, it means some type of
8065 * recording of content has started, and therefore protection level of
8066 * the protected surfaces must be updated to avoid the recording of
8067 * the protected content.
8068 */
8069 if (output->disable_planes == 1)
8070 weston_schedule_surface_protection_update(output->compositor);
Ankit Nautiyal93dde242019-07-08 11:46:42 +05308071}
8072
8073WL_EXPORT void
8074weston_output_disable_planes_decr(struct weston_output *output)
8075{
8076 output->disable_planes--;
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05308077 /*
8078 * If disable_planes changes from non-zero to 0, it means no content
8079 * recording is going on any more, and the protected and surfaces can be
8080 * shown without any apprehensions about content being recorded.
8081 */
8082 if (output->disable_planes == 0)
8083 weston_schedule_surface_protection_update(output->compositor);
8084
Ankit Nautiyal93dde242019-07-08 11:46:42 +05308085}