blob: 4817adb28fedf3aea4adb962054fea0557d0d127 [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/**
Ankit Nautiyalfaa5ab42019-07-08 13:23:24 +05301081 * \param compositor weston_compositor
1082 *
1083 * Schedule an idle task to notify surface about the update in protection,
1084 * if not already scheduled.
1085 */
1086static void
1087weston_schedule_surface_protection_update(struct weston_compositor *compositor)
1088{
1089 struct content_protection *cp = compositor->content_protection;
1090 struct wl_event_loop *loop;
1091
1092 if (!cp || cp->surface_protection_update)
1093 return;
1094 loop = wl_display_get_event_loop(compositor->wl_display);
1095 cp->surface_protection_update = wl_event_loop_add_idle(loop,
1096 notify_surface_protection_change,
1097 compositor);
1098}
1099
1100/**
Bryce Harrington3f650b82015-12-23 11:01:58 -08001101 * \param es The surface
1102 * \param mask The new set of outputs for the surface
1103 *
1104 * Sets the surface's set of outputs to the ones specified by
1105 * the new output mask provided. Identifies the outputs that
1106 * have changed, the posts enter and leave events for these
1107 * outputs as appropriate.
1108 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001109static void
1110weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
1111{
1112 uint32_t different = es->output_mask ^ mask;
1113 uint32_t entered = mask & different;
1114 uint32_t left = es->output_mask & different;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001115 uint32_t output_bit;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001116 struct weston_output *output;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001117 struct weston_head *head;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001118
1119 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001120 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001121 return;
1122 if (different == 0)
1123 return;
1124
1125 wl_list_for_each(output, &es->compositor->output_list, link) {
Pekka Paalanen01e00682017-03-24 16:21:06 +02001126 output_bit = 1u << output->id;
1127 if (!(output_bit & different))
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001128 continue;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001129
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001130 wl_list_for_each(head, &output->head_list, output_link) {
1131 weston_surface_send_enter_leave(es, head,
1132 output_bit & entered,
1133 output_bit & left);
1134 }
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001135 }
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301136 /*
1137 * Change in surfaces' output mask might trigger a change in its
1138 * protection.
1139 */
Ankit Nautiyalfaa5ab42019-07-08 13:23:24 +05301140 weston_schedule_surface_protection_update(es->compositor);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001141}
1142
Semi Malinene7a52fb2018-04-26 11:08:10 +02001143static void
1144notify_view_output_destroy(struct wl_listener *listener, void *data)
1145{
1146 struct weston_view *view =
1147 container_of(listener,
1148 struct weston_view, output_destroy_listener);
1149
1150 view->output = NULL;
1151 view->output_destroy_listener.notify = NULL;
1152}
1153
1154/** Set the primary output of the view
1155 *
1156 * \param view The view whose primary output to set
1157 * \param output The new primary output for the view
1158 *
1159 * Set \a output to be the primary output of the \a view.
1160 *
1161 * Notice that the assignment may be temporary; the primary output could be
1162 * automatically changed. Hence, one cannot rely on the value persisting.
1163 *
1164 * Passing NULL as /a output will set the primary output to NULL.
1165 */
1166WL_EXPORT void
1167weston_view_set_output(struct weston_view *view, struct weston_output *output)
1168{
1169 if (view->output_destroy_listener.notify) {
1170 wl_list_remove(&view->output_destroy_listener.link);
1171 view->output_destroy_listener.notify = NULL;
1172 }
1173 view->output = output;
1174 if (output) {
1175 view->output_destroy_listener.notify =
1176 notify_view_output_destroy;
1177 wl_signal_add(&output->destroy_signal,
1178 &view->output_destroy_listener);
1179 }
1180}
1181
Bryce Harrington3f650b82015-12-23 11:01:58 -08001182/** Recalculate which output(s) the surface has views displayed on
1183 *
1184 * \param es The surface to remap to outputs
1185 *
1186 * Finds the output that is showing the largest amount of one
1187 * of the surface's various views. This output becomes the
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001188 * surface's primary output for vsync and frame callback purposes.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001189 *
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001190 * Also notes all outputs of all of the surface's views
Bryce Harrington3f650b82015-12-23 11:01:58 -08001191 * in the output_mask for the surface.
1192 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001193static void
1194weston_surface_assign_output(struct weston_surface *es)
1195{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001196 struct weston_output *new_output;
1197 struct weston_view *view;
1198 pixman_region32_t region;
1199 uint32_t max, area, mask;
1200 pixman_box32_t *e;
1201
1202 new_output = NULL;
1203 max = 0;
1204 mask = 0;
1205 pixman_region32_init(&region);
1206 wl_list_for_each(view, &es->views, surface_link) {
1207 if (!view->output)
1208 continue;
1209
1210 pixman_region32_intersect(&region, &view->transform.boundingbox,
1211 &view->output->region);
1212
1213 e = pixman_region32_extents(&region);
1214 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1215
1216 mask |= view->output_mask;
1217
1218 if (area >= max) {
1219 new_output = view->output;
1220 max = area;
1221 }
1222 }
1223 pixman_region32_fini(&region);
1224
1225 es->output = new_output;
1226 weston_surface_update_output_mask(es, mask);
1227}
1228
Bryce Harrington3f650b82015-12-23 11:01:58 -08001229/** Recalculate which output(s) the view is displayed on
1230 *
1231 * \param ev The view to remap to outputs
1232 *
1233 * Identifies the set of outputs that the view is visible on,
1234 * noting them into the output_mask. The output that the view
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001235 * is most visible on is set as the view's primary output.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001236 *
1237 * Also does the same for the view's surface. See
1238 * weston_surface_assign_output().
1239 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001240static void
1241weston_view_assign_output(struct weston_view *ev)
1242{
1243 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001244 struct weston_output *output, *new_output;
1245 pixman_region32_t region;
1246 uint32_t max, area, mask;
1247 pixman_box32_t *e;
1248
1249 new_output = NULL;
1250 max = 0;
1251 mask = 0;
1252 pixman_region32_init(&region);
1253 wl_list_for_each(output, &ec->output_list, link) {
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03001254 if (output->destroying)
1255 continue;
1256
Jason Ekstranda7af7042013-10-12 22:38:11 -05001257 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001258 &output->region);
1259
1260 e = pixman_region32_extents(&region);
1261 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1262
1263 if (area > 0)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001264 mask |= 1u << output->id;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001265
1266 if (area >= max) {
1267 new_output = output;
1268 max = area;
1269 }
1270 }
1271 pixman_region32_fini(&region);
1272
Semi Malinene7a52fb2018-04-26 11:08:10 +02001273 weston_view_set_output(ev, new_output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001274 ev->output_mask = mask;
1275
1276 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001277}
1278
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001279static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001280weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1281 int from_x, int from_y, int *to_x, int *to_y)
1282{
1283 float x, y;
1284
1285 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1286 weston_view_from_global_float(to, x, y, &x, &y);
1287
1288 *to_x = round(x);
1289 *to_y = round(y);
1290}
1291
1292static void
1293weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1294{
1295 pixman_box32_t *a;
1296 pixman_box32_t b;
1297
1298 a = pixman_region32_extents(&from->geometry.scissor);
1299
1300 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1301 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1302
1303 pixman_region32_fini(&to->geometry.scissor);
1304 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1305}
1306
1307static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001308view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001309 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001310{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001311 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1312 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001313 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001314 { inbox->x1, inbox->y1 },
1315 { inbox->x1, inbox->y2 },
1316 { inbox->x2, inbox->y1 },
1317 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001318 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001319 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001320 int i;
1321
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001322 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001323 /* avoid rounding empty bbox to 1x1 */
1324 pixman_region32_init(bbox);
1325 return;
1326 }
1327
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001328 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001329 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001330 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001331 if (x < min_x)
1332 min_x = x;
1333 if (x > max_x)
1334 max_x = x;
1335 if (y < min_y)
1336 min_y = y;
1337 if (y > max_y)
1338 max_y = y;
1339 }
1340
Pekka Paalanen219b9822012-02-08 15:38:37 +02001341 int_x = floorf(min_x);
1342 int_y = floorf(min_y);
1343 pixman_region32_init_rect(bbox, int_x, int_y,
1344 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001345}
1346
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001347static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001348weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001349{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001350 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001351
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001352 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001353 view->geometry.x = roundf(view->geometry.x);
1354 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001355
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001356 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001357 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1358 view->transform.position.matrix.d[12] = view->geometry.x;
1359 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001360
Jason Ekstranda7af7042013-10-12 22:38:11 -05001361 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001362
Jason Ekstranda7af7042013-10-12 22:38:11 -05001363 view->transform.inverse = view->transform.position.matrix;
1364 view->transform.inverse.d[12] = -view->geometry.x;
1365 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001366
Jason Ekstranda7af7042013-10-12 22:38:11 -05001367 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001368 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001369 view->surface->width,
1370 view->surface->height);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001371 if (view->geometry.scissor_enabled)
1372 pixman_region32_intersect(&view->transform.boundingbox,
1373 &view->transform.boundingbox,
1374 &view->geometry.scissor);
1375
1376 pixman_region32_translate(&view->transform.boundingbox,
1377 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001378
Jason Ekstranda7af7042013-10-12 22:38:11 -05001379 if (view->alpha == 1.0) {
1380 pixman_region32_copy(&view->transform.opaque,
1381 &view->surface->opaque);
1382 pixman_region32_translate(&view->transform.opaque,
1383 view->geometry.x,
1384 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001385 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001386}
1387
1388static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001389weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001390{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001391 struct weston_view *parent = view->geometry.parent;
1392 struct weston_matrix *matrix = &view->transform.matrix;
1393 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001394 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001395 pixman_region32_t surfregion;
1396 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001397
Jason Ekstranda7af7042013-10-12 22:38:11 -05001398 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001399
1400 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001401 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1402 view->transform.position.matrix.d[12] = view->geometry.x;
1403 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001404
1405 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001406 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001407 weston_matrix_multiply(matrix, &tform->matrix);
1408
Pekka Paalanen483243f2013-03-08 14:56:50 +02001409 if (parent)
1410 weston_matrix_multiply(matrix, &parent->transform.matrix);
1411
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001412 if (weston_matrix_invert(inverse, matrix) < 0) {
1413 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001414 weston_log("error: weston_view %p"
1415 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001416 return -1;
1417 }
1418
Giulio Camuffo148c1992016-09-04 18:50:46 +03001419 if (view->alpha == 1.0 &&
1420 matrix->type == WESTON_MATRIX_TRANSFORM_TRANSLATE) {
1421 pixman_region32_copy(&view->transform.opaque,
1422 &view->surface->opaque);
1423 pixman_region32_translate(&view->transform.opaque,
1424 matrix->d[12],
1425 matrix->d[13]);
1426 }
1427
Pekka Paalanen380adf52015-02-16 14:39:11 +02001428 pixman_region32_init_rect(&surfregion, 0, 0,
1429 view->surface->width, view->surface->height);
1430 if (view->geometry.scissor_enabled)
1431 pixman_region32_intersect(&surfregion, &surfregion,
1432 &view->geometry.scissor);
1433 surfbox = pixman_region32_extents(&surfregion);
1434
1435 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1436 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001437
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001438 return 0;
1439}
1440
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001441static struct weston_layer *
1442get_view_layer(struct weston_view *view)
1443{
1444 if (view->parent_view)
1445 return get_view_layer(view->parent_view);
1446 return view->layer_link.layer;
1447}
1448
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001449WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001450weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001451{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001452 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001453 struct weston_layer *layer;
1454 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001455
Jason Ekstranda7af7042013-10-12 22:38:11 -05001456 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001457 return;
1458
Pekka Paalanen483243f2013-03-08 14:56:50 +02001459 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001460 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001461
Jason Ekstranda7af7042013-10-12 22:38:11 -05001462 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001463
Jason Ekstranda7af7042013-10-12 22:38:11 -05001464 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001465
Jason Ekstranda7af7042013-10-12 22:38:11 -05001466 pixman_region32_fini(&view->transform.boundingbox);
1467 pixman_region32_fini(&view->transform.opaque);
1468 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001469
Pekka Paalanencd403622012-01-25 13:37:39 +02001470 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001471 if (view->geometry.transformation_list.next ==
1472 &view->transform.position.link &&
1473 view->geometry.transformation_list.prev ==
1474 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001475 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001476 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001477 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001478 if (weston_view_update_transform_enable(view) < 0)
1479 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001480 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001481
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001482 layer = get_view_layer(view);
1483 if (layer) {
1484 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001485 pixman_region32_intersect(&view->transform.boundingbox,
1486 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001487 pixman_region32_intersect(&view->transform.opaque,
1488 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001489 pixman_region32_fini(&mask);
1490 }
1491
Pekka Paalanen380adf52015-02-16 14:39:11 +02001492 if (parent) {
1493 if (parent->geometry.scissor_enabled) {
1494 view->geometry.scissor_enabled = true;
1495 weston_view_transfer_scissor(parent, view);
1496 } else {
1497 view->geometry.scissor_enabled = false;
1498 }
1499 }
1500
Jason Ekstranda7af7042013-10-12 22:38:11 -05001501 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001502
Jason Ekstranda7af7042013-10-12 22:38:11 -05001503 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001504
Jason Ekstranda7af7042013-10-12 22:38:11 -05001505 wl_signal_emit(&view->surface->compositor->transform_signal,
1506 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001507}
1508
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001509WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001510weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001511{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001512 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001513
1514 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001515 * The invariant: if view->geometry.dirty, then all views
1516 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001517 * Corollary: if not parent->geometry.dirty, then all ancestors
1518 * are not dirty.
1519 */
1520
Jason Ekstranda7af7042013-10-12 22:38:11 -05001521 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001522 return;
1523
Jason Ekstranda7af7042013-10-12 22:38:11 -05001524 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001525
Jason Ekstranda7af7042013-10-12 22:38:11 -05001526 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001527 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001528 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001529}
1530
1531WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001532weston_view_to_global_fixed(struct weston_view *view,
1533 wl_fixed_t vx, wl_fixed_t vy,
1534 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001535{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001536 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001537
Jason Ekstranda7af7042013-10-12 22:38:11 -05001538 weston_view_to_global_float(view,
1539 wl_fixed_to_double(vx),
1540 wl_fixed_to_double(vy),
1541 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001542 *x = wl_fixed_from_double(xf);
1543 *y = wl_fixed_from_double(yf);
1544}
1545
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001546WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001547weston_view_from_global_float(struct weston_view *view,
1548 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001549{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001550 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001551 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1552
Jason Ekstranda7af7042013-10-12 22:38:11 -05001553 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001554
1555 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001556 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001557 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001558 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001559 *vx = 0;
1560 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001561 return;
1562 }
1563
Jason Ekstranda7af7042013-10-12 22:38:11 -05001564 *vx = v.f[0] / v.f[3];
1565 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001566 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001567 *vx = x - view->geometry.x;
1568 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001569 }
1570}
1571
1572WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001573weston_view_from_global_fixed(struct weston_view *view,
1574 wl_fixed_t x, wl_fixed_t y,
1575 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001576{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001577 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001578
Jason Ekstranda7af7042013-10-12 22:38:11 -05001579 weston_view_from_global_float(view,
1580 wl_fixed_to_double(x),
1581 wl_fixed_to_double(y),
1582 &vxf, &vyf);
1583 *vx = wl_fixed_from_double(vxf);
1584 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001585}
1586
1587WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001588weston_view_from_global(struct weston_view *view,
1589 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001590{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001591 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001592
Jason Ekstranda7af7042013-10-12 22:38:11 -05001593 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1594 *vx = floorf(vxf);
1595 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001596}
1597
Bryce Harrington3f650b82015-12-23 11:01:58 -08001598/**
1599 * \param surface The surface to be repainted
1600 *
1601 * Marks the output(s) that the surface is shown on as needing to be
1602 * repainted. See weston_output_schedule_repaint().
1603 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001604WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001605weston_surface_schedule_repaint(struct weston_surface *surface)
1606{
1607 struct weston_output *output;
1608
1609 wl_list_for_each(output, &surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001610 if (surface->output_mask & (1u << output->id))
Kristian Høgsberg98238702012-08-03 16:29:12 -04001611 weston_output_schedule_repaint(output);
1612}
1613
Bryce Harrington3f650b82015-12-23 11:01:58 -08001614/**
1615 * \param view The view to be repainted
1616 *
1617 * Marks the output(s) that the view is shown on as needing to be
1618 * repainted. See weston_output_schedule_repaint().
1619 */
Kristian Høgsberg98238702012-08-03 16:29:12 -04001620WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001621weston_view_schedule_repaint(struct weston_view *view)
1622{
1623 struct weston_output *output;
1624
1625 wl_list_for_each(output, &view->surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001626 if (view->output_mask & (1u << output->id))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001627 weston_output_schedule_repaint(output);
1628}
1629
Pekka Paalanene508ce62015-02-19 13:59:55 +02001630/**
1631 * XXX: This function does it the wrong way.
1632 * surface->damage is the damage from the client, and causes
1633 * surface_flush_damage() to copy pixels. No window management action can
1634 * cause damage to the client-provided content, warranting re-upload!
1635 *
1636 * Instead of surface->damage, this function should record the damage
1637 * with all the views for this surface to avoid extraneous texture
1638 * uploads.
1639 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001640WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001641weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001642{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001643 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001644 0, 0, surface->width,
1645 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001646
Kristian Høgsberg98238702012-08-03 16:29:12 -04001647 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001648}
1649
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001650WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001651weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001652{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001653 if (view->geometry.x == x && view->geometry.y == y)
1654 return;
1655
Jason Ekstranda7af7042013-10-12 22:38:11 -05001656 view->geometry.x = x;
1657 view->geometry.y = y;
1658 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001659}
1660
Pekka Paalanen483243f2013-03-08 14:56:50 +02001661static void
1662transform_parent_handle_parent_destroy(struct wl_listener *listener,
1663 void *data)
1664{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001665 struct weston_view *view =
1666 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001667 geometry.parent_destroy_listener);
1668
Jason Ekstranda7af7042013-10-12 22:38:11 -05001669 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001670}
1671
1672WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001673weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001674 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001675{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001676 if (view->geometry.parent) {
1677 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1678 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001679
1680 if (!parent)
1681 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001682 }
1683
Jason Ekstranda7af7042013-10-12 22:38:11 -05001684 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001685
Jason Ekstranda7af7042013-10-12 22:38:11 -05001686 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001687 transform_parent_handle_parent_destroy;
1688 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001689 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001690 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001691 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001692 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001693 }
1694
Jason Ekstranda7af7042013-10-12 22:38:11 -05001695 weston_view_geometry_dirty(view);
1696}
1697
Pekka Paalanen380adf52015-02-16 14:39:11 +02001698/** Set a clip mask rectangle on a view
1699 *
1700 * \param view The view to set the clip mask on.
1701 * \param x Top-left corner X coordinate of the clip rectangle.
1702 * \param y Top-left corner Y coordinate of the clip rectangle.
1703 * \param width Width of the clip rectangle, non-negative.
1704 * \param height Height of the clip rectangle, non-negative.
1705 *
1706 * A shell may set a clip mask rectangle on a view. Everything outside
1707 * the rectangle is cut away for input and output purposes: it is
1708 * not drawn and cannot be hit by hit-test based input like pointer
1709 * motion or touch-downs. Everything inside the rectangle will behave
1710 * normally. Clients are unaware of clipping.
1711 *
Yong Bakos4c72e292016-04-28 11:59:10 -05001712 * The rectangle is set in surface-local coordinates. Setting a clip
Pekka Paalanen380adf52015-02-16 14:39:11 +02001713 * mask rectangle does not affect the view position, the view is positioned
1714 * as it would be without a clip. The clip also does not change
1715 * weston_surface::width,height.
1716 *
1717 * The clip mask rectangle is part of transformation inheritance
1718 * (weston_view_set_transform_parent()). A clip set in the root of the
1719 * transformation inheritance tree will affect all views in the tree.
1720 * A clip can be set only on the root view. Attempting to set a clip
1721 * on view that has a transformation parent will fail. Assigning a parent
1722 * to a view that has a clip set will cause the clip to be forgotten.
1723 *
1724 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1725 * on the additional transformations in the child views. These transformations
1726 * may not rotate the coordinate axes, i.e., only translation and scaling
1727 * are allowed. Violating this restriction causes the clipping to malfunction.
1728 * Furthermore, using scaling may cause rounding errors in child clipping.
1729 *
1730 * The clip mask rectangle is not automatically adjusted based on
1731 * wl_surface.attach dx and dy arguments.
1732 *
1733 * A clip mask rectangle can be set only if the compositor capability
1734 * WESTON_CAP_VIEW_CLIP_MASK is present.
1735 *
1736 * This function sets the clip mask rectangle and schedules a repaint for
1737 * the view.
1738 */
1739WL_EXPORT void
1740weston_view_set_mask(struct weston_view *view,
1741 int x, int y, int width, int height)
1742{
1743 struct weston_compositor *compositor = view->surface->compositor;
1744
1745 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1746 weston_log("%s not allowed without capability!\n", __func__);
1747 return;
1748 }
1749
1750 if (view->geometry.parent) {
1751 weston_log("view %p has a parent, clip forbidden!\n", view);
1752 return;
1753 }
1754
1755 if (width < 0 || height < 0) {
1756 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1757 x, y, width, height);
1758 return;
1759 }
1760
1761 pixman_region32_fini(&view->geometry.scissor);
1762 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1763 view->geometry.scissor_enabled = true;
1764 weston_view_geometry_dirty(view);
1765 weston_view_schedule_repaint(view);
1766}
1767
1768/** Remove the clip mask from a view
1769 *
1770 * \param view The view to remove the clip mask from.
1771 *
1772 * Removed the clip mask rectangle and schedules a repaint.
1773 *
1774 * \sa weston_view_set_mask
1775 */
1776WL_EXPORT void
1777weston_view_set_mask_infinite(struct weston_view *view)
1778{
1779 view->geometry.scissor_enabled = false;
1780 weston_view_geometry_dirty(view);
1781 weston_view_schedule_repaint(view);
1782}
1783
Armin Krezović0da12b82016-06-30 06:04:33 +02001784/* Check if view should be displayed
1785 *
1786 * The indicator is set manually when assigning
1787 * a view to a surface.
1788 *
1789 * This needs reworking. See the thread starting at:
1790 *
1791 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1792 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001793WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001794weston_view_is_mapped(struct weston_view *view)
1795{
Armin Krezović0da12b82016-06-30 06:04:33 +02001796 return view->is_mapped;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001797}
1798
Philipp Zabel70decd52018-09-03 20:11:15 +02001799/* Check if view is opaque in specified region
1800 *
1801 * \param view The view to check for opacity.
1802 * \param region The region to check for opacity, in view coordinates.
1803 *
1804 * Returns true if the view is opaque in the specified region, because view
1805 * alpha is 1.0 and either the opaque region set by the client contains the
1806 * specified region, or the buffer pixel format or solid color is opaque.
1807 */
1808WL_EXPORT bool
1809weston_view_is_opaque(struct weston_view *ev, pixman_region32_t *region)
1810{
1811 pixman_region32_t r;
1812 bool ret = false;
1813
1814 if (ev->alpha < 1.0)
1815 return false;
1816
1817 if (ev->surface->is_opaque)
1818 return true;
1819
1820 if (ev->transform.dirty) {
1821 weston_log("%s: transform dirty", __func__);
1822 return false;
1823 }
1824
1825 pixman_region32_init(&r);
1826 pixman_region32_subtract(&r, region, &ev->transform.opaque);
1827
1828 if (!pixman_region32_not_empty(&r))
1829 ret = true;
1830
1831 pixman_region32_fini(&r);
1832
1833 return ret;
1834}
1835
Armin Krezović0da12b82016-06-30 06:04:33 +02001836/* Check if a surface has a view assigned to it
1837 *
1838 * The indicator is set manually when mapping
1839 * a surface and creating a view for it.
1840 *
1841 * This needs to go. See the thread starting at:
1842 *
1843 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1844 *
1845 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001846WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001847weston_surface_is_mapped(struct weston_surface *surface)
1848{
Armin Krezović0da12b82016-06-30 06:04:33 +02001849 return surface->is_mapped;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001850}
1851
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001852static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001853surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001854{
1855 struct weston_view *view;
1856
1857 if (surface->width == width && surface->height == height)
1858 return;
1859
1860 surface->width = width;
1861 surface->height = height;
1862
1863 wl_list_for_each(view, &surface->views, surface_link)
1864 weston_view_geometry_dirty(view);
1865}
1866
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001867WL_EXPORT void
1868weston_surface_set_size(struct weston_surface *surface,
1869 int32_t width, int32_t height)
1870{
1871 assert(!surface->resource);
1872 surface_set_size(surface, width, height);
1873}
1874
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001875static int
1876fixed_round_up_to_int(wl_fixed_t f)
1877{
1878 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1879}
1880
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001881static void
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001882convert_size_by_transform_scale(int32_t *width_out, int32_t *height_out,
1883 int32_t width, int32_t height,
1884 uint32_t transform,
1885 int32_t scale)
1886{
1887 assert(scale > 0);
1888
1889 switch (transform) {
1890 case WL_OUTPUT_TRANSFORM_NORMAL:
1891 case WL_OUTPUT_TRANSFORM_180:
1892 case WL_OUTPUT_TRANSFORM_FLIPPED:
1893 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1894 *width_out = width / scale;
1895 *height_out = height / scale;
1896 break;
1897 case WL_OUTPUT_TRANSFORM_90:
1898 case WL_OUTPUT_TRANSFORM_270:
1899 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1900 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1901 *width_out = height / scale;
1902 *height_out = width / scale;
1903 break;
1904 default:
1905 assert(0 && "invalid transform");
1906 }
1907}
1908
1909static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001910weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001911{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001912 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001913
1914 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001915 surface->width_from_buffer = 0;
1916 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001917 return;
1918 }
1919
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001920 convert_size_by_transform_scale(&surface->width_from_buffer,
1921 &surface->height_from_buffer,
1922 surface->buffer_ref.buffer->width,
1923 surface->buffer_ref.buffer->height,
1924 vp->buffer.transform,
1925 vp->buffer.scale);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001926}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001927
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001928static void
1929weston_surface_update_size(struct weston_surface *surface)
1930{
1931 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1932 int32_t width, height;
1933
1934 width = surface->width_from_buffer;
1935 height = surface->height_from_buffer;
1936
1937 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001938 surface_set_size(surface,
1939 vp->surface.width, vp->surface.height);
1940 return;
1941 }
1942
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001943 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001944 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1945 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1946
1947 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001948 return;
1949 }
1950
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001951 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001952}
1953
Marius Vlad9fdda7f2019-06-11 16:08:55 +03001954/** weston_compositor_get_time
1955 * \ingroup compositor
1956 */
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02001957WL_EXPORT void
1958weston_compositor_get_time(struct timespec *time)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001959{
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02001960 clock_gettime(CLOCK_REALTIME, time);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001961}
1962
Marius Vlad9fdda7f2019-06-11 16:08:55 +03001963/** weston_compositor_pick_view
1964 * \ingroup compositor
1965 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001966WL_EXPORT struct weston_view *
1967weston_compositor_pick_view(struct weston_compositor *compositor,
1968 wl_fixed_t x, wl_fixed_t y,
1969 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001970{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001971 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001972 wl_fixed_t view_x, view_y;
1973 int view_ix, view_iy;
1974 int ix = wl_fixed_to_int(x);
1975 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001976
Jason Ekstranda7af7042013-10-12 22:38:11 -05001977 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001978 if (!pixman_region32_contains_point(
1979 &view->transform.boundingbox, ix, iy, NULL))
1980 continue;
1981
1982 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
1983 view_ix = wl_fixed_to_int(view_x);
1984 view_iy = wl_fixed_to_int(view_y);
1985
1986 if (!pixman_region32_contains_point(&view->surface->input,
1987 view_ix, view_iy, NULL))
1988 continue;
1989
Pekka Paalanen380adf52015-02-16 14:39:11 +02001990 if (view->geometry.scissor_enabled &&
1991 !pixman_region32_contains_point(&view->geometry.scissor,
1992 view_ix, view_iy, NULL))
1993 continue;
1994
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001995 *vx = view_x;
1996 *vy = view_y;
1997 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001998 }
1999
Derek Foremanf9318d12015-05-11 15:40:11 -05002000 *vx = wl_fixed_from_int(-1000000);
2001 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02002002 return NULL;
2003}
2004
2005static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002006weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002007{
Daniel Stone37816df2012-05-16 18:45:18 +01002008 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002009
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07002010 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002011 return;
2012
Daniel Stone37816df2012-05-16 18:45:18 +01002013 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04002014 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002015}
2016
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002017WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002018weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05002019{
Daniel Stone4dab5db2012-05-30 16:31:53 +01002020 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002021
Jason Ekstranda7af7042013-10-12 22:38:11 -05002022 if (!weston_view_is_mapped(view))
2023 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002024
Jason Ekstranda7af7042013-10-12 22:38:11 -05002025 weston_view_damage_below(view);
Semi Malinene7a52fb2018-04-26 11:08:10 +02002026 weston_view_set_output(view, NULL);
Xiong Zhang97116532013-10-23 13:58:31 +08002027 view->plane = NULL;
Armin Krezovićf8486c32016-06-30 06:04:28 +02002028 view->is_mapped = false;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002029 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002030 wl_list_remove(&view->link);
2031 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002032 view->output_mask = 0;
2033 weston_surface_assign_output(view->surface);
2034
2035 if (weston_surface_is_mapped(view->surface))
2036 return;
2037
2038 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002039 struct weston_touch *touch = weston_seat_get_touch(seat);
2040 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2041 struct weston_keyboard *keyboard =
2042 weston_seat_get_keyboard(seat);
2043
2044 if (keyboard && keyboard->focus == view->surface)
2045 weston_keyboard_set_focus(keyboard, NULL);
2046 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05002047 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05002048 if (touch && touch->focus == view)
2049 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01002050 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002051}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002052
Jason Ekstranda7af7042013-10-12 22:38:11 -05002053WL_EXPORT void
2054weston_surface_unmap(struct weston_surface *surface)
2055{
2056 struct weston_view *view;
2057
Armin Krezovićf8486c32016-06-30 06:04:28 +02002058 surface->is_mapped = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002059 wl_list_for_each(view, &surface->views, surface_link)
2060 weston_view_unmap(view);
2061 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05002062}
2063
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002064static void
2065weston_surface_reset_pending_buffer(struct weston_surface *surface)
2066{
Jason Ekstrand7b982072014-05-20 14:33:03 -05002067 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002068 surface->pending.sx = 0;
2069 surface->pending.sy = 0;
2070 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002071 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002072}
2073
Jason Ekstranda7af7042013-10-12 22:38:11 -05002074WL_EXPORT void
2075weston_view_destroy(struct weston_view *view)
2076{
2077 wl_signal_emit(&view->destroy_signal, view);
2078
2079 assert(wl_list_empty(&view->geometry.child_list));
2080
2081 if (weston_view_is_mapped(view)) {
2082 weston_view_unmap(view);
2083 weston_compositor_build_view_list(view->surface->compositor);
2084 }
2085
2086 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002087 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002088
2089 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02002090 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002091 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002092 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002093
2094 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen944fae82018-05-22 13:15:58 +03002095 weston_view_set_output(view, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002096
Jason Ekstranda7af7042013-10-12 22:38:11 -05002097 wl_list_remove(&view->surface_link);
2098
2099 free(view);
2100}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002101
2102WL_EXPORT void
2103weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05002104{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002105 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002106 struct weston_view *ev, *nv;
Jonas Ådahld3414f22016-07-22 17:56:31 +08002107 struct weston_pointer_constraint *constraint, *next_constraint;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04002108
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02002109 if (--surface->ref_count > 0)
2110 return;
2111
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03002112 assert(surface->resource == NULL);
2113
Pekka Paalanenca790762015-04-17 14:23:38 +03002114 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02002115
Pekka Paalanene67858b2013-04-25 13:57:42 +03002116 assert(wl_list_empty(&surface->subsurface_list_pending));
2117 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02002118
Jason Ekstranda7af7042013-10-12 22:38:11 -05002119 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
2120 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002121
Jason Ekstrand7b982072014-05-20 14:33:03 -05002122 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002123
Pekka Paalanende685b82012-12-04 15:58:12 +02002124 weston_buffer_reference(&surface->buffer_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002125 weston_buffer_release_reference(&surface->buffer_release_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04002126
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02002127 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002128 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002129 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02002130
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002131 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002132 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002133
Pekka Paalanen133e4392014-09-23 22:08:46 -04002134 weston_presentation_feedback_discard_list(&surface->feedback_list);
2135
Jonas Ådahld3414f22016-07-22 17:56:31 +08002136 wl_list_for_each_safe(constraint, next_constraint,
2137 &surface->pointer_constraints,
2138 link)
2139 weston_pointer_constraint_destroy(constraint);
2140
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03002141 fd_clear(&surface->acquire_fence_fd);
2142
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04002143 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05002144}
2145
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002146static void
2147destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08002148{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002149 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08002150
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03002151 assert(surface);
2152
Giulio Camuffo0d379742013-11-15 22:06:15 +01002153 /* Set the resource to NULL, since we don't want to leave a
2154 * dangling pointer if the surface was refcounted and survives
2155 * the weston_surface_destroy() call. */
2156 surface->resource = NULL;
Pekka Paalanen4826f872016-04-22 14:14:38 +03002157
2158 if (surface->viewport_resource)
2159 wl_resource_set_user_data(surface->viewport_resource, NULL);
2160
Alexandros Frantzis27d7c392018-10-19 12:14:11 +03002161 if (surface->synchronization_resource) {
2162 wl_resource_set_user_data(surface->synchronization_resource,
2163 NULL);
2164 }
2165
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002166 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08002167}
2168
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002169static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002170weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
2171{
2172 struct weston_buffer *buffer =
2173 container_of(listener, struct weston_buffer, destroy_listener);
2174
2175 wl_signal_emit(&buffer->destroy_signal, buffer);
2176 free(buffer);
2177}
2178
Giulio Camuffoe058cd12013-12-12 14:14:29 +01002179WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002180weston_buffer_from_resource(struct wl_resource *resource)
2181{
2182 struct weston_buffer *buffer;
2183 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002184
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002185 listener = wl_resource_get_destroy_listener(resource,
2186 weston_buffer_destroy_handler);
2187
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002188 if (listener)
2189 return container_of(listener, struct weston_buffer,
2190 destroy_listener);
2191
2192 buffer = zalloc(sizeof *buffer);
2193 if (buffer == NULL)
2194 return NULL;
2195
2196 buffer->resource = resource;
2197 wl_signal_init(&buffer->destroy_signal);
2198 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04002199 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002200 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002201
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002202 return buffer;
2203}
2204
2205static void
Pekka Paalanende685b82012-12-04 15:58:12 +02002206weston_buffer_reference_handle_destroy(struct wl_listener *listener,
2207 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002208{
Pekka Paalanende685b82012-12-04 15:58:12 +02002209 struct weston_buffer_reference *ref =
2210 container_of(listener, struct weston_buffer_reference,
2211 destroy_listener);
2212
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002213 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02002214 ref->buffer = NULL;
2215}
2216
2217WL_EXPORT void
2218weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002219 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002220{
2221 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05002222 ref->buffer->busy_count--;
2223 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002224 assert(wl_resource_get_client(ref->buffer->resource));
Matt Hoosier3052bc72017-09-26 08:09:40 -05002225 wl_buffer_send_release(ref->buffer->resource);
Kristian Høgsberg20347802013-03-04 12:07:46 -05002226 }
Pekka Paalanende685b82012-12-04 15:58:12 +02002227 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002228 }
2229
Pekka Paalanende685b82012-12-04 15:58:12 +02002230 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002231 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002232 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02002233 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02002234 }
2235
Pekka Paalanende685b82012-12-04 15:58:12 +02002236 ref->buffer = buffer;
2237 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
2238}
2239
2240static void
Alexandros Frantzis67629672018-10-19 12:14:11 +03002241weston_buffer_release_reference_handle_destroy(struct wl_listener *listener,
2242 void *data)
2243{
2244 struct weston_buffer_release_reference *ref =
2245 container_of(listener, struct weston_buffer_release_reference,
2246 destroy_listener);
2247
2248 assert((struct wl_resource *)data == ref->buffer_release->resource);
2249 ref->buffer_release = NULL;
2250}
2251
2252static void
2253weston_buffer_release_destroy(struct weston_buffer_release *buffer_release)
2254{
2255 struct wl_resource *resource = buffer_release->resource;
2256 int release_fence_fd = buffer_release->fence_fd;
2257
2258 if (release_fence_fd >= 0) {
2259 zwp_linux_buffer_release_v1_send_fenced_release(
2260 resource, release_fence_fd);
2261 } else {
2262 zwp_linux_buffer_release_v1_send_immediate_release(
2263 resource);
2264 }
2265
2266 wl_resource_destroy(resource);
2267}
2268
2269WL_EXPORT void
2270weston_buffer_release_reference(struct weston_buffer_release_reference *ref,
2271 struct weston_buffer_release *buffer_release)
2272{
2273 if (buffer_release == ref->buffer_release)
2274 return;
2275
2276 if (ref->buffer_release) {
2277 ref->buffer_release->ref_count--;
2278 wl_list_remove(&ref->destroy_listener.link);
2279 if (ref->buffer_release->ref_count == 0)
2280 weston_buffer_release_destroy(ref->buffer_release);
2281 }
2282
2283 if (buffer_release) {
2284 buffer_release->ref_count++;
2285 wl_resource_add_destroy_listener(buffer_release->resource,
2286 &ref->destroy_listener);
2287 }
2288
2289 ref->buffer_release = buffer_release;
2290 ref->destroy_listener.notify =
2291 weston_buffer_release_reference_handle_destroy;
2292}
2293
2294WL_EXPORT void
2295weston_buffer_release_move(struct weston_buffer_release_reference *dest,
2296 struct weston_buffer_release_reference *src)
2297{
2298 weston_buffer_release_reference(dest, src->buffer_release);
2299 weston_buffer_release_reference(src, NULL);
2300}
2301
2302static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002303weston_surface_attach(struct weston_surface *surface,
2304 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002305{
2306 weston_buffer_reference(&surface->buffer_ref, buffer);
2307
Pekka Paalanena6421c42012-12-04 15:58:10 +02002308 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002309 if (weston_surface_is_mapped(surface))
2310 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002311 }
2312
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002313 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02002314
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002315 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002316 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002317}
2318
Marius Vlad9fdda7f2019-06-11 16:08:55 +03002319/** weston_compositor_damage_all
2320 * \ingroup compositor
2321 */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002322WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002323weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002324{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002325 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002326
2327 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002328 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002329}
2330
Marius Vlad55d87362019-06-11 01:15:35 +03002331/**
2332 * \ingroup output
2333 */
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05002334WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002335weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002336{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002337 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002338
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002339 pixman_region32_union(&compositor->primary_plane.damage,
2340 &compositor->primary_plane.damage,
2341 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002342 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002343}
2344
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04002345static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002346surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002347{
Pekka Paalanende685b82012-12-04 15:58:12 +02002348 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002349 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002350 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002351
Pekka Paalanenb5026542014-11-12 15:09:24 +02002352 if (weston_timeline_enabled_ &&
2353 pixman_region32_not_empty(&surface->damage))
2354 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
2355 TLP_OUTPUT(surface->output), TLP_END);
2356
Jason Ekstrandef540082014-06-26 10:37:36 -07002357 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002358}
2359
2360static void
2361view_accumulate_damage(struct weston_view *view,
2362 pixman_region32_t *opaque)
2363{
2364 pixman_region32_t damage;
2365
2366 pixman_region32_init(&damage);
2367 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002368 pixman_box32_t *extents;
2369
Jason Ekstranda7af7042013-10-12 22:38:11 -05002370 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002371 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002372 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002373 pixman_region32_copy(&damage, &view->surface->damage);
2374 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002375 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002376 }
2377
Pekka Paalanen380adf52015-02-16 14:39:11 +02002378 pixman_region32_intersect(&damage, &damage,
2379 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002380 pixman_region32_subtract(&damage, &damage, opaque);
2381 pixman_region32_union(&view->plane->damage,
2382 &view->plane->damage, &damage);
2383 pixman_region32_fini(&damage);
2384 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002385 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002386}
2387
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002388static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002389compositor_accumulate_damage(struct weston_compositor *ec)
2390{
2391 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002392 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002393 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002394
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002395 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002396
2397 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002398 pixman_region32_copy(&plane->clip, &clip);
2399
2400 pixman_region32_init(&opaque);
2401
Jason Ekstranda7af7042013-10-12 22:38:11 -05002402 wl_list_for_each(ev, &ec->view_list, link) {
2403 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002404 continue;
2405
Jason Ekstranda7af7042013-10-12 22:38:11 -05002406 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002407 }
2408
2409 pixman_region32_union(&clip, &clip, &opaque);
2410 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002411 }
2412
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002413 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002414
Jason Ekstranda7af7042013-10-12 22:38:11 -05002415 wl_list_for_each(ev, &ec->view_list, link)
Derek Foreman060cf112015-11-18 16:32:26 -06002416 ev->surface->touched = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002417
2418 wl_list_for_each(ev, &ec->view_list, link) {
2419 if (ev->surface->touched)
2420 continue;
Derek Foreman060cf112015-11-18 16:32:26 -06002421 ev->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002422
2423 surface_flush_damage(ev->surface);
2424
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002425 /* Both the renderer and the backend have seen the buffer
2426 * by now. If renderer needs the buffer, it has its own
2427 * reference set. If the backend wants to keep the buffer
2428 * around for migrating the surface into a non-primary plane
2429 * later, keep_buffer is true. Otherwise, drop the core
2430 * reference now, and allow early buffer release. This enables
2431 * clients to use single-buffering.
2432 */
Alexandros Frantzis67629672018-10-19 12:14:11 +03002433 if (!ev->surface->keep_buffer) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002434 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002435 weston_buffer_release_reference(
2436 &ev->surface->buffer_release_ref, NULL);
2437 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002438 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002439}
2440
2441static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002442surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002443{
2444 struct weston_subsurface *sub;
2445
Pekka Paalanene67858b2013-04-25 13:57:42 +03002446 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002447 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002448 continue;
2449
Jason Ekstranda7af7042013-10-12 22:38:11 -05002450 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2451 wl_list_init(&sub->surface->views);
2452
2453 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002454 }
2455}
2456
2457static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002458surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002459{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002460 struct weston_subsurface *sub;
2461 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002462
Jason Ekstranda7af7042013-10-12 22:38:11 -05002463 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2464 if (sub->surface == surface)
2465 continue;
2466
George Kiagiadakised04d382014-06-13 18:10:26 +02002467 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2468 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002469 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002470 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002471
2472 surface_free_unused_subsurface_views(sub->surface);
2473 }
2474}
2475
2476static void
2477view_list_add_subsurface_view(struct weston_compositor *compositor,
2478 struct weston_subsurface *sub,
2479 struct weston_view *parent)
2480{
2481 struct weston_subsurface *child;
2482 struct weston_view *view = NULL, *iv;
2483
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002484 if (!weston_surface_is_mapped(sub->surface))
2485 return;
2486
Jason Ekstranda7af7042013-10-12 22:38:11 -05002487 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2488 if (iv->geometry.parent == parent) {
2489 view = iv;
2490 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002491 }
2492 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002493
2494 if (view) {
2495 /* Put it back in the surface's list of views */
2496 wl_list_remove(&view->surface_link);
2497 wl_list_insert(&sub->surface->views, &view->surface_link);
2498 } else {
2499 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002500 weston_view_set_position(view,
2501 sub->position.x,
2502 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002503 weston_view_set_transform_parent(view, parent);
2504 }
2505
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002506 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002507 weston_view_update_transform(view);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002508 view->is_mapped = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002509
Pekka Paalanenb188e912013-11-19 14:03:35 +02002510 if (wl_list_empty(&sub->surface->subsurface_list)) {
2511 wl_list_insert(compositor->view_list.prev, &view->link);
2512 return;
2513 }
2514
2515 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2516 if (child->surface == sub->surface)
2517 wl_list_insert(compositor->view_list.prev, &view->link);
2518 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002519 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002520 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002521}
2522
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002523/* This recursively adds the sub-surfaces for a view, relying on the
2524 * sub-surface order. Thus, if a client restacks the sub-surfaces, that
2525 * change first happens to the sub-surface list, and then automatically
2526 * propagates here. See weston_surface_damage_subsurfaces() for how the
2527 * sub-surfaces receive damage when the client changes the state.
2528 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002529static void
2530view_list_add(struct weston_compositor *compositor,
2531 struct weston_view *view)
2532{
2533 struct weston_subsurface *sub;
2534
2535 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002536
Pekka Paalanenb188e912013-11-19 14:03:35 +02002537 if (wl_list_empty(&view->surface->subsurface_list)) {
2538 wl_list_insert(compositor->view_list.prev, &view->link);
2539 return;
2540 }
2541
2542 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2543 if (sub->surface == view->surface)
2544 wl_list_insert(compositor->view_list.prev, &view->link);
2545 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002546 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002547 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002548}
2549
2550static void
2551weston_compositor_build_view_list(struct weston_compositor *compositor)
2552{
2553 struct weston_view *view;
2554 struct weston_layer *layer;
2555
2556 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002557 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002558 surface_stash_subsurface_views(view->surface);
2559
2560 wl_list_init(&compositor->view_list);
2561 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002562 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002563 view_list_add(compositor, view);
2564 }
2565 }
2566
2567 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002568 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002569 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002570}
2571
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002572static void
2573weston_output_take_feedback_list(struct weston_output *output,
2574 struct weston_surface *surface)
2575{
2576 struct weston_view *view;
2577 struct weston_presentation_feedback *feedback;
2578 uint32_t flags = 0xffffffff;
2579
2580 if (wl_list_empty(&surface->feedback_list))
2581 return;
2582
2583 /* All views must have the flag for the flag to survive. */
2584 wl_list_for_each(view, &surface->views, surface_link) {
2585 /* ignore views that are not on this output at all */
2586 if (view->output_mask & (1u << output->id))
2587 flags &= view->psf_flags;
2588 }
2589
2590 wl_list_for_each(feedback, &surface->feedback_list, link)
2591 feedback->psf_flags = flags;
2592
2593 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2594 wl_list_init(&surface->feedback_list);
2595}
2596
David Herrmann1edf44c2013-10-22 17:11:26 +02002597static int
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002598weston_output_repaint(struct weston_output *output, void *repaint_data)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002599{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002600 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002601 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002602 struct weston_animation *animation, *next;
2603 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002604 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002605 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002606 int r;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002607 uint32_t frame_time_msec;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05302608 enum weston_hdcp_protection highest_requested = WESTON_HDCP_DISABLE;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002609
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002610 if (output->destroying)
2611 return 0;
2612
Pekka Paalanenb5026542014-11-12 15:09:24 +02002613 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2614
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002615 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002616 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002617
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05302618 /* Find the highest protection desired for an output */
2619 wl_list_for_each(ev, &ec->view_list, link) {
2620 if (ev->surface->output_mask & (1u << output->id)) {
2621 /*
2622 * The desired_protection of the output should be the
2623 * maximum of the desired_protection of the surfaces,
2624 * that are displayed on that output, to avoid
2625 * reducing the protection for existing surfaces.
2626 */
2627 if (ev->surface->desired_protection > highest_requested)
2628 highest_requested =
2629 ev->surface->desired_protection;
2630 }
2631 }
2632
2633 output->desired_protection = highest_requested;
2634
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002635 if (output->assign_planes && !output->disable_planes) {
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002636 output->assign_planes(output, repaint_data);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002637 } else {
2638 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002639 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002640 ev->psf_flags = 0;
2641 }
2642 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002643
Pekka Paalanene67858b2013-04-25 13:57:42 +03002644 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002645 wl_list_for_each(ev, &ec->view_list, link) {
2646 /* Note: This operation is safe to do multiple times on the
2647 * same surface.
2648 */
2649 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002650 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002651 &ev->surface->frame_callback_list);
2652 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002653
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002654 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002655 }
2656 }
2657
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002658 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002659
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002660 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002661 pixman_region32_intersect(&output_damage,
2662 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002663 pixman_region32_subtract(&output_damage,
2664 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002665
Scott Moreauccbf29d2012-02-22 14:21:41 -07002666 if (output->dirty)
2667 weston_output_update_matrix(output);
2668
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002669 r = output->repaint(output, &output_damage, repaint_data);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002670
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002671 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002672
Daniel Stone09a97e22017-03-01 11:34:06 +00002673 output->repaint_needed = false;
Daniel Stone05df8c12017-03-03 16:59:42 +00002674 if (r == 0)
2675 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002676
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002677 weston_compositor_repick(ec);
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002678
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002679 frame_time_msec = timespec_to_msec(&output->frame_time);
2680
Jonas Ådahldb773762012-06-13 00:01:21 +02002681 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002682 wl_callback_send_done(cb->resource, frame_time_msec);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002683 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002684 }
2685
Scott Moreaud64cf212012-06-08 19:40:54 -06002686 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002687 animation->frame_counter++;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002688 animation->frame(animation, output, &output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002689 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002690
Pekka Paalanenb5026542014-11-12 15:09:24 +02002691 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2692
David Herrmann1edf44c2013-10-22 17:11:26 +02002693 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002694}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002695
Pekka Paalanen82919792014-05-21 13:51:49 +03002696static void
2697weston_output_schedule_repaint_reset(struct weston_output *output)
2698{
Daniel Stone05df8c12017-03-03 16:59:42 +00002699 output->repaint_status = REPAINT_NOT_SCHEDULED;
Pekka Paalanen82919792014-05-21 13:51:49 +03002700 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
Pekka Paalanen82919792014-05-21 13:51:49 +03002701}
2702
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002703static int
2704weston_output_maybe_repaint(struct weston_output *output, struct timespec *now,
2705 void *repaint_data)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002706{
Pekka Paalanen0513a952014-05-21 16:17:27 +03002707 struct weston_compositor *compositor = output->compositor;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002708 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002709 int64_t msec_to_repaint;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002710
Daniel Stone6847b852017-03-01 11:34:08 +00002711 /* We're not ready yet; come back to make a decision later. */
2712 if (output->repaint_status != REPAINT_SCHEDULED)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002713 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002714
2715 msec_to_repaint = timespec_sub_to_msec(&output->next_repaint, now);
2716 if (msec_to_repaint > 1)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002717 return ret;
Daniel Stone05df8c12017-03-03 16:59:42 +00002718
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002719 /* If we're sleeping, drop the repaint machinery entirely; we will
2720 * explicitly repaint all outputs when we come back. */
2721 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2722 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
2723 goto err;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002724
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002725 /* We don't actually need to repaint this output; drop it from
2726 * repaint until something causes damage. */
2727 if (!output->repaint_needed)
2728 goto err;
2729
2730 /* If repaint fails, we aren't going to get weston_output_finish_frame
2731 * to trigger a new repaint, so drop it from repaint and hope
Daniel Stone6847b852017-03-01 11:34:08 +00002732 * something schedules a successful repaint later. As repainting may
2733 * take some time, re-read our clock as a courtesy to the next
2734 * output. */
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002735 ret = weston_output_repaint(output, repaint_data);
Daniel Stone6847b852017-03-01 11:34:08 +00002736 weston_compositor_read_presentation_clock(compositor, now);
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002737 if (ret != 0)
2738 goto err;
2739
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09002740 output->repainted = true;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002741 return ret;
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002742
2743err:
Pekka Paalanen0513a952014-05-21 16:17:27 +03002744 weston_output_schedule_repaint_reset(output);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002745 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002746}
2747
2748static void
2749output_repaint_timer_arm(struct weston_compositor *compositor)
2750{
2751 struct weston_output *output;
2752 bool any_should_repaint = false;
2753 struct timespec now;
Sergi Granellb4c08862017-03-18 13:01:15 +01002754 int64_t msec_to_next = INT64_MAX;
Daniel Stone6847b852017-03-01 11:34:08 +00002755
2756 weston_compositor_read_presentation_clock(compositor, &now);
2757
2758 wl_list_for_each(output, &compositor->output_list, link) {
2759 int64_t msec_to_this;
2760
2761 if (output->repaint_status != REPAINT_SCHEDULED)
2762 continue;
2763
2764 msec_to_this = timespec_sub_to_msec(&output->next_repaint,
2765 &now);
2766 if (!any_should_repaint || msec_to_this < msec_to_next)
2767 msec_to_next = msec_to_this;
2768
2769 any_should_repaint = true;
2770 }
2771
2772 if (!any_should_repaint)
2773 return;
2774
2775 /* Even if we should repaint immediately, add the minimum 1 ms delay.
2776 * This is a workaround to allow coalescing multiple output repaints
2777 * particularly from weston_output_finish_frame()
2778 * into the same call, which would not happen if we called
2779 * output_repaint_timer_handler() directly.
2780 */
2781 if (msec_to_next < 1)
2782 msec_to_next = 1;
2783
2784 wl_event_source_timer_update(compositor->repaint_timer, msec_to_next);
2785}
2786
2787static int
2788output_repaint_timer_handler(void *data)
2789{
2790 struct weston_compositor *compositor = data;
2791 struct weston_output *output;
2792 struct timespec now;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002793 void *repaint_data = NULL;
Emre Ucane479ed82018-03-20 15:29:40 +01002794 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002795
2796 weston_compositor_read_presentation_clock(compositor, &now);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002797
2798 if (compositor->backend->repaint_begin)
2799 repaint_data = compositor->backend->repaint_begin(compositor);
2800
2801 wl_list_for_each(output, &compositor->output_list, link) {
2802 ret = weston_output_maybe_repaint(output, &now, repaint_data);
2803 if (ret)
2804 break;
2805 }
2806
2807 if (ret == 0) {
Tomohito Esaki09bfcd62018-06-05 10:37:05 +09002808 if (compositor->backend->repaint_flush)
Antonio Borneoc90fccc2019-06-30 15:51:10 +02002809 ret = compositor->backend->repaint_flush(compositor,
2810 repaint_data);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002811 } else {
Antonio Borneoc90fccc2019-06-30 15:51:10 +02002812 if (compositor->backend->repaint_cancel)
2813 compositor->backend->repaint_cancel(compositor,
2814 repaint_data);
2815 }
2816
2817 if (ret != 0) {
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09002818 wl_list_for_each(output, &compositor->output_list, link) {
2819 if (output->repainted)
2820 weston_output_schedule_repaint_reset(output);
2821 }
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002822 }
Daniel Stone6847b852017-03-01 11:34:08 +00002823
Tomohito Esakiddaf95c2018-07-10 11:47:15 +09002824 wl_list_for_each(output, &compositor->output_list, link)
2825 output->repainted = false;
2826
Daniel Stone6847b852017-03-01 11:34:08 +00002827 output_repaint_timer_arm(compositor);
2828
Pekka Paalanen0513a952014-05-21 16:17:27 +03002829 return 0;
2830}
2831
Marius Vlad55d87362019-06-11 01:15:35 +03002832/**
2833 * \ingroup output
2834 */
Kristian Høgsbergef044142011-06-21 15:02:12 -04002835WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002836weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002837 const struct timespec *stamp,
2838 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002839{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002840 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002841 int32_t refresh_nsec;
2842 struct timespec now;
Daniel Stone6847b852017-03-01 11:34:08 +00002843 int64_t msec_rel;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002844
Pekka Paalanenb5026542014-11-12 15:09:24 +02002845
Daniel Stone05df8c12017-03-03 16:59:42 +00002846 assert(output->repaint_status == REPAINT_AWAITING_COMPLETION);
Daniel Stone3615ce12017-03-01 11:34:05 +00002847 assert(stamp || (presented_flags & WP_PRESENTATION_FEEDBACK_INVALID));
2848
Daniel Stone6847b852017-03-01 11:34:08 +00002849 weston_compositor_read_presentation_clock(compositor, &now);
2850
Daniel Stone3615ce12017-03-01 11:34:05 +00002851 /* If we haven't been supplied any timestamp at all, we don't have a
2852 * timebase to work against, so any delay just wastes time. Push a
2853 * repaint as soon as possible so we can get on with it. */
Daniel Stone6847b852017-03-01 11:34:08 +00002854 if (!stamp) {
2855 output->next_repaint = now;
Daniel Stone3615ce12017-03-01 11:34:05 +00002856 goto out;
Daniel Stone6847b852017-03-01 11:34:08 +00002857 }
Daniel Stone3615ce12017-03-01 11:34:05 +00002858
Marius Vladdf9278a2018-03-06 18:56:23 +02002859 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2860 TLP_VBLANK(stamp), TLP_END);
2861
Pekka Paalanend7894d02015-07-03 15:08:53 +03002862 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002863 weston_presentation_feedback_present_list(&output->feedback_list,
2864 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002865 output->msc,
2866 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002867
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002868 output->frame_time = *stamp;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002869
Daniel Stone6847b852017-03-01 11:34:08 +00002870 timespec_add_nsec(&output->next_repaint, stamp, refresh_nsec);
2871 timespec_add_msec(&output->next_repaint, &output->next_repaint,
2872 -compositor->repaint_msec);
2873 msec_rel = timespec_sub_to_msec(&output->next_repaint, &now);
Daniel Stone84aff5c2017-03-01 11:34:04 +00002874
2875 if (msec_rel < -1000 || msec_rel > 1000) {
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002876 static bool warned;
2877
2878 if (!warned)
2879 weston_log("Warning: computed repaint delay is "
Daniel Stone6847b852017-03-01 11:34:08 +00002880 "insane: %lld msec\n", (long long) msec_rel);
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002881 warned = true;
2882
Daniel Stone6847b852017-03-01 11:34:08 +00002883 output->next_repaint = now;
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002884 }
2885
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002886 /* Called from restart_repaint_loop and restart happens already after
2887 * the deadline given by repaint_msec? In that case we delay until
2888 * the deadline of the next frame, to give clients a more predictable
2889 * timing of the repaint cycle to lock on. */
Daniel Stoneeca5cca2017-02-28 21:53:51 +00002890 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID &&
2891 msec_rel < 0) {
2892 while (timespec_sub_to_nsec(&output->next_repaint, &now) < 0) {
2893 timespec_add_nsec(&output->next_repaint,
2894 &output->next_repaint,
2895 refresh_nsec);
2896 }
2897 }
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002898
Daniel Stone3615ce12017-03-01 11:34:05 +00002899out:
Daniel Stone05df8c12017-03-03 16:59:42 +00002900 output->repaint_status = REPAINT_SCHEDULED;
Daniel Stone6847b852017-03-01 11:34:08 +00002901 output_repaint_timer_arm(compositor);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002902}
2903
2904static void
2905idle_repaint(void *data)
2906{
2907 struct weston_output *output = data;
Antonio Borneoc90fccc2019-06-30 15:51:10 +02002908 int ret;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002909
Daniel Stone05df8c12017-03-03 16:59:42 +00002910 assert(output->repaint_status == REPAINT_BEGIN_FROM_IDLE);
2911 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03002912 output->idle_repaint_source = NULL;
Antonio Borneoc90fccc2019-06-30 15:51:10 +02002913 ret = output->start_repaint_loop(output);
2914 if (ret != 0)
2915 weston_output_schedule_repaint_reset(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002916}
2917
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002918WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002919weston_layer_entry_insert(struct weston_layer_entry *list,
2920 struct weston_layer_entry *entry)
2921{
2922 wl_list_insert(&list->link, &entry->link);
2923 entry->layer = list->layer;
2924}
2925
2926WL_EXPORT void
2927weston_layer_entry_remove(struct weston_layer_entry *entry)
2928{
2929 wl_list_remove(&entry->link);
2930 wl_list_init(&entry->link);
2931 entry->layer = NULL;
2932}
2933
Quentin Glidic82681572016-12-17 13:40:51 +01002934
2935/** Initialize the weston_layer struct.
2936 *
2937 * \param compositor The compositor instance
2938 * \param layer The layer to initialize
2939 */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002940WL_EXPORT void
Quentin Glidic82681572016-12-17 13:40:51 +01002941weston_layer_init(struct weston_layer *layer,
2942 struct weston_compositor *compositor)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002943{
Quentin Glidic82681572016-12-17 13:40:51 +01002944 layer->compositor = compositor;
2945 wl_list_init(&layer->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002946 wl_list_init(&layer->view_list.link);
2947 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002948 weston_layer_set_mask_infinite(layer);
Quentin Glidic82681572016-12-17 13:40:51 +01002949}
2950
2951/** Sets the position of the layer in the layer list. The layer will be placed
2952 * below any layer with the same position value, if any.
2953 * This function is safe to call if the layer is already on the list, but the
2954 * layer may be moved below other layers at the same position, if any.
2955 *
2956 * \param layer The layer to modify
2957 * \param position The position the layer will be placed at
2958 */
2959WL_EXPORT void
2960weston_layer_set_position(struct weston_layer *layer,
2961 enum weston_layer_position position)
2962{
2963 struct weston_layer *below;
2964
2965 wl_list_remove(&layer->link);
2966
2967 /* layer_list is ordered from top to bottom, the last layer being the
2968 * background with the smallest position value */
2969
2970 layer->position = position;
2971 wl_list_for_each_reverse(below, &layer->compositor->layer_list, link) {
2972 if (below->position >= layer->position) {
2973 wl_list_insert(&below->link, &layer->link);
2974 return;
2975 }
2976 }
2977 wl_list_insert(&layer->compositor->layer_list, &layer->link);
2978}
2979
2980/** Hide a layer by taking it off the layer list.
2981 * This function is safe to call if the layer is not on the list.
2982 *
2983 * \param layer The layer to hide
2984 */
2985WL_EXPORT void
2986weston_layer_unset_position(struct weston_layer *layer)
2987{
2988 wl_list_remove(&layer->link);
2989 wl_list_init(&layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002990}
2991
2992WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002993weston_layer_set_mask(struct weston_layer *layer,
2994 int x, int y, int width, int height)
2995{
2996 struct weston_view *view;
2997
2998 layer->mask.x1 = x;
2999 layer->mask.x2 = x + width;
3000 layer->mask.y1 = y;
3001 layer->mask.y2 = y + height;
3002
3003 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
3004 weston_view_geometry_dirty(view);
3005 }
3006}
3007
3008WL_EXPORT void
3009weston_layer_set_mask_infinite(struct weston_layer *layer)
3010{
3011 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
3012 UINT32_MAX, UINT32_MAX);
3013}
3014
Daniel Stone3b775632018-07-20 08:38:25 +01003015WL_EXPORT bool
3016weston_layer_mask_is_infinite(struct weston_layer *layer)
3017{
3018 return layer->mask.x1 == INT32_MIN &&
3019 layer->mask.y1 == INT32_MIN &&
3020 layer->mask.x2 == INT32_MIN + UINT32_MAX &&
3021 layer->mask.y2 == INT32_MIN + UINT32_MAX;
3022}
3023
Marius Vlad55d87362019-06-11 01:15:35 +03003024/**
3025 * \ingroup output
3026 */
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003027WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003028weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003029{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003030 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04003031 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01003032
Bryce Harrington08976ac2016-08-30 12:05:16 -07003033 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
3034 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003035 return;
3036
Pekka Paalanenb5026542014-11-12 15:09:24 +02003037 if (!output->repaint_needed)
3038 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
3039
Kristian Høgsbergef044142011-06-21 15:02:12 -04003040 loop = wl_display_get_event_loop(compositor->wl_display);
Daniel Stone09a97e22017-03-01 11:34:06 +00003041 output->repaint_needed = true;
Daniel Stone05df8c12017-03-03 16:59:42 +00003042
3043 /* If we already have a repaint scheduled for our idle handler,
3044 * no need to set it again. If the repaint has been called but
3045 * not finished, then weston_output_finish_frame() will notice
3046 * that a repaint is needed and schedule one. */
3047 if (output->repaint_status != REPAINT_NOT_SCHEDULED)
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003048 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01003049
Daniel Stone05df8c12017-03-03 16:59:42 +00003050 output->repaint_status = REPAINT_BEGIN_FROM_IDLE;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03003051 assert(!output->idle_repaint_source);
3052 output->idle_repaint_source = wl_event_loop_add_idle(loop, idle_repaint,
3053 output);
Pekka Paalanenb5026542014-11-12 15:09:24 +02003054 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003055}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05003056
Marius Vlad9fdda7f2019-06-11 16:08:55 +03003057/** weston_compositor_schedule_repaint
3058 * \ingroup compositor
3059 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003060WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003061weston_compositor_schedule_repaint(struct weston_compositor *compositor)
3062{
3063 struct weston_output *output;
3064
3065 wl_list_for_each(output, &compositor->output_list, link)
3066 weston_output_schedule_repaint(output);
3067}
3068
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003069static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003070surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003071{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003072 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003073}
3074
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003075static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003076surface_attach(struct wl_client *client,
3077 struct wl_resource *resource,
3078 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
3079{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003080 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05003081 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003082
Kristian Høgsbergab19f932013-08-20 11:30:54 -07003083 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05003084 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07003085 if (buffer == NULL) {
3086 wl_client_post_no_memory(client);
3087 return;
3088 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07003089 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04003090
Pekka Paalanende685b82012-12-04 15:58:12 +02003091 /* Attach, attach, without commit in between does not send
3092 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003093 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003094
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003095 surface->pending.sx = sx;
3096 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01003097 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003098}
3099
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003100static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003101surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003102 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003103 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05003104{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003105 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003106
Derek Foreman57e92ed2015-11-17 14:11:35 -06003107 if (width <= 0 || height <= 0)
3108 return;
3109
Derek Foreman152254b2015-11-26 14:17:48 -06003110 pixman_region32_union_rect(&surface->pending.damage_surface,
3111 &surface->pending.damage_surface,
3112 x, y, width, height);
3113}
3114
3115static void
3116surface_damage_buffer(struct wl_client *client,
3117 struct wl_resource *resource,
3118 int32_t x, int32_t y, int32_t width, int32_t height)
3119{
3120 struct weston_surface *surface = wl_resource_get_user_data(resource);
3121
3122 if (width <= 0 || height <= 0)
3123 return;
3124
3125 pixman_region32_union_rect(&surface->pending.damage_buffer,
3126 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04003127 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003128}
3129
Kristian Høgsberg33418202011-08-16 23:01:28 -04003130static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003131destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04003132{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05003133 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003134
3135 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02003136 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003137}
3138
3139static void
3140surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003141 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04003142{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003143 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003144 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003145
3146 cb = malloc(sizeof *cb);
3147 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003148 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003149 return;
3150 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03003151
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003152 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
3153 callback);
3154 if (cb->resource == NULL) {
3155 free(cb);
3156 wl_resource_post_no_memory(resource);
3157 return;
3158 }
3159
Jason Ekstranda85118c2013-06-27 20:17:02 -05003160 wl_resource_set_implementation(cb->resource, NULL, cb,
3161 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003162
Pekka Paalanenbc106382012-10-10 12:49:31 +03003163 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003164}
3165
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003166static void
3167surface_set_opaque_region(struct wl_client *client,
3168 struct wl_resource *resource,
3169 struct wl_resource *region_resource)
3170{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003171 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003172 struct weston_region *region;
3173
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003174 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003175 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003176 pixman_region32_copy(&surface->pending.opaque,
3177 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003178 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07003179 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003180 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003181}
3182
3183static void
3184surface_set_input_region(struct wl_client *client,
3185 struct wl_resource *resource,
3186 struct wl_resource *region_resource)
3187{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003188 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003189 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003190
3191 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003192 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003193 pixman_region32_copy(&surface->pending.input,
3194 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003195 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003196 pixman_region32_fini(&surface->pending.input);
3197 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003198 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003199}
3200
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003201/* Cause damage to this sub-surface and all its children.
3202 *
3203 * This is useful when there are state changes that need an implicit
3204 * damage, e.g. a z-order change.
3205 */
3206static void
3207weston_surface_damage_subsurfaces(struct weston_subsurface *sub)
3208{
3209 struct weston_subsurface *child;
3210
3211 weston_surface_damage(sub->surface);
3212 sub->reordered = false;
3213
3214 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link)
3215 if (child != sub)
3216 weston_surface_damage_subsurfaces(child);
3217}
3218
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003219static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003220weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003221{
Pekka Paalanene67858b2013-04-25 13:57:42 +03003222 struct weston_subsurface *sub;
3223
3224 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
3225 parent_link_pending) {
3226 wl_list_remove(&sub->parent_link);
3227 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003228
3229 if (sub->reordered)
3230 weston_surface_damage_subsurfaces(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003231 }
3232}
3233
3234static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03003235weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05003236 struct weston_matrix *matrix)
3237{
Pekka Paalanen04baea52016-04-26 15:50:58 +03003238 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05003239 double src_width, src_height, dest_width, dest_height;
3240
3241 weston_matrix_init(matrix);
3242
3243 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
3244 src_width = surface->width_from_buffer;
3245 src_height = surface->height_from_buffer;
3246 } else {
3247 src_width = wl_fixed_to_double(vp->buffer.src_width);
3248 src_height = wl_fixed_to_double(vp->buffer.src_height);
3249 }
3250
3251 if (vp->surface.width == -1) {
3252 dest_width = src_width;
3253 dest_height = src_height;
3254 } else {
3255 dest_width = vp->surface.width;
3256 dest_height = vp->surface.height;
3257 }
3258
3259 if (src_width != dest_width || src_height != dest_height)
3260 weston_matrix_scale(matrix,
3261 src_width / dest_width,
3262 src_height / dest_height, 1);
3263
3264 if (vp->buffer.src_width != wl_fixed_from_int(-1))
3265 weston_matrix_translate(matrix,
3266 wl_fixed_to_double(vp->buffer.src_x),
3267 wl_fixed_to_double(vp->buffer.src_y),
3268 0);
3269
3270 switch (vp->buffer.transform) {
3271 case WL_OUTPUT_TRANSFORM_FLIPPED:
3272 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3273 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3274 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3275 weston_matrix_scale(matrix, -1, 1, 1);
3276 weston_matrix_translate(matrix,
3277 surface->width_from_buffer, 0, 0);
3278 break;
3279 }
3280
3281 switch (vp->buffer.transform) {
3282 default:
3283 case WL_OUTPUT_TRANSFORM_NORMAL:
3284 case WL_OUTPUT_TRANSFORM_FLIPPED:
3285 break;
3286 case WL_OUTPUT_TRANSFORM_90:
3287 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3288 weston_matrix_rotate_xy(matrix, 0, 1);
3289 weston_matrix_translate(matrix,
3290 surface->height_from_buffer, 0, 0);
3291 break;
3292 case WL_OUTPUT_TRANSFORM_180:
3293 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3294 weston_matrix_rotate_xy(matrix, -1, 0);
3295 weston_matrix_translate(matrix,
3296 surface->width_from_buffer,
3297 surface->height_from_buffer, 0);
3298 break;
3299 case WL_OUTPUT_TRANSFORM_270:
3300 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3301 weston_matrix_rotate_xy(matrix, 0, -1);
3302 weston_matrix_translate(matrix,
3303 0, surface->width_from_buffer, 0);
3304 break;
3305 }
3306
3307 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
3308}
3309
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003310/**
3311 * Compute a + b > c while being safe to overflows.
3312 */
3313static bool
3314fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
3315{
3316 return (int64_t)a + (int64_t)b > (int64_t)c;
3317}
3318
3319static bool
3320weston_surface_is_pending_viewport_source_valid(
3321 const struct weston_surface *surface)
3322{
3323 const struct weston_surface_state *pend = &surface->pending;
3324 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
3325 int width_from_buffer = 0;
3326 int height_from_buffer = 0;
3327 wl_fixed_t w;
3328 wl_fixed_t h;
3329
3330 /* If viewport source rect is not set, it is always ok. */
3331 if (vp->buffer.src_width == wl_fixed_from_int(-1))
3332 return true;
3333
3334 if (pend->newly_attached) {
3335 if (pend->buffer) {
3336 convert_size_by_transform_scale(&width_from_buffer,
3337 &height_from_buffer,
3338 pend->buffer->width,
3339 pend->buffer->height,
3340 vp->buffer.transform,
3341 vp->buffer.scale);
3342 } else {
3343 /* No buffer: viewport is irrelevant. */
3344 return true;
3345 }
3346 } else {
3347 width_from_buffer = surface->width_from_buffer;
3348 height_from_buffer = surface->height_from_buffer;
3349 }
3350
3351 assert((width_from_buffer == 0) == (height_from_buffer == 0));
3352 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
3353
3354 /* No buffer: viewport is irrelevant. */
3355 if (width_from_buffer == 0 || height_from_buffer == 0)
3356 return true;
3357
3358 /* overflow checks for wl_fixed_from_int() */
3359 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
3360 return false;
3361 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
3362 return false;
3363
3364 w = wl_fixed_from_int(width_from_buffer);
3365 h = wl_fixed_from_int(height_from_buffer);
3366
3367 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
3368 return false;
3369 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
3370 return false;
3371
3372 return true;
3373}
3374
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003375static bool
3376fixed_is_integer(wl_fixed_t v)
3377{
3378 return (v & 0xff) == 0;
3379}
3380
3381static bool
3382weston_surface_is_pending_viewport_dst_size_int(
3383 const struct weston_surface *surface)
3384{
3385 const struct weston_buffer_viewport *vp =
3386 &surface->pending.buffer_viewport;
3387
3388 if (vp->surface.width != -1) {
3389 assert(vp->surface.width > 0 && vp->surface.height > 0);
3390 return true;
3391 }
3392
3393 return fixed_is_integer(vp->buffer.src_width) &&
3394 fixed_is_integer(vp->buffer.src_height);
3395}
3396
Derek Foreman152254b2015-11-26 14:17:48 -06003397/* Translate pending damage in buffer co-ordinates to surface
3398 * co-ordinates and union it with a pixman_region32_t.
3399 * This should only be called after the buffer is attached.
3400 */
3401static void
3402apply_damage_buffer(pixman_region32_t *dest,
3403 struct weston_surface *surface,
3404 struct weston_surface_state *state)
3405{
3406 struct weston_buffer *buffer = surface->buffer_ref.buffer;
3407
3408 /* wl_surface.damage_buffer needs to be clipped to the buffer,
3409 * translated into surface co-ordinates and unioned with
3410 * any other surface damage.
3411 * None of this makes sense if there is no buffer though.
3412 */
3413 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
3414 pixman_region32_t buffer_damage;
3415
3416 pixman_region32_intersect_rect(&state->damage_buffer,
3417 &state->damage_buffer,
3418 0, 0, buffer->width,
3419 buffer->height);
3420 pixman_region32_init(&buffer_damage);
3421 weston_matrix_transform_region(&buffer_damage,
3422 &surface->buffer_to_surface_matrix,
3423 &state->damage_buffer);
3424 pixman_region32_union(dest, dest, &buffer_damage);
3425 pixman_region32_fini(&buffer_damage);
3426 }
3427 /* We should clear this on commit even if there was no buffer */
3428 pixman_region32_clear(&state->damage_buffer);
3429}
3430
Jason Ekstrand1e059042014-10-16 10:55:19 -05003431static void
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303432weston_surface_set_desired_protection(struct weston_surface *surface,
3433 enum weston_hdcp_protection protection)
3434{
3435 if (surface->desired_protection == protection)
3436 return;
3437 surface->desired_protection = protection;
3438 weston_surface_damage(surface);
3439}
3440
3441static void
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303442weston_surface_set_protection_mode(struct weston_surface *surface,
3443 enum weston_surface_protection_mode p_mode)
3444{
3445 struct content_protection *cp = surface->compositor->content_protection;
3446 struct protected_surface *psurface;
3447
3448 surface->protection_mode = p_mode;
3449 wl_list_for_each(psurface, &cp->protected_list, link) {
3450 if (!psurface || psurface->surface != surface)
3451 continue;
3452 weston_protected_surface_send_event(psurface,
3453 surface->current_protection);
3454 }
3455}
3456
3457static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05003458weston_surface_commit_state(struct weston_surface *surface,
3459 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003460{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003461 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003462 pixman_region32_t opaque;
3463
Alexander Larsson4ea95522013-05-22 14:41:37 +02003464 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02003465 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03003466 /* wp_viewport.set_source */
3467 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003468 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003469
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003470 /* wl_surface.attach */
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003471 if (state->newly_attached) {
3472 /* zwp_surface_synchronization_v1.set_acquire_fence */
3473 fd_move(&surface->acquire_fence_fd,
3474 &state->acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003475 /* zwp_surface_synchronization_v1.get_release */
3476 weston_buffer_release_move(&surface->buffer_release_ref,
3477 &state->buffer_release_ref);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003478 weston_surface_attach(surface, state->buffer);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003479 }
Jason Ekstrand7b982072014-05-20 14:33:03 -05003480 weston_surface_state_set_buffer(state, NULL);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003481 assert(state->acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003482 assert(state->buffer_release_ref.buffer_release == NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01003483
Jason Ekstrand1e059042014-10-16 10:55:19 -05003484 weston_surface_build_buffer_matrix(surface,
3485 &surface->surface_to_buffer_matrix);
3486 weston_matrix_invert(&surface->buffer_to_surface_matrix,
3487 &surface->surface_to_buffer_matrix);
3488
Jason Ekstrand7b982072014-05-20 14:33:03 -05003489 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003490 weston_surface_update_size(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003491 if (surface->committed)
3492 surface->committed(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003493 }
Giulio Camuffo184df502013-02-21 11:29:21 +01003494
Jason Ekstrand7b982072014-05-20 14:33:03 -05003495 state->sx = 0;
3496 state->sy = 0;
3497 state->newly_attached = 0;
3498 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03003499
Derek Foreman152254b2015-11-26 14:17:48 -06003500 /* wl_surface.damage and wl_surface.damage_buffer */
Pekka Paalanenb5026542014-11-12 15:09:24 +02003501 if (weston_timeline_enabled_ &&
Derek Foreman152254b2015-11-26 14:17:48 -06003502 (pixman_region32_not_empty(&state->damage_surface) ||
3503 pixman_region32_not_empty(&state->damage_buffer)))
Pekka Paalanenb5026542014-11-12 15:09:24 +02003504 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06003505
Pekka Paalanen8e159182012-10-10 12:49:25 +03003506 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06003507 &state->damage_surface);
3508
3509 apply_damage_buffer(&surface->damage, surface, state);
3510
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05003511 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07003512 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06003513 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003514
3515 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003516 pixman_region32_init(&opaque);
3517 pixman_region32_intersect_rect(&opaque, &state->opaque,
3518 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003519
3520 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
3521 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003522 wl_list_for_each(view, &surface->views, surface_link)
3523 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003524 }
3525
3526 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003527
3528 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003529 pixman_region32_intersect_rect(&surface->input, &state->input,
3530 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003531
Pekka Paalanenbc106382012-10-10 12:49:31 +03003532 /* wl_surface.frame */
3533 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05003534 &state->frame_callback_list);
3535 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003536
3537 /* XXX:
3538 * What should happen with a feedback request, if there
3539 * is no wl_buffer attached for this commit?
3540 */
3541
3542 /* presentation.feedback */
3543 wl_list_insert_list(&surface->feedback_list,
3544 &state->feedback_list);
3545 wl_list_init(&state->feedback_list);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003546
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303547 /* weston_protected_surface.enforced/relaxed */
3548 if (surface->protection_mode != state->protection_mode)
3549 weston_surface_set_protection_mode(surface,
3550 state->protection_mode);
3551
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303552 /* weston_protected_surface.set_type */
3553 weston_surface_set_desired_protection(surface, state->desired_protection);
3554
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003555 wl_signal_emit(&surface->commit_signal, surface);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003556}
3557
3558static void
3559weston_surface_commit(struct weston_surface *surface)
3560{
3561 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003562
Pekka Paalanene67858b2013-04-25 13:57:42 +03003563 weston_surface_commit_subsurface_order(surface);
3564
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003565 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003566}
3567
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003568static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003569weston_subsurface_commit(struct weston_subsurface *sub);
3570
3571static void
3572weston_subsurface_parent_commit(struct weston_subsurface *sub,
3573 int parent_is_synchronized);
3574
3575static void
3576surface_commit(struct wl_client *client, struct wl_resource *resource)
3577{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003578 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003579 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
3580
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003581 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
3582 assert(surface->viewport_resource);
3583
3584 wl_resource_post_error(surface->viewport_resource,
3585 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
3586 "wl_surface@%d has viewport source outside buffer",
3587 wl_resource_get_id(resource));
3588 return;
3589 }
3590
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003591 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
3592 assert(surface->viewport_resource);
3593
3594 wl_resource_post_error(surface->viewport_resource,
3595 WP_VIEWPORT_ERROR_BAD_SIZE,
3596 "wl_surface@%d viewport dst size not integer",
3597 wl_resource_get_id(resource));
3598 return;
3599 }
3600
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003601 if (surface->pending.acquire_fence_fd >= 0) {
3602 assert(surface->synchronization_resource);
3603
3604 if (!surface->pending.buffer) {
3605 fd_clear(&surface->pending.acquire_fence_fd);
3606 wl_resource_post_error(surface->synchronization_resource,
3607 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3608 "wl_surface@%"PRIu32" no buffer for synchronization",
3609 wl_resource_get_id(resource));
3610 return;
3611 }
3612
3613 /* We support fences for both wp_linux_dmabuf and opaque EGL
3614 * buffers, as mandated by minor version 2 of the
3615 * zwp_linux_explicit_synchronization_v1 protocol. Since
3616 * renderers that support fences currently only support these
3617 * two buffer types plus SHM buffers, we can just check for the
3618 * SHM buffer case here.
3619 */
3620 if (wl_shm_buffer_get(surface->pending.buffer->resource)) {
3621 fd_clear(&surface->pending.acquire_fence_fd);
3622 wl_resource_post_error(surface->synchronization_resource,
3623 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_UNSUPPORTED_BUFFER,
3624 "wl_surface@%"PRIu32" unsupported buffer for synchronization",
3625 wl_resource_get_id(resource));
3626 return;
3627 }
3628 }
3629
Alexandros Frantzis67629672018-10-19 12:14:11 +03003630 if (surface->pending.buffer_release_ref.buffer_release &&
3631 !surface->pending.buffer) {
3632 assert(surface->synchronization_resource);
3633
3634 wl_resource_post_error(surface->synchronization_resource,
3635 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3636 "wl_surface@%"PRIu32" no buffer for synchronization",
3637 wl_resource_get_id(resource));
3638 return;
3639 }
3640
Pekka Paalanene67858b2013-04-25 13:57:42 +03003641 if (sub) {
3642 weston_subsurface_commit(sub);
3643 return;
3644 }
3645
3646 weston_surface_commit(surface);
3647
3648 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
3649 if (sub->surface != surface)
3650 weston_subsurface_parent_commit(sub, 0);
3651 }
3652}
3653
3654static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003655surface_set_buffer_transform(struct wl_client *client,
3656 struct wl_resource *resource, int transform)
3657{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003658 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003659
Jonny Lamba55f1392014-05-30 12:07:15 +02003660 /* if wl_output.transform grows more members this will need to be updated. */
3661 if (transform < 0 ||
3662 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
3663 wl_resource_post_error(resource,
3664 WL_SURFACE_ERROR_INVALID_TRANSFORM,
3665 "buffer transform must be a valid transform "
3666 "('%d' specified)", transform);
3667 return;
3668 }
3669
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003670 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003671 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003672}
3673
Alexander Larsson4ea95522013-05-22 14:41:37 +02003674static void
3675surface_set_buffer_scale(struct wl_client *client,
3676 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003677 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003678{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003679 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003680
Jonny Lamba55f1392014-05-30 12:07:15 +02003681 if (scale < 1) {
3682 wl_resource_post_error(resource,
3683 WL_SURFACE_ERROR_INVALID_SCALE,
3684 "buffer scale must be at least one "
3685 "('%d' specified)", scale);
3686 return;
3687 }
3688
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003689 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003690 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003691}
3692
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003693static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003694 surface_destroy,
3695 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04003696 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003697 surface_frame,
3698 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003699 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003700 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02003701 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06003702 surface_set_buffer_scale,
3703 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003704};
3705
3706static void
3707compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003708 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003709{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04003710 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003711 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003712
Kristian Høgsberg18c93002012-01-27 11:58:31 -05003713 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003714 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003715 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003716 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003717 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003718
Jason Ekstranda85118c2013-06-27 20:17:02 -05003719 surface->resource =
3720 wl_resource_create(client, &wl_surface_interface,
3721 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003722 if (surface->resource == NULL) {
3723 weston_surface_destroy(surface);
3724 wl_resource_post_no_memory(resource);
3725 return;
3726 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003727 wl_resource_set_implementation(surface->resource, &surface_interface,
3728 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003729
3730 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003731}
3732
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003733static void
3734destroy_region(struct wl_resource *resource)
3735{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003736 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003737
3738 pixman_region32_fini(&region->region);
3739 free(region);
3740}
3741
3742static void
3743region_destroy(struct wl_client *client, struct wl_resource *resource)
3744{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003745 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003746}
3747
3748static void
3749region_add(struct wl_client *client, struct wl_resource *resource,
3750 int32_t x, int32_t y, int32_t width, int32_t height)
3751{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003752 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003753
3754 pixman_region32_union_rect(&region->region, &region->region,
3755 x, y, width, height);
3756}
3757
3758static void
3759region_subtract(struct wl_client *client, struct wl_resource *resource,
3760 int32_t x, int32_t y, int32_t width, int32_t height)
3761{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003762 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003763 pixman_region32_t rect;
3764
3765 pixman_region32_init_rect(&rect, x, y, width, height);
3766 pixman_region32_subtract(&region->region, &region->region, &rect);
3767 pixman_region32_fini(&rect);
3768}
3769
3770static const struct wl_region_interface region_interface = {
3771 region_destroy,
3772 region_add,
3773 region_subtract
3774};
3775
3776static void
3777compositor_create_region(struct wl_client *client,
3778 struct wl_resource *resource, uint32_t id)
3779{
3780 struct weston_region *region;
3781
3782 region = malloc(sizeof *region);
3783 if (region == NULL) {
3784 wl_resource_post_no_memory(resource);
3785 return;
3786 }
3787
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003788 pixman_region32_init(&region->region);
3789
Jason Ekstranda85118c2013-06-27 20:17:02 -05003790 region->resource =
3791 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003792 if (region->resource == NULL) {
3793 free(region);
3794 wl_resource_post_no_memory(resource);
3795 return;
3796 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003797 wl_resource_set_implementation(region->resource, &region_interface,
3798 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003799}
3800
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003801static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003802 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003803 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003804};
3805
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003806static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003807weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3808{
3809 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003810
Jason Ekstrand7b982072014-05-20 14:33:03 -05003811 weston_surface_commit_state(surface, &sub->cached);
3812 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003813
3814 weston_surface_commit_subsurface_order(surface);
3815
3816 weston_surface_schedule_repaint(surface);
3817
Jason Ekstrand7b982072014-05-20 14:33:03 -05003818 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003819}
3820
3821static void
3822weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3823{
3824 struct weston_surface *surface = sub->surface;
3825
3826 /*
3827 * If this commit would cause the surface to move by the
3828 * attach(dx, dy) parameters, the old damage region must be
3829 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05003830 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003831 */
Derek Foreman152254b2015-11-26 14:17:48 -06003832 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003833 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06003834 pixman_region32_union(&sub->cached.damage_surface,
3835 &sub->cached.damage_surface,
3836 &surface->pending.damage_surface);
3837 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003838
3839 if (surface->pending.newly_attached) {
3840 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003841 weston_surface_state_set_buffer(&sub->cached,
3842 surface->pending.buffer);
3843 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003844 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003845 weston_presentation_feedback_discard_list(
3846 &sub->cached.feedback_list);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003847 /* zwp_surface_synchronization_v1.set_acquire_fence */
3848 fd_move(&sub->cached.acquire_fence_fd,
3849 &surface->pending.acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003850 /* zwp_surface_synchronization_v1.get_release */
3851 weston_buffer_release_move(&sub->cached.buffer_release_ref,
3852 &surface->pending.buffer_release_ref);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003853 }
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303854 sub->cached.desired_protection = surface->pending.desired_protection;
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303855 sub->cached.protection_mode = surface->pending.protection_mode;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003856 assert(surface->pending.acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003857 assert(surface->pending.buffer_release_ref.buffer_release == NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003858 sub->cached.sx += surface->pending.sx;
3859 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003860
Derek Foreman152254b2015-11-26 14:17:48 -06003861 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
3862
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003863 sub->cached.buffer_viewport.changed |=
3864 surface->pending.buffer_viewport.changed;
3865 sub->cached.buffer_viewport.buffer =
3866 surface->pending.buffer_viewport.buffer;
3867 sub->cached.buffer_viewport.surface =
3868 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003869
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003870 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003871
3872 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3873
3874 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3875
3876 wl_list_insert_list(&sub->cached.frame_callback_list,
3877 &surface->pending.frame_callback_list);
3878 wl_list_init(&surface->pending.frame_callback_list);
3879
Pekka Paalanen133e4392014-09-23 22:08:46 -04003880 wl_list_insert_list(&sub->cached.feedback_list,
3881 &surface->pending.feedback_list);
3882 wl_list_init(&surface->pending.feedback_list);
3883
Jason Ekstrand7b982072014-05-20 14:33:03 -05003884 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003885}
3886
Derek Foreman280e7dd2014-10-03 13:13:42 -05003887static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003888weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3889{
3890 while (sub) {
3891 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003892 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003893
3894 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003895 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003896
3897 sub = weston_surface_to_subsurface(sub->parent);
3898 }
3899
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003900 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003901}
3902
3903static void
3904weston_subsurface_commit(struct weston_subsurface *sub)
3905{
3906 struct weston_surface *surface = sub->surface;
3907 struct weston_subsurface *tmp;
3908
3909 /* Recursive check for effectively synchronized. */
3910 if (weston_subsurface_is_synchronized(sub)) {
3911 weston_subsurface_commit_to_cache(sub);
3912 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003913 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003914 /* flush accumulated state from cache */
3915 weston_subsurface_commit_to_cache(sub);
3916 weston_subsurface_commit_from_cache(sub);
3917 } else {
3918 weston_surface_commit(surface);
3919 }
3920
3921 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3922 if (tmp->surface != surface)
3923 weston_subsurface_parent_commit(tmp, 0);
3924 }
3925 }
3926}
3927
3928static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003929weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003930{
3931 struct weston_surface *surface = sub->surface;
3932 struct weston_subsurface *tmp;
3933
Pekka Paalanene67858b2013-04-25 13:57:42 +03003934 /* From now on, commit_from_cache the whole sub-tree, regardless of
3935 * the synchronized mode of each child. This sub-surface or some
3936 * of its ancestors were synchronized, so we are synchronized
3937 * all the way down.
3938 */
3939
Jason Ekstrand7b982072014-05-20 14:33:03 -05003940 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003941 weston_subsurface_commit_from_cache(sub);
3942
3943 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3944 if (tmp->surface != surface)
3945 weston_subsurface_parent_commit(tmp, 1);
3946 }
3947}
3948
3949static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003950weston_subsurface_parent_commit(struct weston_subsurface *sub,
3951 int parent_is_synchronized)
3952{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003953 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003954 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003955 wl_list_for_each(view, &sub->surface->views, surface_link)
3956 weston_view_set_position(view,
3957 sub->position.x,
3958 sub->position.y);
3959
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003960 sub->position.set = 0;
3961 }
3962
3963 if (parent_is_synchronized || sub->synchronized)
3964 weston_subsurface_synchronized_commit(sub);
3965}
3966
Pekka Paalanen8274d902014-08-06 19:36:51 +03003967static int
3968subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3969{
3970 return snprintf(buf, len, "sub-surface");
3971}
3972
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003973static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003974subsurface_committed(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003975{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003976 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003977
Jason Ekstranda7af7042013-10-12 22:38:11 -05003978 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003979 weston_view_set_position(view,
3980 view->geometry.x + dx,
3981 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003982
3983 /* No need to check parent mappedness, because if parent is not
3984 * mapped, parent is not in a visible layer, so this sub-surface
3985 * will not be drawn either.
3986 */
Armin Krezovićf8486c32016-06-30 06:04:28 +02003987
Pekka Paalanene67858b2013-04-25 13:57:42 +03003988 if (!weston_surface_is_mapped(surface)) {
Armin Krezovićf8486c32016-06-30 06:04:28 +02003989 surface->is_mapped = true;
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003990
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003991 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003992 * because that would call it also for the parent surface,
3993 * which might not be mapped yet. That would lead to
3994 * inconsistent state, where the window could never be
3995 * mapped.
3996 *
Armin Krezovićf8486c32016-06-30 06:04:28 +02003997 * Instead just force the is_mapped flag on, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003998 * weston_surface_is_mapped() return true, so that when the
3999 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03004000 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03004001 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004002 }
4003}
4004
4005static struct weston_subsurface *
4006weston_surface_to_subsurface(struct weston_surface *surface)
4007{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004008 if (surface->committed == subsurface_committed)
4009 return surface->committed_private;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004010
4011 return NULL;
4012}
4013
Pekka Paalanen01388e22013-04-25 13:57:44 +03004014WL_EXPORT struct weston_surface *
4015weston_surface_get_main_surface(struct weston_surface *surface)
4016{
4017 struct weston_subsurface *sub;
4018
4019 while (surface && (sub = weston_surface_to_subsurface(surface)))
4020 surface = sub->parent;
4021
4022 return surface;
4023}
4024
Pekka Paalanen50b67472014-10-01 15:02:41 +03004025WL_EXPORT int
4026weston_surface_set_role(struct weston_surface *surface,
4027 const char *role_name,
4028 struct wl_resource *error_resource,
4029 uint32_t error_code)
4030{
4031 assert(role_name);
4032
4033 if (surface->role_name == NULL ||
4034 surface->role_name == role_name ||
4035 strcmp(surface->role_name, role_name) == 0) {
4036 surface->role_name = role_name;
4037
4038 return 0;
4039 }
4040
4041 wl_resource_post_error(error_resource, error_code,
4042 "Cannot assign role %s to wl_surface@%d,"
4043 " already has role %s\n",
4044 role_name,
4045 wl_resource_get_id(surface->resource),
4046 surface->role_name);
4047 return -1;
4048}
4049
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02004050WL_EXPORT const char *
4051weston_surface_get_role(struct weston_surface *surface)
4052{
4053 return surface->role_name;
4054}
4055
Pekka Paalanen8274d902014-08-06 19:36:51 +03004056WL_EXPORT void
4057weston_surface_set_label_func(struct weston_surface *surface,
4058 int (*desc)(struct weston_surface *,
4059 char *, size_t))
4060{
4061 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02004062 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004063}
4064
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004065/** Get the size of surface contents
4066 *
4067 * \param surface The surface to query.
4068 * \param width Returns the width of raw contents.
4069 * \param height Returns the height of raw contents.
4070 *
4071 * Retrieves the raw surface content size in pixels for the given surface.
4072 * This is the whole content size in buffer pixels. If the surface
4073 * has no content or the renderer does not implement this feature,
4074 * zeroes are returned.
4075 *
4076 * This function is used to determine the buffer size needed for
4077 * a weston_surface_copy_content() call.
4078 */
4079WL_EXPORT void
4080weston_surface_get_content_size(struct weston_surface *surface,
4081 int *width, int *height)
4082{
4083 struct weston_renderer *rer = surface->compositor->renderer;
4084
4085 if (!rer->surface_get_content_size) {
4086 *width = 0;
4087 *height = 0;
4088 return;
4089 }
4090
4091 rer->surface_get_content_size(surface, width, height);
4092}
4093
Quentin Glidic248dd102016-08-12 10:41:34 +02004094/** Get the bounding box of a surface and its subsurfaces
4095 *
4096 * \param surface The surface to query.
4097 * \return The bounding box relative to the surface origin.
4098 *
4099 */
4100WL_EXPORT struct weston_geometry
4101weston_surface_get_bounding_box(struct weston_surface *surface)
4102{
4103 pixman_region32_t region;
4104 pixman_box32_t *box;
4105 struct weston_subsurface *subsurface;
4106
4107 pixman_region32_init_rect(&region,
4108 0, 0,
4109 surface->width, surface->height);
4110
4111 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link)
4112 pixman_region32_union_rect(&region, &region,
4113 subsurface->position.x,
4114 subsurface->position.y,
4115 subsurface->surface->width,
4116 subsurface->surface->height);
4117
4118 box = pixman_region32_extents(&region);
4119 struct weston_geometry geometry = {
4120 .x = box->x1,
4121 .y = box->y1,
4122 .width = box->x2 - box->x1,
4123 .height = box->y2 - box->y1,
4124 };
4125
4126 pixman_region32_fini(&region);
4127
4128 return geometry;
4129}
4130
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004131/** Copy surface contents to system memory.
4132 *
4133 * \param surface The surface to copy from.
4134 * \param target Pointer to the target memory buffer.
4135 * \param size Size of the target buffer in bytes.
4136 * \param src_x X location on contents to copy from.
4137 * \param src_y Y location on contents to copy from.
4138 * \param width Width in pixels of the area to copy.
4139 * \param height Height in pixels of the area to copy.
4140 * \return 0 for success, -1 for failure.
4141 *
4142 * Surface contents are maintained by the renderer. They can be in a
4143 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
4144 * else.
4145 *
4146 * Surface contents are copied into memory pointed to by target,
4147 * which has size bytes of space available. The target memory
4148 * may be larger than needed, but being smaller returns an error.
4149 * The extra bytes in target may or may not be written; their content is
4150 * unspecified. Size must be large enough to hold the image.
4151 *
4152 * The image in the target memory will be arranged in rows from
4153 * top to bottom, and pixels on a row from left to right. The pixel
4154 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
4155 * width * 4.
4156 *
4157 * Parameters src_x and src_y define the upper-left corner in buffer
4158 * coordinates (pixels) to copy from. Parameters width and height
4159 * define the size of the area to copy in pixels.
4160 *
4161 * The rectangle defined by src_x, src_y, width, height must fit in
4162 * the surface contents. Otherwise an error is returned.
4163 *
Changwoo Chof97d2502017-08-05 00:30:47 +09004164 * Use weston_surface_get_content_size to determine the content size; the
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004165 * needed target buffer size and rectangle limits.
4166 *
4167 * CURRENT IMPLEMENTATION RESTRICTIONS:
4168 * - the machine must be little-endian due to Pixman formats.
4169 *
4170 * NOTE: Pixman formats are premultiplied.
4171 */
4172WL_EXPORT int
4173weston_surface_copy_content(struct weston_surface *surface,
4174 void *target, size_t size,
4175 int src_x, int src_y,
4176 int width, int height)
4177{
4178 struct weston_renderer *rer = surface->compositor->renderer;
4179 int cw, ch;
4180 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
4181
4182 if (!rer->surface_copy_content)
4183 return -1;
4184
4185 weston_surface_get_content_size(surface, &cw, &ch);
4186
4187 if (src_x < 0 || src_y < 0)
4188 return -1;
4189
4190 if (width <= 0 || height <= 0)
4191 return -1;
4192
4193 if (src_x + width > cw || src_y + height > ch)
4194 return -1;
4195
4196 if (width * bytespp * height > size)
4197 return -1;
4198
4199 return rer->surface_copy_content(surface, target, size,
4200 src_x, src_y, width, height);
4201}
4202
Pekka Paalanene67858b2013-04-25 13:57:42 +03004203static void
4204subsurface_set_position(struct wl_client *client,
4205 struct wl_resource *resource, int32_t x, int32_t y)
4206{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004207 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004208
4209 if (!sub)
4210 return;
4211
4212 sub->position.x = x;
4213 sub->position.y = y;
4214 sub->position.set = 1;
4215}
4216
4217static struct weston_subsurface *
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004218subsurface_find_sibling(struct weston_subsurface *sub,
4219 struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004220{
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004221 struct weston_surface *parent = sub->parent;
4222 struct weston_subsurface *sibling;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004223
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004224 wl_list_for_each(sibling, &parent->subsurface_list, parent_link) {
4225 if (sibling->surface == surface && sibling != sub)
4226 return sibling;
4227 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004228
4229 return NULL;
4230}
4231
4232static struct weston_subsurface *
4233subsurface_sibling_check(struct weston_subsurface *sub,
4234 struct weston_surface *surface,
4235 const char *request)
4236{
4237 struct weston_subsurface *sibling;
4238
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004239 sibling = subsurface_find_sibling(sub, surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004240 if (!sibling) {
4241 wl_resource_post_error(sub->resource,
4242 WL_SUBSURFACE_ERROR_BAD_SURFACE,
4243 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004244 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004245 return NULL;
4246 }
4247
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004248 assert(sibling->parent == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004249
4250 return sibling;
4251}
4252
4253static void
4254subsurface_place_above(struct wl_client *client,
4255 struct wl_resource *resource,
4256 struct wl_resource *sibling_resource)
4257{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004258 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004259 struct weston_surface *surface =
4260 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004261 struct weston_subsurface *sibling;
4262
4263 if (!sub)
4264 return;
4265
4266 sibling = subsurface_sibling_check(sub, surface, "place_above");
4267 if (!sibling)
4268 return;
4269
4270 wl_list_remove(&sub->parent_link_pending);
4271 wl_list_insert(sibling->parent_link_pending.prev,
4272 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004273
4274 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004275}
4276
4277static void
4278subsurface_place_below(struct wl_client *client,
4279 struct wl_resource *resource,
4280 struct wl_resource *sibling_resource)
4281{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004282 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004283 struct weston_surface *surface =
4284 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004285 struct weston_subsurface *sibling;
4286
4287 if (!sub)
4288 return;
4289
4290 sibling = subsurface_sibling_check(sub, surface, "place_below");
4291 if (!sibling)
4292 return;
4293
4294 wl_list_remove(&sub->parent_link_pending);
4295 wl_list_insert(&sibling->parent_link_pending,
4296 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004297
4298 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004299}
4300
4301static void
4302subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
4303{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004304 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004305
4306 if (sub)
4307 sub->synchronized = 1;
4308}
4309
4310static void
4311subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
4312{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004313 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004314
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004315 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03004316 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004317
4318 /* If sub became effectively desynchronized, flush. */
4319 if (!weston_subsurface_is_synchronized(sub))
4320 weston_subsurface_synchronized_commit(sub);
4321 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004322}
4323
4324static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03004325weston_subsurface_unlink_parent(struct weston_subsurface *sub)
4326{
4327 wl_list_remove(&sub->parent_link);
4328 wl_list_remove(&sub->parent_link_pending);
4329 wl_list_remove(&sub->parent_destroy_listener.link);
4330 sub->parent = NULL;
4331}
4332
4333static void
4334weston_subsurface_destroy(struct weston_subsurface *sub);
4335
4336static void
4337subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
4338{
4339 struct weston_subsurface *sub =
4340 container_of(listener, struct weston_subsurface,
4341 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004342 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004343
4344 /* The protocol object (wl_resource) is left inert. */
4345 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004346 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004347
4348 weston_subsurface_destroy(sub);
4349}
4350
4351static void
4352subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
4353{
4354 struct weston_subsurface *sub =
4355 container_of(listener, struct weston_subsurface,
4356 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004357 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004358 assert(sub->surface != sub->parent);
4359
4360 if (weston_surface_is_mapped(sub->surface))
4361 weston_surface_unmap(sub->surface);
4362
4363 weston_subsurface_unlink_parent(sub);
4364}
4365
4366static void
4367subsurface_resource_destroy(struct wl_resource *resource)
4368{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004369 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004370
4371 if (sub)
4372 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004373}
4374
4375static void
4376subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
4377{
4378 wl_resource_destroy(resource);
4379}
4380
4381static void
4382weston_subsurface_link_parent(struct weston_subsurface *sub,
4383 struct weston_surface *parent)
4384{
4385 sub->parent = parent;
4386 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004387 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004388 &sub->parent_destroy_listener);
4389
4390 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4391 wl_list_insert(&parent->subsurface_list_pending,
4392 &sub->parent_link_pending);
4393}
4394
4395static void
4396weston_subsurface_link_surface(struct weston_subsurface *sub,
4397 struct weston_surface *surface)
4398{
4399 sub->surface = surface;
4400 sub->surface_destroy_listener.notify =
4401 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004402 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004403 &sub->surface_destroy_listener);
4404}
4405
4406static void
4407weston_subsurface_destroy(struct weston_subsurface *sub)
4408{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004409 struct weston_view *view, *next;
4410
Pekka Paalanene67858b2013-04-25 13:57:42 +03004411 assert(sub->surface);
4412
4413 if (sub->resource) {
4414 assert(weston_surface_to_subsurface(sub->surface) == sub);
4415 assert(sub->parent_destroy_listener.notify ==
4416 subsurface_handle_parent_destroy);
4417
George Kiagiadakised04d382014-06-13 18:10:26 +02004418 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
4419 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004420 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02004421 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05004422
Pekka Paalanene67858b2013-04-25 13:57:42 +03004423 if (sub->parent)
4424 weston_subsurface_unlink_parent(sub);
4425
Jason Ekstrand7b982072014-05-20 14:33:03 -05004426 weston_surface_state_fini(&sub->cached);
4427 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004428
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004429 sub->surface->committed = NULL;
4430 sub->surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004431 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004432 } else {
4433 /* the dummy weston_subsurface for the parent itself */
4434 assert(sub->parent_destroy_listener.notify == NULL);
4435 wl_list_remove(&sub->parent_link);
4436 wl_list_remove(&sub->parent_link_pending);
4437 }
4438
4439 wl_list_remove(&sub->surface_destroy_listener.link);
4440 free(sub);
4441}
4442
4443static const struct wl_subsurface_interface subsurface_implementation = {
4444 subsurface_destroy,
4445 subsurface_set_position,
4446 subsurface_place_above,
4447 subsurface_place_below,
4448 subsurface_set_sync,
4449 subsurface_set_desync
4450};
4451
4452static struct weston_subsurface *
4453weston_subsurface_create(uint32_t id, struct weston_surface *surface,
4454 struct weston_surface *parent)
4455{
4456 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004457 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004458
Bryce Harringtonde16d892014-11-20 22:21:57 -08004459 sub = zalloc(sizeof *sub);
4460 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004461 return NULL;
4462
Jason Ekstranda7af7042013-10-12 22:38:11 -05004463 wl_list_init(&sub->unused_views);
4464
Jason Ekstranda85118c2013-06-27 20:17:02 -05004465 sub->resource =
4466 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004467 if (!sub->resource) {
4468 free(sub);
4469 return NULL;
4470 }
4471
Jason Ekstranda85118c2013-06-27 20:17:02 -05004472 wl_resource_set_implementation(sub->resource,
4473 &subsurface_implementation,
4474 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004475 weston_subsurface_link_surface(sub, surface);
4476 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05004477 weston_surface_state_init(&sub->cached);
4478 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004479 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004480
4481 return sub;
4482}
4483
4484/* Create a dummy subsurface for having the parent itself in its
4485 * sub-surface lists. Makes stacking order manipulation easy.
4486 */
4487static struct weston_subsurface *
4488weston_subsurface_create_for_parent(struct weston_surface *parent)
4489{
4490 struct weston_subsurface *sub;
4491
Bryce Harringtonde16d892014-11-20 22:21:57 -08004492 sub = zalloc(sizeof *sub);
4493 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004494 return NULL;
4495
4496 weston_subsurface_link_surface(sub, parent);
4497 sub->parent = parent;
4498 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4499 wl_list_insert(&parent->subsurface_list_pending,
4500 &sub->parent_link_pending);
4501
4502 return sub;
4503}
4504
4505static void
4506subcompositor_get_subsurface(struct wl_client *client,
4507 struct wl_resource *resource,
4508 uint32_t id,
4509 struct wl_resource *surface_resource,
4510 struct wl_resource *parent_resource)
4511{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004512 struct weston_surface *surface =
4513 wl_resource_get_user_data(surface_resource);
4514 struct weston_surface *parent =
4515 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004516 struct weston_subsurface *sub;
4517 static const char where[] = "get_subsurface: wl_subsurface@";
4518
4519 if (surface == parent) {
4520 wl_resource_post_error(resource,
4521 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4522 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004523 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004524 return;
4525 }
4526
4527 if (weston_surface_to_subsurface(surface)) {
4528 wl_resource_post_error(resource,
4529 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4530 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004531 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004532 return;
4533 }
4534
Pekka Paalanen50b67472014-10-01 15:02:41 +03004535 if (weston_surface_set_role(surface, "wl_subsurface", resource,
4536 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004537 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004538
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004539 if (weston_surface_get_main_surface(parent) == surface) {
4540 wl_resource_post_error(resource,
4541 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4542 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004543 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004544 return;
4545 }
4546
Pekka Paalanene67858b2013-04-25 13:57:42 +03004547 /* make sure the parent is in its own list */
4548 if (wl_list_empty(&parent->subsurface_list)) {
4549 if (!weston_subsurface_create_for_parent(parent)) {
4550 wl_resource_post_no_memory(resource);
4551 return;
4552 }
4553 }
4554
4555 sub = weston_subsurface_create(id, surface, parent);
4556 if (!sub) {
4557 wl_resource_post_no_memory(resource);
4558 return;
4559 }
4560
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004561 surface->committed = subsurface_committed;
4562 surface->committed_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004563 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004564}
4565
4566static void
4567subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
4568{
4569 wl_resource_destroy(resource);
4570}
4571
4572static const struct wl_subcompositor_interface subcompositor_interface = {
4573 subcompositor_destroy,
4574 subcompositor_get_subsurface
4575};
4576
4577static void
4578bind_subcompositor(struct wl_client *client,
4579 void *data, uint32_t version, uint32_t id)
4580{
4581 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004582 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004583
Jason Ekstranda85118c2013-06-27 20:17:02 -05004584 resource =
4585 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004586 if (resource == NULL) {
4587 wl_client_post_no_memory(client);
4588 return;
4589 }
4590 wl_resource_set_implementation(resource, &subcompositor_interface,
4591 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004592}
4593
Bryce Harrington0795ece2016-08-30 12:04:26 -07004594/** Set a DPMS mode on all of the compositor's outputs
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004595 *
4596 * \param compositor The compositor instance
4597 * \param state The DPMS state the outputs will be set to
4598 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004599static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004600weston_compositor_dpms(struct weston_compositor *compositor,
4601 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004602{
4603 struct weston_output *output;
4604
Bryce Harrington08976ac2016-08-30 12:05:16 -07004605 wl_list_for_each(output, &compositor->output_list, link)
4606 if (output->set_dpms)
4607 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004608}
4609
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004610/** Restores the compositor to active status
4611 *
4612 * \param compositor The compositor instance
4613 *
4614 * If the compositor was in a sleeping mode, all outputs are powered
4615 * back on via DPMS. Otherwise if the compositor was inactive
4616 * (idle/locked, offscreen, or sleeping) then the compositor's wake
4617 * signal will fire.
4618 *
4619 * Restarts the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004620 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004621 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004622WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004623weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004624{
Neil Roberts8b62e202013-09-30 13:14:47 +01004625 uint32_t old_state = compositor->state;
4626
4627 /* The state needs to be changed before emitting the wake
4628 * signal because that may try to schedule a repaint which
4629 * will not work if the compositor is still sleeping */
4630 compositor->state = WESTON_COMPOSITOR_ACTIVE;
4631
4632 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004633 case WESTON_COMPOSITOR_SLEEPING:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004634 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004635 case WESTON_COMPOSITOR_OFFSCREEN:
Daniel Stone893b9362016-11-08 15:47:09 +00004636 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004637 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004638 /* fall through */
4639 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004640 wl_event_source_timer_update(compositor->idle_source,
4641 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004642 }
4643}
4644
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004645/** Turns off rendering and frame events for the compositor.
4646 *
4647 * \param compositor The compositor instance
4648 *
4649 * This is used for example to prevent further rendering while the
4650 * compositor is shutting down.
4651 *
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004652 * Stops the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004653 *
4654 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004655 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004656WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004657weston_compositor_offscreen(struct weston_compositor *compositor)
4658{
4659 switch (compositor->state) {
4660 case WESTON_COMPOSITOR_OFFSCREEN:
4661 return;
4662 case WESTON_COMPOSITOR_SLEEPING:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004663 default:
4664 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4665 wl_event_source_timer_update(compositor->idle_source, 0);
4666 }
4667}
4668
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004669/** Powers down all attached output devices
4670 *
4671 * \param compositor The compositor instance
4672 *
4673 * Causes rendering to the outputs to cease, and no frame events to be
4674 * sent. Only powers down the outputs if the compositor is not already
4675 * in sleep mode.
4676 *
4677 * Stops the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004678 *
4679 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004680 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004681WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004682weston_compositor_sleep(struct weston_compositor *compositor)
4683{
4684 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
4685 return;
4686
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004687 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004688 compositor->state = WESTON_COMPOSITOR_SLEEPING;
4689 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
4690}
4691
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004692/** Sets compositor to idle mode
4693 *
4694 * \param data The compositor instance
4695 *
4696 * This is called when the idle timer fires. Once the compositor is in
4697 * idle mode it requires a wake action (e.g. via
4698 * weston_compositor_wake()) to restore it. The compositor's
4699 * idle_signal will be triggered when the idle event occurs.
4700 *
4701 * Idleness can be inhibited by setting the compositor's idle_inhibit
4702 * property.
4703 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004704static int
4705idle_handler(void *data)
4706{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004707 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004708
4709 if (compositor->idle_inhibit)
4710 return 1;
4711
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004712 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004713 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004714
4715 return 1;
4716}
4717
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004718WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08004719weston_plane_init(struct weston_plane *plane,
4720 struct weston_compositor *ec,
4721 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004722{
4723 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004724 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004725 plane->x = x;
4726 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08004727 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004728
4729 /* Init the link so that the call to wl_list_remove() when releasing
4730 * the plane without ever stacking doesn't lead to a crash */
4731 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004732}
4733
4734WL_EXPORT void
4735weston_plane_release(struct weston_plane *plane)
4736{
Xiong Zhang97116532013-10-23 13:58:31 +08004737 struct weston_view *view;
4738
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004739 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004740 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004741
Xiong Zhang97116532013-10-23 13:58:31 +08004742 wl_list_for_each(view, &plane->compositor->view_list, link) {
4743 if (view->plane == plane)
4744 view->plane = NULL;
4745 }
4746
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004747 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004748}
4749
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004750/** weston_compositor_stack_plane
4751 * \ingroup compositor
4752 */
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004753WL_EXPORT void
4754weston_compositor_stack_plane(struct weston_compositor *ec,
4755 struct weston_plane *plane,
4756 struct weston_plane *above)
4757{
4758 if (above)
4759 wl_list_insert(above->link.prev, &plane->link);
4760 else
4761 wl_list_insert(&ec->plane_list, &plane->link);
4762}
4763
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004764static void
4765output_release(struct wl_client *client, struct wl_resource *resource)
4766{
4767 wl_resource_destroy(resource);
4768}
4769
4770static const struct wl_output_interface output_interface = {
4771 output_release,
4772};
4773
4774
Casey Dahlin9074db52012-04-19 22:50:09 -04004775static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004776{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004777 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004778}
4779
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004780static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004781bind_output(struct wl_client *client,
4782 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05004783{
Pekka Paalanen05347622017-03-27 12:24:34 +03004784 struct weston_head *head = data;
4785 struct weston_output *output = head->output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004786 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004787 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004788
Jason Ekstranda85118c2013-06-27 20:17:02 -05004789 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004790 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004791 if (resource == NULL) {
4792 wl_client_post_no_memory(client);
4793 return;
4794 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004795
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03004796 wl_list_insert(&head->resource_list, wl_resource_get_link(resource));
Pekka Paalanen055c1132017-03-27 16:31:25 +03004797 wl_resource_set_implementation(resource, &output_interface, head,
Pekka Paalanen05347622017-03-27 12:24:34 +03004798 unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04004799
Pekka Paalanen05347622017-03-27 12:24:34 +03004800 assert(output);
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004801 wl_output_send_geometry(resource,
4802 output->x,
4803 output->y,
Pekka Paalanen01f60212017-03-24 15:39:24 +02004804 head->mm_width,
4805 head->mm_height,
4806 head->subpixel,
4807 head->make, head->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04004808 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004809 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004810 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02004811 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004812
4813 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004814 wl_output_send_mode(resource,
4815 mode->flags,
4816 mode->width,
4817 mode->height,
4818 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004819 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02004820
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004821 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004822 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004823}
4824
Pekka Paalanendcac3512017-12-08 14:13:34 +02004825static void
4826weston_head_add_global(struct weston_head *head)
4827{
4828 head->global = wl_global_create(head->compositor->wl_display,
4829 &wl_output_interface, 3,
4830 head, bind_output);
4831}
4832
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02004833/** Remove the global wl_output protocol object
4834 *
4835 * \param head The head whose global to remove.
4836 *
4837 * Also orphans the wl_resources for this head (wl_output).
4838 */
4839static void
4840weston_head_remove_global(struct weston_head *head)
4841{
4842 struct wl_resource *resource, *tmp;
4843
4844 if (head->global)
4845 wl_global_destroy(head->global);
4846 head->global = NULL;
4847
4848 wl_resource_for_each_safe(resource, tmp, &head->resource_list) {
4849 unbind_resource(resource);
4850 wl_resource_set_destructor(resource, NULL);
4851 wl_resource_set_user_data(resource, NULL);
4852 }
Roman Gilge97391c2019-03-29 13:01:06 +01004853
4854 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
4855 /* It's sufficient to unset the destructor, then the list elements
4856 * won't be accessed.
4857 */
4858 wl_resource_set_destructor(resource, NULL);
4859 }
4860 wl_list_init(&head->xdg_output_resource_list);
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02004861}
4862
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004863/** Get the backing object of wl_output
4864 *
4865 * \param resource A wl_output protocol object.
4866 * \return The backing object (user data) of a wl_resource representing a
4867 * wl_output protocol object.
Marius Vlad78984ee2019-06-11 00:05:08 +03004868 *
4869 * \ingroup head
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004870 */
Pekka Paalanen055c1132017-03-27 16:31:25 +03004871WL_EXPORT struct weston_head *
4872weston_head_from_resource(struct wl_resource *resource)
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004873{
4874 assert(wl_resource_instance_of(resource, &wl_output_interface,
4875 &output_interface));
4876
4877 return wl_resource_get_user_data(resource);
4878}
4879
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004880/** Initialize a pre-allocated weston_head
4881 *
4882 * \param head The head to initialize.
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004883 * \param name The head name, e.g. the connector name or equivalent.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004884 *
4885 * The head will be safe to attach, detach and release.
4886 *
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004887 * The name is used in logs, and can be used by compositors as a configuration
4888 * identifier.
4889 *
Marius Vlad78984ee2019-06-11 00:05:08 +03004890 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004891 * \internal
4892 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004893WL_EXPORT void
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004894weston_head_init(struct weston_head *head, const char *name)
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004895{
4896 /* Add some (in)sane defaults which can be used
4897 * for checking if an output was properly configured
4898 */
4899 memset(head, 0, sizeof *head);
4900
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004901 wl_list_init(&head->compositor_link);
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03004902 wl_signal_init(&head->destroy_signal);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004903 wl_list_init(&head->output_link);
4904 wl_list_init(&head->resource_list);
Roman Gilge97391c2019-03-29 13:01:06 +01004905 wl_list_init(&head->xdg_output_resource_list);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004906 head->name = strdup(name);
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05304907 head->current_protection = WESTON_HDCP_DISABLE;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004908}
4909
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004910/** Send output heads changed signal
4911 *
4912 * \param output The output that changed.
4913 *
4914 * Notify that the enabled output gained and/or lost heads, or that the
4915 * associated heads may have changed their connection status. This does not
4916 * include cases where the output becomes enabled or disabled. The registered
4917 * callbacks are called after the change has successfully happened.
4918 *
4919 * If connection status change causes the compositor to attach or detach a head
4920 * to an enabled output, the registered callbacks may be called multiple times.
Marius Vlad55d87362019-06-11 01:15:35 +03004921 *
4922 * \ingroup output
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004923 */
4924static void
4925weston_output_emit_heads_changed(struct weston_output *output)
4926{
4927 wl_signal_emit(&output->compositor->output_heads_changed_signal,
4928 output);
4929}
4930
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004931/** Idle task for emitting heads_changed_signal */
4932static void
4933weston_compositor_call_heads_changed(void *data)
4934{
4935 struct weston_compositor *compositor = data;
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004936 struct weston_head *head;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004937
4938 compositor->heads_changed_source = NULL;
4939
4940 wl_signal_emit(&compositor->heads_changed_signal, compositor);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004941
4942 wl_list_for_each(head, &compositor->head_list, compositor_link) {
4943 if (head->output && head->output->enabled)
4944 weston_output_emit_heads_changed(head->output);
4945 }
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004946}
4947
4948/** Schedule a call on idle to heads_changed callback
4949 *
4950 * \param compositor The Compositor.
4951 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004952 * \ingroup compositor
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004953 * \internal
4954 */
4955static void
4956weston_compositor_schedule_heads_changed(struct weston_compositor *compositor)
4957{
4958 struct wl_event_loop *loop;
4959
4960 if (compositor->heads_changed_source)
4961 return;
4962
4963 loop = wl_display_get_event_loop(compositor->wl_display);
4964 compositor->heads_changed_source = wl_event_loop_add_idle(loop,
4965 weston_compositor_call_heads_changed, compositor);
4966}
4967
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004968/** Register a new head
4969 *
4970 * \param compositor The compositor.
4971 * \param head The head to register, must not be already registered.
4972 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004973 * This signals the core that a new head has become available, leading to
4974 * heads_changed hook being called later.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004975 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004976 * \ingroup compositor
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004977 * \internal
4978 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004979WL_EXPORT void
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004980weston_compositor_add_head(struct weston_compositor *compositor,
4981 struct weston_head *head)
4982{
4983 assert(wl_list_empty(&head->compositor_link));
4984 assert(head->name);
4985
4986 wl_list_insert(compositor->head_list.prev, &head->compositor_link);
4987 head->compositor = compositor;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004988 weston_compositor_schedule_heads_changed(compositor);
4989}
4990
4991/** Adds a listener to be called when heads change
4992 *
4993 * \param compositor The compositor.
4994 * \param listener The listener to add.
4995 *
Marius Vlada2dace22019-06-12 16:05:44 +03004996 * The listener notify function argument is weston_compositor.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004997 *
4998 * The listener function will be called after heads are added or their
4999 * connection status has changed. Several changes may be accumulated into a
5000 * single call. The user is expected to iterate over the existing heads and
5001 * check their statuses to find out what changed.
5002 *
5003 * \sa weston_compositor_iterate_heads, weston_head_is_connected,
5004 * weston_head_is_enabled
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005005 * \ingroup compositor
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005006 */
5007WL_EXPORT void
5008weston_compositor_add_heads_changed_listener(struct weston_compositor *compositor,
5009 struct wl_listener *listener)
5010{
5011 wl_signal_add(&compositor->heads_changed_signal, listener);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005012}
5013
5014/** Iterate over available heads
5015 *
5016 * \param compositor The compositor.
Marius Vlada2dace22019-06-12 16:05:44 +03005017 * \param iter The iterator, or NULL for start.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005018 * \return The next available head in the list.
5019 *
5020 * Returns all available heads, regardless of being connected or enabled.
5021 *
5022 * You can iterate over all heads as follows:
5023 * \code
5024 * struct weston_head *head = NULL;
5025 *
5026 * while ((head = weston_compositor_iterate_heads(compositor, head))) {
5027 * ...
5028 * }
5029 * \endcode
5030 *
5031 * If you cause \c iter to be removed from the list, you cannot use it to
5032 * continue iterating. Removing any other item is safe.
5033 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005034 * \ingroup compositor
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005035 */
5036WL_EXPORT struct weston_head *
5037weston_compositor_iterate_heads(struct weston_compositor *compositor,
5038 struct weston_head *iter)
5039{
5040 struct wl_list *list = &compositor->head_list;
5041 struct wl_list *node;
5042
5043 assert(compositor);
5044 assert(!iter || iter->compositor == compositor);
5045
5046 if (iter)
5047 node = iter->compositor_link.next;
5048 else
5049 node = list->next;
5050
5051 assert(node);
5052 assert(!iter || node != &iter->compositor_link);
5053
5054 if (node == list)
5055 return NULL;
5056
5057 return container_of(node, struct weston_head, compositor_link);
5058}
5059
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005060/** Iterate over attached heads
5061 *
5062 * \param output The output whose heads to iterate.
Marius Vlada2dace22019-06-12 16:05:44 +03005063 * \param iter The iterator, or NULL for start.
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005064 * \return The next attached head in the list.
5065 *
5066 * Returns all heads currently attached to the output.
5067 *
5068 * You can iterate over all heads as follows:
5069 * \code
5070 * struct weston_head *head = NULL;
5071 *
5072 * while ((head = weston_output_iterate_heads(output, head))) {
5073 * ...
5074 * }
5075 * \endcode
5076 *
5077 * If you cause \c iter to be removed from the list, you cannot use it to
5078 * continue iterating. Removing any other item is safe.
5079 *
Marius Vlad55d87362019-06-11 01:15:35 +03005080 * \ingroup ouput
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005081 */
5082WL_EXPORT struct weston_head *
5083weston_output_iterate_heads(struct weston_output *output,
5084 struct weston_head *iter)
5085{
5086 struct wl_list *list = &output->head_list;
5087 struct wl_list *node;
5088
5089 assert(output);
5090 assert(!iter || iter->output == output);
5091
5092 if (iter)
5093 node = iter->output_link.next;
5094 else
5095 node = list->next;
5096
5097 assert(node);
5098 assert(!iter || node != &iter->output_link);
5099
5100 if (node == list)
5101 return NULL;
5102
5103 return container_of(node, struct weston_head, output_link);
5104}
5105
Pekka Paalanendcac3512017-12-08 14:13:34 +02005106/** Attach a head to an output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005107 *
5108 * \param output The output to attach to.
5109 * \param head The head that is not yet attached.
5110 * \return 0 on success, -1 on failure.
5111 *
5112 * Attaches the given head to the output. All heads of an output are clones
5113 * and share the resolution and timings.
5114 *
5115 * Cloning heads this way uses less resources than creating an output for
5116 * each head, but is not always possible due to environment, driver and hardware
5117 * limitations.
5118 *
5119 * On failure, the head remains unattached. Success of this function does not
5120 * guarantee the output configuration is actually valid. The final checks are
Pekka Paalanendcac3512017-12-08 14:13:34 +02005121 * made on weston_output_enable() unless the output was already enabled.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005122 *
Marius Vlad55d87362019-06-11 01:15:35 +03005123 * \ingroup output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005124 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005125WL_EXPORT int
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005126weston_output_attach_head(struct weston_output *output,
5127 struct weston_head *head)
5128{
Pekka Paalanendcac3512017-12-08 14:13:34 +02005129 char *head_names;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005130
5131 if (!wl_list_empty(&head->output_link))
5132 return -1;
5133
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005134 if (output->attach_head) {
5135 if (output->attach_head(output, head) < 0)
5136 return -1;
5137 } else if (!wl_list_empty(&output->head_list)) {
5138 /* No support for clones in the legacy path. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005139 return -1;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005140 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005141
5142 head->output = output;
5143 wl_list_insert(output->head_list.prev, &head->output_link);
5144
Pekka Paalanendcac3512017-12-08 14:13:34 +02005145 if (output->enabled) {
5146 weston_head_add_global(head);
5147
5148 head_names = weston_output_create_heads_string(output);
5149 weston_log("Output '%s' updated to have head(s) %s\n",
5150 output->name, head_names);
5151 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005152
5153 weston_output_emit_heads_changed(output);
Pekka Paalanendcac3512017-12-08 14:13:34 +02005154 }
5155
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005156 return 0;
5157}
5158
5159/** Detach a head from its output
5160 *
5161 * \param head The head to detach.
5162 *
5163 * It is safe to detach a non-attached head.
5164 *
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005165 * If the head is attached to an enabled output and the output will be left
5166 * with no heads, the output will be disabled.
5167 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005168 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005169 * \sa weston_output_disable
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005170 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005171WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005172weston_head_detach(struct weston_head *head)
5173{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005174 struct weston_output *output = head->output;
Pekka Paalanena0106992017-12-08 16:11:17 +02005175 char *head_names;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005176
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005177 wl_list_remove(&head->output_link);
5178 wl_list_init(&head->output_link);
5179 head->output = NULL;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005180
5181 if (!output)
5182 return;
5183
5184 if (output->detach_head)
5185 output->detach_head(output, head);
5186
5187 if (output->enabled) {
5188 weston_head_remove_global(head);
5189
Pekka Paalanena0106992017-12-08 16:11:17 +02005190 if (wl_list_empty(&output->head_list)) {
5191 weston_log("Output '%s' no heads left, disabling.\n",
5192 output->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005193 weston_output_disable(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02005194 } else {
5195 head_names = weston_output_create_heads_string(output);
5196 weston_log("Output '%s' updated to have head(s) %s\n",
5197 output->name, head_names);
5198 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005199
5200 weston_output_emit_heads_changed(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02005201 }
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005202 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005203}
5204
5205/** Destroy a head
5206 *
5207 * \param head The head to be released.
5208 *
5209 * Destroys the head. The caller is responsible for freeing the memory pointed
5210 * to by \c head.
5211 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005212 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005213 * \internal
5214 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005215WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005216weston_head_release(struct weston_head *head)
5217{
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005218 wl_signal_emit(&head->destroy_signal, head);
5219
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005220 weston_head_detach(head);
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005221
5222 free(head->make);
5223 free(head->model);
5224 free(head->serial_number);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005225 free(head->name);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005226
5227 wl_list_remove(&head->compositor_link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005228}
5229
Pekka Paalanene19970f2017-08-28 14:11:02 +03005230static void
5231weston_head_set_device_changed(struct weston_head *head)
5232{
5233 head->device_changed = true;
5234
5235 if (head->compositor)
5236 weston_compositor_schedule_heads_changed(head->compositor);
5237}
5238
5239/** String equal comparison with NULLs being equal */
5240static bool
5241str_null_eq(const char *a, const char *b)
5242{
5243 if (!a && !b)
5244 return true;
5245
5246 if (!!a != !!b)
5247 return false;
5248
5249 return strcmp(a, b) == 0;
5250}
5251
Pekka Paalanen01f60212017-03-24 15:39:24 +02005252/** Store monitor make, model and serial number
5253 *
5254 * \param head The head to modify.
5255 * \param make The monitor make. If EDID is available, the PNP ID. Otherwise
5256 * any string, or NULL for none.
5257 * \param model The monitor model or name, or a made-up string, or NULL for
5258 * none.
5259 * \param serialno The monitor serial number, a made-up string, or NULL for
5260 * none.
5261 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005262 * This may set the device_changed flag.
5263 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005264 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005265 * \internal
5266 */
5267WL_EXPORT void
5268weston_head_set_monitor_strings(struct weston_head *head,
5269 const char *make,
5270 const char *model,
5271 const char *serialno)
5272{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005273 if (str_null_eq(head->make, make) &&
5274 str_null_eq(head->model, model) &&
5275 str_null_eq(head->serial_number, serialno))
5276 return;
5277
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005278 free(head->make);
5279 free(head->model);
5280 free(head->serial_number);
5281
5282 head->make = make ? strdup(make) : NULL;
5283 head->model = model ? strdup(model) : NULL;
5284 head->serial_number = serialno ? strdup(serialno) : NULL;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005285
5286 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005287}
5288
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005289/** Store display non-desktop status
5290 *
5291 * \param head The head to modify.
5292 * \param non_desktop Whether the head connects to a non-desktop display.
5293 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005294 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005295 * \internal
5296 */
5297WL_EXPORT void
5298weston_head_set_non_desktop(struct weston_head *head, bool non_desktop)
5299{
5300 if (head->non_desktop == non_desktop)
5301 return;
5302
5303 head->non_desktop = non_desktop;
5304
5305 weston_head_set_device_changed(head);
5306}
5307
Pekka Paalanen01f60212017-03-24 15:39:24 +02005308/** Store physical image size
5309 *
5310 * \param head The head to modify.
5311 * \param mm_width Image area width in millimeters.
5312 * \param mm_height Image area height in millimeters.
5313 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005314 * This may set the device_changed flag.
5315 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005316 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005317 * \internal
5318 */
5319WL_EXPORT void
5320weston_head_set_physical_size(struct weston_head *head,
5321 int32_t mm_width, int32_t mm_height)
5322{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005323 if (head->mm_width == mm_width &&
5324 head->mm_height == mm_height)
5325 return;
5326
Pekka Paalanen01f60212017-03-24 15:39:24 +02005327 head->mm_width = mm_width;
5328 head->mm_height = mm_height;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005329
5330 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005331}
5332
5333/** Store monitor sub-pixel layout
5334 *
5335 * \param head The head to modify.
5336 * \param sp Sub-pixel layout. The possible values are:
5337 * - WL_OUTPUT_SUBPIXEL_UNKNOWN,
5338 * - WL_OUTPUT_SUBPIXEL_NONE,
5339 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB,
5340 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR,
5341 * - WL_OUTPUT_SUBPIXEL_VERTICAL_RGB,
5342 * - WL_OUTPUT_SUBPIXEL_VERTICAL_BGR
5343 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005344 * This may set the device_changed flag.
5345 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005346 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005347 * \internal
5348 */
5349WL_EXPORT void
5350weston_head_set_subpixel(struct weston_head *head,
5351 enum wl_output_subpixel sp)
5352{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005353 if (head->subpixel == sp)
5354 return;
5355
Pekka Paalanen01f60212017-03-24 15:39:24 +02005356 head->subpixel = sp;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005357
5358 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005359}
5360
5361/** Mark the monitor as internal
5362 *
5363 * This is used for embedded screens, like laptop panels.
5364 *
5365 * \param head The head to mark as internal.
5366 *
5367 * By default a head is external. The type is often inferred from the physical
5368 * connector type.
5369 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005370 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005371 * \internal
5372 */
5373WL_EXPORT void
5374weston_head_set_internal(struct weston_head *head)
5375{
5376 head->connection_internal = true;
5377}
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005378
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005379/** Store connector status
5380 *
5381 * \param head The head to modify.
5382 * \param connected Whether the head is connected.
5383 *
5384 * Connectors are created as disconnected. This function can be used to
5385 * set the connector status.
5386 *
5387 * The status should be set to true when a physical connector is connected to
5388 * a video sink device like a monitor and to false when the connector is
5389 * disconnected. For nested backends, the connection status should reflect the
5390 * connection to the parent display server.
5391 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005392 * When the connection status changes, it schedules a call to the heads_changed
Pekka Paalanene19970f2017-08-28 14:11:02 +03005393 * hook and sets the device_changed flag.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005394 *
5395 * \sa weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005396 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005397 * \internal
5398 */
5399WL_EXPORT void
5400weston_head_set_connection_status(struct weston_head *head, bool connected)
5401{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005402 if (head->connected == connected)
5403 return;
5404
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005405 head->connected = connected;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005406
Pekka Paalanene19970f2017-08-28 14:11:02 +03005407 weston_head_set_device_changed(head);
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005408}
5409
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305410static void
5411weston_output_compute_protection(struct weston_output *output)
5412{
5413 struct weston_head *head;
5414 enum weston_hdcp_protection op_protection;
5415 bool op_protection_valid = false;
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305416 struct weston_compositor *wc = output->compositor;
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305417
5418 wl_list_for_each(head, &output->head_list, output_link) {
5419 if (!op_protection_valid) {
5420 op_protection = head->current_protection;
5421 op_protection_valid = true;
5422 }
5423 if (head->current_protection < op_protection)
5424 op_protection = head->current_protection;
5425 }
5426
5427 if (!op_protection_valid)
5428 op_protection = WESTON_HDCP_DISABLE;
5429
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305430 if (output->current_protection != op_protection) {
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305431 output->current_protection = op_protection;
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305432 weston_output_damage(output);
Ankit Nautiyalfaa5ab42019-07-08 13:23:24 +05305433 weston_schedule_surface_protection_update(wc);
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305434 }
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305435}
5436
5437WL_EXPORT void
5438weston_head_set_content_protection_status(struct weston_head *head,
5439 enum weston_hdcp_protection status)
5440{
5441 head->current_protection = status;
5442 if (head->output)
5443 weston_output_compute_protection(head->output);
5444}
5445
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005446/** Is the head currently connected?
5447 *
5448 * \param head The head to query.
5449 * \return Connection status.
5450 *
5451 * Returns true if the head is physically connected to a monitor, or in
5452 * case of a nested backend returns true when there is a connection to the
5453 * parent display server.
5454 *
5455 * This is independent from the head being enabled.
5456 *
5457 * \sa weston_head_is_enabled
Marius Vlad78984ee2019-06-11 00:05:08 +03005458 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005459 */
5460WL_EXPORT bool
5461weston_head_is_connected(struct weston_head *head)
5462{
5463 return head->connected;
5464}
5465
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005466/** Is the head currently enabled?
5467 *
5468 * \param head The head to query.
5469 * \return Video status.
5470 *
5471 * Returns true if the head is currently transmitting a video stream.
5472 *
5473 * This is independent of the head being connected.
5474 *
5475 * \sa weston_head_is_connected
Marius Vlad78984ee2019-06-11 00:05:08 +03005476 * \ingroup head
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005477 */
5478WL_EXPORT bool
5479weston_head_is_enabled(struct weston_head *head)
5480{
5481 if (!head->output)
5482 return false;
5483
5484 return head->output->enabled;
5485}
5486
Pekka Paalanene19970f2017-08-28 14:11:02 +03005487/** Has the device information changed?
5488 *
5489 * \param head The head to query.
5490 * \return True if the device information has changed since last reset.
5491 *
5492 * The information about the connected display device, e.g. a monitor, may
5493 * change without being disconnected in between. Changing information
5494 * causes a call to the heads_changed hook.
5495 *
5496 * The information includes make, model, serial number, physical size,
5497 * and sub-pixel type. The connection status is also included.
5498 *
5499 * \sa weston_head_reset_device_changed, weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005500 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03005501 */
5502WL_EXPORT bool
5503weston_head_is_device_changed(struct weston_head *head)
5504{
5505 return head->device_changed;
5506}
5507
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005508/** Does the head represent a non-desktop display?
5509 *
5510 * \param head The head to query.
5511 * \return True if the device is a non-desktop display.
5512 *
5513 * Non-desktop heads are not attached to outputs by default.
5514 * This stops weston from extending the desktop onto head mounted displays.
5515 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005516 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005517 */
5518WL_EXPORT bool
5519weston_head_is_non_desktop(struct weston_head *head)
5520{
5521 return head->non_desktop;
5522}
5523
Pekka Paalanene19970f2017-08-28 14:11:02 +03005524/** Acknowledge device information change
5525 *
5526 * \param head The head to acknowledge.
5527 *
5528 * Clears the device changed flag on this head. When a compositor has processed
5529 * device information, it should call this to be able to notice further
5530 * changes.
5531 *
5532 * \sa weston_head_is_device_changed
Marius Vlad78984ee2019-06-11 00:05:08 +03005533 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03005534 */
5535WL_EXPORT void
5536weston_head_reset_device_changed(struct weston_head *head)
5537{
5538 head->device_changed = false;
5539}
5540
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005541/** Get the name of a head
5542 *
5543 * \param head The head to query.
5544 * \return The head's name, not NULL.
5545 *
5546 * The name depends on the backend. The DRM backend uses connector names,
5547 * other backends may use hardcoded names or user-given names.
Marius Vlad78984ee2019-06-11 00:05:08 +03005548 *
5549 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005550 */
5551WL_EXPORT const char *
5552weston_head_get_name(struct weston_head *head)
5553{
5554 return head->name;
5555}
5556
5557/** Get the output the head is attached to
5558 *
5559 * \param head The head to query.
5560 * \return The output the head is attached to, or NULL if detached.
Marius Vlad78984ee2019-06-11 00:05:08 +03005561 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005562 */
5563WL_EXPORT struct weston_output *
5564weston_head_get_output(struct weston_head *head)
5565{
5566 return head->output;
5567}
5568
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005569/** Add destroy callback for a head
5570 *
5571 * \param head The head to watch for.
5572 * \param listener The listener to add. The \c notify member must be set.
5573 *
5574 * Heads may get destroyed for various reasons by the backends. If a head is
5575 * attached to an output, the compositor should listen for head destruction
5576 * and reconfigure or destroy the output if necessary.
5577 *
5578 * The destroy callbacks will be called on weston_head destruction before any
5579 * automatic detaching from an associated weston_output and before any
5580 * weston_head information is lost.
5581 *
5582 * The \c data argument to the notify callback is the weston_head being
5583 * destroyed.
Marius Vlad78984ee2019-06-11 00:05:08 +03005584 *
5585 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005586 */
5587WL_EXPORT void
5588weston_head_add_destroy_listener(struct weston_head *head,
5589 struct wl_listener *listener)
5590{
5591 wl_signal_add(&head->destroy_signal, listener);
5592}
5593
5594/** Look up destroy listener for a head
5595 *
5596 * \param head The head to query.
5597 * \param notify The notify function used used for the added destroy listener.
5598 * \return The listener, or NULL if not found.
5599 *
5600 * This looks up the previously added destroy listener struct based on the
5601 * notify function it has. The listener can be used to access user data
5602 * through \c container_of().
5603 *
5604 * \sa wl_signal_get()
Marius Vlad78984ee2019-06-11 00:05:08 +03005605 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005606 */
5607WL_EXPORT struct wl_listener *
5608weston_head_get_destroy_listener(struct weston_head *head,
5609 wl_notify_func_t notify)
5610{
5611 return wl_signal_get(&head->destroy_signal, notify);
5612}
5613
David Fort0de859e2016-05-27 23:22:57 +02005614/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005615static void
David Fort0de859e2016-05-27 23:22:57 +02005616weston_compositor_reflow_outputs(struct weston_compositor *compositor,
5617 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005618{
5619 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02005620 bool start_resizing = false;
5621
5622 if (!delta_width)
5623 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005624
5625 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02005626 if (output == resized_output) {
5627 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005628 continue;
5629 }
5630
David Fort0de859e2016-05-27 23:22:57 +02005631 if (start_resizing) {
5632 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005633 output->dirty = 1;
5634 }
5635 }
5636}
5637
Pekka Paalanend72bad22017-03-29 17:01:41 +03005638static void
Scott Moreauccbf29d2012-02-22 14:21:41 -07005639weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005640{
Scott Moreau850ca422012-05-21 15:21:25 -06005641 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05005642
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005643 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005644 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06005645
Scott Moreauccbf29d2012-02-22 14:21:41 -07005646 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06005647 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005648 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005649 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005650 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005651 weston_matrix_scale(&output->matrix, magnification,
5652 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005653 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005654
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005655 switch (output->transform) {
5656 case WL_OUTPUT_TRANSFORM_FLIPPED:
5657 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5658 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5659 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5660 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5661 weston_matrix_scale(&output->matrix, -1, 1, 1);
5662 break;
5663 }
5664
5665 switch (output->transform) {
5666 default:
5667 case WL_OUTPUT_TRANSFORM_NORMAL:
5668 case WL_OUTPUT_TRANSFORM_FLIPPED:
5669 break;
5670 case WL_OUTPUT_TRANSFORM_90:
5671 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5672 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
5673 weston_matrix_rotate_xy(&output->matrix, 0, 1);
5674 break;
5675 case WL_OUTPUT_TRANSFORM_180:
5676 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5677 weston_matrix_translate(&output->matrix,
5678 -output->width, -output->height, 0);
5679 weston_matrix_rotate_xy(&output->matrix, -1, 0);
5680 break;
5681 case WL_OUTPUT_TRANSFORM_270:
5682 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5683 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5684 weston_matrix_rotate_xy(&output->matrix, 0, -1);
5685 break;
5686 }
5687
5688 if (output->current_scale != 1)
5689 weston_matrix_scale(&output->matrix,
5690 output->current_scale,
5691 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01005692
Scott Moreauccbf29d2012-02-22 14:21:41 -07005693 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05005694
5695 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005696}
5697
Scott Moreau1bad5db2012-08-18 01:04:05 -06005698static void
Alexander Larsson0b135062013-05-28 16:23:36 +02005699weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06005700{
5701 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005702 output->native_scale = scale;
5703 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06005704
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005705 convert_size_by_transform_scale(&output->width, &output->height,
5706 output->current_mode->width,
5707 output->current_mode->height,
5708 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02005709}
5710
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005711static void
5712weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07005713{
5714 output->x = x;
5715 output->y = y;
5716
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005717 pixman_region32_fini(&output->previous_damage);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02005718 pixman_region32_init(&output->previous_damage);
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005719
5720 pixman_region32_fini(&output->region);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005721 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06005722 output->width,
5723 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005724}
5725
Marius Vlad55d87362019-06-11 01:15:35 +03005726/**
5727 * \ingroup output
5728 */
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005729WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005730weston_output_move(struct weston_output *output, int x, int y)
5731{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005732 struct weston_head *head;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005733 struct wl_resource *resource;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005734 int ver;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005735
5736 output->move_x = x - output->x;
5737 output->move_y = y - output->y;
5738
5739 if (output->move_x == 0 && output->move_y == 0)
5740 return;
5741
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005742 weston_output_init_geometry(output, x, y);
5743
5744 output->dirty = 1;
5745
5746 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005747 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005748
5749 /* Notify clients of the change for output position. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005750 wl_list_for_each(head, &output->head_list, output_link) {
5751 wl_resource_for_each(resource, &head->resource_list) {
5752 wl_output_send_geometry(resource,
5753 output->x,
5754 output->y,
5755 head->mm_width,
5756 head->mm_height,
5757 head->subpixel,
5758 head->make,
5759 head->model,
5760 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08005761
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005762 ver = wl_resource_get_version(resource);
5763 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
5764 wl_output_send_done(resource);
5765 }
Roman Gilge97391c2019-03-29 13:01:06 +01005766
5767 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
5768 zxdg_output_v1_send_logical_position(resource,
5769 output->x,
5770 output->y);
5771 zxdg_output_v1_send_done(resource);
5772 }
Quanxian Wangb2c86362014-03-14 09:16:25 +08005773 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005774}
5775
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005776/** Signal that a pending output is taken into use.
5777 *
5778 * Removes the output from the pending list and adds it to the compositor's
5779 * list of enabled outputs. The output created signal is emitted.
Giulio Camuffob1147152015-05-06 21:41:57 +03005780 *
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005781 * The output gets an internal ID assigned, and the wl_output global is
5782 * created.
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005783 *
Giulio Camuffob1147152015-05-06 21:41:57 +03005784 * \param compositor The compositor instance.
5785 * \param output The output to be added.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005786 *
5787 * \internal
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005788 * \ingroup compositor
Giulio Camuffob1147152015-05-06 21:41:57 +03005789 */
Pekka Paalanenf9681b52017-03-29 16:58:48 +03005790static void
Giulio Camuffob1147152015-05-06 21:41:57 +03005791weston_compositor_add_output(struct weston_compositor *compositor,
5792 struct weston_output *output)
5793{
Armin Krezoviće5403842016-08-05 15:28:29 +02005794 struct weston_view *view, *next;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005795 struct weston_head *head;
Armin Krezoviće5403842016-08-05 15:28:29 +02005796
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005797 assert(!output->enabled);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005798
5799 /* Verify we haven't reached the limit of 32 available output IDs */
5800 assert(ffs(~compositor->output_id_pool) > 0);
5801
5802 /* Invert the output id pool and look for the lowest numbered
5803 * switch (the least significant bit). Take that bit's position
5804 * as our ID, and mark it used in the compositor's output_id_pool.
5805 */
5806 output->id = ffs(~compositor->output_id_pool) - 1;
5807 compositor->output_id_pool |= 1u << output->id;
5808
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005809 wl_list_remove(&output->link);
Giulio Camuffob1147152015-05-06 21:41:57 +03005810 wl_list_insert(compositor->output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005811 output->enabled = true;
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005812
Pekka Paalanendcac3512017-12-08 14:13:34 +02005813 wl_list_for_each(head, &output->head_list, output_link)
5814 weston_head_add_global(head);
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005815
Giulio Camuffob1147152015-05-06 21:41:57 +03005816 wl_signal_emit(&compositor->output_created_signal, output);
Armin Krezoviće5403842016-08-05 15:28:29 +02005817
5818 wl_list_for_each_safe(view, next, &compositor->view_list, link)
5819 weston_view_geometry_dirty(view);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005820}
5821
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005822/** Transform device coordinates into global coordinates
5823 *
Marius Vlada2dace22019-06-12 16:05:44 +03005824 * \param output the weston_output object
5825 * \param[in] device_x X coordinate in device units.
5826 * \param[in] device_y Y coordinate in device units.
5827 * \param[out] x X coordinate in the global space.
5828 * \param[out] y Y coordinate in the global space.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005829 *
Marius Vlada2dace22019-06-12 16:05:44 +03005830 * Transforms coordinates from the device coordinate space (physical pixel
5831 * units) to the global coordinate space (logical pixel units). This takes
5832 * into account output transform and scale.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005833 *
Marius Vlad55d87362019-06-11 01:15:35 +03005834 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005835 * \internal
5836 */
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005837WL_EXPORT void
5838weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005839 double device_x, double device_y,
5840 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005841{
Derek Foreman0f679412014-10-02 13:41:17 -05005842 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005843 device_x,
5844 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05005845 0.0,
5846 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005847
Derek Foreman67a18b92015-03-24 11:36:14 -05005848 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005849
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005850 *x = p.f[0] / p.f[3];
5851 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005852}
5853
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005854/** Removes output from compositor's list of enabled outputs
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005855 *
5856 * \param output The weston_output object that is being removed.
5857 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005858 * The following happens:
5859 *
5860 * - The output assignments of all views in the current scenegraph are
5861 * recomputed.
5862 *
5863 * - Presentation feedback is discarded.
5864 *
5865 * - Compositor is notified that outputs were changed and
5866 * applies the necessary changes to re-layout outputs.
5867 *
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005868 * - The output is put back in the pending outputs list.
5869 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005870 * - Signal is emitted to notify all users of the weston_output
5871 * object that the output is being destroyed.
5872 *
5873 * - wl_output protocol objects referencing this weston_output
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005874 * are made inert, and the wl_output global is removed.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005875 *
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005876 * - The output's internal ID is released.
5877 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005878 * \ingroup compositor
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005879 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005880 */
5881static void
5882weston_compositor_remove_output(struct weston_output *output)
5883{
Pekka Paalanenbccda712017-03-29 16:16:04 +03005884 struct weston_compositor *compositor = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005885 struct weston_view *view;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005886 struct weston_head *head;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005887
5888 assert(output->destroying);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005889 assert(output->enabled);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005890
Pekka Paalanenbccda712017-03-29 16:16:04 +03005891 wl_list_for_each(view, &compositor->view_list, link) {
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005892 if (view->output_mask & (1u << output->id))
5893 weston_view_assign_output(view);
5894 }
5895
5896 weston_presentation_feedback_discard_list(&output->feedback_list);
5897
Pekka Paalanen9711fd92018-06-21 14:26:18 +03005898 weston_compositor_reflow_outputs(compositor, output, -output->width);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005899
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005900 wl_list_remove(&output->link);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005901 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005902 output->enabled = false;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005903
Pekka Paalanenbccda712017-03-29 16:16:04 +03005904 wl_signal_emit(&compositor->output_destroyed_signal, output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005905 wl_signal_emit(&output->destroy_signal, output);
5906
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02005907 wl_list_for_each(head, &output->head_list, output_link)
5908 weston_head_remove_global(head);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005909
5910 compositor->output_id_pool &= ~(1u << output->id);
5911 output->id = 0xffffffff; /* invalid */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005912}
5913
5914/** Sets the output scale for a given output.
5915 *
5916 * \param output The weston_output object that the scale is set for.
5917 * \param scale Scale factor for the given output.
5918 *
5919 * It only supports setting scale for an output that
5920 * is not enabled and it can only be ran once.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005921 *
Marius Vlad55d87362019-06-11 01:15:35 +03005922 * \ingroup ouput
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005923 */
5924WL_EXPORT void
5925weston_output_set_scale(struct weston_output *output,
5926 int32_t scale)
5927{
5928 /* We can only set scale on a disabled output */
5929 assert(!output->enabled);
5930
5931 /* We only want to set scale once */
5932 assert(!output->scale);
5933
5934 output->scale = scale;
5935}
5936
5937/** Sets the output transform for a given output.
5938 *
5939 * \param output The weston_output object that the transform is set for.
5940 * \param transform Transform value for the given output.
5941 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005942 * Refer to wl_output::transform section located at
5943 * https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_output
5944 * for list of values that can be passed to this function.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005945 *
Marius Vlad55d87362019-06-11 01:15:35 +03005946 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005947 */
5948WL_EXPORT void
5949weston_output_set_transform(struct weston_output *output,
5950 uint32_t transform)
5951{
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005952 struct weston_pointer_motion_event ev;
5953 struct wl_resource *resource;
5954 struct weston_seat *seat;
5955 pixman_region32_t old_region;
5956 int mid_x, mid_y;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005957 struct weston_head *head;
5958 int ver;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005959
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005960 if (!output->enabled && output->transform == UINT32_MAX) {
5961 output->transform = transform;
5962 return;
5963 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005964
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005965 weston_output_transform_scale_init(output, transform, output->scale);
5966
5967 pixman_region32_init(&old_region);
5968 pixman_region32_copy(&old_region, &output->region);
5969
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005970 weston_output_init_geometry(output, output->x, output->y);
5971
5972 output->dirty = 1;
5973
5974 /* Notify clients of the change for output transform. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005975 wl_list_for_each(head, &output->head_list, output_link) {
5976 wl_resource_for_each(resource, &head->resource_list) {
5977 wl_output_send_geometry(resource,
5978 output->x,
5979 output->y,
5980 head->mm_width,
5981 head->mm_height,
5982 head->subpixel,
5983 head->make,
5984 head->model,
5985 output->transform);
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005986
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005987 ver = wl_resource_get_version(resource);
5988 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
5989 wl_output_send_done(resource);
5990 }
Roman Gilge97391c2019-03-29 13:01:06 +01005991 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
5992 zxdg_output_v1_send_logical_position(resource,
5993 output->x,
5994 output->y);
5995 zxdg_output_v1_send_logical_size(resource,
5996 output->width,
5997 output->height);
5998 zxdg_output_v1_send_done(resource);
5999 }
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00006000 }
6001
6002 /* we must ensure that pointers are inside output, otherwise they disappear */
6003 mid_x = output->x + output->width / 2;
6004 mid_y = output->y + output->height / 2;
6005
6006 ev.mask = WESTON_POINTER_MOTION_ABS;
6007 ev.x = wl_fixed_to_double(wl_fixed_from_int(mid_x));
6008 ev.y = wl_fixed_to_double(wl_fixed_from_int(mid_y));
6009
6010 wl_list_for_each(seat, &output->compositor->seat_list, link) {
6011 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
6012
6013 if (pointer && pixman_region32_contains_point(&old_region,
6014 wl_fixed_to_int(pointer->x),
6015 wl_fixed_to_int(pointer->y),
6016 NULL))
6017 weston_pointer_move(pointer, &ev);
6018 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006019}
6020
6021/** Initializes a weston_output object with enough data so
6022 ** an output can be configured.
6023 *
6024 * \param output The weston_output object to initialize
6025 * \param compositor The compositor instance.
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006026 * \param name Name for the output (the string is copied).
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006027 *
6028 * Sets initial values for fields that are expected to be
6029 * configured either by compositors or backends.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006030 *
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006031 * The name is used in logs, and can be used by compositors as a configuration
6032 * identifier.
6033 *
Marius Vlad55d87362019-06-11 01:15:35 +03006034 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006035 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006036 */
6037WL_EXPORT void
Armin Krezović40087402016-09-30 14:11:12 +02006038weston_output_init(struct weston_output *output,
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006039 struct weston_compositor *compositor,
6040 const char *name)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006041{
6042 output->compositor = compositor;
6043 output->destroying = 0;
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006044 output->name = strdup(name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006045 wl_list_init(&output->link);
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006046 wl_signal_init(&output->user_destroy_signal);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006047 output->enabled = false;
Ankit Nautiyal2690a772019-03-25 17:57:59 +05306048 output->desired_protection = WESTON_HDCP_DISABLE;
Ankit Nautiyal2844f8e2019-04-03 10:14:59 +05306049 output->allow_protection = true;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006050
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006051 wl_list_init(&output->head_list);
6052
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006053 /* Add some (in)sane defaults which can be used
6054 * for checking if an output was properly configured
6055 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006056 output->scale = 0;
6057 /* Can't use -1 on uint32_t and 0 is valid enum value */
6058 output->transform = UINT32_MAX;
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006059
6060 pixman_region32_init(&output->previous_damage);
6061 pixman_region32_init(&output->region);
Pekka Paalanen42704142017-09-06 16:47:52 +03006062 wl_list_init(&output->mode_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006063}
6064
6065/** Adds weston_output object to pending output list.
6066 *
6067 * \param output The weston_output object to add
6068 * \param compositor The compositor instance.
6069 *
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03006070 * The opposite of this operation is built into weston_output_release().
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006071 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006072 * \ingroup compositor
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006073 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006074 */
6075WL_EXPORT void
6076weston_compositor_add_pending_output(struct weston_output *output,
6077 struct weston_compositor *compositor)
6078{
Pekka Paalanene952a012017-03-29 17:14:00 +03006079 assert(output->disable);
6080 assert(output->enable);
6081
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006082 wl_list_remove(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006083 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006084}
6085
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006086/** Create a string with the attached heads' names.
6087 *
6088 * The string must be free()'d.
Marius Vlad55d87362019-06-11 01:15:35 +03006089 *
6090 * \ingroup output
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006091 */
6092static char *
6093weston_output_create_heads_string(struct weston_output *output)
6094{
6095 FILE *fp;
6096 char *str = NULL;
6097 size_t size = 0;
6098 struct weston_head *head;
6099 const char *sep = "";
6100
6101 fp = open_memstream(&str, &size);
6102 if (!fp)
6103 return NULL;
6104
6105 wl_list_for_each(head, &output->head_list, output_link) {
6106 fprintf(fp, "%s%s", sep, head->name);
6107 sep = ", ";
6108 }
6109 fclose(fp);
6110
6111 return str;
6112}
6113
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006114/** Constructs a weston_output object that can be used by the compositor.
6115 *
Pekka Paalanencc201e42017-03-30 15:11:25 +03006116 * \param output The weston_output object that needs to be enabled. Must not
Pekka Paalanenddce54d2017-08-23 16:00:21 +03006117 * be enabled already. Must have at least one head attached.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006118 *
6119 * Output coordinates are calculated and each new output is by default
6120 * assigned to the right of previous one.
6121 *
6122 * Sets up the transformation, zoom, and geometry of the output using
6123 * the properties that need to be configured by the compositor.
6124 *
6125 * Establishes a repaint timer for the output with the relevant display
6126 * object's event loop. See output_repaint_timer_handler().
6127 *
6128 * The output is assigned an ID. Weston can support up to 32 distinct
6129 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
6130 * is referred to and used to find the first available ID number, and
6131 * then this ID is marked as used in output_id_pool.
6132 *
6133 * The output is also assigned a Wayland global with the wl_output
6134 * external interface.
6135 *
6136 * Backend specific function is called to set up the output output.
6137 *
6138 * Output is added to the compositor's output list
6139 *
6140 * If the backend specific function fails, the weston_output object
6141 * is returned to a state it was before calling this function and
6142 * is added to the compositor's pending_output_list in case it needs
6143 * to be reconfigured or just so it can be destroyed at shutdown.
6144 *
6145 * 0 is returned on success, -1 on failure.
Marius Vlad55d87362019-06-11 01:15:35 +03006146 *
6147 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006148 */
6149WL_EXPORT int
6150weston_output_enable(struct weston_output *output)
6151{
Armin Krezović782f5df2016-09-30 14:11:11 +02006152 struct weston_compositor *c = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006153 struct weston_output *iterator;
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03006154 struct weston_head *head;
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006155 char *head_names;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006156 int x = 0, y = 0;
6157
Pekka Paalanencc201e42017-03-30 15:11:25 +03006158 if (output->enabled) {
6159 weston_log("Error: attempt to enable an enabled output '%s'\n",
6160 output->name);
6161 return -1;
6162 }
6163
Pekka Paalanenddce54d2017-08-23 16:00:21 +03006164 if (wl_list_empty(&output->head_list)) {
6165 weston_log("Error: cannot enable output '%s' without heads.\n",
6166 output->name);
6167 return -1;
6168 }
6169
Pekka Paalanen586e1ac2017-09-14 16:17:59 +03006170 if (wl_list_empty(&output->mode_list) || !output->current_mode) {
6171 weston_log("Error: no video mode for output '%s'.\n",
6172 output->name);
6173 return -1;
6174 }
6175
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03006176 wl_list_for_each(head, &output->head_list, output_link) {
6177 assert(head->make);
6178 assert(head->model);
6179 }
6180
Armin Krezović782f5df2016-09-30 14:11:11 +02006181 iterator = container_of(c->output_list.prev,
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006182 struct weston_output, link);
6183
Armin Krezović782f5df2016-09-30 14:11:11 +02006184 if (!wl_list_empty(&c->output_list))
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006185 x = iterator->x + iterator->width;
6186
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006187 /* Make sure the scale is set up */
6188 assert(output->scale);
6189
6190 /* Make sure we have a transform set */
6191 assert(output->transform != UINT32_MAX);
6192
Armin Krezović782f5df2016-09-30 14:11:11 +02006193 output->x = x;
6194 output->y = y;
6195 output->dirty = 1;
6196 output->original_scale = output->scale;
6197
6198 weston_output_transform_scale_init(output, output->transform, output->scale);
6199 weston_output_init_zoom(output);
6200
6201 weston_output_init_geometry(output, x, y);
6202 weston_output_damage(output);
6203
6204 wl_signal_init(&output->frame_signal);
6205 wl_signal_init(&output->destroy_signal);
6206 wl_list_init(&output->animation_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02006207 wl_list_init(&output->feedback_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02006208
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006209 /* Enable the output (set up the crtc or create a
6210 * window representing the output, set up the
6211 * renderer, etc)
6212 */
6213 if (output->enable(output) < 0) {
6214 weston_log("Enabling output \"%s\" failed.\n", output->name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006215 return -1;
6216 }
6217
6218 weston_compositor_add_output(output->compositor, output);
6219
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006220 head_names = weston_output_create_heads_string(output);
6221 weston_log("Output '%s' enabled with head(s) %s\n",
6222 output->name, head_names);
6223 free(head_names);
6224
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006225 return 0;
6226}
6227
6228/** Converts a weston_output object to a pending output state, so it
6229 ** can be configured again or destroyed.
6230 *
6231 * \param output The weston_output object that needs to be disabled.
6232 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006233 * Calls a backend specific function to disable an output, in case
6234 * such function exists.
6235 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03006236 * The backend specific disable function may choose to postpone the disabling
6237 * by returning a negative value, in which case this function returns early.
6238 * In that case the backend will guarantee the output will be disabled soon
6239 * by the backend calling this function again. One must not attempt to re-enable
6240 * the output until that happens.
6241 *
6242 * Otherwise, if the output is being used by the compositor, it is removed
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006243 * from weston's output_list (see weston_compositor_remove_output())
6244 * and is returned to a state it was before weston_output_enable()
6245 * was ran (see weston_output_enable_undo()).
6246 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03006247 * See weston_output_init() for more information on the
6248 * state output is returned to.
Pekka Paalanencc201e42017-03-30 15:11:25 +03006249 *
6250 * If the output has never been enabled yet, this function can still be
6251 * called to ensure that the output is actually turned off rather than left
6252 * in the state it was discovered in.
Marius Vlad55d87362019-06-11 01:15:35 +03006253 *
6254 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006255 */
6256WL_EXPORT void
6257weston_output_disable(struct weston_output *output)
6258{
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006259 /* Should we rename this? */
6260 output->destroying = 1;
6261
Pekka Paalanenc65df642017-03-29 15:45:46 +03006262 /* Disable is called unconditionally also for not-enabled outputs,
6263 * because at compositor start-up, if there is an output that is
6264 * already on but the compositor wants to turn it off, we have to
6265 * forward the turn-off to the backend so it knows to do it.
6266 * The backend cannot initially turn off everything, because it
6267 * would cause unnecessary mode-sets for all outputs the compositor
6268 * wants to be on.
6269 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006270 if (output->disable(output) < 0)
6271 return;
6272
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006273 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006274 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006275
6276 output->destroying = 0;
6277}
6278
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006279/** Forces a synchronous call to heads_changed hook
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006280 *
6281 * \param compositor The compositor instance
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006282 *
6283 * If there are new or changed heads, calls the heads_changed hook and
6284 * returns after the hook returns.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006285 *
6286 * \ingroup compositor
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006287 */
6288WL_EXPORT void
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006289weston_compositor_flush_heads_changed(struct weston_compositor *compositor)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006290{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03006291 if (compositor->heads_changed_source) {
6292 wl_event_source_remove(compositor->heads_changed_source);
6293 weston_compositor_call_heads_changed(compositor);
6294 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006295}
6296
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006297/** Add destroy callback for an output
6298 *
6299 * \param output The output to watch.
6300 * \param listener The listener to add. The \c notify member must be set.
6301 *
6302 * The listener callback will be called when user destroys an output. This
6303 * may be delayed by a backend in some cases. The main purpose of the
6304 * listener is to allow hooking up custom data to the output. The custom data
6305 * can be fetched via weston_output_get_destroy_listener() followed by
6306 * container_of().
6307 *
6308 * The \c data argument to the notify callback is the weston_output being
6309 * destroyed.
6310 *
6311 * @note This is for the final destruction of an output, not when it gets
6312 * disabled. If you want to keep track of enabled outputs, this is not it.
Marius Vlad55d87362019-06-11 01:15:35 +03006313 *
6314 * \ingroup ouput
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006315 */
6316WL_EXPORT void
6317weston_output_add_destroy_listener(struct weston_output *output,
6318 struct wl_listener *listener)
6319{
6320 wl_signal_add(&output->user_destroy_signal, listener);
6321}
6322
6323/** Look up destroy listener for an output
6324 *
6325 * \param output The output to query.
6326 * \param notify The notify function used used for the added destroy listener.
6327 * \return The listener, or NULL if not found.
6328 *
6329 * This looks up the previously added destroy listener struct based on the
6330 * notify function it has. The listener can be used to access user data
6331 * through \c container_of().
6332 *
6333 * \sa wl_signal_get() weston_output_add_destroy_listener()
Marius Vlad55d87362019-06-11 01:15:35 +03006334 * \ingroup output
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006335 */
6336WL_EXPORT struct wl_listener *
6337weston_output_get_destroy_listener(struct weston_output *output,
6338 wl_notify_func_t notify)
6339{
6340 return wl_signal_get(&output->user_destroy_signal, notify);
6341}
6342
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006343/** Uninitialize an output
6344 *
6345 * Removes the output from the list of enabled outputs if necessary, but
6346 * does not call the backend's output disable function. The output will no
6347 * longer be in the list of pending outputs either.
6348 *
6349 * All fields of weston_output become uninitialized, i.e. should not be used
6350 * anymore. The caller can free the memory after this.
6351 *
Marius Vlad55d87362019-06-11 01:15:35 +03006352 * \ingroup ouput
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006353 * \internal
6354 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006355WL_EXPORT void
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03006356weston_output_release(struct weston_output *output)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006357{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006358 struct weston_head *head, *tmp;
6359
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006360 output->destroying = 1;
6361
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006362 wl_signal_emit(&output->user_destroy_signal, output);
6363
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03006364 if (output->idle_repaint_source)
6365 wl_event_source_remove(output->idle_repaint_source);
6366
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006367 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006368 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006369
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006370 pixman_region32_fini(&output->region);
6371 pixman_region32_fini(&output->previous_damage);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006372 wl_list_remove(&output->link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006373
6374 wl_list_for_each_safe(head, tmp, &output->head_list, output_link)
6375 weston_head_detach(head);
6376
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006377 free(output->name);
6378}
6379
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006380/** Find an output by its given name
6381 *
6382 * \param compositor The compositor to search in.
6383 * \param name The output name to search for.
6384 * \return An existing output with the given name, or NULL if not found.
6385 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006386 * \ingroup compositor
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006387 */
6388WL_EXPORT struct weston_output *
6389weston_compositor_find_output_by_name(struct weston_compositor *compositor,
6390 const char *name)
6391{
6392 struct weston_output *output;
6393
6394 wl_list_for_each(output, &compositor->output_list, link)
6395 if (strcmp(output->name, name) == 0)
6396 return output;
6397
6398 wl_list_for_each(output, &compositor->pending_output_list, link)
6399 if (strcmp(output->name, name) == 0)
6400 return output;
6401
6402 return NULL;
6403}
6404
6405/** Create a named output
6406 *
6407 * \param compositor The compositor.
6408 * \param name The name for the output.
6409 * \return A new \c weston_output, or NULL on failure.
6410 *
6411 * This creates a new weston_output that starts with no heads attached.
6412 *
6413 * An output must be configured and it must have at least one head before
6414 * it can be enabled.
6415 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006416 * \ingroup compositor
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006417 */
6418WL_EXPORT struct weston_output *
6419weston_compositor_create_output(struct weston_compositor *compositor,
6420 const char *name)
6421{
6422 assert(compositor->backend->create_output);
6423
6424 if (weston_compositor_find_output_by_name(compositor, name)) {
6425 weston_log("Warning: attempted to create an output with a "
6426 "duplicate name '%s'.\n", name);
6427 return NULL;
6428 }
6429
6430 return compositor->backend->create_output(compositor, name);
6431}
6432
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006433/** Create an output for an unused head
6434 *
6435 * \param compositor The compositor.
6436 * \param head The head to attach to the output.
6437 * \return A new \c weston_output, or NULL on failure.
6438 *
6439 * This creates a new weston_output that starts with the given head attached.
6440 * The output inherits the name of the head. The head must not be already
6441 * attached to another output.
6442 *
6443 * An output must be configured before it can be enabled.
6444 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006445 * \ingroup compositor
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006446 */
6447WL_EXPORT struct weston_output *
6448weston_compositor_create_output_with_head(struct weston_compositor *compositor,
6449 struct weston_head *head)
6450{
6451 struct weston_output *output;
6452
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006453 output = weston_compositor_create_output(compositor, head->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006454 if (!output)
6455 return NULL;
6456
6457 if (weston_output_attach_head(output, head) < 0) {
Pekka Paalanen42c0e142017-10-27 12:07:49 +03006458 weston_output_destroy(output);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006459 return NULL;
6460 }
6461
6462 return output;
6463}
6464
6465/** Destroy an output
6466 *
6467 * \param output The output to destroy.
6468 *
6469 * The heads attached to the given output are detached and become unused again.
6470 *
6471 * It is not necessary to explicitly destroy all outputs at compositor exit.
6472 * weston_compositor_destroy() will automatically destroy any remaining
6473 * outputs.
6474 *
Marius Vlad55d87362019-06-11 01:15:35 +03006475 * \ingroup ouput
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006476 */
6477WL_EXPORT void
6478weston_output_destroy(struct weston_output *output)
6479{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006480 output->destroy(output);
6481}
6482
Pekka Paalanencf0a4762017-04-04 16:36:07 +03006483/** When you need a head...
6484 *
6485 * This function is a hack, used until all code has been converted to become
6486 * multi-head aware.
6487 *
6488 * \param output The weston_output whose head to get.
6489 * \return The first head in the output's list.
Marius Vlad55d87362019-06-11 01:15:35 +03006490 *
6491 * \ingroup ouput
Pekka Paalanencf0a4762017-04-04 16:36:07 +03006492 */
6493WL_EXPORT struct weston_head *
6494weston_output_get_first_head(struct weston_output *output)
6495{
6496 if (wl_list_empty(&output->head_list))
6497 return NULL;
6498
6499 return container_of(output->head_list.next,
6500 struct weston_head, output_link);
6501}
6502
Ankit Nautiyal2844f8e2019-04-03 10:14:59 +05306503/** Allow/Disallow content-protection support for an output
6504 *
6505 * This function sets the allow_protection member for an output. Setting of
6506 * this field will allow the compositor to attempt content-protection for this
6507 * output, for a backend that supports the content-protection protocol.
6508 *
6509 * \param output The weston_output for whom the content-protection is to be
6510 * allowed.
6511 * \param allow_protection The bool value which is to be set.
6512 */
6513WL_EXPORT void
6514weston_output_allow_protection(struct weston_output *output,
6515 bool allow_protection)
6516{
6517 output->allow_protection = allow_protection;
6518}
6519
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01006520static void
Roman Gilge97391c2019-03-29 13:01:06 +01006521xdg_output_unlist(struct wl_resource *resource)
6522{
6523 wl_list_remove(wl_resource_get_link(resource));
6524}
6525
6526static void
6527xdg_output_destroy(struct wl_client *client, struct wl_resource *resource)
6528{
6529 wl_resource_destroy(resource);
6530}
6531
6532static const struct zxdg_output_v1_interface xdg_output_interface = {
6533 xdg_output_destroy
6534};
6535
6536static void
6537xdg_output_manager_destroy(struct wl_client *client,
6538 struct wl_resource *resource)
6539{
6540 wl_resource_destroy(resource);
6541}
6542
6543static void
6544xdg_output_manager_get_xdg_output(struct wl_client *client,
6545 struct wl_resource *manager,
6546 uint32_t id,
6547 struct wl_resource *output_resource)
6548{
6549 int version = wl_resource_get_version(manager);
6550 struct weston_head *head = wl_resource_get_user_data(output_resource);
6551 struct weston_output *output = head->output;
6552 struct wl_resource *resource;
6553
6554 resource = wl_resource_create(client, &zxdg_output_v1_interface,
6555 version, id);
6556 if (resource == NULL) {
6557 wl_client_post_no_memory(client);
6558 return;
6559 }
6560
6561 wl_list_insert(&head->xdg_output_resource_list,
6562 wl_resource_get_link(resource));
6563
6564 wl_resource_set_implementation(resource, &xdg_output_interface,
6565 NULL, xdg_output_unlist);
6566
6567 zxdg_output_v1_send_logical_position(resource, output->x, output->y);
6568 zxdg_output_v1_send_logical_size(resource,
6569 output->width,
6570 output->height);
6571 if (version >= ZXDG_OUTPUT_V1_NAME_SINCE_VERSION)
6572 zxdg_output_v1_send_name(resource, head->name);
6573
6574 zxdg_output_v1_send_done(resource);
6575}
6576
6577static const struct zxdg_output_manager_v1_interface xdg_output_manager_interface = {
6578 xdg_output_manager_destroy,
6579 xdg_output_manager_get_xdg_output
6580};
6581
6582static void
6583bind_xdg_output_manager(struct wl_client *client,
6584 void *data, uint32_t version, uint32_t id)
6585{
6586 struct wl_resource *resource;
6587
6588 resource = wl_resource_create(client, &zxdg_output_manager_v1_interface,
6589 version, id);
6590 if (resource == NULL) {
6591 wl_client_post_no_memory(client);
6592 return;
6593 }
6594
6595 wl_resource_set_implementation(resource, &xdg_output_manager_interface,
6596 NULL, NULL);
6597}
6598
6599static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006600destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006601{
Jonny Lamb74130762013-11-26 18:19:46 +01006602 struct weston_surface *surface =
6603 wl_resource_get_user_data(resource);
6604
Pekka Paalanen4826f872016-04-22 14:14:38 +03006605 if (!surface)
6606 return;
6607
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006608 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02006609 surface->pending.buffer_viewport.buffer.src_width =
6610 wl_fixed_from_int(-1);
6611 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006612 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006613}
6614
6615static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006616viewport_destroy(struct wl_client *client,
6617 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006618{
6619 wl_resource_destroy(resource);
6620}
6621
6622static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006623viewport_set_source(struct wl_client *client,
6624 struct wl_resource *resource,
6625 wl_fixed_t src_x,
6626 wl_fixed_t src_y,
6627 wl_fixed_t src_width,
6628 wl_fixed_t src_height)
6629{
6630 struct weston_surface *surface =
6631 wl_resource_get_user_data(resource);
6632
Pekka Paalanen4826f872016-04-22 14:14:38 +03006633 if (!surface) {
6634 wl_resource_post_error(resource,
6635 WP_VIEWPORT_ERROR_NO_SURFACE,
6636 "wl_surface for this viewport is no longer exists");
6637 return;
6638 }
6639
6640 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03006641 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006642
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006643 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03006644 src_height == wl_fixed_from_int(-1) &&
6645 src_x == wl_fixed_from_int(-1) &&
6646 src_y == wl_fixed_from_int(-1)) {
6647 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006648 surface->pending.buffer_viewport.buffer.src_width =
6649 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006650 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006651 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006652 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006653
Pekka Paalanen201769a2016-04-26 14:42:11 +03006654 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006655 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006656 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03006657 "wl_surface@%d viewport source "
6658 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
6659 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006660 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03006661 wl_fixed_to_double(src_height),
6662 wl_fixed_to_double(src_x),
6663 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006664 return;
6665 }
6666
6667 surface->pending.buffer_viewport.buffer.src_x = src_x;
6668 surface->pending.buffer_viewport.buffer.src_y = src_y;
6669 surface->pending.buffer_viewport.buffer.src_width = src_width;
6670 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006671 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006672}
6673
6674static void
6675viewport_set_destination(struct wl_client *client,
6676 struct wl_resource *resource,
6677 int32_t dst_width,
6678 int32_t dst_height)
6679{
6680 struct weston_surface *surface =
6681 wl_resource_get_user_data(resource);
6682
Pekka Paalanen4826f872016-04-22 14:14:38 +03006683 if (!surface) {
6684 wl_resource_post_error(resource,
6685 WP_VIEWPORT_ERROR_NO_SURFACE,
6686 "wl_surface for this viewport no longer exists");
6687 return;
6688 }
6689
6690 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006691
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006692 if (dst_width == -1 && dst_height == -1) {
6693 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006694 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006695 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006696 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006697 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006698
6699 if (dst_width <= 0 || dst_height <= 0) {
6700 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006701 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006702 "destination size must be positive (%dx%d)",
6703 dst_width, dst_height);
6704 return;
6705 }
6706
6707 surface->pending.buffer_viewport.surface.width = dst_width;
6708 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006709 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006710}
6711
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006712static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006713 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006714 viewport_set_source,
6715 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01006716};
6717
6718static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006719viewporter_destroy(struct wl_client *client,
6720 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006721{
6722 wl_resource_destroy(resource);
6723}
6724
6725static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006726viewporter_get_viewport(struct wl_client *client,
6727 struct wl_resource *viewporter,
6728 uint32_t id,
6729 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006730{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006731 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006732 struct weston_surface *surface =
6733 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006734 struct wl_resource *resource;
6735
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006736 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006737 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006738 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006739 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01006740 return;
6741 }
6742
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006743 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006744 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006745 if (resource == NULL) {
6746 wl_client_post_no_memory(client);
6747 return;
6748 }
6749
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006750 wl_resource_set_implementation(resource, &viewport_interface,
6751 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01006752
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006753 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006754}
6755
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006756static const struct wp_viewporter_interface viewporter_interface = {
6757 viewporter_destroy,
6758 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01006759};
6760
6761static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006762bind_viewporter(struct wl_client *client,
6763 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006764{
6765 struct wl_resource *resource;
6766
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006767 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006768 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006769 if (resource == NULL) {
6770 wl_client_post_no_memory(client);
6771 return;
6772 }
6773
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006774 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01006775 NULL, NULL);
6776}
6777
6778static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04006779destroy_presentation_feedback(struct wl_resource *feedback_resource)
6780{
6781 struct weston_presentation_feedback *feedback;
6782
6783 feedback = wl_resource_get_user_data(feedback_resource);
6784
6785 wl_list_remove(&feedback->link);
6786 free(feedback);
6787}
6788
6789static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006790presentation_destroy(struct wl_client *client, struct wl_resource *resource)
6791{
6792 wl_resource_destroy(resource);
6793}
6794
6795static void
6796presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04006797 struct wl_resource *presentation_resource,
6798 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006799 uint32_t callback)
6800{
Pekka Paalanen133e4392014-09-23 22:08:46 -04006801 struct weston_surface *surface;
6802 struct weston_presentation_feedback *feedback;
6803
6804 surface = wl_resource_get_user_data(surface_resource);
6805
Bryce Harringtonde16d892014-11-20 22:21:57 -08006806 feedback = zalloc(sizeof *feedback);
6807 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04006808 goto err_calloc;
6809
6810 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006811 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04006812 1, callback);
6813 if (!feedback->resource)
6814 goto err_create;
6815
6816 wl_resource_set_implementation(feedback->resource, NULL, feedback,
6817 destroy_presentation_feedback);
6818
6819 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
6820
6821 return;
6822
6823err_create:
6824 free(feedback);
6825
6826err_calloc:
6827 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006828}
6829
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006830static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006831 presentation_destroy,
6832 presentation_feedback
6833};
6834
6835static void
6836bind_presentation(struct wl_client *client,
6837 void *data, uint32_t version, uint32_t id)
6838{
6839 struct weston_compositor *compositor = data;
6840 struct wl_resource *resource;
6841
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006842 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006843 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006844 if (resource == NULL) {
6845 wl_client_post_no_memory(client);
6846 return;
6847 }
6848
6849 wl_resource_set_implementation(resource, &presentation_implementation,
6850 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006851 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006852}
6853
6854static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05006855compositor_bind(struct wl_client *client,
6856 void *data, uint32_t version, uint32_t id)
6857{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006858 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05006859 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05006860
Jason Ekstranda85118c2013-06-27 20:17:02 -05006861 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006862 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07006863 if (resource == NULL) {
6864 wl_client_post_no_memory(client);
6865 return;
6866 }
6867
6868 wl_resource_set_implementation(resource, &compositor_interface,
6869 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05006870}
6871
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006872WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006873weston_environment_get_fd(const char *env)
6874{
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07006875 char *e;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006876 int fd, flags;
6877
6878 e = getenv(env);
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07006879 if (!e || !safe_strtoint(e, &fd))
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006880 return -1;
6881
6882 flags = fcntl(fd, F_GETFD);
6883 if (flags == -1)
6884 return -1;
6885
6886 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
6887 unsetenv(env);
6888
6889 return fd;
6890}
6891
Pekka Paalanenb5026542014-11-12 15:09:24 +02006892static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02006893timeline_key_binding_handler(struct weston_keyboard *keyboard,
6894 const struct timespec *time, uint32_t key,
6895 void *data)
Pekka Paalanenb5026542014-11-12 15:09:24 +02006896{
6897 struct weston_compositor *compositor = data;
6898
6899 if (weston_timeline_enabled_)
6900 weston_timeline_close();
6901 else
6902 weston_timeline_open(compositor);
6903}
6904
Daniel Stonece62cb32018-07-20 09:46:24 +01006905static const char *
6906output_repaint_status_text(struct weston_output *output)
6907{
6908 switch (output->repaint_status) {
6909 case REPAINT_NOT_SCHEDULED:
6910 return "no repaint";
6911 case REPAINT_BEGIN_FROM_IDLE:
6912 return "start_repaint_loop scheduled";
6913 case REPAINT_SCHEDULED:
6914 return "repaint scheduled";
6915 case REPAINT_AWAITING_COMPLETION:
6916 return "awaiting completion";
6917 }
6918
6919 assert(!"output_repaint_status_text missing enum");
6920 return NULL;
6921}
6922
6923static void
6924debug_scene_view_print_buffer(FILE *fp, struct weston_view *view)
6925{
6926 struct weston_buffer *buffer = view->surface->buffer_ref.buffer;
6927 struct wl_shm_buffer *shm;
6928 struct linux_dmabuf_buffer *dmabuf;
Marius Vlad00a6e012018-11-20 17:52:31 +02006929 const struct pixel_format_info *pixel_info = NULL;
Daniel Stonece62cb32018-07-20 09:46:24 +01006930
6931 if (!buffer) {
6932 fprintf(fp, "\t\t[buffer not available]\n");
6933 return;
6934 }
6935
6936 shm = wl_shm_buffer_get(buffer->resource);
6937 if (shm) {
Marius Vlad00a6e012018-11-20 17:52:31 +02006938 uint32_t _format = wl_shm_buffer_get_format(shm);
6939 pixel_info = pixel_format_get_info_shm(_format);
Daniel Stonece62cb32018-07-20 09:46:24 +01006940 fprintf(fp, "\t\tSHM buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02006941 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
6942 (unsigned long) _format,
6943 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Daniel Stonece62cb32018-07-20 09:46:24 +01006944 return;
6945 }
6946
6947 dmabuf = linux_dmabuf_buffer_get(buffer->resource);
6948 if (dmabuf) {
Marius Vlad00a6e012018-11-20 17:52:31 +02006949 pixel_info = pixel_format_get_info(dmabuf->attributes.format);
Daniel Stonece62cb32018-07-20 09:46:24 +01006950 fprintf(fp, "\t\tdmabuf buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02006951 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
6952 (unsigned long) dmabuf->attributes.format,
6953 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Daniel Stonece62cb32018-07-20 09:46:24 +01006954 fprintf(fp, "\t\t\tmodifier: 0x%llx\n",
6955 (unsigned long long) dmabuf->attributes.modifier[0]);
6956 return;
6957 }
6958
Marius Vlad253ba9a2019-03-17 18:22:21 +02006959 fprintf(fp, "\t\tEGL buffer\n");
Daniel Stonece62cb32018-07-20 09:46:24 +01006960}
6961
6962static void
6963debug_scene_view_print(FILE *fp, struct weston_view *view, int view_idx)
6964{
6965 struct weston_compositor *ec = view->surface->compositor;
6966 struct weston_output *output;
6967 char desc[512];
6968 pixman_box32_t *box;
6969 uint32_t surface_id = 0;
6970 pid_t pid = 0;
6971
6972 if (view->surface->resource) {
6973 struct wl_resource *resource = view->surface->resource;
6974 wl_client_get_credentials(wl_resource_get_client(resource),
6975 &pid, NULL, NULL);
6976 surface_id = wl_resource_get_id(view->surface->resource);
6977 }
6978
6979 if (!view->surface->get_label ||
6980 view->surface->get_label(view->surface, desc, sizeof(desc)) < 0) {
6981 strcpy(desc, "[no description available]");
6982 }
6983 fprintf(fp, "\tView %d (role %s, PID %d, surface ID %u, %s, %p):\n",
6984 view_idx, view->surface->role_name, pid, surface_id,
6985 desc, view);
6986
6987 box = pixman_region32_extents(&view->transform.boundingbox);
6988 fprintf(fp, "\t\tposition: (%d, %d) -> (%d, %d)\n",
6989 box->x1, box->y1, box->x2, box->y2);
6990 box = pixman_region32_extents(&view->transform.opaque);
6991
6992 if (pixman_region32_equal(&view->transform.opaque,
6993 &view->transform.boundingbox)) {
6994 fprintf(fp, "\t\t[fully opaque]\n");
6995 } else if (!pixman_region32_not_empty(&view->transform.opaque)) {
6996 fprintf(fp, "\t\t[not opaque]\n");
6997 } else {
6998 fprintf(fp, "\t\t[opaque: (%d, %d) -> (%d, %d)]\n",
6999 box->x1, box->y1, box->x2, box->y2);
7000 }
7001
7002 if (view->alpha < 1.0)
7003 fprintf(fp, "\t\talpha: %f\n", view->alpha);
7004
7005 if (view->output_mask != 0) {
7006 bool first_output = true;
7007 fprintf(fp, "\t\toutputs: ");
7008 wl_list_for_each(output, &ec->output_list, link) {
7009 if (!(view->output_mask & (1 << output->id)))
7010 continue;
7011 fprintf(fp, "%s%d (%s)%s",
7012 (first_output) ? "" : ", ",
7013 output->id, output->name,
7014 (view->output == output) ? " (primary)" : "");
7015 first_output = false;
7016 }
7017 } else {
7018 fprintf(fp, "\t\t[no outputs]");
7019 }
7020
7021 fprintf(fp, "\n");
7022
7023 debug_scene_view_print_buffer(fp, view);
7024}
7025
Marius Vlad433f4e72019-02-17 22:14:23 +02007026static void
7027debug_scene_view_print_tree(struct weston_view *view,
Marius Vlada6acfa82019-03-17 18:10:09 +02007028 FILE *fp, int *view_idx)
Marius Vlad433f4e72019-02-17 22:14:23 +02007029{
7030 struct weston_subsurface *sub;
7031 struct weston_view *ev;
7032
7033 /*
7034 * print the view first, then we recursively go on printing
7035 * sub-surfaces. We bail out once no more sub-surfaces are available.
7036 */
Marius Vlada6acfa82019-03-17 18:10:09 +02007037 debug_scene_view_print(fp, view, *view_idx);
Marius Vlad433f4e72019-02-17 22:14:23 +02007038
7039 /* no more sub-surfaces */
7040 if (wl_list_empty(&view->surface->subsurface_list))
7041 return;
7042
7043 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
7044 wl_list_for_each(ev, &sub->surface->views, surface_link) {
7045 /* do not print again the parent view */
7046 if (view == ev)
7047 continue;
Marius Vlada6acfa82019-03-17 18:10:09 +02007048
7049 (*view_idx)++;
Marius Vlad433f4e72019-02-17 22:14:23 +02007050 debug_scene_view_print_tree(ev, fp, view_idx);
7051 }
7052 }
7053}
7054
Daniel Stonece62cb32018-07-20 09:46:24 +01007055/**
7056 * Output information on how libweston is currently composing the scene
7057 * graph.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007058 *
7059 * \ingroup compositor
Daniel Stonece62cb32018-07-20 09:46:24 +01007060 */
7061WL_EXPORT char *
7062weston_compositor_print_scene_graph(struct weston_compositor *ec)
7063{
7064 struct weston_output *output;
7065 struct weston_layer *layer;
7066 struct timespec now;
7067 int layer_idx = 0;
7068 FILE *fp;
7069 char *ret;
7070 size_t len;
7071 int err;
7072
7073 fp = open_memstream(&ret, &len);
7074 assert(fp);
7075
7076 weston_compositor_read_presentation_clock(ec, &now);
7077 fprintf(fp, "Weston scene graph at %ld.%09ld:\n\n",
7078 now.tv_sec, now.tv_nsec);
7079
7080 wl_list_for_each(output, &ec->output_list, link) {
7081 struct weston_head *head;
7082 int head_idx = 0;
7083
7084 fprintf(fp, "Output %d (%s):\n", output->id, output->name);
7085 assert(output->enabled);
7086
7087 fprintf(fp, "\tposition: (%d, %d) -> (%d, %d)\n",
7088 output->x, output->y,
7089 output->x + output->width,
7090 output->y + output->height);
7091 fprintf(fp, "\tmode: %dx%d@%.3fHz\n",
7092 output->current_mode->width,
7093 output->current_mode->height,
7094 output->current_mode->refresh / 1000.0);
7095 fprintf(fp, "\tscale: %d\n", output->scale);
7096
7097 fprintf(fp, "\trepaint status: %s\n",
7098 output_repaint_status_text(output));
7099 if (output->repaint_status == REPAINT_SCHEDULED)
7100 fprintf(fp, "\tnext repaint: %ld.%09ld\n",
7101 output->next_repaint.tv_sec,
7102 output->next_repaint.tv_nsec);
7103
7104 wl_list_for_each(head, &output->head_list, output_link) {
7105 fprintf(fp, "\tHead %d (%s): %sconnected\n",
7106 head_idx++, head->name,
7107 (head->connected) ? "" : "not ");
7108 }
7109 }
7110
7111 fprintf(fp, "\n");
7112
7113 wl_list_for_each(layer, &ec->layer_list, link) {
7114 struct weston_view *view;
7115 int view_idx = 0;
7116
7117 fprintf(fp, "Layer %d (pos 0x%lx):\n", layer_idx++,
7118 (unsigned long) layer->position);
7119
7120 if (!weston_layer_mask_is_infinite(layer)) {
7121 fprintf(fp, "\t[mask: (%d, %d) -> (%d,%d)]\n\n",
7122 layer->mask.x1, layer->mask.y1,
7123 layer->mask.x2, layer->mask.y2);
7124 }
7125
Marius Vlada6acfa82019-03-17 18:10:09 +02007126 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
7127 debug_scene_view_print_tree(view, fp, &view_idx);
7128 view_idx++;
7129 }
Daniel Stonece62cb32018-07-20 09:46:24 +01007130
7131 if (wl_list_empty(&layer->view_list.link))
7132 fprintf(fp, "\t[no views]\n");
7133
7134 fprintf(fp, "\n");
7135 }
7136
7137 err = fclose(fp);
7138 assert(err == 0);
7139
7140 return ret;
7141}
7142
7143/**
7144 * Called when the 'scene-graph' debug scope is bound by a client. This
7145 * one-shot weston-debug scope prints the current scene graph when bound,
7146 * and then terminates the stream.
7147 */
7148static void
Marius Vladdad882a2019-07-17 15:43:53 +03007149debug_scene_graph_cb(struct weston_log_subscription *sub, void *data)
Daniel Stonece62cb32018-07-20 09:46:24 +01007150{
7151 struct weston_compositor *ec = data;
7152 char *str = weston_compositor_print_scene_graph(ec);
7153
Marius Vladdad882a2019-07-17 15:43:53 +03007154 weston_log_subscription_printf(sub, "%s", str);
Daniel Stonece62cb32018-07-20 09:46:24 +01007155 free(str);
Marius Vladdad882a2019-07-17 15:43:53 +03007156 weston_log_subscription_complete(sub);
Daniel Stonece62cb32018-07-20 09:46:24 +01007157}
7158
Giulio Camuffo459137b2014-10-11 23:56:24 +03007159/** Create the compositor.
7160 *
7161 * This functions creates and initializes a compositor instance.
7162 *
7163 * \param display The Wayland display to be used.
7164 * \param user_data A pointer to an object that can later be retrieved
Marius Vlada2dace22019-06-12 16:05:44 +03007165 * \param log_ctx A pointer to weston_debug_compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007166 * using the \ref weston_compositor_get_user_data function.
7167 * \return The compositor instance on success or NULL on failure.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007168 *
7169 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007170 */
7171WL_EXPORT struct weston_compositor *
Marius Vlad880b4852019-04-07 17:07:58 +03007172weston_compositor_create(struct wl_display *display,
Marius Vlad3d7d9782019-04-17 12:35:38 +03007173 struct weston_log_context *log_ctx,
Marius Vlad880b4852019-04-07 17:07:58 +03007174 void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04007175{
Giulio Camuffo459137b2014-10-11 23:56:24 +03007176 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05007177 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07007178
Giulio Camuffo459137b2014-10-11 23:56:24 +03007179 ec = zalloc(sizeof *ec);
7180 if (!ec)
7181 return NULL;
7182
7183 ec->wl_display = display;
7184 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04007185 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07007186 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04007187 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03007188 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03007189 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02007190 wl_signal_init(&ec->idle_signal);
7191 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02007192 wl_signal_init(&ec->show_input_panel_signal);
7193 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02007194 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01007195 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01007196 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02007197 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02007198 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02007199 wl_signal_init(&ec->output_resized_signal);
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03007200 wl_signal_init(&ec->heads_changed_signal);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02007201 wl_signal_init(&ec->output_heads_changed_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07007202 wl_signal_init(&ec->session_signal);
Robert Beckettc569bdc2019-07-10 16:40:12 +01007203 ec->session_active = true;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04007204
Casey Dahlin58ba1372012-04-19 22:50:08 -04007205 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03007206 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04007207
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02007208 ec->activate_serial = 1;
7209
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05007210 ec->touch_mode = WESTON_TOUCH_MODE_NORMAL;
7211
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05307212 ec->content_protection = NULL;
7213
Derek Foreman152254b2015-11-26 14:17:48 -06007214 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04007215 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007216 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05007217
Giulio Camuffo954f1832014-10-11 18:27:30 +03007218 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04007219 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007220 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03007221
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007222 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007223 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007224 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01007225
Roman Gilge97391c2019-03-29 13:01:06 +01007226 if (!wl_global_create(ec->wl_display, &zxdg_output_manager_v1_interface, 2,
7227 ec, bind_xdg_output_manager))
7228 goto fail;
7229
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007230 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007231 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007232 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007233
Marius Vlad3d7d9782019-04-17 12:35:38 +03007234 if (weston_log_ctx_compositor_setup(ec, log_ctx) < 0)
Pekka Paalanena5630ea2017-10-12 13:13:42 +02007235 goto fail;
7236
Jonas Ådahl30d61d82014-10-22 21:21:17 +02007237 if (weston_input_init(ec) != 0)
7238 goto fail;
7239
Jason Ekstranda7af7042013-10-12 22:38:11 -05007240 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02007241 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007242 wl_list_init(&ec->layer_list);
7243 wl_list_init(&ec->seat_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007244 wl_list_init(&ec->pending_output_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007245 wl_list_init(&ec->output_list);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03007246 wl_list_init(&ec->head_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007247 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01007248 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007249 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01007250 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007251 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02007252 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007253
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007254 wl_list_init(&ec->plugin_api_list);
7255
Xiong Zhang97116532013-10-23 13:58:31 +08007256 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02007257 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04007258
Giulio Camuffo459137b2014-10-11 23:56:24 +03007259 wl_data_device_manager_init(ec->wl_display);
7260
7261 wl_display_init_shm(ec->wl_display);
7262
7263 loop = wl_display_get_event_loop(ec->wl_display);
7264 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Daniel Stone6847b852017-03-01 11:34:08 +00007265 ec->repaint_timer =
7266 wl_event_loop_add_timer(loop, output_repaint_timer_handler,
7267 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007268
Quentin Glidic82681572016-12-17 13:40:51 +01007269 weston_layer_init(&ec->fade_layer, ec);
7270 weston_layer_init(&ec->cursor_layer, ec);
7271
7272 weston_layer_set_position(&ec->fade_layer, WESTON_LAYER_POSITION_FADE);
7273 weston_layer_set_position(&ec->cursor_layer,
7274 WESTON_LAYER_POSITION_CURSOR);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007275
7276 weston_compositor_add_debug_binding(ec, KEY_T,
7277 timeline_key_binding_handler, ec);
7278
Daniel Stonece62cb32018-07-20 09:46:24 +01007279 ec->debug_scene =
Marius Vlad7e4db952019-04-17 13:47:06 +03007280 weston_compositor_add_log_scope(ec->weston_log_ctx, "scene-graph",
7281 "Scene graph details\n",
7282 debug_scene_graph_cb,
7283 ec);
Daniel Stonece62cb32018-07-20 09:46:24 +01007284
Giulio Camuffo459137b2014-10-11 23:56:24 +03007285 return ec;
7286
7287fail:
7288 free(ec);
7289 return NULL;
7290}
7291
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007292/** weston_compositor_shutdown
7293 * \ingroup compositor
7294 */
Benjamin Franzkeb8263022011-08-30 11:32:47 +02007295WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007296weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07007297{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007298 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07007299
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007300 wl_event_source_remove(ec->idle_source);
7301
Matt Roper361d2ad2011-08-29 13:52:23 -07007302 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02007303 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07007304 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02007305
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007306 /* Destroy all pending outputs associated with this compositor */
7307 wl_list_for_each_safe(output, next, &ec->pending_output_list, link)
7308 output->destroy(output);
7309
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02007310 if (ec->renderer)
7311 ec->renderer->destroy(ec);
7312
Daniel Stone325fc2d2012-05-30 16:31:58 +01007313 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09007314 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01007315 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01007316 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01007317 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02007318 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007319
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04007320 weston_plane_release(&ec->primary_plane);
Matt Roper361d2ad2011-08-29 13:52:23 -07007321}
7322
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007323/** weston_compositor_exit_with_code
7324 * \ingroup compositor
7325 */
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05007326WL_EXPORT void
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007327weston_compositor_exit_with_code(struct weston_compositor *compositor,
Frederic Plourdec336f062014-10-29 14:44:33 -04007328 int exit_code)
7329{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02007330 if (compositor->exit_code == EXIT_SUCCESS)
7331 compositor->exit_code = exit_code;
7332
Giulio Camuffo459137b2014-10-11 23:56:24 +03007333 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04007334}
7335
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007336/** weston_compositor_set_default_pointer_grab
7337 * \ingroup compositor
7338 */
Frederic Plourdec336f062014-10-29 14:44:33 -04007339WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007340weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
7341 const struct weston_pointer_grab_interface *interface)
7342{
7343 struct weston_seat *seat;
7344
7345 ec->default_pointer_grab = interface;
7346 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05007347 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
7348
7349 if (pointer)
7350 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007351 }
7352}
7353
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007354/** weston_compositor_set_presentation_clock
7355 * \ingroup compositor
7356 */
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007357WL_EXPORT int
7358weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
7359 clockid_t clk_id)
7360{
7361 struct timespec ts;
7362
7363 if (clock_gettime(clk_id, &ts) < 0)
7364 return -1;
7365
7366 compositor->presentation_clock = clk_id;
7367
7368 return 0;
7369}
7370
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007371/** For choosing the software clock, when the display hardware or API
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007372 * does not expose a compatible presentation timestamp.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007373 *
7374 * \ingroup compositor
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007375 */
7376WL_EXPORT int
7377weston_compositor_set_presentation_clock_software(
7378 struct weston_compositor *compositor)
7379{
7380 /* In order of preference */
7381 static const clockid_t clocks[] = {
7382 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
7383 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
7384 CLOCK_MONOTONIC, /* no jumps, may crawl */
7385 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
7386 CLOCK_REALTIME /* may jump and crawl */
7387 };
7388 unsigned i;
7389
7390 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
7391 if (weston_compositor_set_presentation_clock(compositor,
7392 clocks[i]) == 0)
7393 return 0;
7394
7395 weston_log("Error: no suitable presentation clock available.\n");
7396
7397 return -1;
7398}
7399
Pekka Paalanen662f3842015-03-18 12:17:26 +02007400/** Read the current time from the Presentation clock
7401 *
7402 * \param compositor
Marius Vlada2dace22019-06-12 16:05:44 +03007403 * \param[out] ts The current time.
Pekka Paalanen662f3842015-03-18 12:17:26 +02007404 *
7405 * \note Reading the current time in user space is always imprecise to some
7406 * degree.
7407 *
7408 * This function is never meant to fail. If reading the clock does fail,
7409 * an error message is logged and a zero time is returned. Callers are not
7410 * supposed to detect or react to failures.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007411 *
7412 * \ingroup compositor
Pekka Paalanen662f3842015-03-18 12:17:26 +02007413 */
7414WL_EXPORT void
7415weston_compositor_read_presentation_clock(
7416 const struct weston_compositor *compositor,
7417 struct timespec *ts)
7418{
7419 static bool warned;
7420 int ret;
7421
7422 ret = clock_gettime(compositor->presentation_clock, ts);
7423 if (ret < 0) {
7424 ts->tv_sec = 0;
7425 ts->tv_nsec = 0;
7426
7427 if (!warned)
7428 weston_log("Error: failure to read "
Antonio Borneo39578632019-04-26 23:57:31 +02007429 "the presentation clock %#x: '%s' (%d)\n",
7430 compositor->presentation_clock,
7431 strerror(errno), errno);
Pekka Paalanen662f3842015-03-18 12:17:26 +02007432 warned = true;
7433 }
7434}
7435
Pekka Paalanen230f3b12014-09-29 14:18:40 -04007436/** Import dmabuf buffer into current renderer
7437 *
7438 * \param compositor
7439 * \param buffer the dmabuf buffer to import
7440 * \return true on usable buffers, false otherwise
7441 *
7442 * This function tests that the linux_dmabuf_buffer is usable
7443 * for the current renderer. Returns false on unusable buffers. Usually
7444 * usability is tested by importing the dmabufs for composition.
7445 *
7446 * This hook is also used for detecting if the renderer supports
7447 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
7448 * supported.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007449 *
7450 * \ingroup compositor
7451 */
Pekka Paalanen230f3b12014-09-29 14:18:40 -04007452WL_EXPORT bool
7453weston_compositor_import_dmabuf(struct weston_compositor *compositor,
7454 struct linux_dmabuf_buffer *buffer)
7455{
7456 struct weston_renderer *renderer;
7457
7458 renderer = compositor->renderer;
7459
7460 if (renderer->import_dmabuf == NULL)
7461 return false;
7462
7463 return renderer->import_dmabuf(compositor, buffer);
7464}
7465
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007466WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05007467weston_version(int *major, int *minor, int *micro)
7468{
7469 *major = WESTON_VERSION_MAJOR;
7470 *minor = WESTON_VERSION_MINOR;
7471 *micro = WESTON_VERSION_MICRO;
7472}
7473
Daniel Stonee03c1112016-11-24 20:45:45 +00007474/**
7475 * Attempts to find a module path from the module map specified in the
7476 * environment. If found, writes the full path into the path variable.
7477 *
7478 * The module map is a string in environment variable WESTON_MODULE_MAP, where
7479 * each entry is of the form "name=path" and entries are separated by
7480 * semicolons. Whitespace is significant.
7481 *
7482 * \param name The name to search for.
7483 * \param path Where the path is written to if found.
7484 * \param path_len Allocated bytes at \c path .
7485 * \returns The length of the string written to path on success, or 0 if the
7486 * module was not specified in the environment map or path_len was too small.
7487 */
7488WL_EXPORT size_t
7489weston_module_path_from_env(const char *name, char *path, size_t path_len)
7490{
7491 const char *mapping = getenv("WESTON_MODULE_MAP");
7492 const char *end;
7493 const int name_len = strlen(name);
7494
7495 if (!mapping)
7496 return 0;
7497
7498 end = mapping + strlen(mapping);
7499 while (mapping < end && *mapping) {
7500 const char *filename, *next;
7501
7502 /* early out: impossibly short string */
7503 if (end - mapping < name_len + 1)
7504 return 0;
7505
7506 filename = &mapping[name_len + 1];
7507 next = strchrnul(mapping, ';');
7508
7509 if (strncmp(mapping, name, name_len) == 0 &&
7510 mapping[name_len] == '=') {
7511 size_t file_len = next - filename; /* no trailing NUL */
7512 if (file_len >= path_len)
7513 return 0;
7514 strncpy(path, filename, file_len);
7515 path[file_len] = '\0';
7516 return file_len;
7517 }
7518
7519 mapping = next + 1;
7520 }
7521
7522 return 0;
7523}
7524
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03007525WL_EXPORT void *
7526weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007527{
7528 char path[PATH_MAX];
7529 void *module, *init;
Daniel Stonebeb97e52016-11-28 12:13:54 +00007530 size_t len;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007531
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08007532 if (name == NULL)
7533 return NULL;
7534
Derek Foreman3f86e502015-06-08 11:46:54 -05007535 if (name[0] != '/') {
Daniel Stonee03c1112016-11-24 20:45:45 +00007536 len = weston_module_path_from_env(name, path, sizeof path);
7537 if (len == 0)
Daniel Stonebeb97e52016-11-28 12:13:54 +00007538 len = snprintf(path, sizeof path, "%s/%s",
7539 LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05007540 } else {
Daniel Stonebeb97e52016-11-28 12:13:54 +00007541 len = snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05007542 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007543
Daniel Stonebeb97e52016-11-28 12:13:54 +00007544 /* snprintf returns the length of the string it would've written,
7545 * _excluding_ the NUL byte. So even being equal to the size of
7546 * our buffer is an error here. */
7547 if (len >= sizeof path)
7548 return NULL;
7549
Kristian Høgsberga6813d22012-09-12 12:21:01 -04007550 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
7551 if (module) {
7552 weston_log("Module '%s' already loaded\n", path);
7553 dlclose(module);
7554 return NULL;
7555 }
7556
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03007557 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04007558 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007559 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03007560 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007561 return NULL;
7562 }
7563
7564 init = dlsym(module, entrypoint);
7565 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03007566 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00007567 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007568 return NULL;
7569 }
7570
7571 return init;
7572}
7573
Giulio Camuffo459137b2014-10-11 23:56:24 +03007574
Marius Vlad284d5342019-06-24 12:00:47 +03007575/** Tear down the compositor.
Giulio Camuffo459137b2014-10-11 23:56:24 +03007576 *
Marius Vlad284d5342019-06-24 12:00:47 +03007577 * This function cleans up the compositor state. While the compositor state has
7578 * been cleaned do note that **only** weston_compositor_destroy() can be called
7579 * afterwards, in order to destroy the compositor instance.
Giulio Camuffo459137b2014-10-11 23:56:24 +03007580 *
Marius Vlad284d5342019-06-24 12:00:47 +03007581 * @param compositor The compositor to be tear-down/cleaned.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007582 *
Marius Vlad284d5342019-06-24 12:00:47 +03007583 * @ingroup compositor
7584 * @sa weston_compositor_destroy
Giulio Camuffo459137b2014-10-11 23:56:24 +03007585 */
7586WL_EXPORT void
Marius Vlad284d5342019-06-24 12:00:47 +03007587weston_compositor_tear_down(struct weston_compositor *compositor)
Giulio Camuffo459137b2014-10-11 23:56:24 +03007588{
7589 /* prevent further rendering while shutting down */
7590 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
7591
7592 wl_signal_emit(&compositor->destroy_signal, compositor);
7593
7594 weston_compositor_xkb_destroy(compositor);
7595
Giulio Camuffo2d24e642015-10-03 16:25:15 +03007596 if (compositor->backend)
7597 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007598
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03007599 /* The backend is responsible for destroying the heads. */
7600 assert(wl_list_empty(&compositor->head_list));
7601
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007602 weston_plugin_api_destroy_list(compositor);
7603
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03007604 if (compositor->heads_changed_source)
7605 wl_event_source_remove(compositor->heads_changed_source);
7606
Marius Vlad7e4db952019-04-17 13:47:06 +03007607 weston_compositor_log_scope_destroy(compositor->debug_scene);
Daniel Stonece62cb32018-07-20 09:46:24 +01007608 compositor->debug_scene = NULL;
Marius Vlad284d5342019-06-24 12:00:47 +03007609}
Pekka Paalanena5630ea2017-10-12 13:13:42 +02007610
Marius Vlad284d5342019-06-24 12:00:47 +03007611/** Destroys the compositor.
7612 *
7613 * This function destroys the compositor. **Do not** call this before
7614 * calling weston_compositor_tear_down()
7615 *
7616 * @param compositor The compositor to be destroyed.
7617 * @ingroup compositor
7618 * @sa weston_compositor_tear_down()
7619 */
7620WL_EXPORT void
7621weston_compositor_destroy(struct weston_compositor *compositor)
7622{
Giulio Camuffo459137b2014-10-11 23:56:24 +03007623 free(compositor);
7624}
7625
7626/** Instruct the compositor to exit.
7627 *
7628 * This functions does not directly destroy the compositor object, it merely
7629 * command it to start the tear down process. It is not guaranteed that the
7630 * tear down will happen immediately.
7631 *
7632 * \param compositor The compositor to tear down.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007633 *
7634 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007635 */
7636WL_EXPORT void
7637weston_compositor_exit(struct weston_compositor *compositor)
7638{
7639 compositor->exit(compositor);
7640}
7641
7642/** Return the user data stored in the compositor.
7643 *
7644 * This function returns the user data pointer set with user_data parameter
7645 * to the \ref weston_compositor_create function.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007646 *
7647 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007648 */
7649WL_EXPORT void *
7650weston_compositor_get_user_data(struct weston_compositor *compositor)
7651{
7652 return compositor->user_data;
7653}
Pekka Paalanendd186732016-06-03 14:49:54 +03007654
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007655static const char * const backend_map[] = {
7656 [WESTON_BACKEND_DRM] = "drm-backend.so",
7657 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
7658 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
7659 [WESTON_BACKEND_RDP] = "rdp-backend.so",
7660 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
7661 [WESTON_BACKEND_X11] = "x11-backend.so",
7662};
7663
Pekka Paalanendd186732016-06-03 14:49:54 +03007664/** Load a backend into a weston_compositor
7665 *
7666 * A backend must be loaded to make a weston_compositor work. A backend
7667 * provides input and output capabilities, and determines the renderer to use.
7668 *
7669 * \param compositor A compositor that has not had a backend loaded yet.
7670 * \param backend Name of the backend file.
7671 * \param config_base A pointer to a backend-specific configuration
7672 * structure's 'base' member.
7673 *
7674 * \return 0 on success, or -1 on error.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007675 *
7676 * \ingroup compositor
Pekka Paalanendd186732016-06-03 14:49:54 +03007677 */
7678WL_EXPORT int
7679weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007680 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03007681 struct weston_backend_config *config_base)
7682{
7683 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03007684 struct weston_backend_config *config_base);
7685
Pekka Paalanend7e35112017-08-29 17:04:12 +03007686 if (compositor->backend) {
7687 weston_log("Error: attempt to load a backend when one is already loaded\n");
7688 return -1;
7689 }
7690
Quentin Glidic887c0182016-07-10 11:00:53 +02007691 if (backend >= ARRAY_LENGTH(backend_map))
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007692 return -1;
7693
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01007694 backend_init = weston_load_module(backend_map[backend], "weston_backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03007695 if (!backend_init)
7696 return -1;
7697
Pekka Paalanend7e35112017-08-29 17:04:12 +03007698 if (backend_init(compositor, config_base) < 0) {
7699 compositor->backend = NULL;
7700 return -1;
7701 }
7702
7703 return 0;
Pekka Paalanendd186732016-06-03 14:49:54 +03007704}
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007705
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007706/** weston_compositor_load_xwayland
7707 * \ingroup compositor
7708 */
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007709WL_EXPORT int
7710weston_compositor_load_xwayland(struct weston_compositor *compositor)
7711{
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007712 int (*module_init)(struct weston_compositor *ec);
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007713
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007714 module_init = weston_load_module("xwayland.so", "weston_module_init");
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007715 if (!module_init)
7716 return -1;
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007717 if (module_init(compositor) < 0)
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007718 return -1;
7719 return 0;
7720}
Vasilis Liaskovitis486b4632018-10-10 16:14:55 +02007721
7722/** Resolve an internal compositor error by disconnecting the client.
7723 *
7724 * This function is used in cases when the wl_buffer turns out
7725 * unusable and there is no fallback path.
7726 *
7727 * It is possible the fault is caused by a compositor bug, the underlying
7728 * graphics stack bug or normal behaviour, or perhaps a client mistake.
7729 * In any case, the options are to either composite garbage or nothing,
7730 * or disconnect the client. This is a helper function for the latter.
7731 *
7732 * The error is sent as an INVALID_OBJECT error on the client's wl_display.
7733 *
7734 * \param buffer The weston buffer that is unusable.
7735 * \param msg A custom error message attached to the protocol error.
7736 */
7737WL_EXPORT void
7738weston_buffer_send_server_error(struct weston_buffer *buffer,
7739 const char *msg)
7740{
7741 struct wl_client *client;
7742 struct wl_resource *display_resource;
7743 uint32_t id;
7744
7745 assert(buffer->resource);
7746 id = wl_resource_get_id(buffer->resource);
7747 client = wl_resource_get_client(buffer->resource);
7748 display_resource = wl_client_get_object(client, 1);
7749
7750 assert(display_resource);
7751 wl_resource_post_error(display_resource,
7752 WL_DISPLAY_ERROR_INVALID_OBJECT,
7753 "server error with "
7754 "wl_buffer@%u: %s", id, msg);
7755}
Ankit Nautiyal93dde242019-07-08 11:46:42 +05307756
7757WL_EXPORT void
7758weston_output_disable_planes_incr(struct weston_output *output)
7759{
7760 output->disable_planes++;
7761}
7762
7763WL_EXPORT void
7764weston_output_disable_planes_decr(struct weston_output *output)
7765{
7766 output->disable_planes--;
7767}