blob: 64342592250e41f50c968564c00453aeca711ec7 [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);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002922
2923 if (compositor->backend->repaint_begin)
2924 repaint_data = compositor->backend->repaint_begin(compositor);
2925
2926 wl_list_for_each(output, &compositor->output_list, link) {
2927 ret = weston_output_maybe_repaint(output, &now, repaint_data);
2928 if (ret)
2929 break;
2930 }
2931
2932 if (ret == 0) {
Tomohito Esaki09bfcd62018-06-05 10:37:05 +09002933 if (compositor->backend->repaint_flush)
Antonio Borneoc90fccc2019-06-30 15:51:10 +02002934 ret = compositor->backend->repaint_flush(compositor,
2935 repaint_data);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002936 } else {
Antonio Borneoc90fccc2019-06-30 15:51:10 +02002937 if (compositor->backend->repaint_cancel)
2938 compositor->backend->repaint_cancel(compositor,
2939 repaint_data);
2940 }
2941
2942 if (ret != 0) {
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09002943 wl_list_for_each(output, &compositor->output_list, link) {
2944 if (output->repainted)
2945 weston_output_schedule_repaint_reset(output);
2946 }
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002947 }
Daniel Stone6847b852017-03-01 11:34:08 +00002948
Tomohito Esakiddaf95c2018-07-10 11:47:15 +09002949 wl_list_for_each(output, &compositor->output_list, link)
2950 output->repainted = false;
2951
Daniel Stone6847b852017-03-01 11:34:08 +00002952 output_repaint_timer_arm(compositor);
2953
Pekka Paalanen0513a952014-05-21 16:17:27 +03002954 return 0;
2955}
2956
Pekka Paalanen50aa3a72020-05-28 11:34:04 +03002957/** Convert a presentation timestamp to another clock domain
2958 *
2959 * \param compositor The compositor defines the presentation clock domain.
2960 * \param presentation_stamp The timestamp in presentation clock domain.
2961 * \param presentation_now Current time in presentation clock domain.
2962 * \param target_clock Defines the target clock domain.
2963 *
2964 * This approximation relies on presentation_stamp to be close to current time.
2965 * The further it is from current time and the bigger the speed difference
2966 * between the two clock domains, the bigger the conversion error.
2967 *
2968 * Conversion error due to system load is biased and unbounded.
2969 */
2970static struct timespec
2971convert_presentation_time_now(struct weston_compositor *compositor,
2972 const struct timespec *presentation_stamp,
2973 const struct timespec *presentation_now,
2974 clockid_t target_clock)
2975{
2976 struct timespec target_now = {};
2977 struct timespec target_stamp;
2978 int64_t delta_ns;
2979
2980 if (compositor->presentation_clock == target_clock)
2981 return *presentation_stamp;
2982
2983 clock_gettime(target_clock, &target_now);
2984 delta_ns = timespec_sub_to_nsec(presentation_stamp, presentation_now);
2985 timespec_add_nsec(&target_stamp, &target_now, delta_ns);
2986
2987 return target_stamp;
2988}
2989
Marius Vlad55d87362019-06-11 01:15:35 +03002990/**
2991 * \ingroup output
2992 */
Kristian Høgsbergef044142011-06-21 15:02:12 -04002993WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002994weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002995 const struct timespec *stamp,
2996 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002997{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002998 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002999 int32_t refresh_nsec;
3000 struct timespec now;
Pekka Paalanen50aa3a72020-05-28 11:34:04 +03003001 struct timespec vblank_monotonic;
Daniel Stone6847b852017-03-01 11:34:08 +00003002 int64_t msec_rel;
Pekka Paalanen133e4392014-09-23 22:08:46 -04003003
Daniel Stone05df8c12017-03-03 16:59:42 +00003004 assert(output->repaint_status == REPAINT_AWAITING_COMPLETION);
Daniel Stone3615ce12017-03-01 11:34:05 +00003005 assert(stamp || (presented_flags & WP_PRESENTATION_FEEDBACK_INVALID));
3006
Daniel Stone6847b852017-03-01 11:34:08 +00003007 weston_compositor_read_presentation_clock(compositor, &now);
3008
Daniel Stone3615ce12017-03-01 11:34:05 +00003009 /* If we haven't been supplied any timestamp at all, we don't have a
3010 * timebase to work against, so any delay just wastes time. Push a
3011 * repaint as soon as possible so we can get on with it. */
Daniel Stone6847b852017-03-01 11:34:08 +00003012 if (!stamp) {
3013 output->next_repaint = now;
Daniel Stone3615ce12017-03-01 11:34:05 +00003014 goto out;
Daniel Stone6847b852017-03-01 11:34:08 +00003015 }
Daniel Stone3615ce12017-03-01 11:34:05 +00003016
Pekka Paalanen50aa3a72020-05-28 11:34:04 +03003017 vblank_monotonic = convert_presentation_time_now(compositor,
3018 stamp, &now,
3019 CLOCK_MONOTONIC);
Marius Vlad3203ff62019-09-05 14:56:12 +03003020 TL_POINT(compositor, "core_repaint_finished", TLP_OUTPUT(output),
Pekka Paalanen50aa3a72020-05-28 11:34:04 +03003021 TLP_VBLANK(&vblank_monotonic), TLP_END);
Marius Vladdf9278a2018-03-06 18:56:23 +02003022
Pekka Paalanend7894d02015-07-03 15:08:53 +03003023 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003024 weston_presentation_feedback_present_list(&output->feedback_list,
3025 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02003026 output->msc,
3027 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003028
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02003029 output->frame_time = *stamp;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003030
Daniel Stone6847b852017-03-01 11:34:08 +00003031 timespec_add_nsec(&output->next_repaint, stamp, refresh_nsec);
3032 timespec_add_msec(&output->next_repaint, &output->next_repaint,
3033 -compositor->repaint_msec);
3034 msec_rel = timespec_sub_to_msec(&output->next_repaint, &now);
Daniel Stone84aff5c2017-03-01 11:34:04 +00003035
3036 if (msec_rel < -1000 || msec_rel > 1000) {
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02003037 static bool warned;
3038
3039 if (!warned)
3040 weston_log("Warning: computed repaint delay is "
Daniel Stone6847b852017-03-01 11:34:08 +00003041 "insane: %lld msec\n", (long long) msec_rel);
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02003042 warned = true;
3043
Daniel Stone6847b852017-03-01 11:34:08 +00003044 output->next_repaint = now;
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02003045 }
3046
Mario Kleinerb7df04e2015-06-21 21:25:15 +02003047 /* Called from restart_repaint_loop and restart happens already after
3048 * the deadline given by repaint_msec? In that case we delay until
3049 * the deadline of the next frame, to give clients a more predictable
3050 * timing of the repaint cycle to lock on. */
Daniel Stoneeca5cca2017-02-28 21:53:51 +00003051 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID &&
3052 msec_rel < 0) {
3053 while (timespec_sub_to_nsec(&output->next_repaint, &now) < 0) {
3054 timespec_add_nsec(&output->next_repaint,
3055 &output->next_repaint,
3056 refresh_nsec);
3057 }
3058 }
Mario Kleinerb7df04e2015-06-21 21:25:15 +02003059
Daniel Stone3615ce12017-03-01 11:34:05 +00003060out:
Daniel Stone05df8c12017-03-03 16:59:42 +00003061 output->repaint_status = REPAINT_SCHEDULED;
Daniel Stone6847b852017-03-01 11:34:08 +00003062 output_repaint_timer_arm(compositor);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003063}
3064
3065static void
3066idle_repaint(void *data)
3067{
3068 struct weston_output *output = data;
Antonio Borneoc90fccc2019-06-30 15:51:10 +02003069 int ret;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003070
Daniel Stone05df8c12017-03-03 16:59:42 +00003071 assert(output->repaint_status == REPAINT_BEGIN_FROM_IDLE);
3072 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03003073 output->idle_repaint_source = NULL;
Antonio Borneoc90fccc2019-06-30 15:51:10 +02003074 ret = output->start_repaint_loop(output);
3075 if (ret != 0)
3076 weston_output_schedule_repaint_reset(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003077}
3078
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003079WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003080weston_layer_entry_insert(struct weston_layer_entry *list,
3081 struct weston_layer_entry *entry)
3082{
3083 wl_list_insert(&list->link, &entry->link);
3084 entry->layer = list->layer;
3085}
3086
3087WL_EXPORT void
3088weston_layer_entry_remove(struct weston_layer_entry *entry)
3089{
3090 wl_list_remove(&entry->link);
3091 wl_list_init(&entry->link);
3092 entry->layer = NULL;
3093}
3094
Quentin Glidic82681572016-12-17 13:40:51 +01003095
3096/** Initialize the weston_layer struct.
3097 *
3098 * \param compositor The compositor instance
3099 * \param layer The layer to initialize
3100 */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003101WL_EXPORT void
Quentin Glidic82681572016-12-17 13:40:51 +01003102weston_layer_init(struct weston_layer *layer,
3103 struct weston_compositor *compositor)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003104{
Quentin Glidic82681572016-12-17 13:40:51 +01003105 layer->compositor = compositor;
3106 wl_list_init(&layer->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003107 wl_list_init(&layer->view_list.link);
3108 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003109 weston_layer_set_mask_infinite(layer);
Quentin Glidic82681572016-12-17 13:40:51 +01003110}
3111
3112/** Sets the position of the layer in the layer list. The layer will be placed
3113 * below any layer with the same position value, if any.
3114 * This function is safe to call if the layer is already on the list, but the
3115 * layer may be moved below other layers at the same position, if any.
3116 *
3117 * \param layer The layer to modify
3118 * \param position The position the layer will be placed at
3119 */
3120WL_EXPORT void
3121weston_layer_set_position(struct weston_layer *layer,
3122 enum weston_layer_position position)
3123{
3124 struct weston_layer *below;
3125
3126 wl_list_remove(&layer->link);
3127
3128 /* layer_list is ordered from top to bottom, the last layer being the
3129 * background with the smallest position value */
3130
3131 layer->position = position;
3132 wl_list_for_each_reverse(below, &layer->compositor->layer_list, link) {
3133 if (below->position >= layer->position) {
3134 wl_list_insert(&below->link, &layer->link);
3135 return;
3136 }
3137 }
3138 wl_list_insert(&layer->compositor->layer_list, &layer->link);
3139}
3140
3141/** Hide a layer by taking it off the layer list.
3142 * This function is safe to call if the layer is not on the list.
3143 *
3144 * \param layer The layer to hide
3145 */
3146WL_EXPORT void
3147weston_layer_unset_position(struct weston_layer *layer)
3148{
3149 wl_list_remove(&layer->link);
3150 wl_list_init(&layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003151}
3152
3153WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003154weston_layer_set_mask(struct weston_layer *layer,
3155 int x, int y, int width, int height)
3156{
3157 struct weston_view *view;
3158
3159 layer->mask.x1 = x;
3160 layer->mask.x2 = x + width;
3161 layer->mask.y1 = y;
3162 layer->mask.y2 = y + height;
3163
3164 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
3165 weston_view_geometry_dirty(view);
3166 }
3167}
3168
3169WL_EXPORT void
3170weston_layer_set_mask_infinite(struct weston_layer *layer)
3171{
Adam Jackson3c3f3b12019-10-16 16:02:59 -04003172 struct weston_view *view;
3173
3174 layer->mask.x1 = INT32_MIN;
3175 layer->mask.x2 = INT32_MAX;
3176 layer->mask.y1 = INT32_MIN;
3177 layer->mask.y2 = INT32_MAX;
3178
3179 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
3180 weston_view_geometry_dirty(view);
3181 }
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003182}
3183
Daniel Stone3b775632018-07-20 08:38:25 +01003184WL_EXPORT bool
3185weston_layer_mask_is_infinite(struct weston_layer *layer)
3186{
3187 return layer->mask.x1 == INT32_MIN &&
3188 layer->mask.y1 == INT32_MIN &&
Adam Jackson3c3f3b12019-10-16 16:02:59 -04003189 layer->mask.x2 == INT32_MAX &&
3190 layer->mask.y2 == INT32_MAX;
Daniel Stone3b775632018-07-20 08:38:25 +01003191}
3192
Marius Vlad55d87362019-06-11 01:15:35 +03003193/**
3194 * \ingroup output
3195 */
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003196WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003197weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003198{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003199 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04003200 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01003201
Bryce Harrington08976ac2016-08-30 12:05:16 -07003202 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
3203 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003204 return;
3205
Pekka Paalanenb5026542014-11-12 15:09:24 +02003206 if (!output->repaint_needed)
Marius Vlad3203ff62019-09-05 14:56:12 +03003207 TL_POINT(compositor, "core_repaint_req", TLP_OUTPUT(output), TLP_END);
Pekka Paalanenb5026542014-11-12 15:09:24 +02003208
Kristian Høgsbergef044142011-06-21 15:02:12 -04003209 loop = wl_display_get_event_loop(compositor->wl_display);
Daniel Stone09a97e22017-03-01 11:34:06 +00003210 output->repaint_needed = true;
Daniel Stone05df8c12017-03-03 16:59:42 +00003211
3212 /* If we already have a repaint scheduled for our idle handler,
3213 * no need to set it again. If the repaint has been called but
3214 * not finished, then weston_output_finish_frame() will notice
3215 * that a repaint is needed and schedule one. */
3216 if (output->repaint_status != REPAINT_NOT_SCHEDULED)
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003217 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01003218
Daniel Stone05df8c12017-03-03 16:59:42 +00003219 output->repaint_status = REPAINT_BEGIN_FROM_IDLE;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03003220 assert(!output->idle_repaint_source);
3221 output->idle_repaint_source = wl_event_loop_add_idle(loop, idle_repaint,
3222 output);
Marius Vlad3203ff62019-09-05 14:56:12 +03003223 TL_POINT(compositor, "core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003224}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05003225
Marius Vlad9fdda7f2019-06-11 16:08:55 +03003226/** weston_compositor_schedule_repaint
3227 * \ingroup compositor
3228 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003229WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003230weston_compositor_schedule_repaint(struct weston_compositor *compositor)
3231{
3232 struct weston_output *output;
3233
3234 wl_list_for_each(output, &compositor->output_list, link)
3235 weston_output_schedule_repaint(output);
3236}
3237
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003238static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003239surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003240{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003241 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003242}
3243
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003244static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003245surface_attach(struct wl_client *client,
3246 struct wl_resource *resource,
3247 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
3248{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003249 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05003250 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003251
Kristian Høgsbergab19f932013-08-20 11:30:54 -07003252 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05003253 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07003254 if (buffer == NULL) {
3255 wl_client_post_no_memory(client);
3256 return;
3257 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07003258 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04003259
Pekka Paalanende685b82012-12-04 15:58:12 +02003260 /* Attach, attach, without commit in between does not send
3261 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003262 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003263
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003264 surface->pending.sx = sx;
3265 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01003266 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003267}
3268
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003269static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003270surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003271 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003272 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05003273{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003274 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003275
Derek Foreman57e92ed2015-11-17 14:11:35 -06003276 if (width <= 0 || height <= 0)
3277 return;
3278
Derek Foreman152254b2015-11-26 14:17:48 -06003279 pixman_region32_union_rect(&surface->pending.damage_surface,
3280 &surface->pending.damage_surface,
3281 x, y, width, height);
3282}
3283
3284static void
3285surface_damage_buffer(struct wl_client *client,
3286 struct wl_resource *resource,
3287 int32_t x, int32_t y, int32_t width, int32_t height)
3288{
3289 struct weston_surface *surface = wl_resource_get_user_data(resource);
3290
3291 if (width <= 0 || height <= 0)
3292 return;
3293
3294 pixman_region32_union_rect(&surface->pending.damage_buffer,
3295 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04003296 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003297}
3298
Kristian Høgsberg33418202011-08-16 23:01:28 -04003299static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003300destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04003301{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05003302 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003303
3304 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02003305 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003306}
3307
3308static void
3309surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003310 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04003311{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003312 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003313 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003314
3315 cb = malloc(sizeof *cb);
3316 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003317 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003318 return;
3319 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03003320
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003321 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
3322 callback);
3323 if (cb->resource == NULL) {
3324 free(cb);
3325 wl_resource_post_no_memory(resource);
3326 return;
3327 }
3328
Jason Ekstranda85118c2013-06-27 20:17:02 -05003329 wl_resource_set_implementation(cb->resource, NULL, cb,
3330 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003331
Pekka Paalanenbc106382012-10-10 12:49:31 +03003332 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003333}
3334
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003335static void
3336surface_set_opaque_region(struct wl_client *client,
3337 struct wl_resource *resource,
3338 struct wl_resource *region_resource)
3339{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003340 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003341 struct weston_region *region;
3342
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003343 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003344 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003345 pixman_region32_copy(&surface->pending.opaque,
3346 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003347 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07003348 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003349 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003350}
3351
3352static void
3353surface_set_input_region(struct wl_client *client,
3354 struct wl_resource *resource,
3355 struct wl_resource *region_resource)
3356{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003357 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003358 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003359
3360 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003361 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003362 pixman_region32_copy(&surface->pending.input,
3363 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003364 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003365 pixman_region32_fini(&surface->pending.input);
3366 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003367 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003368}
3369
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003370/* Cause damage to this sub-surface and all its children.
3371 *
3372 * This is useful when there are state changes that need an implicit
3373 * damage, e.g. a z-order change.
3374 */
3375static void
3376weston_surface_damage_subsurfaces(struct weston_subsurface *sub)
3377{
3378 struct weston_subsurface *child;
3379
3380 weston_surface_damage(sub->surface);
3381 sub->reordered = false;
3382
3383 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link)
3384 if (child != sub)
3385 weston_surface_damage_subsurfaces(child);
3386}
3387
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003388static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003389weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003390{
Pekka Paalanene67858b2013-04-25 13:57:42 +03003391 struct weston_subsurface *sub;
3392
3393 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
3394 parent_link_pending) {
3395 wl_list_remove(&sub->parent_link);
3396 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003397
3398 if (sub->reordered)
3399 weston_surface_damage_subsurfaces(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003400 }
3401}
3402
3403static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03003404weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05003405 struct weston_matrix *matrix)
3406{
Pekka Paalanen04baea52016-04-26 15:50:58 +03003407 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05003408 double src_width, src_height, dest_width, dest_height;
3409
3410 weston_matrix_init(matrix);
3411
3412 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
3413 src_width = surface->width_from_buffer;
3414 src_height = surface->height_from_buffer;
3415 } else {
3416 src_width = wl_fixed_to_double(vp->buffer.src_width);
3417 src_height = wl_fixed_to_double(vp->buffer.src_height);
3418 }
3419
3420 if (vp->surface.width == -1) {
3421 dest_width = src_width;
3422 dest_height = src_height;
3423 } else {
3424 dest_width = vp->surface.width;
3425 dest_height = vp->surface.height;
3426 }
3427
3428 if (src_width != dest_width || src_height != dest_height)
3429 weston_matrix_scale(matrix,
3430 src_width / dest_width,
3431 src_height / dest_height, 1);
3432
3433 if (vp->buffer.src_width != wl_fixed_from_int(-1))
3434 weston_matrix_translate(matrix,
3435 wl_fixed_to_double(vp->buffer.src_x),
3436 wl_fixed_to_double(vp->buffer.src_y),
3437 0);
3438
3439 switch (vp->buffer.transform) {
3440 case WL_OUTPUT_TRANSFORM_FLIPPED:
3441 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3442 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3443 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3444 weston_matrix_scale(matrix, -1, 1, 1);
3445 weston_matrix_translate(matrix,
3446 surface->width_from_buffer, 0, 0);
3447 break;
3448 }
3449
3450 switch (vp->buffer.transform) {
3451 default:
3452 case WL_OUTPUT_TRANSFORM_NORMAL:
3453 case WL_OUTPUT_TRANSFORM_FLIPPED:
3454 break;
3455 case WL_OUTPUT_TRANSFORM_90:
3456 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +02003457 weston_matrix_rotate_xy(matrix, 0, -1);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003458 weston_matrix_translate(matrix,
Pekka Paalanen8060d822020-02-06 15:27:54 +02003459 0, surface->width_from_buffer, 0);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003460 break;
3461 case WL_OUTPUT_TRANSFORM_180:
3462 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3463 weston_matrix_rotate_xy(matrix, -1, 0);
3464 weston_matrix_translate(matrix,
3465 surface->width_from_buffer,
3466 surface->height_from_buffer, 0);
3467 break;
3468 case WL_OUTPUT_TRANSFORM_270:
3469 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +02003470 weston_matrix_rotate_xy(matrix, 0, 1);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003471 weston_matrix_translate(matrix,
Pekka Paalanen8060d822020-02-06 15:27:54 +02003472 surface->height_from_buffer, 0, 0);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003473 break;
3474 }
3475
3476 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
3477}
3478
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003479/**
3480 * Compute a + b > c while being safe to overflows.
3481 */
3482static bool
3483fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
3484{
3485 return (int64_t)a + (int64_t)b > (int64_t)c;
3486}
3487
3488static bool
3489weston_surface_is_pending_viewport_source_valid(
3490 const struct weston_surface *surface)
3491{
3492 const struct weston_surface_state *pend = &surface->pending;
3493 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
3494 int width_from_buffer = 0;
3495 int height_from_buffer = 0;
3496 wl_fixed_t w;
3497 wl_fixed_t h;
3498
3499 /* If viewport source rect is not set, it is always ok. */
3500 if (vp->buffer.src_width == wl_fixed_from_int(-1))
3501 return true;
3502
3503 if (pend->newly_attached) {
3504 if (pend->buffer) {
3505 convert_size_by_transform_scale(&width_from_buffer,
3506 &height_from_buffer,
3507 pend->buffer->width,
3508 pend->buffer->height,
3509 vp->buffer.transform,
3510 vp->buffer.scale);
3511 } else {
3512 /* No buffer: viewport is irrelevant. */
3513 return true;
3514 }
3515 } else {
3516 width_from_buffer = surface->width_from_buffer;
3517 height_from_buffer = surface->height_from_buffer;
3518 }
3519
3520 assert((width_from_buffer == 0) == (height_from_buffer == 0));
3521 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
3522
3523 /* No buffer: viewport is irrelevant. */
3524 if (width_from_buffer == 0 || height_from_buffer == 0)
3525 return true;
3526
3527 /* overflow checks for wl_fixed_from_int() */
3528 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
3529 return false;
3530 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
3531 return false;
3532
3533 w = wl_fixed_from_int(width_from_buffer);
3534 h = wl_fixed_from_int(height_from_buffer);
3535
3536 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
3537 return false;
3538 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
3539 return false;
3540
3541 return true;
3542}
3543
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003544static bool
3545fixed_is_integer(wl_fixed_t v)
3546{
3547 return (v & 0xff) == 0;
3548}
3549
3550static bool
3551weston_surface_is_pending_viewport_dst_size_int(
3552 const struct weston_surface *surface)
3553{
3554 const struct weston_buffer_viewport *vp =
3555 &surface->pending.buffer_viewport;
3556
3557 if (vp->surface.width != -1) {
3558 assert(vp->surface.width > 0 && vp->surface.height > 0);
3559 return true;
3560 }
3561
3562 return fixed_is_integer(vp->buffer.src_width) &&
3563 fixed_is_integer(vp->buffer.src_height);
3564}
3565
Derek Foreman152254b2015-11-26 14:17:48 -06003566/* Translate pending damage in buffer co-ordinates to surface
3567 * co-ordinates and union it with a pixman_region32_t.
3568 * This should only be called after the buffer is attached.
3569 */
3570static void
3571apply_damage_buffer(pixman_region32_t *dest,
3572 struct weston_surface *surface,
3573 struct weston_surface_state *state)
3574{
3575 struct weston_buffer *buffer = surface->buffer_ref.buffer;
3576
3577 /* wl_surface.damage_buffer needs to be clipped to the buffer,
3578 * translated into surface co-ordinates and unioned with
3579 * any other surface damage.
3580 * None of this makes sense if there is no buffer though.
3581 */
3582 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
3583 pixman_region32_t buffer_damage;
3584
3585 pixman_region32_intersect_rect(&state->damage_buffer,
3586 &state->damage_buffer,
3587 0, 0, buffer->width,
3588 buffer->height);
3589 pixman_region32_init(&buffer_damage);
3590 weston_matrix_transform_region(&buffer_damage,
3591 &surface->buffer_to_surface_matrix,
3592 &state->damage_buffer);
3593 pixman_region32_union(dest, dest, &buffer_damage);
3594 pixman_region32_fini(&buffer_damage);
3595 }
3596 /* We should clear this on commit even if there was no buffer */
3597 pixman_region32_clear(&state->damage_buffer);
3598}
3599
Jason Ekstrand1e059042014-10-16 10:55:19 -05003600static void
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303601weston_surface_set_desired_protection(struct weston_surface *surface,
3602 enum weston_hdcp_protection protection)
3603{
3604 if (surface->desired_protection == protection)
3605 return;
3606 surface->desired_protection = protection;
3607 weston_surface_damage(surface);
3608}
3609
3610static void
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303611weston_surface_set_protection_mode(struct weston_surface *surface,
3612 enum weston_surface_protection_mode p_mode)
3613{
3614 struct content_protection *cp = surface->compositor->content_protection;
3615 struct protected_surface *psurface;
3616
3617 surface->protection_mode = p_mode;
3618 wl_list_for_each(psurface, &cp->protected_list, link) {
3619 if (!psurface || psurface->surface != surface)
3620 continue;
3621 weston_protected_surface_send_event(psurface,
3622 surface->current_protection);
3623 }
3624}
3625
3626static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05003627weston_surface_commit_state(struct weston_surface *surface,
3628 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003629{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003630 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003631 pixman_region32_t opaque;
3632
Alexander Larsson4ea95522013-05-22 14:41:37 +02003633 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02003634 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03003635 /* wp_viewport.set_source */
3636 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003637 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003638
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003639 /* wl_surface.attach */
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003640 if (state->newly_attached) {
3641 /* zwp_surface_synchronization_v1.set_acquire_fence */
3642 fd_move(&surface->acquire_fence_fd,
3643 &state->acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003644 /* zwp_surface_synchronization_v1.get_release */
3645 weston_buffer_release_move(&surface->buffer_release_ref,
3646 &state->buffer_release_ref);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003647 weston_surface_attach(surface, state->buffer);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003648 }
Jason Ekstrand7b982072014-05-20 14:33:03 -05003649 weston_surface_state_set_buffer(state, NULL);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003650 assert(state->acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003651 assert(state->buffer_release_ref.buffer_release == NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01003652
Jason Ekstrand1e059042014-10-16 10:55:19 -05003653 weston_surface_build_buffer_matrix(surface,
3654 &surface->surface_to_buffer_matrix);
3655 weston_matrix_invert(&surface->buffer_to_surface_matrix,
3656 &surface->surface_to_buffer_matrix);
3657
Jason Ekstrand7b982072014-05-20 14:33:03 -05003658 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003659 weston_surface_update_size(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003660 if (surface->committed)
3661 surface->committed(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003662 }
Giulio Camuffo184df502013-02-21 11:29:21 +01003663
Jason Ekstrand7b982072014-05-20 14:33:03 -05003664 state->sx = 0;
3665 state->sy = 0;
3666 state->newly_attached = 0;
3667 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03003668
Derek Foreman152254b2015-11-26 14:17:48 -06003669 /* wl_surface.damage and wl_surface.damage_buffer */
Marius Vlad2a1b7862019-09-05 13:12:18 +03003670 if (pixman_region32_not_empty(&state->damage_surface) ||
3671 pixman_region32_not_empty(&state->damage_buffer))
Marius Vlad3203ff62019-09-05 14:56:12 +03003672 TL_POINT(surface->compositor, "core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06003673
Pekka Paalanen8e159182012-10-10 12:49:25 +03003674 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06003675 &state->damage_surface);
3676
3677 apply_damage_buffer(&surface->damage, surface, state);
3678
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05003679 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07003680 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06003681 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003682
3683 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003684 pixman_region32_init(&opaque);
3685 pixman_region32_intersect_rect(&opaque, &state->opaque,
3686 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003687
3688 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
3689 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003690 wl_list_for_each(view, &surface->views, surface_link)
3691 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003692 }
3693
3694 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003695
3696 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003697 pixman_region32_intersect_rect(&surface->input, &state->input,
3698 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003699
Pekka Paalanenbc106382012-10-10 12:49:31 +03003700 /* wl_surface.frame */
3701 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05003702 &state->frame_callback_list);
3703 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003704
3705 /* XXX:
3706 * What should happen with a feedback request, if there
3707 * is no wl_buffer attached for this commit?
3708 */
3709
3710 /* presentation.feedback */
3711 wl_list_insert_list(&surface->feedback_list,
3712 &state->feedback_list);
3713 wl_list_init(&state->feedback_list);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003714
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303715 /* weston_protected_surface.enforced/relaxed */
3716 if (surface->protection_mode != state->protection_mode)
3717 weston_surface_set_protection_mode(surface,
3718 state->protection_mode);
3719
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303720 /* weston_protected_surface.set_type */
3721 weston_surface_set_desired_protection(surface, state->desired_protection);
3722
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003723 wl_signal_emit(&surface->commit_signal, surface);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003724}
3725
3726static void
3727weston_surface_commit(struct weston_surface *surface)
3728{
3729 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003730
Pekka Paalanene67858b2013-04-25 13:57:42 +03003731 weston_surface_commit_subsurface_order(surface);
3732
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003733 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003734}
3735
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003736static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003737weston_subsurface_commit(struct weston_subsurface *sub);
3738
3739static void
3740weston_subsurface_parent_commit(struct weston_subsurface *sub,
3741 int parent_is_synchronized);
3742
3743static void
3744surface_commit(struct wl_client *client, struct wl_resource *resource)
3745{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003746 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003747 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
3748
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003749 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
3750 assert(surface->viewport_resource);
3751
3752 wl_resource_post_error(surface->viewport_resource,
3753 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
3754 "wl_surface@%d has viewport source outside buffer",
3755 wl_resource_get_id(resource));
3756 return;
3757 }
3758
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003759 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
3760 assert(surface->viewport_resource);
3761
3762 wl_resource_post_error(surface->viewport_resource,
3763 WP_VIEWPORT_ERROR_BAD_SIZE,
3764 "wl_surface@%d viewport dst size not integer",
3765 wl_resource_get_id(resource));
3766 return;
3767 }
3768
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003769 if (surface->pending.acquire_fence_fd >= 0) {
3770 assert(surface->synchronization_resource);
3771
3772 if (!surface->pending.buffer) {
3773 fd_clear(&surface->pending.acquire_fence_fd);
3774 wl_resource_post_error(surface->synchronization_resource,
3775 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3776 "wl_surface@%"PRIu32" no buffer for synchronization",
3777 wl_resource_get_id(resource));
3778 return;
3779 }
3780
3781 /* We support fences for both wp_linux_dmabuf and opaque EGL
3782 * buffers, as mandated by minor version 2 of the
3783 * zwp_linux_explicit_synchronization_v1 protocol. Since
3784 * renderers that support fences currently only support these
3785 * two buffer types plus SHM buffers, we can just check for the
3786 * SHM buffer case here.
3787 */
3788 if (wl_shm_buffer_get(surface->pending.buffer->resource)) {
3789 fd_clear(&surface->pending.acquire_fence_fd);
3790 wl_resource_post_error(surface->synchronization_resource,
3791 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_UNSUPPORTED_BUFFER,
3792 "wl_surface@%"PRIu32" unsupported buffer for synchronization",
3793 wl_resource_get_id(resource));
3794 return;
3795 }
3796 }
3797
Alexandros Frantzis67629672018-10-19 12:14:11 +03003798 if (surface->pending.buffer_release_ref.buffer_release &&
3799 !surface->pending.buffer) {
3800 assert(surface->synchronization_resource);
3801
3802 wl_resource_post_error(surface->synchronization_resource,
3803 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3804 "wl_surface@%"PRIu32" no buffer for synchronization",
3805 wl_resource_get_id(resource));
3806 return;
3807 }
3808
Pekka Paalanene67858b2013-04-25 13:57:42 +03003809 if (sub) {
3810 weston_subsurface_commit(sub);
3811 return;
3812 }
3813
3814 weston_surface_commit(surface);
3815
3816 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
3817 if (sub->surface != surface)
3818 weston_subsurface_parent_commit(sub, 0);
3819 }
3820}
3821
3822static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003823surface_set_buffer_transform(struct wl_client *client,
3824 struct wl_resource *resource, int transform)
3825{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003826 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003827
Jonny Lamba55f1392014-05-30 12:07:15 +02003828 /* if wl_output.transform grows more members this will need to be updated. */
3829 if (transform < 0 ||
3830 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
3831 wl_resource_post_error(resource,
3832 WL_SURFACE_ERROR_INVALID_TRANSFORM,
3833 "buffer transform must be a valid transform "
3834 "('%d' specified)", transform);
3835 return;
3836 }
3837
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003838 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003839 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003840}
3841
Alexander Larsson4ea95522013-05-22 14:41:37 +02003842static void
3843surface_set_buffer_scale(struct wl_client *client,
3844 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003845 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003846{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003847 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003848
Jonny Lamba55f1392014-05-30 12:07:15 +02003849 if (scale < 1) {
3850 wl_resource_post_error(resource,
3851 WL_SURFACE_ERROR_INVALID_SCALE,
3852 "buffer scale must be at least one "
3853 "('%d' specified)", scale);
3854 return;
3855 }
3856
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003857 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003858 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003859}
3860
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003861static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003862 surface_destroy,
3863 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04003864 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003865 surface_frame,
3866 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003867 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003868 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02003869 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06003870 surface_set_buffer_scale,
3871 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003872};
3873
3874static void
3875compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003876 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003877{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04003878 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003879 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003880
Kristian Høgsberg18c93002012-01-27 11:58:31 -05003881 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003882 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003883 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003884 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003885 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003886
Jason Ekstranda85118c2013-06-27 20:17:02 -05003887 surface->resource =
3888 wl_resource_create(client, &wl_surface_interface,
3889 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003890 if (surface->resource == NULL) {
3891 weston_surface_destroy(surface);
3892 wl_resource_post_no_memory(resource);
3893 return;
3894 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003895 wl_resource_set_implementation(surface->resource, &surface_interface,
3896 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003897
3898 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003899}
3900
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003901static void
3902destroy_region(struct wl_resource *resource)
3903{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003904 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003905
3906 pixman_region32_fini(&region->region);
3907 free(region);
3908}
3909
3910static void
3911region_destroy(struct wl_client *client, struct wl_resource *resource)
3912{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003913 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003914}
3915
3916static void
3917region_add(struct wl_client *client, struct wl_resource *resource,
3918 int32_t x, int32_t y, int32_t width, int32_t height)
3919{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003920 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003921
3922 pixman_region32_union_rect(&region->region, &region->region,
3923 x, y, width, height);
3924}
3925
3926static void
3927region_subtract(struct wl_client *client, struct wl_resource *resource,
3928 int32_t x, int32_t y, int32_t width, int32_t height)
3929{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003930 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003931 pixman_region32_t rect;
3932
3933 pixman_region32_init_rect(&rect, x, y, width, height);
3934 pixman_region32_subtract(&region->region, &region->region, &rect);
3935 pixman_region32_fini(&rect);
3936}
3937
3938static const struct wl_region_interface region_interface = {
3939 region_destroy,
3940 region_add,
3941 region_subtract
3942};
3943
3944static void
3945compositor_create_region(struct wl_client *client,
3946 struct wl_resource *resource, uint32_t id)
3947{
3948 struct weston_region *region;
3949
3950 region = malloc(sizeof *region);
3951 if (region == NULL) {
3952 wl_resource_post_no_memory(resource);
3953 return;
3954 }
3955
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003956 pixman_region32_init(&region->region);
3957
Jason Ekstranda85118c2013-06-27 20:17:02 -05003958 region->resource =
3959 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003960 if (region->resource == NULL) {
3961 free(region);
3962 wl_resource_post_no_memory(resource);
3963 return;
3964 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003965 wl_resource_set_implementation(region->resource, &region_interface,
3966 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003967}
3968
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003969static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003970 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003971 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003972};
3973
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003974static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003975weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3976{
3977 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003978
Jason Ekstrand7b982072014-05-20 14:33:03 -05003979 weston_surface_commit_state(surface, &sub->cached);
3980 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003981
3982 weston_surface_commit_subsurface_order(surface);
3983
3984 weston_surface_schedule_repaint(surface);
3985
Jason Ekstrand7b982072014-05-20 14:33:03 -05003986 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003987}
3988
3989static void
3990weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3991{
3992 struct weston_surface *surface = sub->surface;
3993
3994 /*
3995 * If this commit would cause the surface to move by the
3996 * attach(dx, dy) parameters, the old damage region must be
3997 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05003998 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003999 */
Derek Foreman152254b2015-11-26 14:17:48 -06004000 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004001 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06004002 pixman_region32_union(&sub->cached.damage_surface,
4003 &sub->cached.damage_surface,
4004 &surface->pending.damage_surface);
4005 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004006
4007 if (surface->pending.newly_attached) {
4008 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05004009 weston_surface_state_set_buffer(&sub->cached,
4010 surface->pending.buffer);
4011 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004012 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04004013 weston_presentation_feedback_discard_list(
4014 &sub->cached.feedback_list);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03004015 /* zwp_surface_synchronization_v1.set_acquire_fence */
4016 fd_move(&sub->cached.acquire_fence_fd,
4017 &surface->pending.acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03004018 /* zwp_surface_synchronization_v1.get_release */
4019 weston_buffer_release_move(&sub->cached.buffer_release_ref,
4020 &surface->pending.buffer_release_ref);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004021 }
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05304022 sub->cached.desired_protection = surface->pending.desired_protection;
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05304023 sub->cached.protection_mode = surface->pending.protection_mode;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03004024 assert(surface->pending.acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03004025 assert(surface->pending.buffer_release_ref.buffer_release == NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004026 sub->cached.sx += surface->pending.sx;
4027 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02004028
Derek Foreman152254b2015-11-26 14:17:48 -06004029 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
4030
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004031 sub->cached.buffer_viewport.changed |=
4032 surface->pending.buffer_viewport.changed;
4033 sub->cached.buffer_viewport.buffer =
4034 surface->pending.buffer_viewport.buffer;
4035 sub->cached.buffer_viewport.surface =
4036 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004037
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004038 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004039
4040 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
4041
4042 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
4043
4044 wl_list_insert_list(&sub->cached.frame_callback_list,
4045 &surface->pending.frame_callback_list);
4046 wl_list_init(&surface->pending.frame_callback_list);
4047
Pekka Paalanen133e4392014-09-23 22:08:46 -04004048 wl_list_insert_list(&sub->cached.feedback_list,
4049 &surface->pending.feedback_list);
4050 wl_list_init(&surface->pending.feedback_list);
4051
Jason Ekstrand7b982072014-05-20 14:33:03 -05004052 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004053}
4054
Derek Foreman280e7dd2014-10-03 13:13:42 -05004055static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03004056weston_subsurface_is_synchronized(struct weston_subsurface *sub)
4057{
4058 while (sub) {
4059 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05004060 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004061
4062 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05004063 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004064
4065 sub = weston_surface_to_subsurface(sub->parent);
4066 }
4067
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01004068 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004069}
4070
4071static void
4072weston_subsurface_commit(struct weston_subsurface *sub)
4073{
4074 struct weston_surface *surface = sub->surface;
4075 struct weston_subsurface *tmp;
4076
4077 /* Recursive check for effectively synchronized. */
4078 if (weston_subsurface_is_synchronized(sub)) {
4079 weston_subsurface_commit_to_cache(sub);
4080 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05004081 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03004082 /* flush accumulated state from cache */
4083 weston_subsurface_commit_to_cache(sub);
4084 weston_subsurface_commit_from_cache(sub);
4085 } else {
4086 weston_surface_commit(surface);
4087 }
4088
4089 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
4090 if (tmp->surface != surface)
4091 weston_subsurface_parent_commit(tmp, 0);
4092 }
4093 }
4094}
4095
4096static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004097weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004098{
4099 struct weston_surface *surface = sub->surface;
4100 struct weston_subsurface *tmp;
4101
Pekka Paalanene67858b2013-04-25 13:57:42 +03004102 /* From now on, commit_from_cache the whole sub-tree, regardless of
4103 * the synchronized mode of each child. This sub-surface or some
4104 * of its ancestors were synchronized, so we are synchronized
4105 * all the way down.
4106 */
4107
Jason Ekstrand7b982072014-05-20 14:33:03 -05004108 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004109 weston_subsurface_commit_from_cache(sub);
4110
4111 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
4112 if (tmp->surface != surface)
4113 weston_subsurface_parent_commit(tmp, 1);
4114 }
4115}
4116
4117static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004118weston_subsurface_parent_commit(struct weston_subsurface *sub,
4119 int parent_is_synchronized)
4120{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004121 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004122 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004123 wl_list_for_each(view, &sub->surface->views, surface_link)
4124 weston_view_set_position(view,
4125 sub->position.x,
4126 sub->position.y);
4127
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004128 sub->position.set = 0;
4129 }
4130
4131 if (parent_is_synchronized || sub->synchronized)
4132 weston_subsurface_synchronized_commit(sub);
4133}
4134
Pekka Paalanen8274d902014-08-06 19:36:51 +03004135static int
4136subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
4137{
4138 return snprintf(buf, len, "sub-surface");
4139}
4140
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004141static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004142subsurface_committed(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004143{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004144 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004145
Jason Ekstranda7af7042013-10-12 22:38:11 -05004146 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004147 weston_view_set_position(view,
4148 view->geometry.x + dx,
4149 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004150
4151 /* No need to check parent mappedness, because if parent is not
4152 * mapped, parent is not in a visible layer, so this sub-surface
4153 * will not be drawn either.
4154 */
Armin Krezovićf8486c32016-06-30 06:04:28 +02004155
Pekka Paalanene67858b2013-04-25 13:57:42 +03004156 if (!weston_surface_is_mapped(surface)) {
Armin Krezovićf8486c32016-06-30 06:04:28 +02004157 surface->is_mapped = true;
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03004158
Derek Foreman4b1a0a12014-09-10 15:37:33 -05004159 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03004160 * because that would call it also for the parent surface,
4161 * which might not be mapped yet. That would lead to
4162 * inconsistent state, where the window could never be
4163 * mapped.
4164 *
Armin Krezovićf8486c32016-06-30 06:04:28 +02004165 * Instead just force the is_mapped flag on, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03004166 * weston_surface_is_mapped() return true, so that when the
4167 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03004168 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03004169 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004170 }
4171}
4172
4173static struct weston_subsurface *
4174weston_surface_to_subsurface(struct weston_surface *surface)
4175{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004176 if (surface->committed == subsurface_committed)
4177 return surface->committed_private;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004178
4179 return NULL;
4180}
4181
Pekka Paalanen01388e22013-04-25 13:57:44 +03004182WL_EXPORT struct weston_surface *
4183weston_surface_get_main_surface(struct weston_surface *surface)
4184{
4185 struct weston_subsurface *sub;
4186
4187 while (surface && (sub = weston_surface_to_subsurface(surface)))
4188 surface = sub->parent;
4189
4190 return surface;
4191}
4192
Pekka Paalanen50b67472014-10-01 15:02:41 +03004193WL_EXPORT int
4194weston_surface_set_role(struct weston_surface *surface,
4195 const char *role_name,
4196 struct wl_resource *error_resource,
4197 uint32_t error_code)
4198{
4199 assert(role_name);
4200
4201 if (surface->role_name == NULL ||
4202 surface->role_name == role_name ||
4203 strcmp(surface->role_name, role_name) == 0) {
4204 surface->role_name = role_name;
4205
4206 return 0;
4207 }
4208
4209 wl_resource_post_error(error_resource, error_code,
4210 "Cannot assign role %s to wl_surface@%d,"
4211 " already has role %s\n",
4212 role_name,
4213 wl_resource_get_id(surface->resource),
4214 surface->role_name);
4215 return -1;
4216}
4217
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02004218WL_EXPORT const char *
4219weston_surface_get_role(struct weston_surface *surface)
4220{
4221 return surface->role_name;
4222}
4223
Pekka Paalanen8274d902014-08-06 19:36:51 +03004224WL_EXPORT void
4225weston_surface_set_label_func(struct weston_surface *surface,
4226 int (*desc)(struct weston_surface *,
4227 char *, size_t))
4228{
4229 surface->get_label = desc;
Marius Vlad5de92972019-10-15 13:25:41 +03004230 weston_timeline_refresh_subscription_objects(surface->compositor,
4231 surface);
Pekka Paalanen8274d902014-08-06 19:36:51 +03004232}
4233
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004234/** Get the size of surface contents
4235 *
4236 * \param surface The surface to query.
4237 * \param width Returns the width of raw contents.
4238 * \param height Returns the height of raw contents.
4239 *
4240 * Retrieves the raw surface content size in pixels for the given surface.
4241 * This is the whole content size in buffer pixels. If the surface
4242 * has no content or the renderer does not implement this feature,
4243 * zeroes are returned.
4244 *
4245 * This function is used to determine the buffer size needed for
4246 * a weston_surface_copy_content() call.
4247 */
4248WL_EXPORT void
4249weston_surface_get_content_size(struct weston_surface *surface,
4250 int *width, int *height)
4251{
4252 struct weston_renderer *rer = surface->compositor->renderer;
4253
4254 if (!rer->surface_get_content_size) {
4255 *width = 0;
4256 *height = 0;
4257 return;
4258 }
4259
4260 rer->surface_get_content_size(surface, width, height);
4261}
4262
Quentin Glidic248dd102016-08-12 10:41:34 +02004263/** Get the bounding box of a surface and its subsurfaces
4264 *
4265 * \param surface The surface to query.
4266 * \return The bounding box relative to the surface origin.
4267 *
4268 */
4269WL_EXPORT struct weston_geometry
4270weston_surface_get_bounding_box(struct weston_surface *surface)
4271{
4272 pixman_region32_t region;
4273 pixman_box32_t *box;
4274 struct weston_subsurface *subsurface;
4275
4276 pixman_region32_init_rect(&region,
4277 0, 0,
4278 surface->width, surface->height);
4279
4280 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link)
4281 pixman_region32_union_rect(&region, &region,
4282 subsurface->position.x,
4283 subsurface->position.y,
4284 subsurface->surface->width,
4285 subsurface->surface->height);
4286
4287 box = pixman_region32_extents(&region);
4288 struct weston_geometry geometry = {
4289 .x = box->x1,
4290 .y = box->y1,
4291 .width = box->x2 - box->x1,
4292 .height = box->y2 - box->y1,
4293 };
4294
4295 pixman_region32_fini(&region);
4296
4297 return geometry;
4298}
4299
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004300/** Copy surface contents to system memory.
4301 *
4302 * \param surface The surface to copy from.
4303 * \param target Pointer to the target memory buffer.
4304 * \param size Size of the target buffer in bytes.
4305 * \param src_x X location on contents to copy from.
4306 * \param src_y Y location on contents to copy from.
4307 * \param width Width in pixels of the area to copy.
4308 * \param height Height in pixels of the area to copy.
4309 * \return 0 for success, -1 for failure.
4310 *
4311 * Surface contents are maintained by the renderer. They can be in a
4312 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
4313 * else.
4314 *
4315 * Surface contents are copied into memory pointed to by target,
4316 * which has size bytes of space available. The target memory
4317 * may be larger than needed, but being smaller returns an error.
4318 * The extra bytes in target may or may not be written; their content is
4319 * unspecified. Size must be large enough to hold the image.
4320 *
4321 * The image in the target memory will be arranged in rows from
4322 * top to bottom, and pixels on a row from left to right. The pixel
4323 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
4324 * width * 4.
4325 *
4326 * Parameters src_x and src_y define the upper-left corner in buffer
4327 * coordinates (pixels) to copy from. Parameters width and height
4328 * define the size of the area to copy in pixels.
4329 *
4330 * The rectangle defined by src_x, src_y, width, height must fit in
4331 * the surface contents. Otherwise an error is returned.
4332 *
Changwoo Chof97d2502017-08-05 00:30:47 +09004333 * Use weston_surface_get_content_size to determine the content size; the
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004334 * needed target buffer size and rectangle limits.
4335 *
4336 * CURRENT IMPLEMENTATION RESTRICTIONS:
4337 * - the machine must be little-endian due to Pixman formats.
4338 *
4339 * NOTE: Pixman formats are premultiplied.
4340 */
4341WL_EXPORT int
4342weston_surface_copy_content(struct weston_surface *surface,
4343 void *target, size_t size,
4344 int src_x, int src_y,
4345 int width, int height)
4346{
4347 struct weston_renderer *rer = surface->compositor->renderer;
4348 int cw, ch;
4349 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
4350
4351 if (!rer->surface_copy_content)
4352 return -1;
4353
4354 weston_surface_get_content_size(surface, &cw, &ch);
4355
4356 if (src_x < 0 || src_y < 0)
4357 return -1;
4358
4359 if (width <= 0 || height <= 0)
4360 return -1;
4361
4362 if (src_x + width > cw || src_y + height > ch)
4363 return -1;
4364
4365 if (width * bytespp * height > size)
4366 return -1;
4367
4368 return rer->surface_copy_content(surface, target, size,
4369 src_x, src_y, width, height);
4370}
4371
Pekka Paalanene67858b2013-04-25 13:57:42 +03004372static void
4373subsurface_set_position(struct wl_client *client,
4374 struct wl_resource *resource, int32_t x, int32_t y)
4375{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004376 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004377
4378 if (!sub)
4379 return;
4380
4381 sub->position.x = x;
4382 sub->position.y = y;
4383 sub->position.set = 1;
4384}
4385
4386static struct weston_subsurface *
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004387subsurface_find_sibling(struct weston_subsurface *sub,
4388 struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004389{
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004390 struct weston_surface *parent = sub->parent;
4391 struct weston_subsurface *sibling;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004392
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004393 wl_list_for_each(sibling, &parent->subsurface_list, parent_link) {
4394 if (sibling->surface == surface && sibling != sub)
4395 return sibling;
4396 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004397
4398 return NULL;
4399}
4400
4401static struct weston_subsurface *
4402subsurface_sibling_check(struct weston_subsurface *sub,
4403 struct weston_surface *surface,
4404 const char *request)
4405{
4406 struct weston_subsurface *sibling;
4407
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004408 sibling = subsurface_find_sibling(sub, surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004409 if (!sibling) {
4410 wl_resource_post_error(sub->resource,
4411 WL_SUBSURFACE_ERROR_BAD_SURFACE,
4412 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004413 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004414 return NULL;
4415 }
4416
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004417 assert(sibling->parent == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004418
4419 return sibling;
4420}
4421
4422static void
4423subsurface_place_above(struct wl_client *client,
4424 struct wl_resource *resource,
4425 struct wl_resource *sibling_resource)
4426{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004427 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004428 struct weston_surface *surface =
4429 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004430 struct weston_subsurface *sibling;
4431
4432 if (!sub)
4433 return;
4434
4435 sibling = subsurface_sibling_check(sub, surface, "place_above");
4436 if (!sibling)
4437 return;
4438
4439 wl_list_remove(&sub->parent_link_pending);
4440 wl_list_insert(sibling->parent_link_pending.prev,
4441 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004442
4443 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004444}
4445
4446static void
4447subsurface_place_below(struct wl_client *client,
4448 struct wl_resource *resource,
4449 struct wl_resource *sibling_resource)
4450{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004451 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004452 struct weston_surface *surface =
4453 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004454 struct weston_subsurface *sibling;
4455
4456 if (!sub)
4457 return;
4458
4459 sibling = subsurface_sibling_check(sub, surface, "place_below");
4460 if (!sibling)
4461 return;
4462
4463 wl_list_remove(&sub->parent_link_pending);
4464 wl_list_insert(&sibling->parent_link_pending,
4465 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004466
4467 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004468}
4469
4470static void
4471subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
4472{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004473 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004474
4475 if (sub)
4476 sub->synchronized = 1;
4477}
4478
4479static void
4480subsurface_set_desync(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
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004484 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03004485 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004486
4487 /* If sub became effectively desynchronized, flush. */
4488 if (!weston_subsurface_is_synchronized(sub))
4489 weston_subsurface_synchronized_commit(sub);
4490 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004491}
4492
4493static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03004494weston_subsurface_unlink_parent(struct weston_subsurface *sub)
4495{
4496 wl_list_remove(&sub->parent_link);
4497 wl_list_remove(&sub->parent_link_pending);
4498 wl_list_remove(&sub->parent_destroy_listener.link);
4499 sub->parent = NULL;
4500}
4501
4502static void
4503weston_subsurface_destroy(struct weston_subsurface *sub);
4504
4505static void
4506subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
4507{
4508 struct weston_subsurface *sub =
4509 container_of(listener, struct weston_subsurface,
4510 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004511 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004512
4513 /* The protocol object (wl_resource) is left inert. */
4514 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004515 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004516
4517 weston_subsurface_destroy(sub);
4518}
4519
4520static void
4521subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
4522{
4523 struct weston_subsurface *sub =
4524 container_of(listener, struct weston_subsurface,
4525 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004526 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004527 assert(sub->surface != sub->parent);
4528
4529 if (weston_surface_is_mapped(sub->surface))
4530 weston_surface_unmap(sub->surface);
4531
4532 weston_subsurface_unlink_parent(sub);
4533}
4534
4535static void
4536subsurface_resource_destroy(struct wl_resource *resource)
4537{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004538 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004539
4540 if (sub)
4541 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004542}
4543
4544static void
4545subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
4546{
4547 wl_resource_destroy(resource);
4548}
4549
4550static void
4551weston_subsurface_link_parent(struct weston_subsurface *sub,
4552 struct weston_surface *parent)
4553{
4554 sub->parent = parent;
4555 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004556 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004557 &sub->parent_destroy_listener);
4558
4559 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4560 wl_list_insert(&parent->subsurface_list_pending,
4561 &sub->parent_link_pending);
4562}
4563
4564static void
4565weston_subsurface_link_surface(struct weston_subsurface *sub,
4566 struct weston_surface *surface)
4567{
4568 sub->surface = surface;
4569 sub->surface_destroy_listener.notify =
4570 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004571 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004572 &sub->surface_destroy_listener);
4573}
4574
4575static void
4576weston_subsurface_destroy(struct weston_subsurface *sub)
4577{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004578 struct weston_view *view, *next;
4579
Pekka Paalanene67858b2013-04-25 13:57:42 +03004580 assert(sub->surface);
4581
4582 if (sub->resource) {
4583 assert(weston_surface_to_subsurface(sub->surface) == sub);
4584 assert(sub->parent_destroy_listener.notify ==
4585 subsurface_handle_parent_destroy);
4586
George Kiagiadakised04d382014-06-13 18:10:26 +02004587 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
4588 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004589 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02004590 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05004591
Pekka Paalanene67858b2013-04-25 13:57:42 +03004592 if (sub->parent)
4593 weston_subsurface_unlink_parent(sub);
4594
Jason Ekstrand7b982072014-05-20 14:33:03 -05004595 weston_surface_state_fini(&sub->cached);
4596 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004597
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004598 sub->surface->committed = NULL;
4599 sub->surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004600 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004601 } else {
4602 /* the dummy weston_subsurface for the parent itself */
4603 assert(sub->parent_destroy_listener.notify == NULL);
4604 wl_list_remove(&sub->parent_link);
4605 wl_list_remove(&sub->parent_link_pending);
4606 }
4607
4608 wl_list_remove(&sub->surface_destroy_listener.link);
4609 free(sub);
4610}
4611
4612static const struct wl_subsurface_interface subsurface_implementation = {
4613 subsurface_destroy,
4614 subsurface_set_position,
4615 subsurface_place_above,
4616 subsurface_place_below,
4617 subsurface_set_sync,
4618 subsurface_set_desync
4619};
4620
4621static struct weston_subsurface *
4622weston_subsurface_create(uint32_t id, struct weston_surface *surface,
4623 struct weston_surface *parent)
4624{
4625 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004626 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004627
Bryce Harringtonde16d892014-11-20 22:21:57 -08004628 sub = zalloc(sizeof *sub);
4629 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004630 return NULL;
4631
Jason Ekstranda7af7042013-10-12 22:38:11 -05004632 wl_list_init(&sub->unused_views);
4633
Jason Ekstranda85118c2013-06-27 20:17:02 -05004634 sub->resource =
4635 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004636 if (!sub->resource) {
4637 free(sub);
4638 return NULL;
4639 }
4640
Jason Ekstranda85118c2013-06-27 20:17:02 -05004641 wl_resource_set_implementation(sub->resource,
4642 &subsurface_implementation,
4643 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004644 weston_subsurface_link_surface(sub, surface);
4645 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05004646 weston_surface_state_init(&sub->cached);
4647 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004648 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004649
4650 return sub;
4651}
4652
4653/* Create a dummy subsurface for having the parent itself in its
4654 * sub-surface lists. Makes stacking order manipulation easy.
4655 */
4656static struct weston_subsurface *
4657weston_subsurface_create_for_parent(struct weston_surface *parent)
4658{
4659 struct weston_subsurface *sub;
4660
Bryce Harringtonde16d892014-11-20 22:21:57 -08004661 sub = zalloc(sizeof *sub);
4662 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004663 return NULL;
4664
4665 weston_subsurface_link_surface(sub, parent);
4666 sub->parent = parent;
4667 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4668 wl_list_insert(&parent->subsurface_list_pending,
4669 &sub->parent_link_pending);
4670
4671 return sub;
4672}
4673
4674static void
4675subcompositor_get_subsurface(struct wl_client *client,
4676 struct wl_resource *resource,
4677 uint32_t id,
4678 struct wl_resource *surface_resource,
4679 struct wl_resource *parent_resource)
4680{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004681 struct weston_surface *surface =
4682 wl_resource_get_user_data(surface_resource);
4683 struct weston_surface *parent =
4684 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004685 struct weston_subsurface *sub;
4686 static const char where[] = "get_subsurface: wl_subsurface@";
4687
4688 if (surface == parent) {
4689 wl_resource_post_error(resource,
4690 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4691 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004692 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004693 return;
4694 }
4695
4696 if (weston_surface_to_subsurface(surface)) {
4697 wl_resource_post_error(resource,
4698 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4699 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004700 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004701 return;
4702 }
4703
Pekka Paalanen50b67472014-10-01 15:02:41 +03004704 if (weston_surface_set_role(surface, "wl_subsurface", resource,
4705 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004706 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004707
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004708 if (weston_surface_get_main_surface(parent) == surface) {
4709 wl_resource_post_error(resource,
4710 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4711 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004712 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004713 return;
4714 }
4715
Pekka Paalanene67858b2013-04-25 13:57:42 +03004716 /* make sure the parent is in its own list */
4717 if (wl_list_empty(&parent->subsurface_list)) {
4718 if (!weston_subsurface_create_for_parent(parent)) {
4719 wl_resource_post_no_memory(resource);
4720 return;
4721 }
4722 }
4723
4724 sub = weston_subsurface_create(id, surface, parent);
4725 if (!sub) {
4726 wl_resource_post_no_memory(resource);
4727 return;
4728 }
4729
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004730 surface->committed = subsurface_committed;
4731 surface->committed_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004732 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004733}
4734
4735static void
4736subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
4737{
4738 wl_resource_destroy(resource);
4739}
4740
4741static const struct wl_subcompositor_interface subcompositor_interface = {
4742 subcompositor_destroy,
4743 subcompositor_get_subsurface
4744};
4745
4746static void
4747bind_subcompositor(struct wl_client *client,
4748 void *data, uint32_t version, uint32_t id)
4749{
4750 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004751 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004752
Jason Ekstranda85118c2013-06-27 20:17:02 -05004753 resource =
4754 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004755 if (resource == NULL) {
4756 wl_client_post_no_memory(client);
4757 return;
4758 }
4759 wl_resource_set_implementation(resource, &subcompositor_interface,
4760 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004761}
4762
Bryce Harrington0795ece2016-08-30 12:04:26 -07004763/** Set a DPMS mode on all of the compositor's outputs
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004764 *
4765 * \param compositor The compositor instance
4766 * \param state The DPMS state the outputs will be set to
4767 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004768static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004769weston_compositor_dpms(struct weston_compositor *compositor,
4770 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004771{
4772 struct weston_output *output;
4773
Bryce Harrington08976ac2016-08-30 12:05:16 -07004774 wl_list_for_each(output, &compositor->output_list, link)
4775 if (output->set_dpms)
4776 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004777}
4778
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004779/** Restores the compositor to active status
4780 *
4781 * \param compositor The compositor instance
4782 *
4783 * If the compositor was in a sleeping mode, all outputs are powered
4784 * back on via DPMS. Otherwise if the compositor was inactive
4785 * (idle/locked, offscreen, or sleeping) then the compositor's wake
4786 * signal will fire.
4787 *
4788 * Restarts the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004789 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004790 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004791WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004792weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004793{
Neil Roberts8b62e202013-09-30 13:14:47 +01004794 uint32_t old_state = compositor->state;
4795
4796 /* The state needs to be changed before emitting the wake
4797 * signal because that may try to schedule a repaint which
4798 * will not work if the compositor is still sleeping */
4799 compositor->state = WESTON_COMPOSITOR_ACTIVE;
4800
4801 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004802 case WESTON_COMPOSITOR_SLEEPING:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004803 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004804 case WESTON_COMPOSITOR_OFFSCREEN:
Daniel Stone893b9362016-11-08 15:47:09 +00004805 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004806 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004807 /* fall through */
4808 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004809 wl_event_source_timer_update(compositor->idle_source,
4810 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004811 }
4812}
4813
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004814/** Turns off rendering and frame events for the compositor.
4815 *
4816 * \param compositor The compositor instance
4817 *
4818 * This is used for example to prevent further rendering while the
4819 * compositor is shutting down.
4820 *
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004821 * Stops the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004822 *
4823 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004824 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004825WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004826weston_compositor_offscreen(struct weston_compositor *compositor)
4827{
4828 switch (compositor->state) {
4829 case WESTON_COMPOSITOR_OFFSCREEN:
4830 return;
4831 case WESTON_COMPOSITOR_SLEEPING:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004832 default:
4833 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4834 wl_event_source_timer_update(compositor->idle_source, 0);
4835 }
4836}
4837
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004838/** Powers down all attached output devices
4839 *
4840 * \param compositor The compositor instance
4841 *
4842 * Causes rendering to the outputs to cease, and no frame events to be
4843 * sent. Only powers down the outputs if the compositor is not already
4844 * in sleep mode.
4845 *
4846 * Stops the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004847 *
4848 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004849 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004850WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004851weston_compositor_sleep(struct weston_compositor *compositor)
4852{
4853 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
4854 return;
4855
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004856 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004857 compositor->state = WESTON_COMPOSITOR_SLEEPING;
4858 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
4859}
4860
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004861/** Sets compositor to idle mode
4862 *
4863 * \param data The compositor instance
4864 *
4865 * This is called when the idle timer fires. Once the compositor is in
4866 * idle mode it requires a wake action (e.g. via
4867 * weston_compositor_wake()) to restore it. The compositor's
4868 * idle_signal will be triggered when the idle event occurs.
4869 *
4870 * Idleness can be inhibited by setting the compositor's idle_inhibit
4871 * property.
4872 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004873static int
4874idle_handler(void *data)
4875{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004876 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004877
4878 if (compositor->idle_inhibit)
4879 return 1;
4880
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004881 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004882 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004883
4884 return 1;
4885}
4886
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004887WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08004888weston_plane_init(struct weston_plane *plane,
4889 struct weston_compositor *ec,
4890 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004891{
4892 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004893 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004894 plane->x = x;
4895 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08004896 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004897
4898 /* Init the link so that the call to wl_list_remove() when releasing
4899 * the plane without ever stacking doesn't lead to a crash */
4900 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004901}
4902
4903WL_EXPORT void
4904weston_plane_release(struct weston_plane *plane)
4905{
Xiong Zhang97116532013-10-23 13:58:31 +08004906 struct weston_view *view;
4907
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004908 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004909 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004910
Xiong Zhang97116532013-10-23 13:58:31 +08004911 wl_list_for_each(view, &plane->compositor->view_list, link) {
4912 if (view->plane == plane)
4913 view->plane = NULL;
4914 }
4915
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004916 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004917}
4918
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004919/** weston_compositor_stack_plane
4920 * \ingroup compositor
4921 */
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004922WL_EXPORT void
4923weston_compositor_stack_plane(struct weston_compositor *ec,
4924 struct weston_plane *plane,
4925 struct weston_plane *above)
4926{
4927 if (above)
4928 wl_list_insert(above->link.prev, &plane->link);
4929 else
4930 wl_list_insert(&ec->plane_list, &plane->link);
4931}
4932
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004933static void
4934output_release(struct wl_client *client, struct wl_resource *resource)
4935{
4936 wl_resource_destroy(resource);
4937}
4938
4939static const struct wl_output_interface output_interface = {
4940 output_release,
4941};
4942
4943
Casey Dahlin9074db52012-04-19 22:50:09 -04004944static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004945{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004946 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004947}
4948
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004949static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004950bind_output(struct wl_client *client,
4951 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05004952{
Pekka Paalanen05347622017-03-27 12:24:34 +03004953 struct weston_head *head = data;
4954 struct weston_output *output = head->output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004955 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004956 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004957
Jason Ekstranda85118c2013-06-27 20:17:02 -05004958 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004959 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004960 if (resource == NULL) {
4961 wl_client_post_no_memory(client);
4962 return;
4963 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004964
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03004965 wl_list_insert(&head->resource_list, wl_resource_get_link(resource));
Pekka Paalanen055c1132017-03-27 16:31:25 +03004966 wl_resource_set_implementation(resource, &output_interface, head,
Pekka Paalanen05347622017-03-27 12:24:34 +03004967 unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04004968
Pekka Paalanen05347622017-03-27 12:24:34 +03004969 assert(output);
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004970 wl_output_send_geometry(resource,
4971 output->x,
4972 output->y,
Pekka Paalanen01f60212017-03-24 15:39:24 +02004973 head->mm_width,
4974 head->mm_height,
4975 head->subpixel,
4976 head->make, head->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04004977 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004978 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004979 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02004980 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004981
4982 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004983 wl_output_send_mode(resource,
4984 mode->flags,
4985 mode->width,
4986 mode->height,
4987 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004988 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02004989
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004990 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004991 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004992}
4993
Pekka Paalanendcac3512017-12-08 14:13:34 +02004994static void
4995weston_head_add_global(struct weston_head *head)
4996{
4997 head->global = wl_global_create(head->compositor->wl_display,
4998 &wl_output_interface, 3,
4999 head, bind_output);
5000}
5001
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02005002/** Remove the global wl_output protocol object
5003 *
5004 * \param head The head whose global to remove.
5005 *
5006 * Also orphans the wl_resources for this head (wl_output).
5007 */
5008static void
5009weston_head_remove_global(struct weston_head *head)
5010{
5011 struct wl_resource *resource, *tmp;
5012
5013 if (head->global)
5014 wl_global_destroy(head->global);
5015 head->global = NULL;
5016
5017 wl_resource_for_each_safe(resource, tmp, &head->resource_list) {
5018 unbind_resource(resource);
5019 wl_resource_set_destructor(resource, NULL);
5020 wl_resource_set_user_data(resource, NULL);
5021 }
Roman Gilge97391c2019-03-29 13:01:06 +01005022
5023 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
5024 /* It's sufficient to unset the destructor, then the list elements
5025 * won't be accessed.
5026 */
5027 wl_resource_set_destructor(resource, NULL);
5028 }
5029 wl_list_init(&head->xdg_output_resource_list);
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02005030}
5031
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005032/** Get the backing object of wl_output
5033 *
5034 * \param resource A wl_output protocol object.
5035 * \return The backing object (user data) of a wl_resource representing a
5036 * wl_output protocol object.
Marius Vlad78984ee2019-06-11 00:05:08 +03005037 *
5038 * \ingroup head
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005039 */
Pekka Paalanen055c1132017-03-27 16:31:25 +03005040WL_EXPORT struct weston_head *
5041weston_head_from_resource(struct wl_resource *resource)
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005042{
5043 assert(wl_resource_instance_of(resource, &wl_output_interface,
5044 &output_interface));
5045
5046 return wl_resource_get_user_data(resource);
5047}
5048
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005049/** Initialize a pre-allocated weston_head
5050 *
5051 * \param head The head to initialize.
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005052 * \param name The head name, e.g. the connector name or equivalent.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005053 *
5054 * The head will be safe to attach, detach and release.
5055 *
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005056 * The name is used in logs, and can be used by compositors as a configuration
5057 * identifier.
5058 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005059 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005060 * \internal
5061 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005062WL_EXPORT void
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005063weston_head_init(struct weston_head *head, const char *name)
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005064{
5065 /* Add some (in)sane defaults which can be used
5066 * for checking if an output was properly configured
5067 */
5068 memset(head, 0, sizeof *head);
5069
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005070 wl_list_init(&head->compositor_link);
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005071 wl_signal_init(&head->destroy_signal);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005072 wl_list_init(&head->output_link);
5073 wl_list_init(&head->resource_list);
Roman Gilge97391c2019-03-29 13:01:06 +01005074 wl_list_init(&head->xdg_output_resource_list);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005075 head->name = strdup(name);
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305076 head->current_protection = WESTON_HDCP_DISABLE;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005077}
5078
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005079/** Send output heads changed signal
5080 *
5081 * \param output The output that changed.
5082 *
5083 * Notify that the enabled output gained and/or lost heads, or that the
5084 * associated heads may have changed their connection status. This does not
5085 * include cases where the output becomes enabled or disabled. The registered
5086 * callbacks are called after the change has successfully happened.
5087 *
5088 * If connection status change causes the compositor to attach or detach a head
5089 * to an enabled output, the registered callbacks may be called multiple times.
Marius Vlad55d87362019-06-11 01:15:35 +03005090 *
5091 * \ingroup output
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005092 */
5093static void
5094weston_output_emit_heads_changed(struct weston_output *output)
5095{
5096 wl_signal_emit(&output->compositor->output_heads_changed_signal,
5097 output);
5098}
5099
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005100/** Idle task for emitting heads_changed_signal */
5101static void
5102weston_compositor_call_heads_changed(void *data)
5103{
5104 struct weston_compositor *compositor = data;
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005105 struct weston_head *head;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005106
5107 compositor->heads_changed_source = NULL;
5108
5109 wl_signal_emit(&compositor->heads_changed_signal, compositor);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005110
5111 wl_list_for_each(head, &compositor->head_list, compositor_link) {
5112 if (head->output && head->output->enabled)
5113 weston_output_emit_heads_changed(head->output);
5114 }
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005115}
5116
5117/** Schedule a call on idle to heads_changed callback
5118 *
5119 * \param compositor The Compositor.
5120 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005121 * \ingroup compositor
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005122 * \internal
5123 */
5124static void
5125weston_compositor_schedule_heads_changed(struct weston_compositor *compositor)
5126{
5127 struct wl_event_loop *loop;
5128
5129 if (compositor->heads_changed_source)
5130 return;
5131
5132 loop = wl_display_get_event_loop(compositor->wl_display);
5133 compositor->heads_changed_source = wl_event_loop_add_idle(loop,
5134 weston_compositor_call_heads_changed, compositor);
5135}
5136
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005137/** Register a new head
5138 *
5139 * \param compositor The compositor.
5140 * \param head The head to register, must not be already registered.
5141 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005142 * This signals the core that a new head has become available, leading to
5143 * heads_changed hook being called later.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005144 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005145 * \ingroup compositor
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005146 * \internal
5147 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005148WL_EXPORT void
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005149weston_compositor_add_head(struct weston_compositor *compositor,
5150 struct weston_head *head)
5151{
5152 assert(wl_list_empty(&head->compositor_link));
5153 assert(head->name);
5154
5155 wl_list_insert(compositor->head_list.prev, &head->compositor_link);
5156 head->compositor = compositor;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005157 weston_compositor_schedule_heads_changed(compositor);
5158}
5159
5160/** Adds a listener to be called when heads change
5161 *
5162 * \param compositor The compositor.
5163 * \param listener The listener to add.
5164 *
Marius Vlada2dace22019-06-12 16:05:44 +03005165 * The listener notify function argument is weston_compositor.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005166 *
5167 * The listener function will be called after heads are added or their
5168 * connection status has changed. Several changes may be accumulated into a
5169 * single call. The user is expected to iterate over the existing heads and
5170 * check their statuses to find out what changed.
5171 *
5172 * \sa weston_compositor_iterate_heads, weston_head_is_connected,
5173 * weston_head_is_enabled
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005174 * \ingroup compositor
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005175 */
5176WL_EXPORT void
5177weston_compositor_add_heads_changed_listener(struct weston_compositor *compositor,
5178 struct wl_listener *listener)
5179{
5180 wl_signal_add(&compositor->heads_changed_signal, listener);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005181}
5182
5183/** Iterate over available heads
5184 *
5185 * \param compositor The compositor.
Marius Vlada2dace22019-06-12 16:05:44 +03005186 * \param iter The iterator, or NULL for start.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005187 * \return The next available head in the list.
5188 *
5189 * Returns all available heads, regardless of being connected or enabled.
5190 *
5191 * You can iterate over all heads as follows:
5192 * \code
5193 * struct weston_head *head = NULL;
5194 *
5195 * while ((head = weston_compositor_iterate_heads(compositor, head))) {
5196 * ...
5197 * }
5198 * \endcode
5199 *
5200 * If you cause \c iter to be removed from the list, you cannot use it to
5201 * continue iterating. Removing any other item is safe.
5202 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005203 * \ingroup compositor
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005204 */
5205WL_EXPORT struct weston_head *
5206weston_compositor_iterate_heads(struct weston_compositor *compositor,
5207 struct weston_head *iter)
5208{
5209 struct wl_list *list = &compositor->head_list;
5210 struct wl_list *node;
5211
5212 assert(compositor);
5213 assert(!iter || iter->compositor == compositor);
5214
5215 if (iter)
5216 node = iter->compositor_link.next;
5217 else
5218 node = list->next;
5219
5220 assert(node);
5221 assert(!iter || node != &iter->compositor_link);
5222
5223 if (node == list)
5224 return NULL;
5225
5226 return container_of(node, struct weston_head, compositor_link);
5227}
5228
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005229/** Iterate over attached heads
5230 *
5231 * \param output The output whose heads to iterate.
Marius Vlada2dace22019-06-12 16:05:44 +03005232 * \param iter The iterator, or NULL for start.
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005233 * \return The next attached head in the list.
5234 *
5235 * Returns all heads currently attached to the output.
5236 *
5237 * You can iterate over all heads as follows:
5238 * \code
5239 * struct weston_head *head = NULL;
5240 *
5241 * while ((head = weston_output_iterate_heads(output, head))) {
5242 * ...
5243 * }
5244 * \endcode
5245 *
5246 * If you cause \c iter to be removed from the list, you cannot use it to
5247 * continue iterating. Removing any other item is safe.
5248 *
Marius Vlad55d87362019-06-11 01:15:35 +03005249 * \ingroup ouput
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005250 */
5251WL_EXPORT struct weston_head *
5252weston_output_iterate_heads(struct weston_output *output,
5253 struct weston_head *iter)
5254{
5255 struct wl_list *list = &output->head_list;
5256 struct wl_list *node;
5257
5258 assert(output);
5259 assert(!iter || iter->output == output);
5260
5261 if (iter)
5262 node = iter->output_link.next;
5263 else
5264 node = list->next;
5265
5266 assert(node);
5267 assert(!iter || node != &iter->output_link);
5268
5269 if (node == list)
5270 return NULL;
5271
5272 return container_of(node, struct weston_head, output_link);
5273}
5274
Pekka Paalanendcac3512017-12-08 14:13:34 +02005275/** Attach a head to an output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005276 *
5277 * \param output The output to attach to.
5278 * \param head The head that is not yet attached.
5279 * \return 0 on success, -1 on failure.
5280 *
5281 * Attaches the given head to the output. All heads of an output are clones
5282 * and share the resolution and timings.
5283 *
5284 * Cloning heads this way uses less resources than creating an output for
5285 * each head, but is not always possible due to environment, driver and hardware
5286 * limitations.
5287 *
5288 * On failure, the head remains unattached. Success of this function does not
5289 * guarantee the output configuration is actually valid. The final checks are
Pekka Paalanendcac3512017-12-08 14:13:34 +02005290 * made on weston_output_enable() unless the output was already enabled.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005291 *
Marius Vlad55d87362019-06-11 01:15:35 +03005292 * \ingroup output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005293 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005294WL_EXPORT int
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005295weston_output_attach_head(struct weston_output *output,
5296 struct weston_head *head)
5297{
Pekka Paalanendcac3512017-12-08 14:13:34 +02005298 char *head_names;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005299
5300 if (!wl_list_empty(&head->output_link))
5301 return -1;
5302
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005303 if (output->attach_head) {
5304 if (output->attach_head(output, head) < 0)
5305 return -1;
5306 } else if (!wl_list_empty(&output->head_list)) {
5307 /* No support for clones in the legacy path. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005308 return -1;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005309 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005310
5311 head->output = output;
5312 wl_list_insert(output->head_list.prev, &head->output_link);
5313
Pekka Paalanendcac3512017-12-08 14:13:34 +02005314 if (output->enabled) {
5315 weston_head_add_global(head);
5316
5317 head_names = weston_output_create_heads_string(output);
5318 weston_log("Output '%s' updated to have head(s) %s\n",
5319 output->name, head_names);
5320 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005321
5322 weston_output_emit_heads_changed(output);
Pekka Paalanendcac3512017-12-08 14:13:34 +02005323 }
5324
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005325 return 0;
5326}
5327
5328/** Detach a head from its output
5329 *
5330 * \param head The head to detach.
5331 *
5332 * It is safe to detach a non-attached head.
5333 *
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005334 * If the head is attached to an enabled output and the output will be left
5335 * with no heads, the output will be disabled.
5336 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005337 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005338 * \sa weston_output_disable
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005339 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005340WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005341weston_head_detach(struct weston_head *head)
5342{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005343 struct weston_output *output = head->output;
Pekka Paalanena0106992017-12-08 16:11:17 +02005344 char *head_names;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005345
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005346 wl_list_remove(&head->output_link);
5347 wl_list_init(&head->output_link);
5348 head->output = NULL;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005349
5350 if (!output)
5351 return;
5352
5353 if (output->detach_head)
5354 output->detach_head(output, head);
5355
5356 if (output->enabled) {
5357 weston_head_remove_global(head);
5358
Pekka Paalanena0106992017-12-08 16:11:17 +02005359 if (wl_list_empty(&output->head_list)) {
5360 weston_log("Output '%s' no heads left, disabling.\n",
5361 output->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005362 weston_output_disable(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02005363 } else {
5364 head_names = weston_output_create_heads_string(output);
5365 weston_log("Output '%s' updated to have head(s) %s\n",
5366 output->name, head_names);
5367 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005368
5369 weston_output_emit_heads_changed(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02005370 }
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005371 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005372}
5373
5374/** Destroy a head
5375 *
5376 * \param head The head to be released.
5377 *
5378 * Destroys the head. The caller is responsible for freeing the memory pointed
5379 * to by \c head.
5380 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005381 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005382 * \internal
5383 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005384WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005385weston_head_release(struct weston_head *head)
5386{
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005387 wl_signal_emit(&head->destroy_signal, head);
5388
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005389 weston_head_detach(head);
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005390
5391 free(head->make);
5392 free(head->model);
5393 free(head->serial_number);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005394 free(head->name);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005395
5396 wl_list_remove(&head->compositor_link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005397}
5398
Pekka Paalanene19970f2017-08-28 14:11:02 +03005399static void
5400weston_head_set_device_changed(struct weston_head *head)
5401{
5402 head->device_changed = true;
5403
5404 if (head->compositor)
5405 weston_compositor_schedule_heads_changed(head->compositor);
5406}
5407
5408/** String equal comparison with NULLs being equal */
5409static bool
5410str_null_eq(const char *a, const char *b)
5411{
5412 if (!a && !b)
5413 return true;
5414
5415 if (!!a != !!b)
5416 return false;
5417
5418 return strcmp(a, b) == 0;
5419}
5420
Pekka Paalanen01f60212017-03-24 15:39:24 +02005421/** Store monitor make, model and serial number
5422 *
5423 * \param head The head to modify.
5424 * \param make The monitor make. If EDID is available, the PNP ID. Otherwise
5425 * any string, or NULL for none.
5426 * \param model The monitor model or name, or a made-up string, or NULL for
5427 * none.
5428 * \param serialno The monitor serial number, a made-up string, or NULL for
5429 * none.
5430 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005431 * This may set the device_changed flag.
5432 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005433 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005434 * \internal
5435 */
5436WL_EXPORT void
5437weston_head_set_monitor_strings(struct weston_head *head,
5438 const char *make,
5439 const char *model,
5440 const char *serialno)
5441{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005442 if (str_null_eq(head->make, make) &&
5443 str_null_eq(head->model, model) &&
5444 str_null_eq(head->serial_number, serialno))
5445 return;
5446
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005447 free(head->make);
5448 free(head->model);
5449 free(head->serial_number);
5450
5451 head->make = make ? strdup(make) : NULL;
5452 head->model = model ? strdup(model) : NULL;
5453 head->serial_number = serialno ? strdup(serialno) : NULL;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005454
5455 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005456}
5457
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005458/** Store display non-desktop status
5459 *
5460 * \param head The head to modify.
5461 * \param non_desktop Whether the head connects to a non-desktop display.
5462 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005463 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005464 * \internal
5465 */
5466WL_EXPORT void
5467weston_head_set_non_desktop(struct weston_head *head, bool non_desktop)
5468{
5469 if (head->non_desktop == non_desktop)
5470 return;
5471
5472 head->non_desktop = non_desktop;
5473
5474 weston_head_set_device_changed(head);
5475}
5476
Lucas Stacha69cb712019-11-25 23:29:31 +00005477/** Store display transformation
5478 *
5479 * \param head The head to modify.
5480 * \param transform The transformation to apply for this head
5481 *
5482 * This may set the device_changed flag.
5483 *
5484 * \ingroup head
5485 * \internal
5486 */
5487WL_EXPORT void
5488weston_head_set_transform(struct weston_head *head, uint32_t transform)
5489{
5490 if (head->transform == transform)
5491 return;
5492
5493 head->transform = transform;
5494
5495 weston_head_set_device_changed(head);
5496}
5497
5498
Pekka Paalanen01f60212017-03-24 15:39:24 +02005499/** Store physical image size
5500 *
5501 * \param head The head to modify.
5502 * \param mm_width Image area width in millimeters.
5503 * \param mm_height Image area height in millimeters.
5504 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005505 * This may set the device_changed flag.
5506 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005507 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005508 * \internal
5509 */
5510WL_EXPORT void
5511weston_head_set_physical_size(struct weston_head *head,
5512 int32_t mm_width, int32_t mm_height)
5513{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005514 if (head->mm_width == mm_width &&
5515 head->mm_height == mm_height)
5516 return;
5517
Pekka Paalanen01f60212017-03-24 15:39:24 +02005518 head->mm_width = mm_width;
5519 head->mm_height = mm_height;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005520
5521 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005522}
5523
5524/** Store monitor sub-pixel layout
5525 *
5526 * \param head The head to modify.
5527 * \param sp Sub-pixel layout. The possible values are:
5528 * - WL_OUTPUT_SUBPIXEL_UNKNOWN,
5529 * - WL_OUTPUT_SUBPIXEL_NONE,
5530 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB,
5531 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR,
5532 * - WL_OUTPUT_SUBPIXEL_VERTICAL_RGB,
5533 * - WL_OUTPUT_SUBPIXEL_VERTICAL_BGR
5534 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005535 * This may set the device_changed flag.
5536 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005537 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005538 * \internal
5539 */
5540WL_EXPORT void
5541weston_head_set_subpixel(struct weston_head *head,
5542 enum wl_output_subpixel sp)
5543{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005544 if (head->subpixel == sp)
5545 return;
5546
Pekka Paalanen01f60212017-03-24 15:39:24 +02005547 head->subpixel = sp;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005548
5549 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005550}
5551
5552/** Mark the monitor as internal
5553 *
5554 * This is used for embedded screens, like laptop panels.
5555 *
5556 * \param head The head to mark as internal.
5557 *
5558 * By default a head is external. The type is often inferred from the physical
5559 * connector type.
5560 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005561 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005562 * \internal
5563 */
5564WL_EXPORT void
5565weston_head_set_internal(struct weston_head *head)
5566{
5567 head->connection_internal = true;
5568}
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005569
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005570/** Store connector status
5571 *
5572 * \param head The head to modify.
5573 * \param connected Whether the head is connected.
5574 *
5575 * Connectors are created as disconnected. This function can be used to
5576 * set the connector status.
5577 *
5578 * The status should be set to true when a physical connector is connected to
5579 * a video sink device like a monitor and to false when the connector is
5580 * disconnected. For nested backends, the connection status should reflect the
5581 * connection to the parent display server.
5582 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005583 * When the connection status changes, it schedules a call to the heads_changed
Pekka Paalanene19970f2017-08-28 14:11:02 +03005584 * hook and sets the device_changed flag.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005585 *
5586 * \sa weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005587 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005588 * \internal
5589 */
5590WL_EXPORT void
5591weston_head_set_connection_status(struct weston_head *head, bool connected)
5592{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005593 if (head->connected == connected)
5594 return;
5595
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005596 head->connected = connected;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005597
Pekka Paalanene19970f2017-08-28 14:11:02 +03005598 weston_head_set_device_changed(head);
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005599}
5600
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305601static void
5602weston_output_compute_protection(struct weston_output *output)
5603{
5604 struct weston_head *head;
5605 enum weston_hdcp_protection op_protection;
5606 bool op_protection_valid = false;
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305607 struct weston_compositor *wc = output->compositor;
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305608
5609 wl_list_for_each(head, &output->head_list, output_link) {
5610 if (!op_protection_valid) {
5611 op_protection = head->current_protection;
5612 op_protection_valid = true;
5613 }
5614 if (head->current_protection < op_protection)
5615 op_protection = head->current_protection;
5616 }
5617
5618 if (!op_protection_valid)
5619 op_protection = WESTON_HDCP_DISABLE;
5620
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305621 if (output->current_protection != op_protection) {
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305622 output->current_protection = op_protection;
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305623 weston_output_damage(output);
Ankit Nautiyalfaa5ab42019-07-08 13:23:24 +05305624 weston_schedule_surface_protection_update(wc);
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305625 }
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305626}
5627
5628WL_EXPORT void
5629weston_head_set_content_protection_status(struct weston_head *head,
5630 enum weston_hdcp_protection status)
5631{
5632 head->current_protection = status;
5633 if (head->output)
5634 weston_output_compute_protection(head->output);
5635}
5636
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005637/** Is the head currently connected?
5638 *
5639 * \param head The head to query.
5640 * \return Connection status.
5641 *
5642 * Returns true if the head is physically connected to a monitor, or in
5643 * case of a nested backend returns true when there is a connection to the
5644 * parent display server.
5645 *
5646 * This is independent from the head being enabled.
5647 *
5648 * \sa weston_head_is_enabled
Marius Vlad78984ee2019-06-11 00:05:08 +03005649 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005650 */
5651WL_EXPORT bool
5652weston_head_is_connected(struct weston_head *head)
5653{
5654 return head->connected;
5655}
5656
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005657/** Is the head currently enabled?
5658 *
5659 * \param head The head to query.
5660 * \return Video status.
5661 *
5662 * Returns true if the head is currently transmitting a video stream.
5663 *
5664 * This is independent of the head being connected.
5665 *
5666 * \sa weston_head_is_connected
Marius Vlad78984ee2019-06-11 00:05:08 +03005667 * \ingroup head
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005668 */
5669WL_EXPORT bool
5670weston_head_is_enabled(struct weston_head *head)
5671{
5672 if (!head->output)
5673 return false;
5674
5675 return head->output->enabled;
5676}
5677
Pekka Paalanene19970f2017-08-28 14:11:02 +03005678/** Has the device information changed?
5679 *
5680 * \param head The head to query.
5681 * \return True if the device information has changed since last reset.
5682 *
5683 * The information about the connected display device, e.g. a monitor, may
5684 * change without being disconnected in between. Changing information
5685 * causes a call to the heads_changed hook.
5686 *
5687 * The information includes make, model, serial number, physical size,
5688 * and sub-pixel type. The connection status is also included.
5689 *
5690 * \sa weston_head_reset_device_changed, weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005691 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03005692 */
5693WL_EXPORT bool
5694weston_head_is_device_changed(struct weston_head *head)
5695{
5696 return head->device_changed;
5697}
5698
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005699/** Does the head represent a non-desktop display?
5700 *
5701 * \param head The head to query.
5702 * \return True if the device is a non-desktop display.
5703 *
5704 * Non-desktop heads are not attached to outputs by default.
5705 * This stops weston from extending the desktop onto head mounted displays.
5706 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005707 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005708 */
5709WL_EXPORT bool
5710weston_head_is_non_desktop(struct weston_head *head)
5711{
5712 return head->non_desktop;
5713}
5714
Pekka Paalanene19970f2017-08-28 14:11:02 +03005715/** Acknowledge device information change
5716 *
5717 * \param head The head to acknowledge.
5718 *
5719 * Clears the device changed flag on this head. When a compositor has processed
5720 * device information, it should call this to be able to notice further
5721 * changes.
5722 *
5723 * \sa weston_head_is_device_changed
Marius Vlad78984ee2019-06-11 00:05:08 +03005724 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03005725 */
5726WL_EXPORT void
5727weston_head_reset_device_changed(struct weston_head *head)
5728{
5729 head->device_changed = false;
5730}
5731
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005732/** Get the name of a head
5733 *
5734 * \param head The head to query.
5735 * \return The head's name, not NULL.
5736 *
5737 * The name depends on the backend. The DRM backend uses connector names,
5738 * other backends may use hardcoded names or user-given names.
Marius Vlad78984ee2019-06-11 00:05:08 +03005739 *
5740 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005741 */
5742WL_EXPORT const char *
5743weston_head_get_name(struct weston_head *head)
5744{
5745 return head->name;
5746}
5747
5748/** Get the output the head is attached to
5749 *
5750 * \param head The head to query.
5751 * \return The output the head is attached to, or NULL if detached.
Marius Vlad78984ee2019-06-11 00:05:08 +03005752 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005753 */
5754WL_EXPORT struct weston_output *
5755weston_head_get_output(struct weston_head *head)
5756{
5757 return head->output;
5758}
5759
Lucas Stacha69cb712019-11-25 23:29:31 +00005760/** Get the head's native transformation
5761 *
5762 * \param head The head to query.
5763 * \return The head's native transform, as a WL_OUTPUT_TRANSFORM_* value
5764 *
5765 * A weston_head may have a 'native' transform provided by the backend.
5766 * Examples include panels which are physically rotated, where the rotation
5767 * is recorded and described as part of the system configuration. This call
5768 * will return any known native transform for the head.
5769 *
5770 * \ingroup head
5771 */
5772WL_EXPORT uint32_t
5773weston_head_get_transform(struct weston_head *head)
5774{
5775 return head->transform;
5776}
5777
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005778/** Add destroy callback for a head
5779 *
5780 * \param head The head to watch for.
5781 * \param listener The listener to add. The \c notify member must be set.
5782 *
5783 * Heads may get destroyed for various reasons by the backends. If a head is
5784 * attached to an output, the compositor should listen for head destruction
5785 * and reconfigure or destroy the output if necessary.
5786 *
5787 * The destroy callbacks will be called on weston_head destruction before any
5788 * automatic detaching from an associated weston_output and before any
5789 * weston_head information is lost.
5790 *
5791 * The \c data argument to the notify callback is the weston_head being
5792 * destroyed.
Marius Vlad78984ee2019-06-11 00:05:08 +03005793 *
5794 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005795 */
5796WL_EXPORT void
5797weston_head_add_destroy_listener(struct weston_head *head,
5798 struct wl_listener *listener)
5799{
5800 wl_signal_add(&head->destroy_signal, listener);
5801}
5802
5803/** Look up destroy listener for a head
5804 *
5805 * \param head The head to query.
5806 * \param notify The notify function used used for the added destroy listener.
5807 * \return The listener, or NULL if not found.
5808 *
5809 * This looks up the previously added destroy listener struct based on the
5810 * notify function it has. The listener can be used to access user data
5811 * through \c container_of().
5812 *
5813 * \sa wl_signal_get()
Marius Vlad78984ee2019-06-11 00:05:08 +03005814 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005815 */
5816WL_EXPORT struct wl_listener *
5817weston_head_get_destroy_listener(struct weston_head *head,
5818 wl_notify_func_t notify)
5819{
5820 return wl_signal_get(&head->destroy_signal, notify);
5821}
5822
David Fort0de859e2016-05-27 23:22:57 +02005823/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005824static void
David Fort0de859e2016-05-27 23:22:57 +02005825weston_compositor_reflow_outputs(struct weston_compositor *compositor,
5826 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005827{
5828 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02005829 bool start_resizing = false;
5830
5831 if (!delta_width)
5832 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005833
5834 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02005835 if (output == resized_output) {
5836 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005837 continue;
5838 }
5839
David Fort0de859e2016-05-27 23:22:57 +02005840 if (start_resizing) {
5841 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005842 output->dirty = 1;
5843 }
5844 }
5845}
5846
Pekka Paalanend72bad22017-03-29 17:01:41 +03005847static void
Scott Moreauccbf29d2012-02-22 14:21:41 -07005848weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005849{
Scott Moreau850ca422012-05-21 15:21:25 -06005850 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05005851
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005852 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005853 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06005854
Scott Moreauccbf29d2012-02-22 14:21:41 -07005855 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06005856 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005857 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005858 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005859 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005860 weston_matrix_scale(&output->matrix, magnification,
5861 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005862 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005863
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005864 switch (output->transform) {
5865 case WL_OUTPUT_TRANSFORM_FLIPPED:
5866 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5867 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5868 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5869 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5870 weston_matrix_scale(&output->matrix, -1, 1, 1);
5871 break;
5872 }
5873
5874 switch (output->transform) {
5875 default:
5876 case WL_OUTPUT_TRANSFORM_NORMAL:
5877 case WL_OUTPUT_TRANSFORM_FLIPPED:
5878 break;
5879 case WL_OUTPUT_TRANSFORM_90:
5880 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +02005881 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5882 weston_matrix_rotate_xy(&output->matrix, 0, -1);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005883 break;
5884 case WL_OUTPUT_TRANSFORM_180:
5885 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5886 weston_matrix_translate(&output->matrix,
5887 -output->width, -output->height, 0);
5888 weston_matrix_rotate_xy(&output->matrix, -1, 0);
5889 break;
5890 case WL_OUTPUT_TRANSFORM_270:
5891 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +02005892 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
5893 weston_matrix_rotate_xy(&output->matrix, 0, 1);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005894 break;
5895 }
5896
5897 if (output->current_scale != 1)
5898 weston_matrix_scale(&output->matrix,
5899 output->current_scale,
5900 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01005901
Scott Moreauccbf29d2012-02-22 14:21:41 -07005902 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05005903
5904 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005905}
5906
Scott Moreau1bad5db2012-08-18 01:04:05 -06005907static void
Alexander Larsson0b135062013-05-28 16:23:36 +02005908weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06005909{
5910 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005911 output->native_scale = scale;
5912 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06005913
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005914 convert_size_by_transform_scale(&output->width, &output->height,
5915 output->current_mode->width,
5916 output->current_mode->height,
5917 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02005918}
5919
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005920static void
5921weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07005922{
5923 output->x = x;
5924 output->y = y;
5925
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005926 pixman_region32_fini(&output->region);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005927 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06005928 output->width,
5929 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005930}
5931
Marius Vlad55d87362019-06-11 01:15:35 +03005932/**
5933 * \ingroup output
5934 */
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005935WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005936weston_output_move(struct weston_output *output, int x, int y)
5937{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005938 struct weston_head *head;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005939 struct wl_resource *resource;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005940 int ver;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005941
5942 output->move_x = x - output->x;
5943 output->move_y = y - output->y;
5944
5945 if (output->move_x == 0 && output->move_y == 0)
5946 return;
5947
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005948 weston_output_init_geometry(output, x, y);
5949
5950 output->dirty = 1;
5951
5952 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005953 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005954
5955 /* Notify clients of the change for output position. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005956 wl_list_for_each(head, &output->head_list, output_link) {
5957 wl_resource_for_each(resource, &head->resource_list) {
5958 wl_output_send_geometry(resource,
5959 output->x,
5960 output->y,
5961 head->mm_width,
5962 head->mm_height,
5963 head->subpixel,
5964 head->make,
5965 head->model,
5966 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08005967
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005968 ver = wl_resource_get_version(resource);
5969 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
5970 wl_output_send_done(resource);
5971 }
Roman Gilge97391c2019-03-29 13:01:06 +01005972
5973 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
5974 zxdg_output_v1_send_logical_position(resource,
5975 output->x,
5976 output->y);
5977 zxdg_output_v1_send_done(resource);
5978 }
Quanxian Wangb2c86362014-03-14 09:16:25 +08005979 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005980}
5981
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005982/** Signal that a pending output is taken into use.
5983 *
5984 * Removes the output from the pending list and adds it to the compositor's
5985 * list of enabled outputs. The output created signal is emitted.
Giulio Camuffob1147152015-05-06 21:41:57 +03005986 *
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005987 * The output gets an internal ID assigned, and the wl_output global is
5988 * created.
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005989 *
Giulio Camuffob1147152015-05-06 21:41:57 +03005990 * \param compositor The compositor instance.
5991 * \param output The output to be added.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005992 *
5993 * \internal
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005994 * \ingroup compositor
Giulio Camuffob1147152015-05-06 21:41:57 +03005995 */
Pekka Paalanenf9681b52017-03-29 16:58:48 +03005996static void
Giulio Camuffob1147152015-05-06 21:41:57 +03005997weston_compositor_add_output(struct weston_compositor *compositor,
5998 struct weston_output *output)
5999{
Armin Krezoviće5403842016-08-05 15:28:29 +02006000 struct weston_view *view, *next;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03006001 struct weston_head *head;
Armin Krezoviće5403842016-08-05 15:28:29 +02006002
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006003 assert(!output->enabled);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03006004
6005 /* Verify we haven't reached the limit of 32 available output IDs */
6006 assert(ffs(~compositor->output_id_pool) > 0);
6007
6008 /* Invert the output id pool and look for the lowest numbered
6009 * switch (the least significant bit). Take that bit's position
6010 * as our ID, and mark it used in the compositor's output_id_pool.
6011 */
6012 output->id = ffs(~compositor->output_id_pool) - 1;
6013 compositor->output_id_pool |= 1u << output->id;
6014
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006015 wl_list_remove(&output->link);
Giulio Camuffob1147152015-05-06 21:41:57 +03006016 wl_list_insert(compositor->output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006017 output->enabled = true;
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006018
Pekka Paalanendcac3512017-12-08 14:13:34 +02006019 wl_list_for_each(head, &output->head_list, output_link)
6020 weston_head_add_global(head);
Pekka Paalanen2210ad02017-03-30 15:48:06 +03006021
Giulio Camuffob1147152015-05-06 21:41:57 +03006022 wl_signal_emit(&compositor->output_created_signal, output);
Armin Krezoviće5403842016-08-05 15:28:29 +02006023
6024 wl_list_for_each_safe(view, next, &compositor->view_list, link)
6025 weston_view_geometry_dirty(view);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04006026}
6027
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006028/** Transform device coordinates into global coordinates
6029 *
Marius Vlada2dace22019-06-12 16:05:44 +03006030 * \param output the weston_output object
6031 * \param[in] device_x X coordinate in device units.
6032 * \param[in] device_y Y coordinate in device units.
6033 * \param[out] x X coordinate in the global space.
6034 * \param[out] y Y coordinate in the global space.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006035 *
Marius Vlada2dace22019-06-12 16:05:44 +03006036 * Transforms coordinates from the device coordinate space (physical pixel
6037 * units) to the global coordinate space (logical pixel units). This takes
6038 * into account output transform and scale.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006039 *
Marius Vlad55d87362019-06-11 01:15:35 +03006040 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006041 * \internal
6042 */
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006043WL_EXPORT void
6044weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02006045 double device_x, double device_y,
6046 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006047{
Derek Foreman0f679412014-10-02 13:41:17 -05006048 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02006049 device_x,
6050 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05006051 0.0,
6052 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006053
Derek Foreman67a18b92015-03-24 11:36:14 -05006054 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006055
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02006056 *x = p.f[0] / p.f[3];
6057 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006058}
6059
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006060/** Removes output from compositor's list of enabled outputs
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006061 *
6062 * \param output The weston_output object that is being removed.
6063 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006064 * The following happens:
6065 *
6066 * - The output assignments of all views in the current scenegraph are
6067 * recomputed.
6068 *
6069 * - Presentation feedback is discarded.
6070 *
6071 * - Compositor is notified that outputs were changed and
6072 * applies the necessary changes to re-layout outputs.
6073 *
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006074 * - The output is put back in the pending outputs list.
6075 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006076 * - Signal is emitted to notify all users of the weston_output
6077 * object that the output is being destroyed.
6078 *
6079 * - wl_output protocol objects referencing this weston_output
Pekka Paalanen2210ad02017-03-30 15:48:06 +03006080 * are made inert, and the wl_output global is removed.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006081 *
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03006082 * - The output's internal ID is released.
6083 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006084 * \ingroup compositor
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006085 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006086 */
6087static void
6088weston_compositor_remove_output(struct weston_output *output)
6089{
Pekka Paalanenbccda712017-03-29 16:16:04 +03006090 struct weston_compositor *compositor = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006091 struct weston_view *view;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03006092 struct weston_head *head;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006093
6094 assert(output->destroying);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006095 assert(output->enabled);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006096
Pekka Paalanenbccda712017-03-29 16:16:04 +03006097 wl_list_for_each(view, &compositor->view_list, link) {
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006098 if (view->output_mask & (1u << output->id))
6099 weston_view_assign_output(view);
6100 }
6101
6102 weston_presentation_feedback_discard_list(&output->feedback_list);
6103
Pekka Paalanen9711fd92018-06-21 14:26:18 +03006104 weston_compositor_reflow_outputs(compositor, output, -output->width);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006105
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006106 wl_list_remove(&output->link);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006107 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006108 output->enabled = false;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006109
Pekka Paalanenbccda712017-03-29 16:16:04 +03006110 wl_signal_emit(&compositor->output_destroyed_signal, output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006111 wl_signal_emit(&output->destroy_signal, output);
6112
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02006113 wl_list_for_each(head, &output->head_list, output_link)
6114 weston_head_remove_global(head);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03006115
6116 compositor->output_id_pool &= ~(1u << output->id);
6117 output->id = 0xffffffff; /* invalid */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006118}
6119
6120/** Sets the output scale for a given output.
6121 *
6122 * \param output The weston_output object that the scale is set for.
6123 * \param scale Scale factor for the given output.
6124 *
6125 * It only supports setting scale for an output that
6126 * is not enabled and it can only be ran once.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006127 *
Marius Vlad55d87362019-06-11 01:15:35 +03006128 * \ingroup ouput
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006129 */
6130WL_EXPORT void
6131weston_output_set_scale(struct weston_output *output,
6132 int32_t scale)
6133{
6134 /* We can only set scale on a disabled output */
6135 assert(!output->enabled);
6136
6137 /* We only want to set scale once */
6138 assert(!output->scale);
6139
6140 output->scale = scale;
6141}
6142
6143/** Sets the output transform for a given output.
6144 *
6145 * \param output The weston_output object that the transform is set for.
6146 * \param transform Transform value for the given output.
6147 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006148 * Refer to wl_output::transform section located at
6149 * https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_output
6150 * for list of values that can be passed to this function.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006151 *
Marius Vlad55d87362019-06-11 01:15:35 +03006152 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006153 */
6154WL_EXPORT void
6155weston_output_set_transform(struct weston_output *output,
6156 uint32_t transform)
6157{
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006158 struct weston_pointer_motion_event ev;
6159 struct wl_resource *resource;
6160 struct weston_seat *seat;
6161 pixman_region32_t old_region;
6162 int mid_x, mid_y;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006163 struct weston_head *head;
6164 int ver;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006165
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006166 if (!output->enabled && output->transform == UINT32_MAX) {
6167 output->transform = transform;
6168 return;
6169 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006170
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006171 weston_output_transform_scale_init(output, transform, output->scale);
6172
6173 pixman_region32_init(&old_region);
6174 pixman_region32_copy(&old_region, &output->region);
6175
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006176 weston_output_init_geometry(output, output->x, output->y);
6177
6178 output->dirty = 1;
6179
6180 /* Notify clients of the change for output transform. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006181 wl_list_for_each(head, &output->head_list, output_link) {
6182 wl_resource_for_each(resource, &head->resource_list) {
6183 wl_output_send_geometry(resource,
6184 output->x,
6185 output->y,
6186 head->mm_width,
6187 head->mm_height,
6188 head->subpixel,
6189 head->make,
6190 head->model,
6191 output->transform);
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006192
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006193 ver = wl_resource_get_version(resource);
6194 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
6195 wl_output_send_done(resource);
6196 }
Roman Gilge97391c2019-03-29 13:01:06 +01006197 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
6198 zxdg_output_v1_send_logical_position(resource,
6199 output->x,
6200 output->y);
6201 zxdg_output_v1_send_logical_size(resource,
6202 output->width,
6203 output->height);
6204 zxdg_output_v1_send_done(resource);
6205 }
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006206 }
6207
6208 /* we must ensure that pointers are inside output, otherwise they disappear */
6209 mid_x = output->x + output->width / 2;
6210 mid_y = output->y + output->height / 2;
6211
6212 ev.mask = WESTON_POINTER_MOTION_ABS;
6213 ev.x = wl_fixed_to_double(wl_fixed_from_int(mid_x));
6214 ev.y = wl_fixed_to_double(wl_fixed_from_int(mid_y));
6215
6216 wl_list_for_each(seat, &output->compositor->seat_list, link) {
6217 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
6218
6219 if (pointer && pixman_region32_contains_point(&old_region,
6220 wl_fixed_to_int(pointer->x),
6221 wl_fixed_to_int(pointer->y),
6222 NULL))
6223 weston_pointer_move(pointer, &ev);
6224 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006225}
6226
6227/** Initializes a weston_output object with enough data so
6228 ** an output can be configured.
6229 *
6230 * \param output The weston_output object to initialize
6231 * \param compositor The compositor instance.
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006232 * \param name Name for the output (the string is copied).
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006233 *
6234 * Sets initial values for fields that are expected to be
6235 * configured either by compositors or backends.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006236 *
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006237 * The name is used in logs, and can be used by compositors as a configuration
6238 * identifier.
6239 *
Marius Vlad55d87362019-06-11 01:15:35 +03006240 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006241 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006242 */
6243WL_EXPORT void
Armin Krezović40087402016-09-30 14:11:12 +02006244weston_output_init(struct weston_output *output,
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006245 struct weston_compositor *compositor,
6246 const char *name)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006247{
6248 output->compositor = compositor;
6249 output->destroying = 0;
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006250 output->name = strdup(name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006251 wl_list_init(&output->link);
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006252 wl_signal_init(&output->user_destroy_signal);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006253 output->enabled = false;
Ankit Nautiyal2690a772019-03-25 17:57:59 +05306254 output->desired_protection = WESTON_HDCP_DISABLE;
Ankit Nautiyal2844f8e2019-04-03 10:14:59 +05306255 output->allow_protection = true;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006256
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006257 wl_list_init(&output->head_list);
6258
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006259 /* Add some (in)sane defaults which can be used
6260 * for checking if an output was properly configured
6261 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006262 output->scale = 0;
6263 /* Can't use -1 on uint32_t and 0 is valid enum value */
6264 output->transform = UINT32_MAX;
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006265
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006266 pixman_region32_init(&output->region);
Pekka Paalanen42704142017-09-06 16:47:52 +03006267 wl_list_init(&output->mode_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006268}
6269
6270/** Adds weston_output object to pending output list.
6271 *
6272 * \param output The weston_output object to add
6273 * \param compositor The compositor instance.
6274 *
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03006275 * The opposite of this operation is built into weston_output_release().
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006276 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006277 * \ingroup compositor
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006278 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006279 */
6280WL_EXPORT void
6281weston_compositor_add_pending_output(struct weston_output *output,
6282 struct weston_compositor *compositor)
6283{
Pekka Paalanene952a012017-03-29 17:14:00 +03006284 assert(output->disable);
6285 assert(output->enable);
6286
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006287 wl_list_remove(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006288 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006289}
6290
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006291/** Create a string with the attached heads' names.
6292 *
6293 * The string must be free()'d.
Marius Vlad55d87362019-06-11 01:15:35 +03006294 *
6295 * \ingroup output
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006296 */
6297static char *
6298weston_output_create_heads_string(struct weston_output *output)
6299{
6300 FILE *fp;
6301 char *str = NULL;
6302 size_t size = 0;
6303 struct weston_head *head;
6304 const char *sep = "";
6305
6306 fp = open_memstream(&str, &size);
6307 if (!fp)
6308 return NULL;
6309
6310 wl_list_for_each(head, &output->head_list, output_link) {
6311 fprintf(fp, "%s%s", sep, head->name);
6312 sep = ", ";
6313 }
6314 fclose(fp);
6315
6316 return str;
6317}
6318
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006319/** Constructs a weston_output object that can be used by the compositor.
6320 *
Pekka Paalanencc201e42017-03-30 15:11:25 +03006321 * \param output The weston_output object that needs to be enabled. Must not
Pekka Paalanenddce54d2017-08-23 16:00:21 +03006322 * be enabled already. Must have at least one head attached.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006323 *
6324 * Output coordinates are calculated and each new output is by default
6325 * assigned to the right of previous one.
6326 *
6327 * Sets up the transformation, zoom, and geometry of the output using
6328 * the properties that need to be configured by the compositor.
6329 *
6330 * Establishes a repaint timer for the output with the relevant display
6331 * object's event loop. See output_repaint_timer_handler().
6332 *
6333 * The output is assigned an ID. Weston can support up to 32 distinct
6334 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
6335 * is referred to and used to find the first available ID number, and
6336 * then this ID is marked as used in output_id_pool.
6337 *
6338 * The output is also assigned a Wayland global with the wl_output
6339 * external interface.
6340 *
6341 * Backend specific function is called to set up the output output.
6342 *
6343 * Output is added to the compositor's output list
6344 *
6345 * If the backend specific function fails, the weston_output object
6346 * is returned to a state it was before calling this function and
6347 * is added to the compositor's pending_output_list in case it needs
6348 * to be reconfigured or just so it can be destroyed at shutdown.
6349 *
6350 * 0 is returned on success, -1 on failure.
Marius Vlad55d87362019-06-11 01:15:35 +03006351 *
6352 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006353 */
6354WL_EXPORT int
6355weston_output_enable(struct weston_output *output)
6356{
Armin Krezović782f5df2016-09-30 14:11:11 +02006357 struct weston_compositor *c = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006358 struct weston_output *iterator;
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03006359 struct weston_head *head;
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006360 char *head_names;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006361 int x = 0, y = 0;
6362
Pekka Paalanencc201e42017-03-30 15:11:25 +03006363 if (output->enabled) {
6364 weston_log("Error: attempt to enable an enabled output '%s'\n",
6365 output->name);
6366 return -1;
6367 }
6368
Pekka Paalanenddce54d2017-08-23 16:00:21 +03006369 if (wl_list_empty(&output->head_list)) {
6370 weston_log("Error: cannot enable output '%s' without heads.\n",
6371 output->name);
6372 return -1;
6373 }
6374
Pekka Paalanen586e1ac2017-09-14 16:17:59 +03006375 if (wl_list_empty(&output->mode_list) || !output->current_mode) {
6376 weston_log("Error: no video mode for output '%s'.\n",
6377 output->name);
6378 return -1;
6379 }
6380
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03006381 wl_list_for_each(head, &output->head_list, output_link) {
6382 assert(head->make);
6383 assert(head->model);
6384 }
6385
Armin Krezović782f5df2016-09-30 14:11:11 +02006386 iterator = container_of(c->output_list.prev,
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006387 struct weston_output, link);
6388
Armin Krezović782f5df2016-09-30 14:11:11 +02006389 if (!wl_list_empty(&c->output_list))
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006390 x = iterator->x + iterator->width;
6391
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006392 /* Make sure the scale is set up */
6393 assert(output->scale);
6394
6395 /* Make sure we have a transform set */
6396 assert(output->transform != UINT32_MAX);
6397
Armin Krezović782f5df2016-09-30 14:11:11 +02006398 output->x = x;
6399 output->y = y;
6400 output->dirty = 1;
6401 output->original_scale = output->scale;
6402
Marius Vlad3a2f8292019-11-04 17:53:46 +02006403 wl_signal_init(&output->frame_signal);
6404 wl_signal_init(&output->destroy_signal);
6405
Armin Krezović782f5df2016-09-30 14:11:11 +02006406 weston_output_transform_scale_init(output, output->transform, output->scale);
6407 weston_output_init_zoom(output);
6408
6409 weston_output_init_geometry(output, x, y);
6410 weston_output_damage(output);
6411
Armin Krezović782f5df2016-09-30 14:11:11 +02006412 wl_list_init(&output->animation_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02006413 wl_list_init(&output->feedback_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02006414
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006415 /* Enable the output (set up the crtc or create a
6416 * window representing the output, set up the
6417 * renderer, etc)
6418 */
6419 if (output->enable(output) < 0) {
6420 weston_log("Enabling output \"%s\" failed.\n", output->name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006421 return -1;
6422 }
6423
6424 weston_compositor_add_output(output->compositor, output);
6425
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006426 head_names = weston_output_create_heads_string(output);
6427 weston_log("Output '%s' enabled with head(s) %s\n",
6428 output->name, head_names);
6429 free(head_names);
6430
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006431 return 0;
6432}
6433
6434/** Converts a weston_output object to a pending output state, so it
6435 ** can be configured again or destroyed.
6436 *
6437 * \param output The weston_output object that needs to be disabled.
6438 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006439 * Calls a backend specific function to disable an output, in case
6440 * such function exists.
6441 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03006442 * The backend specific disable function may choose to postpone the disabling
6443 * by returning a negative value, in which case this function returns early.
6444 * In that case the backend will guarantee the output will be disabled soon
6445 * by the backend calling this function again. One must not attempt to re-enable
6446 * the output until that happens.
6447 *
6448 * Otherwise, if the output is being used by the compositor, it is removed
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006449 * from weston's output_list (see weston_compositor_remove_output())
6450 * and is returned to a state it was before weston_output_enable()
6451 * was ran (see weston_output_enable_undo()).
6452 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03006453 * See weston_output_init() for more information on the
6454 * state output is returned to.
Pekka Paalanencc201e42017-03-30 15:11:25 +03006455 *
6456 * If the output has never been enabled yet, this function can still be
6457 * called to ensure that the output is actually turned off rather than left
6458 * in the state it was discovered in.
Marius Vlad55d87362019-06-11 01:15:35 +03006459 *
6460 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006461 */
6462WL_EXPORT void
6463weston_output_disable(struct weston_output *output)
6464{
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006465 /* Should we rename this? */
6466 output->destroying = 1;
6467
Pekka Paalanenc65df642017-03-29 15:45:46 +03006468 /* Disable is called unconditionally also for not-enabled outputs,
6469 * because at compositor start-up, if there is an output that is
6470 * already on but the compositor wants to turn it off, we have to
6471 * forward the turn-off to the backend so it knows to do it.
6472 * The backend cannot initially turn off everything, because it
6473 * would cause unnecessary mode-sets for all outputs the compositor
6474 * wants to be on.
6475 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006476 if (output->disable(output) < 0)
6477 return;
6478
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006479 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006480 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006481
6482 output->destroying = 0;
6483}
6484
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006485/** Forces a synchronous call to heads_changed hook
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006486 *
6487 * \param compositor The compositor instance
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006488 *
6489 * If there are new or changed heads, calls the heads_changed hook and
6490 * returns after the hook returns.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006491 *
6492 * \ingroup compositor
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006493 */
6494WL_EXPORT void
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006495weston_compositor_flush_heads_changed(struct weston_compositor *compositor)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006496{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03006497 if (compositor->heads_changed_source) {
6498 wl_event_source_remove(compositor->heads_changed_source);
6499 weston_compositor_call_heads_changed(compositor);
6500 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006501}
6502
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006503/** Add destroy callback for an output
6504 *
6505 * \param output The output to watch.
6506 * \param listener The listener to add. The \c notify member must be set.
6507 *
6508 * The listener callback will be called when user destroys an output. This
6509 * may be delayed by a backend in some cases. The main purpose of the
6510 * listener is to allow hooking up custom data to the output. The custom data
6511 * can be fetched via weston_output_get_destroy_listener() followed by
6512 * container_of().
6513 *
6514 * The \c data argument to the notify callback is the weston_output being
6515 * destroyed.
6516 *
6517 * @note This is for the final destruction of an output, not when it gets
6518 * disabled. If you want to keep track of enabled outputs, this is not it.
Marius Vlad55d87362019-06-11 01:15:35 +03006519 *
6520 * \ingroup ouput
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006521 */
6522WL_EXPORT void
6523weston_output_add_destroy_listener(struct weston_output *output,
6524 struct wl_listener *listener)
6525{
6526 wl_signal_add(&output->user_destroy_signal, listener);
6527}
6528
6529/** Look up destroy listener for an output
6530 *
6531 * \param output The output to query.
6532 * \param notify The notify function used used for the added destroy listener.
6533 * \return The listener, or NULL if not found.
6534 *
6535 * This looks up the previously added destroy listener struct based on the
6536 * notify function it has. The listener can be used to access user data
6537 * through \c container_of().
6538 *
6539 * \sa wl_signal_get() weston_output_add_destroy_listener()
Marius Vlad55d87362019-06-11 01:15:35 +03006540 * \ingroup output
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006541 */
6542WL_EXPORT struct wl_listener *
6543weston_output_get_destroy_listener(struct weston_output *output,
6544 wl_notify_func_t notify)
6545{
6546 return wl_signal_get(&output->user_destroy_signal, notify);
6547}
6548
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006549/** Uninitialize an output
6550 *
6551 * Removes the output from the list of enabled outputs if necessary, but
6552 * does not call the backend's output disable function. The output will no
6553 * longer be in the list of pending outputs either.
6554 *
6555 * All fields of weston_output become uninitialized, i.e. should not be used
6556 * anymore. The caller can free the memory after this.
6557 *
Marius Vlad55d87362019-06-11 01:15:35 +03006558 * \ingroup ouput
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006559 * \internal
6560 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006561WL_EXPORT void
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03006562weston_output_release(struct weston_output *output)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006563{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006564 struct weston_head *head, *tmp;
6565
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006566 output->destroying = 1;
6567
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006568 wl_signal_emit(&output->user_destroy_signal, output);
6569
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03006570 if (output->idle_repaint_source)
6571 wl_event_source_remove(output->idle_repaint_source);
6572
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006573 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006574 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006575
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006576 pixman_region32_fini(&output->region);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006577 wl_list_remove(&output->link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006578
6579 wl_list_for_each_safe(head, tmp, &output->head_list, output_link)
6580 weston_head_detach(head);
6581
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006582 free(output->name);
6583}
6584
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006585/** Find an output by its given name
6586 *
6587 * \param compositor The compositor to search in.
6588 * \param name The output name to search for.
6589 * \return An existing output with the given name, or NULL if not found.
6590 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006591 * \ingroup compositor
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006592 */
6593WL_EXPORT struct weston_output *
6594weston_compositor_find_output_by_name(struct weston_compositor *compositor,
6595 const char *name)
6596{
6597 struct weston_output *output;
6598
6599 wl_list_for_each(output, &compositor->output_list, link)
6600 if (strcmp(output->name, name) == 0)
6601 return output;
6602
6603 wl_list_for_each(output, &compositor->pending_output_list, link)
6604 if (strcmp(output->name, name) == 0)
6605 return output;
6606
6607 return NULL;
6608}
6609
6610/** Create a named output
6611 *
6612 * \param compositor The compositor.
6613 * \param name The name for the output.
6614 * \return A new \c weston_output, or NULL on failure.
6615 *
6616 * This creates a new weston_output that starts with no heads attached.
6617 *
6618 * An output must be configured and it must have at least one head before
6619 * it can be enabled.
6620 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006621 * \ingroup compositor
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006622 */
6623WL_EXPORT struct weston_output *
6624weston_compositor_create_output(struct weston_compositor *compositor,
6625 const char *name)
6626{
6627 assert(compositor->backend->create_output);
6628
6629 if (weston_compositor_find_output_by_name(compositor, name)) {
6630 weston_log("Warning: attempted to create an output with a "
6631 "duplicate name '%s'.\n", name);
6632 return NULL;
6633 }
6634
6635 return compositor->backend->create_output(compositor, name);
6636}
6637
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006638/** Create an output for an unused head
6639 *
6640 * \param compositor The compositor.
6641 * \param head The head to attach to the output.
6642 * \return A new \c weston_output, or NULL on failure.
6643 *
6644 * This creates a new weston_output that starts with the given head attached.
6645 * The output inherits the name of the head. The head must not be already
6646 * attached to another output.
6647 *
6648 * An output must be configured before it can be enabled.
6649 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006650 * \ingroup compositor
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006651 */
6652WL_EXPORT struct weston_output *
6653weston_compositor_create_output_with_head(struct weston_compositor *compositor,
6654 struct weston_head *head)
6655{
6656 struct weston_output *output;
6657
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006658 output = weston_compositor_create_output(compositor, head->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006659 if (!output)
6660 return NULL;
6661
6662 if (weston_output_attach_head(output, head) < 0) {
Pekka Paalanen42c0e142017-10-27 12:07:49 +03006663 weston_output_destroy(output);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006664 return NULL;
6665 }
6666
6667 return output;
6668}
6669
6670/** Destroy an output
6671 *
6672 * \param output The output to destroy.
6673 *
6674 * The heads attached to the given output are detached and become unused again.
6675 *
6676 * It is not necessary to explicitly destroy all outputs at compositor exit.
Leandro Ribeiroca640d52020-01-27 19:12:01 -03006677 * weston_compositor_destroy() will automatically destroy any remaining
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006678 * outputs.
6679 *
Marius Vlad55d87362019-06-11 01:15:35 +03006680 * \ingroup ouput
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006681 */
6682WL_EXPORT void
6683weston_output_destroy(struct weston_output *output)
6684{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006685 output->destroy(output);
6686}
6687
Pekka Paalanencf0a4762017-04-04 16:36:07 +03006688/** When you need a head...
6689 *
6690 * This function is a hack, used until all code has been converted to become
6691 * multi-head aware.
6692 *
6693 * \param output The weston_output whose head to get.
6694 * \return The first head in the output's list.
Marius Vlad55d87362019-06-11 01:15:35 +03006695 *
6696 * \ingroup ouput
Pekka Paalanencf0a4762017-04-04 16:36:07 +03006697 */
6698WL_EXPORT struct weston_head *
6699weston_output_get_first_head(struct weston_output *output)
6700{
6701 if (wl_list_empty(&output->head_list))
6702 return NULL;
6703
6704 return container_of(output->head_list.next,
6705 struct weston_head, output_link);
6706}
6707
Ankit Nautiyal2844f8e2019-04-03 10:14:59 +05306708/** Allow/Disallow content-protection support for an output
6709 *
6710 * This function sets the allow_protection member for an output. Setting of
6711 * this field will allow the compositor to attempt content-protection for this
6712 * output, for a backend that supports the content-protection protocol.
6713 *
6714 * \param output The weston_output for whom the content-protection is to be
6715 * allowed.
6716 * \param allow_protection The bool value which is to be set.
6717 */
6718WL_EXPORT void
6719weston_output_allow_protection(struct weston_output *output,
6720 bool allow_protection)
6721{
6722 output->allow_protection = allow_protection;
6723}
6724
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01006725static void
Roman Gilge97391c2019-03-29 13:01:06 +01006726xdg_output_unlist(struct wl_resource *resource)
6727{
6728 wl_list_remove(wl_resource_get_link(resource));
6729}
6730
6731static void
6732xdg_output_destroy(struct wl_client *client, struct wl_resource *resource)
6733{
6734 wl_resource_destroy(resource);
6735}
6736
6737static const struct zxdg_output_v1_interface xdg_output_interface = {
6738 xdg_output_destroy
6739};
6740
6741static void
6742xdg_output_manager_destroy(struct wl_client *client,
6743 struct wl_resource *resource)
6744{
6745 wl_resource_destroy(resource);
6746}
6747
6748static void
6749xdg_output_manager_get_xdg_output(struct wl_client *client,
6750 struct wl_resource *manager,
6751 uint32_t id,
6752 struct wl_resource *output_resource)
6753{
6754 int version = wl_resource_get_version(manager);
6755 struct weston_head *head = wl_resource_get_user_data(output_resource);
6756 struct weston_output *output = head->output;
6757 struct wl_resource *resource;
6758
6759 resource = wl_resource_create(client, &zxdg_output_v1_interface,
6760 version, id);
6761 if (resource == NULL) {
6762 wl_client_post_no_memory(client);
6763 return;
6764 }
6765
6766 wl_list_insert(&head->xdg_output_resource_list,
6767 wl_resource_get_link(resource));
6768
6769 wl_resource_set_implementation(resource, &xdg_output_interface,
6770 NULL, xdg_output_unlist);
6771
6772 zxdg_output_v1_send_logical_position(resource, output->x, output->y);
6773 zxdg_output_v1_send_logical_size(resource,
6774 output->width,
6775 output->height);
6776 if (version >= ZXDG_OUTPUT_V1_NAME_SINCE_VERSION)
6777 zxdg_output_v1_send_name(resource, head->name);
6778
6779 zxdg_output_v1_send_done(resource);
6780}
6781
6782static const struct zxdg_output_manager_v1_interface xdg_output_manager_interface = {
6783 xdg_output_manager_destroy,
6784 xdg_output_manager_get_xdg_output
6785};
6786
6787static void
6788bind_xdg_output_manager(struct wl_client *client,
6789 void *data, uint32_t version, uint32_t id)
6790{
6791 struct wl_resource *resource;
6792
6793 resource = wl_resource_create(client, &zxdg_output_manager_v1_interface,
6794 version, id);
6795 if (resource == NULL) {
6796 wl_client_post_no_memory(client);
6797 return;
6798 }
6799
6800 wl_resource_set_implementation(resource, &xdg_output_manager_interface,
6801 NULL, NULL);
6802}
6803
6804static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006805destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006806{
Jonny Lamb74130762013-11-26 18:19:46 +01006807 struct weston_surface *surface =
6808 wl_resource_get_user_data(resource);
6809
Pekka Paalanen4826f872016-04-22 14:14:38 +03006810 if (!surface)
6811 return;
6812
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006813 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02006814 surface->pending.buffer_viewport.buffer.src_width =
6815 wl_fixed_from_int(-1);
6816 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006817 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006818}
6819
6820static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006821viewport_destroy(struct wl_client *client,
6822 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006823{
6824 wl_resource_destroy(resource);
6825}
6826
6827static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006828viewport_set_source(struct wl_client *client,
6829 struct wl_resource *resource,
6830 wl_fixed_t src_x,
6831 wl_fixed_t src_y,
6832 wl_fixed_t src_width,
6833 wl_fixed_t src_height)
6834{
6835 struct weston_surface *surface =
6836 wl_resource_get_user_data(resource);
6837
Pekka Paalanen4826f872016-04-22 14:14:38 +03006838 if (!surface) {
6839 wl_resource_post_error(resource,
6840 WP_VIEWPORT_ERROR_NO_SURFACE,
6841 "wl_surface for this viewport is no longer exists");
6842 return;
6843 }
6844
6845 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03006846 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006847
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006848 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03006849 src_height == wl_fixed_from_int(-1) &&
6850 src_x == wl_fixed_from_int(-1) &&
6851 src_y == wl_fixed_from_int(-1)) {
6852 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006853 surface->pending.buffer_viewport.buffer.src_width =
6854 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006855 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006856 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006857 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006858
Pekka Paalanen201769a2016-04-26 14:42:11 +03006859 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006860 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006861 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03006862 "wl_surface@%d viewport source "
6863 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
6864 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006865 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03006866 wl_fixed_to_double(src_height),
6867 wl_fixed_to_double(src_x),
6868 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006869 return;
6870 }
6871
6872 surface->pending.buffer_viewport.buffer.src_x = src_x;
6873 surface->pending.buffer_viewport.buffer.src_y = src_y;
6874 surface->pending.buffer_viewport.buffer.src_width = src_width;
6875 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006876 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006877}
6878
6879static void
6880viewport_set_destination(struct wl_client *client,
6881 struct wl_resource *resource,
6882 int32_t dst_width,
6883 int32_t dst_height)
6884{
6885 struct weston_surface *surface =
6886 wl_resource_get_user_data(resource);
6887
Pekka Paalanen4826f872016-04-22 14:14:38 +03006888 if (!surface) {
6889 wl_resource_post_error(resource,
6890 WP_VIEWPORT_ERROR_NO_SURFACE,
6891 "wl_surface for this viewport no longer exists");
6892 return;
6893 }
6894
6895 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006896
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006897 if (dst_width == -1 && dst_height == -1) {
6898 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006899 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006900 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006901 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006902 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006903
6904 if (dst_width <= 0 || dst_height <= 0) {
6905 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006906 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006907 "destination size must be positive (%dx%d)",
6908 dst_width, dst_height);
6909 return;
6910 }
6911
6912 surface->pending.buffer_viewport.surface.width = dst_width;
6913 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006914 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006915}
6916
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006917static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006918 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006919 viewport_set_source,
6920 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01006921};
6922
6923static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006924viewporter_destroy(struct wl_client *client,
6925 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006926{
6927 wl_resource_destroy(resource);
6928}
6929
6930static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006931viewporter_get_viewport(struct wl_client *client,
6932 struct wl_resource *viewporter,
6933 uint32_t id,
6934 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006935{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006936 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006937 struct weston_surface *surface =
6938 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006939 struct wl_resource *resource;
6940
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006941 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006942 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006943 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006944 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01006945 return;
6946 }
6947
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006948 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006949 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006950 if (resource == NULL) {
6951 wl_client_post_no_memory(client);
6952 return;
6953 }
6954
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006955 wl_resource_set_implementation(resource, &viewport_interface,
6956 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01006957
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006958 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006959}
6960
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006961static const struct wp_viewporter_interface viewporter_interface = {
6962 viewporter_destroy,
6963 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01006964};
6965
6966static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006967bind_viewporter(struct wl_client *client,
6968 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006969{
6970 struct wl_resource *resource;
6971
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006972 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006973 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006974 if (resource == NULL) {
6975 wl_client_post_no_memory(client);
6976 return;
6977 }
6978
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006979 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01006980 NULL, NULL);
6981}
6982
6983static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04006984destroy_presentation_feedback(struct wl_resource *feedback_resource)
6985{
6986 struct weston_presentation_feedback *feedback;
6987
6988 feedback = wl_resource_get_user_data(feedback_resource);
6989
6990 wl_list_remove(&feedback->link);
6991 free(feedback);
6992}
6993
6994static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006995presentation_destroy(struct wl_client *client, struct wl_resource *resource)
6996{
6997 wl_resource_destroy(resource);
6998}
6999
7000static void
7001presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04007002 struct wl_resource *presentation_resource,
7003 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007004 uint32_t callback)
7005{
Pekka Paalanen133e4392014-09-23 22:08:46 -04007006 struct weston_surface *surface;
7007 struct weston_presentation_feedback *feedback;
7008
7009 surface = wl_resource_get_user_data(surface_resource);
7010
Bryce Harringtonde16d892014-11-20 22:21:57 -08007011 feedback = zalloc(sizeof *feedback);
7012 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04007013 goto err_calloc;
7014
7015 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007016 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04007017 1, callback);
7018 if (!feedback->resource)
7019 goto err_create;
7020
7021 wl_resource_set_implementation(feedback->resource, NULL, feedback,
7022 destroy_presentation_feedback);
7023
7024 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
7025
7026 return;
7027
7028err_create:
7029 free(feedback);
7030
7031err_calloc:
7032 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007033}
7034
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007035static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007036 presentation_destroy,
7037 presentation_feedback
7038};
7039
7040static void
7041bind_presentation(struct wl_client *client,
7042 void *data, uint32_t version, uint32_t id)
7043{
7044 struct weston_compositor *compositor = data;
7045 struct wl_resource *resource;
7046
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007047 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06007048 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007049 if (resource == NULL) {
7050 wl_client_post_no_memory(client);
7051 return;
7052 }
7053
7054 wl_resource_set_implementation(resource, &presentation_implementation,
7055 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007056 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007057}
7058
7059static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05007060compositor_bind(struct wl_client *client,
7061 void *data, uint32_t version, uint32_t id)
7062{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007063 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05007064 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05007065
Jason Ekstranda85118c2013-06-27 20:17:02 -05007066 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06007067 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07007068 if (resource == NULL) {
7069 wl_client_post_no_memory(client);
7070 return;
7071 }
7072
7073 wl_resource_set_implementation(resource, &compositor_interface,
7074 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05007075}
7076
Daniel Stonece62cb32018-07-20 09:46:24 +01007077static const char *
7078output_repaint_status_text(struct weston_output *output)
7079{
7080 switch (output->repaint_status) {
7081 case REPAINT_NOT_SCHEDULED:
7082 return "no repaint";
7083 case REPAINT_BEGIN_FROM_IDLE:
7084 return "start_repaint_loop scheduled";
7085 case REPAINT_SCHEDULED:
7086 return "repaint scheduled";
7087 case REPAINT_AWAITING_COMPLETION:
7088 return "awaiting completion";
7089 }
7090
7091 assert(!"output_repaint_status_text missing enum");
7092 return NULL;
7093}
7094
7095static void
7096debug_scene_view_print_buffer(FILE *fp, struct weston_view *view)
7097{
7098 struct weston_buffer *buffer = view->surface->buffer_ref.buffer;
7099 struct wl_shm_buffer *shm;
7100 struct linux_dmabuf_buffer *dmabuf;
Marius Vlad00a6e012018-11-20 17:52:31 +02007101 const struct pixel_format_info *pixel_info = NULL;
Daniel Stonece62cb32018-07-20 09:46:24 +01007102
7103 if (!buffer) {
7104 fprintf(fp, "\t\t[buffer not available]\n");
7105 return;
7106 }
7107
7108 shm = wl_shm_buffer_get(buffer->resource);
7109 if (shm) {
Marius Vlad00a6e012018-11-20 17:52:31 +02007110 uint32_t _format = wl_shm_buffer_get_format(shm);
7111 pixel_info = pixel_format_get_info_shm(_format);
Daniel Stonece62cb32018-07-20 09:46:24 +01007112 fprintf(fp, "\t\tSHM buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02007113 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
7114 (unsigned long) _format,
7115 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Daniel Stonece62cb32018-07-20 09:46:24 +01007116 return;
7117 }
7118
7119 dmabuf = linux_dmabuf_buffer_get(buffer->resource);
7120 if (dmabuf) {
Marius Vlad00a6e012018-11-20 17:52:31 +02007121 pixel_info = pixel_format_get_info(dmabuf->attributes.format);
Daniel Stonece62cb32018-07-20 09:46:24 +01007122 fprintf(fp, "\t\tdmabuf buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02007123 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
7124 (unsigned long) dmabuf->attributes.format,
7125 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Daniel Stonece62cb32018-07-20 09:46:24 +01007126 fprintf(fp, "\t\t\tmodifier: 0x%llx\n",
7127 (unsigned long long) dmabuf->attributes.modifier[0]);
7128 return;
7129 }
7130
Marius Vlad253ba9a2019-03-17 18:22:21 +02007131 fprintf(fp, "\t\tEGL buffer\n");
Daniel Stonece62cb32018-07-20 09:46:24 +01007132}
7133
7134static void
7135debug_scene_view_print(FILE *fp, struct weston_view *view, int view_idx)
7136{
7137 struct weston_compositor *ec = view->surface->compositor;
7138 struct weston_output *output;
7139 char desc[512];
7140 pixman_box32_t *box;
7141 uint32_t surface_id = 0;
7142 pid_t pid = 0;
7143
7144 if (view->surface->resource) {
7145 struct wl_resource *resource = view->surface->resource;
7146 wl_client_get_credentials(wl_resource_get_client(resource),
7147 &pid, NULL, NULL);
7148 surface_id = wl_resource_get_id(view->surface->resource);
7149 }
7150
7151 if (!view->surface->get_label ||
7152 view->surface->get_label(view->surface, desc, sizeof(desc)) < 0) {
7153 strcpy(desc, "[no description available]");
7154 }
7155 fprintf(fp, "\tView %d (role %s, PID %d, surface ID %u, %s, %p):\n",
7156 view_idx, view->surface->role_name, pid, surface_id,
7157 desc, view);
7158
7159 box = pixman_region32_extents(&view->transform.boundingbox);
7160 fprintf(fp, "\t\tposition: (%d, %d) -> (%d, %d)\n",
7161 box->x1, box->y1, box->x2, box->y2);
7162 box = pixman_region32_extents(&view->transform.opaque);
7163
Michael Olbrichb7e5f102020-08-11 16:33:35 +02007164 if (weston_view_is_opaque(view, &view->transform.boundingbox)) {
Daniel Stonece62cb32018-07-20 09:46:24 +01007165 fprintf(fp, "\t\t[fully opaque]\n");
7166 } else if (!pixman_region32_not_empty(&view->transform.opaque)) {
7167 fprintf(fp, "\t\t[not opaque]\n");
7168 } else {
7169 fprintf(fp, "\t\t[opaque: (%d, %d) -> (%d, %d)]\n",
7170 box->x1, box->y1, box->x2, box->y2);
7171 }
7172
7173 if (view->alpha < 1.0)
7174 fprintf(fp, "\t\talpha: %f\n", view->alpha);
7175
7176 if (view->output_mask != 0) {
7177 bool first_output = true;
7178 fprintf(fp, "\t\toutputs: ");
7179 wl_list_for_each(output, &ec->output_list, link) {
7180 if (!(view->output_mask & (1 << output->id)))
7181 continue;
7182 fprintf(fp, "%s%d (%s)%s",
7183 (first_output) ? "" : ", ",
7184 output->id, output->name,
7185 (view->output == output) ? " (primary)" : "");
7186 first_output = false;
7187 }
7188 } else {
7189 fprintf(fp, "\t\t[no outputs]");
7190 }
7191
7192 fprintf(fp, "\n");
7193
7194 debug_scene_view_print_buffer(fp, view);
7195}
7196
Marius Vlad433f4e72019-02-17 22:14:23 +02007197static void
7198debug_scene_view_print_tree(struct weston_view *view,
Marius Vlada6acfa82019-03-17 18:10:09 +02007199 FILE *fp, int *view_idx)
Marius Vlad433f4e72019-02-17 22:14:23 +02007200{
7201 struct weston_subsurface *sub;
7202 struct weston_view *ev;
7203
7204 /*
7205 * print the view first, then we recursively go on printing
7206 * sub-surfaces. We bail out once no more sub-surfaces are available.
7207 */
Marius Vlada6acfa82019-03-17 18:10:09 +02007208 debug_scene_view_print(fp, view, *view_idx);
Marius Vlad433f4e72019-02-17 22:14:23 +02007209
7210 /* no more sub-surfaces */
7211 if (wl_list_empty(&view->surface->subsurface_list))
7212 return;
7213
7214 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
7215 wl_list_for_each(ev, &sub->surface->views, surface_link) {
Michael Olbrichef5f3232020-04-29 09:03:15 +02007216 /* only print the child views of the current view */
7217 if (ev->parent_view != view)
Marius Vlad433f4e72019-02-17 22:14:23 +02007218 continue;
Marius Vlada6acfa82019-03-17 18:10:09 +02007219
7220 (*view_idx)++;
Marius Vlad433f4e72019-02-17 22:14:23 +02007221 debug_scene_view_print_tree(ev, fp, view_idx);
7222 }
7223 }
7224}
7225
Daniel Stonece62cb32018-07-20 09:46:24 +01007226/**
7227 * Output information on how libweston is currently composing the scene
7228 * graph.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007229 *
7230 * \ingroup compositor
Daniel Stonece62cb32018-07-20 09:46:24 +01007231 */
7232WL_EXPORT char *
7233weston_compositor_print_scene_graph(struct weston_compositor *ec)
7234{
7235 struct weston_output *output;
7236 struct weston_layer *layer;
7237 struct timespec now;
7238 int layer_idx = 0;
7239 FILE *fp;
7240 char *ret;
7241 size_t len;
7242 int err;
7243
7244 fp = open_memstream(&ret, &len);
7245 assert(fp);
7246
7247 weston_compositor_read_presentation_clock(ec, &now);
7248 fprintf(fp, "Weston scene graph at %ld.%09ld:\n\n",
7249 now.tv_sec, now.tv_nsec);
7250
7251 wl_list_for_each(output, &ec->output_list, link) {
7252 struct weston_head *head;
7253 int head_idx = 0;
7254
7255 fprintf(fp, "Output %d (%s):\n", output->id, output->name);
7256 assert(output->enabled);
7257
7258 fprintf(fp, "\tposition: (%d, %d) -> (%d, %d)\n",
7259 output->x, output->y,
7260 output->x + output->width,
7261 output->y + output->height);
7262 fprintf(fp, "\tmode: %dx%d@%.3fHz\n",
7263 output->current_mode->width,
7264 output->current_mode->height,
7265 output->current_mode->refresh / 1000.0);
7266 fprintf(fp, "\tscale: %d\n", output->scale);
7267
7268 fprintf(fp, "\trepaint status: %s\n",
7269 output_repaint_status_text(output));
7270 if (output->repaint_status == REPAINT_SCHEDULED)
7271 fprintf(fp, "\tnext repaint: %ld.%09ld\n",
7272 output->next_repaint.tv_sec,
7273 output->next_repaint.tv_nsec);
7274
7275 wl_list_for_each(head, &output->head_list, output_link) {
7276 fprintf(fp, "\tHead %d (%s): %sconnected\n",
7277 head_idx++, head->name,
7278 (head->connected) ? "" : "not ");
7279 }
7280 }
7281
7282 fprintf(fp, "\n");
7283
7284 wl_list_for_each(layer, &ec->layer_list, link) {
7285 struct weston_view *view;
7286 int view_idx = 0;
7287
7288 fprintf(fp, "Layer %d (pos 0x%lx):\n", layer_idx++,
7289 (unsigned long) layer->position);
7290
7291 if (!weston_layer_mask_is_infinite(layer)) {
7292 fprintf(fp, "\t[mask: (%d, %d) -> (%d,%d)]\n\n",
7293 layer->mask.x1, layer->mask.y1,
7294 layer->mask.x2, layer->mask.y2);
7295 }
7296
Marius Vlada6acfa82019-03-17 18:10:09 +02007297 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
7298 debug_scene_view_print_tree(view, fp, &view_idx);
7299 view_idx++;
7300 }
Daniel Stonece62cb32018-07-20 09:46:24 +01007301
7302 if (wl_list_empty(&layer->view_list.link))
7303 fprintf(fp, "\t[no views]\n");
7304
7305 fprintf(fp, "\n");
7306 }
7307
7308 err = fclose(fp);
7309 assert(err == 0);
7310
7311 return ret;
7312}
7313
7314/**
7315 * Called when the 'scene-graph' debug scope is bound by a client. This
7316 * one-shot weston-debug scope prints the current scene graph when bound,
7317 * and then terminates the stream.
7318 */
7319static void
Marius Vladdad882a2019-07-17 15:43:53 +03007320debug_scene_graph_cb(struct weston_log_subscription *sub, void *data)
Daniel Stonece62cb32018-07-20 09:46:24 +01007321{
7322 struct weston_compositor *ec = data;
7323 char *str = weston_compositor_print_scene_graph(ec);
7324
Marius Vladdad882a2019-07-17 15:43:53 +03007325 weston_log_subscription_printf(sub, "%s", str);
Daniel Stonece62cb32018-07-20 09:46:24 +01007326 free(str);
Marius Vladdad882a2019-07-17 15:43:53 +03007327 weston_log_subscription_complete(sub);
Daniel Stonece62cb32018-07-20 09:46:24 +01007328}
7329
Leandro Ribeiro32a5acd2020-10-19 16:06:22 -03007330/** Init the compositor testsuite data
7331 *
7332 * The struct weston_testsuite_data contains two members:
7333 *
7334 * 1. The struct weston_testsuite_quirks, which can be used by the tests to
7335 * change certain behavior of Weston when running these tests.
7336 *
Leandro Ribeiro2d4dd702020-10-19 16:24:44 -03007337 * 2. The void *test_private_data member which can be used by the test suite
Leandro Ribeiro32a5acd2020-10-19 16:06:22 -03007338 * of projects that uses libweston in order to give arbitrary test data to the
Leandro Ribeiro2d4dd702020-10-19 16:24:44 -03007339 * compositor. Its type should be defined by the test suite of the project.
Leandro Ribeiro32a5acd2020-10-19 16:06:22 -03007340 *
7341 * This function can be called at most once per compositor instance, just after
7342 * creating the weston_compositor object and never again. This happens because
7343 * changing the quirks after e.g. loading the backend is not going to work,
7344 * there are certain behaviors that need to be set up before this point.
7345 *
7346 * \param ec The weston compositor.
7347 * \param test_data The testsuite data.
7348 *
7349 * \ingroup compositor
7350 * \sa weston_compositor_get_test_data
7351 */
7352WL_EXPORT void
7353weston_compositor_test_data_init(struct weston_compositor *ec,
7354 const struct weston_testsuite_data *test_data)
7355{
7356 assert(ec->backend == NULL);
7357 ec->test_data = *test_data;
7358}
7359
7360/** Retrieve testsuite data from compositor
7361 *
7362 * The testsuite data can be defined by the test suite of projects that uses
7363 * libweston and given to the compositor at the moment of its creation. This
7364 * function should be used when we need to retrieve the testsuite private data
7365 * from the compositor.
7366 *
7367 * \param ec The weston compositor.
7368 * \return The testsuite data.
7369 *
7370 * \ingroup compositor
7371 * \sa weston_compositor_test_data_init
7372 */
7373WL_EXPORT void *
7374weston_compositor_get_test_data(struct weston_compositor *ec)
7375{
7376 return ec->test_data.test_private_data;
7377}
7378
Giulio Camuffo459137b2014-10-11 23:56:24 +03007379/** Create the compositor.
7380 *
7381 * This functions creates and initializes a compositor instance.
7382 *
7383 * \param display The Wayland display to be used.
7384 * \param user_data A pointer to an object that can later be retrieved
Marius Vlada2dace22019-06-12 16:05:44 +03007385 * \param log_ctx A pointer to weston_debug_compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007386 * using the \ref weston_compositor_get_user_data function.
7387 * \return The compositor instance on success or NULL on failure.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007388 *
7389 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007390 */
7391WL_EXPORT struct weston_compositor *
Marius Vlad880b4852019-04-07 17:07:58 +03007392weston_compositor_create(struct wl_display *display,
Marius Vlad3d7d9782019-04-17 12:35:38 +03007393 struct weston_log_context *log_ctx,
Marius Vlad880b4852019-04-07 17:07:58 +03007394 void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04007395{
Giulio Camuffo459137b2014-10-11 23:56:24 +03007396 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05007397 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07007398
Leandro Ribeirobd9c0a62020-01-17 10:47:49 -03007399 if (!log_ctx)
7400 return NULL;
7401
Giulio Camuffo459137b2014-10-11 23:56:24 +03007402 ec = zalloc(sizeof *ec);
7403 if (!ec)
7404 return NULL;
7405
Leandro Ribeirobd9c0a62020-01-17 10:47:49 -03007406 ec->weston_log_ctx = log_ctx;
Giulio Camuffo459137b2014-10-11 23:56:24 +03007407 ec->wl_display = display;
7408 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04007409 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07007410 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04007411 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03007412 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03007413 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02007414 wl_signal_init(&ec->idle_signal);
7415 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02007416 wl_signal_init(&ec->show_input_panel_signal);
7417 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02007418 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01007419 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01007420 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02007421 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02007422 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02007423 wl_signal_init(&ec->output_resized_signal);
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03007424 wl_signal_init(&ec->heads_changed_signal);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02007425 wl_signal_init(&ec->output_heads_changed_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07007426 wl_signal_init(&ec->session_signal);
Robert Beckettc569bdc2019-07-10 16:40:12 +01007427 ec->session_active = true;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04007428
Casey Dahlin58ba1372012-04-19 22:50:08 -04007429 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03007430 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04007431
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02007432 ec->activate_serial = 1;
7433
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05007434 ec->touch_mode = WESTON_TOUCH_MODE_NORMAL;
7435
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05307436 ec->content_protection = NULL;
7437
Derek Foreman152254b2015-11-26 14:17:48 -06007438 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04007439 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007440 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05007441
Giulio Camuffo954f1832014-10-11 18:27:30 +03007442 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04007443 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007444 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03007445
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007446 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007447 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007448 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01007449
Roman Gilge97391c2019-03-29 13:01:06 +01007450 if (!wl_global_create(ec->wl_display, &zxdg_output_manager_v1_interface, 2,
7451 ec, bind_xdg_output_manager))
7452 goto fail;
7453
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007454 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007455 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007456 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007457
Jonas Ådahl30d61d82014-10-22 21:21:17 +02007458 if (weston_input_init(ec) != 0)
7459 goto fail;
7460
Jason Ekstranda7af7042013-10-12 22:38:11 -05007461 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02007462 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007463 wl_list_init(&ec->layer_list);
7464 wl_list_init(&ec->seat_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007465 wl_list_init(&ec->pending_output_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007466 wl_list_init(&ec->output_list);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03007467 wl_list_init(&ec->head_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007468 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01007469 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007470 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01007471 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007472 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02007473 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007474
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007475 wl_list_init(&ec->plugin_api_list);
7476
Xiong Zhang97116532013-10-23 13:58:31 +08007477 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02007478 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04007479
Giulio Camuffo459137b2014-10-11 23:56:24 +03007480 wl_data_device_manager_init(ec->wl_display);
7481
7482 wl_display_init_shm(ec->wl_display);
7483
7484 loop = wl_display_get_event_loop(ec->wl_display);
7485 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Daniel Stone6847b852017-03-01 11:34:08 +00007486 ec->repaint_timer =
7487 wl_event_loop_add_timer(loop, output_repaint_timer_handler,
7488 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007489
Quentin Glidic82681572016-12-17 13:40:51 +01007490 weston_layer_init(&ec->fade_layer, ec);
7491 weston_layer_init(&ec->cursor_layer, ec);
7492
7493 weston_layer_set_position(&ec->fade_layer, WESTON_LAYER_POSITION_FADE);
7494 weston_layer_set_position(&ec->cursor_layer,
7495 WESTON_LAYER_POSITION_CURSOR);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007496
Daniel Stonece62cb32018-07-20 09:46:24 +01007497 ec->debug_scene =
Leandro Ribeiroce100192019-12-26 16:35:49 -03007498 weston_compositor_add_log_scope(ec, "scene-graph",
7499 "Scene graph details\n",
7500 debug_scene_graph_cb, NULL,
7501 ec);
Daniel Stonece62cb32018-07-20 09:46:24 +01007502
Marius Vladda104eb2019-09-05 14:31:01 +03007503 ec->timeline =
Leandro Ribeiroce100192019-12-26 16:35:49 -03007504 weston_compositor_add_log_scope(ec, "timeline",
7505 "Timeline event points\n",
7506 weston_timeline_create_subscription,
7507 weston_timeline_destroy_subscription,
7508 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007509 return ec;
7510
7511fail:
7512 free(ec);
7513 return NULL;
7514}
7515
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007516/** weston_compositor_shutdown
7517 * \ingroup compositor
7518 */
Benjamin Franzkeb8263022011-08-30 11:32:47 +02007519WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007520weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07007521{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007522 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07007523
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007524 wl_event_source_remove(ec->idle_source);
7525
Matt Roper361d2ad2011-08-29 13:52:23 -07007526 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02007527 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07007528 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02007529
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007530 /* Destroy all pending outputs associated with this compositor */
7531 wl_list_for_each_safe(output, next, &ec->pending_output_list, link)
7532 output->destroy(output);
7533
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02007534 if (ec->renderer)
7535 ec->renderer->destroy(ec);
7536
Daniel Stone325fc2d2012-05-30 16:31:58 +01007537 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09007538 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01007539 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01007540 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01007541 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02007542 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007543
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04007544 weston_plane_release(&ec->primary_plane);
Matt Roper361d2ad2011-08-29 13:52:23 -07007545}
7546
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007547/** weston_compositor_exit_with_code
7548 * \ingroup compositor
7549 */
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05007550WL_EXPORT void
Adam Jackson3c3f3b12019-10-16 16:02:59 -04007551weston_compositor_exit_with_code(struct weston_compositor *compositor,
Frederic Plourdec336f062014-10-29 14:44:33 -04007552 int exit_code)
7553{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02007554 if (compositor->exit_code == EXIT_SUCCESS)
7555 compositor->exit_code = exit_code;
7556
Giulio Camuffo459137b2014-10-11 23:56:24 +03007557 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04007558}
7559
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007560/** weston_compositor_set_default_pointer_grab
7561 * \ingroup compositor
7562 */
Frederic Plourdec336f062014-10-29 14:44:33 -04007563WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007564weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
7565 const struct weston_pointer_grab_interface *interface)
7566{
7567 struct weston_seat *seat;
7568
7569 ec->default_pointer_grab = interface;
7570 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05007571 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
7572
7573 if (pointer)
7574 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007575 }
7576}
7577
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007578/** weston_compositor_set_presentation_clock
7579 * \ingroup compositor
7580 */
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007581WL_EXPORT int
7582weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
7583 clockid_t clk_id)
7584{
7585 struct timespec ts;
7586
7587 if (clock_gettime(clk_id, &ts) < 0)
7588 return -1;
7589
7590 compositor->presentation_clock = clk_id;
7591
7592 return 0;
7593}
7594
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007595/** For choosing the software clock, when the display hardware or API
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007596 * does not expose a compatible presentation timestamp.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007597 *
7598 * \ingroup compositor
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007599 */
7600WL_EXPORT int
7601weston_compositor_set_presentation_clock_software(
7602 struct weston_compositor *compositor)
7603{
7604 /* In order of preference */
7605 static const clockid_t clocks[] = {
7606 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
7607 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
7608 CLOCK_MONOTONIC, /* no jumps, may crawl */
7609 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
7610 CLOCK_REALTIME /* may jump and crawl */
7611 };
7612 unsigned i;
7613
7614 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
7615 if (weston_compositor_set_presentation_clock(compositor,
7616 clocks[i]) == 0)
7617 return 0;
7618
7619 weston_log("Error: no suitable presentation clock available.\n");
7620
7621 return -1;
7622}
7623
Pekka Paalanen662f3842015-03-18 12:17:26 +02007624/** Read the current time from the Presentation clock
7625 *
7626 * \param compositor
Marius Vlada2dace22019-06-12 16:05:44 +03007627 * \param[out] ts The current time.
Pekka Paalanen662f3842015-03-18 12:17:26 +02007628 *
7629 * \note Reading the current time in user space is always imprecise to some
7630 * degree.
7631 *
7632 * This function is never meant to fail. If reading the clock does fail,
7633 * an error message is logged and a zero time is returned. Callers are not
7634 * supposed to detect or react to failures.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007635 *
7636 * \ingroup compositor
Pekka Paalanen662f3842015-03-18 12:17:26 +02007637 */
7638WL_EXPORT void
7639weston_compositor_read_presentation_clock(
7640 const struct weston_compositor *compositor,
7641 struct timespec *ts)
7642{
7643 static bool warned;
7644 int ret;
7645
7646 ret = clock_gettime(compositor->presentation_clock, ts);
7647 if (ret < 0) {
7648 ts->tv_sec = 0;
7649 ts->tv_nsec = 0;
7650
7651 if (!warned)
7652 weston_log("Error: failure to read "
Antonio Borneo39578632019-04-26 23:57:31 +02007653 "the presentation clock %#x: '%s' (%d)\n",
7654 compositor->presentation_clock,
7655 strerror(errno), errno);
Pekka Paalanen662f3842015-03-18 12:17:26 +02007656 warned = true;
7657 }
7658}
7659
Pekka Paalanen230f3b12014-09-29 14:18:40 -04007660/** Import dmabuf buffer into current renderer
7661 *
7662 * \param compositor
7663 * \param buffer the dmabuf buffer to import
7664 * \return true on usable buffers, false otherwise
7665 *
7666 * This function tests that the linux_dmabuf_buffer is usable
7667 * for the current renderer. Returns false on unusable buffers. Usually
7668 * usability is tested by importing the dmabufs for composition.
7669 *
7670 * This hook is also used for detecting if the renderer supports
7671 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
7672 * supported.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007673 *
7674 * \ingroup compositor
7675 */
Pekka Paalanen230f3b12014-09-29 14:18:40 -04007676WL_EXPORT bool
7677weston_compositor_import_dmabuf(struct weston_compositor *compositor,
7678 struct linux_dmabuf_buffer *buffer)
7679{
7680 struct weston_renderer *renderer;
7681
7682 renderer = compositor->renderer;
7683
7684 if (renderer->import_dmabuf == NULL)
7685 return false;
7686
7687 return renderer->import_dmabuf(compositor, buffer);
7688}
7689
Marius Vlad5a701542019-11-16 20:26:52 +02007690WL_EXPORT bool
7691weston_compositor_dmabuf_can_scanout(struct weston_compositor *compositor,
7692 struct linux_dmabuf_buffer *buffer)
7693{
7694 struct weston_backend *backend = compositor->backend;
7695
7696 if (backend->can_scanout_dmabuf == NULL)
7697 return false;
7698
7699 return backend->can_scanout_dmabuf(compositor, buffer);
7700}
7701
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007702WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05007703weston_version(int *major, int *minor, int *micro)
7704{
7705 *major = WESTON_VERSION_MAJOR;
7706 *minor = WESTON_VERSION_MINOR;
7707 *micro = WESTON_VERSION_MICRO;
7708}
7709
Daniel Stonee03c1112016-11-24 20:45:45 +00007710/**
7711 * Attempts to find a module path from the module map specified in the
7712 * environment. If found, writes the full path into the path variable.
7713 *
7714 * The module map is a string in environment variable WESTON_MODULE_MAP, where
7715 * each entry is of the form "name=path" and entries are separated by
7716 * semicolons. Whitespace is significant.
7717 *
7718 * \param name The name to search for.
7719 * \param path Where the path is written to if found.
7720 * \param path_len Allocated bytes at \c path .
7721 * \returns The length of the string written to path on success, or 0 if the
7722 * module was not specified in the environment map or path_len was too small.
7723 */
7724WL_EXPORT size_t
7725weston_module_path_from_env(const char *name, char *path, size_t path_len)
7726{
7727 const char *mapping = getenv("WESTON_MODULE_MAP");
7728 const char *end;
7729 const int name_len = strlen(name);
7730
7731 if (!mapping)
7732 return 0;
7733
7734 end = mapping + strlen(mapping);
7735 while (mapping < end && *mapping) {
7736 const char *filename, *next;
7737
7738 /* early out: impossibly short string */
7739 if (end - mapping < name_len + 1)
7740 return 0;
7741
7742 filename = &mapping[name_len + 1];
7743 next = strchrnul(mapping, ';');
7744
7745 if (strncmp(mapping, name, name_len) == 0 &&
7746 mapping[name_len] == '=') {
7747 size_t file_len = next - filename; /* no trailing NUL */
7748 if (file_len >= path_len)
7749 return 0;
7750 strncpy(path, filename, file_len);
7751 path[file_len] = '\0';
7752 return file_len;
7753 }
7754
7755 mapping = next + 1;
7756 }
7757
7758 return 0;
7759}
7760
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03007761WL_EXPORT void *
7762weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007763{
7764 char path[PATH_MAX];
7765 void *module, *init;
Daniel Stonebeb97e52016-11-28 12:13:54 +00007766 size_t len;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007767
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08007768 if (name == NULL)
7769 return NULL;
7770
Derek Foreman3f86e502015-06-08 11:46:54 -05007771 if (name[0] != '/') {
Daniel Stonee03c1112016-11-24 20:45:45 +00007772 len = weston_module_path_from_env(name, path, sizeof path);
7773 if (len == 0)
Daniel Stonebeb97e52016-11-28 12:13:54 +00007774 len = snprintf(path, sizeof path, "%s/%s",
7775 LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05007776 } else {
Daniel Stonebeb97e52016-11-28 12:13:54 +00007777 len = snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05007778 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007779
Daniel Stonebeb97e52016-11-28 12:13:54 +00007780 /* snprintf returns the length of the string it would've written,
7781 * _excluding_ the NUL byte. So even being equal to the size of
7782 * our buffer is an error here. */
7783 if (len >= sizeof path)
7784 return NULL;
7785
Kristian Høgsberga6813d22012-09-12 12:21:01 -04007786 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
7787 if (module) {
7788 weston_log("Module '%s' already loaded\n", path);
Pekka Paalanenf696ee92019-11-04 14:07:51 +02007789 } else {
7790 weston_log("Loading module '%s'\n", path);
7791 module = dlopen(path, RTLD_NOW);
7792 if (!module) {
7793 weston_log("Failed to load module: %s\n", dlerror());
7794 return NULL;
7795 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007796 }
7797
7798 init = dlsym(module, entrypoint);
7799 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03007800 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00007801 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007802 return NULL;
7803 }
7804
7805 return init;
7806}
7807
Pekka Paalanen6ffbba32019-11-06 12:59:32 +02007808/** Add a compositor destroy listener only once
7809 *
7810 * \param compositor The compositor whose destroy to watch for.
7811 * \param listener The listener struct to initialize.
7812 * \param destroy_handler The callback when compositor is destroyed.
7813 * \return True if listener is added, or false if there already is a listener
7814 * with the given \c destroy_handler.
7815 *
7816 * This function does nothing and returns false if the given callback function
7817 * is already present in the weston_compositor destroy callbacks list.
7818 * Otherwise, this function initializes the given listener with the given
7819 * callback pointer and adds it to the compositor's destroy callbacks list.
7820 *
7821 * This can be used to ensure that plugin initialization is done only once
7822 * in case the same plugin is loaded multiple times. If this function returns
7823 * false, the plugin should be already initialized successfully.
7824 *
7825 * All plugins should register a destroy listener for cleaning up. Note, that
7826 * the plugin destruction order is not guaranteed: plugins that depend on other
7827 * plugins must be able to be torn down in arbitrary order.
7828 *
Leandro Ribeiroca640d52020-01-27 19:12:01 -03007829 * \sa weston_compositor_destroy
Pekka Paalanen6ffbba32019-11-06 12:59:32 +02007830 */
7831WL_EXPORT bool
7832weston_compositor_add_destroy_listener_once(struct weston_compositor *compositor,
7833 struct wl_listener *listener,
7834 wl_notify_func_t destroy_handler)
7835{
7836 if (wl_signal_get(&compositor->destroy_signal, destroy_handler))
7837 return false;
7838
7839 listener->notify = destroy_handler;
7840 wl_signal_add(&compositor->destroy_signal, listener);
7841 return true;
7842}
Giulio Camuffo459137b2014-10-11 23:56:24 +03007843
Leandro Ribeiroca640d52020-01-27 19:12:01 -03007844/** Destroys the compositor.
Giulio Camuffo459137b2014-10-11 23:56:24 +03007845 *
Leandro Ribeiroca640d52020-01-27 19:12:01 -03007846 * This function cleans up the compositor state and then destroys it.
Giulio Camuffo459137b2014-10-11 23:56:24 +03007847 *
Leandro Ribeiroca640d52020-01-27 19:12:01 -03007848 * @param compositor The compositor to be destroyed.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007849 *
Marius Vlad284d5342019-06-24 12:00:47 +03007850 * @ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007851 */
7852WL_EXPORT void
Leandro Ribeiroca640d52020-01-27 19:12:01 -03007853weston_compositor_destroy(struct weston_compositor *compositor)
Giulio Camuffo459137b2014-10-11 23:56:24 +03007854{
7855 /* prevent further rendering while shutting down */
7856 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
7857
7858 wl_signal_emit(&compositor->destroy_signal, compositor);
7859
7860 weston_compositor_xkb_destroy(compositor);
7861
Giulio Camuffo2d24e642015-10-03 16:25:15 +03007862 if (compositor->backend)
7863 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007864
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03007865 /* The backend is responsible for destroying the heads. */
7866 assert(wl_list_empty(&compositor->head_list));
7867
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007868 weston_plugin_api_destroy_list(compositor);
7869
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03007870 if (compositor->heads_changed_source)
7871 wl_event_source_remove(compositor->heads_changed_source);
7872
Leandro Ribeirof0149642019-12-18 15:52:18 -03007873 weston_log_scope_destroy(compositor->debug_scene);
Daniel Stonece62cb32018-07-20 09:46:24 +01007874 compositor->debug_scene = NULL;
Marius Vladda104eb2019-09-05 14:31:01 +03007875
Leandro Ribeirof0149642019-12-18 15:52:18 -03007876 weston_log_scope_destroy(compositor->timeline);
Marius Vladda104eb2019-09-05 14:31:01 +03007877 compositor->timeline = NULL;
Pekka Paalanena5630ea2017-10-12 13:13:42 +02007878
Giulio Camuffo459137b2014-10-11 23:56:24 +03007879 free(compositor);
7880}
7881
7882/** Instruct the compositor to exit.
7883 *
7884 * This functions does not directly destroy the compositor object, it merely
7885 * command it to start the tear down process. It is not guaranteed that the
7886 * tear down will happen immediately.
7887 *
7888 * \param compositor The compositor to tear down.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007889 *
7890 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007891 */
7892WL_EXPORT void
7893weston_compositor_exit(struct weston_compositor *compositor)
7894{
7895 compositor->exit(compositor);
7896}
7897
7898/** Return the user data stored in the compositor.
7899 *
7900 * This function returns the user data pointer set with user_data parameter
7901 * to the \ref weston_compositor_create function.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007902 *
7903 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007904 */
7905WL_EXPORT void *
7906weston_compositor_get_user_data(struct weston_compositor *compositor)
7907{
7908 return compositor->user_data;
7909}
Pekka Paalanendd186732016-06-03 14:49:54 +03007910
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007911static const char * const backend_map[] = {
7912 [WESTON_BACKEND_DRM] = "drm-backend.so",
7913 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
7914 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
7915 [WESTON_BACKEND_RDP] = "rdp-backend.so",
7916 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
7917 [WESTON_BACKEND_X11] = "x11-backend.so",
7918};
7919
Pekka Paalanendd186732016-06-03 14:49:54 +03007920/** Load a backend into a weston_compositor
7921 *
7922 * A backend must be loaded to make a weston_compositor work. A backend
7923 * provides input and output capabilities, and determines the renderer to use.
7924 *
7925 * \param compositor A compositor that has not had a backend loaded yet.
7926 * \param backend Name of the backend file.
7927 * \param config_base A pointer to a backend-specific configuration
7928 * structure's 'base' member.
7929 *
7930 * \return 0 on success, or -1 on error.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007931 *
7932 * \ingroup compositor
Pekka Paalanendd186732016-06-03 14:49:54 +03007933 */
7934WL_EXPORT int
7935weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007936 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03007937 struct weston_backend_config *config_base)
7938{
7939 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03007940 struct weston_backend_config *config_base);
7941
Pekka Paalanend7e35112017-08-29 17:04:12 +03007942 if (compositor->backend) {
7943 weston_log("Error: attempt to load a backend when one is already loaded\n");
7944 return -1;
7945 }
7946
Quentin Glidic887c0182016-07-10 11:00:53 +02007947 if (backend >= ARRAY_LENGTH(backend_map))
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007948 return -1;
7949
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01007950 backend_init = weston_load_module(backend_map[backend], "weston_backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03007951 if (!backend_init)
7952 return -1;
7953
Pekka Paalanend7e35112017-08-29 17:04:12 +03007954 if (backend_init(compositor, config_base) < 0) {
7955 compositor->backend = NULL;
7956 return -1;
7957 }
7958
7959 return 0;
Pekka Paalanendd186732016-06-03 14:49:54 +03007960}
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007961
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007962/** weston_compositor_load_xwayland
7963 * \ingroup compositor
7964 */
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007965WL_EXPORT int
7966weston_compositor_load_xwayland(struct weston_compositor *compositor)
7967{
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007968 int (*module_init)(struct weston_compositor *ec);
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007969
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007970 module_init = weston_load_module("xwayland.so", "weston_module_init");
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007971 if (!module_init)
7972 return -1;
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007973 if (module_init(compositor) < 0)
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007974 return -1;
7975 return 0;
7976}
Vasilis Liaskovitis486b4632018-10-10 16:14:55 +02007977
7978/** Resolve an internal compositor error by disconnecting the client.
7979 *
7980 * This function is used in cases when the wl_buffer turns out
7981 * unusable and there is no fallback path.
7982 *
7983 * It is possible the fault is caused by a compositor bug, the underlying
7984 * graphics stack bug or normal behaviour, or perhaps a client mistake.
7985 * In any case, the options are to either composite garbage or nothing,
7986 * or disconnect the client. This is a helper function for the latter.
7987 *
7988 * The error is sent as an INVALID_OBJECT error on the client's wl_display.
7989 *
7990 * \param buffer The weston buffer that is unusable.
7991 * \param msg A custom error message attached to the protocol error.
7992 */
7993WL_EXPORT void
7994weston_buffer_send_server_error(struct weston_buffer *buffer,
7995 const char *msg)
7996{
7997 struct wl_client *client;
7998 struct wl_resource *display_resource;
7999 uint32_t id;
8000
8001 assert(buffer->resource);
8002 id = wl_resource_get_id(buffer->resource);
8003 client = wl_resource_get_client(buffer->resource);
8004 display_resource = wl_client_get_object(client, 1);
8005
8006 assert(display_resource);
8007 wl_resource_post_error(display_resource,
8008 WL_DISPLAY_ERROR_INVALID_OBJECT,
8009 "server error with "
8010 "wl_buffer@%u: %s", id, msg);
8011}
Ankit Nautiyal93dde242019-07-08 11:46:42 +05308012
8013WL_EXPORT void
8014weston_output_disable_planes_incr(struct weston_output *output)
8015{
8016 output->disable_planes++;
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05308017 /*
8018 * If disable_planes changes from 0 to non-zero, it means some type of
8019 * recording of content has started, and therefore protection level of
8020 * the protected surfaces must be updated to avoid the recording of
8021 * the protected content.
8022 */
8023 if (output->disable_planes == 1)
8024 weston_schedule_surface_protection_update(output->compositor);
Ankit Nautiyal93dde242019-07-08 11:46:42 +05308025}
8026
8027WL_EXPORT void
8028weston_output_disable_planes_decr(struct weston_output *output)
8029{
8030 output->disable_planes--;
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05308031 /*
8032 * If disable_planes changes from non-zero to 0, it means no content
8033 * recording is going on any more, and the protected and surfaces can be
8034 * shown without any apprehensions about content being recorded.
8035 */
8036 if (output->disable_planes == 0)
8037 weston_schedule_surface_protection_update(output->compositor);
8038
Ankit Nautiyal93dde242019-07-08 11:46:42 +05308039}