blob: 57b220302b39ffc00235d354c56c67f9bbcba8f2 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04002 * Copyright © 2010-2011 Intel Corporation
3 * Copyright © 2008-2011 Kristian Høgsberg
Pekka Paalanen925788f2018-04-19 14:20:01 +03004 * Copyright © 2012-2018 Collabora, Ltd.
5 * Copyright © 2017, 2018 General Electric Company
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05006 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -07007 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050014 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -070015 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial
17 * portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
23 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
24 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050027 */
28
Kristian Høgsberga9410222011-01-14 17:22:35 -050029#include "config.h"
30
Daniel Stoneb7452fe2012-06-01 12:14:06 +010031#include <fcntl.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040032#include <stdio.h>
33#include <string.h>
34#include <stdlib.h>
35#include <stdint.h>
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +010036#include <limits.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050037#include <stdarg.h>
Benjamin Franzke6f5fc692011-06-21 19:34:19 +020038#include <assert.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040039#include <sys/ioctl.h>
Daniel Stoneb7452fe2012-06-01 12:14:06 +010040#include <sys/mman.h>
Kristian Høgsberg27da5382011-06-21 17:32:25 -040041#include <sys/wait.h>
Pekka Paalanen409ef0a2011-12-02 15:30:21 +020042#include <sys/socket.h>
Martin Minarikf12c2872012-06-11 00:57:39 +020043#include <sys/utsname.h>
Martin Minarik37032f82012-06-18 20:15:18 +020044#include <sys/stat.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040045#include <unistd.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050046#include <math.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040047#include <linux/input.h>
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040048#include <dlfcn.h>
Kristian Høgsberg85449032011-05-02 12:11:07 -040049#include <signal.h>
Kristian Høgsberg0690da62012-01-16 11:53:54 -050050#include <setjmp.h>
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040051#include <sys/time.h>
52#include <time.h>
Pekka Paalanen23ade622014-08-27 13:31:26 +030053#include <errno.h>
Alexandros Frantzisacff29b2018-10-19 12:14:11 +030054#include <inttypes.h>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050055
Pekka Paalanenb5026542014-11-12 15:09:24 +020056#include "timeline.h"
57
Pekka Paalanen3d5d9472019-03-28 16:28:47 +020058#include <libweston/libweston.h>
Marius Vladc901e892019-06-21 22:49:18 +030059#include <libweston/weston-log.h>
Daniel Stonece62cb32018-07-20 09:46:24 +010060#include "linux-dmabuf.h"
Pekka Paalanene95ad5c2016-04-15 14:47:08 +030061#include "viewporter-server-protocol.h"
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020062#include "presentation-time-server-protocol.h"
Roman Gilge97391c2019-03-29 13:01:06 +010063#include "xdg-output-unstable-v1-server-protocol.h"
Alexandros Frantzisacff29b2018-10-19 12:14:11 +030064#include "linux-explicit-synchronization-unstable-v1-server-protocol.h"
Alexandros Frantzis67629672018-10-19 12:14:11 +030065#include "linux-explicit-synchronization.h"
Alexandros Frantzisacff29b2018-10-19 12:14:11 +030066#include "shared/fd-util.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070067#include "shared/helpers.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070068#include "shared/os-compatibility.h"
Bryce Harrington25a2bdd2016-08-03 17:40:52 -070069#include "shared/string-helpers.h"
Pekka Paalanenaa21f622015-07-03 15:44:50 +030070#include "shared/timespec-util.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040071#include "git-version.h"
Pekka Paalanencda14882019-04-04 15:41:02 +030072#include <libweston/version.h>
Pekka Paalanen27b377f2019-03-29 17:07:34 +020073#include <libweston/plugin-registry.h>
Marius Vlad00a6e012018-11-20 17:52:31 +020074#include "pixel-formats.h"
Marius Vlad63ef0782019-07-16 23:34:14 +030075#include "backend.h"
Marius Vlada72e3712019-07-10 13:46:39 +030076#include "libweston-internal.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050077
Marius Vlad4e036292019-07-09 00:36:20 +030078#include "weston-log-internal.h"
79
Marius Vlad78984ee2019-06-11 00:05:08 +030080/**
81 * \defgroup head Head
82 * \defgroup output Output
83 * \defgroup compositor Compositor
84 */
85
Pekka Paalanen0513a952014-05-21 16:17:27 +030086#define DEFAULT_REPAINT_WINDOW 7 /* milliseconds */
87
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020088static void
Pekka Paalanend72bad22017-03-29 17:01:41 +030089weston_output_update_matrix(struct weston_output *output);
90
91static void
Alexander Larsson0b135062013-05-28 16:23:36 +020092weston_output_transform_scale_init(struct weston_output *output,
93 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020094
Rob Bradford27b17932013-06-26 18:08:46 +010095static void
Jason Ekstranda7af7042013-10-12 22:38:11 -050096weston_compositor_build_view_list(struct weston_compositor *compositor);
Rob Bradford27b17932013-06-26 18:08:46 +010097
Pekka Paalanendcac3512017-12-08 14:13:34 +020098static char *
99weston_output_create_heads_string(struct weston_output *output);
100
Pekka Paalanen6528c032017-03-28 15:27:10 +0300101/** Send wl_output events for mode and scale changes
102 *
103 * \param head Send on all resources bound to this head.
104 * \param mode_changed If true, send the current mode.
105 * \param scale_changed If true, send the current scale.
106 */
107static void
108weston_mode_switch_send_events(struct weston_head *head,
109 bool mode_changed, bool scale_changed)
110{
111 struct weston_output *output = head->output;
112 struct wl_resource *resource;
113 int version;
114
115 wl_resource_for_each(resource, &head->resource_list) {
116 if (mode_changed) {
117 wl_output_send_mode(resource,
118 output->current_mode->flags,
119 output->current_mode->width,
120 output->current_mode->height,
121 output->current_mode->refresh);
122 }
123
124 version = wl_resource_get_version(resource);
125 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION && scale_changed)
126 wl_output_send_scale(resource, output->current_scale);
127
128 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
129 wl_output_send_done(resource);
130 }
Roman Gilge97391c2019-03-29 13:01:06 +0100131 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
132 zxdg_output_v1_send_logical_position(resource,
133 output->x,
134 output->y);
135 zxdg_output_v1_send_logical_size(resource,
136 output->width,
137 output->height);
138 zxdg_output_v1_send_done(resource);
139 }
Pekka Paalanen6528c032017-03-28 15:27:10 +0300140}
141
142static void
143weston_mode_switch_finish(struct weston_output *output,
144 int mode_changed, int scale_changed)
Alex Wu2dda6042012-04-17 17:20:47 +0800145{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200146 struct weston_seat *seat;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300147 struct weston_head *head;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200148 pixman_region32_t old_output_region;
Alexander Larsson355748e2013-05-28 16:23:38 +0200149
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200150 pixman_region32_init(&old_output_region);
151 pixman_region32_copy(&old_output_region, &output->region);
152
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200153 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +0200154 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200155
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200156 pixman_region32_init_rect(&output->region, output->x, output->y,
157 output->width, output->height);
158
159 weston_output_update_matrix(output);
160
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200161 /* If a pointer falls outside the outputs new geometry, move it to its
162 * lower-right corner */
163 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500164 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200165 int32_t x, y;
166
167 if (!pointer)
168 continue;
169
170 x = wl_fixed_to_int(pointer->x);
171 y = wl_fixed_to_int(pointer->y);
172
173 if (!pixman_region32_contains_point(&old_output_region,
174 x, y, NULL) ||
175 pixman_region32_contains_point(&output->region,
176 x, y, NULL))
177 continue;
178
179 if (x >= output->x + output->width)
180 x = output->x + output->width - 1;
181 if (y >= output->y + output->height)
182 y = output->y + output->height - 1;
183
184 pointer->x = wl_fixed_from_int(x);
185 pointer->y = wl_fixed_from_int(y);
186 }
187
188 pixman_region32_fini(&old_output_region);
189
Derek Foremandd4cd332014-11-10 10:29:59 -0600190 if (!mode_changed && !scale_changed)
191 return;
192
Hardening57388e42013-09-18 23:56:36 +0200193 /* notify clients of the changes */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300194 wl_list_for_each(head, &output->head_list, output_link)
195 weston_mode_switch_send_events(head,
196 mode_changed, scale_changed);
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600197}
198
David Fort0de859e2016-05-27 23:22:57 +0200199static void
200weston_compositor_reflow_outputs(struct weston_compositor *compositor,
201 struct weston_output *resized_output, int delta_width);
202
Marius Vlad55d87362019-06-11 01:15:35 +0300203/**
204 * \ingroup output
205 */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600206WL_EXPORT int
207weston_output_mode_set_native(struct weston_output *output,
208 struct weston_mode *mode,
209 int32_t scale)
210{
211 int ret;
212 int mode_changed = 0, scale_changed = 0;
David Fort0de859e2016-05-27 23:22:57 +0200213 int32_t old_width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600214
215 if (!output->switch_mode)
216 return -1;
217
218 if (!output->original_mode) {
219 mode_changed = 1;
220 ret = output->switch_mode(output, mode);
221 if (ret < 0)
222 return ret;
223 if (output->current_scale != scale) {
224 scale_changed = 1;
225 output->current_scale = scale;
Hardening57388e42013-09-18 23:56:36 +0200226 }
227 }
228
David Fort0de859e2016-05-27 23:22:57 +0200229 old_width = output->width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600230 output->native_mode = mode;
231 output->native_scale = scale;
232
233 weston_mode_switch_finish(output, mode_changed, scale_changed);
234
David Fort0de859e2016-05-27 23:22:57 +0200235 if (mode_changed || scale_changed) {
236 weston_compositor_reflow_outputs(output->compositor, output, output->width - old_width);
237
238 wl_signal_emit(&output->compositor->output_resized_signal, output);
239 }
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600240 return 0;
241}
242
Marius Vlad55d87362019-06-11 01:15:35 +0300243/**
244 * \ingroup output
245 */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600246WL_EXPORT int
247weston_output_mode_switch_to_native(struct weston_output *output)
248{
249 int ret;
250 int mode_changed = 0, scale_changed = 0;
251
252 if (!output->switch_mode)
253 return -1;
254
255 if (!output->original_mode) {
256 weston_log("already in the native mode\n");
257 return -1;
258 }
259 /* the non fullscreen clients haven't seen a mode set since we
260 * switched into a temporary, so we need to notify them if the
261 * mode at that time is different from the native mode now.
262 */
263 mode_changed = (output->original_mode != output->native_mode);
264 scale_changed = (output->original_scale != output->native_scale);
265
266 ret = output->switch_mode(output, output->native_mode);
267 if (ret < 0)
268 return ret;
269
270 output->current_scale = output->native_scale;
271
272 output->original_mode = NULL;
273 output->original_scale = 0;
274
275 weston_mode_switch_finish(output, mode_changed, scale_changed);
276
277 return 0;
278}
279
Marius Vlad55d87362019-06-11 01:15:35 +0300280/**
281 * \ingroup output
282 */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600283WL_EXPORT int
284weston_output_mode_switch_to_temporary(struct weston_output *output,
285 struct weston_mode *mode,
286 int32_t scale)
287{
288 int ret;
289
290 if (!output->switch_mode)
291 return -1;
292
293 /* original_mode is the last mode non full screen clients have seen,
294 * so we shouldn't change it if we already have one set.
295 */
296 if (!output->original_mode) {
297 output->original_mode = output->native_mode;
298 output->original_scale = output->native_scale;
299 }
300 ret = output->switch_mode(output, mode);
301 if (ret < 0)
302 return ret;
303
304 output->current_scale = scale;
305
306 weston_mode_switch_finish(output, 0, 0);
307
308 return 0;
Alex Wu2dda6042012-04-17 17:20:47 +0800309}
310
Benjamin Franzke06286262011-05-06 19:12:33 +0200311static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300312region_init_infinite(pixman_region32_t *region)
313{
314 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
315 UINT32_MAX, UINT32_MAX);
316}
317
Pekka Paalanene67858b2013-04-25 13:57:42 +0300318static struct weston_subsurface *
319weston_surface_to_subsurface(struct weston_surface *surface);
320
Jason Ekstranda7af7042013-10-12 22:38:11 -0500321WL_EXPORT struct weston_view *
322weston_view_create(struct weston_surface *surface)
323{
324 struct weston_view *view;
325
Bryce Harringtonde16d892014-11-20 22:21:57 -0800326 view = zalloc(sizeof *view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500327 if (view == NULL)
328 return NULL;
329
330 view->surface = surface;
Daniel Stonefb4869d2016-12-09 16:27:54 +0000331 view->plane = &surface->compositor->primary_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500332
Jason Ekstranda7af7042013-10-12 22:38:11 -0500333 /* Assign to surface */
334 wl_list_insert(&surface->views, &view->surface_link);
335
336 wl_signal_init(&view->destroy_signal);
337 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300338 wl_list_init(&view->layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500339
Jason Ekstranda7af7042013-10-12 22:38:11 -0500340 pixman_region32_init(&view->clip);
341
342 view->alpha = 1.0;
343 pixman_region32_init(&view->transform.opaque);
344
345 wl_list_init(&view->geometry.transformation_list);
346 wl_list_insert(&view->geometry.transformation_list,
347 &view->transform.position.link);
348 weston_matrix_init(&view->transform.position.matrix);
349 wl_list_init(&view->geometry.child_list);
Pekka Paalanen380adf52015-02-16 14:39:11 +0200350 pixman_region32_init(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500351 pixman_region32_init(&view->transform.boundingbox);
352 view->transform.dirty = 1;
353
Jason Ekstranda7af7042013-10-12 22:38:11 -0500354 return view;
355}
356
Jason Ekstrand108865d2014-06-26 10:04:49 -0700357struct weston_frame_callback {
358 struct wl_resource *resource;
359 struct wl_list link;
360};
361
Pekka Paalanen133e4392014-09-23 22:08:46 -0400362struct weston_presentation_feedback {
363 struct wl_resource *resource;
364
365 /* XXX: could use just wl_resource_get_link() instead */
366 struct wl_list link;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200367
368 /* The per-surface feedback flags */
369 uint32_t psf_flags;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400370};
371
372static void
373weston_presentation_feedback_discard(
374 struct weston_presentation_feedback *feedback)
375{
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200376 wp_presentation_feedback_send_discarded(feedback->resource);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400377 wl_resource_destroy(feedback->resource);
378}
379
380static void
381weston_presentation_feedback_discard_list(struct wl_list *list)
382{
383 struct weston_presentation_feedback *feedback, *tmp;
384
385 wl_list_for_each_safe(feedback, tmp, list, link)
386 weston_presentation_feedback_discard(feedback);
387}
388
389static void
390weston_presentation_feedback_present(
391 struct weston_presentation_feedback *feedback,
392 struct weston_output *output,
393 uint32_t refresh_nsec,
394 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200395 uint64_t seq,
396 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400397{
398 struct wl_client *client = wl_resource_get_client(feedback->resource);
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300399 struct weston_head *head;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400400 struct wl_resource *o;
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200401 uint32_t tv_sec_hi;
402 uint32_t tv_sec_lo;
403 uint32_t tv_nsec;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300404 bool done = false;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400405
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300406 wl_list_for_each(head, &output->head_list, output_link) {
407 wl_resource_for_each(o, &head->resource_list) {
408 if (wl_resource_get_client(o) != client)
409 continue;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400410
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300411 wp_presentation_feedback_send_sync_output(feedback->resource, o);
412 done = true;
413 }
414
415 /* For clone mode, send it for just one wl_output global,
416 * they are all equivalent anyway.
417 */
418 if (done)
419 break;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400420 }
421
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200422 timespec_to_proto(ts, &tv_sec_hi, &tv_sec_lo, &tv_nsec);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200423 wp_presentation_feedback_send_presented(feedback->resource,
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200424 tv_sec_hi, tv_sec_lo, tv_nsec,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200425 refresh_nsec,
426 seq >> 32, seq & 0xffffffff,
427 flags | feedback->psf_flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400428 wl_resource_destroy(feedback->resource);
429}
430
431static void
432weston_presentation_feedback_present_list(struct wl_list *list,
433 struct weston_output *output,
434 uint32_t refresh_nsec,
435 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200436 uint64_t seq,
437 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400438{
439 struct weston_presentation_feedback *feedback, *tmp;
440
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200441 assert(!(flags & WP_PRESENTATION_FEEDBACK_INVALID) ||
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200442 wl_list_empty(list));
443
Pekka Paalanen133e4392014-09-23 22:08:46 -0400444 wl_list_for_each_safe(feedback, tmp, list, link)
445 weston_presentation_feedback_present(feedback, output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200446 refresh_nsec, ts, seq,
447 flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400448}
449
Jason Ekstrand7b982072014-05-20 14:33:03 -0500450static void
451surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
452{
453 struct weston_surface_state *state =
454 container_of(listener, struct weston_surface_state,
455 buffer_destroy_listener);
456
457 state->buffer = NULL;
458}
459
460static void
461weston_surface_state_init(struct weston_surface_state *state)
462{
463 state->newly_attached = 0;
464 state->buffer = NULL;
465 state->buffer_destroy_listener.notify =
466 surface_state_handle_buffer_destroy;
467 state->sx = 0;
468 state->sy = 0;
469
Derek Foreman152254b2015-11-26 14:17:48 -0600470 pixman_region32_init(&state->damage_surface);
471 pixman_region32_init(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500472 pixman_region32_init(&state->opaque);
473 region_init_infinite(&state->input);
474
475 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400476 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500477
478 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
479 state->buffer_viewport.buffer.scale = 1;
480 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
481 state->buffer_viewport.surface.width = -1;
482 state->buffer_viewport.changed = 0;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300483
484 state->acquire_fence_fd = -1;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +0530485
486 state->desired_protection = WESTON_HDCP_DISABLE;
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +0530487 state->protection_mode = WESTON_SURFACE_PROTECTION_MODE_RELAXED;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500488}
489
490static void
491weston_surface_state_fini(struct weston_surface_state *state)
492{
493 struct weston_frame_callback *cb, *next;
494
495 wl_list_for_each_safe(cb, next,
496 &state->frame_callback_list, link)
497 wl_resource_destroy(cb->resource);
498
Pekka Paalanen133e4392014-09-23 22:08:46 -0400499 weston_presentation_feedback_discard_list(&state->feedback_list);
500
Jason Ekstrand7b982072014-05-20 14:33:03 -0500501 pixman_region32_fini(&state->input);
502 pixman_region32_fini(&state->opaque);
Derek Foreman152254b2015-11-26 14:17:48 -0600503 pixman_region32_fini(&state->damage_surface);
504 pixman_region32_fini(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500505
506 if (state->buffer)
507 wl_list_remove(&state->buffer_destroy_listener.link);
508 state->buffer = NULL;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300509
510 fd_clear(&state->acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +0300511 weston_buffer_release_reference(&state->buffer_release_ref, NULL);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500512}
513
514static void
515weston_surface_state_set_buffer(struct weston_surface_state *state,
516 struct weston_buffer *buffer)
517{
518 if (state->buffer == buffer)
519 return;
520
521 if (state->buffer)
522 wl_list_remove(&state->buffer_destroy_listener.link);
523 state->buffer = buffer;
524 if (state->buffer)
525 wl_signal_add(&state->buffer->destroy_signal,
526 &state->buffer_destroy_listener);
527}
528
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500529WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500530weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500531{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500532 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400533
Bryce Harringtonde16d892014-11-20 22:21:57 -0800534 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400535 if (surface == NULL)
536 return NULL;
537
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500538 wl_signal_init(&surface->destroy_signal);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +0800539 wl_signal_init(&surface->commit_signal);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500540
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500541 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200542 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400543
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200544 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
545 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200546 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
547 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500548
549 weston_surface_state_init(&surface->pending);
550
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400551 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500552 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300553 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400554
Jason Ekstranda7af7042013-10-12 22:38:11 -0500555 wl_list_init(&surface->views);
556
557 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400558 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500559
Pekka Paalanene67858b2013-04-25 13:57:42 +0300560 wl_list_init(&surface->subsurface_list);
561 wl_list_init(&surface->subsurface_list_pending);
562
Jason Ekstrand1e059042014-10-16 10:55:19 -0500563 weston_matrix_init(&surface->buffer_to_surface_matrix);
564 weston_matrix_init(&surface->surface_to_buffer_matrix);
565
Jonas Ådahld3414f22016-07-22 17:56:31 +0800566 wl_list_init(&surface->pointer_constraints);
567
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300568 surface->acquire_fence_fd = -1;
569
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +0530570 surface->desired_protection = WESTON_HDCP_DISABLE;
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +0530571 surface->current_protection = WESTON_HDCP_DISABLE;
572 surface->protection_mode = WESTON_SURFACE_PROTECTION_MODE_RELAXED;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +0530573
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400574 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500575}
576
Alex Wu8811bf92012-02-28 18:07:54 +0800577WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500578weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200579 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500580{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100581 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Philipp Zabel195dade2018-09-03 19:44:59 +0200582 surface->is_opaque = !(alpha < 1.0);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500583}
584
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400585WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500586weston_view_to_global_float(struct weston_view *view,
587 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200588{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500589 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200590 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
591
Jason Ekstranda7af7042013-10-12 22:38:11 -0500592 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200593
594 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200595 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700596 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200597 v.f[3]);
598 *x = 0;
599 *y = 0;
600 return;
601 }
602
603 *x = v.f[0] / v.f[3];
604 *y = v.f[1] / v.f[3];
605 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500606 *x = sx + view->geometry.x;
607 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200608 }
609}
610
Pekka Paalanen0df44772020-02-14 14:50:03 +0200611/** Transform a point to buffer coordinates
612 *
613 * \param width Surface width.
614 * \param height Surface height.
615 * \param transform Buffer transform.
616 * \param scale Buffer scale.
617 * \param sx Surface x coordinate of a point.
618 * \param sy Surface y coordinate of a point.
619 * \param[out] bx Buffer x coordinate of the point.
620 * \param[out] by Buffer Y coordinate of the point.
621 *
622 * Converts the given surface-local coordinates to buffer coordinates
623 * according to the given buffer transform and scale.
624 * This ignores wp_viewport.
625 *
626 * The given width and height must be the result of inverse scaled and
627 * inverse transformed buffer size.
628 */
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500629WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200630weston_transformed_coord(int width, int height,
631 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200632 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200633 float sx, float sy, float *bx, float *by)
634{
635 switch (transform) {
636 case WL_OUTPUT_TRANSFORM_NORMAL:
637 default:
638 *bx = sx;
639 *by = sy;
640 break;
641 case WL_OUTPUT_TRANSFORM_FLIPPED:
642 *bx = width - sx;
643 *by = sy;
644 break;
645 case WL_OUTPUT_TRANSFORM_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200646 *bx = sy;
647 *by = width - sx;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200648 break;
649 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200650 *bx = sy;
651 *by = sx;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200652 break;
653 case WL_OUTPUT_TRANSFORM_180:
654 *bx = width - sx;
655 *by = height - sy;
656 break;
657 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
658 *bx = sx;
659 *by = height - sy;
660 break;
661 case WL_OUTPUT_TRANSFORM_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200662 *bx = height - sy;
663 *by = sx;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200664 break;
665 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200666 *bx = height - sy;
667 *by = width - sx;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200668 break;
669 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200670
671 *bx *= scale;
672 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200673}
674
Pekka Paalanen0df44772020-02-14 14:50:03 +0200675/** Transform a rectangle to buffer coordinates
676 *
677 * \param width Surface width.
678 * \param height Surface height.
679 * \param transform Buffer transform.
680 * \param scale Buffer scale.
681 * \param rect Rectangle in surface coordinates.
682 * \return Rectangle in buffer coordinates.
683 *
684 * Converts the given surface-local rectangle to buffer coordinates
685 * according to the given buffer transform and scale. The resulting
686 * rectangle is guaranteed to be well-formed.
687 * This ignores wp_viewport.
688 *
689 * The given width and height must be the result of inverse scaled and
690 * inverse transformed buffer size.
691 */
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200692WL_EXPORT pixman_box32_t
693weston_transformed_rect(int width, int height,
694 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200695 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200696 pixman_box32_t rect)
697{
698 float x1, x2, y1, y2;
699
700 pixman_box32_t ret;
701
Alexander Larsson4ea95522013-05-22 14:41:37 +0200702 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200703 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200704 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200705 rect.x2, rect.y2, &x2, &y2);
706
707 if (x1 <= x2) {
708 ret.x1 = x1;
709 ret.x2 = x2;
710 } else {
711 ret.x1 = x2;
712 ret.x2 = x1;
713 }
714
715 if (y1 <= y2) {
716 ret.y1 = y1;
717 ret.y2 = y2;
718 } else {
719 ret.y1 = y2;
720 ret.y2 = y1;
721 }
722
723 return ret;
724}
725
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600726/** Transform a region by a matrix, restricted to axis-aligned transformations
727 *
728 * Warning: This function does not work for projective, affine, or matrices
729 * that encode arbitrary rotations. Only 90-degree step rotations are
730 * supported.
731 */
732WL_EXPORT void
733weston_matrix_transform_region(pixman_region32_t *dest,
734 struct weston_matrix *matrix,
735 pixman_region32_t *src)
736{
737 pixman_box32_t *src_rects, *dest_rects;
738 int nrects, i;
739
740 src_rects = pixman_region32_rectangles(src, &nrects);
741 dest_rects = malloc(nrects * sizeof(*dest_rects));
742 if (!dest_rects)
743 return;
744
745 for (i = 0; i < nrects; i++) {
746 struct weston_vector vec1 = {{
747 src_rects[i].x1, src_rects[i].y1, 0, 1
748 }};
749 weston_matrix_transform(matrix, &vec1);
750 vec1.f[0] /= vec1.f[3];
751 vec1.f[1] /= vec1.f[3];
752
753 struct weston_vector vec2 = {{
754 src_rects[i].x2, src_rects[i].y2, 0, 1
755 }};
756 weston_matrix_transform(matrix, &vec2);
757 vec2.f[0] /= vec2.f[3];
758 vec2.f[1] /= vec2.f[3];
759
760 if (vec1.f[0] < vec2.f[0]) {
761 dest_rects[i].x1 = floor(vec1.f[0]);
762 dest_rects[i].x2 = ceil(vec2.f[0]);
763 } else {
764 dest_rects[i].x1 = floor(vec2.f[0]);
765 dest_rects[i].x2 = ceil(vec1.f[0]);
766 }
767
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600768 if (vec1.f[1] < vec2.f[1]) {
769 dest_rects[i].y1 = floor(vec1.f[1]);
770 dest_rects[i].y2 = ceil(vec2.f[1]);
771 } else {
772 dest_rects[i].y1 = floor(vec2.f[1]);
773 dest_rects[i].y2 = ceil(vec1.f[1]);
774 }
775 }
776
777 pixman_region32_clear(dest);
778 pixman_region32_init_rects(dest, dest_rects, nrects);
779 free(dest_rects);
780}
781
Pekka Paalanen0df44772020-02-14 14:50:03 +0200782/** Transform a region to buffer coordinates
783 *
784 * \param width Surface width.
785 * \param height Surface height.
786 * \param transform Buffer transform.
787 * \param scale Buffer scale.
788 * \param[in] src Region in surface coordinates.
789 * \param[out] dest Resulting region in buffer coordinates.
790 *
791 * Converts the given surface-local region to buffer coordinates
792 * according to the given buffer transform and scale.
793 * This ignores wp_viewport.
794 *
795 * The given width and height must be the result of inverse scaled and
796 * inverse transformed buffer size.
797 *
798 * src and dest are allowed to point to the same memory for in-place conversion.
799 */
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200800WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500801weston_transformed_region(int width, int height,
802 enum wl_output_transform transform,
803 int32_t scale,
804 pixman_region32_t *src, pixman_region32_t *dest)
805{
806 pixman_box32_t *src_rects, *dest_rects;
807 int nrects, i;
808
809 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
810 if (src != dest)
811 pixman_region32_copy(dest, src);
812 return;
813 }
814
815 src_rects = pixman_region32_rectangles(src, &nrects);
816 dest_rects = malloc(nrects * sizeof(*dest_rects));
817 if (!dest_rects)
818 return;
819
820 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
821 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
822 } else {
823 for (i = 0; i < nrects; i++) {
824 switch (transform) {
825 default:
826 case WL_OUTPUT_TRANSFORM_NORMAL:
827 dest_rects[i].x1 = src_rects[i].x1;
828 dest_rects[i].y1 = src_rects[i].y1;
829 dest_rects[i].x2 = src_rects[i].x2;
830 dest_rects[i].y2 = src_rects[i].y2;
831 break;
832 case WL_OUTPUT_TRANSFORM_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200833 dest_rects[i].x1 = src_rects[i].y1;
834 dest_rects[i].y1 = width - src_rects[i].x2;
835 dest_rects[i].x2 = src_rects[i].y2;
836 dest_rects[i].y2 = width - src_rects[i].x1;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500837 break;
838 case WL_OUTPUT_TRANSFORM_180:
839 dest_rects[i].x1 = width - src_rects[i].x2;
840 dest_rects[i].y1 = height - src_rects[i].y2;
841 dest_rects[i].x2 = width - src_rects[i].x1;
842 dest_rects[i].y2 = height - src_rects[i].y1;
843 break;
844 case WL_OUTPUT_TRANSFORM_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200845 dest_rects[i].x1 = height - src_rects[i].y2;
846 dest_rects[i].y1 = src_rects[i].x1;
847 dest_rects[i].x2 = height - src_rects[i].y1;
848 dest_rects[i].y2 = src_rects[i].x2;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500849 break;
850 case WL_OUTPUT_TRANSFORM_FLIPPED:
851 dest_rects[i].x1 = width - src_rects[i].x2;
852 dest_rects[i].y1 = src_rects[i].y1;
853 dest_rects[i].x2 = width - src_rects[i].x1;
854 dest_rects[i].y2 = src_rects[i].y2;
855 break;
856 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200857 dest_rects[i].x1 = src_rects[i].y1;
858 dest_rects[i].y1 = src_rects[i].x1;
859 dest_rects[i].x2 = src_rects[i].y2;
860 dest_rects[i].y2 = src_rects[i].x2;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500861 break;
862 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
863 dest_rects[i].x1 = src_rects[i].x1;
864 dest_rects[i].y1 = height - src_rects[i].y2;
865 dest_rects[i].x2 = src_rects[i].x2;
866 dest_rects[i].y2 = height - src_rects[i].y1;
867 break;
868 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +0200869 dest_rects[i].x1 = height - src_rects[i].y2;
870 dest_rects[i].y1 = width - src_rects[i].x2;
871 dest_rects[i].x2 = height - src_rects[i].y1;
872 dest_rects[i].y2 = width - src_rects[i].x1;
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500873 break;
874 }
875 }
876 }
877
878 if (scale != 1) {
879 for (i = 0; i < nrects; i++) {
880 dest_rects[i].x1 *= scale;
881 dest_rects[i].x2 *= scale;
882 dest_rects[i].y1 *= scale;
883 dest_rects[i].y2 *= scale;
884 }
885 }
886
887 pixman_region32_clear(dest);
888 pixman_region32_init_rects(dest, dest_rects, nrects);
889 free(dest_rects);
890}
891
Jonny Lamb74130762013-11-26 18:19:46 +0100892static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300893viewport_surface_to_buffer(struct weston_surface *surface,
894 float sx, float sy, float *bx, float *by)
Jonny Lamb74130762013-11-26 18:19:46 +0100895{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200896 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200897 double src_width, src_height;
898 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200899
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200900 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
901 if (vp->surface.width == -1) {
902 *bx = sx;
903 *by = sy;
904 return;
905 }
Jonny Lamb74130762013-11-26 18:19:46 +0100906
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200907 src_x = 0.0;
908 src_y = 0.0;
909 src_width = surface->width_from_buffer;
910 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100911 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200912 src_x = wl_fixed_to_double(vp->buffer.src_x);
913 src_y = wl_fixed_to_double(vp->buffer.src_y);
914 src_width = wl_fixed_to_double(vp->buffer.src_width);
915 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100916 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200917
918 *bx = sx * src_width / surface->width + src_x;
919 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100920}
921
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500922WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200923weston_surface_to_buffer_float(struct weston_surface *surface,
924 float sx, float sy, float *bx, float *by)
925{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200926 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
927
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300928 /* first transform coordinates if the viewport is set */
929 viewport_surface_to_buffer(surface, sx, sy, bx, by);
Jonny Lamb74130762013-11-26 18:19:46 +0100930
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500931 weston_transformed_coord(surface->width_from_buffer,
932 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200933 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100934 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200935}
936
Derek Foreman99739672015-12-03 16:38:11 -0600937/** Transform a rectangle from surface coordinates to buffer coordinates
938 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300939 * \param surface The surface to fetch wp_viewport and buffer transformation
Derek Foreman99739672015-12-03 16:38:11 -0600940 * from.
941 * \param rect The rectangle to transform.
942 * \return The transformed rectangle.
943 *
944 * Viewport and buffer transformations can only do translation, scaling,
945 * and rotations in 90-degree steps. Therefore the only loss in the
946 * conversion is coordinate rounding.
947 *
948 * However, some coordinate rounding takes place as an intermediate
949 * step before the buffer scale factor is applied, so the rectangle
950 * boundary may not be exactly as expected.
951 *
952 * This is OK for damage tracking since a little extra coverage is
953 * not a problem.
954 */
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200955WL_EXPORT pixman_box32_t
956weston_surface_to_buffer_rect(struct weston_surface *surface,
957 pixman_box32_t rect)
958{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200959 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100960 float xf, yf;
961
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300962 /* first transform box coordinates if the viewport is set */
963 viewport_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100964 rect.x1 = floorf(xf);
965 rect.y1 = floorf(yf);
966
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300967 viewport_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
Derek Foremane2e15ac2015-12-01 13:00:43 -0600968 rect.x2 = ceilf(xf);
969 rect.y2 = ceilf(yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100970
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500971 return weston_transformed_rect(surface->width_from_buffer,
972 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200973 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200974 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200975}
976
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200977/** Transform a region from surface coordinates to buffer coordinates
978 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300979 * \param surface The surface to fetch wp_viewport and buffer transformation
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200980 * from.
Marius Vlada2dace22019-06-12 16:05:44 +0300981 * \param[in] surface_region The region in surface coordinates.
982 * \param[out] buffer_region The region converted to buffer coordinates.
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200983 *
984 * Buffer_region must be init'd, but will be completely overwritten.
985 *
986 * Viewport and buffer transformations can only do translation, scaling,
987 * and rotations in 90-degree steps. Therefore the only loss in the
Derek Foreman99739672015-12-03 16:38:11 -0600988 * conversion is from the coordinate rounding that takes place in
989 * \ref weston_surface_to_buffer_rect.
Marius Vlada2dace22019-06-12 16:05:44 +0300990 *
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200991 */
992WL_EXPORT void
993weston_surface_to_buffer_region(struct weston_surface *surface,
994 pixman_region32_t *surface_region,
995 pixman_region32_t *buffer_region)
996{
997 pixman_box32_t *src_rects, *dest_rects;
998 int nrects, i;
999
1000 src_rects = pixman_region32_rectangles(surface_region, &nrects);
1001 dest_rects = malloc(nrects * sizeof(*dest_rects));
1002 if (!dest_rects)
1003 return;
1004
1005 for (i = 0; i < nrects; i++) {
1006 dest_rects[i] = weston_surface_to_buffer_rect(surface,
1007 src_rects[i]);
1008 }
1009
1010 pixman_region32_fini(buffer_region);
1011 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
1012 free(dest_rects);
1013}
1014
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +02001015WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001016weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001017 struct weston_plane *plane)
1018{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001019 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001020 return;
1021
Jason Ekstranda7af7042013-10-12 22:38:11 -05001022 weston_view_damage_below(view);
1023 view->plane = plane;
1024 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001025}
1026
Pekka Paalanen51723d52015-02-17 13:10:01 +02001027/** Inflict damage on the plane where the view is visible.
1028 *
1029 * \param view The view that causes the damage.
1030 *
1031 * If the view is currently on a plane (including the primary plane),
1032 * take the view's boundingbox, subtract all the opaque views that cover it,
1033 * and add the remaining region as damage to the plane. This corresponds
1034 * to the damage inflicted to the plane if this view disappeared.
1035 *
1036 * A repaint is scheduled for this view.
1037 *
1038 * The region of all opaque views covering this view is stored in
1039 * weston_view::clip and updated by view_accumulate_damage() during
1040 * weston_output_repaint(). Specifically, that region matches the
1041 * scenegraph as it was last painted.
1042 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001043WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001044weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001045{
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001046 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001047
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001048 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001049 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001050 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +08001051 if (view->plane)
1052 pixman_region32_union(&view->plane->damage,
1053 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001054 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -08001055 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001056}
1057
Pekka Paalanen01e00682017-03-24 16:21:06 +02001058/** Send wl_surface.enter/leave events
1059 *
1060 * \param surface The surface.
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03001061 * \param head A head of the entered/left output.
Pekka Paalanen01e00682017-03-24 16:21:06 +02001062 * \param enter True if entered.
Marius Vlada2dace22019-06-12 16:05:44 +03001063 * \param leave True if left.
Pekka Paalanen01e00682017-03-24 16:21:06 +02001064 *
1065 * Send the enter/leave events for all protocol objects bound to the given
1066 * output by the client owning the surface.
1067 */
1068static void
1069weston_surface_send_enter_leave(struct weston_surface *surface,
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03001070 struct weston_head *head,
Pekka Paalanen01e00682017-03-24 16:21:06 +02001071 bool enter,
1072 bool leave)
1073{
1074 struct wl_resource *wloutput;
1075 struct wl_client *client;
1076
1077 assert(enter != leave);
1078
1079 client = wl_resource_get_client(surface->resource);
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03001080 wl_resource_for_each(wloutput, &head->resource_list) {
Pekka Paalanen01e00682017-03-24 16:21:06 +02001081 if (wl_resource_get_client(wloutput) != client)
1082 continue;
1083
1084 if (enter)
1085 wl_surface_send_enter(surface->resource, wloutput);
1086 if (leave)
1087 wl_surface_send_leave(surface->resource, wloutput);
1088 }
1089}
1090
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301091static void
1092weston_surface_compute_protection(struct protected_surface *psurface)
1093{
1094 enum weston_hdcp_protection min_protection;
1095 bool min_protection_valid = false;
1096 struct weston_surface *surface = psurface->surface;
1097 struct weston_output *output;
1098
1099 wl_list_for_each(output, &surface->compositor->output_list, link)
1100 if (surface->output_mask & (1u << output->id)) {
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05301101 /*
1102 * If the content-protection is enabled with protection
1103 * mode as RELAXED for a surface, and if
1104 * content-recording features like: screen-shooter,
1105 * recorder, screen-sharing, etc are on, then notify the
1106 * client, that the protection is disabled.
1107 *
1108 * Note: If the protection mode is ENFORCED then there
1109 * is no need to bother the client as the renderer takes
1110 * care of censoring the visibility of the protected
1111 * content.
1112 */
1113
1114 if (output->disable_planes > 0 &&
1115 surface->protection_mode == WESTON_SURFACE_PROTECTION_MODE_RELAXED) {
1116 min_protection = WESTON_HDCP_DISABLE;
1117 min_protection_valid = true;
1118 break;
1119 }
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301120 if (!min_protection_valid) {
1121 min_protection = output->current_protection;
1122 min_protection_valid = true;
1123 }
1124 if (output->current_protection < min_protection)
1125 min_protection = output->current_protection;
1126 }
1127 if (!min_protection_valid)
1128 min_protection = WESTON_HDCP_DISABLE;
1129
1130 surface->current_protection = min_protection;
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05301131
1132 weston_protected_surface_send_event(psurface, surface->current_protection);
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301133}
1134
1135static void
1136notify_surface_protection_change(void *data)
1137{
1138 struct weston_compositor *compositor = data;
1139 struct content_protection *cp;
1140 struct protected_surface *psurface;
1141
1142 cp = compositor->content_protection;
1143 cp->surface_protection_update = NULL;
1144
1145 /* Notify the clients, whose surfaces are changed */
1146 wl_list_for_each(psurface, &cp->protected_list, link)
1147 if (psurface && psurface->surface)
1148 weston_surface_compute_protection(psurface);
1149}
1150
Bryce Harrington3f650b82015-12-23 11:01:58 -08001151/**
Ankit Nautiyalfaa5ab42019-07-08 13:23:24 +05301152 * \param compositor weston_compositor
1153 *
1154 * Schedule an idle task to notify surface about the update in protection,
1155 * if not already scheduled.
1156 */
1157static void
1158weston_schedule_surface_protection_update(struct weston_compositor *compositor)
1159{
1160 struct content_protection *cp = compositor->content_protection;
1161 struct wl_event_loop *loop;
1162
1163 if (!cp || cp->surface_protection_update)
1164 return;
1165 loop = wl_display_get_event_loop(compositor->wl_display);
1166 cp->surface_protection_update = wl_event_loop_add_idle(loop,
1167 notify_surface_protection_change,
1168 compositor);
1169}
1170
1171/**
Bryce Harrington3f650b82015-12-23 11:01:58 -08001172 * \param es The surface
1173 * \param mask The new set of outputs for the surface
1174 *
1175 * Sets the surface's set of outputs to the ones specified by
1176 * the new output mask provided. Identifies the outputs that
1177 * have changed, the posts enter and leave events for these
1178 * outputs as appropriate.
1179 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001180static void
1181weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
1182{
1183 uint32_t different = es->output_mask ^ mask;
1184 uint32_t entered = mask & different;
1185 uint32_t left = es->output_mask & different;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001186 uint32_t output_bit;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001187 struct weston_output *output;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001188 struct weston_head *head;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001189
1190 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001191 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001192 return;
1193 if (different == 0)
1194 return;
1195
1196 wl_list_for_each(output, &es->compositor->output_list, link) {
Pekka Paalanen01e00682017-03-24 16:21:06 +02001197 output_bit = 1u << output->id;
1198 if (!(output_bit & different))
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001199 continue;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001200
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001201 wl_list_for_each(head, &output->head_list, output_link) {
1202 weston_surface_send_enter_leave(es, head,
1203 output_bit & entered,
1204 output_bit & left);
1205 }
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001206 }
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301207 /*
1208 * Change in surfaces' output mask might trigger a change in its
1209 * protection.
1210 */
Ankit Nautiyalfaa5ab42019-07-08 13:23:24 +05301211 weston_schedule_surface_protection_update(es->compositor);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001212}
1213
Semi Malinene7a52fb2018-04-26 11:08:10 +02001214static void
1215notify_view_output_destroy(struct wl_listener *listener, void *data)
1216{
1217 struct weston_view *view =
1218 container_of(listener,
1219 struct weston_view, output_destroy_listener);
1220
1221 view->output = NULL;
1222 view->output_destroy_listener.notify = NULL;
1223}
1224
1225/** Set the primary output of the view
1226 *
1227 * \param view The view whose primary output to set
1228 * \param output The new primary output for the view
1229 *
1230 * Set \a output to be the primary output of the \a view.
1231 *
1232 * Notice that the assignment may be temporary; the primary output could be
1233 * automatically changed. Hence, one cannot rely on the value persisting.
1234 *
1235 * Passing NULL as /a output will set the primary output to NULL.
1236 */
1237WL_EXPORT void
1238weston_view_set_output(struct weston_view *view, struct weston_output *output)
1239{
1240 if (view->output_destroy_listener.notify) {
1241 wl_list_remove(&view->output_destroy_listener.link);
1242 view->output_destroy_listener.notify = NULL;
1243 }
1244 view->output = output;
1245 if (output) {
1246 view->output_destroy_listener.notify =
1247 notify_view_output_destroy;
1248 wl_signal_add(&output->destroy_signal,
1249 &view->output_destroy_listener);
1250 }
1251}
1252
Bryce Harrington3f650b82015-12-23 11:01:58 -08001253/** Recalculate which output(s) the surface has views displayed on
1254 *
1255 * \param es The surface to remap to outputs
1256 *
1257 * Finds the output that is showing the largest amount of one
1258 * of the surface's various views. This output becomes the
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001259 * surface's primary output for vsync and frame callback purposes.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001260 *
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001261 * Also notes all outputs of all of the surface's views
Bryce Harrington3f650b82015-12-23 11:01:58 -08001262 * in the output_mask for the surface.
1263 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001264static void
1265weston_surface_assign_output(struct weston_surface *es)
1266{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001267 struct weston_output *new_output;
1268 struct weston_view *view;
1269 pixman_region32_t region;
1270 uint32_t max, area, mask;
1271 pixman_box32_t *e;
1272
1273 new_output = NULL;
1274 max = 0;
1275 mask = 0;
1276 pixman_region32_init(&region);
1277 wl_list_for_each(view, &es->views, surface_link) {
1278 if (!view->output)
1279 continue;
1280
1281 pixman_region32_intersect(&region, &view->transform.boundingbox,
1282 &view->output->region);
1283
1284 e = pixman_region32_extents(&region);
1285 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1286
1287 mask |= view->output_mask;
1288
1289 if (area >= max) {
1290 new_output = view->output;
1291 max = area;
1292 }
1293 }
1294 pixman_region32_fini(&region);
1295
1296 es->output = new_output;
1297 weston_surface_update_output_mask(es, mask);
1298}
1299
Bryce Harrington3f650b82015-12-23 11:01:58 -08001300/** Recalculate which output(s) the view is displayed on
1301 *
1302 * \param ev The view to remap to outputs
1303 *
1304 * Identifies the set of outputs that the view is visible on,
1305 * noting them into the output_mask. The output that the view
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001306 * is most visible on is set as the view's primary output.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001307 *
1308 * Also does the same for the view's surface. See
1309 * weston_surface_assign_output().
1310 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001311static void
1312weston_view_assign_output(struct weston_view *ev)
1313{
1314 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001315 struct weston_output *output, *new_output;
1316 pixman_region32_t region;
1317 uint32_t max, area, mask;
1318 pixman_box32_t *e;
1319
1320 new_output = NULL;
1321 max = 0;
1322 mask = 0;
1323 pixman_region32_init(&region);
1324 wl_list_for_each(output, &ec->output_list, link) {
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03001325 if (output->destroying)
1326 continue;
1327
Jason Ekstranda7af7042013-10-12 22:38:11 -05001328 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001329 &output->region);
1330
1331 e = pixman_region32_extents(&region);
1332 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1333
1334 if (area > 0)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001335 mask |= 1u << output->id;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001336
1337 if (area >= max) {
1338 new_output = output;
1339 max = area;
1340 }
1341 }
1342 pixman_region32_fini(&region);
1343
Semi Malinene7a52fb2018-04-26 11:08:10 +02001344 weston_view_set_output(ev, new_output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001345 ev->output_mask = mask;
1346
1347 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001348}
1349
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001350static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001351weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1352 int from_x, int from_y, int *to_x, int *to_y)
1353{
1354 float x, y;
1355
1356 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1357 weston_view_from_global_float(to, x, y, &x, &y);
1358
1359 *to_x = round(x);
1360 *to_y = round(y);
1361}
1362
1363static void
1364weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1365{
1366 pixman_box32_t *a;
1367 pixman_box32_t b;
1368
1369 a = pixman_region32_extents(&from->geometry.scissor);
1370
1371 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1372 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1373
1374 pixman_region32_fini(&to->geometry.scissor);
1375 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1376}
1377
1378static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001379view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001380 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001381{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001382 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1383 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001384 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001385 { inbox->x1, inbox->y1 },
1386 { inbox->x1, inbox->y2 },
1387 { inbox->x2, inbox->y1 },
1388 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001389 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001390 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001391 int i;
1392
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001393 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001394 /* avoid rounding empty bbox to 1x1 */
1395 pixman_region32_init(bbox);
1396 return;
1397 }
1398
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001399 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001400 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001401 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001402 if (x < min_x)
1403 min_x = x;
1404 if (x > max_x)
1405 max_x = x;
1406 if (y < min_y)
1407 min_y = y;
1408 if (y > max_y)
1409 max_y = y;
1410 }
1411
Pekka Paalanen219b9822012-02-08 15:38:37 +02001412 int_x = floorf(min_x);
1413 int_y = floorf(min_y);
1414 pixman_region32_init_rect(bbox, int_x, int_y,
1415 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001416}
1417
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001418static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001419weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001420{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001421 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001422
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001423 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001424 view->geometry.x = roundf(view->geometry.x);
1425 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001426
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001427 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001428 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1429 view->transform.position.matrix.d[12] = view->geometry.x;
1430 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001431
Jason Ekstranda7af7042013-10-12 22:38:11 -05001432 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001433
Jason Ekstranda7af7042013-10-12 22:38:11 -05001434 view->transform.inverse = view->transform.position.matrix;
1435 view->transform.inverse.d[12] = -view->geometry.x;
1436 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001437
Jason Ekstranda7af7042013-10-12 22:38:11 -05001438 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001439 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001440 view->surface->width,
1441 view->surface->height);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001442 if (view->geometry.scissor_enabled)
1443 pixman_region32_intersect(&view->transform.boundingbox,
1444 &view->transform.boundingbox,
1445 &view->geometry.scissor);
1446
1447 pixman_region32_translate(&view->transform.boundingbox,
1448 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001449
Jason Ekstranda7af7042013-10-12 22:38:11 -05001450 if (view->alpha == 1.0) {
1451 pixman_region32_copy(&view->transform.opaque,
1452 &view->surface->opaque);
1453 pixman_region32_translate(&view->transform.opaque,
1454 view->geometry.x,
1455 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001456 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001457}
1458
1459static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001460weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001461{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001462 struct weston_view *parent = view->geometry.parent;
1463 struct weston_matrix *matrix = &view->transform.matrix;
1464 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001465 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001466 pixman_region32_t surfregion;
1467 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001468
Jason Ekstranda7af7042013-10-12 22:38:11 -05001469 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001470
1471 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001472 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1473 view->transform.position.matrix.d[12] = view->geometry.x;
1474 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001475
1476 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001477 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001478 weston_matrix_multiply(matrix, &tform->matrix);
1479
Pekka Paalanen483243f2013-03-08 14:56:50 +02001480 if (parent)
1481 weston_matrix_multiply(matrix, &parent->transform.matrix);
1482
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001483 if (weston_matrix_invert(inverse, matrix) < 0) {
1484 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001485 weston_log("error: weston_view %p"
1486 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001487 return -1;
1488 }
1489
Giulio Camuffo148c1992016-09-04 18:50:46 +03001490 if (view->alpha == 1.0 &&
1491 matrix->type == WESTON_MATRIX_TRANSFORM_TRANSLATE) {
1492 pixman_region32_copy(&view->transform.opaque,
1493 &view->surface->opaque);
1494 pixman_region32_translate(&view->transform.opaque,
1495 matrix->d[12],
1496 matrix->d[13]);
1497 }
1498
Pekka Paalanen380adf52015-02-16 14:39:11 +02001499 pixman_region32_init_rect(&surfregion, 0, 0,
1500 view->surface->width, view->surface->height);
1501 if (view->geometry.scissor_enabled)
1502 pixman_region32_intersect(&surfregion, &surfregion,
1503 &view->geometry.scissor);
1504 surfbox = pixman_region32_extents(&surfregion);
1505
1506 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1507 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001508
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001509 return 0;
1510}
1511
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001512static struct weston_layer *
1513get_view_layer(struct weston_view *view)
1514{
1515 if (view->parent_view)
1516 return get_view_layer(view->parent_view);
1517 return view->layer_link.layer;
1518}
1519
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001520WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001521weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001522{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001523 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001524 struct weston_layer *layer;
1525 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001526
Jason Ekstranda7af7042013-10-12 22:38:11 -05001527 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001528 return;
1529
Pekka Paalanen483243f2013-03-08 14:56:50 +02001530 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001531 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001532
Jason Ekstranda7af7042013-10-12 22:38:11 -05001533 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001534
Jason Ekstranda7af7042013-10-12 22:38:11 -05001535 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001536
Jason Ekstranda7af7042013-10-12 22:38:11 -05001537 pixman_region32_fini(&view->transform.boundingbox);
1538 pixman_region32_fini(&view->transform.opaque);
1539 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001540
Pekka Paalanencd403622012-01-25 13:37:39 +02001541 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001542 if (view->geometry.transformation_list.next ==
1543 &view->transform.position.link &&
1544 view->geometry.transformation_list.prev ==
1545 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001546 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001547 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001548 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001549 if (weston_view_update_transform_enable(view) < 0)
1550 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001551 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001552
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001553 layer = get_view_layer(view);
1554 if (layer) {
1555 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001556 pixman_region32_intersect(&view->transform.boundingbox,
1557 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001558 pixman_region32_intersect(&view->transform.opaque,
1559 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001560 pixman_region32_fini(&mask);
1561 }
1562
Pekka Paalanen380adf52015-02-16 14:39:11 +02001563 if (parent) {
1564 if (parent->geometry.scissor_enabled) {
1565 view->geometry.scissor_enabled = true;
1566 weston_view_transfer_scissor(parent, view);
1567 } else {
1568 view->geometry.scissor_enabled = false;
1569 }
1570 }
1571
Jason Ekstranda7af7042013-10-12 22:38:11 -05001572 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001573
Jason Ekstranda7af7042013-10-12 22:38:11 -05001574 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001575
Jason Ekstranda7af7042013-10-12 22:38:11 -05001576 wl_signal_emit(&view->surface->compositor->transform_signal,
1577 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001578}
1579
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001580WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001581weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001582{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001583 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001584
1585 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001586 * The invariant: if view->geometry.dirty, then all views
1587 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001588 * Corollary: if not parent->geometry.dirty, then all ancestors
1589 * are not dirty.
1590 */
1591
Jason Ekstranda7af7042013-10-12 22:38:11 -05001592 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001593 return;
1594
Jason Ekstranda7af7042013-10-12 22:38:11 -05001595 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001596
Jason Ekstranda7af7042013-10-12 22:38:11 -05001597 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001598 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001599 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001600}
1601
1602WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001603weston_view_to_global_fixed(struct weston_view *view,
1604 wl_fixed_t vx, wl_fixed_t vy,
1605 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001606{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001607 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001608
Jason Ekstranda7af7042013-10-12 22:38:11 -05001609 weston_view_to_global_float(view,
1610 wl_fixed_to_double(vx),
1611 wl_fixed_to_double(vy),
1612 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001613 *x = wl_fixed_from_double(xf);
1614 *y = wl_fixed_from_double(yf);
1615}
1616
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001617WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001618weston_view_from_global_float(struct weston_view *view,
1619 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001620{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001621 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001622 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1623
Jason Ekstranda7af7042013-10-12 22:38:11 -05001624 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001625
1626 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001627 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001628 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001629 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001630 *vx = 0;
1631 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001632 return;
1633 }
1634
Jason Ekstranda7af7042013-10-12 22:38:11 -05001635 *vx = v.f[0] / v.f[3];
1636 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001637 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001638 *vx = x - view->geometry.x;
1639 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001640 }
1641}
1642
1643WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001644weston_view_from_global_fixed(struct weston_view *view,
1645 wl_fixed_t x, wl_fixed_t y,
1646 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001647{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001648 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001649
Jason Ekstranda7af7042013-10-12 22:38:11 -05001650 weston_view_from_global_float(view,
1651 wl_fixed_to_double(x),
1652 wl_fixed_to_double(y),
1653 &vxf, &vyf);
1654 *vx = wl_fixed_from_double(vxf);
1655 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001656}
1657
1658WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001659weston_view_from_global(struct weston_view *view,
1660 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001661{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001662 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001663
Jason Ekstranda7af7042013-10-12 22:38:11 -05001664 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1665 *vx = floorf(vxf);
1666 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001667}
1668
Bryce Harrington3f650b82015-12-23 11:01:58 -08001669/**
1670 * \param surface The surface to be repainted
1671 *
1672 * Marks the output(s) that the surface is shown on as needing to be
1673 * repainted. See weston_output_schedule_repaint().
1674 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001675WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001676weston_surface_schedule_repaint(struct weston_surface *surface)
1677{
1678 struct weston_output *output;
1679
1680 wl_list_for_each(output, &surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001681 if (surface->output_mask & (1u << output->id))
Kristian Høgsberg98238702012-08-03 16:29:12 -04001682 weston_output_schedule_repaint(output);
1683}
1684
Bryce Harrington3f650b82015-12-23 11:01:58 -08001685/**
1686 * \param view The view to be repainted
1687 *
1688 * Marks the output(s) that the view is shown on as needing to be
1689 * repainted. See weston_output_schedule_repaint().
1690 */
Kristian Høgsberg98238702012-08-03 16:29:12 -04001691WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001692weston_view_schedule_repaint(struct weston_view *view)
1693{
1694 struct weston_output *output;
1695
1696 wl_list_for_each(output, &view->surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001697 if (view->output_mask & (1u << output->id))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001698 weston_output_schedule_repaint(output);
1699}
1700
Pekka Paalanene508ce62015-02-19 13:59:55 +02001701/**
1702 * XXX: This function does it the wrong way.
1703 * surface->damage is the damage from the client, and causes
1704 * surface_flush_damage() to copy pixels. No window management action can
1705 * cause damage to the client-provided content, warranting re-upload!
1706 *
1707 * Instead of surface->damage, this function should record the damage
1708 * with all the views for this surface to avoid extraneous texture
1709 * uploads.
1710 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001711WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001712weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001713{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001714 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001715 0, 0, surface->width,
1716 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001717
Kristian Høgsberg98238702012-08-03 16:29:12 -04001718 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001719}
1720
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001721WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001722weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001723{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001724 if (view->geometry.x == x && view->geometry.y == y)
1725 return;
1726
Jason Ekstranda7af7042013-10-12 22:38:11 -05001727 view->geometry.x = x;
1728 view->geometry.y = y;
1729 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001730}
1731
Pekka Paalanen483243f2013-03-08 14:56:50 +02001732static void
1733transform_parent_handle_parent_destroy(struct wl_listener *listener,
1734 void *data)
1735{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001736 struct weston_view *view =
1737 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001738 geometry.parent_destroy_listener);
1739
Jason Ekstranda7af7042013-10-12 22:38:11 -05001740 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001741}
1742
1743WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001744weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001745 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001746{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001747 if (view->geometry.parent) {
1748 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1749 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001750
1751 if (!parent)
1752 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001753 }
1754
Jason Ekstranda7af7042013-10-12 22:38:11 -05001755 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001756
Jason Ekstranda7af7042013-10-12 22:38:11 -05001757 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001758 transform_parent_handle_parent_destroy;
1759 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001760 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001761 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001762 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001763 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001764 }
1765
Jason Ekstranda7af7042013-10-12 22:38:11 -05001766 weston_view_geometry_dirty(view);
1767}
1768
Pekka Paalanen380adf52015-02-16 14:39:11 +02001769/** Set a clip mask rectangle on a view
1770 *
1771 * \param view The view to set the clip mask on.
1772 * \param x Top-left corner X coordinate of the clip rectangle.
1773 * \param y Top-left corner Y coordinate of the clip rectangle.
1774 * \param width Width of the clip rectangle, non-negative.
1775 * \param height Height of the clip rectangle, non-negative.
1776 *
1777 * A shell may set a clip mask rectangle on a view. Everything outside
1778 * the rectangle is cut away for input and output purposes: it is
1779 * not drawn and cannot be hit by hit-test based input like pointer
1780 * motion or touch-downs. Everything inside the rectangle will behave
1781 * normally. Clients are unaware of clipping.
1782 *
Yong Bakos4c72e292016-04-28 11:59:10 -05001783 * The rectangle is set in surface-local coordinates. Setting a clip
Pekka Paalanen380adf52015-02-16 14:39:11 +02001784 * mask rectangle does not affect the view position, the view is positioned
1785 * as it would be without a clip. The clip also does not change
1786 * weston_surface::width,height.
1787 *
1788 * The clip mask rectangle is part of transformation inheritance
1789 * (weston_view_set_transform_parent()). A clip set in the root of the
1790 * transformation inheritance tree will affect all views in the tree.
1791 * A clip can be set only on the root view. Attempting to set a clip
1792 * on view that has a transformation parent will fail. Assigning a parent
1793 * to a view that has a clip set will cause the clip to be forgotten.
1794 *
1795 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1796 * on the additional transformations in the child views. These transformations
1797 * may not rotate the coordinate axes, i.e., only translation and scaling
1798 * are allowed. Violating this restriction causes the clipping to malfunction.
1799 * Furthermore, using scaling may cause rounding errors in child clipping.
1800 *
1801 * The clip mask rectangle is not automatically adjusted based on
1802 * wl_surface.attach dx and dy arguments.
1803 *
1804 * A clip mask rectangle can be set only if the compositor capability
1805 * WESTON_CAP_VIEW_CLIP_MASK is present.
1806 *
1807 * This function sets the clip mask rectangle and schedules a repaint for
1808 * the view.
1809 */
1810WL_EXPORT void
1811weston_view_set_mask(struct weston_view *view,
1812 int x, int y, int width, int height)
1813{
1814 struct weston_compositor *compositor = view->surface->compositor;
1815
1816 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1817 weston_log("%s not allowed without capability!\n", __func__);
1818 return;
1819 }
1820
1821 if (view->geometry.parent) {
1822 weston_log("view %p has a parent, clip forbidden!\n", view);
1823 return;
1824 }
1825
1826 if (width < 0 || height < 0) {
1827 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1828 x, y, width, height);
1829 return;
1830 }
1831
1832 pixman_region32_fini(&view->geometry.scissor);
1833 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1834 view->geometry.scissor_enabled = true;
1835 weston_view_geometry_dirty(view);
1836 weston_view_schedule_repaint(view);
1837}
1838
1839/** Remove the clip mask from a view
1840 *
1841 * \param view The view to remove the clip mask from.
1842 *
1843 * Removed the clip mask rectangle and schedules a repaint.
1844 *
1845 * \sa weston_view_set_mask
1846 */
1847WL_EXPORT void
1848weston_view_set_mask_infinite(struct weston_view *view)
1849{
1850 view->geometry.scissor_enabled = false;
1851 weston_view_geometry_dirty(view);
1852 weston_view_schedule_repaint(view);
1853}
1854
Armin Krezović0da12b82016-06-30 06:04:33 +02001855/* Check if view should be displayed
1856 *
1857 * The indicator is set manually when assigning
1858 * a view to a surface.
1859 *
1860 * This needs reworking. See the thread starting at:
1861 *
1862 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1863 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001864WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001865weston_view_is_mapped(struct weston_view *view)
1866{
Armin Krezović0da12b82016-06-30 06:04:33 +02001867 return view->is_mapped;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001868}
1869
Philipp Zabel70decd52018-09-03 20:11:15 +02001870/* Check if view is opaque in specified region
1871 *
1872 * \param view The view to check for opacity.
1873 * \param region The region to check for opacity, in view coordinates.
1874 *
1875 * Returns true if the view is opaque in the specified region, because view
1876 * alpha is 1.0 and either the opaque region set by the client contains the
1877 * specified region, or the buffer pixel format or solid color is opaque.
1878 */
1879WL_EXPORT bool
1880weston_view_is_opaque(struct weston_view *ev, pixman_region32_t *region)
1881{
1882 pixman_region32_t r;
1883 bool ret = false;
1884
1885 if (ev->alpha < 1.0)
1886 return false;
1887
1888 if (ev->surface->is_opaque)
1889 return true;
1890
1891 if (ev->transform.dirty) {
1892 weston_log("%s: transform dirty", __func__);
1893 return false;
1894 }
1895
1896 pixman_region32_init(&r);
1897 pixman_region32_subtract(&r, region, &ev->transform.opaque);
1898
1899 if (!pixman_region32_not_empty(&r))
1900 ret = true;
1901
1902 pixman_region32_fini(&r);
1903
1904 return ret;
1905}
1906
Marius Vlad5f6bee42019-09-11 16:41:04 +03001907/** Check if the view has a valid buffer available
1908 *
1909 * @param ev The view to check if it has a valid buffer.
1910 *
1911 * Returns true if the view has a valid buffer or false otherwise.
1912 */
1913WL_EXPORT bool
1914weston_view_has_valid_buffer(struct weston_view *ev)
1915{
1916 return ev->surface->buffer_ref.buffer != NULL;
1917}
1918
Marius Vlad47e3d1e2019-09-11 16:53:08 +03001919/** Check if the view matches the entire output
1920 *
1921 * @param ev The view to check.
1922 * @param output The output to check against.
1923 *
1924 * Returns true if the view does indeed matches the entire output.
1925 */
1926WL_EXPORT bool
1927weston_view_matches_output_entirely(struct weston_view *ev,
1928 struct weston_output *output)
1929{
1930 pixman_box32_t *extents =
1931 pixman_region32_extents(&ev->transform.boundingbox);
1932
1933 if (extents->x1 != output->x ||
1934 extents->y1 != output->y ||
1935 extents->x2 != output->x + output->width ||
1936 extents->y2 != output->y + output->height)
1937 return false;
1938
1939 return true;
1940}
1941
Armin Krezović0da12b82016-06-30 06:04:33 +02001942/* Check if a surface has a view assigned to it
1943 *
1944 * The indicator is set manually when mapping
1945 * a surface and creating a view for it.
1946 *
1947 * This needs to go. See the thread starting at:
1948 *
1949 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1950 *
1951 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001952WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001953weston_surface_is_mapped(struct weston_surface *surface)
1954{
Armin Krezović0da12b82016-06-30 06:04:33 +02001955 return surface->is_mapped;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001956}
1957
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001958static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001959surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001960{
1961 struct weston_view *view;
1962
1963 if (surface->width == width && surface->height == height)
1964 return;
1965
1966 surface->width = width;
1967 surface->height = height;
1968
1969 wl_list_for_each(view, &surface->views, surface_link)
1970 weston_view_geometry_dirty(view);
1971}
1972
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001973WL_EXPORT void
1974weston_surface_set_size(struct weston_surface *surface,
1975 int32_t width, int32_t height)
1976{
1977 assert(!surface->resource);
1978 surface_set_size(surface, width, height);
1979}
1980
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001981static int
1982fixed_round_up_to_int(wl_fixed_t f)
1983{
1984 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1985}
1986
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001987static void
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001988convert_size_by_transform_scale(int32_t *width_out, int32_t *height_out,
1989 int32_t width, int32_t height,
1990 uint32_t transform,
1991 int32_t scale)
1992{
1993 assert(scale > 0);
1994
1995 switch (transform) {
1996 case WL_OUTPUT_TRANSFORM_NORMAL:
1997 case WL_OUTPUT_TRANSFORM_180:
1998 case WL_OUTPUT_TRANSFORM_FLIPPED:
1999 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2000 *width_out = width / scale;
2001 *height_out = height / scale;
2002 break;
2003 case WL_OUTPUT_TRANSFORM_90:
2004 case WL_OUTPUT_TRANSFORM_270:
2005 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2006 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2007 *width_out = height / scale;
2008 *height_out = width / scale;
2009 break;
2010 default:
2011 assert(0 && "invalid transform");
2012 }
2013}
2014
2015static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002016weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002017{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002018 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01002019
2020 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02002021 surface->width_from_buffer = 0;
2022 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01002023 return;
2024 }
2025
Pekka Paalanen59987fa2016-04-26 15:50:59 +03002026 convert_size_by_transform_scale(&surface->width_from_buffer,
2027 &surface->height_from_buffer,
2028 surface->buffer_ref.buffer->width,
2029 surface->buffer_ref.buffer->height,
2030 vp->buffer.transform,
2031 vp->buffer.scale);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002032}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02002033
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002034static void
2035weston_surface_update_size(struct weston_surface *surface)
2036{
2037 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
2038 int32_t width, height;
2039
2040 width = surface->width_from_buffer;
2041 height = surface->height_from_buffer;
2042
2043 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02002044 surface_set_size(surface,
2045 vp->surface.width, vp->surface.height);
2046 return;
2047 }
2048
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002049 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03002050 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
2051 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
2052
2053 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02002054 return;
2055 }
2056
Jason Ekstrand5c11a332013-12-04 20:32:03 -06002057 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002058}
2059
Marius Vlad9fdda7f2019-06-11 16:08:55 +03002060/** weston_compositor_get_time
2061 * \ingroup compositor
2062 */
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02002063WL_EXPORT void
2064weston_compositor_get_time(struct timespec *time)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05002065{
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02002066 clock_gettime(CLOCK_REALTIME, time);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05002067}
2068
Marius Vlad9fdda7f2019-06-11 16:08:55 +03002069/** weston_compositor_pick_view
2070 * \ingroup compositor
2071 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002072WL_EXPORT struct weston_view *
2073weston_compositor_pick_view(struct weston_compositor *compositor,
2074 wl_fixed_t x, wl_fixed_t y,
2075 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02002076{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002077 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02002078 wl_fixed_t view_x, view_y;
2079 int view_ix, view_iy;
2080 int ix = wl_fixed_to_int(x);
2081 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02002082
Jason Ekstranda7af7042013-10-12 22:38:11 -05002083 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02002084 if (!pixman_region32_contains_point(
2085 &view->transform.boundingbox, ix, iy, NULL))
2086 continue;
2087
2088 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
2089 view_ix = wl_fixed_to_int(view_x);
2090 view_iy = wl_fixed_to_int(view_y);
2091
2092 if (!pixman_region32_contains_point(&view->surface->input,
2093 view_ix, view_iy, NULL))
2094 continue;
2095
Pekka Paalanen380adf52015-02-16 14:39:11 +02002096 if (view->geometry.scissor_enabled &&
2097 !pixman_region32_contains_point(&view->geometry.scissor,
2098 view_ix, view_iy, NULL))
2099 continue;
2100
Pekka Paalanenfc22a522015-02-18 15:08:29 +02002101 *vx = view_x;
2102 *vy = view_y;
2103 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02002104 }
2105
Derek Foremanf9318d12015-05-11 15:40:11 -05002106 *vx = wl_fixed_from_int(-1000000);
2107 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02002108 return NULL;
2109}
2110
2111static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002112weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002113{
Daniel Stone37816df2012-05-16 18:45:18 +01002114 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002115
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07002116 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002117 return;
2118
Daniel Stone37816df2012-05-16 18:45:18 +01002119 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04002120 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002121}
2122
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002123WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002124weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05002125{
Daniel Stone4dab5db2012-05-30 16:31:53 +01002126 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002127
Jason Ekstranda7af7042013-10-12 22:38:11 -05002128 if (!weston_view_is_mapped(view))
2129 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002130
Jason Ekstranda7af7042013-10-12 22:38:11 -05002131 weston_view_damage_below(view);
Semi Malinene7a52fb2018-04-26 11:08:10 +02002132 weston_view_set_output(view, NULL);
Xiong Zhang97116532013-10-23 13:58:31 +08002133 view->plane = NULL;
Armin Krezovićf8486c32016-06-30 06:04:28 +02002134 view->is_mapped = false;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002135 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002136 wl_list_remove(&view->link);
2137 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002138 view->output_mask = 0;
2139 weston_surface_assign_output(view->surface);
2140
2141 if (weston_surface_is_mapped(view->surface))
2142 return;
2143
2144 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002145 struct weston_touch *touch = weston_seat_get_touch(seat);
2146 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2147 struct weston_keyboard *keyboard =
2148 weston_seat_get_keyboard(seat);
2149
2150 if (keyboard && keyboard->focus == view->surface)
2151 weston_keyboard_set_focus(keyboard, NULL);
2152 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05002153 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05002154 if (touch && touch->focus == view)
2155 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01002156 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002157}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002158
Jason Ekstranda7af7042013-10-12 22:38:11 -05002159WL_EXPORT void
2160weston_surface_unmap(struct weston_surface *surface)
2161{
2162 struct weston_view *view;
2163
Armin Krezovićf8486c32016-06-30 06:04:28 +02002164 surface->is_mapped = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002165 wl_list_for_each(view, &surface->views, surface_link)
2166 weston_view_unmap(view);
2167 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05002168}
2169
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002170static void
2171weston_surface_reset_pending_buffer(struct weston_surface *surface)
2172{
Jason Ekstrand7b982072014-05-20 14:33:03 -05002173 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002174 surface->pending.sx = 0;
2175 surface->pending.sy = 0;
2176 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002177 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002178}
2179
Jason Ekstranda7af7042013-10-12 22:38:11 -05002180WL_EXPORT void
2181weston_view_destroy(struct weston_view *view)
2182{
2183 wl_signal_emit(&view->destroy_signal, view);
2184
2185 assert(wl_list_empty(&view->geometry.child_list));
2186
2187 if (weston_view_is_mapped(view)) {
2188 weston_view_unmap(view);
2189 weston_compositor_build_view_list(view->surface->compositor);
2190 }
2191
2192 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002193 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002194
2195 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02002196 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002197 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002198 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002199
2200 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen944fae82018-05-22 13:15:58 +03002201 weston_view_set_output(view, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002202
Jason Ekstranda7af7042013-10-12 22:38:11 -05002203 wl_list_remove(&view->surface_link);
2204
2205 free(view);
2206}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002207
2208WL_EXPORT void
2209weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05002210{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002211 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002212 struct weston_view *ev, *nv;
Jonas Ådahld3414f22016-07-22 17:56:31 +08002213 struct weston_pointer_constraint *constraint, *next_constraint;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04002214
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02002215 if (--surface->ref_count > 0)
2216 return;
2217
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03002218 assert(surface->resource == NULL);
2219
Pekka Paalanenca790762015-04-17 14:23:38 +03002220 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02002221
Pekka Paalanene67858b2013-04-25 13:57:42 +03002222 assert(wl_list_empty(&surface->subsurface_list_pending));
2223 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02002224
Jason Ekstranda7af7042013-10-12 22:38:11 -05002225 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
2226 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002227
Jason Ekstrand7b982072014-05-20 14:33:03 -05002228 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002229
Pekka Paalanende685b82012-12-04 15:58:12 +02002230 weston_buffer_reference(&surface->buffer_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002231 weston_buffer_release_reference(&surface->buffer_release_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04002232
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02002233 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002234 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002235 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02002236
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002237 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002238 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002239
Pekka Paalanen133e4392014-09-23 22:08:46 -04002240 weston_presentation_feedback_discard_list(&surface->feedback_list);
2241
Jonas Ådahld3414f22016-07-22 17:56:31 +08002242 wl_list_for_each_safe(constraint, next_constraint,
2243 &surface->pointer_constraints,
2244 link)
2245 weston_pointer_constraint_destroy(constraint);
2246
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03002247 fd_clear(&surface->acquire_fence_fd);
2248
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04002249 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05002250}
2251
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002252static void
2253destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08002254{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002255 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08002256
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03002257 assert(surface);
2258
Giulio Camuffo0d379742013-11-15 22:06:15 +01002259 /* Set the resource to NULL, since we don't want to leave a
2260 * dangling pointer if the surface was refcounted and survives
2261 * the weston_surface_destroy() call. */
2262 surface->resource = NULL;
Pekka Paalanen4826f872016-04-22 14:14:38 +03002263
2264 if (surface->viewport_resource)
2265 wl_resource_set_user_data(surface->viewport_resource, NULL);
2266
Alexandros Frantzis27d7c392018-10-19 12:14:11 +03002267 if (surface->synchronization_resource) {
2268 wl_resource_set_user_data(surface->synchronization_resource,
2269 NULL);
2270 }
2271
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002272 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08002273}
2274
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002275static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002276weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
2277{
2278 struct weston_buffer *buffer =
2279 container_of(listener, struct weston_buffer, destroy_listener);
2280
2281 wl_signal_emit(&buffer->destroy_signal, buffer);
2282 free(buffer);
2283}
2284
Giulio Camuffoe058cd12013-12-12 14:14:29 +01002285WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002286weston_buffer_from_resource(struct wl_resource *resource)
2287{
2288 struct weston_buffer *buffer;
2289 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002290
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002291 listener = wl_resource_get_destroy_listener(resource,
2292 weston_buffer_destroy_handler);
2293
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002294 if (listener)
2295 return container_of(listener, struct weston_buffer,
2296 destroy_listener);
2297
2298 buffer = zalloc(sizeof *buffer);
2299 if (buffer == NULL)
2300 return NULL;
2301
2302 buffer->resource = resource;
2303 wl_signal_init(&buffer->destroy_signal);
2304 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04002305 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002306 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002307
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002308 return buffer;
2309}
2310
2311static void
Pekka Paalanende685b82012-12-04 15:58:12 +02002312weston_buffer_reference_handle_destroy(struct wl_listener *listener,
2313 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002314{
Pekka Paalanende685b82012-12-04 15:58:12 +02002315 struct weston_buffer_reference *ref =
2316 container_of(listener, struct weston_buffer_reference,
2317 destroy_listener);
2318
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002319 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02002320 ref->buffer = NULL;
2321}
2322
2323WL_EXPORT void
2324weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002325 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002326{
2327 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05002328 ref->buffer->busy_count--;
2329 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002330 assert(wl_resource_get_client(ref->buffer->resource));
Matt Hoosier3052bc72017-09-26 08:09:40 -05002331 wl_buffer_send_release(ref->buffer->resource);
Kristian Høgsberg20347802013-03-04 12:07:46 -05002332 }
Pekka Paalanende685b82012-12-04 15:58:12 +02002333 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002334 }
2335
Pekka Paalanende685b82012-12-04 15:58:12 +02002336 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002337 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002338 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02002339 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02002340 }
2341
Pekka Paalanende685b82012-12-04 15:58:12 +02002342 ref->buffer = buffer;
2343 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
2344}
2345
2346static void
Alexandros Frantzis67629672018-10-19 12:14:11 +03002347weston_buffer_release_reference_handle_destroy(struct wl_listener *listener,
2348 void *data)
2349{
2350 struct weston_buffer_release_reference *ref =
2351 container_of(listener, struct weston_buffer_release_reference,
2352 destroy_listener);
2353
2354 assert((struct wl_resource *)data == ref->buffer_release->resource);
2355 ref->buffer_release = NULL;
2356}
2357
2358static void
2359weston_buffer_release_destroy(struct weston_buffer_release *buffer_release)
2360{
2361 struct wl_resource *resource = buffer_release->resource;
2362 int release_fence_fd = buffer_release->fence_fd;
2363
2364 if (release_fence_fd >= 0) {
2365 zwp_linux_buffer_release_v1_send_fenced_release(
2366 resource, release_fence_fd);
2367 } else {
2368 zwp_linux_buffer_release_v1_send_immediate_release(
2369 resource);
2370 }
2371
2372 wl_resource_destroy(resource);
2373}
2374
2375WL_EXPORT void
2376weston_buffer_release_reference(struct weston_buffer_release_reference *ref,
2377 struct weston_buffer_release *buffer_release)
2378{
2379 if (buffer_release == ref->buffer_release)
2380 return;
2381
2382 if (ref->buffer_release) {
2383 ref->buffer_release->ref_count--;
2384 wl_list_remove(&ref->destroy_listener.link);
2385 if (ref->buffer_release->ref_count == 0)
2386 weston_buffer_release_destroy(ref->buffer_release);
2387 }
2388
2389 if (buffer_release) {
2390 buffer_release->ref_count++;
2391 wl_resource_add_destroy_listener(buffer_release->resource,
2392 &ref->destroy_listener);
2393 }
2394
2395 ref->buffer_release = buffer_release;
2396 ref->destroy_listener.notify =
2397 weston_buffer_release_reference_handle_destroy;
2398}
2399
2400WL_EXPORT void
2401weston_buffer_release_move(struct weston_buffer_release_reference *dest,
2402 struct weston_buffer_release_reference *src)
2403{
2404 weston_buffer_release_reference(dest, src->buffer_release);
2405 weston_buffer_release_reference(src, NULL);
2406}
2407
2408static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002409weston_surface_attach(struct weston_surface *surface,
2410 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002411{
2412 weston_buffer_reference(&surface->buffer_ref, buffer);
2413
Pekka Paalanena6421c42012-12-04 15:58:10 +02002414 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002415 if (weston_surface_is_mapped(surface))
2416 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002417 }
2418
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002419 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02002420
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002421 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002422 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002423}
2424
Marius Vlad9fdda7f2019-06-11 16:08:55 +03002425/** weston_compositor_damage_all
2426 * \ingroup compositor
2427 */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002428WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002429weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002430{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002431 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002432
2433 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002434 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002435}
2436
Marius Vlad55d87362019-06-11 01:15:35 +03002437/**
2438 * \ingroup output
2439 */
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05002440WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002441weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002442{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002443 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002444
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002445 pixman_region32_union(&compositor->primary_plane.damage,
2446 &compositor->primary_plane.damage,
2447 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002448 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002449}
2450
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04002451static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002452surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002453{
Pekka Paalanende685b82012-12-04 15:58:12 +02002454 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002455 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002456 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002457
Marius Vlad2a1b7862019-09-05 13:12:18 +03002458 if (pixman_region32_not_empty(&surface->damage))
Marius Vlad3203ff62019-09-05 14:56:12 +03002459 TL_POINT(surface->compositor, "core_flush_damage", TLP_SURFACE(surface),
Pekka Paalanenb5026542014-11-12 15:09:24 +02002460 TLP_OUTPUT(surface->output), TLP_END);
2461
Jason Ekstrandef540082014-06-26 10:37:36 -07002462 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002463}
2464
2465static void
2466view_accumulate_damage(struct weston_view *view,
2467 pixman_region32_t *opaque)
2468{
2469 pixman_region32_t damage;
2470
2471 pixman_region32_init(&damage);
2472 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002473 pixman_box32_t *extents;
2474
Jason Ekstranda7af7042013-10-12 22:38:11 -05002475 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002476 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002477 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002478 pixman_region32_copy(&damage, &view->surface->damage);
2479 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002480 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002481 }
2482
Pekka Paalanen380adf52015-02-16 14:39:11 +02002483 pixman_region32_intersect(&damage, &damage,
2484 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002485 pixman_region32_subtract(&damage, &damage, opaque);
2486 pixman_region32_union(&view->plane->damage,
2487 &view->plane->damage, &damage);
2488 pixman_region32_fini(&damage);
2489 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002490 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002491}
2492
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002493static void
Michael Olbrichc5ea4952020-08-11 12:42:35 +02002494output_accumulate_damage(struct weston_output *output)
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002495{
Michael Olbrichc5ea4952020-08-11 12:42:35 +02002496 struct weston_compositor *ec = output->compositor;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002497 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002498 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002499 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002500
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002501 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002502
2503 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002504 pixman_region32_copy(&plane->clip, &clip);
2505
2506 pixman_region32_init(&opaque);
2507
Jason Ekstranda7af7042013-10-12 22:38:11 -05002508 wl_list_for_each(ev, &ec->view_list, link) {
2509 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002510 continue;
2511
Jason Ekstranda7af7042013-10-12 22:38:11 -05002512 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002513 }
2514
2515 pixman_region32_union(&clip, &clip, &opaque);
2516 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002517 }
2518
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002519 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002520
Jason Ekstranda7af7042013-10-12 22:38:11 -05002521 wl_list_for_each(ev, &ec->view_list, link)
Derek Foreman060cf112015-11-18 16:32:26 -06002522 ev->surface->touched = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002523
2524 wl_list_for_each(ev, &ec->view_list, link) {
Michael Olbrichc5ea4952020-08-11 12:42:35 +02002525 /* Ignore views not visible on the current output */
2526 if (!(ev->output_mask & (1u << output->id)))
2527 continue;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002528 if (ev->surface->touched)
2529 continue;
Derek Foreman060cf112015-11-18 16:32:26 -06002530 ev->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002531
2532 surface_flush_damage(ev->surface);
2533
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002534 /* Both the renderer and the backend have seen the buffer
2535 * by now. If renderer needs the buffer, it has its own
2536 * reference set. If the backend wants to keep the buffer
2537 * around for migrating the surface into a non-primary plane
2538 * later, keep_buffer is true. Otherwise, drop the core
2539 * reference now, and allow early buffer release. This enables
2540 * clients to use single-buffering.
2541 */
Alexandros Frantzis67629672018-10-19 12:14:11 +03002542 if (!ev->surface->keep_buffer) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002543 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002544 weston_buffer_release_reference(
2545 &ev->surface->buffer_release_ref, NULL);
2546 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002547 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002548}
2549
2550static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002551surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002552{
2553 struct weston_subsurface *sub;
2554
Pekka Paalanene67858b2013-04-25 13:57:42 +03002555 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002556 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002557 continue;
2558
Jason Ekstranda7af7042013-10-12 22:38:11 -05002559 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2560 wl_list_init(&sub->surface->views);
2561
2562 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002563 }
2564}
2565
2566static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002567surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002568{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002569 struct weston_subsurface *sub;
2570 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002571
Jason Ekstranda7af7042013-10-12 22:38:11 -05002572 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2573 if (sub->surface == surface)
2574 continue;
2575
George Kiagiadakised04d382014-06-13 18:10:26 +02002576 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2577 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002578 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002579 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002580
2581 surface_free_unused_subsurface_views(sub->surface);
2582 }
2583}
2584
2585static void
2586view_list_add_subsurface_view(struct weston_compositor *compositor,
2587 struct weston_subsurface *sub,
2588 struct weston_view *parent)
2589{
2590 struct weston_subsurface *child;
2591 struct weston_view *view = NULL, *iv;
2592
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002593 if (!weston_surface_is_mapped(sub->surface))
2594 return;
2595
Jason Ekstranda7af7042013-10-12 22:38:11 -05002596 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2597 if (iv->geometry.parent == parent) {
2598 view = iv;
2599 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002600 }
2601 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002602
2603 if (view) {
2604 /* Put it back in the surface's list of views */
2605 wl_list_remove(&view->surface_link);
2606 wl_list_insert(&sub->surface->views, &view->surface_link);
2607 } else {
2608 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002609 weston_view_set_position(view,
2610 sub->position.x,
2611 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002612 weston_view_set_transform_parent(view, parent);
2613 }
2614
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002615 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002616 weston_view_update_transform(view);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002617 view->is_mapped = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002618
Pekka Paalanenb188e912013-11-19 14:03:35 +02002619 if (wl_list_empty(&sub->surface->subsurface_list)) {
2620 wl_list_insert(compositor->view_list.prev, &view->link);
2621 return;
2622 }
2623
2624 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2625 if (child->surface == sub->surface)
2626 wl_list_insert(compositor->view_list.prev, &view->link);
2627 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002628 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002629 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002630}
2631
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002632/* This recursively adds the sub-surfaces for a view, relying on the
2633 * sub-surface order. Thus, if a client restacks the sub-surfaces, that
2634 * change first happens to the sub-surface list, and then automatically
2635 * propagates here. See weston_surface_damage_subsurfaces() for how the
2636 * sub-surfaces receive damage when the client changes the state.
2637 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002638static void
2639view_list_add(struct weston_compositor *compositor,
2640 struct weston_view *view)
2641{
2642 struct weston_subsurface *sub;
2643
2644 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002645
Pekka Paalanenb188e912013-11-19 14:03:35 +02002646 if (wl_list_empty(&view->surface->subsurface_list)) {
2647 wl_list_insert(compositor->view_list.prev, &view->link);
2648 return;
2649 }
2650
2651 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2652 if (sub->surface == view->surface)
2653 wl_list_insert(compositor->view_list.prev, &view->link);
2654 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002655 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002656 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002657}
2658
2659static void
2660weston_compositor_build_view_list(struct weston_compositor *compositor)
2661{
Loïc Yhuel267b16e2019-09-17 20:14:56 +02002662 struct weston_view *view, *tmp;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002663 struct weston_layer *layer;
2664
2665 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002666 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002667 surface_stash_subsurface_views(view->surface);
2668
Loïc Yhuel267b16e2019-09-17 20:14:56 +02002669 wl_list_for_each_safe(view, tmp, &compositor->view_list, link)
2670 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002671 wl_list_init(&compositor->view_list);
Loïc Yhuel267b16e2019-09-17 20:14:56 +02002672
Jason Ekstranda7af7042013-10-12 22:38:11 -05002673 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002674 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002675 view_list_add(compositor, view);
2676 }
2677 }
2678
2679 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002680 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002681 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002682}
2683
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002684static void
2685weston_output_take_feedback_list(struct weston_output *output,
2686 struct weston_surface *surface)
2687{
2688 struct weston_view *view;
2689 struct weston_presentation_feedback *feedback;
2690 uint32_t flags = 0xffffffff;
2691
2692 if (wl_list_empty(&surface->feedback_list))
2693 return;
2694
2695 /* All views must have the flag for the flag to survive. */
2696 wl_list_for_each(view, &surface->views, surface_link) {
2697 /* ignore views that are not on this output at all */
2698 if (view->output_mask & (1u << output->id))
2699 flags &= view->psf_flags;
2700 }
2701
2702 wl_list_for_each(feedback, &surface->feedback_list, link)
2703 feedback->psf_flags = flags;
2704
2705 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2706 wl_list_init(&surface->feedback_list);
2707}
2708
David Herrmann1edf44c2013-10-22 17:11:26 +02002709static int
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002710weston_output_repaint(struct weston_output *output, void *repaint_data)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002711{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002712 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002713 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002714 struct weston_animation *animation, *next;
2715 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002716 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002717 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002718 int r;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002719 uint32_t frame_time_msec;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05302720 enum weston_hdcp_protection highest_requested = WESTON_HDCP_DISABLE;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002721
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002722 if (output->destroying)
2723 return 0;
2724
Marius Vlad3203ff62019-09-05 14:56:12 +03002725 TL_POINT(ec, "core_repaint_begin", TLP_OUTPUT(output), TLP_END);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002726
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002727 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002728 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002729
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05302730 /* Find the highest protection desired for an output */
2731 wl_list_for_each(ev, &ec->view_list, link) {
2732 if (ev->surface->output_mask & (1u << output->id)) {
2733 /*
2734 * The desired_protection of the output should be the
2735 * maximum of the desired_protection of the surfaces,
2736 * that are displayed on that output, to avoid
2737 * reducing the protection for existing surfaces.
2738 */
2739 if (ev->surface->desired_protection > highest_requested)
2740 highest_requested =
2741 ev->surface->desired_protection;
2742 }
2743 }
2744
2745 output->desired_protection = highest_requested;
2746
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002747 if (output->assign_planes && !output->disable_planes) {
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002748 output->assign_planes(output, repaint_data);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002749 } else {
2750 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002751 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002752 ev->psf_flags = 0;
2753 }
2754 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002755
Pekka Paalanene67858b2013-04-25 13:57:42 +03002756 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002757 wl_list_for_each(ev, &ec->view_list, link) {
2758 /* Note: This operation is safe to do multiple times on the
2759 * same surface.
2760 */
2761 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002762 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002763 &ev->surface->frame_callback_list);
2764 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002765
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002766 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002767 }
2768 }
2769
Michael Olbrichc5ea4952020-08-11 12:42:35 +02002770 output_accumulate_damage(output);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002771
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002772 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002773 pixman_region32_intersect(&output_damage,
2774 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002775 pixman_region32_subtract(&output_damage,
2776 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002777
Scott Moreauccbf29d2012-02-22 14:21:41 -07002778 if (output->dirty)
2779 weston_output_update_matrix(output);
2780
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002781 r = output->repaint(output, &output_damage, repaint_data);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002782
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002783 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002784
Daniel Stone09a97e22017-03-01 11:34:06 +00002785 output->repaint_needed = false;
Daniel Stone05df8c12017-03-03 16:59:42 +00002786 if (r == 0)
2787 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002788
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002789 weston_compositor_repick(ec);
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002790
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002791 frame_time_msec = timespec_to_msec(&output->frame_time);
2792
Jonas Ådahldb773762012-06-13 00:01:21 +02002793 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002794 wl_callback_send_done(cb->resource, frame_time_msec);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002795 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002796 }
2797
Scott Moreaud64cf212012-06-08 19:40:54 -06002798 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002799 animation->frame_counter++;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002800 animation->frame(animation, output, &output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002801 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002802
Marius Vlad3203ff62019-09-05 14:56:12 +03002803 TL_POINT(ec, "core_repaint_posted", TLP_OUTPUT(output), TLP_END);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002804
David Herrmann1edf44c2013-10-22 17:11:26 +02002805 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002806}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002807
Pekka Paalanen82919792014-05-21 13:51:49 +03002808static void
2809weston_output_schedule_repaint_reset(struct weston_output *output)
2810{
Daniel Stone05df8c12017-03-03 16:59:42 +00002811 output->repaint_status = REPAINT_NOT_SCHEDULED;
Marius Vlad3203ff62019-09-05 14:56:12 +03002812 TL_POINT(output->compositor, "core_repaint_exit_loop",
2813 TLP_OUTPUT(output), TLP_END);
Pekka Paalanen82919792014-05-21 13:51:49 +03002814}
2815
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002816static int
2817weston_output_maybe_repaint(struct weston_output *output, struct timespec *now,
2818 void *repaint_data)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002819{
Pekka Paalanen0513a952014-05-21 16:17:27 +03002820 struct weston_compositor *compositor = output->compositor;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002821 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002822 int64_t msec_to_repaint;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002823
Daniel Stone6847b852017-03-01 11:34:08 +00002824 /* We're not ready yet; come back to make a decision later. */
2825 if (output->repaint_status != REPAINT_SCHEDULED)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002826 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002827
2828 msec_to_repaint = timespec_sub_to_msec(&output->next_repaint, now);
2829 if (msec_to_repaint > 1)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002830 return ret;
Daniel Stone05df8c12017-03-03 16:59:42 +00002831
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002832 /* If we're sleeping, drop the repaint machinery entirely; we will
2833 * explicitly repaint all outputs when we come back. */
2834 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2835 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
2836 goto err;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002837
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002838 /* We don't actually need to repaint this output; drop it from
2839 * repaint until something causes damage. */
2840 if (!output->repaint_needed)
2841 goto err;
2842
2843 /* If repaint fails, we aren't going to get weston_output_finish_frame
2844 * to trigger a new repaint, so drop it from repaint and hope
Daniel Stone6847b852017-03-01 11:34:08 +00002845 * something schedules a successful repaint later. As repainting may
2846 * take some time, re-read our clock as a courtesy to the next
2847 * output. */
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002848 ret = weston_output_repaint(output, repaint_data);
Daniel Stone6847b852017-03-01 11:34:08 +00002849 weston_compositor_read_presentation_clock(compositor, now);
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002850 if (ret != 0)
2851 goto err;
2852
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09002853 output->repainted = true;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002854 return ret;
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002855
2856err:
Pekka Paalanen0513a952014-05-21 16:17:27 +03002857 weston_output_schedule_repaint_reset(output);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002858 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002859}
2860
2861static void
2862output_repaint_timer_arm(struct weston_compositor *compositor)
2863{
2864 struct weston_output *output;
2865 bool any_should_repaint = false;
2866 struct timespec now;
Sergi Granellb4c08862017-03-18 13:01:15 +01002867 int64_t msec_to_next = INT64_MAX;
Daniel Stone6847b852017-03-01 11:34:08 +00002868
2869 weston_compositor_read_presentation_clock(compositor, &now);
2870
2871 wl_list_for_each(output, &compositor->output_list, link) {
2872 int64_t msec_to_this;
2873
2874 if (output->repaint_status != REPAINT_SCHEDULED)
2875 continue;
2876
2877 msec_to_this = timespec_sub_to_msec(&output->next_repaint,
2878 &now);
2879 if (!any_should_repaint || msec_to_this < msec_to_next)
2880 msec_to_next = msec_to_this;
2881
2882 any_should_repaint = true;
2883 }
2884
2885 if (!any_should_repaint)
2886 return;
2887
2888 /* Even if we should repaint immediately, add the minimum 1 ms delay.
2889 * This is a workaround to allow coalescing multiple output repaints
2890 * particularly from weston_output_finish_frame()
2891 * into the same call, which would not happen if we called
2892 * output_repaint_timer_handler() directly.
2893 */
2894 if (msec_to_next < 1)
2895 msec_to_next = 1;
2896
2897 wl_event_source_timer_update(compositor->repaint_timer, msec_to_next);
2898}
2899
2900static int
2901output_repaint_timer_handler(void *data)
2902{
2903 struct weston_compositor *compositor = data;
2904 struct weston_output *output;
2905 struct timespec now;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002906 void *repaint_data = NULL;
Emre Ucane479ed82018-03-20 15:29:40 +01002907 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002908
2909 weston_compositor_read_presentation_clock(compositor, &now);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002910
2911 if (compositor->backend->repaint_begin)
2912 repaint_data = compositor->backend->repaint_begin(compositor);
2913
2914 wl_list_for_each(output, &compositor->output_list, link) {
2915 ret = weston_output_maybe_repaint(output, &now, repaint_data);
2916 if (ret)
2917 break;
2918 }
2919
2920 if (ret == 0) {
Tomohito Esaki09bfcd62018-06-05 10:37:05 +09002921 if (compositor->backend->repaint_flush)
Antonio Borneoc90fccc2019-06-30 15:51:10 +02002922 ret = compositor->backend->repaint_flush(compositor,
2923 repaint_data);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002924 } else {
Antonio Borneoc90fccc2019-06-30 15:51:10 +02002925 if (compositor->backend->repaint_cancel)
2926 compositor->backend->repaint_cancel(compositor,
2927 repaint_data);
2928 }
2929
2930 if (ret != 0) {
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09002931 wl_list_for_each(output, &compositor->output_list, link) {
2932 if (output->repainted)
2933 weston_output_schedule_repaint_reset(output);
2934 }
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002935 }
Daniel Stone6847b852017-03-01 11:34:08 +00002936
Tomohito Esakiddaf95c2018-07-10 11:47:15 +09002937 wl_list_for_each(output, &compositor->output_list, link)
2938 output->repainted = false;
2939
Daniel Stone6847b852017-03-01 11:34:08 +00002940 output_repaint_timer_arm(compositor);
2941
Pekka Paalanen0513a952014-05-21 16:17:27 +03002942 return 0;
2943}
2944
Pekka Paalanen50aa3a72020-05-28 11:34:04 +03002945/** Convert a presentation timestamp to another clock domain
2946 *
2947 * \param compositor The compositor defines the presentation clock domain.
2948 * \param presentation_stamp The timestamp in presentation clock domain.
2949 * \param presentation_now Current time in presentation clock domain.
2950 * \param target_clock Defines the target clock domain.
2951 *
2952 * This approximation relies on presentation_stamp to be close to current time.
2953 * The further it is from current time and the bigger the speed difference
2954 * between the two clock domains, the bigger the conversion error.
2955 *
2956 * Conversion error due to system load is biased and unbounded.
2957 */
2958static struct timespec
2959convert_presentation_time_now(struct weston_compositor *compositor,
2960 const struct timespec *presentation_stamp,
2961 const struct timespec *presentation_now,
2962 clockid_t target_clock)
2963{
2964 struct timespec target_now = {};
2965 struct timespec target_stamp;
2966 int64_t delta_ns;
2967
2968 if (compositor->presentation_clock == target_clock)
2969 return *presentation_stamp;
2970
2971 clock_gettime(target_clock, &target_now);
2972 delta_ns = timespec_sub_to_nsec(presentation_stamp, presentation_now);
2973 timespec_add_nsec(&target_stamp, &target_now, delta_ns);
2974
2975 return target_stamp;
2976}
2977
Marius Vlad55d87362019-06-11 01:15:35 +03002978/**
2979 * \ingroup output
2980 */
Kristian Høgsbergef044142011-06-21 15:02:12 -04002981WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002982weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002983 const struct timespec *stamp,
2984 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002985{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002986 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002987 int32_t refresh_nsec;
2988 struct timespec now;
Pekka Paalanen50aa3a72020-05-28 11:34:04 +03002989 struct timespec vblank_monotonic;
Daniel Stone6847b852017-03-01 11:34:08 +00002990 int64_t msec_rel;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002991
Daniel Stone05df8c12017-03-03 16:59:42 +00002992 assert(output->repaint_status == REPAINT_AWAITING_COMPLETION);
Daniel Stone3615ce12017-03-01 11:34:05 +00002993 assert(stamp || (presented_flags & WP_PRESENTATION_FEEDBACK_INVALID));
2994
Daniel Stone6847b852017-03-01 11:34:08 +00002995 weston_compositor_read_presentation_clock(compositor, &now);
2996
Daniel Stone3615ce12017-03-01 11:34:05 +00002997 /* If we haven't been supplied any timestamp at all, we don't have a
2998 * timebase to work against, so any delay just wastes time. Push a
2999 * repaint as soon as possible so we can get on with it. */
Daniel Stone6847b852017-03-01 11:34:08 +00003000 if (!stamp) {
3001 output->next_repaint = now;
Daniel Stone3615ce12017-03-01 11:34:05 +00003002 goto out;
Daniel Stone6847b852017-03-01 11:34:08 +00003003 }
Daniel Stone3615ce12017-03-01 11:34:05 +00003004
Pekka Paalanen50aa3a72020-05-28 11:34:04 +03003005 vblank_monotonic = convert_presentation_time_now(compositor,
3006 stamp, &now,
3007 CLOCK_MONOTONIC);
Marius Vlad3203ff62019-09-05 14:56:12 +03003008 TL_POINT(compositor, "core_repaint_finished", TLP_OUTPUT(output),
Pekka Paalanen50aa3a72020-05-28 11:34:04 +03003009 TLP_VBLANK(&vblank_monotonic), TLP_END);
Marius Vladdf9278a2018-03-06 18:56:23 +02003010
Pekka Paalanend7894d02015-07-03 15:08:53 +03003011 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003012 weston_presentation_feedback_present_list(&output->feedback_list,
3013 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02003014 output->msc,
3015 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003016
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02003017 output->frame_time = *stamp;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003018
Daniel Stone6847b852017-03-01 11:34:08 +00003019 timespec_add_nsec(&output->next_repaint, stamp, refresh_nsec);
3020 timespec_add_msec(&output->next_repaint, &output->next_repaint,
3021 -compositor->repaint_msec);
3022 msec_rel = timespec_sub_to_msec(&output->next_repaint, &now);
Daniel Stone84aff5c2017-03-01 11:34:04 +00003023
3024 if (msec_rel < -1000 || msec_rel > 1000) {
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02003025 static bool warned;
3026
3027 if (!warned)
3028 weston_log("Warning: computed repaint delay is "
Daniel Stone6847b852017-03-01 11:34:08 +00003029 "insane: %lld msec\n", (long long) msec_rel);
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02003030 warned = true;
3031
Daniel Stone6847b852017-03-01 11:34:08 +00003032 output->next_repaint = now;
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02003033 }
3034
Mario Kleinerb7df04e2015-06-21 21:25:15 +02003035 /* Called from restart_repaint_loop and restart happens already after
3036 * the deadline given by repaint_msec? In that case we delay until
3037 * the deadline of the next frame, to give clients a more predictable
3038 * timing of the repaint cycle to lock on. */
Daniel Stoneeca5cca2017-02-28 21:53:51 +00003039 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID &&
3040 msec_rel < 0) {
3041 while (timespec_sub_to_nsec(&output->next_repaint, &now) < 0) {
3042 timespec_add_nsec(&output->next_repaint,
3043 &output->next_repaint,
3044 refresh_nsec);
3045 }
3046 }
Mario Kleinerb7df04e2015-06-21 21:25:15 +02003047
Daniel Stone3615ce12017-03-01 11:34:05 +00003048out:
Daniel Stone05df8c12017-03-03 16:59:42 +00003049 output->repaint_status = REPAINT_SCHEDULED;
Daniel Stone6847b852017-03-01 11:34:08 +00003050 output_repaint_timer_arm(compositor);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003051}
3052
3053static void
3054idle_repaint(void *data)
3055{
3056 struct weston_output *output = data;
Antonio Borneoc90fccc2019-06-30 15:51:10 +02003057 int ret;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05003058
Daniel Stone05df8c12017-03-03 16:59:42 +00003059 assert(output->repaint_status == REPAINT_BEGIN_FROM_IDLE);
3060 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03003061 output->idle_repaint_source = NULL;
Antonio Borneoc90fccc2019-06-30 15:51:10 +02003062 ret = output->start_repaint_loop(output);
3063 if (ret != 0)
3064 weston_output_schedule_repaint_reset(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003065}
3066
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003067WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003068weston_layer_entry_insert(struct weston_layer_entry *list,
3069 struct weston_layer_entry *entry)
3070{
3071 wl_list_insert(&list->link, &entry->link);
3072 entry->layer = list->layer;
3073}
3074
3075WL_EXPORT void
3076weston_layer_entry_remove(struct weston_layer_entry *entry)
3077{
3078 wl_list_remove(&entry->link);
3079 wl_list_init(&entry->link);
3080 entry->layer = NULL;
3081}
3082
Quentin Glidic82681572016-12-17 13:40:51 +01003083
3084/** Initialize the weston_layer struct.
3085 *
3086 * \param compositor The compositor instance
3087 * \param layer The layer to initialize
3088 */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003089WL_EXPORT void
Quentin Glidic82681572016-12-17 13:40:51 +01003090weston_layer_init(struct weston_layer *layer,
3091 struct weston_compositor *compositor)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003092{
Quentin Glidic82681572016-12-17 13:40:51 +01003093 layer->compositor = compositor;
3094 wl_list_init(&layer->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003095 wl_list_init(&layer->view_list.link);
3096 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003097 weston_layer_set_mask_infinite(layer);
Quentin Glidic82681572016-12-17 13:40:51 +01003098}
3099
3100/** Sets the position of the layer in the layer list. The layer will be placed
3101 * below any layer with the same position value, if any.
3102 * This function is safe to call if the layer is already on the list, but the
3103 * layer may be moved below other layers at the same position, if any.
3104 *
3105 * \param layer The layer to modify
3106 * \param position The position the layer will be placed at
3107 */
3108WL_EXPORT void
3109weston_layer_set_position(struct weston_layer *layer,
3110 enum weston_layer_position position)
3111{
3112 struct weston_layer *below;
3113
3114 wl_list_remove(&layer->link);
3115
3116 /* layer_list is ordered from top to bottom, the last layer being the
3117 * background with the smallest position value */
3118
3119 layer->position = position;
3120 wl_list_for_each_reverse(below, &layer->compositor->layer_list, link) {
3121 if (below->position >= layer->position) {
3122 wl_list_insert(&below->link, &layer->link);
3123 return;
3124 }
3125 }
3126 wl_list_insert(&layer->compositor->layer_list, &layer->link);
3127}
3128
3129/** Hide a layer by taking it off the layer list.
3130 * This function is safe to call if the layer is not on the list.
3131 *
3132 * \param layer The layer to hide
3133 */
3134WL_EXPORT void
3135weston_layer_unset_position(struct weston_layer *layer)
3136{
3137 wl_list_remove(&layer->link);
3138 wl_list_init(&layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003139}
3140
3141WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003142weston_layer_set_mask(struct weston_layer *layer,
3143 int x, int y, int width, int height)
3144{
3145 struct weston_view *view;
3146
3147 layer->mask.x1 = x;
3148 layer->mask.x2 = x + width;
3149 layer->mask.y1 = y;
3150 layer->mask.y2 = y + height;
3151
3152 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
3153 weston_view_geometry_dirty(view);
3154 }
3155}
3156
3157WL_EXPORT void
3158weston_layer_set_mask_infinite(struct weston_layer *layer)
3159{
Adam Jackson3c3f3b12019-10-16 16:02:59 -04003160 struct weston_view *view;
3161
3162 layer->mask.x1 = INT32_MIN;
3163 layer->mask.x2 = INT32_MAX;
3164 layer->mask.y1 = INT32_MIN;
3165 layer->mask.y2 = INT32_MAX;
3166
3167 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
3168 weston_view_geometry_dirty(view);
3169 }
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003170}
3171
Daniel Stone3b775632018-07-20 08:38:25 +01003172WL_EXPORT bool
3173weston_layer_mask_is_infinite(struct weston_layer *layer)
3174{
3175 return layer->mask.x1 == INT32_MIN &&
3176 layer->mask.y1 == INT32_MIN &&
Adam Jackson3c3f3b12019-10-16 16:02:59 -04003177 layer->mask.x2 == INT32_MAX &&
3178 layer->mask.y2 == INT32_MAX;
Daniel Stone3b775632018-07-20 08:38:25 +01003179}
3180
Marius Vlad55d87362019-06-11 01:15:35 +03003181/**
3182 * \ingroup output
3183 */
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003184WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003185weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003186{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003187 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04003188 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01003189
Bryce Harrington08976ac2016-08-30 12:05:16 -07003190 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
3191 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003192 return;
3193
Pekka Paalanenb5026542014-11-12 15:09:24 +02003194 if (!output->repaint_needed)
Marius Vlad3203ff62019-09-05 14:56:12 +03003195 TL_POINT(compositor, "core_repaint_req", TLP_OUTPUT(output), TLP_END);
Pekka Paalanenb5026542014-11-12 15:09:24 +02003196
Kristian Høgsbergef044142011-06-21 15:02:12 -04003197 loop = wl_display_get_event_loop(compositor->wl_display);
Daniel Stone09a97e22017-03-01 11:34:06 +00003198 output->repaint_needed = true;
Daniel Stone05df8c12017-03-03 16:59:42 +00003199
3200 /* If we already have a repaint scheduled for our idle handler,
3201 * no need to set it again. If the repaint has been called but
3202 * not finished, then weston_output_finish_frame() will notice
3203 * that a repaint is needed and schedule one. */
3204 if (output->repaint_status != REPAINT_NOT_SCHEDULED)
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003205 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01003206
Daniel Stone05df8c12017-03-03 16:59:42 +00003207 output->repaint_status = REPAINT_BEGIN_FROM_IDLE;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03003208 assert(!output->idle_repaint_source);
3209 output->idle_repaint_source = wl_event_loop_add_idle(loop, idle_repaint,
3210 output);
Marius Vlad3203ff62019-09-05 14:56:12 +03003211 TL_POINT(compositor, "core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003212}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05003213
Marius Vlad9fdda7f2019-06-11 16:08:55 +03003214/** weston_compositor_schedule_repaint
3215 * \ingroup compositor
3216 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003217WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003218weston_compositor_schedule_repaint(struct weston_compositor *compositor)
3219{
3220 struct weston_output *output;
3221
3222 wl_list_for_each(output, &compositor->output_list, link)
3223 weston_output_schedule_repaint(output);
3224}
3225
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003226static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003227surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003228{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003229 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003230}
3231
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003232static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003233surface_attach(struct wl_client *client,
3234 struct wl_resource *resource,
3235 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
3236{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003237 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05003238 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003239
Kristian Høgsbergab19f932013-08-20 11:30:54 -07003240 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05003241 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07003242 if (buffer == NULL) {
3243 wl_client_post_no_memory(client);
3244 return;
3245 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07003246 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04003247
Pekka Paalanende685b82012-12-04 15:58:12 +02003248 /* Attach, attach, without commit in between does not send
3249 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003250 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003251
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003252 surface->pending.sx = sx;
3253 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01003254 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003255}
3256
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003257static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003258surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003259 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003260 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05003261{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003262 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003263
Derek Foreman57e92ed2015-11-17 14:11:35 -06003264 if (width <= 0 || height <= 0)
3265 return;
3266
Derek Foreman152254b2015-11-26 14:17:48 -06003267 pixman_region32_union_rect(&surface->pending.damage_surface,
3268 &surface->pending.damage_surface,
3269 x, y, width, height);
3270}
3271
3272static void
3273surface_damage_buffer(struct wl_client *client,
3274 struct wl_resource *resource,
3275 int32_t x, int32_t y, int32_t width, int32_t height)
3276{
3277 struct weston_surface *surface = wl_resource_get_user_data(resource);
3278
3279 if (width <= 0 || height <= 0)
3280 return;
3281
3282 pixman_region32_union_rect(&surface->pending.damage_buffer,
3283 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04003284 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003285}
3286
Kristian Høgsberg33418202011-08-16 23:01:28 -04003287static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003288destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04003289{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05003290 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003291
3292 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02003293 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003294}
3295
3296static void
3297surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003298 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04003299{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003300 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003301 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003302
3303 cb = malloc(sizeof *cb);
3304 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003305 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003306 return;
3307 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03003308
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003309 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
3310 callback);
3311 if (cb->resource == NULL) {
3312 free(cb);
3313 wl_resource_post_no_memory(resource);
3314 return;
3315 }
3316
Jason Ekstranda85118c2013-06-27 20:17:02 -05003317 wl_resource_set_implementation(cb->resource, NULL, cb,
3318 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003319
Pekka Paalanenbc106382012-10-10 12:49:31 +03003320 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003321}
3322
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003323static void
3324surface_set_opaque_region(struct wl_client *client,
3325 struct wl_resource *resource,
3326 struct wl_resource *region_resource)
3327{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003328 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003329 struct weston_region *region;
3330
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003331 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003332 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003333 pixman_region32_copy(&surface->pending.opaque,
3334 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003335 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07003336 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003337 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003338}
3339
3340static void
3341surface_set_input_region(struct wl_client *client,
3342 struct wl_resource *resource,
3343 struct wl_resource *region_resource)
3344{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003345 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003346 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003347
3348 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003349 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003350 pixman_region32_copy(&surface->pending.input,
3351 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003352 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003353 pixman_region32_fini(&surface->pending.input);
3354 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003355 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003356}
3357
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003358/* Cause damage to this sub-surface and all its children.
3359 *
3360 * This is useful when there are state changes that need an implicit
3361 * damage, e.g. a z-order change.
3362 */
3363static void
3364weston_surface_damage_subsurfaces(struct weston_subsurface *sub)
3365{
3366 struct weston_subsurface *child;
3367
3368 weston_surface_damage(sub->surface);
3369 sub->reordered = false;
3370
3371 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link)
3372 if (child != sub)
3373 weston_surface_damage_subsurfaces(child);
3374}
3375
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003376static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003377weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003378{
Pekka Paalanene67858b2013-04-25 13:57:42 +03003379 struct weston_subsurface *sub;
3380
3381 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
3382 parent_link_pending) {
3383 wl_list_remove(&sub->parent_link);
3384 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003385
3386 if (sub->reordered)
3387 weston_surface_damage_subsurfaces(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003388 }
3389}
3390
3391static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03003392weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05003393 struct weston_matrix *matrix)
3394{
Pekka Paalanen04baea52016-04-26 15:50:58 +03003395 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05003396 double src_width, src_height, dest_width, dest_height;
3397
3398 weston_matrix_init(matrix);
3399
3400 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
3401 src_width = surface->width_from_buffer;
3402 src_height = surface->height_from_buffer;
3403 } else {
3404 src_width = wl_fixed_to_double(vp->buffer.src_width);
3405 src_height = wl_fixed_to_double(vp->buffer.src_height);
3406 }
3407
3408 if (vp->surface.width == -1) {
3409 dest_width = src_width;
3410 dest_height = src_height;
3411 } else {
3412 dest_width = vp->surface.width;
3413 dest_height = vp->surface.height;
3414 }
3415
3416 if (src_width != dest_width || src_height != dest_height)
3417 weston_matrix_scale(matrix,
3418 src_width / dest_width,
3419 src_height / dest_height, 1);
3420
3421 if (vp->buffer.src_width != wl_fixed_from_int(-1))
3422 weston_matrix_translate(matrix,
3423 wl_fixed_to_double(vp->buffer.src_x),
3424 wl_fixed_to_double(vp->buffer.src_y),
3425 0);
3426
3427 switch (vp->buffer.transform) {
3428 case WL_OUTPUT_TRANSFORM_FLIPPED:
3429 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3430 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3431 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3432 weston_matrix_scale(matrix, -1, 1, 1);
3433 weston_matrix_translate(matrix,
3434 surface->width_from_buffer, 0, 0);
3435 break;
3436 }
3437
3438 switch (vp->buffer.transform) {
3439 default:
3440 case WL_OUTPUT_TRANSFORM_NORMAL:
3441 case WL_OUTPUT_TRANSFORM_FLIPPED:
3442 break;
3443 case WL_OUTPUT_TRANSFORM_90:
3444 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +02003445 weston_matrix_rotate_xy(matrix, 0, -1);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003446 weston_matrix_translate(matrix,
Pekka Paalanen8060d822020-02-06 15:27:54 +02003447 0, surface->width_from_buffer, 0);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003448 break;
3449 case WL_OUTPUT_TRANSFORM_180:
3450 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3451 weston_matrix_rotate_xy(matrix, -1, 0);
3452 weston_matrix_translate(matrix,
3453 surface->width_from_buffer,
3454 surface->height_from_buffer, 0);
3455 break;
3456 case WL_OUTPUT_TRANSFORM_270:
3457 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +02003458 weston_matrix_rotate_xy(matrix, 0, 1);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003459 weston_matrix_translate(matrix,
Pekka Paalanen8060d822020-02-06 15:27:54 +02003460 surface->height_from_buffer, 0, 0);
Jason Ekstrand1e059042014-10-16 10:55:19 -05003461 break;
3462 }
3463
3464 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
3465}
3466
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003467/**
3468 * Compute a + b > c while being safe to overflows.
3469 */
3470static bool
3471fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
3472{
3473 return (int64_t)a + (int64_t)b > (int64_t)c;
3474}
3475
3476static bool
3477weston_surface_is_pending_viewport_source_valid(
3478 const struct weston_surface *surface)
3479{
3480 const struct weston_surface_state *pend = &surface->pending;
3481 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
3482 int width_from_buffer = 0;
3483 int height_from_buffer = 0;
3484 wl_fixed_t w;
3485 wl_fixed_t h;
3486
3487 /* If viewport source rect is not set, it is always ok. */
3488 if (vp->buffer.src_width == wl_fixed_from_int(-1))
3489 return true;
3490
3491 if (pend->newly_attached) {
3492 if (pend->buffer) {
3493 convert_size_by_transform_scale(&width_from_buffer,
3494 &height_from_buffer,
3495 pend->buffer->width,
3496 pend->buffer->height,
3497 vp->buffer.transform,
3498 vp->buffer.scale);
3499 } else {
3500 /* No buffer: viewport is irrelevant. */
3501 return true;
3502 }
3503 } else {
3504 width_from_buffer = surface->width_from_buffer;
3505 height_from_buffer = surface->height_from_buffer;
3506 }
3507
3508 assert((width_from_buffer == 0) == (height_from_buffer == 0));
3509 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
3510
3511 /* No buffer: viewport is irrelevant. */
3512 if (width_from_buffer == 0 || height_from_buffer == 0)
3513 return true;
3514
3515 /* overflow checks for wl_fixed_from_int() */
3516 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
3517 return false;
3518 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
3519 return false;
3520
3521 w = wl_fixed_from_int(width_from_buffer);
3522 h = wl_fixed_from_int(height_from_buffer);
3523
3524 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
3525 return false;
3526 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
3527 return false;
3528
3529 return true;
3530}
3531
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003532static bool
3533fixed_is_integer(wl_fixed_t v)
3534{
3535 return (v & 0xff) == 0;
3536}
3537
3538static bool
3539weston_surface_is_pending_viewport_dst_size_int(
3540 const struct weston_surface *surface)
3541{
3542 const struct weston_buffer_viewport *vp =
3543 &surface->pending.buffer_viewport;
3544
3545 if (vp->surface.width != -1) {
3546 assert(vp->surface.width > 0 && vp->surface.height > 0);
3547 return true;
3548 }
3549
3550 return fixed_is_integer(vp->buffer.src_width) &&
3551 fixed_is_integer(vp->buffer.src_height);
3552}
3553
Derek Foreman152254b2015-11-26 14:17:48 -06003554/* Translate pending damage in buffer co-ordinates to surface
3555 * co-ordinates and union it with a pixman_region32_t.
3556 * This should only be called after the buffer is attached.
3557 */
3558static void
3559apply_damage_buffer(pixman_region32_t *dest,
3560 struct weston_surface *surface,
3561 struct weston_surface_state *state)
3562{
3563 struct weston_buffer *buffer = surface->buffer_ref.buffer;
3564
3565 /* wl_surface.damage_buffer needs to be clipped to the buffer,
3566 * translated into surface co-ordinates and unioned with
3567 * any other surface damage.
3568 * None of this makes sense if there is no buffer though.
3569 */
3570 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
3571 pixman_region32_t buffer_damage;
3572
3573 pixman_region32_intersect_rect(&state->damage_buffer,
3574 &state->damage_buffer,
3575 0, 0, buffer->width,
3576 buffer->height);
3577 pixman_region32_init(&buffer_damage);
3578 weston_matrix_transform_region(&buffer_damage,
3579 &surface->buffer_to_surface_matrix,
3580 &state->damage_buffer);
3581 pixman_region32_union(dest, dest, &buffer_damage);
3582 pixman_region32_fini(&buffer_damage);
3583 }
3584 /* We should clear this on commit even if there was no buffer */
3585 pixman_region32_clear(&state->damage_buffer);
3586}
3587
Jason Ekstrand1e059042014-10-16 10:55:19 -05003588static void
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303589weston_surface_set_desired_protection(struct weston_surface *surface,
3590 enum weston_hdcp_protection protection)
3591{
3592 if (surface->desired_protection == protection)
3593 return;
3594 surface->desired_protection = protection;
3595 weston_surface_damage(surface);
3596}
3597
3598static void
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303599weston_surface_set_protection_mode(struct weston_surface *surface,
3600 enum weston_surface_protection_mode p_mode)
3601{
3602 struct content_protection *cp = surface->compositor->content_protection;
3603 struct protected_surface *psurface;
3604
3605 surface->protection_mode = p_mode;
3606 wl_list_for_each(psurface, &cp->protected_list, link) {
3607 if (!psurface || psurface->surface != surface)
3608 continue;
3609 weston_protected_surface_send_event(psurface,
3610 surface->current_protection);
3611 }
3612}
3613
3614static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05003615weston_surface_commit_state(struct weston_surface *surface,
3616 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003617{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003618 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003619 pixman_region32_t opaque;
3620
Alexander Larsson4ea95522013-05-22 14:41:37 +02003621 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02003622 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03003623 /* wp_viewport.set_source */
3624 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003625 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003626
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003627 /* wl_surface.attach */
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003628 if (state->newly_attached) {
3629 /* zwp_surface_synchronization_v1.set_acquire_fence */
3630 fd_move(&surface->acquire_fence_fd,
3631 &state->acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003632 /* zwp_surface_synchronization_v1.get_release */
3633 weston_buffer_release_move(&surface->buffer_release_ref,
3634 &state->buffer_release_ref);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003635 weston_surface_attach(surface, state->buffer);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003636 }
Jason Ekstrand7b982072014-05-20 14:33:03 -05003637 weston_surface_state_set_buffer(state, NULL);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003638 assert(state->acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003639 assert(state->buffer_release_ref.buffer_release == NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01003640
Jason Ekstrand1e059042014-10-16 10:55:19 -05003641 weston_surface_build_buffer_matrix(surface,
3642 &surface->surface_to_buffer_matrix);
3643 weston_matrix_invert(&surface->buffer_to_surface_matrix,
3644 &surface->surface_to_buffer_matrix);
3645
Jason Ekstrand7b982072014-05-20 14:33:03 -05003646 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003647 weston_surface_update_size(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003648 if (surface->committed)
3649 surface->committed(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003650 }
Giulio Camuffo184df502013-02-21 11:29:21 +01003651
Jason Ekstrand7b982072014-05-20 14:33:03 -05003652 state->sx = 0;
3653 state->sy = 0;
3654 state->newly_attached = 0;
3655 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03003656
Derek Foreman152254b2015-11-26 14:17:48 -06003657 /* wl_surface.damage and wl_surface.damage_buffer */
Marius Vlad2a1b7862019-09-05 13:12:18 +03003658 if (pixman_region32_not_empty(&state->damage_surface) ||
3659 pixman_region32_not_empty(&state->damage_buffer))
Marius Vlad3203ff62019-09-05 14:56:12 +03003660 TL_POINT(surface->compositor, "core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06003661
Pekka Paalanen8e159182012-10-10 12:49:25 +03003662 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06003663 &state->damage_surface);
3664
3665 apply_damage_buffer(&surface->damage, surface, state);
3666
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05003667 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07003668 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06003669 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003670
3671 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003672 pixman_region32_init(&opaque);
3673 pixman_region32_intersect_rect(&opaque, &state->opaque,
3674 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003675
3676 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
3677 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003678 wl_list_for_each(view, &surface->views, surface_link)
3679 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003680 }
3681
3682 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003683
3684 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003685 pixman_region32_intersect_rect(&surface->input, &state->input,
3686 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003687
Pekka Paalanenbc106382012-10-10 12:49:31 +03003688 /* wl_surface.frame */
3689 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05003690 &state->frame_callback_list);
3691 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003692
3693 /* XXX:
3694 * What should happen with a feedback request, if there
3695 * is no wl_buffer attached for this commit?
3696 */
3697
3698 /* presentation.feedback */
3699 wl_list_insert_list(&surface->feedback_list,
3700 &state->feedback_list);
3701 wl_list_init(&state->feedback_list);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003702
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303703 /* weston_protected_surface.enforced/relaxed */
3704 if (surface->protection_mode != state->protection_mode)
3705 weston_surface_set_protection_mode(surface,
3706 state->protection_mode);
3707
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303708 /* weston_protected_surface.set_type */
3709 weston_surface_set_desired_protection(surface, state->desired_protection);
3710
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003711 wl_signal_emit(&surface->commit_signal, surface);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003712}
3713
3714static void
3715weston_surface_commit(struct weston_surface *surface)
3716{
3717 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003718
Pekka Paalanene67858b2013-04-25 13:57:42 +03003719 weston_surface_commit_subsurface_order(surface);
3720
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003721 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003722}
3723
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003724static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003725weston_subsurface_commit(struct weston_subsurface *sub);
3726
3727static void
3728weston_subsurface_parent_commit(struct weston_subsurface *sub,
3729 int parent_is_synchronized);
3730
3731static void
3732surface_commit(struct wl_client *client, struct wl_resource *resource)
3733{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003734 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003735 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
3736
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003737 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
3738 assert(surface->viewport_resource);
3739
3740 wl_resource_post_error(surface->viewport_resource,
3741 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
3742 "wl_surface@%d has viewport source outside buffer",
3743 wl_resource_get_id(resource));
3744 return;
3745 }
3746
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003747 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
3748 assert(surface->viewport_resource);
3749
3750 wl_resource_post_error(surface->viewport_resource,
3751 WP_VIEWPORT_ERROR_BAD_SIZE,
3752 "wl_surface@%d viewport dst size not integer",
3753 wl_resource_get_id(resource));
3754 return;
3755 }
3756
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003757 if (surface->pending.acquire_fence_fd >= 0) {
3758 assert(surface->synchronization_resource);
3759
3760 if (!surface->pending.buffer) {
3761 fd_clear(&surface->pending.acquire_fence_fd);
3762 wl_resource_post_error(surface->synchronization_resource,
3763 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3764 "wl_surface@%"PRIu32" no buffer for synchronization",
3765 wl_resource_get_id(resource));
3766 return;
3767 }
3768
3769 /* We support fences for both wp_linux_dmabuf and opaque EGL
3770 * buffers, as mandated by minor version 2 of the
3771 * zwp_linux_explicit_synchronization_v1 protocol. Since
3772 * renderers that support fences currently only support these
3773 * two buffer types plus SHM buffers, we can just check for the
3774 * SHM buffer case here.
3775 */
3776 if (wl_shm_buffer_get(surface->pending.buffer->resource)) {
3777 fd_clear(&surface->pending.acquire_fence_fd);
3778 wl_resource_post_error(surface->synchronization_resource,
3779 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_UNSUPPORTED_BUFFER,
3780 "wl_surface@%"PRIu32" unsupported buffer for synchronization",
3781 wl_resource_get_id(resource));
3782 return;
3783 }
3784 }
3785
Alexandros Frantzis67629672018-10-19 12:14:11 +03003786 if (surface->pending.buffer_release_ref.buffer_release &&
3787 !surface->pending.buffer) {
3788 assert(surface->synchronization_resource);
3789
3790 wl_resource_post_error(surface->synchronization_resource,
3791 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3792 "wl_surface@%"PRIu32" no buffer for synchronization",
3793 wl_resource_get_id(resource));
3794 return;
3795 }
3796
Pekka Paalanene67858b2013-04-25 13:57:42 +03003797 if (sub) {
3798 weston_subsurface_commit(sub);
3799 return;
3800 }
3801
3802 weston_surface_commit(surface);
3803
3804 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
3805 if (sub->surface != surface)
3806 weston_subsurface_parent_commit(sub, 0);
3807 }
3808}
3809
3810static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003811surface_set_buffer_transform(struct wl_client *client,
3812 struct wl_resource *resource, int transform)
3813{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003814 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003815
Jonny Lamba55f1392014-05-30 12:07:15 +02003816 /* if wl_output.transform grows more members this will need to be updated. */
3817 if (transform < 0 ||
3818 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
3819 wl_resource_post_error(resource,
3820 WL_SURFACE_ERROR_INVALID_TRANSFORM,
3821 "buffer transform must be a valid transform "
3822 "('%d' specified)", transform);
3823 return;
3824 }
3825
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003826 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003827 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003828}
3829
Alexander Larsson4ea95522013-05-22 14:41:37 +02003830static void
3831surface_set_buffer_scale(struct wl_client *client,
3832 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003833 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003834{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003835 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003836
Jonny Lamba55f1392014-05-30 12:07:15 +02003837 if (scale < 1) {
3838 wl_resource_post_error(resource,
3839 WL_SURFACE_ERROR_INVALID_SCALE,
3840 "buffer scale must be at least one "
3841 "('%d' specified)", scale);
3842 return;
3843 }
3844
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003845 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003846 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003847}
3848
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003849static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003850 surface_destroy,
3851 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04003852 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003853 surface_frame,
3854 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003855 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003856 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02003857 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06003858 surface_set_buffer_scale,
3859 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003860};
3861
3862static void
3863compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003864 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003865{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04003866 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003867 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003868
Kristian Høgsberg18c93002012-01-27 11:58:31 -05003869 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003870 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003871 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003872 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003873 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003874
Jason Ekstranda85118c2013-06-27 20:17:02 -05003875 surface->resource =
3876 wl_resource_create(client, &wl_surface_interface,
3877 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003878 if (surface->resource == NULL) {
3879 weston_surface_destroy(surface);
3880 wl_resource_post_no_memory(resource);
3881 return;
3882 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003883 wl_resource_set_implementation(surface->resource, &surface_interface,
3884 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003885
3886 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003887}
3888
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003889static void
3890destroy_region(struct wl_resource *resource)
3891{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003892 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003893
3894 pixman_region32_fini(&region->region);
3895 free(region);
3896}
3897
3898static void
3899region_destroy(struct wl_client *client, struct wl_resource *resource)
3900{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003901 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003902}
3903
3904static void
3905region_add(struct wl_client *client, struct wl_resource *resource,
3906 int32_t x, int32_t y, int32_t width, int32_t height)
3907{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003908 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003909
3910 pixman_region32_union_rect(&region->region, &region->region,
3911 x, y, width, height);
3912}
3913
3914static void
3915region_subtract(struct wl_client *client, struct wl_resource *resource,
3916 int32_t x, int32_t y, int32_t width, int32_t height)
3917{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003918 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003919 pixman_region32_t rect;
3920
3921 pixman_region32_init_rect(&rect, x, y, width, height);
3922 pixman_region32_subtract(&region->region, &region->region, &rect);
3923 pixman_region32_fini(&rect);
3924}
3925
3926static const struct wl_region_interface region_interface = {
3927 region_destroy,
3928 region_add,
3929 region_subtract
3930};
3931
3932static void
3933compositor_create_region(struct wl_client *client,
3934 struct wl_resource *resource, uint32_t id)
3935{
3936 struct weston_region *region;
3937
3938 region = malloc(sizeof *region);
3939 if (region == NULL) {
3940 wl_resource_post_no_memory(resource);
3941 return;
3942 }
3943
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003944 pixman_region32_init(&region->region);
3945
Jason Ekstranda85118c2013-06-27 20:17:02 -05003946 region->resource =
3947 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003948 if (region->resource == NULL) {
3949 free(region);
3950 wl_resource_post_no_memory(resource);
3951 return;
3952 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003953 wl_resource_set_implementation(region->resource, &region_interface,
3954 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003955}
3956
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003957static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003958 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003959 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003960};
3961
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003962static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003963weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3964{
3965 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003966
Jason Ekstrand7b982072014-05-20 14:33:03 -05003967 weston_surface_commit_state(surface, &sub->cached);
3968 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003969
3970 weston_surface_commit_subsurface_order(surface);
3971
3972 weston_surface_schedule_repaint(surface);
3973
Jason Ekstrand7b982072014-05-20 14:33:03 -05003974 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003975}
3976
3977static void
3978weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3979{
3980 struct weston_surface *surface = sub->surface;
3981
3982 /*
3983 * If this commit would cause the surface to move by the
3984 * attach(dx, dy) parameters, the old damage region must be
3985 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05003986 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003987 */
Derek Foreman152254b2015-11-26 14:17:48 -06003988 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003989 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06003990 pixman_region32_union(&sub->cached.damage_surface,
3991 &sub->cached.damage_surface,
3992 &surface->pending.damage_surface);
3993 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003994
3995 if (surface->pending.newly_attached) {
3996 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003997 weston_surface_state_set_buffer(&sub->cached,
3998 surface->pending.buffer);
3999 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004000 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04004001 weston_presentation_feedback_discard_list(
4002 &sub->cached.feedback_list);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03004003 /* zwp_surface_synchronization_v1.set_acquire_fence */
4004 fd_move(&sub->cached.acquire_fence_fd,
4005 &surface->pending.acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03004006 /* zwp_surface_synchronization_v1.get_release */
4007 weston_buffer_release_move(&sub->cached.buffer_release_ref,
4008 &surface->pending.buffer_release_ref);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004009 }
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05304010 sub->cached.desired_protection = surface->pending.desired_protection;
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05304011 sub->cached.protection_mode = surface->pending.protection_mode;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03004012 assert(surface->pending.acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03004013 assert(surface->pending.buffer_release_ref.buffer_release == NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004014 sub->cached.sx += surface->pending.sx;
4015 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02004016
Derek Foreman152254b2015-11-26 14:17:48 -06004017 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
4018
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004019 sub->cached.buffer_viewport.changed |=
4020 surface->pending.buffer_viewport.changed;
4021 sub->cached.buffer_viewport.buffer =
4022 surface->pending.buffer_viewport.buffer;
4023 sub->cached.buffer_viewport.surface =
4024 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004025
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004026 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004027
4028 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
4029
4030 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
4031
4032 wl_list_insert_list(&sub->cached.frame_callback_list,
4033 &surface->pending.frame_callback_list);
4034 wl_list_init(&surface->pending.frame_callback_list);
4035
Pekka Paalanen133e4392014-09-23 22:08:46 -04004036 wl_list_insert_list(&sub->cached.feedback_list,
4037 &surface->pending.feedback_list);
4038 wl_list_init(&surface->pending.feedback_list);
4039
Jason Ekstrand7b982072014-05-20 14:33:03 -05004040 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004041}
4042
Derek Foreman280e7dd2014-10-03 13:13:42 -05004043static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03004044weston_subsurface_is_synchronized(struct weston_subsurface *sub)
4045{
4046 while (sub) {
4047 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05004048 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004049
4050 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05004051 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004052
4053 sub = weston_surface_to_subsurface(sub->parent);
4054 }
4055
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01004056 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004057}
4058
4059static void
4060weston_subsurface_commit(struct weston_subsurface *sub)
4061{
4062 struct weston_surface *surface = sub->surface;
4063 struct weston_subsurface *tmp;
4064
4065 /* Recursive check for effectively synchronized. */
4066 if (weston_subsurface_is_synchronized(sub)) {
4067 weston_subsurface_commit_to_cache(sub);
4068 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05004069 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03004070 /* flush accumulated state from cache */
4071 weston_subsurface_commit_to_cache(sub);
4072 weston_subsurface_commit_from_cache(sub);
4073 } else {
4074 weston_surface_commit(surface);
4075 }
4076
4077 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
4078 if (tmp->surface != surface)
4079 weston_subsurface_parent_commit(tmp, 0);
4080 }
4081 }
4082}
4083
4084static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004085weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004086{
4087 struct weston_surface *surface = sub->surface;
4088 struct weston_subsurface *tmp;
4089
Pekka Paalanene67858b2013-04-25 13:57:42 +03004090 /* From now on, commit_from_cache the whole sub-tree, regardless of
4091 * the synchronized mode of each child. This sub-surface or some
4092 * of its ancestors were synchronized, so we are synchronized
4093 * all the way down.
4094 */
4095
Jason Ekstrand7b982072014-05-20 14:33:03 -05004096 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004097 weston_subsurface_commit_from_cache(sub);
4098
4099 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
4100 if (tmp->surface != surface)
4101 weston_subsurface_parent_commit(tmp, 1);
4102 }
4103}
4104
4105static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004106weston_subsurface_parent_commit(struct weston_subsurface *sub,
4107 int parent_is_synchronized)
4108{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004109 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004110 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004111 wl_list_for_each(view, &sub->surface->views, surface_link)
4112 weston_view_set_position(view,
4113 sub->position.x,
4114 sub->position.y);
4115
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004116 sub->position.set = 0;
4117 }
4118
4119 if (parent_is_synchronized || sub->synchronized)
4120 weston_subsurface_synchronized_commit(sub);
4121}
4122
Pekka Paalanen8274d902014-08-06 19:36:51 +03004123static int
4124subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
4125{
4126 return snprintf(buf, len, "sub-surface");
4127}
4128
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004129static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004130subsurface_committed(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004131{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004132 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004133
Jason Ekstranda7af7042013-10-12 22:38:11 -05004134 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004135 weston_view_set_position(view,
4136 view->geometry.x + dx,
4137 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004138
4139 /* No need to check parent mappedness, because if parent is not
4140 * mapped, parent is not in a visible layer, so this sub-surface
4141 * will not be drawn either.
4142 */
Armin Krezovićf8486c32016-06-30 06:04:28 +02004143
Pekka Paalanene67858b2013-04-25 13:57:42 +03004144 if (!weston_surface_is_mapped(surface)) {
Armin Krezovićf8486c32016-06-30 06:04:28 +02004145 surface->is_mapped = true;
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03004146
Derek Foreman4b1a0a12014-09-10 15:37:33 -05004147 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03004148 * because that would call it also for the parent surface,
4149 * which might not be mapped yet. That would lead to
4150 * inconsistent state, where the window could never be
4151 * mapped.
4152 *
Armin Krezovićf8486c32016-06-30 06:04:28 +02004153 * Instead just force the is_mapped flag on, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03004154 * weston_surface_is_mapped() return true, so that when the
4155 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03004156 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03004157 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004158 }
4159}
4160
4161static struct weston_subsurface *
4162weston_surface_to_subsurface(struct weston_surface *surface)
4163{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004164 if (surface->committed == subsurface_committed)
4165 return surface->committed_private;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004166
4167 return NULL;
4168}
4169
Pekka Paalanen01388e22013-04-25 13:57:44 +03004170WL_EXPORT struct weston_surface *
4171weston_surface_get_main_surface(struct weston_surface *surface)
4172{
4173 struct weston_subsurface *sub;
4174
4175 while (surface && (sub = weston_surface_to_subsurface(surface)))
4176 surface = sub->parent;
4177
4178 return surface;
4179}
4180
Pekka Paalanen50b67472014-10-01 15:02:41 +03004181WL_EXPORT int
4182weston_surface_set_role(struct weston_surface *surface,
4183 const char *role_name,
4184 struct wl_resource *error_resource,
4185 uint32_t error_code)
4186{
4187 assert(role_name);
4188
4189 if (surface->role_name == NULL ||
4190 surface->role_name == role_name ||
4191 strcmp(surface->role_name, role_name) == 0) {
4192 surface->role_name = role_name;
4193
4194 return 0;
4195 }
4196
4197 wl_resource_post_error(error_resource, error_code,
4198 "Cannot assign role %s to wl_surface@%d,"
4199 " already has role %s\n",
4200 role_name,
4201 wl_resource_get_id(surface->resource),
4202 surface->role_name);
4203 return -1;
4204}
4205
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02004206WL_EXPORT const char *
4207weston_surface_get_role(struct weston_surface *surface)
4208{
4209 return surface->role_name;
4210}
4211
Pekka Paalanen8274d902014-08-06 19:36:51 +03004212WL_EXPORT void
4213weston_surface_set_label_func(struct weston_surface *surface,
4214 int (*desc)(struct weston_surface *,
4215 char *, size_t))
4216{
4217 surface->get_label = desc;
Marius Vlad5de92972019-10-15 13:25:41 +03004218 weston_timeline_refresh_subscription_objects(surface->compositor,
4219 surface);
Pekka Paalanen8274d902014-08-06 19:36:51 +03004220}
4221
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004222/** Get the size of surface contents
4223 *
4224 * \param surface The surface to query.
4225 * \param width Returns the width of raw contents.
4226 * \param height Returns the height of raw contents.
4227 *
4228 * Retrieves the raw surface content size in pixels for the given surface.
4229 * This is the whole content size in buffer pixels. If the surface
4230 * has no content or the renderer does not implement this feature,
4231 * zeroes are returned.
4232 *
4233 * This function is used to determine the buffer size needed for
4234 * a weston_surface_copy_content() call.
4235 */
4236WL_EXPORT void
4237weston_surface_get_content_size(struct weston_surface *surface,
4238 int *width, int *height)
4239{
4240 struct weston_renderer *rer = surface->compositor->renderer;
4241
4242 if (!rer->surface_get_content_size) {
4243 *width = 0;
4244 *height = 0;
4245 return;
4246 }
4247
4248 rer->surface_get_content_size(surface, width, height);
4249}
4250
Quentin Glidic248dd102016-08-12 10:41:34 +02004251/** Get the bounding box of a surface and its subsurfaces
4252 *
4253 * \param surface The surface to query.
4254 * \return The bounding box relative to the surface origin.
4255 *
4256 */
4257WL_EXPORT struct weston_geometry
4258weston_surface_get_bounding_box(struct weston_surface *surface)
4259{
4260 pixman_region32_t region;
4261 pixman_box32_t *box;
4262 struct weston_subsurface *subsurface;
4263
4264 pixman_region32_init_rect(&region,
4265 0, 0,
4266 surface->width, surface->height);
4267
4268 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link)
4269 pixman_region32_union_rect(&region, &region,
4270 subsurface->position.x,
4271 subsurface->position.y,
4272 subsurface->surface->width,
4273 subsurface->surface->height);
4274
4275 box = pixman_region32_extents(&region);
4276 struct weston_geometry geometry = {
4277 .x = box->x1,
4278 .y = box->y1,
4279 .width = box->x2 - box->x1,
4280 .height = box->y2 - box->y1,
4281 };
4282
4283 pixman_region32_fini(&region);
4284
4285 return geometry;
4286}
4287
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004288/** Copy surface contents to system memory.
4289 *
4290 * \param surface The surface to copy from.
4291 * \param target Pointer to the target memory buffer.
4292 * \param size Size of the target buffer in bytes.
4293 * \param src_x X location on contents to copy from.
4294 * \param src_y Y location on contents to copy from.
4295 * \param width Width in pixels of the area to copy.
4296 * \param height Height in pixels of the area to copy.
4297 * \return 0 for success, -1 for failure.
4298 *
4299 * Surface contents are maintained by the renderer. They can be in a
4300 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
4301 * else.
4302 *
4303 * Surface contents are copied into memory pointed to by target,
4304 * which has size bytes of space available. The target memory
4305 * may be larger than needed, but being smaller returns an error.
4306 * The extra bytes in target may or may not be written; their content is
4307 * unspecified. Size must be large enough to hold the image.
4308 *
4309 * The image in the target memory will be arranged in rows from
4310 * top to bottom, and pixels on a row from left to right. The pixel
4311 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
4312 * width * 4.
4313 *
4314 * Parameters src_x and src_y define the upper-left corner in buffer
4315 * coordinates (pixels) to copy from. Parameters width and height
4316 * define the size of the area to copy in pixels.
4317 *
4318 * The rectangle defined by src_x, src_y, width, height must fit in
4319 * the surface contents. Otherwise an error is returned.
4320 *
Changwoo Chof97d2502017-08-05 00:30:47 +09004321 * Use weston_surface_get_content_size to determine the content size; the
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004322 * needed target buffer size and rectangle limits.
4323 *
4324 * CURRENT IMPLEMENTATION RESTRICTIONS:
4325 * - the machine must be little-endian due to Pixman formats.
4326 *
4327 * NOTE: Pixman formats are premultiplied.
4328 */
4329WL_EXPORT int
4330weston_surface_copy_content(struct weston_surface *surface,
4331 void *target, size_t size,
4332 int src_x, int src_y,
4333 int width, int height)
4334{
4335 struct weston_renderer *rer = surface->compositor->renderer;
4336 int cw, ch;
4337 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
4338
4339 if (!rer->surface_copy_content)
4340 return -1;
4341
4342 weston_surface_get_content_size(surface, &cw, &ch);
4343
4344 if (src_x < 0 || src_y < 0)
4345 return -1;
4346
4347 if (width <= 0 || height <= 0)
4348 return -1;
4349
4350 if (src_x + width > cw || src_y + height > ch)
4351 return -1;
4352
4353 if (width * bytespp * height > size)
4354 return -1;
4355
4356 return rer->surface_copy_content(surface, target, size,
4357 src_x, src_y, width, height);
4358}
4359
Pekka Paalanene67858b2013-04-25 13:57:42 +03004360static void
4361subsurface_set_position(struct wl_client *client,
4362 struct wl_resource *resource, int32_t x, int32_t y)
4363{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004364 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004365
4366 if (!sub)
4367 return;
4368
4369 sub->position.x = x;
4370 sub->position.y = y;
4371 sub->position.set = 1;
4372}
4373
4374static struct weston_subsurface *
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004375subsurface_find_sibling(struct weston_subsurface *sub,
4376 struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004377{
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004378 struct weston_surface *parent = sub->parent;
4379 struct weston_subsurface *sibling;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004380
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004381 wl_list_for_each(sibling, &parent->subsurface_list, parent_link) {
4382 if (sibling->surface == surface && sibling != sub)
4383 return sibling;
4384 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004385
4386 return NULL;
4387}
4388
4389static struct weston_subsurface *
4390subsurface_sibling_check(struct weston_subsurface *sub,
4391 struct weston_surface *surface,
4392 const char *request)
4393{
4394 struct weston_subsurface *sibling;
4395
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004396 sibling = subsurface_find_sibling(sub, surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004397 if (!sibling) {
4398 wl_resource_post_error(sub->resource,
4399 WL_SUBSURFACE_ERROR_BAD_SURFACE,
4400 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004401 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004402 return NULL;
4403 }
4404
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004405 assert(sibling->parent == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004406
4407 return sibling;
4408}
4409
4410static void
4411subsurface_place_above(struct wl_client *client,
4412 struct wl_resource *resource,
4413 struct wl_resource *sibling_resource)
4414{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004415 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004416 struct weston_surface *surface =
4417 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004418 struct weston_subsurface *sibling;
4419
4420 if (!sub)
4421 return;
4422
4423 sibling = subsurface_sibling_check(sub, surface, "place_above");
4424 if (!sibling)
4425 return;
4426
4427 wl_list_remove(&sub->parent_link_pending);
4428 wl_list_insert(sibling->parent_link_pending.prev,
4429 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004430
4431 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004432}
4433
4434static void
4435subsurface_place_below(struct wl_client *client,
4436 struct wl_resource *resource,
4437 struct wl_resource *sibling_resource)
4438{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004439 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004440 struct weston_surface *surface =
4441 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004442 struct weston_subsurface *sibling;
4443
4444 if (!sub)
4445 return;
4446
4447 sibling = subsurface_sibling_check(sub, surface, "place_below");
4448 if (!sibling)
4449 return;
4450
4451 wl_list_remove(&sub->parent_link_pending);
4452 wl_list_insert(&sibling->parent_link_pending,
4453 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004454
4455 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004456}
4457
4458static void
4459subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
4460{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004461 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004462
4463 if (sub)
4464 sub->synchronized = 1;
4465}
4466
4467static void
4468subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
4469{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004470 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004471
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004472 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03004473 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004474
4475 /* If sub became effectively desynchronized, flush. */
4476 if (!weston_subsurface_is_synchronized(sub))
4477 weston_subsurface_synchronized_commit(sub);
4478 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004479}
4480
4481static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03004482weston_subsurface_unlink_parent(struct weston_subsurface *sub)
4483{
4484 wl_list_remove(&sub->parent_link);
4485 wl_list_remove(&sub->parent_link_pending);
4486 wl_list_remove(&sub->parent_destroy_listener.link);
4487 sub->parent = NULL;
4488}
4489
4490static void
4491weston_subsurface_destroy(struct weston_subsurface *sub);
4492
4493static void
4494subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
4495{
4496 struct weston_subsurface *sub =
4497 container_of(listener, struct weston_subsurface,
4498 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004499 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004500
4501 /* The protocol object (wl_resource) is left inert. */
4502 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004503 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004504
4505 weston_subsurface_destroy(sub);
4506}
4507
4508static void
4509subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
4510{
4511 struct weston_subsurface *sub =
4512 container_of(listener, struct weston_subsurface,
4513 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004514 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004515 assert(sub->surface != sub->parent);
4516
4517 if (weston_surface_is_mapped(sub->surface))
4518 weston_surface_unmap(sub->surface);
4519
4520 weston_subsurface_unlink_parent(sub);
4521}
4522
4523static void
4524subsurface_resource_destroy(struct wl_resource *resource)
4525{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004526 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004527
4528 if (sub)
4529 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004530}
4531
4532static void
4533subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
4534{
4535 wl_resource_destroy(resource);
4536}
4537
4538static void
4539weston_subsurface_link_parent(struct weston_subsurface *sub,
4540 struct weston_surface *parent)
4541{
4542 sub->parent = parent;
4543 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004544 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004545 &sub->parent_destroy_listener);
4546
4547 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4548 wl_list_insert(&parent->subsurface_list_pending,
4549 &sub->parent_link_pending);
4550}
4551
4552static void
4553weston_subsurface_link_surface(struct weston_subsurface *sub,
4554 struct weston_surface *surface)
4555{
4556 sub->surface = surface;
4557 sub->surface_destroy_listener.notify =
4558 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004559 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004560 &sub->surface_destroy_listener);
4561}
4562
4563static void
4564weston_subsurface_destroy(struct weston_subsurface *sub)
4565{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004566 struct weston_view *view, *next;
4567
Pekka Paalanene67858b2013-04-25 13:57:42 +03004568 assert(sub->surface);
4569
4570 if (sub->resource) {
4571 assert(weston_surface_to_subsurface(sub->surface) == sub);
4572 assert(sub->parent_destroy_listener.notify ==
4573 subsurface_handle_parent_destroy);
4574
George Kiagiadakised04d382014-06-13 18:10:26 +02004575 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
4576 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004577 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02004578 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05004579
Pekka Paalanene67858b2013-04-25 13:57:42 +03004580 if (sub->parent)
4581 weston_subsurface_unlink_parent(sub);
4582
Jason Ekstrand7b982072014-05-20 14:33:03 -05004583 weston_surface_state_fini(&sub->cached);
4584 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004585
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004586 sub->surface->committed = NULL;
4587 sub->surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004588 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004589 } else {
4590 /* the dummy weston_subsurface for the parent itself */
4591 assert(sub->parent_destroy_listener.notify == NULL);
4592 wl_list_remove(&sub->parent_link);
4593 wl_list_remove(&sub->parent_link_pending);
4594 }
4595
4596 wl_list_remove(&sub->surface_destroy_listener.link);
4597 free(sub);
4598}
4599
4600static const struct wl_subsurface_interface subsurface_implementation = {
4601 subsurface_destroy,
4602 subsurface_set_position,
4603 subsurface_place_above,
4604 subsurface_place_below,
4605 subsurface_set_sync,
4606 subsurface_set_desync
4607};
4608
4609static struct weston_subsurface *
4610weston_subsurface_create(uint32_t id, struct weston_surface *surface,
4611 struct weston_surface *parent)
4612{
4613 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004614 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004615
Bryce Harringtonde16d892014-11-20 22:21:57 -08004616 sub = zalloc(sizeof *sub);
4617 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004618 return NULL;
4619
Jason Ekstranda7af7042013-10-12 22:38:11 -05004620 wl_list_init(&sub->unused_views);
4621
Jason Ekstranda85118c2013-06-27 20:17:02 -05004622 sub->resource =
4623 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004624 if (!sub->resource) {
4625 free(sub);
4626 return NULL;
4627 }
4628
Jason Ekstranda85118c2013-06-27 20:17:02 -05004629 wl_resource_set_implementation(sub->resource,
4630 &subsurface_implementation,
4631 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004632 weston_subsurface_link_surface(sub, surface);
4633 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05004634 weston_surface_state_init(&sub->cached);
4635 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004636 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004637
4638 return sub;
4639}
4640
4641/* Create a dummy subsurface for having the parent itself in its
4642 * sub-surface lists. Makes stacking order manipulation easy.
4643 */
4644static struct weston_subsurface *
4645weston_subsurface_create_for_parent(struct weston_surface *parent)
4646{
4647 struct weston_subsurface *sub;
4648
Bryce Harringtonde16d892014-11-20 22:21:57 -08004649 sub = zalloc(sizeof *sub);
4650 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004651 return NULL;
4652
4653 weston_subsurface_link_surface(sub, parent);
4654 sub->parent = parent;
4655 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4656 wl_list_insert(&parent->subsurface_list_pending,
4657 &sub->parent_link_pending);
4658
4659 return sub;
4660}
4661
4662static void
4663subcompositor_get_subsurface(struct wl_client *client,
4664 struct wl_resource *resource,
4665 uint32_t id,
4666 struct wl_resource *surface_resource,
4667 struct wl_resource *parent_resource)
4668{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004669 struct weston_surface *surface =
4670 wl_resource_get_user_data(surface_resource);
4671 struct weston_surface *parent =
4672 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004673 struct weston_subsurface *sub;
4674 static const char where[] = "get_subsurface: wl_subsurface@";
4675
4676 if (surface == parent) {
4677 wl_resource_post_error(resource,
4678 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4679 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004680 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004681 return;
4682 }
4683
4684 if (weston_surface_to_subsurface(surface)) {
4685 wl_resource_post_error(resource,
4686 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4687 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004688 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004689 return;
4690 }
4691
Pekka Paalanen50b67472014-10-01 15:02:41 +03004692 if (weston_surface_set_role(surface, "wl_subsurface", resource,
4693 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004694 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004695
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004696 if (weston_surface_get_main_surface(parent) == surface) {
4697 wl_resource_post_error(resource,
4698 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4699 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004700 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004701 return;
4702 }
4703
Pekka Paalanene67858b2013-04-25 13:57:42 +03004704 /* make sure the parent is in its own list */
4705 if (wl_list_empty(&parent->subsurface_list)) {
4706 if (!weston_subsurface_create_for_parent(parent)) {
4707 wl_resource_post_no_memory(resource);
4708 return;
4709 }
4710 }
4711
4712 sub = weston_subsurface_create(id, surface, parent);
4713 if (!sub) {
4714 wl_resource_post_no_memory(resource);
4715 return;
4716 }
4717
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004718 surface->committed = subsurface_committed;
4719 surface->committed_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004720 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004721}
4722
4723static void
4724subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
4725{
4726 wl_resource_destroy(resource);
4727}
4728
4729static const struct wl_subcompositor_interface subcompositor_interface = {
4730 subcompositor_destroy,
4731 subcompositor_get_subsurface
4732};
4733
4734static void
4735bind_subcompositor(struct wl_client *client,
4736 void *data, uint32_t version, uint32_t id)
4737{
4738 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004739 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004740
Jason Ekstranda85118c2013-06-27 20:17:02 -05004741 resource =
4742 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004743 if (resource == NULL) {
4744 wl_client_post_no_memory(client);
4745 return;
4746 }
4747 wl_resource_set_implementation(resource, &subcompositor_interface,
4748 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004749}
4750
Bryce Harrington0795ece2016-08-30 12:04:26 -07004751/** Set a DPMS mode on all of the compositor's outputs
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004752 *
4753 * \param compositor The compositor instance
4754 * \param state The DPMS state the outputs will be set to
4755 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004756static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004757weston_compositor_dpms(struct weston_compositor *compositor,
4758 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004759{
4760 struct weston_output *output;
4761
Bryce Harrington08976ac2016-08-30 12:05:16 -07004762 wl_list_for_each(output, &compositor->output_list, link)
4763 if (output->set_dpms)
4764 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004765}
4766
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004767/** Restores the compositor to active status
4768 *
4769 * \param compositor The compositor instance
4770 *
4771 * If the compositor was in a sleeping mode, all outputs are powered
4772 * back on via DPMS. Otherwise if the compositor was inactive
4773 * (idle/locked, offscreen, or sleeping) then the compositor's wake
4774 * signal will fire.
4775 *
4776 * Restarts the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004777 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004778 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004779WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004780weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004781{
Neil Roberts8b62e202013-09-30 13:14:47 +01004782 uint32_t old_state = compositor->state;
4783
4784 /* The state needs to be changed before emitting the wake
4785 * signal because that may try to schedule a repaint which
4786 * will not work if the compositor is still sleeping */
4787 compositor->state = WESTON_COMPOSITOR_ACTIVE;
4788
4789 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004790 case WESTON_COMPOSITOR_SLEEPING:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004791 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004792 case WESTON_COMPOSITOR_OFFSCREEN:
Daniel Stone893b9362016-11-08 15:47:09 +00004793 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004794 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004795 /* fall through */
4796 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004797 wl_event_source_timer_update(compositor->idle_source,
4798 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004799 }
4800}
4801
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004802/** Turns off rendering and frame events for the compositor.
4803 *
4804 * \param compositor The compositor instance
4805 *
4806 * This is used for example to prevent further rendering while the
4807 * compositor is shutting down.
4808 *
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004809 * Stops the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004810 *
4811 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004812 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004813WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004814weston_compositor_offscreen(struct weston_compositor *compositor)
4815{
4816 switch (compositor->state) {
4817 case WESTON_COMPOSITOR_OFFSCREEN:
4818 return;
4819 case WESTON_COMPOSITOR_SLEEPING:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004820 default:
4821 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4822 wl_event_source_timer_update(compositor->idle_source, 0);
4823 }
4824}
4825
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004826/** Powers down all attached output devices
4827 *
4828 * \param compositor The compositor instance
4829 *
4830 * Causes rendering to the outputs to cease, and no frame events to be
4831 * sent. Only powers down the outputs if the compositor is not already
4832 * in sleep mode.
4833 *
4834 * Stops the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004835 *
4836 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004837 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004838WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004839weston_compositor_sleep(struct weston_compositor *compositor)
4840{
4841 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
4842 return;
4843
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004844 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004845 compositor->state = WESTON_COMPOSITOR_SLEEPING;
4846 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
4847}
4848
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004849/** Sets compositor to idle mode
4850 *
4851 * \param data The compositor instance
4852 *
4853 * This is called when the idle timer fires. Once the compositor is in
4854 * idle mode it requires a wake action (e.g. via
4855 * weston_compositor_wake()) to restore it. The compositor's
4856 * idle_signal will be triggered when the idle event occurs.
4857 *
4858 * Idleness can be inhibited by setting the compositor's idle_inhibit
4859 * property.
4860 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004861static int
4862idle_handler(void *data)
4863{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004864 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004865
4866 if (compositor->idle_inhibit)
4867 return 1;
4868
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004869 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004870 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004871
4872 return 1;
4873}
4874
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004875WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08004876weston_plane_init(struct weston_plane *plane,
4877 struct weston_compositor *ec,
4878 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004879{
4880 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004881 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004882 plane->x = x;
4883 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08004884 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004885
4886 /* Init the link so that the call to wl_list_remove() when releasing
4887 * the plane without ever stacking doesn't lead to a crash */
4888 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004889}
4890
4891WL_EXPORT void
4892weston_plane_release(struct weston_plane *plane)
4893{
Xiong Zhang97116532013-10-23 13:58:31 +08004894 struct weston_view *view;
4895
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004896 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004897 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004898
Xiong Zhang97116532013-10-23 13:58:31 +08004899 wl_list_for_each(view, &plane->compositor->view_list, link) {
4900 if (view->plane == plane)
4901 view->plane = NULL;
4902 }
4903
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004904 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004905}
4906
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004907/** weston_compositor_stack_plane
4908 * \ingroup compositor
4909 */
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004910WL_EXPORT void
4911weston_compositor_stack_plane(struct weston_compositor *ec,
4912 struct weston_plane *plane,
4913 struct weston_plane *above)
4914{
4915 if (above)
4916 wl_list_insert(above->link.prev, &plane->link);
4917 else
4918 wl_list_insert(&ec->plane_list, &plane->link);
4919}
4920
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004921static void
4922output_release(struct wl_client *client, struct wl_resource *resource)
4923{
4924 wl_resource_destroy(resource);
4925}
4926
4927static const struct wl_output_interface output_interface = {
4928 output_release,
4929};
4930
4931
Casey Dahlin9074db52012-04-19 22:50:09 -04004932static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004933{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004934 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004935}
4936
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004937static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004938bind_output(struct wl_client *client,
4939 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05004940{
Pekka Paalanen05347622017-03-27 12:24:34 +03004941 struct weston_head *head = data;
4942 struct weston_output *output = head->output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004943 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004944 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004945
Jason Ekstranda85118c2013-06-27 20:17:02 -05004946 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004947 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004948 if (resource == NULL) {
4949 wl_client_post_no_memory(client);
4950 return;
4951 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004952
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03004953 wl_list_insert(&head->resource_list, wl_resource_get_link(resource));
Pekka Paalanen055c1132017-03-27 16:31:25 +03004954 wl_resource_set_implementation(resource, &output_interface, head,
Pekka Paalanen05347622017-03-27 12:24:34 +03004955 unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04004956
Pekka Paalanen05347622017-03-27 12:24:34 +03004957 assert(output);
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004958 wl_output_send_geometry(resource,
4959 output->x,
4960 output->y,
Pekka Paalanen01f60212017-03-24 15:39:24 +02004961 head->mm_width,
4962 head->mm_height,
4963 head->subpixel,
4964 head->make, head->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04004965 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004966 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004967 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02004968 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004969
4970 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004971 wl_output_send_mode(resource,
4972 mode->flags,
4973 mode->width,
4974 mode->height,
4975 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004976 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02004977
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004978 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004979 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004980}
4981
Pekka Paalanendcac3512017-12-08 14:13:34 +02004982static void
4983weston_head_add_global(struct weston_head *head)
4984{
4985 head->global = wl_global_create(head->compositor->wl_display,
4986 &wl_output_interface, 3,
4987 head, bind_output);
4988}
4989
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02004990/** Remove the global wl_output protocol object
4991 *
4992 * \param head The head whose global to remove.
4993 *
4994 * Also orphans the wl_resources for this head (wl_output).
4995 */
4996static void
4997weston_head_remove_global(struct weston_head *head)
4998{
4999 struct wl_resource *resource, *tmp;
5000
5001 if (head->global)
5002 wl_global_destroy(head->global);
5003 head->global = NULL;
5004
5005 wl_resource_for_each_safe(resource, tmp, &head->resource_list) {
5006 unbind_resource(resource);
5007 wl_resource_set_destructor(resource, NULL);
5008 wl_resource_set_user_data(resource, NULL);
5009 }
Roman Gilge97391c2019-03-29 13:01:06 +01005010
5011 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
5012 /* It's sufficient to unset the destructor, then the list elements
5013 * won't be accessed.
5014 */
5015 wl_resource_set_destructor(resource, NULL);
5016 }
5017 wl_list_init(&head->xdg_output_resource_list);
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02005018}
5019
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005020/** Get the backing object of wl_output
5021 *
5022 * \param resource A wl_output protocol object.
5023 * \return The backing object (user data) of a wl_resource representing a
5024 * wl_output protocol object.
Marius Vlad78984ee2019-06-11 00:05:08 +03005025 *
5026 * \ingroup head
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005027 */
Pekka Paalanen055c1132017-03-27 16:31:25 +03005028WL_EXPORT struct weston_head *
5029weston_head_from_resource(struct wl_resource *resource)
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005030{
5031 assert(wl_resource_instance_of(resource, &wl_output_interface,
5032 &output_interface));
5033
5034 return wl_resource_get_user_data(resource);
5035}
5036
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005037/** Initialize a pre-allocated weston_head
5038 *
5039 * \param head The head to initialize.
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005040 * \param name The head name, e.g. the connector name or equivalent.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005041 *
5042 * The head will be safe to attach, detach and release.
5043 *
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005044 * The name is used in logs, and can be used by compositors as a configuration
5045 * identifier.
5046 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005047 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005048 * \internal
5049 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005050WL_EXPORT void
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005051weston_head_init(struct weston_head *head, const char *name)
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005052{
5053 /* Add some (in)sane defaults which can be used
5054 * for checking if an output was properly configured
5055 */
5056 memset(head, 0, sizeof *head);
5057
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005058 wl_list_init(&head->compositor_link);
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005059 wl_signal_init(&head->destroy_signal);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005060 wl_list_init(&head->output_link);
5061 wl_list_init(&head->resource_list);
Roman Gilge97391c2019-03-29 13:01:06 +01005062 wl_list_init(&head->xdg_output_resource_list);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005063 head->name = strdup(name);
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305064 head->current_protection = WESTON_HDCP_DISABLE;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005065}
5066
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005067/** Send output heads changed signal
5068 *
5069 * \param output The output that changed.
5070 *
5071 * Notify that the enabled output gained and/or lost heads, or that the
5072 * associated heads may have changed their connection status. This does not
5073 * include cases where the output becomes enabled or disabled. The registered
5074 * callbacks are called after the change has successfully happened.
5075 *
5076 * If connection status change causes the compositor to attach or detach a head
5077 * to an enabled output, the registered callbacks may be called multiple times.
Marius Vlad55d87362019-06-11 01:15:35 +03005078 *
5079 * \ingroup output
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005080 */
5081static void
5082weston_output_emit_heads_changed(struct weston_output *output)
5083{
5084 wl_signal_emit(&output->compositor->output_heads_changed_signal,
5085 output);
5086}
5087
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005088/** Idle task for emitting heads_changed_signal */
5089static void
5090weston_compositor_call_heads_changed(void *data)
5091{
5092 struct weston_compositor *compositor = data;
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005093 struct weston_head *head;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005094
5095 compositor->heads_changed_source = NULL;
5096
5097 wl_signal_emit(&compositor->heads_changed_signal, compositor);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005098
5099 wl_list_for_each(head, &compositor->head_list, compositor_link) {
5100 if (head->output && head->output->enabled)
5101 weston_output_emit_heads_changed(head->output);
5102 }
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005103}
5104
5105/** Schedule a call on idle to heads_changed callback
5106 *
5107 * \param compositor The Compositor.
5108 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005109 * \ingroup compositor
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005110 * \internal
5111 */
5112static void
5113weston_compositor_schedule_heads_changed(struct weston_compositor *compositor)
5114{
5115 struct wl_event_loop *loop;
5116
5117 if (compositor->heads_changed_source)
5118 return;
5119
5120 loop = wl_display_get_event_loop(compositor->wl_display);
5121 compositor->heads_changed_source = wl_event_loop_add_idle(loop,
5122 weston_compositor_call_heads_changed, compositor);
5123}
5124
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005125/** Register a new head
5126 *
5127 * \param compositor The compositor.
5128 * \param head The head to register, must not be already registered.
5129 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005130 * This signals the core that a new head has become available, leading to
5131 * heads_changed hook being called later.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005132 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005133 * \ingroup compositor
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005134 * \internal
5135 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005136WL_EXPORT void
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005137weston_compositor_add_head(struct weston_compositor *compositor,
5138 struct weston_head *head)
5139{
5140 assert(wl_list_empty(&head->compositor_link));
5141 assert(head->name);
5142
5143 wl_list_insert(compositor->head_list.prev, &head->compositor_link);
5144 head->compositor = compositor;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005145 weston_compositor_schedule_heads_changed(compositor);
5146}
5147
5148/** Adds a listener to be called when heads change
5149 *
5150 * \param compositor The compositor.
5151 * \param listener The listener to add.
5152 *
Marius Vlada2dace22019-06-12 16:05:44 +03005153 * The listener notify function argument is weston_compositor.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005154 *
5155 * The listener function will be called after heads are added or their
5156 * connection status has changed. Several changes may be accumulated into a
5157 * single call. The user is expected to iterate over the existing heads and
5158 * check their statuses to find out what changed.
5159 *
5160 * \sa weston_compositor_iterate_heads, weston_head_is_connected,
5161 * weston_head_is_enabled
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005162 * \ingroup compositor
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005163 */
5164WL_EXPORT void
5165weston_compositor_add_heads_changed_listener(struct weston_compositor *compositor,
5166 struct wl_listener *listener)
5167{
5168 wl_signal_add(&compositor->heads_changed_signal, listener);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005169}
5170
5171/** Iterate over available heads
5172 *
5173 * \param compositor The compositor.
Marius Vlada2dace22019-06-12 16:05:44 +03005174 * \param iter The iterator, or NULL for start.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005175 * \return The next available head in the list.
5176 *
5177 * Returns all available heads, regardless of being connected or enabled.
5178 *
5179 * You can iterate over all heads as follows:
5180 * \code
5181 * struct weston_head *head = NULL;
5182 *
5183 * while ((head = weston_compositor_iterate_heads(compositor, head))) {
5184 * ...
5185 * }
5186 * \endcode
5187 *
5188 * If you cause \c iter to be removed from the list, you cannot use it to
5189 * continue iterating. Removing any other item is safe.
5190 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005191 * \ingroup compositor
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005192 */
5193WL_EXPORT struct weston_head *
5194weston_compositor_iterate_heads(struct weston_compositor *compositor,
5195 struct weston_head *iter)
5196{
5197 struct wl_list *list = &compositor->head_list;
5198 struct wl_list *node;
5199
5200 assert(compositor);
5201 assert(!iter || iter->compositor == compositor);
5202
5203 if (iter)
5204 node = iter->compositor_link.next;
5205 else
5206 node = list->next;
5207
5208 assert(node);
5209 assert(!iter || node != &iter->compositor_link);
5210
5211 if (node == list)
5212 return NULL;
5213
5214 return container_of(node, struct weston_head, compositor_link);
5215}
5216
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005217/** Iterate over attached heads
5218 *
5219 * \param output The output whose heads to iterate.
Marius Vlada2dace22019-06-12 16:05:44 +03005220 * \param iter The iterator, or NULL for start.
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005221 * \return The next attached head in the list.
5222 *
5223 * Returns all heads currently attached to the output.
5224 *
5225 * You can iterate over all heads as follows:
5226 * \code
5227 * struct weston_head *head = NULL;
5228 *
5229 * while ((head = weston_output_iterate_heads(output, head))) {
5230 * ...
5231 * }
5232 * \endcode
5233 *
5234 * If you cause \c iter to be removed from the list, you cannot use it to
5235 * continue iterating. Removing any other item is safe.
5236 *
Marius Vlad55d87362019-06-11 01:15:35 +03005237 * \ingroup ouput
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005238 */
5239WL_EXPORT struct weston_head *
5240weston_output_iterate_heads(struct weston_output *output,
5241 struct weston_head *iter)
5242{
5243 struct wl_list *list = &output->head_list;
5244 struct wl_list *node;
5245
5246 assert(output);
5247 assert(!iter || iter->output == output);
5248
5249 if (iter)
5250 node = iter->output_link.next;
5251 else
5252 node = list->next;
5253
5254 assert(node);
5255 assert(!iter || node != &iter->output_link);
5256
5257 if (node == list)
5258 return NULL;
5259
5260 return container_of(node, struct weston_head, output_link);
5261}
5262
Pekka Paalanendcac3512017-12-08 14:13:34 +02005263/** Attach a head to an output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005264 *
5265 * \param output The output to attach to.
5266 * \param head The head that is not yet attached.
5267 * \return 0 on success, -1 on failure.
5268 *
5269 * Attaches the given head to the output. All heads of an output are clones
5270 * and share the resolution and timings.
5271 *
5272 * Cloning heads this way uses less resources than creating an output for
5273 * each head, but is not always possible due to environment, driver and hardware
5274 * limitations.
5275 *
5276 * On failure, the head remains unattached. Success of this function does not
5277 * guarantee the output configuration is actually valid. The final checks are
Pekka Paalanendcac3512017-12-08 14:13:34 +02005278 * made on weston_output_enable() unless the output was already enabled.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005279 *
Marius Vlad55d87362019-06-11 01:15:35 +03005280 * \ingroup output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005281 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005282WL_EXPORT int
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005283weston_output_attach_head(struct weston_output *output,
5284 struct weston_head *head)
5285{
Pekka Paalanendcac3512017-12-08 14:13:34 +02005286 char *head_names;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005287
5288 if (!wl_list_empty(&head->output_link))
5289 return -1;
5290
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005291 if (output->attach_head) {
5292 if (output->attach_head(output, head) < 0)
5293 return -1;
5294 } else if (!wl_list_empty(&output->head_list)) {
5295 /* No support for clones in the legacy path. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005296 return -1;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005297 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005298
5299 head->output = output;
5300 wl_list_insert(output->head_list.prev, &head->output_link);
5301
Pekka Paalanendcac3512017-12-08 14:13:34 +02005302 if (output->enabled) {
5303 weston_head_add_global(head);
5304
5305 head_names = weston_output_create_heads_string(output);
5306 weston_log("Output '%s' updated to have head(s) %s\n",
5307 output->name, head_names);
5308 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005309
5310 weston_output_emit_heads_changed(output);
Pekka Paalanendcac3512017-12-08 14:13:34 +02005311 }
5312
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005313 return 0;
5314}
5315
5316/** Detach a head from its output
5317 *
5318 * \param head The head to detach.
5319 *
5320 * It is safe to detach a non-attached head.
5321 *
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005322 * If the head is attached to an enabled output and the output will be left
5323 * with no heads, the output will be disabled.
5324 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005325 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005326 * \sa weston_output_disable
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005327 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005328WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005329weston_head_detach(struct weston_head *head)
5330{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005331 struct weston_output *output = head->output;
Pekka Paalanena0106992017-12-08 16:11:17 +02005332 char *head_names;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005333
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005334 wl_list_remove(&head->output_link);
5335 wl_list_init(&head->output_link);
5336 head->output = NULL;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005337
5338 if (!output)
5339 return;
5340
5341 if (output->detach_head)
5342 output->detach_head(output, head);
5343
5344 if (output->enabled) {
5345 weston_head_remove_global(head);
5346
Pekka Paalanena0106992017-12-08 16:11:17 +02005347 if (wl_list_empty(&output->head_list)) {
5348 weston_log("Output '%s' no heads left, disabling.\n",
5349 output->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005350 weston_output_disable(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02005351 } else {
5352 head_names = weston_output_create_heads_string(output);
5353 weston_log("Output '%s' updated to have head(s) %s\n",
5354 output->name, head_names);
5355 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005356
5357 weston_output_emit_heads_changed(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02005358 }
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005359 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005360}
5361
5362/** Destroy a head
5363 *
5364 * \param head The head to be released.
5365 *
5366 * Destroys the head. The caller is responsible for freeing the memory pointed
5367 * to by \c head.
5368 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005369 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005370 * \internal
5371 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005372WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005373weston_head_release(struct weston_head *head)
5374{
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005375 wl_signal_emit(&head->destroy_signal, head);
5376
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005377 weston_head_detach(head);
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005378
5379 free(head->make);
5380 free(head->model);
5381 free(head->serial_number);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005382 free(head->name);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005383
5384 wl_list_remove(&head->compositor_link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005385}
5386
Pekka Paalanene19970f2017-08-28 14:11:02 +03005387static void
5388weston_head_set_device_changed(struct weston_head *head)
5389{
5390 head->device_changed = true;
5391
5392 if (head->compositor)
5393 weston_compositor_schedule_heads_changed(head->compositor);
5394}
5395
5396/** String equal comparison with NULLs being equal */
5397static bool
5398str_null_eq(const char *a, const char *b)
5399{
5400 if (!a && !b)
5401 return true;
5402
5403 if (!!a != !!b)
5404 return false;
5405
5406 return strcmp(a, b) == 0;
5407}
5408
Pekka Paalanen01f60212017-03-24 15:39:24 +02005409/** Store monitor make, model and serial number
5410 *
5411 * \param head The head to modify.
5412 * \param make The monitor make. If EDID is available, the PNP ID. Otherwise
5413 * any string, or NULL for none.
5414 * \param model The monitor model or name, or a made-up string, or NULL for
5415 * none.
5416 * \param serialno The monitor serial number, a made-up string, or NULL for
5417 * none.
5418 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005419 * This may set the device_changed flag.
5420 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005421 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005422 * \internal
5423 */
5424WL_EXPORT void
5425weston_head_set_monitor_strings(struct weston_head *head,
5426 const char *make,
5427 const char *model,
5428 const char *serialno)
5429{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005430 if (str_null_eq(head->make, make) &&
5431 str_null_eq(head->model, model) &&
5432 str_null_eq(head->serial_number, serialno))
5433 return;
5434
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005435 free(head->make);
5436 free(head->model);
5437 free(head->serial_number);
5438
5439 head->make = make ? strdup(make) : NULL;
5440 head->model = model ? strdup(model) : NULL;
5441 head->serial_number = serialno ? strdup(serialno) : NULL;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005442
5443 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005444}
5445
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005446/** Store display non-desktop status
5447 *
5448 * \param head The head to modify.
5449 * \param non_desktop Whether the head connects to a non-desktop display.
5450 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005451 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005452 * \internal
5453 */
5454WL_EXPORT void
5455weston_head_set_non_desktop(struct weston_head *head, bool non_desktop)
5456{
5457 if (head->non_desktop == non_desktop)
5458 return;
5459
5460 head->non_desktop = non_desktop;
5461
5462 weston_head_set_device_changed(head);
5463}
5464
Lucas Stacha69cb712019-11-25 23:29:31 +00005465/** Store display transformation
5466 *
5467 * \param head The head to modify.
5468 * \param transform The transformation to apply for this head
5469 *
5470 * This may set the device_changed flag.
5471 *
5472 * \ingroup head
5473 * \internal
5474 */
5475WL_EXPORT void
5476weston_head_set_transform(struct weston_head *head, uint32_t transform)
5477{
5478 if (head->transform == transform)
5479 return;
5480
5481 head->transform = transform;
5482
5483 weston_head_set_device_changed(head);
5484}
5485
5486
Pekka Paalanen01f60212017-03-24 15:39:24 +02005487/** Store physical image size
5488 *
5489 * \param head The head to modify.
5490 * \param mm_width Image area width in millimeters.
5491 * \param mm_height Image area height in millimeters.
5492 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005493 * This may set the device_changed flag.
5494 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005495 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005496 * \internal
5497 */
5498WL_EXPORT void
5499weston_head_set_physical_size(struct weston_head *head,
5500 int32_t mm_width, int32_t mm_height)
5501{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005502 if (head->mm_width == mm_width &&
5503 head->mm_height == mm_height)
5504 return;
5505
Pekka Paalanen01f60212017-03-24 15:39:24 +02005506 head->mm_width = mm_width;
5507 head->mm_height = mm_height;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005508
5509 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005510}
5511
5512/** Store monitor sub-pixel layout
5513 *
5514 * \param head The head to modify.
5515 * \param sp Sub-pixel layout. The possible values are:
5516 * - WL_OUTPUT_SUBPIXEL_UNKNOWN,
5517 * - WL_OUTPUT_SUBPIXEL_NONE,
5518 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB,
5519 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR,
5520 * - WL_OUTPUT_SUBPIXEL_VERTICAL_RGB,
5521 * - WL_OUTPUT_SUBPIXEL_VERTICAL_BGR
5522 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005523 * This may set the device_changed flag.
5524 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005525 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005526 * \internal
5527 */
5528WL_EXPORT void
5529weston_head_set_subpixel(struct weston_head *head,
5530 enum wl_output_subpixel sp)
5531{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005532 if (head->subpixel == sp)
5533 return;
5534
Pekka Paalanen01f60212017-03-24 15:39:24 +02005535 head->subpixel = sp;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005536
5537 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005538}
5539
5540/** Mark the monitor as internal
5541 *
5542 * This is used for embedded screens, like laptop panels.
5543 *
5544 * \param head The head to mark as internal.
5545 *
5546 * By default a head is external. The type is often inferred from the physical
5547 * connector type.
5548 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005549 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005550 * \internal
5551 */
5552WL_EXPORT void
5553weston_head_set_internal(struct weston_head *head)
5554{
5555 head->connection_internal = true;
5556}
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005557
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005558/** Store connector status
5559 *
5560 * \param head The head to modify.
5561 * \param connected Whether the head is connected.
5562 *
5563 * Connectors are created as disconnected. This function can be used to
5564 * set the connector status.
5565 *
5566 * The status should be set to true when a physical connector is connected to
5567 * a video sink device like a monitor and to false when the connector is
5568 * disconnected. For nested backends, the connection status should reflect the
5569 * connection to the parent display server.
5570 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005571 * When the connection status changes, it schedules a call to the heads_changed
Pekka Paalanene19970f2017-08-28 14:11:02 +03005572 * hook and sets the device_changed flag.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005573 *
5574 * \sa weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005575 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005576 * \internal
5577 */
5578WL_EXPORT void
5579weston_head_set_connection_status(struct weston_head *head, bool connected)
5580{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005581 if (head->connected == connected)
5582 return;
5583
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005584 head->connected = connected;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005585
Pekka Paalanene19970f2017-08-28 14:11:02 +03005586 weston_head_set_device_changed(head);
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005587}
5588
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305589static void
5590weston_output_compute_protection(struct weston_output *output)
5591{
5592 struct weston_head *head;
5593 enum weston_hdcp_protection op_protection;
5594 bool op_protection_valid = false;
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305595 struct weston_compositor *wc = output->compositor;
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305596
5597 wl_list_for_each(head, &output->head_list, output_link) {
5598 if (!op_protection_valid) {
5599 op_protection = head->current_protection;
5600 op_protection_valid = true;
5601 }
5602 if (head->current_protection < op_protection)
5603 op_protection = head->current_protection;
5604 }
5605
5606 if (!op_protection_valid)
5607 op_protection = WESTON_HDCP_DISABLE;
5608
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305609 if (output->current_protection != op_protection) {
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305610 output->current_protection = op_protection;
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305611 weston_output_damage(output);
Ankit Nautiyalfaa5ab42019-07-08 13:23:24 +05305612 weston_schedule_surface_protection_update(wc);
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305613 }
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305614}
5615
5616WL_EXPORT void
5617weston_head_set_content_protection_status(struct weston_head *head,
5618 enum weston_hdcp_protection status)
5619{
5620 head->current_protection = status;
5621 if (head->output)
5622 weston_output_compute_protection(head->output);
5623}
5624
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005625/** Is the head currently connected?
5626 *
5627 * \param head The head to query.
5628 * \return Connection status.
5629 *
5630 * Returns true if the head is physically connected to a monitor, or in
5631 * case of a nested backend returns true when there is a connection to the
5632 * parent display server.
5633 *
5634 * This is independent from the head being enabled.
5635 *
5636 * \sa weston_head_is_enabled
Marius Vlad78984ee2019-06-11 00:05:08 +03005637 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005638 */
5639WL_EXPORT bool
5640weston_head_is_connected(struct weston_head *head)
5641{
5642 return head->connected;
5643}
5644
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005645/** Is the head currently enabled?
5646 *
5647 * \param head The head to query.
5648 * \return Video status.
5649 *
5650 * Returns true if the head is currently transmitting a video stream.
5651 *
5652 * This is independent of the head being connected.
5653 *
5654 * \sa weston_head_is_connected
Marius Vlad78984ee2019-06-11 00:05:08 +03005655 * \ingroup head
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005656 */
5657WL_EXPORT bool
5658weston_head_is_enabled(struct weston_head *head)
5659{
5660 if (!head->output)
5661 return false;
5662
5663 return head->output->enabled;
5664}
5665
Pekka Paalanene19970f2017-08-28 14:11:02 +03005666/** Has the device information changed?
5667 *
5668 * \param head The head to query.
5669 * \return True if the device information has changed since last reset.
5670 *
5671 * The information about the connected display device, e.g. a monitor, may
5672 * change without being disconnected in between. Changing information
5673 * causes a call to the heads_changed hook.
5674 *
5675 * The information includes make, model, serial number, physical size,
5676 * and sub-pixel type. The connection status is also included.
5677 *
5678 * \sa weston_head_reset_device_changed, weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005679 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03005680 */
5681WL_EXPORT bool
5682weston_head_is_device_changed(struct weston_head *head)
5683{
5684 return head->device_changed;
5685}
5686
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005687/** Does the head represent a non-desktop display?
5688 *
5689 * \param head The head to query.
5690 * \return True if the device is a non-desktop display.
5691 *
5692 * Non-desktop heads are not attached to outputs by default.
5693 * This stops weston from extending the desktop onto head mounted displays.
5694 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005695 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005696 */
5697WL_EXPORT bool
5698weston_head_is_non_desktop(struct weston_head *head)
5699{
5700 return head->non_desktop;
5701}
5702
Pekka Paalanene19970f2017-08-28 14:11:02 +03005703/** Acknowledge device information change
5704 *
5705 * \param head The head to acknowledge.
5706 *
5707 * Clears the device changed flag on this head. When a compositor has processed
5708 * device information, it should call this to be able to notice further
5709 * changes.
5710 *
5711 * \sa weston_head_is_device_changed
Marius Vlad78984ee2019-06-11 00:05:08 +03005712 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03005713 */
5714WL_EXPORT void
5715weston_head_reset_device_changed(struct weston_head *head)
5716{
5717 head->device_changed = false;
5718}
5719
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005720/** Get the name of a head
5721 *
5722 * \param head The head to query.
5723 * \return The head's name, not NULL.
5724 *
5725 * The name depends on the backend. The DRM backend uses connector names,
5726 * other backends may use hardcoded names or user-given names.
Marius Vlad78984ee2019-06-11 00:05:08 +03005727 *
5728 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005729 */
5730WL_EXPORT const char *
5731weston_head_get_name(struct weston_head *head)
5732{
5733 return head->name;
5734}
5735
5736/** Get the output the head is attached to
5737 *
5738 * \param head The head to query.
5739 * \return The output the head is attached to, or NULL if detached.
Marius Vlad78984ee2019-06-11 00:05:08 +03005740 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005741 */
5742WL_EXPORT struct weston_output *
5743weston_head_get_output(struct weston_head *head)
5744{
5745 return head->output;
5746}
5747
Lucas Stacha69cb712019-11-25 23:29:31 +00005748/** Get the head's native transformation
5749 *
5750 * \param head The head to query.
5751 * \return The head's native transform, as a WL_OUTPUT_TRANSFORM_* value
5752 *
5753 * A weston_head may have a 'native' transform provided by the backend.
5754 * Examples include panels which are physically rotated, where the rotation
5755 * is recorded and described as part of the system configuration. This call
5756 * will return any known native transform for the head.
5757 *
5758 * \ingroup head
5759 */
5760WL_EXPORT uint32_t
5761weston_head_get_transform(struct weston_head *head)
5762{
5763 return head->transform;
5764}
5765
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005766/** Add destroy callback for a head
5767 *
5768 * \param head The head to watch for.
5769 * \param listener The listener to add. The \c notify member must be set.
5770 *
5771 * Heads may get destroyed for various reasons by the backends. If a head is
5772 * attached to an output, the compositor should listen for head destruction
5773 * and reconfigure or destroy the output if necessary.
5774 *
5775 * The destroy callbacks will be called on weston_head destruction before any
5776 * automatic detaching from an associated weston_output and before any
5777 * weston_head information is lost.
5778 *
5779 * The \c data argument to the notify callback is the weston_head being
5780 * destroyed.
Marius Vlad78984ee2019-06-11 00:05:08 +03005781 *
5782 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005783 */
5784WL_EXPORT void
5785weston_head_add_destroy_listener(struct weston_head *head,
5786 struct wl_listener *listener)
5787{
5788 wl_signal_add(&head->destroy_signal, listener);
5789}
5790
5791/** Look up destroy listener for a head
5792 *
5793 * \param head The head to query.
5794 * \param notify The notify function used used for the added destroy listener.
5795 * \return The listener, or NULL if not found.
5796 *
5797 * This looks up the previously added destroy listener struct based on the
5798 * notify function it has. The listener can be used to access user data
5799 * through \c container_of().
5800 *
5801 * \sa wl_signal_get()
Marius Vlad78984ee2019-06-11 00:05:08 +03005802 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005803 */
5804WL_EXPORT struct wl_listener *
5805weston_head_get_destroy_listener(struct weston_head *head,
5806 wl_notify_func_t notify)
5807{
5808 return wl_signal_get(&head->destroy_signal, notify);
5809}
5810
David Fort0de859e2016-05-27 23:22:57 +02005811/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005812static void
David Fort0de859e2016-05-27 23:22:57 +02005813weston_compositor_reflow_outputs(struct weston_compositor *compositor,
5814 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005815{
5816 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02005817 bool start_resizing = false;
5818
5819 if (!delta_width)
5820 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005821
5822 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02005823 if (output == resized_output) {
5824 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005825 continue;
5826 }
5827
David Fort0de859e2016-05-27 23:22:57 +02005828 if (start_resizing) {
5829 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005830 output->dirty = 1;
5831 }
5832 }
5833}
5834
Pekka Paalanend72bad22017-03-29 17:01:41 +03005835static void
Scott Moreauccbf29d2012-02-22 14:21:41 -07005836weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005837{
Scott Moreau850ca422012-05-21 15:21:25 -06005838 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05005839
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005840 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005841 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06005842
Scott Moreauccbf29d2012-02-22 14:21:41 -07005843 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06005844 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005845 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005846 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005847 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005848 weston_matrix_scale(&output->matrix, magnification,
5849 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005850 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005851
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005852 switch (output->transform) {
5853 case WL_OUTPUT_TRANSFORM_FLIPPED:
5854 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5855 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5856 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5857 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5858 weston_matrix_scale(&output->matrix, -1, 1, 1);
5859 break;
5860 }
5861
5862 switch (output->transform) {
5863 default:
5864 case WL_OUTPUT_TRANSFORM_NORMAL:
5865 case WL_OUTPUT_TRANSFORM_FLIPPED:
5866 break;
5867 case WL_OUTPUT_TRANSFORM_90:
5868 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +02005869 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5870 weston_matrix_rotate_xy(&output->matrix, 0, -1);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005871 break;
5872 case WL_OUTPUT_TRANSFORM_180:
5873 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5874 weston_matrix_translate(&output->matrix,
5875 -output->width, -output->height, 0);
5876 weston_matrix_rotate_xy(&output->matrix, -1, 0);
5877 break;
5878 case WL_OUTPUT_TRANSFORM_270:
5879 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +02005880 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
5881 weston_matrix_rotate_xy(&output->matrix, 0, 1);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005882 break;
5883 }
5884
5885 if (output->current_scale != 1)
5886 weston_matrix_scale(&output->matrix,
5887 output->current_scale,
5888 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01005889
Scott Moreauccbf29d2012-02-22 14:21:41 -07005890 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05005891
5892 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005893}
5894
Scott Moreau1bad5db2012-08-18 01:04:05 -06005895static void
Alexander Larsson0b135062013-05-28 16:23:36 +02005896weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06005897{
5898 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005899 output->native_scale = scale;
5900 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06005901
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005902 convert_size_by_transform_scale(&output->width, &output->height,
5903 output->current_mode->width,
5904 output->current_mode->height,
5905 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02005906}
5907
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005908static void
5909weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07005910{
5911 output->x = x;
5912 output->y = y;
5913
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005914 pixman_region32_fini(&output->region);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005915 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06005916 output->width,
5917 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005918}
5919
Marius Vlad55d87362019-06-11 01:15:35 +03005920/**
5921 * \ingroup output
5922 */
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005923WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005924weston_output_move(struct weston_output *output, int x, int y)
5925{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005926 struct weston_head *head;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005927 struct wl_resource *resource;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005928 int ver;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005929
5930 output->move_x = x - output->x;
5931 output->move_y = y - output->y;
5932
5933 if (output->move_x == 0 && output->move_y == 0)
5934 return;
5935
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005936 weston_output_init_geometry(output, x, y);
5937
5938 output->dirty = 1;
5939
5940 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005941 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005942
5943 /* Notify clients of the change for output position. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005944 wl_list_for_each(head, &output->head_list, output_link) {
5945 wl_resource_for_each(resource, &head->resource_list) {
5946 wl_output_send_geometry(resource,
5947 output->x,
5948 output->y,
5949 head->mm_width,
5950 head->mm_height,
5951 head->subpixel,
5952 head->make,
5953 head->model,
5954 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08005955
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005956 ver = wl_resource_get_version(resource);
5957 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
5958 wl_output_send_done(resource);
5959 }
Roman Gilge97391c2019-03-29 13:01:06 +01005960
5961 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
5962 zxdg_output_v1_send_logical_position(resource,
5963 output->x,
5964 output->y);
5965 zxdg_output_v1_send_done(resource);
5966 }
Quanxian Wangb2c86362014-03-14 09:16:25 +08005967 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005968}
5969
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005970/** Signal that a pending output is taken into use.
5971 *
5972 * Removes the output from the pending list and adds it to the compositor's
5973 * list of enabled outputs. The output created signal is emitted.
Giulio Camuffob1147152015-05-06 21:41:57 +03005974 *
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005975 * The output gets an internal ID assigned, and the wl_output global is
5976 * created.
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005977 *
Giulio Camuffob1147152015-05-06 21:41:57 +03005978 * \param compositor The compositor instance.
5979 * \param output The output to be added.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005980 *
5981 * \internal
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005982 * \ingroup compositor
Giulio Camuffob1147152015-05-06 21:41:57 +03005983 */
Pekka Paalanenf9681b52017-03-29 16:58:48 +03005984static void
Giulio Camuffob1147152015-05-06 21:41:57 +03005985weston_compositor_add_output(struct weston_compositor *compositor,
5986 struct weston_output *output)
5987{
Armin Krezoviće5403842016-08-05 15:28:29 +02005988 struct weston_view *view, *next;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005989 struct weston_head *head;
Armin Krezoviće5403842016-08-05 15:28:29 +02005990
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005991 assert(!output->enabled);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005992
5993 /* Verify we haven't reached the limit of 32 available output IDs */
5994 assert(ffs(~compositor->output_id_pool) > 0);
5995
5996 /* Invert the output id pool and look for the lowest numbered
5997 * switch (the least significant bit). Take that bit's position
5998 * as our ID, and mark it used in the compositor's output_id_pool.
5999 */
6000 output->id = ffs(~compositor->output_id_pool) - 1;
6001 compositor->output_id_pool |= 1u << output->id;
6002
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006003 wl_list_remove(&output->link);
Giulio Camuffob1147152015-05-06 21:41:57 +03006004 wl_list_insert(compositor->output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006005 output->enabled = true;
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006006
Pekka Paalanendcac3512017-12-08 14:13:34 +02006007 wl_list_for_each(head, &output->head_list, output_link)
6008 weston_head_add_global(head);
Pekka Paalanen2210ad02017-03-30 15:48:06 +03006009
Giulio Camuffob1147152015-05-06 21:41:57 +03006010 wl_signal_emit(&compositor->output_created_signal, output);
Armin Krezoviće5403842016-08-05 15:28:29 +02006011
6012 wl_list_for_each_safe(view, next, &compositor->view_list, link)
6013 weston_view_geometry_dirty(view);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04006014}
6015
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006016/** Transform device coordinates into global coordinates
6017 *
Marius Vlada2dace22019-06-12 16:05:44 +03006018 * \param output the weston_output object
6019 * \param[in] device_x X coordinate in device units.
6020 * \param[in] device_y Y coordinate in device units.
6021 * \param[out] x X coordinate in the global space.
6022 * \param[out] y Y coordinate in the global space.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006023 *
Marius Vlada2dace22019-06-12 16:05:44 +03006024 * Transforms coordinates from the device coordinate space (physical pixel
6025 * units) to the global coordinate space (logical pixel units). This takes
6026 * into account output transform and scale.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006027 *
Marius Vlad55d87362019-06-11 01:15:35 +03006028 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006029 * \internal
6030 */
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006031WL_EXPORT void
6032weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02006033 double device_x, double device_y,
6034 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006035{
Derek Foreman0f679412014-10-02 13:41:17 -05006036 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02006037 device_x,
6038 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05006039 0.0,
6040 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006041
Derek Foreman67a18b92015-03-24 11:36:14 -05006042 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006043
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02006044 *x = p.f[0] / p.f[3];
6045 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07006046}
6047
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006048/** Removes output from compositor's list of enabled outputs
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006049 *
6050 * \param output The weston_output object that is being removed.
6051 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006052 * The following happens:
6053 *
6054 * - The output assignments of all views in the current scenegraph are
6055 * recomputed.
6056 *
6057 * - Presentation feedback is discarded.
6058 *
6059 * - Compositor is notified that outputs were changed and
6060 * applies the necessary changes to re-layout outputs.
6061 *
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006062 * - The output is put back in the pending outputs list.
6063 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03006064 * - Signal is emitted to notify all users of the weston_output
6065 * object that the output is being destroyed.
6066 *
6067 * - wl_output protocol objects referencing this weston_output
Pekka Paalanen2210ad02017-03-30 15:48:06 +03006068 * are made inert, and the wl_output global is removed.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006069 *
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03006070 * - The output's internal ID is released.
6071 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006072 * \ingroup compositor
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006073 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006074 */
6075static void
6076weston_compositor_remove_output(struct weston_output *output)
6077{
Pekka Paalanenbccda712017-03-29 16:16:04 +03006078 struct weston_compositor *compositor = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006079 struct weston_view *view;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03006080 struct weston_head *head;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006081
6082 assert(output->destroying);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006083 assert(output->enabled);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006084
Pekka Paalanenbccda712017-03-29 16:16:04 +03006085 wl_list_for_each(view, &compositor->view_list, link) {
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006086 if (view->output_mask & (1u << output->id))
6087 weston_view_assign_output(view);
6088 }
6089
6090 weston_presentation_feedback_discard_list(&output->feedback_list);
6091
Pekka Paalanen9711fd92018-06-21 14:26:18 +03006092 weston_compositor_reflow_outputs(compositor, output, -output->width);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006093
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006094 wl_list_remove(&output->link);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006095 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03006096 output->enabled = false;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006097
Pekka Paalanenbccda712017-03-29 16:16:04 +03006098 wl_signal_emit(&compositor->output_destroyed_signal, output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006099 wl_signal_emit(&output->destroy_signal, output);
6100
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02006101 wl_list_for_each(head, &output->head_list, output_link)
6102 weston_head_remove_global(head);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03006103
6104 compositor->output_id_pool &= ~(1u << output->id);
6105 output->id = 0xffffffff; /* invalid */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006106}
6107
6108/** Sets the output scale for a given output.
6109 *
6110 * \param output The weston_output object that the scale is set for.
6111 * \param scale Scale factor for the given output.
6112 *
6113 * It only supports setting scale for an output that
6114 * is not enabled and it can only be ran once.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006115 *
Marius Vlad55d87362019-06-11 01:15:35 +03006116 * \ingroup ouput
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006117 */
6118WL_EXPORT void
6119weston_output_set_scale(struct weston_output *output,
6120 int32_t scale)
6121{
6122 /* We can only set scale on a disabled output */
6123 assert(!output->enabled);
6124
6125 /* We only want to set scale once */
6126 assert(!output->scale);
6127
6128 output->scale = scale;
6129}
6130
6131/** Sets the output transform for a given output.
6132 *
6133 * \param output The weston_output object that the transform is set for.
6134 * \param transform Transform value for the given output.
6135 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006136 * Refer to wl_output::transform section located at
6137 * https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_output
6138 * for list of values that can be passed to this function.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006139 *
Marius Vlad55d87362019-06-11 01:15:35 +03006140 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006141 */
6142WL_EXPORT void
6143weston_output_set_transform(struct weston_output *output,
6144 uint32_t transform)
6145{
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006146 struct weston_pointer_motion_event ev;
6147 struct wl_resource *resource;
6148 struct weston_seat *seat;
6149 pixman_region32_t old_region;
6150 int mid_x, mid_y;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006151 struct weston_head *head;
6152 int ver;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006153
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006154 if (!output->enabled && output->transform == UINT32_MAX) {
6155 output->transform = transform;
6156 return;
6157 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006158
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006159 weston_output_transform_scale_init(output, transform, output->scale);
6160
6161 pixman_region32_init(&old_region);
6162 pixman_region32_copy(&old_region, &output->region);
6163
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006164 weston_output_init_geometry(output, output->x, output->y);
6165
6166 output->dirty = 1;
6167
6168 /* Notify clients of the change for output transform. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006169 wl_list_for_each(head, &output->head_list, output_link) {
6170 wl_resource_for_each(resource, &head->resource_list) {
6171 wl_output_send_geometry(resource,
6172 output->x,
6173 output->y,
6174 head->mm_width,
6175 head->mm_height,
6176 head->subpixel,
6177 head->make,
6178 head->model,
6179 output->transform);
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006180
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006181 ver = wl_resource_get_version(resource);
6182 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
6183 wl_output_send_done(resource);
6184 }
Roman Gilge97391c2019-03-29 13:01:06 +01006185 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
6186 zxdg_output_v1_send_logical_position(resource,
6187 output->x,
6188 output->y);
6189 zxdg_output_v1_send_logical_size(resource,
6190 output->width,
6191 output->height);
6192 zxdg_output_v1_send_done(resource);
6193 }
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006194 }
6195
6196 /* we must ensure that pointers are inside output, otherwise they disappear */
6197 mid_x = output->x + output->width / 2;
6198 mid_y = output->y + output->height / 2;
6199
6200 ev.mask = WESTON_POINTER_MOTION_ABS;
6201 ev.x = wl_fixed_to_double(wl_fixed_from_int(mid_x));
6202 ev.y = wl_fixed_to_double(wl_fixed_from_int(mid_y));
6203
6204 wl_list_for_each(seat, &output->compositor->seat_list, link) {
6205 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
6206
6207 if (pointer && pixman_region32_contains_point(&old_region,
6208 wl_fixed_to_int(pointer->x),
6209 wl_fixed_to_int(pointer->y),
6210 NULL))
6211 weston_pointer_move(pointer, &ev);
6212 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006213}
6214
6215/** Initializes a weston_output object with enough data so
6216 ** an output can be configured.
6217 *
6218 * \param output The weston_output object to initialize
6219 * \param compositor The compositor instance.
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006220 * \param name Name for the output (the string is copied).
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006221 *
6222 * Sets initial values for fields that are expected to be
6223 * configured either by compositors or backends.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006224 *
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006225 * The name is used in logs, and can be used by compositors as a configuration
6226 * identifier.
6227 *
Marius Vlad55d87362019-06-11 01:15:35 +03006228 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006229 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006230 */
6231WL_EXPORT void
Armin Krezović40087402016-09-30 14:11:12 +02006232weston_output_init(struct weston_output *output,
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006233 struct weston_compositor *compositor,
6234 const char *name)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006235{
6236 output->compositor = compositor;
6237 output->destroying = 0;
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006238 output->name = strdup(name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006239 wl_list_init(&output->link);
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006240 wl_signal_init(&output->user_destroy_signal);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006241 output->enabled = false;
Ankit Nautiyal2690a772019-03-25 17:57:59 +05306242 output->desired_protection = WESTON_HDCP_DISABLE;
Ankit Nautiyal2844f8e2019-04-03 10:14:59 +05306243 output->allow_protection = true;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006244
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006245 wl_list_init(&output->head_list);
6246
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006247 /* Add some (in)sane defaults which can be used
6248 * for checking if an output was properly configured
6249 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006250 output->scale = 0;
6251 /* Can't use -1 on uint32_t and 0 is valid enum value */
6252 output->transform = UINT32_MAX;
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006253
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006254 pixman_region32_init(&output->region);
Pekka Paalanen42704142017-09-06 16:47:52 +03006255 wl_list_init(&output->mode_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006256}
6257
6258/** Adds weston_output object to pending output list.
6259 *
6260 * \param output The weston_output object to add
6261 * \param compositor The compositor instance.
6262 *
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03006263 * The opposite of this operation is built into weston_output_release().
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006264 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006265 * \ingroup compositor
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006266 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006267 */
6268WL_EXPORT void
6269weston_compositor_add_pending_output(struct weston_output *output,
6270 struct weston_compositor *compositor)
6271{
Pekka Paalanene952a012017-03-29 17:14:00 +03006272 assert(output->disable);
6273 assert(output->enable);
6274
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006275 wl_list_remove(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006276 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006277}
6278
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006279/** Create a string with the attached heads' names.
6280 *
6281 * The string must be free()'d.
Marius Vlad55d87362019-06-11 01:15:35 +03006282 *
6283 * \ingroup output
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006284 */
6285static char *
6286weston_output_create_heads_string(struct weston_output *output)
6287{
6288 FILE *fp;
6289 char *str = NULL;
6290 size_t size = 0;
6291 struct weston_head *head;
6292 const char *sep = "";
6293
6294 fp = open_memstream(&str, &size);
6295 if (!fp)
6296 return NULL;
6297
6298 wl_list_for_each(head, &output->head_list, output_link) {
6299 fprintf(fp, "%s%s", sep, head->name);
6300 sep = ", ";
6301 }
6302 fclose(fp);
6303
6304 return str;
6305}
6306
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006307/** Constructs a weston_output object that can be used by the compositor.
6308 *
Pekka Paalanencc201e42017-03-30 15:11:25 +03006309 * \param output The weston_output object that needs to be enabled. Must not
Pekka Paalanenddce54d2017-08-23 16:00:21 +03006310 * be enabled already. Must have at least one head attached.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006311 *
6312 * Output coordinates are calculated and each new output is by default
6313 * assigned to the right of previous one.
6314 *
6315 * Sets up the transformation, zoom, and geometry of the output using
6316 * the properties that need to be configured by the compositor.
6317 *
6318 * Establishes a repaint timer for the output with the relevant display
6319 * object's event loop. See output_repaint_timer_handler().
6320 *
6321 * The output is assigned an ID. Weston can support up to 32 distinct
6322 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
6323 * is referred to and used to find the first available ID number, and
6324 * then this ID is marked as used in output_id_pool.
6325 *
6326 * The output is also assigned a Wayland global with the wl_output
6327 * external interface.
6328 *
6329 * Backend specific function is called to set up the output output.
6330 *
6331 * Output is added to the compositor's output list
6332 *
6333 * If the backend specific function fails, the weston_output object
6334 * is returned to a state it was before calling this function and
6335 * is added to the compositor's pending_output_list in case it needs
6336 * to be reconfigured or just so it can be destroyed at shutdown.
6337 *
6338 * 0 is returned on success, -1 on failure.
Marius Vlad55d87362019-06-11 01:15:35 +03006339 *
6340 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006341 */
6342WL_EXPORT int
6343weston_output_enable(struct weston_output *output)
6344{
Armin Krezović782f5df2016-09-30 14:11:11 +02006345 struct weston_compositor *c = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006346 struct weston_output *iterator;
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03006347 struct weston_head *head;
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006348 char *head_names;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006349 int x = 0, y = 0;
6350
Pekka Paalanencc201e42017-03-30 15:11:25 +03006351 if (output->enabled) {
6352 weston_log("Error: attempt to enable an enabled output '%s'\n",
6353 output->name);
6354 return -1;
6355 }
6356
Pekka Paalanenddce54d2017-08-23 16:00:21 +03006357 if (wl_list_empty(&output->head_list)) {
6358 weston_log("Error: cannot enable output '%s' without heads.\n",
6359 output->name);
6360 return -1;
6361 }
6362
Pekka Paalanen586e1ac2017-09-14 16:17:59 +03006363 if (wl_list_empty(&output->mode_list) || !output->current_mode) {
6364 weston_log("Error: no video mode for output '%s'.\n",
6365 output->name);
6366 return -1;
6367 }
6368
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03006369 wl_list_for_each(head, &output->head_list, output_link) {
6370 assert(head->make);
6371 assert(head->model);
6372 }
6373
Armin Krezović782f5df2016-09-30 14:11:11 +02006374 iterator = container_of(c->output_list.prev,
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006375 struct weston_output, link);
6376
Armin Krezović782f5df2016-09-30 14:11:11 +02006377 if (!wl_list_empty(&c->output_list))
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006378 x = iterator->x + iterator->width;
6379
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006380 /* Make sure the scale is set up */
6381 assert(output->scale);
6382
6383 /* Make sure we have a transform set */
6384 assert(output->transform != UINT32_MAX);
6385
Armin Krezović782f5df2016-09-30 14:11:11 +02006386 output->x = x;
6387 output->y = y;
6388 output->dirty = 1;
6389 output->original_scale = output->scale;
6390
Marius Vlad3a2f8292019-11-04 17:53:46 +02006391 wl_signal_init(&output->frame_signal);
6392 wl_signal_init(&output->destroy_signal);
6393
Armin Krezović782f5df2016-09-30 14:11:11 +02006394 weston_output_transform_scale_init(output, output->transform, output->scale);
6395 weston_output_init_zoom(output);
6396
6397 weston_output_init_geometry(output, x, y);
6398 weston_output_damage(output);
6399
Armin Krezović782f5df2016-09-30 14:11:11 +02006400 wl_list_init(&output->animation_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02006401 wl_list_init(&output->feedback_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02006402
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006403 /* Enable the output (set up the crtc or create a
6404 * window representing the output, set up the
6405 * renderer, etc)
6406 */
6407 if (output->enable(output) < 0) {
6408 weston_log("Enabling output \"%s\" failed.\n", output->name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006409 return -1;
6410 }
6411
6412 weston_compositor_add_output(output->compositor, output);
6413
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006414 head_names = weston_output_create_heads_string(output);
6415 weston_log("Output '%s' enabled with head(s) %s\n",
6416 output->name, head_names);
6417 free(head_names);
6418
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006419 return 0;
6420}
6421
6422/** Converts a weston_output object to a pending output state, so it
6423 ** can be configured again or destroyed.
6424 *
6425 * \param output The weston_output object that needs to be disabled.
6426 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006427 * Calls a backend specific function to disable an output, in case
6428 * such function exists.
6429 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03006430 * The backend specific disable function may choose to postpone the disabling
6431 * by returning a negative value, in which case this function returns early.
6432 * In that case the backend will guarantee the output will be disabled soon
6433 * by the backend calling this function again. One must not attempt to re-enable
6434 * the output until that happens.
6435 *
6436 * Otherwise, if the output is being used by the compositor, it is removed
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006437 * from weston's output_list (see weston_compositor_remove_output())
6438 * and is returned to a state it was before weston_output_enable()
6439 * was ran (see weston_output_enable_undo()).
6440 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03006441 * See weston_output_init() for more information on the
6442 * state output is returned to.
Pekka Paalanencc201e42017-03-30 15:11:25 +03006443 *
6444 * If the output has never been enabled yet, this function can still be
6445 * called to ensure that the output is actually turned off rather than left
6446 * in the state it was discovered in.
Marius Vlad55d87362019-06-11 01:15:35 +03006447 *
6448 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006449 */
6450WL_EXPORT void
6451weston_output_disable(struct weston_output *output)
6452{
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006453 /* Should we rename this? */
6454 output->destroying = 1;
6455
Pekka Paalanenc65df642017-03-29 15:45:46 +03006456 /* Disable is called unconditionally also for not-enabled outputs,
6457 * because at compositor start-up, if there is an output that is
6458 * already on but the compositor wants to turn it off, we have to
6459 * forward the turn-off to the backend so it knows to do it.
6460 * The backend cannot initially turn off everything, because it
6461 * would cause unnecessary mode-sets for all outputs the compositor
6462 * wants to be on.
6463 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006464 if (output->disable(output) < 0)
6465 return;
6466
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006467 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006468 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006469
6470 output->destroying = 0;
6471}
6472
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006473/** Forces a synchronous call to heads_changed hook
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006474 *
6475 * \param compositor The compositor instance
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006476 *
6477 * If there are new or changed heads, calls the heads_changed hook and
6478 * returns after the hook returns.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006479 *
6480 * \ingroup compositor
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006481 */
6482WL_EXPORT void
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006483weston_compositor_flush_heads_changed(struct weston_compositor *compositor)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006484{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03006485 if (compositor->heads_changed_source) {
6486 wl_event_source_remove(compositor->heads_changed_source);
6487 weston_compositor_call_heads_changed(compositor);
6488 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006489}
6490
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006491/** Add destroy callback for an output
6492 *
6493 * \param output The output to watch.
6494 * \param listener The listener to add. The \c notify member must be set.
6495 *
6496 * The listener callback will be called when user destroys an output. This
6497 * may be delayed by a backend in some cases. The main purpose of the
6498 * listener is to allow hooking up custom data to the output. The custom data
6499 * can be fetched via weston_output_get_destroy_listener() followed by
6500 * container_of().
6501 *
6502 * The \c data argument to the notify callback is the weston_output being
6503 * destroyed.
6504 *
6505 * @note This is for the final destruction of an output, not when it gets
6506 * disabled. If you want to keep track of enabled outputs, this is not it.
Marius Vlad55d87362019-06-11 01:15:35 +03006507 *
6508 * \ingroup ouput
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006509 */
6510WL_EXPORT void
6511weston_output_add_destroy_listener(struct weston_output *output,
6512 struct wl_listener *listener)
6513{
6514 wl_signal_add(&output->user_destroy_signal, listener);
6515}
6516
6517/** Look up destroy listener for an output
6518 *
6519 * \param output The output to query.
6520 * \param notify The notify function used used for the added destroy listener.
6521 * \return The listener, or NULL if not found.
6522 *
6523 * This looks up the previously added destroy listener struct based on the
6524 * notify function it has. The listener can be used to access user data
6525 * through \c container_of().
6526 *
6527 * \sa wl_signal_get() weston_output_add_destroy_listener()
Marius Vlad55d87362019-06-11 01:15:35 +03006528 * \ingroup output
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006529 */
6530WL_EXPORT struct wl_listener *
6531weston_output_get_destroy_listener(struct weston_output *output,
6532 wl_notify_func_t notify)
6533{
6534 return wl_signal_get(&output->user_destroy_signal, notify);
6535}
6536
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006537/** Uninitialize an output
6538 *
6539 * Removes the output from the list of enabled outputs if necessary, but
6540 * does not call the backend's output disable function. The output will no
6541 * longer be in the list of pending outputs either.
6542 *
6543 * All fields of weston_output become uninitialized, i.e. should not be used
6544 * anymore. The caller can free the memory after this.
6545 *
Marius Vlad55d87362019-06-11 01:15:35 +03006546 * \ingroup ouput
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006547 * \internal
6548 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006549WL_EXPORT void
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03006550weston_output_release(struct weston_output *output)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006551{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006552 struct weston_head *head, *tmp;
6553
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006554 output->destroying = 1;
6555
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006556 wl_signal_emit(&output->user_destroy_signal, output);
6557
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03006558 if (output->idle_repaint_source)
6559 wl_event_source_remove(output->idle_repaint_source);
6560
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006561 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006562 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006563
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006564 pixman_region32_fini(&output->region);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006565 wl_list_remove(&output->link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006566
6567 wl_list_for_each_safe(head, tmp, &output->head_list, output_link)
6568 weston_head_detach(head);
6569
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006570 free(output->name);
6571}
6572
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006573/** Find an output by its given name
6574 *
6575 * \param compositor The compositor to search in.
6576 * \param name The output name to search for.
6577 * \return An existing output with the given name, or NULL if not found.
6578 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006579 * \ingroup compositor
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006580 */
6581WL_EXPORT struct weston_output *
6582weston_compositor_find_output_by_name(struct weston_compositor *compositor,
6583 const char *name)
6584{
6585 struct weston_output *output;
6586
6587 wl_list_for_each(output, &compositor->output_list, link)
6588 if (strcmp(output->name, name) == 0)
6589 return output;
6590
6591 wl_list_for_each(output, &compositor->pending_output_list, link)
6592 if (strcmp(output->name, name) == 0)
6593 return output;
6594
6595 return NULL;
6596}
6597
6598/** Create a named output
6599 *
6600 * \param compositor The compositor.
6601 * \param name The name for the output.
6602 * \return A new \c weston_output, or NULL on failure.
6603 *
6604 * This creates a new weston_output that starts with no heads attached.
6605 *
6606 * An output must be configured and it must have at least one head before
6607 * it can be enabled.
6608 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006609 * \ingroup compositor
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006610 */
6611WL_EXPORT struct weston_output *
6612weston_compositor_create_output(struct weston_compositor *compositor,
6613 const char *name)
6614{
6615 assert(compositor->backend->create_output);
6616
6617 if (weston_compositor_find_output_by_name(compositor, name)) {
6618 weston_log("Warning: attempted to create an output with a "
6619 "duplicate name '%s'.\n", name);
6620 return NULL;
6621 }
6622
6623 return compositor->backend->create_output(compositor, name);
6624}
6625
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006626/** Create an output for an unused head
6627 *
6628 * \param compositor The compositor.
6629 * \param head The head to attach to the output.
6630 * \return A new \c weston_output, or NULL on failure.
6631 *
6632 * This creates a new weston_output that starts with the given head attached.
6633 * The output inherits the name of the head. The head must not be already
6634 * attached to another output.
6635 *
6636 * An output must be configured before it can be enabled.
6637 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006638 * \ingroup compositor
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006639 */
6640WL_EXPORT struct weston_output *
6641weston_compositor_create_output_with_head(struct weston_compositor *compositor,
6642 struct weston_head *head)
6643{
6644 struct weston_output *output;
6645
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006646 output = weston_compositor_create_output(compositor, head->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006647 if (!output)
6648 return NULL;
6649
6650 if (weston_output_attach_head(output, head) < 0) {
Pekka Paalanen42c0e142017-10-27 12:07:49 +03006651 weston_output_destroy(output);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006652 return NULL;
6653 }
6654
6655 return output;
6656}
6657
6658/** Destroy an output
6659 *
6660 * \param output The output to destroy.
6661 *
6662 * The heads attached to the given output are detached and become unused again.
6663 *
6664 * It is not necessary to explicitly destroy all outputs at compositor exit.
Leandro Ribeiroca640d52020-01-27 19:12:01 -03006665 * weston_compositor_destroy() will automatically destroy any remaining
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006666 * outputs.
6667 *
Marius Vlad55d87362019-06-11 01:15:35 +03006668 * \ingroup ouput
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006669 */
6670WL_EXPORT void
6671weston_output_destroy(struct weston_output *output)
6672{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006673 output->destroy(output);
6674}
6675
Pekka Paalanencf0a4762017-04-04 16:36:07 +03006676/** When you need a head...
6677 *
6678 * This function is a hack, used until all code has been converted to become
6679 * multi-head aware.
6680 *
6681 * \param output The weston_output whose head to get.
6682 * \return The first head in the output's list.
Marius Vlad55d87362019-06-11 01:15:35 +03006683 *
6684 * \ingroup ouput
Pekka Paalanencf0a4762017-04-04 16:36:07 +03006685 */
6686WL_EXPORT struct weston_head *
6687weston_output_get_first_head(struct weston_output *output)
6688{
6689 if (wl_list_empty(&output->head_list))
6690 return NULL;
6691
6692 return container_of(output->head_list.next,
6693 struct weston_head, output_link);
6694}
6695
Ankit Nautiyal2844f8e2019-04-03 10:14:59 +05306696/** Allow/Disallow content-protection support for an output
6697 *
6698 * This function sets the allow_protection member for an output. Setting of
6699 * this field will allow the compositor to attempt content-protection for this
6700 * output, for a backend that supports the content-protection protocol.
6701 *
6702 * \param output The weston_output for whom the content-protection is to be
6703 * allowed.
6704 * \param allow_protection The bool value which is to be set.
6705 */
6706WL_EXPORT void
6707weston_output_allow_protection(struct weston_output *output,
6708 bool allow_protection)
6709{
6710 output->allow_protection = allow_protection;
6711}
6712
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01006713static void
Roman Gilge97391c2019-03-29 13:01:06 +01006714xdg_output_unlist(struct wl_resource *resource)
6715{
6716 wl_list_remove(wl_resource_get_link(resource));
6717}
6718
6719static void
6720xdg_output_destroy(struct wl_client *client, struct wl_resource *resource)
6721{
6722 wl_resource_destroy(resource);
6723}
6724
6725static const struct zxdg_output_v1_interface xdg_output_interface = {
6726 xdg_output_destroy
6727};
6728
6729static void
6730xdg_output_manager_destroy(struct wl_client *client,
6731 struct wl_resource *resource)
6732{
6733 wl_resource_destroy(resource);
6734}
6735
6736static void
6737xdg_output_manager_get_xdg_output(struct wl_client *client,
6738 struct wl_resource *manager,
6739 uint32_t id,
6740 struct wl_resource *output_resource)
6741{
6742 int version = wl_resource_get_version(manager);
6743 struct weston_head *head = wl_resource_get_user_data(output_resource);
6744 struct weston_output *output = head->output;
6745 struct wl_resource *resource;
6746
6747 resource = wl_resource_create(client, &zxdg_output_v1_interface,
6748 version, id);
6749 if (resource == NULL) {
6750 wl_client_post_no_memory(client);
6751 return;
6752 }
6753
6754 wl_list_insert(&head->xdg_output_resource_list,
6755 wl_resource_get_link(resource));
6756
6757 wl_resource_set_implementation(resource, &xdg_output_interface,
6758 NULL, xdg_output_unlist);
6759
6760 zxdg_output_v1_send_logical_position(resource, output->x, output->y);
6761 zxdg_output_v1_send_logical_size(resource,
6762 output->width,
6763 output->height);
6764 if (version >= ZXDG_OUTPUT_V1_NAME_SINCE_VERSION)
6765 zxdg_output_v1_send_name(resource, head->name);
6766
6767 zxdg_output_v1_send_done(resource);
6768}
6769
6770static const struct zxdg_output_manager_v1_interface xdg_output_manager_interface = {
6771 xdg_output_manager_destroy,
6772 xdg_output_manager_get_xdg_output
6773};
6774
6775static void
6776bind_xdg_output_manager(struct wl_client *client,
6777 void *data, uint32_t version, uint32_t id)
6778{
6779 struct wl_resource *resource;
6780
6781 resource = wl_resource_create(client, &zxdg_output_manager_v1_interface,
6782 version, id);
6783 if (resource == NULL) {
6784 wl_client_post_no_memory(client);
6785 return;
6786 }
6787
6788 wl_resource_set_implementation(resource, &xdg_output_manager_interface,
6789 NULL, NULL);
6790}
6791
6792static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006793destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006794{
Jonny Lamb74130762013-11-26 18:19:46 +01006795 struct weston_surface *surface =
6796 wl_resource_get_user_data(resource);
6797
Pekka Paalanen4826f872016-04-22 14:14:38 +03006798 if (!surface)
6799 return;
6800
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006801 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02006802 surface->pending.buffer_viewport.buffer.src_width =
6803 wl_fixed_from_int(-1);
6804 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006805 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006806}
6807
6808static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006809viewport_destroy(struct wl_client *client,
6810 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006811{
6812 wl_resource_destroy(resource);
6813}
6814
6815static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006816viewport_set_source(struct wl_client *client,
6817 struct wl_resource *resource,
6818 wl_fixed_t src_x,
6819 wl_fixed_t src_y,
6820 wl_fixed_t src_width,
6821 wl_fixed_t src_height)
6822{
6823 struct weston_surface *surface =
6824 wl_resource_get_user_data(resource);
6825
Pekka Paalanen4826f872016-04-22 14:14:38 +03006826 if (!surface) {
6827 wl_resource_post_error(resource,
6828 WP_VIEWPORT_ERROR_NO_SURFACE,
6829 "wl_surface for this viewport is no longer exists");
6830 return;
6831 }
6832
6833 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03006834 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006835
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006836 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03006837 src_height == wl_fixed_from_int(-1) &&
6838 src_x == wl_fixed_from_int(-1) &&
6839 src_y == wl_fixed_from_int(-1)) {
6840 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006841 surface->pending.buffer_viewport.buffer.src_width =
6842 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006843 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006844 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006845 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006846
Pekka Paalanen201769a2016-04-26 14:42:11 +03006847 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006848 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006849 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03006850 "wl_surface@%d viewport source "
6851 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
6852 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006853 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03006854 wl_fixed_to_double(src_height),
6855 wl_fixed_to_double(src_x),
6856 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006857 return;
6858 }
6859
6860 surface->pending.buffer_viewport.buffer.src_x = src_x;
6861 surface->pending.buffer_viewport.buffer.src_y = src_y;
6862 surface->pending.buffer_viewport.buffer.src_width = src_width;
6863 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006864 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006865}
6866
6867static void
6868viewport_set_destination(struct wl_client *client,
6869 struct wl_resource *resource,
6870 int32_t dst_width,
6871 int32_t dst_height)
6872{
6873 struct weston_surface *surface =
6874 wl_resource_get_user_data(resource);
6875
Pekka Paalanen4826f872016-04-22 14:14:38 +03006876 if (!surface) {
6877 wl_resource_post_error(resource,
6878 WP_VIEWPORT_ERROR_NO_SURFACE,
6879 "wl_surface for this viewport no longer exists");
6880 return;
6881 }
6882
6883 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006884
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006885 if (dst_width == -1 && dst_height == -1) {
6886 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006887 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006888 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006889 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006890 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006891
6892 if (dst_width <= 0 || dst_height <= 0) {
6893 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006894 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006895 "destination size must be positive (%dx%d)",
6896 dst_width, dst_height);
6897 return;
6898 }
6899
6900 surface->pending.buffer_viewport.surface.width = dst_width;
6901 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006902 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006903}
6904
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006905static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006906 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006907 viewport_set_source,
6908 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01006909};
6910
6911static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006912viewporter_destroy(struct wl_client *client,
6913 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006914{
6915 wl_resource_destroy(resource);
6916}
6917
6918static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006919viewporter_get_viewport(struct wl_client *client,
6920 struct wl_resource *viewporter,
6921 uint32_t id,
6922 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006923{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006924 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006925 struct weston_surface *surface =
6926 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006927 struct wl_resource *resource;
6928
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006929 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006930 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006931 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006932 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01006933 return;
6934 }
6935
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006936 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006937 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006938 if (resource == NULL) {
6939 wl_client_post_no_memory(client);
6940 return;
6941 }
6942
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006943 wl_resource_set_implementation(resource, &viewport_interface,
6944 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01006945
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006946 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006947}
6948
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006949static const struct wp_viewporter_interface viewporter_interface = {
6950 viewporter_destroy,
6951 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01006952};
6953
6954static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006955bind_viewporter(struct wl_client *client,
6956 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006957{
6958 struct wl_resource *resource;
6959
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006960 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006961 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006962 if (resource == NULL) {
6963 wl_client_post_no_memory(client);
6964 return;
6965 }
6966
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006967 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01006968 NULL, NULL);
6969}
6970
6971static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04006972destroy_presentation_feedback(struct wl_resource *feedback_resource)
6973{
6974 struct weston_presentation_feedback *feedback;
6975
6976 feedback = wl_resource_get_user_data(feedback_resource);
6977
6978 wl_list_remove(&feedback->link);
6979 free(feedback);
6980}
6981
6982static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006983presentation_destroy(struct wl_client *client, struct wl_resource *resource)
6984{
6985 wl_resource_destroy(resource);
6986}
6987
6988static void
6989presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04006990 struct wl_resource *presentation_resource,
6991 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006992 uint32_t callback)
6993{
Pekka Paalanen133e4392014-09-23 22:08:46 -04006994 struct weston_surface *surface;
6995 struct weston_presentation_feedback *feedback;
6996
6997 surface = wl_resource_get_user_data(surface_resource);
6998
Bryce Harringtonde16d892014-11-20 22:21:57 -08006999 feedback = zalloc(sizeof *feedback);
7000 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04007001 goto err_calloc;
7002
7003 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007004 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04007005 1, callback);
7006 if (!feedback->resource)
7007 goto err_create;
7008
7009 wl_resource_set_implementation(feedback->resource, NULL, feedback,
7010 destroy_presentation_feedback);
7011
7012 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
7013
7014 return;
7015
7016err_create:
7017 free(feedback);
7018
7019err_calloc:
7020 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007021}
7022
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007023static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007024 presentation_destroy,
7025 presentation_feedback
7026};
7027
7028static void
7029bind_presentation(struct wl_client *client,
7030 void *data, uint32_t version, uint32_t id)
7031{
7032 struct weston_compositor *compositor = data;
7033 struct wl_resource *resource;
7034
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007035 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06007036 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007037 if (resource == NULL) {
7038 wl_client_post_no_memory(client);
7039 return;
7040 }
7041
7042 wl_resource_set_implementation(resource, &presentation_implementation,
7043 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007044 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007045}
7046
7047static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05007048compositor_bind(struct wl_client *client,
7049 void *data, uint32_t version, uint32_t id)
7050{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007051 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05007052 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05007053
Jason Ekstranda85118c2013-06-27 20:17:02 -05007054 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06007055 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07007056 if (resource == NULL) {
7057 wl_client_post_no_memory(client);
7058 return;
7059 }
7060
7061 wl_resource_set_implementation(resource, &compositor_interface,
7062 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05007063}
7064
Daniel Stonece62cb32018-07-20 09:46:24 +01007065static const char *
7066output_repaint_status_text(struct weston_output *output)
7067{
7068 switch (output->repaint_status) {
7069 case REPAINT_NOT_SCHEDULED:
7070 return "no repaint";
7071 case REPAINT_BEGIN_FROM_IDLE:
7072 return "start_repaint_loop scheduled";
7073 case REPAINT_SCHEDULED:
7074 return "repaint scheduled";
7075 case REPAINT_AWAITING_COMPLETION:
7076 return "awaiting completion";
7077 }
7078
7079 assert(!"output_repaint_status_text missing enum");
7080 return NULL;
7081}
7082
7083static void
7084debug_scene_view_print_buffer(FILE *fp, struct weston_view *view)
7085{
7086 struct weston_buffer *buffer = view->surface->buffer_ref.buffer;
7087 struct wl_shm_buffer *shm;
7088 struct linux_dmabuf_buffer *dmabuf;
Marius Vlad00a6e012018-11-20 17:52:31 +02007089 const struct pixel_format_info *pixel_info = NULL;
Daniel Stonece62cb32018-07-20 09:46:24 +01007090
7091 if (!buffer) {
7092 fprintf(fp, "\t\t[buffer not available]\n");
7093 return;
7094 }
7095
7096 shm = wl_shm_buffer_get(buffer->resource);
7097 if (shm) {
Marius Vlad00a6e012018-11-20 17:52:31 +02007098 uint32_t _format = wl_shm_buffer_get_format(shm);
7099 pixel_info = pixel_format_get_info_shm(_format);
Daniel Stonece62cb32018-07-20 09:46:24 +01007100 fprintf(fp, "\t\tSHM buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02007101 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
7102 (unsigned long) _format,
7103 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Daniel Stonece62cb32018-07-20 09:46:24 +01007104 return;
7105 }
7106
7107 dmabuf = linux_dmabuf_buffer_get(buffer->resource);
7108 if (dmabuf) {
Marius Vlad00a6e012018-11-20 17:52:31 +02007109 pixel_info = pixel_format_get_info(dmabuf->attributes.format);
Daniel Stonece62cb32018-07-20 09:46:24 +01007110 fprintf(fp, "\t\tdmabuf buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02007111 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
7112 (unsigned long) dmabuf->attributes.format,
7113 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Daniel Stonece62cb32018-07-20 09:46:24 +01007114 fprintf(fp, "\t\t\tmodifier: 0x%llx\n",
7115 (unsigned long long) dmabuf->attributes.modifier[0]);
7116 return;
7117 }
7118
Marius Vlad253ba9a2019-03-17 18:22:21 +02007119 fprintf(fp, "\t\tEGL buffer\n");
Daniel Stonece62cb32018-07-20 09:46:24 +01007120}
7121
7122static void
7123debug_scene_view_print(FILE *fp, struct weston_view *view, int view_idx)
7124{
7125 struct weston_compositor *ec = view->surface->compositor;
7126 struct weston_output *output;
7127 char desc[512];
7128 pixman_box32_t *box;
7129 uint32_t surface_id = 0;
7130 pid_t pid = 0;
7131
7132 if (view->surface->resource) {
7133 struct wl_resource *resource = view->surface->resource;
7134 wl_client_get_credentials(wl_resource_get_client(resource),
7135 &pid, NULL, NULL);
7136 surface_id = wl_resource_get_id(view->surface->resource);
7137 }
7138
7139 if (!view->surface->get_label ||
7140 view->surface->get_label(view->surface, desc, sizeof(desc)) < 0) {
7141 strcpy(desc, "[no description available]");
7142 }
7143 fprintf(fp, "\tView %d (role %s, PID %d, surface ID %u, %s, %p):\n",
7144 view_idx, view->surface->role_name, pid, surface_id,
7145 desc, view);
7146
7147 box = pixman_region32_extents(&view->transform.boundingbox);
7148 fprintf(fp, "\t\tposition: (%d, %d) -> (%d, %d)\n",
7149 box->x1, box->y1, box->x2, box->y2);
7150 box = pixman_region32_extents(&view->transform.opaque);
7151
Michael Olbrichb7e5f102020-08-11 16:33:35 +02007152 if (weston_view_is_opaque(view, &view->transform.boundingbox)) {
Daniel Stonece62cb32018-07-20 09:46:24 +01007153 fprintf(fp, "\t\t[fully opaque]\n");
7154 } else if (!pixman_region32_not_empty(&view->transform.opaque)) {
7155 fprintf(fp, "\t\t[not opaque]\n");
7156 } else {
7157 fprintf(fp, "\t\t[opaque: (%d, %d) -> (%d, %d)]\n",
7158 box->x1, box->y1, box->x2, box->y2);
7159 }
7160
7161 if (view->alpha < 1.0)
7162 fprintf(fp, "\t\talpha: %f\n", view->alpha);
7163
7164 if (view->output_mask != 0) {
7165 bool first_output = true;
7166 fprintf(fp, "\t\toutputs: ");
7167 wl_list_for_each(output, &ec->output_list, link) {
7168 if (!(view->output_mask & (1 << output->id)))
7169 continue;
7170 fprintf(fp, "%s%d (%s)%s",
7171 (first_output) ? "" : ", ",
7172 output->id, output->name,
7173 (view->output == output) ? " (primary)" : "");
7174 first_output = false;
7175 }
7176 } else {
7177 fprintf(fp, "\t\t[no outputs]");
7178 }
7179
7180 fprintf(fp, "\n");
7181
7182 debug_scene_view_print_buffer(fp, view);
7183}
7184
Marius Vlad433f4e72019-02-17 22:14:23 +02007185static void
7186debug_scene_view_print_tree(struct weston_view *view,
Marius Vlada6acfa82019-03-17 18:10:09 +02007187 FILE *fp, int *view_idx)
Marius Vlad433f4e72019-02-17 22:14:23 +02007188{
7189 struct weston_subsurface *sub;
7190 struct weston_view *ev;
7191
7192 /*
7193 * print the view first, then we recursively go on printing
7194 * sub-surfaces. We bail out once no more sub-surfaces are available.
7195 */
Marius Vlada6acfa82019-03-17 18:10:09 +02007196 debug_scene_view_print(fp, view, *view_idx);
Marius Vlad433f4e72019-02-17 22:14:23 +02007197
7198 /* no more sub-surfaces */
7199 if (wl_list_empty(&view->surface->subsurface_list))
7200 return;
7201
7202 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
7203 wl_list_for_each(ev, &sub->surface->views, surface_link) {
Michael Olbrichef5f3232020-04-29 09:03:15 +02007204 /* only print the child views of the current view */
7205 if (ev->parent_view != view)
Marius Vlad433f4e72019-02-17 22:14:23 +02007206 continue;
Marius Vlada6acfa82019-03-17 18:10:09 +02007207
7208 (*view_idx)++;
Marius Vlad433f4e72019-02-17 22:14:23 +02007209 debug_scene_view_print_tree(ev, fp, view_idx);
7210 }
7211 }
7212}
7213
Daniel Stonece62cb32018-07-20 09:46:24 +01007214/**
7215 * Output information on how libweston is currently composing the scene
7216 * graph.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007217 *
7218 * \ingroup compositor
Daniel Stonece62cb32018-07-20 09:46:24 +01007219 */
7220WL_EXPORT char *
7221weston_compositor_print_scene_graph(struct weston_compositor *ec)
7222{
7223 struct weston_output *output;
7224 struct weston_layer *layer;
7225 struct timespec now;
7226 int layer_idx = 0;
7227 FILE *fp;
7228 char *ret;
7229 size_t len;
7230 int err;
7231
7232 fp = open_memstream(&ret, &len);
7233 assert(fp);
7234
7235 weston_compositor_read_presentation_clock(ec, &now);
7236 fprintf(fp, "Weston scene graph at %ld.%09ld:\n\n",
7237 now.tv_sec, now.tv_nsec);
7238
7239 wl_list_for_each(output, &ec->output_list, link) {
7240 struct weston_head *head;
7241 int head_idx = 0;
7242
7243 fprintf(fp, "Output %d (%s):\n", output->id, output->name);
7244 assert(output->enabled);
7245
7246 fprintf(fp, "\tposition: (%d, %d) -> (%d, %d)\n",
7247 output->x, output->y,
7248 output->x + output->width,
7249 output->y + output->height);
7250 fprintf(fp, "\tmode: %dx%d@%.3fHz\n",
7251 output->current_mode->width,
7252 output->current_mode->height,
7253 output->current_mode->refresh / 1000.0);
7254 fprintf(fp, "\tscale: %d\n", output->scale);
7255
7256 fprintf(fp, "\trepaint status: %s\n",
7257 output_repaint_status_text(output));
7258 if (output->repaint_status == REPAINT_SCHEDULED)
7259 fprintf(fp, "\tnext repaint: %ld.%09ld\n",
7260 output->next_repaint.tv_sec,
7261 output->next_repaint.tv_nsec);
7262
7263 wl_list_for_each(head, &output->head_list, output_link) {
7264 fprintf(fp, "\tHead %d (%s): %sconnected\n",
7265 head_idx++, head->name,
7266 (head->connected) ? "" : "not ");
7267 }
7268 }
7269
7270 fprintf(fp, "\n");
7271
7272 wl_list_for_each(layer, &ec->layer_list, link) {
7273 struct weston_view *view;
7274 int view_idx = 0;
7275
7276 fprintf(fp, "Layer %d (pos 0x%lx):\n", layer_idx++,
7277 (unsigned long) layer->position);
7278
7279 if (!weston_layer_mask_is_infinite(layer)) {
7280 fprintf(fp, "\t[mask: (%d, %d) -> (%d,%d)]\n\n",
7281 layer->mask.x1, layer->mask.y1,
7282 layer->mask.x2, layer->mask.y2);
7283 }
7284
Marius Vlada6acfa82019-03-17 18:10:09 +02007285 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
7286 debug_scene_view_print_tree(view, fp, &view_idx);
7287 view_idx++;
7288 }
Daniel Stonece62cb32018-07-20 09:46:24 +01007289
7290 if (wl_list_empty(&layer->view_list.link))
7291 fprintf(fp, "\t[no views]\n");
7292
7293 fprintf(fp, "\n");
7294 }
7295
7296 err = fclose(fp);
7297 assert(err == 0);
7298
7299 return ret;
7300}
7301
7302/**
7303 * Called when the 'scene-graph' debug scope is bound by a client. This
7304 * one-shot weston-debug scope prints the current scene graph when bound,
7305 * and then terminates the stream.
7306 */
7307static void
Marius Vladdad882a2019-07-17 15:43:53 +03007308debug_scene_graph_cb(struct weston_log_subscription *sub, void *data)
Daniel Stonece62cb32018-07-20 09:46:24 +01007309{
7310 struct weston_compositor *ec = data;
7311 char *str = weston_compositor_print_scene_graph(ec);
7312
Marius Vladdad882a2019-07-17 15:43:53 +03007313 weston_log_subscription_printf(sub, "%s", str);
Daniel Stonece62cb32018-07-20 09:46:24 +01007314 free(str);
Marius Vladdad882a2019-07-17 15:43:53 +03007315 weston_log_subscription_complete(sub);
Daniel Stonece62cb32018-07-20 09:46:24 +01007316}
7317
Giulio Camuffo459137b2014-10-11 23:56:24 +03007318/** Create the compositor.
7319 *
7320 * This functions creates and initializes a compositor instance.
7321 *
7322 * \param display The Wayland display to be used.
7323 * \param user_data A pointer to an object that can later be retrieved
Marius Vlada2dace22019-06-12 16:05:44 +03007324 * \param log_ctx A pointer to weston_debug_compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007325 * using the \ref weston_compositor_get_user_data function.
7326 * \return The compositor instance on success or NULL on failure.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007327 *
7328 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007329 */
7330WL_EXPORT struct weston_compositor *
Marius Vlad880b4852019-04-07 17:07:58 +03007331weston_compositor_create(struct wl_display *display,
Marius Vlad3d7d9782019-04-17 12:35:38 +03007332 struct weston_log_context *log_ctx,
Marius Vlad880b4852019-04-07 17:07:58 +03007333 void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04007334{
Giulio Camuffo459137b2014-10-11 23:56:24 +03007335 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05007336 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07007337
Leandro Ribeirobd9c0a62020-01-17 10:47:49 -03007338 if (!log_ctx)
7339 return NULL;
7340
Giulio Camuffo459137b2014-10-11 23:56:24 +03007341 ec = zalloc(sizeof *ec);
7342 if (!ec)
7343 return NULL;
7344
Leandro Ribeirobd9c0a62020-01-17 10:47:49 -03007345 ec->weston_log_ctx = log_ctx;
Giulio Camuffo459137b2014-10-11 23:56:24 +03007346 ec->wl_display = display;
7347 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04007348 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07007349 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04007350 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03007351 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03007352 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02007353 wl_signal_init(&ec->idle_signal);
7354 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02007355 wl_signal_init(&ec->show_input_panel_signal);
7356 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02007357 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01007358 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01007359 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02007360 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02007361 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02007362 wl_signal_init(&ec->output_resized_signal);
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03007363 wl_signal_init(&ec->heads_changed_signal);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02007364 wl_signal_init(&ec->output_heads_changed_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07007365 wl_signal_init(&ec->session_signal);
Robert Beckettc569bdc2019-07-10 16:40:12 +01007366 ec->session_active = true;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04007367
Casey Dahlin58ba1372012-04-19 22:50:08 -04007368 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03007369 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04007370
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02007371 ec->activate_serial = 1;
7372
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05007373 ec->touch_mode = WESTON_TOUCH_MODE_NORMAL;
7374
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05307375 ec->content_protection = NULL;
7376
Derek Foreman152254b2015-11-26 14:17:48 -06007377 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04007378 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007379 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05007380
Giulio Camuffo954f1832014-10-11 18:27:30 +03007381 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04007382 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007383 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03007384
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007385 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007386 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007387 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01007388
Roman Gilge97391c2019-03-29 13:01:06 +01007389 if (!wl_global_create(ec->wl_display, &zxdg_output_manager_v1_interface, 2,
7390 ec, bind_xdg_output_manager))
7391 goto fail;
7392
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007393 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007394 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007395 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007396
Jonas Ådahl30d61d82014-10-22 21:21:17 +02007397 if (weston_input_init(ec) != 0)
7398 goto fail;
7399
Jason Ekstranda7af7042013-10-12 22:38:11 -05007400 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02007401 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007402 wl_list_init(&ec->layer_list);
7403 wl_list_init(&ec->seat_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007404 wl_list_init(&ec->pending_output_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007405 wl_list_init(&ec->output_list);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03007406 wl_list_init(&ec->head_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007407 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01007408 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007409 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01007410 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007411 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02007412 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007413
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007414 wl_list_init(&ec->plugin_api_list);
7415
Xiong Zhang97116532013-10-23 13:58:31 +08007416 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02007417 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04007418
Giulio Camuffo459137b2014-10-11 23:56:24 +03007419 wl_data_device_manager_init(ec->wl_display);
7420
7421 wl_display_init_shm(ec->wl_display);
7422
7423 loop = wl_display_get_event_loop(ec->wl_display);
7424 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Daniel Stone6847b852017-03-01 11:34:08 +00007425 ec->repaint_timer =
7426 wl_event_loop_add_timer(loop, output_repaint_timer_handler,
7427 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007428
Quentin Glidic82681572016-12-17 13:40:51 +01007429 weston_layer_init(&ec->fade_layer, ec);
7430 weston_layer_init(&ec->cursor_layer, ec);
7431
7432 weston_layer_set_position(&ec->fade_layer, WESTON_LAYER_POSITION_FADE);
7433 weston_layer_set_position(&ec->cursor_layer,
7434 WESTON_LAYER_POSITION_CURSOR);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007435
Daniel Stonece62cb32018-07-20 09:46:24 +01007436 ec->debug_scene =
Leandro Ribeiroce100192019-12-26 16:35:49 -03007437 weston_compositor_add_log_scope(ec, "scene-graph",
7438 "Scene graph details\n",
7439 debug_scene_graph_cb, NULL,
7440 ec);
Daniel Stonece62cb32018-07-20 09:46:24 +01007441
Marius Vladda104eb2019-09-05 14:31:01 +03007442 ec->timeline =
Leandro Ribeiroce100192019-12-26 16:35:49 -03007443 weston_compositor_add_log_scope(ec, "timeline",
7444 "Timeline event points\n",
7445 weston_timeline_create_subscription,
7446 weston_timeline_destroy_subscription,
7447 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007448 return ec;
7449
7450fail:
7451 free(ec);
7452 return NULL;
7453}
7454
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007455/** weston_compositor_shutdown
7456 * \ingroup compositor
7457 */
Benjamin Franzkeb8263022011-08-30 11:32:47 +02007458WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007459weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07007460{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007461 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07007462
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007463 wl_event_source_remove(ec->idle_source);
7464
Matt Roper361d2ad2011-08-29 13:52:23 -07007465 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02007466 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07007467 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02007468
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007469 /* Destroy all pending outputs associated with this compositor */
7470 wl_list_for_each_safe(output, next, &ec->pending_output_list, link)
7471 output->destroy(output);
7472
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02007473 if (ec->renderer)
7474 ec->renderer->destroy(ec);
7475
Daniel Stone325fc2d2012-05-30 16:31:58 +01007476 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09007477 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01007478 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01007479 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01007480 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02007481 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007482
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04007483 weston_plane_release(&ec->primary_plane);
Matt Roper361d2ad2011-08-29 13:52:23 -07007484}
7485
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007486/** weston_compositor_exit_with_code
7487 * \ingroup compositor
7488 */
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05007489WL_EXPORT void
Adam Jackson3c3f3b12019-10-16 16:02:59 -04007490weston_compositor_exit_with_code(struct weston_compositor *compositor,
Frederic Plourdec336f062014-10-29 14:44:33 -04007491 int exit_code)
7492{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02007493 if (compositor->exit_code == EXIT_SUCCESS)
7494 compositor->exit_code = exit_code;
7495
Giulio Camuffo459137b2014-10-11 23:56:24 +03007496 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04007497}
7498
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007499/** weston_compositor_set_default_pointer_grab
7500 * \ingroup compositor
7501 */
Frederic Plourdec336f062014-10-29 14:44:33 -04007502WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007503weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
7504 const struct weston_pointer_grab_interface *interface)
7505{
7506 struct weston_seat *seat;
7507
7508 ec->default_pointer_grab = interface;
7509 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05007510 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
7511
7512 if (pointer)
7513 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007514 }
7515}
7516
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007517/** weston_compositor_set_presentation_clock
7518 * \ingroup compositor
7519 */
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007520WL_EXPORT int
7521weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
7522 clockid_t clk_id)
7523{
7524 struct timespec ts;
7525
7526 if (clock_gettime(clk_id, &ts) < 0)
7527 return -1;
7528
7529 compositor->presentation_clock = clk_id;
7530
7531 return 0;
7532}
7533
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007534/** For choosing the software clock, when the display hardware or API
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007535 * does not expose a compatible presentation timestamp.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007536 *
7537 * \ingroup compositor
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007538 */
7539WL_EXPORT int
7540weston_compositor_set_presentation_clock_software(
7541 struct weston_compositor *compositor)
7542{
7543 /* In order of preference */
7544 static const clockid_t clocks[] = {
7545 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
7546 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
7547 CLOCK_MONOTONIC, /* no jumps, may crawl */
7548 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
7549 CLOCK_REALTIME /* may jump and crawl */
7550 };
7551 unsigned i;
7552
7553 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
7554 if (weston_compositor_set_presentation_clock(compositor,
7555 clocks[i]) == 0)
7556 return 0;
7557
7558 weston_log("Error: no suitable presentation clock available.\n");
7559
7560 return -1;
7561}
7562
Pekka Paalanen662f3842015-03-18 12:17:26 +02007563/** Read the current time from the Presentation clock
7564 *
7565 * \param compositor
Marius Vlada2dace22019-06-12 16:05:44 +03007566 * \param[out] ts The current time.
Pekka Paalanen662f3842015-03-18 12:17:26 +02007567 *
7568 * \note Reading the current time in user space is always imprecise to some
7569 * degree.
7570 *
7571 * This function is never meant to fail. If reading the clock does fail,
7572 * an error message is logged and a zero time is returned. Callers are not
7573 * supposed to detect or react to failures.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007574 *
7575 * \ingroup compositor
Pekka Paalanen662f3842015-03-18 12:17:26 +02007576 */
7577WL_EXPORT void
7578weston_compositor_read_presentation_clock(
7579 const struct weston_compositor *compositor,
7580 struct timespec *ts)
7581{
7582 static bool warned;
7583 int ret;
7584
7585 ret = clock_gettime(compositor->presentation_clock, ts);
7586 if (ret < 0) {
7587 ts->tv_sec = 0;
7588 ts->tv_nsec = 0;
7589
7590 if (!warned)
7591 weston_log("Error: failure to read "
Antonio Borneo39578632019-04-26 23:57:31 +02007592 "the presentation clock %#x: '%s' (%d)\n",
7593 compositor->presentation_clock,
7594 strerror(errno), errno);
Pekka Paalanen662f3842015-03-18 12:17:26 +02007595 warned = true;
7596 }
7597}
7598
Pekka Paalanen230f3b12014-09-29 14:18:40 -04007599/** Import dmabuf buffer into current renderer
7600 *
7601 * \param compositor
7602 * \param buffer the dmabuf buffer to import
7603 * \return true on usable buffers, false otherwise
7604 *
7605 * This function tests that the linux_dmabuf_buffer is usable
7606 * for the current renderer. Returns false on unusable buffers. Usually
7607 * usability is tested by importing the dmabufs for composition.
7608 *
7609 * This hook is also used for detecting if the renderer supports
7610 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
7611 * supported.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007612 *
7613 * \ingroup compositor
7614 */
Pekka Paalanen230f3b12014-09-29 14:18:40 -04007615WL_EXPORT bool
7616weston_compositor_import_dmabuf(struct weston_compositor *compositor,
7617 struct linux_dmabuf_buffer *buffer)
7618{
7619 struct weston_renderer *renderer;
7620
7621 renderer = compositor->renderer;
7622
7623 if (renderer->import_dmabuf == NULL)
7624 return false;
7625
7626 return renderer->import_dmabuf(compositor, buffer);
7627}
7628
Marius Vlad5a701542019-11-16 20:26:52 +02007629WL_EXPORT bool
7630weston_compositor_dmabuf_can_scanout(struct weston_compositor *compositor,
7631 struct linux_dmabuf_buffer *buffer)
7632{
7633 struct weston_backend *backend = compositor->backend;
7634
7635 if (backend->can_scanout_dmabuf == NULL)
7636 return false;
7637
7638 return backend->can_scanout_dmabuf(compositor, buffer);
7639}
7640
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007641WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05007642weston_version(int *major, int *minor, int *micro)
7643{
7644 *major = WESTON_VERSION_MAJOR;
7645 *minor = WESTON_VERSION_MINOR;
7646 *micro = WESTON_VERSION_MICRO;
7647}
7648
Daniel Stonee03c1112016-11-24 20:45:45 +00007649/**
7650 * Attempts to find a module path from the module map specified in the
7651 * environment. If found, writes the full path into the path variable.
7652 *
7653 * The module map is a string in environment variable WESTON_MODULE_MAP, where
7654 * each entry is of the form "name=path" and entries are separated by
7655 * semicolons. Whitespace is significant.
7656 *
7657 * \param name The name to search for.
7658 * \param path Where the path is written to if found.
7659 * \param path_len Allocated bytes at \c path .
7660 * \returns The length of the string written to path on success, or 0 if the
7661 * module was not specified in the environment map or path_len was too small.
7662 */
7663WL_EXPORT size_t
7664weston_module_path_from_env(const char *name, char *path, size_t path_len)
7665{
7666 const char *mapping = getenv("WESTON_MODULE_MAP");
7667 const char *end;
7668 const int name_len = strlen(name);
7669
7670 if (!mapping)
7671 return 0;
7672
7673 end = mapping + strlen(mapping);
7674 while (mapping < end && *mapping) {
7675 const char *filename, *next;
7676
7677 /* early out: impossibly short string */
7678 if (end - mapping < name_len + 1)
7679 return 0;
7680
7681 filename = &mapping[name_len + 1];
7682 next = strchrnul(mapping, ';');
7683
7684 if (strncmp(mapping, name, name_len) == 0 &&
7685 mapping[name_len] == '=') {
7686 size_t file_len = next - filename; /* no trailing NUL */
7687 if (file_len >= path_len)
7688 return 0;
7689 strncpy(path, filename, file_len);
7690 path[file_len] = '\0';
7691 return file_len;
7692 }
7693
7694 mapping = next + 1;
7695 }
7696
7697 return 0;
7698}
7699
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03007700WL_EXPORT void *
7701weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007702{
7703 char path[PATH_MAX];
7704 void *module, *init;
Daniel Stonebeb97e52016-11-28 12:13:54 +00007705 size_t len;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007706
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08007707 if (name == NULL)
7708 return NULL;
7709
Derek Foreman3f86e502015-06-08 11:46:54 -05007710 if (name[0] != '/') {
Daniel Stonee03c1112016-11-24 20:45:45 +00007711 len = weston_module_path_from_env(name, path, sizeof path);
7712 if (len == 0)
Daniel Stonebeb97e52016-11-28 12:13:54 +00007713 len = snprintf(path, sizeof path, "%s/%s",
7714 LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05007715 } else {
Daniel Stonebeb97e52016-11-28 12:13:54 +00007716 len = snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05007717 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007718
Daniel Stonebeb97e52016-11-28 12:13:54 +00007719 /* snprintf returns the length of the string it would've written,
7720 * _excluding_ the NUL byte. So even being equal to the size of
7721 * our buffer is an error here. */
7722 if (len >= sizeof path)
7723 return NULL;
7724
Kristian Høgsberga6813d22012-09-12 12:21:01 -04007725 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
7726 if (module) {
7727 weston_log("Module '%s' already loaded\n", path);
Pekka Paalanenf696ee92019-11-04 14:07:51 +02007728 } else {
7729 weston_log("Loading module '%s'\n", path);
7730 module = dlopen(path, RTLD_NOW);
7731 if (!module) {
7732 weston_log("Failed to load module: %s\n", dlerror());
7733 return NULL;
7734 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007735 }
7736
7737 init = dlsym(module, entrypoint);
7738 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03007739 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00007740 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007741 return NULL;
7742 }
7743
7744 return init;
7745}
7746
Pekka Paalanen6ffbba32019-11-06 12:59:32 +02007747/** Add a compositor destroy listener only once
7748 *
7749 * \param compositor The compositor whose destroy to watch for.
7750 * \param listener The listener struct to initialize.
7751 * \param destroy_handler The callback when compositor is destroyed.
7752 * \return True if listener is added, or false if there already is a listener
7753 * with the given \c destroy_handler.
7754 *
7755 * This function does nothing and returns false if the given callback function
7756 * is already present in the weston_compositor destroy callbacks list.
7757 * Otherwise, this function initializes the given listener with the given
7758 * callback pointer and adds it to the compositor's destroy callbacks list.
7759 *
7760 * This can be used to ensure that plugin initialization is done only once
7761 * in case the same plugin is loaded multiple times. If this function returns
7762 * false, the plugin should be already initialized successfully.
7763 *
7764 * All plugins should register a destroy listener for cleaning up. Note, that
7765 * the plugin destruction order is not guaranteed: plugins that depend on other
7766 * plugins must be able to be torn down in arbitrary order.
7767 *
Leandro Ribeiroca640d52020-01-27 19:12:01 -03007768 * \sa weston_compositor_destroy
Pekka Paalanen6ffbba32019-11-06 12:59:32 +02007769 */
7770WL_EXPORT bool
7771weston_compositor_add_destroy_listener_once(struct weston_compositor *compositor,
7772 struct wl_listener *listener,
7773 wl_notify_func_t destroy_handler)
7774{
7775 if (wl_signal_get(&compositor->destroy_signal, destroy_handler))
7776 return false;
7777
7778 listener->notify = destroy_handler;
7779 wl_signal_add(&compositor->destroy_signal, listener);
7780 return true;
7781}
Giulio Camuffo459137b2014-10-11 23:56:24 +03007782
Leandro Ribeiroca640d52020-01-27 19:12:01 -03007783/** Destroys the compositor.
Giulio Camuffo459137b2014-10-11 23:56:24 +03007784 *
Leandro Ribeiroca640d52020-01-27 19:12:01 -03007785 * This function cleans up the compositor state and then destroys it.
Giulio Camuffo459137b2014-10-11 23:56:24 +03007786 *
Leandro Ribeiroca640d52020-01-27 19:12:01 -03007787 * @param compositor The compositor to be destroyed.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007788 *
Marius Vlad284d5342019-06-24 12:00:47 +03007789 * @ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007790 */
7791WL_EXPORT void
Leandro Ribeiroca640d52020-01-27 19:12:01 -03007792weston_compositor_destroy(struct weston_compositor *compositor)
Giulio Camuffo459137b2014-10-11 23:56:24 +03007793{
7794 /* prevent further rendering while shutting down */
7795 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
7796
7797 wl_signal_emit(&compositor->destroy_signal, compositor);
7798
7799 weston_compositor_xkb_destroy(compositor);
7800
Giulio Camuffo2d24e642015-10-03 16:25:15 +03007801 if (compositor->backend)
7802 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007803
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03007804 /* The backend is responsible for destroying the heads. */
7805 assert(wl_list_empty(&compositor->head_list));
7806
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007807 weston_plugin_api_destroy_list(compositor);
7808
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03007809 if (compositor->heads_changed_source)
7810 wl_event_source_remove(compositor->heads_changed_source);
7811
Leandro Ribeirof0149642019-12-18 15:52:18 -03007812 weston_log_scope_destroy(compositor->debug_scene);
Daniel Stonece62cb32018-07-20 09:46:24 +01007813 compositor->debug_scene = NULL;
Marius Vladda104eb2019-09-05 14:31:01 +03007814
Leandro Ribeirof0149642019-12-18 15:52:18 -03007815 weston_log_scope_destroy(compositor->timeline);
Marius Vladda104eb2019-09-05 14:31:01 +03007816 compositor->timeline = NULL;
Pekka Paalanena5630ea2017-10-12 13:13:42 +02007817
Giulio Camuffo459137b2014-10-11 23:56:24 +03007818 free(compositor);
7819}
7820
7821/** Instruct the compositor to exit.
7822 *
7823 * This functions does not directly destroy the compositor object, it merely
7824 * command it to start the tear down process. It is not guaranteed that the
7825 * tear down will happen immediately.
7826 *
7827 * \param compositor The compositor to tear down.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007828 *
7829 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007830 */
7831WL_EXPORT void
7832weston_compositor_exit(struct weston_compositor *compositor)
7833{
7834 compositor->exit(compositor);
7835}
7836
7837/** Return the user data stored in the compositor.
7838 *
7839 * This function returns the user data pointer set with user_data parameter
7840 * to the \ref weston_compositor_create function.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007841 *
7842 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007843 */
7844WL_EXPORT void *
7845weston_compositor_get_user_data(struct weston_compositor *compositor)
7846{
7847 return compositor->user_data;
7848}
Pekka Paalanendd186732016-06-03 14:49:54 +03007849
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007850static const char * const backend_map[] = {
7851 [WESTON_BACKEND_DRM] = "drm-backend.so",
7852 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
7853 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
7854 [WESTON_BACKEND_RDP] = "rdp-backend.so",
7855 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
7856 [WESTON_BACKEND_X11] = "x11-backend.so",
7857};
7858
Pekka Paalanendd186732016-06-03 14:49:54 +03007859/** Load a backend into a weston_compositor
7860 *
7861 * A backend must be loaded to make a weston_compositor work. A backend
7862 * provides input and output capabilities, and determines the renderer to use.
7863 *
7864 * \param compositor A compositor that has not had a backend loaded yet.
7865 * \param backend Name of the backend file.
7866 * \param config_base A pointer to a backend-specific configuration
7867 * structure's 'base' member.
7868 *
7869 * \return 0 on success, or -1 on error.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007870 *
7871 * \ingroup compositor
Pekka Paalanendd186732016-06-03 14:49:54 +03007872 */
7873WL_EXPORT int
7874weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007875 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03007876 struct weston_backend_config *config_base)
7877{
7878 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03007879 struct weston_backend_config *config_base);
7880
Pekka Paalanend7e35112017-08-29 17:04:12 +03007881 if (compositor->backend) {
7882 weston_log("Error: attempt to load a backend when one is already loaded\n");
7883 return -1;
7884 }
7885
Quentin Glidic887c0182016-07-10 11:00:53 +02007886 if (backend >= ARRAY_LENGTH(backend_map))
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007887 return -1;
7888
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01007889 backend_init = weston_load_module(backend_map[backend], "weston_backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03007890 if (!backend_init)
7891 return -1;
7892
Pekka Paalanend7e35112017-08-29 17:04:12 +03007893 if (backend_init(compositor, config_base) < 0) {
7894 compositor->backend = NULL;
7895 return -1;
7896 }
7897
7898 return 0;
Pekka Paalanendd186732016-06-03 14:49:54 +03007899}
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007900
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007901/** weston_compositor_load_xwayland
7902 * \ingroup compositor
7903 */
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007904WL_EXPORT int
7905weston_compositor_load_xwayland(struct weston_compositor *compositor)
7906{
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007907 int (*module_init)(struct weston_compositor *ec);
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007908
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007909 module_init = weston_load_module("xwayland.so", "weston_module_init");
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007910 if (!module_init)
7911 return -1;
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007912 if (module_init(compositor) < 0)
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007913 return -1;
7914 return 0;
7915}
Vasilis Liaskovitis486b4632018-10-10 16:14:55 +02007916
7917/** Resolve an internal compositor error by disconnecting the client.
7918 *
7919 * This function is used in cases when the wl_buffer turns out
7920 * unusable and there is no fallback path.
7921 *
7922 * It is possible the fault is caused by a compositor bug, the underlying
7923 * graphics stack bug or normal behaviour, or perhaps a client mistake.
7924 * In any case, the options are to either composite garbage or nothing,
7925 * or disconnect the client. This is a helper function for the latter.
7926 *
7927 * The error is sent as an INVALID_OBJECT error on the client's wl_display.
7928 *
7929 * \param buffer The weston buffer that is unusable.
7930 * \param msg A custom error message attached to the protocol error.
7931 */
7932WL_EXPORT void
7933weston_buffer_send_server_error(struct weston_buffer *buffer,
7934 const char *msg)
7935{
7936 struct wl_client *client;
7937 struct wl_resource *display_resource;
7938 uint32_t id;
7939
7940 assert(buffer->resource);
7941 id = wl_resource_get_id(buffer->resource);
7942 client = wl_resource_get_client(buffer->resource);
7943 display_resource = wl_client_get_object(client, 1);
7944
7945 assert(display_resource);
7946 wl_resource_post_error(display_resource,
7947 WL_DISPLAY_ERROR_INVALID_OBJECT,
7948 "server error with "
7949 "wl_buffer@%u: %s", id, msg);
7950}
Ankit Nautiyal93dde242019-07-08 11:46:42 +05307951
7952WL_EXPORT void
7953weston_output_disable_planes_incr(struct weston_output *output)
7954{
7955 output->disable_planes++;
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05307956 /*
7957 * If disable_planes changes from 0 to non-zero, it means some type of
7958 * recording of content has started, and therefore protection level of
7959 * the protected surfaces must be updated to avoid the recording of
7960 * the protected content.
7961 */
7962 if (output->disable_planes == 1)
7963 weston_schedule_surface_protection_update(output->compositor);
Ankit Nautiyal93dde242019-07-08 11:46:42 +05307964}
7965
7966WL_EXPORT void
7967weston_output_disable_planes_decr(struct weston_output *output)
7968{
7969 output->disable_planes--;
Ankit Nautiyal4fd38132019-07-08 15:12:19 +05307970 /*
7971 * If disable_planes changes from non-zero to 0, it means no content
7972 * recording is going on any more, and the protected and surfaces can be
7973 * shown without any apprehensions about content being recorded.
7974 */
7975 if (output->disable_planes == 0)
7976 weston_schedule_surface_protection_update(output->compositor);
7977
Ankit Nautiyal93dde242019-07-08 11:46:42 +05307978}