blob: de6931ae990eef3fd9e35b55cdd09629993c2502 [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
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500611WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200612weston_transformed_coord(int width, int height,
613 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200614 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200615 float sx, float sy, float *bx, float *by)
616{
617 switch (transform) {
618 case WL_OUTPUT_TRANSFORM_NORMAL:
619 default:
620 *bx = sx;
621 *by = sy;
622 break;
623 case WL_OUTPUT_TRANSFORM_FLIPPED:
624 *bx = width - sx;
625 *by = sy;
626 break;
627 case WL_OUTPUT_TRANSFORM_90:
628 *bx = height - sy;
629 *by = sx;
630 break;
631 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
632 *bx = height - sy;
633 *by = width - sx;
634 break;
635 case WL_OUTPUT_TRANSFORM_180:
636 *bx = width - sx;
637 *by = height - sy;
638 break;
639 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
640 *bx = sx;
641 *by = height - sy;
642 break;
643 case WL_OUTPUT_TRANSFORM_270:
644 *bx = sy;
645 *by = width - sx;
646 break;
647 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
648 *bx = sy;
649 *by = sx;
650 break;
651 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200652
653 *bx *= scale;
654 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200655}
656
657WL_EXPORT pixman_box32_t
658weston_transformed_rect(int width, int height,
659 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200660 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200661 pixman_box32_t rect)
662{
663 float x1, x2, y1, y2;
664
665 pixman_box32_t ret;
666
Alexander Larsson4ea95522013-05-22 14:41:37 +0200667 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200668 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200669 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200670 rect.x2, rect.y2, &x2, &y2);
671
672 if (x1 <= x2) {
673 ret.x1 = x1;
674 ret.x2 = x2;
675 } else {
676 ret.x1 = x2;
677 ret.x2 = x1;
678 }
679
680 if (y1 <= y2) {
681 ret.y1 = y1;
682 ret.y2 = y2;
683 } else {
684 ret.y1 = y2;
685 ret.y2 = y1;
686 }
687
688 return ret;
689}
690
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600691/** Transform a region by a matrix, restricted to axis-aligned transformations
692 *
693 * Warning: This function does not work for projective, affine, or matrices
694 * that encode arbitrary rotations. Only 90-degree step rotations are
695 * supported.
696 */
697WL_EXPORT void
698weston_matrix_transform_region(pixman_region32_t *dest,
699 struct weston_matrix *matrix,
700 pixman_region32_t *src)
701{
702 pixman_box32_t *src_rects, *dest_rects;
703 int nrects, i;
704
705 src_rects = pixman_region32_rectangles(src, &nrects);
706 dest_rects = malloc(nrects * sizeof(*dest_rects));
707 if (!dest_rects)
708 return;
709
710 for (i = 0; i < nrects; i++) {
711 struct weston_vector vec1 = {{
712 src_rects[i].x1, src_rects[i].y1, 0, 1
713 }};
714 weston_matrix_transform(matrix, &vec1);
715 vec1.f[0] /= vec1.f[3];
716 vec1.f[1] /= vec1.f[3];
717
718 struct weston_vector vec2 = {{
719 src_rects[i].x2, src_rects[i].y2, 0, 1
720 }};
721 weston_matrix_transform(matrix, &vec2);
722 vec2.f[0] /= vec2.f[3];
723 vec2.f[1] /= vec2.f[3];
724
725 if (vec1.f[0] < vec2.f[0]) {
726 dest_rects[i].x1 = floor(vec1.f[0]);
727 dest_rects[i].x2 = ceil(vec2.f[0]);
728 } else {
729 dest_rects[i].x1 = floor(vec2.f[0]);
730 dest_rects[i].x2 = ceil(vec1.f[0]);
731 }
732
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600733 if (vec1.f[1] < vec2.f[1]) {
734 dest_rects[i].y1 = floor(vec1.f[1]);
735 dest_rects[i].y2 = ceil(vec2.f[1]);
736 } else {
737 dest_rects[i].y1 = floor(vec2.f[1]);
738 dest_rects[i].y2 = ceil(vec1.f[1]);
739 }
740 }
741
742 pixman_region32_clear(dest);
743 pixman_region32_init_rects(dest, dest_rects, nrects);
744 free(dest_rects);
745}
746
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200747WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500748weston_transformed_region(int width, int height,
749 enum wl_output_transform transform,
750 int32_t scale,
751 pixman_region32_t *src, pixman_region32_t *dest)
752{
753 pixman_box32_t *src_rects, *dest_rects;
754 int nrects, i;
755
756 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
757 if (src != dest)
758 pixman_region32_copy(dest, src);
759 return;
760 }
761
762 src_rects = pixman_region32_rectangles(src, &nrects);
763 dest_rects = malloc(nrects * sizeof(*dest_rects));
764 if (!dest_rects)
765 return;
766
767 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
768 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
769 } else {
770 for (i = 0; i < nrects; i++) {
771 switch (transform) {
772 default:
773 case WL_OUTPUT_TRANSFORM_NORMAL:
774 dest_rects[i].x1 = src_rects[i].x1;
775 dest_rects[i].y1 = src_rects[i].y1;
776 dest_rects[i].x2 = src_rects[i].x2;
777 dest_rects[i].y2 = src_rects[i].y2;
778 break;
779 case WL_OUTPUT_TRANSFORM_90:
780 dest_rects[i].x1 = height - src_rects[i].y2;
781 dest_rects[i].y1 = src_rects[i].x1;
782 dest_rects[i].x2 = height - src_rects[i].y1;
783 dest_rects[i].y2 = src_rects[i].x2;
784 break;
785 case WL_OUTPUT_TRANSFORM_180:
786 dest_rects[i].x1 = width - src_rects[i].x2;
787 dest_rects[i].y1 = height - src_rects[i].y2;
788 dest_rects[i].x2 = width - src_rects[i].x1;
789 dest_rects[i].y2 = height - src_rects[i].y1;
790 break;
791 case WL_OUTPUT_TRANSFORM_270:
792 dest_rects[i].x1 = src_rects[i].y1;
793 dest_rects[i].y1 = width - src_rects[i].x2;
794 dest_rects[i].x2 = src_rects[i].y2;
795 dest_rects[i].y2 = width - src_rects[i].x1;
796 break;
797 case WL_OUTPUT_TRANSFORM_FLIPPED:
798 dest_rects[i].x1 = width - src_rects[i].x2;
799 dest_rects[i].y1 = src_rects[i].y1;
800 dest_rects[i].x2 = width - src_rects[i].x1;
801 dest_rects[i].y2 = src_rects[i].y2;
802 break;
803 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
804 dest_rects[i].x1 = height - src_rects[i].y2;
805 dest_rects[i].y1 = width - src_rects[i].x2;
806 dest_rects[i].x2 = height - src_rects[i].y1;
807 dest_rects[i].y2 = width - src_rects[i].x1;
808 break;
809 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
810 dest_rects[i].x1 = src_rects[i].x1;
811 dest_rects[i].y1 = height - src_rects[i].y2;
812 dest_rects[i].x2 = src_rects[i].x2;
813 dest_rects[i].y2 = height - src_rects[i].y1;
814 break;
815 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
816 dest_rects[i].x1 = src_rects[i].y1;
817 dest_rects[i].y1 = src_rects[i].x1;
818 dest_rects[i].x2 = src_rects[i].y2;
819 dest_rects[i].y2 = src_rects[i].x2;
820 break;
821 }
822 }
823 }
824
825 if (scale != 1) {
826 for (i = 0; i < nrects; i++) {
827 dest_rects[i].x1 *= scale;
828 dest_rects[i].x2 *= scale;
829 dest_rects[i].y1 *= scale;
830 dest_rects[i].y2 *= scale;
831 }
832 }
833
834 pixman_region32_clear(dest);
835 pixman_region32_init_rects(dest, dest_rects, nrects);
836 free(dest_rects);
837}
838
Jonny Lamb74130762013-11-26 18:19:46 +0100839static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300840viewport_surface_to_buffer(struct weston_surface *surface,
841 float sx, float sy, float *bx, float *by)
Jonny Lamb74130762013-11-26 18:19:46 +0100842{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200843 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200844 double src_width, src_height;
845 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200846
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200847 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
848 if (vp->surface.width == -1) {
849 *bx = sx;
850 *by = sy;
851 return;
852 }
Jonny Lamb74130762013-11-26 18:19:46 +0100853
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200854 src_x = 0.0;
855 src_y = 0.0;
856 src_width = surface->width_from_buffer;
857 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100858 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200859 src_x = wl_fixed_to_double(vp->buffer.src_x);
860 src_y = wl_fixed_to_double(vp->buffer.src_y);
861 src_width = wl_fixed_to_double(vp->buffer.src_width);
862 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100863 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200864
865 *bx = sx * src_width / surface->width + src_x;
866 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100867}
868
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500869WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200870weston_surface_to_buffer_float(struct weston_surface *surface,
871 float sx, float sy, float *bx, float *by)
872{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200873 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
874
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300875 /* first transform coordinates if the viewport is set */
876 viewport_surface_to_buffer(surface, sx, sy, bx, by);
Jonny Lamb74130762013-11-26 18:19:46 +0100877
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500878 weston_transformed_coord(surface->width_from_buffer,
879 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200880 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100881 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200882}
883
Derek Foreman99739672015-12-03 16:38:11 -0600884/** Transform a rectangle from surface coordinates to buffer coordinates
885 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300886 * \param surface The surface to fetch wp_viewport and buffer transformation
Derek Foreman99739672015-12-03 16:38:11 -0600887 * from.
888 * \param rect The rectangle to transform.
889 * \return The transformed rectangle.
890 *
891 * Viewport and buffer transformations can only do translation, scaling,
892 * and rotations in 90-degree steps. Therefore the only loss in the
893 * conversion is coordinate rounding.
894 *
895 * However, some coordinate rounding takes place as an intermediate
896 * step before the buffer scale factor is applied, so the rectangle
897 * boundary may not be exactly as expected.
898 *
899 * This is OK for damage tracking since a little extra coverage is
900 * not a problem.
901 */
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200902WL_EXPORT pixman_box32_t
903weston_surface_to_buffer_rect(struct weston_surface *surface,
904 pixman_box32_t rect)
905{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200906 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100907 float xf, yf;
908
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300909 /* first transform box coordinates if the viewport is set */
910 viewport_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100911 rect.x1 = floorf(xf);
912 rect.y1 = floorf(yf);
913
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300914 viewport_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
Derek Foremane2e15ac2015-12-01 13:00:43 -0600915 rect.x2 = ceilf(xf);
916 rect.y2 = ceilf(yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100917
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500918 return weston_transformed_rect(surface->width_from_buffer,
919 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200920 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200921 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200922}
923
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200924/** Transform a region from surface coordinates to buffer coordinates
925 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300926 * \param surface The surface to fetch wp_viewport and buffer transformation
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200927 * from.
Marius Vlada2dace22019-06-12 16:05:44 +0300928 * \param[in] surface_region The region in surface coordinates.
929 * \param[out] buffer_region The region converted to buffer coordinates.
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200930 *
931 * Buffer_region must be init'd, but will be completely overwritten.
932 *
933 * Viewport and buffer transformations can only do translation, scaling,
934 * and rotations in 90-degree steps. Therefore the only loss in the
Derek Foreman99739672015-12-03 16:38:11 -0600935 * conversion is from the coordinate rounding that takes place in
936 * \ref weston_surface_to_buffer_rect.
Marius Vlada2dace22019-06-12 16:05:44 +0300937 *
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200938 */
939WL_EXPORT void
940weston_surface_to_buffer_region(struct weston_surface *surface,
941 pixman_region32_t *surface_region,
942 pixman_region32_t *buffer_region)
943{
944 pixman_box32_t *src_rects, *dest_rects;
945 int nrects, i;
946
947 src_rects = pixman_region32_rectangles(surface_region, &nrects);
948 dest_rects = malloc(nrects * sizeof(*dest_rects));
949 if (!dest_rects)
950 return;
951
952 for (i = 0; i < nrects; i++) {
953 dest_rects[i] = weston_surface_to_buffer_rect(surface,
954 src_rects[i]);
955 }
956
957 pixman_region32_fini(buffer_region);
958 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
959 free(dest_rects);
960}
961
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200962WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500963weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400964 struct weston_plane *plane)
965{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500966 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400967 return;
968
Jason Ekstranda7af7042013-10-12 22:38:11 -0500969 weston_view_damage_below(view);
970 view->plane = plane;
971 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400972}
973
Pekka Paalanen51723d52015-02-17 13:10:01 +0200974/** Inflict damage on the plane where the view is visible.
975 *
976 * \param view The view that causes the damage.
977 *
978 * If the view is currently on a plane (including the primary plane),
979 * take the view's boundingbox, subtract all the opaque views that cover it,
980 * and add the remaining region as damage to the plane. This corresponds
981 * to the damage inflicted to the plane if this view disappeared.
982 *
983 * A repaint is scheduled for this view.
984 *
985 * The region of all opaque views covering this view is stored in
986 * weston_view::clip and updated by view_accumulate_damage() during
987 * weston_output_repaint(). Specifically, that region matches the
988 * scenegraph as it was last painted.
989 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400990WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500991weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200992{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500993 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200994
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500995 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +0200996 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500997 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800998 if (view->plane)
999 pixman_region32_union(&view->plane->damage,
1000 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001001 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -08001002 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001003}
1004
Pekka Paalanen01e00682017-03-24 16:21:06 +02001005/** Send wl_surface.enter/leave events
1006 *
1007 * \param surface The surface.
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03001008 * \param head A head of the entered/left output.
Pekka Paalanen01e00682017-03-24 16:21:06 +02001009 * \param enter True if entered.
Marius Vlada2dace22019-06-12 16:05:44 +03001010 * \param leave True if left.
Pekka Paalanen01e00682017-03-24 16:21:06 +02001011 *
1012 * Send the enter/leave events for all protocol objects bound to the given
1013 * output by the client owning the surface.
1014 */
1015static void
1016weston_surface_send_enter_leave(struct weston_surface *surface,
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03001017 struct weston_head *head,
Pekka Paalanen01e00682017-03-24 16:21:06 +02001018 bool enter,
1019 bool leave)
1020{
1021 struct wl_resource *wloutput;
1022 struct wl_client *client;
1023
1024 assert(enter != leave);
1025
1026 client = wl_resource_get_client(surface->resource);
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03001027 wl_resource_for_each(wloutput, &head->resource_list) {
Pekka Paalanen01e00682017-03-24 16:21:06 +02001028 if (wl_resource_get_client(wloutput) != client)
1029 continue;
1030
1031 if (enter)
1032 wl_surface_send_enter(surface->resource, wloutput);
1033 if (leave)
1034 wl_surface_send_leave(surface->resource, wloutput);
1035 }
1036}
1037
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301038static void
1039weston_surface_compute_protection(struct protected_surface *psurface)
1040{
1041 enum weston_hdcp_protection min_protection;
1042 bool min_protection_valid = false;
1043 struct weston_surface *surface = psurface->surface;
1044 struct weston_output *output;
1045
1046 wl_list_for_each(output, &surface->compositor->output_list, link)
1047 if (surface->output_mask & (1u << output->id)) {
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05301048 /*
1049 * If the content-protection is enabled with protection
1050 * mode as RELAXED for a surface, and if
1051 * content-recording features like: screen-shooter,
1052 * recorder, screen-sharing, etc are on, then notify the
1053 * client, that the protection is disabled.
1054 *
1055 * Note: If the protection mode is ENFORCED then there
1056 * is no need to bother the client as the renderer takes
1057 * care of censoring the visibility of the protected
1058 * content.
1059 */
1060
1061 if (output->disable_planes > 0 &&
1062 surface->protection_mode == WESTON_SURFACE_PROTECTION_MODE_RELAXED) {
1063 min_protection = WESTON_HDCP_DISABLE;
1064 min_protection_valid = true;
1065 break;
1066 }
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301067 if (!min_protection_valid) {
1068 min_protection = output->current_protection;
1069 min_protection_valid = true;
1070 }
1071 if (output->current_protection < min_protection)
1072 min_protection = output->current_protection;
1073 }
1074 if (!min_protection_valid)
1075 min_protection = WESTON_HDCP_DISABLE;
1076
1077 surface->current_protection = min_protection;
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05301078
1079 weston_protected_surface_send_event(psurface, surface->current_protection);
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301080}
1081
1082static void
1083notify_surface_protection_change(void *data)
1084{
1085 struct weston_compositor *compositor = data;
1086 struct content_protection *cp;
1087 struct protected_surface *psurface;
1088
1089 cp = compositor->content_protection;
1090 cp->surface_protection_update = NULL;
1091
1092 /* Notify the clients, whose surfaces are changed */
1093 wl_list_for_each(psurface, &cp->protected_list, link)
1094 if (psurface && psurface->surface)
1095 weston_surface_compute_protection(psurface);
1096}
1097
Bryce Harrington3f650b82015-12-23 11:01:58 -08001098/**
Ankit Nautiyalfaa5ab42019-07-08 13:23:24 +05301099 * \param compositor weston_compositor
1100 *
1101 * Schedule an idle task to notify surface about the update in protection,
1102 * if not already scheduled.
1103 */
1104static void
1105weston_schedule_surface_protection_update(struct weston_compositor *compositor)
1106{
1107 struct content_protection *cp = compositor->content_protection;
1108 struct wl_event_loop *loop;
1109
1110 if (!cp || cp->surface_protection_update)
1111 return;
1112 loop = wl_display_get_event_loop(compositor->wl_display);
1113 cp->surface_protection_update = wl_event_loop_add_idle(loop,
1114 notify_surface_protection_change,
1115 compositor);
1116}
1117
1118/**
Bryce Harrington3f650b82015-12-23 11:01:58 -08001119 * \param es The surface
1120 * \param mask The new set of outputs for the surface
1121 *
1122 * Sets the surface's set of outputs to the ones specified by
1123 * the new output mask provided. Identifies the outputs that
1124 * have changed, the posts enter and leave events for these
1125 * outputs as appropriate.
1126 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001127static void
1128weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
1129{
1130 uint32_t different = es->output_mask ^ mask;
1131 uint32_t entered = mask & different;
1132 uint32_t left = es->output_mask & different;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001133 uint32_t output_bit;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001134 struct weston_output *output;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001135 struct weston_head *head;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001136
1137 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001138 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001139 return;
1140 if (different == 0)
1141 return;
1142
1143 wl_list_for_each(output, &es->compositor->output_list, link) {
Pekka Paalanen01e00682017-03-24 16:21:06 +02001144 output_bit = 1u << output->id;
1145 if (!(output_bit & different))
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001146 continue;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001147
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001148 wl_list_for_each(head, &output->head_list, output_link) {
1149 weston_surface_send_enter_leave(es, head,
1150 output_bit & entered,
1151 output_bit & left);
1152 }
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001153 }
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301154 /*
1155 * Change in surfaces' output mask might trigger a change in its
1156 * protection.
1157 */
Ankit Nautiyalfaa5ab42019-07-08 13:23:24 +05301158 weston_schedule_surface_protection_update(es->compositor);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001159}
1160
Semi Malinene7a52fb2018-04-26 11:08:10 +02001161static void
1162notify_view_output_destroy(struct wl_listener *listener, void *data)
1163{
1164 struct weston_view *view =
1165 container_of(listener,
1166 struct weston_view, output_destroy_listener);
1167
1168 view->output = NULL;
1169 view->output_destroy_listener.notify = NULL;
1170}
1171
1172/** Set the primary output of the view
1173 *
1174 * \param view The view whose primary output to set
1175 * \param output The new primary output for the view
1176 *
1177 * Set \a output to be the primary output of the \a view.
1178 *
1179 * Notice that the assignment may be temporary; the primary output could be
1180 * automatically changed. Hence, one cannot rely on the value persisting.
1181 *
1182 * Passing NULL as /a output will set the primary output to NULL.
1183 */
1184WL_EXPORT void
1185weston_view_set_output(struct weston_view *view, struct weston_output *output)
1186{
1187 if (view->output_destroy_listener.notify) {
1188 wl_list_remove(&view->output_destroy_listener.link);
1189 view->output_destroy_listener.notify = NULL;
1190 }
1191 view->output = output;
1192 if (output) {
1193 view->output_destroy_listener.notify =
1194 notify_view_output_destroy;
1195 wl_signal_add(&output->destroy_signal,
1196 &view->output_destroy_listener);
1197 }
1198}
1199
Bryce Harrington3f650b82015-12-23 11:01:58 -08001200/** Recalculate which output(s) the surface has views displayed on
1201 *
1202 * \param es The surface to remap to outputs
1203 *
1204 * Finds the output that is showing the largest amount of one
1205 * of the surface's various views. This output becomes the
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001206 * surface's primary output for vsync and frame callback purposes.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001207 *
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001208 * Also notes all outputs of all of the surface's views
Bryce Harrington3f650b82015-12-23 11:01:58 -08001209 * in the output_mask for the surface.
1210 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001211static void
1212weston_surface_assign_output(struct weston_surface *es)
1213{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001214 struct weston_output *new_output;
1215 struct weston_view *view;
1216 pixman_region32_t region;
1217 uint32_t max, area, mask;
1218 pixman_box32_t *e;
1219
1220 new_output = NULL;
1221 max = 0;
1222 mask = 0;
1223 pixman_region32_init(&region);
1224 wl_list_for_each(view, &es->views, surface_link) {
1225 if (!view->output)
1226 continue;
1227
1228 pixman_region32_intersect(&region, &view->transform.boundingbox,
1229 &view->output->region);
1230
1231 e = pixman_region32_extents(&region);
1232 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1233
1234 mask |= view->output_mask;
1235
1236 if (area >= max) {
1237 new_output = view->output;
1238 max = area;
1239 }
1240 }
1241 pixman_region32_fini(&region);
1242
1243 es->output = new_output;
1244 weston_surface_update_output_mask(es, mask);
1245}
1246
Bryce Harrington3f650b82015-12-23 11:01:58 -08001247/** Recalculate which output(s) the view is displayed on
1248 *
1249 * \param ev The view to remap to outputs
1250 *
1251 * Identifies the set of outputs that the view is visible on,
1252 * noting them into the output_mask. The output that the view
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001253 * is most visible on is set as the view's primary output.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001254 *
1255 * Also does the same for the view's surface. See
1256 * weston_surface_assign_output().
1257 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001258static void
1259weston_view_assign_output(struct weston_view *ev)
1260{
1261 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001262 struct weston_output *output, *new_output;
1263 pixman_region32_t region;
1264 uint32_t max, area, mask;
1265 pixman_box32_t *e;
1266
1267 new_output = NULL;
1268 max = 0;
1269 mask = 0;
1270 pixman_region32_init(&region);
1271 wl_list_for_each(output, &ec->output_list, link) {
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03001272 if (output->destroying)
1273 continue;
1274
Jason Ekstranda7af7042013-10-12 22:38:11 -05001275 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001276 &output->region);
1277
1278 e = pixman_region32_extents(&region);
1279 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1280
1281 if (area > 0)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001282 mask |= 1u << output->id;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001283
1284 if (area >= max) {
1285 new_output = output;
1286 max = area;
1287 }
1288 }
1289 pixman_region32_fini(&region);
1290
Semi Malinene7a52fb2018-04-26 11:08:10 +02001291 weston_view_set_output(ev, new_output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001292 ev->output_mask = mask;
1293
1294 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001295}
1296
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001297static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001298weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1299 int from_x, int from_y, int *to_x, int *to_y)
1300{
1301 float x, y;
1302
1303 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1304 weston_view_from_global_float(to, x, y, &x, &y);
1305
1306 *to_x = round(x);
1307 *to_y = round(y);
1308}
1309
1310static void
1311weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1312{
1313 pixman_box32_t *a;
1314 pixman_box32_t b;
1315
1316 a = pixman_region32_extents(&from->geometry.scissor);
1317
1318 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1319 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1320
1321 pixman_region32_fini(&to->geometry.scissor);
1322 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1323}
1324
1325static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001326view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001327 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001328{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001329 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1330 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001331 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001332 { inbox->x1, inbox->y1 },
1333 { inbox->x1, inbox->y2 },
1334 { inbox->x2, inbox->y1 },
1335 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001336 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001337 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001338 int i;
1339
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001340 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001341 /* avoid rounding empty bbox to 1x1 */
1342 pixman_region32_init(bbox);
1343 return;
1344 }
1345
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001346 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001347 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001348 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001349 if (x < min_x)
1350 min_x = x;
1351 if (x > max_x)
1352 max_x = x;
1353 if (y < min_y)
1354 min_y = y;
1355 if (y > max_y)
1356 max_y = y;
1357 }
1358
Pekka Paalanen219b9822012-02-08 15:38:37 +02001359 int_x = floorf(min_x);
1360 int_y = floorf(min_y);
1361 pixman_region32_init_rect(bbox, int_x, int_y,
1362 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001363}
1364
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001365static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001366weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001367{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001368 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001369
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001370 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001371 view->geometry.x = roundf(view->geometry.x);
1372 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001373
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001374 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001375 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1376 view->transform.position.matrix.d[12] = view->geometry.x;
1377 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001378
Jason Ekstranda7af7042013-10-12 22:38:11 -05001379 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001380
Jason Ekstranda7af7042013-10-12 22:38:11 -05001381 view->transform.inverse = view->transform.position.matrix;
1382 view->transform.inverse.d[12] = -view->geometry.x;
1383 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001384
Jason Ekstranda7af7042013-10-12 22:38:11 -05001385 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001386 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001387 view->surface->width,
1388 view->surface->height);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001389 if (view->geometry.scissor_enabled)
1390 pixman_region32_intersect(&view->transform.boundingbox,
1391 &view->transform.boundingbox,
1392 &view->geometry.scissor);
1393
1394 pixman_region32_translate(&view->transform.boundingbox,
1395 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001396
Jason Ekstranda7af7042013-10-12 22:38:11 -05001397 if (view->alpha == 1.0) {
1398 pixman_region32_copy(&view->transform.opaque,
1399 &view->surface->opaque);
1400 pixman_region32_translate(&view->transform.opaque,
1401 view->geometry.x,
1402 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001403 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001404}
1405
1406static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001407weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001408{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001409 struct weston_view *parent = view->geometry.parent;
1410 struct weston_matrix *matrix = &view->transform.matrix;
1411 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001412 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001413 pixman_region32_t surfregion;
1414 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001415
Jason Ekstranda7af7042013-10-12 22:38:11 -05001416 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001417
1418 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001419 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1420 view->transform.position.matrix.d[12] = view->geometry.x;
1421 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001422
1423 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001424 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001425 weston_matrix_multiply(matrix, &tform->matrix);
1426
Pekka Paalanen483243f2013-03-08 14:56:50 +02001427 if (parent)
1428 weston_matrix_multiply(matrix, &parent->transform.matrix);
1429
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001430 if (weston_matrix_invert(inverse, matrix) < 0) {
1431 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001432 weston_log("error: weston_view %p"
1433 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001434 return -1;
1435 }
1436
Giulio Camuffo148c1992016-09-04 18:50:46 +03001437 if (view->alpha == 1.0 &&
1438 matrix->type == WESTON_MATRIX_TRANSFORM_TRANSLATE) {
1439 pixman_region32_copy(&view->transform.opaque,
1440 &view->surface->opaque);
1441 pixman_region32_translate(&view->transform.opaque,
1442 matrix->d[12],
1443 matrix->d[13]);
1444 }
1445
Pekka Paalanen380adf52015-02-16 14:39:11 +02001446 pixman_region32_init_rect(&surfregion, 0, 0,
1447 view->surface->width, view->surface->height);
1448 if (view->geometry.scissor_enabled)
1449 pixman_region32_intersect(&surfregion, &surfregion,
1450 &view->geometry.scissor);
1451 surfbox = pixman_region32_extents(&surfregion);
1452
1453 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1454 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001455
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001456 return 0;
1457}
1458
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001459static struct weston_layer *
1460get_view_layer(struct weston_view *view)
1461{
1462 if (view->parent_view)
1463 return get_view_layer(view->parent_view);
1464 return view->layer_link.layer;
1465}
1466
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001467WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001468weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001469{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001470 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001471 struct weston_layer *layer;
1472 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001473
Jason Ekstranda7af7042013-10-12 22:38:11 -05001474 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001475 return;
1476
Pekka Paalanen483243f2013-03-08 14:56:50 +02001477 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001478 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001479
Jason Ekstranda7af7042013-10-12 22:38:11 -05001480 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001481
Jason Ekstranda7af7042013-10-12 22:38:11 -05001482 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001483
Jason Ekstranda7af7042013-10-12 22:38:11 -05001484 pixman_region32_fini(&view->transform.boundingbox);
1485 pixman_region32_fini(&view->transform.opaque);
1486 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001487
Pekka Paalanencd403622012-01-25 13:37:39 +02001488 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001489 if (view->geometry.transformation_list.next ==
1490 &view->transform.position.link &&
1491 view->geometry.transformation_list.prev ==
1492 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001493 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001494 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001495 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001496 if (weston_view_update_transform_enable(view) < 0)
1497 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001498 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001499
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001500 layer = get_view_layer(view);
1501 if (layer) {
1502 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001503 pixman_region32_intersect(&view->transform.boundingbox,
1504 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001505 pixman_region32_intersect(&view->transform.opaque,
1506 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001507 pixman_region32_fini(&mask);
1508 }
1509
Pekka Paalanen380adf52015-02-16 14:39:11 +02001510 if (parent) {
1511 if (parent->geometry.scissor_enabled) {
1512 view->geometry.scissor_enabled = true;
1513 weston_view_transfer_scissor(parent, view);
1514 } else {
1515 view->geometry.scissor_enabled = false;
1516 }
1517 }
1518
Jason Ekstranda7af7042013-10-12 22:38:11 -05001519 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001520
Jason Ekstranda7af7042013-10-12 22:38:11 -05001521 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001522
Jason Ekstranda7af7042013-10-12 22:38:11 -05001523 wl_signal_emit(&view->surface->compositor->transform_signal,
1524 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001525}
1526
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001527WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001528weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001529{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001530 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001531
1532 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001533 * The invariant: if view->geometry.dirty, then all views
1534 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001535 * Corollary: if not parent->geometry.dirty, then all ancestors
1536 * are not dirty.
1537 */
1538
Jason Ekstranda7af7042013-10-12 22:38:11 -05001539 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001540 return;
1541
Jason Ekstranda7af7042013-10-12 22:38:11 -05001542 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001543
Jason Ekstranda7af7042013-10-12 22:38:11 -05001544 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001545 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001546 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001547}
1548
1549WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001550weston_view_to_global_fixed(struct weston_view *view,
1551 wl_fixed_t vx, wl_fixed_t vy,
1552 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001553{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001554 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001555
Jason Ekstranda7af7042013-10-12 22:38:11 -05001556 weston_view_to_global_float(view,
1557 wl_fixed_to_double(vx),
1558 wl_fixed_to_double(vy),
1559 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001560 *x = wl_fixed_from_double(xf);
1561 *y = wl_fixed_from_double(yf);
1562}
1563
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001564WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001565weston_view_from_global_float(struct weston_view *view,
1566 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001567{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001568 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001569 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1570
Jason Ekstranda7af7042013-10-12 22:38:11 -05001571 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001572
1573 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001574 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001575 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001576 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001577 *vx = 0;
1578 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001579 return;
1580 }
1581
Jason Ekstranda7af7042013-10-12 22:38:11 -05001582 *vx = v.f[0] / v.f[3];
1583 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001584 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001585 *vx = x - view->geometry.x;
1586 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001587 }
1588}
1589
1590WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001591weston_view_from_global_fixed(struct weston_view *view,
1592 wl_fixed_t x, wl_fixed_t y,
1593 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001594{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001595 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001596
Jason Ekstranda7af7042013-10-12 22:38:11 -05001597 weston_view_from_global_float(view,
1598 wl_fixed_to_double(x),
1599 wl_fixed_to_double(y),
1600 &vxf, &vyf);
1601 *vx = wl_fixed_from_double(vxf);
1602 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001603}
1604
1605WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001606weston_view_from_global(struct weston_view *view,
1607 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001608{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001609 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001610
Jason Ekstranda7af7042013-10-12 22:38:11 -05001611 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1612 *vx = floorf(vxf);
1613 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001614}
1615
Bryce Harrington3f650b82015-12-23 11:01:58 -08001616/**
1617 * \param surface The surface to be repainted
1618 *
1619 * Marks the output(s) that the surface is shown on as needing to be
1620 * repainted. See weston_output_schedule_repaint().
1621 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001622WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001623weston_surface_schedule_repaint(struct weston_surface *surface)
1624{
1625 struct weston_output *output;
1626
1627 wl_list_for_each(output, &surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001628 if (surface->output_mask & (1u << output->id))
Kristian Høgsberg98238702012-08-03 16:29:12 -04001629 weston_output_schedule_repaint(output);
1630}
1631
Bryce Harrington3f650b82015-12-23 11:01:58 -08001632/**
1633 * \param view The view to be repainted
1634 *
1635 * Marks the output(s) that the view is shown on as needing to be
1636 * repainted. See weston_output_schedule_repaint().
1637 */
Kristian Høgsberg98238702012-08-03 16:29:12 -04001638WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001639weston_view_schedule_repaint(struct weston_view *view)
1640{
1641 struct weston_output *output;
1642
1643 wl_list_for_each(output, &view->surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001644 if (view->output_mask & (1u << output->id))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001645 weston_output_schedule_repaint(output);
1646}
1647
Pekka Paalanene508ce62015-02-19 13:59:55 +02001648/**
1649 * XXX: This function does it the wrong way.
1650 * surface->damage is the damage from the client, and causes
1651 * surface_flush_damage() to copy pixels. No window management action can
1652 * cause damage to the client-provided content, warranting re-upload!
1653 *
1654 * Instead of surface->damage, this function should record the damage
1655 * with all the views for this surface to avoid extraneous texture
1656 * uploads.
1657 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001658WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001659weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001660{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001661 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001662 0, 0, surface->width,
1663 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001664
Kristian Høgsberg98238702012-08-03 16:29:12 -04001665 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001666}
1667
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001668WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001669weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001670{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001671 if (view->geometry.x == x && view->geometry.y == y)
1672 return;
1673
Jason Ekstranda7af7042013-10-12 22:38:11 -05001674 view->geometry.x = x;
1675 view->geometry.y = y;
1676 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001677}
1678
Pekka Paalanen483243f2013-03-08 14:56:50 +02001679static void
1680transform_parent_handle_parent_destroy(struct wl_listener *listener,
1681 void *data)
1682{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001683 struct weston_view *view =
1684 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001685 geometry.parent_destroy_listener);
1686
Jason Ekstranda7af7042013-10-12 22:38:11 -05001687 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001688}
1689
1690WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001691weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001692 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001693{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001694 if (view->geometry.parent) {
1695 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1696 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001697
1698 if (!parent)
1699 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001700 }
1701
Jason Ekstranda7af7042013-10-12 22:38:11 -05001702 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001703
Jason Ekstranda7af7042013-10-12 22:38:11 -05001704 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001705 transform_parent_handle_parent_destroy;
1706 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001707 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001708 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001709 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001710 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001711 }
1712
Jason Ekstranda7af7042013-10-12 22:38:11 -05001713 weston_view_geometry_dirty(view);
1714}
1715
Pekka Paalanen380adf52015-02-16 14:39:11 +02001716/** Set a clip mask rectangle on a view
1717 *
1718 * \param view The view to set the clip mask on.
1719 * \param x Top-left corner X coordinate of the clip rectangle.
1720 * \param y Top-left corner Y coordinate of the clip rectangle.
1721 * \param width Width of the clip rectangle, non-negative.
1722 * \param height Height of the clip rectangle, non-negative.
1723 *
1724 * A shell may set a clip mask rectangle on a view. Everything outside
1725 * the rectangle is cut away for input and output purposes: it is
1726 * not drawn and cannot be hit by hit-test based input like pointer
1727 * motion or touch-downs. Everything inside the rectangle will behave
1728 * normally. Clients are unaware of clipping.
1729 *
Yong Bakos4c72e292016-04-28 11:59:10 -05001730 * The rectangle is set in surface-local coordinates. Setting a clip
Pekka Paalanen380adf52015-02-16 14:39:11 +02001731 * mask rectangle does not affect the view position, the view is positioned
1732 * as it would be without a clip. The clip also does not change
1733 * weston_surface::width,height.
1734 *
1735 * The clip mask rectangle is part of transformation inheritance
1736 * (weston_view_set_transform_parent()). A clip set in the root of the
1737 * transformation inheritance tree will affect all views in the tree.
1738 * A clip can be set only on the root view. Attempting to set a clip
1739 * on view that has a transformation parent will fail. Assigning a parent
1740 * to a view that has a clip set will cause the clip to be forgotten.
1741 *
1742 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1743 * on the additional transformations in the child views. These transformations
1744 * may not rotate the coordinate axes, i.e., only translation and scaling
1745 * are allowed. Violating this restriction causes the clipping to malfunction.
1746 * Furthermore, using scaling may cause rounding errors in child clipping.
1747 *
1748 * The clip mask rectangle is not automatically adjusted based on
1749 * wl_surface.attach dx and dy arguments.
1750 *
1751 * A clip mask rectangle can be set only if the compositor capability
1752 * WESTON_CAP_VIEW_CLIP_MASK is present.
1753 *
1754 * This function sets the clip mask rectangle and schedules a repaint for
1755 * the view.
1756 */
1757WL_EXPORT void
1758weston_view_set_mask(struct weston_view *view,
1759 int x, int y, int width, int height)
1760{
1761 struct weston_compositor *compositor = view->surface->compositor;
1762
1763 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1764 weston_log("%s not allowed without capability!\n", __func__);
1765 return;
1766 }
1767
1768 if (view->geometry.parent) {
1769 weston_log("view %p has a parent, clip forbidden!\n", view);
1770 return;
1771 }
1772
1773 if (width < 0 || height < 0) {
1774 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1775 x, y, width, height);
1776 return;
1777 }
1778
1779 pixman_region32_fini(&view->geometry.scissor);
1780 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1781 view->geometry.scissor_enabled = true;
1782 weston_view_geometry_dirty(view);
1783 weston_view_schedule_repaint(view);
1784}
1785
1786/** Remove the clip mask from a view
1787 *
1788 * \param view The view to remove the clip mask from.
1789 *
1790 * Removed the clip mask rectangle and schedules a repaint.
1791 *
1792 * \sa weston_view_set_mask
1793 */
1794WL_EXPORT void
1795weston_view_set_mask_infinite(struct weston_view *view)
1796{
1797 view->geometry.scissor_enabled = false;
1798 weston_view_geometry_dirty(view);
1799 weston_view_schedule_repaint(view);
1800}
1801
Armin Krezović0da12b82016-06-30 06:04:33 +02001802/* Check if view should be displayed
1803 *
1804 * The indicator is set manually when assigning
1805 * a view to a surface.
1806 *
1807 * This needs reworking. See the thread starting at:
1808 *
1809 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1810 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001811WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001812weston_view_is_mapped(struct weston_view *view)
1813{
Armin Krezović0da12b82016-06-30 06:04:33 +02001814 return view->is_mapped;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001815}
1816
Philipp Zabel70decd52018-09-03 20:11:15 +02001817/* Check if view is opaque in specified region
1818 *
1819 * \param view The view to check for opacity.
1820 * \param region The region to check for opacity, in view coordinates.
1821 *
1822 * Returns true if the view is opaque in the specified region, because view
1823 * alpha is 1.0 and either the opaque region set by the client contains the
1824 * specified region, or the buffer pixel format or solid color is opaque.
1825 */
1826WL_EXPORT bool
1827weston_view_is_opaque(struct weston_view *ev, pixman_region32_t *region)
1828{
1829 pixman_region32_t r;
1830 bool ret = false;
1831
1832 if (ev->alpha < 1.0)
1833 return false;
1834
1835 if (ev->surface->is_opaque)
1836 return true;
1837
1838 if (ev->transform.dirty) {
1839 weston_log("%s: transform dirty", __func__);
1840 return false;
1841 }
1842
1843 pixman_region32_init(&r);
1844 pixman_region32_subtract(&r, region, &ev->transform.opaque);
1845
1846 if (!pixman_region32_not_empty(&r))
1847 ret = true;
1848
1849 pixman_region32_fini(&r);
1850
1851 return ret;
1852}
1853
Marius Vlad5f6bee42019-09-11 16:41:04 +03001854/** Check if the view has a valid buffer available
1855 *
1856 * @param ev The view to check if it has a valid buffer.
1857 *
1858 * Returns true if the view has a valid buffer or false otherwise.
1859 */
1860WL_EXPORT bool
1861weston_view_has_valid_buffer(struct weston_view *ev)
1862{
1863 return ev->surface->buffer_ref.buffer != NULL;
1864}
1865
Marius Vlad47e3d1e2019-09-11 16:53:08 +03001866/** Check if the view matches the entire output
1867 *
1868 * @param ev The view to check.
1869 * @param output The output to check against.
1870 *
1871 * Returns true if the view does indeed matches the entire output.
1872 */
1873WL_EXPORT bool
1874weston_view_matches_output_entirely(struct weston_view *ev,
1875 struct weston_output *output)
1876{
1877 pixman_box32_t *extents =
1878 pixman_region32_extents(&ev->transform.boundingbox);
1879
1880 if (extents->x1 != output->x ||
1881 extents->y1 != output->y ||
1882 extents->x2 != output->x + output->width ||
1883 extents->y2 != output->y + output->height)
1884 return false;
1885
1886 return true;
1887}
1888
Armin Krezović0da12b82016-06-30 06:04:33 +02001889/* Check if a surface has a view assigned to it
1890 *
1891 * The indicator is set manually when mapping
1892 * a surface and creating a view for it.
1893 *
1894 * This needs to go. See the thread starting at:
1895 *
1896 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1897 *
1898 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001899WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001900weston_surface_is_mapped(struct weston_surface *surface)
1901{
Armin Krezović0da12b82016-06-30 06:04:33 +02001902 return surface->is_mapped;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001903}
1904
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001905static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001906surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001907{
1908 struct weston_view *view;
1909
1910 if (surface->width == width && surface->height == height)
1911 return;
1912
1913 surface->width = width;
1914 surface->height = height;
1915
1916 wl_list_for_each(view, &surface->views, surface_link)
1917 weston_view_geometry_dirty(view);
1918}
1919
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001920WL_EXPORT void
1921weston_surface_set_size(struct weston_surface *surface,
1922 int32_t width, int32_t height)
1923{
1924 assert(!surface->resource);
1925 surface_set_size(surface, width, height);
1926}
1927
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001928static int
1929fixed_round_up_to_int(wl_fixed_t f)
1930{
1931 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1932}
1933
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001934static void
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001935convert_size_by_transform_scale(int32_t *width_out, int32_t *height_out,
1936 int32_t width, int32_t height,
1937 uint32_t transform,
1938 int32_t scale)
1939{
1940 assert(scale > 0);
1941
1942 switch (transform) {
1943 case WL_OUTPUT_TRANSFORM_NORMAL:
1944 case WL_OUTPUT_TRANSFORM_180:
1945 case WL_OUTPUT_TRANSFORM_FLIPPED:
1946 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1947 *width_out = width / scale;
1948 *height_out = height / scale;
1949 break;
1950 case WL_OUTPUT_TRANSFORM_90:
1951 case WL_OUTPUT_TRANSFORM_270:
1952 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1953 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1954 *width_out = height / scale;
1955 *height_out = width / scale;
1956 break;
1957 default:
1958 assert(0 && "invalid transform");
1959 }
1960}
1961
1962static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001963weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001964{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001965 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001966
1967 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001968 surface->width_from_buffer = 0;
1969 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001970 return;
1971 }
1972
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001973 convert_size_by_transform_scale(&surface->width_from_buffer,
1974 &surface->height_from_buffer,
1975 surface->buffer_ref.buffer->width,
1976 surface->buffer_ref.buffer->height,
1977 vp->buffer.transform,
1978 vp->buffer.scale);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001979}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001980
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001981static void
1982weston_surface_update_size(struct weston_surface *surface)
1983{
1984 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1985 int32_t width, height;
1986
1987 width = surface->width_from_buffer;
1988 height = surface->height_from_buffer;
1989
1990 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001991 surface_set_size(surface,
1992 vp->surface.width, vp->surface.height);
1993 return;
1994 }
1995
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001996 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001997 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1998 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1999
2000 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02002001 return;
2002 }
2003
Jason Ekstrand5c11a332013-12-04 20:32:03 -06002004 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002005}
2006
Marius Vlad9fdda7f2019-06-11 16:08:55 +03002007/** weston_compositor_get_time
2008 * \ingroup compositor
2009 */
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02002010WL_EXPORT void
2011weston_compositor_get_time(struct timespec *time)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05002012{
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02002013 clock_gettime(CLOCK_REALTIME, time);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05002014}
2015
Marius Vlad9fdda7f2019-06-11 16:08:55 +03002016/** weston_compositor_pick_view
2017 * \ingroup compositor
2018 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002019WL_EXPORT struct weston_view *
2020weston_compositor_pick_view(struct weston_compositor *compositor,
2021 wl_fixed_t x, wl_fixed_t y,
2022 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02002023{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002024 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02002025 wl_fixed_t view_x, view_y;
2026 int view_ix, view_iy;
2027 int ix = wl_fixed_to_int(x);
2028 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02002029
Jason Ekstranda7af7042013-10-12 22:38:11 -05002030 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02002031 if (!pixman_region32_contains_point(
2032 &view->transform.boundingbox, ix, iy, NULL))
2033 continue;
2034
2035 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
2036 view_ix = wl_fixed_to_int(view_x);
2037 view_iy = wl_fixed_to_int(view_y);
2038
2039 if (!pixman_region32_contains_point(&view->surface->input,
2040 view_ix, view_iy, NULL))
2041 continue;
2042
Pekka Paalanen380adf52015-02-16 14:39:11 +02002043 if (view->geometry.scissor_enabled &&
2044 !pixman_region32_contains_point(&view->geometry.scissor,
2045 view_ix, view_iy, NULL))
2046 continue;
2047
Pekka Paalanenfc22a522015-02-18 15:08:29 +02002048 *vx = view_x;
2049 *vy = view_y;
2050 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02002051 }
2052
Derek Foremanf9318d12015-05-11 15:40:11 -05002053 *vx = wl_fixed_from_int(-1000000);
2054 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02002055 return NULL;
2056}
2057
2058static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002059weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002060{
Daniel Stone37816df2012-05-16 18:45:18 +01002061 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002062
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07002063 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002064 return;
2065
Daniel Stone37816df2012-05-16 18:45:18 +01002066 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04002067 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002068}
2069
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002070WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002071weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05002072{
Daniel Stone4dab5db2012-05-30 16:31:53 +01002073 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002074
Jason Ekstranda7af7042013-10-12 22:38:11 -05002075 if (!weston_view_is_mapped(view))
2076 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002077
Jason Ekstranda7af7042013-10-12 22:38:11 -05002078 weston_view_damage_below(view);
Semi Malinene7a52fb2018-04-26 11:08:10 +02002079 weston_view_set_output(view, NULL);
Xiong Zhang97116532013-10-23 13:58:31 +08002080 view->plane = NULL;
Armin Krezovićf8486c32016-06-30 06:04:28 +02002081 view->is_mapped = false;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002082 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002083 wl_list_remove(&view->link);
2084 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002085 view->output_mask = 0;
2086 weston_surface_assign_output(view->surface);
2087
2088 if (weston_surface_is_mapped(view->surface))
2089 return;
2090
2091 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002092 struct weston_touch *touch = weston_seat_get_touch(seat);
2093 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2094 struct weston_keyboard *keyboard =
2095 weston_seat_get_keyboard(seat);
2096
2097 if (keyboard && keyboard->focus == view->surface)
2098 weston_keyboard_set_focus(keyboard, NULL);
2099 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05002100 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05002101 if (touch && touch->focus == view)
2102 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01002103 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002104}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002105
Jason Ekstranda7af7042013-10-12 22:38:11 -05002106WL_EXPORT void
2107weston_surface_unmap(struct weston_surface *surface)
2108{
2109 struct weston_view *view;
2110
Armin Krezovićf8486c32016-06-30 06:04:28 +02002111 surface->is_mapped = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002112 wl_list_for_each(view, &surface->views, surface_link)
2113 weston_view_unmap(view);
2114 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05002115}
2116
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002117static void
2118weston_surface_reset_pending_buffer(struct weston_surface *surface)
2119{
Jason Ekstrand7b982072014-05-20 14:33:03 -05002120 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002121 surface->pending.sx = 0;
2122 surface->pending.sy = 0;
2123 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002124 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002125}
2126
Jason Ekstranda7af7042013-10-12 22:38:11 -05002127WL_EXPORT void
2128weston_view_destroy(struct weston_view *view)
2129{
2130 wl_signal_emit(&view->destroy_signal, view);
2131
2132 assert(wl_list_empty(&view->geometry.child_list));
2133
2134 if (weston_view_is_mapped(view)) {
2135 weston_view_unmap(view);
2136 weston_compositor_build_view_list(view->surface->compositor);
2137 }
2138
2139 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002140 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002141
2142 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02002143 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002144 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002145 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002146
2147 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen944fae82018-05-22 13:15:58 +03002148 weston_view_set_output(view, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002149
Jason Ekstranda7af7042013-10-12 22:38:11 -05002150 wl_list_remove(&view->surface_link);
2151
2152 free(view);
2153}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002154
2155WL_EXPORT void
2156weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05002157{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002158 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002159 struct weston_view *ev, *nv;
Jonas Ådahld3414f22016-07-22 17:56:31 +08002160 struct weston_pointer_constraint *constraint, *next_constraint;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04002161
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02002162 if (--surface->ref_count > 0)
2163 return;
2164
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03002165 assert(surface->resource == NULL);
2166
Pekka Paalanenca790762015-04-17 14:23:38 +03002167 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02002168
Pekka Paalanene67858b2013-04-25 13:57:42 +03002169 assert(wl_list_empty(&surface->subsurface_list_pending));
2170 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02002171
Jason Ekstranda7af7042013-10-12 22:38:11 -05002172 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
2173 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002174
Jason Ekstrand7b982072014-05-20 14:33:03 -05002175 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002176
Pekka Paalanende685b82012-12-04 15:58:12 +02002177 weston_buffer_reference(&surface->buffer_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002178 weston_buffer_release_reference(&surface->buffer_release_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04002179
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02002180 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002181 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002182 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02002183
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002184 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002185 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002186
Pekka Paalanen133e4392014-09-23 22:08:46 -04002187 weston_presentation_feedback_discard_list(&surface->feedback_list);
2188
Jonas Ådahld3414f22016-07-22 17:56:31 +08002189 wl_list_for_each_safe(constraint, next_constraint,
2190 &surface->pointer_constraints,
2191 link)
2192 weston_pointer_constraint_destroy(constraint);
2193
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03002194 fd_clear(&surface->acquire_fence_fd);
2195
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04002196 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05002197}
2198
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002199static void
2200destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08002201{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002202 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08002203
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03002204 assert(surface);
2205
Giulio Camuffo0d379742013-11-15 22:06:15 +01002206 /* Set the resource to NULL, since we don't want to leave a
2207 * dangling pointer if the surface was refcounted and survives
2208 * the weston_surface_destroy() call. */
2209 surface->resource = NULL;
Pekka Paalanen4826f872016-04-22 14:14:38 +03002210
2211 if (surface->viewport_resource)
2212 wl_resource_set_user_data(surface->viewport_resource, NULL);
2213
Alexandros Frantzis27d7c392018-10-19 12:14:11 +03002214 if (surface->synchronization_resource) {
2215 wl_resource_set_user_data(surface->synchronization_resource,
2216 NULL);
2217 }
2218
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002219 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08002220}
2221
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002222static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002223weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
2224{
2225 struct weston_buffer *buffer =
2226 container_of(listener, struct weston_buffer, destroy_listener);
2227
2228 wl_signal_emit(&buffer->destroy_signal, buffer);
2229 free(buffer);
2230}
2231
Giulio Camuffoe058cd12013-12-12 14:14:29 +01002232WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002233weston_buffer_from_resource(struct wl_resource *resource)
2234{
2235 struct weston_buffer *buffer;
2236 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002237
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002238 listener = wl_resource_get_destroy_listener(resource,
2239 weston_buffer_destroy_handler);
2240
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002241 if (listener)
2242 return container_of(listener, struct weston_buffer,
2243 destroy_listener);
2244
2245 buffer = zalloc(sizeof *buffer);
2246 if (buffer == NULL)
2247 return NULL;
2248
2249 buffer->resource = resource;
2250 wl_signal_init(&buffer->destroy_signal);
2251 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04002252 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002253 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002254
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002255 return buffer;
2256}
2257
2258static void
Pekka Paalanende685b82012-12-04 15:58:12 +02002259weston_buffer_reference_handle_destroy(struct wl_listener *listener,
2260 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002261{
Pekka Paalanende685b82012-12-04 15:58:12 +02002262 struct weston_buffer_reference *ref =
2263 container_of(listener, struct weston_buffer_reference,
2264 destroy_listener);
2265
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002266 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02002267 ref->buffer = NULL;
2268}
2269
2270WL_EXPORT void
2271weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002272 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002273{
2274 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05002275 ref->buffer->busy_count--;
2276 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002277 assert(wl_resource_get_client(ref->buffer->resource));
Matt Hoosier3052bc72017-09-26 08:09:40 -05002278 wl_buffer_send_release(ref->buffer->resource);
Kristian Høgsberg20347802013-03-04 12:07:46 -05002279 }
Pekka Paalanende685b82012-12-04 15:58:12 +02002280 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002281 }
2282
Pekka Paalanende685b82012-12-04 15:58:12 +02002283 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002284 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002285 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02002286 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02002287 }
2288
Pekka Paalanende685b82012-12-04 15:58:12 +02002289 ref->buffer = buffer;
2290 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
2291}
2292
2293static void
Alexandros Frantzis67629672018-10-19 12:14:11 +03002294weston_buffer_release_reference_handle_destroy(struct wl_listener *listener,
2295 void *data)
2296{
2297 struct weston_buffer_release_reference *ref =
2298 container_of(listener, struct weston_buffer_release_reference,
2299 destroy_listener);
2300
2301 assert((struct wl_resource *)data == ref->buffer_release->resource);
2302 ref->buffer_release = NULL;
2303}
2304
2305static void
2306weston_buffer_release_destroy(struct weston_buffer_release *buffer_release)
2307{
2308 struct wl_resource *resource = buffer_release->resource;
2309 int release_fence_fd = buffer_release->fence_fd;
2310
2311 if (release_fence_fd >= 0) {
2312 zwp_linux_buffer_release_v1_send_fenced_release(
2313 resource, release_fence_fd);
2314 } else {
2315 zwp_linux_buffer_release_v1_send_immediate_release(
2316 resource);
2317 }
2318
2319 wl_resource_destroy(resource);
2320}
2321
2322WL_EXPORT void
2323weston_buffer_release_reference(struct weston_buffer_release_reference *ref,
2324 struct weston_buffer_release *buffer_release)
2325{
2326 if (buffer_release == ref->buffer_release)
2327 return;
2328
2329 if (ref->buffer_release) {
2330 ref->buffer_release->ref_count--;
2331 wl_list_remove(&ref->destroy_listener.link);
2332 if (ref->buffer_release->ref_count == 0)
2333 weston_buffer_release_destroy(ref->buffer_release);
2334 }
2335
2336 if (buffer_release) {
2337 buffer_release->ref_count++;
2338 wl_resource_add_destroy_listener(buffer_release->resource,
2339 &ref->destroy_listener);
2340 }
2341
2342 ref->buffer_release = buffer_release;
2343 ref->destroy_listener.notify =
2344 weston_buffer_release_reference_handle_destroy;
2345}
2346
2347WL_EXPORT void
2348weston_buffer_release_move(struct weston_buffer_release_reference *dest,
2349 struct weston_buffer_release_reference *src)
2350{
2351 weston_buffer_release_reference(dest, src->buffer_release);
2352 weston_buffer_release_reference(src, NULL);
2353}
2354
2355static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002356weston_surface_attach(struct weston_surface *surface,
2357 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002358{
2359 weston_buffer_reference(&surface->buffer_ref, buffer);
2360
Pekka Paalanena6421c42012-12-04 15:58:10 +02002361 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002362 if (weston_surface_is_mapped(surface))
2363 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002364 }
2365
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002366 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02002367
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002368 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002369 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002370}
2371
Marius Vlad9fdda7f2019-06-11 16:08:55 +03002372/** weston_compositor_damage_all
2373 * \ingroup compositor
2374 */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002375WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002376weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002377{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002378 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002379
2380 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002381 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002382}
2383
Marius Vlad55d87362019-06-11 01:15:35 +03002384/**
2385 * \ingroup output
2386 */
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05002387WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002388weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002389{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002390 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002391
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002392 pixman_region32_union(&compositor->primary_plane.damage,
2393 &compositor->primary_plane.damage,
2394 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002395 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002396}
2397
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04002398static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002399surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002400{
Pekka Paalanende685b82012-12-04 15:58:12 +02002401 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002402 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002403 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002404
Marius Vlad2a1b7862019-09-05 13:12:18 +03002405 if (pixman_region32_not_empty(&surface->damage))
Marius Vlad3203ff62019-09-05 14:56:12 +03002406 TL_POINT(surface->compositor, "core_flush_damage", TLP_SURFACE(surface),
Pekka Paalanenb5026542014-11-12 15:09:24 +02002407 TLP_OUTPUT(surface->output), TLP_END);
2408
Jason Ekstrandef540082014-06-26 10:37:36 -07002409 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002410}
2411
2412static void
2413view_accumulate_damage(struct weston_view *view,
2414 pixman_region32_t *opaque)
2415{
2416 pixman_region32_t damage;
2417
2418 pixman_region32_init(&damage);
2419 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002420 pixman_box32_t *extents;
2421
Jason Ekstranda7af7042013-10-12 22:38:11 -05002422 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002423 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002424 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002425 pixman_region32_copy(&damage, &view->surface->damage);
2426 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002427 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002428 }
2429
Pekka Paalanen380adf52015-02-16 14:39:11 +02002430 pixman_region32_intersect(&damage, &damage,
2431 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002432 pixman_region32_subtract(&damage, &damage, opaque);
2433 pixman_region32_union(&view->plane->damage,
2434 &view->plane->damage, &damage);
2435 pixman_region32_fini(&damage);
2436 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002437 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002438}
2439
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002440static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002441compositor_accumulate_damage(struct weston_compositor *ec)
2442{
2443 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002444 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002445 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002446
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002447 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002448
2449 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002450 pixman_region32_copy(&plane->clip, &clip);
2451
2452 pixman_region32_init(&opaque);
2453
Jason Ekstranda7af7042013-10-12 22:38:11 -05002454 wl_list_for_each(ev, &ec->view_list, link) {
2455 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002456 continue;
2457
Jason Ekstranda7af7042013-10-12 22:38:11 -05002458 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002459 }
2460
2461 pixman_region32_union(&clip, &clip, &opaque);
2462 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002463 }
2464
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002465 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002466
Jason Ekstranda7af7042013-10-12 22:38:11 -05002467 wl_list_for_each(ev, &ec->view_list, link)
Derek Foreman060cf112015-11-18 16:32:26 -06002468 ev->surface->touched = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002469
2470 wl_list_for_each(ev, &ec->view_list, link) {
2471 if (ev->surface->touched)
2472 continue;
Derek Foreman060cf112015-11-18 16:32:26 -06002473 ev->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002474
2475 surface_flush_damage(ev->surface);
2476
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002477 /* Both the renderer and the backend have seen the buffer
2478 * by now. If renderer needs the buffer, it has its own
2479 * reference set. If the backend wants to keep the buffer
2480 * around for migrating the surface into a non-primary plane
2481 * later, keep_buffer is true. Otherwise, drop the core
2482 * reference now, and allow early buffer release. This enables
2483 * clients to use single-buffering.
2484 */
Alexandros Frantzis67629672018-10-19 12:14:11 +03002485 if (!ev->surface->keep_buffer) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002486 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002487 weston_buffer_release_reference(
2488 &ev->surface->buffer_release_ref, NULL);
2489 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002490 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002491}
2492
2493static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002494surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002495{
2496 struct weston_subsurface *sub;
2497
Pekka Paalanene67858b2013-04-25 13:57:42 +03002498 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002499 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002500 continue;
2501
Jason Ekstranda7af7042013-10-12 22:38:11 -05002502 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2503 wl_list_init(&sub->surface->views);
2504
2505 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002506 }
2507}
2508
2509static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002510surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002511{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002512 struct weston_subsurface *sub;
2513 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002514
Jason Ekstranda7af7042013-10-12 22:38:11 -05002515 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2516 if (sub->surface == surface)
2517 continue;
2518
George Kiagiadakised04d382014-06-13 18:10:26 +02002519 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2520 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002521 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002522 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002523
2524 surface_free_unused_subsurface_views(sub->surface);
2525 }
2526}
2527
2528static void
2529view_list_add_subsurface_view(struct weston_compositor *compositor,
2530 struct weston_subsurface *sub,
2531 struct weston_view *parent)
2532{
2533 struct weston_subsurface *child;
2534 struct weston_view *view = NULL, *iv;
2535
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002536 if (!weston_surface_is_mapped(sub->surface))
2537 return;
2538
Jason Ekstranda7af7042013-10-12 22:38:11 -05002539 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2540 if (iv->geometry.parent == parent) {
2541 view = iv;
2542 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002543 }
2544 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002545
2546 if (view) {
2547 /* Put it back in the surface's list of views */
2548 wl_list_remove(&view->surface_link);
2549 wl_list_insert(&sub->surface->views, &view->surface_link);
2550 } else {
2551 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002552 weston_view_set_position(view,
2553 sub->position.x,
2554 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002555 weston_view_set_transform_parent(view, parent);
2556 }
2557
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002558 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002559 weston_view_update_transform(view);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002560 view->is_mapped = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002561
Pekka Paalanenb188e912013-11-19 14:03:35 +02002562 if (wl_list_empty(&sub->surface->subsurface_list)) {
2563 wl_list_insert(compositor->view_list.prev, &view->link);
2564 return;
2565 }
2566
2567 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2568 if (child->surface == sub->surface)
2569 wl_list_insert(compositor->view_list.prev, &view->link);
2570 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002571 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002572 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002573}
2574
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002575/* This recursively adds the sub-surfaces for a view, relying on the
2576 * sub-surface order. Thus, if a client restacks the sub-surfaces, that
2577 * change first happens to the sub-surface list, and then automatically
2578 * propagates here. See weston_surface_damage_subsurfaces() for how the
2579 * sub-surfaces receive damage when the client changes the state.
2580 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002581static void
2582view_list_add(struct weston_compositor *compositor,
2583 struct weston_view *view)
2584{
2585 struct weston_subsurface *sub;
2586
2587 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002588
Pekka Paalanenb188e912013-11-19 14:03:35 +02002589 if (wl_list_empty(&view->surface->subsurface_list)) {
2590 wl_list_insert(compositor->view_list.prev, &view->link);
2591 return;
2592 }
2593
2594 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2595 if (sub->surface == view->surface)
2596 wl_list_insert(compositor->view_list.prev, &view->link);
2597 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002598 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002599 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002600}
2601
2602static void
2603weston_compositor_build_view_list(struct weston_compositor *compositor)
2604{
Loïc Yhuel267b16e2019-09-17 20:14:56 +02002605 struct weston_view *view, *tmp;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002606 struct weston_layer *layer;
2607
2608 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002609 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002610 surface_stash_subsurface_views(view->surface);
2611
Loïc Yhuel267b16e2019-09-17 20:14:56 +02002612 wl_list_for_each_safe(view, tmp, &compositor->view_list, link)
2613 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002614 wl_list_init(&compositor->view_list);
Loïc Yhuel267b16e2019-09-17 20:14:56 +02002615
Jason Ekstranda7af7042013-10-12 22:38:11 -05002616 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002617 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002618 view_list_add(compositor, view);
2619 }
2620 }
2621
2622 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002623 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002624 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002625}
2626
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002627static void
2628weston_output_take_feedback_list(struct weston_output *output,
2629 struct weston_surface *surface)
2630{
2631 struct weston_view *view;
2632 struct weston_presentation_feedback *feedback;
2633 uint32_t flags = 0xffffffff;
2634
2635 if (wl_list_empty(&surface->feedback_list))
2636 return;
2637
2638 /* All views must have the flag for the flag to survive. */
2639 wl_list_for_each(view, &surface->views, surface_link) {
2640 /* ignore views that are not on this output at all */
2641 if (view->output_mask & (1u << output->id))
2642 flags &= view->psf_flags;
2643 }
2644
2645 wl_list_for_each(feedback, &surface->feedback_list, link)
2646 feedback->psf_flags = flags;
2647
2648 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2649 wl_list_init(&surface->feedback_list);
2650}
2651
David Herrmann1edf44c2013-10-22 17:11:26 +02002652static int
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002653weston_output_repaint(struct weston_output *output, void *repaint_data)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002654{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002655 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002656 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002657 struct weston_animation *animation, *next;
2658 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002659 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002660 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002661 int r;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002662 uint32_t frame_time_msec;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05302663 enum weston_hdcp_protection highest_requested = WESTON_HDCP_DISABLE;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002664
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002665 if (output->destroying)
2666 return 0;
2667
Marius Vlad3203ff62019-09-05 14:56:12 +03002668 TL_POINT(ec, "core_repaint_begin", TLP_OUTPUT(output), TLP_END);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002669
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002670 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002671 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002672
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05302673 /* Find the highest protection desired for an output */
2674 wl_list_for_each(ev, &ec->view_list, link) {
2675 if (ev->surface->output_mask & (1u << output->id)) {
2676 /*
2677 * The desired_protection of the output should be the
2678 * maximum of the desired_protection of the surfaces,
2679 * that are displayed on that output, to avoid
2680 * reducing the protection for existing surfaces.
2681 */
2682 if (ev->surface->desired_protection > highest_requested)
2683 highest_requested =
2684 ev->surface->desired_protection;
2685 }
2686 }
2687
2688 output->desired_protection = highest_requested;
2689
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002690 if (output->assign_planes && !output->disable_planes) {
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002691 output->assign_planes(output, repaint_data);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002692 } else {
2693 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002694 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002695 ev->psf_flags = 0;
2696 }
2697 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002698
Pekka Paalanene67858b2013-04-25 13:57:42 +03002699 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002700 wl_list_for_each(ev, &ec->view_list, link) {
2701 /* Note: This operation is safe to do multiple times on the
2702 * same surface.
2703 */
2704 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002705 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002706 &ev->surface->frame_callback_list);
2707 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002708
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002709 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002710 }
2711 }
2712
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002713 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002714
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002715 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002716 pixman_region32_intersect(&output_damage,
2717 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002718 pixman_region32_subtract(&output_damage,
2719 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002720
Scott Moreauccbf29d2012-02-22 14:21:41 -07002721 if (output->dirty)
2722 weston_output_update_matrix(output);
2723
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002724 r = output->repaint(output, &output_damage, repaint_data);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002725
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002726 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002727
Daniel Stone09a97e22017-03-01 11:34:06 +00002728 output->repaint_needed = false;
Daniel Stone05df8c12017-03-03 16:59:42 +00002729 if (r == 0)
2730 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002731
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002732 weston_compositor_repick(ec);
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002733
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002734 frame_time_msec = timespec_to_msec(&output->frame_time);
2735
Jonas Ådahldb773762012-06-13 00:01:21 +02002736 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002737 wl_callback_send_done(cb->resource, frame_time_msec);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002738 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002739 }
2740
Scott Moreaud64cf212012-06-08 19:40:54 -06002741 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002742 animation->frame_counter++;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002743 animation->frame(animation, output, &output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002744 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002745
Marius Vlad3203ff62019-09-05 14:56:12 +03002746 TL_POINT(ec, "core_repaint_posted", TLP_OUTPUT(output), TLP_END);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002747
David Herrmann1edf44c2013-10-22 17:11:26 +02002748 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002749}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002750
Pekka Paalanen82919792014-05-21 13:51:49 +03002751static void
2752weston_output_schedule_repaint_reset(struct weston_output *output)
2753{
Daniel Stone05df8c12017-03-03 16:59:42 +00002754 output->repaint_status = REPAINT_NOT_SCHEDULED;
Marius Vlad3203ff62019-09-05 14:56:12 +03002755 TL_POINT(output->compositor, "core_repaint_exit_loop",
2756 TLP_OUTPUT(output), TLP_END);
Pekka Paalanen82919792014-05-21 13:51:49 +03002757}
2758
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002759static int
2760weston_output_maybe_repaint(struct weston_output *output, struct timespec *now,
2761 void *repaint_data)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002762{
Pekka Paalanen0513a952014-05-21 16:17:27 +03002763 struct weston_compositor *compositor = output->compositor;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002764 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002765 int64_t msec_to_repaint;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002766
Daniel Stone6847b852017-03-01 11:34:08 +00002767 /* We're not ready yet; come back to make a decision later. */
2768 if (output->repaint_status != REPAINT_SCHEDULED)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002769 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002770
2771 msec_to_repaint = timespec_sub_to_msec(&output->next_repaint, now);
2772 if (msec_to_repaint > 1)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002773 return ret;
Daniel Stone05df8c12017-03-03 16:59:42 +00002774
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002775 /* If we're sleeping, drop the repaint machinery entirely; we will
2776 * explicitly repaint all outputs when we come back. */
2777 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2778 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
2779 goto err;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002780
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002781 /* We don't actually need to repaint this output; drop it from
2782 * repaint until something causes damage. */
2783 if (!output->repaint_needed)
2784 goto err;
2785
2786 /* If repaint fails, we aren't going to get weston_output_finish_frame
2787 * to trigger a new repaint, so drop it from repaint and hope
Daniel Stone6847b852017-03-01 11:34:08 +00002788 * something schedules a successful repaint later. As repainting may
2789 * take some time, re-read our clock as a courtesy to the next
2790 * output. */
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002791 ret = weston_output_repaint(output, repaint_data);
Daniel Stone6847b852017-03-01 11:34:08 +00002792 weston_compositor_read_presentation_clock(compositor, now);
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002793 if (ret != 0)
2794 goto err;
2795
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09002796 output->repainted = true;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002797 return ret;
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002798
2799err:
Pekka Paalanen0513a952014-05-21 16:17:27 +03002800 weston_output_schedule_repaint_reset(output);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002801 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002802}
2803
2804static void
2805output_repaint_timer_arm(struct weston_compositor *compositor)
2806{
2807 struct weston_output *output;
2808 bool any_should_repaint = false;
2809 struct timespec now;
Sergi Granellb4c08862017-03-18 13:01:15 +01002810 int64_t msec_to_next = INT64_MAX;
Daniel Stone6847b852017-03-01 11:34:08 +00002811
2812 weston_compositor_read_presentation_clock(compositor, &now);
2813
2814 wl_list_for_each(output, &compositor->output_list, link) {
2815 int64_t msec_to_this;
2816
2817 if (output->repaint_status != REPAINT_SCHEDULED)
2818 continue;
2819
2820 msec_to_this = timespec_sub_to_msec(&output->next_repaint,
2821 &now);
2822 if (!any_should_repaint || msec_to_this < msec_to_next)
2823 msec_to_next = msec_to_this;
2824
2825 any_should_repaint = true;
2826 }
2827
2828 if (!any_should_repaint)
2829 return;
2830
2831 /* Even if we should repaint immediately, add the minimum 1 ms delay.
2832 * This is a workaround to allow coalescing multiple output repaints
2833 * particularly from weston_output_finish_frame()
2834 * into the same call, which would not happen if we called
2835 * output_repaint_timer_handler() directly.
2836 */
2837 if (msec_to_next < 1)
2838 msec_to_next = 1;
2839
2840 wl_event_source_timer_update(compositor->repaint_timer, msec_to_next);
2841}
2842
2843static int
2844output_repaint_timer_handler(void *data)
2845{
2846 struct weston_compositor *compositor = data;
2847 struct weston_output *output;
2848 struct timespec now;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002849 void *repaint_data = NULL;
Emre Ucane479ed82018-03-20 15:29:40 +01002850 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002851
2852 weston_compositor_read_presentation_clock(compositor, &now);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002853
2854 if (compositor->backend->repaint_begin)
2855 repaint_data = compositor->backend->repaint_begin(compositor);
2856
2857 wl_list_for_each(output, &compositor->output_list, link) {
2858 ret = weston_output_maybe_repaint(output, &now, repaint_data);
2859 if (ret)
2860 break;
2861 }
2862
2863 if (ret == 0) {
Tomohito Esaki09bfcd62018-06-05 10:37:05 +09002864 if (compositor->backend->repaint_flush)
Antonio Borneoc90fccc2019-06-30 15:51:10 +02002865 ret = compositor->backend->repaint_flush(compositor,
2866 repaint_data);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002867 } else {
Antonio Borneoc90fccc2019-06-30 15:51:10 +02002868 if (compositor->backend->repaint_cancel)
2869 compositor->backend->repaint_cancel(compositor,
2870 repaint_data);
2871 }
2872
2873 if (ret != 0) {
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09002874 wl_list_for_each(output, &compositor->output_list, link) {
2875 if (output->repainted)
2876 weston_output_schedule_repaint_reset(output);
2877 }
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002878 }
Daniel Stone6847b852017-03-01 11:34:08 +00002879
Tomohito Esakiddaf95c2018-07-10 11:47:15 +09002880 wl_list_for_each(output, &compositor->output_list, link)
2881 output->repainted = false;
2882
Daniel Stone6847b852017-03-01 11:34:08 +00002883 output_repaint_timer_arm(compositor);
2884
Pekka Paalanen0513a952014-05-21 16:17:27 +03002885 return 0;
2886}
2887
Marius Vlad55d87362019-06-11 01:15:35 +03002888/**
2889 * \ingroup output
2890 */
Kristian Høgsbergef044142011-06-21 15:02:12 -04002891WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002892weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002893 const struct timespec *stamp,
2894 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002895{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002896 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002897 int32_t refresh_nsec;
2898 struct timespec now;
Daniel Stone6847b852017-03-01 11:34:08 +00002899 int64_t msec_rel;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002900
Pekka Paalanenb5026542014-11-12 15:09:24 +02002901
Daniel Stone05df8c12017-03-03 16:59:42 +00002902 assert(output->repaint_status == REPAINT_AWAITING_COMPLETION);
Daniel Stone3615ce12017-03-01 11:34:05 +00002903 assert(stamp || (presented_flags & WP_PRESENTATION_FEEDBACK_INVALID));
2904
Daniel Stone6847b852017-03-01 11:34:08 +00002905 weston_compositor_read_presentation_clock(compositor, &now);
2906
Daniel Stone3615ce12017-03-01 11:34:05 +00002907 /* If we haven't been supplied any timestamp at all, we don't have a
2908 * timebase to work against, so any delay just wastes time. Push a
2909 * repaint as soon as possible so we can get on with it. */
Daniel Stone6847b852017-03-01 11:34:08 +00002910 if (!stamp) {
2911 output->next_repaint = now;
Daniel Stone3615ce12017-03-01 11:34:05 +00002912 goto out;
Daniel Stone6847b852017-03-01 11:34:08 +00002913 }
Daniel Stone3615ce12017-03-01 11:34:05 +00002914
Marius Vlad3203ff62019-09-05 14:56:12 +03002915 TL_POINT(compositor, "core_repaint_finished", TLP_OUTPUT(output),
Marius Vladdf9278a2018-03-06 18:56:23 +02002916 TLP_VBLANK(stamp), TLP_END);
2917
Pekka Paalanend7894d02015-07-03 15:08:53 +03002918 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002919 weston_presentation_feedback_present_list(&output->feedback_list,
2920 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002921 output->msc,
2922 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002923
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002924 output->frame_time = *stamp;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002925
Daniel Stone6847b852017-03-01 11:34:08 +00002926 timespec_add_nsec(&output->next_repaint, stamp, refresh_nsec);
2927 timespec_add_msec(&output->next_repaint, &output->next_repaint,
2928 -compositor->repaint_msec);
2929 msec_rel = timespec_sub_to_msec(&output->next_repaint, &now);
Daniel Stone84aff5c2017-03-01 11:34:04 +00002930
2931 if (msec_rel < -1000 || msec_rel > 1000) {
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002932 static bool warned;
2933
2934 if (!warned)
2935 weston_log("Warning: computed repaint delay is "
Daniel Stone6847b852017-03-01 11:34:08 +00002936 "insane: %lld msec\n", (long long) msec_rel);
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002937 warned = true;
2938
Daniel Stone6847b852017-03-01 11:34:08 +00002939 output->next_repaint = now;
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002940 }
2941
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002942 /* Called from restart_repaint_loop and restart happens already after
2943 * the deadline given by repaint_msec? In that case we delay until
2944 * the deadline of the next frame, to give clients a more predictable
2945 * timing of the repaint cycle to lock on. */
Daniel Stoneeca5cca2017-02-28 21:53:51 +00002946 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID &&
2947 msec_rel < 0) {
2948 while (timespec_sub_to_nsec(&output->next_repaint, &now) < 0) {
2949 timespec_add_nsec(&output->next_repaint,
2950 &output->next_repaint,
2951 refresh_nsec);
2952 }
2953 }
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002954
Daniel Stone3615ce12017-03-01 11:34:05 +00002955out:
Daniel Stone05df8c12017-03-03 16:59:42 +00002956 output->repaint_status = REPAINT_SCHEDULED;
Daniel Stone6847b852017-03-01 11:34:08 +00002957 output_repaint_timer_arm(compositor);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002958}
2959
2960static void
2961idle_repaint(void *data)
2962{
2963 struct weston_output *output = data;
Antonio Borneoc90fccc2019-06-30 15:51:10 +02002964 int ret;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002965
Daniel Stone05df8c12017-03-03 16:59:42 +00002966 assert(output->repaint_status == REPAINT_BEGIN_FROM_IDLE);
2967 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03002968 output->idle_repaint_source = NULL;
Antonio Borneoc90fccc2019-06-30 15:51:10 +02002969 ret = output->start_repaint_loop(output);
2970 if (ret != 0)
2971 weston_output_schedule_repaint_reset(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002972}
2973
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002974WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002975weston_layer_entry_insert(struct weston_layer_entry *list,
2976 struct weston_layer_entry *entry)
2977{
2978 wl_list_insert(&list->link, &entry->link);
2979 entry->layer = list->layer;
2980}
2981
2982WL_EXPORT void
2983weston_layer_entry_remove(struct weston_layer_entry *entry)
2984{
2985 wl_list_remove(&entry->link);
2986 wl_list_init(&entry->link);
2987 entry->layer = NULL;
2988}
2989
Quentin Glidic82681572016-12-17 13:40:51 +01002990
2991/** Initialize the weston_layer struct.
2992 *
2993 * \param compositor The compositor instance
2994 * \param layer The layer to initialize
2995 */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002996WL_EXPORT void
Quentin Glidic82681572016-12-17 13:40:51 +01002997weston_layer_init(struct weston_layer *layer,
2998 struct weston_compositor *compositor)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002999{
Quentin Glidic82681572016-12-17 13:40:51 +01003000 layer->compositor = compositor;
3001 wl_list_init(&layer->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003002 wl_list_init(&layer->view_list.link);
3003 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003004 weston_layer_set_mask_infinite(layer);
Quentin Glidic82681572016-12-17 13:40:51 +01003005}
3006
3007/** Sets the position of the layer in the layer list. The layer will be placed
3008 * below any layer with the same position value, if any.
3009 * This function is safe to call if the layer is already on the list, but the
3010 * layer may be moved below other layers at the same position, if any.
3011 *
3012 * \param layer The layer to modify
3013 * \param position The position the layer will be placed at
3014 */
3015WL_EXPORT void
3016weston_layer_set_position(struct weston_layer *layer,
3017 enum weston_layer_position position)
3018{
3019 struct weston_layer *below;
3020
3021 wl_list_remove(&layer->link);
3022
3023 /* layer_list is ordered from top to bottom, the last layer being the
3024 * background with the smallest position value */
3025
3026 layer->position = position;
3027 wl_list_for_each_reverse(below, &layer->compositor->layer_list, link) {
3028 if (below->position >= layer->position) {
3029 wl_list_insert(&below->link, &layer->link);
3030 return;
3031 }
3032 }
3033 wl_list_insert(&layer->compositor->layer_list, &layer->link);
3034}
3035
3036/** Hide a layer by taking it off the layer list.
3037 * This function is safe to call if the layer is not on the list.
3038 *
3039 * \param layer The layer to hide
3040 */
3041WL_EXPORT void
3042weston_layer_unset_position(struct weston_layer *layer)
3043{
3044 wl_list_remove(&layer->link);
3045 wl_list_init(&layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003046}
3047
3048WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003049weston_layer_set_mask(struct weston_layer *layer,
3050 int x, int y, int width, int height)
3051{
3052 struct weston_view *view;
3053
3054 layer->mask.x1 = x;
3055 layer->mask.x2 = x + width;
3056 layer->mask.y1 = y;
3057 layer->mask.y2 = y + height;
3058
3059 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
3060 weston_view_geometry_dirty(view);
3061 }
3062}
3063
3064WL_EXPORT void
3065weston_layer_set_mask_infinite(struct weston_layer *layer)
3066{
Adam Jackson3c3f3b12019-10-16 16:02:59 -04003067 struct weston_view *view;
3068
3069 layer->mask.x1 = INT32_MIN;
3070 layer->mask.x2 = INT32_MAX;
3071 layer->mask.y1 = INT32_MIN;
3072 layer->mask.y2 = INT32_MAX;
3073
3074 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
3075 weston_view_geometry_dirty(view);
3076 }
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003077}
3078
Daniel Stone3b775632018-07-20 08:38:25 +01003079WL_EXPORT bool
3080weston_layer_mask_is_infinite(struct weston_layer *layer)
3081{
3082 return layer->mask.x1 == INT32_MIN &&
3083 layer->mask.y1 == INT32_MIN &&
Adam Jackson3c3f3b12019-10-16 16:02:59 -04003084 layer->mask.x2 == INT32_MAX &&
3085 layer->mask.y2 == INT32_MAX;
Daniel Stone3b775632018-07-20 08:38:25 +01003086}
3087
Marius Vlad55d87362019-06-11 01:15:35 +03003088/**
3089 * \ingroup output
3090 */
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003091WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003092weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003093{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003094 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04003095 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01003096
Bryce Harrington08976ac2016-08-30 12:05:16 -07003097 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
3098 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003099 return;
3100
Pekka Paalanenb5026542014-11-12 15:09:24 +02003101 if (!output->repaint_needed)
Marius Vlad3203ff62019-09-05 14:56:12 +03003102 TL_POINT(compositor, "core_repaint_req", TLP_OUTPUT(output), TLP_END);
Pekka Paalanenb5026542014-11-12 15:09:24 +02003103
Kristian Høgsbergef044142011-06-21 15:02:12 -04003104 loop = wl_display_get_event_loop(compositor->wl_display);
Daniel Stone09a97e22017-03-01 11:34:06 +00003105 output->repaint_needed = true;
Daniel Stone05df8c12017-03-03 16:59:42 +00003106
3107 /* If we already have a repaint scheduled for our idle handler,
3108 * no need to set it again. If the repaint has been called but
3109 * not finished, then weston_output_finish_frame() will notice
3110 * that a repaint is needed and schedule one. */
3111 if (output->repaint_status != REPAINT_NOT_SCHEDULED)
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003112 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01003113
Daniel Stone05df8c12017-03-03 16:59:42 +00003114 output->repaint_status = REPAINT_BEGIN_FROM_IDLE;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03003115 assert(!output->idle_repaint_source);
3116 output->idle_repaint_source = wl_event_loop_add_idle(loop, idle_repaint,
3117 output);
Marius Vlad3203ff62019-09-05 14:56:12 +03003118 TL_POINT(compositor, "core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003119}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05003120
Marius Vlad9fdda7f2019-06-11 16:08:55 +03003121/** weston_compositor_schedule_repaint
3122 * \ingroup compositor
3123 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003124WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003125weston_compositor_schedule_repaint(struct weston_compositor *compositor)
3126{
3127 struct weston_output *output;
3128
3129 wl_list_for_each(output, &compositor->output_list, link)
3130 weston_output_schedule_repaint(output);
3131}
3132
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003133static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003134surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003135{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003136 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003137}
3138
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003139static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003140surface_attach(struct wl_client *client,
3141 struct wl_resource *resource,
3142 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
3143{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003144 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05003145 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003146
Kristian Høgsbergab19f932013-08-20 11:30:54 -07003147 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05003148 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07003149 if (buffer == NULL) {
3150 wl_client_post_no_memory(client);
3151 return;
3152 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07003153 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04003154
Pekka Paalanende685b82012-12-04 15:58:12 +02003155 /* Attach, attach, without commit in between does not send
3156 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003157 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003158
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003159 surface->pending.sx = sx;
3160 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01003161 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003162}
3163
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003164static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003165surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003166 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003167 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05003168{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003169 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003170
Derek Foreman57e92ed2015-11-17 14:11:35 -06003171 if (width <= 0 || height <= 0)
3172 return;
3173
Derek Foreman152254b2015-11-26 14:17:48 -06003174 pixman_region32_union_rect(&surface->pending.damage_surface,
3175 &surface->pending.damage_surface,
3176 x, y, width, height);
3177}
3178
3179static void
3180surface_damage_buffer(struct wl_client *client,
3181 struct wl_resource *resource,
3182 int32_t x, int32_t y, int32_t width, int32_t height)
3183{
3184 struct weston_surface *surface = wl_resource_get_user_data(resource);
3185
3186 if (width <= 0 || height <= 0)
3187 return;
3188
3189 pixman_region32_union_rect(&surface->pending.damage_buffer,
3190 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04003191 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003192}
3193
Kristian Høgsberg33418202011-08-16 23:01:28 -04003194static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003195destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04003196{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05003197 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003198
3199 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02003200 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003201}
3202
3203static void
3204surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003205 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04003206{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003207 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003208 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003209
3210 cb = malloc(sizeof *cb);
3211 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003212 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003213 return;
3214 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03003215
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003216 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
3217 callback);
3218 if (cb->resource == NULL) {
3219 free(cb);
3220 wl_resource_post_no_memory(resource);
3221 return;
3222 }
3223
Jason Ekstranda85118c2013-06-27 20:17:02 -05003224 wl_resource_set_implementation(cb->resource, NULL, cb,
3225 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003226
Pekka Paalanenbc106382012-10-10 12:49:31 +03003227 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003228}
3229
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003230static void
3231surface_set_opaque_region(struct wl_client *client,
3232 struct wl_resource *resource,
3233 struct wl_resource *region_resource)
3234{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003235 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003236 struct weston_region *region;
3237
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003238 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003239 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003240 pixman_region32_copy(&surface->pending.opaque,
3241 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003242 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07003243 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003244 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003245}
3246
3247static void
3248surface_set_input_region(struct wl_client *client,
3249 struct wl_resource *resource,
3250 struct wl_resource *region_resource)
3251{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003252 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003253 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003254
3255 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003256 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003257 pixman_region32_copy(&surface->pending.input,
3258 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003259 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003260 pixman_region32_fini(&surface->pending.input);
3261 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003262 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003263}
3264
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003265/* Cause damage to this sub-surface and all its children.
3266 *
3267 * This is useful when there are state changes that need an implicit
3268 * damage, e.g. a z-order change.
3269 */
3270static void
3271weston_surface_damage_subsurfaces(struct weston_subsurface *sub)
3272{
3273 struct weston_subsurface *child;
3274
3275 weston_surface_damage(sub->surface);
3276 sub->reordered = false;
3277
3278 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link)
3279 if (child != sub)
3280 weston_surface_damage_subsurfaces(child);
3281}
3282
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003283static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003284weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003285{
Pekka Paalanene67858b2013-04-25 13:57:42 +03003286 struct weston_subsurface *sub;
3287
3288 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
3289 parent_link_pending) {
3290 wl_list_remove(&sub->parent_link);
3291 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003292
3293 if (sub->reordered)
3294 weston_surface_damage_subsurfaces(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003295 }
3296}
3297
3298static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03003299weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05003300 struct weston_matrix *matrix)
3301{
Pekka Paalanen04baea52016-04-26 15:50:58 +03003302 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05003303 double src_width, src_height, dest_width, dest_height;
3304
3305 weston_matrix_init(matrix);
3306
3307 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
3308 src_width = surface->width_from_buffer;
3309 src_height = surface->height_from_buffer;
3310 } else {
3311 src_width = wl_fixed_to_double(vp->buffer.src_width);
3312 src_height = wl_fixed_to_double(vp->buffer.src_height);
3313 }
3314
3315 if (vp->surface.width == -1) {
3316 dest_width = src_width;
3317 dest_height = src_height;
3318 } else {
3319 dest_width = vp->surface.width;
3320 dest_height = vp->surface.height;
3321 }
3322
3323 if (src_width != dest_width || src_height != dest_height)
3324 weston_matrix_scale(matrix,
3325 src_width / dest_width,
3326 src_height / dest_height, 1);
3327
3328 if (vp->buffer.src_width != wl_fixed_from_int(-1))
3329 weston_matrix_translate(matrix,
3330 wl_fixed_to_double(vp->buffer.src_x),
3331 wl_fixed_to_double(vp->buffer.src_y),
3332 0);
3333
3334 switch (vp->buffer.transform) {
3335 case WL_OUTPUT_TRANSFORM_FLIPPED:
3336 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3337 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3338 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3339 weston_matrix_scale(matrix, -1, 1, 1);
3340 weston_matrix_translate(matrix,
3341 surface->width_from_buffer, 0, 0);
3342 break;
3343 }
3344
3345 switch (vp->buffer.transform) {
3346 default:
3347 case WL_OUTPUT_TRANSFORM_NORMAL:
3348 case WL_OUTPUT_TRANSFORM_FLIPPED:
3349 break;
3350 case WL_OUTPUT_TRANSFORM_90:
3351 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3352 weston_matrix_rotate_xy(matrix, 0, 1);
3353 weston_matrix_translate(matrix,
3354 surface->height_from_buffer, 0, 0);
3355 break;
3356 case WL_OUTPUT_TRANSFORM_180:
3357 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3358 weston_matrix_rotate_xy(matrix, -1, 0);
3359 weston_matrix_translate(matrix,
3360 surface->width_from_buffer,
3361 surface->height_from_buffer, 0);
3362 break;
3363 case WL_OUTPUT_TRANSFORM_270:
3364 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3365 weston_matrix_rotate_xy(matrix, 0, -1);
3366 weston_matrix_translate(matrix,
3367 0, surface->width_from_buffer, 0);
3368 break;
3369 }
3370
3371 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
3372}
3373
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003374/**
3375 * Compute a + b > c while being safe to overflows.
3376 */
3377static bool
3378fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
3379{
3380 return (int64_t)a + (int64_t)b > (int64_t)c;
3381}
3382
3383static bool
3384weston_surface_is_pending_viewport_source_valid(
3385 const struct weston_surface *surface)
3386{
3387 const struct weston_surface_state *pend = &surface->pending;
3388 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
3389 int width_from_buffer = 0;
3390 int height_from_buffer = 0;
3391 wl_fixed_t w;
3392 wl_fixed_t h;
3393
3394 /* If viewport source rect is not set, it is always ok. */
3395 if (vp->buffer.src_width == wl_fixed_from_int(-1))
3396 return true;
3397
3398 if (pend->newly_attached) {
3399 if (pend->buffer) {
3400 convert_size_by_transform_scale(&width_from_buffer,
3401 &height_from_buffer,
3402 pend->buffer->width,
3403 pend->buffer->height,
3404 vp->buffer.transform,
3405 vp->buffer.scale);
3406 } else {
3407 /* No buffer: viewport is irrelevant. */
3408 return true;
3409 }
3410 } else {
3411 width_from_buffer = surface->width_from_buffer;
3412 height_from_buffer = surface->height_from_buffer;
3413 }
3414
3415 assert((width_from_buffer == 0) == (height_from_buffer == 0));
3416 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
3417
3418 /* No buffer: viewport is irrelevant. */
3419 if (width_from_buffer == 0 || height_from_buffer == 0)
3420 return true;
3421
3422 /* overflow checks for wl_fixed_from_int() */
3423 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
3424 return false;
3425 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
3426 return false;
3427
3428 w = wl_fixed_from_int(width_from_buffer);
3429 h = wl_fixed_from_int(height_from_buffer);
3430
3431 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
3432 return false;
3433 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
3434 return false;
3435
3436 return true;
3437}
3438
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003439static bool
3440fixed_is_integer(wl_fixed_t v)
3441{
3442 return (v & 0xff) == 0;
3443}
3444
3445static bool
3446weston_surface_is_pending_viewport_dst_size_int(
3447 const struct weston_surface *surface)
3448{
3449 const struct weston_buffer_viewport *vp =
3450 &surface->pending.buffer_viewport;
3451
3452 if (vp->surface.width != -1) {
3453 assert(vp->surface.width > 0 && vp->surface.height > 0);
3454 return true;
3455 }
3456
3457 return fixed_is_integer(vp->buffer.src_width) &&
3458 fixed_is_integer(vp->buffer.src_height);
3459}
3460
Derek Foreman152254b2015-11-26 14:17:48 -06003461/* Translate pending damage in buffer co-ordinates to surface
3462 * co-ordinates and union it with a pixman_region32_t.
3463 * This should only be called after the buffer is attached.
3464 */
3465static void
3466apply_damage_buffer(pixman_region32_t *dest,
3467 struct weston_surface *surface,
3468 struct weston_surface_state *state)
3469{
3470 struct weston_buffer *buffer = surface->buffer_ref.buffer;
3471
3472 /* wl_surface.damage_buffer needs to be clipped to the buffer,
3473 * translated into surface co-ordinates and unioned with
3474 * any other surface damage.
3475 * None of this makes sense if there is no buffer though.
3476 */
3477 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
3478 pixman_region32_t buffer_damage;
3479
3480 pixman_region32_intersect_rect(&state->damage_buffer,
3481 &state->damage_buffer,
3482 0, 0, buffer->width,
3483 buffer->height);
3484 pixman_region32_init(&buffer_damage);
3485 weston_matrix_transform_region(&buffer_damage,
3486 &surface->buffer_to_surface_matrix,
3487 &state->damage_buffer);
3488 pixman_region32_union(dest, dest, &buffer_damage);
3489 pixman_region32_fini(&buffer_damage);
3490 }
3491 /* We should clear this on commit even if there was no buffer */
3492 pixman_region32_clear(&state->damage_buffer);
3493}
3494
Jason Ekstrand1e059042014-10-16 10:55:19 -05003495static void
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303496weston_surface_set_desired_protection(struct weston_surface *surface,
3497 enum weston_hdcp_protection protection)
3498{
3499 if (surface->desired_protection == protection)
3500 return;
3501 surface->desired_protection = protection;
3502 weston_surface_damage(surface);
3503}
3504
3505static void
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303506weston_surface_set_protection_mode(struct weston_surface *surface,
3507 enum weston_surface_protection_mode p_mode)
3508{
3509 struct content_protection *cp = surface->compositor->content_protection;
3510 struct protected_surface *psurface;
3511
3512 surface->protection_mode = p_mode;
3513 wl_list_for_each(psurface, &cp->protected_list, link) {
3514 if (!psurface || psurface->surface != surface)
3515 continue;
3516 weston_protected_surface_send_event(psurface,
3517 surface->current_protection);
3518 }
3519}
3520
3521static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05003522weston_surface_commit_state(struct weston_surface *surface,
3523 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003524{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003525 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003526 pixman_region32_t opaque;
3527
Alexander Larsson4ea95522013-05-22 14:41:37 +02003528 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02003529 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03003530 /* wp_viewport.set_source */
3531 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003532 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003533
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003534 /* wl_surface.attach */
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003535 if (state->newly_attached) {
3536 /* zwp_surface_synchronization_v1.set_acquire_fence */
3537 fd_move(&surface->acquire_fence_fd,
3538 &state->acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003539 /* zwp_surface_synchronization_v1.get_release */
3540 weston_buffer_release_move(&surface->buffer_release_ref,
3541 &state->buffer_release_ref);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003542 weston_surface_attach(surface, state->buffer);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003543 }
Jason Ekstrand7b982072014-05-20 14:33:03 -05003544 weston_surface_state_set_buffer(state, NULL);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003545 assert(state->acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003546 assert(state->buffer_release_ref.buffer_release == NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01003547
Jason Ekstrand1e059042014-10-16 10:55:19 -05003548 weston_surface_build_buffer_matrix(surface,
3549 &surface->surface_to_buffer_matrix);
3550 weston_matrix_invert(&surface->buffer_to_surface_matrix,
3551 &surface->surface_to_buffer_matrix);
3552
Jason Ekstrand7b982072014-05-20 14:33:03 -05003553 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003554 weston_surface_update_size(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003555 if (surface->committed)
3556 surface->committed(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003557 }
Giulio Camuffo184df502013-02-21 11:29:21 +01003558
Jason Ekstrand7b982072014-05-20 14:33:03 -05003559 state->sx = 0;
3560 state->sy = 0;
3561 state->newly_attached = 0;
3562 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03003563
Derek Foreman152254b2015-11-26 14:17:48 -06003564 /* wl_surface.damage and wl_surface.damage_buffer */
Marius Vlad2a1b7862019-09-05 13:12:18 +03003565 if (pixman_region32_not_empty(&state->damage_surface) ||
3566 pixman_region32_not_empty(&state->damage_buffer))
Marius Vlad3203ff62019-09-05 14:56:12 +03003567 TL_POINT(surface->compositor, "core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06003568
Pekka Paalanen8e159182012-10-10 12:49:25 +03003569 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06003570 &state->damage_surface);
3571
3572 apply_damage_buffer(&surface->damage, surface, state);
3573
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05003574 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07003575 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06003576 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003577
3578 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003579 pixman_region32_init(&opaque);
3580 pixman_region32_intersect_rect(&opaque, &state->opaque,
3581 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003582
3583 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
3584 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003585 wl_list_for_each(view, &surface->views, surface_link)
3586 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003587 }
3588
3589 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003590
3591 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003592 pixman_region32_intersect_rect(&surface->input, &state->input,
3593 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003594
Pekka Paalanenbc106382012-10-10 12:49:31 +03003595 /* wl_surface.frame */
3596 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05003597 &state->frame_callback_list);
3598 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003599
3600 /* XXX:
3601 * What should happen with a feedback request, if there
3602 * is no wl_buffer attached for this commit?
3603 */
3604
3605 /* presentation.feedback */
3606 wl_list_insert_list(&surface->feedback_list,
3607 &state->feedback_list);
3608 wl_list_init(&state->feedback_list);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003609
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303610 /* weston_protected_surface.enforced/relaxed */
3611 if (surface->protection_mode != state->protection_mode)
3612 weston_surface_set_protection_mode(surface,
3613 state->protection_mode);
3614
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303615 /* weston_protected_surface.set_type */
3616 weston_surface_set_desired_protection(surface, state->desired_protection);
3617
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003618 wl_signal_emit(&surface->commit_signal, surface);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003619}
3620
3621static void
3622weston_surface_commit(struct weston_surface *surface)
3623{
3624 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003625
Pekka Paalanene67858b2013-04-25 13:57:42 +03003626 weston_surface_commit_subsurface_order(surface);
3627
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003628 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003629}
3630
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003631static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003632weston_subsurface_commit(struct weston_subsurface *sub);
3633
3634static void
3635weston_subsurface_parent_commit(struct weston_subsurface *sub,
3636 int parent_is_synchronized);
3637
3638static void
3639surface_commit(struct wl_client *client, struct wl_resource *resource)
3640{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003641 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003642 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
3643
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003644 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
3645 assert(surface->viewport_resource);
3646
3647 wl_resource_post_error(surface->viewport_resource,
3648 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
3649 "wl_surface@%d has viewport source outside buffer",
3650 wl_resource_get_id(resource));
3651 return;
3652 }
3653
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003654 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
3655 assert(surface->viewport_resource);
3656
3657 wl_resource_post_error(surface->viewport_resource,
3658 WP_VIEWPORT_ERROR_BAD_SIZE,
3659 "wl_surface@%d viewport dst size not integer",
3660 wl_resource_get_id(resource));
3661 return;
3662 }
3663
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003664 if (surface->pending.acquire_fence_fd >= 0) {
3665 assert(surface->synchronization_resource);
3666
3667 if (!surface->pending.buffer) {
3668 fd_clear(&surface->pending.acquire_fence_fd);
3669 wl_resource_post_error(surface->synchronization_resource,
3670 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3671 "wl_surface@%"PRIu32" no buffer for synchronization",
3672 wl_resource_get_id(resource));
3673 return;
3674 }
3675
3676 /* We support fences for both wp_linux_dmabuf and opaque EGL
3677 * buffers, as mandated by minor version 2 of the
3678 * zwp_linux_explicit_synchronization_v1 protocol. Since
3679 * renderers that support fences currently only support these
3680 * two buffer types plus SHM buffers, we can just check for the
3681 * SHM buffer case here.
3682 */
3683 if (wl_shm_buffer_get(surface->pending.buffer->resource)) {
3684 fd_clear(&surface->pending.acquire_fence_fd);
3685 wl_resource_post_error(surface->synchronization_resource,
3686 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_UNSUPPORTED_BUFFER,
3687 "wl_surface@%"PRIu32" unsupported buffer for synchronization",
3688 wl_resource_get_id(resource));
3689 return;
3690 }
3691 }
3692
Alexandros Frantzis67629672018-10-19 12:14:11 +03003693 if (surface->pending.buffer_release_ref.buffer_release &&
3694 !surface->pending.buffer) {
3695 assert(surface->synchronization_resource);
3696
3697 wl_resource_post_error(surface->synchronization_resource,
3698 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3699 "wl_surface@%"PRIu32" no buffer for synchronization",
3700 wl_resource_get_id(resource));
3701 return;
3702 }
3703
Pekka Paalanene67858b2013-04-25 13:57:42 +03003704 if (sub) {
3705 weston_subsurface_commit(sub);
3706 return;
3707 }
3708
3709 weston_surface_commit(surface);
3710
3711 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
3712 if (sub->surface != surface)
3713 weston_subsurface_parent_commit(sub, 0);
3714 }
3715}
3716
3717static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003718surface_set_buffer_transform(struct wl_client *client,
3719 struct wl_resource *resource, int transform)
3720{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003721 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003722
Jonny Lamba55f1392014-05-30 12:07:15 +02003723 /* if wl_output.transform grows more members this will need to be updated. */
3724 if (transform < 0 ||
3725 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
3726 wl_resource_post_error(resource,
3727 WL_SURFACE_ERROR_INVALID_TRANSFORM,
3728 "buffer transform must be a valid transform "
3729 "('%d' specified)", transform);
3730 return;
3731 }
3732
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003733 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003734 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003735}
3736
Alexander Larsson4ea95522013-05-22 14:41:37 +02003737static void
3738surface_set_buffer_scale(struct wl_client *client,
3739 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003740 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003741{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003742 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003743
Jonny Lamba55f1392014-05-30 12:07:15 +02003744 if (scale < 1) {
3745 wl_resource_post_error(resource,
3746 WL_SURFACE_ERROR_INVALID_SCALE,
3747 "buffer scale must be at least one "
3748 "('%d' specified)", scale);
3749 return;
3750 }
3751
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003752 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003753 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003754}
3755
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003756static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003757 surface_destroy,
3758 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04003759 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003760 surface_frame,
3761 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003762 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003763 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02003764 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06003765 surface_set_buffer_scale,
3766 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003767};
3768
3769static void
3770compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003771 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003772{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04003773 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003774 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003775
Kristian Høgsberg18c93002012-01-27 11:58:31 -05003776 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003777 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003778 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003779 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003780 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003781
Jason Ekstranda85118c2013-06-27 20:17:02 -05003782 surface->resource =
3783 wl_resource_create(client, &wl_surface_interface,
3784 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003785 if (surface->resource == NULL) {
3786 weston_surface_destroy(surface);
3787 wl_resource_post_no_memory(resource);
3788 return;
3789 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003790 wl_resource_set_implementation(surface->resource, &surface_interface,
3791 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003792
3793 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003794}
3795
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003796static void
3797destroy_region(struct wl_resource *resource)
3798{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003799 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003800
3801 pixman_region32_fini(&region->region);
3802 free(region);
3803}
3804
3805static void
3806region_destroy(struct wl_client *client, struct wl_resource *resource)
3807{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003808 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003809}
3810
3811static void
3812region_add(struct wl_client *client, struct wl_resource *resource,
3813 int32_t x, int32_t y, int32_t width, int32_t height)
3814{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003815 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003816
3817 pixman_region32_union_rect(&region->region, &region->region,
3818 x, y, width, height);
3819}
3820
3821static void
3822region_subtract(struct wl_client *client, struct wl_resource *resource,
3823 int32_t x, int32_t y, int32_t width, int32_t height)
3824{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003825 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003826 pixman_region32_t rect;
3827
3828 pixman_region32_init_rect(&rect, x, y, width, height);
3829 pixman_region32_subtract(&region->region, &region->region, &rect);
3830 pixman_region32_fini(&rect);
3831}
3832
3833static const struct wl_region_interface region_interface = {
3834 region_destroy,
3835 region_add,
3836 region_subtract
3837};
3838
3839static void
3840compositor_create_region(struct wl_client *client,
3841 struct wl_resource *resource, uint32_t id)
3842{
3843 struct weston_region *region;
3844
3845 region = malloc(sizeof *region);
3846 if (region == NULL) {
3847 wl_resource_post_no_memory(resource);
3848 return;
3849 }
3850
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003851 pixman_region32_init(&region->region);
3852
Jason Ekstranda85118c2013-06-27 20:17:02 -05003853 region->resource =
3854 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003855 if (region->resource == NULL) {
3856 free(region);
3857 wl_resource_post_no_memory(resource);
3858 return;
3859 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003860 wl_resource_set_implementation(region->resource, &region_interface,
3861 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003862}
3863
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003864static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003865 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003866 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003867};
3868
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003869static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003870weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3871{
3872 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003873
Jason Ekstrand7b982072014-05-20 14:33:03 -05003874 weston_surface_commit_state(surface, &sub->cached);
3875 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003876
3877 weston_surface_commit_subsurface_order(surface);
3878
3879 weston_surface_schedule_repaint(surface);
3880
Jason Ekstrand7b982072014-05-20 14:33:03 -05003881 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003882}
3883
3884static void
3885weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3886{
3887 struct weston_surface *surface = sub->surface;
3888
3889 /*
3890 * If this commit would cause the surface to move by the
3891 * attach(dx, dy) parameters, the old damage region must be
3892 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05003893 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003894 */
Derek Foreman152254b2015-11-26 14:17:48 -06003895 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003896 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06003897 pixman_region32_union(&sub->cached.damage_surface,
3898 &sub->cached.damage_surface,
3899 &surface->pending.damage_surface);
3900 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003901
3902 if (surface->pending.newly_attached) {
3903 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003904 weston_surface_state_set_buffer(&sub->cached,
3905 surface->pending.buffer);
3906 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003907 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003908 weston_presentation_feedback_discard_list(
3909 &sub->cached.feedback_list);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003910 /* zwp_surface_synchronization_v1.set_acquire_fence */
3911 fd_move(&sub->cached.acquire_fence_fd,
3912 &surface->pending.acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003913 /* zwp_surface_synchronization_v1.get_release */
3914 weston_buffer_release_move(&sub->cached.buffer_release_ref,
3915 &surface->pending.buffer_release_ref);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003916 }
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303917 sub->cached.desired_protection = surface->pending.desired_protection;
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303918 sub->cached.protection_mode = surface->pending.protection_mode;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003919 assert(surface->pending.acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003920 assert(surface->pending.buffer_release_ref.buffer_release == NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003921 sub->cached.sx += surface->pending.sx;
3922 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003923
Derek Foreman152254b2015-11-26 14:17:48 -06003924 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
3925
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003926 sub->cached.buffer_viewport.changed |=
3927 surface->pending.buffer_viewport.changed;
3928 sub->cached.buffer_viewport.buffer =
3929 surface->pending.buffer_viewport.buffer;
3930 sub->cached.buffer_viewport.surface =
3931 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003932
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003933 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003934
3935 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3936
3937 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3938
3939 wl_list_insert_list(&sub->cached.frame_callback_list,
3940 &surface->pending.frame_callback_list);
3941 wl_list_init(&surface->pending.frame_callback_list);
3942
Pekka Paalanen133e4392014-09-23 22:08:46 -04003943 wl_list_insert_list(&sub->cached.feedback_list,
3944 &surface->pending.feedback_list);
3945 wl_list_init(&surface->pending.feedback_list);
3946
Jason Ekstrand7b982072014-05-20 14:33:03 -05003947 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003948}
3949
Derek Foreman280e7dd2014-10-03 13:13:42 -05003950static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003951weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3952{
3953 while (sub) {
3954 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003955 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003956
3957 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003958 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003959
3960 sub = weston_surface_to_subsurface(sub->parent);
3961 }
3962
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003963 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003964}
3965
3966static void
3967weston_subsurface_commit(struct weston_subsurface *sub)
3968{
3969 struct weston_surface *surface = sub->surface;
3970 struct weston_subsurface *tmp;
3971
3972 /* Recursive check for effectively synchronized. */
3973 if (weston_subsurface_is_synchronized(sub)) {
3974 weston_subsurface_commit_to_cache(sub);
3975 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003976 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003977 /* flush accumulated state from cache */
3978 weston_subsurface_commit_to_cache(sub);
3979 weston_subsurface_commit_from_cache(sub);
3980 } else {
3981 weston_surface_commit(surface);
3982 }
3983
3984 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3985 if (tmp->surface != surface)
3986 weston_subsurface_parent_commit(tmp, 0);
3987 }
3988 }
3989}
3990
3991static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003992weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003993{
3994 struct weston_surface *surface = sub->surface;
3995 struct weston_subsurface *tmp;
3996
Pekka Paalanene67858b2013-04-25 13:57:42 +03003997 /* From now on, commit_from_cache the whole sub-tree, regardless of
3998 * the synchronized mode of each child. This sub-surface or some
3999 * of its ancestors were synchronized, so we are synchronized
4000 * all the way down.
4001 */
4002
Jason Ekstrand7b982072014-05-20 14:33:03 -05004003 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004004 weston_subsurface_commit_from_cache(sub);
4005
4006 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
4007 if (tmp->surface != surface)
4008 weston_subsurface_parent_commit(tmp, 1);
4009 }
4010}
4011
4012static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004013weston_subsurface_parent_commit(struct weston_subsurface *sub,
4014 int parent_is_synchronized)
4015{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004016 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004017 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004018 wl_list_for_each(view, &sub->surface->views, surface_link)
4019 weston_view_set_position(view,
4020 sub->position.x,
4021 sub->position.y);
4022
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004023 sub->position.set = 0;
4024 }
4025
4026 if (parent_is_synchronized || sub->synchronized)
4027 weston_subsurface_synchronized_commit(sub);
4028}
4029
Pekka Paalanen8274d902014-08-06 19:36:51 +03004030static int
4031subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
4032{
4033 return snprintf(buf, len, "sub-surface");
4034}
4035
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004036static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004037subsurface_committed(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004038{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004039 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004040
Jason Ekstranda7af7042013-10-12 22:38:11 -05004041 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004042 weston_view_set_position(view,
4043 view->geometry.x + dx,
4044 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004045
4046 /* No need to check parent mappedness, because if parent is not
4047 * mapped, parent is not in a visible layer, so this sub-surface
4048 * will not be drawn either.
4049 */
Armin Krezovićf8486c32016-06-30 06:04:28 +02004050
Pekka Paalanene67858b2013-04-25 13:57:42 +03004051 if (!weston_surface_is_mapped(surface)) {
Armin Krezovićf8486c32016-06-30 06:04:28 +02004052 surface->is_mapped = true;
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03004053
Derek Foreman4b1a0a12014-09-10 15:37:33 -05004054 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03004055 * because that would call it also for the parent surface,
4056 * which might not be mapped yet. That would lead to
4057 * inconsistent state, where the window could never be
4058 * mapped.
4059 *
Armin Krezovićf8486c32016-06-30 06:04:28 +02004060 * Instead just force the is_mapped flag on, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03004061 * weston_surface_is_mapped() return true, so that when the
4062 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03004063 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03004064 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004065 }
4066}
4067
4068static struct weston_subsurface *
4069weston_surface_to_subsurface(struct weston_surface *surface)
4070{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004071 if (surface->committed == subsurface_committed)
4072 return surface->committed_private;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004073
4074 return NULL;
4075}
4076
Pekka Paalanen01388e22013-04-25 13:57:44 +03004077WL_EXPORT struct weston_surface *
4078weston_surface_get_main_surface(struct weston_surface *surface)
4079{
4080 struct weston_subsurface *sub;
4081
4082 while (surface && (sub = weston_surface_to_subsurface(surface)))
4083 surface = sub->parent;
4084
4085 return surface;
4086}
4087
Pekka Paalanen50b67472014-10-01 15:02:41 +03004088WL_EXPORT int
4089weston_surface_set_role(struct weston_surface *surface,
4090 const char *role_name,
4091 struct wl_resource *error_resource,
4092 uint32_t error_code)
4093{
4094 assert(role_name);
4095
4096 if (surface->role_name == NULL ||
4097 surface->role_name == role_name ||
4098 strcmp(surface->role_name, role_name) == 0) {
4099 surface->role_name = role_name;
4100
4101 return 0;
4102 }
4103
4104 wl_resource_post_error(error_resource, error_code,
4105 "Cannot assign role %s to wl_surface@%d,"
4106 " already has role %s\n",
4107 role_name,
4108 wl_resource_get_id(surface->resource),
4109 surface->role_name);
4110 return -1;
4111}
4112
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02004113WL_EXPORT const char *
4114weston_surface_get_role(struct weston_surface *surface)
4115{
4116 return surface->role_name;
4117}
4118
Pekka Paalanen8274d902014-08-06 19:36:51 +03004119WL_EXPORT void
4120weston_surface_set_label_func(struct weston_surface *surface,
4121 int (*desc)(struct weston_surface *,
4122 char *, size_t))
4123{
4124 surface->get_label = desc;
Marius Vlad5de92972019-10-15 13:25:41 +03004125 weston_timeline_refresh_subscription_objects(surface->compositor,
4126 surface);
Pekka Paalanen8274d902014-08-06 19:36:51 +03004127}
4128
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004129/** Get the size of surface contents
4130 *
4131 * \param surface The surface to query.
4132 * \param width Returns the width of raw contents.
4133 * \param height Returns the height of raw contents.
4134 *
4135 * Retrieves the raw surface content size in pixels for the given surface.
4136 * This is the whole content size in buffer pixels. If the surface
4137 * has no content or the renderer does not implement this feature,
4138 * zeroes are returned.
4139 *
4140 * This function is used to determine the buffer size needed for
4141 * a weston_surface_copy_content() call.
4142 */
4143WL_EXPORT void
4144weston_surface_get_content_size(struct weston_surface *surface,
4145 int *width, int *height)
4146{
4147 struct weston_renderer *rer = surface->compositor->renderer;
4148
4149 if (!rer->surface_get_content_size) {
4150 *width = 0;
4151 *height = 0;
4152 return;
4153 }
4154
4155 rer->surface_get_content_size(surface, width, height);
4156}
4157
Quentin Glidic248dd102016-08-12 10:41:34 +02004158/** Get the bounding box of a surface and its subsurfaces
4159 *
4160 * \param surface The surface to query.
4161 * \return The bounding box relative to the surface origin.
4162 *
4163 */
4164WL_EXPORT struct weston_geometry
4165weston_surface_get_bounding_box(struct weston_surface *surface)
4166{
4167 pixman_region32_t region;
4168 pixman_box32_t *box;
4169 struct weston_subsurface *subsurface;
4170
4171 pixman_region32_init_rect(&region,
4172 0, 0,
4173 surface->width, surface->height);
4174
4175 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link)
4176 pixman_region32_union_rect(&region, &region,
4177 subsurface->position.x,
4178 subsurface->position.y,
4179 subsurface->surface->width,
4180 subsurface->surface->height);
4181
4182 box = pixman_region32_extents(&region);
4183 struct weston_geometry geometry = {
4184 .x = box->x1,
4185 .y = box->y1,
4186 .width = box->x2 - box->x1,
4187 .height = box->y2 - box->y1,
4188 };
4189
4190 pixman_region32_fini(&region);
4191
4192 return geometry;
4193}
4194
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004195/** Copy surface contents to system memory.
4196 *
4197 * \param surface The surface to copy from.
4198 * \param target Pointer to the target memory buffer.
4199 * \param size Size of the target buffer in bytes.
4200 * \param src_x X location on contents to copy from.
4201 * \param src_y Y location on contents to copy from.
4202 * \param width Width in pixels of the area to copy.
4203 * \param height Height in pixels of the area to copy.
4204 * \return 0 for success, -1 for failure.
4205 *
4206 * Surface contents are maintained by the renderer. They can be in a
4207 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
4208 * else.
4209 *
4210 * Surface contents are copied into memory pointed to by target,
4211 * which has size bytes of space available. The target memory
4212 * may be larger than needed, but being smaller returns an error.
4213 * The extra bytes in target may or may not be written; their content is
4214 * unspecified. Size must be large enough to hold the image.
4215 *
4216 * The image in the target memory will be arranged in rows from
4217 * top to bottom, and pixels on a row from left to right. The pixel
4218 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
4219 * width * 4.
4220 *
4221 * Parameters src_x and src_y define the upper-left corner in buffer
4222 * coordinates (pixels) to copy from. Parameters width and height
4223 * define the size of the area to copy in pixels.
4224 *
4225 * The rectangle defined by src_x, src_y, width, height must fit in
4226 * the surface contents. Otherwise an error is returned.
4227 *
Changwoo Chof97d2502017-08-05 00:30:47 +09004228 * Use weston_surface_get_content_size to determine the content size; the
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004229 * needed target buffer size and rectangle limits.
4230 *
4231 * CURRENT IMPLEMENTATION RESTRICTIONS:
4232 * - the machine must be little-endian due to Pixman formats.
4233 *
4234 * NOTE: Pixman formats are premultiplied.
4235 */
4236WL_EXPORT int
4237weston_surface_copy_content(struct weston_surface *surface,
4238 void *target, size_t size,
4239 int src_x, int src_y,
4240 int width, int height)
4241{
4242 struct weston_renderer *rer = surface->compositor->renderer;
4243 int cw, ch;
4244 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
4245
4246 if (!rer->surface_copy_content)
4247 return -1;
4248
4249 weston_surface_get_content_size(surface, &cw, &ch);
4250
4251 if (src_x < 0 || src_y < 0)
4252 return -1;
4253
4254 if (width <= 0 || height <= 0)
4255 return -1;
4256
4257 if (src_x + width > cw || src_y + height > ch)
4258 return -1;
4259
4260 if (width * bytespp * height > size)
4261 return -1;
4262
4263 return rer->surface_copy_content(surface, target, size,
4264 src_x, src_y, width, height);
4265}
4266
Pekka Paalanene67858b2013-04-25 13:57:42 +03004267static void
4268subsurface_set_position(struct wl_client *client,
4269 struct wl_resource *resource, int32_t x, int32_t y)
4270{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004271 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004272
4273 if (!sub)
4274 return;
4275
4276 sub->position.x = x;
4277 sub->position.y = y;
4278 sub->position.set = 1;
4279}
4280
4281static struct weston_subsurface *
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004282subsurface_find_sibling(struct weston_subsurface *sub,
4283 struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004284{
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004285 struct weston_surface *parent = sub->parent;
4286 struct weston_subsurface *sibling;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004287
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004288 wl_list_for_each(sibling, &parent->subsurface_list, parent_link) {
4289 if (sibling->surface == surface && sibling != sub)
4290 return sibling;
4291 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004292
4293 return NULL;
4294}
4295
4296static struct weston_subsurface *
4297subsurface_sibling_check(struct weston_subsurface *sub,
4298 struct weston_surface *surface,
4299 const char *request)
4300{
4301 struct weston_subsurface *sibling;
4302
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004303 sibling = subsurface_find_sibling(sub, surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004304 if (!sibling) {
4305 wl_resource_post_error(sub->resource,
4306 WL_SUBSURFACE_ERROR_BAD_SURFACE,
4307 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004308 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004309 return NULL;
4310 }
4311
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004312 assert(sibling->parent == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004313
4314 return sibling;
4315}
4316
4317static void
4318subsurface_place_above(struct wl_client *client,
4319 struct wl_resource *resource,
4320 struct wl_resource *sibling_resource)
4321{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004322 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004323 struct weston_surface *surface =
4324 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004325 struct weston_subsurface *sibling;
4326
4327 if (!sub)
4328 return;
4329
4330 sibling = subsurface_sibling_check(sub, surface, "place_above");
4331 if (!sibling)
4332 return;
4333
4334 wl_list_remove(&sub->parent_link_pending);
4335 wl_list_insert(sibling->parent_link_pending.prev,
4336 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004337
4338 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004339}
4340
4341static void
4342subsurface_place_below(struct wl_client *client,
4343 struct wl_resource *resource,
4344 struct wl_resource *sibling_resource)
4345{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004346 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004347 struct weston_surface *surface =
4348 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004349 struct weston_subsurface *sibling;
4350
4351 if (!sub)
4352 return;
4353
4354 sibling = subsurface_sibling_check(sub, surface, "place_below");
4355 if (!sibling)
4356 return;
4357
4358 wl_list_remove(&sub->parent_link_pending);
4359 wl_list_insert(&sibling->parent_link_pending,
4360 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004361
4362 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004363}
4364
4365static void
4366subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
4367{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004368 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004369
4370 if (sub)
4371 sub->synchronized = 1;
4372}
4373
4374static void
4375subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
4376{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004377 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004378
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004379 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03004380 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004381
4382 /* If sub became effectively desynchronized, flush. */
4383 if (!weston_subsurface_is_synchronized(sub))
4384 weston_subsurface_synchronized_commit(sub);
4385 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004386}
4387
4388static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03004389weston_subsurface_unlink_parent(struct weston_subsurface *sub)
4390{
4391 wl_list_remove(&sub->parent_link);
4392 wl_list_remove(&sub->parent_link_pending);
4393 wl_list_remove(&sub->parent_destroy_listener.link);
4394 sub->parent = NULL;
4395}
4396
4397static void
4398weston_subsurface_destroy(struct weston_subsurface *sub);
4399
4400static void
4401subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
4402{
4403 struct weston_subsurface *sub =
4404 container_of(listener, struct weston_subsurface,
4405 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004406 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004407
4408 /* The protocol object (wl_resource) is left inert. */
4409 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004410 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004411
4412 weston_subsurface_destroy(sub);
4413}
4414
4415static void
4416subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
4417{
4418 struct weston_subsurface *sub =
4419 container_of(listener, struct weston_subsurface,
4420 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004421 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004422 assert(sub->surface != sub->parent);
4423
4424 if (weston_surface_is_mapped(sub->surface))
4425 weston_surface_unmap(sub->surface);
4426
4427 weston_subsurface_unlink_parent(sub);
4428}
4429
4430static void
4431subsurface_resource_destroy(struct wl_resource *resource)
4432{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004433 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004434
4435 if (sub)
4436 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004437}
4438
4439static void
4440subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
4441{
4442 wl_resource_destroy(resource);
4443}
4444
4445static void
4446weston_subsurface_link_parent(struct weston_subsurface *sub,
4447 struct weston_surface *parent)
4448{
4449 sub->parent = parent;
4450 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004451 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004452 &sub->parent_destroy_listener);
4453
4454 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4455 wl_list_insert(&parent->subsurface_list_pending,
4456 &sub->parent_link_pending);
4457}
4458
4459static void
4460weston_subsurface_link_surface(struct weston_subsurface *sub,
4461 struct weston_surface *surface)
4462{
4463 sub->surface = surface;
4464 sub->surface_destroy_listener.notify =
4465 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004466 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004467 &sub->surface_destroy_listener);
4468}
4469
4470static void
4471weston_subsurface_destroy(struct weston_subsurface *sub)
4472{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004473 struct weston_view *view, *next;
4474
Pekka Paalanene67858b2013-04-25 13:57:42 +03004475 assert(sub->surface);
4476
4477 if (sub->resource) {
4478 assert(weston_surface_to_subsurface(sub->surface) == sub);
4479 assert(sub->parent_destroy_listener.notify ==
4480 subsurface_handle_parent_destroy);
4481
George Kiagiadakised04d382014-06-13 18:10:26 +02004482 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
4483 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004484 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02004485 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05004486
Pekka Paalanene67858b2013-04-25 13:57:42 +03004487 if (sub->parent)
4488 weston_subsurface_unlink_parent(sub);
4489
Jason Ekstrand7b982072014-05-20 14:33:03 -05004490 weston_surface_state_fini(&sub->cached);
4491 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004492
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004493 sub->surface->committed = NULL;
4494 sub->surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004495 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004496 } else {
4497 /* the dummy weston_subsurface for the parent itself */
4498 assert(sub->parent_destroy_listener.notify == NULL);
4499 wl_list_remove(&sub->parent_link);
4500 wl_list_remove(&sub->parent_link_pending);
4501 }
4502
4503 wl_list_remove(&sub->surface_destroy_listener.link);
4504 free(sub);
4505}
4506
4507static const struct wl_subsurface_interface subsurface_implementation = {
4508 subsurface_destroy,
4509 subsurface_set_position,
4510 subsurface_place_above,
4511 subsurface_place_below,
4512 subsurface_set_sync,
4513 subsurface_set_desync
4514};
4515
4516static struct weston_subsurface *
4517weston_subsurface_create(uint32_t id, struct weston_surface *surface,
4518 struct weston_surface *parent)
4519{
4520 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004521 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004522
Bryce Harringtonde16d892014-11-20 22:21:57 -08004523 sub = zalloc(sizeof *sub);
4524 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004525 return NULL;
4526
Jason Ekstranda7af7042013-10-12 22:38:11 -05004527 wl_list_init(&sub->unused_views);
4528
Jason Ekstranda85118c2013-06-27 20:17:02 -05004529 sub->resource =
4530 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004531 if (!sub->resource) {
4532 free(sub);
4533 return NULL;
4534 }
4535
Jason Ekstranda85118c2013-06-27 20:17:02 -05004536 wl_resource_set_implementation(sub->resource,
4537 &subsurface_implementation,
4538 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004539 weston_subsurface_link_surface(sub, surface);
4540 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05004541 weston_surface_state_init(&sub->cached);
4542 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004543 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004544
4545 return sub;
4546}
4547
4548/* Create a dummy subsurface for having the parent itself in its
4549 * sub-surface lists. Makes stacking order manipulation easy.
4550 */
4551static struct weston_subsurface *
4552weston_subsurface_create_for_parent(struct weston_surface *parent)
4553{
4554 struct weston_subsurface *sub;
4555
Bryce Harringtonde16d892014-11-20 22:21:57 -08004556 sub = zalloc(sizeof *sub);
4557 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004558 return NULL;
4559
4560 weston_subsurface_link_surface(sub, parent);
4561 sub->parent = parent;
4562 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4563 wl_list_insert(&parent->subsurface_list_pending,
4564 &sub->parent_link_pending);
4565
4566 return sub;
4567}
4568
4569static void
4570subcompositor_get_subsurface(struct wl_client *client,
4571 struct wl_resource *resource,
4572 uint32_t id,
4573 struct wl_resource *surface_resource,
4574 struct wl_resource *parent_resource)
4575{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004576 struct weston_surface *surface =
4577 wl_resource_get_user_data(surface_resource);
4578 struct weston_surface *parent =
4579 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004580 struct weston_subsurface *sub;
4581 static const char where[] = "get_subsurface: wl_subsurface@";
4582
4583 if (surface == parent) {
4584 wl_resource_post_error(resource,
4585 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4586 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004587 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004588 return;
4589 }
4590
4591 if (weston_surface_to_subsurface(surface)) {
4592 wl_resource_post_error(resource,
4593 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4594 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004595 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004596 return;
4597 }
4598
Pekka Paalanen50b67472014-10-01 15:02:41 +03004599 if (weston_surface_set_role(surface, "wl_subsurface", resource,
4600 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004601 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004602
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004603 if (weston_surface_get_main_surface(parent) == surface) {
4604 wl_resource_post_error(resource,
4605 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4606 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004607 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004608 return;
4609 }
4610
Pekka Paalanene67858b2013-04-25 13:57:42 +03004611 /* make sure the parent is in its own list */
4612 if (wl_list_empty(&parent->subsurface_list)) {
4613 if (!weston_subsurface_create_for_parent(parent)) {
4614 wl_resource_post_no_memory(resource);
4615 return;
4616 }
4617 }
4618
4619 sub = weston_subsurface_create(id, surface, parent);
4620 if (!sub) {
4621 wl_resource_post_no_memory(resource);
4622 return;
4623 }
4624
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004625 surface->committed = subsurface_committed;
4626 surface->committed_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004627 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004628}
4629
4630static void
4631subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
4632{
4633 wl_resource_destroy(resource);
4634}
4635
4636static const struct wl_subcompositor_interface subcompositor_interface = {
4637 subcompositor_destroy,
4638 subcompositor_get_subsurface
4639};
4640
4641static void
4642bind_subcompositor(struct wl_client *client,
4643 void *data, uint32_t version, uint32_t id)
4644{
4645 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004646 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004647
Jason Ekstranda85118c2013-06-27 20:17:02 -05004648 resource =
4649 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004650 if (resource == NULL) {
4651 wl_client_post_no_memory(client);
4652 return;
4653 }
4654 wl_resource_set_implementation(resource, &subcompositor_interface,
4655 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004656}
4657
Bryce Harrington0795ece2016-08-30 12:04:26 -07004658/** Set a DPMS mode on all of the compositor's outputs
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004659 *
4660 * \param compositor The compositor instance
4661 * \param state The DPMS state the outputs will be set to
4662 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004663static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004664weston_compositor_dpms(struct weston_compositor *compositor,
4665 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004666{
4667 struct weston_output *output;
4668
Bryce Harrington08976ac2016-08-30 12:05:16 -07004669 wl_list_for_each(output, &compositor->output_list, link)
4670 if (output->set_dpms)
4671 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004672}
4673
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004674/** Restores the compositor to active status
4675 *
4676 * \param compositor The compositor instance
4677 *
4678 * If the compositor was in a sleeping mode, all outputs are powered
4679 * back on via DPMS. Otherwise if the compositor was inactive
4680 * (idle/locked, offscreen, or sleeping) then the compositor's wake
4681 * signal will fire.
4682 *
4683 * Restarts the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004684 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004685 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004686WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004687weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004688{
Neil Roberts8b62e202013-09-30 13:14:47 +01004689 uint32_t old_state = compositor->state;
4690
4691 /* The state needs to be changed before emitting the wake
4692 * signal because that may try to schedule a repaint which
4693 * will not work if the compositor is still sleeping */
4694 compositor->state = WESTON_COMPOSITOR_ACTIVE;
4695
4696 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004697 case WESTON_COMPOSITOR_SLEEPING:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004698 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004699 case WESTON_COMPOSITOR_OFFSCREEN:
Daniel Stone893b9362016-11-08 15:47:09 +00004700 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004701 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004702 /* fall through */
4703 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004704 wl_event_source_timer_update(compositor->idle_source,
4705 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004706 }
4707}
4708
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004709/** Turns off rendering and frame events for the compositor.
4710 *
4711 * \param compositor The compositor instance
4712 *
4713 * This is used for example to prevent further rendering while the
4714 * compositor is shutting down.
4715 *
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004716 * Stops the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004717 *
4718 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004719 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004720WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004721weston_compositor_offscreen(struct weston_compositor *compositor)
4722{
4723 switch (compositor->state) {
4724 case WESTON_COMPOSITOR_OFFSCREEN:
4725 return;
4726 case WESTON_COMPOSITOR_SLEEPING:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004727 default:
4728 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4729 wl_event_source_timer_update(compositor->idle_source, 0);
4730 }
4731}
4732
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004733/** Powers down all attached output devices
4734 *
4735 * \param compositor The compositor instance
4736 *
4737 * Causes rendering to the outputs to cease, and no frame events to be
4738 * sent. Only powers down the outputs if the compositor is not already
4739 * in sleep mode.
4740 *
4741 * Stops the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004742 *
4743 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004744 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004745WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004746weston_compositor_sleep(struct weston_compositor *compositor)
4747{
4748 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
4749 return;
4750
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004751 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004752 compositor->state = WESTON_COMPOSITOR_SLEEPING;
4753 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
4754}
4755
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004756/** Sets compositor to idle mode
4757 *
4758 * \param data The compositor instance
4759 *
4760 * This is called when the idle timer fires. Once the compositor is in
4761 * idle mode it requires a wake action (e.g. via
4762 * weston_compositor_wake()) to restore it. The compositor's
4763 * idle_signal will be triggered when the idle event occurs.
4764 *
4765 * Idleness can be inhibited by setting the compositor's idle_inhibit
4766 * property.
4767 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004768static int
4769idle_handler(void *data)
4770{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004771 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004772
4773 if (compositor->idle_inhibit)
4774 return 1;
4775
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004776 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004777 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004778
4779 return 1;
4780}
4781
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004782WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08004783weston_plane_init(struct weston_plane *plane,
4784 struct weston_compositor *ec,
4785 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004786{
4787 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004788 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004789 plane->x = x;
4790 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08004791 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004792
4793 /* Init the link so that the call to wl_list_remove() when releasing
4794 * the plane without ever stacking doesn't lead to a crash */
4795 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004796}
4797
4798WL_EXPORT void
4799weston_plane_release(struct weston_plane *plane)
4800{
Xiong Zhang97116532013-10-23 13:58:31 +08004801 struct weston_view *view;
4802
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004803 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004804 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004805
Xiong Zhang97116532013-10-23 13:58:31 +08004806 wl_list_for_each(view, &plane->compositor->view_list, link) {
4807 if (view->plane == plane)
4808 view->plane = NULL;
4809 }
4810
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004811 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004812}
4813
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004814/** weston_compositor_stack_plane
4815 * \ingroup compositor
4816 */
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004817WL_EXPORT void
4818weston_compositor_stack_plane(struct weston_compositor *ec,
4819 struct weston_plane *plane,
4820 struct weston_plane *above)
4821{
4822 if (above)
4823 wl_list_insert(above->link.prev, &plane->link);
4824 else
4825 wl_list_insert(&ec->plane_list, &plane->link);
4826}
4827
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004828static void
4829output_release(struct wl_client *client, struct wl_resource *resource)
4830{
4831 wl_resource_destroy(resource);
4832}
4833
4834static const struct wl_output_interface output_interface = {
4835 output_release,
4836};
4837
4838
Casey Dahlin9074db52012-04-19 22:50:09 -04004839static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004840{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004841 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004842}
4843
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004844static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004845bind_output(struct wl_client *client,
4846 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05004847{
Pekka Paalanen05347622017-03-27 12:24:34 +03004848 struct weston_head *head = data;
4849 struct weston_output *output = head->output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004850 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004851 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004852
Jason Ekstranda85118c2013-06-27 20:17:02 -05004853 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004854 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004855 if (resource == NULL) {
4856 wl_client_post_no_memory(client);
4857 return;
4858 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004859
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03004860 wl_list_insert(&head->resource_list, wl_resource_get_link(resource));
Pekka Paalanen055c1132017-03-27 16:31:25 +03004861 wl_resource_set_implementation(resource, &output_interface, head,
Pekka Paalanen05347622017-03-27 12:24:34 +03004862 unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04004863
Pekka Paalanen05347622017-03-27 12:24:34 +03004864 assert(output);
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004865 wl_output_send_geometry(resource,
4866 output->x,
4867 output->y,
Pekka Paalanen01f60212017-03-24 15:39:24 +02004868 head->mm_width,
4869 head->mm_height,
4870 head->subpixel,
4871 head->make, head->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04004872 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004873 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004874 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02004875 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004876
4877 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004878 wl_output_send_mode(resource,
4879 mode->flags,
4880 mode->width,
4881 mode->height,
4882 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004883 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02004884
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004885 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004886 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004887}
4888
Pekka Paalanendcac3512017-12-08 14:13:34 +02004889static void
4890weston_head_add_global(struct weston_head *head)
4891{
4892 head->global = wl_global_create(head->compositor->wl_display,
4893 &wl_output_interface, 3,
4894 head, bind_output);
4895}
4896
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02004897/** Remove the global wl_output protocol object
4898 *
4899 * \param head The head whose global to remove.
4900 *
4901 * Also orphans the wl_resources for this head (wl_output).
4902 */
4903static void
4904weston_head_remove_global(struct weston_head *head)
4905{
4906 struct wl_resource *resource, *tmp;
4907
4908 if (head->global)
4909 wl_global_destroy(head->global);
4910 head->global = NULL;
4911
4912 wl_resource_for_each_safe(resource, tmp, &head->resource_list) {
4913 unbind_resource(resource);
4914 wl_resource_set_destructor(resource, NULL);
4915 wl_resource_set_user_data(resource, NULL);
4916 }
Roman Gilge97391c2019-03-29 13:01:06 +01004917
4918 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
4919 /* It's sufficient to unset the destructor, then the list elements
4920 * won't be accessed.
4921 */
4922 wl_resource_set_destructor(resource, NULL);
4923 }
4924 wl_list_init(&head->xdg_output_resource_list);
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02004925}
4926
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004927/** Get the backing object of wl_output
4928 *
4929 * \param resource A wl_output protocol object.
4930 * \return The backing object (user data) of a wl_resource representing a
4931 * wl_output protocol object.
Marius Vlad78984ee2019-06-11 00:05:08 +03004932 *
4933 * \ingroup head
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004934 */
Pekka Paalanen055c1132017-03-27 16:31:25 +03004935WL_EXPORT struct weston_head *
4936weston_head_from_resource(struct wl_resource *resource)
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004937{
4938 assert(wl_resource_instance_of(resource, &wl_output_interface,
4939 &output_interface));
4940
4941 return wl_resource_get_user_data(resource);
4942}
4943
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004944/** Initialize a pre-allocated weston_head
4945 *
4946 * \param head The head to initialize.
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004947 * \param name The head name, e.g. the connector name or equivalent.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004948 *
4949 * The head will be safe to attach, detach and release.
4950 *
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004951 * The name is used in logs, and can be used by compositors as a configuration
4952 * identifier.
4953 *
Marius Vlad78984ee2019-06-11 00:05:08 +03004954 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004955 * \internal
4956 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004957WL_EXPORT void
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004958weston_head_init(struct weston_head *head, const char *name)
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004959{
4960 /* Add some (in)sane defaults which can be used
4961 * for checking if an output was properly configured
4962 */
4963 memset(head, 0, sizeof *head);
4964
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004965 wl_list_init(&head->compositor_link);
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03004966 wl_signal_init(&head->destroy_signal);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004967 wl_list_init(&head->output_link);
4968 wl_list_init(&head->resource_list);
Roman Gilge97391c2019-03-29 13:01:06 +01004969 wl_list_init(&head->xdg_output_resource_list);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004970 head->name = strdup(name);
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05304971 head->current_protection = WESTON_HDCP_DISABLE;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004972}
4973
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004974/** Send output heads changed signal
4975 *
4976 * \param output The output that changed.
4977 *
4978 * Notify that the enabled output gained and/or lost heads, or that the
4979 * associated heads may have changed their connection status. This does not
4980 * include cases where the output becomes enabled or disabled. The registered
4981 * callbacks are called after the change has successfully happened.
4982 *
4983 * If connection status change causes the compositor to attach or detach a head
4984 * to an enabled output, the registered callbacks may be called multiple times.
Marius Vlad55d87362019-06-11 01:15:35 +03004985 *
4986 * \ingroup output
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004987 */
4988static void
4989weston_output_emit_heads_changed(struct weston_output *output)
4990{
4991 wl_signal_emit(&output->compositor->output_heads_changed_signal,
4992 output);
4993}
4994
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004995/** Idle task for emitting heads_changed_signal */
4996static void
4997weston_compositor_call_heads_changed(void *data)
4998{
4999 struct weston_compositor *compositor = data;
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005000 struct weston_head *head;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005001
5002 compositor->heads_changed_source = NULL;
5003
5004 wl_signal_emit(&compositor->heads_changed_signal, compositor);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005005
5006 wl_list_for_each(head, &compositor->head_list, compositor_link) {
5007 if (head->output && head->output->enabled)
5008 weston_output_emit_heads_changed(head->output);
5009 }
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005010}
5011
5012/** Schedule a call on idle to heads_changed callback
5013 *
5014 * \param compositor The Compositor.
5015 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005016 * \ingroup compositor
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005017 * \internal
5018 */
5019static void
5020weston_compositor_schedule_heads_changed(struct weston_compositor *compositor)
5021{
5022 struct wl_event_loop *loop;
5023
5024 if (compositor->heads_changed_source)
5025 return;
5026
5027 loop = wl_display_get_event_loop(compositor->wl_display);
5028 compositor->heads_changed_source = wl_event_loop_add_idle(loop,
5029 weston_compositor_call_heads_changed, compositor);
5030}
5031
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005032/** Register a new head
5033 *
5034 * \param compositor The compositor.
5035 * \param head The head to register, must not be already registered.
5036 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005037 * This signals the core that a new head has become available, leading to
5038 * heads_changed hook being called later.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005039 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005040 * \ingroup compositor
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005041 * \internal
5042 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005043WL_EXPORT void
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005044weston_compositor_add_head(struct weston_compositor *compositor,
5045 struct weston_head *head)
5046{
5047 assert(wl_list_empty(&head->compositor_link));
5048 assert(head->name);
5049
5050 wl_list_insert(compositor->head_list.prev, &head->compositor_link);
5051 head->compositor = compositor;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005052 weston_compositor_schedule_heads_changed(compositor);
5053}
5054
5055/** Adds a listener to be called when heads change
5056 *
5057 * \param compositor The compositor.
5058 * \param listener The listener to add.
5059 *
Marius Vlada2dace22019-06-12 16:05:44 +03005060 * The listener notify function argument is weston_compositor.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005061 *
5062 * The listener function will be called after heads are added or their
5063 * connection status has changed. Several changes may be accumulated into a
5064 * single call. The user is expected to iterate over the existing heads and
5065 * check their statuses to find out what changed.
5066 *
5067 * \sa weston_compositor_iterate_heads, weston_head_is_connected,
5068 * weston_head_is_enabled
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005069 * \ingroup compositor
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005070 */
5071WL_EXPORT void
5072weston_compositor_add_heads_changed_listener(struct weston_compositor *compositor,
5073 struct wl_listener *listener)
5074{
5075 wl_signal_add(&compositor->heads_changed_signal, listener);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005076}
5077
5078/** Iterate over available heads
5079 *
5080 * \param compositor The compositor.
Marius Vlada2dace22019-06-12 16:05:44 +03005081 * \param iter The iterator, or NULL for start.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005082 * \return The next available head in the list.
5083 *
5084 * Returns all available heads, regardless of being connected or enabled.
5085 *
5086 * You can iterate over all heads as follows:
5087 * \code
5088 * struct weston_head *head = NULL;
5089 *
5090 * while ((head = weston_compositor_iterate_heads(compositor, head))) {
5091 * ...
5092 * }
5093 * \endcode
5094 *
5095 * If you cause \c iter to be removed from the list, you cannot use it to
5096 * continue iterating. Removing any other item is safe.
5097 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005098 * \ingroup compositor
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005099 */
5100WL_EXPORT struct weston_head *
5101weston_compositor_iterate_heads(struct weston_compositor *compositor,
5102 struct weston_head *iter)
5103{
5104 struct wl_list *list = &compositor->head_list;
5105 struct wl_list *node;
5106
5107 assert(compositor);
5108 assert(!iter || iter->compositor == compositor);
5109
5110 if (iter)
5111 node = iter->compositor_link.next;
5112 else
5113 node = list->next;
5114
5115 assert(node);
5116 assert(!iter || node != &iter->compositor_link);
5117
5118 if (node == list)
5119 return NULL;
5120
5121 return container_of(node, struct weston_head, compositor_link);
5122}
5123
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005124/** Iterate over attached heads
5125 *
5126 * \param output The output whose heads to iterate.
Marius Vlada2dace22019-06-12 16:05:44 +03005127 * \param iter The iterator, or NULL for start.
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005128 * \return The next attached head in the list.
5129 *
5130 * Returns all heads currently attached to the output.
5131 *
5132 * You can iterate over all heads as follows:
5133 * \code
5134 * struct weston_head *head = NULL;
5135 *
5136 * while ((head = weston_output_iterate_heads(output, head))) {
5137 * ...
5138 * }
5139 * \endcode
5140 *
5141 * If you cause \c iter to be removed from the list, you cannot use it to
5142 * continue iterating. Removing any other item is safe.
5143 *
Marius Vlad55d87362019-06-11 01:15:35 +03005144 * \ingroup ouput
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005145 */
5146WL_EXPORT struct weston_head *
5147weston_output_iterate_heads(struct weston_output *output,
5148 struct weston_head *iter)
5149{
5150 struct wl_list *list = &output->head_list;
5151 struct wl_list *node;
5152
5153 assert(output);
5154 assert(!iter || iter->output == output);
5155
5156 if (iter)
5157 node = iter->output_link.next;
5158 else
5159 node = list->next;
5160
5161 assert(node);
5162 assert(!iter || node != &iter->output_link);
5163
5164 if (node == list)
5165 return NULL;
5166
5167 return container_of(node, struct weston_head, output_link);
5168}
5169
Pekka Paalanendcac3512017-12-08 14:13:34 +02005170/** Attach a head to an output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005171 *
5172 * \param output The output to attach to.
5173 * \param head The head that is not yet attached.
5174 * \return 0 on success, -1 on failure.
5175 *
5176 * Attaches the given head to the output. All heads of an output are clones
5177 * and share the resolution and timings.
5178 *
5179 * Cloning heads this way uses less resources than creating an output for
5180 * each head, but is not always possible due to environment, driver and hardware
5181 * limitations.
5182 *
5183 * On failure, the head remains unattached. Success of this function does not
5184 * guarantee the output configuration is actually valid. The final checks are
Pekka Paalanendcac3512017-12-08 14:13:34 +02005185 * made on weston_output_enable() unless the output was already enabled.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005186 *
Marius Vlad55d87362019-06-11 01:15:35 +03005187 * \ingroup output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005188 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005189WL_EXPORT int
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005190weston_output_attach_head(struct weston_output *output,
5191 struct weston_head *head)
5192{
Pekka Paalanendcac3512017-12-08 14:13:34 +02005193 char *head_names;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005194
5195 if (!wl_list_empty(&head->output_link))
5196 return -1;
5197
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005198 if (output->attach_head) {
5199 if (output->attach_head(output, head) < 0)
5200 return -1;
5201 } else if (!wl_list_empty(&output->head_list)) {
5202 /* No support for clones in the legacy path. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005203 return -1;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005204 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005205
5206 head->output = output;
5207 wl_list_insert(output->head_list.prev, &head->output_link);
5208
Pekka Paalanendcac3512017-12-08 14:13:34 +02005209 if (output->enabled) {
5210 weston_head_add_global(head);
5211
5212 head_names = weston_output_create_heads_string(output);
5213 weston_log("Output '%s' updated to have head(s) %s\n",
5214 output->name, head_names);
5215 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005216
5217 weston_output_emit_heads_changed(output);
Pekka Paalanendcac3512017-12-08 14:13:34 +02005218 }
5219
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005220 return 0;
5221}
5222
5223/** Detach a head from its output
5224 *
5225 * \param head The head to detach.
5226 *
5227 * It is safe to detach a non-attached head.
5228 *
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005229 * If the head is attached to an enabled output and the output will be left
5230 * with no heads, the output will be disabled.
5231 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005232 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005233 * \sa weston_output_disable
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005234 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005235WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005236weston_head_detach(struct weston_head *head)
5237{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005238 struct weston_output *output = head->output;
Pekka Paalanena0106992017-12-08 16:11:17 +02005239 char *head_names;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005240
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005241 wl_list_remove(&head->output_link);
5242 wl_list_init(&head->output_link);
5243 head->output = NULL;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005244
5245 if (!output)
5246 return;
5247
5248 if (output->detach_head)
5249 output->detach_head(output, head);
5250
5251 if (output->enabled) {
5252 weston_head_remove_global(head);
5253
Pekka Paalanena0106992017-12-08 16:11:17 +02005254 if (wl_list_empty(&output->head_list)) {
5255 weston_log("Output '%s' no heads left, disabling.\n",
5256 output->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005257 weston_output_disable(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02005258 } else {
5259 head_names = weston_output_create_heads_string(output);
5260 weston_log("Output '%s' updated to have head(s) %s\n",
5261 output->name, head_names);
5262 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005263
5264 weston_output_emit_heads_changed(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02005265 }
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005266 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005267}
5268
5269/** Destroy a head
5270 *
5271 * \param head The head to be released.
5272 *
5273 * Destroys the head. The caller is responsible for freeing the memory pointed
5274 * to by \c head.
5275 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005276 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005277 * \internal
5278 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005279WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005280weston_head_release(struct weston_head *head)
5281{
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005282 wl_signal_emit(&head->destroy_signal, head);
5283
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005284 weston_head_detach(head);
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005285
5286 free(head->make);
5287 free(head->model);
5288 free(head->serial_number);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005289 free(head->name);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005290
5291 wl_list_remove(&head->compositor_link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005292}
5293
Pekka Paalanene19970f2017-08-28 14:11:02 +03005294static void
5295weston_head_set_device_changed(struct weston_head *head)
5296{
5297 head->device_changed = true;
5298
5299 if (head->compositor)
5300 weston_compositor_schedule_heads_changed(head->compositor);
5301}
5302
5303/** String equal comparison with NULLs being equal */
5304static bool
5305str_null_eq(const char *a, const char *b)
5306{
5307 if (!a && !b)
5308 return true;
5309
5310 if (!!a != !!b)
5311 return false;
5312
5313 return strcmp(a, b) == 0;
5314}
5315
Pekka Paalanen01f60212017-03-24 15:39:24 +02005316/** Store monitor make, model and serial number
5317 *
5318 * \param head The head to modify.
5319 * \param make The monitor make. If EDID is available, the PNP ID. Otherwise
5320 * any string, or NULL for none.
5321 * \param model The monitor model or name, or a made-up string, or NULL for
5322 * none.
5323 * \param serialno The monitor serial number, a made-up string, or NULL for
5324 * none.
5325 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005326 * This may set the device_changed flag.
5327 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005328 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005329 * \internal
5330 */
5331WL_EXPORT void
5332weston_head_set_monitor_strings(struct weston_head *head,
5333 const char *make,
5334 const char *model,
5335 const char *serialno)
5336{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005337 if (str_null_eq(head->make, make) &&
5338 str_null_eq(head->model, model) &&
5339 str_null_eq(head->serial_number, serialno))
5340 return;
5341
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005342 free(head->make);
5343 free(head->model);
5344 free(head->serial_number);
5345
5346 head->make = make ? strdup(make) : NULL;
5347 head->model = model ? strdup(model) : NULL;
5348 head->serial_number = serialno ? strdup(serialno) : NULL;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005349
5350 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005351}
5352
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005353/** Store display non-desktop status
5354 *
5355 * \param head The head to modify.
5356 * \param non_desktop Whether the head connects to a non-desktop display.
5357 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005358 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005359 * \internal
5360 */
5361WL_EXPORT void
5362weston_head_set_non_desktop(struct weston_head *head, bool non_desktop)
5363{
5364 if (head->non_desktop == non_desktop)
5365 return;
5366
5367 head->non_desktop = non_desktop;
5368
5369 weston_head_set_device_changed(head);
5370}
5371
Pekka Paalanen01f60212017-03-24 15:39:24 +02005372/** Store physical image size
5373 *
5374 * \param head The head to modify.
5375 * \param mm_width Image area width in millimeters.
5376 * \param mm_height Image area height in millimeters.
5377 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005378 * This may set the device_changed flag.
5379 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005380 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005381 * \internal
5382 */
5383WL_EXPORT void
5384weston_head_set_physical_size(struct weston_head *head,
5385 int32_t mm_width, int32_t mm_height)
5386{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005387 if (head->mm_width == mm_width &&
5388 head->mm_height == mm_height)
5389 return;
5390
Pekka Paalanen01f60212017-03-24 15:39:24 +02005391 head->mm_width = mm_width;
5392 head->mm_height = mm_height;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005393
5394 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005395}
5396
5397/** Store monitor sub-pixel layout
5398 *
5399 * \param head The head to modify.
5400 * \param sp Sub-pixel layout. The possible values are:
5401 * - WL_OUTPUT_SUBPIXEL_UNKNOWN,
5402 * - WL_OUTPUT_SUBPIXEL_NONE,
5403 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB,
5404 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR,
5405 * - WL_OUTPUT_SUBPIXEL_VERTICAL_RGB,
5406 * - WL_OUTPUT_SUBPIXEL_VERTICAL_BGR
5407 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005408 * This may set the device_changed flag.
5409 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005410 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005411 * \internal
5412 */
5413WL_EXPORT void
5414weston_head_set_subpixel(struct weston_head *head,
5415 enum wl_output_subpixel sp)
5416{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005417 if (head->subpixel == sp)
5418 return;
5419
Pekka Paalanen01f60212017-03-24 15:39:24 +02005420 head->subpixel = sp;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005421
5422 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005423}
5424
5425/** Mark the monitor as internal
5426 *
5427 * This is used for embedded screens, like laptop panels.
5428 *
5429 * \param head The head to mark as internal.
5430 *
5431 * By default a head is external. The type is often inferred from the physical
5432 * connector type.
5433 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005434 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005435 * \internal
5436 */
5437WL_EXPORT void
5438weston_head_set_internal(struct weston_head *head)
5439{
5440 head->connection_internal = true;
5441}
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005442
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005443/** Store connector status
5444 *
5445 * \param head The head to modify.
5446 * \param connected Whether the head is connected.
5447 *
5448 * Connectors are created as disconnected. This function can be used to
5449 * set the connector status.
5450 *
5451 * The status should be set to true when a physical connector is connected to
5452 * a video sink device like a monitor and to false when the connector is
5453 * disconnected. For nested backends, the connection status should reflect the
5454 * connection to the parent display server.
5455 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005456 * When the connection status changes, it schedules a call to the heads_changed
Pekka Paalanene19970f2017-08-28 14:11:02 +03005457 * hook and sets the device_changed flag.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005458 *
5459 * \sa weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005460 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005461 * \internal
5462 */
5463WL_EXPORT void
5464weston_head_set_connection_status(struct weston_head *head, bool connected)
5465{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005466 if (head->connected == connected)
5467 return;
5468
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005469 head->connected = connected;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005470
Pekka Paalanene19970f2017-08-28 14:11:02 +03005471 weston_head_set_device_changed(head);
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005472}
5473
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305474static void
5475weston_output_compute_protection(struct weston_output *output)
5476{
5477 struct weston_head *head;
5478 enum weston_hdcp_protection op_protection;
5479 bool op_protection_valid = false;
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305480 struct weston_compositor *wc = output->compositor;
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305481
5482 wl_list_for_each(head, &output->head_list, output_link) {
5483 if (!op_protection_valid) {
5484 op_protection = head->current_protection;
5485 op_protection_valid = true;
5486 }
5487 if (head->current_protection < op_protection)
5488 op_protection = head->current_protection;
5489 }
5490
5491 if (!op_protection_valid)
5492 op_protection = WESTON_HDCP_DISABLE;
5493
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305494 if (output->current_protection != op_protection) {
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305495 output->current_protection = op_protection;
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305496 weston_output_damage(output);
Ankit Nautiyalfaa5ab42019-07-08 13:23:24 +05305497 weston_schedule_surface_protection_update(wc);
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305498 }
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305499}
5500
5501WL_EXPORT void
5502weston_head_set_content_protection_status(struct weston_head *head,
5503 enum weston_hdcp_protection status)
5504{
5505 head->current_protection = status;
5506 if (head->output)
5507 weston_output_compute_protection(head->output);
5508}
5509
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005510/** Is the head currently connected?
5511 *
5512 * \param head The head to query.
5513 * \return Connection status.
5514 *
5515 * Returns true if the head is physically connected to a monitor, or in
5516 * case of a nested backend returns true when there is a connection to the
5517 * parent display server.
5518 *
5519 * This is independent from the head being enabled.
5520 *
5521 * \sa weston_head_is_enabled
Marius Vlad78984ee2019-06-11 00:05:08 +03005522 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005523 */
5524WL_EXPORT bool
5525weston_head_is_connected(struct weston_head *head)
5526{
5527 return head->connected;
5528}
5529
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005530/** Is the head currently enabled?
5531 *
5532 * \param head The head to query.
5533 * \return Video status.
5534 *
5535 * Returns true if the head is currently transmitting a video stream.
5536 *
5537 * This is independent of the head being connected.
5538 *
5539 * \sa weston_head_is_connected
Marius Vlad78984ee2019-06-11 00:05:08 +03005540 * \ingroup head
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005541 */
5542WL_EXPORT bool
5543weston_head_is_enabled(struct weston_head *head)
5544{
5545 if (!head->output)
5546 return false;
5547
5548 return head->output->enabled;
5549}
5550
Pekka Paalanene19970f2017-08-28 14:11:02 +03005551/** Has the device information changed?
5552 *
5553 * \param head The head to query.
5554 * \return True if the device information has changed since last reset.
5555 *
5556 * The information about the connected display device, e.g. a monitor, may
5557 * change without being disconnected in between. Changing information
5558 * causes a call to the heads_changed hook.
5559 *
5560 * The information includes make, model, serial number, physical size,
5561 * and sub-pixel type. The connection status is also included.
5562 *
5563 * \sa weston_head_reset_device_changed, weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005564 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03005565 */
5566WL_EXPORT bool
5567weston_head_is_device_changed(struct weston_head *head)
5568{
5569 return head->device_changed;
5570}
5571
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005572/** Does the head represent a non-desktop display?
5573 *
5574 * \param head The head to query.
5575 * \return True if the device is a non-desktop display.
5576 *
5577 * Non-desktop heads are not attached to outputs by default.
5578 * This stops weston from extending the desktop onto head mounted displays.
5579 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005580 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005581 */
5582WL_EXPORT bool
5583weston_head_is_non_desktop(struct weston_head *head)
5584{
5585 return head->non_desktop;
5586}
5587
Pekka Paalanene19970f2017-08-28 14:11:02 +03005588/** Acknowledge device information change
5589 *
5590 * \param head The head to acknowledge.
5591 *
5592 * Clears the device changed flag on this head. When a compositor has processed
5593 * device information, it should call this to be able to notice further
5594 * changes.
5595 *
5596 * \sa weston_head_is_device_changed
Marius Vlad78984ee2019-06-11 00:05:08 +03005597 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03005598 */
5599WL_EXPORT void
5600weston_head_reset_device_changed(struct weston_head *head)
5601{
5602 head->device_changed = false;
5603}
5604
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005605/** Get the name of a head
5606 *
5607 * \param head The head to query.
5608 * \return The head's name, not NULL.
5609 *
5610 * The name depends on the backend. The DRM backend uses connector names,
5611 * other backends may use hardcoded names or user-given names.
Marius Vlad78984ee2019-06-11 00:05:08 +03005612 *
5613 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005614 */
5615WL_EXPORT const char *
5616weston_head_get_name(struct weston_head *head)
5617{
5618 return head->name;
5619}
5620
5621/** Get the output the head is attached to
5622 *
5623 * \param head The head to query.
5624 * \return The output the head is attached to, or NULL if detached.
Marius Vlad78984ee2019-06-11 00:05:08 +03005625 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005626 */
5627WL_EXPORT struct weston_output *
5628weston_head_get_output(struct weston_head *head)
5629{
5630 return head->output;
5631}
5632
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005633/** Add destroy callback for a head
5634 *
5635 * \param head The head to watch for.
5636 * \param listener The listener to add. The \c notify member must be set.
5637 *
5638 * Heads may get destroyed for various reasons by the backends. If a head is
5639 * attached to an output, the compositor should listen for head destruction
5640 * and reconfigure or destroy the output if necessary.
5641 *
5642 * The destroy callbacks will be called on weston_head destruction before any
5643 * automatic detaching from an associated weston_output and before any
5644 * weston_head information is lost.
5645 *
5646 * The \c data argument to the notify callback is the weston_head being
5647 * destroyed.
Marius Vlad78984ee2019-06-11 00:05:08 +03005648 *
5649 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005650 */
5651WL_EXPORT void
5652weston_head_add_destroy_listener(struct weston_head *head,
5653 struct wl_listener *listener)
5654{
5655 wl_signal_add(&head->destroy_signal, listener);
5656}
5657
5658/** Look up destroy listener for a head
5659 *
5660 * \param head The head to query.
5661 * \param notify The notify function used used for the added destroy listener.
5662 * \return The listener, or NULL if not found.
5663 *
5664 * This looks up the previously added destroy listener struct based on the
5665 * notify function it has. The listener can be used to access user data
5666 * through \c container_of().
5667 *
5668 * \sa wl_signal_get()
Marius Vlad78984ee2019-06-11 00:05:08 +03005669 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005670 */
5671WL_EXPORT struct wl_listener *
5672weston_head_get_destroy_listener(struct weston_head *head,
5673 wl_notify_func_t notify)
5674{
5675 return wl_signal_get(&head->destroy_signal, notify);
5676}
5677
David Fort0de859e2016-05-27 23:22:57 +02005678/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005679static void
David Fort0de859e2016-05-27 23:22:57 +02005680weston_compositor_reflow_outputs(struct weston_compositor *compositor,
5681 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005682{
5683 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02005684 bool start_resizing = false;
5685
5686 if (!delta_width)
5687 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005688
5689 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02005690 if (output == resized_output) {
5691 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005692 continue;
5693 }
5694
David Fort0de859e2016-05-27 23:22:57 +02005695 if (start_resizing) {
5696 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005697 output->dirty = 1;
5698 }
5699 }
5700}
5701
Pekka Paalanend72bad22017-03-29 17:01:41 +03005702static void
Scott Moreauccbf29d2012-02-22 14:21:41 -07005703weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005704{
Scott Moreau850ca422012-05-21 15:21:25 -06005705 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05005706
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005707 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005708 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06005709
Scott Moreauccbf29d2012-02-22 14:21:41 -07005710 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06005711 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005712 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005713 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005714 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005715 weston_matrix_scale(&output->matrix, magnification,
5716 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005717 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005718
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005719 switch (output->transform) {
5720 case WL_OUTPUT_TRANSFORM_FLIPPED:
5721 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5722 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5723 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5724 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5725 weston_matrix_scale(&output->matrix, -1, 1, 1);
5726 break;
5727 }
5728
5729 switch (output->transform) {
5730 default:
5731 case WL_OUTPUT_TRANSFORM_NORMAL:
5732 case WL_OUTPUT_TRANSFORM_FLIPPED:
5733 break;
5734 case WL_OUTPUT_TRANSFORM_90:
5735 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5736 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
5737 weston_matrix_rotate_xy(&output->matrix, 0, 1);
5738 break;
5739 case WL_OUTPUT_TRANSFORM_180:
5740 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5741 weston_matrix_translate(&output->matrix,
5742 -output->width, -output->height, 0);
5743 weston_matrix_rotate_xy(&output->matrix, -1, 0);
5744 break;
5745 case WL_OUTPUT_TRANSFORM_270:
5746 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5747 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5748 weston_matrix_rotate_xy(&output->matrix, 0, -1);
5749 break;
5750 }
5751
5752 if (output->current_scale != 1)
5753 weston_matrix_scale(&output->matrix,
5754 output->current_scale,
5755 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01005756
Scott Moreauccbf29d2012-02-22 14:21:41 -07005757 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05005758
5759 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005760}
5761
Scott Moreau1bad5db2012-08-18 01:04:05 -06005762static void
Alexander Larsson0b135062013-05-28 16:23:36 +02005763weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06005764{
5765 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005766 output->native_scale = scale;
5767 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06005768
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005769 convert_size_by_transform_scale(&output->width, &output->height,
5770 output->current_mode->width,
5771 output->current_mode->height,
5772 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02005773}
5774
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005775static void
5776weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07005777{
5778 output->x = x;
5779 output->y = y;
5780
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005781 pixman_region32_fini(&output->region);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005782 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06005783 output->width,
5784 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005785}
5786
Marius Vlad55d87362019-06-11 01:15:35 +03005787/**
5788 * \ingroup output
5789 */
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005790WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005791weston_output_move(struct weston_output *output, int x, int y)
5792{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005793 struct weston_head *head;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005794 struct wl_resource *resource;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005795 int ver;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005796
5797 output->move_x = x - output->x;
5798 output->move_y = y - output->y;
5799
5800 if (output->move_x == 0 && output->move_y == 0)
5801 return;
5802
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005803 weston_output_init_geometry(output, x, y);
5804
5805 output->dirty = 1;
5806
5807 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005808 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005809
5810 /* Notify clients of the change for output position. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005811 wl_list_for_each(head, &output->head_list, output_link) {
5812 wl_resource_for_each(resource, &head->resource_list) {
5813 wl_output_send_geometry(resource,
5814 output->x,
5815 output->y,
5816 head->mm_width,
5817 head->mm_height,
5818 head->subpixel,
5819 head->make,
5820 head->model,
5821 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08005822
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005823 ver = wl_resource_get_version(resource);
5824 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
5825 wl_output_send_done(resource);
5826 }
Roman Gilge97391c2019-03-29 13:01:06 +01005827
5828 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
5829 zxdg_output_v1_send_logical_position(resource,
5830 output->x,
5831 output->y);
5832 zxdg_output_v1_send_done(resource);
5833 }
Quanxian Wangb2c86362014-03-14 09:16:25 +08005834 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005835}
5836
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005837/** Signal that a pending output is taken into use.
5838 *
5839 * Removes the output from the pending list and adds it to the compositor's
5840 * list of enabled outputs. The output created signal is emitted.
Giulio Camuffob1147152015-05-06 21:41:57 +03005841 *
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005842 * The output gets an internal ID assigned, and the wl_output global is
5843 * created.
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005844 *
Giulio Camuffob1147152015-05-06 21:41:57 +03005845 * \param compositor The compositor instance.
5846 * \param output The output to be added.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005847 *
5848 * \internal
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005849 * \ingroup compositor
Giulio Camuffob1147152015-05-06 21:41:57 +03005850 */
Pekka Paalanenf9681b52017-03-29 16:58:48 +03005851static void
Giulio Camuffob1147152015-05-06 21:41:57 +03005852weston_compositor_add_output(struct weston_compositor *compositor,
5853 struct weston_output *output)
5854{
Armin Krezoviće5403842016-08-05 15:28:29 +02005855 struct weston_view *view, *next;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005856 struct weston_head *head;
Armin Krezoviće5403842016-08-05 15:28:29 +02005857
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005858 assert(!output->enabled);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005859
5860 /* Verify we haven't reached the limit of 32 available output IDs */
5861 assert(ffs(~compositor->output_id_pool) > 0);
5862
5863 /* Invert the output id pool and look for the lowest numbered
5864 * switch (the least significant bit). Take that bit's position
5865 * as our ID, and mark it used in the compositor's output_id_pool.
5866 */
5867 output->id = ffs(~compositor->output_id_pool) - 1;
5868 compositor->output_id_pool |= 1u << output->id;
5869
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005870 wl_list_remove(&output->link);
Giulio Camuffob1147152015-05-06 21:41:57 +03005871 wl_list_insert(compositor->output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005872 output->enabled = true;
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005873
Pekka Paalanendcac3512017-12-08 14:13:34 +02005874 wl_list_for_each(head, &output->head_list, output_link)
5875 weston_head_add_global(head);
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005876
Giulio Camuffob1147152015-05-06 21:41:57 +03005877 wl_signal_emit(&compositor->output_created_signal, output);
Armin Krezoviće5403842016-08-05 15:28:29 +02005878
5879 wl_list_for_each_safe(view, next, &compositor->view_list, link)
5880 weston_view_geometry_dirty(view);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005881}
5882
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005883/** Transform device coordinates into global coordinates
5884 *
Marius Vlada2dace22019-06-12 16:05:44 +03005885 * \param output the weston_output object
5886 * \param[in] device_x X coordinate in device units.
5887 * \param[in] device_y Y coordinate in device units.
5888 * \param[out] x X coordinate in the global space.
5889 * \param[out] y Y coordinate in the global space.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005890 *
Marius Vlada2dace22019-06-12 16:05:44 +03005891 * Transforms coordinates from the device coordinate space (physical pixel
5892 * units) to the global coordinate space (logical pixel units). This takes
5893 * into account output transform and scale.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005894 *
Marius Vlad55d87362019-06-11 01:15:35 +03005895 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005896 * \internal
5897 */
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005898WL_EXPORT void
5899weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005900 double device_x, double device_y,
5901 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005902{
Derek Foreman0f679412014-10-02 13:41:17 -05005903 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005904 device_x,
5905 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05005906 0.0,
5907 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005908
Derek Foreman67a18b92015-03-24 11:36:14 -05005909 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005910
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005911 *x = p.f[0] / p.f[3];
5912 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005913}
5914
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005915/** Removes output from compositor's list of enabled outputs
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005916 *
5917 * \param output The weston_output object that is being removed.
5918 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005919 * The following happens:
5920 *
5921 * - The output assignments of all views in the current scenegraph are
5922 * recomputed.
5923 *
5924 * - Presentation feedback is discarded.
5925 *
5926 * - Compositor is notified that outputs were changed and
5927 * applies the necessary changes to re-layout outputs.
5928 *
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005929 * - The output is put back in the pending outputs list.
5930 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005931 * - Signal is emitted to notify all users of the weston_output
5932 * object that the output is being destroyed.
5933 *
5934 * - wl_output protocol objects referencing this weston_output
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005935 * are made inert, and the wl_output global is removed.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005936 *
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005937 * - The output's internal ID is released.
5938 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005939 * \ingroup compositor
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005940 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005941 */
5942static void
5943weston_compositor_remove_output(struct weston_output *output)
5944{
Pekka Paalanenbccda712017-03-29 16:16:04 +03005945 struct weston_compositor *compositor = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005946 struct weston_view *view;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005947 struct weston_head *head;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005948
5949 assert(output->destroying);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005950 assert(output->enabled);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005951
Pekka Paalanenbccda712017-03-29 16:16:04 +03005952 wl_list_for_each(view, &compositor->view_list, link) {
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005953 if (view->output_mask & (1u << output->id))
5954 weston_view_assign_output(view);
5955 }
5956
5957 weston_presentation_feedback_discard_list(&output->feedback_list);
5958
Pekka Paalanen9711fd92018-06-21 14:26:18 +03005959 weston_compositor_reflow_outputs(compositor, output, -output->width);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005960
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005961 wl_list_remove(&output->link);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005962 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005963 output->enabled = false;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005964
Pekka Paalanenbccda712017-03-29 16:16:04 +03005965 wl_signal_emit(&compositor->output_destroyed_signal, output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005966 wl_signal_emit(&output->destroy_signal, output);
5967
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02005968 wl_list_for_each(head, &output->head_list, output_link)
5969 weston_head_remove_global(head);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005970
5971 compositor->output_id_pool &= ~(1u << output->id);
5972 output->id = 0xffffffff; /* invalid */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005973}
5974
5975/** Sets the output scale for a given output.
5976 *
5977 * \param output The weston_output object that the scale is set for.
5978 * \param scale Scale factor for the given output.
5979 *
5980 * It only supports setting scale for an output that
5981 * is not enabled and it can only be ran once.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005982 *
Marius Vlad55d87362019-06-11 01:15:35 +03005983 * \ingroup ouput
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005984 */
5985WL_EXPORT void
5986weston_output_set_scale(struct weston_output *output,
5987 int32_t scale)
5988{
5989 /* We can only set scale on a disabled output */
5990 assert(!output->enabled);
5991
5992 /* We only want to set scale once */
5993 assert(!output->scale);
5994
5995 output->scale = scale;
5996}
5997
5998/** Sets the output transform for a given output.
5999 *
6000 * \param output The weston_output object that the transform is set for.
6001 * \param transform Transform value for the given output.
6002 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006003 * Refer to wl_output::transform section located at
6004 * https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_output
6005 * for list of values that can be passed to this function.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006006 *
Marius Vlad55d87362019-06-11 01:15:35 +03006007 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006008 */
6009WL_EXPORT void
6010weston_output_set_transform(struct weston_output *output,
6011 uint32_t transform)
6012{
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006013 struct weston_pointer_motion_event ev;
6014 struct wl_resource *resource;
6015 struct weston_seat *seat;
6016 pixman_region32_t old_region;
6017 int mid_x, mid_y;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006018 struct weston_head *head;
6019 int ver;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006020
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006021 if (!output->enabled && output->transform == UINT32_MAX) {
6022 output->transform = transform;
6023 return;
6024 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006025
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006026 weston_output_transform_scale_init(output, transform, output->scale);
6027
6028 pixman_region32_init(&old_region);
6029 pixman_region32_copy(&old_region, &output->region);
6030
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006031 weston_output_init_geometry(output, output->x, output->y);
6032
6033 output->dirty = 1;
6034
6035 /* Notify clients of the change for output transform. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006036 wl_list_for_each(head, &output->head_list, output_link) {
6037 wl_resource_for_each(resource, &head->resource_list) {
6038 wl_output_send_geometry(resource,
6039 output->x,
6040 output->y,
6041 head->mm_width,
6042 head->mm_height,
6043 head->subpixel,
6044 head->make,
6045 head->model,
6046 output->transform);
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006047
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006048 ver = wl_resource_get_version(resource);
6049 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
6050 wl_output_send_done(resource);
6051 }
Roman Gilge97391c2019-03-29 13:01:06 +01006052 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
6053 zxdg_output_v1_send_logical_position(resource,
6054 output->x,
6055 output->y);
6056 zxdg_output_v1_send_logical_size(resource,
6057 output->width,
6058 output->height);
6059 zxdg_output_v1_send_done(resource);
6060 }
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006061 }
6062
6063 /* we must ensure that pointers are inside output, otherwise they disappear */
6064 mid_x = output->x + output->width / 2;
6065 mid_y = output->y + output->height / 2;
6066
6067 ev.mask = WESTON_POINTER_MOTION_ABS;
6068 ev.x = wl_fixed_to_double(wl_fixed_from_int(mid_x));
6069 ev.y = wl_fixed_to_double(wl_fixed_from_int(mid_y));
6070
6071 wl_list_for_each(seat, &output->compositor->seat_list, link) {
6072 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
6073
6074 if (pointer && pixman_region32_contains_point(&old_region,
6075 wl_fixed_to_int(pointer->x),
6076 wl_fixed_to_int(pointer->y),
6077 NULL))
6078 weston_pointer_move(pointer, &ev);
6079 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006080}
6081
6082/** Initializes a weston_output object with enough data so
6083 ** an output can be configured.
6084 *
6085 * \param output The weston_output object to initialize
6086 * \param compositor The compositor instance.
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006087 * \param name Name for the output (the string is copied).
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006088 *
6089 * Sets initial values for fields that are expected to be
6090 * configured either by compositors or backends.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006091 *
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006092 * The name is used in logs, and can be used by compositors as a configuration
6093 * identifier.
6094 *
Marius Vlad55d87362019-06-11 01:15:35 +03006095 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006096 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006097 */
6098WL_EXPORT void
Armin Krezović40087402016-09-30 14:11:12 +02006099weston_output_init(struct weston_output *output,
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006100 struct weston_compositor *compositor,
6101 const char *name)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006102{
6103 output->compositor = compositor;
6104 output->destroying = 0;
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006105 output->name = strdup(name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006106 wl_list_init(&output->link);
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006107 wl_signal_init(&output->user_destroy_signal);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006108 output->enabled = false;
Ankit Nautiyal2690a772019-03-25 17:57:59 +05306109 output->desired_protection = WESTON_HDCP_DISABLE;
Ankit Nautiyal2844f8e2019-04-03 10:14:59 +05306110 output->allow_protection = true;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006111
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006112 wl_list_init(&output->head_list);
6113
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006114 /* Add some (in)sane defaults which can be used
6115 * for checking if an output was properly configured
6116 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006117 output->scale = 0;
6118 /* Can't use -1 on uint32_t and 0 is valid enum value */
6119 output->transform = UINT32_MAX;
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006120
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006121 pixman_region32_init(&output->region);
Pekka Paalanen42704142017-09-06 16:47:52 +03006122 wl_list_init(&output->mode_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006123}
6124
6125/** Adds weston_output object to pending output list.
6126 *
6127 * \param output The weston_output object to add
6128 * \param compositor The compositor instance.
6129 *
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03006130 * The opposite of this operation is built into weston_output_release().
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006131 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006132 * \ingroup compositor
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006133 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006134 */
6135WL_EXPORT void
6136weston_compositor_add_pending_output(struct weston_output *output,
6137 struct weston_compositor *compositor)
6138{
Pekka Paalanene952a012017-03-29 17:14:00 +03006139 assert(output->disable);
6140 assert(output->enable);
6141
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006142 wl_list_remove(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006143 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006144}
6145
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006146/** Create a string with the attached heads' names.
6147 *
6148 * The string must be free()'d.
Marius Vlad55d87362019-06-11 01:15:35 +03006149 *
6150 * \ingroup output
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006151 */
6152static char *
6153weston_output_create_heads_string(struct weston_output *output)
6154{
6155 FILE *fp;
6156 char *str = NULL;
6157 size_t size = 0;
6158 struct weston_head *head;
6159 const char *sep = "";
6160
6161 fp = open_memstream(&str, &size);
6162 if (!fp)
6163 return NULL;
6164
6165 wl_list_for_each(head, &output->head_list, output_link) {
6166 fprintf(fp, "%s%s", sep, head->name);
6167 sep = ", ";
6168 }
6169 fclose(fp);
6170
6171 return str;
6172}
6173
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006174/** Constructs a weston_output object that can be used by the compositor.
6175 *
Pekka Paalanencc201e42017-03-30 15:11:25 +03006176 * \param output The weston_output object that needs to be enabled. Must not
Pekka Paalanenddce54d2017-08-23 16:00:21 +03006177 * be enabled already. Must have at least one head attached.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006178 *
6179 * Output coordinates are calculated and each new output is by default
6180 * assigned to the right of previous one.
6181 *
6182 * Sets up the transformation, zoom, and geometry of the output using
6183 * the properties that need to be configured by the compositor.
6184 *
6185 * Establishes a repaint timer for the output with the relevant display
6186 * object's event loop. See output_repaint_timer_handler().
6187 *
6188 * The output is assigned an ID. Weston can support up to 32 distinct
6189 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
6190 * is referred to and used to find the first available ID number, and
6191 * then this ID is marked as used in output_id_pool.
6192 *
6193 * The output is also assigned a Wayland global with the wl_output
6194 * external interface.
6195 *
6196 * Backend specific function is called to set up the output output.
6197 *
6198 * Output is added to the compositor's output list
6199 *
6200 * If the backend specific function fails, the weston_output object
6201 * is returned to a state it was before calling this function and
6202 * is added to the compositor's pending_output_list in case it needs
6203 * to be reconfigured or just so it can be destroyed at shutdown.
6204 *
6205 * 0 is returned on success, -1 on failure.
Marius Vlad55d87362019-06-11 01:15:35 +03006206 *
6207 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006208 */
6209WL_EXPORT int
6210weston_output_enable(struct weston_output *output)
6211{
Armin Krezović782f5df2016-09-30 14:11:11 +02006212 struct weston_compositor *c = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006213 struct weston_output *iterator;
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03006214 struct weston_head *head;
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006215 char *head_names;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006216 int x = 0, y = 0;
6217
Pekka Paalanencc201e42017-03-30 15:11:25 +03006218 if (output->enabled) {
6219 weston_log("Error: attempt to enable an enabled output '%s'\n",
6220 output->name);
6221 return -1;
6222 }
6223
Pekka Paalanenddce54d2017-08-23 16:00:21 +03006224 if (wl_list_empty(&output->head_list)) {
6225 weston_log("Error: cannot enable output '%s' without heads.\n",
6226 output->name);
6227 return -1;
6228 }
6229
Pekka Paalanen586e1ac2017-09-14 16:17:59 +03006230 if (wl_list_empty(&output->mode_list) || !output->current_mode) {
6231 weston_log("Error: no video mode for output '%s'.\n",
6232 output->name);
6233 return -1;
6234 }
6235
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03006236 wl_list_for_each(head, &output->head_list, output_link) {
6237 assert(head->make);
6238 assert(head->model);
6239 }
6240
Armin Krezović782f5df2016-09-30 14:11:11 +02006241 iterator = container_of(c->output_list.prev,
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006242 struct weston_output, link);
6243
Armin Krezović782f5df2016-09-30 14:11:11 +02006244 if (!wl_list_empty(&c->output_list))
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006245 x = iterator->x + iterator->width;
6246
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006247 /* Make sure the scale is set up */
6248 assert(output->scale);
6249
6250 /* Make sure we have a transform set */
6251 assert(output->transform != UINT32_MAX);
6252
Armin Krezović782f5df2016-09-30 14:11:11 +02006253 output->x = x;
6254 output->y = y;
6255 output->dirty = 1;
6256 output->original_scale = output->scale;
6257
Marius Vlad3a2f8292019-11-04 17:53:46 +02006258 wl_signal_init(&output->frame_signal);
6259 wl_signal_init(&output->destroy_signal);
6260
Armin Krezović782f5df2016-09-30 14:11:11 +02006261 weston_output_transform_scale_init(output, output->transform, output->scale);
6262 weston_output_init_zoom(output);
6263
6264 weston_output_init_geometry(output, x, y);
6265 weston_output_damage(output);
6266
Armin Krezović782f5df2016-09-30 14:11:11 +02006267 wl_list_init(&output->animation_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02006268 wl_list_init(&output->feedback_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02006269
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006270 /* Enable the output (set up the crtc or create a
6271 * window representing the output, set up the
6272 * renderer, etc)
6273 */
6274 if (output->enable(output) < 0) {
6275 weston_log("Enabling output \"%s\" failed.\n", output->name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006276 return -1;
6277 }
6278
6279 weston_compositor_add_output(output->compositor, output);
6280
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006281 head_names = weston_output_create_heads_string(output);
6282 weston_log("Output '%s' enabled with head(s) %s\n",
6283 output->name, head_names);
6284 free(head_names);
6285
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006286 return 0;
6287}
6288
6289/** Converts a weston_output object to a pending output state, so it
6290 ** can be configured again or destroyed.
6291 *
6292 * \param output The weston_output object that needs to be disabled.
6293 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006294 * Calls a backend specific function to disable an output, in case
6295 * such function exists.
6296 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03006297 * The backend specific disable function may choose to postpone the disabling
6298 * by returning a negative value, in which case this function returns early.
6299 * In that case the backend will guarantee the output will be disabled soon
6300 * by the backend calling this function again. One must not attempt to re-enable
6301 * the output until that happens.
6302 *
6303 * Otherwise, if the output is being used by the compositor, it is removed
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006304 * from weston's output_list (see weston_compositor_remove_output())
6305 * and is returned to a state it was before weston_output_enable()
6306 * was ran (see weston_output_enable_undo()).
6307 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03006308 * See weston_output_init() for more information on the
6309 * state output is returned to.
Pekka Paalanencc201e42017-03-30 15:11:25 +03006310 *
6311 * If the output has never been enabled yet, this function can still be
6312 * called to ensure that the output is actually turned off rather than left
6313 * in the state it was discovered in.
Marius Vlad55d87362019-06-11 01:15:35 +03006314 *
6315 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006316 */
6317WL_EXPORT void
6318weston_output_disable(struct weston_output *output)
6319{
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006320 /* Should we rename this? */
6321 output->destroying = 1;
6322
Pekka Paalanenc65df642017-03-29 15:45:46 +03006323 /* Disable is called unconditionally also for not-enabled outputs,
6324 * because at compositor start-up, if there is an output that is
6325 * already on but the compositor wants to turn it off, we have to
6326 * forward the turn-off to the backend so it knows to do it.
6327 * The backend cannot initially turn off everything, because it
6328 * would cause unnecessary mode-sets for all outputs the compositor
6329 * wants to be on.
6330 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006331 if (output->disable(output) < 0)
6332 return;
6333
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006334 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006335 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006336
6337 output->destroying = 0;
6338}
6339
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006340/** Forces a synchronous call to heads_changed hook
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006341 *
6342 * \param compositor The compositor instance
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006343 *
6344 * If there are new or changed heads, calls the heads_changed hook and
6345 * returns after the hook returns.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006346 *
6347 * \ingroup compositor
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006348 */
6349WL_EXPORT void
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006350weston_compositor_flush_heads_changed(struct weston_compositor *compositor)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006351{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03006352 if (compositor->heads_changed_source) {
6353 wl_event_source_remove(compositor->heads_changed_source);
6354 weston_compositor_call_heads_changed(compositor);
6355 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006356}
6357
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006358/** Add destroy callback for an output
6359 *
6360 * \param output The output to watch.
6361 * \param listener The listener to add. The \c notify member must be set.
6362 *
6363 * The listener callback will be called when user destroys an output. This
6364 * may be delayed by a backend in some cases. The main purpose of the
6365 * listener is to allow hooking up custom data to the output. The custom data
6366 * can be fetched via weston_output_get_destroy_listener() followed by
6367 * container_of().
6368 *
6369 * The \c data argument to the notify callback is the weston_output being
6370 * destroyed.
6371 *
6372 * @note This is for the final destruction of an output, not when it gets
6373 * disabled. If you want to keep track of enabled outputs, this is not it.
Marius Vlad55d87362019-06-11 01:15:35 +03006374 *
6375 * \ingroup ouput
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006376 */
6377WL_EXPORT void
6378weston_output_add_destroy_listener(struct weston_output *output,
6379 struct wl_listener *listener)
6380{
6381 wl_signal_add(&output->user_destroy_signal, listener);
6382}
6383
6384/** Look up destroy listener for an output
6385 *
6386 * \param output The output to query.
6387 * \param notify The notify function used used for the added destroy listener.
6388 * \return The listener, or NULL if not found.
6389 *
6390 * This looks up the previously added destroy listener struct based on the
6391 * notify function it has. The listener can be used to access user data
6392 * through \c container_of().
6393 *
6394 * \sa wl_signal_get() weston_output_add_destroy_listener()
Marius Vlad55d87362019-06-11 01:15:35 +03006395 * \ingroup output
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006396 */
6397WL_EXPORT struct wl_listener *
6398weston_output_get_destroy_listener(struct weston_output *output,
6399 wl_notify_func_t notify)
6400{
6401 return wl_signal_get(&output->user_destroy_signal, notify);
6402}
6403
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006404/** Uninitialize an output
6405 *
6406 * Removes the output from the list of enabled outputs if necessary, but
6407 * does not call the backend's output disable function. The output will no
6408 * longer be in the list of pending outputs either.
6409 *
6410 * All fields of weston_output become uninitialized, i.e. should not be used
6411 * anymore. The caller can free the memory after this.
6412 *
Marius Vlad55d87362019-06-11 01:15:35 +03006413 * \ingroup ouput
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006414 * \internal
6415 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006416WL_EXPORT void
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03006417weston_output_release(struct weston_output *output)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006418{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006419 struct weston_head *head, *tmp;
6420
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006421 output->destroying = 1;
6422
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006423 wl_signal_emit(&output->user_destroy_signal, output);
6424
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03006425 if (output->idle_repaint_source)
6426 wl_event_source_remove(output->idle_repaint_source);
6427
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006428 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006429 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006430
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006431 pixman_region32_fini(&output->region);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006432 wl_list_remove(&output->link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006433
6434 wl_list_for_each_safe(head, tmp, &output->head_list, output_link)
6435 weston_head_detach(head);
6436
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006437 free(output->name);
6438}
6439
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006440/** Find an output by its given name
6441 *
6442 * \param compositor The compositor to search in.
6443 * \param name The output name to search for.
6444 * \return An existing output with the given name, or NULL if not found.
6445 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006446 * \ingroup compositor
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006447 */
6448WL_EXPORT struct weston_output *
6449weston_compositor_find_output_by_name(struct weston_compositor *compositor,
6450 const char *name)
6451{
6452 struct weston_output *output;
6453
6454 wl_list_for_each(output, &compositor->output_list, link)
6455 if (strcmp(output->name, name) == 0)
6456 return output;
6457
6458 wl_list_for_each(output, &compositor->pending_output_list, link)
6459 if (strcmp(output->name, name) == 0)
6460 return output;
6461
6462 return NULL;
6463}
6464
6465/** Create a named output
6466 *
6467 * \param compositor The compositor.
6468 * \param name The name for the output.
6469 * \return A new \c weston_output, or NULL on failure.
6470 *
6471 * This creates a new weston_output that starts with no heads attached.
6472 *
6473 * An output must be configured and it must have at least one head before
6474 * it can be enabled.
6475 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006476 * \ingroup compositor
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006477 */
6478WL_EXPORT struct weston_output *
6479weston_compositor_create_output(struct weston_compositor *compositor,
6480 const char *name)
6481{
6482 assert(compositor->backend->create_output);
6483
6484 if (weston_compositor_find_output_by_name(compositor, name)) {
6485 weston_log("Warning: attempted to create an output with a "
6486 "duplicate name '%s'.\n", name);
6487 return NULL;
6488 }
6489
6490 return compositor->backend->create_output(compositor, name);
6491}
6492
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006493/** Create an output for an unused head
6494 *
6495 * \param compositor The compositor.
6496 * \param head The head to attach to the output.
6497 * \return A new \c weston_output, or NULL on failure.
6498 *
6499 * This creates a new weston_output that starts with the given head attached.
6500 * The output inherits the name of the head. The head must not be already
6501 * attached to another output.
6502 *
6503 * An output must be configured before it can be enabled.
6504 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006505 * \ingroup compositor
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006506 */
6507WL_EXPORT struct weston_output *
6508weston_compositor_create_output_with_head(struct weston_compositor *compositor,
6509 struct weston_head *head)
6510{
6511 struct weston_output *output;
6512
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006513 output = weston_compositor_create_output(compositor, head->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006514 if (!output)
6515 return NULL;
6516
6517 if (weston_output_attach_head(output, head) < 0) {
Pekka Paalanen42c0e142017-10-27 12:07:49 +03006518 weston_output_destroy(output);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006519 return NULL;
6520 }
6521
6522 return output;
6523}
6524
6525/** Destroy an output
6526 *
6527 * \param output The output to destroy.
6528 *
6529 * The heads attached to the given output are detached and become unused again.
6530 *
6531 * It is not necessary to explicitly destroy all outputs at compositor exit.
Marius Vlad413cda52019-08-23 17:42:40 +03006532 * weston_compositor_tear_down() will automatically destroy any remaining
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006533 * outputs.
6534 *
Marius Vlad55d87362019-06-11 01:15:35 +03006535 * \ingroup ouput
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006536 */
6537WL_EXPORT void
6538weston_output_destroy(struct weston_output *output)
6539{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006540 output->destroy(output);
6541}
6542
Pekka Paalanencf0a4762017-04-04 16:36:07 +03006543/** When you need a head...
6544 *
6545 * This function is a hack, used until all code has been converted to become
6546 * multi-head aware.
6547 *
6548 * \param output The weston_output whose head to get.
6549 * \return The first head in the output's list.
Marius Vlad55d87362019-06-11 01:15:35 +03006550 *
6551 * \ingroup ouput
Pekka Paalanencf0a4762017-04-04 16:36:07 +03006552 */
6553WL_EXPORT struct weston_head *
6554weston_output_get_first_head(struct weston_output *output)
6555{
6556 if (wl_list_empty(&output->head_list))
6557 return NULL;
6558
6559 return container_of(output->head_list.next,
6560 struct weston_head, output_link);
6561}
6562
Ankit Nautiyal2844f8e2019-04-03 10:14:59 +05306563/** Allow/Disallow content-protection support for an output
6564 *
6565 * This function sets the allow_protection member for an output. Setting of
6566 * this field will allow the compositor to attempt content-protection for this
6567 * output, for a backend that supports the content-protection protocol.
6568 *
6569 * \param output The weston_output for whom the content-protection is to be
6570 * allowed.
6571 * \param allow_protection The bool value which is to be set.
6572 */
6573WL_EXPORT void
6574weston_output_allow_protection(struct weston_output *output,
6575 bool allow_protection)
6576{
6577 output->allow_protection = allow_protection;
6578}
6579
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01006580static void
Roman Gilge97391c2019-03-29 13:01:06 +01006581xdg_output_unlist(struct wl_resource *resource)
6582{
6583 wl_list_remove(wl_resource_get_link(resource));
6584}
6585
6586static void
6587xdg_output_destroy(struct wl_client *client, struct wl_resource *resource)
6588{
6589 wl_resource_destroy(resource);
6590}
6591
6592static const struct zxdg_output_v1_interface xdg_output_interface = {
6593 xdg_output_destroy
6594};
6595
6596static void
6597xdg_output_manager_destroy(struct wl_client *client,
6598 struct wl_resource *resource)
6599{
6600 wl_resource_destroy(resource);
6601}
6602
6603static void
6604xdg_output_manager_get_xdg_output(struct wl_client *client,
6605 struct wl_resource *manager,
6606 uint32_t id,
6607 struct wl_resource *output_resource)
6608{
6609 int version = wl_resource_get_version(manager);
6610 struct weston_head *head = wl_resource_get_user_data(output_resource);
6611 struct weston_output *output = head->output;
6612 struct wl_resource *resource;
6613
6614 resource = wl_resource_create(client, &zxdg_output_v1_interface,
6615 version, id);
6616 if (resource == NULL) {
6617 wl_client_post_no_memory(client);
6618 return;
6619 }
6620
6621 wl_list_insert(&head->xdg_output_resource_list,
6622 wl_resource_get_link(resource));
6623
6624 wl_resource_set_implementation(resource, &xdg_output_interface,
6625 NULL, xdg_output_unlist);
6626
6627 zxdg_output_v1_send_logical_position(resource, output->x, output->y);
6628 zxdg_output_v1_send_logical_size(resource,
6629 output->width,
6630 output->height);
6631 if (version >= ZXDG_OUTPUT_V1_NAME_SINCE_VERSION)
6632 zxdg_output_v1_send_name(resource, head->name);
6633
6634 zxdg_output_v1_send_done(resource);
6635}
6636
6637static const struct zxdg_output_manager_v1_interface xdg_output_manager_interface = {
6638 xdg_output_manager_destroy,
6639 xdg_output_manager_get_xdg_output
6640};
6641
6642static void
6643bind_xdg_output_manager(struct wl_client *client,
6644 void *data, uint32_t version, uint32_t id)
6645{
6646 struct wl_resource *resource;
6647
6648 resource = wl_resource_create(client, &zxdg_output_manager_v1_interface,
6649 version, id);
6650 if (resource == NULL) {
6651 wl_client_post_no_memory(client);
6652 return;
6653 }
6654
6655 wl_resource_set_implementation(resource, &xdg_output_manager_interface,
6656 NULL, NULL);
6657}
6658
6659static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006660destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006661{
Jonny Lamb74130762013-11-26 18:19:46 +01006662 struct weston_surface *surface =
6663 wl_resource_get_user_data(resource);
6664
Pekka Paalanen4826f872016-04-22 14:14:38 +03006665 if (!surface)
6666 return;
6667
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006668 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02006669 surface->pending.buffer_viewport.buffer.src_width =
6670 wl_fixed_from_int(-1);
6671 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006672 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006673}
6674
6675static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006676viewport_destroy(struct wl_client *client,
6677 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006678{
6679 wl_resource_destroy(resource);
6680}
6681
6682static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006683viewport_set_source(struct wl_client *client,
6684 struct wl_resource *resource,
6685 wl_fixed_t src_x,
6686 wl_fixed_t src_y,
6687 wl_fixed_t src_width,
6688 wl_fixed_t src_height)
6689{
6690 struct weston_surface *surface =
6691 wl_resource_get_user_data(resource);
6692
Pekka Paalanen4826f872016-04-22 14:14:38 +03006693 if (!surface) {
6694 wl_resource_post_error(resource,
6695 WP_VIEWPORT_ERROR_NO_SURFACE,
6696 "wl_surface for this viewport is no longer exists");
6697 return;
6698 }
6699
6700 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03006701 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006702
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006703 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03006704 src_height == wl_fixed_from_int(-1) &&
6705 src_x == wl_fixed_from_int(-1) &&
6706 src_y == wl_fixed_from_int(-1)) {
6707 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006708 surface->pending.buffer_viewport.buffer.src_width =
6709 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006710 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006711 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006712 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006713
Pekka Paalanen201769a2016-04-26 14:42:11 +03006714 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006715 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006716 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03006717 "wl_surface@%d viewport source "
6718 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
6719 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006720 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03006721 wl_fixed_to_double(src_height),
6722 wl_fixed_to_double(src_x),
6723 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006724 return;
6725 }
6726
6727 surface->pending.buffer_viewport.buffer.src_x = src_x;
6728 surface->pending.buffer_viewport.buffer.src_y = src_y;
6729 surface->pending.buffer_viewport.buffer.src_width = src_width;
6730 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006731 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006732}
6733
6734static void
6735viewport_set_destination(struct wl_client *client,
6736 struct wl_resource *resource,
6737 int32_t dst_width,
6738 int32_t dst_height)
6739{
6740 struct weston_surface *surface =
6741 wl_resource_get_user_data(resource);
6742
Pekka Paalanen4826f872016-04-22 14:14:38 +03006743 if (!surface) {
6744 wl_resource_post_error(resource,
6745 WP_VIEWPORT_ERROR_NO_SURFACE,
6746 "wl_surface for this viewport no longer exists");
6747 return;
6748 }
6749
6750 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006751
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006752 if (dst_width == -1 && dst_height == -1) {
6753 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006754 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006755 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006756 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006757 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006758
6759 if (dst_width <= 0 || dst_height <= 0) {
6760 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006761 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006762 "destination size must be positive (%dx%d)",
6763 dst_width, dst_height);
6764 return;
6765 }
6766
6767 surface->pending.buffer_viewport.surface.width = dst_width;
6768 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006769 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006770}
6771
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006772static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006773 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006774 viewport_set_source,
6775 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01006776};
6777
6778static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006779viewporter_destroy(struct wl_client *client,
6780 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006781{
6782 wl_resource_destroy(resource);
6783}
6784
6785static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006786viewporter_get_viewport(struct wl_client *client,
6787 struct wl_resource *viewporter,
6788 uint32_t id,
6789 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006790{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006791 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006792 struct weston_surface *surface =
6793 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006794 struct wl_resource *resource;
6795
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006796 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006797 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006798 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006799 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01006800 return;
6801 }
6802
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006803 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006804 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006805 if (resource == NULL) {
6806 wl_client_post_no_memory(client);
6807 return;
6808 }
6809
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006810 wl_resource_set_implementation(resource, &viewport_interface,
6811 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01006812
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006813 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006814}
6815
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006816static const struct wp_viewporter_interface viewporter_interface = {
6817 viewporter_destroy,
6818 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01006819};
6820
6821static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006822bind_viewporter(struct wl_client *client,
6823 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006824{
6825 struct wl_resource *resource;
6826
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006827 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006828 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006829 if (resource == NULL) {
6830 wl_client_post_no_memory(client);
6831 return;
6832 }
6833
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006834 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01006835 NULL, NULL);
6836}
6837
6838static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04006839destroy_presentation_feedback(struct wl_resource *feedback_resource)
6840{
6841 struct weston_presentation_feedback *feedback;
6842
6843 feedback = wl_resource_get_user_data(feedback_resource);
6844
6845 wl_list_remove(&feedback->link);
6846 free(feedback);
6847}
6848
6849static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006850presentation_destroy(struct wl_client *client, struct wl_resource *resource)
6851{
6852 wl_resource_destroy(resource);
6853}
6854
6855static void
6856presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04006857 struct wl_resource *presentation_resource,
6858 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006859 uint32_t callback)
6860{
Pekka Paalanen133e4392014-09-23 22:08:46 -04006861 struct weston_surface *surface;
6862 struct weston_presentation_feedback *feedback;
6863
6864 surface = wl_resource_get_user_data(surface_resource);
6865
Bryce Harringtonde16d892014-11-20 22:21:57 -08006866 feedback = zalloc(sizeof *feedback);
6867 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04006868 goto err_calloc;
6869
6870 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006871 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04006872 1, callback);
6873 if (!feedback->resource)
6874 goto err_create;
6875
6876 wl_resource_set_implementation(feedback->resource, NULL, feedback,
6877 destroy_presentation_feedback);
6878
6879 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
6880
6881 return;
6882
6883err_create:
6884 free(feedback);
6885
6886err_calloc:
6887 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006888}
6889
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006890static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006891 presentation_destroy,
6892 presentation_feedback
6893};
6894
6895static void
6896bind_presentation(struct wl_client *client,
6897 void *data, uint32_t version, uint32_t id)
6898{
6899 struct weston_compositor *compositor = data;
6900 struct wl_resource *resource;
6901
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006902 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006903 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006904 if (resource == NULL) {
6905 wl_client_post_no_memory(client);
6906 return;
6907 }
6908
6909 wl_resource_set_implementation(resource, &presentation_implementation,
6910 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006911 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006912}
6913
6914static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05006915compositor_bind(struct wl_client *client,
6916 void *data, uint32_t version, uint32_t id)
6917{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006918 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05006919 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05006920
Jason Ekstranda85118c2013-06-27 20:17:02 -05006921 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006922 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07006923 if (resource == NULL) {
6924 wl_client_post_no_memory(client);
6925 return;
6926 }
6927
6928 wl_resource_set_implementation(resource, &compositor_interface,
6929 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05006930}
6931
Daniel Stonece62cb32018-07-20 09:46:24 +01006932static const char *
6933output_repaint_status_text(struct weston_output *output)
6934{
6935 switch (output->repaint_status) {
6936 case REPAINT_NOT_SCHEDULED:
6937 return "no repaint";
6938 case REPAINT_BEGIN_FROM_IDLE:
6939 return "start_repaint_loop scheduled";
6940 case REPAINT_SCHEDULED:
6941 return "repaint scheduled";
6942 case REPAINT_AWAITING_COMPLETION:
6943 return "awaiting completion";
6944 }
6945
6946 assert(!"output_repaint_status_text missing enum");
6947 return NULL;
6948}
6949
6950static void
6951debug_scene_view_print_buffer(FILE *fp, struct weston_view *view)
6952{
6953 struct weston_buffer *buffer = view->surface->buffer_ref.buffer;
6954 struct wl_shm_buffer *shm;
6955 struct linux_dmabuf_buffer *dmabuf;
Marius Vlad00a6e012018-11-20 17:52:31 +02006956 const struct pixel_format_info *pixel_info = NULL;
Daniel Stonece62cb32018-07-20 09:46:24 +01006957
6958 if (!buffer) {
6959 fprintf(fp, "\t\t[buffer not available]\n");
6960 return;
6961 }
6962
6963 shm = wl_shm_buffer_get(buffer->resource);
6964 if (shm) {
Marius Vlad00a6e012018-11-20 17:52:31 +02006965 uint32_t _format = wl_shm_buffer_get_format(shm);
6966 pixel_info = pixel_format_get_info_shm(_format);
Daniel Stonece62cb32018-07-20 09:46:24 +01006967 fprintf(fp, "\t\tSHM buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02006968 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
6969 (unsigned long) _format,
6970 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Daniel Stonece62cb32018-07-20 09:46:24 +01006971 return;
6972 }
6973
6974 dmabuf = linux_dmabuf_buffer_get(buffer->resource);
6975 if (dmabuf) {
Marius Vlad00a6e012018-11-20 17:52:31 +02006976 pixel_info = pixel_format_get_info(dmabuf->attributes.format);
Daniel Stonece62cb32018-07-20 09:46:24 +01006977 fprintf(fp, "\t\tdmabuf buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02006978 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
6979 (unsigned long) dmabuf->attributes.format,
6980 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Daniel Stonece62cb32018-07-20 09:46:24 +01006981 fprintf(fp, "\t\t\tmodifier: 0x%llx\n",
6982 (unsigned long long) dmabuf->attributes.modifier[0]);
6983 return;
6984 }
6985
Marius Vlad253ba9a2019-03-17 18:22:21 +02006986 fprintf(fp, "\t\tEGL buffer\n");
Daniel Stonece62cb32018-07-20 09:46:24 +01006987}
6988
6989static void
6990debug_scene_view_print(FILE *fp, struct weston_view *view, int view_idx)
6991{
6992 struct weston_compositor *ec = view->surface->compositor;
6993 struct weston_output *output;
6994 char desc[512];
6995 pixman_box32_t *box;
6996 uint32_t surface_id = 0;
6997 pid_t pid = 0;
6998
6999 if (view->surface->resource) {
7000 struct wl_resource *resource = view->surface->resource;
7001 wl_client_get_credentials(wl_resource_get_client(resource),
7002 &pid, NULL, NULL);
7003 surface_id = wl_resource_get_id(view->surface->resource);
7004 }
7005
7006 if (!view->surface->get_label ||
7007 view->surface->get_label(view->surface, desc, sizeof(desc)) < 0) {
7008 strcpy(desc, "[no description available]");
7009 }
7010 fprintf(fp, "\tView %d (role %s, PID %d, surface ID %u, %s, %p):\n",
7011 view_idx, view->surface->role_name, pid, surface_id,
7012 desc, view);
7013
7014 box = pixman_region32_extents(&view->transform.boundingbox);
7015 fprintf(fp, "\t\tposition: (%d, %d) -> (%d, %d)\n",
7016 box->x1, box->y1, box->x2, box->y2);
7017 box = pixman_region32_extents(&view->transform.opaque);
7018
7019 if (pixman_region32_equal(&view->transform.opaque,
7020 &view->transform.boundingbox)) {
7021 fprintf(fp, "\t\t[fully opaque]\n");
7022 } else if (!pixman_region32_not_empty(&view->transform.opaque)) {
7023 fprintf(fp, "\t\t[not opaque]\n");
7024 } else {
7025 fprintf(fp, "\t\t[opaque: (%d, %d) -> (%d, %d)]\n",
7026 box->x1, box->y1, box->x2, box->y2);
7027 }
7028
7029 if (view->alpha < 1.0)
7030 fprintf(fp, "\t\talpha: %f\n", view->alpha);
7031
7032 if (view->output_mask != 0) {
7033 bool first_output = true;
7034 fprintf(fp, "\t\toutputs: ");
7035 wl_list_for_each(output, &ec->output_list, link) {
7036 if (!(view->output_mask & (1 << output->id)))
7037 continue;
7038 fprintf(fp, "%s%d (%s)%s",
7039 (first_output) ? "" : ", ",
7040 output->id, output->name,
7041 (view->output == output) ? " (primary)" : "");
7042 first_output = false;
7043 }
7044 } else {
7045 fprintf(fp, "\t\t[no outputs]");
7046 }
7047
7048 fprintf(fp, "\n");
7049
7050 debug_scene_view_print_buffer(fp, view);
7051}
7052
Marius Vlad433f4e72019-02-17 22:14:23 +02007053static void
7054debug_scene_view_print_tree(struct weston_view *view,
Marius Vlada6acfa82019-03-17 18:10:09 +02007055 FILE *fp, int *view_idx)
Marius Vlad433f4e72019-02-17 22:14:23 +02007056{
7057 struct weston_subsurface *sub;
7058 struct weston_view *ev;
7059
7060 /*
7061 * print the view first, then we recursively go on printing
7062 * sub-surfaces. We bail out once no more sub-surfaces are available.
7063 */
Marius Vlada6acfa82019-03-17 18:10:09 +02007064 debug_scene_view_print(fp, view, *view_idx);
Marius Vlad433f4e72019-02-17 22:14:23 +02007065
7066 /* no more sub-surfaces */
7067 if (wl_list_empty(&view->surface->subsurface_list))
7068 return;
7069
7070 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
7071 wl_list_for_each(ev, &sub->surface->views, surface_link) {
7072 /* do not print again the parent view */
7073 if (view == ev)
7074 continue;
Marius Vlada6acfa82019-03-17 18:10:09 +02007075
7076 (*view_idx)++;
Marius Vlad433f4e72019-02-17 22:14:23 +02007077 debug_scene_view_print_tree(ev, fp, view_idx);
7078 }
7079 }
7080}
7081
Daniel Stonece62cb32018-07-20 09:46:24 +01007082/**
7083 * Output information on how libweston is currently composing the scene
7084 * graph.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007085 *
7086 * \ingroup compositor
Daniel Stonece62cb32018-07-20 09:46:24 +01007087 */
7088WL_EXPORT char *
7089weston_compositor_print_scene_graph(struct weston_compositor *ec)
7090{
7091 struct weston_output *output;
7092 struct weston_layer *layer;
7093 struct timespec now;
7094 int layer_idx = 0;
7095 FILE *fp;
7096 char *ret;
7097 size_t len;
7098 int err;
7099
7100 fp = open_memstream(&ret, &len);
7101 assert(fp);
7102
7103 weston_compositor_read_presentation_clock(ec, &now);
7104 fprintf(fp, "Weston scene graph at %ld.%09ld:\n\n",
7105 now.tv_sec, now.tv_nsec);
7106
7107 wl_list_for_each(output, &ec->output_list, link) {
7108 struct weston_head *head;
7109 int head_idx = 0;
7110
7111 fprintf(fp, "Output %d (%s):\n", output->id, output->name);
7112 assert(output->enabled);
7113
7114 fprintf(fp, "\tposition: (%d, %d) -> (%d, %d)\n",
7115 output->x, output->y,
7116 output->x + output->width,
7117 output->y + output->height);
7118 fprintf(fp, "\tmode: %dx%d@%.3fHz\n",
7119 output->current_mode->width,
7120 output->current_mode->height,
7121 output->current_mode->refresh / 1000.0);
7122 fprintf(fp, "\tscale: %d\n", output->scale);
7123
7124 fprintf(fp, "\trepaint status: %s\n",
7125 output_repaint_status_text(output));
7126 if (output->repaint_status == REPAINT_SCHEDULED)
7127 fprintf(fp, "\tnext repaint: %ld.%09ld\n",
7128 output->next_repaint.tv_sec,
7129 output->next_repaint.tv_nsec);
7130
7131 wl_list_for_each(head, &output->head_list, output_link) {
7132 fprintf(fp, "\tHead %d (%s): %sconnected\n",
7133 head_idx++, head->name,
7134 (head->connected) ? "" : "not ");
7135 }
7136 }
7137
7138 fprintf(fp, "\n");
7139
7140 wl_list_for_each(layer, &ec->layer_list, link) {
7141 struct weston_view *view;
7142 int view_idx = 0;
7143
7144 fprintf(fp, "Layer %d (pos 0x%lx):\n", layer_idx++,
7145 (unsigned long) layer->position);
7146
7147 if (!weston_layer_mask_is_infinite(layer)) {
7148 fprintf(fp, "\t[mask: (%d, %d) -> (%d,%d)]\n\n",
7149 layer->mask.x1, layer->mask.y1,
7150 layer->mask.x2, layer->mask.y2);
7151 }
7152
Marius Vlada6acfa82019-03-17 18:10:09 +02007153 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
7154 debug_scene_view_print_tree(view, fp, &view_idx);
7155 view_idx++;
7156 }
Daniel Stonece62cb32018-07-20 09:46:24 +01007157
7158 if (wl_list_empty(&layer->view_list.link))
7159 fprintf(fp, "\t[no views]\n");
7160
7161 fprintf(fp, "\n");
7162 }
7163
7164 err = fclose(fp);
7165 assert(err == 0);
7166
7167 return ret;
7168}
7169
7170/**
7171 * Called when the 'scene-graph' debug scope is bound by a client. This
7172 * one-shot weston-debug scope prints the current scene graph when bound,
7173 * and then terminates the stream.
7174 */
7175static void
Marius Vladdad882a2019-07-17 15:43:53 +03007176debug_scene_graph_cb(struct weston_log_subscription *sub, void *data)
Daniel Stonece62cb32018-07-20 09:46:24 +01007177{
7178 struct weston_compositor *ec = data;
7179 char *str = weston_compositor_print_scene_graph(ec);
7180
Marius Vladdad882a2019-07-17 15:43:53 +03007181 weston_log_subscription_printf(sub, "%s", str);
Daniel Stonece62cb32018-07-20 09:46:24 +01007182 free(str);
Marius Vladdad882a2019-07-17 15:43:53 +03007183 weston_log_subscription_complete(sub);
Daniel Stonece62cb32018-07-20 09:46:24 +01007184}
7185
Giulio Camuffo459137b2014-10-11 23:56:24 +03007186/** Create the compositor.
7187 *
7188 * This functions creates and initializes a compositor instance.
7189 *
7190 * \param display The Wayland display to be used.
7191 * \param user_data A pointer to an object that can later be retrieved
Marius Vlada2dace22019-06-12 16:05:44 +03007192 * \param log_ctx A pointer to weston_debug_compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007193 * using the \ref weston_compositor_get_user_data function.
7194 * \return The compositor instance on success or NULL on failure.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007195 *
7196 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007197 */
7198WL_EXPORT struct weston_compositor *
Marius Vlad880b4852019-04-07 17:07:58 +03007199weston_compositor_create(struct wl_display *display,
Marius Vlad3d7d9782019-04-17 12:35:38 +03007200 struct weston_log_context *log_ctx,
Marius Vlad880b4852019-04-07 17:07:58 +03007201 void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04007202{
Giulio Camuffo459137b2014-10-11 23:56:24 +03007203 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05007204 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07007205
Giulio Camuffo459137b2014-10-11 23:56:24 +03007206 ec = zalloc(sizeof *ec);
7207 if (!ec)
7208 return NULL;
7209
7210 ec->wl_display = display;
7211 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04007212 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07007213 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04007214 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03007215 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03007216 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02007217 wl_signal_init(&ec->idle_signal);
7218 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02007219 wl_signal_init(&ec->show_input_panel_signal);
7220 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02007221 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01007222 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01007223 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02007224 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02007225 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02007226 wl_signal_init(&ec->output_resized_signal);
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03007227 wl_signal_init(&ec->heads_changed_signal);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02007228 wl_signal_init(&ec->output_heads_changed_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07007229 wl_signal_init(&ec->session_signal);
Robert Beckettc569bdc2019-07-10 16:40:12 +01007230 ec->session_active = true;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04007231
Casey Dahlin58ba1372012-04-19 22:50:08 -04007232 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03007233 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04007234
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02007235 ec->activate_serial = 1;
7236
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05007237 ec->touch_mode = WESTON_TOUCH_MODE_NORMAL;
7238
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05307239 ec->content_protection = NULL;
7240
Derek Foreman152254b2015-11-26 14:17:48 -06007241 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04007242 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007243 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05007244
Giulio Camuffo954f1832014-10-11 18:27:30 +03007245 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04007246 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007247 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03007248
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007249 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007250 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007251 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01007252
Roman Gilge97391c2019-03-29 13:01:06 +01007253 if (!wl_global_create(ec->wl_display, &zxdg_output_manager_v1_interface, 2,
7254 ec, bind_xdg_output_manager))
7255 goto fail;
7256
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007257 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007258 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007259 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007260
Marius Vlad3d7d9782019-04-17 12:35:38 +03007261 if (weston_log_ctx_compositor_setup(ec, log_ctx) < 0)
Pekka Paalanena5630ea2017-10-12 13:13:42 +02007262 goto fail;
7263
Jonas Ådahl30d61d82014-10-22 21:21:17 +02007264 if (weston_input_init(ec) != 0)
7265 goto fail;
7266
Jason Ekstranda7af7042013-10-12 22:38:11 -05007267 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02007268 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007269 wl_list_init(&ec->layer_list);
7270 wl_list_init(&ec->seat_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007271 wl_list_init(&ec->pending_output_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007272 wl_list_init(&ec->output_list);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03007273 wl_list_init(&ec->head_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007274 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01007275 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007276 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01007277 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007278 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02007279 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007280
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007281 wl_list_init(&ec->plugin_api_list);
7282
Xiong Zhang97116532013-10-23 13:58:31 +08007283 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02007284 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04007285
Giulio Camuffo459137b2014-10-11 23:56:24 +03007286 wl_data_device_manager_init(ec->wl_display);
7287
7288 wl_display_init_shm(ec->wl_display);
7289
7290 loop = wl_display_get_event_loop(ec->wl_display);
7291 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Daniel Stone6847b852017-03-01 11:34:08 +00007292 ec->repaint_timer =
7293 wl_event_loop_add_timer(loop, output_repaint_timer_handler,
7294 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007295
Quentin Glidic82681572016-12-17 13:40:51 +01007296 weston_layer_init(&ec->fade_layer, ec);
7297 weston_layer_init(&ec->cursor_layer, ec);
7298
7299 weston_layer_set_position(&ec->fade_layer, WESTON_LAYER_POSITION_FADE);
7300 weston_layer_set_position(&ec->cursor_layer,
7301 WESTON_LAYER_POSITION_CURSOR);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007302
Daniel Stonece62cb32018-07-20 09:46:24 +01007303 ec->debug_scene =
Leandro Ribeiroce100192019-12-26 16:35:49 -03007304 weston_compositor_add_log_scope(ec, "scene-graph",
7305 "Scene graph details\n",
7306 debug_scene_graph_cb, NULL,
7307 ec);
Daniel Stonece62cb32018-07-20 09:46:24 +01007308
Marius Vladda104eb2019-09-05 14:31:01 +03007309 ec->timeline =
Leandro Ribeiroce100192019-12-26 16:35:49 -03007310 weston_compositor_add_log_scope(ec, "timeline",
7311 "Timeline event points\n",
7312 weston_timeline_create_subscription,
7313 weston_timeline_destroy_subscription,
7314 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007315 return ec;
7316
7317fail:
7318 free(ec);
7319 return NULL;
7320}
7321
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007322/** weston_compositor_shutdown
7323 * \ingroup compositor
7324 */
Benjamin Franzkeb8263022011-08-30 11:32:47 +02007325WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007326weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07007327{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007328 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07007329
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007330 wl_event_source_remove(ec->idle_source);
7331
Matt Roper361d2ad2011-08-29 13:52:23 -07007332 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02007333 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07007334 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02007335
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007336 /* Destroy all pending outputs associated with this compositor */
7337 wl_list_for_each_safe(output, next, &ec->pending_output_list, link)
7338 output->destroy(output);
7339
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02007340 if (ec->renderer)
7341 ec->renderer->destroy(ec);
7342
Daniel Stone325fc2d2012-05-30 16:31:58 +01007343 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09007344 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01007345 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01007346 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01007347 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02007348 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007349
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04007350 weston_plane_release(&ec->primary_plane);
Matt Roper361d2ad2011-08-29 13:52:23 -07007351}
7352
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007353/** weston_compositor_exit_with_code
7354 * \ingroup compositor
7355 */
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05007356WL_EXPORT void
Adam Jackson3c3f3b12019-10-16 16:02:59 -04007357weston_compositor_exit_with_code(struct weston_compositor *compositor,
Frederic Plourdec336f062014-10-29 14:44:33 -04007358 int exit_code)
7359{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02007360 if (compositor->exit_code == EXIT_SUCCESS)
7361 compositor->exit_code = exit_code;
7362
Giulio Camuffo459137b2014-10-11 23:56:24 +03007363 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04007364}
7365
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007366/** weston_compositor_set_default_pointer_grab
7367 * \ingroup compositor
7368 */
Frederic Plourdec336f062014-10-29 14:44:33 -04007369WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007370weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
7371 const struct weston_pointer_grab_interface *interface)
7372{
7373 struct weston_seat *seat;
7374
7375 ec->default_pointer_grab = interface;
7376 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05007377 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
7378
7379 if (pointer)
7380 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007381 }
7382}
7383
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007384/** weston_compositor_set_presentation_clock
7385 * \ingroup compositor
7386 */
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007387WL_EXPORT int
7388weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
7389 clockid_t clk_id)
7390{
7391 struct timespec ts;
7392
7393 if (clock_gettime(clk_id, &ts) < 0)
7394 return -1;
7395
7396 compositor->presentation_clock = clk_id;
7397
7398 return 0;
7399}
7400
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007401/** For choosing the software clock, when the display hardware or API
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007402 * does not expose a compatible presentation timestamp.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007403 *
7404 * \ingroup compositor
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007405 */
7406WL_EXPORT int
7407weston_compositor_set_presentation_clock_software(
7408 struct weston_compositor *compositor)
7409{
7410 /* In order of preference */
7411 static const clockid_t clocks[] = {
7412 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
7413 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
7414 CLOCK_MONOTONIC, /* no jumps, may crawl */
7415 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
7416 CLOCK_REALTIME /* may jump and crawl */
7417 };
7418 unsigned i;
7419
7420 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
7421 if (weston_compositor_set_presentation_clock(compositor,
7422 clocks[i]) == 0)
7423 return 0;
7424
7425 weston_log("Error: no suitable presentation clock available.\n");
7426
7427 return -1;
7428}
7429
Pekka Paalanen662f3842015-03-18 12:17:26 +02007430/** Read the current time from the Presentation clock
7431 *
7432 * \param compositor
Marius Vlada2dace22019-06-12 16:05:44 +03007433 * \param[out] ts The current time.
Pekka Paalanen662f3842015-03-18 12:17:26 +02007434 *
7435 * \note Reading the current time in user space is always imprecise to some
7436 * degree.
7437 *
7438 * This function is never meant to fail. If reading the clock does fail,
7439 * an error message is logged and a zero time is returned. Callers are not
7440 * supposed to detect or react to failures.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007441 *
7442 * \ingroup compositor
Pekka Paalanen662f3842015-03-18 12:17:26 +02007443 */
7444WL_EXPORT void
7445weston_compositor_read_presentation_clock(
7446 const struct weston_compositor *compositor,
7447 struct timespec *ts)
7448{
7449 static bool warned;
7450 int ret;
7451
7452 ret = clock_gettime(compositor->presentation_clock, ts);
7453 if (ret < 0) {
7454 ts->tv_sec = 0;
7455 ts->tv_nsec = 0;
7456
7457 if (!warned)
7458 weston_log("Error: failure to read "
Antonio Borneo39578632019-04-26 23:57:31 +02007459 "the presentation clock %#x: '%s' (%d)\n",
7460 compositor->presentation_clock,
7461 strerror(errno), errno);
Pekka Paalanen662f3842015-03-18 12:17:26 +02007462 warned = true;
7463 }
7464}
7465
Pekka Paalanen230f3b12014-09-29 14:18:40 -04007466/** Import dmabuf buffer into current renderer
7467 *
7468 * \param compositor
7469 * \param buffer the dmabuf buffer to import
7470 * \return true on usable buffers, false otherwise
7471 *
7472 * This function tests that the linux_dmabuf_buffer is usable
7473 * for the current renderer. Returns false on unusable buffers. Usually
7474 * usability is tested by importing the dmabufs for composition.
7475 *
7476 * This hook is also used for detecting if the renderer supports
7477 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
7478 * supported.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007479 *
7480 * \ingroup compositor
7481 */
Pekka Paalanen230f3b12014-09-29 14:18:40 -04007482WL_EXPORT bool
7483weston_compositor_import_dmabuf(struct weston_compositor *compositor,
7484 struct linux_dmabuf_buffer *buffer)
7485{
7486 struct weston_renderer *renderer;
7487
7488 renderer = compositor->renderer;
7489
7490 if (renderer->import_dmabuf == NULL)
7491 return false;
7492
7493 return renderer->import_dmabuf(compositor, buffer);
7494}
7495
Marius Vlad5a701542019-11-16 20:26:52 +02007496WL_EXPORT bool
7497weston_compositor_dmabuf_can_scanout(struct weston_compositor *compositor,
7498 struct linux_dmabuf_buffer *buffer)
7499{
7500 struct weston_backend *backend = compositor->backend;
7501
7502 if (backend->can_scanout_dmabuf == NULL)
7503 return false;
7504
7505 return backend->can_scanout_dmabuf(compositor, buffer);
7506}
7507
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007508WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05007509weston_version(int *major, int *minor, int *micro)
7510{
7511 *major = WESTON_VERSION_MAJOR;
7512 *minor = WESTON_VERSION_MINOR;
7513 *micro = WESTON_VERSION_MICRO;
7514}
7515
Daniel Stonee03c1112016-11-24 20:45:45 +00007516/**
7517 * Attempts to find a module path from the module map specified in the
7518 * environment. If found, writes the full path into the path variable.
7519 *
7520 * The module map is a string in environment variable WESTON_MODULE_MAP, where
7521 * each entry is of the form "name=path" and entries are separated by
7522 * semicolons. Whitespace is significant.
7523 *
7524 * \param name The name to search for.
7525 * \param path Where the path is written to if found.
7526 * \param path_len Allocated bytes at \c path .
7527 * \returns The length of the string written to path on success, or 0 if the
7528 * module was not specified in the environment map or path_len was too small.
7529 */
7530WL_EXPORT size_t
7531weston_module_path_from_env(const char *name, char *path, size_t path_len)
7532{
7533 const char *mapping = getenv("WESTON_MODULE_MAP");
7534 const char *end;
7535 const int name_len = strlen(name);
7536
7537 if (!mapping)
7538 return 0;
7539
7540 end = mapping + strlen(mapping);
7541 while (mapping < end && *mapping) {
7542 const char *filename, *next;
7543
7544 /* early out: impossibly short string */
7545 if (end - mapping < name_len + 1)
7546 return 0;
7547
7548 filename = &mapping[name_len + 1];
7549 next = strchrnul(mapping, ';');
7550
7551 if (strncmp(mapping, name, name_len) == 0 &&
7552 mapping[name_len] == '=') {
7553 size_t file_len = next - filename; /* no trailing NUL */
7554 if (file_len >= path_len)
7555 return 0;
7556 strncpy(path, filename, file_len);
7557 path[file_len] = '\0';
7558 return file_len;
7559 }
7560
7561 mapping = next + 1;
7562 }
7563
7564 return 0;
7565}
7566
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03007567WL_EXPORT void *
7568weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007569{
7570 char path[PATH_MAX];
7571 void *module, *init;
Daniel Stonebeb97e52016-11-28 12:13:54 +00007572 size_t len;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007573
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08007574 if (name == NULL)
7575 return NULL;
7576
Derek Foreman3f86e502015-06-08 11:46:54 -05007577 if (name[0] != '/') {
Daniel Stonee03c1112016-11-24 20:45:45 +00007578 len = weston_module_path_from_env(name, path, sizeof path);
7579 if (len == 0)
Daniel Stonebeb97e52016-11-28 12:13:54 +00007580 len = snprintf(path, sizeof path, "%s/%s",
7581 LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05007582 } else {
Daniel Stonebeb97e52016-11-28 12:13:54 +00007583 len = snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05007584 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007585
Daniel Stonebeb97e52016-11-28 12:13:54 +00007586 /* snprintf returns the length of the string it would've written,
7587 * _excluding_ the NUL byte. So even being equal to the size of
7588 * our buffer is an error here. */
7589 if (len >= sizeof path)
7590 return NULL;
7591
Kristian Høgsberga6813d22012-09-12 12:21:01 -04007592 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
7593 if (module) {
7594 weston_log("Module '%s' already loaded\n", path);
Pekka Paalanenf696ee92019-11-04 14:07:51 +02007595 } else {
7596 weston_log("Loading module '%s'\n", path);
7597 module = dlopen(path, RTLD_NOW);
7598 if (!module) {
7599 weston_log("Failed to load module: %s\n", dlerror());
7600 return NULL;
7601 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007602 }
7603
7604 init = dlsym(module, entrypoint);
7605 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03007606 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00007607 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007608 return NULL;
7609 }
7610
7611 return init;
7612}
7613
Pekka Paalanen6ffbba32019-11-06 12:59:32 +02007614/** Add a compositor destroy listener only once
7615 *
7616 * \param compositor The compositor whose destroy to watch for.
7617 * \param listener The listener struct to initialize.
7618 * \param destroy_handler The callback when compositor is destroyed.
7619 * \return True if listener is added, or false if there already is a listener
7620 * with the given \c destroy_handler.
7621 *
7622 * This function does nothing and returns false if the given callback function
7623 * is already present in the weston_compositor destroy callbacks list.
7624 * Otherwise, this function initializes the given listener with the given
7625 * callback pointer and adds it to the compositor's destroy callbacks list.
7626 *
7627 * This can be used to ensure that plugin initialization is done only once
7628 * in case the same plugin is loaded multiple times. If this function returns
7629 * false, the plugin should be already initialized successfully.
7630 *
7631 * All plugins should register a destroy listener for cleaning up. Note, that
7632 * the plugin destruction order is not guaranteed: plugins that depend on other
7633 * plugins must be able to be torn down in arbitrary order.
7634 *
7635 * \sa weston_compositor_tear_down, weston_compositor_destroy
7636 */
7637WL_EXPORT bool
7638weston_compositor_add_destroy_listener_once(struct weston_compositor *compositor,
7639 struct wl_listener *listener,
7640 wl_notify_func_t destroy_handler)
7641{
7642 if (wl_signal_get(&compositor->destroy_signal, destroy_handler))
7643 return false;
7644
7645 listener->notify = destroy_handler;
7646 wl_signal_add(&compositor->destroy_signal, listener);
7647 return true;
7648}
Giulio Camuffo459137b2014-10-11 23:56:24 +03007649
Marius Vlad284d5342019-06-24 12:00:47 +03007650/** Tear down the compositor.
Giulio Camuffo459137b2014-10-11 23:56:24 +03007651 *
Marius Vlad284d5342019-06-24 12:00:47 +03007652 * This function cleans up the compositor state. While the compositor state has
7653 * been cleaned do note that **only** weston_compositor_destroy() can be called
7654 * afterwards, in order to destroy the compositor instance.
Giulio Camuffo459137b2014-10-11 23:56:24 +03007655 *
Marius Vlad284d5342019-06-24 12:00:47 +03007656 * @param compositor The compositor to be tear-down/cleaned.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007657 *
Marius Vlad284d5342019-06-24 12:00:47 +03007658 * @ingroup compositor
7659 * @sa weston_compositor_destroy
Giulio Camuffo459137b2014-10-11 23:56:24 +03007660 */
7661WL_EXPORT void
Marius Vlad284d5342019-06-24 12:00:47 +03007662weston_compositor_tear_down(struct weston_compositor *compositor)
Giulio Camuffo459137b2014-10-11 23:56:24 +03007663{
7664 /* prevent further rendering while shutting down */
7665 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
7666
7667 wl_signal_emit(&compositor->destroy_signal, compositor);
7668
7669 weston_compositor_xkb_destroy(compositor);
7670
Giulio Camuffo2d24e642015-10-03 16:25:15 +03007671 if (compositor->backend)
7672 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007673
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03007674 /* The backend is responsible for destroying the heads. */
7675 assert(wl_list_empty(&compositor->head_list));
7676
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007677 weston_plugin_api_destroy_list(compositor);
7678
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03007679 if (compositor->heads_changed_source)
7680 wl_event_source_remove(compositor->heads_changed_source);
7681
Leandro Ribeirof0149642019-12-18 15:52:18 -03007682 weston_log_scope_destroy(compositor->debug_scene);
Daniel Stonece62cb32018-07-20 09:46:24 +01007683 compositor->debug_scene = NULL;
Marius Vladda104eb2019-09-05 14:31:01 +03007684
Leandro Ribeirof0149642019-12-18 15:52:18 -03007685 weston_log_scope_destroy(compositor->timeline);
Marius Vladda104eb2019-09-05 14:31:01 +03007686 compositor->timeline = NULL;
Marius Vlad284d5342019-06-24 12:00:47 +03007687}
Pekka Paalanena5630ea2017-10-12 13:13:42 +02007688
Marius Vlad284d5342019-06-24 12:00:47 +03007689/** Destroys the compositor.
7690 *
7691 * This function destroys the compositor. **Do not** call this before
7692 * calling weston_compositor_tear_down()
7693 *
7694 * @param compositor The compositor to be destroyed.
7695 * @ingroup compositor
7696 * @sa weston_compositor_tear_down()
7697 */
7698WL_EXPORT void
7699weston_compositor_destroy(struct weston_compositor *compositor)
7700{
Giulio Camuffo459137b2014-10-11 23:56:24 +03007701 free(compositor);
7702}
7703
7704/** Instruct the compositor to exit.
7705 *
7706 * This functions does not directly destroy the compositor object, it merely
7707 * command it to start the tear down process. It is not guaranteed that the
7708 * tear down will happen immediately.
7709 *
7710 * \param compositor The compositor to tear down.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007711 *
7712 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007713 */
7714WL_EXPORT void
7715weston_compositor_exit(struct weston_compositor *compositor)
7716{
7717 compositor->exit(compositor);
7718}
7719
7720/** Return the user data stored in the compositor.
7721 *
7722 * This function returns the user data pointer set with user_data parameter
7723 * to the \ref weston_compositor_create function.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007724 *
7725 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007726 */
7727WL_EXPORT void *
7728weston_compositor_get_user_data(struct weston_compositor *compositor)
7729{
7730 return compositor->user_data;
7731}
Pekka Paalanendd186732016-06-03 14:49:54 +03007732
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007733static const char * const backend_map[] = {
7734 [WESTON_BACKEND_DRM] = "drm-backend.so",
7735 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
7736 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
7737 [WESTON_BACKEND_RDP] = "rdp-backend.so",
7738 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
7739 [WESTON_BACKEND_X11] = "x11-backend.so",
7740};
7741
Pekka Paalanendd186732016-06-03 14:49:54 +03007742/** Load a backend into a weston_compositor
7743 *
7744 * A backend must be loaded to make a weston_compositor work. A backend
7745 * provides input and output capabilities, and determines the renderer to use.
7746 *
7747 * \param compositor A compositor that has not had a backend loaded yet.
7748 * \param backend Name of the backend file.
7749 * \param config_base A pointer to a backend-specific configuration
7750 * structure's 'base' member.
7751 *
7752 * \return 0 on success, or -1 on error.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007753 *
7754 * \ingroup compositor
Pekka Paalanendd186732016-06-03 14:49:54 +03007755 */
7756WL_EXPORT int
7757weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007758 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03007759 struct weston_backend_config *config_base)
7760{
7761 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03007762 struct weston_backend_config *config_base);
7763
Pekka Paalanend7e35112017-08-29 17:04:12 +03007764 if (compositor->backend) {
7765 weston_log("Error: attempt to load a backend when one is already loaded\n");
7766 return -1;
7767 }
7768
Quentin Glidic887c0182016-07-10 11:00:53 +02007769 if (backend >= ARRAY_LENGTH(backend_map))
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007770 return -1;
7771
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01007772 backend_init = weston_load_module(backend_map[backend], "weston_backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03007773 if (!backend_init)
7774 return -1;
7775
Pekka Paalanend7e35112017-08-29 17:04:12 +03007776 if (backend_init(compositor, config_base) < 0) {
7777 compositor->backend = NULL;
7778 return -1;
7779 }
7780
7781 return 0;
Pekka Paalanendd186732016-06-03 14:49:54 +03007782}
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007783
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007784/** weston_compositor_load_xwayland
7785 * \ingroup compositor
7786 */
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007787WL_EXPORT int
7788weston_compositor_load_xwayland(struct weston_compositor *compositor)
7789{
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007790 int (*module_init)(struct weston_compositor *ec);
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007791
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007792 module_init = weston_load_module("xwayland.so", "weston_module_init");
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007793 if (!module_init)
7794 return -1;
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007795 if (module_init(compositor) < 0)
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007796 return -1;
7797 return 0;
7798}
Vasilis Liaskovitis486b4632018-10-10 16:14:55 +02007799
7800/** Resolve an internal compositor error by disconnecting the client.
7801 *
7802 * This function is used in cases when the wl_buffer turns out
7803 * unusable and there is no fallback path.
7804 *
7805 * It is possible the fault is caused by a compositor bug, the underlying
7806 * graphics stack bug or normal behaviour, or perhaps a client mistake.
7807 * In any case, the options are to either composite garbage or nothing,
7808 * or disconnect the client. This is a helper function for the latter.
7809 *
7810 * The error is sent as an INVALID_OBJECT error on the client's wl_display.
7811 *
7812 * \param buffer The weston buffer that is unusable.
7813 * \param msg A custom error message attached to the protocol error.
7814 */
7815WL_EXPORT void
7816weston_buffer_send_server_error(struct weston_buffer *buffer,
7817 const char *msg)
7818{
7819 struct wl_client *client;
7820 struct wl_resource *display_resource;
7821 uint32_t id;
7822
7823 assert(buffer->resource);
7824 id = wl_resource_get_id(buffer->resource);
7825 client = wl_resource_get_client(buffer->resource);
7826 display_resource = wl_client_get_object(client, 1);
7827
7828 assert(display_resource);
7829 wl_resource_post_error(display_resource,
7830 WL_DISPLAY_ERROR_INVALID_OBJECT,
7831 "server error with "
7832 "wl_buffer@%u: %s", id, msg);
7833}
Ankit Nautiyal93dde242019-07-08 11:46:42 +05307834
7835WL_EXPORT void
7836weston_output_disable_planes_incr(struct weston_output *output)
7837{
7838 output->disable_planes++;
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05307839 /*
7840 * If disable_planes changes from 0 to non-zero, it means some type of
7841 * recording of content has started, and therefore protection level of
7842 * the protected surfaces must be updated to avoid the recording of
7843 * the protected content.
7844 */
7845 if (output->disable_planes == 1)
7846 weston_schedule_surface_protection_update(output->compositor);
Ankit Nautiyal93dde242019-07-08 11:46:42 +05307847}
7848
7849WL_EXPORT void
7850weston_output_disable_planes_decr(struct weston_output *output)
7851{
7852 output->disable_planes--;
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05307853 /*
7854 * If disable_planes changes from non-zero to 0, it means no content
7855 * recording is going on any more, and the protected and surfaces can be
7856 * shown without any apprehensions about content being recorded.
7857 */
7858 if (output->disable_planes == 0)
7859 weston_schedule_surface_protection_update(output->compositor);
7860
Ankit Nautiyal93dde242019-07-08 11:46:42 +05307861}