blob: 283f3f68c5c419925e30d5113aaee2b7e5d54612 [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
Jason Ekstranda7af7042013-10-12 22:38:11 -05001419weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001420{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001421 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001422
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001423 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001424 view->geometry.x = roundf(view->geometry.x);
1425 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001426
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001427 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001428 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1429 view->transform.position.matrix.d[12] = view->geometry.x;
1430 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001431
Jason Ekstranda7af7042013-10-12 22:38:11 -05001432 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001433
Jason Ekstranda7af7042013-10-12 22:38:11 -05001434 view->transform.inverse = view->transform.position.matrix;
1435 view->transform.inverse.d[12] = -view->geometry.x;
1436 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001437
Jason Ekstranda7af7042013-10-12 22:38:11 -05001438 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001439 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001440 view->surface->width,
1441 view->surface->height);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001442 if (view->geometry.scissor_enabled)
1443 pixman_region32_intersect(&view->transform.boundingbox,
1444 &view->transform.boundingbox,
1445 &view->geometry.scissor);
1446
1447 pixman_region32_translate(&view->transform.boundingbox,
1448 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001449
Jason Ekstranda7af7042013-10-12 22:38:11 -05001450 if (view->alpha == 1.0) {
1451 pixman_region32_copy(&view->transform.opaque,
1452 &view->surface->opaque);
1453 pixman_region32_translate(&view->transform.opaque,
1454 view->geometry.x,
1455 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001456 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001457}
1458
1459static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001460weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001461{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001462 struct weston_view *parent = view->geometry.parent;
1463 struct weston_matrix *matrix = &view->transform.matrix;
1464 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001465 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001466 pixman_region32_t surfregion;
1467 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001468
Jason Ekstranda7af7042013-10-12 22:38:11 -05001469 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001470
1471 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001472 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1473 view->transform.position.matrix.d[12] = view->geometry.x;
1474 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001475
1476 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001477 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001478 weston_matrix_multiply(matrix, &tform->matrix);
1479
Pekka Paalanen483243f2013-03-08 14:56:50 +02001480 if (parent)
1481 weston_matrix_multiply(matrix, &parent->transform.matrix);
1482
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001483 if (weston_matrix_invert(inverse, matrix) < 0) {
1484 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001485 weston_log("error: weston_view %p"
1486 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001487 return -1;
1488 }
1489
Giulio Camuffo148c1992016-09-04 18:50:46 +03001490 if (view->alpha == 1.0 &&
1491 matrix->type == WESTON_MATRIX_TRANSFORM_TRANSLATE) {
1492 pixman_region32_copy(&view->transform.opaque,
1493 &view->surface->opaque);
1494 pixman_region32_translate(&view->transform.opaque,
1495 matrix->d[12],
1496 matrix->d[13]);
1497 }
1498
Pekka Paalanen380adf52015-02-16 14:39:11 +02001499 pixman_region32_init_rect(&surfregion, 0, 0,
1500 view->surface->width, view->surface->height);
1501 if (view->geometry.scissor_enabled)
1502 pixman_region32_intersect(&surfregion, &surfregion,
1503 &view->geometry.scissor);
1504 surfbox = pixman_region32_extents(&surfregion);
1505
1506 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1507 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001508
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001509 return 0;
1510}
1511
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001512static struct weston_layer *
1513get_view_layer(struct weston_view *view)
1514{
1515 if (view->parent_view)
1516 return get_view_layer(view->parent_view);
1517 return view->layer_link.layer;
1518}
1519
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001520WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001521weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001522{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001523 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001524 struct weston_layer *layer;
1525 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001526
Jason Ekstranda7af7042013-10-12 22:38:11 -05001527 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001528 return;
1529
Pekka Paalanen483243f2013-03-08 14:56:50 +02001530 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001531 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001532
Jason Ekstranda7af7042013-10-12 22:38:11 -05001533 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001534
Jason Ekstranda7af7042013-10-12 22:38:11 -05001535 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001536
Jason Ekstranda7af7042013-10-12 22:38:11 -05001537 pixman_region32_fini(&view->transform.boundingbox);
1538 pixman_region32_fini(&view->transform.opaque);
1539 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001540
Pekka Paalanencd403622012-01-25 13:37:39 +02001541 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001542 if (view->geometry.transformation_list.next ==
1543 &view->transform.position.link &&
1544 view->geometry.transformation_list.prev ==
1545 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001546 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001547 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001548 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001549 if (weston_view_update_transform_enable(view) < 0)
1550 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001551 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001552
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001553 layer = get_view_layer(view);
1554 if (layer) {
1555 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001556 pixman_region32_intersect(&view->transform.boundingbox,
1557 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001558 pixman_region32_intersect(&view->transform.opaque,
1559 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001560 pixman_region32_fini(&mask);
1561 }
1562
Pekka Paalanen380adf52015-02-16 14:39:11 +02001563 if (parent) {
1564 if (parent->geometry.scissor_enabled) {
1565 view->geometry.scissor_enabled = true;
1566 weston_view_transfer_scissor(parent, view);
1567 } else {
1568 view->geometry.scissor_enabled = false;
1569 }
1570 }
1571
Jason Ekstranda7af7042013-10-12 22:38:11 -05001572 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001573
Jason Ekstranda7af7042013-10-12 22:38:11 -05001574 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001575
Jason Ekstranda7af7042013-10-12 22:38:11 -05001576 wl_signal_emit(&view->surface->compositor->transform_signal,
1577 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001578}
1579
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001580WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001581weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001582{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001583 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001584
1585 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001586 * The invariant: if view->geometry.dirty, then all views
1587 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001588 * Corollary: if not parent->geometry.dirty, then all ancestors
1589 * are not dirty.
1590 */
1591
Jason Ekstranda7af7042013-10-12 22:38:11 -05001592 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001593 return;
1594
Jason Ekstranda7af7042013-10-12 22:38:11 -05001595 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001596
Jason Ekstranda7af7042013-10-12 22:38:11 -05001597 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001598 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001599 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001600}
1601
1602WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001603weston_view_to_global_fixed(struct weston_view *view,
1604 wl_fixed_t vx, wl_fixed_t vy,
1605 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001606{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001607 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001608
Jason Ekstranda7af7042013-10-12 22:38:11 -05001609 weston_view_to_global_float(view,
1610 wl_fixed_to_double(vx),
1611 wl_fixed_to_double(vy),
1612 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001613 *x = wl_fixed_from_double(xf);
1614 *y = wl_fixed_from_double(yf);
1615}
1616
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001617WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001618weston_view_from_global_float(struct weston_view *view,
1619 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001620{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001621 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001622 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1623
Jason Ekstranda7af7042013-10-12 22:38:11 -05001624 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001625
1626 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001627 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001628 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001629 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001630 *vx = 0;
1631 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001632 return;
1633 }
1634
Jason Ekstranda7af7042013-10-12 22:38:11 -05001635 *vx = v.f[0] / v.f[3];
1636 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001637 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001638 *vx = x - view->geometry.x;
1639 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001640 }
1641}
1642
1643WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001644weston_view_from_global_fixed(struct weston_view *view,
1645 wl_fixed_t x, wl_fixed_t y,
1646 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001647{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001648 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001649
Jason Ekstranda7af7042013-10-12 22:38:11 -05001650 weston_view_from_global_float(view,
1651 wl_fixed_to_double(x),
1652 wl_fixed_to_double(y),
1653 &vxf, &vyf);
1654 *vx = wl_fixed_from_double(vxf);
1655 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001656}
1657
1658WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001659weston_view_from_global(struct weston_view *view,
1660 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001661{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001662 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001663
Jason Ekstranda7af7042013-10-12 22:38:11 -05001664 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1665 *vx = floorf(vxf);
1666 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001667}
1668
Bryce Harrington3f650b82015-12-23 11:01:58 -08001669/**
1670 * \param surface The surface to be repainted
1671 *
1672 * Marks the output(s) that the surface is shown on as needing to be
1673 * repainted. See weston_output_schedule_repaint().
1674 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001675WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001676weston_surface_schedule_repaint(struct weston_surface *surface)
1677{
1678 struct weston_output *output;
1679
1680 wl_list_for_each(output, &surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001681 if (surface->output_mask & (1u << output->id))
Kristian Høgsberg98238702012-08-03 16:29:12 -04001682 weston_output_schedule_repaint(output);
1683}
1684
Bryce Harrington3f650b82015-12-23 11:01:58 -08001685/**
1686 * \param view The view to be repainted
1687 *
1688 * Marks the output(s) that the view is shown on as needing to be
1689 * repainted. See weston_output_schedule_repaint().
1690 */
Kristian Høgsberg98238702012-08-03 16:29:12 -04001691WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001692weston_view_schedule_repaint(struct weston_view *view)
1693{
1694 struct weston_output *output;
1695
1696 wl_list_for_each(output, &view->surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001697 if (view->output_mask & (1u << output->id))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001698 weston_output_schedule_repaint(output);
1699}
1700
Pekka Paalanene508ce62015-02-19 13:59:55 +02001701/**
1702 * XXX: This function does it the wrong way.
1703 * surface->damage is the damage from the client, and causes
1704 * surface_flush_damage() to copy pixels. No window management action can
1705 * cause damage to the client-provided content, warranting re-upload!
1706 *
1707 * Instead of surface->damage, this function should record the damage
1708 * with all the views for this surface to avoid extraneous texture
1709 * uploads.
1710 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001711WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001712weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001713{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001714 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001715 0, 0, surface->width,
1716 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001717
Kristian Høgsberg98238702012-08-03 16:29:12 -04001718 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001719}
1720
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001721WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001722weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001723{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001724 if (view->geometry.x == x && view->geometry.y == y)
1725 return;
1726
Jason Ekstranda7af7042013-10-12 22:38:11 -05001727 view->geometry.x = x;
1728 view->geometry.y = y;
1729 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001730}
1731
Pekka Paalanen483243f2013-03-08 14:56:50 +02001732static void
1733transform_parent_handle_parent_destroy(struct wl_listener *listener,
1734 void *data)
1735{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001736 struct weston_view *view =
1737 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001738 geometry.parent_destroy_listener);
1739
Jason Ekstranda7af7042013-10-12 22:38:11 -05001740 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001741}
1742
1743WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001744weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001745 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001746{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001747 if (view->geometry.parent) {
1748 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1749 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001750
1751 if (!parent)
1752 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001753 }
1754
Jason Ekstranda7af7042013-10-12 22:38:11 -05001755 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001756
Jason Ekstranda7af7042013-10-12 22:38:11 -05001757 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001758 transform_parent_handle_parent_destroy;
1759 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001760 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001761 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001762 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001763 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001764 }
1765
Jason Ekstranda7af7042013-10-12 22:38:11 -05001766 weston_view_geometry_dirty(view);
1767}
1768
Pekka Paalanen380adf52015-02-16 14:39:11 +02001769/** Set a clip mask rectangle on a view
1770 *
1771 * \param view The view to set the clip mask on.
1772 * \param x Top-left corner X coordinate of the clip rectangle.
1773 * \param y Top-left corner Y coordinate of the clip rectangle.
1774 * \param width Width of the clip rectangle, non-negative.
1775 * \param height Height of the clip rectangle, non-negative.
1776 *
1777 * A shell may set a clip mask rectangle on a view. Everything outside
1778 * the rectangle is cut away for input and output purposes: it is
1779 * not drawn and cannot be hit by hit-test based input like pointer
1780 * motion or touch-downs. Everything inside the rectangle will behave
1781 * normally. Clients are unaware of clipping.
1782 *
Yong Bakos4c72e292016-04-28 11:59:10 -05001783 * The rectangle is set in surface-local coordinates. Setting a clip
Pekka Paalanen380adf52015-02-16 14:39:11 +02001784 * mask rectangle does not affect the view position, the view is positioned
1785 * as it would be without a clip. The clip also does not change
1786 * weston_surface::width,height.
1787 *
1788 * The clip mask rectangle is part of transformation inheritance
1789 * (weston_view_set_transform_parent()). A clip set in the root of the
1790 * transformation inheritance tree will affect all views in the tree.
1791 * A clip can be set only on the root view. Attempting to set a clip
1792 * on view that has a transformation parent will fail. Assigning a parent
1793 * to a view that has a clip set will cause the clip to be forgotten.
1794 *
1795 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1796 * on the additional transformations in the child views. These transformations
1797 * may not rotate the coordinate axes, i.e., only translation and scaling
1798 * are allowed. Violating this restriction causes the clipping to malfunction.
1799 * Furthermore, using scaling may cause rounding errors in child clipping.
1800 *
1801 * The clip mask rectangle is not automatically adjusted based on
1802 * wl_surface.attach dx and dy arguments.
1803 *
1804 * A clip mask rectangle can be set only if the compositor capability
1805 * WESTON_CAP_VIEW_CLIP_MASK is present.
1806 *
1807 * This function sets the clip mask rectangle and schedules a repaint for
1808 * the view.
1809 */
1810WL_EXPORT void
1811weston_view_set_mask(struct weston_view *view,
1812 int x, int y, int width, int height)
1813{
1814 struct weston_compositor *compositor = view->surface->compositor;
1815
1816 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1817 weston_log("%s not allowed without capability!\n", __func__);
1818 return;
1819 }
1820
1821 if (view->geometry.parent) {
1822 weston_log("view %p has a parent, clip forbidden!\n", view);
1823 return;
1824 }
1825
1826 if (width < 0 || height < 0) {
1827 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1828 x, y, width, height);
1829 return;
1830 }
1831
1832 pixman_region32_fini(&view->geometry.scissor);
1833 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1834 view->geometry.scissor_enabled = true;
1835 weston_view_geometry_dirty(view);
1836 weston_view_schedule_repaint(view);
1837}
1838
1839/** Remove the clip mask from a view
1840 *
1841 * \param view The view to remove the clip mask from.
1842 *
1843 * Removed the clip mask rectangle and schedules a repaint.
1844 *
1845 * \sa weston_view_set_mask
1846 */
1847WL_EXPORT void
1848weston_view_set_mask_infinite(struct weston_view *view)
1849{
1850 view->geometry.scissor_enabled = false;
1851 weston_view_geometry_dirty(view);
1852 weston_view_schedule_repaint(view);
1853}
1854
Armin Krezović0da12b82016-06-30 06:04:33 +02001855/* Check if view should be displayed
1856 *
1857 * The indicator is set manually when assigning
1858 * a view to a surface.
1859 *
1860 * This needs reworking. See the thread starting at:
1861 *
1862 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1863 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001864WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001865weston_view_is_mapped(struct weston_view *view)
1866{
Armin Krezović0da12b82016-06-30 06:04:33 +02001867 return view->is_mapped;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001868}
1869
Philipp Zabel70decd52018-09-03 20:11:15 +02001870/* Check if view is opaque in specified region
1871 *
1872 * \param view The view to check for opacity.
1873 * \param region The region to check for opacity, in view coordinates.
1874 *
1875 * Returns true if the view is opaque in the specified region, because view
1876 * alpha is 1.0 and either the opaque region set by the client contains the
1877 * specified region, or the buffer pixel format or solid color is opaque.
1878 */
1879WL_EXPORT bool
1880weston_view_is_opaque(struct weston_view *ev, pixman_region32_t *region)
1881{
1882 pixman_region32_t r;
1883 bool ret = false;
1884
1885 if (ev->alpha < 1.0)
1886 return false;
1887
1888 if (ev->surface->is_opaque)
1889 return true;
1890
1891 if (ev->transform.dirty) {
1892 weston_log("%s: transform dirty", __func__);
1893 return false;
1894 }
1895
1896 pixman_region32_init(&r);
1897 pixman_region32_subtract(&r, region, &ev->transform.opaque);
1898
1899 if (!pixman_region32_not_empty(&r))
1900 ret = true;
1901
1902 pixman_region32_fini(&r);
1903
1904 return ret;
1905}
1906
Marius Vlad5f6bee42019-09-11 16:41:04 +03001907/** Check if the view has a valid buffer available
1908 *
1909 * @param ev The view to check if it has a valid buffer.
1910 *
1911 * Returns true if the view has a valid buffer or false otherwise.
1912 */
1913WL_EXPORT bool
1914weston_view_has_valid_buffer(struct weston_view *ev)
1915{
1916 return ev->surface->buffer_ref.buffer != NULL;
1917}
1918
Marius Vlad47e3d1e2019-09-11 16:53:08 +03001919/** Check if the view matches the entire output
1920 *
1921 * @param ev The view to check.
1922 * @param output The output to check against.
1923 *
1924 * Returns true if the view does indeed matches the entire output.
1925 */
1926WL_EXPORT bool
1927weston_view_matches_output_entirely(struct weston_view *ev,
1928 struct weston_output *output)
1929{
1930 pixman_box32_t *extents =
1931 pixman_region32_extents(&ev->transform.boundingbox);
1932
1933 if (extents->x1 != output->x ||
1934 extents->y1 != output->y ||
1935 extents->x2 != output->x + output->width ||
1936 extents->y2 != output->y + output->height)
1937 return false;
1938
1939 return true;
1940}
1941
Armin Krezović0da12b82016-06-30 06:04:33 +02001942/* Check if a surface has a view assigned to it
1943 *
1944 * The indicator is set manually when mapping
1945 * a surface and creating a view for it.
1946 *
1947 * This needs to go. See the thread starting at:
1948 *
1949 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1950 *
1951 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001952WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001953weston_surface_is_mapped(struct weston_surface *surface)
1954{
Armin Krezović0da12b82016-06-30 06:04:33 +02001955 return surface->is_mapped;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001956}
1957
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001958static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001959surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001960{
1961 struct weston_view *view;
1962
1963 if (surface->width == width && surface->height == height)
1964 return;
1965
1966 surface->width = width;
1967 surface->height = height;
1968
1969 wl_list_for_each(view, &surface->views, surface_link)
1970 weston_view_geometry_dirty(view);
1971}
1972
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001973WL_EXPORT void
1974weston_surface_set_size(struct weston_surface *surface,
1975 int32_t width, int32_t height)
1976{
1977 assert(!surface->resource);
1978 surface_set_size(surface, width, height);
1979}
1980
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001981static int
1982fixed_round_up_to_int(wl_fixed_t f)
1983{
1984 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1985}
1986
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001987static void
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001988convert_size_by_transform_scale(int32_t *width_out, int32_t *height_out,
1989 int32_t width, int32_t height,
1990 uint32_t transform,
1991 int32_t scale)
1992{
1993 assert(scale > 0);
1994
1995 switch (transform) {
1996 case WL_OUTPUT_TRANSFORM_NORMAL:
1997 case WL_OUTPUT_TRANSFORM_180:
1998 case WL_OUTPUT_TRANSFORM_FLIPPED:
1999 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2000 *width_out = width / scale;
2001 *height_out = height / scale;
2002 break;
2003 case WL_OUTPUT_TRANSFORM_90:
2004 case WL_OUTPUT_TRANSFORM_270:
2005 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2006 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2007 *width_out = height / scale;
2008 *height_out = width / scale;
2009 break;
2010 default:
2011 assert(0 && "invalid transform");
2012 }
2013}
2014
2015static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002016weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002017{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002018 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01002019
2020 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02002021 surface->width_from_buffer = 0;
2022 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01002023 return;
2024 }
2025
Pekka Paalanen59987fa2016-04-26 15:50:59 +03002026 convert_size_by_transform_scale(&surface->width_from_buffer,
2027 &surface->height_from_buffer,
2028 surface->buffer_ref.buffer->width,
2029 surface->buffer_ref.buffer->height,
2030 vp->buffer.transform,
2031 vp->buffer.scale);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002032}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02002033
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002034static void
2035weston_surface_update_size(struct weston_surface *surface)
2036{
2037 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
2038 int32_t width, height;
2039
2040 width = surface->width_from_buffer;
2041 height = surface->height_from_buffer;
2042
2043 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02002044 surface_set_size(surface,
2045 vp->surface.width, vp->surface.height);
2046 return;
2047 }
2048
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002049 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03002050 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
2051 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
2052
2053 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02002054 return;
2055 }
2056
Jason Ekstrand5c11a332013-12-04 20:32:03 -06002057 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002058}
2059
Marius Vlad9fdda7f2019-06-11 16:08:55 +03002060/** weston_compositor_get_time
2061 * \ingroup compositor
2062 */
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02002063WL_EXPORT void
2064weston_compositor_get_time(struct timespec *time)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05002065{
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02002066 clock_gettime(CLOCK_REALTIME, time);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05002067}
2068
Marius Vlad9fdda7f2019-06-11 16:08:55 +03002069/** weston_compositor_pick_view
2070 * \ingroup compositor
2071 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002072WL_EXPORT struct weston_view *
2073weston_compositor_pick_view(struct weston_compositor *compositor,
2074 wl_fixed_t x, wl_fixed_t y,
2075 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02002076{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002077 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02002078 wl_fixed_t view_x, view_y;
2079 int view_ix, view_iy;
2080 int ix = wl_fixed_to_int(x);
2081 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02002082
Jason Ekstranda7af7042013-10-12 22:38:11 -05002083 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02002084 if (!pixman_region32_contains_point(
2085 &view->transform.boundingbox, ix, iy, NULL))
2086 continue;
2087
2088 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
2089 view_ix = wl_fixed_to_int(view_x);
2090 view_iy = wl_fixed_to_int(view_y);
2091
2092 if (!pixman_region32_contains_point(&view->surface->input,
2093 view_ix, view_iy, NULL))
2094 continue;
2095
Pekka Paalanen380adf52015-02-16 14:39:11 +02002096 if (view->geometry.scissor_enabled &&
2097 !pixman_region32_contains_point(&view->geometry.scissor,
2098 view_ix, view_iy, NULL))
2099 continue;
2100
Pekka Paalanenfc22a522015-02-18 15:08:29 +02002101 *vx = view_x;
2102 *vy = view_y;
2103 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02002104 }
2105
Derek Foremanf9318d12015-05-11 15:40:11 -05002106 *vx = wl_fixed_from_int(-1000000);
2107 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02002108 return NULL;
2109}
2110
2111static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002112weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002113{
Daniel Stone37816df2012-05-16 18:45:18 +01002114 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002115
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07002116 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002117 return;
2118
Daniel Stone37816df2012-05-16 18:45:18 +01002119 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04002120 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002121}
2122
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002123WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002124weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05002125{
Daniel Stone4dab5db2012-05-30 16:31:53 +01002126 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002127
Jason Ekstranda7af7042013-10-12 22:38:11 -05002128 if (!weston_view_is_mapped(view))
2129 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002130
Jason Ekstranda7af7042013-10-12 22:38:11 -05002131 weston_view_damage_below(view);
Semi Malinene7a52fb2018-04-26 11:08:10 +02002132 weston_view_set_output(view, NULL);
Xiong Zhang97116532013-10-23 13:58:31 +08002133 view->plane = NULL;
Armin Krezovićf8486c32016-06-30 06:04:28 +02002134 view->is_mapped = false;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002135 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002136 wl_list_remove(&view->link);
2137 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002138 view->output_mask = 0;
2139 weston_surface_assign_output(view->surface);
2140
2141 if (weston_surface_is_mapped(view->surface))
2142 return;
2143
2144 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002145 struct weston_touch *touch = weston_seat_get_touch(seat);
2146 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2147 struct weston_keyboard *keyboard =
2148 weston_seat_get_keyboard(seat);
2149
2150 if (keyboard && keyboard->focus == view->surface)
2151 weston_keyboard_set_focus(keyboard, NULL);
2152 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05002153 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05002154 if (touch && touch->focus == view)
2155 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01002156 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002157}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002158
Jason Ekstranda7af7042013-10-12 22:38:11 -05002159WL_EXPORT void
2160weston_surface_unmap(struct weston_surface *surface)
2161{
2162 struct weston_view *view;
2163
Armin Krezovićf8486c32016-06-30 06:04:28 +02002164 surface->is_mapped = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002165 wl_list_for_each(view, &surface->views, surface_link)
2166 weston_view_unmap(view);
2167 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05002168}
2169
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002170static void
2171weston_surface_reset_pending_buffer(struct weston_surface *surface)
2172{
Jason Ekstrand7b982072014-05-20 14:33:03 -05002173 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002174 surface->pending.sx = 0;
2175 surface->pending.sy = 0;
2176 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002177 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002178}
2179
Jason Ekstranda7af7042013-10-12 22:38:11 -05002180WL_EXPORT void
2181weston_view_destroy(struct weston_view *view)
2182{
2183 wl_signal_emit(&view->destroy_signal, view);
2184
2185 assert(wl_list_empty(&view->geometry.child_list));
2186
2187 if (weston_view_is_mapped(view)) {
2188 weston_view_unmap(view);
2189 weston_compositor_build_view_list(view->surface->compositor);
2190 }
2191
2192 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002193 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002194
2195 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02002196 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002197 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002198 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002199
2200 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen944fae82018-05-22 13:15:58 +03002201 weston_view_set_output(view, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002202
Jason Ekstranda7af7042013-10-12 22:38:11 -05002203 wl_list_remove(&view->surface_link);
2204
2205 free(view);
2206}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002207
2208WL_EXPORT void
2209weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05002210{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002211 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002212 struct weston_view *ev, *nv;
Jonas Ådahld3414f22016-07-22 17:56:31 +08002213 struct weston_pointer_constraint *constraint, *next_constraint;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04002214
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02002215 if (--surface->ref_count > 0)
2216 return;
2217
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03002218 assert(surface->resource == NULL);
2219
Pekka Paalanenca790762015-04-17 14:23:38 +03002220 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02002221
Pekka Paalanene67858b2013-04-25 13:57:42 +03002222 assert(wl_list_empty(&surface->subsurface_list_pending));
2223 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02002224
Jason Ekstranda7af7042013-10-12 22:38:11 -05002225 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
2226 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002227
Jason Ekstrand7b982072014-05-20 14:33:03 -05002228 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002229
Pekka Paalanende685b82012-12-04 15:58:12 +02002230 weston_buffer_reference(&surface->buffer_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002231 weston_buffer_release_reference(&surface->buffer_release_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04002232
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02002233 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002234 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002235 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02002236
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002237 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002238 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002239
Pekka Paalanen133e4392014-09-23 22:08:46 -04002240 weston_presentation_feedback_discard_list(&surface->feedback_list);
2241
Jonas Ådahld3414f22016-07-22 17:56:31 +08002242 wl_list_for_each_safe(constraint, next_constraint,
2243 &surface->pointer_constraints,
2244 link)
2245 weston_pointer_constraint_destroy(constraint);
2246
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03002247 fd_clear(&surface->acquire_fence_fd);
2248
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04002249 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05002250}
2251
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002252static void
2253destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08002254{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002255 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08002256
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03002257 assert(surface);
2258
Giulio Camuffo0d379742013-11-15 22:06:15 +01002259 /* Set the resource to NULL, since we don't want to leave a
2260 * dangling pointer if the surface was refcounted and survives
2261 * the weston_surface_destroy() call. */
2262 surface->resource = NULL;
Pekka Paalanen4826f872016-04-22 14:14:38 +03002263
2264 if (surface->viewport_resource)
2265 wl_resource_set_user_data(surface->viewport_resource, NULL);
2266
Alexandros Frantzis27d7c392018-10-19 12:14:11 +03002267 if (surface->synchronization_resource) {
2268 wl_resource_set_user_data(surface->synchronization_resource,
2269 NULL);
2270 }
2271
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002272 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08002273}
2274
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002275static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002276weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
2277{
2278 struct weston_buffer *buffer =
2279 container_of(listener, struct weston_buffer, destroy_listener);
2280
2281 wl_signal_emit(&buffer->destroy_signal, buffer);
2282 free(buffer);
2283}
2284
Giulio Camuffoe058cd12013-12-12 14:14:29 +01002285WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002286weston_buffer_from_resource(struct wl_resource *resource)
2287{
2288 struct weston_buffer *buffer;
2289 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002290
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002291 listener = wl_resource_get_destroy_listener(resource,
2292 weston_buffer_destroy_handler);
2293
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002294 if (listener)
2295 return container_of(listener, struct weston_buffer,
2296 destroy_listener);
2297
2298 buffer = zalloc(sizeof *buffer);
2299 if (buffer == NULL)
2300 return NULL;
2301
2302 buffer->resource = resource;
2303 wl_signal_init(&buffer->destroy_signal);
2304 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04002305 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002306 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002307
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002308 return buffer;
2309}
2310
2311static void
Pekka Paalanende685b82012-12-04 15:58:12 +02002312weston_buffer_reference_handle_destroy(struct wl_listener *listener,
2313 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002314{
Pekka Paalanende685b82012-12-04 15:58:12 +02002315 struct weston_buffer_reference *ref =
2316 container_of(listener, struct weston_buffer_reference,
2317 destroy_listener);
2318
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002319 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02002320 ref->buffer = NULL;
2321}
2322
2323WL_EXPORT void
2324weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002325 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002326{
2327 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05002328 ref->buffer->busy_count--;
2329 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002330 assert(wl_resource_get_client(ref->buffer->resource));
Matt Hoosier3052bc72017-09-26 08:09:40 -05002331 wl_buffer_send_release(ref->buffer->resource);
Kristian Høgsberg20347802013-03-04 12:07:46 -05002332 }
Pekka Paalanende685b82012-12-04 15:58:12 +02002333 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002334 }
2335
Pekka Paalanende685b82012-12-04 15:58:12 +02002336 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002337 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002338 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02002339 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02002340 }
2341
Pekka Paalanende685b82012-12-04 15:58:12 +02002342 ref->buffer = buffer;
2343 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
2344}
2345
2346static void
Alexandros Frantzis67629672018-10-19 12:14:11 +03002347weston_buffer_release_reference_handle_destroy(struct wl_listener *listener,
2348 void *data)
2349{
2350 struct weston_buffer_release_reference *ref =
2351 container_of(listener, struct weston_buffer_release_reference,
2352 destroy_listener);
2353
2354 assert((struct wl_resource *)data == ref->buffer_release->resource);
2355 ref->buffer_release = NULL;
2356}
2357
2358static void
2359weston_buffer_release_destroy(struct weston_buffer_release *buffer_release)
2360{
2361 struct wl_resource *resource = buffer_release->resource;
2362 int release_fence_fd = buffer_release->fence_fd;
2363
2364 if (release_fence_fd >= 0) {
2365 zwp_linux_buffer_release_v1_send_fenced_release(
2366 resource, release_fence_fd);
2367 } else {
2368 zwp_linux_buffer_release_v1_send_immediate_release(
2369 resource);
2370 }
2371
2372 wl_resource_destroy(resource);
2373}
2374
2375WL_EXPORT void
2376weston_buffer_release_reference(struct weston_buffer_release_reference *ref,
2377 struct weston_buffer_release *buffer_release)
2378{
2379 if (buffer_release == ref->buffer_release)
2380 return;
2381
2382 if (ref->buffer_release) {
2383 ref->buffer_release->ref_count--;
2384 wl_list_remove(&ref->destroy_listener.link);
2385 if (ref->buffer_release->ref_count == 0)
2386 weston_buffer_release_destroy(ref->buffer_release);
2387 }
2388
2389 if (buffer_release) {
2390 buffer_release->ref_count++;
2391 wl_resource_add_destroy_listener(buffer_release->resource,
2392 &ref->destroy_listener);
2393 }
2394
2395 ref->buffer_release = buffer_release;
2396 ref->destroy_listener.notify =
2397 weston_buffer_release_reference_handle_destroy;
2398}
2399
2400WL_EXPORT void
2401weston_buffer_release_move(struct weston_buffer_release_reference *dest,
2402 struct weston_buffer_release_reference *src)
2403{
2404 weston_buffer_release_reference(dest, src->buffer_release);
2405 weston_buffer_release_reference(src, NULL);
2406}
2407
2408static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002409weston_surface_attach(struct weston_surface *surface,
2410 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002411{
2412 weston_buffer_reference(&surface->buffer_ref, buffer);
2413
Pekka Paalanena6421c42012-12-04 15:58:10 +02002414 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002415 if (weston_surface_is_mapped(surface))
2416 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002417 }
2418
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002419 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02002420
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002421 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002422 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002423}
2424
Marius Vlad9fdda7f2019-06-11 16:08:55 +03002425/** weston_compositor_damage_all
2426 * \ingroup compositor
2427 */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002428WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002429weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002430{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002431 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002432
2433 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002434 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002435}
2436
Marius Vlad55d87362019-06-11 01:15:35 +03002437/**
2438 * \ingroup output
2439 */
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05002440WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002441weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002442{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002443 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002444
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002445 pixman_region32_union(&compositor->primary_plane.damage,
2446 &compositor->primary_plane.damage,
2447 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002448 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002449}
2450
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04002451static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002452surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002453{
Pekka Paalanende685b82012-12-04 15:58:12 +02002454 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002455 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002456 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002457
Marius Vlad2a1b7862019-09-05 13:12:18 +03002458 if (pixman_region32_not_empty(&surface->damage))
Marius Vlad3203ff62019-09-05 14:56:12 +03002459 TL_POINT(surface->compositor, "core_flush_damage", TLP_SURFACE(surface),
Pekka Paalanenb5026542014-11-12 15:09:24 +02002460 TLP_OUTPUT(surface->output), TLP_END);
2461
Jason Ekstrandef540082014-06-26 10:37:36 -07002462 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002463}
2464
2465static void
2466view_accumulate_damage(struct weston_view *view,
2467 pixman_region32_t *opaque)
2468{
2469 pixman_region32_t damage;
2470
2471 pixman_region32_init(&damage);
2472 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002473 pixman_box32_t *extents;
2474
Jason Ekstranda7af7042013-10-12 22:38:11 -05002475 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002476 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002477 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002478 pixman_region32_copy(&damage, &view->surface->damage);
2479 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002480 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002481 }
2482
Pekka Paalanen380adf52015-02-16 14:39:11 +02002483 pixman_region32_intersect(&damage, &damage,
2484 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002485 pixman_region32_subtract(&damage, &damage, opaque);
2486 pixman_region32_union(&view->plane->damage,
2487 &view->plane->damage, &damage);
2488 pixman_region32_fini(&damage);
2489 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002490 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002491}
2492
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002493static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002494compositor_accumulate_damage(struct weston_compositor *ec)
2495{
2496 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002497 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002498 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002499
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002500 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002501
2502 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002503 pixman_region32_copy(&plane->clip, &clip);
2504
2505 pixman_region32_init(&opaque);
2506
Jason Ekstranda7af7042013-10-12 22:38:11 -05002507 wl_list_for_each(ev, &ec->view_list, link) {
2508 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002509 continue;
2510
Jason Ekstranda7af7042013-10-12 22:38:11 -05002511 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002512 }
2513
2514 pixman_region32_union(&clip, &clip, &opaque);
2515 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002516 }
2517
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002518 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002519
Jason Ekstranda7af7042013-10-12 22:38:11 -05002520 wl_list_for_each(ev, &ec->view_list, link)
Derek Foreman060cf112015-11-18 16:32:26 -06002521 ev->surface->touched = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002522
2523 wl_list_for_each(ev, &ec->view_list, link) {
2524 if (ev->surface->touched)
2525 continue;
Derek Foreman060cf112015-11-18 16:32:26 -06002526 ev->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002527
2528 surface_flush_damage(ev->surface);
2529
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002530 /* Both the renderer and the backend have seen the buffer
2531 * by now. If renderer needs the buffer, it has its own
2532 * reference set. If the backend wants to keep the buffer
2533 * around for migrating the surface into a non-primary plane
2534 * later, keep_buffer is true. Otherwise, drop the core
2535 * reference now, and allow early buffer release. This enables
2536 * clients to use single-buffering.
2537 */
Alexandros Frantzis67629672018-10-19 12:14:11 +03002538 if (!ev->surface->keep_buffer) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002539 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002540 weston_buffer_release_reference(
2541 &ev->surface->buffer_release_ref, NULL);
2542 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002543 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002544}
2545
2546static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002547surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002548{
2549 struct weston_subsurface *sub;
2550
Pekka Paalanene67858b2013-04-25 13:57:42 +03002551 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002552 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002553 continue;
2554
Jason Ekstranda7af7042013-10-12 22:38:11 -05002555 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2556 wl_list_init(&sub->surface->views);
2557
2558 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002559 }
2560}
2561
2562static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002563surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002564{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002565 struct weston_subsurface *sub;
2566 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002567
Jason Ekstranda7af7042013-10-12 22:38:11 -05002568 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2569 if (sub->surface == surface)
2570 continue;
2571
George Kiagiadakised04d382014-06-13 18:10:26 +02002572 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2573 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002574 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002575 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002576
2577 surface_free_unused_subsurface_views(sub->surface);
2578 }
2579}
2580
2581static void
2582view_list_add_subsurface_view(struct weston_compositor *compositor,
2583 struct weston_subsurface *sub,
2584 struct weston_view *parent)
2585{
2586 struct weston_subsurface *child;
2587 struct weston_view *view = NULL, *iv;
2588
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002589 if (!weston_surface_is_mapped(sub->surface))
2590 return;
2591
Jason Ekstranda7af7042013-10-12 22:38:11 -05002592 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2593 if (iv->geometry.parent == parent) {
2594 view = iv;
2595 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002596 }
2597 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002598
2599 if (view) {
2600 /* Put it back in the surface's list of views */
2601 wl_list_remove(&view->surface_link);
2602 wl_list_insert(&sub->surface->views, &view->surface_link);
2603 } else {
2604 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002605 weston_view_set_position(view,
2606 sub->position.x,
2607 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002608 weston_view_set_transform_parent(view, parent);
2609 }
2610
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002611 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002612 weston_view_update_transform(view);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002613 view->is_mapped = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002614
Pekka Paalanenb188e912013-11-19 14:03:35 +02002615 if (wl_list_empty(&sub->surface->subsurface_list)) {
2616 wl_list_insert(compositor->view_list.prev, &view->link);
2617 return;
2618 }
2619
2620 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2621 if (child->surface == sub->surface)
2622 wl_list_insert(compositor->view_list.prev, &view->link);
2623 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002624 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002625 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002626}
2627
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002628/* This recursively adds the sub-surfaces for a view, relying on the
2629 * sub-surface order. Thus, if a client restacks the sub-surfaces, that
2630 * change first happens to the sub-surface list, and then automatically
2631 * propagates here. See weston_surface_damage_subsurfaces() for how the
2632 * sub-surfaces receive damage when the client changes the state.
2633 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002634static void
2635view_list_add(struct weston_compositor *compositor,
2636 struct weston_view *view)
2637{
2638 struct weston_subsurface *sub;
2639
2640 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002641
Pekka Paalanenb188e912013-11-19 14:03:35 +02002642 if (wl_list_empty(&view->surface->subsurface_list)) {
2643 wl_list_insert(compositor->view_list.prev, &view->link);
2644 return;
2645 }
2646
2647 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2648 if (sub->surface == view->surface)
2649 wl_list_insert(compositor->view_list.prev, &view->link);
2650 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002651 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002652 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002653}
2654
2655static void
2656weston_compositor_build_view_list(struct weston_compositor *compositor)
2657{
Loïc Yhuel267b16e2019-09-17 20:14:56 +02002658 struct weston_view *view, *tmp;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002659 struct weston_layer *layer;
2660
2661 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002662 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002663 surface_stash_subsurface_views(view->surface);
2664
Loïc Yhuel267b16e2019-09-17 20:14:56 +02002665 wl_list_for_each_safe(view, tmp, &compositor->view_list, link)
2666 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002667 wl_list_init(&compositor->view_list);
Loïc Yhuel267b16e2019-09-17 20:14:56 +02002668
Jason Ekstranda7af7042013-10-12 22:38:11 -05002669 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002670 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002671 view_list_add(compositor, view);
2672 }
2673 }
2674
2675 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002676 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002677 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002678}
2679
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002680static void
2681weston_output_take_feedback_list(struct weston_output *output,
2682 struct weston_surface *surface)
2683{
2684 struct weston_view *view;
2685 struct weston_presentation_feedback *feedback;
2686 uint32_t flags = 0xffffffff;
2687
2688 if (wl_list_empty(&surface->feedback_list))
2689 return;
2690
2691 /* All views must have the flag for the flag to survive. */
2692 wl_list_for_each(view, &surface->views, surface_link) {
2693 /* ignore views that are not on this output at all */
2694 if (view->output_mask & (1u << output->id))
2695 flags &= view->psf_flags;
2696 }
2697
2698 wl_list_for_each(feedback, &surface->feedback_list, link)
2699 feedback->psf_flags = flags;
2700
2701 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2702 wl_list_init(&surface->feedback_list);
2703}
2704
David Herrmann1edf44c2013-10-22 17:11:26 +02002705static int
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002706weston_output_repaint(struct weston_output *output, void *repaint_data)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002707{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002708 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002709 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002710 struct weston_animation *animation, *next;
2711 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002712 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002713 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002714 int r;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002715 uint32_t frame_time_msec;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05302716 enum weston_hdcp_protection highest_requested = WESTON_HDCP_DISABLE;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002717
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002718 if (output->destroying)
2719 return 0;
2720
Marius Vlad3203ff62019-09-05 14:56:12 +03002721 TL_POINT(ec, "core_repaint_begin", TLP_OUTPUT(output), TLP_END);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002722
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002723 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002724 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002725
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05302726 /* Find the highest protection desired for an output */
2727 wl_list_for_each(ev, &ec->view_list, link) {
2728 if (ev->surface->output_mask & (1u << output->id)) {
2729 /*
2730 * The desired_protection of the output should be the
2731 * maximum of the desired_protection of the surfaces,
2732 * that are displayed on that output, to avoid
2733 * reducing the protection for existing surfaces.
2734 */
2735 if (ev->surface->desired_protection > highest_requested)
2736 highest_requested =
2737 ev->surface->desired_protection;
2738 }
2739 }
2740
2741 output->desired_protection = highest_requested;
2742
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002743 if (output->assign_planes && !output->disable_planes) {
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002744 output->assign_planes(output, repaint_data);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002745 } else {
2746 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002747 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002748 ev->psf_flags = 0;
2749 }
2750 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002751
Pekka Paalanene67858b2013-04-25 13:57:42 +03002752 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002753 wl_list_for_each(ev, &ec->view_list, link) {
2754 /* Note: This operation is safe to do multiple times on the
2755 * same surface.
2756 */
2757 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002758 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002759 &ev->surface->frame_callback_list);
2760 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002761
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002762 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002763 }
2764 }
2765
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002766 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002767
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002768 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002769 pixman_region32_intersect(&output_damage,
2770 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002771 pixman_region32_subtract(&output_damage,
2772 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002773
Scott Moreauccbf29d2012-02-22 14:21:41 -07002774 if (output->dirty)
2775 weston_output_update_matrix(output);
2776
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002777 r = output->repaint(output, &output_damage, repaint_data);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002778
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002779 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002780
Daniel Stone09a97e22017-03-01 11:34:06 +00002781 output->repaint_needed = false;
Daniel Stone05df8c12017-03-03 16:59:42 +00002782 if (r == 0)
2783 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002784
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002785 weston_compositor_repick(ec);
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002786
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002787 frame_time_msec = timespec_to_msec(&output->frame_time);
2788
Jonas Ådahldb773762012-06-13 00:01:21 +02002789 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002790 wl_callback_send_done(cb->resource, frame_time_msec);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002791 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002792 }
2793
Scott Moreaud64cf212012-06-08 19:40:54 -06002794 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002795 animation->frame_counter++;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002796 animation->frame(animation, output, &output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002797 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002798
Marius Vlad3203ff62019-09-05 14:56:12 +03002799 TL_POINT(ec, "core_repaint_posted", TLP_OUTPUT(output), TLP_END);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002800
David Herrmann1edf44c2013-10-22 17:11:26 +02002801 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002802}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002803
Pekka Paalanen82919792014-05-21 13:51:49 +03002804static void
2805weston_output_schedule_repaint_reset(struct weston_output *output)
2806{
Daniel Stone05df8c12017-03-03 16:59:42 +00002807 output->repaint_status = REPAINT_NOT_SCHEDULED;
Marius Vlad3203ff62019-09-05 14:56:12 +03002808 TL_POINT(output->compositor, "core_repaint_exit_loop",
2809 TLP_OUTPUT(output), TLP_END);
Pekka Paalanen82919792014-05-21 13:51:49 +03002810}
2811
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002812static int
2813weston_output_maybe_repaint(struct weston_output *output, struct timespec *now,
2814 void *repaint_data)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002815{
Pekka Paalanen0513a952014-05-21 16:17:27 +03002816 struct weston_compositor *compositor = output->compositor;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002817 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002818 int64_t msec_to_repaint;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002819
Daniel Stone6847b852017-03-01 11:34:08 +00002820 /* We're not ready yet; come back to make a decision later. */
2821 if (output->repaint_status != REPAINT_SCHEDULED)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002822 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002823
2824 msec_to_repaint = timespec_sub_to_msec(&output->next_repaint, now);
2825 if (msec_to_repaint > 1)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002826 return ret;
Daniel Stone05df8c12017-03-03 16:59:42 +00002827
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002828 /* If we're sleeping, drop the repaint machinery entirely; we will
2829 * explicitly repaint all outputs when we come back. */
2830 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2831 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
2832 goto err;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002833
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002834 /* We don't actually need to repaint this output; drop it from
2835 * repaint until something causes damage. */
2836 if (!output->repaint_needed)
2837 goto err;
2838
2839 /* If repaint fails, we aren't going to get weston_output_finish_frame
2840 * to trigger a new repaint, so drop it from repaint and hope
Daniel Stone6847b852017-03-01 11:34:08 +00002841 * something schedules a successful repaint later. As repainting may
2842 * take some time, re-read our clock as a courtesy to the next
2843 * output. */
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002844 ret = weston_output_repaint(output, repaint_data);
Daniel Stone6847b852017-03-01 11:34:08 +00002845 weston_compositor_read_presentation_clock(compositor, now);
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002846 if (ret != 0)
2847 goto err;
2848
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09002849 output->repainted = true;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002850 return ret;
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002851
2852err:
Pekka Paalanen0513a952014-05-21 16:17:27 +03002853 weston_output_schedule_repaint_reset(output);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002854 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002855}
2856
2857static void
2858output_repaint_timer_arm(struct weston_compositor *compositor)
2859{
2860 struct weston_output *output;
2861 bool any_should_repaint = false;
2862 struct timespec now;
Sergi Granellb4c08862017-03-18 13:01:15 +01002863 int64_t msec_to_next = INT64_MAX;
Daniel Stone6847b852017-03-01 11:34:08 +00002864
2865 weston_compositor_read_presentation_clock(compositor, &now);
2866
2867 wl_list_for_each(output, &compositor->output_list, link) {
2868 int64_t msec_to_this;
2869
2870 if (output->repaint_status != REPAINT_SCHEDULED)
2871 continue;
2872
2873 msec_to_this = timespec_sub_to_msec(&output->next_repaint,
2874 &now);
2875 if (!any_should_repaint || msec_to_this < msec_to_next)
2876 msec_to_next = msec_to_this;
2877
2878 any_should_repaint = true;
2879 }
2880
2881 if (!any_should_repaint)
2882 return;
2883
2884 /* Even if we should repaint immediately, add the minimum 1 ms delay.
2885 * This is a workaround to allow coalescing multiple output repaints
2886 * particularly from weston_output_finish_frame()
2887 * into the same call, which would not happen if we called
2888 * output_repaint_timer_handler() directly.
2889 */
2890 if (msec_to_next < 1)
2891 msec_to_next = 1;
2892
2893 wl_event_source_timer_update(compositor->repaint_timer, msec_to_next);
2894}
2895
2896static int
2897output_repaint_timer_handler(void *data)
2898{
2899 struct weston_compositor *compositor = data;
2900 struct weston_output *output;
2901 struct timespec now;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002902 void *repaint_data = NULL;
Emre Ucane479ed82018-03-20 15:29:40 +01002903 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002904
2905 weston_compositor_read_presentation_clock(compositor, &now);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002906
2907 if (compositor->backend->repaint_begin)
2908 repaint_data = compositor->backend->repaint_begin(compositor);
2909
2910 wl_list_for_each(output, &compositor->output_list, link) {
2911 ret = weston_output_maybe_repaint(output, &now, repaint_data);
2912 if (ret)
2913 break;
2914 }
2915
2916 if (ret == 0) {
Tomohito Esaki09bfcd62018-06-05 10:37:05 +09002917 if (compositor->backend->repaint_flush)
Antonio Borneoc90fccc2019-06-30 15:51:10 +02002918 ret = compositor->backend->repaint_flush(compositor,
2919 repaint_data);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002920 } else {
Antonio Borneoc90fccc2019-06-30 15:51:10 +02002921 if (compositor->backend->repaint_cancel)
2922 compositor->backend->repaint_cancel(compositor,
2923 repaint_data);
2924 }
2925
2926 if (ret != 0) {
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09002927 wl_list_for_each(output, &compositor->output_list, link) {
2928 if (output->repainted)
2929 weston_output_schedule_repaint_reset(output);
2930 }
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002931 }
Daniel Stone6847b852017-03-01 11:34:08 +00002932
Tomohito Esakiddaf95c2018-07-10 11:47:15 +09002933 wl_list_for_each(output, &compositor->output_list, link)
2934 output->repainted = false;
2935
Daniel Stone6847b852017-03-01 11:34:08 +00002936 output_repaint_timer_arm(compositor);
2937
Pekka Paalanen0513a952014-05-21 16:17:27 +03002938 return 0;
2939}
2940
Pekka Paalanen50aa3a72020-05-28 11:34:04 +03002941/** Convert a presentation timestamp to another clock domain
2942 *
2943 * \param compositor The compositor defines the presentation clock domain.
2944 * \param presentation_stamp The timestamp in presentation clock domain.
2945 * \param presentation_now Current time in presentation clock domain.
2946 * \param target_clock Defines the target clock domain.
2947 *
2948 * This approximation relies on presentation_stamp to be close to current time.
2949 * The further it is from current time and the bigger the speed difference
2950 * between the two clock domains, the bigger the conversion error.
2951 *
2952 * Conversion error due to system load is biased and unbounded.
2953 */
2954static struct timespec
2955convert_presentation_time_now(struct weston_compositor *compositor,
2956 const struct timespec *presentation_stamp,
2957 const struct timespec *presentation_now,
2958 clockid_t target_clock)
2959{
2960 struct timespec target_now = {};
2961 struct timespec target_stamp;
2962 int64_t delta_ns;
2963
2964 if (compositor->presentation_clock == target_clock)
2965 return *presentation_stamp;
2966
2967 clock_gettime(target_clock, &target_now);
2968 delta_ns = timespec_sub_to_nsec(presentation_stamp, presentation_now);
2969 timespec_add_nsec(&target_stamp, &target_now, delta_ns);
2970
2971 return target_stamp;
2972}
2973
Marius Vlad55d87362019-06-11 01:15:35 +03002974/**
2975 * \ingroup output
2976 */
Kristian Høgsbergef044142011-06-21 15:02:12 -04002977WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002978weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002979 const struct timespec *stamp,
2980 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002981{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002982 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002983 int32_t refresh_nsec;
2984 struct timespec now;
Pekka Paalanen50aa3a72020-05-28 11:34:04 +03002985 struct timespec vblank_monotonic;
Daniel Stone6847b852017-03-01 11:34:08 +00002986 int64_t msec_rel;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002987
Daniel Stone05df8c12017-03-03 16:59:42 +00002988 assert(output->repaint_status == REPAINT_AWAITING_COMPLETION);
Daniel Stone3615ce12017-03-01 11:34:05 +00002989 assert(stamp || (presented_flags & WP_PRESENTATION_FEEDBACK_INVALID));
2990
Daniel Stone6847b852017-03-01 11:34:08 +00002991 weston_compositor_read_presentation_clock(compositor, &now);
2992
Daniel Stone3615ce12017-03-01 11:34:05 +00002993 /* If we haven't been supplied any timestamp at all, we don't have a
2994 * timebase to work against, so any delay just wastes time. Push a
2995 * repaint as soon as possible so we can get on with it. */
Daniel Stone6847b852017-03-01 11:34:08 +00002996 if (!stamp) {
2997 output->next_repaint = now;
Daniel Stone3615ce12017-03-01 11:34:05 +00002998 goto out;
Daniel Stone6847b852017-03-01 11:34:08 +00002999 }
Daniel Stone3615ce12017-03-01 11:34:05 +00003000
Pekka Paalanen50aa3a72020-05-28 11:34:04 +03003001 vblank_monotonic = convert_presentation_time_now(compositor,
3002 stamp, &now,
3003 CLOCK_MONOTONIC);
Marius Vlad3203ff62019-09-05 14:56:12 +03003004 TL_POINT(compositor, "core_repaint_finished", TLP_OUTPUT(output),
Pekka Paalanen50aa3a72020-05-28 11:34:04 +03003005 TLP_VBLANK(&vblank_monotonic), TLP_END);
Marius Vladdf9278a2018-03-06 18:56:23 +02003006
Pekka Paalanend7894d02015-07-03 15:08:53 +03003007 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003008 weston_presentation_feedback_present_list(&output->feedback_list,
3009 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02003010 output->msc,
3011 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003012
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02003013 output->frame_time = *stamp;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003014
Daniel Stone6847b852017-03-01 11:34:08 +00003015 timespec_add_nsec(&output->next_repaint, stamp, refresh_nsec);
3016 timespec_add_msec(&output->next_repaint, &output->next_repaint,
3017 -compositor->repaint_msec);
3018 msec_rel = timespec_sub_to_msec(&output->next_repaint, &now);
Daniel Stone84aff5c2017-03-01 11:34:04 +00003019
3020 if (msec_rel < -1000 || msec_rel > 1000) {
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02003021 static bool warned;
3022
3023 if (!warned)
3024 weston_log("Warning: computed repaint delay is "
Daniel Stone6847b852017-03-01 11:34:08 +00003025 "insane: %lld msec\n", (long long) msec_rel);
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02003026 warned = true;
3027
Daniel Stone6847b852017-03-01 11:34:08 +00003028 output->next_repaint = now;
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02003029 }
3030
Mario Kleinerb7df04e2015-06-21 21:25:15 +02003031 /* Called from restart_repaint_loop and restart happens already after
3032 * the deadline given by repaint_msec? In that case we delay until
3033 * the deadline of the next frame, to give clients a more predictable
3034 * timing of the repaint cycle to lock on. */
Daniel Stoneeca5cca2017-02-28 21:53:51 +00003035 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID &&
3036 msec_rel < 0) {
3037 while (timespec_sub_to_nsec(&output->next_repaint, &now) < 0) {
3038 timespec_add_nsec(&output->next_repaint,
3039 &output->next_repaint,
3040 refresh_nsec);
3041 }
3042 }
Mario Kleinerb7df04e2015-06-21 21:25:15 +02003043
Daniel Stone3615ce12017-03-01 11:34:05 +00003044out:
Daniel Stone05df8c12017-03-03 16:59:42 +00003045 output->repaint_status = REPAINT_SCHEDULED;
Daniel Stone6847b852017-03-01 11:34:08 +00003046 output_repaint_timer_arm(compositor);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003047}
3048
3049static void
3050idle_repaint(void *data)
3051{
3052 struct weston_output *output = data;
Antonio Borneoc90fccc2019-06-30 15:51:10 +02003053 int ret;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003054
Daniel Stone05df8c12017-03-03 16:59:42 +00003055 assert(output->repaint_status == REPAINT_BEGIN_FROM_IDLE);
3056 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03003057 output->idle_repaint_source = NULL;
Antonio Borneoc90fccc2019-06-30 15:51:10 +02003058 ret = output->start_repaint_loop(output);
3059 if (ret != 0)
3060 weston_output_schedule_repaint_reset(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003061}
3062
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003063WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003064weston_layer_entry_insert(struct weston_layer_entry *list,
3065 struct weston_layer_entry *entry)
3066{
3067 wl_list_insert(&list->link, &entry->link);
3068 entry->layer = list->layer;
3069}
3070
3071WL_EXPORT void
3072weston_layer_entry_remove(struct weston_layer_entry *entry)
3073{
3074 wl_list_remove(&entry->link);
3075 wl_list_init(&entry->link);
3076 entry->layer = NULL;
3077}
3078
Quentin Glidic82681572016-12-17 13:40:51 +01003079
3080/** Initialize the weston_layer struct.
3081 *
3082 * \param compositor The compositor instance
3083 * \param layer The layer to initialize
3084 */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003085WL_EXPORT void
Quentin Glidic82681572016-12-17 13:40:51 +01003086weston_layer_init(struct weston_layer *layer,
3087 struct weston_compositor *compositor)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003088{
Quentin Glidic82681572016-12-17 13:40:51 +01003089 layer->compositor = compositor;
3090 wl_list_init(&layer->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003091 wl_list_init(&layer->view_list.link);
3092 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003093 weston_layer_set_mask_infinite(layer);
Quentin Glidic82681572016-12-17 13:40:51 +01003094}
3095
3096/** Sets the position of the layer in the layer list. The layer will be placed
3097 * below any layer with the same position value, if any.
3098 * This function is safe to call if the layer is already on the list, but the
3099 * layer may be moved below other layers at the same position, if any.
3100 *
3101 * \param layer The layer to modify
3102 * \param position The position the layer will be placed at
3103 */
3104WL_EXPORT void
3105weston_layer_set_position(struct weston_layer *layer,
3106 enum weston_layer_position position)
3107{
3108 struct weston_layer *below;
3109
3110 wl_list_remove(&layer->link);
3111
3112 /* layer_list is ordered from top to bottom, the last layer being the
3113 * background with the smallest position value */
3114
3115 layer->position = position;
3116 wl_list_for_each_reverse(below, &layer->compositor->layer_list, link) {
3117 if (below->position >= layer->position) {
3118 wl_list_insert(&below->link, &layer->link);
3119 return;
3120 }
3121 }
3122 wl_list_insert(&layer->compositor->layer_list, &layer->link);
3123}
3124
3125/** Hide a layer by taking it off the layer list.
3126 * This function is safe to call if the layer is not on the list.
3127 *
3128 * \param layer The layer to hide
3129 */
3130WL_EXPORT void
3131weston_layer_unset_position(struct weston_layer *layer)
3132{
3133 wl_list_remove(&layer->link);
3134 wl_list_init(&layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003135}
3136
3137WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003138weston_layer_set_mask(struct weston_layer *layer,
3139 int x, int y, int width, int height)
3140{
3141 struct weston_view *view;
3142
3143 layer->mask.x1 = x;
3144 layer->mask.x2 = x + width;
3145 layer->mask.y1 = y;
3146 layer->mask.y2 = y + height;
3147
3148 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
3149 weston_view_geometry_dirty(view);
3150 }
3151}
3152
3153WL_EXPORT void
3154weston_layer_set_mask_infinite(struct weston_layer *layer)
3155{
Adam Jackson3c3f3b12019-10-16 16:02:59 -04003156 struct weston_view *view;
3157
3158 layer->mask.x1 = INT32_MIN;
3159 layer->mask.x2 = INT32_MAX;
3160 layer->mask.y1 = INT32_MIN;
3161 layer->mask.y2 = INT32_MAX;
3162
3163 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
3164 weston_view_geometry_dirty(view);
3165 }
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003166}
3167
Daniel Stone3b775632018-07-20 08:38:25 +01003168WL_EXPORT bool
3169weston_layer_mask_is_infinite(struct weston_layer *layer)
3170{
3171 return layer->mask.x1 == INT32_MIN &&
3172 layer->mask.y1 == INT32_MIN &&
Adam Jackson3c3f3b12019-10-16 16:02:59 -04003173 layer->mask.x2 == INT32_MAX &&
3174 layer->mask.y2 == INT32_MAX;
Daniel Stone3b775632018-07-20 08:38:25 +01003175}
3176
Marius Vlad55d87362019-06-11 01:15:35 +03003177/**
3178 * \ingroup output
3179 */
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003180WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003181weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003182{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003183 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04003184 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01003185
Bryce Harrington08976ac2016-08-30 12:05:16 -07003186 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
3187 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003188 return;
3189
Pekka Paalanenb5026542014-11-12 15:09:24 +02003190 if (!output->repaint_needed)
Marius Vlad3203ff62019-09-05 14:56:12 +03003191 TL_POINT(compositor, "core_repaint_req", TLP_OUTPUT(output), TLP_END);
Pekka Paalanenb5026542014-11-12 15:09:24 +02003192
Kristian Høgsbergef044142011-06-21 15:02:12 -04003193 loop = wl_display_get_event_loop(compositor->wl_display);
Daniel Stone09a97e22017-03-01 11:34:06 +00003194 output->repaint_needed = true;
Daniel Stone05df8c12017-03-03 16:59:42 +00003195
3196 /* If we already have a repaint scheduled for our idle handler,
3197 * no need to set it again. If the repaint has been called but
3198 * not finished, then weston_output_finish_frame() will notice
3199 * that a repaint is needed and schedule one. */
3200 if (output->repaint_status != REPAINT_NOT_SCHEDULED)
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003201 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01003202
Daniel Stone05df8c12017-03-03 16:59:42 +00003203 output->repaint_status = REPAINT_BEGIN_FROM_IDLE;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03003204 assert(!output->idle_repaint_source);
3205 output->idle_repaint_source = wl_event_loop_add_idle(loop, idle_repaint,
3206 output);
Marius Vlad3203ff62019-09-05 14:56:12 +03003207 TL_POINT(compositor, "core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003208}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05003209
Marius Vlad9fdda7f2019-06-11 16:08:55 +03003210/** weston_compositor_schedule_repaint
3211 * \ingroup compositor
3212 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003213WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003214weston_compositor_schedule_repaint(struct weston_compositor *compositor)
3215{
3216 struct weston_output *output;
3217
3218 wl_list_for_each(output, &compositor->output_list, link)
3219 weston_output_schedule_repaint(output);
3220}
3221
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003222static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003223surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003224{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003225 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003226}
3227
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003228static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003229surface_attach(struct wl_client *client,
3230 struct wl_resource *resource,
3231 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
3232{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003233 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05003234 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003235
Kristian Høgsbergab19f932013-08-20 11:30:54 -07003236 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05003237 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07003238 if (buffer == NULL) {
3239 wl_client_post_no_memory(client);
3240 return;
3241 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07003242 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04003243
Pekka Paalanende685b82012-12-04 15:58:12 +02003244 /* Attach, attach, without commit in between does not send
3245 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003246 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003247
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003248 surface->pending.sx = sx;
3249 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01003250 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003251}
3252
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003253static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003254surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003255 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003256 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05003257{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003258 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003259
Derek Foreman57e92ed2015-11-17 14:11:35 -06003260 if (width <= 0 || height <= 0)
3261 return;
3262
Derek Foreman152254b2015-11-26 14:17:48 -06003263 pixman_region32_union_rect(&surface->pending.damage_surface,
3264 &surface->pending.damage_surface,
3265 x, y, width, height);
3266}
3267
3268static void
3269surface_damage_buffer(struct wl_client *client,
3270 struct wl_resource *resource,
3271 int32_t x, int32_t y, int32_t width, int32_t height)
3272{
3273 struct weston_surface *surface = wl_resource_get_user_data(resource);
3274
3275 if (width <= 0 || height <= 0)
3276 return;
3277
3278 pixman_region32_union_rect(&surface->pending.damage_buffer,
3279 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04003280 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003281}
3282
Kristian Høgsberg33418202011-08-16 23:01:28 -04003283static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003284destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04003285{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05003286 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003287
3288 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02003289 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003290}
3291
3292static void
3293surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003294 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04003295{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003296 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003297 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003298
3299 cb = malloc(sizeof *cb);
3300 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003301 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003302 return;
3303 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03003304
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003305 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
3306 callback);
3307 if (cb->resource == NULL) {
3308 free(cb);
3309 wl_resource_post_no_memory(resource);
3310 return;
3311 }
3312
Jason Ekstranda85118c2013-06-27 20:17:02 -05003313 wl_resource_set_implementation(cb->resource, NULL, cb,
3314 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003315
Pekka Paalanenbc106382012-10-10 12:49:31 +03003316 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003317}
3318
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003319static void
3320surface_set_opaque_region(struct wl_client *client,
3321 struct wl_resource *resource,
3322 struct wl_resource *region_resource)
3323{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003324 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003325 struct weston_region *region;
3326
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003327 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003328 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003329 pixman_region32_copy(&surface->pending.opaque,
3330 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003331 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07003332 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003333 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003334}
3335
3336static void
3337surface_set_input_region(struct wl_client *client,
3338 struct wl_resource *resource,
3339 struct wl_resource *region_resource)
3340{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003341 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003342 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003343
3344 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003345 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003346 pixman_region32_copy(&surface->pending.input,
3347 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003348 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003349 pixman_region32_fini(&surface->pending.input);
3350 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003351 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003352}
3353
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003354/* Cause damage to this sub-surface and all its children.
3355 *
3356 * This is useful when there are state changes that need an implicit
3357 * damage, e.g. a z-order change.
3358 */
3359static void
3360weston_surface_damage_subsurfaces(struct weston_subsurface *sub)
3361{
3362 struct weston_subsurface *child;
3363
3364 weston_surface_damage(sub->surface);
3365 sub->reordered = false;
3366
3367 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link)
3368 if (child != sub)
3369 weston_surface_damage_subsurfaces(child);
3370}
3371
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003372static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003373weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003374{
Pekka Paalanene67858b2013-04-25 13:57:42 +03003375 struct weston_subsurface *sub;
3376
3377 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
3378 parent_link_pending) {
3379 wl_list_remove(&sub->parent_link);
3380 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003381
3382 if (sub->reordered)
3383 weston_surface_damage_subsurfaces(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003384 }
3385}
3386
3387static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03003388weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05003389 struct weston_matrix *matrix)
3390{
Pekka Paalanen04baea52016-04-26 15:50:58 +03003391 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05003392 double src_width, src_height, dest_width, dest_height;
3393
3394 weston_matrix_init(matrix);
3395
3396 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
3397 src_width = surface->width_from_buffer;
3398 src_height = surface->height_from_buffer;
3399 } else {
3400 src_width = wl_fixed_to_double(vp->buffer.src_width);
3401 src_height = wl_fixed_to_double(vp->buffer.src_height);
3402 }
3403
3404 if (vp->surface.width == -1) {
3405 dest_width = src_width;
3406 dest_height = src_height;
3407 } else {
3408 dest_width = vp->surface.width;
3409 dest_height = vp->surface.height;
3410 }
3411
3412 if (src_width != dest_width || src_height != dest_height)
3413 weston_matrix_scale(matrix,
3414 src_width / dest_width,
3415 src_height / dest_height, 1);
3416
3417 if (vp->buffer.src_width != wl_fixed_from_int(-1))
3418 weston_matrix_translate(matrix,
3419 wl_fixed_to_double(vp->buffer.src_x),
3420 wl_fixed_to_double(vp->buffer.src_y),
3421 0);
3422
3423 switch (vp->buffer.transform) {
3424 case WL_OUTPUT_TRANSFORM_FLIPPED:
3425 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3426 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3427 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3428 weston_matrix_scale(matrix, -1, 1, 1);
3429 weston_matrix_translate(matrix,
3430 surface->width_from_buffer, 0, 0);
3431 break;
3432 }
3433
3434 switch (vp->buffer.transform) {
3435 default:
3436 case WL_OUTPUT_TRANSFORM_NORMAL:
3437 case WL_OUTPUT_TRANSFORM_FLIPPED:
3438 break;
3439 case WL_OUTPUT_TRANSFORM_90:
3440 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +02003441 weston_matrix_rotate_xy(matrix, 0, -1);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003442 weston_matrix_translate(matrix,
Pekka Paalanen8060d822020-02-06 15:27:54 +02003443 0, surface->width_from_buffer, 0);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003444 break;
3445 case WL_OUTPUT_TRANSFORM_180:
3446 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3447 weston_matrix_rotate_xy(matrix, -1, 0);
3448 weston_matrix_translate(matrix,
3449 surface->width_from_buffer,
3450 surface->height_from_buffer, 0);
3451 break;
3452 case WL_OUTPUT_TRANSFORM_270:
3453 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +02003454 weston_matrix_rotate_xy(matrix, 0, 1);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003455 weston_matrix_translate(matrix,
Pekka Paalanen8060d822020-02-06 15:27:54 +02003456 surface->height_from_buffer, 0, 0);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003457 break;
3458 }
3459
3460 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
3461}
3462
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003463/**
3464 * Compute a + b > c while being safe to overflows.
3465 */
3466static bool
3467fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
3468{
3469 return (int64_t)a + (int64_t)b > (int64_t)c;
3470}
3471
3472static bool
3473weston_surface_is_pending_viewport_source_valid(
3474 const struct weston_surface *surface)
3475{
3476 const struct weston_surface_state *pend = &surface->pending;
3477 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
3478 int width_from_buffer = 0;
3479 int height_from_buffer = 0;
3480 wl_fixed_t w;
3481 wl_fixed_t h;
3482
3483 /* If viewport source rect is not set, it is always ok. */
3484 if (vp->buffer.src_width == wl_fixed_from_int(-1))
3485 return true;
3486
3487 if (pend->newly_attached) {
3488 if (pend->buffer) {
3489 convert_size_by_transform_scale(&width_from_buffer,
3490 &height_from_buffer,
3491 pend->buffer->width,
3492 pend->buffer->height,
3493 vp->buffer.transform,
3494 vp->buffer.scale);
3495 } else {
3496 /* No buffer: viewport is irrelevant. */
3497 return true;
3498 }
3499 } else {
3500 width_from_buffer = surface->width_from_buffer;
3501 height_from_buffer = surface->height_from_buffer;
3502 }
3503
3504 assert((width_from_buffer == 0) == (height_from_buffer == 0));
3505 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
3506
3507 /* No buffer: viewport is irrelevant. */
3508 if (width_from_buffer == 0 || height_from_buffer == 0)
3509 return true;
3510
3511 /* overflow checks for wl_fixed_from_int() */
3512 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
3513 return false;
3514 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
3515 return false;
3516
3517 w = wl_fixed_from_int(width_from_buffer);
3518 h = wl_fixed_from_int(height_from_buffer);
3519
3520 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
3521 return false;
3522 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
3523 return false;
3524
3525 return true;
3526}
3527
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003528static bool
3529fixed_is_integer(wl_fixed_t v)
3530{
3531 return (v & 0xff) == 0;
3532}
3533
3534static bool
3535weston_surface_is_pending_viewport_dst_size_int(
3536 const struct weston_surface *surface)
3537{
3538 const struct weston_buffer_viewport *vp =
3539 &surface->pending.buffer_viewport;
3540
3541 if (vp->surface.width != -1) {
3542 assert(vp->surface.width > 0 && vp->surface.height > 0);
3543 return true;
3544 }
3545
3546 return fixed_is_integer(vp->buffer.src_width) &&
3547 fixed_is_integer(vp->buffer.src_height);
3548}
3549
Derek Foreman152254b2015-11-26 14:17:48 -06003550/* Translate pending damage in buffer co-ordinates to surface
3551 * co-ordinates and union it with a pixman_region32_t.
3552 * This should only be called after the buffer is attached.
3553 */
3554static void
3555apply_damage_buffer(pixman_region32_t *dest,
3556 struct weston_surface *surface,
3557 struct weston_surface_state *state)
3558{
3559 struct weston_buffer *buffer = surface->buffer_ref.buffer;
3560
3561 /* wl_surface.damage_buffer needs to be clipped to the buffer,
3562 * translated into surface co-ordinates and unioned with
3563 * any other surface damage.
3564 * None of this makes sense if there is no buffer though.
3565 */
3566 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
3567 pixman_region32_t buffer_damage;
3568
3569 pixman_region32_intersect_rect(&state->damage_buffer,
3570 &state->damage_buffer,
3571 0, 0, buffer->width,
3572 buffer->height);
3573 pixman_region32_init(&buffer_damage);
3574 weston_matrix_transform_region(&buffer_damage,
3575 &surface->buffer_to_surface_matrix,
3576 &state->damage_buffer);
3577 pixman_region32_union(dest, dest, &buffer_damage);
3578 pixman_region32_fini(&buffer_damage);
3579 }
3580 /* We should clear this on commit even if there was no buffer */
3581 pixman_region32_clear(&state->damage_buffer);
3582}
3583
Jason Ekstrand1e059042014-10-16 10:55:19 -05003584static void
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303585weston_surface_set_desired_protection(struct weston_surface *surface,
3586 enum weston_hdcp_protection protection)
3587{
3588 if (surface->desired_protection == protection)
3589 return;
3590 surface->desired_protection = protection;
3591 weston_surface_damage(surface);
3592}
3593
3594static void
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303595weston_surface_set_protection_mode(struct weston_surface *surface,
3596 enum weston_surface_protection_mode p_mode)
3597{
3598 struct content_protection *cp = surface->compositor->content_protection;
3599 struct protected_surface *psurface;
3600
3601 surface->protection_mode = p_mode;
3602 wl_list_for_each(psurface, &cp->protected_list, link) {
3603 if (!psurface || psurface->surface != surface)
3604 continue;
3605 weston_protected_surface_send_event(psurface,
3606 surface->current_protection);
3607 }
3608}
3609
3610static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05003611weston_surface_commit_state(struct weston_surface *surface,
3612 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003613{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003614 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003615 pixman_region32_t opaque;
3616
Alexander Larsson4ea95522013-05-22 14:41:37 +02003617 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02003618 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03003619 /* wp_viewport.set_source */
3620 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003621 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003622
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003623 /* wl_surface.attach */
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003624 if (state->newly_attached) {
3625 /* zwp_surface_synchronization_v1.set_acquire_fence */
3626 fd_move(&surface->acquire_fence_fd,
3627 &state->acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003628 /* zwp_surface_synchronization_v1.get_release */
3629 weston_buffer_release_move(&surface->buffer_release_ref,
3630 &state->buffer_release_ref);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003631 weston_surface_attach(surface, state->buffer);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003632 }
Jason Ekstrand7b982072014-05-20 14:33:03 -05003633 weston_surface_state_set_buffer(state, NULL);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003634 assert(state->acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003635 assert(state->buffer_release_ref.buffer_release == NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01003636
Jason Ekstrand1e059042014-10-16 10:55:19 -05003637 weston_surface_build_buffer_matrix(surface,
3638 &surface->surface_to_buffer_matrix);
3639 weston_matrix_invert(&surface->buffer_to_surface_matrix,
3640 &surface->surface_to_buffer_matrix);
3641
Jason Ekstrand7b982072014-05-20 14:33:03 -05003642 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003643 weston_surface_update_size(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003644 if (surface->committed)
3645 surface->committed(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003646 }
Giulio Camuffo184df502013-02-21 11:29:21 +01003647
Jason Ekstrand7b982072014-05-20 14:33:03 -05003648 state->sx = 0;
3649 state->sy = 0;
3650 state->newly_attached = 0;
3651 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03003652
Derek Foreman152254b2015-11-26 14:17:48 -06003653 /* wl_surface.damage and wl_surface.damage_buffer */
Marius Vlad2a1b7862019-09-05 13:12:18 +03003654 if (pixman_region32_not_empty(&state->damage_surface) ||
3655 pixman_region32_not_empty(&state->damage_buffer))
Marius Vlad3203ff62019-09-05 14:56:12 +03003656 TL_POINT(surface->compositor, "core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06003657
Pekka Paalanen8e159182012-10-10 12:49:25 +03003658 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06003659 &state->damage_surface);
3660
3661 apply_damage_buffer(&surface->damage, surface, state);
3662
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05003663 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07003664 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06003665 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003666
3667 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003668 pixman_region32_init(&opaque);
3669 pixman_region32_intersect_rect(&opaque, &state->opaque,
3670 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003671
3672 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
3673 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003674 wl_list_for_each(view, &surface->views, surface_link)
3675 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003676 }
3677
3678 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003679
3680 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003681 pixman_region32_intersect_rect(&surface->input, &state->input,
3682 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003683
Pekka Paalanenbc106382012-10-10 12:49:31 +03003684 /* wl_surface.frame */
3685 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05003686 &state->frame_callback_list);
3687 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003688
3689 /* XXX:
3690 * What should happen with a feedback request, if there
3691 * is no wl_buffer attached for this commit?
3692 */
3693
3694 /* presentation.feedback */
3695 wl_list_insert_list(&surface->feedback_list,
3696 &state->feedback_list);
3697 wl_list_init(&state->feedback_list);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003698
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303699 /* weston_protected_surface.enforced/relaxed */
3700 if (surface->protection_mode != state->protection_mode)
3701 weston_surface_set_protection_mode(surface,
3702 state->protection_mode);
3703
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303704 /* weston_protected_surface.set_type */
3705 weston_surface_set_desired_protection(surface, state->desired_protection);
3706
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003707 wl_signal_emit(&surface->commit_signal, surface);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003708}
3709
3710static void
3711weston_surface_commit(struct weston_surface *surface)
3712{
3713 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003714
Pekka Paalanene67858b2013-04-25 13:57:42 +03003715 weston_surface_commit_subsurface_order(surface);
3716
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003717 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003718}
3719
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003720static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003721weston_subsurface_commit(struct weston_subsurface *sub);
3722
3723static void
3724weston_subsurface_parent_commit(struct weston_subsurface *sub,
3725 int parent_is_synchronized);
3726
3727static void
3728surface_commit(struct wl_client *client, struct wl_resource *resource)
3729{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003730 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003731 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
3732
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003733 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
3734 assert(surface->viewport_resource);
3735
3736 wl_resource_post_error(surface->viewport_resource,
3737 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
3738 "wl_surface@%d has viewport source outside buffer",
3739 wl_resource_get_id(resource));
3740 return;
3741 }
3742
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003743 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
3744 assert(surface->viewport_resource);
3745
3746 wl_resource_post_error(surface->viewport_resource,
3747 WP_VIEWPORT_ERROR_BAD_SIZE,
3748 "wl_surface@%d viewport dst size not integer",
3749 wl_resource_get_id(resource));
3750 return;
3751 }
3752
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003753 if (surface->pending.acquire_fence_fd >= 0) {
3754 assert(surface->synchronization_resource);
3755
3756 if (!surface->pending.buffer) {
3757 fd_clear(&surface->pending.acquire_fence_fd);
3758 wl_resource_post_error(surface->synchronization_resource,
3759 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3760 "wl_surface@%"PRIu32" no buffer for synchronization",
3761 wl_resource_get_id(resource));
3762 return;
3763 }
3764
3765 /* We support fences for both wp_linux_dmabuf and opaque EGL
3766 * buffers, as mandated by minor version 2 of the
3767 * zwp_linux_explicit_synchronization_v1 protocol. Since
3768 * renderers that support fences currently only support these
3769 * two buffer types plus SHM buffers, we can just check for the
3770 * SHM buffer case here.
3771 */
3772 if (wl_shm_buffer_get(surface->pending.buffer->resource)) {
3773 fd_clear(&surface->pending.acquire_fence_fd);
3774 wl_resource_post_error(surface->synchronization_resource,
3775 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_UNSUPPORTED_BUFFER,
3776 "wl_surface@%"PRIu32" unsupported buffer for synchronization",
3777 wl_resource_get_id(resource));
3778 return;
3779 }
3780 }
3781
Alexandros Frantzis67629672018-10-19 12:14:11 +03003782 if (surface->pending.buffer_release_ref.buffer_release &&
3783 !surface->pending.buffer) {
3784 assert(surface->synchronization_resource);
3785
3786 wl_resource_post_error(surface->synchronization_resource,
3787 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3788 "wl_surface@%"PRIu32" no buffer for synchronization",
3789 wl_resource_get_id(resource));
3790 return;
3791 }
3792
Pekka Paalanene67858b2013-04-25 13:57:42 +03003793 if (sub) {
3794 weston_subsurface_commit(sub);
3795 return;
3796 }
3797
3798 weston_surface_commit(surface);
3799
3800 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
3801 if (sub->surface != surface)
3802 weston_subsurface_parent_commit(sub, 0);
3803 }
3804}
3805
3806static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003807surface_set_buffer_transform(struct wl_client *client,
3808 struct wl_resource *resource, int transform)
3809{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003810 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003811
Jonny Lamba55f1392014-05-30 12:07:15 +02003812 /* if wl_output.transform grows more members this will need to be updated. */
3813 if (transform < 0 ||
3814 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
3815 wl_resource_post_error(resource,
3816 WL_SURFACE_ERROR_INVALID_TRANSFORM,
3817 "buffer transform must be a valid transform "
3818 "('%d' specified)", transform);
3819 return;
3820 }
3821
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003822 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003823 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003824}
3825
Alexander Larsson4ea95522013-05-22 14:41:37 +02003826static void
3827surface_set_buffer_scale(struct wl_client *client,
3828 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003829 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003830{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003831 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003832
Jonny Lamba55f1392014-05-30 12:07:15 +02003833 if (scale < 1) {
3834 wl_resource_post_error(resource,
3835 WL_SURFACE_ERROR_INVALID_SCALE,
3836 "buffer scale must be at least one "
3837 "('%d' specified)", scale);
3838 return;
3839 }
3840
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003841 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003842 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003843}
3844
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003845static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003846 surface_destroy,
3847 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04003848 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003849 surface_frame,
3850 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003851 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003852 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02003853 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06003854 surface_set_buffer_scale,
3855 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003856};
3857
3858static void
3859compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003860 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003861{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04003862 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003863 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003864
Kristian Høgsberg18c93002012-01-27 11:58:31 -05003865 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003866 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003867 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003868 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003869 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003870
Jason Ekstranda85118c2013-06-27 20:17:02 -05003871 surface->resource =
3872 wl_resource_create(client, &wl_surface_interface,
3873 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003874 if (surface->resource == NULL) {
3875 weston_surface_destroy(surface);
3876 wl_resource_post_no_memory(resource);
3877 return;
3878 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003879 wl_resource_set_implementation(surface->resource, &surface_interface,
3880 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003881
3882 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003883}
3884
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003885static void
3886destroy_region(struct wl_resource *resource)
3887{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003888 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003889
3890 pixman_region32_fini(&region->region);
3891 free(region);
3892}
3893
3894static void
3895region_destroy(struct wl_client *client, struct wl_resource *resource)
3896{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003897 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003898}
3899
3900static void
3901region_add(struct wl_client *client, struct wl_resource *resource,
3902 int32_t x, int32_t y, int32_t width, int32_t height)
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_union_rect(&region->region, &region->region,
3907 x, y, width, height);
3908}
3909
3910static void
3911region_subtract(struct wl_client *client, struct wl_resource *resource,
3912 int32_t x, int32_t y, int32_t width, int32_t height)
3913{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003914 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003915 pixman_region32_t rect;
3916
3917 pixman_region32_init_rect(&rect, x, y, width, height);
3918 pixman_region32_subtract(&region->region, &region->region, &rect);
3919 pixman_region32_fini(&rect);
3920}
3921
3922static const struct wl_region_interface region_interface = {
3923 region_destroy,
3924 region_add,
3925 region_subtract
3926};
3927
3928static void
3929compositor_create_region(struct wl_client *client,
3930 struct wl_resource *resource, uint32_t id)
3931{
3932 struct weston_region *region;
3933
3934 region = malloc(sizeof *region);
3935 if (region == NULL) {
3936 wl_resource_post_no_memory(resource);
3937 return;
3938 }
3939
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003940 pixman_region32_init(&region->region);
3941
Jason Ekstranda85118c2013-06-27 20:17:02 -05003942 region->resource =
3943 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003944 if (region->resource == NULL) {
3945 free(region);
3946 wl_resource_post_no_memory(resource);
3947 return;
3948 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003949 wl_resource_set_implementation(region->resource, &region_interface,
3950 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003951}
3952
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003953static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003954 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003955 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003956};
3957
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003958static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003959weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3960{
3961 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003962
Jason Ekstrand7b982072014-05-20 14:33:03 -05003963 weston_surface_commit_state(surface, &sub->cached);
3964 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003965
3966 weston_surface_commit_subsurface_order(surface);
3967
3968 weston_surface_schedule_repaint(surface);
3969
Jason Ekstrand7b982072014-05-20 14:33:03 -05003970 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003971}
3972
3973static void
3974weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3975{
3976 struct weston_surface *surface = sub->surface;
3977
3978 /*
3979 * If this commit would cause the surface to move by the
3980 * attach(dx, dy) parameters, the old damage region must be
3981 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05003982 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003983 */
Derek Foreman152254b2015-11-26 14:17:48 -06003984 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003985 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06003986 pixman_region32_union(&sub->cached.damage_surface,
3987 &sub->cached.damage_surface,
3988 &surface->pending.damage_surface);
3989 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003990
3991 if (surface->pending.newly_attached) {
3992 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003993 weston_surface_state_set_buffer(&sub->cached,
3994 surface->pending.buffer);
3995 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003996 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003997 weston_presentation_feedback_discard_list(
3998 &sub->cached.feedback_list);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003999 /* zwp_surface_synchronization_v1.set_acquire_fence */
4000 fd_move(&sub->cached.acquire_fence_fd,
4001 &surface->pending.acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03004002 /* zwp_surface_synchronization_v1.get_release */
4003 weston_buffer_release_move(&sub->cached.buffer_release_ref,
4004 &surface->pending.buffer_release_ref);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004005 }
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05304006 sub->cached.desired_protection = surface->pending.desired_protection;
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05304007 sub->cached.protection_mode = surface->pending.protection_mode;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03004008 assert(surface->pending.acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03004009 assert(surface->pending.buffer_release_ref.buffer_release == NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004010 sub->cached.sx += surface->pending.sx;
4011 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02004012
Derek Foreman152254b2015-11-26 14:17:48 -06004013 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
4014
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004015 sub->cached.buffer_viewport.changed |=
4016 surface->pending.buffer_viewport.changed;
4017 sub->cached.buffer_viewport.buffer =
4018 surface->pending.buffer_viewport.buffer;
4019 sub->cached.buffer_viewport.surface =
4020 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004021
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004022 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004023
4024 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
4025
4026 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
4027
4028 wl_list_insert_list(&sub->cached.frame_callback_list,
4029 &surface->pending.frame_callback_list);
4030 wl_list_init(&surface->pending.frame_callback_list);
4031
Pekka Paalanen133e4392014-09-23 22:08:46 -04004032 wl_list_insert_list(&sub->cached.feedback_list,
4033 &surface->pending.feedback_list);
4034 wl_list_init(&surface->pending.feedback_list);
4035
Jason Ekstrand7b982072014-05-20 14:33:03 -05004036 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004037}
4038
Derek Foreman280e7dd2014-10-03 13:13:42 -05004039static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03004040weston_subsurface_is_synchronized(struct weston_subsurface *sub)
4041{
4042 while (sub) {
4043 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05004044 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004045
4046 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05004047 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004048
4049 sub = weston_surface_to_subsurface(sub->parent);
4050 }
4051
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01004052 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004053}
4054
4055static void
4056weston_subsurface_commit(struct weston_subsurface *sub)
4057{
4058 struct weston_surface *surface = sub->surface;
4059 struct weston_subsurface *tmp;
4060
4061 /* Recursive check for effectively synchronized. */
4062 if (weston_subsurface_is_synchronized(sub)) {
4063 weston_subsurface_commit_to_cache(sub);
4064 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05004065 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03004066 /* flush accumulated state from cache */
4067 weston_subsurface_commit_to_cache(sub);
4068 weston_subsurface_commit_from_cache(sub);
4069 } else {
4070 weston_surface_commit(surface);
4071 }
4072
4073 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
4074 if (tmp->surface != surface)
4075 weston_subsurface_parent_commit(tmp, 0);
4076 }
4077 }
4078}
4079
4080static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004081weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004082{
4083 struct weston_surface *surface = sub->surface;
4084 struct weston_subsurface *tmp;
4085
Pekka Paalanene67858b2013-04-25 13:57:42 +03004086 /* From now on, commit_from_cache the whole sub-tree, regardless of
4087 * the synchronized mode of each child. This sub-surface or some
4088 * of its ancestors were synchronized, so we are synchronized
4089 * all the way down.
4090 */
4091
Jason Ekstrand7b982072014-05-20 14:33:03 -05004092 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004093 weston_subsurface_commit_from_cache(sub);
4094
4095 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
4096 if (tmp->surface != surface)
4097 weston_subsurface_parent_commit(tmp, 1);
4098 }
4099}
4100
4101static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004102weston_subsurface_parent_commit(struct weston_subsurface *sub,
4103 int parent_is_synchronized)
4104{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004105 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004106 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004107 wl_list_for_each(view, &sub->surface->views, surface_link)
4108 weston_view_set_position(view,
4109 sub->position.x,
4110 sub->position.y);
4111
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004112 sub->position.set = 0;
4113 }
4114
4115 if (parent_is_synchronized || sub->synchronized)
4116 weston_subsurface_synchronized_commit(sub);
4117}
4118
Pekka Paalanen8274d902014-08-06 19:36:51 +03004119static int
4120subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
4121{
4122 return snprintf(buf, len, "sub-surface");
4123}
4124
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004125static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004126subsurface_committed(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004127{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004128 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004129
Jason Ekstranda7af7042013-10-12 22:38:11 -05004130 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004131 weston_view_set_position(view,
4132 view->geometry.x + dx,
4133 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004134
4135 /* No need to check parent mappedness, because if parent is not
4136 * mapped, parent is not in a visible layer, so this sub-surface
4137 * will not be drawn either.
4138 */
Armin Krezovićf8486c32016-06-30 06:04:28 +02004139
Pekka Paalanene67858b2013-04-25 13:57:42 +03004140 if (!weston_surface_is_mapped(surface)) {
Armin Krezovićf8486c32016-06-30 06:04:28 +02004141 surface->is_mapped = true;
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03004142
Derek Foreman4b1a0a12014-09-10 15:37:33 -05004143 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03004144 * because that would call it also for the parent surface,
4145 * which might not be mapped yet. That would lead to
4146 * inconsistent state, where the window could never be
4147 * mapped.
4148 *
Armin Krezovićf8486c32016-06-30 06:04:28 +02004149 * Instead just force the is_mapped flag on, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03004150 * weston_surface_is_mapped() return true, so that when the
4151 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03004152 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03004153 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004154 }
4155}
4156
4157static struct weston_subsurface *
4158weston_surface_to_subsurface(struct weston_surface *surface)
4159{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004160 if (surface->committed == subsurface_committed)
4161 return surface->committed_private;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004162
4163 return NULL;
4164}
4165
Pekka Paalanen01388e22013-04-25 13:57:44 +03004166WL_EXPORT struct weston_surface *
4167weston_surface_get_main_surface(struct weston_surface *surface)
4168{
4169 struct weston_subsurface *sub;
4170
4171 while (surface && (sub = weston_surface_to_subsurface(surface)))
4172 surface = sub->parent;
4173
4174 return surface;
4175}
4176
Pekka Paalanen50b67472014-10-01 15:02:41 +03004177WL_EXPORT int
4178weston_surface_set_role(struct weston_surface *surface,
4179 const char *role_name,
4180 struct wl_resource *error_resource,
4181 uint32_t error_code)
4182{
4183 assert(role_name);
4184
4185 if (surface->role_name == NULL ||
4186 surface->role_name == role_name ||
4187 strcmp(surface->role_name, role_name) == 0) {
4188 surface->role_name = role_name;
4189
4190 return 0;
4191 }
4192
4193 wl_resource_post_error(error_resource, error_code,
4194 "Cannot assign role %s to wl_surface@%d,"
4195 " already has role %s\n",
4196 role_name,
4197 wl_resource_get_id(surface->resource),
4198 surface->role_name);
4199 return -1;
4200}
4201
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02004202WL_EXPORT const char *
4203weston_surface_get_role(struct weston_surface *surface)
4204{
4205 return surface->role_name;
4206}
4207
Pekka Paalanen8274d902014-08-06 19:36:51 +03004208WL_EXPORT void
4209weston_surface_set_label_func(struct weston_surface *surface,
4210 int (*desc)(struct weston_surface *,
4211 char *, size_t))
4212{
4213 surface->get_label = desc;
Marius Vlad5de92972019-10-15 13:25:41 +03004214 weston_timeline_refresh_subscription_objects(surface->compositor,
4215 surface);
Pekka Paalanen8274d902014-08-06 19:36:51 +03004216}
4217
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004218/** Get the size of surface contents
4219 *
4220 * \param surface The surface to query.
4221 * \param width Returns the width of raw contents.
4222 * \param height Returns the height of raw contents.
4223 *
4224 * Retrieves the raw surface content size in pixels for the given surface.
4225 * This is the whole content size in buffer pixels. If the surface
4226 * has no content or the renderer does not implement this feature,
4227 * zeroes are returned.
4228 *
4229 * This function is used to determine the buffer size needed for
4230 * a weston_surface_copy_content() call.
4231 */
4232WL_EXPORT void
4233weston_surface_get_content_size(struct weston_surface *surface,
4234 int *width, int *height)
4235{
4236 struct weston_renderer *rer = surface->compositor->renderer;
4237
4238 if (!rer->surface_get_content_size) {
4239 *width = 0;
4240 *height = 0;
4241 return;
4242 }
4243
4244 rer->surface_get_content_size(surface, width, height);
4245}
4246
Quentin Glidic248dd102016-08-12 10:41:34 +02004247/** Get the bounding box of a surface and its subsurfaces
4248 *
4249 * \param surface The surface to query.
4250 * \return The bounding box relative to the surface origin.
4251 *
4252 */
4253WL_EXPORT struct weston_geometry
4254weston_surface_get_bounding_box(struct weston_surface *surface)
4255{
4256 pixman_region32_t region;
4257 pixman_box32_t *box;
4258 struct weston_subsurface *subsurface;
4259
4260 pixman_region32_init_rect(&region,
4261 0, 0,
4262 surface->width, surface->height);
4263
4264 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link)
4265 pixman_region32_union_rect(&region, &region,
4266 subsurface->position.x,
4267 subsurface->position.y,
4268 subsurface->surface->width,
4269 subsurface->surface->height);
4270
4271 box = pixman_region32_extents(&region);
4272 struct weston_geometry geometry = {
4273 .x = box->x1,
4274 .y = box->y1,
4275 .width = box->x2 - box->x1,
4276 .height = box->y2 - box->y1,
4277 };
4278
4279 pixman_region32_fini(&region);
4280
4281 return geometry;
4282}
4283
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004284/** Copy surface contents to system memory.
4285 *
4286 * \param surface The surface to copy from.
4287 * \param target Pointer to the target memory buffer.
4288 * \param size Size of the target buffer in bytes.
4289 * \param src_x X location on contents to copy from.
4290 * \param src_y Y location on contents to copy from.
4291 * \param width Width in pixels of the area to copy.
4292 * \param height Height in pixels of the area to copy.
4293 * \return 0 for success, -1 for failure.
4294 *
4295 * Surface contents are maintained by the renderer. They can be in a
4296 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
4297 * else.
4298 *
4299 * Surface contents are copied into memory pointed to by target,
4300 * which has size bytes of space available. The target memory
4301 * may be larger than needed, but being smaller returns an error.
4302 * The extra bytes in target may or may not be written; their content is
4303 * unspecified. Size must be large enough to hold the image.
4304 *
4305 * The image in the target memory will be arranged in rows from
4306 * top to bottom, and pixels on a row from left to right. The pixel
4307 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
4308 * width * 4.
4309 *
4310 * Parameters src_x and src_y define the upper-left corner in buffer
4311 * coordinates (pixels) to copy from. Parameters width and height
4312 * define the size of the area to copy in pixels.
4313 *
4314 * The rectangle defined by src_x, src_y, width, height must fit in
4315 * the surface contents. Otherwise an error is returned.
4316 *
Changwoo Chof97d2502017-08-05 00:30:47 +09004317 * Use weston_surface_get_content_size to determine the content size; the
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004318 * needed target buffer size and rectangle limits.
4319 *
4320 * CURRENT IMPLEMENTATION RESTRICTIONS:
4321 * - the machine must be little-endian due to Pixman formats.
4322 *
4323 * NOTE: Pixman formats are premultiplied.
4324 */
4325WL_EXPORT int
4326weston_surface_copy_content(struct weston_surface *surface,
4327 void *target, size_t size,
4328 int src_x, int src_y,
4329 int width, int height)
4330{
4331 struct weston_renderer *rer = surface->compositor->renderer;
4332 int cw, ch;
4333 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
4334
4335 if (!rer->surface_copy_content)
4336 return -1;
4337
4338 weston_surface_get_content_size(surface, &cw, &ch);
4339
4340 if (src_x < 0 || src_y < 0)
4341 return -1;
4342
4343 if (width <= 0 || height <= 0)
4344 return -1;
4345
4346 if (src_x + width > cw || src_y + height > ch)
4347 return -1;
4348
4349 if (width * bytespp * height > size)
4350 return -1;
4351
4352 return rer->surface_copy_content(surface, target, size,
4353 src_x, src_y, width, height);
4354}
4355
Pekka Paalanene67858b2013-04-25 13:57:42 +03004356static void
4357subsurface_set_position(struct wl_client *client,
4358 struct wl_resource *resource, int32_t x, int32_t y)
4359{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004360 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004361
4362 if (!sub)
4363 return;
4364
4365 sub->position.x = x;
4366 sub->position.y = y;
4367 sub->position.set = 1;
4368}
4369
4370static struct weston_subsurface *
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004371subsurface_find_sibling(struct weston_subsurface *sub,
4372 struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004373{
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004374 struct weston_surface *parent = sub->parent;
4375 struct weston_subsurface *sibling;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004376
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004377 wl_list_for_each(sibling, &parent->subsurface_list, parent_link) {
4378 if (sibling->surface == surface && sibling != sub)
4379 return sibling;
4380 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004381
4382 return NULL;
4383}
4384
4385static struct weston_subsurface *
4386subsurface_sibling_check(struct weston_subsurface *sub,
4387 struct weston_surface *surface,
4388 const char *request)
4389{
4390 struct weston_subsurface *sibling;
4391
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004392 sibling = subsurface_find_sibling(sub, surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004393 if (!sibling) {
4394 wl_resource_post_error(sub->resource,
4395 WL_SUBSURFACE_ERROR_BAD_SURFACE,
4396 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004397 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004398 return NULL;
4399 }
4400
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004401 assert(sibling->parent == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004402
4403 return sibling;
4404}
4405
4406static void
4407subsurface_place_above(struct wl_client *client,
4408 struct wl_resource *resource,
4409 struct wl_resource *sibling_resource)
4410{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004411 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004412 struct weston_surface *surface =
4413 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004414 struct weston_subsurface *sibling;
4415
4416 if (!sub)
4417 return;
4418
4419 sibling = subsurface_sibling_check(sub, surface, "place_above");
4420 if (!sibling)
4421 return;
4422
4423 wl_list_remove(&sub->parent_link_pending);
4424 wl_list_insert(sibling->parent_link_pending.prev,
4425 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004426
4427 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004428}
4429
4430static void
4431subsurface_place_below(struct wl_client *client,
4432 struct wl_resource *resource,
4433 struct wl_resource *sibling_resource)
4434{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004435 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004436 struct weston_surface *surface =
4437 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004438 struct weston_subsurface *sibling;
4439
4440 if (!sub)
4441 return;
4442
4443 sibling = subsurface_sibling_check(sub, surface, "place_below");
4444 if (!sibling)
4445 return;
4446
4447 wl_list_remove(&sub->parent_link_pending);
4448 wl_list_insert(&sibling->parent_link_pending,
4449 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004450
4451 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004452}
4453
4454static void
4455subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
4456{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004457 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004458
4459 if (sub)
4460 sub->synchronized = 1;
4461}
4462
4463static void
4464subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
4465{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004466 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004467
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004468 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03004469 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004470
4471 /* If sub became effectively desynchronized, flush. */
4472 if (!weston_subsurface_is_synchronized(sub))
4473 weston_subsurface_synchronized_commit(sub);
4474 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004475}
4476
4477static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03004478weston_subsurface_unlink_parent(struct weston_subsurface *sub)
4479{
4480 wl_list_remove(&sub->parent_link);
4481 wl_list_remove(&sub->parent_link_pending);
4482 wl_list_remove(&sub->parent_destroy_listener.link);
4483 sub->parent = NULL;
4484}
4485
4486static void
4487weston_subsurface_destroy(struct weston_subsurface *sub);
4488
4489static void
4490subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
4491{
4492 struct weston_subsurface *sub =
4493 container_of(listener, struct weston_subsurface,
4494 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004495 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004496
4497 /* The protocol object (wl_resource) is left inert. */
4498 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004499 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004500
4501 weston_subsurface_destroy(sub);
4502}
4503
4504static void
4505subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
4506{
4507 struct weston_subsurface *sub =
4508 container_of(listener, struct weston_subsurface,
4509 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004510 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004511 assert(sub->surface != sub->parent);
4512
4513 if (weston_surface_is_mapped(sub->surface))
4514 weston_surface_unmap(sub->surface);
4515
4516 weston_subsurface_unlink_parent(sub);
4517}
4518
4519static void
4520subsurface_resource_destroy(struct wl_resource *resource)
4521{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004522 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004523
4524 if (sub)
4525 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004526}
4527
4528static void
4529subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
4530{
4531 wl_resource_destroy(resource);
4532}
4533
4534static void
4535weston_subsurface_link_parent(struct weston_subsurface *sub,
4536 struct weston_surface *parent)
4537{
4538 sub->parent = parent;
4539 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004540 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004541 &sub->parent_destroy_listener);
4542
4543 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4544 wl_list_insert(&parent->subsurface_list_pending,
4545 &sub->parent_link_pending);
4546}
4547
4548static void
4549weston_subsurface_link_surface(struct weston_subsurface *sub,
4550 struct weston_surface *surface)
4551{
4552 sub->surface = surface;
4553 sub->surface_destroy_listener.notify =
4554 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004555 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004556 &sub->surface_destroy_listener);
4557}
4558
4559static void
4560weston_subsurface_destroy(struct weston_subsurface *sub)
4561{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004562 struct weston_view *view, *next;
4563
Pekka Paalanene67858b2013-04-25 13:57:42 +03004564 assert(sub->surface);
4565
4566 if (sub->resource) {
4567 assert(weston_surface_to_subsurface(sub->surface) == sub);
4568 assert(sub->parent_destroy_listener.notify ==
4569 subsurface_handle_parent_destroy);
4570
George Kiagiadakised04d382014-06-13 18:10:26 +02004571 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
4572 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004573 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02004574 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05004575
Pekka Paalanene67858b2013-04-25 13:57:42 +03004576 if (sub->parent)
4577 weston_subsurface_unlink_parent(sub);
4578
Jason Ekstrand7b982072014-05-20 14:33:03 -05004579 weston_surface_state_fini(&sub->cached);
4580 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004581
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004582 sub->surface->committed = NULL;
4583 sub->surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004584 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004585 } else {
4586 /* the dummy weston_subsurface for the parent itself */
4587 assert(sub->parent_destroy_listener.notify == NULL);
4588 wl_list_remove(&sub->parent_link);
4589 wl_list_remove(&sub->parent_link_pending);
4590 }
4591
4592 wl_list_remove(&sub->surface_destroy_listener.link);
4593 free(sub);
4594}
4595
4596static const struct wl_subsurface_interface subsurface_implementation = {
4597 subsurface_destroy,
4598 subsurface_set_position,
4599 subsurface_place_above,
4600 subsurface_place_below,
4601 subsurface_set_sync,
4602 subsurface_set_desync
4603};
4604
4605static struct weston_subsurface *
4606weston_subsurface_create(uint32_t id, struct weston_surface *surface,
4607 struct weston_surface *parent)
4608{
4609 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004610 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004611
Bryce Harringtonde16d892014-11-20 22:21:57 -08004612 sub = zalloc(sizeof *sub);
4613 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004614 return NULL;
4615
Jason Ekstranda7af7042013-10-12 22:38:11 -05004616 wl_list_init(&sub->unused_views);
4617
Jason Ekstranda85118c2013-06-27 20:17:02 -05004618 sub->resource =
4619 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004620 if (!sub->resource) {
4621 free(sub);
4622 return NULL;
4623 }
4624
Jason Ekstranda85118c2013-06-27 20:17:02 -05004625 wl_resource_set_implementation(sub->resource,
4626 &subsurface_implementation,
4627 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004628 weston_subsurface_link_surface(sub, surface);
4629 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05004630 weston_surface_state_init(&sub->cached);
4631 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004632 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004633
4634 return sub;
4635}
4636
4637/* Create a dummy subsurface for having the parent itself in its
4638 * sub-surface lists. Makes stacking order manipulation easy.
4639 */
4640static struct weston_subsurface *
4641weston_subsurface_create_for_parent(struct weston_surface *parent)
4642{
4643 struct weston_subsurface *sub;
4644
Bryce Harringtonde16d892014-11-20 22:21:57 -08004645 sub = zalloc(sizeof *sub);
4646 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004647 return NULL;
4648
4649 weston_subsurface_link_surface(sub, parent);
4650 sub->parent = parent;
4651 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4652 wl_list_insert(&parent->subsurface_list_pending,
4653 &sub->parent_link_pending);
4654
4655 return sub;
4656}
4657
4658static void
4659subcompositor_get_subsurface(struct wl_client *client,
4660 struct wl_resource *resource,
4661 uint32_t id,
4662 struct wl_resource *surface_resource,
4663 struct wl_resource *parent_resource)
4664{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004665 struct weston_surface *surface =
4666 wl_resource_get_user_data(surface_resource);
4667 struct weston_surface *parent =
4668 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004669 struct weston_subsurface *sub;
4670 static const char where[] = "get_subsurface: wl_subsurface@";
4671
4672 if (surface == parent) {
4673 wl_resource_post_error(resource,
4674 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4675 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004676 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004677 return;
4678 }
4679
4680 if (weston_surface_to_subsurface(surface)) {
4681 wl_resource_post_error(resource,
4682 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4683 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004684 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004685 return;
4686 }
4687
Pekka Paalanen50b67472014-10-01 15:02:41 +03004688 if (weston_surface_set_role(surface, "wl_subsurface", resource,
4689 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004690 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004691
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004692 if (weston_surface_get_main_surface(parent) == surface) {
4693 wl_resource_post_error(resource,
4694 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4695 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004696 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004697 return;
4698 }
4699
Pekka Paalanene67858b2013-04-25 13:57:42 +03004700 /* make sure the parent is in its own list */
4701 if (wl_list_empty(&parent->subsurface_list)) {
4702 if (!weston_subsurface_create_for_parent(parent)) {
4703 wl_resource_post_no_memory(resource);
4704 return;
4705 }
4706 }
4707
4708 sub = weston_subsurface_create(id, surface, parent);
4709 if (!sub) {
4710 wl_resource_post_no_memory(resource);
4711 return;
4712 }
4713
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004714 surface->committed = subsurface_committed;
4715 surface->committed_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004716 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004717}
4718
4719static void
4720subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
4721{
4722 wl_resource_destroy(resource);
4723}
4724
4725static const struct wl_subcompositor_interface subcompositor_interface = {
4726 subcompositor_destroy,
4727 subcompositor_get_subsurface
4728};
4729
4730static void
4731bind_subcompositor(struct wl_client *client,
4732 void *data, uint32_t version, uint32_t id)
4733{
4734 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004735 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004736
Jason Ekstranda85118c2013-06-27 20:17:02 -05004737 resource =
4738 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004739 if (resource == NULL) {
4740 wl_client_post_no_memory(client);
4741 return;
4742 }
4743 wl_resource_set_implementation(resource, &subcompositor_interface,
4744 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004745}
4746
Bryce Harrington0795ece2016-08-30 12:04:26 -07004747/** Set a DPMS mode on all of the compositor's outputs
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004748 *
4749 * \param compositor The compositor instance
4750 * \param state The DPMS state the outputs will be set to
4751 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004752static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004753weston_compositor_dpms(struct weston_compositor *compositor,
4754 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004755{
4756 struct weston_output *output;
4757
Bryce Harrington08976ac2016-08-30 12:05:16 -07004758 wl_list_for_each(output, &compositor->output_list, link)
4759 if (output->set_dpms)
4760 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004761}
4762
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004763/** Restores the compositor to active status
4764 *
4765 * \param compositor The compositor instance
4766 *
4767 * If the compositor was in a sleeping mode, all outputs are powered
4768 * back on via DPMS. Otherwise if the compositor was inactive
4769 * (idle/locked, offscreen, or sleeping) then the compositor's wake
4770 * signal will fire.
4771 *
4772 * Restarts the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004773 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004774 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004775WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004776weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004777{
Neil Roberts8b62e202013-09-30 13:14:47 +01004778 uint32_t old_state = compositor->state;
4779
4780 /* The state needs to be changed before emitting the wake
4781 * signal because that may try to schedule a repaint which
4782 * will not work if the compositor is still sleeping */
4783 compositor->state = WESTON_COMPOSITOR_ACTIVE;
4784
4785 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004786 case WESTON_COMPOSITOR_SLEEPING:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004787 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004788 case WESTON_COMPOSITOR_OFFSCREEN:
Daniel Stone893b9362016-11-08 15:47:09 +00004789 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004790 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004791 /* fall through */
4792 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004793 wl_event_source_timer_update(compositor->idle_source,
4794 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004795 }
4796}
4797
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004798/** Turns off rendering and frame events for the compositor.
4799 *
4800 * \param compositor The compositor instance
4801 *
4802 * This is used for example to prevent further rendering while the
4803 * compositor is shutting down.
4804 *
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004805 * Stops the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004806 *
4807 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004808 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004809WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004810weston_compositor_offscreen(struct weston_compositor *compositor)
4811{
4812 switch (compositor->state) {
4813 case WESTON_COMPOSITOR_OFFSCREEN:
4814 return;
4815 case WESTON_COMPOSITOR_SLEEPING:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004816 default:
4817 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4818 wl_event_source_timer_update(compositor->idle_source, 0);
4819 }
4820}
4821
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004822/** Powers down all attached output devices
4823 *
4824 * \param compositor The compositor instance
4825 *
4826 * Causes rendering to the outputs to cease, and no frame events to be
4827 * sent. Only powers down the outputs if the compositor is not already
4828 * in sleep mode.
4829 *
4830 * Stops the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004831 *
4832 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004833 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004834WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004835weston_compositor_sleep(struct weston_compositor *compositor)
4836{
4837 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
4838 return;
4839
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004840 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004841 compositor->state = WESTON_COMPOSITOR_SLEEPING;
4842 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
4843}
4844
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004845/** Sets compositor to idle mode
4846 *
4847 * \param data The compositor instance
4848 *
4849 * This is called when the idle timer fires. Once the compositor is in
4850 * idle mode it requires a wake action (e.g. via
4851 * weston_compositor_wake()) to restore it. The compositor's
4852 * idle_signal will be triggered when the idle event occurs.
4853 *
4854 * Idleness can be inhibited by setting the compositor's idle_inhibit
4855 * property.
4856 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004857static int
4858idle_handler(void *data)
4859{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004860 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004861
4862 if (compositor->idle_inhibit)
4863 return 1;
4864
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004865 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004866 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004867
4868 return 1;
4869}
4870
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004871WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08004872weston_plane_init(struct weston_plane *plane,
4873 struct weston_compositor *ec,
4874 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004875{
4876 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004877 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004878 plane->x = x;
4879 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08004880 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004881
4882 /* Init the link so that the call to wl_list_remove() when releasing
4883 * the plane without ever stacking doesn't lead to a crash */
4884 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004885}
4886
4887WL_EXPORT void
4888weston_plane_release(struct weston_plane *plane)
4889{
Xiong Zhang97116532013-10-23 13:58:31 +08004890 struct weston_view *view;
4891
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004892 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004893 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004894
Xiong Zhang97116532013-10-23 13:58:31 +08004895 wl_list_for_each(view, &plane->compositor->view_list, link) {
4896 if (view->plane == plane)
4897 view->plane = NULL;
4898 }
4899
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004900 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004901}
4902
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004903/** weston_compositor_stack_plane
4904 * \ingroup compositor
4905 */
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004906WL_EXPORT void
4907weston_compositor_stack_plane(struct weston_compositor *ec,
4908 struct weston_plane *plane,
4909 struct weston_plane *above)
4910{
4911 if (above)
4912 wl_list_insert(above->link.prev, &plane->link);
4913 else
4914 wl_list_insert(&ec->plane_list, &plane->link);
4915}
4916
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004917static void
4918output_release(struct wl_client *client, struct wl_resource *resource)
4919{
4920 wl_resource_destroy(resource);
4921}
4922
4923static const struct wl_output_interface output_interface = {
4924 output_release,
4925};
4926
4927
Casey Dahlin9074db52012-04-19 22:50:09 -04004928static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004929{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004930 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004931}
4932
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004933static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004934bind_output(struct wl_client *client,
4935 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05004936{
Pekka Paalanen05347622017-03-27 12:24:34 +03004937 struct weston_head *head = data;
4938 struct weston_output *output = head->output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004939 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004940 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004941
Jason Ekstranda85118c2013-06-27 20:17:02 -05004942 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004943 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004944 if (resource == NULL) {
4945 wl_client_post_no_memory(client);
4946 return;
4947 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004948
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03004949 wl_list_insert(&head->resource_list, wl_resource_get_link(resource));
Pekka Paalanen055c1132017-03-27 16:31:25 +03004950 wl_resource_set_implementation(resource, &output_interface, head,
Pekka Paalanen05347622017-03-27 12:24:34 +03004951 unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04004952
Pekka Paalanen05347622017-03-27 12:24:34 +03004953 assert(output);
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004954 wl_output_send_geometry(resource,
4955 output->x,
4956 output->y,
Pekka Paalanen01f60212017-03-24 15:39:24 +02004957 head->mm_width,
4958 head->mm_height,
4959 head->subpixel,
4960 head->make, head->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04004961 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004962 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004963 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02004964 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004965
4966 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004967 wl_output_send_mode(resource,
4968 mode->flags,
4969 mode->width,
4970 mode->height,
4971 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004972 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02004973
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004974 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004975 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004976}
4977
Pekka Paalanendcac3512017-12-08 14:13:34 +02004978static void
4979weston_head_add_global(struct weston_head *head)
4980{
4981 head->global = wl_global_create(head->compositor->wl_display,
4982 &wl_output_interface, 3,
4983 head, bind_output);
4984}
4985
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02004986/** Remove the global wl_output protocol object
4987 *
4988 * \param head The head whose global to remove.
4989 *
4990 * Also orphans the wl_resources for this head (wl_output).
4991 */
4992static void
4993weston_head_remove_global(struct weston_head *head)
4994{
4995 struct wl_resource *resource, *tmp;
4996
4997 if (head->global)
4998 wl_global_destroy(head->global);
4999 head->global = NULL;
5000
5001 wl_resource_for_each_safe(resource, tmp, &head->resource_list) {
5002 unbind_resource(resource);
5003 wl_resource_set_destructor(resource, NULL);
5004 wl_resource_set_user_data(resource, NULL);
5005 }
Roman Gilge97391c2019-03-29 13:01:06 +01005006
5007 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
5008 /* It's sufficient to unset the destructor, then the list elements
5009 * won't be accessed.
5010 */
5011 wl_resource_set_destructor(resource, NULL);
5012 }
5013 wl_list_init(&head->xdg_output_resource_list);
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02005014}
5015
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005016/** Get the backing object of wl_output
5017 *
5018 * \param resource A wl_output protocol object.
5019 * \return The backing object (user data) of a wl_resource representing a
5020 * wl_output protocol object.
Marius Vlad78984ee2019-06-11 00:05:08 +03005021 *
5022 * \ingroup head
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005023 */
Pekka Paalanen055c1132017-03-27 16:31:25 +03005024WL_EXPORT struct weston_head *
5025weston_head_from_resource(struct wl_resource *resource)
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005026{
5027 assert(wl_resource_instance_of(resource, &wl_output_interface,
5028 &output_interface));
5029
5030 return wl_resource_get_user_data(resource);
5031}
5032
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005033/** Initialize a pre-allocated weston_head
5034 *
5035 * \param head The head to initialize.
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005036 * \param name The head name, e.g. the connector name or equivalent.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005037 *
5038 * The head will be safe to attach, detach and release.
5039 *
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005040 * The name is used in logs, and can be used by compositors as a configuration
5041 * identifier.
5042 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005043 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005044 * \internal
5045 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005046WL_EXPORT void
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005047weston_head_init(struct weston_head *head, const char *name)
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005048{
5049 /* Add some (in)sane defaults which can be used
5050 * for checking if an output was properly configured
5051 */
5052 memset(head, 0, sizeof *head);
5053
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005054 wl_list_init(&head->compositor_link);
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005055 wl_signal_init(&head->destroy_signal);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005056 wl_list_init(&head->output_link);
5057 wl_list_init(&head->resource_list);
Roman Gilge97391c2019-03-29 13:01:06 +01005058 wl_list_init(&head->xdg_output_resource_list);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005059 head->name = strdup(name);
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305060 head->current_protection = WESTON_HDCP_DISABLE;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005061}
5062
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005063/** Send output heads changed signal
5064 *
5065 * \param output The output that changed.
5066 *
5067 * Notify that the enabled output gained and/or lost heads, or that the
5068 * associated heads may have changed their connection status. This does not
5069 * include cases where the output becomes enabled or disabled. The registered
5070 * callbacks are called after the change has successfully happened.
5071 *
5072 * If connection status change causes the compositor to attach or detach a head
5073 * to an enabled output, the registered callbacks may be called multiple times.
Marius Vlad55d87362019-06-11 01:15:35 +03005074 *
5075 * \ingroup output
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005076 */
5077static void
5078weston_output_emit_heads_changed(struct weston_output *output)
5079{
5080 wl_signal_emit(&output->compositor->output_heads_changed_signal,
5081 output);
5082}
5083
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005084/** Idle task for emitting heads_changed_signal */
5085static void
5086weston_compositor_call_heads_changed(void *data)
5087{
5088 struct weston_compositor *compositor = data;
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005089 struct weston_head *head;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005090
5091 compositor->heads_changed_source = NULL;
5092
5093 wl_signal_emit(&compositor->heads_changed_signal, compositor);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005094
5095 wl_list_for_each(head, &compositor->head_list, compositor_link) {
5096 if (head->output && head->output->enabled)
5097 weston_output_emit_heads_changed(head->output);
5098 }
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005099}
5100
5101/** Schedule a call on idle to heads_changed callback
5102 *
5103 * \param compositor The Compositor.
5104 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005105 * \ingroup compositor
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005106 * \internal
5107 */
5108static void
5109weston_compositor_schedule_heads_changed(struct weston_compositor *compositor)
5110{
5111 struct wl_event_loop *loop;
5112
5113 if (compositor->heads_changed_source)
5114 return;
5115
5116 loop = wl_display_get_event_loop(compositor->wl_display);
5117 compositor->heads_changed_source = wl_event_loop_add_idle(loop,
5118 weston_compositor_call_heads_changed, compositor);
5119}
5120
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005121/** Register a new head
5122 *
5123 * \param compositor The compositor.
5124 * \param head The head to register, must not be already registered.
5125 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005126 * This signals the core that a new head has become available, leading to
5127 * heads_changed hook being called later.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005128 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005129 * \ingroup compositor
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005130 * \internal
5131 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005132WL_EXPORT void
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005133weston_compositor_add_head(struct weston_compositor *compositor,
5134 struct weston_head *head)
5135{
5136 assert(wl_list_empty(&head->compositor_link));
5137 assert(head->name);
5138
5139 wl_list_insert(compositor->head_list.prev, &head->compositor_link);
5140 head->compositor = compositor;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005141 weston_compositor_schedule_heads_changed(compositor);
5142}
5143
5144/** Adds a listener to be called when heads change
5145 *
5146 * \param compositor The compositor.
5147 * \param listener The listener to add.
5148 *
Marius Vlada2dace22019-06-12 16:05:44 +03005149 * The listener notify function argument is weston_compositor.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005150 *
5151 * The listener function will be called after heads are added or their
5152 * connection status has changed. Several changes may be accumulated into a
5153 * single call. The user is expected to iterate over the existing heads and
5154 * check their statuses to find out what changed.
5155 *
5156 * \sa weston_compositor_iterate_heads, weston_head_is_connected,
5157 * weston_head_is_enabled
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005158 * \ingroup compositor
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005159 */
5160WL_EXPORT void
5161weston_compositor_add_heads_changed_listener(struct weston_compositor *compositor,
5162 struct wl_listener *listener)
5163{
5164 wl_signal_add(&compositor->heads_changed_signal, listener);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005165}
5166
5167/** Iterate over available heads
5168 *
5169 * \param compositor The compositor.
Marius Vlada2dace22019-06-12 16:05:44 +03005170 * \param iter The iterator, or NULL for start.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005171 * \return The next available head in the list.
5172 *
5173 * Returns all available heads, regardless of being connected or enabled.
5174 *
5175 * You can iterate over all heads as follows:
5176 * \code
5177 * struct weston_head *head = NULL;
5178 *
5179 * while ((head = weston_compositor_iterate_heads(compositor, head))) {
5180 * ...
5181 * }
5182 * \endcode
5183 *
5184 * If you cause \c iter to be removed from the list, you cannot use it to
5185 * continue iterating. Removing any other item is safe.
5186 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005187 * \ingroup compositor
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005188 */
5189WL_EXPORT struct weston_head *
5190weston_compositor_iterate_heads(struct weston_compositor *compositor,
5191 struct weston_head *iter)
5192{
5193 struct wl_list *list = &compositor->head_list;
5194 struct wl_list *node;
5195
5196 assert(compositor);
5197 assert(!iter || iter->compositor == compositor);
5198
5199 if (iter)
5200 node = iter->compositor_link.next;
5201 else
5202 node = list->next;
5203
5204 assert(node);
5205 assert(!iter || node != &iter->compositor_link);
5206
5207 if (node == list)
5208 return NULL;
5209
5210 return container_of(node, struct weston_head, compositor_link);
5211}
5212
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005213/** Iterate over attached heads
5214 *
5215 * \param output The output whose heads to iterate.
Marius Vlada2dace22019-06-12 16:05:44 +03005216 * \param iter The iterator, or NULL for start.
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005217 * \return The next attached head in the list.
5218 *
5219 * Returns all heads currently attached to the output.
5220 *
5221 * You can iterate over all heads as follows:
5222 * \code
5223 * struct weston_head *head = NULL;
5224 *
5225 * while ((head = weston_output_iterate_heads(output, head))) {
5226 * ...
5227 * }
5228 * \endcode
5229 *
5230 * If you cause \c iter to be removed from the list, you cannot use it to
5231 * continue iterating. Removing any other item is safe.
5232 *
Marius Vlad55d87362019-06-11 01:15:35 +03005233 * \ingroup ouput
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005234 */
5235WL_EXPORT struct weston_head *
5236weston_output_iterate_heads(struct weston_output *output,
5237 struct weston_head *iter)
5238{
5239 struct wl_list *list = &output->head_list;
5240 struct wl_list *node;
5241
5242 assert(output);
5243 assert(!iter || iter->output == output);
5244
5245 if (iter)
5246 node = iter->output_link.next;
5247 else
5248 node = list->next;
5249
5250 assert(node);
5251 assert(!iter || node != &iter->output_link);
5252
5253 if (node == list)
5254 return NULL;
5255
5256 return container_of(node, struct weston_head, output_link);
5257}
5258
Pekka Paalanendcac3512017-12-08 14:13:34 +02005259/** Attach a head to an output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005260 *
5261 * \param output The output to attach to.
5262 * \param head The head that is not yet attached.
5263 * \return 0 on success, -1 on failure.
5264 *
5265 * Attaches the given head to the output. All heads of an output are clones
5266 * and share the resolution and timings.
5267 *
5268 * Cloning heads this way uses less resources than creating an output for
5269 * each head, but is not always possible due to environment, driver and hardware
5270 * limitations.
5271 *
5272 * On failure, the head remains unattached. Success of this function does not
5273 * guarantee the output configuration is actually valid. The final checks are
Pekka Paalanendcac3512017-12-08 14:13:34 +02005274 * made on weston_output_enable() unless the output was already enabled.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005275 *
Marius Vlad55d87362019-06-11 01:15:35 +03005276 * \ingroup output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005277 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005278WL_EXPORT int
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005279weston_output_attach_head(struct weston_output *output,
5280 struct weston_head *head)
5281{
Pekka Paalanendcac3512017-12-08 14:13:34 +02005282 char *head_names;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005283
5284 if (!wl_list_empty(&head->output_link))
5285 return -1;
5286
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005287 if (output->attach_head) {
5288 if (output->attach_head(output, head) < 0)
5289 return -1;
5290 } else if (!wl_list_empty(&output->head_list)) {
5291 /* No support for clones in the legacy path. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005292 return -1;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005293 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005294
5295 head->output = output;
5296 wl_list_insert(output->head_list.prev, &head->output_link);
5297
Pekka Paalanendcac3512017-12-08 14:13:34 +02005298 if (output->enabled) {
5299 weston_head_add_global(head);
5300
5301 head_names = weston_output_create_heads_string(output);
5302 weston_log("Output '%s' updated to have head(s) %s\n",
5303 output->name, head_names);
5304 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005305
5306 weston_output_emit_heads_changed(output);
Pekka Paalanendcac3512017-12-08 14:13:34 +02005307 }
5308
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005309 return 0;
5310}
5311
5312/** Detach a head from its output
5313 *
5314 * \param head The head to detach.
5315 *
5316 * It is safe to detach a non-attached head.
5317 *
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005318 * If the head is attached to an enabled output and the output will be left
5319 * with no heads, the output will be disabled.
5320 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005321 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005322 * \sa weston_output_disable
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005323 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005324WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005325weston_head_detach(struct weston_head *head)
5326{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005327 struct weston_output *output = head->output;
Pekka Paalanena0106992017-12-08 16:11:17 +02005328 char *head_names;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005329
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005330 wl_list_remove(&head->output_link);
5331 wl_list_init(&head->output_link);
5332 head->output = NULL;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005333
5334 if (!output)
5335 return;
5336
5337 if (output->detach_head)
5338 output->detach_head(output, head);
5339
5340 if (output->enabled) {
5341 weston_head_remove_global(head);
5342
Pekka Paalanena0106992017-12-08 16:11:17 +02005343 if (wl_list_empty(&output->head_list)) {
5344 weston_log("Output '%s' no heads left, disabling.\n",
5345 output->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005346 weston_output_disable(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02005347 } else {
5348 head_names = weston_output_create_heads_string(output);
5349 weston_log("Output '%s' updated to have head(s) %s\n",
5350 output->name, head_names);
5351 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005352
5353 weston_output_emit_heads_changed(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02005354 }
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005355 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005356}
5357
5358/** Destroy a head
5359 *
5360 * \param head The head to be released.
5361 *
5362 * Destroys the head. The caller is responsible for freeing the memory pointed
5363 * to by \c head.
5364 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005365 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005366 * \internal
5367 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005368WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005369weston_head_release(struct weston_head *head)
5370{
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005371 wl_signal_emit(&head->destroy_signal, head);
5372
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005373 weston_head_detach(head);
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005374
5375 free(head->make);
5376 free(head->model);
5377 free(head->serial_number);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005378 free(head->name);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005379
5380 wl_list_remove(&head->compositor_link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005381}
5382
Pekka Paalanene19970f2017-08-28 14:11:02 +03005383static void
5384weston_head_set_device_changed(struct weston_head *head)
5385{
5386 head->device_changed = true;
5387
5388 if (head->compositor)
5389 weston_compositor_schedule_heads_changed(head->compositor);
5390}
5391
5392/** String equal comparison with NULLs being equal */
5393static bool
5394str_null_eq(const char *a, const char *b)
5395{
5396 if (!a && !b)
5397 return true;
5398
5399 if (!!a != !!b)
5400 return false;
5401
5402 return strcmp(a, b) == 0;
5403}
5404
Pekka Paalanen01f60212017-03-24 15:39:24 +02005405/** Store monitor make, model and serial number
5406 *
5407 * \param head The head to modify.
5408 * \param make The monitor make. If EDID is available, the PNP ID. Otherwise
5409 * any string, or NULL for none.
5410 * \param model The monitor model or name, or a made-up string, or NULL for
5411 * none.
5412 * \param serialno The monitor serial number, a made-up string, or NULL for
5413 * none.
5414 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005415 * This may set the device_changed flag.
5416 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005417 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005418 * \internal
5419 */
5420WL_EXPORT void
5421weston_head_set_monitor_strings(struct weston_head *head,
5422 const char *make,
5423 const char *model,
5424 const char *serialno)
5425{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005426 if (str_null_eq(head->make, make) &&
5427 str_null_eq(head->model, model) &&
5428 str_null_eq(head->serial_number, serialno))
5429 return;
5430
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005431 free(head->make);
5432 free(head->model);
5433 free(head->serial_number);
5434
5435 head->make = make ? strdup(make) : NULL;
5436 head->model = model ? strdup(model) : NULL;
5437 head->serial_number = serialno ? strdup(serialno) : NULL;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005438
5439 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005440}
5441
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005442/** Store display non-desktop status
5443 *
5444 * \param head The head to modify.
5445 * \param non_desktop Whether the head connects to a non-desktop display.
5446 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005447 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005448 * \internal
5449 */
5450WL_EXPORT void
5451weston_head_set_non_desktop(struct weston_head *head, bool non_desktop)
5452{
5453 if (head->non_desktop == non_desktop)
5454 return;
5455
5456 head->non_desktop = non_desktop;
5457
5458 weston_head_set_device_changed(head);
5459}
5460
Lucas Stacha69cb712019-11-25 23:29:31 +00005461/** Store display transformation
5462 *
5463 * \param head The head to modify.
5464 * \param transform The transformation to apply for this head
5465 *
5466 * This may set the device_changed flag.
5467 *
5468 * \ingroup head
5469 * \internal
5470 */
5471WL_EXPORT void
5472weston_head_set_transform(struct weston_head *head, uint32_t transform)
5473{
5474 if (head->transform == transform)
5475 return;
5476
5477 head->transform = transform;
5478
5479 weston_head_set_device_changed(head);
5480}
5481
5482
Pekka Paalanen01f60212017-03-24 15:39:24 +02005483/** Store physical image size
5484 *
5485 * \param head The head to modify.
5486 * \param mm_width Image area width in millimeters.
5487 * \param mm_height Image area height in millimeters.
5488 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005489 * This may set the device_changed flag.
5490 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005491 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005492 * \internal
5493 */
5494WL_EXPORT void
5495weston_head_set_physical_size(struct weston_head *head,
5496 int32_t mm_width, int32_t mm_height)
5497{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005498 if (head->mm_width == mm_width &&
5499 head->mm_height == mm_height)
5500 return;
5501
Pekka Paalanen01f60212017-03-24 15:39:24 +02005502 head->mm_width = mm_width;
5503 head->mm_height = mm_height;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005504
5505 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005506}
5507
5508/** Store monitor sub-pixel layout
5509 *
5510 * \param head The head to modify.
5511 * \param sp Sub-pixel layout. The possible values are:
5512 * - WL_OUTPUT_SUBPIXEL_UNKNOWN,
5513 * - WL_OUTPUT_SUBPIXEL_NONE,
5514 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB,
5515 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR,
5516 * - WL_OUTPUT_SUBPIXEL_VERTICAL_RGB,
5517 * - WL_OUTPUT_SUBPIXEL_VERTICAL_BGR
5518 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005519 * This may set the device_changed flag.
5520 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005521 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005522 * \internal
5523 */
5524WL_EXPORT void
5525weston_head_set_subpixel(struct weston_head *head,
5526 enum wl_output_subpixel sp)
5527{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005528 if (head->subpixel == sp)
5529 return;
5530
Pekka Paalanen01f60212017-03-24 15:39:24 +02005531 head->subpixel = sp;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005532
5533 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005534}
5535
5536/** Mark the monitor as internal
5537 *
5538 * This is used for embedded screens, like laptop panels.
5539 *
5540 * \param head The head to mark as internal.
5541 *
5542 * By default a head is external. The type is often inferred from the physical
5543 * connector type.
5544 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005545 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005546 * \internal
5547 */
5548WL_EXPORT void
5549weston_head_set_internal(struct weston_head *head)
5550{
5551 head->connection_internal = true;
5552}
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005553
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005554/** Store connector status
5555 *
5556 * \param head The head to modify.
5557 * \param connected Whether the head is connected.
5558 *
5559 * Connectors are created as disconnected. This function can be used to
5560 * set the connector status.
5561 *
5562 * The status should be set to true when a physical connector is connected to
5563 * a video sink device like a monitor and to false when the connector is
5564 * disconnected. For nested backends, the connection status should reflect the
5565 * connection to the parent display server.
5566 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005567 * When the connection status changes, it schedules a call to the heads_changed
Pekka Paalanene19970f2017-08-28 14:11:02 +03005568 * hook and sets the device_changed flag.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005569 *
5570 * \sa weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005571 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005572 * \internal
5573 */
5574WL_EXPORT void
5575weston_head_set_connection_status(struct weston_head *head, bool connected)
5576{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005577 if (head->connected == connected)
5578 return;
5579
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005580 head->connected = connected;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005581
Pekka Paalanene19970f2017-08-28 14:11:02 +03005582 weston_head_set_device_changed(head);
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005583}
5584
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305585static void
5586weston_output_compute_protection(struct weston_output *output)
5587{
5588 struct weston_head *head;
5589 enum weston_hdcp_protection op_protection;
5590 bool op_protection_valid = false;
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305591 struct weston_compositor *wc = output->compositor;
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305592
5593 wl_list_for_each(head, &output->head_list, output_link) {
5594 if (!op_protection_valid) {
5595 op_protection = head->current_protection;
5596 op_protection_valid = true;
5597 }
5598 if (head->current_protection < op_protection)
5599 op_protection = head->current_protection;
5600 }
5601
5602 if (!op_protection_valid)
5603 op_protection = WESTON_HDCP_DISABLE;
5604
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305605 if (output->current_protection != op_protection) {
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305606 output->current_protection = op_protection;
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305607 weston_output_damage(output);
Ankit Nautiyalfaa5ab42019-07-08 13:23:24 +05305608 weston_schedule_surface_protection_update(wc);
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305609 }
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305610}
5611
5612WL_EXPORT void
5613weston_head_set_content_protection_status(struct weston_head *head,
5614 enum weston_hdcp_protection status)
5615{
5616 head->current_protection = status;
5617 if (head->output)
5618 weston_output_compute_protection(head->output);
5619}
5620
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005621/** Is the head currently connected?
5622 *
5623 * \param head The head to query.
5624 * \return Connection status.
5625 *
5626 * Returns true if the head is physically connected to a monitor, or in
5627 * case of a nested backend returns true when there is a connection to the
5628 * parent display server.
5629 *
5630 * This is independent from the head being enabled.
5631 *
5632 * \sa weston_head_is_enabled
Marius Vlad78984ee2019-06-11 00:05:08 +03005633 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005634 */
5635WL_EXPORT bool
5636weston_head_is_connected(struct weston_head *head)
5637{
5638 return head->connected;
5639}
5640
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005641/** Is the head currently enabled?
5642 *
5643 * \param head The head to query.
5644 * \return Video status.
5645 *
5646 * Returns true if the head is currently transmitting a video stream.
5647 *
5648 * This is independent of the head being connected.
5649 *
5650 * \sa weston_head_is_connected
Marius Vlad78984ee2019-06-11 00:05:08 +03005651 * \ingroup head
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005652 */
5653WL_EXPORT bool
5654weston_head_is_enabled(struct weston_head *head)
5655{
5656 if (!head->output)
5657 return false;
5658
5659 return head->output->enabled;
5660}
5661
Pekka Paalanene19970f2017-08-28 14:11:02 +03005662/** Has the device information changed?
5663 *
5664 * \param head The head to query.
5665 * \return True if the device information has changed since last reset.
5666 *
5667 * The information about the connected display device, e.g. a monitor, may
5668 * change without being disconnected in between. Changing information
5669 * causes a call to the heads_changed hook.
5670 *
5671 * The information includes make, model, serial number, physical size,
5672 * and sub-pixel type. The connection status is also included.
5673 *
5674 * \sa weston_head_reset_device_changed, weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005675 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03005676 */
5677WL_EXPORT bool
5678weston_head_is_device_changed(struct weston_head *head)
5679{
5680 return head->device_changed;
5681}
5682
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005683/** Does the head represent a non-desktop display?
5684 *
5685 * \param head The head to query.
5686 * \return True if the device is a non-desktop display.
5687 *
5688 * Non-desktop heads are not attached to outputs by default.
5689 * This stops weston from extending the desktop onto head mounted displays.
5690 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005691 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005692 */
5693WL_EXPORT bool
5694weston_head_is_non_desktop(struct weston_head *head)
5695{
5696 return head->non_desktop;
5697}
5698
Pekka Paalanene19970f2017-08-28 14:11:02 +03005699/** Acknowledge device information change
5700 *
5701 * \param head The head to acknowledge.
5702 *
5703 * Clears the device changed flag on this head. When a compositor has processed
5704 * device information, it should call this to be able to notice further
5705 * changes.
5706 *
5707 * \sa weston_head_is_device_changed
Marius Vlad78984ee2019-06-11 00:05:08 +03005708 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03005709 */
5710WL_EXPORT void
5711weston_head_reset_device_changed(struct weston_head *head)
5712{
5713 head->device_changed = false;
5714}
5715
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005716/** Get the name of a head
5717 *
5718 * \param head The head to query.
5719 * \return The head's name, not NULL.
5720 *
5721 * The name depends on the backend. The DRM backend uses connector names,
5722 * other backends may use hardcoded names or user-given names.
Marius Vlad78984ee2019-06-11 00:05:08 +03005723 *
5724 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005725 */
5726WL_EXPORT const char *
5727weston_head_get_name(struct weston_head *head)
5728{
5729 return head->name;
5730}
5731
5732/** Get the output the head is attached to
5733 *
5734 * \param head The head to query.
5735 * \return The output the head is attached to, or NULL if detached.
Marius Vlad78984ee2019-06-11 00:05:08 +03005736 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005737 */
5738WL_EXPORT struct weston_output *
5739weston_head_get_output(struct weston_head *head)
5740{
5741 return head->output;
5742}
5743
Lucas Stacha69cb712019-11-25 23:29:31 +00005744/** Get the head's native transformation
5745 *
5746 * \param head The head to query.
5747 * \return The head's native transform, as a WL_OUTPUT_TRANSFORM_* value
5748 *
5749 * A weston_head may have a 'native' transform provided by the backend.
5750 * Examples include panels which are physically rotated, where the rotation
5751 * is recorded and described as part of the system configuration. This call
5752 * will return any known native transform for the head.
5753 *
5754 * \ingroup head
5755 */
5756WL_EXPORT uint32_t
5757weston_head_get_transform(struct weston_head *head)
5758{
5759 return head->transform;
5760}
5761
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005762/** Add destroy callback for a head
5763 *
5764 * \param head The head to watch for.
5765 * \param listener The listener to add. The \c notify member must be set.
5766 *
5767 * Heads may get destroyed for various reasons by the backends. If a head is
5768 * attached to an output, the compositor should listen for head destruction
5769 * and reconfigure or destroy the output if necessary.
5770 *
5771 * The destroy callbacks will be called on weston_head destruction before any
5772 * automatic detaching from an associated weston_output and before any
5773 * weston_head information is lost.
5774 *
5775 * The \c data argument to the notify callback is the weston_head being
5776 * destroyed.
Marius Vlad78984ee2019-06-11 00:05:08 +03005777 *
5778 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005779 */
5780WL_EXPORT void
5781weston_head_add_destroy_listener(struct weston_head *head,
5782 struct wl_listener *listener)
5783{
5784 wl_signal_add(&head->destroy_signal, listener);
5785}
5786
5787/** Look up destroy listener for a head
5788 *
5789 * \param head The head to query.
5790 * \param notify The notify function used used for the added destroy listener.
5791 * \return The listener, or NULL if not found.
5792 *
5793 * This looks up the previously added destroy listener struct based on the
5794 * notify function it has. The listener can be used to access user data
5795 * through \c container_of().
5796 *
5797 * \sa wl_signal_get()
Marius Vlad78984ee2019-06-11 00:05:08 +03005798 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005799 */
5800WL_EXPORT struct wl_listener *
5801weston_head_get_destroy_listener(struct weston_head *head,
5802 wl_notify_func_t notify)
5803{
5804 return wl_signal_get(&head->destroy_signal, notify);
5805}
5806
David Fort0de859e2016-05-27 23:22:57 +02005807/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005808static void
David Fort0de859e2016-05-27 23:22:57 +02005809weston_compositor_reflow_outputs(struct weston_compositor *compositor,
5810 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005811{
5812 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02005813 bool start_resizing = false;
5814
5815 if (!delta_width)
5816 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005817
5818 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02005819 if (output == resized_output) {
5820 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005821 continue;
5822 }
5823
David Fort0de859e2016-05-27 23:22:57 +02005824 if (start_resizing) {
5825 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005826 output->dirty = 1;
5827 }
5828 }
5829}
5830
Pekka Paalanend72bad22017-03-29 17:01:41 +03005831static void
Scott Moreauccbf29d2012-02-22 14:21:41 -07005832weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005833{
Scott Moreau850ca422012-05-21 15:21:25 -06005834 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05005835
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005836 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005837 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06005838
Scott Moreauccbf29d2012-02-22 14:21:41 -07005839 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06005840 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005841 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005842 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005843 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005844 weston_matrix_scale(&output->matrix, magnification,
5845 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005846 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005847
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005848 switch (output->transform) {
5849 case WL_OUTPUT_TRANSFORM_FLIPPED:
5850 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5851 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5852 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5853 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5854 weston_matrix_scale(&output->matrix, -1, 1, 1);
5855 break;
5856 }
5857
5858 switch (output->transform) {
5859 default:
5860 case WL_OUTPUT_TRANSFORM_NORMAL:
5861 case WL_OUTPUT_TRANSFORM_FLIPPED:
5862 break;
5863 case WL_OUTPUT_TRANSFORM_90:
5864 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +02005865 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5866 weston_matrix_rotate_xy(&output->matrix, 0, -1);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005867 break;
5868 case WL_OUTPUT_TRANSFORM_180:
5869 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5870 weston_matrix_translate(&output->matrix,
5871 -output->width, -output->height, 0);
5872 weston_matrix_rotate_xy(&output->matrix, -1, 0);
5873 break;
5874 case WL_OUTPUT_TRANSFORM_270:
5875 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +02005876 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
5877 weston_matrix_rotate_xy(&output->matrix, 0, 1);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005878 break;
5879 }
5880
5881 if (output->current_scale != 1)
5882 weston_matrix_scale(&output->matrix,
5883 output->current_scale,
5884 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01005885
Scott Moreauccbf29d2012-02-22 14:21:41 -07005886 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05005887
5888 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005889}
5890
Scott Moreau1bad5db2012-08-18 01:04:05 -06005891static void
Alexander Larsson0b135062013-05-28 16:23:36 +02005892weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06005893{
5894 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005895 output->native_scale = scale;
5896 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06005897
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005898 convert_size_by_transform_scale(&output->width, &output->height,
5899 output->current_mode->width,
5900 output->current_mode->height,
5901 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02005902}
5903
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005904static void
5905weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07005906{
5907 output->x = x;
5908 output->y = y;
5909
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005910 pixman_region32_fini(&output->region);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005911 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06005912 output->width,
5913 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005914}
5915
Marius Vlad55d87362019-06-11 01:15:35 +03005916/**
5917 * \ingroup output
5918 */
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005919WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005920weston_output_move(struct weston_output *output, int x, int y)
5921{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005922 struct weston_head *head;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005923 struct wl_resource *resource;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005924 int ver;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005925
5926 output->move_x = x - output->x;
5927 output->move_y = y - output->y;
5928
5929 if (output->move_x == 0 && output->move_y == 0)
5930 return;
5931
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005932 weston_output_init_geometry(output, x, y);
5933
5934 output->dirty = 1;
5935
5936 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005937 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005938
5939 /* Notify clients of the change for output position. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005940 wl_list_for_each(head, &output->head_list, output_link) {
5941 wl_resource_for_each(resource, &head->resource_list) {
5942 wl_output_send_geometry(resource,
5943 output->x,
5944 output->y,
5945 head->mm_width,
5946 head->mm_height,
5947 head->subpixel,
5948 head->make,
5949 head->model,
5950 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08005951
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005952 ver = wl_resource_get_version(resource);
5953 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
5954 wl_output_send_done(resource);
5955 }
Roman Gilge97391c2019-03-29 13:01:06 +01005956
5957 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
5958 zxdg_output_v1_send_logical_position(resource,
5959 output->x,
5960 output->y);
5961 zxdg_output_v1_send_done(resource);
5962 }
Quanxian Wangb2c86362014-03-14 09:16:25 +08005963 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005964}
5965
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005966/** Signal that a pending output is taken into use.
5967 *
5968 * Removes the output from the pending list and adds it to the compositor's
5969 * list of enabled outputs. The output created signal is emitted.
Giulio Camuffob1147152015-05-06 21:41:57 +03005970 *
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005971 * The output gets an internal ID assigned, and the wl_output global is
5972 * created.
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005973 *
Giulio Camuffob1147152015-05-06 21:41:57 +03005974 * \param compositor The compositor instance.
5975 * \param output The output to be added.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005976 *
5977 * \internal
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005978 * \ingroup compositor
Giulio Camuffob1147152015-05-06 21:41:57 +03005979 */
Pekka Paalanenf9681b52017-03-29 16:58:48 +03005980static void
Giulio Camuffob1147152015-05-06 21:41:57 +03005981weston_compositor_add_output(struct weston_compositor *compositor,
5982 struct weston_output *output)
5983{
Armin Krezoviće5403842016-08-05 15:28:29 +02005984 struct weston_view *view, *next;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005985 struct weston_head *head;
Armin Krezoviće5403842016-08-05 15:28:29 +02005986
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005987 assert(!output->enabled);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005988
5989 /* Verify we haven't reached the limit of 32 available output IDs */
5990 assert(ffs(~compositor->output_id_pool) > 0);
5991
5992 /* Invert the output id pool and look for the lowest numbered
5993 * switch (the least significant bit). Take that bit's position
5994 * as our ID, and mark it used in the compositor's output_id_pool.
5995 */
5996 output->id = ffs(~compositor->output_id_pool) - 1;
5997 compositor->output_id_pool |= 1u << output->id;
5998
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005999 wl_list_remove(&output->link);
Giulio Camuffob1147152015-05-06 21:41:57 +03006000 wl_list_insert(compositor->output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006001 output->enabled = true;
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006002
Pekka Paalanendcac3512017-12-08 14:13:34 +02006003 wl_list_for_each(head, &output->head_list, output_link)
6004 weston_head_add_global(head);
Pekka Paalanen2210ad02017-03-30 15:48:06 +03006005
Giulio Camuffob1147152015-05-06 21:41:57 +03006006 wl_signal_emit(&compositor->output_created_signal, output);
Armin Krezoviće5403842016-08-05 15:28:29 +02006007
6008 wl_list_for_each_safe(view, next, &compositor->view_list, link)
6009 weston_view_geometry_dirty(view);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04006010}
6011
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006012/** Transform device coordinates into global coordinates
6013 *
Marius Vlada2dace22019-06-12 16:05:44 +03006014 * \param output the weston_output object
6015 * \param[in] device_x X coordinate in device units.
6016 * \param[in] device_y Y coordinate in device units.
6017 * \param[out] x X coordinate in the global space.
6018 * \param[out] y Y coordinate in the global space.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006019 *
Marius Vlada2dace22019-06-12 16:05:44 +03006020 * Transforms coordinates from the device coordinate space (physical pixel
6021 * units) to the global coordinate space (logical pixel units). This takes
6022 * into account output transform and scale.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006023 *
Marius Vlad55d87362019-06-11 01:15:35 +03006024 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006025 * \internal
6026 */
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006027WL_EXPORT void
6028weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02006029 double device_x, double device_y,
6030 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006031{
Derek Foreman0f679412014-10-02 13:41:17 -05006032 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02006033 device_x,
6034 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05006035 0.0,
6036 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006037
Derek Foreman67a18b92015-03-24 11:36:14 -05006038 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006039
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02006040 *x = p.f[0] / p.f[3];
6041 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006042}
6043
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006044/** Removes output from compositor's list of enabled outputs
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006045 *
6046 * \param output The weston_output object that is being removed.
6047 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006048 * The following happens:
6049 *
6050 * - The output assignments of all views in the current scenegraph are
6051 * recomputed.
6052 *
6053 * - Presentation feedback is discarded.
6054 *
6055 * - Compositor is notified that outputs were changed and
6056 * applies the necessary changes to re-layout outputs.
6057 *
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006058 * - The output is put back in the pending outputs list.
6059 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006060 * - Signal is emitted to notify all users of the weston_output
6061 * object that the output is being destroyed.
6062 *
6063 * - wl_output protocol objects referencing this weston_output
Pekka Paalanen2210ad02017-03-30 15:48:06 +03006064 * are made inert, and the wl_output global is removed.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006065 *
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03006066 * - The output's internal ID is released.
6067 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006068 * \ingroup compositor
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006069 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006070 */
6071static void
6072weston_compositor_remove_output(struct weston_output *output)
6073{
Pekka Paalanenbccda712017-03-29 16:16:04 +03006074 struct weston_compositor *compositor = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006075 struct weston_view *view;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03006076 struct weston_head *head;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006077
6078 assert(output->destroying);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006079 assert(output->enabled);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006080
Pekka Paalanenbccda712017-03-29 16:16:04 +03006081 wl_list_for_each(view, &compositor->view_list, link) {
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006082 if (view->output_mask & (1u << output->id))
6083 weston_view_assign_output(view);
6084 }
6085
6086 weston_presentation_feedback_discard_list(&output->feedback_list);
6087
Pekka Paalanen9711fd92018-06-21 14:26:18 +03006088 weston_compositor_reflow_outputs(compositor, output, -output->width);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006089
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006090 wl_list_remove(&output->link);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006091 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006092 output->enabled = false;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006093
Pekka Paalanenbccda712017-03-29 16:16:04 +03006094 wl_signal_emit(&compositor->output_destroyed_signal, output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006095 wl_signal_emit(&output->destroy_signal, output);
6096
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02006097 wl_list_for_each(head, &output->head_list, output_link)
6098 weston_head_remove_global(head);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03006099
6100 compositor->output_id_pool &= ~(1u << output->id);
6101 output->id = 0xffffffff; /* invalid */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006102}
6103
6104/** Sets the output scale for a given output.
6105 *
6106 * \param output The weston_output object that the scale is set for.
6107 * \param scale Scale factor for the given output.
6108 *
6109 * It only supports setting scale for an output that
6110 * is not enabled and it can only be ran once.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006111 *
Marius Vlad55d87362019-06-11 01:15:35 +03006112 * \ingroup ouput
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006113 */
6114WL_EXPORT void
6115weston_output_set_scale(struct weston_output *output,
6116 int32_t scale)
6117{
6118 /* We can only set scale on a disabled output */
6119 assert(!output->enabled);
6120
6121 /* We only want to set scale once */
6122 assert(!output->scale);
6123
6124 output->scale = scale;
6125}
6126
6127/** Sets the output transform for a given output.
6128 *
6129 * \param output The weston_output object that the transform is set for.
6130 * \param transform Transform value for the given output.
6131 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006132 * Refer to wl_output::transform section located at
6133 * https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_output
6134 * for list of values that can be passed to this function.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006135 *
Marius Vlad55d87362019-06-11 01:15:35 +03006136 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006137 */
6138WL_EXPORT void
6139weston_output_set_transform(struct weston_output *output,
6140 uint32_t transform)
6141{
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006142 struct weston_pointer_motion_event ev;
6143 struct wl_resource *resource;
6144 struct weston_seat *seat;
6145 pixman_region32_t old_region;
6146 int mid_x, mid_y;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006147 struct weston_head *head;
6148 int ver;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006149
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006150 if (!output->enabled && output->transform == UINT32_MAX) {
6151 output->transform = transform;
6152 return;
6153 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006154
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006155 weston_output_transform_scale_init(output, transform, output->scale);
6156
6157 pixman_region32_init(&old_region);
6158 pixman_region32_copy(&old_region, &output->region);
6159
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006160 weston_output_init_geometry(output, output->x, output->y);
6161
6162 output->dirty = 1;
6163
6164 /* Notify clients of the change for output transform. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006165 wl_list_for_each(head, &output->head_list, output_link) {
6166 wl_resource_for_each(resource, &head->resource_list) {
6167 wl_output_send_geometry(resource,
6168 output->x,
6169 output->y,
6170 head->mm_width,
6171 head->mm_height,
6172 head->subpixel,
6173 head->make,
6174 head->model,
6175 output->transform);
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006176
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006177 ver = wl_resource_get_version(resource);
6178 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
6179 wl_output_send_done(resource);
6180 }
Roman Gilge97391c2019-03-29 13:01:06 +01006181 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
6182 zxdg_output_v1_send_logical_position(resource,
6183 output->x,
6184 output->y);
6185 zxdg_output_v1_send_logical_size(resource,
6186 output->width,
6187 output->height);
6188 zxdg_output_v1_send_done(resource);
6189 }
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006190 }
6191
6192 /* we must ensure that pointers are inside output, otherwise they disappear */
6193 mid_x = output->x + output->width / 2;
6194 mid_y = output->y + output->height / 2;
6195
6196 ev.mask = WESTON_POINTER_MOTION_ABS;
6197 ev.x = wl_fixed_to_double(wl_fixed_from_int(mid_x));
6198 ev.y = wl_fixed_to_double(wl_fixed_from_int(mid_y));
6199
6200 wl_list_for_each(seat, &output->compositor->seat_list, link) {
6201 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
6202
6203 if (pointer && pixman_region32_contains_point(&old_region,
6204 wl_fixed_to_int(pointer->x),
6205 wl_fixed_to_int(pointer->y),
6206 NULL))
6207 weston_pointer_move(pointer, &ev);
6208 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006209}
6210
6211/** Initializes a weston_output object with enough data so
6212 ** an output can be configured.
6213 *
6214 * \param output The weston_output object to initialize
6215 * \param compositor The compositor instance.
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006216 * \param name Name for the output (the string is copied).
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006217 *
6218 * Sets initial values for fields that are expected to be
6219 * configured either by compositors or backends.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006220 *
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006221 * The name is used in logs, and can be used by compositors as a configuration
6222 * identifier.
6223 *
Marius Vlad55d87362019-06-11 01:15:35 +03006224 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006225 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006226 */
6227WL_EXPORT void
Armin Krezović40087402016-09-30 14:11:12 +02006228weston_output_init(struct weston_output *output,
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006229 struct weston_compositor *compositor,
6230 const char *name)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006231{
6232 output->compositor = compositor;
6233 output->destroying = 0;
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006234 output->name = strdup(name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006235 wl_list_init(&output->link);
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006236 wl_signal_init(&output->user_destroy_signal);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006237 output->enabled = false;
Ankit Nautiyal2690a772019-03-25 17:57:59 +05306238 output->desired_protection = WESTON_HDCP_DISABLE;
Ankit Nautiyal2844f8e2019-04-03 10:14:59 +05306239 output->allow_protection = true;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006240
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006241 wl_list_init(&output->head_list);
6242
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006243 /* Add some (in)sane defaults which can be used
6244 * for checking if an output was properly configured
6245 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006246 output->scale = 0;
6247 /* Can't use -1 on uint32_t and 0 is valid enum value */
6248 output->transform = UINT32_MAX;
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006249
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006250 pixman_region32_init(&output->region);
Pekka Paalanen42704142017-09-06 16:47:52 +03006251 wl_list_init(&output->mode_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006252}
6253
6254/** Adds weston_output object to pending output list.
6255 *
6256 * \param output The weston_output object to add
6257 * \param compositor The compositor instance.
6258 *
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03006259 * The opposite of this operation is built into weston_output_release().
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006260 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006261 * \ingroup compositor
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006262 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006263 */
6264WL_EXPORT void
6265weston_compositor_add_pending_output(struct weston_output *output,
6266 struct weston_compositor *compositor)
6267{
Pekka Paalanene952a012017-03-29 17:14:00 +03006268 assert(output->disable);
6269 assert(output->enable);
6270
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006271 wl_list_remove(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006272 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006273}
6274
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006275/** Create a string with the attached heads' names.
6276 *
6277 * The string must be free()'d.
Marius Vlad55d87362019-06-11 01:15:35 +03006278 *
6279 * \ingroup output
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006280 */
6281static char *
6282weston_output_create_heads_string(struct weston_output *output)
6283{
6284 FILE *fp;
6285 char *str = NULL;
6286 size_t size = 0;
6287 struct weston_head *head;
6288 const char *sep = "";
6289
6290 fp = open_memstream(&str, &size);
6291 if (!fp)
6292 return NULL;
6293
6294 wl_list_for_each(head, &output->head_list, output_link) {
6295 fprintf(fp, "%s%s", sep, head->name);
6296 sep = ", ";
6297 }
6298 fclose(fp);
6299
6300 return str;
6301}
6302
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006303/** Constructs a weston_output object that can be used by the compositor.
6304 *
Pekka Paalanencc201e42017-03-30 15:11:25 +03006305 * \param output The weston_output object that needs to be enabled. Must not
Pekka Paalanenddce54d2017-08-23 16:00:21 +03006306 * be enabled already. Must have at least one head attached.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006307 *
6308 * Output coordinates are calculated and each new output is by default
6309 * assigned to the right of previous one.
6310 *
6311 * Sets up the transformation, zoom, and geometry of the output using
6312 * the properties that need to be configured by the compositor.
6313 *
6314 * Establishes a repaint timer for the output with the relevant display
6315 * object's event loop. See output_repaint_timer_handler().
6316 *
6317 * The output is assigned an ID. Weston can support up to 32 distinct
6318 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
6319 * is referred to and used to find the first available ID number, and
6320 * then this ID is marked as used in output_id_pool.
6321 *
6322 * The output is also assigned a Wayland global with the wl_output
6323 * external interface.
6324 *
6325 * Backend specific function is called to set up the output output.
6326 *
6327 * Output is added to the compositor's output list
6328 *
6329 * If the backend specific function fails, the weston_output object
6330 * is returned to a state it was before calling this function and
6331 * is added to the compositor's pending_output_list in case it needs
6332 * to be reconfigured or just so it can be destroyed at shutdown.
6333 *
6334 * 0 is returned on success, -1 on failure.
Marius Vlad55d87362019-06-11 01:15:35 +03006335 *
6336 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006337 */
6338WL_EXPORT int
6339weston_output_enable(struct weston_output *output)
6340{
Armin Krezović782f5df2016-09-30 14:11:11 +02006341 struct weston_compositor *c = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006342 struct weston_output *iterator;
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03006343 struct weston_head *head;
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006344 char *head_names;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006345 int x = 0, y = 0;
6346
Pekka Paalanencc201e42017-03-30 15:11:25 +03006347 if (output->enabled) {
6348 weston_log("Error: attempt to enable an enabled output '%s'\n",
6349 output->name);
6350 return -1;
6351 }
6352
Pekka Paalanenddce54d2017-08-23 16:00:21 +03006353 if (wl_list_empty(&output->head_list)) {
6354 weston_log("Error: cannot enable output '%s' without heads.\n",
6355 output->name);
6356 return -1;
6357 }
6358
Pekka Paalanen586e1ac2017-09-14 16:17:59 +03006359 if (wl_list_empty(&output->mode_list) || !output->current_mode) {
6360 weston_log("Error: no video mode for output '%s'.\n",
6361 output->name);
6362 return -1;
6363 }
6364
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03006365 wl_list_for_each(head, &output->head_list, output_link) {
6366 assert(head->make);
6367 assert(head->model);
6368 }
6369
Armin Krezović782f5df2016-09-30 14:11:11 +02006370 iterator = container_of(c->output_list.prev,
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006371 struct weston_output, link);
6372
Armin Krezović782f5df2016-09-30 14:11:11 +02006373 if (!wl_list_empty(&c->output_list))
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006374 x = iterator->x + iterator->width;
6375
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006376 /* Make sure the scale is set up */
6377 assert(output->scale);
6378
6379 /* Make sure we have a transform set */
6380 assert(output->transform != UINT32_MAX);
6381
Armin Krezović782f5df2016-09-30 14:11:11 +02006382 output->x = x;
6383 output->y = y;
6384 output->dirty = 1;
6385 output->original_scale = output->scale;
6386
Marius Vlad3a2f8292019-11-04 17:53:46 +02006387 wl_signal_init(&output->frame_signal);
6388 wl_signal_init(&output->destroy_signal);
6389
Armin Krezović782f5df2016-09-30 14:11:11 +02006390 weston_output_transform_scale_init(output, output->transform, output->scale);
6391 weston_output_init_zoom(output);
6392
6393 weston_output_init_geometry(output, x, y);
6394 weston_output_damage(output);
6395
Armin Krezović782f5df2016-09-30 14:11:11 +02006396 wl_list_init(&output->animation_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02006397 wl_list_init(&output->feedback_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02006398
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006399 /* Enable the output (set up the crtc or create a
6400 * window representing the output, set up the
6401 * renderer, etc)
6402 */
6403 if (output->enable(output) < 0) {
6404 weston_log("Enabling output \"%s\" failed.\n", output->name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006405 return -1;
6406 }
6407
6408 weston_compositor_add_output(output->compositor, output);
6409
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006410 head_names = weston_output_create_heads_string(output);
6411 weston_log("Output '%s' enabled with head(s) %s\n",
6412 output->name, head_names);
6413 free(head_names);
6414
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006415 return 0;
6416}
6417
6418/** Converts a weston_output object to a pending output state, so it
6419 ** can be configured again or destroyed.
6420 *
6421 * \param output The weston_output object that needs to be disabled.
6422 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006423 * Calls a backend specific function to disable an output, in case
6424 * such function exists.
6425 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03006426 * The backend specific disable function may choose to postpone the disabling
6427 * by returning a negative value, in which case this function returns early.
6428 * In that case the backend will guarantee the output will be disabled soon
6429 * by the backend calling this function again. One must not attempt to re-enable
6430 * the output until that happens.
6431 *
6432 * Otherwise, if the output is being used by the compositor, it is removed
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006433 * from weston's output_list (see weston_compositor_remove_output())
6434 * and is returned to a state it was before weston_output_enable()
6435 * was ran (see weston_output_enable_undo()).
6436 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03006437 * See weston_output_init() for more information on the
6438 * state output is returned to.
Pekka Paalanencc201e42017-03-30 15:11:25 +03006439 *
6440 * If the output has never been enabled yet, this function can still be
6441 * called to ensure that the output is actually turned off rather than left
6442 * in the state it was discovered in.
Marius Vlad55d87362019-06-11 01:15:35 +03006443 *
6444 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006445 */
6446WL_EXPORT void
6447weston_output_disable(struct weston_output *output)
6448{
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006449 /* Should we rename this? */
6450 output->destroying = 1;
6451
Pekka Paalanenc65df642017-03-29 15:45:46 +03006452 /* Disable is called unconditionally also for not-enabled outputs,
6453 * because at compositor start-up, if there is an output that is
6454 * already on but the compositor wants to turn it off, we have to
6455 * forward the turn-off to the backend so it knows to do it.
6456 * The backend cannot initially turn off everything, because it
6457 * would cause unnecessary mode-sets for all outputs the compositor
6458 * wants to be on.
6459 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006460 if (output->disable(output) < 0)
6461 return;
6462
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006463 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006464 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006465
6466 output->destroying = 0;
6467}
6468
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006469/** Forces a synchronous call to heads_changed hook
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006470 *
6471 * \param compositor The compositor instance
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006472 *
6473 * If there are new or changed heads, calls the heads_changed hook and
6474 * returns after the hook returns.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006475 *
6476 * \ingroup compositor
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006477 */
6478WL_EXPORT void
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006479weston_compositor_flush_heads_changed(struct weston_compositor *compositor)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006480{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03006481 if (compositor->heads_changed_source) {
6482 wl_event_source_remove(compositor->heads_changed_source);
6483 weston_compositor_call_heads_changed(compositor);
6484 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006485}
6486
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006487/** Add destroy callback for an output
6488 *
6489 * \param output The output to watch.
6490 * \param listener The listener to add. The \c notify member must be set.
6491 *
6492 * The listener callback will be called when user destroys an output. This
6493 * may be delayed by a backend in some cases. The main purpose of the
6494 * listener is to allow hooking up custom data to the output. The custom data
6495 * can be fetched via weston_output_get_destroy_listener() followed by
6496 * container_of().
6497 *
6498 * The \c data argument to the notify callback is the weston_output being
6499 * destroyed.
6500 *
6501 * @note This is for the final destruction of an output, not when it gets
6502 * disabled. If you want to keep track of enabled outputs, this is not it.
Marius Vlad55d87362019-06-11 01:15:35 +03006503 *
6504 * \ingroup ouput
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006505 */
6506WL_EXPORT void
6507weston_output_add_destroy_listener(struct weston_output *output,
6508 struct wl_listener *listener)
6509{
6510 wl_signal_add(&output->user_destroy_signal, listener);
6511}
6512
6513/** Look up destroy listener for an output
6514 *
6515 * \param output The output to query.
6516 * \param notify The notify function used used for the added destroy listener.
6517 * \return The listener, or NULL if not found.
6518 *
6519 * This looks up the previously added destroy listener struct based on the
6520 * notify function it has. The listener can be used to access user data
6521 * through \c container_of().
6522 *
6523 * \sa wl_signal_get() weston_output_add_destroy_listener()
Marius Vlad55d87362019-06-11 01:15:35 +03006524 * \ingroup output
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006525 */
6526WL_EXPORT struct wl_listener *
6527weston_output_get_destroy_listener(struct weston_output *output,
6528 wl_notify_func_t notify)
6529{
6530 return wl_signal_get(&output->user_destroy_signal, notify);
6531}
6532
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006533/** Uninitialize an output
6534 *
6535 * Removes the output from the list of enabled outputs if necessary, but
6536 * does not call the backend's output disable function. The output will no
6537 * longer be in the list of pending outputs either.
6538 *
6539 * All fields of weston_output become uninitialized, i.e. should not be used
6540 * anymore. The caller can free the memory after this.
6541 *
Marius Vlad55d87362019-06-11 01:15:35 +03006542 * \ingroup ouput
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006543 * \internal
6544 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006545WL_EXPORT void
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03006546weston_output_release(struct weston_output *output)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006547{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006548 struct weston_head *head, *tmp;
6549
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006550 output->destroying = 1;
6551
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006552 wl_signal_emit(&output->user_destroy_signal, output);
6553
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03006554 if (output->idle_repaint_source)
6555 wl_event_source_remove(output->idle_repaint_source);
6556
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006557 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006558 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006559
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006560 pixman_region32_fini(&output->region);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006561 wl_list_remove(&output->link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006562
6563 wl_list_for_each_safe(head, tmp, &output->head_list, output_link)
6564 weston_head_detach(head);
6565
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006566 free(output->name);
6567}
6568
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006569/** Find an output by its given name
6570 *
6571 * \param compositor The compositor to search in.
6572 * \param name The output name to search for.
6573 * \return An existing output with the given name, or NULL if not found.
6574 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006575 * \ingroup compositor
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006576 */
6577WL_EXPORT struct weston_output *
6578weston_compositor_find_output_by_name(struct weston_compositor *compositor,
6579 const char *name)
6580{
6581 struct weston_output *output;
6582
6583 wl_list_for_each(output, &compositor->output_list, link)
6584 if (strcmp(output->name, name) == 0)
6585 return output;
6586
6587 wl_list_for_each(output, &compositor->pending_output_list, link)
6588 if (strcmp(output->name, name) == 0)
6589 return output;
6590
6591 return NULL;
6592}
6593
6594/** Create a named output
6595 *
6596 * \param compositor The compositor.
6597 * \param name The name for the output.
6598 * \return A new \c weston_output, or NULL on failure.
6599 *
6600 * This creates a new weston_output that starts with no heads attached.
6601 *
6602 * An output must be configured and it must have at least one head before
6603 * it can be enabled.
6604 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006605 * \ingroup compositor
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006606 */
6607WL_EXPORT struct weston_output *
6608weston_compositor_create_output(struct weston_compositor *compositor,
6609 const char *name)
6610{
6611 assert(compositor->backend->create_output);
6612
6613 if (weston_compositor_find_output_by_name(compositor, name)) {
6614 weston_log("Warning: attempted to create an output with a "
6615 "duplicate name '%s'.\n", name);
6616 return NULL;
6617 }
6618
6619 return compositor->backend->create_output(compositor, name);
6620}
6621
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006622/** Create an output for an unused head
6623 *
6624 * \param compositor The compositor.
6625 * \param head The head to attach to the output.
6626 * \return A new \c weston_output, or NULL on failure.
6627 *
6628 * This creates a new weston_output that starts with the given head attached.
6629 * The output inherits the name of the head. The head must not be already
6630 * attached to another output.
6631 *
6632 * An output must be configured before it can be enabled.
6633 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006634 * \ingroup compositor
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006635 */
6636WL_EXPORT struct weston_output *
6637weston_compositor_create_output_with_head(struct weston_compositor *compositor,
6638 struct weston_head *head)
6639{
6640 struct weston_output *output;
6641
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006642 output = weston_compositor_create_output(compositor, head->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006643 if (!output)
6644 return NULL;
6645
6646 if (weston_output_attach_head(output, head) < 0) {
Pekka Paalanen42c0e142017-10-27 12:07:49 +03006647 weston_output_destroy(output);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006648 return NULL;
6649 }
6650
6651 return output;
6652}
6653
6654/** Destroy an output
6655 *
6656 * \param output The output to destroy.
6657 *
6658 * The heads attached to the given output are detached and become unused again.
6659 *
6660 * It is not necessary to explicitly destroy all outputs at compositor exit.
Leandro Ribeiroca640d52020-01-27 19:12:01 -03006661 * weston_compositor_destroy() will automatically destroy any remaining
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006662 * outputs.
6663 *
Marius Vlad55d87362019-06-11 01:15:35 +03006664 * \ingroup ouput
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006665 */
6666WL_EXPORT void
6667weston_output_destroy(struct weston_output *output)
6668{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006669 output->destroy(output);
6670}
6671
Pekka Paalanencf0a4762017-04-04 16:36:07 +03006672/** When you need a head...
6673 *
6674 * This function is a hack, used until all code has been converted to become
6675 * multi-head aware.
6676 *
6677 * \param output The weston_output whose head to get.
6678 * \return The first head in the output's list.
Marius Vlad55d87362019-06-11 01:15:35 +03006679 *
6680 * \ingroup ouput
Pekka Paalanencf0a4762017-04-04 16:36:07 +03006681 */
6682WL_EXPORT struct weston_head *
6683weston_output_get_first_head(struct weston_output *output)
6684{
6685 if (wl_list_empty(&output->head_list))
6686 return NULL;
6687
6688 return container_of(output->head_list.next,
6689 struct weston_head, output_link);
6690}
6691
Ankit Nautiyal2844f8e2019-04-03 10:14:59 +05306692/** Allow/Disallow content-protection support for an output
6693 *
6694 * This function sets the allow_protection member for an output. Setting of
6695 * this field will allow the compositor to attempt content-protection for this
6696 * output, for a backend that supports the content-protection protocol.
6697 *
6698 * \param output The weston_output for whom the content-protection is to be
6699 * allowed.
6700 * \param allow_protection The bool value which is to be set.
6701 */
6702WL_EXPORT void
6703weston_output_allow_protection(struct weston_output *output,
6704 bool allow_protection)
6705{
6706 output->allow_protection = allow_protection;
6707}
6708
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01006709static void
Roman Gilge97391c2019-03-29 13:01:06 +01006710xdg_output_unlist(struct wl_resource *resource)
6711{
6712 wl_list_remove(wl_resource_get_link(resource));
6713}
6714
6715static void
6716xdg_output_destroy(struct wl_client *client, struct wl_resource *resource)
6717{
6718 wl_resource_destroy(resource);
6719}
6720
6721static const struct zxdg_output_v1_interface xdg_output_interface = {
6722 xdg_output_destroy
6723};
6724
6725static void
6726xdg_output_manager_destroy(struct wl_client *client,
6727 struct wl_resource *resource)
6728{
6729 wl_resource_destroy(resource);
6730}
6731
6732static void
6733xdg_output_manager_get_xdg_output(struct wl_client *client,
6734 struct wl_resource *manager,
6735 uint32_t id,
6736 struct wl_resource *output_resource)
6737{
6738 int version = wl_resource_get_version(manager);
6739 struct weston_head *head = wl_resource_get_user_data(output_resource);
6740 struct weston_output *output = head->output;
6741 struct wl_resource *resource;
6742
6743 resource = wl_resource_create(client, &zxdg_output_v1_interface,
6744 version, id);
6745 if (resource == NULL) {
6746 wl_client_post_no_memory(client);
6747 return;
6748 }
6749
6750 wl_list_insert(&head->xdg_output_resource_list,
6751 wl_resource_get_link(resource));
6752
6753 wl_resource_set_implementation(resource, &xdg_output_interface,
6754 NULL, xdg_output_unlist);
6755
6756 zxdg_output_v1_send_logical_position(resource, output->x, output->y);
6757 zxdg_output_v1_send_logical_size(resource,
6758 output->width,
6759 output->height);
6760 if (version >= ZXDG_OUTPUT_V1_NAME_SINCE_VERSION)
6761 zxdg_output_v1_send_name(resource, head->name);
6762
6763 zxdg_output_v1_send_done(resource);
6764}
6765
6766static const struct zxdg_output_manager_v1_interface xdg_output_manager_interface = {
6767 xdg_output_manager_destroy,
6768 xdg_output_manager_get_xdg_output
6769};
6770
6771static void
6772bind_xdg_output_manager(struct wl_client *client,
6773 void *data, uint32_t version, uint32_t id)
6774{
6775 struct wl_resource *resource;
6776
6777 resource = wl_resource_create(client, &zxdg_output_manager_v1_interface,
6778 version, id);
6779 if (resource == NULL) {
6780 wl_client_post_no_memory(client);
6781 return;
6782 }
6783
6784 wl_resource_set_implementation(resource, &xdg_output_manager_interface,
6785 NULL, NULL);
6786}
6787
6788static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006789destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006790{
Jonny Lamb74130762013-11-26 18:19:46 +01006791 struct weston_surface *surface =
6792 wl_resource_get_user_data(resource);
6793
Pekka Paalanen4826f872016-04-22 14:14:38 +03006794 if (!surface)
6795 return;
6796
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006797 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02006798 surface->pending.buffer_viewport.buffer.src_width =
6799 wl_fixed_from_int(-1);
6800 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006801 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006802}
6803
6804static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006805viewport_destroy(struct wl_client *client,
6806 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006807{
6808 wl_resource_destroy(resource);
6809}
6810
6811static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006812viewport_set_source(struct wl_client *client,
6813 struct wl_resource *resource,
6814 wl_fixed_t src_x,
6815 wl_fixed_t src_y,
6816 wl_fixed_t src_width,
6817 wl_fixed_t src_height)
6818{
6819 struct weston_surface *surface =
6820 wl_resource_get_user_data(resource);
6821
Pekka Paalanen4826f872016-04-22 14:14:38 +03006822 if (!surface) {
6823 wl_resource_post_error(resource,
6824 WP_VIEWPORT_ERROR_NO_SURFACE,
6825 "wl_surface for this viewport is no longer exists");
6826 return;
6827 }
6828
6829 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03006830 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006831
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006832 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03006833 src_height == wl_fixed_from_int(-1) &&
6834 src_x == wl_fixed_from_int(-1) &&
6835 src_y == wl_fixed_from_int(-1)) {
6836 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006837 surface->pending.buffer_viewport.buffer.src_width =
6838 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006839 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006840 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006841 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006842
Pekka Paalanen201769a2016-04-26 14:42:11 +03006843 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006844 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006845 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03006846 "wl_surface@%d viewport source "
6847 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
6848 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006849 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03006850 wl_fixed_to_double(src_height),
6851 wl_fixed_to_double(src_x),
6852 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006853 return;
6854 }
6855
6856 surface->pending.buffer_viewport.buffer.src_x = src_x;
6857 surface->pending.buffer_viewport.buffer.src_y = src_y;
6858 surface->pending.buffer_viewport.buffer.src_width = src_width;
6859 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006860 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006861}
6862
6863static void
6864viewport_set_destination(struct wl_client *client,
6865 struct wl_resource *resource,
6866 int32_t dst_width,
6867 int32_t dst_height)
6868{
6869 struct weston_surface *surface =
6870 wl_resource_get_user_data(resource);
6871
Pekka Paalanen4826f872016-04-22 14:14:38 +03006872 if (!surface) {
6873 wl_resource_post_error(resource,
6874 WP_VIEWPORT_ERROR_NO_SURFACE,
6875 "wl_surface for this viewport no longer exists");
6876 return;
6877 }
6878
6879 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006880
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006881 if (dst_width == -1 && dst_height == -1) {
6882 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006883 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006884 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006885 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006886 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006887
6888 if (dst_width <= 0 || dst_height <= 0) {
6889 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006890 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006891 "destination size must be positive (%dx%d)",
6892 dst_width, dst_height);
6893 return;
6894 }
6895
6896 surface->pending.buffer_viewport.surface.width = dst_width;
6897 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006898 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006899}
6900
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006901static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006902 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006903 viewport_set_source,
6904 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01006905};
6906
6907static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006908viewporter_destroy(struct wl_client *client,
6909 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006910{
6911 wl_resource_destroy(resource);
6912}
6913
6914static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006915viewporter_get_viewport(struct wl_client *client,
6916 struct wl_resource *viewporter,
6917 uint32_t id,
6918 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006919{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006920 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006921 struct weston_surface *surface =
6922 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006923 struct wl_resource *resource;
6924
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006925 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006926 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006927 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006928 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01006929 return;
6930 }
6931
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006932 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006933 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006934 if (resource == NULL) {
6935 wl_client_post_no_memory(client);
6936 return;
6937 }
6938
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006939 wl_resource_set_implementation(resource, &viewport_interface,
6940 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01006941
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006942 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006943}
6944
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006945static const struct wp_viewporter_interface viewporter_interface = {
6946 viewporter_destroy,
6947 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01006948};
6949
6950static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006951bind_viewporter(struct wl_client *client,
6952 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006953{
6954 struct wl_resource *resource;
6955
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006956 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006957 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006958 if (resource == NULL) {
6959 wl_client_post_no_memory(client);
6960 return;
6961 }
6962
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006963 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01006964 NULL, NULL);
6965}
6966
6967static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04006968destroy_presentation_feedback(struct wl_resource *feedback_resource)
6969{
6970 struct weston_presentation_feedback *feedback;
6971
6972 feedback = wl_resource_get_user_data(feedback_resource);
6973
6974 wl_list_remove(&feedback->link);
6975 free(feedback);
6976}
6977
6978static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006979presentation_destroy(struct wl_client *client, struct wl_resource *resource)
6980{
6981 wl_resource_destroy(resource);
6982}
6983
6984static void
6985presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04006986 struct wl_resource *presentation_resource,
6987 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006988 uint32_t callback)
6989{
Pekka Paalanen133e4392014-09-23 22:08:46 -04006990 struct weston_surface *surface;
6991 struct weston_presentation_feedback *feedback;
6992
6993 surface = wl_resource_get_user_data(surface_resource);
6994
Bryce Harringtonde16d892014-11-20 22:21:57 -08006995 feedback = zalloc(sizeof *feedback);
6996 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04006997 goto err_calloc;
6998
6999 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007000 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04007001 1, callback);
7002 if (!feedback->resource)
7003 goto err_create;
7004
7005 wl_resource_set_implementation(feedback->resource, NULL, feedback,
7006 destroy_presentation_feedback);
7007
7008 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
7009
7010 return;
7011
7012err_create:
7013 free(feedback);
7014
7015err_calloc:
7016 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007017}
7018
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007019static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007020 presentation_destroy,
7021 presentation_feedback
7022};
7023
7024static void
7025bind_presentation(struct wl_client *client,
7026 void *data, uint32_t version, uint32_t id)
7027{
7028 struct weston_compositor *compositor = data;
7029 struct wl_resource *resource;
7030
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007031 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06007032 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007033 if (resource == NULL) {
7034 wl_client_post_no_memory(client);
7035 return;
7036 }
7037
7038 wl_resource_set_implementation(resource, &presentation_implementation,
7039 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007040 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007041}
7042
7043static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05007044compositor_bind(struct wl_client *client,
7045 void *data, uint32_t version, uint32_t id)
7046{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007047 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05007048 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05007049
Jason Ekstranda85118c2013-06-27 20:17:02 -05007050 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06007051 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07007052 if (resource == NULL) {
7053 wl_client_post_no_memory(client);
7054 return;
7055 }
7056
7057 wl_resource_set_implementation(resource, &compositor_interface,
7058 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05007059}
7060
Daniel Stonece62cb32018-07-20 09:46:24 +01007061static const char *
7062output_repaint_status_text(struct weston_output *output)
7063{
7064 switch (output->repaint_status) {
7065 case REPAINT_NOT_SCHEDULED:
7066 return "no repaint";
7067 case REPAINT_BEGIN_FROM_IDLE:
7068 return "start_repaint_loop scheduled";
7069 case REPAINT_SCHEDULED:
7070 return "repaint scheduled";
7071 case REPAINT_AWAITING_COMPLETION:
7072 return "awaiting completion";
7073 }
7074
7075 assert(!"output_repaint_status_text missing enum");
7076 return NULL;
7077}
7078
7079static void
7080debug_scene_view_print_buffer(FILE *fp, struct weston_view *view)
7081{
7082 struct weston_buffer *buffer = view->surface->buffer_ref.buffer;
7083 struct wl_shm_buffer *shm;
7084 struct linux_dmabuf_buffer *dmabuf;
Marius Vlad00a6e012018-11-20 17:52:31 +02007085 const struct pixel_format_info *pixel_info = NULL;
Daniel Stonece62cb32018-07-20 09:46:24 +01007086
7087 if (!buffer) {
7088 fprintf(fp, "\t\t[buffer not available]\n");
7089 return;
7090 }
7091
7092 shm = wl_shm_buffer_get(buffer->resource);
7093 if (shm) {
Marius Vlad00a6e012018-11-20 17:52:31 +02007094 uint32_t _format = wl_shm_buffer_get_format(shm);
7095 pixel_info = pixel_format_get_info_shm(_format);
Daniel Stonece62cb32018-07-20 09:46:24 +01007096 fprintf(fp, "\t\tSHM buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02007097 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
7098 (unsigned long) _format,
7099 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Daniel Stonece62cb32018-07-20 09:46:24 +01007100 return;
7101 }
7102
7103 dmabuf = linux_dmabuf_buffer_get(buffer->resource);
7104 if (dmabuf) {
Marius Vlad00a6e012018-11-20 17:52:31 +02007105 pixel_info = pixel_format_get_info(dmabuf->attributes.format);
Daniel Stonece62cb32018-07-20 09:46:24 +01007106 fprintf(fp, "\t\tdmabuf buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02007107 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
7108 (unsigned long) dmabuf->attributes.format,
7109 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Daniel Stonece62cb32018-07-20 09:46:24 +01007110 fprintf(fp, "\t\t\tmodifier: 0x%llx\n",
7111 (unsigned long long) dmabuf->attributes.modifier[0]);
7112 return;
7113 }
7114
Marius Vlad253ba9a2019-03-17 18:22:21 +02007115 fprintf(fp, "\t\tEGL buffer\n");
Daniel Stonece62cb32018-07-20 09:46:24 +01007116}
7117
7118static void
7119debug_scene_view_print(FILE *fp, struct weston_view *view, int view_idx)
7120{
7121 struct weston_compositor *ec = view->surface->compositor;
7122 struct weston_output *output;
7123 char desc[512];
7124 pixman_box32_t *box;
7125 uint32_t surface_id = 0;
7126 pid_t pid = 0;
7127
7128 if (view->surface->resource) {
7129 struct wl_resource *resource = view->surface->resource;
7130 wl_client_get_credentials(wl_resource_get_client(resource),
7131 &pid, NULL, NULL);
7132 surface_id = wl_resource_get_id(view->surface->resource);
7133 }
7134
7135 if (!view->surface->get_label ||
7136 view->surface->get_label(view->surface, desc, sizeof(desc)) < 0) {
7137 strcpy(desc, "[no description available]");
7138 }
7139 fprintf(fp, "\tView %d (role %s, PID %d, surface ID %u, %s, %p):\n",
7140 view_idx, view->surface->role_name, pid, surface_id,
7141 desc, view);
7142
7143 box = pixman_region32_extents(&view->transform.boundingbox);
7144 fprintf(fp, "\t\tposition: (%d, %d) -> (%d, %d)\n",
7145 box->x1, box->y1, box->x2, box->y2);
7146 box = pixman_region32_extents(&view->transform.opaque);
7147
7148 if (pixman_region32_equal(&view->transform.opaque,
7149 &view->transform.boundingbox)) {
7150 fprintf(fp, "\t\t[fully opaque]\n");
7151 } else if (!pixman_region32_not_empty(&view->transform.opaque)) {
7152 fprintf(fp, "\t\t[not opaque]\n");
7153 } else {
7154 fprintf(fp, "\t\t[opaque: (%d, %d) -> (%d, %d)]\n",
7155 box->x1, box->y1, box->x2, box->y2);
7156 }
7157
7158 if (view->alpha < 1.0)
7159 fprintf(fp, "\t\talpha: %f\n", view->alpha);
7160
7161 if (view->output_mask != 0) {
7162 bool first_output = true;
7163 fprintf(fp, "\t\toutputs: ");
7164 wl_list_for_each(output, &ec->output_list, link) {
7165 if (!(view->output_mask & (1 << output->id)))
7166 continue;
7167 fprintf(fp, "%s%d (%s)%s",
7168 (first_output) ? "" : ", ",
7169 output->id, output->name,
7170 (view->output == output) ? " (primary)" : "");
7171 first_output = false;
7172 }
7173 } else {
7174 fprintf(fp, "\t\t[no outputs]");
7175 }
7176
7177 fprintf(fp, "\n");
7178
7179 debug_scene_view_print_buffer(fp, view);
7180}
7181
Marius Vlad433f4e72019-02-17 22:14:23 +02007182static void
7183debug_scene_view_print_tree(struct weston_view *view,
Marius Vlada6acfa82019-03-17 18:10:09 +02007184 FILE *fp, int *view_idx)
Marius Vlad433f4e72019-02-17 22:14:23 +02007185{
7186 struct weston_subsurface *sub;
7187 struct weston_view *ev;
7188
7189 /*
7190 * print the view first, then we recursively go on printing
7191 * sub-surfaces. We bail out once no more sub-surfaces are available.
7192 */
Marius Vlada6acfa82019-03-17 18:10:09 +02007193 debug_scene_view_print(fp, view, *view_idx);
Marius Vlad433f4e72019-02-17 22:14:23 +02007194
7195 /* no more sub-surfaces */
7196 if (wl_list_empty(&view->surface->subsurface_list))
7197 return;
7198
7199 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
7200 wl_list_for_each(ev, &sub->surface->views, surface_link) {
Michael Olbrichef5f3232020-04-29 09:03:15 +02007201 /* only print the child views of the current view */
7202 if (ev->parent_view != view)
Marius Vlad433f4e72019-02-17 22:14:23 +02007203 continue;
Marius Vlada6acfa82019-03-17 18:10:09 +02007204
7205 (*view_idx)++;
Marius Vlad433f4e72019-02-17 22:14:23 +02007206 debug_scene_view_print_tree(ev, fp, view_idx);
7207 }
7208 }
7209}
7210
Daniel Stonece62cb32018-07-20 09:46:24 +01007211/**
7212 * Output information on how libweston is currently composing the scene
7213 * graph.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007214 *
7215 * \ingroup compositor
Daniel Stonece62cb32018-07-20 09:46:24 +01007216 */
7217WL_EXPORT char *
7218weston_compositor_print_scene_graph(struct weston_compositor *ec)
7219{
7220 struct weston_output *output;
7221 struct weston_layer *layer;
7222 struct timespec now;
7223 int layer_idx = 0;
7224 FILE *fp;
7225 char *ret;
7226 size_t len;
7227 int err;
7228
7229 fp = open_memstream(&ret, &len);
7230 assert(fp);
7231
7232 weston_compositor_read_presentation_clock(ec, &now);
7233 fprintf(fp, "Weston scene graph at %ld.%09ld:\n\n",
7234 now.tv_sec, now.tv_nsec);
7235
7236 wl_list_for_each(output, &ec->output_list, link) {
7237 struct weston_head *head;
7238 int head_idx = 0;
7239
7240 fprintf(fp, "Output %d (%s):\n", output->id, output->name);
7241 assert(output->enabled);
7242
7243 fprintf(fp, "\tposition: (%d, %d) -> (%d, %d)\n",
7244 output->x, output->y,
7245 output->x + output->width,
7246 output->y + output->height);
7247 fprintf(fp, "\tmode: %dx%d@%.3fHz\n",
7248 output->current_mode->width,
7249 output->current_mode->height,
7250 output->current_mode->refresh / 1000.0);
7251 fprintf(fp, "\tscale: %d\n", output->scale);
7252
7253 fprintf(fp, "\trepaint status: %s\n",
7254 output_repaint_status_text(output));
7255 if (output->repaint_status == REPAINT_SCHEDULED)
7256 fprintf(fp, "\tnext repaint: %ld.%09ld\n",
7257 output->next_repaint.tv_sec,
7258 output->next_repaint.tv_nsec);
7259
7260 wl_list_for_each(head, &output->head_list, output_link) {
7261 fprintf(fp, "\tHead %d (%s): %sconnected\n",
7262 head_idx++, head->name,
7263 (head->connected) ? "" : "not ");
7264 }
7265 }
7266
7267 fprintf(fp, "\n");
7268
7269 wl_list_for_each(layer, &ec->layer_list, link) {
7270 struct weston_view *view;
7271 int view_idx = 0;
7272
7273 fprintf(fp, "Layer %d (pos 0x%lx):\n", layer_idx++,
7274 (unsigned long) layer->position);
7275
7276 if (!weston_layer_mask_is_infinite(layer)) {
7277 fprintf(fp, "\t[mask: (%d, %d) -> (%d,%d)]\n\n",
7278 layer->mask.x1, layer->mask.y1,
7279 layer->mask.x2, layer->mask.y2);
7280 }
7281
Marius Vlada6acfa82019-03-17 18:10:09 +02007282 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
7283 debug_scene_view_print_tree(view, fp, &view_idx);
7284 view_idx++;
7285 }
Daniel Stonece62cb32018-07-20 09:46:24 +01007286
7287 if (wl_list_empty(&layer->view_list.link))
7288 fprintf(fp, "\t[no views]\n");
7289
7290 fprintf(fp, "\n");
7291 }
7292
7293 err = fclose(fp);
7294 assert(err == 0);
7295
7296 return ret;
7297}
7298
7299/**
7300 * Called when the 'scene-graph' debug scope is bound by a client. This
7301 * one-shot weston-debug scope prints the current scene graph when bound,
7302 * and then terminates the stream.
7303 */
7304static void
Marius Vladdad882a2019-07-17 15:43:53 +03007305debug_scene_graph_cb(struct weston_log_subscription *sub, void *data)
Daniel Stonece62cb32018-07-20 09:46:24 +01007306{
7307 struct weston_compositor *ec = data;
7308 char *str = weston_compositor_print_scene_graph(ec);
7309
Marius Vladdad882a2019-07-17 15:43:53 +03007310 weston_log_subscription_printf(sub, "%s", str);
Daniel Stonece62cb32018-07-20 09:46:24 +01007311 free(str);
Marius Vladdad882a2019-07-17 15:43:53 +03007312 weston_log_subscription_complete(sub);
Daniel Stonece62cb32018-07-20 09:46:24 +01007313}
7314
Giulio Camuffo459137b2014-10-11 23:56:24 +03007315/** Create the compositor.
7316 *
7317 * This functions creates and initializes a compositor instance.
7318 *
7319 * \param display The Wayland display to be used.
7320 * \param user_data A pointer to an object that can later be retrieved
Marius Vlada2dace22019-06-12 16:05:44 +03007321 * \param log_ctx A pointer to weston_debug_compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007322 * using the \ref weston_compositor_get_user_data function.
7323 * \return The compositor instance on success or NULL on failure.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007324 *
7325 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007326 */
7327WL_EXPORT struct weston_compositor *
Marius Vlad880b4852019-04-07 17:07:58 +03007328weston_compositor_create(struct wl_display *display,
Marius Vlad3d7d9782019-04-17 12:35:38 +03007329 struct weston_log_context *log_ctx,
Marius Vlad880b4852019-04-07 17:07:58 +03007330 void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04007331{
Giulio Camuffo459137b2014-10-11 23:56:24 +03007332 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05007333 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07007334
Leandro Ribeirobd9c0a62020-01-17 10:47:49 -03007335 if (!log_ctx)
7336 return NULL;
7337
Giulio Camuffo459137b2014-10-11 23:56:24 +03007338 ec = zalloc(sizeof *ec);
7339 if (!ec)
7340 return NULL;
7341
Leandro Ribeirobd9c0a62020-01-17 10:47:49 -03007342 ec->weston_log_ctx = log_ctx;
Giulio Camuffo459137b2014-10-11 23:56:24 +03007343 ec->wl_display = display;
7344 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04007345 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07007346 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04007347 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03007348 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03007349 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02007350 wl_signal_init(&ec->idle_signal);
7351 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02007352 wl_signal_init(&ec->show_input_panel_signal);
7353 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02007354 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01007355 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01007356 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02007357 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02007358 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02007359 wl_signal_init(&ec->output_resized_signal);
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03007360 wl_signal_init(&ec->heads_changed_signal);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02007361 wl_signal_init(&ec->output_heads_changed_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07007362 wl_signal_init(&ec->session_signal);
Robert Beckettc569bdc2019-07-10 16:40:12 +01007363 ec->session_active = true;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04007364
Casey Dahlin58ba1372012-04-19 22:50:08 -04007365 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03007366 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04007367
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02007368 ec->activate_serial = 1;
7369
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05007370 ec->touch_mode = WESTON_TOUCH_MODE_NORMAL;
7371
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05307372 ec->content_protection = NULL;
7373
Derek Foreman152254b2015-11-26 14:17:48 -06007374 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04007375 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007376 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05007377
Giulio Camuffo954f1832014-10-11 18:27:30 +03007378 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04007379 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007380 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03007381
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007382 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007383 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007384 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01007385
Roman Gilge97391c2019-03-29 13:01:06 +01007386 if (!wl_global_create(ec->wl_display, &zxdg_output_manager_v1_interface, 2,
7387 ec, bind_xdg_output_manager))
7388 goto fail;
7389
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007390 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007391 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007392 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007393
Jonas Ådahl30d61d82014-10-22 21:21:17 +02007394 if (weston_input_init(ec) != 0)
7395 goto fail;
7396
Jason Ekstranda7af7042013-10-12 22:38:11 -05007397 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02007398 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007399 wl_list_init(&ec->layer_list);
7400 wl_list_init(&ec->seat_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007401 wl_list_init(&ec->pending_output_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007402 wl_list_init(&ec->output_list);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03007403 wl_list_init(&ec->head_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007404 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01007405 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007406 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01007407 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007408 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02007409 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007410
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007411 wl_list_init(&ec->plugin_api_list);
7412
Xiong Zhang97116532013-10-23 13:58:31 +08007413 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02007414 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04007415
Giulio Camuffo459137b2014-10-11 23:56:24 +03007416 wl_data_device_manager_init(ec->wl_display);
7417
7418 wl_display_init_shm(ec->wl_display);
7419
7420 loop = wl_display_get_event_loop(ec->wl_display);
7421 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Daniel Stone6847b852017-03-01 11:34:08 +00007422 ec->repaint_timer =
7423 wl_event_loop_add_timer(loop, output_repaint_timer_handler,
7424 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007425
Quentin Glidic82681572016-12-17 13:40:51 +01007426 weston_layer_init(&ec->fade_layer, ec);
7427 weston_layer_init(&ec->cursor_layer, ec);
7428
7429 weston_layer_set_position(&ec->fade_layer, WESTON_LAYER_POSITION_FADE);
7430 weston_layer_set_position(&ec->cursor_layer,
7431 WESTON_LAYER_POSITION_CURSOR);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007432
Daniel Stonece62cb32018-07-20 09:46:24 +01007433 ec->debug_scene =
Leandro Ribeiroce100192019-12-26 16:35:49 -03007434 weston_compositor_add_log_scope(ec, "scene-graph",
7435 "Scene graph details\n",
7436 debug_scene_graph_cb, NULL,
7437 ec);
Daniel Stonece62cb32018-07-20 09:46:24 +01007438
Marius Vladda104eb2019-09-05 14:31:01 +03007439 ec->timeline =
Leandro Ribeiroce100192019-12-26 16:35:49 -03007440 weston_compositor_add_log_scope(ec, "timeline",
7441 "Timeline event points\n",
7442 weston_timeline_create_subscription,
7443 weston_timeline_destroy_subscription,
7444 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007445 return ec;
7446
7447fail:
7448 free(ec);
7449 return NULL;
7450}
7451
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007452/** weston_compositor_shutdown
7453 * \ingroup compositor
7454 */
Benjamin Franzkeb8263022011-08-30 11:32:47 +02007455WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007456weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07007457{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007458 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07007459
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007460 wl_event_source_remove(ec->idle_source);
7461
Matt Roper361d2ad2011-08-29 13:52:23 -07007462 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02007463 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07007464 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02007465
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007466 /* Destroy all pending outputs associated with this compositor */
7467 wl_list_for_each_safe(output, next, &ec->pending_output_list, link)
7468 output->destroy(output);
7469
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02007470 if (ec->renderer)
7471 ec->renderer->destroy(ec);
7472
Daniel Stone325fc2d2012-05-30 16:31:58 +01007473 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09007474 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01007475 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01007476 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01007477 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02007478 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007479
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04007480 weston_plane_release(&ec->primary_plane);
Matt Roper361d2ad2011-08-29 13:52:23 -07007481}
7482
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007483/** weston_compositor_exit_with_code
7484 * \ingroup compositor
7485 */
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05007486WL_EXPORT void
Adam Jackson3c3f3b12019-10-16 16:02:59 -04007487weston_compositor_exit_with_code(struct weston_compositor *compositor,
Frederic Plourdec336f062014-10-29 14:44:33 -04007488 int exit_code)
7489{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02007490 if (compositor->exit_code == EXIT_SUCCESS)
7491 compositor->exit_code = exit_code;
7492
Giulio Camuffo459137b2014-10-11 23:56:24 +03007493 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04007494}
7495
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007496/** weston_compositor_set_default_pointer_grab
7497 * \ingroup compositor
7498 */
Frederic Plourdec336f062014-10-29 14:44:33 -04007499WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007500weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
7501 const struct weston_pointer_grab_interface *interface)
7502{
7503 struct weston_seat *seat;
7504
7505 ec->default_pointer_grab = interface;
7506 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05007507 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
7508
7509 if (pointer)
7510 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007511 }
7512}
7513
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007514/** weston_compositor_set_presentation_clock
7515 * \ingroup compositor
7516 */
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007517WL_EXPORT int
7518weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
7519 clockid_t clk_id)
7520{
7521 struct timespec ts;
7522
7523 if (clock_gettime(clk_id, &ts) < 0)
7524 return -1;
7525
7526 compositor->presentation_clock = clk_id;
7527
7528 return 0;
7529}
7530
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007531/** For choosing the software clock, when the display hardware or API
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007532 * does not expose a compatible presentation timestamp.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007533 *
7534 * \ingroup compositor
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007535 */
7536WL_EXPORT int
7537weston_compositor_set_presentation_clock_software(
7538 struct weston_compositor *compositor)
7539{
7540 /* In order of preference */
7541 static const clockid_t clocks[] = {
7542 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
7543 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
7544 CLOCK_MONOTONIC, /* no jumps, may crawl */
7545 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
7546 CLOCK_REALTIME /* may jump and crawl */
7547 };
7548 unsigned i;
7549
7550 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
7551 if (weston_compositor_set_presentation_clock(compositor,
7552 clocks[i]) == 0)
7553 return 0;
7554
7555 weston_log("Error: no suitable presentation clock available.\n");
7556
7557 return -1;
7558}
7559
Pekka Paalanen662f3842015-03-18 12:17:26 +02007560/** Read the current time from the Presentation clock
7561 *
7562 * \param compositor
Marius Vlada2dace22019-06-12 16:05:44 +03007563 * \param[out] ts The current time.
Pekka Paalanen662f3842015-03-18 12:17:26 +02007564 *
7565 * \note Reading the current time in user space is always imprecise to some
7566 * degree.
7567 *
7568 * This function is never meant to fail. If reading the clock does fail,
7569 * an error message is logged and a zero time is returned. Callers are not
7570 * supposed to detect or react to failures.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007571 *
7572 * \ingroup compositor
Pekka Paalanen662f3842015-03-18 12:17:26 +02007573 */
7574WL_EXPORT void
7575weston_compositor_read_presentation_clock(
7576 const struct weston_compositor *compositor,
7577 struct timespec *ts)
7578{
7579 static bool warned;
7580 int ret;
7581
7582 ret = clock_gettime(compositor->presentation_clock, ts);
7583 if (ret < 0) {
7584 ts->tv_sec = 0;
7585 ts->tv_nsec = 0;
7586
7587 if (!warned)
7588 weston_log("Error: failure to read "
Antonio Borneo39578632019-04-26 23:57:31 +02007589 "the presentation clock %#x: '%s' (%d)\n",
7590 compositor->presentation_clock,
7591 strerror(errno), errno);
Pekka Paalanen662f3842015-03-18 12:17:26 +02007592 warned = true;
7593 }
7594}
7595
Pekka Paalanen230f3b12014-09-29 14:18:40 -04007596/** Import dmabuf buffer into current renderer
7597 *
7598 * \param compositor
7599 * \param buffer the dmabuf buffer to import
7600 * \return true on usable buffers, false otherwise
7601 *
7602 * This function tests that the linux_dmabuf_buffer is usable
7603 * for the current renderer. Returns false on unusable buffers. Usually
7604 * usability is tested by importing the dmabufs for composition.
7605 *
7606 * This hook is also used for detecting if the renderer supports
7607 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
7608 * supported.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007609 *
7610 * \ingroup compositor
7611 */
Pekka Paalanen230f3b12014-09-29 14:18:40 -04007612WL_EXPORT bool
7613weston_compositor_import_dmabuf(struct weston_compositor *compositor,
7614 struct linux_dmabuf_buffer *buffer)
7615{
7616 struct weston_renderer *renderer;
7617
7618 renderer = compositor->renderer;
7619
7620 if (renderer->import_dmabuf == NULL)
7621 return false;
7622
7623 return renderer->import_dmabuf(compositor, buffer);
7624}
7625
Marius Vlad5a701542019-11-16 20:26:52 +02007626WL_EXPORT bool
7627weston_compositor_dmabuf_can_scanout(struct weston_compositor *compositor,
7628 struct linux_dmabuf_buffer *buffer)
7629{
7630 struct weston_backend *backend = compositor->backend;
7631
7632 if (backend->can_scanout_dmabuf == NULL)
7633 return false;
7634
7635 return backend->can_scanout_dmabuf(compositor, buffer);
7636}
7637
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007638WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05007639weston_version(int *major, int *minor, int *micro)
7640{
7641 *major = WESTON_VERSION_MAJOR;
7642 *minor = WESTON_VERSION_MINOR;
7643 *micro = WESTON_VERSION_MICRO;
7644}
7645
Daniel Stonee03c1112016-11-24 20:45:45 +00007646/**
7647 * Attempts to find a module path from the module map specified in the
7648 * environment. If found, writes the full path into the path variable.
7649 *
7650 * The module map is a string in environment variable WESTON_MODULE_MAP, where
7651 * each entry is of the form "name=path" and entries are separated by
7652 * semicolons. Whitespace is significant.
7653 *
7654 * \param name The name to search for.
7655 * \param path Where the path is written to if found.
7656 * \param path_len Allocated bytes at \c path .
7657 * \returns The length of the string written to path on success, or 0 if the
7658 * module was not specified in the environment map or path_len was too small.
7659 */
7660WL_EXPORT size_t
7661weston_module_path_from_env(const char *name, char *path, size_t path_len)
7662{
7663 const char *mapping = getenv("WESTON_MODULE_MAP");
7664 const char *end;
7665 const int name_len = strlen(name);
7666
7667 if (!mapping)
7668 return 0;
7669
7670 end = mapping + strlen(mapping);
7671 while (mapping < end && *mapping) {
7672 const char *filename, *next;
7673
7674 /* early out: impossibly short string */
7675 if (end - mapping < name_len + 1)
7676 return 0;
7677
7678 filename = &mapping[name_len + 1];
7679 next = strchrnul(mapping, ';');
7680
7681 if (strncmp(mapping, name, name_len) == 0 &&
7682 mapping[name_len] == '=') {
7683 size_t file_len = next - filename; /* no trailing NUL */
7684 if (file_len >= path_len)
7685 return 0;
7686 strncpy(path, filename, file_len);
7687 path[file_len] = '\0';
7688 return file_len;
7689 }
7690
7691 mapping = next + 1;
7692 }
7693
7694 return 0;
7695}
7696
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03007697WL_EXPORT void *
7698weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007699{
7700 char path[PATH_MAX];
7701 void *module, *init;
Daniel Stonebeb97e52016-11-28 12:13:54 +00007702 size_t len;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007703
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08007704 if (name == NULL)
7705 return NULL;
7706
Derek Foreman3f86e502015-06-08 11:46:54 -05007707 if (name[0] != '/') {
Daniel Stonee03c1112016-11-24 20:45:45 +00007708 len = weston_module_path_from_env(name, path, sizeof path);
7709 if (len == 0)
Daniel Stonebeb97e52016-11-28 12:13:54 +00007710 len = snprintf(path, sizeof path, "%s/%s",
7711 LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05007712 } else {
Daniel Stonebeb97e52016-11-28 12:13:54 +00007713 len = snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05007714 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007715
Daniel Stonebeb97e52016-11-28 12:13:54 +00007716 /* snprintf returns the length of the string it would've written,
7717 * _excluding_ the NUL byte. So even being equal to the size of
7718 * our buffer is an error here. */
7719 if (len >= sizeof path)
7720 return NULL;
7721
Kristian Høgsberga6813d22012-09-12 12:21:01 -04007722 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
7723 if (module) {
7724 weston_log("Module '%s' already loaded\n", path);
Pekka Paalanenf696ee92019-11-04 14:07:51 +02007725 } else {
7726 weston_log("Loading module '%s'\n", path);
7727 module = dlopen(path, RTLD_NOW);
7728 if (!module) {
7729 weston_log("Failed to load module: %s\n", dlerror());
7730 return NULL;
7731 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007732 }
7733
7734 init = dlsym(module, entrypoint);
7735 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03007736 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00007737 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007738 return NULL;
7739 }
7740
7741 return init;
7742}
7743
Pekka Paalanen6ffbba32019-11-06 12:59:32 +02007744/** Add a compositor destroy listener only once
7745 *
7746 * \param compositor The compositor whose destroy to watch for.
7747 * \param listener The listener struct to initialize.
7748 * \param destroy_handler The callback when compositor is destroyed.
7749 * \return True if listener is added, or false if there already is a listener
7750 * with the given \c destroy_handler.
7751 *
7752 * This function does nothing and returns false if the given callback function
7753 * is already present in the weston_compositor destroy callbacks list.
7754 * Otherwise, this function initializes the given listener with the given
7755 * callback pointer and adds it to the compositor's destroy callbacks list.
7756 *
7757 * This can be used to ensure that plugin initialization is done only once
7758 * in case the same plugin is loaded multiple times. If this function returns
7759 * false, the plugin should be already initialized successfully.
7760 *
7761 * All plugins should register a destroy listener for cleaning up. Note, that
7762 * the plugin destruction order is not guaranteed: plugins that depend on other
7763 * plugins must be able to be torn down in arbitrary order.
7764 *
Leandro Ribeiroca640d52020-01-27 19:12:01 -03007765 * \sa weston_compositor_destroy
Pekka Paalanen6ffbba32019-11-06 12:59:32 +02007766 */
7767WL_EXPORT bool
7768weston_compositor_add_destroy_listener_once(struct weston_compositor *compositor,
7769 struct wl_listener *listener,
7770 wl_notify_func_t destroy_handler)
7771{
7772 if (wl_signal_get(&compositor->destroy_signal, destroy_handler))
7773 return false;
7774
7775 listener->notify = destroy_handler;
7776 wl_signal_add(&compositor->destroy_signal, listener);
7777 return true;
7778}
Giulio Camuffo459137b2014-10-11 23:56:24 +03007779
Leandro Ribeiroca640d52020-01-27 19:12:01 -03007780/** Destroys the compositor.
Giulio Camuffo459137b2014-10-11 23:56:24 +03007781 *
Leandro Ribeiroca640d52020-01-27 19:12:01 -03007782 * This function cleans up the compositor state and then destroys it.
Giulio Camuffo459137b2014-10-11 23:56:24 +03007783 *
Leandro Ribeiroca640d52020-01-27 19:12:01 -03007784 * @param compositor The compositor to be destroyed.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007785 *
Marius Vlad284d5342019-06-24 12:00:47 +03007786 * @ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007787 */
7788WL_EXPORT void
Leandro Ribeiroca640d52020-01-27 19:12:01 -03007789weston_compositor_destroy(struct weston_compositor *compositor)
Giulio Camuffo459137b2014-10-11 23:56:24 +03007790{
7791 /* prevent further rendering while shutting down */
7792 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
7793
7794 wl_signal_emit(&compositor->destroy_signal, compositor);
7795
7796 weston_compositor_xkb_destroy(compositor);
7797
Giulio Camuffo2d24e642015-10-03 16:25:15 +03007798 if (compositor->backend)
7799 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007800
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03007801 /* The backend is responsible for destroying the heads. */
7802 assert(wl_list_empty(&compositor->head_list));
7803
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007804 weston_plugin_api_destroy_list(compositor);
7805
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03007806 if (compositor->heads_changed_source)
7807 wl_event_source_remove(compositor->heads_changed_source);
7808
Leandro Ribeirof0149642019-12-18 15:52:18 -03007809 weston_log_scope_destroy(compositor->debug_scene);
Daniel Stonece62cb32018-07-20 09:46:24 +01007810 compositor->debug_scene = NULL;
Marius Vladda104eb2019-09-05 14:31:01 +03007811
Leandro Ribeirof0149642019-12-18 15:52:18 -03007812 weston_log_scope_destroy(compositor->timeline);
Marius Vladda104eb2019-09-05 14:31:01 +03007813 compositor->timeline = NULL;
Pekka Paalanena5630ea2017-10-12 13:13:42 +02007814
Giulio Camuffo459137b2014-10-11 23:56:24 +03007815 free(compositor);
7816}
7817
7818/** Instruct the compositor to exit.
7819 *
7820 * This functions does not directly destroy the compositor object, it merely
7821 * command it to start the tear down process. It is not guaranteed that the
7822 * tear down will happen immediately.
7823 *
7824 * \param compositor The compositor to tear down.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007825 *
7826 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007827 */
7828WL_EXPORT void
7829weston_compositor_exit(struct weston_compositor *compositor)
7830{
7831 compositor->exit(compositor);
7832}
7833
7834/** Return the user data stored in the compositor.
7835 *
7836 * This function returns the user data pointer set with user_data parameter
7837 * to the \ref weston_compositor_create function.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007838 *
7839 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007840 */
7841WL_EXPORT void *
7842weston_compositor_get_user_data(struct weston_compositor *compositor)
7843{
7844 return compositor->user_data;
7845}
Pekka Paalanendd186732016-06-03 14:49:54 +03007846
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007847static const char * const backend_map[] = {
7848 [WESTON_BACKEND_DRM] = "drm-backend.so",
7849 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
7850 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
7851 [WESTON_BACKEND_RDP] = "rdp-backend.so",
7852 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
7853 [WESTON_BACKEND_X11] = "x11-backend.so",
7854};
7855
Pekka Paalanendd186732016-06-03 14:49:54 +03007856/** Load a backend into a weston_compositor
7857 *
7858 * A backend must be loaded to make a weston_compositor work. A backend
7859 * provides input and output capabilities, and determines the renderer to use.
7860 *
7861 * \param compositor A compositor that has not had a backend loaded yet.
7862 * \param backend Name of the backend file.
7863 * \param config_base A pointer to a backend-specific configuration
7864 * structure's 'base' member.
7865 *
7866 * \return 0 on success, or -1 on error.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007867 *
7868 * \ingroup compositor
Pekka Paalanendd186732016-06-03 14:49:54 +03007869 */
7870WL_EXPORT int
7871weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007872 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03007873 struct weston_backend_config *config_base)
7874{
7875 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03007876 struct weston_backend_config *config_base);
7877
Pekka Paalanend7e35112017-08-29 17:04:12 +03007878 if (compositor->backend) {
7879 weston_log("Error: attempt to load a backend when one is already loaded\n");
7880 return -1;
7881 }
7882
Quentin Glidic887c0182016-07-10 11:00:53 +02007883 if (backend >= ARRAY_LENGTH(backend_map))
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007884 return -1;
7885
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01007886 backend_init = weston_load_module(backend_map[backend], "weston_backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03007887 if (!backend_init)
7888 return -1;
7889
Pekka Paalanend7e35112017-08-29 17:04:12 +03007890 if (backend_init(compositor, config_base) < 0) {
7891 compositor->backend = NULL;
7892 return -1;
7893 }
7894
7895 return 0;
Pekka Paalanendd186732016-06-03 14:49:54 +03007896}
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007897
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007898/** weston_compositor_load_xwayland
7899 * \ingroup compositor
7900 */
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007901WL_EXPORT int
7902weston_compositor_load_xwayland(struct weston_compositor *compositor)
7903{
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007904 int (*module_init)(struct weston_compositor *ec);
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007905
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007906 module_init = weston_load_module("xwayland.so", "weston_module_init");
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007907 if (!module_init)
7908 return -1;
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007909 if (module_init(compositor) < 0)
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007910 return -1;
7911 return 0;
7912}
Vasilis Liaskovitis486b4632018-10-10 16:14:55 +02007913
7914/** Resolve an internal compositor error by disconnecting the client.
7915 *
7916 * This function is used in cases when the wl_buffer turns out
7917 * unusable and there is no fallback path.
7918 *
7919 * It is possible the fault is caused by a compositor bug, the underlying
7920 * graphics stack bug or normal behaviour, or perhaps a client mistake.
7921 * In any case, the options are to either composite garbage or nothing,
7922 * or disconnect the client. This is a helper function for the latter.
7923 *
7924 * The error is sent as an INVALID_OBJECT error on the client's wl_display.
7925 *
7926 * \param buffer The weston buffer that is unusable.
7927 * \param msg A custom error message attached to the protocol error.
7928 */
7929WL_EXPORT void
7930weston_buffer_send_server_error(struct weston_buffer *buffer,
7931 const char *msg)
7932{
7933 struct wl_client *client;
7934 struct wl_resource *display_resource;
7935 uint32_t id;
7936
7937 assert(buffer->resource);
7938 id = wl_resource_get_id(buffer->resource);
7939 client = wl_resource_get_client(buffer->resource);
7940 display_resource = wl_client_get_object(client, 1);
7941
7942 assert(display_resource);
7943 wl_resource_post_error(display_resource,
7944 WL_DISPLAY_ERROR_INVALID_OBJECT,
7945 "server error with "
7946 "wl_buffer@%u: %s", id, msg);
7947}
Ankit Nautiyal93dde242019-07-08 11:46:42 +05307948
7949WL_EXPORT void
7950weston_output_disable_planes_incr(struct weston_output *output)
7951{
7952 output->disable_planes++;
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05307953 /*
7954 * If disable_planes changes from 0 to non-zero, it means some type of
7955 * recording of content has started, and therefore protection level of
7956 * the protected surfaces must be updated to avoid the recording of
7957 * the protected content.
7958 */
7959 if (output->disable_planes == 1)
7960 weston_schedule_surface_protection_update(output->compositor);
Ankit Nautiyal93dde242019-07-08 11:46:42 +05307961}
7962
7963WL_EXPORT void
7964weston_output_disable_planes_decr(struct weston_output *output)
7965{
7966 output->disable_planes--;
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05307967 /*
7968 * If disable_planes changes from non-zero to 0, it means no content
7969 * recording is going on any more, and the protected and surfaces can be
7970 * shown without any apprehensions about content being recorded.
7971 */
7972 if (output->disable_planes == 0)
7973 weston_schedule_surface_protection_update(output->compositor);
7974
Ankit Nautiyal93dde242019-07-08 11:46:42 +05307975}