blob: 85327159a1d9047c2e38e4d773506213b3f7dcbe [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
156 pixman_region32_init(&output->previous_damage);
157 pixman_region32_init_rect(&output->region, output->x, output->y,
158 output->width, output->height);
159
160 weston_output_update_matrix(output);
161
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200162 /* If a pointer falls outside the outputs new geometry, move it to its
163 * lower-right corner */
164 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500165 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200166 int32_t x, y;
167
168 if (!pointer)
169 continue;
170
171 x = wl_fixed_to_int(pointer->x);
172 y = wl_fixed_to_int(pointer->y);
173
174 if (!pixman_region32_contains_point(&old_output_region,
175 x, y, NULL) ||
176 pixman_region32_contains_point(&output->region,
177 x, y, NULL))
178 continue;
179
180 if (x >= output->x + output->width)
181 x = output->x + output->width - 1;
182 if (y >= output->y + output->height)
183 y = output->y + output->height - 1;
184
185 pointer->x = wl_fixed_from_int(x);
186 pointer->y = wl_fixed_from_int(y);
187 }
188
189 pixman_region32_fini(&old_output_region);
190
Derek Foremandd4cd332014-11-10 10:29:59 -0600191 if (!mode_changed && !scale_changed)
192 return;
193
Hardening57388e42013-09-18 23:56:36 +0200194 /* notify clients of the changes */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300195 wl_list_for_each(head, &output->head_list, output_link)
196 weston_mode_switch_send_events(head,
197 mode_changed, scale_changed);
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600198}
199
David Fort0de859e2016-05-27 23:22:57 +0200200static void
201weston_compositor_reflow_outputs(struct weston_compositor *compositor,
202 struct weston_output *resized_output, int delta_width);
203
Marius Vlad55d87362019-06-11 01:15:35 +0300204/**
205 * \ingroup output
206 */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600207WL_EXPORT int
208weston_output_mode_set_native(struct weston_output *output,
209 struct weston_mode *mode,
210 int32_t scale)
211{
212 int ret;
213 int mode_changed = 0, scale_changed = 0;
David Fort0de859e2016-05-27 23:22:57 +0200214 int32_t old_width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600215
216 if (!output->switch_mode)
217 return -1;
218
219 if (!output->original_mode) {
220 mode_changed = 1;
221 ret = output->switch_mode(output, mode);
222 if (ret < 0)
223 return ret;
224 if (output->current_scale != scale) {
225 scale_changed = 1;
226 output->current_scale = scale;
Hardening57388e42013-09-18 23:56:36 +0200227 }
228 }
229
David Fort0de859e2016-05-27 23:22:57 +0200230 old_width = output->width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600231 output->native_mode = mode;
232 output->native_scale = scale;
233
234 weston_mode_switch_finish(output, mode_changed, scale_changed);
235
David Fort0de859e2016-05-27 23:22:57 +0200236 if (mode_changed || scale_changed) {
237 weston_compositor_reflow_outputs(output->compositor, output, output->width - old_width);
238
239 wl_signal_emit(&output->compositor->output_resized_signal, output);
240 }
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600241 return 0;
242}
243
Marius Vlad55d87362019-06-11 01:15:35 +0300244/**
245 * \ingroup output
246 */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600247WL_EXPORT int
248weston_output_mode_switch_to_native(struct weston_output *output)
249{
250 int ret;
251 int mode_changed = 0, scale_changed = 0;
252
253 if (!output->switch_mode)
254 return -1;
255
256 if (!output->original_mode) {
257 weston_log("already in the native mode\n");
258 return -1;
259 }
260 /* the non fullscreen clients haven't seen a mode set since we
261 * switched into a temporary, so we need to notify them if the
262 * mode at that time is different from the native mode now.
263 */
264 mode_changed = (output->original_mode != output->native_mode);
265 scale_changed = (output->original_scale != output->native_scale);
266
267 ret = output->switch_mode(output, output->native_mode);
268 if (ret < 0)
269 return ret;
270
271 output->current_scale = output->native_scale;
272
273 output->original_mode = NULL;
274 output->original_scale = 0;
275
276 weston_mode_switch_finish(output, mode_changed, scale_changed);
277
278 return 0;
279}
280
Marius Vlad55d87362019-06-11 01:15:35 +0300281/**
282 * \ingroup output
283 */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600284WL_EXPORT int
285weston_output_mode_switch_to_temporary(struct weston_output *output,
286 struct weston_mode *mode,
287 int32_t scale)
288{
289 int ret;
290
291 if (!output->switch_mode)
292 return -1;
293
294 /* original_mode is the last mode non full screen clients have seen,
295 * so we shouldn't change it if we already have one set.
296 */
297 if (!output->original_mode) {
298 output->original_mode = output->native_mode;
299 output->original_scale = output->native_scale;
300 }
301 ret = output->switch_mode(output, mode);
302 if (ret < 0)
303 return ret;
304
305 output->current_scale = scale;
306
307 weston_mode_switch_finish(output, 0, 0);
308
309 return 0;
Alex Wu2dda6042012-04-17 17:20:47 +0800310}
311
Benjamin Franzke06286262011-05-06 19:12:33 +0200312static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300313region_init_infinite(pixman_region32_t *region)
314{
315 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
316 UINT32_MAX, UINT32_MAX);
317}
318
Pekka Paalanene67858b2013-04-25 13:57:42 +0300319static struct weston_subsurface *
320weston_surface_to_subsurface(struct weston_surface *surface);
321
Jason Ekstranda7af7042013-10-12 22:38:11 -0500322WL_EXPORT struct weston_view *
323weston_view_create(struct weston_surface *surface)
324{
325 struct weston_view *view;
326
Bryce Harringtonde16d892014-11-20 22:21:57 -0800327 view = zalloc(sizeof *view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500328 if (view == NULL)
329 return NULL;
330
331 view->surface = surface;
Daniel Stonefb4869d2016-12-09 16:27:54 +0000332 view->plane = &surface->compositor->primary_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500333
Jason Ekstranda7af7042013-10-12 22:38:11 -0500334 /* Assign to surface */
335 wl_list_insert(&surface->views, &view->surface_link);
336
337 wl_signal_init(&view->destroy_signal);
338 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300339 wl_list_init(&view->layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500340
Jason Ekstranda7af7042013-10-12 22:38:11 -0500341 pixman_region32_init(&view->clip);
342
343 view->alpha = 1.0;
344 pixman_region32_init(&view->transform.opaque);
345
346 wl_list_init(&view->geometry.transformation_list);
347 wl_list_insert(&view->geometry.transformation_list,
348 &view->transform.position.link);
349 weston_matrix_init(&view->transform.position.matrix);
350 wl_list_init(&view->geometry.child_list);
Pekka Paalanen380adf52015-02-16 14:39:11 +0200351 pixman_region32_init(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500352 pixman_region32_init(&view->transform.boundingbox);
353 view->transform.dirty = 1;
354
Jason Ekstranda7af7042013-10-12 22:38:11 -0500355 return view;
356}
357
Jason Ekstrand108865d2014-06-26 10:04:49 -0700358struct weston_frame_callback {
359 struct wl_resource *resource;
360 struct wl_list link;
361};
362
Pekka Paalanen133e4392014-09-23 22:08:46 -0400363struct weston_presentation_feedback {
364 struct wl_resource *resource;
365
366 /* XXX: could use just wl_resource_get_link() instead */
367 struct wl_list link;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200368
369 /* The per-surface feedback flags */
370 uint32_t psf_flags;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400371};
372
373static void
374weston_presentation_feedback_discard(
375 struct weston_presentation_feedback *feedback)
376{
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200377 wp_presentation_feedback_send_discarded(feedback->resource);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400378 wl_resource_destroy(feedback->resource);
379}
380
381static void
382weston_presentation_feedback_discard_list(struct wl_list *list)
383{
384 struct weston_presentation_feedback *feedback, *tmp;
385
386 wl_list_for_each_safe(feedback, tmp, list, link)
387 weston_presentation_feedback_discard(feedback);
388}
389
390static void
391weston_presentation_feedback_present(
392 struct weston_presentation_feedback *feedback,
393 struct weston_output *output,
394 uint32_t refresh_nsec,
395 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200396 uint64_t seq,
397 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400398{
399 struct wl_client *client = wl_resource_get_client(feedback->resource);
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300400 struct weston_head *head;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400401 struct wl_resource *o;
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200402 uint32_t tv_sec_hi;
403 uint32_t tv_sec_lo;
404 uint32_t tv_nsec;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300405 bool done = false;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400406
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300407 wl_list_for_each(head, &output->head_list, output_link) {
408 wl_resource_for_each(o, &head->resource_list) {
409 if (wl_resource_get_client(o) != client)
410 continue;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400411
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300412 wp_presentation_feedback_send_sync_output(feedback->resource, o);
413 done = true;
414 }
415
416 /* For clone mode, send it for just one wl_output global,
417 * they are all equivalent anyway.
418 */
419 if (done)
420 break;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400421 }
422
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200423 timespec_to_proto(ts, &tv_sec_hi, &tv_sec_lo, &tv_nsec);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200424 wp_presentation_feedback_send_presented(feedback->resource,
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200425 tv_sec_hi, tv_sec_lo, tv_nsec,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200426 refresh_nsec,
427 seq >> 32, seq & 0xffffffff,
428 flags | feedback->psf_flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400429 wl_resource_destroy(feedback->resource);
430}
431
432static void
433weston_presentation_feedback_present_list(struct wl_list *list,
434 struct weston_output *output,
435 uint32_t refresh_nsec,
436 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200437 uint64_t seq,
438 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400439{
440 struct weston_presentation_feedback *feedback, *tmp;
441
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200442 assert(!(flags & WP_PRESENTATION_FEEDBACK_INVALID) ||
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200443 wl_list_empty(list));
444
Pekka Paalanen133e4392014-09-23 22:08:46 -0400445 wl_list_for_each_safe(feedback, tmp, list, link)
446 weston_presentation_feedback_present(feedback, output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200447 refresh_nsec, ts, seq,
448 flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400449}
450
Jason Ekstrand7b982072014-05-20 14:33:03 -0500451static void
452surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
453{
454 struct weston_surface_state *state =
455 container_of(listener, struct weston_surface_state,
456 buffer_destroy_listener);
457
458 state->buffer = NULL;
459}
460
461static void
462weston_surface_state_init(struct weston_surface_state *state)
463{
464 state->newly_attached = 0;
465 state->buffer = NULL;
466 state->buffer_destroy_listener.notify =
467 surface_state_handle_buffer_destroy;
468 state->sx = 0;
469 state->sy = 0;
470
Derek Foreman152254b2015-11-26 14:17:48 -0600471 pixman_region32_init(&state->damage_surface);
472 pixman_region32_init(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500473 pixman_region32_init(&state->opaque);
474 region_init_infinite(&state->input);
475
476 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400477 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500478
479 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
480 state->buffer_viewport.buffer.scale = 1;
481 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
482 state->buffer_viewport.surface.width = -1;
483 state->buffer_viewport.changed = 0;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300484
485 state->acquire_fence_fd = -1;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +0530486
487 state->desired_protection = WESTON_HDCP_DISABLE;
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +0530488 state->protection_mode = WESTON_SURFACE_PROTECTION_MODE_RELAXED;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500489}
490
491static void
492weston_surface_state_fini(struct weston_surface_state *state)
493{
494 struct weston_frame_callback *cb, *next;
495
496 wl_list_for_each_safe(cb, next,
497 &state->frame_callback_list, link)
498 wl_resource_destroy(cb->resource);
499
Pekka Paalanen133e4392014-09-23 22:08:46 -0400500 weston_presentation_feedback_discard_list(&state->feedback_list);
501
Jason Ekstrand7b982072014-05-20 14:33:03 -0500502 pixman_region32_fini(&state->input);
503 pixman_region32_fini(&state->opaque);
Derek Foreman152254b2015-11-26 14:17:48 -0600504 pixman_region32_fini(&state->damage_surface);
505 pixman_region32_fini(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500506
507 if (state->buffer)
508 wl_list_remove(&state->buffer_destroy_listener.link);
509 state->buffer = NULL;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300510
511 fd_clear(&state->acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +0300512 weston_buffer_release_reference(&state->buffer_release_ref, NULL);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500513}
514
515static void
516weston_surface_state_set_buffer(struct weston_surface_state *state,
517 struct weston_buffer *buffer)
518{
519 if (state->buffer == buffer)
520 return;
521
522 if (state->buffer)
523 wl_list_remove(&state->buffer_destroy_listener.link);
524 state->buffer = buffer;
525 if (state->buffer)
526 wl_signal_add(&state->buffer->destroy_signal,
527 &state->buffer_destroy_listener);
528}
529
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500530WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500531weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500532{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500533 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400534
Bryce Harringtonde16d892014-11-20 22:21:57 -0800535 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400536 if (surface == NULL)
537 return NULL;
538
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500539 wl_signal_init(&surface->destroy_signal);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +0800540 wl_signal_init(&surface->commit_signal);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500541
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500542 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200543 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400544
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200545 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
546 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200547 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
548 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500549
550 weston_surface_state_init(&surface->pending);
551
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400552 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500553 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300554 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400555
Jason Ekstranda7af7042013-10-12 22:38:11 -0500556 wl_list_init(&surface->views);
557
558 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400559 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500560
Pekka Paalanene67858b2013-04-25 13:57:42 +0300561 wl_list_init(&surface->subsurface_list);
562 wl_list_init(&surface->subsurface_list_pending);
563
Jason Ekstrand1e059042014-10-16 10:55:19 -0500564 weston_matrix_init(&surface->buffer_to_surface_matrix);
565 weston_matrix_init(&surface->surface_to_buffer_matrix);
566
Jonas Ådahld3414f22016-07-22 17:56:31 +0800567 wl_list_init(&surface->pointer_constraints);
568
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300569 surface->acquire_fence_fd = -1;
570
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +0530571 surface->desired_protection = WESTON_HDCP_DISABLE;
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +0530572 surface->current_protection = WESTON_HDCP_DISABLE;
573 surface->protection_mode = WESTON_SURFACE_PROTECTION_MODE_RELAXED;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +0530574
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400575 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500576}
577
Alex Wu8811bf92012-02-28 18:07:54 +0800578WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500579weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200580 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500581{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100582 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Philipp Zabel195dade2018-09-03 19:44:59 +0200583 surface->is_opaque = !(alpha < 1.0);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500584}
585
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400586WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500587weston_view_to_global_float(struct weston_view *view,
588 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200589{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500590 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200591 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
592
Jason Ekstranda7af7042013-10-12 22:38:11 -0500593 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200594
595 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200596 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700597 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200598 v.f[3]);
599 *x = 0;
600 *y = 0;
601 return;
602 }
603
604 *x = v.f[0] / v.f[3];
605 *y = v.f[1] / v.f[3];
606 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500607 *x = sx + view->geometry.x;
608 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200609 }
610}
611
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500612WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200613weston_transformed_coord(int width, int height,
614 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200615 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200616 float sx, float sy, float *bx, float *by)
617{
618 switch (transform) {
619 case WL_OUTPUT_TRANSFORM_NORMAL:
620 default:
621 *bx = sx;
622 *by = sy;
623 break;
624 case WL_OUTPUT_TRANSFORM_FLIPPED:
625 *bx = width - sx;
626 *by = sy;
627 break;
628 case WL_OUTPUT_TRANSFORM_90:
629 *bx = height - sy;
630 *by = sx;
631 break;
632 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
633 *bx = height - sy;
634 *by = width - sx;
635 break;
636 case WL_OUTPUT_TRANSFORM_180:
637 *bx = width - sx;
638 *by = height - sy;
639 break;
640 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
641 *bx = sx;
642 *by = height - sy;
643 break;
644 case WL_OUTPUT_TRANSFORM_270:
645 *bx = sy;
646 *by = width - sx;
647 break;
648 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
649 *bx = sy;
650 *by = sx;
651 break;
652 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200653
654 *bx *= scale;
655 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200656}
657
658WL_EXPORT pixman_box32_t
659weston_transformed_rect(int width, int height,
660 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200661 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200662 pixman_box32_t rect)
663{
664 float x1, x2, y1, y2;
665
666 pixman_box32_t ret;
667
Alexander Larsson4ea95522013-05-22 14:41:37 +0200668 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200669 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200670 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200671 rect.x2, rect.y2, &x2, &y2);
672
673 if (x1 <= x2) {
674 ret.x1 = x1;
675 ret.x2 = x2;
676 } else {
677 ret.x1 = x2;
678 ret.x2 = x1;
679 }
680
681 if (y1 <= y2) {
682 ret.y1 = y1;
683 ret.y2 = y2;
684 } else {
685 ret.y1 = y2;
686 ret.y2 = y1;
687 }
688
689 return ret;
690}
691
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600692/** Transform a region by a matrix, restricted to axis-aligned transformations
693 *
694 * Warning: This function does not work for projective, affine, or matrices
695 * that encode arbitrary rotations. Only 90-degree step rotations are
696 * supported.
697 */
698WL_EXPORT void
699weston_matrix_transform_region(pixman_region32_t *dest,
700 struct weston_matrix *matrix,
701 pixman_region32_t *src)
702{
703 pixman_box32_t *src_rects, *dest_rects;
704 int nrects, i;
705
706 src_rects = pixman_region32_rectangles(src, &nrects);
707 dest_rects = malloc(nrects * sizeof(*dest_rects));
708 if (!dest_rects)
709 return;
710
711 for (i = 0; i < nrects; i++) {
712 struct weston_vector vec1 = {{
713 src_rects[i].x1, src_rects[i].y1, 0, 1
714 }};
715 weston_matrix_transform(matrix, &vec1);
716 vec1.f[0] /= vec1.f[3];
717 vec1.f[1] /= vec1.f[3];
718
719 struct weston_vector vec2 = {{
720 src_rects[i].x2, src_rects[i].y2, 0, 1
721 }};
722 weston_matrix_transform(matrix, &vec2);
723 vec2.f[0] /= vec2.f[3];
724 vec2.f[1] /= vec2.f[3];
725
726 if (vec1.f[0] < vec2.f[0]) {
727 dest_rects[i].x1 = floor(vec1.f[0]);
728 dest_rects[i].x2 = ceil(vec2.f[0]);
729 } else {
730 dest_rects[i].x1 = floor(vec2.f[0]);
731 dest_rects[i].x2 = ceil(vec1.f[0]);
732 }
733
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600734 if (vec1.f[1] < vec2.f[1]) {
735 dest_rects[i].y1 = floor(vec1.f[1]);
736 dest_rects[i].y2 = ceil(vec2.f[1]);
737 } else {
738 dest_rects[i].y1 = floor(vec2.f[1]);
739 dest_rects[i].y2 = ceil(vec1.f[1]);
740 }
741 }
742
743 pixman_region32_clear(dest);
744 pixman_region32_init_rects(dest, dest_rects, nrects);
745 free(dest_rects);
746}
747
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200748WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500749weston_transformed_region(int width, int height,
750 enum wl_output_transform transform,
751 int32_t scale,
752 pixman_region32_t *src, pixman_region32_t *dest)
753{
754 pixman_box32_t *src_rects, *dest_rects;
755 int nrects, i;
756
757 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
758 if (src != dest)
759 pixman_region32_copy(dest, src);
760 return;
761 }
762
763 src_rects = pixman_region32_rectangles(src, &nrects);
764 dest_rects = malloc(nrects * sizeof(*dest_rects));
765 if (!dest_rects)
766 return;
767
768 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
769 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
770 } else {
771 for (i = 0; i < nrects; i++) {
772 switch (transform) {
773 default:
774 case WL_OUTPUT_TRANSFORM_NORMAL:
775 dest_rects[i].x1 = src_rects[i].x1;
776 dest_rects[i].y1 = src_rects[i].y1;
777 dest_rects[i].x2 = src_rects[i].x2;
778 dest_rects[i].y2 = src_rects[i].y2;
779 break;
780 case WL_OUTPUT_TRANSFORM_90:
781 dest_rects[i].x1 = height - src_rects[i].y2;
782 dest_rects[i].y1 = src_rects[i].x1;
783 dest_rects[i].x2 = height - src_rects[i].y1;
784 dest_rects[i].y2 = src_rects[i].x2;
785 break;
786 case WL_OUTPUT_TRANSFORM_180:
787 dest_rects[i].x1 = width - src_rects[i].x2;
788 dest_rects[i].y1 = height - src_rects[i].y2;
789 dest_rects[i].x2 = width - src_rects[i].x1;
790 dest_rects[i].y2 = height - src_rects[i].y1;
791 break;
792 case WL_OUTPUT_TRANSFORM_270:
793 dest_rects[i].x1 = src_rects[i].y1;
794 dest_rects[i].y1 = width - src_rects[i].x2;
795 dest_rects[i].x2 = src_rects[i].y2;
796 dest_rects[i].y2 = width - src_rects[i].x1;
797 break;
798 case WL_OUTPUT_TRANSFORM_FLIPPED:
799 dest_rects[i].x1 = width - src_rects[i].x2;
800 dest_rects[i].y1 = src_rects[i].y1;
801 dest_rects[i].x2 = width - src_rects[i].x1;
802 dest_rects[i].y2 = src_rects[i].y2;
803 break;
804 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
805 dest_rects[i].x1 = height - src_rects[i].y2;
806 dest_rects[i].y1 = width - src_rects[i].x2;
807 dest_rects[i].x2 = height - src_rects[i].y1;
808 dest_rects[i].y2 = width - src_rects[i].x1;
809 break;
810 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
811 dest_rects[i].x1 = src_rects[i].x1;
812 dest_rects[i].y1 = height - src_rects[i].y2;
813 dest_rects[i].x2 = src_rects[i].x2;
814 dest_rects[i].y2 = height - src_rects[i].y1;
815 break;
816 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
817 dest_rects[i].x1 = src_rects[i].y1;
818 dest_rects[i].y1 = src_rects[i].x1;
819 dest_rects[i].x2 = src_rects[i].y2;
820 dest_rects[i].y2 = src_rects[i].x2;
821 break;
822 }
823 }
824 }
825
826 if (scale != 1) {
827 for (i = 0; i < nrects; i++) {
828 dest_rects[i].x1 *= scale;
829 dest_rects[i].x2 *= scale;
830 dest_rects[i].y1 *= scale;
831 dest_rects[i].y2 *= scale;
832 }
833 }
834
835 pixman_region32_clear(dest);
836 pixman_region32_init_rects(dest, dest_rects, nrects);
837 free(dest_rects);
838}
839
Jonny Lamb74130762013-11-26 18:19:46 +0100840static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300841viewport_surface_to_buffer(struct weston_surface *surface,
842 float sx, float sy, float *bx, float *by)
Jonny Lamb74130762013-11-26 18:19:46 +0100843{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200844 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200845 double src_width, src_height;
846 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200847
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200848 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
849 if (vp->surface.width == -1) {
850 *bx = sx;
851 *by = sy;
852 return;
853 }
Jonny Lamb74130762013-11-26 18:19:46 +0100854
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200855 src_x = 0.0;
856 src_y = 0.0;
857 src_width = surface->width_from_buffer;
858 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100859 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200860 src_x = wl_fixed_to_double(vp->buffer.src_x);
861 src_y = wl_fixed_to_double(vp->buffer.src_y);
862 src_width = wl_fixed_to_double(vp->buffer.src_width);
863 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100864 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200865
866 *bx = sx * src_width / surface->width + src_x;
867 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100868}
869
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500870WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200871weston_surface_to_buffer_float(struct weston_surface *surface,
872 float sx, float sy, float *bx, float *by)
873{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200874 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
875
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300876 /* first transform coordinates if the viewport is set */
877 viewport_surface_to_buffer(surface, sx, sy, bx, by);
Jonny Lamb74130762013-11-26 18:19:46 +0100878
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500879 weston_transformed_coord(surface->width_from_buffer,
880 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200881 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100882 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200883}
884
Derek Foreman99739672015-12-03 16:38:11 -0600885/** Transform a rectangle from surface coordinates to buffer coordinates
886 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300887 * \param surface The surface to fetch wp_viewport and buffer transformation
Derek Foreman99739672015-12-03 16:38:11 -0600888 * from.
889 * \param rect The rectangle to transform.
890 * \return The transformed rectangle.
891 *
892 * Viewport and buffer transformations can only do translation, scaling,
893 * and rotations in 90-degree steps. Therefore the only loss in the
894 * conversion is coordinate rounding.
895 *
896 * However, some coordinate rounding takes place as an intermediate
897 * step before the buffer scale factor is applied, so the rectangle
898 * boundary may not be exactly as expected.
899 *
900 * This is OK for damage tracking since a little extra coverage is
901 * not a problem.
902 */
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200903WL_EXPORT pixman_box32_t
904weston_surface_to_buffer_rect(struct weston_surface *surface,
905 pixman_box32_t rect)
906{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200907 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100908 float xf, yf;
909
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300910 /* first transform box coordinates if the viewport is set */
911 viewport_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100912 rect.x1 = floorf(xf);
913 rect.y1 = floorf(yf);
914
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300915 viewport_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
Derek Foremane2e15ac2015-12-01 13:00:43 -0600916 rect.x2 = ceilf(xf);
917 rect.y2 = ceilf(yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100918
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500919 return weston_transformed_rect(surface->width_from_buffer,
920 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200921 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200922 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200923}
924
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200925/** Transform a region from surface coordinates to buffer coordinates
926 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300927 * \param surface The surface to fetch wp_viewport and buffer transformation
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200928 * from.
Marius Vlada2dace22019-06-12 16:05:44 +0300929 * \param[in] surface_region The region in surface coordinates.
930 * \param[out] buffer_region The region converted to buffer coordinates.
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200931 *
932 * Buffer_region must be init'd, but will be completely overwritten.
933 *
934 * Viewport and buffer transformations can only do translation, scaling,
935 * and rotations in 90-degree steps. Therefore the only loss in the
Derek Foreman99739672015-12-03 16:38:11 -0600936 * conversion is from the coordinate rounding that takes place in
937 * \ref weston_surface_to_buffer_rect.
Marius Vlada2dace22019-06-12 16:05:44 +0300938 *
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200939 */
940WL_EXPORT void
941weston_surface_to_buffer_region(struct weston_surface *surface,
942 pixman_region32_t *surface_region,
943 pixman_region32_t *buffer_region)
944{
945 pixman_box32_t *src_rects, *dest_rects;
946 int nrects, i;
947
948 src_rects = pixman_region32_rectangles(surface_region, &nrects);
949 dest_rects = malloc(nrects * sizeof(*dest_rects));
950 if (!dest_rects)
951 return;
952
953 for (i = 0; i < nrects; i++) {
954 dest_rects[i] = weston_surface_to_buffer_rect(surface,
955 src_rects[i]);
956 }
957
958 pixman_region32_fini(buffer_region);
959 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
960 free(dest_rects);
961}
962
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200963WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500964weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400965 struct weston_plane *plane)
966{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500967 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400968 return;
969
Jason Ekstranda7af7042013-10-12 22:38:11 -0500970 weston_view_damage_below(view);
971 view->plane = plane;
972 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400973}
974
Pekka Paalanen51723d52015-02-17 13:10:01 +0200975/** Inflict damage on the plane where the view is visible.
976 *
977 * \param view The view that causes the damage.
978 *
979 * If the view is currently on a plane (including the primary plane),
980 * take the view's boundingbox, subtract all the opaque views that cover it,
981 * and add the remaining region as damage to the plane. This corresponds
982 * to the damage inflicted to the plane if this view disappeared.
983 *
984 * A repaint is scheduled for this view.
985 *
986 * The region of all opaque views covering this view is stored in
987 * weston_view::clip and updated by view_accumulate_damage() during
988 * weston_output_repaint(). Specifically, that region matches the
989 * scenegraph as it was last painted.
990 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400991WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500992weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200993{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500994 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200995
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500996 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +0200997 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500998 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800999 if (view->plane)
1000 pixman_region32_union(&view->plane->damage,
1001 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001002 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -08001003 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001004}
1005
Pekka Paalanen01e00682017-03-24 16:21:06 +02001006/** Send wl_surface.enter/leave events
1007 *
1008 * \param surface The surface.
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03001009 * \param head A head of the entered/left output.
Pekka Paalanen01e00682017-03-24 16:21:06 +02001010 * \param enter True if entered.
Marius Vlada2dace22019-06-12 16:05:44 +03001011 * \param leave True if left.
Pekka Paalanen01e00682017-03-24 16:21:06 +02001012 *
1013 * Send the enter/leave events for all protocol objects bound to the given
1014 * output by the client owning the surface.
1015 */
1016static void
1017weston_surface_send_enter_leave(struct weston_surface *surface,
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03001018 struct weston_head *head,
Pekka Paalanen01e00682017-03-24 16:21:06 +02001019 bool enter,
1020 bool leave)
1021{
1022 struct wl_resource *wloutput;
1023 struct wl_client *client;
1024
1025 assert(enter != leave);
1026
1027 client = wl_resource_get_client(surface->resource);
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03001028 wl_resource_for_each(wloutput, &head->resource_list) {
Pekka Paalanen01e00682017-03-24 16:21:06 +02001029 if (wl_resource_get_client(wloutput) != client)
1030 continue;
1031
1032 if (enter)
1033 wl_surface_send_enter(surface->resource, wloutput);
1034 if (leave)
1035 wl_surface_send_leave(surface->resource, wloutput);
1036 }
1037}
1038
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301039static void
1040weston_surface_compute_protection(struct protected_surface *psurface)
1041{
1042 enum weston_hdcp_protection min_protection;
1043 bool min_protection_valid = false;
1044 struct weston_surface *surface = psurface->surface;
1045 struct weston_output *output;
1046
1047 wl_list_for_each(output, &surface->compositor->output_list, link)
1048 if (surface->output_mask & (1u << output->id)) {
1049 if (!min_protection_valid) {
1050 min_protection = output->current_protection;
1051 min_protection_valid = true;
1052 }
1053 if (output->current_protection < min_protection)
1054 min_protection = output->current_protection;
1055 }
1056 if (!min_protection_valid)
1057 min_protection = WESTON_HDCP_DISABLE;
1058
1059 surface->current_protection = min_protection;
1060 weston_protected_surface_send_event(psurface,
1061 surface->current_protection);
1062}
1063
1064static void
1065notify_surface_protection_change(void *data)
1066{
1067 struct weston_compositor *compositor = data;
1068 struct content_protection *cp;
1069 struct protected_surface *psurface;
1070
1071 cp = compositor->content_protection;
1072 cp->surface_protection_update = NULL;
1073
1074 /* Notify the clients, whose surfaces are changed */
1075 wl_list_for_each(psurface, &cp->protected_list, link)
1076 if (psurface && psurface->surface)
1077 weston_surface_compute_protection(psurface);
1078}
1079
Bryce Harrington3f650b82015-12-23 11:01:58 -08001080/**
1081 * \param es The surface
1082 * \param mask The new set of outputs for the surface
1083 *
1084 * Sets the surface's set of outputs to the ones specified by
1085 * the new output mask provided. Identifies the outputs that
1086 * have changed, the posts enter and leave events for these
1087 * outputs as appropriate.
1088 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001089static void
1090weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
1091{
1092 uint32_t different = es->output_mask ^ mask;
1093 uint32_t entered = mask & different;
1094 uint32_t left = es->output_mask & different;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001095 uint32_t output_bit;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001096 struct weston_output *output;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001097 struct weston_head *head;
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301098 struct content_protection *cp;
1099 struct wl_event_loop *loop;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001100
1101 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001102 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001103 return;
1104 if (different == 0)
1105 return;
1106
1107 wl_list_for_each(output, &es->compositor->output_list, link) {
Pekka Paalanen01e00682017-03-24 16:21:06 +02001108 output_bit = 1u << output->id;
1109 if (!(output_bit & different))
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001110 continue;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001111
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001112 wl_list_for_each(head, &output->head_list, output_link) {
1113 weston_surface_send_enter_leave(es, head,
1114 output_bit & entered,
1115 output_bit & left);
1116 }
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001117 }
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301118 /*
1119 * Change in surfaces' output mask might trigger a change in its
1120 * protection.
1121 */
1122 loop = wl_display_get_event_loop(es->compositor->wl_display);
1123 cp = es->compositor->content_protection;
1124 if (!cp || cp->surface_protection_update)
1125 return;
1126 cp->surface_protection_update = wl_event_loop_add_idle(loop,
1127 notify_surface_protection_change,
1128 es->compositor);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001129}
1130
Semi Malinene7a52fb2018-04-26 11:08:10 +02001131static void
1132notify_view_output_destroy(struct wl_listener *listener, void *data)
1133{
1134 struct weston_view *view =
1135 container_of(listener,
1136 struct weston_view, output_destroy_listener);
1137
1138 view->output = NULL;
1139 view->output_destroy_listener.notify = NULL;
1140}
1141
1142/** Set the primary output of the view
1143 *
1144 * \param view The view whose primary output to set
1145 * \param output The new primary output for the view
1146 *
1147 * Set \a output to be the primary output of the \a view.
1148 *
1149 * Notice that the assignment may be temporary; the primary output could be
1150 * automatically changed. Hence, one cannot rely on the value persisting.
1151 *
1152 * Passing NULL as /a output will set the primary output to NULL.
1153 */
1154WL_EXPORT void
1155weston_view_set_output(struct weston_view *view, struct weston_output *output)
1156{
1157 if (view->output_destroy_listener.notify) {
1158 wl_list_remove(&view->output_destroy_listener.link);
1159 view->output_destroy_listener.notify = NULL;
1160 }
1161 view->output = output;
1162 if (output) {
1163 view->output_destroy_listener.notify =
1164 notify_view_output_destroy;
1165 wl_signal_add(&output->destroy_signal,
1166 &view->output_destroy_listener);
1167 }
1168}
1169
Bryce Harrington3f650b82015-12-23 11:01:58 -08001170/** Recalculate which output(s) the surface has views displayed on
1171 *
1172 * \param es The surface to remap to outputs
1173 *
1174 * Finds the output that is showing the largest amount of one
1175 * of the surface's various views. This output becomes the
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001176 * surface's primary output for vsync and frame callback purposes.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001177 *
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001178 * Also notes all outputs of all of the surface's views
Bryce Harrington3f650b82015-12-23 11:01:58 -08001179 * in the output_mask for the surface.
1180 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001181static void
1182weston_surface_assign_output(struct weston_surface *es)
1183{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001184 struct weston_output *new_output;
1185 struct weston_view *view;
1186 pixman_region32_t region;
1187 uint32_t max, area, mask;
1188 pixman_box32_t *e;
1189
1190 new_output = NULL;
1191 max = 0;
1192 mask = 0;
1193 pixman_region32_init(&region);
1194 wl_list_for_each(view, &es->views, surface_link) {
1195 if (!view->output)
1196 continue;
1197
1198 pixman_region32_intersect(&region, &view->transform.boundingbox,
1199 &view->output->region);
1200
1201 e = pixman_region32_extents(&region);
1202 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1203
1204 mask |= view->output_mask;
1205
1206 if (area >= max) {
1207 new_output = view->output;
1208 max = area;
1209 }
1210 }
1211 pixman_region32_fini(&region);
1212
1213 es->output = new_output;
1214 weston_surface_update_output_mask(es, mask);
1215}
1216
Bryce Harrington3f650b82015-12-23 11:01:58 -08001217/** Recalculate which output(s) the view is displayed on
1218 *
1219 * \param ev The view to remap to outputs
1220 *
1221 * Identifies the set of outputs that the view is visible on,
1222 * noting them into the output_mask. The output that the view
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001223 * is most visible on is set as the view's primary output.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001224 *
1225 * Also does the same for the view's surface. See
1226 * weston_surface_assign_output().
1227 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001228static void
1229weston_view_assign_output(struct weston_view *ev)
1230{
1231 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001232 struct weston_output *output, *new_output;
1233 pixman_region32_t region;
1234 uint32_t max, area, mask;
1235 pixman_box32_t *e;
1236
1237 new_output = NULL;
1238 max = 0;
1239 mask = 0;
1240 pixman_region32_init(&region);
1241 wl_list_for_each(output, &ec->output_list, link) {
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03001242 if (output->destroying)
1243 continue;
1244
Jason Ekstranda7af7042013-10-12 22:38:11 -05001245 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001246 &output->region);
1247
1248 e = pixman_region32_extents(&region);
1249 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1250
1251 if (area > 0)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001252 mask |= 1u << output->id;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001253
1254 if (area >= max) {
1255 new_output = output;
1256 max = area;
1257 }
1258 }
1259 pixman_region32_fini(&region);
1260
Semi Malinene7a52fb2018-04-26 11:08:10 +02001261 weston_view_set_output(ev, new_output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001262 ev->output_mask = mask;
1263
1264 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001265}
1266
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001267static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001268weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1269 int from_x, int from_y, int *to_x, int *to_y)
1270{
1271 float x, y;
1272
1273 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1274 weston_view_from_global_float(to, x, y, &x, &y);
1275
1276 *to_x = round(x);
1277 *to_y = round(y);
1278}
1279
1280static void
1281weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1282{
1283 pixman_box32_t *a;
1284 pixman_box32_t b;
1285
1286 a = pixman_region32_extents(&from->geometry.scissor);
1287
1288 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1289 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1290
1291 pixman_region32_fini(&to->geometry.scissor);
1292 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1293}
1294
1295static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001296view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001297 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001298{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001299 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1300 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001301 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001302 { inbox->x1, inbox->y1 },
1303 { inbox->x1, inbox->y2 },
1304 { inbox->x2, inbox->y1 },
1305 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001306 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001307 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001308 int i;
1309
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001310 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001311 /* avoid rounding empty bbox to 1x1 */
1312 pixman_region32_init(bbox);
1313 return;
1314 }
1315
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001316 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001317 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001318 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001319 if (x < min_x)
1320 min_x = x;
1321 if (x > max_x)
1322 max_x = x;
1323 if (y < min_y)
1324 min_y = y;
1325 if (y > max_y)
1326 max_y = y;
1327 }
1328
Pekka Paalanen219b9822012-02-08 15:38:37 +02001329 int_x = floorf(min_x);
1330 int_y = floorf(min_y);
1331 pixman_region32_init_rect(bbox, int_x, int_y,
1332 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001333}
1334
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001335static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001336weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001337{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001338 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001339
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001340 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001341 view->geometry.x = roundf(view->geometry.x);
1342 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001343
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001344 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001345 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1346 view->transform.position.matrix.d[12] = view->geometry.x;
1347 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001348
Jason Ekstranda7af7042013-10-12 22:38:11 -05001349 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001350
Jason Ekstranda7af7042013-10-12 22:38:11 -05001351 view->transform.inverse = view->transform.position.matrix;
1352 view->transform.inverse.d[12] = -view->geometry.x;
1353 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001354
Jason Ekstranda7af7042013-10-12 22:38:11 -05001355 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001356 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001357 view->surface->width,
1358 view->surface->height);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001359 if (view->geometry.scissor_enabled)
1360 pixman_region32_intersect(&view->transform.boundingbox,
1361 &view->transform.boundingbox,
1362 &view->geometry.scissor);
1363
1364 pixman_region32_translate(&view->transform.boundingbox,
1365 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001366
Jason Ekstranda7af7042013-10-12 22:38:11 -05001367 if (view->alpha == 1.0) {
1368 pixman_region32_copy(&view->transform.opaque,
1369 &view->surface->opaque);
1370 pixman_region32_translate(&view->transform.opaque,
1371 view->geometry.x,
1372 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001373 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001374}
1375
1376static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001377weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001378{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001379 struct weston_view *parent = view->geometry.parent;
1380 struct weston_matrix *matrix = &view->transform.matrix;
1381 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001382 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001383 pixman_region32_t surfregion;
1384 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001385
Jason Ekstranda7af7042013-10-12 22:38:11 -05001386 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001387
1388 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001389 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1390 view->transform.position.matrix.d[12] = view->geometry.x;
1391 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001392
1393 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001394 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001395 weston_matrix_multiply(matrix, &tform->matrix);
1396
Pekka Paalanen483243f2013-03-08 14:56:50 +02001397 if (parent)
1398 weston_matrix_multiply(matrix, &parent->transform.matrix);
1399
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001400 if (weston_matrix_invert(inverse, matrix) < 0) {
1401 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001402 weston_log("error: weston_view %p"
1403 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001404 return -1;
1405 }
1406
Giulio Camuffo148c1992016-09-04 18:50:46 +03001407 if (view->alpha == 1.0 &&
1408 matrix->type == WESTON_MATRIX_TRANSFORM_TRANSLATE) {
1409 pixman_region32_copy(&view->transform.opaque,
1410 &view->surface->opaque);
1411 pixman_region32_translate(&view->transform.opaque,
1412 matrix->d[12],
1413 matrix->d[13]);
1414 }
1415
Pekka Paalanen380adf52015-02-16 14:39:11 +02001416 pixman_region32_init_rect(&surfregion, 0, 0,
1417 view->surface->width, view->surface->height);
1418 if (view->geometry.scissor_enabled)
1419 pixman_region32_intersect(&surfregion, &surfregion,
1420 &view->geometry.scissor);
1421 surfbox = pixman_region32_extents(&surfregion);
1422
1423 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1424 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001425
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001426 return 0;
1427}
1428
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001429static struct weston_layer *
1430get_view_layer(struct weston_view *view)
1431{
1432 if (view->parent_view)
1433 return get_view_layer(view->parent_view);
1434 return view->layer_link.layer;
1435}
1436
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001437WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001438weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001439{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001440 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001441 struct weston_layer *layer;
1442 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001443
Jason Ekstranda7af7042013-10-12 22:38:11 -05001444 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001445 return;
1446
Pekka Paalanen483243f2013-03-08 14:56:50 +02001447 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001448 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001449
Jason Ekstranda7af7042013-10-12 22:38:11 -05001450 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001451
Jason Ekstranda7af7042013-10-12 22:38:11 -05001452 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001453
Jason Ekstranda7af7042013-10-12 22:38:11 -05001454 pixman_region32_fini(&view->transform.boundingbox);
1455 pixman_region32_fini(&view->transform.opaque);
1456 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001457
Pekka Paalanencd403622012-01-25 13:37:39 +02001458 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001459 if (view->geometry.transformation_list.next ==
1460 &view->transform.position.link &&
1461 view->geometry.transformation_list.prev ==
1462 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001463 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001464 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001465 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001466 if (weston_view_update_transform_enable(view) < 0)
1467 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001468 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001469
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001470 layer = get_view_layer(view);
1471 if (layer) {
1472 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001473 pixman_region32_intersect(&view->transform.boundingbox,
1474 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001475 pixman_region32_intersect(&view->transform.opaque,
1476 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001477 pixman_region32_fini(&mask);
1478 }
1479
Pekka Paalanen380adf52015-02-16 14:39:11 +02001480 if (parent) {
1481 if (parent->geometry.scissor_enabled) {
1482 view->geometry.scissor_enabled = true;
1483 weston_view_transfer_scissor(parent, view);
1484 } else {
1485 view->geometry.scissor_enabled = false;
1486 }
1487 }
1488
Jason Ekstranda7af7042013-10-12 22:38:11 -05001489 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001490
Jason Ekstranda7af7042013-10-12 22:38:11 -05001491 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001492
Jason Ekstranda7af7042013-10-12 22:38:11 -05001493 wl_signal_emit(&view->surface->compositor->transform_signal,
1494 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001495}
1496
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001497WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001498weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001499{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001500 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001501
1502 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001503 * The invariant: if view->geometry.dirty, then all views
1504 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001505 * Corollary: if not parent->geometry.dirty, then all ancestors
1506 * are not dirty.
1507 */
1508
Jason Ekstranda7af7042013-10-12 22:38:11 -05001509 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001510 return;
1511
Jason Ekstranda7af7042013-10-12 22:38:11 -05001512 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001513
Jason Ekstranda7af7042013-10-12 22:38:11 -05001514 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001515 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001516 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001517}
1518
1519WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001520weston_view_to_global_fixed(struct weston_view *view,
1521 wl_fixed_t vx, wl_fixed_t vy,
1522 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001523{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001524 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001525
Jason Ekstranda7af7042013-10-12 22:38:11 -05001526 weston_view_to_global_float(view,
1527 wl_fixed_to_double(vx),
1528 wl_fixed_to_double(vy),
1529 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001530 *x = wl_fixed_from_double(xf);
1531 *y = wl_fixed_from_double(yf);
1532}
1533
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001534WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001535weston_view_from_global_float(struct weston_view *view,
1536 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001537{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001538 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001539 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1540
Jason Ekstranda7af7042013-10-12 22:38:11 -05001541 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001542
1543 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001544 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001545 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001546 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001547 *vx = 0;
1548 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001549 return;
1550 }
1551
Jason Ekstranda7af7042013-10-12 22:38:11 -05001552 *vx = v.f[0] / v.f[3];
1553 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001554 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001555 *vx = x - view->geometry.x;
1556 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001557 }
1558}
1559
1560WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001561weston_view_from_global_fixed(struct weston_view *view,
1562 wl_fixed_t x, wl_fixed_t y,
1563 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001564{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001565 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001566
Jason Ekstranda7af7042013-10-12 22:38:11 -05001567 weston_view_from_global_float(view,
1568 wl_fixed_to_double(x),
1569 wl_fixed_to_double(y),
1570 &vxf, &vyf);
1571 *vx = wl_fixed_from_double(vxf);
1572 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001573}
1574
1575WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001576weston_view_from_global(struct weston_view *view,
1577 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001578{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001579 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001580
Jason Ekstranda7af7042013-10-12 22:38:11 -05001581 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1582 *vx = floorf(vxf);
1583 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001584}
1585
Bryce Harrington3f650b82015-12-23 11:01:58 -08001586/**
1587 * \param surface The surface to be repainted
1588 *
1589 * Marks the output(s) that the surface is shown on as needing to be
1590 * repainted. See weston_output_schedule_repaint().
1591 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001592WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001593weston_surface_schedule_repaint(struct weston_surface *surface)
1594{
1595 struct weston_output *output;
1596
1597 wl_list_for_each(output, &surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001598 if (surface->output_mask & (1u << output->id))
Kristian Høgsberg98238702012-08-03 16:29:12 -04001599 weston_output_schedule_repaint(output);
1600}
1601
Bryce Harrington3f650b82015-12-23 11:01:58 -08001602/**
1603 * \param view The view to be repainted
1604 *
1605 * Marks the output(s) that the view is shown on as needing to be
1606 * repainted. See weston_output_schedule_repaint().
1607 */
Kristian Høgsberg98238702012-08-03 16:29:12 -04001608WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001609weston_view_schedule_repaint(struct weston_view *view)
1610{
1611 struct weston_output *output;
1612
1613 wl_list_for_each(output, &view->surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001614 if (view->output_mask & (1u << output->id))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001615 weston_output_schedule_repaint(output);
1616}
1617
Pekka Paalanene508ce62015-02-19 13:59:55 +02001618/**
1619 * XXX: This function does it the wrong way.
1620 * surface->damage is the damage from the client, and causes
1621 * surface_flush_damage() to copy pixels. No window management action can
1622 * cause damage to the client-provided content, warranting re-upload!
1623 *
1624 * Instead of surface->damage, this function should record the damage
1625 * with all the views for this surface to avoid extraneous texture
1626 * uploads.
1627 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001628WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001629weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001630{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001631 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001632 0, 0, surface->width,
1633 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001634
Kristian Høgsberg98238702012-08-03 16:29:12 -04001635 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001636}
1637
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001638WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001639weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001640{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001641 if (view->geometry.x == x && view->geometry.y == y)
1642 return;
1643
Jason Ekstranda7af7042013-10-12 22:38:11 -05001644 view->geometry.x = x;
1645 view->geometry.y = y;
1646 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001647}
1648
Pekka Paalanen483243f2013-03-08 14:56:50 +02001649static void
1650transform_parent_handle_parent_destroy(struct wl_listener *listener,
1651 void *data)
1652{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001653 struct weston_view *view =
1654 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001655 geometry.parent_destroy_listener);
1656
Jason Ekstranda7af7042013-10-12 22:38:11 -05001657 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001658}
1659
1660WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001661weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001662 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001663{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001664 if (view->geometry.parent) {
1665 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1666 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001667
1668 if (!parent)
1669 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001670 }
1671
Jason Ekstranda7af7042013-10-12 22:38:11 -05001672 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001673
Jason Ekstranda7af7042013-10-12 22:38:11 -05001674 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001675 transform_parent_handle_parent_destroy;
1676 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001677 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001678 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001679 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001680 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001681 }
1682
Jason Ekstranda7af7042013-10-12 22:38:11 -05001683 weston_view_geometry_dirty(view);
1684}
1685
Pekka Paalanen380adf52015-02-16 14:39:11 +02001686/** Set a clip mask rectangle on a view
1687 *
1688 * \param view The view to set the clip mask on.
1689 * \param x Top-left corner X coordinate of the clip rectangle.
1690 * \param y Top-left corner Y coordinate of the clip rectangle.
1691 * \param width Width of the clip rectangle, non-negative.
1692 * \param height Height of the clip rectangle, non-negative.
1693 *
1694 * A shell may set a clip mask rectangle on a view. Everything outside
1695 * the rectangle is cut away for input and output purposes: it is
1696 * not drawn and cannot be hit by hit-test based input like pointer
1697 * motion or touch-downs. Everything inside the rectangle will behave
1698 * normally. Clients are unaware of clipping.
1699 *
Yong Bakos4c72e292016-04-28 11:59:10 -05001700 * The rectangle is set in surface-local coordinates. Setting a clip
Pekka Paalanen380adf52015-02-16 14:39:11 +02001701 * mask rectangle does not affect the view position, the view is positioned
1702 * as it would be without a clip. The clip also does not change
1703 * weston_surface::width,height.
1704 *
1705 * The clip mask rectangle is part of transformation inheritance
1706 * (weston_view_set_transform_parent()). A clip set in the root of the
1707 * transformation inheritance tree will affect all views in the tree.
1708 * A clip can be set only on the root view. Attempting to set a clip
1709 * on view that has a transformation parent will fail. Assigning a parent
1710 * to a view that has a clip set will cause the clip to be forgotten.
1711 *
1712 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1713 * on the additional transformations in the child views. These transformations
1714 * may not rotate the coordinate axes, i.e., only translation and scaling
1715 * are allowed. Violating this restriction causes the clipping to malfunction.
1716 * Furthermore, using scaling may cause rounding errors in child clipping.
1717 *
1718 * The clip mask rectangle is not automatically adjusted based on
1719 * wl_surface.attach dx and dy arguments.
1720 *
1721 * A clip mask rectangle can be set only if the compositor capability
1722 * WESTON_CAP_VIEW_CLIP_MASK is present.
1723 *
1724 * This function sets the clip mask rectangle and schedules a repaint for
1725 * the view.
1726 */
1727WL_EXPORT void
1728weston_view_set_mask(struct weston_view *view,
1729 int x, int y, int width, int height)
1730{
1731 struct weston_compositor *compositor = view->surface->compositor;
1732
1733 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1734 weston_log("%s not allowed without capability!\n", __func__);
1735 return;
1736 }
1737
1738 if (view->geometry.parent) {
1739 weston_log("view %p has a parent, clip forbidden!\n", view);
1740 return;
1741 }
1742
1743 if (width < 0 || height < 0) {
1744 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1745 x, y, width, height);
1746 return;
1747 }
1748
1749 pixman_region32_fini(&view->geometry.scissor);
1750 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1751 view->geometry.scissor_enabled = true;
1752 weston_view_geometry_dirty(view);
1753 weston_view_schedule_repaint(view);
1754}
1755
1756/** Remove the clip mask from a view
1757 *
1758 * \param view The view to remove the clip mask from.
1759 *
1760 * Removed the clip mask rectangle and schedules a repaint.
1761 *
1762 * \sa weston_view_set_mask
1763 */
1764WL_EXPORT void
1765weston_view_set_mask_infinite(struct weston_view *view)
1766{
1767 view->geometry.scissor_enabled = false;
1768 weston_view_geometry_dirty(view);
1769 weston_view_schedule_repaint(view);
1770}
1771
Armin Krezović0da12b82016-06-30 06:04:33 +02001772/* Check if view should be displayed
1773 *
1774 * The indicator is set manually when assigning
1775 * a view to a surface.
1776 *
1777 * This needs reworking. See the thread starting at:
1778 *
1779 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1780 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001781WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001782weston_view_is_mapped(struct weston_view *view)
1783{
Armin Krezović0da12b82016-06-30 06:04:33 +02001784 return view->is_mapped;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001785}
1786
Philipp Zabel70decd52018-09-03 20:11:15 +02001787/* Check if view is opaque in specified region
1788 *
1789 * \param view The view to check for opacity.
1790 * \param region The region to check for opacity, in view coordinates.
1791 *
1792 * Returns true if the view is opaque in the specified region, because view
1793 * alpha is 1.0 and either the opaque region set by the client contains the
1794 * specified region, or the buffer pixel format or solid color is opaque.
1795 */
1796WL_EXPORT bool
1797weston_view_is_opaque(struct weston_view *ev, pixman_region32_t *region)
1798{
1799 pixman_region32_t r;
1800 bool ret = false;
1801
1802 if (ev->alpha < 1.0)
1803 return false;
1804
1805 if (ev->surface->is_opaque)
1806 return true;
1807
1808 if (ev->transform.dirty) {
1809 weston_log("%s: transform dirty", __func__);
1810 return false;
1811 }
1812
1813 pixman_region32_init(&r);
1814 pixman_region32_subtract(&r, region, &ev->transform.opaque);
1815
1816 if (!pixman_region32_not_empty(&r))
1817 ret = true;
1818
1819 pixman_region32_fini(&r);
1820
1821 return ret;
1822}
1823
Armin Krezović0da12b82016-06-30 06:04:33 +02001824/* Check if a surface has a view assigned to it
1825 *
1826 * The indicator is set manually when mapping
1827 * a surface and creating a view for it.
1828 *
1829 * This needs to go. See the thread starting at:
1830 *
1831 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1832 *
1833 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001834WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001835weston_surface_is_mapped(struct weston_surface *surface)
1836{
Armin Krezović0da12b82016-06-30 06:04:33 +02001837 return surface->is_mapped;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001838}
1839
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001840static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001841surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001842{
1843 struct weston_view *view;
1844
1845 if (surface->width == width && surface->height == height)
1846 return;
1847
1848 surface->width = width;
1849 surface->height = height;
1850
1851 wl_list_for_each(view, &surface->views, surface_link)
1852 weston_view_geometry_dirty(view);
1853}
1854
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001855WL_EXPORT void
1856weston_surface_set_size(struct weston_surface *surface,
1857 int32_t width, int32_t height)
1858{
1859 assert(!surface->resource);
1860 surface_set_size(surface, width, height);
1861}
1862
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001863static int
1864fixed_round_up_to_int(wl_fixed_t f)
1865{
1866 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1867}
1868
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001869static void
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001870convert_size_by_transform_scale(int32_t *width_out, int32_t *height_out,
1871 int32_t width, int32_t height,
1872 uint32_t transform,
1873 int32_t scale)
1874{
1875 assert(scale > 0);
1876
1877 switch (transform) {
1878 case WL_OUTPUT_TRANSFORM_NORMAL:
1879 case WL_OUTPUT_TRANSFORM_180:
1880 case WL_OUTPUT_TRANSFORM_FLIPPED:
1881 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1882 *width_out = width / scale;
1883 *height_out = height / scale;
1884 break;
1885 case WL_OUTPUT_TRANSFORM_90:
1886 case WL_OUTPUT_TRANSFORM_270:
1887 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1888 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1889 *width_out = height / scale;
1890 *height_out = width / scale;
1891 break;
1892 default:
1893 assert(0 && "invalid transform");
1894 }
1895}
1896
1897static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001898weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001899{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001900 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001901
1902 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001903 surface->width_from_buffer = 0;
1904 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001905 return;
1906 }
1907
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001908 convert_size_by_transform_scale(&surface->width_from_buffer,
1909 &surface->height_from_buffer,
1910 surface->buffer_ref.buffer->width,
1911 surface->buffer_ref.buffer->height,
1912 vp->buffer.transform,
1913 vp->buffer.scale);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001914}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001915
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001916static void
1917weston_surface_update_size(struct weston_surface *surface)
1918{
1919 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1920 int32_t width, height;
1921
1922 width = surface->width_from_buffer;
1923 height = surface->height_from_buffer;
1924
1925 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001926 surface_set_size(surface,
1927 vp->surface.width, vp->surface.height);
1928 return;
1929 }
1930
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001931 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001932 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1933 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1934
1935 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001936 return;
1937 }
1938
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001939 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001940}
1941
Marius Vlad9fdda7f2019-06-11 16:08:55 +03001942/** weston_compositor_get_time
1943 * \ingroup compositor
1944 */
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02001945WL_EXPORT void
1946weston_compositor_get_time(struct timespec *time)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001947{
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02001948 clock_gettime(CLOCK_REALTIME, time);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001949}
1950
Marius Vlad9fdda7f2019-06-11 16:08:55 +03001951/** weston_compositor_pick_view
1952 * \ingroup compositor
1953 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001954WL_EXPORT struct weston_view *
1955weston_compositor_pick_view(struct weston_compositor *compositor,
1956 wl_fixed_t x, wl_fixed_t y,
1957 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001958{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001959 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001960 wl_fixed_t view_x, view_y;
1961 int view_ix, view_iy;
1962 int ix = wl_fixed_to_int(x);
1963 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001964
Jason Ekstranda7af7042013-10-12 22:38:11 -05001965 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001966 if (!pixman_region32_contains_point(
1967 &view->transform.boundingbox, ix, iy, NULL))
1968 continue;
1969
1970 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
1971 view_ix = wl_fixed_to_int(view_x);
1972 view_iy = wl_fixed_to_int(view_y);
1973
1974 if (!pixman_region32_contains_point(&view->surface->input,
1975 view_ix, view_iy, NULL))
1976 continue;
1977
Pekka Paalanen380adf52015-02-16 14:39:11 +02001978 if (view->geometry.scissor_enabled &&
1979 !pixman_region32_contains_point(&view->geometry.scissor,
1980 view_ix, view_iy, NULL))
1981 continue;
1982
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001983 *vx = view_x;
1984 *vy = view_y;
1985 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001986 }
1987
Derek Foremanf9318d12015-05-11 15:40:11 -05001988 *vx = wl_fixed_from_int(-1000000);
1989 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001990 return NULL;
1991}
1992
1993static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001994weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001995{
Daniel Stone37816df2012-05-16 18:45:18 +01001996 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001997
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001998 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001999 return;
2000
Daniel Stone37816df2012-05-16 18:45:18 +01002001 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04002002 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002003}
2004
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002005WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002006weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05002007{
Daniel Stone4dab5db2012-05-30 16:31:53 +01002008 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002009
Jason Ekstranda7af7042013-10-12 22:38:11 -05002010 if (!weston_view_is_mapped(view))
2011 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002012
Jason Ekstranda7af7042013-10-12 22:38:11 -05002013 weston_view_damage_below(view);
Semi Malinene7a52fb2018-04-26 11:08:10 +02002014 weston_view_set_output(view, NULL);
Xiong Zhang97116532013-10-23 13:58:31 +08002015 view->plane = NULL;
Armin Krezovićf8486c32016-06-30 06:04:28 +02002016 view->is_mapped = false;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002017 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002018 wl_list_remove(&view->link);
2019 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002020 view->output_mask = 0;
2021 weston_surface_assign_output(view->surface);
2022
2023 if (weston_surface_is_mapped(view->surface))
2024 return;
2025
2026 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002027 struct weston_touch *touch = weston_seat_get_touch(seat);
2028 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2029 struct weston_keyboard *keyboard =
2030 weston_seat_get_keyboard(seat);
2031
2032 if (keyboard && keyboard->focus == view->surface)
2033 weston_keyboard_set_focus(keyboard, NULL);
2034 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05002035 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05002036 if (touch && touch->focus == view)
2037 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01002038 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002039}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002040
Jason Ekstranda7af7042013-10-12 22:38:11 -05002041WL_EXPORT void
2042weston_surface_unmap(struct weston_surface *surface)
2043{
2044 struct weston_view *view;
2045
Armin Krezovićf8486c32016-06-30 06:04:28 +02002046 surface->is_mapped = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002047 wl_list_for_each(view, &surface->views, surface_link)
2048 weston_view_unmap(view);
2049 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05002050}
2051
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002052static void
2053weston_surface_reset_pending_buffer(struct weston_surface *surface)
2054{
Jason Ekstrand7b982072014-05-20 14:33:03 -05002055 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002056 surface->pending.sx = 0;
2057 surface->pending.sy = 0;
2058 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002059 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002060}
2061
Jason Ekstranda7af7042013-10-12 22:38:11 -05002062WL_EXPORT void
2063weston_view_destroy(struct weston_view *view)
2064{
2065 wl_signal_emit(&view->destroy_signal, view);
2066
2067 assert(wl_list_empty(&view->geometry.child_list));
2068
2069 if (weston_view_is_mapped(view)) {
2070 weston_view_unmap(view);
2071 weston_compositor_build_view_list(view->surface->compositor);
2072 }
2073
2074 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002075 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002076
2077 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02002078 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002079 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002080 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002081
2082 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen944fae82018-05-22 13:15:58 +03002083 weston_view_set_output(view, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002084
Jason Ekstranda7af7042013-10-12 22:38:11 -05002085 wl_list_remove(&view->surface_link);
2086
2087 free(view);
2088}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002089
2090WL_EXPORT void
2091weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05002092{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002093 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002094 struct weston_view *ev, *nv;
Jonas Ådahld3414f22016-07-22 17:56:31 +08002095 struct weston_pointer_constraint *constraint, *next_constraint;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04002096
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02002097 if (--surface->ref_count > 0)
2098 return;
2099
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03002100 assert(surface->resource == NULL);
2101
Pekka Paalanenca790762015-04-17 14:23:38 +03002102 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02002103
Pekka Paalanene67858b2013-04-25 13:57:42 +03002104 assert(wl_list_empty(&surface->subsurface_list_pending));
2105 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02002106
Jason Ekstranda7af7042013-10-12 22:38:11 -05002107 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
2108 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002109
Jason Ekstrand7b982072014-05-20 14:33:03 -05002110 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002111
Pekka Paalanende685b82012-12-04 15:58:12 +02002112 weston_buffer_reference(&surface->buffer_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002113 weston_buffer_release_reference(&surface->buffer_release_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04002114
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02002115 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002116 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002117 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02002118
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002119 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002120 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002121
Pekka Paalanen133e4392014-09-23 22:08:46 -04002122 weston_presentation_feedback_discard_list(&surface->feedback_list);
2123
Jonas Ådahld3414f22016-07-22 17:56:31 +08002124 wl_list_for_each_safe(constraint, next_constraint,
2125 &surface->pointer_constraints,
2126 link)
2127 weston_pointer_constraint_destroy(constraint);
2128
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03002129 fd_clear(&surface->acquire_fence_fd);
2130
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04002131 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05002132}
2133
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002134static void
2135destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08002136{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002137 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08002138
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03002139 assert(surface);
2140
Giulio Camuffo0d379742013-11-15 22:06:15 +01002141 /* Set the resource to NULL, since we don't want to leave a
2142 * dangling pointer if the surface was refcounted and survives
2143 * the weston_surface_destroy() call. */
2144 surface->resource = NULL;
Pekka Paalanen4826f872016-04-22 14:14:38 +03002145
2146 if (surface->viewport_resource)
2147 wl_resource_set_user_data(surface->viewport_resource, NULL);
2148
Alexandros Frantzis27d7c392018-10-19 12:14:11 +03002149 if (surface->synchronization_resource) {
2150 wl_resource_set_user_data(surface->synchronization_resource,
2151 NULL);
2152 }
2153
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002154 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08002155}
2156
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002157static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002158weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
2159{
2160 struct weston_buffer *buffer =
2161 container_of(listener, struct weston_buffer, destroy_listener);
2162
2163 wl_signal_emit(&buffer->destroy_signal, buffer);
2164 free(buffer);
2165}
2166
Giulio Camuffoe058cd12013-12-12 14:14:29 +01002167WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002168weston_buffer_from_resource(struct wl_resource *resource)
2169{
2170 struct weston_buffer *buffer;
2171 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002172
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002173 listener = wl_resource_get_destroy_listener(resource,
2174 weston_buffer_destroy_handler);
2175
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002176 if (listener)
2177 return container_of(listener, struct weston_buffer,
2178 destroy_listener);
2179
2180 buffer = zalloc(sizeof *buffer);
2181 if (buffer == NULL)
2182 return NULL;
2183
2184 buffer->resource = resource;
2185 wl_signal_init(&buffer->destroy_signal);
2186 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04002187 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002188 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002189
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002190 return buffer;
2191}
2192
2193static void
Pekka Paalanende685b82012-12-04 15:58:12 +02002194weston_buffer_reference_handle_destroy(struct wl_listener *listener,
2195 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002196{
Pekka Paalanende685b82012-12-04 15:58:12 +02002197 struct weston_buffer_reference *ref =
2198 container_of(listener, struct weston_buffer_reference,
2199 destroy_listener);
2200
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002201 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02002202 ref->buffer = NULL;
2203}
2204
2205WL_EXPORT void
2206weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002207 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002208{
2209 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05002210 ref->buffer->busy_count--;
2211 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002212 assert(wl_resource_get_client(ref->buffer->resource));
Matt Hoosier3052bc72017-09-26 08:09:40 -05002213 wl_buffer_send_release(ref->buffer->resource);
Kristian Høgsberg20347802013-03-04 12:07:46 -05002214 }
Pekka Paalanende685b82012-12-04 15:58:12 +02002215 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002216 }
2217
Pekka Paalanende685b82012-12-04 15:58:12 +02002218 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002219 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002220 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02002221 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02002222 }
2223
Pekka Paalanende685b82012-12-04 15:58:12 +02002224 ref->buffer = buffer;
2225 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
2226}
2227
2228static void
Alexandros Frantzis67629672018-10-19 12:14:11 +03002229weston_buffer_release_reference_handle_destroy(struct wl_listener *listener,
2230 void *data)
2231{
2232 struct weston_buffer_release_reference *ref =
2233 container_of(listener, struct weston_buffer_release_reference,
2234 destroy_listener);
2235
2236 assert((struct wl_resource *)data == ref->buffer_release->resource);
2237 ref->buffer_release = NULL;
2238}
2239
2240static void
2241weston_buffer_release_destroy(struct weston_buffer_release *buffer_release)
2242{
2243 struct wl_resource *resource = buffer_release->resource;
2244 int release_fence_fd = buffer_release->fence_fd;
2245
2246 if (release_fence_fd >= 0) {
2247 zwp_linux_buffer_release_v1_send_fenced_release(
2248 resource, release_fence_fd);
2249 } else {
2250 zwp_linux_buffer_release_v1_send_immediate_release(
2251 resource);
2252 }
2253
2254 wl_resource_destroy(resource);
2255}
2256
2257WL_EXPORT void
2258weston_buffer_release_reference(struct weston_buffer_release_reference *ref,
2259 struct weston_buffer_release *buffer_release)
2260{
2261 if (buffer_release == ref->buffer_release)
2262 return;
2263
2264 if (ref->buffer_release) {
2265 ref->buffer_release->ref_count--;
2266 wl_list_remove(&ref->destroy_listener.link);
2267 if (ref->buffer_release->ref_count == 0)
2268 weston_buffer_release_destroy(ref->buffer_release);
2269 }
2270
2271 if (buffer_release) {
2272 buffer_release->ref_count++;
2273 wl_resource_add_destroy_listener(buffer_release->resource,
2274 &ref->destroy_listener);
2275 }
2276
2277 ref->buffer_release = buffer_release;
2278 ref->destroy_listener.notify =
2279 weston_buffer_release_reference_handle_destroy;
2280}
2281
2282WL_EXPORT void
2283weston_buffer_release_move(struct weston_buffer_release_reference *dest,
2284 struct weston_buffer_release_reference *src)
2285{
2286 weston_buffer_release_reference(dest, src->buffer_release);
2287 weston_buffer_release_reference(src, NULL);
2288}
2289
2290static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002291weston_surface_attach(struct weston_surface *surface,
2292 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002293{
2294 weston_buffer_reference(&surface->buffer_ref, buffer);
2295
Pekka Paalanena6421c42012-12-04 15:58:10 +02002296 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002297 if (weston_surface_is_mapped(surface))
2298 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002299 }
2300
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002301 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02002302
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002303 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002304 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002305}
2306
Marius Vlad9fdda7f2019-06-11 16:08:55 +03002307/** weston_compositor_damage_all
2308 * \ingroup compositor
2309 */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002310WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002311weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002312{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002313 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002314
2315 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002316 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002317}
2318
Marius Vlad55d87362019-06-11 01:15:35 +03002319/**
2320 * \ingroup output
2321 */
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05002322WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002323weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002324{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002325 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002326
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002327 pixman_region32_union(&compositor->primary_plane.damage,
2328 &compositor->primary_plane.damage,
2329 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002330 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002331}
2332
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04002333static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002334surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002335{
Pekka Paalanende685b82012-12-04 15:58:12 +02002336 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002337 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002338 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002339
Pekka Paalanenb5026542014-11-12 15:09:24 +02002340 if (weston_timeline_enabled_ &&
2341 pixman_region32_not_empty(&surface->damage))
2342 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
2343 TLP_OUTPUT(surface->output), TLP_END);
2344
Jason Ekstrandef540082014-06-26 10:37:36 -07002345 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002346}
2347
2348static void
2349view_accumulate_damage(struct weston_view *view,
2350 pixman_region32_t *opaque)
2351{
2352 pixman_region32_t damage;
2353
2354 pixman_region32_init(&damage);
2355 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002356 pixman_box32_t *extents;
2357
Jason Ekstranda7af7042013-10-12 22:38:11 -05002358 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002359 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002360 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002361 pixman_region32_copy(&damage, &view->surface->damage);
2362 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002363 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002364 }
2365
Pekka Paalanen380adf52015-02-16 14:39:11 +02002366 pixman_region32_intersect(&damage, &damage,
2367 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002368 pixman_region32_subtract(&damage, &damage, opaque);
2369 pixman_region32_union(&view->plane->damage,
2370 &view->plane->damage, &damage);
2371 pixman_region32_fini(&damage);
2372 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002373 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002374}
2375
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002376static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002377compositor_accumulate_damage(struct weston_compositor *ec)
2378{
2379 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002380 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002381 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002382
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002383 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002384
2385 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002386 pixman_region32_copy(&plane->clip, &clip);
2387
2388 pixman_region32_init(&opaque);
2389
Jason Ekstranda7af7042013-10-12 22:38:11 -05002390 wl_list_for_each(ev, &ec->view_list, link) {
2391 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002392 continue;
2393
Jason Ekstranda7af7042013-10-12 22:38:11 -05002394 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002395 }
2396
2397 pixman_region32_union(&clip, &clip, &opaque);
2398 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002399 }
2400
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002401 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002402
Jason Ekstranda7af7042013-10-12 22:38:11 -05002403 wl_list_for_each(ev, &ec->view_list, link)
Derek Foreman060cf112015-11-18 16:32:26 -06002404 ev->surface->touched = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002405
2406 wl_list_for_each(ev, &ec->view_list, link) {
2407 if (ev->surface->touched)
2408 continue;
Derek Foreman060cf112015-11-18 16:32:26 -06002409 ev->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002410
2411 surface_flush_damage(ev->surface);
2412
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002413 /* Both the renderer and the backend have seen the buffer
2414 * by now. If renderer needs the buffer, it has its own
2415 * reference set. If the backend wants to keep the buffer
2416 * around for migrating the surface into a non-primary plane
2417 * later, keep_buffer is true. Otherwise, drop the core
2418 * reference now, and allow early buffer release. This enables
2419 * clients to use single-buffering.
2420 */
Alexandros Frantzis67629672018-10-19 12:14:11 +03002421 if (!ev->surface->keep_buffer) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002422 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002423 weston_buffer_release_reference(
2424 &ev->surface->buffer_release_ref, NULL);
2425 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002426 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002427}
2428
2429static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002430surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002431{
2432 struct weston_subsurface *sub;
2433
Pekka Paalanene67858b2013-04-25 13:57:42 +03002434 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002435 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002436 continue;
2437
Jason Ekstranda7af7042013-10-12 22:38:11 -05002438 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2439 wl_list_init(&sub->surface->views);
2440
2441 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002442 }
2443}
2444
2445static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002446surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002447{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002448 struct weston_subsurface *sub;
2449 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002450
Jason Ekstranda7af7042013-10-12 22:38:11 -05002451 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2452 if (sub->surface == surface)
2453 continue;
2454
George Kiagiadakised04d382014-06-13 18:10:26 +02002455 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2456 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002457 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002458 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002459
2460 surface_free_unused_subsurface_views(sub->surface);
2461 }
2462}
2463
2464static void
2465view_list_add_subsurface_view(struct weston_compositor *compositor,
2466 struct weston_subsurface *sub,
2467 struct weston_view *parent)
2468{
2469 struct weston_subsurface *child;
2470 struct weston_view *view = NULL, *iv;
2471
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002472 if (!weston_surface_is_mapped(sub->surface))
2473 return;
2474
Jason Ekstranda7af7042013-10-12 22:38:11 -05002475 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2476 if (iv->geometry.parent == parent) {
2477 view = iv;
2478 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002479 }
2480 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002481
2482 if (view) {
2483 /* Put it back in the surface's list of views */
2484 wl_list_remove(&view->surface_link);
2485 wl_list_insert(&sub->surface->views, &view->surface_link);
2486 } else {
2487 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002488 weston_view_set_position(view,
2489 sub->position.x,
2490 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002491 weston_view_set_transform_parent(view, parent);
2492 }
2493
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002494 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002495 weston_view_update_transform(view);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002496 view->is_mapped = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002497
Pekka Paalanenb188e912013-11-19 14:03:35 +02002498 if (wl_list_empty(&sub->surface->subsurface_list)) {
2499 wl_list_insert(compositor->view_list.prev, &view->link);
2500 return;
2501 }
2502
2503 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2504 if (child->surface == sub->surface)
2505 wl_list_insert(compositor->view_list.prev, &view->link);
2506 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002507 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002508 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002509}
2510
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002511/* This recursively adds the sub-surfaces for a view, relying on the
2512 * sub-surface order. Thus, if a client restacks the sub-surfaces, that
2513 * change first happens to the sub-surface list, and then automatically
2514 * propagates here. See weston_surface_damage_subsurfaces() for how the
2515 * sub-surfaces receive damage when the client changes the state.
2516 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002517static void
2518view_list_add(struct weston_compositor *compositor,
2519 struct weston_view *view)
2520{
2521 struct weston_subsurface *sub;
2522
2523 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002524
Pekka Paalanenb188e912013-11-19 14:03:35 +02002525 if (wl_list_empty(&view->surface->subsurface_list)) {
2526 wl_list_insert(compositor->view_list.prev, &view->link);
2527 return;
2528 }
2529
2530 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2531 if (sub->surface == view->surface)
2532 wl_list_insert(compositor->view_list.prev, &view->link);
2533 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002534 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002535 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002536}
2537
2538static void
2539weston_compositor_build_view_list(struct weston_compositor *compositor)
2540{
2541 struct weston_view *view;
2542 struct weston_layer *layer;
2543
2544 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002545 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002546 surface_stash_subsurface_views(view->surface);
2547
2548 wl_list_init(&compositor->view_list);
2549 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002550 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002551 view_list_add(compositor, view);
2552 }
2553 }
2554
2555 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002556 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002557 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002558}
2559
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002560static void
2561weston_output_take_feedback_list(struct weston_output *output,
2562 struct weston_surface *surface)
2563{
2564 struct weston_view *view;
2565 struct weston_presentation_feedback *feedback;
2566 uint32_t flags = 0xffffffff;
2567
2568 if (wl_list_empty(&surface->feedback_list))
2569 return;
2570
2571 /* All views must have the flag for the flag to survive. */
2572 wl_list_for_each(view, &surface->views, surface_link) {
2573 /* ignore views that are not on this output at all */
2574 if (view->output_mask & (1u << output->id))
2575 flags &= view->psf_flags;
2576 }
2577
2578 wl_list_for_each(feedback, &surface->feedback_list, link)
2579 feedback->psf_flags = flags;
2580
2581 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2582 wl_list_init(&surface->feedback_list);
2583}
2584
David Herrmann1edf44c2013-10-22 17:11:26 +02002585static int
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002586weston_output_repaint(struct weston_output *output, void *repaint_data)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002587{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002588 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002589 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002590 struct weston_animation *animation, *next;
2591 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002592 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002593 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002594 int r;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002595 uint32_t frame_time_msec;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05302596 enum weston_hdcp_protection highest_requested = WESTON_HDCP_DISABLE;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002597
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002598 if (output->destroying)
2599 return 0;
2600
Pekka Paalanenb5026542014-11-12 15:09:24 +02002601 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2602
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002603 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002604 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002605
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05302606 /* Find the highest protection desired for an output */
2607 wl_list_for_each(ev, &ec->view_list, link) {
2608 if (ev->surface->output_mask & (1u << output->id)) {
2609 /*
2610 * The desired_protection of the output should be the
2611 * maximum of the desired_protection of the surfaces,
2612 * that are displayed on that output, to avoid
2613 * reducing the protection for existing surfaces.
2614 */
2615 if (ev->surface->desired_protection > highest_requested)
2616 highest_requested =
2617 ev->surface->desired_protection;
2618 }
2619 }
2620
2621 output->desired_protection = highest_requested;
2622
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002623 if (output->assign_planes && !output->disable_planes) {
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002624 output->assign_planes(output, repaint_data);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002625 } else {
2626 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002627 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002628 ev->psf_flags = 0;
2629 }
2630 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002631
Pekka Paalanene67858b2013-04-25 13:57:42 +03002632 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002633 wl_list_for_each(ev, &ec->view_list, link) {
2634 /* Note: This operation is safe to do multiple times on the
2635 * same surface.
2636 */
2637 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002638 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002639 &ev->surface->frame_callback_list);
2640 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002641
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002642 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002643 }
2644 }
2645
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002646 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002647
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002648 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002649 pixman_region32_intersect(&output_damage,
2650 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002651 pixman_region32_subtract(&output_damage,
2652 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002653
Scott Moreauccbf29d2012-02-22 14:21:41 -07002654 if (output->dirty)
2655 weston_output_update_matrix(output);
2656
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002657 r = output->repaint(output, &output_damage, repaint_data);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002658
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002659 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002660
Daniel Stone09a97e22017-03-01 11:34:06 +00002661 output->repaint_needed = false;
Daniel Stone05df8c12017-03-03 16:59:42 +00002662 if (r == 0)
2663 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002664
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002665 weston_compositor_repick(ec);
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002666
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002667 frame_time_msec = timespec_to_msec(&output->frame_time);
2668
Jonas Ådahldb773762012-06-13 00:01:21 +02002669 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002670 wl_callback_send_done(cb->resource, frame_time_msec);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002671 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002672 }
2673
Scott Moreaud64cf212012-06-08 19:40:54 -06002674 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002675 animation->frame_counter++;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002676 animation->frame(animation, output, &output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002677 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002678
Pekka Paalanenb5026542014-11-12 15:09:24 +02002679 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2680
David Herrmann1edf44c2013-10-22 17:11:26 +02002681 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002682}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002683
Pekka Paalanen82919792014-05-21 13:51:49 +03002684static void
2685weston_output_schedule_repaint_reset(struct weston_output *output)
2686{
Daniel Stone05df8c12017-03-03 16:59:42 +00002687 output->repaint_status = REPAINT_NOT_SCHEDULED;
Pekka Paalanen82919792014-05-21 13:51:49 +03002688 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
Pekka Paalanen82919792014-05-21 13:51:49 +03002689}
2690
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002691static int
2692weston_output_maybe_repaint(struct weston_output *output, struct timespec *now,
2693 void *repaint_data)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002694{
Pekka Paalanen0513a952014-05-21 16:17:27 +03002695 struct weston_compositor *compositor = output->compositor;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002696 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002697 int64_t msec_to_repaint;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002698
Daniel Stone6847b852017-03-01 11:34:08 +00002699 /* We're not ready yet; come back to make a decision later. */
2700 if (output->repaint_status != REPAINT_SCHEDULED)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002701 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002702
2703 msec_to_repaint = timespec_sub_to_msec(&output->next_repaint, now);
2704 if (msec_to_repaint > 1)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002705 return ret;
Daniel Stone05df8c12017-03-03 16:59:42 +00002706
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002707 /* If we're sleeping, drop the repaint machinery entirely; we will
2708 * explicitly repaint all outputs when we come back. */
2709 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2710 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
2711 goto err;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002712
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002713 /* We don't actually need to repaint this output; drop it from
2714 * repaint until something causes damage. */
2715 if (!output->repaint_needed)
2716 goto err;
2717
2718 /* If repaint fails, we aren't going to get weston_output_finish_frame
2719 * to trigger a new repaint, so drop it from repaint and hope
Daniel Stone6847b852017-03-01 11:34:08 +00002720 * something schedules a successful repaint later. As repainting may
2721 * take some time, re-read our clock as a courtesy to the next
2722 * output. */
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002723 ret = weston_output_repaint(output, repaint_data);
Daniel Stone6847b852017-03-01 11:34:08 +00002724 weston_compositor_read_presentation_clock(compositor, now);
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002725 if (ret != 0)
2726 goto err;
2727
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09002728 output->repainted = true;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002729 return ret;
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002730
2731err:
Pekka Paalanen0513a952014-05-21 16:17:27 +03002732 weston_output_schedule_repaint_reset(output);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002733 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002734}
2735
2736static void
2737output_repaint_timer_arm(struct weston_compositor *compositor)
2738{
2739 struct weston_output *output;
2740 bool any_should_repaint = false;
2741 struct timespec now;
Sergi Granellb4c08862017-03-18 13:01:15 +01002742 int64_t msec_to_next = INT64_MAX;
Daniel Stone6847b852017-03-01 11:34:08 +00002743
2744 weston_compositor_read_presentation_clock(compositor, &now);
2745
2746 wl_list_for_each(output, &compositor->output_list, link) {
2747 int64_t msec_to_this;
2748
2749 if (output->repaint_status != REPAINT_SCHEDULED)
2750 continue;
2751
2752 msec_to_this = timespec_sub_to_msec(&output->next_repaint,
2753 &now);
2754 if (!any_should_repaint || msec_to_this < msec_to_next)
2755 msec_to_next = msec_to_this;
2756
2757 any_should_repaint = true;
2758 }
2759
2760 if (!any_should_repaint)
2761 return;
2762
2763 /* Even if we should repaint immediately, add the minimum 1 ms delay.
2764 * This is a workaround to allow coalescing multiple output repaints
2765 * particularly from weston_output_finish_frame()
2766 * into the same call, which would not happen if we called
2767 * output_repaint_timer_handler() directly.
2768 */
2769 if (msec_to_next < 1)
2770 msec_to_next = 1;
2771
2772 wl_event_source_timer_update(compositor->repaint_timer, msec_to_next);
2773}
2774
2775static int
2776output_repaint_timer_handler(void *data)
2777{
2778 struct weston_compositor *compositor = data;
2779 struct weston_output *output;
2780 struct timespec now;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002781 void *repaint_data = NULL;
Emre Ucane479ed82018-03-20 15:29:40 +01002782 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002783
2784 weston_compositor_read_presentation_clock(compositor, &now);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002785
2786 if (compositor->backend->repaint_begin)
2787 repaint_data = compositor->backend->repaint_begin(compositor);
2788
2789 wl_list_for_each(output, &compositor->output_list, link) {
2790 ret = weston_output_maybe_repaint(output, &now, repaint_data);
2791 if (ret)
2792 break;
2793 }
2794
2795 if (ret == 0) {
Tomohito Esaki09bfcd62018-06-05 10:37:05 +09002796 if (compositor->backend->repaint_flush)
Antonio Borneoc90fccc2019-06-30 15:51:10 +02002797 ret = compositor->backend->repaint_flush(compositor,
2798 repaint_data);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002799 } else {
Antonio Borneoc90fccc2019-06-30 15:51:10 +02002800 if (compositor->backend->repaint_cancel)
2801 compositor->backend->repaint_cancel(compositor,
2802 repaint_data);
2803 }
2804
2805 if (ret != 0) {
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09002806 wl_list_for_each(output, &compositor->output_list, link) {
2807 if (output->repainted)
2808 weston_output_schedule_repaint_reset(output);
2809 }
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002810 }
Daniel Stone6847b852017-03-01 11:34:08 +00002811
Tomohito Esakiddaf95c2018-07-10 11:47:15 +09002812 wl_list_for_each(output, &compositor->output_list, link)
2813 output->repainted = false;
2814
Daniel Stone6847b852017-03-01 11:34:08 +00002815 output_repaint_timer_arm(compositor);
2816
Pekka Paalanen0513a952014-05-21 16:17:27 +03002817 return 0;
2818}
2819
Marius Vlad55d87362019-06-11 01:15:35 +03002820/**
2821 * \ingroup output
2822 */
Kristian Høgsbergef044142011-06-21 15:02:12 -04002823WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002824weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002825 const struct timespec *stamp,
2826 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002827{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002828 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002829 int32_t refresh_nsec;
2830 struct timespec now;
Daniel Stone6847b852017-03-01 11:34:08 +00002831 int64_t msec_rel;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002832
Pekka Paalanenb5026542014-11-12 15:09:24 +02002833
Daniel Stone05df8c12017-03-03 16:59:42 +00002834 assert(output->repaint_status == REPAINT_AWAITING_COMPLETION);
Daniel Stone3615ce12017-03-01 11:34:05 +00002835 assert(stamp || (presented_flags & WP_PRESENTATION_FEEDBACK_INVALID));
2836
Daniel Stone6847b852017-03-01 11:34:08 +00002837 weston_compositor_read_presentation_clock(compositor, &now);
2838
Daniel Stone3615ce12017-03-01 11:34:05 +00002839 /* If we haven't been supplied any timestamp at all, we don't have a
2840 * timebase to work against, so any delay just wastes time. Push a
2841 * repaint as soon as possible so we can get on with it. */
Daniel Stone6847b852017-03-01 11:34:08 +00002842 if (!stamp) {
2843 output->next_repaint = now;
Daniel Stone3615ce12017-03-01 11:34:05 +00002844 goto out;
Daniel Stone6847b852017-03-01 11:34:08 +00002845 }
Daniel Stone3615ce12017-03-01 11:34:05 +00002846
Marius Vladdf9278a2018-03-06 18:56:23 +02002847 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2848 TLP_VBLANK(stamp), TLP_END);
2849
Pekka Paalanend7894d02015-07-03 15:08:53 +03002850 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002851 weston_presentation_feedback_present_list(&output->feedback_list,
2852 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002853 output->msc,
2854 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002855
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002856 output->frame_time = *stamp;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002857
Daniel Stone6847b852017-03-01 11:34:08 +00002858 timespec_add_nsec(&output->next_repaint, stamp, refresh_nsec);
2859 timespec_add_msec(&output->next_repaint, &output->next_repaint,
2860 -compositor->repaint_msec);
2861 msec_rel = timespec_sub_to_msec(&output->next_repaint, &now);
Daniel Stone84aff5c2017-03-01 11:34:04 +00002862
2863 if (msec_rel < -1000 || msec_rel > 1000) {
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002864 static bool warned;
2865
2866 if (!warned)
2867 weston_log("Warning: computed repaint delay is "
Daniel Stone6847b852017-03-01 11:34:08 +00002868 "insane: %lld msec\n", (long long) msec_rel);
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002869 warned = true;
2870
Daniel Stone6847b852017-03-01 11:34:08 +00002871 output->next_repaint = now;
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002872 }
2873
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002874 /* Called from restart_repaint_loop and restart happens already after
2875 * the deadline given by repaint_msec? In that case we delay until
2876 * the deadline of the next frame, to give clients a more predictable
2877 * timing of the repaint cycle to lock on. */
Daniel Stoneeca5cca2017-02-28 21:53:51 +00002878 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID &&
2879 msec_rel < 0) {
2880 while (timespec_sub_to_nsec(&output->next_repaint, &now) < 0) {
2881 timespec_add_nsec(&output->next_repaint,
2882 &output->next_repaint,
2883 refresh_nsec);
2884 }
2885 }
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002886
Daniel Stone3615ce12017-03-01 11:34:05 +00002887out:
Daniel Stone05df8c12017-03-03 16:59:42 +00002888 output->repaint_status = REPAINT_SCHEDULED;
Daniel Stone6847b852017-03-01 11:34:08 +00002889 output_repaint_timer_arm(compositor);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002890}
2891
2892static void
2893idle_repaint(void *data)
2894{
2895 struct weston_output *output = data;
Antonio Borneoc90fccc2019-06-30 15:51:10 +02002896 int ret;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002897
Daniel Stone05df8c12017-03-03 16:59:42 +00002898 assert(output->repaint_status == REPAINT_BEGIN_FROM_IDLE);
2899 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03002900 output->idle_repaint_source = NULL;
Antonio Borneoc90fccc2019-06-30 15:51:10 +02002901 ret = output->start_repaint_loop(output);
2902 if (ret != 0)
2903 weston_output_schedule_repaint_reset(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002904}
2905
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002906WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002907weston_layer_entry_insert(struct weston_layer_entry *list,
2908 struct weston_layer_entry *entry)
2909{
2910 wl_list_insert(&list->link, &entry->link);
2911 entry->layer = list->layer;
2912}
2913
2914WL_EXPORT void
2915weston_layer_entry_remove(struct weston_layer_entry *entry)
2916{
2917 wl_list_remove(&entry->link);
2918 wl_list_init(&entry->link);
2919 entry->layer = NULL;
2920}
2921
Quentin Glidic82681572016-12-17 13:40:51 +01002922
2923/** Initialize the weston_layer struct.
2924 *
2925 * \param compositor The compositor instance
2926 * \param layer The layer to initialize
2927 */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002928WL_EXPORT void
Quentin Glidic82681572016-12-17 13:40:51 +01002929weston_layer_init(struct weston_layer *layer,
2930 struct weston_compositor *compositor)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002931{
Quentin Glidic82681572016-12-17 13:40:51 +01002932 layer->compositor = compositor;
2933 wl_list_init(&layer->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002934 wl_list_init(&layer->view_list.link);
2935 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002936 weston_layer_set_mask_infinite(layer);
Quentin Glidic82681572016-12-17 13:40:51 +01002937}
2938
2939/** Sets the position of the layer in the layer list. The layer will be placed
2940 * below any layer with the same position value, if any.
2941 * This function is safe to call if the layer is already on the list, but the
2942 * layer may be moved below other layers at the same position, if any.
2943 *
2944 * \param layer The layer to modify
2945 * \param position The position the layer will be placed at
2946 */
2947WL_EXPORT void
2948weston_layer_set_position(struct weston_layer *layer,
2949 enum weston_layer_position position)
2950{
2951 struct weston_layer *below;
2952
2953 wl_list_remove(&layer->link);
2954
2955 /* layer_list is ordered from top to bottom, the last layer being the
2956 * background with the smallest position value */
2957
2958 layer->position = position;
2959 wl_list_for_each_reverse(below, &layer->compositor->layer_list, link) {
2960 if (below->position >= layer->position) {
2961 wl_list_insert(&below->link, &layer->link);
2962 return;
2963 }
2964 }
2965 wl_list_insert(&layer->compositor->layer_list, &layer->link);
2966}
2967
2968/** Hide a layer by taking it off the layer list.
2969 * This function is safe to call if the layer is not on the list.
2970 *
2971 * \param layer The layer to hide
2972 */
2973WL_EXPORT void
2974weston_layer_unset_position(struct weston_layer *layer)
2975{
2976 wl_list_remove(&layer->link);
2977 wl_list_init(&layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002978}
2979
2980WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002981weston_layer_set_mask(struct weston_layer *layer,
2982 int x, int y, int width, int height)
2983{
2984 struct weston_view *view;
2985
2986 layer->mask.x1 = x;
2987 layer->mask.x2 = x + width;
2988 layer->mask.y1 = y;
2989 layer->mask.y2 = y + height;
2990
2991 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2992 weston_view_geometry_dirty(view);
2993 }
2994}
2995
2996WL_EXPORT void
2997weston_layer_set_mask_infinite(struct weston_layer *layer)
2998{
2999 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
3000 UINT32_MAX, UINT32_MAX);
3001}
3002
Daniel Stone3b775632018-07-20 08:38:25 +01003003WL_EXPORT bool
3004weston_layer_mask_is_infinite(struct weston_layer *layer)
3005{
3006 return layer->mask.x1 == INT32_MIN &&
3007 layer->mask.y1 == INT32_MIN &&
3008 layer->mask.x2 == INT32_MIN + UINT32_MAX &&
3009 layer->mask.y2 == INT32_MIN + UINT32_MAX;
3010}
3011
Marius Vlad55d87362019-06-11 01:15:35 +03003012/**
3013 * \ingroup output
3014 */
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003015WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003016weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003017{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003018 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04003019 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01003020
Bryce Harrington08976ac2016-08-30 12:05:16 -07003021 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
3022 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003023 return;
3024
Pekka Paalanenb5026542014-11-12 15:09:24 +02003025 if (!output->repaint_needed)
3026 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
3027
Kristian Høgsbergef044142011-06-21 15:02:12 -04003028 loop = wl_display_get_event_loop(compositor->wl_display);
Daniel Stone09a97e22017-03-01 11:34:06 +00003029 output->repaint_needed = true;
Daniel Stone05df8c12017-03-03 16:59:42 +00003030
3031 /* If we already have a repaint scheduled for our idle handler,
3032 * no need to set it again. If the repaint has been called but
3033 * not finished, then weston_output_finish_frame() will notice
3034 * that a repaint is needed and schedule one. */
3035 if (output->repaint_status != REPAINT_NOT_SCHEDULED)
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003036 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01003037
Daniel Stone05df8c12017-03-03 16:59:42 +00003038 output->repaint_status = REPAINT_BEGIN_FROM_IDLE;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03003039 assert(!output->idle_repaint_source);
3040 output->idle_repaint_source = wl_event_loop_add_idle(loop, idle_repaint,
3041 output);
Pekka Paalanenb5026542014-11-12 15:09:24 +02003042 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003043}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05003044
Marius Vlad9fdda7f2019-06-11 16:08:55 +03003045/** weston_compositor_schedule_repaint
3046 * \ingroup compositor
3047 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003048WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003049weston_compositor_schedule_repaint(struct weston_compositor *compositor)
3050{
3051 struct weston_output *output;
3052
3053 wl_list_for_each(output, &compositor->output_list, link)
3054 weston_output_schedule_repaint(output);
3055}
3056
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003057static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003058surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003059{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003060 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003061}
3062
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003063static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003064surface_attach(struct wl_client *client,
3065 struct wl_resource *resource,
3066 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
3067{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003068 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05003069 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003070
Kristian Høgsbergab19f932013-08-20 11:30:54 -07003071 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05003072 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07003073 if (buffer == NULL) {
3074 wl_client_post_no_memory(client);
3075 return;
3076 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07003077 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04003078
Pekka Paalanende685b82012-12-04 15:58:12 +02003079 /* Attach, attach, without commit in between does not send
3080 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003081 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003082
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003083 surface->pending.sx = sx;
3084 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01003085 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003086}
3087
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003088static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003089surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003090 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003091 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05003092{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003093 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003094
Derek Foreman57e92ed2015-11-17 14:11:35 -06003095 if (width <= 0 || height <= 0)
3096 return;
3097
Derek Foreman152254b2015-11-26 14:17:48 -06003098 pixman_region32_union_rect(&surface->pending.damage_surface,
3099 &surface->pending.damage_surface,
3100 x, y, width, height);
3101}
3102
3103static void
3104surface_damage_buffer(struct wl_client *client,
3105 struct wl_resource *resource,
3106 int32_t x, int32_t y, int32_t width, int32_t height)
3107{
3108 struct weston_surface *surface = wl_resource_get_user_data(resource);
3109
3110 if (width <= 0 || height <= 0)
3111 return;
3112
3113 pixman_region32_union_rect(&surface->pending.damage_buffer,
3114 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04003115 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003116}
3117
Kristian Høgsberg33418202011-08-16 23:01:28 -04003118static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003119destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04003120{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05003121 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003122
3123 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02003124 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003125}
3126
3127static void
3128surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003129 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04003130{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003131 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003132 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003133
3134 cb = malloc(sizeof *cb);
3135 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003136 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003137 return;
3138 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03003139
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003140 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
3141 callback);
3142 if (cb->resource == NULL) {
3143 free(cb);
3144 wl_resource_post_no_memory(resource);
3145 return;
3146 }
3147
Jason Ekstranda85118c2013-06-27 20:17:02 -05003148 wl_resource_set_implementation(cb->resource, NULL, cb,
3149 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003150
Pekka Paalanenbc106382012-10-10 12:49:31 +03003151 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003152}
3153
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003154static void
3155surface_set_opaque_region(struct wl_client *client,
3156 struct wl_resource *resource,
3157 struct wl_resource *region_resource)
3158{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003159 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003160 struct weston_region *region;
3161
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003162 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003163 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003164 pixman_region32_copy(&surface->pending.opaque,
3165 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003166 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07003167 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003168 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003169}
3170
3171static void
3172surface_set_input_region(struct wl_client *client,
3173 struct wl_resource *resource,
3174 struct wl_resource *region_resource)
3175{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003176 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003177 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003178
3179 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003180 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003181 pixman_region32_copy(&surface->pending.input,
3182 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003183 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003184 pixman_region32_fini(&surface->pending.input);
3185 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003186 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003187}
3188
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003189/* Cause damage to this sub-surface and all its children.
3190 *
3191 * This is useful when there are state changes that need an implicit
3192 * damage, e.g. a z-order change.
3193 */
3194static void
3195weston_surface_damage_subsurfaces(struct weston_subsurface *sub)
3196{
3197 struct weston_subsurface *child;
3198
3199 weston_surface_damage(sub->surface);
3200 sub->reordered = false;
3201
3202 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link)
3203 if (child != sub)
3204 weston_surface_damage_subsurfaces(child);
3205}
3206
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003207static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003208weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003209{
Pekka Paalanene67858b2013-04-25 13:57:42 +03003210 struct weston_subsurface *sub;
3211
3212 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
3213 parent_link_pending) {
3214 wl_list_remove(&sub->parent_link);
3215 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003216
3217 if (sub->reordered)
3218 weston_surface_damage_subsurfaces(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003219 }
3220}
3221
3222static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03003223weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05003224 struct weston_matrix *matrix)
3225{
Pekka Paalanen04baea52016-04-26 15:50:58 +03003226 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05003227 double src_width, src_height, dest_width, dest_height;
3228
3229 weston_matrix_init(matrix);
3230
3231 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
3232 src_width = surface->width_from_buffer;
3233 src_height = surface->height_from_buffer;
3234 } else {
3235 src_width = wl_fixed_to_double(vp->buffer.src_width);
3236 src_height = wl_fixed_to_double(vp->buffer.src_height);
3237 }
3238
3239 if (vp->surface.width == -1) {
3240 dest_width = src_width;
3241 dest_height = src_height;
3242 } else {
3243 dest_width = vp->surface.width;
3244 dest_height = vp->surface.height;
3245 }
3246
3247 if (src_width != dest_width || src_height != dest_height)
3248 weston_matrix_scale(matrix,
3249 src_width / dest_width,
3250 src_height / dest_height, 1);
3251
3252 if (vp->buffer.src_width != wl_fixed_from_int(-1))
3253 weston_matrix_translate(matrix,
3254 wl_fixed_to_double(vp->buffer.src_x),
3255 wl_fixed_to_double(vp->buffer.src_y),
3256 0);
3257
3258 switch (vp->buffer.transform) {
3259 case WL_OUTPUT_TRANSFORM_FLIPPED:
3260 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3261 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3262 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3263 weston_matrix_scale(matrix, -1, 1, 1);
3264 weston_matrix_translate(matrix,
3265 surface->width_from_buffer, 0, 0);
3266 break;
3267 }
3268
3269 switch (vp->buffer.transform) {
3270 default:
3271 case WL_OUTPUT_TRANSFORM_NORMAL:
3272 case WL_OUTPUT_TRANSFORM_FLIPPED:
3273 break;
3274 case WL_OUTPUT_TRANSFORM_90:
3275 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3276 weston_matrix_rotate_xy(matrix, 0, 1);
3277 weston_matrix_translate(matrix,
3278 surface->height_from_buffer, 0, 0);
3279 break;
3280 case WL_OUTPUT_TRANSFORM_180:
3281 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3282 weston_matrix_rotate_xy(matrix, -1, 0);
3283 weston_matrix_translate(matrix,
3284 surface->width_from_buffer,
3285 surface->height_from_buffer, 0);
3286 break;
3287 case WL_OUTPUT_TRANSFORM_270:
3288 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3289 weston_matrix_rotate_xy(matrix, 0, -1);
3290 weston_matrix_translate(matrix,
3291 0, surface->width_from_buffer, 0);
3292 break;
3293 }
3294
3295 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
3296}
3297
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003298/**
3299 * Compute a + b > c while being safe to overflows.
3300 */
3301static bool
3302fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
3303{
3304 return (int64_t)a + (int64_t)b > (int64_t)c;
3305}
3306
3307static bool
3308weston_surface_is_pending_viewport_source_valid(
3309 const struct weston_surface *surface)
3310{
3311 const struct weston_surface_state *pend = &surface->pending;
3312 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
3313 int width_from_buffer = 0;
3314 int height_from_buffer = 0;
3315 wl_fixed_t w;
3316 wl_fixed_t h;
3317
3318 /* If viewport source rect is not set, it is always ok. */
3319 if (vp->buffer.src_width == wl_fixed_from_int(-1))
3320 return true;
3321
3322 if (pend->newly_attached) {
3323 if (pend->buffer) {
3324 convert_size_by_transform_scale(&width_from_buffer,
3325 &height_from_buffer,
3326 pend->buffer->width,
3327 pend->buffer->height,
3328 vp->buffer.transform,
3329 vp->buffer.scale);
3330 } else {
3331 /* No buffer: viewport is irrelevant. */
3332 return true;
3333 }
3334 } else {
3335 width_from_buffer = surface->width_from_buffer;
3336 height_from_buffer = surface->height_from_buffer;
3337 }
3338
3339 assert((width_from_buffer == 0) == (height_from_buffer == 0));
3340 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
3341
3342 /* No buffer: viewport is irrelevant. */
3343 if (width_from_buffer == 0 || height_from_buffer == 0)
3344 return true;
3345
3346 /* overflow checks for wl_fixed_from_int() */
3347 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
3348 return false;
3349 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
3350 return false;
3351
3352 w = wl_fixed_from_int(width_from_buffer);
3353 h = wl_fixed_from_int(height_from_buffer);
3354
3355 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
3356 return false;
3357 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
3358 return false;
3359
3360 return true;
3361}
3362
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003363static bool
3364fixed_is_integer(wl_fixed_t v)
3365{
3366 return (v & 0xff) == 0;
3367}
3368
3369static bool
3370weston_surface_is_pending_viewport_dst_size_int(
3371 const struct weston_surface *surface)
3372{
3373 const struct weston_buffer_viewport *vp =
3374 &surface->pending.buffer_viewport;
3375
3376 if (vp->surface.width != -1) {
3377 assert(vp->surface.width > 0 && vp->surface.height > 0);
3378 return true;
3379 }
3380
3381 return fixed_is_integer(vp->buffer.src_width) &&
3382 fixed_is_integer(vp->buffer.src_height);
3383}
3384
Derek Foreman152254b2015-11-26 14:17:48 -06003385/* Translate pending damage in buffer co-ordinates to surface
3386 * co-ordinates and union it with a pixman_region32_t.
3387 * This should only be called after the buffer is attached.
3388 */
3389static void
3390apply_damage_buffer(pixman_region32_t *dest,
3391 struct weston_surface *surface,
3392 struct weston_surface_state *state)
3393{
3394 struct weston_buffer *buffer = surface->buffer_ref.buffer;
3395
3396 /* wl_surface.damage_buffer needs to be clipped to the buffer,
3397 * translated into surface co-ordinates and unioned with
3398 * any other surface damage.
3399 * None of this makes sense if there is no buffer though.
3400 */
3401 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
3402 pixman_region32_t buffer_damage;
3403
3404 pixman_region32_intersect_rect(&state->damage_buffer,
3405 &state->damage_buffer,
3406 0, 0, buffer->width,
3407 buffer->height);
3408 pixman_region32_init(&buffer_damage);
3409 weston_matrix_transform_region(&buffer_damage,
3410 &surface->buffer_to_surface_matrix,
3411 &state->damage_buffer);
3412 pixman_region32_union(dest, dest, &buffer_damage);
3413 pixman_region32_fini(&buffer_damage);
3414 }
3415 /* We should clear this on commit even if there was no buffer */
3416 pixman_region32_clear(&state->damage_buffer);
3417}
3418
Jason Ekstrand1e059042014-10-16 10:55:19 -05003419static void
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303420weston_surface_set_desired_protection(struct weston_surface *surface,
3421 enum weston_hdcp_protection protection)
3422{
3423 if (surface->desired_protection == protection)
3424 return;
3425 surface->desired_protection = protection;
3426 weston_surface_damage(surface);
3427}
3428
3429static void
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303430weston_surface_set_protection_mode(struct weston_surface *surface,
3431 enum weston_surface_protection_mode p_mode)
3432{
3433 struct content_protection *cp = surface->compositor->content_protection;
3434 struct protected_surface *psurface;
3435
3436 surface->protection_mode = p_mode;
3437 wl_list_for_each(psurface, &cp->protected_list, link) {
3438 if (!psurface || psurface->surface != surface)
3439 continue;
3440 weston_protected_surface_send_event(psurface,
3441 surface->current_protection);
3442 }
3443}
3444
3445static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05003446weston_surface_commit_state(struct weston_surface *surface,
3447 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003448{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003449 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003450 pixman_region32_t opaque;
3451
Alexander Larsson4ea95522013-05-22 14:41:37 +02003452 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02003453 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03003454 /* wp_viewport.set_source */
3455 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003456 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003457
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003458 /* wl_surface.attach */
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003459 if (state->newly_attached) {
3460 /* zwp_surface_synchronization_v1.set_acquire_fence */
3461 fd_move(&surface->acquire_fence_fd,
3462 &state->acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003463 /* zwp_surface_synchronization_v1.get_release */
3464 weston_buffer_release_move(&surface->buffer_release_ref,
3465 &state->buffer_release_ref);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003466 weston_surface_attach(surface, state->buffer);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003467 }
Jason Ekstrand7b982072014-05-20 14:33:03 -05003468 weston_surface_state_set_buffer(state, NULL);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003469 assert(state->acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003470 assert(state->buffer_release_ref.buffer_release == NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01003471
Jason Ekstrand1e059042014-10-16 10:55:19 -05003472 weston_surface_build_buffer_matrix(surface,
3473 &surface->surface_to_buffer_matrix);
3474 weston_matrix_invert(&surface->buffer_to_surface_matrix,
3475 &surface->surface_to_buffer_matrix);
3476
Jason Ekstrand7b982072014-05-20 14:33:03 -05003477 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003478 weston_surface_update_size(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003479 if (surface->committed)
3480 surface->committed(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003481 }
Giulio Camuffo184df502013-02-21 11:29:21 +01003482
Jason Ekstrand7b982072014-05-20 14:33:03 -05003483 state->sx = 0;
3484 state->sy = 0;
3485 state->newly_attached = 0;
3486 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03003487
Derek Foreman152254b2015-11-26 14:17:48 -06003488 /* wl_surface.damage and wl_surface.damage_buffer */
Pekka Paalanenb5026542014-11-12 15:09:24 +02003489 if (weston_timeline_enabled_ &&
Derek Foreman152254b2015-11-26 14:17:48 -06003490 (pixman_region32_not_empty(&state->damage_surface) ||
3491 pixman_region32_not_empty(&state->damage_buffer)))
Pekka Paalanenb5026542014-11-12 15:09:24 +02003492 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06003493
Pekka Paalanen8e159182012-10-10 12:49:25 +03003494 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06003495 &state->damage_surface);
3496
3497 apply_damage_buffer(&surface->damage, surface, state);
3498
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05003499 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07003500 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06003501 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003502
3503 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003504 pixman_region32_init(&opaque);
3505 pixman_region32_intersect_rect(&opaque, &state->opaque,
3506 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003507
3508 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
3509 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003510 wl_list_for_each(view, &surface->views, surface_link)
3511 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003512 }
3513
3514 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003515
3516 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003517 pixman_region32_intersect_rect(&surface->input, &state->input,
3518 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003519
Pekka Paalanenbc106382012-10-10 12:49:31 +03003520 /* wl_surface.frame */
3521 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05003522 &state->frame_callback_list);
3523 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003524
3525 /* XXX:
3526 * What should happen with a feedback request, if there
3527 * is no wl_buffer attached for this commit?
3528 */
3529
3530 /* presentation.feedback */
3531 wl_list_insert_list(&surface->feedback_list,
3532 &state->feedback_list);
3533 wl_list_init(&state->feedback_list);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003534
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303535 /* weston_protected_surface.enforced/relaxed */
3536 if (surface->protection_mode != state->protection_mode)
3537 weston_surface_set_protection_mode(surface,
3538 state->protection_mode);
3539
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303540 /* weston_protected_surface.set_type */
3541 weston_surface_set_desired_protection(surface, state->desired_protection);
3542
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003543 wl_signal_emit(&surface->commit_signal, surface);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003544}
3545
3546static void
3547weston_surface_commit(struct weston_surface *surface)
3548{
3549 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003550
Pekka Paalanene67858b2013-04-25 13:57:42 +03003551 weston_surface_commit_subsurface_order(surface);
3552
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003553 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003554}
3555
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003556static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003557weston_subsurface_commit(struct weston_subsurface *sub);
3558
3559static void
3560weston_subsurface_parent_commit(struct weston_subsurface *sub,
3561 int parent_is_synchronized);
3562
3563static void
3564surface_commit(struct wl_client *client, struct wl_resource *resource)
3565{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003566 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003567 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
3568
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003569 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
3570 assert(surface->viewport_resource);
3571
3572 wl_resource_post_error(surface->viewport_resource,
3573 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
3574 "wl_surface@%d has viewport source outside buffer",
3575 wl_resource_get_id(resource));
3576 return;
3577 }
3578
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003579 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
3580 assert(surface->viewport_resource);
3581
3582 wl_resource_post_error(surface->viewport_resource,
3583 WP_VIEWPORT_ERROR_BAD_SIZE,
3584 "wl_surface@%d viewport dst size not integer",
3585 wl_resource_get_id(resource));
3586 return;
3587 }
3588
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003589 if (surface->pending.acquire_fence_fd >= 0) {
3590 assert(surface->synchronization_resource);
3591
3592 if (!surface->pending.buffer) {
3593 fd_clear(&surface->pending.acquire_fence_fd);
3594 wl_resource_post_error(surface->synchronization_resource,
3595 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3596 "wl_surface@%"PRIu32" no buffer for synchronization",
3597 wl_resource_get_id(resource));
3598 return;
3599 }
3600
3601 /* We support fences for both wp_linux_dmabuf and opaque EGL
3602 * buffers, as mandated by minor version 2 of the
3603 * zwp_linux_explicit_synchronization_v1 protocol. Since
3604 * renderers that support fences currently only support these
3605 * two buffer types plus SHM buffers, we can just check for the
3606 * SHM buffer case here.
3607 */
3608 if (wl_shm_buffer_get(surface->pending.buffer->resource)) {
3609 fd_clear(&surface->pending.acquire_fence_fd);
3610 wl_resource_post_error(surface->synchronization_resource,
3611 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_UNSUPPORTED_BUFFER,
3612 "wl_surface@%"PRIu32" unsupported buffer for synchronization",
3613 wl_resource_get_id(resource));
3614 return;
3615 }
3616 }
3617
Alexandros Frantzis67629672018-10-19 12:14:11 +03003618 if (surface->pending.buffer_release_ref.buffer_release &&
3619 !surface->pending.buffer) {
3620 assert(surface->synchronization_resource);
3621
3622 wl_resource_post_error(surface->synchronization_resource,
3623 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3624 "wl_surface@%"PRIu32" no buffer for synchronization",
3625 wl_resource_get_id(resource));
3626 return;
3627 }
3628
Pekka Paalanene67858b2013-04-25 13:57:42 +03003629 if (sub) {
3630 weston_subsurface_commit(sub);
3631 return;
3632 }
3633
3634 weston_surface_commit(surface);
3635
3636 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
3637 if (sub->surface != surface)
3638 weston_subsurface_parent_commit(sub, 0);
3639 }
3640}
3641
3642static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003643surface_set_buffer_transform(struct wl_client *client,
3644 struct wl_resource *resource, int transform)
3645{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003646 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003647
Jonny Lamba55f1392014-05-30 12:07:15 +02003648 /* if wl_output.transform grows more members this will need to be updated. */
3649 if (transform < 0 ||
3650 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
3651 wl_resource_post_error(resource,
3652 WL_SURFACE_ERROR_INVALID_TRANSFORM,
3653 "buffer transform must be a valid transform "
3654 "('%d' specified)", transform);
3655 return;
3656 }
3657
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003658 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003659 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003660}
3661
Alexander Larsson4ea95522013-05-22 14:41:37 +02003662static void
3663surface_set_buffer_scale(struct wl_client *client,
3664 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003665 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003666{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003667 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003668
Jonny Lamba55f1392014-05-30 12:07:15 +02003669 if (scale < 1) {
3670 wl_resource_post_error(resource,
3671 WL_SURFACE_ERROR_INVALID_SCALE,
3672 "buffer scale must be at least one "
3673 "('%d' specified)", scale);
3674 return;
3675 }
3676
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003677 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003678 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003679}
3680
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003681static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003682 surface_destroy,
3683 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04003684 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003685 surface_frame,
3686 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003687 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003688 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02003689 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06003690 surface_set_buffer_scale,
3691 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003692};
3693
3694static void
3695compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003696 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003697{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04003698 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003699 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003700
Kristian Høgsberg18c93002012-01-27 11:58:31 -05003701 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003702 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003703 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003704 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003705 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003706
Jason Ekstranda85118c2013-06-27 20:17:02 -05003707 surface->resource =
3708 wl_resource_create(client, &wl_surface_interface,
3709 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003710 if (surface->resource == NULL) {
3711 weston_surface_destroy(surface);
3712 wl_resource_post_no_memory(resource);
3713 return;
3714 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003715 wl_resource_set_implementation(surface->resource, &surface_interface,
3716 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003717
3718 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003719}
3720
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003721static void
3722destroy_region(struct wl_resource *resource)
3723{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003724 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003725
3726 pixman_region32_fini(&region->region);
3727 free(region);
3728}
3729
3730static void
3731region_destroy(struct wl_client *client, struct wl_resource *resource)
3732{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003733 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003734}
3735
3736static void
3737region_add(struct wl_client *client, struct wl_resource *resource,
3738 int32_t x, int32_t y, int32_t width, int32_t height)
3739{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003740 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003741
3742 pixman_region32_union_rect(&region->region, &region->region,
3743 x, y, width, height);
3744}
3745
3746static void
3747region_subtract(struct wl_client *client, struct wl_resource *resource,
3748 int32_t x, int32_t y, int32_t width, int32_t height)
3749{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003750 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003751 pixman_region32_t rect;
3752
3753 pixman_region32_init_rect(&rect, x, y, width, height);
3754 pixman_region32_subtract(&region->region, &region->region, &rect);
3755 pixman_region32_fini(&rect);
3756}
3757
3758static const struct wl_region_interface region_interface = {
3759 region_destroy,
3760 region_add,
3761 region_subtract
3762};
3763
3764static void
3765compositor_create_region(struct wl_client *client,
3766 struct wl_resource *resource, uint32_t id)
3767{
3768 struct weston_region *region;
3769
3770 region = malloc(sizeof *region);
3771 if (region == NULL) {
3772 wl_resource_post_no_memory(resource);
3773 return;
3774 }
3775
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003776 pixman_region32_init(&region->region);
3777
Jason Ekstranda85118c2013-06-27 20:17:02 -05003778 region->resource =
3779 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003780 if (region->resource == NULL) {
3781 free(region);
3782 wl_resource_post_no_memory(resource);
3783 return;
3784 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003785 wl_resource_set_implementation(region->resource, &region_interface,
3786 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003787}
3788
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003789static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003790 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003791 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003792};
3793
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003794static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003795weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3796{
3797 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003798
Jason Ekstrand7b982072014-05-20 14:33:03 -05003799 weston_surface_commit_state(surface, &sub->cached);
3800 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003801
3802 weston_surface_commit_subsurface_order(surface);
3803
3804 weston_surface_schedule_repaint(surface);
3805
Jason Ekstrand7b982072014-05-20 14:33:03 -05003806 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003807}
3808
3809static void
3810weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3811{
3812 struct weston_surface *surface = sub->surface;
3813
3814 /*
3815 * If this commit would cause the surface to move by the
3816 * attach(dx, dy) parameters, the old damage region must be
3817 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05003818 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003819 */
Derek Foreman152254b2015-11-26 14:17:48 -06003820 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003821 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06003822 pixman_region32_union(&sub->cached.damage_surface,
3823 &sub->cached.damage_surface,
3824 &surface->pending.damage_surface);
3825 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003826
3827 if (surface->pending.newly_attached) {
3828 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003829 weston_surface_state_set_buffer(&sub->cached,
3830 surface->pending.buffer);
3831 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003832 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003833 weston_presentation_feedback_discard_list(
3834 &sub->cached.feedback_list);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003835 /* zwp_surface_synchronization_v1.set_acquire_fence */
3836 fd_move(&sub->cached.acquire_fence_fd,
3837 &surface->pending.acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003838 /* zwp_surface_synchronization_v1.get_release */
3839 weston_buffer_release_move(&sub->cached.buffer_release_ref,
3840 &surface->pending.buffer_release_ref);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003841 }
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303842 sub->cached.desired_protection = surface->pending.desired_protection;
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303843 sub->cached.protection_mode = surface->pending.protection_mode;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003844 assert(surface->pending.acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003845 assert(surface->pending.buffer_release_ref.buffer_release == NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003846 sub->cached.sx += surface->pending.sx;
3847 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003848
Derek Foreman152254b2015-11-26 14:17:48 -06003849 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
3850
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003851 sub->cached.buffer_viewport.changed |=
3852 surface->pending.buffer_viewport.changed;
3853 sub->cached.buffer_viewport.buffer =
3854 surface->pending.buffer_viewport.buffer;
3855 sub->cached.buffer_viewport.surface =
3856 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003857
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003858 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003859
3860 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3861
3862 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3863
3864 wl_list_insert_list(&sub->cached.frame_callback_list,
3865 &surface->pending.frame_callback_list);
3866 wl_list_init(&surface->pending.frame_callback_list);
3867
Pekka Paalanen133e4392014-09-23 22:08:46 -04003868 wl_list_insert_list(&sub->cached.feedback_list,
3869 &surface->pending.feedback_list);
3870 wl_list_init(&surface->pending.feedback_list);
3871
Jason Ekstrand7b982072014-05-20 14:33:03 -05003872 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003873}
3874
Derek Foreman280e7dd2014-10-03 13:13:42 -05003875static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003876weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3877{
3878 while (sub) {
3879 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003880 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003881
3882 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003883 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003884
3885 sub = weston_surface_to_subsurface(sub->parent);
3886 }
3887
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003888 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003889}
3890
3891static void
3892weston_subsurface_commit(struct weston_subsurface *sub)
3893{
3894 struct weston_surface *surface = sub->surface;
3895 struct weston_subsurface *tmp;
3896
3897 /* Recursive check for effectively synchronized. */
3898 if (weston_subsurface_is_synchronized(sub)) {
3899 weston_subsurface_commit_to_cache(sub);
3900 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003901 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003902 /* flush accumulated state from cache */
3903 weston_subsurface_commit_to_cache(sub);
3904 weston_subsurface_commit_from_cache(sub);
3905 } else {
3906 weston_surface_commit(surface);
3907 }
3908
3909 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3910 if (tmp->surface != surface)
3911 weston_subsurface_parent_commit(tmp, 0);
3912 }
3913 }
3914}
3915
3916static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003917weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003918{
3919 struct weston_surface *surface = sub->surface;
3920 struct weston_subsurface *tmp;
3921
Pekka Paalanene67858b2013-04-25 13:57:42 +03003922 /* From now on, commit_from_cache the whole sub-tree, regardless of
3923 * the synchronized mode of each child. This sub-surface or some
3924 * of its ancestors were synchronized, so we are synchronized
3925 * all the way down.
3926 */
3927
Jason Ekstrand7b982072014-05-20 14:33:03 -05003928 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003929 weston_subsurface_commit_from_cache(sub);
3930
3931 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3932 if (tmp->surface != surface)
3933 weston_subsurface_parent_commit(tmp, 1);
3934 }
3935}
3936
3937static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003938weston_subsurface_parent_commit(struct weston_subsurface *sub,
3939 int parent_is_synchronized)
3940{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003941 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003942 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003943 wl_list_for_each(view, &sub->surface->views, surface_link)
3944 weston_view_set_position(view,
3945 sub->position.x,
3946 sub->position.y);
3947
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003948 sub->position.set = 0;
3949 }
3950
3951 if (parent_is_synchronized || sub->synchronized)
3952 weston_subsurface_synchronized_commit(sub);
3953}
3954
Pekka Paalanen8274d902014-08-06 19:36:51 +03003955static int
3956subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3957{
3958 return snprintf(buf, len, "sub-surface");
3959}
3960
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003961static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003962subsurface_committed(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003963{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003964 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003965
Jason Ekstranda7af7042013-10-12 22:38:11 -05003966 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003967 weston_view_set_position(view,
3968 view->geometry.x + dx,
3969 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003970
3971 /* No need to check parent mappedness, because if parent is not
3972 * mapped, parent is not in a visible layer, so this sub-surface
3973 * will not be drawn either.
3974 */
Armin Krezovićf8486c32016-06-30 06:04:28 +02003975
Pekka Paalanene67858b2013-04-25 13:57:42 +03003976 if (!weston_surface_is_mapped(surface)) {
Armin Krezovićf8486c32016-06-30 06:04:28 +02003977 surface->is_mapped = true;
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003978
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003979 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003980 * because that would call it also for the parent surface,
3981 * which might not be mapped yet. That would lead to
3982 * inconsistent state, where the window could never be
3983 * mapped.
3984 *
Armin Krezovićf8486c32016-06-30 06:04:28 +02003985 * Instead just force the is_mapped flag on, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003986 * weston_surface_is_mapped() return true, so that when the
3987 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003988 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003989 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03003990 }
3991}
3992
3993static struct weston_subsurface *
3994weston_surface_to_subsurface(struct weston_surface *surface)
3995{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003996 if (surface->committed == subsurface_committed)
3997 return surface->committed_private;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003998
3999 return NULL;
4000}
4001
Pekka Paalanen01388e22013-04-25 13:57:44 +03004002WL_EXPORT struct weston_surface *
4003weston_surface_get_main_surface(struct weston_surface *surface)
4004{
4005 struct weston_subsurface *sub;
4006
4007 while (surface && (sub = weston_surface_to_subsurface(surface)))
4008 surface = sub->parent;
4009
4010 return surface;
4011}
4012
Pekka Paalanen50b67472014-10-01 15:02:41 +03004013WL_EXPORT int
4014weston_surface_set_role(struct weston_surface *surface,
4015 const char *role_name,
4016 struct wl_resource *error_resource,
4017 uint32_t error_code)
4018{
4019 assert(role_name);
4020
4021 if (surface->role_name == NULL ||
4022 surface->role_name == role_name ||
4023 strcmp(surface->role_name, role_name) == 0) {
4024 surface->role_name = role_name;
4025
4026 return 0;
4027 }
4028
4029 wl_resource_post_error(error_resource, error_code,
4030 "Cannot assign role %s to wl_surface@%d,"
4031 " already has role %s\n",
4032 role_name,
4033 wl_resource_get_id(surface->resource),
4034 surface->role_name);
4035 return -1;
4036}
4037
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02004038WL_EXPORT const char *
4039weston_surface_get_role(struct weston_surface *surface)
4040{
4041 return surface->role_name;
4042}
4043
Pekka Paalanen8274d902014-08-06 19:36:51 +03004044WL_EXPORT void
4045weston_surface_set_label_func(struct weston_surface *surface,
4046 int (*desc)(struct weston_surface *,
4047 char *, size_t))
4048{
4049 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02004050 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004051}
4052
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004053/** Get the size of surface contents
4054 *
4055 * \param surface The surface to query.
4056 * \param width Returns the width of raw contents.
4057 * \param height Returns the height of raw contents.
4058 *
4059 * Retrieves the raw surface content size in pixels for the given surface.
4060 * This is the whole content size in buffer pixels. If the surface
4061 * has no content or the renderer does not implement this feature,
4062 * zeroes are returned.
4063 *
4064 * This function is used to determine the buffer size needed for
4065 * a weston_surface_copy_content() call.
4066 */
4067WL_EXPORT void
4068weston_surface_get_content_size(struct weston_surface *surface,
4069 int *width, int *height)
4070{
4071 struct weston_renderer *rer = surface->compositor->renderer;
4072
4073 if (!rer->surface_get_content_size) {
4074 *width = 0;
4075 *height = 0;
4076 return;
4077 }
4078
4079 rer->surface_get_content_size(surface, width, height);
4080}
4081
Quentin Glidic248dd102016-08-12 10:41:34 +02004082/** Get the bounding box of a surface and its subsurfaces
4083 *
4084 * \param surface The surface to query.
4085 * \return The bounding box relative to the surface origin.
4086 *
4087 */
4088WL_EXPORT struct weston_geometry
4089weston_surface_get_bounding_box(struct weston_surface *surface)
4090{
4091 pixman_region32_t region;
4092 pixman_box32_t *box;
4093 struct weston_subsurface *subsurface;
4094
4095 pixman_region32_init_rect(&region,
4096 0, 0,
4097 surface->width, surface->height);
4098
4099 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link)
4100 pixman_region32_union_rect(&region, &region,
4101 subsurface->position.x,
4102 subsurface->position.y,
4103 subsurface->surface->width,
4104 subsurface->surface->height);
4105
4106 box = pixman_region32_extents(&region);
4107 struct weston_geometry geometry = {
4108 .x = box->x1,
4109 .y = box->y1,
4110 .width = box->x2 - box->x1,
4111 .height = box->y2 - box->y1,
4112 };
4113
4114 pixman_region32_fini(&region);
4115
4116 return geometry;
4117}
4118
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004119/** Copy surface contents to system memory.
4120 *
4121 * \param surface The surface to copy from.
4122 * \param target Pointer to the target memory buffer.
4123 * \param size Size of the target buffer in bytes.
4124 * \param src_x X location on contents to copy from.
4125 * \param src_y Y location on contents to copy from.
4126 * \param width Width in pixels of the area to copy.
4127 * \param height Height in pixels of the area to copy.
4128 * \return 0 for success, -1 for failure.
4129 *
4130 * Surface contents are maintained by the renderer. They can be in a
4131 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
4132 * else.
4133 *
4134 * Surface contents are copied into memory pointed to by target,
4135 * which has size bytes of space available. The target memory
4136 * may be larger than needed, but being smaller returns an error.
4137 * The extra bytes in target may or may not be written; their content is
4138 * unspecified. Size must be large enough to hold the image.
4139 *
4140 * The image in the target memory will be arranged in rows from
4141 * top to bottom, and pixels on a row from left to right. The pixel
4142 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
4143 * width * 4.
4144 *
4145 * Parameters src_x and src_y define the upper-left corner in buffer
4146 * coordinates (pixels) to copy from. Parameters width and height
4147 * define the size of the area to copy in pixels.
4148 *
4149 * The rectangle defined by src_x, src_y, width, height must fit in
4150 * the surface contents. Otherwise an error is returned.
4151 *
Changwoo Chof97d2502017-08-05 00:30:47 +09004152 * Use weston_surface_get_content_size to determine the content size; the
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004153 * needed target buffer size and rectangle limits.
4154 *
4155 * CURRENT IMPLEMENTATION RESTRICTIONS:
4156 * - the machine must be little-endian due to Pixman formats.
4157 *
4158 * NOTE: Pixman formats are premultiplied.
4159 */
4160WL_EXPORT int
4161weston_surface_copy_content(struct weston_surface *surface,
4162 void *target, size_t size,
4163 int src_x, int src_y,
4164 int width, int height)
4165{
4166 struct weston_renderer *rer = surface->compositor->renderer;
4167 int cw, ch;
4168 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
4169
4170 if (!rer->surface_copy_content)
4171 return -1;
4172
4173 weston_surface_get_content_size(surface, &cw, &ch);
4174
4175 if (src_x < 0 || src_y < 0)
4176 return -1;
4177
4178 if (width <= 0 || height <= 0)
4179 return -1;
4180
4181 if (src_x + width > cw || src_y + height > ch)
4182 return -1;
4183
4184 if (width * bytespp * height > size)
4185 return -1;
4186
4187 return rer->surface_copy_content(surface, target, size,
4188 src_x, src_y, width, height);
4189}
4190
Pekka Paalanene67858b2013-04-25 13:57:42 +03004191static void
4192subsurface_set_position(struct wl_client *client,
4193 struct wl_resource *resource, int32_t x, int32_t y)
4194{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004195 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004196
4197 if (!sub)
4198 return;
4199
4200 sub->position.x = x;
4201 sub->position.y = y;
4202 sub->position.set = 1;
4203}
4204
4205static struct weston_subsurface *
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004206subsurface_find_sibling(struct weston_subsurface *sub,
4207 struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004208{
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004209 struct weston_surface *parent = sub->parent;
4210 struct weston_subsurface *sibling;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004211
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004212 wl_list_for_each(sibling, &parent->subsurface_list, parent_link) {
4213 if (sibling->surface == surface && sibling != sub)
4214 return sibling;
4215 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004216
4217 return NULL;
4218}
4219
4220static struct weston_subsurface *
4221subsurface_sibling_check(struct weston_subsurface *sub,
4222 struct weston_surface *surface,
4223 const char *request)
4224{
4225 struct weston_subsurface *sibling;
4226
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004227 sibling = subsurface_find_sibling(sub, surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004228 if (!sibling) {
4229 wl_resource_post_error(sub->resource,
4230 WL_SUBSURFACE_ERROR_BAD_SURFACE,
4231 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004232 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004233 return NULL;
4234 }
4235
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004236 assert(sibling->parent == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004237
4238 return sibling;
4239}
4240
4241static void
4242subsurface_place_above(struct wl_client *client,
4243 struct wl_resource *resource,
4244 struct wl_resource *sibling_resource)
4245{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004246 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004247 struct weston_surface *surface =
4248 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004249 struct weston_subsurface *sibling;
4250
4251 if (!sub)
4252 return;
4253
4254 sibling = subsurface_sibling_check(sub, surface, "place_above");
4255 if (!sibling)
4256 return;
4257
4258 wl_list_remove(&sub->parent_link_pending);
4259 wl_list_insert(sibling->parent_link_pending.prev,
4260 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004261
4262 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004263}
4264
4265static void
4266subsurface_place_below(struct wl_client *client,
4267 struct wl_resource *resource,
4268 struct wl_resource *sibling_resource)
4269{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004270 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004271 struct weston_surface *surface =
4272 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004273 struct weston_subsurface *sibling;
4274
4275 if (!sub)
4276 return;
4277
4278 sibling = subsurface_sibling_check(sub, surface, "place_below");
4279 if (!sibling)
4280 return;
4281
4282 wl_list_remove(&sub->parent_link_pending);
4283 wl_list_insert(&sibling->parent_link_pending,
4284 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004285
4286 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004287}
4288
4289static void
4290subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
4291{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004292 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004293
4294 if (sub)
4295 sub->synchronized = 1;
4296}
4297
4298static void
4299subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
4300{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004301 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004302
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004303 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03004304 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004305
4306 /* If sub became effectively desynchronized, flush. */
4307 if (!weston_subsurface_is_synchronized(sub))
4308 weston_subsurface_synchronized_commit(sub);
4309 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004310}
4311
4312static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03004313weston_subsurface_unlink_parent(struct weston_subsurface *sub)
4314{
4315 wl_list_remove(&sub->parent_link);
4316 wl_list_remove(&sub->parent_link_pending);
4317 wl_list_remove(&sub->parent_destroy_listener.link);
4318 sub->parent = NULL;
4319}
4320
4321static void
4322weston_subsurface_destroy(struct weston_subsurface *sub);
4323
4324static void
4325subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
4326{
4327 struct weston_subsurface *sub =
4328 container_of(listener, struct weston_subsurface,
4329 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004330 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004331
4332 /* The protocol object (wl_resource) is left inert. */
4333 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004334 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004335
4336 weston_subsurface_destroy(sub);
4337}
4338
4339static void
4340subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
4341{
4342 struct weston_subsurface *sub =
4343 container_of(listener, struct weston_subsurface,
4344 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004345 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004346 assert(sub->surface != sub->parent);
4347
4348 if (weston_surface_is_mapped(sub->surface))
4349 weston_surface_unmap(sub->surface);
4350
4351 weston_subsurface_unlink_parent(sub);
4352}
4353
4354static void
4355subsurface_resource_destroy(struct wl_resource *resource)
4356{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004357 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004358
4359 if (sub)
4360 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004361}
4362
4363static void
4364subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
4365{
4366 wl_resource_destroy(resource);
4367}
4368
4369static void
4370weston_subsurface_link_parent(struct weston_subsurface *sub,
4371 struct weston_surface *parent)
4372{
4373 sub->parent = parent;
4374 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004375 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004376 &sub->parent_destroy_listener);
4377
4378 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4379 wl_list_insert(&parent->subsurface_list_pending,
4380 &sub->parent_link_pending);
4381}
4382
4383static void
4384weston_subsurface_link_surface(struct weston_subsurface *sub,
4385 struct weston_surface *surface)
4386{
4387 sub->surface = surface;
4388 sub->surface_destroy_listener.notify =
4389 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004390 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004391 &sub->surface_destroy_listener);
4392}
4393
4394static void
4395weston_subsurface_destroy(struct weston_subsurface *sub)
4396{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004397 struct weston_view *view, *next;
4398
Pekka Paalanene67858b2013-04-25 13:57:42 +03004399 assert(sub->surface);
4400
4401 if (sub->resource) {
4402 assert(weston_surface_to_subsurface(sub->surface) == sub);
4403 assert(sub->parent_destroy_listener.notify ==
4404 subsurface_handle_parent_destroy);
4405
George Kiagiadakised04d382014-06-13 18:10:26 +02004406 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
4407 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004408 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02004409 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05004410
Pekka Paalanene67858b2013-04-25 13:57:42 +03004411 if (sub->parent)
4412 weston_subsurface_unlink_parent(sub);
4413
Jason Ekstrand7b982072014-05-20 14:33:03 -05004414 weston_surface_state_fini(&sub->cached);
4415 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004416
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004417 sub->surface->committed = NULL;
4418 sub->surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004419 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004420 } else {
4421 /* the dummy weston_subsurface for the parent itself */
4422 assert(sub->parent_destroy_listener.notify == NULL);
4423 wl_list_remove(&sub->parent_link);
4424 wl_list_remove(&sub->parent_link_pending);
4425 }
4426
4427 wl_list_remove(&sub->surface_destroy_listener.link);
4428 free(sub);
4429}
4430
4431static const struct wl_subsurface_interface subsurface_implementation = {
4432 subsurface_destroy,
4433 subsurface_set_position,
4434 subsurface_place_above,
4435 subsurface_place_below,
4436 subsurface_set_sync,
4437 subsurface_set_desync
4438};
4439
4440static struct weston_subsurface *
4441weston_subsurface_create(uint32_t id, struct weston_surface *surface,
4442 struct weston_surface *parent)
4443{
4444 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004445 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004446
Bryce Harringtonde16d892014-11-20 22:21:57 -08004447 sub = zalloc(sizeof *sub);
4448 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004449 return NULL;
4450
Jason Ekstranda7af7042013-10-12 22:38:11 -05004451 wl_list_init(&sub->unused_views);
4452
Jason Ekstranda85118c2013-06-27 20:17:02 -05004453 sub->resource =
4454 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004455 if (!sub->resource) {
4456 free(sub);
4457 return NULL;
4458 }
4459
Jason Ekstranda85118c2013-06-27 20:17:02 -05004460 wl_resource_set_implementation(sub->resource,
4461 &subsurface_implementation,
4462 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004463 weston_subsurface_link_surface(sub, surface);
4464 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05004465 weston_surface_state_init(&sub->cached);
4466 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004467 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004468
4469 return sub;
4470}
4471
4472/* Create a dummy subsurface for having the parent itself in its
4473 * sub-surface lists. Makes stacking order manipulation easy.
4474 */
4475static struct weston_subsurface *
4476weston_subsurface_create_for_parent(struct weston_surface *parent)
4477{
4478 struct weston_subsurface *sub;
4479
Bryce Harringtonde16d892014-11-20 22:21:57 -08004480 sub = zalloc(sizeof *sub);
4481 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004482 return NULL;
4483
4484 weston_subsurface_link_surface(sub, parent);
4485 sub->parent = parent;
4486 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4487 wl_list_insert(&parent->subsurface_list_pending,
4488 &sub->parent_link_pending);
4489
4490 return sub;
4491}
4492
4493static void
4494subcompositor_get_subsurface(struct wl_client *client,
4495 struct wl_resource *resource,
4496 uint32_t id,
4497 struct wl_resource *surface_resource,
4498 struct wl_resource *parent_resource)
4499{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004500 struct weston_surface *surface =
4501 wl_resource_get_user_data(surface_resource);
4502 struct weston_surface *parent =
4503 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004504 struct weston_subsurface *sub;
4505 static const char where[] = "get_subsurface: wl_subsurface@";
4506
4507 if (surface == parent) {
4508 wl_resource_post_error(resource,
4509 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4510 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004511 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004512 return;
4513 }
4514
4515 if (weston_surface_to_subsurface(surface)) {
4516 wl_resource_post_error(resource,
4517 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4518 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004519 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004520 return;
4521 }
4522
Pekka Paalanen50b67472014-10-01 15:02:41 +03004523 if (weston_surface_set_role(surface, "wl_subsurface", resource,
4524 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004525 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004526
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004527 if (weston_surface_get_main_surface(parent) == surface) {
4528 wl_resource_post_error(resource,
4529 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4530 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004531 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004532 return;
4533 }
4534
Pekka Paalanene67858b2013-04-25 13:57:42 +03004535 /* make sure the parent is in its own list */
4536 if (wl_list_empty(&parent->subsurface_list)) {
4537 if (!weston_subsurface_create_for_parent(parent)) {
4538 wl_resource_post_no_memory(resource);
4539 return;
4540 }
4541 }
4542
4543 sub = weston_subsurface_create(id, surface, parent);
4544 if (!sub) {
4545 wl_resource_post_no_memory(resource);
4546 return;
4547 }
4548
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004549 surface->committed = subsurface_committed;
4550 surface->committed_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004551 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004552}
4553
4554static void
4555subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
4556{
4557 wl_resource_destroy(resource);
4558}
4559
4560static const struct wl_subcompositor_interface subcompositor_interface = {
4561 subcompositor_destroy,
4562 subcompositor_get_subsurface
4563};
4564
4565static void
4566bind_subcompositor(struct wl_client *client,
4567 void *data, uint32_t version, uint32_t id)
4568{
4569 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004570 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004571
Jason Ekstranda85118c2013-06-27 20:17:02 -05004572 resource =
4573 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004574 if (resource == NULL) {
4575 wl_client_post_no_memory(client);
4576 return;
4577 }
4578 wl_resource_set_implementation(resource, &subcompositor_interface,
4579 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004580}
4581
Bryce Harrington0795ece2016-08-30 12:04:26 -07004582/** Set a DPMS mode on all of the compositor's outputs
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004583 *
4584 * \param compositor The compositor instance
4585 * \param state The DPMS state the outputs will be set to
4586 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004587static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004588weston_compositor_dpms(struct weston_compositor *compositor,
4589 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004590{
4591 struct weston_output *output;
4592
Bryce Harrington08976ac2016-08-30 12:05:16 -07004593 wl_list_for_each(output, &compositor->output_list, link)
4594 if (output->set_dpms)
4595 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004596}
4597
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004598/** Restores the compositor to active status
4599 *
4600 * \param compositor The compositor instance
4601 *
4602 * If the compositor was in a sleeping mode, all outputs are powered
4603 * back on via DPMS. Otherwise if the compositor was inactive
4604 * (idle/locked, offscreen, or sleeping) then the compositor's wake
4605 * signal will fire.
4606 *
4607 * Restarts the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004608 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004609 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004610WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004611weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004612{
Neil Roberts8b62e202013-09-30 13:14:47 +01004613 uint32_t old_state = compositor->state;
4614
4615 /* The state needs to be changed before emitting the wake
4616 * signal because that may try to schedule a repaint which
4617 * will not work if the compositor is still sleeping */
4618 compositor->state = WESTON_COMPOSITOR_ACTIVE;
4619
4620 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004621 case WESTON_COMPOSITOR_SLEEPING:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004622 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004623 case WESTON_COMPOSITOR_OFFSCREEN:
Daniel Stone893b9362016-11-08 15:47:09 +00004624 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004625 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004626 /* fall through */
4627 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004628 wl_event_source_timer_update(compositor->idle_source,
4629 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004630 }
4631}
4632
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004633/** Turns off rendering and frame events for the compositor.
4634 *
4635 * \param compositor The compositor instance
4636 *
4637 * This is used for example to prevent further rendering while the
4638 * compositor is shutting down.
4639 *
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004640 * Stops the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004641 *
4642 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004643 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004644WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004645weston_compositor_offscreen(struct weston_compositor *compositor)
4646{
4647 switch (compositor->state) {
4648 case WESTON_COMPOSITOR_OFFSCREEN:
4649 return;
4650 case WESTON_COMPOSITOR_SLEEPING:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004651 default:
4652 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4653 wl_event_source_timer_update(compositor->idle_source, 0);
4654 }
4655}
4656
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004657/** Powers down all attached output devices
4658 *
4659 * \param compositor The compositor instance
4660 *
4661 * Causes rendering to the outputs to cease, and no frame events to be
4662 * sent. Only powers down the outputs if the compositor is not already
4663 * in sleep mode.
4664 *
4665 * Stops the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004666 *
4667 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004668 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004669WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004670weston_compositor_sleep(struct weston_compositor *compositor)
4671{
4672 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
4673 return;
4674
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004675 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004676 compositor->state = WESTON_COMPOSITOR_SLEEPING;
4677 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
4678}
4679
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004680/** Sets compositor to idle mode
4681 *
4682 * \param data The compositor instance
4683 *
4684 * This is called when the idle timer fires. Once the compositor is in
4685 * idle mode it requires a wake action (e.g. via
4686 * weston_compositor_wake()) to restore it. The compositor's
4687 * idle_signal will be triggered when the idle event occurs.
4688 *
4689 * Idleness can be inhibited by setting the compositor's idle_inhibit
4690 * property.
4691 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004692static int
4693idle_handler(void *data)
4694{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004695 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004696
4697 if (compositor->idle_inhibit)
4698 return 1;
4699
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004700 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004701 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004702
4703 return 1;
4704}
4705
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004706WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08004707weston_plane_init(struct weston_plane *plane,
4708 struct weston_compositor *ec,
4709 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004710{
4711 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004712 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004713 plane->x = x;
4714 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08004715 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004716
4717 /* Init the link so that the call to wl_list_remove() when releasing
4718 * the plane without ever stacking doesn't lead to a crash */
4719 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004720}
4721
4722WL_EXPORT void
4723weston_plane_release(struct weston_plane *plane)
4724{
Xiong Zhang97116532013-10-23 13:58:31 +08004725 struct weston_view *view;
4726
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004727 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004728 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004729
Xiong Zhang97116532013-10-23 13:58:31 +08004730 wl_list_for_each(view, &plane->compositor->view_list, link) {
4731 if (view->plane == plane)
4732 view->plane = NULL;
4733 }
4734
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004735 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004736}
4737
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004738/** weston_compositor_stack_plane
4739 * \ingroup compositor
4740 */
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004741WL_EXPORT void
4742weston_compositor_stack_plane(struct weston_compositor *ec,
4743 struct weston_plane *plane,
4744 struct weston_plane *above)
4745{
4746 if (above)
4747 wl_list_insert(above->link.prev, &plane->link);
4748 else
4749 wl_list_insert(&ec->plane_list, &plane->link);
4750}
4751
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004752static void
4753output_release(struct wl_client *client, struct wl_resource *resource)
4754{
4755 wl_resource_destroy(resource);
4756}
4757
4758static const struct wl_output_interface output_interface = {
4759 output_release,
4760};
4761
4762
Casey Dahlin9074db52012-04-19 22:50:09 -04004763static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004764{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004765 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004766}
4767
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004768static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004769bind_output(struct wl_client *client,
4770 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05004771{
Pekka Paalanen05347622017-03-27 12:24:34 +03004772 struct weston_head *head = data;
4773 struct weston_output *output = head->output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004774 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004775 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004776
Jason Ekstranda85118c2013-06-27 20:17:02 -05004777 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004778 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004779 if (resource == NULL) {
4780 wl_client_post_no_memory(client);
4781 return;
4782 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004783
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03004784 wl_list_insert(&head->resource_list, wl_resource_get_link(resource));
Pekka Paalanen055c1132017-03-27 16:31:25 +03004785 wl_resource_set_implementation(resource, &output_interface, head,
Pekka Paalanen05347622017-03-27 12:24:34 +03004786 unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04004787
Pekka Paalanen05347622017-03-27 12:24:34 +03004788 assert(output);
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004789 wl_output_send_geometry(resource,
4790 output->x,
4791 output->y,
Pekka Paalanen01f60212017-03-24 15:39:24 +02004792 head->mm_width,
4793 head->mm_height,
4794 head->subpixel,
4795 head->make, head->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04004796 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004797 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004798 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02004799 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004800
4801 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004802 wl_output_send_mode(resource,
4803 mode->flags,
4804 mode->width,
4805 mode->height,
4806 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004807 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02004808
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004809 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004810 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004811}
4812
Pekka Paalanendcac3512017-12-08 14:13:34 +02004813static void
4814weston_head_add_global(struct weston_head *head)
4815{
4816 head->global = wl_global_create(head->compositor->wl_display,
4817 &wl_output_interface, 3,
4818 head, bind_output);
4819}
4820
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02004821/** Remove the global wl_output protocol object
4822 *
4823 * \param head The head whose global to remove.
4824 *
4825 * Also orphans the wl_resources for this head (wl_output).
4826 */
4827static void
4828weston_head_remove_global(struct weston_head *head)
4829{
4830 struct wl_resource *resource, *tmp;
4831
4832 if (head->global)
4833 wl_global_destroy(head->global);
4834 head->global = NULL;
4835
4836 wl_resource_for_each_safe(resource, tmp, &head->resource_list) {
4837 unbind_resource(resource);
4838 wl_resource_set_destructor(resource, NULL);
4839 wl_resource_set_user_data(resource, NULL);
4840 }
Roman Gilge97391c2019-03-29 13:01:06 +01004841
4842 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
4843 /* It's sufficient to unset the destructor, then the list elements
4844 * won't be accessed.
4845 */
4846 wl_resource_set_destructor(resource, NULL);
4847 }
4848 wl_list_init(&head->xdg_output_resource_list);
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02004849}
4850
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004851/** Get the backing object of wl_output
4852 *
4853 * \param resource A wl_output protocol object.
4854 * \return The backing object (user data) of a wl_resource representing a
4855 * wl_output protocol object.
Marius Vlad78984ee2019-06-11 00:05:08 +03004856 *
4857 * \ingroup head
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004858 */
Pekka Paalanen055c1132017-03-27 16:31:25 +03004859WL_EXPORT struct weston_head *
4860weston_head_from_resource(struct wl_resource *resource)
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004861{
4862 assert(wl_resource_instance_of(resource, &wl_output_interface,
4863 &output_interface));
4864
4865 return wl_resource_get_user_data(resource);
4866}
4867
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004868/** Initialize a pre-allocated weston_head
4869 *
4870 * \param head The head to initialize.
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004871 * \param name The head name, e.g. the connector name or equivalent.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004872 *
4873 * The head will be safe to attach, detach and release.
4874 *
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004875 * The name is used in logs, and can be used by compositors as a configuration
4876 * identifier.
4877 *
Marius Vlad78984ee2019-06-11 00:05:08 +03004878 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004879 * \internal
4880 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004881WL_EXPORT void
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004882weston_head_init(struct weston_head *head, const char *name)
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004883{
4884 /* Add some (in)sane defaults which can be used
4885 * for checking if an output was properly configured
4886 */
4887 memset(head, 0, sizeof *head);
4888
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004889 wl_list_init(&head->compositor_link);
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03004890 wl_signal_init(&head->destroy_signal);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004891 wl_list_init(&head->output_link);
4892 wl_list_init(&head->resource_list);
Roman Gilge97391c2019-03-29 13:01:06 +01004893 wl_list_init(&head->xdg_output_resource_list);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004894 head->name = strdup(name);
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05304895 head->current_protection = WESTON_HDCP_DISABLE;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004896}
4897
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004898/** Send output heads changed signal
4899 *
4900 * \param output The output that changed.
4901 *
4902 * Notify that the enabled output gained and/or lost heads, or that the
4903 * associated heads may have changed their connection status. This does not
4904 * include cases where the output becomes enabled or disabled. The registered
4905 * callbacks are called after the change has successfully happened.
4906 *
4907 * If connection status change causes the compositor to attach or detach a head
4908 * to an enabled output, the registered callbacks may be called multiple times.
Marius Vlad55d87362019-06-11 01:15:35 +03004909 *
4910 * \ingroup output
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004911 */
4912static void
4913weston_output_emit_heads_changed(struct weston_output *output)
4914{
4915 wl_signal_emit(&output->compositor->output_heads_changed_signal,
4916 output);
4917}
4918
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004919/** Idle task for emitting heads_changed_signal */
4920static void
4921weston_compositor_call_heads_changed(void *data)
4922{
4923 struct weston_compositor *compositor = data;
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004924 struct weston_head *head;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004925
4926 compositor->heads_changed_source = NULL;
4927
4928 wl_signal_emit(&compositor->heads_changed_signal, compositor);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004929
4930 wl_list_for_each(head, &compositor->head_list, compositor_link) {
4931 if (head->output && head->output->enabled)
4932 weston_output_emit_heads_changed(head->output);
4933 }
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004934}
4935
4936/** Schedule a call on idle to heads_changed callback
4937 *
4938 * \param compositor The Compositor.
4939 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004940 * \ingroup compositor
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004941 * \internal
4942 */
4943static void
4944weston_compositor_schedule_heads_changed(struct weston_compositor *compositor)
4945{
4946 struct wl_event_loop *loop;
4947
4948 if (compositor->heads_changed_source)
4949 return;
4950
4951 loop = wl_display_get_event_loop(compositor->wl_display);
4952 compositor->heads_changed_source = wl_event_loop_add_idle(loop,
4953 weston_compositor_call_heads_changed, compositor);
4954}
4955
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004956/** Register a new head
4957 *
4958 * \param compositor The compositor.
4959 * \param head The head to register, must not be already registered.
4960 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004961 * This signals the core that a new head has become available, leading to
4962 * heads_changed hook being called later.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004963 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004964 * \ingroup compositor
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004965 * \internal
4966 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004967WL_EXPORT void
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004968weston_compositor_add_head(struct weston_compositor *compositor,
4969 struct weston_head *head)
4970{
4971 assert(wl_list_empty(&head->compositor_link));
4972 assert(head->name);
4973
4974 wl_list_insert(compositor->head_list.prev, &head->compositor_link);
4975 head->compositor = compositor;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004976 weston_compositor_schedule_heads_changed(compositor);
4977}
4978
4979/** Adds a listener to be called when heads change
4980 *
4981 * \param compositor The compositor.
4982 * \param listener The listener to add.
4983 *
Marius Vlada2dace22019-06-12 16:05:44 +03004984 * The listener notify function argument is weston_compositor.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004985 *
4986 * The listener function will be called after heads are added or their
4987 * connection status has changed. Several changes may be accumulated into a
4988 * single call. The user is expected to iterate over the existing heads and
4989 * check their statuses to find out what changed.
4990 *
4991 * \sa weston_compositor_iterate_heads, weston_head_is_connected,
4992 * weston_head_is_enabled
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004993 * \ingroup compositor
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004994 */
4995WL_EXPORT void
4996weston_compositor_add_heads_changed_listener(struct weston_compositor *compositor,
4997 struct wl_listener *listener)
4998{
4999 wl_signal_add(&compositor->heads_changed_signal, listener);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005000}
5001
5002/** Iterate over available heads
5003 *
5004 * \param compositor The compositor.
Marius Vlada2dace22019-06-12 16:05:44 +03005005 * \param iter The iterator, or NULL for start.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005006 * \return The next available head in the list.
5007 *
5008 * Returns all available heads, regardless of being connected or enabled.
5009 *
5010 * You can iterate over all heads as follows:
5011 * \code
5012 * struct weston_head *head = NULL;
5013 *
5014 * while ((head = weston_compositor_iterate_heads(compositor, head))) {
5015 * ...
5016 * }
5017 * \endcode
5018 *
5019 * If you cause \c iter to be removed from the list, you cannot use it to
5020 * continue iterating. Removing any other item is safe.
5021 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005022 * \ingroup compositor
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005023 */
5024WL_EXPORT struct weston_head *
5025weston_compositor_iterate_heads(struct weston_compositor *compositor,
5026 struct weston_head *iter)
5027{
5028 struct wl_list *list = &compositor->head_list;
5029 struct wl_list *node;
5030
5031 assert(compositor);
5032 assert(!iter || iter->compositor == compositor);
5033
5034 if (iter)
5035 node = iter->compositor_link.next;
5036 else
5037 node = list->next;
5038
5039 assert(node);
5040 assert(!iter || node != &iter->compositor_link);
5041
5042 if (node == list)
5043 return NULL;
5044
5045 return container_of(node, struct weston_head, compositor_link);
5046}
5047
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005048/** Iterate over attached heads
5049 *
5050 * \param output The output whose heads to iterate.
Marius Vlada2dace22019-06-12 16:05:44 +03005051 * \param iter The iterator, or NULL for start.
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005052 * \return The next attached head in the list.
5053 *
5054 * Returns all heads currently attached to the output.
5055 *
5056 * You can iterate over all heads as follows:
5057 * \code
5058 * struct weston_head *head = NULL;
5059 *
5060 * while ((head = weston_output_iterate_heads(output, head))) {
5061 * ...
5062 * }
5063 * \endcode
5064 *
5065 * If you cause \c iter to be removed from the list, you cannot use it to
5066 * continue iterating. Removing any other item is safe.
5067 *
Marius Vlad55d87362019-06-11 01:15:35 +03005068 * \ingroup ouput
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005069 */
5070WL_EXPORT struct weston_head *
5071weston_output_iterate_heads(struct weston_output *output,
5072 struct weston_head *iter)
5073{
5074 struct wl_list *list = &output->head_list;
5075 struct wl_list *node;
5076
5077 assert(output);
5078 assert(!iter || iter->output == output);
5079
5080 if (iter)
5081 node = iter->output_link.next;
5082 else
5083 node = list->next;
5084
5085 assert(node);
5086 assert(!iter || node != &iter->output_link);
5087
5088 if (node == list)
5089 return NULL;
5090
5091 return container_of(node, struct weston_head, output_link);
5092}
5093
Pekka Paalanendcac3512017-12-08 14:13:34 +02005094/** Attach a head to an output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005095 *
5096 * \param output The output to attach to.
5097 * \param head The head that is not yet attached.
5098 * \return 0 on success, -1 on failure.
5099 *
5100 * Attaches the given head to the output. All heads of an output are clones
5101 * and share the resolution and timings.
5102 *
5103 * Cloning heads this way uses less resources than creating an output for
5104 * each head, but is not always possible due to environment, driver and hardware
5105 * limitations.
5106 *
5107 * On failure, the head remains unattached. Success of this function does not
5108 * guarantee the output configuration is actually valid. The final checks are
Pekka Paalanendcac3512017-12-08 14:13:34 +02005109 * made on weston_output_enable() unless the output was already enabled.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005110 *
Marius Vlad55d87362019-06-11 01:15:35 +03005111 * \ingroup output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005112 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005113WL_EXPORT int
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005114weston_output_attach_head(struct weston_output *output,
5115 struct weston_head *head)
5116{
Pekka Paalanendcac3512017-12-08 14:13:34 +02005117 char *head_names;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005118
5119 if (!wl_list_empty(&head->output_link))
5120 return -1;
5121
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005122 if (output->attach_head) {
5123 if (output->attach_head(output, head) < 0)
5124 return -1;
5125 } else if (!wl_list_empty(&output->head_list)) {
5126 /* No support for clones in the legacy path. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005127 return -1;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005128 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005129
5130 head->output = output;
5131 wl_list_insert(output->head_list.prev, &head->output_link);
5132
Pekka Paalanendcac3512017-12-08 14:13:34 +02005133 if (output->enabled) {
5134 weston_head_add_global(head);
5135
5136 head_names = weston_output_create_heads_string(output);
5137 weston_log("Output '%s' updated to have head(s) %s\n",
5138 output->name, head_names);
5139 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005140
5141 weston_output_emit_heads_changed(output);
Pekka Paalanendcac3512017-12-08 14:13:34 +02005142 }
5143
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005144 return 0;
5145}
5146
5147/** Detach a head from its output
5148 *
5149 * \param head The head to detach.
5150 *
5151 * It is safe to detach a non-attached head.
5152 *
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005153 * If the head is attached to an enabled output and the output will be left
5154 * with no heads, the output will be disabled.
5155 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005156 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005157 * \sa weston_output_disable
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005158 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005159WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005160weston_head_detach(struct weston_head *head)
5161{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005162 struct weston_output *output = head->output;
Pekka Paalanena0106992017-12-08 16:11:17 +02005163 char *head_names;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005164
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005165 wl_list_remove(&head->output_link);
5166 wl_list_init(&head->output_link);
5167 head->output = NULL;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005168
5169 if (!output)
5170 return;
5171
5172 if (output->detach_head)
5173 output->detach_head(output, head);
5174
5175 if (output->enabled) {
5176 weston_head_remove_global(head);
5177
Pekka Paalanena0106992017-12-08 16:11:17 +02005178 if (wl_list_empty(&output->head_list)) {
5179 weston_log("Output '%s' no heads left, disabling.\n",
5180 output->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005181 weston_output_disable(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02005182 } else {
5183 head_names = weston_output_create_heads_string(output);
5184 weston_log("Output '%s' updated to have head(s) %s\n",
5185 output->name, head_names);
5186 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005187
5188 weston_output_emit_heads_changed(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02005189 }
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005190 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005191}
5192
5193/** Destroy a head
5194 *
5195 * \param head The head to be released.
5196 *
5197 * Destroys the head. The caller is responsible for freeing the memory pointed
5198 * to by \c head.
5199 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005200 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005201 * \internal
5202 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005203WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005204weston_head_release(struct weston_head *head)
5205{
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005206 wl_signal_emit(&head->destroy_signal, head);
5207
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005208 weston_head_detach(head);
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005209
5210 free(head->make);
5211 free(head->model);
5212 free(head->serial_number);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005213 free(head->name);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005214
5215 wl_list_remove(&head->compositor_link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005216}
5217
Pekka Paalanene19970f2017-08-28 14:11:02 +03005218static void
5219weston_head_set_device_changed(struct weston_head *head)
5220{
5221 head->device_changed = true;
5222
5223 if (head->compositor)
5224 weston_compositor_schedule_heads_changed(head->compositor);
5225}
5226
5227/** String equal comparison with NULLs being equal */
5228static bool
5229str_null_eq(const char *a, const char *b)
5230{
5231 if (!a && !b)
5232 return true;
5233
5234 if (!!a != !!b)
5235 return false;
5236
5237 return strcmp(a, b) == 0;
5238}
5239
Pekka Paalanen01f60212017-03-24 15:39:24 +02005240/** Store monitor make, model and serial number
5241 *
5242 * \param head The head to modify.
5243 * \param make The monitor make. If EDID is available, the PNP ID. Otherwise
5244 * any string, or NULL for none.
5245 * \param model The monitor model or name, or a made-up string, or NULL for
5246 * none.
5247 * \param serialno The monitor serial number, a made-up string, or NULL for
5248 * none.
5249 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005250 * This may set the device_changed flag.
5251 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005252 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005253 * \internal
5254 */
5255WL_EXPORT void
5256weston_head_set_monitor_strings(struct weston_head *head,
5257 const char *make,
5258 const char *model,
5259 const char *serialno)
5260{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005261 if (str_null_eq(head->make, make) &&
5262 str_null_eq(head->model, model) &&
5263 str_null_eq(head->serial_number, serialno))
5264 return;
5265
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005266 free(head->make);
5267 free(head->model);
5268 free(head->serial_number);
5269
5270 head->make = make ? strdup(make) : NULL;
5271 head->model = model ? strdup(model) : NULL;
5272 head->serial_number = serialno ? strdup(serialno) : NULL;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005273
5274 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005275}
5276
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005277/** Store display non-desktop status
5278 *
5279 * \param head The head to modify.
5280 * \param non_desktop Whether the head connects to a non-desktop display.
5281 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005282 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005283 * \internal
5284 */
5285WL_EXPORT void
5286weston_head_set_non_desktop(struct weston_head *head, bool non_desktop)
5287{
5288 if (head->non_desktop == non_desktop)
5289 return;
5290
5291 head->non_desktop = non_desktop;
5292
5293 weston_head_set_device_changed(head);
5294}
5295
Pekka Paalanen01f60212017-03-24 15:39:24 +02005296/** Store physical image size
5297 *
5298 * \param head The head to modify.
5299 * \param mm_width Image area width in millimeters.
5300 * \param mm_height Image area height in millimeters.
5301 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005302 * This may set the device_changed flag.
5303 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005304 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005305 * \internal
5306 */
5307WL_EXPORT void
5308weston_head_set_physical_size(struct weston_head *head,
5309 int32_t mm_width, int32_t mm_height)
5310{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005311 if (head->mm_width == mm_width &&
5312 head->mm_height == mm_height)
5313 return;
5314
Pekka Paalanen01f60212017-03-24 15:39:24 +02005315 head->mm_width = mm_width;
5316 head->mm_height = mm_height;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005317
5318 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005319}
5320
5321/** Store monitor sub-pixel layout
5322 *
5323 * \param head The head to modify.
5324 * \param sp Sub-pixel layout. The possible values are:
5325 * - WL_OUTPUT_SUBPIXEL_UNKNOWN,
5326 * - WL_OUTPUT_SUBPIXEL_NONE,
5327 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB,
5328 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR,
5329 * - WL_OUTPUT_SUBPIXEL_VERTICAL_RGB,
5330 * - WL_OUTPUT_SUBPIXEL_VERTICAL_BGR
5331 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005332 * This may set the device_changed flag.
5333 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005334 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005335 * \internal
5336 */
5337WL_EXPORT void
5338weston_head_set_subpixel(struct weston_head *head,
5339 enum wl_output_subpixel sp)
5340{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005341 if (head->subpixel == sp)
5342 return;
5343
Pekka Paalanen01f60212017-03-24 15:39:24 +02005344 head->subpixel = sp;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005345
5346 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005347}
5348
5349/** Mark the monitor as internal
5350 *
5351 * This is used for embedded screens, like laptop panels.
5352 *
5353 * \param head The head to mark as internal.
5354 *
5355 * By default a head is external. The type is often inferred from the physical
5356 * connector type.
5357 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005358 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005359 * \internal
5360 */
5361WL_EXPORT void
5362weston_head_set_internal(struct weston_head *head)
5363{
5364 head->connection_internal = true;
5365}
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005366
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005367/** Store connector status
5368 *
5369 * \param head The head to modify.
5370 * \param connected Whether the head is connected.
5371 *
5372 * Connectors are created as disconnected. This function can be used to
5373 * set the connector status.
5374 *
5375 * The status should be set to true when a physical connector is connected to
5376 * a video sink device like a monitor and to false when the connector is
5377 * disconnected. For nested backends, the connection status should reflect the
5378 * connection to the parent display server.
5379 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005380 * When the connection status changes, it schedules a call to the heads_changed
Pekka Paalanene19970f2017-08-28 14:11:02 +03005381 * hook and sets the device_changed flag.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005382 *
5383 * \sa weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005384 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005385 * \internal
5386 */
5387WL_EXPORT void
5388weston_head_set_connection_status(struct weston_head *head, bool connected)
5389{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005390 if (head->connected == connected)
5391 return;
5392
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005393 head->connected = connected;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005394
Pekka Paalanene19970f2017-08-28 14:11:02 +03005395 weston_head_set_device_changed(head);
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005396}
5397
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305398static void
5399weston_output_compute_protection(struct weston_output *output)
5400{
5401 struct weston_head *head;
5402 enum weston_hdcp_protection op_protection;
5403 bool op_protection_valid = false;
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305404 struct weston_compositor *wc = output->compositor;
5405 struct content_protection *cp = wc->content_protection;
5406
5407 if (!cp)
5408 return;
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305409
5410 wl_list_for_each(head, &output->head_list, output_link) {
5411 if (!op_protection_valid) {
5412 op_protection = head->current_protection;
5413 op_protection_valid = true;
5414 }
5415 if (head->current_protection < op_protection)
5416 op_protection = head->current_protection;
5417 }
5418
5419 if (!op_protection_valid)
5420 op_protection = WESTON_HDCP_DISABLE;
5421
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305422 if (output->current_protection != op_protection) {
5423 struct wl_event_loop *loop;
5424
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305425 output->current_protection = op_protection;
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305426 weston_output_damage(output);
5427 if (cp->surface_protection_update)
5428 return;
5429 loop = wl_display_get_event_loop(wc->wl_display);
5430 cp->surface_protection_update = wl_event_loop_add_idle(loop,
5431 notify_surface_protection_change,
5432 wc);
5433 }
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305434}
5435
5436WL_EXPORT void
5437weston_head_set_content_protection_status(struct weston_head *head,
5438 enum weston_hdcp_protection status)
5439{
5440 head->current_protection = status;
5441 if (head->output)
5442 weston_output_compute_protection(head->output);
5443}
5444
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005445/** Is the head currently connected?
5446 *
5447 * \param head The head to query.
5448 * \return Connection status.
5449 *
5450 * Returns true if the head is physically connected to a monitor, or in
5451 * case of a nested backend returns true when there is a connection to the
5452 * parent display server.
5453 *
5454 * This is independent from the head being enabled.
5455 *
5456 * \sa weston_head_is_enabled
Marius Vlad78984ee2019-06-11 00:05:08 +03005457 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005458 */
5459WL_EXPORT bool
5460weston_head_is_connected(struct weston_head *head)
5461{
5462 return head->connected;
5463}
5464
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005465/** Is the head currently enabled?
5466 *
5467 * \param head The head to query.
5468 * \return Video status.
5469 *
5470 * Returns true if the head is currently transmitting a video stream.
5471 *
5472 * This is independent of the head being connected.
5473 *
5474 * \sa weston_head_is_connected
Marius Vlad78984ee2019-06-11 00:05:08 +03005475 * \ingroup head
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005476 */
5477WL_EXPORT bool
5478weston_head_is_enabled(struct weston_head *head)
5479{
5480 if (!head->output)
5481 return false;
5482
5483 return head->output->enabled;
5484}
5485
Pekka Paalanene19970f2017-08-28 14:11:02 +03005486/** Has the device information changed?
5487 *
5488 * \param head The head to query.
5489 * \return True if the device information has changed since last reset.
5490 *
5491 * The information about the connected display device, e.g. a monitor, may
5492 * change without being disconnected in between. Changing information
5493 * causes a call to the heads_changed hook.
5494 *
5495 * The information includes make, model, serial number, physical size,
5496 * and sub-pixel type. The connection status is also included.
5497 *
5498 * \sa weston_head_reset_device_changed, weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005499 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03005500 */
5501WL_EXPORT bool
5502weston_head_is_device_changed(struct weston_head *head)
5503{
5504 return head->device_changed;
5505}
5506
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005507/** Does the head represent a non-desktop display?
5508 *
5509 * \param head The head to query.
5510 * \return True if the device is a non-desktop display.
5511 *
5512 * Non-desktop heads are not attached to outputs by default.
5513 * This stops weston from extending the desktop onto head mounted displays.
5514 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005515 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005516 */
5517WL_EXPORT bool
5518weston_head_is_non_desktop(struct weston_head *head)
5519{
5520 return head->non_desktop;
5521}
5522
Pekka Paalanene19970f2017-08-28 14:11:02 +03005523/** Acknowledge device information change
5524 *
5525 * \param head The head to acknowledge.
5526 *
5527 * Clears the device changed flag on this head. When a compositor has processed
5528 * device information, it should call this to be able to notice further
5529 * changes.
5530 *
5531 * \sa weston_head_is_device_changed
Marius Vlad78984ee2019-06-11 00:05:08 +03005532 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03005533 */
5534WL_EXPORT void
5535weston_head_reset_device_changed(struct weston_head *head)
5536{
5537 head->device_changed = false;
5538}
5539
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005540/** Get the name of a head
5541 *
5542 * \param head The head to query.
5543 * \return The head's name, not NULL.
5544 *
5545 * The name depends on the backend. The DRM backend uses connector names,
5546 * other backends may use hardcoded names or user-given names.
Marius Vlad78984ee2019-06-11 00:05:08 +03005547 *
5548 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005549 */
5550WL_EXPORT const char *
5551weston_head_get_name(struct weston_head *head)
5552{
5553 return head->name;
5554}
5555
5556/** Get the output the head is attached to
5557 *
5558 * \param head The head to query.
5559 * \return The output the head is attached to, or NULL if detached.
Marius Vlad78984ee2019-06-11 00:05:08 +03005560 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005561 */
5562WL_EXPORT struct weston_output *
5563weston_head_get_output(struct weston_head *head)
5564{
5565 return head->output;
5566}
5567
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005568/** Add destroy callback for a head
5569 *
5570 * \param head The head to watch for.
5571 * \param listener The listener to add. The \c notify member must be set.
5572 *
5573 * Heads may get destroyed for various reasons by the backends. If a head is
5574 * attached to an output, the compositor should listen for head destruction
5575 * and reconfigure or destroy the output if necessary.
5576 *
5577 * The destroy callbacks will be called on weston_head destruction before any
5578 * automatic detaching from an associated weston_output and before any
5579 * weston_head information is lost.
5580 *
5581 * The \c data argument to the notify callback is the weston_head being
5582 * destroyed.
Marius Vlad78984ee2019-06-11 00:05:08 +03005583 *
5584 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005585 */
5586WL_EXPORT void
5587weston_head_add_destroy_listener(struct weston_head *head,
5588 struct wl_listener *listener)
5589{
5590 wl_signal_add(&head->destroy_signal, listener);
5591}
5592
5593/** Look up destroy listener for a head
5594 *
5595 * \param head The head to query.
5596 * \param notify The notify function used used for the added destroy listener.
5597 * \return The listener, or NULL if not found.
5598 *
5599 * This looks up the previously added destroy listener struct based on the
5600 * notify function it has. The listener can be used to access user data
5601 * through \c container_of().
5602 *
5603 * \sa wl_signal_get()
Marius Vlad78984ee2019-06-11 00:05:08 +03005604 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005605 */
5606WL_EXPORT struct wl_listener *
5607weston_head_get_destroy_listener(struct weston_head *head,
5608 wl_notify_func_t notify)
5609{
5610 return wl_signal_get(&head->destroy_signal, notify);
5611}
5612
David Fort0de859e2016-05-27 23:22:57 +02005613/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005614static void
David Fort0de859e2016-05-27 23:22:57 +02005615weston_compositor_reflow_outputs(struct weston_compositor *compositor,
5616 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005617{
5618 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02005619 bool start_resizing = false;
5620
5621 if (!delta_width)
5622 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005623
5624 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02005625 if (output == resized_output) {
5626 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005627 continue;
5628 }
5629
David Fort0de859e2016-05-27 23:22:57 +02005630 if (start_resizing) {
5631 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005632 output->dirty = 1;
5633 }
5634 }
5635}
5636
Pekka Paalanend72bad22017-03-29 17:01:41 +03005637static void
Scott Moreauccbf29d2012-02-22 14:21:41 -07005638weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005639{
Scott Moreau850ca422012-05-21 15:21:25 -06005640 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05005641
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005642 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005643 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06005644
Scott Moreauccbf29d2012-02-22 14:21:41 -07005645 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06005646 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005647 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005648 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005649 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005650 weston_matrix_scale(&output->matrix, magnification,
5651 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005652 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005653
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005654 switch (output->transform) {
5655 case WL_OUTPUT_TRANSFORM_FLIPPED:
5656 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5657 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5658 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5659 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5660 weston_matrix_scale(&output->matrix, -1, 1, 1);
5661 break;
5662 }
5663
5664 switch (output->transform) {
5665 default:
5666 case WL_OUTPUT_TRANSFORM_NORMAL:
5667 case WL_OUTPUT_TRANSFORM_FLIPPED:
5668 break;
5669 case WL_OUTPUT_TRANSFORM_90:
5670 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5671 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
5672 weston_matrix_rotate_xy(&output->matrix, 0, 1);
5673 break;
5674 case WL_OUTPUT_TRANSFORM_180:
5675 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5676 weston_matrix_translate(&output->matrix,
5677 -output->width, -output->height, 0);
5678 weston_matrix_rotate_xy(&output->matrix, -1, 0);
5679 break;
5680 case WL_OUTPUT_TRANSFORM_270:
5681 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5682 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5683 weston_matrix_rotate_xy(&output->matrix, 0, -1);
5684 break;
5685 }
5686
5687 if (output->current_scale != 1)
5688 weston_matrix_scale(&output->matrix,
5689 output->current_scale,
5690 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01005691
Scott Moreauccbf29d2012-02-22 14:21:41 -07005692 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05005693
5694 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005695}
5696
Scott Moreau1bad5db2012-08-18 01:04:05 -06005697static void
Alexander Larsson0b135062013-05-28 16:23:36 +02005698weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06005699{
5700 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005701 output->native_scale = scale;
5702 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06005703
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005704 convert_size_by_transform_scale(&output->width, &output->height,
5705 output->current_mode->width,
5706 output->current_mode->height,
5707 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02005708}
5709
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005710static void
5711weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07005712{
5713 output->x = x;
5714 output->y = y;
5715
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005716 pixman_region32_fini(&output->previous_damage);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02005717 pixman_region32_init(&output->previous_damage);
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005718
5719 pixman_region32_fini(&output->region);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005720 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06005721 output->width,
5722 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005723}
5724
Marius Vlad55d87362019-06-11 01:15:35 +03005725/**
5726 * \ingroup output
5727 */
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005728WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005729weston_output_move(struct weston_output *output, int x, int y)
5730{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005731 struct weston_head *head;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005732 struct wl_resource *resource;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005733 int ver;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005734
5735 output->move_x = x - output->x;
5736 output->move_y = y - output->y;
5737
5738 if (output->move_x == 0 && output->move_y == 0)
5739 return;
5740
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005741 weston_output_init_geometry(output, x, y);
5742
5743 output->dirty = 1;
5744
5745 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005746 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005747
5748 /* Notify clients of the change for output position. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005749 wl_list_for_each(head, &output->head_list, output_link) {
5750 wl_resource_for_each(resource, &head->resource_list) {
5751 wl_output_send_geometry(resource,
5752 output->x,
5753 output->y,
5754 head->mm_width,
5755 head->mm_height,
5756 head->subpixel,
5757 head->make,
5758 head->model,
5759 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08005760
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005761 ver = wl_resource_get_version(resource);
5762 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
5763 wl_output_send_done(resource);
5764 }
Roman Gilge97391c2019-03-29 13:01:06 +01005765
5766 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
5767 zxdg_output_v1_send_logical_position(resource,
5768 output->x,
5769 output->y);
5770 zxdg_output_v1_send_done(resource);
5771 }
Quanxian Wangb2c86362014-03-14 09:16:25 +08005772 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005773}
5774
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005775/** Signal that a pending output is taken into use.
5776 *
5777 * Removes the output from the pending list and adds it to the compositor's
5778 * list of enabled outputs. The output created signal is emitted.
Giulio Camuffob1147152015-05-06 21:41:57 +03005779 *
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005780 * The output gets an internal ID assigned, and the wl_output global is
5781 * created.
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005782 *
Giulio Camuffob1147152015-05-06 21:41:57 +03005783 * \param compositor The compositor instance.
5784 * \param output The output to be added.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005785 *
5786 * \internal
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005787 * \ingroup compositor
Giulio Camuffob1147152015-05-06 21:41:57 +03005788 */
Pekka Paalanenf9681b52017-03-29 16:58:48 +03005789static void
Giulio Camuffob1147152015-05-06 21:41:57 +03005790weston_compositor_add_output(struct weston_compositor *compositor,
5791 struct weston_output *output)
5792{
Armin Krezoviće5403842016-08-05 15:28:29 +02005793 struct weston_view *view, *next;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005794 struct weston_head *head;
Armin Krezoviće5403842016-08-05 15:28:29 +02005795
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005796 assert(!output->enabled);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005797
5798 /* Verify we haven't reached the limit of 32 available output IDs */
5799 assert(ffs(~compositor->output_id_pool) > 0);
5800
5801 /* Invert the output id pool and look for the lowest numbered
5802 * switch (the least significant bit). Take that bit's position
5803 * as our ID, and mark it used in the compositor's output_id_pool.
5804 */
5805 output->id = ffs(~compositor->output_id_pool) - 1;
5806 compositor->output_id_pool |= 1u << output->id;
5807
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005808 wl_list_remove(&output->link);
Giulio Camuffob1147152015-05-06 21:41:57 +03005809 wl_list_insert(compositor->output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005810 output->enabled = true;
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005811
Pekka Paalanendcac3512017-12-08 14:13:34 +02005812 wl_list_for_each(head, &output->head_list, output_link)
5813 weston_head_add_global(head);
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005814
Giulio Camuffob1147152015-05-06 21:41:57 +03005815 wl_signal_emit(&compositor->output_created_signal, output);
Armin Krezoviće5403842016-08-05 15:28:29 +02005816
5817 wl_list_for_each_safe(view, next, &compositor->view_list, link)
5818 weston_view_geometry_dirty(view);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005819}
5820
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005821/** Transform device coordinates into global coordinates
5822 *
Marius Vlada2dace22019-06-12 16:05:44 +03005823 * \param output the weston_output object
5824 * \param[in] device_x X coordinate in device units.
5825 * \param[in] device_y Y coordinate in device units.
5826 * \param[out] x X coordinate in the global space.
5827 * \param[out] y Y coordinate in the global space.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005828 *
Marius Vlada2dace22019-06-12 16:05:44 +03005829 * Transforms coordinates from the device coordinate space (physical pixel
5830 * units) to the global coordinate space (logical pixel units). This takes
5831 * into account output transform and scale.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005832 *
Marius Vlad55d87362019-06-11 01:15:35 +03005833 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005834 * \internal
5835 */
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005836WL_EXPORT void
5837weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005838 double device_x, double device_y,
5839 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005840{
Derek Foreman0f679412014-10-02 13:41:17 -05005841 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005842 device_x,
5843 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05005844 0.0,
5845 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005846
Derek Foreman67a18b92015-03-24 11:36:14 -05005847 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005848
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005849 *x = p.f[0] / p.f[3];
5850 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005851}
5852
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005853/** Removes output from compositor's list of enabled outputs
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005854 *
5855 * \param output The weston_output object that is being removed.
5856 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005857 * The following happens:
5858 *
5859 * - The output assignments of all views in the current scenegraph are
5860 * recomputed.
5861 *
5862 * - Presentation feedback is discarded.
5863 *
5864 * - Compositor is notified that outputs were changed and
5865 * applies the necessary changes to re-layout outputs.
5866 *
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005867 * - The output is put back in the pending outputs list.
5868 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005869 * - Signal is emitted to notify all users of the weston_output
5870 * object that the output is being destroyed.
5871 *
5872 * - wl_output protocol objects referencing this weston_output
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005873 * are made inert, and the wl_output global is removed.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005874 *
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005875 * - The output's internal ID is released.
5876 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005877 * \ingroup compositor
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005878 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005879 */
5880static void
5881weston_compositor_remove_output(struct weston_output *output)
5882{
Pekka Paalanenbccda712017-03-29 16:16:04 +03005883 struct weston_compositor *compositor = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005884 struct weston_view *view;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005885 struct weston_head *head;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005886
5887 assert(output->destroying);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005888 assert(output->enabled);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005889
Pekka Paalanenbccda712017-03-29 16:16:04 +03005890 wl_list_for_each(view, &compositor->view_list, link) {
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005891 if (view->output_mask & (1u << output->id))
5892 weston_view_assign_output(view);
5893 }
5894
5895 weston_presentation_feedback_discard_list(&output->feedback_list);
5896
Pekka Paalanen9711fd92018-06-21 14:26:18 +03005897 weston_compositor_reflow_outputs(compositor, output, -output->width);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005898
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005899 wl_list_remove(&output->link);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005900 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005901 output->enabled = false;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005902
Pekka Paalanenbccda712017-03-29 16:16:04 +03005903 wl_signal_emit(&compositor->output_destroyed_signal, output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005904 wl_signal_emit(&output->destroy_signal, output);
5905
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02005906 wl_list_for_each(head, &output->head_list, output_link)
5907 weston_head_remove_global(head);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005908
5909 compositor->output_id_pool &= ~(1u << output->id);
5910 output->id = 0xffffffff; /* invalid */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005911}
5912
5913/** Sets the output scale for a given output.
5914 *
5915 * \param output The weston_output object that the scale is set for.
5916 * \param scale Scale factor for the given output.
5917 *
5918 * It only supports setting scale for an output that
5919 * is not enabled and it can only be ran once.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005920 *
Marius Vlad55d87362019-06-11 01:15:35 +03005921 * \ingroup ouput
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005922 */
5923WL_EXPORT void
5924weston_output_set_scale(struct weston_output *output,
5925 int32_t scale)
5926{
5927 /* We can only set scale on a disabled output */
5928 assert(!output->enabled);
5929
5930 /* We only want to set scale once */
5931 assert(!output->scale);
5932
5933 output->scale = scale;
5934}
5935
5936/** Sets the output transform for a given output.
5937 *
5938 * \param output The weston_output object that the transform is set for.
5939 * \param transform Transform value for the given output.
5940 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005941 * Refer to wl_output::transform section located at
5942 * https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_output
5943 * for list of values that can be passed to this function.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005944 *
Marius Vlad55d87362019-06-11 01:15:35 +03005945 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005946 */
5947WL_EXPORT void
5948weston_output_set_transform(struct weston_output *output,
5949 uint32_t transform)
5950{
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005951 struct weston_pointer_motion_event ev;
5952 struct wl_resource *resource;
5953 struct weston_seat *seat;
5954 pixman_region32_t old_region;
5955 int mid_x, mid_y;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005956 struct weston_head *head;
5957 int ver;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005958
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005959 if (!output->enabled && output->transform == UINT32_MAX) {
5960 output->transform = transform;
5961 return;
5962 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005963
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005964 weston_output_transform_scale_init(output, transform, output->scale);
5965
5966 pixman_region32_init(&old_region);
5967 pixman_region32_copy(&old_region, &output->region);
5968
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005969 weston_output_init_geometry(output, output->x, output->y);
5970
5971 output->dirty = 1;
5972
5973 /* Notify clients of the change for output transform. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005974 wl_list_for_each(head, &output->head_list, output_link) {
5975 wl_resource_for_each(resource, &head->resource_list) {
5976 wl_output_send_geometry(resource,
5977 output->x,
5978 output->y,
5979 head->mm_width,
5980 head->mm_height,
5981 head->subpixel,
5982 head->make,
5983 head->model,
5984 output->transform);
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005985
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005986 ver = wl_resource_get_version(resource);
5987 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
5988 wl_output_send_done(resource);
5989 }
Roman Gilge97391c2019-03-29 13:01:06 +01005990 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
5991 zxdg_output_v1_send_logical_position(resource,
5992 output->x,
5993 output->y);
5994 zxdg_output_v1_send_logical_size(resource,
5995 output->width,
5996 output->height);
5997 zxdg_output_v1_send_done(resource);
5998 }
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005999 }
6000
6001 /* we must ensure that pointers are inside output, otherwise they disappear */
6002 mid_x = output->x + output->width / 2;
6003 mid_y = output->y + output->height / 2;
6004
6005 ev.mask = WESTON_POINTER_MOTION_ABS;
6006 ev.x = wl_fixed_to_double(wl_fixed_from_int(mid_x));
6007 ev.y = wl_fixed_to_double(wl_fixed_from_int(mid_y));
6008
6009 wl_list_for_each(seat, &output->compositor->seat_list, link) {
6010 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
6011
6012 if (pointer && pixman_region32_contains_point(&old_region,
6013 wl_fixed_to_int(pointer->x),
6014 wl_fixed_to_int(pointer->y),
6015 NULL))
6016 weston_pointer_move(pointer, &ev);
6017 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006018}
6019
6020/** Initializes a weston_output object with enough data so
6021 ** an output can be configured.
6022 *
6023 * \param output The weston_output object to initialize
6024 * \param compositor The compositor instance.
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006025 * \param name Name for the output (the string is copied).
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006026 *
6027 * Sets initial values for fields that are expected to be
6028 * configured either by compositors or backends.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006029 *
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006030 * The name is used in logs, and can be used by compositors as a configuration
6031 * identifier.
6032 *
Marius Vlad55d87362019-06-11 01:15:35 +03006033 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006034 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006035 */
6036WL_EXPORT void
Armin Krezović40087402016-09-30 14:11:12 +02006037weston_output_init(struct weston_output *output,
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006038 struct weston_compositor *compositor,
6039 const char *name)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006040{
6041 output->compositor = compositor;
6042 output->destroying = 0;
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006043 output->name = strdup(name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006044 wl_list_init(&output->link);
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006045 wl_signal_init(&output->user_destroy_signal);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006046 output->enabled = false;
Ankit Nautiyal2690a772019-03-25 17:57:59 +05306047 output->desired_protection = WESTON_HDCP_DISABLE;
Ankit Nautiyal2844f8e2019-04-03 10:14:59 +05306048 output->allow_protection = true;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006049
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006050 wl_list_init(&output->head_list);
6051
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006052 /* Add some (in)sane defaults which can be used
6053 * for checking if an output was properly configured
6054 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006055 output->scale = 0;
6056 /* Can't use -1 on uint32_t and 0 is valid enum value */
6057 output->transform = UINT32_MAX;
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006058
6059 pixman_region32_init(&output->previous_damage);
6060 pixman_region32_init(&output->region);
Pekka Paalanen42704142017-09-06 16:47:52 +03006061 wl_list_init(&output->mode_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006062}
6063
6064/** Adds weston_output object to pending output list.
6065 *
6066 * \param output The weston_output object to add
6067 * \param compositor The compositor instance.
6068 *
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03006069 * The opposite of this operation is built into weston_output_release().
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006070 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006071 * \ingroup compositor
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006072 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006073 */
6074WL_EXPORT void
6075weston_compositor_add_pending_output(struct weston_output *output,
6076 struct weston_compositor *compositor)
6077{
Pekka Paalanene952a012017-03-29 17:14:00 +03006078 assert(output->disable);
6079 assert(output->enable);
6080
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006081 wl_list_remove(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006082 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006083}
6084
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006085/** Create a string with the attached heads' names.
6086 *
6087 * The string must be free()'d.
Marius Vlad55d87362019-06-11 01:15:35 +03006088 *
6089 * \ingroup output
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006090 */
6091static char *
6092weston_output_create_heads_string(struct weston_output *output)
6093{
6094 FILE *fp;
6095 char *str = NULL;
6096 size_t size = 0;
6097 struct weston_head *head;
6098 const char *sep = "";
6099
6100 fp = open_memstream(&str, &size);
6101 if (!fp)
6102 return NULL;
6103
6104 wl_list_for_each(head, &output->head_list, output_link) {
6105 fprintf(fp, "%s%s", sep, head->name);
6106 sep = ", ";
6107 }
6108 fclose(fp);
6109
6110 return str;
6111}
6112
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006113/** Constructs a weston_output object that can be used by the compositor.
6114 *
Pekka Paalanencc201e42017-03-30 15:11:25 +03006115 * \param output The weston_output object that needs to be enabled. Must not
Pekka Paalanenddce54d2017-08-23 16:00:21 +03006116 * be enabled already. Must have at least one head attached.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006117 *
6118 * Output coordinates are calculated and each new output is by default
6119 * assigned to the right of previous one.
6120 *
6121 * Sets up the transformation, zoom, and geometry of the output using
6122 * the properties that need to be configured by the compositor.
6123 *
6124 * Establishes a repaint timer for the output with the relevant display
6125 * object's event loop. See output_repaint_timer_handler().
6126 *
6127 * The output is assigned an ID. Weston can support up to 32 distinct
6128 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
6129 * is referred to and used to find the first available ID number, and
6130 * then this ID is marked as used in output_id_pool.
6131 *
6132 * The output is also assigned a Wayland global with the wl_output
6133 * external interface.
6134 *
6135 * Backend specific function is called to set up the output output.
6136 *
6137 * Output is added to the compositor's output list
6138 *
6139 * If the backend specific function fails, the weston_output object
6140 * is returned to a state it was before calling this function and
6141 * is added to the compositor's pending_output_list in case it needs
6142 * to be reconfigured or just so it can be destroyed at shutdown.
6143 *
6144 * 0 is returned on success, -1 on failure.
Marius Vlad55d87362019-06-11 01:15:35 +03006145 *
6146 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006147 */
6148WL_EXPORT int
6149weston_output_enable(struct weston_output *output)
6150{
Armin Krezović782f5df2016-09-30 14:11:11 +02006151 struct weston_compositor *c = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006152 struct weston_output *iterator;
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03006153 struct weston_head *head;
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006154 char *head_names;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006155 int x = 0, y = 0;
6156
Pekka Paalanencc201e42017-03-30 15:11:25 +03006157 if (output->enabled) {
6158 weston_log("Error: attempt to enable an enabled output '%s'\n",
6159 output->name);
6160 return -1;
6161 }
6162
Pekka Paalanenddce54d2017-08-23 16:00:21 +03006163 if (wl_list_empty(&output->head_list)) {
6164 weston_log("Error: cannot enable output '%s' without heads.\n",
6165 output->name);
6166 return -1;
6167 }
6168
Pekka Paalanen586e1ac2017-09-14 16:17:59 +03006169 if (wl_list_empty(&output->mode_list) || !output->current_mode) {
6170 weston_log("Error: no video mode for output '%s'.\n",
6171 output->name);
6172 return -1;
6173 }
6174
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03006175 wl_list_for_each(head, &output->head_list, output_link) {
6176 assert(head->make);
6177 assert(head->model);
6178 }
6179
Armin Krezović782f5df2016-09-30 14:11:11 +02006180 iterator = container_of(c->output_list.prev,
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006181 struct weston_output, link);
6182
Armin Krezović782f5df2016-09-30 14:11:11 +02006183 if (!wl_list_empty(&c->output_list))
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006184 x = iterator->x + iterator->width;
6185
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006186 /* Make sure the scale is set up */
6187 assert(output->scale);
6188
6189 /* Make sure we have a transform set */
6190 assert(output->transform != UINT32_MAX);
6191
Armin Krezović782f5df2016-09-30 14:11:11 +02006192 output->x = x;
6193 output->y = y;
6194 output->dirty = 1;
6195 output->original_scale = output->scale;
6196
6197 weston_output_transform_scale_init(output, output->transform, output->scale);
6198 weston_output_init_zoom(output);
6199
6200 weston_output_init_geometry(output, x, y);
6201 weston_output_damage(output);
6202
6203 wl_signal_init(&output->frame_signal);
6204 wl_signal_init(&output->destroy_signal);
6205 wl_list_init(&output->animation_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02006206 wl_list_init(&output->feedback_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02006207
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006208 /* Enable the output (set up the crtc or create a
6209 * window representing the output, set up the
6210 * renderer, etc)
6211 */
6212 if (output->enable(output) < 0) {
6213 weston_log("Enabling output \"%s\" failed.\n", output->name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006214 return -1;
6215 }
6216
6217 weston_compositor_add_output(output->compositor, output);
6218
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006219 head_names = weston_output_create_heads_string(output);
6220 weston_log("Output '%s' enabled with head(s) %s\n",
6221 output->name, head_names);
6222 free(head_names);
6223
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006224 return 0;
6225}
6226
6227/** Converts a weston_output object to a pending output state, so it
6228 ** can be configured again or destroyed.
6229 *
6230 * \param output The weston_output object that needs to be disabled.
6231 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006232 * Calls a backend specific function to disable an output, in case
6233 * such function exists.
6234 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03006235 * The backend specific disable function may choose to postpone the disabling
6236 * by returning a negative value, in which case this function returns early.
6237 * In that case the backend will guarantee the output will be disabled soon
6238 * by the backend calling this function again. One must not attempt to re-enable
6239 * the output until that happens.
6240 *
6241 * Otherwise, if the output is being used by the compositor, it is removed
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006242 * from weston's output_list (see weston_compositor_remove_output())
6243 * and is returned to a state it was before weston_output_enable()
6244 * was ran (see weston_output_enable_undo()).
6245 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03006246 * See weston_output_init() for more information on the
6247 * state output is returned to.
Pekka Paalanencc201e42017-03-30 15:11:25 +03006248 *
6249 * If the output has never been enabled yet, this function can still be
6250 * called to ensure that the output is actually turned off rather than left
6251 * in the state it was discovered in.
Marius Vlad55d87362019-06-11 01:15:35 +03006252 *
6253 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006254 */
6255WL_EXPORT void
6256weston_output_disable(struct weston_output *output)
6257{
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006258 /* Should we rename this? */
6259 output->destroying = 1;
6260
Pekka Paalanenc65df642017-03-29 15:45:46 +03006261 /* Disable is called unconditionally also for not-enabled outputs,
6262 * because at compositor start-up, if there is an output that is
6263 * already on but the compositor wants to turn it off, we have to
6264 * forward the turn-off to the backend so it knows to do it.
6265 * The backend cannot initially turn off everything, because it
6266 * would cause unnecessary mode-sets for all outputs the compositor
6267 * wants to be on.
6268 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006269 if (output->disable(output) < 0)
6270 return;
6271
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006272 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006273 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006274
6275 output->destroying = 0;
6276}
6277
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006278/** Forces a synchronous call to heads_changed hook
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006279 *
6280 * \param compositor The compositor instance
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006281 *
6282 * If there are new or changed heads, calls the heads_changed hook and
6283 * returns after the hook returns.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006284 *
6285 * \ingroup compositor
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006286 */
6287WL_EXPORT void
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006288weston_compositor_flush_heads_changed(struct weston_compositor *compositor)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006289{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03006290 if (compositor->heads_changed_source) {
6291 wl_event_source_remove(compositor->heads_changed_source);
6292 weston_compositor_call_heads_changed(compositor);
6293 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006294}
6295
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006296/** Add destroy callback for an output
6297 *
6298 * \param output The output to watch.
6299 * \param listener The listener to add. The \c notify member must be set.
6300 *
6301 * The listener callback will be called when user destroys an output. This
6302 * may be delayed by a backend in some cases. The main purpose of the
6303 * listener is to allow hooking up custom data to the output. The custom data
6304 * can be fetched via weston_output_get_destroy_listener() followed by
6305 * container_of().
6306 *
6307 * The \c data argument to the notify callback is the weston_output being
6308 * destroyed.
6309 *
6310 * @note This is for the final destruction of an output, not when it gets
6311 * disabled. If you want to keep track of enabled outputs, this is not it.
Marius Vlad55d87362019-06-11 01:15:35 +03006312 *
6313 * \ingroup ouput
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006314 */
6315WL_EXPORT void
6316weston_output_add_destroy_listener(struct weston_output *output,
6317 struct wl_listener *listener)
6318{
6319 wl_signal_add(&output->user_destroy_signal, listener);
6320}
6321
6322/** Look up destroy listener for an output
6323 *
6324 * \param output The output to query.
6325 * \param notify The notify function used used for the added destroy listener.
6326 * \return The listener, or NULL if not found.
6327 *
6328 * This looks up the previously added destroy listener struct based on the
6329 * notify function it has. The listener can be used to access user data
6330 * through \c container_of().
6331 *
6332 * \sa wl_signal_get() weston_output_add_destroy_listener()
Marius Vlad55d87362019-06-11 01:15:35 +03006333 * \ingroup output
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006334 */
6335WL_EXPORT struct wl_listener *
6336weston_output_get_destroy_listener(struct weston_output *output,
6337 wl_notify_func_t notify)
6338{
6339 return wl_signal_get(&output->user_destroy_signal, notify);
6340}
6341
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006342/** Uninitialize an output
6343 *
6344 * Removes the output from the list of enabled outputs if necessary, but
6345 * does not call the backend's output disable function. The output will no
6346 * longer be in the list of pending outputs either.
6347 *
6348 * All fields of weston_output become uninitialized, i.e. should not be used
6349 * anymore. The caller can free the memory after this.
6350 *
Marius Vlad55d87362019-06-11 01:15:35 +03006351 * \ingroup ouput
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006352 * \internal
6353 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006354WL_EXPORT void
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03006355weston_output_release(struct weston_output *output)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006356{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006357 struct weston_head *head, *tmp;
6358
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006359 output->destroying = 1;
6360
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006361 wl_signal_emit(&output->user_destroy_signal, output);
6362
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03006363 if (output->idle_repaint_source)
6364 wl_event_source_remove(output->idle_repaint_source);
6365
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006366 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006367 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006368
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006369 pixman_region32_fini(&output->region);
6370 pixman_region32_fini(&output->previous_damage);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006371 wl_list_remove(&output->link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006372
6373 wl_list_for_each_safe(head, tmp, &output->head_list, output_link)
6374 weston_head_detach(head);
6375
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006376 free(output->name);
6377}
6378
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006379/** Find an output by its given name
6380 *
6381 * \param compositor The compositor to search in.
6382 * \param name The output name to search for.
6383 * \return An existing output with the given name, or NULL if not found.
6384 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006385 * \ingroup compositor
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006386 */
6387WL_EXPORT struct weston_output *
6388weston_compositor_find_output_by_name(struct weston_compositor *compositor,
6389 const char *name)
6390{
6391 struct weston_output *output;
6392
6393 wl_list_for_each(output, &compositor->output_list, link)
6394 if (strcmp(output->name, name) == 0)
6395 return output;
6396
6397 wl_list_for_each(output, &compositor->pending_output_list, link)
6398 if (strcmp(output->name, name) == 0)
6399 return output;
6400
6401 return NULL;
6402}
6403
6404/** Create a named output
6405 *
6406 * \param compositor The compositor.
6407 * \param name The name for the output.
6408 * \return A new \c weston_output, or NULL on failure.
6409 *
6410 * This creates a new weston_output that starts with no heads attached.
6411 *
6412 * An output must be configured and it must have at least one head before
6413 * it can be enabled.
6414 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006415 * \ingroup compositor
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006416 */
6417WL_EXPORT struct weston_output *
6418weston_compositor_create_output(struct weston_compositor *compositor,
6419 const char *name)
6420{
6421 assert(compositor->backend->create_output);
6422
6423 if (weston_compositor_find_output_by_name(compositor, name)) {
6424 weston_log("Warning: attempted to create an output with a "
6425 "duplicate name '%s'.\n", name);
6426 return NULL;
6427 }
6428
6429 return compositor->backend->create_output(compositor, name);
6430}
6431
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006432/** Create an output for an unused head
6433 *
6434 * \param compositor The compositor.
6435 * \param head The head to attach to the output.
6436 * \return A new \c weston_output, or NULL on failure.
6437 *
6438 * This creates a new weston_output that starts with the given head attached.
6439 * The output inherits the name of the head. The head must not be already
6440 * attached to another output.
6441 *
6442 * An output must be configured before it can be enabled.
6443 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006444 * \ingroup compositor
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006445 */
6446WL_EXPORT struct weston_output *
6447weston_compositor_create_output_with_head(struct weston_compositor *compositor,
6448 struct weston_head *head)
6449{
6450 struct weston_output *output;
6451
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006452 output = weston_compositor_create_output(compositor, head->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006453 if (!output)
6454 return NULL;
6455
6456 if (weston_output_attach_head(output, head) < 0) {
Pekka Paalanen42c0e142017-10-27 12:07:49 +03006457 weston_output_destroy(output);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006458 return NULL;
6459 }
6460
6461 return output;
6462}
6463
6464/** Destroy an output
6465 *
6466 * \param output The output to destroy.
6467 *
6468 * The heads attached to the given output are detached and become unused again.
6469 *
6470 * It is not necessary to explicitly destroy all outputs at compositor exit.
6471 * weston_compositor_destroy() will automatically destroy any remaining
6472 * outputs.
6473 *
Marius Vlad55d87362019-06-11 01:15:35 +03006474 * \ingroup ouput
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006475 */
6476WL_EXPORT void
6477weston_output_destroy(struct weston_output *output)
6478{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006479 output->destroy(output);
6480}
6481
Pekka Paalanencf0a4762017-04-04 16:36:07 +03006482/** When you need a head...
6483 *
6484 * This function is a hack, used until all code has been converted to become
6485 * multi-head aware.
6486 *
6487 * \param output The weston_output whose head to get.
6488 * \return The first head in the output's list.
Marius Vlad55d87362019-06-11 01:15:35 +03006489 *
6490 * \ingroup ouput
Pekka Paalanencf0a4762017-04-04 16:36:07 +03006491 */
6492WL_EXPORT struct weston_head *
6493weston_output_get_first_head(struct weston_output *output)
6494{
6495 if (wl_list_empty(&output->head_list))
6496 return NULL;
6497
6498 return container_of(output->head_list.next,
6499 struct weston_head, output_link);
6500}
6501
Ankit Nautiyal2844f8e2019-04-03 10:14:59 +05306502/** Allow/Disallow content-protection support for an output
6503 *
6504 * This function sets the allow_protection member for an output. Setting of
6505 * this field will allow the compositor to attempt content-protection for this
6506 * output, for a backend that supports the content-protection protocol.
6507 *
6508 * \param output The weston_output for whom the content-protection is to be
6509 * allowed.
6510 * \param allow_protection The bool value which is to be set.
6511 */
6512WL_EXPORT void
6513weston_output_allow_protection(struct weston_output *output,
6514 bool allow_protection)
6515{
6516 output->allow_protection = allow_protection;
6517}
6518
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01006519static void
Roman Gilge97391c2019-03-29 13:01:06 +01006520xdg_output_unlist(struct wl_resource *resource)
6521{
6522 wl_list_remove(wl_resource_get_link(resource));
6523}
6524
6525static void
6526xdg_output_destroy(struct wl_client *client, struct wl_resource *resource)
6527{
6528 wl_resource_destroy(resource);
6529}
6530
6531static const struct zxdg_output_v1_interface xdg_output_interface = {
6532 xdg_output_destroy
6533};
6534
6535static void
6536xdg_output_manager_destroy(struct wl_client *client,
6537 struct wl_resource *resource)
6538{
6539 wl_resource_destroy(resource);
6540}
6541
6542static void
6543xdg_output_manager_get_xdg_output(struct wl_client *client,
6544 struct wl_resource *manager,
6545 uint32_t id,
6546 struct wl_resource *output_resource)
6547{
6548 int version = wl_resource_get_version(manager);
6549 struct weston_head *head = wl_resource_get_user_data(output_resource);
6550 struct weston_output *output = head->output;
6551 struct wl_resource *resource;
6552
6553 resource = wl_resource_create(client, &zxdg_output_v1_interface,
6554 version, id);
6555 if (resource == NULL) {
6556 wl_client_post_no_memory(client);
6557 return;
6558 }
6559
6560 wl_list_insert(&head->xdg_output_resource_list,
6561 wl_resource_get_link(resource));
6562
6563 wl_resource_set_implementation(resource, &xdg_output_interface,
6564 NULL, xdg_output_unlist);
6565
6566 zxdg_output_v1_send_logical_position(resource, output->x, output->y);
6567 zxdg_output_v1_send_logical_size(resource,
6568 output->width,
6569 output->height);
6570 if (version >= ZXDG_OUTPUT_V1_NAME_SINCE_VERSION)
6571 zxdg_output_v1_send_name(resource, head->name);
6572
6573 zxdg_output_v1_send_done(resource);
6574}
6575
6576static const struct zxdg_output_manager_v1_interface xdg_output_manager_interface = {
6577 xdg_output_manager_destroy,
6578 xdg_output_manager_get_xdg_output
6579};
6580
6581static void
6582bind_xdg_output_manager(struct wl_client *client,
6583 void *data, uint32_t version, uint32_t id)
6584{
6585 struct wl_resource *resource;
6586
6587 resource = wl_resource_create(client, &zxdg_output_manager_v1_interface,
6588 version, id);
6589 if (resource == NULL) {
6590 wl_client_post_no_memory(client);
6591 return;
6592 }
6593
6594 wl_resource_set_implementation(resource, &xdg_output_manager_interface,
6595 NULL, NULL);
6596}
6597
6598static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006599destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006600{
Jonny Lamb74130762013-11-26 18:19:46 +01006601 struct weston_surface *surface =
6602 wl_resource_get_user_data(resource);
6603
Pekka Paalanen4826f872016-04-22 14:14:38 +03006604 if (!surface)
6605 return;
6606
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006607 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02006608 surface->pending.buffer_viewport.buffer.src_width =
6609 wl_fixed_from_int(-1);
6610 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006611 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006612}
6613
6614static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006615viewport_destroy(struct wl_client *client,
6616 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006617{
6618 wl_resource_destroy(resource);
6619}
6620
6621static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006622viewport_set_source(struct wl_client *client,
6623 struct wl_resource *resource,
6624 wl_fixed_t src_x,
6625 wl_fixed_t src_y,
6626 wl_fixed_t src_width,
6627 wl_fixed_t src_height)
6628{
6629 struct weston_surface *surface =
6630 wl_resource_get_user_data(resource);
6631
Pekka Paalanen4826f872016-04-22 14:14:38 +03006632 if (!surface) {
6633 wl_resource_post_error(resource,
6634 WP_VIEWPORT_ERROR_NO_SURFACE,
6635 "wl_surface for this viewport is no longer exists");
6636 return;
6637 }
6638
6639 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03006640 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006641
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006642 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03006643 src_height == wl_fixed_from_int(-1) &&
6644 src_x == wl_fixed_from_int(-1) &&
6645 src_y == wl_fixed_from_int(-1)) {
6646 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006647 surface->pending.buffer_viewport.buffer.src_width =
6648 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006649 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006650 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006651 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006652
Pekka Paalanen201769a2016-04-26 14:42:11 +03006653 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006654 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006655 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03006656 "wl_surface@%d viewport source "
6657 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
6658 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006659 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03006660 wl_fixed_to_double(src_height),
6661 wl_fixed_to_double(src_x),
6662 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006663 return;
6664 }
6665
6666 surface->pending.buffer_viewport.buffer.src_x = src_x;
6667 surface->pending.buffer_viewport.buffer.src_y = src_y;
6668 surface->pending.buffer_viewport.buffer.src_width = src_width;
6669 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006670 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006671}
6672
6673static void
6674viewport_set_destination(struct wl_client *client,
6675 struct wl_resource *resource,
6676 int32_t dst_width,
6677 int32_t dst_height)
6678{
6679 struct weston_surface *surface =
6680 wl_resource_get_user_data(resource);
6681
Pekka Paalanen4826f872016-04-22 14:14:38 +03006682 if (!surface) {
6683 wl_resource_post_error(resource,
6684 WP_VIEWPORT_ERROR_NO_SURFACE,
6685 "wl_surface for this viewport no longer exists");
6686 return;
6687 }
6688
6689 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006690
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006691 if (dst_width == -1 && dst_height == -1) {
6692 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006693 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006694 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006695 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006696 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006697
6698 if (dst_width <= 0 || dst_height <= 0) {
6699 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006700 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006701 "destination size must be positive (%dx%d)",
6702 dst_width, dst_height);
6703 return;
6704 }
6705
6706 surface->pending.buffer_viewport.surface.width = dst_width;
6707 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006708 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006709}
6710
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006711static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006712 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006713 viewport_set_source,
6714 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01006715};
6716
6717static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006718viewporter_destroy(struct wl_client *client,
6719 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006720{
6721 wl_resource_destroy(resource);
6722}
6723
6724static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006725viewporter_get_viewport(struct wl_client *client,
6726 struct wl_resource *viewporter,
6727 uint32_t id,
6728 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006729{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006730 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006731 struct weston_surface *surface =
6732 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006733 struct wl_resource *resource;
6734
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006735 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006736 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006737 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006738 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01006739 return;
6740 }
6741
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006742 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006743 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006744 if (resource == NULL) {
6745 wl_client_post_no_memory(client);
6746 return;
6747 }
6748
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006749 wl_resource_set_implementation(resource, &viewport_interface,
6750 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01006751
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006752 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006753}
6754
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006755static const struct wp_viewporter_interface viewporter_interface = {
6756 viewporter_destroy,
6757 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01006758};
6759
6760static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006761bind_viewporter(struct wl_client *client,
6762 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006763{
6764 struct wl_resource *resource;
6765
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006766 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006767 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006768 if (resource == NULL) {
6769 wl_client_post_no_memory(client);
6770 return;
6771 }
6772
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006773 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01006774 NULL, NULL);
6775}
6776
6777static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04006778destroy_presentation_feedback(struct wl_resource *feedback_resource)
6779{
6780 struct weston_presentation_feedback *feedback;
6781
6782 feedback = wl_resource_get_user_data(feedback_resource);
6783
6784 wl_list_remove(&feedback->link);
6785 free(feedback);
6786}
6787
6788static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006789presentation_destroy(struct wl_client *client, struct wl_resource *resource)
6790{
6791 wl_resource_destroy(resource);
6792}
6793
6794static void
6795presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04006796 struct wl_resource *presentation_resource,
6797 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006798 uint32_t callback)
6799{
Pekka Paalanen133e4392014-09-23 22:08:46 -04006800 struct weston_surface *surface;
6801 struct weston_presentation_feedback *feedback;
6802
6803 surface = wl_resource_get_user_data(surface_resource);
6804
Bryce Harringtonde16d892014-11-20 22:21:57 -08006805 feedback = zalloc(sizeof *feedback);
6806 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04006807 goto err_calloc;
6808
6809 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006810 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04006811 1, callback);
6812 if (!feedback->resource)
6813 goto err_create;
6814
6815 wl_resource_set_implementation(feedback->resource, NULL, feedback,
6816 destroy_presentation_feedback);
6817
6818 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
6819
6820 return;
6821
6822err_create:
6823 free(feedback);
6824
6825err_calloc:
6826 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006827}
6828
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006829static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006830 presentation_destroy,
6831 presentation_feedback
6832};
6833
6834static void
6835bind_presentation(struct wl_client *client,
6836 void *data, uint32_t version, uint32_t id)
6837{
6838 struct weston_compositor *compositor = data;
6839 struct wl_resource *resource;
6840
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006841 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006842 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006843 if (resource == NULL) {
6844 wl_client_post_no_memory(client);
6845 return;
6846 }
6847
6848 wl_resource_set_implementation(resource, &presentation_implementation,
6849 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006850 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006851}
6852
6853static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05006854compositor_bind(struct wl_client *client,
6855 void *data, uint32_t version, uint32_t id)
6856{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006857 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05006858 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05006859
Jason Ekstranda85118c2013-06-27 20:17:02 -05006860 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006861 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07006862 if (resource == NULL) {
6863 wl_client_post_no_memory(client);
6864 return;
6865 }
6866
6867 wl_resource_set_implementation(resource, &compositor_interface,
6868 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05006869}
6870
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006871WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006872weston_environment_get_fd(const char *env)
6873{
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07006874 char *e;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006875 int fd, flags;
6876
6877 e = getenv(env);
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07006878 if (!e || !safe_strtoint(e, &fd))
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006879 return -1;
6880
6881 flags = fcntl(fd, F_GETFD);
6882 if (flags == -1)
6883 return -1;
6884
6885 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
6886 unsetenv(env);
6887
6888 return fd;
6889}
6890
Pekka Paalanenb5026542014-11-12 15:09:24 +02006891static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02006892timeline_key_binding_handler(struct weston_keyboard *keyboard,
6893 const struct timespec *time, uint32_t key,
6894 void *data)
Pekka Paalanenb5026542014-11-12 15:09:24 +02006895{
6896 struct weston_compositor *compositor = data;
6897
6898 if (weston_timeline_enabled_)
6899 weston_timeline_close();
6900 else
6901 weston_timeline_open(compositor);
6902}
6903
Daniel Stonece62cb32018-07-20 09:46:24 +01006904static const char *
6905output_repaint_status_text(struct weston_output *output)
6906{
6907 switch (output->repaint_status) {
6908 case REPAINT_NOT_SCHEDULED:
6909 return "no repaint";
6910 case REPAINT_BEGIN_FROM_IDLE:
6911 return "start_repaint_loop scheduled";
6912 case REPAINT_SCHEDULED:
6913 return "repaint scheduled";
6914 case REPAINT_AWAITING_COMPLETION:
6915 return "awaiting completion";
6916 }
6917
6918 assert(!"output_repaint_status_text missing enum");
6919 return NULL;
6920}
6921
6922static void
6923debug_scene_view_print_buffer(FILE *fp, struct weston_view *view)
6924{
6925 struct weston_buffer *buffer = view->surface->buffer_ref.buffer;
6926 struct wl_shm_buffer *shm;
6927 struct linux_dmabuf_buffer *dmabuf;
Marius Vlad00a6e012018-11-20 17:52:31 +02006928 const struct pixel_format_info *pixel_info = NULL;
Daniel Stonece62cb32018-07-20 09:46:24 +01006929
6930 if (!buffer) {
6931 fprintf(fp, "\t\t[buffer not available]\n");
6932 return;
6933 }
6934
6935 shm = wl_shm_buffer_get(buffer->resource);
6936 if (shm) {
Marius Vlad00a6e012018-11-20 17:52:31 +02006937 uint32_t _format = wl_shm_buffer_get_format(shm);
6938 pixel_info = pixel_format_get_info_shm(_format);
Daniel Stonece62cb32018-07-20 09:46:24 +01006939 fprintf(fp, "\t\tSHM buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02006940 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
6941 (unsigned long) _format,
6942 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Daniel Stonece62cb32018-07-20 09:46:24 +01006943 return;
6944 }
6945
6946 dmabuf = linux_dmabuf_buffer_get(buffer->resource);
6947 if (dmabuf) {
Marius Vlad00a6e012018-11-20 17:52:31 +02006948 pixel_info = pixel_format_get_info(dmabuf->attributes.format);
Daniel Stonece62cb32018-07-20 09:46:24 +01006949 fprintf(fp, "\t\tdmabuf buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02006950 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
6951 (unsigned long) dmabuf->attributes.format,
6952 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Daniel Stonece62cb32018-07-20 09:46:24 +01006953 fprintf(fp, "\t\t\tmodifier: 0x%llx\n",
6954 (unsigned long long) dmabuf->attributes.modifier[0]);
6955 return;
6956 }
6957
Marius Vlad253ba9a2019-03-17 18:22:21 +02006958 fprintf(fp, "\t\tEGL buffer\n");
Daniel Stonece62cb32018-07-20 09:46:24 +01006959}
6960
6961static void
6962debug_scene_view_print(FILE *fp, struct weston_view *view, int view_idx)
6963{
6964 struct weston_compositor *ec = view->surface->compositor;
6965 struct weston_output *output;
6966 char desc[512];
6967 pixman_box32_t *box;
6968 uint32_t surface_id = 0;
6969 pid_t pid = 0;
6970
6971 if (view->surface->resource) {
6972 struct wl_resource *resource = view->surface->resource;
6973 wl_client_get_credentials(wl_resource_get_client(resource),
6974 &pid, NULL, NULL);
6975 surface_id = wl_resource_get_id(view->surface->resource);
6976 }
6977
6978 if (!view->surface->get_label ||
6979 view->surface->get_label(view->surface, desc, sizeof(desc)) < 0) {
6980 strcpy(desc, "[no description available]");
6981 }
6982 fprintf(fp, "\tView %d (role %s, PID %d, surface ID %u, %s, %p):\n",
6983 view_idx, view->surface->role_name, pid, surface_id,
6984 desc, view);
6985
6986 box = pixman_region32_extents(&view->transform.boundingbox);
6987 fprintf(fp, "\t\tposition: (%d, %d) -> (%d, %d)\n",
6988 box->x1, box->y1, box->x2, box->y2);
6989 box = pixman_region32_extents(&view->transform.opaque);
6990
6991 if (pixman_region32_equal(&view->transform.opaque,
6992 &view->transform.boundingbox)) {
6993 fprintf(fp, "\t\t[fully opaque]\n");
6994 } else if (!pixman_region32_not_empty(&view->transform.opaque)) {
6995 fprintf(fp, "\t\t[not opaque]\n");
6996 } else {
6997 fprintf(fp, "\t\t[opaque: (%d, %d) -> (%d, %d)]\n",
6998 box->x1, box->y1, box->x2, box->y2);
6999 }
7000
7001 if (view->alpha < 1.0)
7002 fprintf(fp, "\t\talpha: %f\n", view->alpha);
7003
7004 if (view->output_mask != 0) {
7005 bool first_output = true;
7006 fprintf(fp, "\t\toutputs: ");
7007 wl_list_for_each(output, &ec->output_list, link) {
7008 if (!(view->output_mask & (1 << output->id)))
7009 continue;
7010 fprintf(fp, "%s%d (%s)%s",
7011 (first_output) ? "" : ", ",
7012 output->id, output->name,
7013 (view->output == output) ? " (primary)" : "");
7014 first_output = false;
7015 }
7016 } else {
7017 fprintf(fp, "\t\t[no outputs]");
7018 }
7019
7020 fprintf(fp, "\n");
7021
7022 debug_scene_view_print_buffer(fp, view);
7023}
7024
Marius Vlad433f4e72019-02-17 22:14:23 +02007025static void
7026debug_scene_view_print_tree(struct weston_view *view,
Marius Vlada6acfa82019-03-17 18:10:09 +02007027 FILE *fp, int *view_idx)
Marius Vlad433f4e72019-02-17 22:14:23 +02007028{
7029 struct weston_subsurface *sub;
7030 struct weston_view *ev;
7031
7032 /*
7033 * print the view first, then we recursively go on printing
7034 * sub-surfaces. We bail out once no more sub-surfaces are available.
7035 */
Marius Vlada6acfa82019-03-17 18:10:09 +02007036 debug_scene_view_print(fp, view, *view_idx);
Marius Vlad433f4e72019-02-17 22:14:23 +02007037
7038 /* no more sub-surfaces */
7039 if (wl_list_empty(&view->surface->subsurface_list))
7040 return;
7041
7042 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
7043 wl_list_for_each(ev, &sub->surface->views, surface_link) {
7044 /* do not print again the parent view */
7045 if (view == ev)
7046 continue;
Marius Vlada6acfa82019-03-17 18:10:09 +02007047
7048 (*view_idx)++;
Marius Vlad433f4e72019-02-17 22:14:23 +02007049 debug_scene_view_print_tree(ev, fp, view_idx);
7050 }
7051 }
7052}
7053
Daniel Stonece62cb32018-07-20 09:46:24 +01007054/**
7055 * Output information on how libweston is currently composing the scene
7056 * graph.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007057 *
7058 * \ingroup compositor
Daniel Stonece62cb32018-07-20 09:46:24 +01007059 */
7060WL_EXPORT char *
7061weston_compositor_print_scene_graph(struct weston_compositor *ec)
7062{
7063 struct weston_output *output;
7064 struct weston_layer *layer;
7065 struct timespec now;
7066 int layer_idx = 0;
7067 FILE *fp;
7068 char *ret;
7069 size_t len;
7070 int err;
7071
7072 fp = open_memstream(&ret, &len);
7073 assert(fp);
7074
7075 weston_compositor_read_presentation_clock(ec, &now);
7076 fprintf(fp, "Weston scene graph at %ld.%09ld:\n\n",
7077 now.tv_sec, now.tv_nsec);
7078
7079 wl_list_for_each(output, &ec->output_list, link) {
7080 struct weston_head *head;
7081 int head_idx = 0;
7082
7083 fprintf(fp, "Output %d (%s):\n", output->id, output->name);
7084 assert(output->enabled);
7085
7086 fprintf(fp, "\tposition: (%d, %d) -> (%d, %d)\n",
7087 output->x, output->y,
7088 output->x + output->width,
7089 output->y + output->height);
7090 fprintf(fp, "\tmode: %dx%d@%.3fHz\n",
7091 output->current_mode->width,
7092 output->current_mode->height,
7093 output->current_mode->refresh / 1000.0);
7094 fprintf(fp, "\tscale: %d\n", output->scale);
7095
7096 fprintf(fp, "\trepaint status: %s\n",
7097 output_repaint_status_text(output));
7098 if (output->repaint_status == REPAINT_SCHEDULED)
7099 fprintf(fp, "\tnext repaint: %ld.%09ld\n",
7100 output->next_repaint.tv_sec,
7101 output->next_repaint.tv_nsec);
7102
7103 wl_list_for_each(head, &output->head_list, output_link) {
7104 fprintf(fp, "\tHead %d (%s): %sconnected\n",
7105 head_idx++, head->name,
7106 (head->connected) ? "" : "not ");
7107 }
7108 }
7109
7110 fprintf(fp, "\n");
7111
7112 wl_list_for_each(layer, &ec->layer_list, link) {
7113 struct weston_view *view;
7114 int view_idx = 0;
7115
7116 fprintf(fp, "Layer %d (pos 0x%lx):\n", layer_idx++,
7117 (unsigned long) layer->position);
7118
7119 if (!weston_layer_mask_is_infinite(layer)) {
7120 fprintf(fp, "\t[mask: (%d, %d) -> (%d,%d)]\n\n",
7121 layer->mask.x1, layer->mask.y1,
7122 layer->mask.x2, layer->mask.y2);
7123 }
7124
Marius Vlada6acfa82019-03-17 18:10:09 +02007125 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
7126 debug_scene_view_print_tree(view, fp, &view_idx);
7127 view_idx++;
7128 }
Daniel Stonece62cb32018-07-20 09:46:24 +01007129
7130 if (wl_list_empty(&layer->view_list.link))
7131 fprintf(fp, "\t[no views]\n");
7132
7133 fprintf(fp, "\n");
7134 }
7135
7136 err = fclose(fp);
7137 assert(err == 0);
7138
7139 return ret;
7140}
7141
7142/**
7143 * Called when the 'scene-graph' debug scope is bound by a client. This
7144 * one-shot weston-debug scope prints the current scene graph when bound,
7145 * and then terminates the stream.
7146 */
7147static void
Marius Vladdad882a2019-07-17 15:43:53 +03007148debug_scene_graph_cb(struct weston_log_subscription *sub, void *data)
Daniel Stonece62cb32018-07-20 09:46:24 +01007149{
7150 struct weston_compositor *ec = data;
7151 char *str = weston_compositor_print_scene_graph(ec);
7152
Marius Vladdad882a2019-07-17 15:43:53 +03007153 weston_log_subscription_printf(sub, "%s", str);
Daniel Stonece62cb32018-07-20 09:46:24 +01007154 free(str);
Marius Vladdad882a2019-07-17 15:43:53 +03007155 weston_log_subscription_complete(sub);
Daniel Stonece62cb32018-07-20 09:46:24 +01007156}
7157
Giulio Camuffo459137b2014-10-11 23:56:24 +03007158/** Create the compositor.
7159 *
7160 * This functions creates and initializes a compositor instance.
7161 *
7162 * \param display The Wayland display to be used.
7163 * \param user_data A pointer to an object that can later be retrieved
Marius Vlada2dace22019-06-12 16:05:44 +03007164 * \param log_ctx A pointer to weston_debug_compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007165 * using the \ref weston_compositor_get_user_data function.
7166 * \return The compositor instance on success or NULL on failure.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007167 *
7168 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007169 */
7170WL_EXPORT struct weston_compositor *
Marius Vlad880b4852019-04-07 17:07:58 +03007171weston_compositor_create(struct wl_display *display,
Marius Vlad3d7d9782019-04-17 12:35:38 +03007172 struct weston_log_context *log_ctx,
Marius Vlad880b4852019-04-07 17:07:58 +03007173 void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04007174{
Giulio Camuffo459137b2014-10-11 23:56:24 +03007175 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05007176 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07007177
Giulio Camuffo459137b2014-10-11 23:56:24 +03007178 ec = zalloc(sizeof *ec);
7179 if (!ec)
7180 return NULL;
7181
7182 ec->wl_display = display;
7183 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04007184 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07007185 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04007186 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03007187 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03007188 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02007189 wl_signal_init(&ec->idle_signal);
7190 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02007191 wl_signal_init(&ec->show_input_panel_signal);
7192 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02007193 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01007194 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01007195 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02007196 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02007197 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02007198 wl_signal_init(&ec->output_resized_signal);
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03007199 wl_signal_init(&ec->heads_changed_signal);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02007200 wl_signal_init(&ec->output_heads_changed_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07007201 wl_signal_init(&ec->session_signal);
Robert Beckettc569bdc2019-07-10 16:40:12 +01007202 ec->session_active = true;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04007203
Casey Dahlin58ba1372012-04-19 22:50:08 -04007204 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03007205 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04007206
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02007207 ec->activate_serial = 1;
7208
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05007209 ec->touch_mode = WESTON_TOUCH_MODE_NORMAL;
7210
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05307211 ec->content_protection = NULL;
7212
Derek Foreman152254b2015-11-26 14:17:48 -06007213 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04007214 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007215 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05007216
Giulio Camuffo954f1832014-10-11 18:27:30 +03007217 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04007218 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007219 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03007220
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007221 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007222 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007223 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01007224
Roman Gilge97391c2019-03-29 13:01:06 +01007225 if (!wl_global_create(ec->wl_display, &zxdg_output_manager_v1_interface, 2,
7226 ec, bind_xdg_output_manager))
7227 goto fail;
7228
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007229 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007230 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007231 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007232
Marius Vlad3d7d9782019-04-17 12:35:38 +03007233 if (weston_log_ctx_compositor_setup(ec, log_ctx) < 0)
Pekka Paalanena5630ea2017-10-12 13:13:42 +02007234 goto fail;
7235
Jonas Ådahl30d61d82014-10-22 21:21:17 +02007236 if (weston_input_init(ec) != 0)
7237 goto fail;
7238
Jason Ekstranda7af7042013-10-12 22:38:11 -05007239 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02007240 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007241 wl_list_init(&ec->layer_list);
7242 wl_list_init(&ec->seat_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007243 wl_list_init(&ec->pending_output_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007244 wl_list_init(&ec->output_list);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03007245 wl_list_init(&ec->head_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007246 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01007247 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007248 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01007249 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007250 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02007251 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007252
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007253 wl_list_init(&ec->plugin_api_list);
7254
Xiong Zhang97116532013-10-23 13:58:31 +08007255 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02007256 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04007257
Giulio Camuffo459137b2014-10-11 23:56:24 +03007258 wl_data_device_manager_init(ec->wl_display);
7259
7260 wl_display_init_shm(ec->wl_display);
7261
7262 loop = wl_display_get_event_loop(ec->wl_display);
7263 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Daniel Stone6847b852017-03-01 11:34:08 +00007264 ec->repaint_timer =
7265 wl_event_loop_add_timer(loop, output_repaint_timer_handler,
7266 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007267
Quentin Glidic82681572016-12-17 13:40:51 +01007268 weston_layer_init(&ec->fade_layer, ec);
7269 weston_layer_init(&ec->cursor_layer, ec);
7270
7271 weston_layer_set_position(&ec->fade_layer, WESTON_LAYER_POSITION_FADE);
7272 weston_layer_set_position(&ec->cursor_layer,
7273 WESTON_LAYER_POSITION_CURSOR);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007274
7275 weston_compositor_add_debug_binding(ec, KEY_T,
7276 timeline_key_binding_handler, ec);
7277
Daniel Stonece62cb32018-07-20 09:46:24 +01007278 ec->debug_scene =
Marius Vlad7e4db952019-04-17 13:47:06 +03007279 weston_compositor_add_log_scope(ec->weston_log_ctx, "scene-graph",
7280 "Scene graph details\n",
7281 debug_scene_graph_cb,
7282 ec);
Daniel Stonece62cb32018-07-20 09:46:24 +01007283
Giulio Camuffo459137b2014-10-11 23:56:24 +03007284 return ec;
7285
7286fail:
7287 free(ec);
7288 return NULL;
7289}
7290
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007291/** weston_compositor_shutdown
7292 * \ingroup compositor
7293 */
Benjamin Franzkeb8263022011-08-30 11:32:47 +02007294WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007295weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07007296{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007297 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07007298
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007299 wl_event_source_remove(ec->idle_source);
7300
Matt Roper361d2ad2011-08-29 13:52:23 -07007301 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02007302 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07007303 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02007304
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007305 /* Destroy all pending outputs associated with this compositor */
7306 wl_list_for_each_safe(output, next, &ec->pending_output_list, link)
7307 output->destroy(output);
7308
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02007309 if (ec->renderer)
7310 ec->renderer->destroy(ec);
7311
Daniel Stone325fc2d2012-05-30 16:31:58 +01007312 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09007313 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01007314 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01007315 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01007316 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02007317 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007318
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04007319 weston_plane_release(&ec->primary_plane);
Matt Roper361d2ad2011-08-29 13:52:23 -07007320}
7321
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007322/** weston_compositor_exit_with_code
7323 * \ingroup compositor
7324 */
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05007325WL_EXPORT void
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007326weston_compositor_exit_with_code(struct weston_compositor *compositor,
Frederic Plourdec336f062014-10-29 14:44:33 -04007327 int exit_code)
7328{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02007329 if (compositor->exit_code == EXIT_SUCCESS)
7330 compositor->exit_code = exit_code;
7331
Giulio Camuffo459137b2014-10-11 23:56:24 +03007332 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04007333}
7334
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007335/** weston_compositor_set_default_pointer_grab
7336 * \ingroup compositor
7337 */
Frederic Plourdec336f062014-10-29 14:44:33 -04007338WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007339weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
7340 const struct weston_pointer_grab_interface *interface)
7341{
7342 struct weston_seat *seat;
7343
7344 ec->default_pointer_grab = interface;
7345 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05007346 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
7347
7348 if (pointer)
7349 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007350 }
7351}
7352
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007353/** weston_compositor_set_presentation_clock
7354 * \ingroup compositor
7355 */
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007356WL_EXPORT int
7357weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
7358 clockid_t clk_id)
7359{
7360 struct timespec ts;
7361
7362 if (clock_gettime(clk_id, &ts) < 0)
7363 return -1;
7364
7365 compositor->presentation_clock = clk_id;
7366
7367 return 0;
7368}
7369
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007370/** For choosing the software clock, when the display hardware or API
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007371 * does not expose a compatible presentation timestamp.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007372 *
7373 * \ingroup compositor
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007374 */
7375WL_EXPORT int
7376weston_compositor_set_presentation_clock_software(
7377 struct weston_compositor *compositor)
7378{
7379 /* In order of preference */
7380 static const clockid_t clocks[] = {
7381 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
7382 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
7383 CLOCK_MONOTONIC, /* no jumps, may crawl */
7384 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
7385 CLOCK_REALTIME /* may jump and crawl */
7386 };
7387 unsigned i;
7388
7389 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
7390 if (weston_compositor_set_presentation_clock(compositor,
7391 clocks[i]) == 0)
7392 return 0;
7393
7394 weston_log("Error: no suitable presentation clock available.\n");
7395
7396 return -1;
7397}
7398
Pekka Paalanen662f3842015-03-18 12:17:26 +02007399/** Read the current time from the Presentation clock
7400 *
7401 * \param compositor
Marius Vlada2dace22019-06-12 16:05:44 +03007402 * \param[out] ts The current time.
Pekka Paalanen662f3842015-03-18 12:17:26 +02007403 *
7404 * \note Reading the current time in user space is always imprecise to some
7405 * degree.
7406 *
7407 * This function is never meant to fail. If reading the clock does fail,
7408 * an error message is logged and a zero time is returned. Callers are not
7409 * supposed to detect or react to failures.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007410 *
7411 * \ingroup compositor
Pekka Paalanen662f3842015-03-18 12:17:26 +02007412 */
7413WL_EXPORT void
7414weston_compositor_read_presentation_clock(
7415 const struct weston_compositor *compositor,
7416 struct timespec *ts)
7417{
7418 static bool warned;
7419 int ret;
7420
7421 ret = clock_gettime(compositor->presentation_clock, ts);
7422 if (ret < 0) {
7423 ts->tv_sec = 0;
7424 ts->tv_nsec = 0;
7425
7426 if (!warned)
7427 weston_log("Error: failure to read "
Antonio Borneo39578632019-04-26 23:57:31 +02007428 "the presentation clock %#x: '%s' (%d)\n",
7429 compositor->presentation_clock,
7430 strerror(errno), errno);
Pekka Paalanen662f3842015-03-18 12:17:26 +02007431 warned = true;
7432 }
7433}
7434
Pekka Paalanen230f3b12014-09-29 14:18:40 -04007435/** Import dmabuf buffer into current renderer
7436 *
7437 * \param compositor
7438 * \param buffer the dmabuf buffer to import
7439 * \return true on usable buffers, false otherwise
7440 *
7441 * This function tests that the linux_dmabuf_buffer is usable
7442 * for the current renderer. Returns false on unusable buffers. Usually
7443 * usability is tested by importing the dmabufs for composition.
7444 *
7445 * This hook is also used for detecting if the renderer supports
7446 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
7447 * supported.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007448 *
7449 * \ingroup compositor
7450 */
Pekka Paalanen230f3b12014-09-29 14:18:40 -04007451WL_EXPORT bool
7452weston_compositor_import_dmabuf(struct weston_compositor *compositor,
7453 struct linux_dmabuf_buffer *buffer)
7454{
7455 struct weston_renderer *renderer;
7456
7457 renderer = compositor->renderer;
7458
7459 if (renderer->import_dmabuf == NULL)
7460 return false;
7461
7462 return renderer->import_dmabuf(compositor, buffer);
7463}
7464
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007465WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05007466weston_version(int *major, int *minor, int *micro)
7467{
7468 *major = WESTON_VERSION_MAJOR;
7469 *minor = WESTON_VERSION_MINOR;
7470 *micro = WESTON_VERSION_MICRO;
7471}
7472
Daniel Stonee03c1112016-11-24 20:45:45 +00007473/**
7474 * Attempts to find a module path from the module map specified in the
7475 * environment. If found, writes the full path into the path variable.
7476 *
7477 * The module map is a string in environment variable WESTON_MODULE_MAP, where
7478 * each entry is of the form "name=path" and entries are separated by
7479 * semicolons. Whitespace is significant.
7480 *
7481 * \param name The name to search for.
7482 * \param path Where the path is written to if found.
7483 * \param path_len Allocated bytes at \c path .
7484 * \returns The length of the string written to path on success, or 0 if the
7485 * module was not specified in the environment map or path_len was too small.
7486 */
7487WL_EXPORT size_t
7488weston_module_path_from_env(const char *name, char *path, size_t path_len)
7489{
7490 const char *mapping = getenv("WESTON_MODULE_MAP");
7491 const char *end;
7492 const int name_len = strlen(name);
7493
7494 if (!mapping)
7495 return 0;
7496
7497 end = mapping + strlen(mapping);
7498 while (mapping < end && *mapping) {
7499 const char *filename, *next;
7500
7501 /* early out: impossibly short string */
7502 if (end - mapping < name_len + 1)
7503 return 0;
7504
7505 filename = &mapping[name_len + 1];
7506 next = strchrnul(mapping, ';');
7507
7508 if (strncmp(mapping, name, name_len) == 0 &&
7509 mapping[name_len] == '=') {
7510 size_t file_len = next - filename; /* no trailing NUL */
7511 if (file_len >= path_len)
7512 return 0;
7513 strncpy(path, filename, file_len);
7514 path[file_len] = '\0';
7515 return file_len;
7516 }
7517
7518 mapping = next + 1;
7519 }
7520
7521 return 0;
7522}
7523
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03007524WL_EXPORT void *
7525weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007526{
7527 char path[PATH_MAX];
7528 void *module, *init;
Daniel Stonebeb97e52016-11-28 12:13:54 +00007529 size_t len;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007530
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08007531 if (name == NULL)
7532 return NULL;
7533
Derek Foreman3f86e502015-06-08 11:46:54 -05007534 if (name[0] != '/') {
Daniel Stonee03c1112016-11-24 20:45:45 +00007535 len = weston_module_path_from_env(name, path, sizeof path);
7536 if (len == 0)
Daniel Stonebeb97e52016-11-28 12:13:54 +00007537 len = snprintf(path, sizeof path, "%s/%s",
7538 LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05007539 } else {
Daniel Stonebeb97e52016-11-28 12:13:54 +00007540 len = snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05007541 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007542
Daniel Stonebeb97e52016-11-28 12:13:54 +00007543 /* snprintf returns the length of the string it would've written,
7544 * _excluding_ the NUL byte. So even being equal to the size of
7545 * our buffer is an error here. */
7546 if (len >= sizeof path)
7547 return NULL;
7548
Kristian Høgsberga6813d22012-09-12 12:21:01 -04007549 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
7550 if (module) {
7551 weston_log("Module '%s' already loaded\n", path);
7552 dlclose(module);
7553 return NULL;
7554 }
7555
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03007556 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04007557 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007558 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03007559 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007560 return NULL;
7561 }
7562
7563 init = dlsym(module, entrypoint);
7564 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03007565 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00007566 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007567 return NULL;
7568 }
7569
7570 return init;
7571}
7572
Giulio Camuffo459137b2014-10-11 23:56:24 +03007573
Marius Vlad284d5342019-06-24 12:00:47 +03007574/** Tear down the compositor.
Giulio Camuffo459137b2014-10-11 23:56:24 +03007575 *
Marius Vlad284d5342019-06-24 12:00:47 +03007576 * This function cleans up the compositor state. While the compositor state has
7577 * been cleaned do note that **only** weston_compositor_destroy() can be called
7578 * afterwards, in order to destroy the compositor instance.
Giulio Camuffo459137b2014-10-11 23:56:24 +03007579 *
Marius Vlad284d5342019-06-24 12:00:47 +03007580 * @param compositor The compositor to be tear-down/cleaned.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007581 *
Marius Vlad284d5342019-06-24 12:00:47 +03007582 * @ingroup compositor
7583 * @sa weston_compositor_destroy
Giulio Camuffo459137b2014-10-11 23:56:24 +03007584 */
7585WL_EXPORT void
Marius Vlad284d5342019-06-24 12:00:47 +03007586weston_compositor_tear_down(struct weston_compositor *compositor)
Giulio Camuffo459137b2014-10-11 23:56:24 +03007587{
7588 /* prevent further rendering while shutting down */
7589 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
7590
7591 wl_signal_emit(&compositor->destroy_signal, compositor);
7592
7593 weston_compositor_xkb_destroy(compositor);
7594
Giulio Camuffo2d24e642015-10-03 16:25:15 +03007595 if (compositor->backend)
7596 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007597
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03007598 /* The backend is responsible for destroying the heads. */
7599 assert(wl_list_empty(&compositor->head_list));
7600
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007601 weston_plugin_api_destroy_list(compositor);
7602
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03007603 if (compositor->heads_changed_source)
7604 wl_event_source_remove(compositor->heads_changed_source);
7605
Marius Vlad7e4db952019-04-17 13:47:06 +03007606 weston_compositor_log_scope_destroy(compositor->debug_scene);
Daniel Stonece62cb32018-07-20 09:46:24 +01007607 compositor->debug_scene = NULL;
Marius Vlad284d5342019-06-24 12:00:47 +03007608}
Pekka Paalanena5630ea2017-10-12 13:13:42 +02007609
Marius Vlad284d5342019-06-24 12:00:47 +03007610/** Destroys the compositor.
7611 *
7612 * This function destroys the compositor. **Do not** call this before
7613 * calling weston_compositor_tear_down()
7614 *
7615 * @param compositor The compositor to be destroyed.
7616 * @ingroup compositor
7617 * @sa weston_compositor_tear_down()
7618 */
7619WL_EXPORT void
7620weston_compositor_destroy(struct weston_compositor *compositor)
7621{
Giulio Camuffo459137b2014-10-11 23:56:24 +03007622 free(compositor);
7623}
7624
7625/** Instruct the compositor to exit.
7626 *
7627 * This functions does not directly destroy the compositor object, it merely
7628 * command it to start the tear down process. It is not guaranteed that the
7629 * tear down will happen immediately.
7630 *
7631 * \param compositor The compositor to tear down.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007632 *
7633 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007634 */
7635WL_EXPORT void
7636weston_compositor_exit(struct weston_compositor *compositor)
7637{
7638 compositor->exit(compositor);
7639}
7640
7641/** Return the user data stored in the compositor.
7642 *
7643 * This function returns the user data pointer set with user_data parameter
7644 * to the \ref weston_compositor_create function.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007645 *
7646 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007647 */
7648WL_EXPORT void *
7649weston_compositor_get_user_data(struct weston_compositor *compositor)
7650{
7651 return compositor->user_data;
7652}
Pekka Paalanendd186732016-06-03 14:49:54 +03007653
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007654static const char * const backend_map[] = {
7655 [WESTON_BACKEND_DRM] = "drm-backend.so",
7656 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
7657 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
7658 [WESTON_BACKEND_RDP] = "rdp-backend.so",
7659 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
7660 [WESTON_BACKEND_X11] = "x11-backend.so",
7661};
7662
Pekka Paalanendd186732016-06-03 14:49:54 +03007663/** Load a backend into a weston_compositor
7664 *
7665 * A backend must be loaded to make a weston_compositor work. A backend
7666 * provides input and output capabilities, and determines the renderer to use.
7667 *
7668 * \param compositor A compositor that has not had a backend loaded yet.
7669 * \param backend Name of the backend file.
7670 * \param config_base A pointer to a backend-specific configuration
7671 * structure's 'base' member.
7672 *
7673 * \return 0 on success, or -1 on error.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007674 *
7675 * \ingroup compositor
Pekka Paalanendd186732016-06-03 14:49:54 +03007676 */
7677WL_EXPORT int
7678weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007679 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03007680 struct weston_backend_config *config_base)
7681{
7682 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03007683 struct weston_backend_config *config_base);
7684
Pekka Paalanend7e35112017-08-29 17:04:12 +03007685 if (compositor->backend) {
7686 weston_log("Error: attempt to load a backend when one is already loaded\n");
7687 return -1;
7688 }
7689
Quentin Glidic887c0182016-07-10 11:00:53 +02007690 if (backend >= ARRAY_LENGTH(backend_map))
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007691 return -1;
7692
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01007693 backend_init = weston_load_module(backend_map[backend], "weston_backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03007694 if (!backend_init)
7695 return -1;
7696
Pekka Paalanend7e35112017-08-29 17:04:12 +03007697 if (backend_init(compositor, config_base) < 0) {
7698 compositor->backend = NULL;
7699 return -1;
7700 }
7701
7702 return 0;
Pekka Paalanendd186732016-06-03 14:49:54 +03007703}
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007704
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007705/** weston_compositor_load_xwayland
7706 * \ingroup compositor
7707 */
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007708WL_EXPORT int
7709weston_compositor_load_xwayland(struct weston_compositor *compositor)
7710{
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007711 int (*module_init)(struct weston_compositor *ec);
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007712
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007713 module_init = weston_load_module("xwayland.so", "weston_module_init");
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007714 if (!module_init)
7715 return -1;
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007716 if (module_init(compositor) < 0)
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007717 return -1;
7718 return 0;
7719}
Vasilis Liaskovitis486b4632018-10-10 16:14:55 +02007720
7721/** Resolve an internal compositor error by disconnecting the client.
7722 *
7723 * This function is used in cases when the wl_buffer turns out
7724 * unusable and there is no fallback path.
7725 *
7726 * It is possible the fault is caused by a compositor bug, the underlying
7727 * graphics stack bug or normal behaviour, or perhaps a client mistake.
7728 * In any case, the options are to either composite garbage or nothing,
7729 * or disconnect the client. This is a helper function for the latter.
7730 *
7731 * The error is sent as an INVALID_OBJECT error on the client's wl_display.
7732 *
7733 * \param buffer The weston buffer that is unusable.
7734 * \param msg A custom error message attached to the protocol error.
7735 */
7736WL_EXPORT void
7737weston_buffer_send_server_error(struct weston_buffer *buffer,
7738 const char *msg)
7739{
7740 struct wl_client *client;
7741 struct wl_resource *display_resource;
7742 uint32_t id;
7743
7744 assert(buffer->resource);
7745 id = wl_resource_get_id(buffer->resource);
7746 client = wl_resource_get_client(buffer->resource);
7747 display_resource = wl_client_get_object(client, 1);
7748
7749 assert(display_resource);
7750 wl_resource_post_error(display_resource,
7751 WL_DISPLAY_ERROR_INVALID_OBJECT,
7752 "server error with "
7753 "wl_buffer@%u: %s", id, msg);
7754}