blob: 444b4064090dce94ca9401a207785637c14cb5fa [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 Vladf4f4c2b2019-04-29 13:27:47 +030059#include <libweston/weston-debug.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 Vlada72e3712019-07-10 13:46:39 +030075#include "libweston-internal.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050076
Marius Vlad78984ee2019-06-11 00:05:08 +030077/**
78 * \defgroup head Head
79 * \defgroup output Output
80 * \defgroup compositor Compositor
81 */
82
Pekka Paalanen0513a952014-05-21 16:17:27 +030083#define DEFAULT_REPAINT_WINDOW 7 /* milliseconds */
84
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020085static void
Pekka Paalanend72bad22017-03-29 17:01:41 +030086weston_output_update_matrix(struct weston_output *output);
87
88static void
Alexander Larsson0b135062013-05-28 16:23:36 +020089weston_output_transform_scale_init(struct weston_output *output,
90 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020091
Rob Bradford27b17932013-06-26 18:08:46 +010092static void
Jason Ekstranda7af7042013-10-12 22:38:11 -050093weston_compositor_build_view_list(struct weston_compositor *compositor);
Rob Bradford27b17932013-06-26 18:08:46 +010094
Pekka Paalanendcac3512017-12-08 14:13:34 +020095static char *
96weston_output_create_heads_string(struct weston_output *output);
97
Pekka Paalanen6528c032017-03-28 15:27:10 +030098/** Send wl_output events for mode and scale changes
99 *
100 * \param head Send on all resources bound to this head.
101 * \param mode_changed If true, send the current mode.
102 * \param scale_changed If true, send the current scale.
103 */
104static void
105weston_mode_switch_send_events(struct weston_head *head,
106 bool mode_changed, bool scale_changed)
107{
108 struct weston_output *output = head->output;
109 struct wl_resource *resource;
110 int version;
111
112 wl_resource_for_each(resource, &head->resource_list) {
113 if (mode_changed) {
114 wl_output_send_mode(resource,
115 output->current_mode->flags,
116 output->current_mode->width,
117 output->current_mode->height,
118 output->current_mode->refresh);
119 }
120
121 version = wl_resource_get_version(resource);
122 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION && scale_changed)
123 wl_output_send_scale(resource, output->current_scale);
124
125 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
126 wl_output_send_done(resource);
127 }
Roman Gilge97391c2019-03-29 13:01:06 +0100128 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
129 zxdg_output_v1_send_logical_position(resource,
130 output->x,
131 output->y);
132 zxdg_output_v1_send_logical_size(resource,
133 output->width,
134 output->height);
135 zxdg_output_v1_send_done(resource);
136 }
Pekka Paalanen6528c032017-03-28 15:27:10 +0300137}
138
139static void
140weston_mode_switch_finish(struct weston_output *output,
141 int mode_changed, int scale_changed)
Alex Wu2dda6042012-04-17 17:20:47 +0800142{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200143 struct weston_seat *seat;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300144 struct weston_head *head;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200145 pixman_region32_t old_output_region;
Alexander Larsson355748e2013-05-28 16:23:38 +0200146
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200147 pixman_region32_init(&old_output_region);
148 pixman_region32_copy(&old_output_region, &output->region);
149
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200150 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +0200151 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200152
153 pixman_region32_init(&output->previous_damage);
154 pixman_region32_init_rect(&output->region, output->x, output->y,
155 output->width, output->height);
156
157 weston_output_update_matrix(output);
158
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200159 /* If a pointer falls outside the outputs new geometry, move it to its
160 * lower-right corner */
161 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500162 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200163 int32_t x, y;
164
165 if (!pointer)
166 continue;
167
168 x = wl_fixed_to_int(pointer->x);
169 y = wl_fixed_to_int(pointer->y);
170
171 if (!pixman_region32_contains_point(&old_output_region,
172 x, y, NULL) ||
173 pixman_region32_contains_point(&output->region,
174 x, y, NULL))
175 continue;
176
177 if (x >= output->x + output->width)
178 x = output->x + output->width - 1;
179 if (y >= output->y + output->height)
180 y = output->y + output->height - 1;
181
182 pointer->x = wl_fixed_from_int(x);
183 pointer->y = wl_fixed_from_int(y);
184 }
185
186 pixman_region32_fini(&old_output_region);
187
Derek Foremandd4cd332014-11-10 10:29:59 -0600188 if (!mode_changed && !scale_changed)
189 return;
190
Hardening57388e42013-09-18 23:56:36 +0200191 /* notify clients of the changes */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300192 wl_list_for_each(head, &output->head_list, output_link)
193 weston_mode_switch_send_events(head,
194 mode_changed, scale_changed);
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600195}
196
David Fort0de859e2016-05-27 23:22:57 +0200197static void
198weston_compositor_reflow_outputs(struct weston_compositor *compositor,
199 struct weston_output *resized_output, int delta_width);
200
Marius Vlad55d87362019-06-11 01:15:35 +0300201/**
202 * \ingroup output
203 */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600204WL_EXPORT int
205weston_output_mode_set_native(struct weston_output *output,
206 struct weston_mode *mode,
207 int32_t scale)
208{
209 int ret;
210 int mode_changed = 0, scale_changed = 0;
David Fort0de859e2016-05-27 23:22:57 +0200211 int32_t old_width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600212
213 if (!output->switch_mode)
214 return -1;
215
216 if (!output->original_mode) {
217 mode_changed = 1;
218 ret = output->switch_mode(output, mode);
219 if (ret < 0)
220 return ret;
221 if (output->current_scale != scale) {
222 scale_changed = 1;
223 output->current_scale = scale;
Hardening57388e42013-09-18 23:56:36 +0200224 }
225 }
226
David Fort0de859e2016-05-27 23:22:57 +0200227 old_width = output->width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600228 output->native_mode = mode;
229 output->native_scale = scale;
230
231 weston_mode_switch_finish(output, mode_changed, scale_changed);
232
David Fort0de859e2016-05-27 23:22:57 +0200233 if (mode_changed || scale_changed) {
234 weston_compositor_reflow_outputs(output->compositor, output, output->width - old_width);
235
236 wl_signal_emit(&output->compositor->output_resized_signal, output);
237 }
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600238 return 0;
239}
240
Marius Vlad55d87362019-06-11 01:15:35 +0300241/**
242 * \ingroup output
243 */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600244WL_EXPORT int
245weston_output_mode_switch_to_native(struct weston_output *output)
246{
247 int ret;
248 int mode_changed = 0, scale_changed = 0;
249
250 if (!output->switch_mode)
251 return -1;
252
253 if (!output->original_mode) {
254 weston_log("already in the native mode\n");
255 return -1;
256 }
257 /* the non fullscreen clients haven't seen a mode set since we
258 * switched into a temporary, so we need to notify them if the
259 * mode at that time is different from the native mode now.
260 */
261 mode_changed = (output->original_mode != output->native_mode);
262 scale_changed = (output->original_scale != output->native_scale);
263
264 ret = output->switch_mode(output, output->native_mode);
265 if (ret < 0)
266 return ret;
267
268 output->current_scale = output->native_scale;
269
270 output->original_mode = NULL;
271 output->original_scale = 0;
272
273 weston_mode_switch_finish(output, mode_changed, scale_changed);
274
275 return 0;
276}
277
Marius Vlad55d87362019-06-11 01:15:35 +0300278/**
279 * \ingroup output
280 */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600281WL_EXPORT int
282weston_output_mode_switch_to_temporary(struct weston_output *output,
283 struct weston_mode *mode,
284 int32_t scale)
285{
286 int ret;
287
288 if (!output->switch_mode)
289 return -1;
290
291 /* original_mode is the last mode non full screen clients have seen,
292 * so we shouldn't change it if we already have one set.
293 */
294 if (!output->original_mode) {
295 output->original_mode = output->native_mode;
296 output->original_scale = output->native_scale;
297 }
298 ret = output->switch_mode(output, mode);
299 if (ret < 0)
300 return ret;
301
302 output->current_scale = scale;
303
304 weston_mode_switch_finish(output, 0, 0);
305
306 return 0;
Alex Wu2dda6042012-04-17 17:20:47 +0800307}
308
Benjamin Franzke06286262011-05-06 19:12:33 +0200309static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300310region_init_infinite(pixman_region32_t *region)
311{
312 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
313 UINT32_MAX, UINT32_MAX);
314}
315
Pekka Paalanene67858b2013-04-25 13:57:42 +0300316static struct weston_subsurface *
317weston_surface_to_subsurface(struct weston_surface *surface);
318
Jason Ekstranda7af7042013-10-12 22:38:11 -0500319WL_EXPORT struct weston_view *
320weston_view_create(struct weston_surface *surface)
321{
322 struct weston_view *view;
323
Bryce Harringtonde16d892014-11-20 22:21:57 -0800324 view = zalloc(sizeof *view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500325 if (view == NULL)
326 return NULL;
327
328 view->surface = surface;
Daniel Stonefb4869d2016-12-09 16:27:54 +0000329 view->plane = &surface->compositor->primary_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500330
Jason Ekstranda7af7042013-10-12 22:38:11 -0500331 /* Assign to surface */
332 wl_list_insert(&surface->views, &view->surface_link);
333
334 wl_signal_init(&view->destroy_signal);
335 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300336 wl_list_init(&view->layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500337
Jason Ekstranda7af7042013-10-12 22:38:11 -0500338 pixman_region32_init(&view->clip);
339
340 view->alpha = 1.0;
341 pixman_region32_init(&view->transform.opaque);
342
343 wl_list_init(&view->geometry.transformation_list);
344 wl_list_insert(&view->geometry.transformation_list,
345 &view->transform.position.link);
346 weston_matrix_init(&view->transform.position.matrix);
347 wl_list_init(&view->geometry.child_list);
Pekka Paalanen380adf52015-02-16 14:39:11 +0200348 pixman_region32_init(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500349 pixman_region32_init(&view->transform.boundingbox);
350 view->transform.dirty = 1;
351
Jason Ekstranda7af7042013-10-12 22:38:11 -0500352 return view;
353}
354
Jason Ekstrand108865d2014-06-26 10:04:49 -0700355struct weston_frame_callback {
356 struct wl_resource *resource;
357 struct wl_list link;
358};
359
Pekka Paalanen133e4392014-09-23 22:08:46 -0400360struct weston_presentation_feedback {
361 struct wl_resource *resource;
362
363 /* XXX: could use just wl_resource_get_link() instead */
364 struct wl_list link;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200365
366 /* The per-surface feedback flags */
367 uint32_t psf_flags;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400368};
369
370static void
371weston_presentation_feedback_discard(
372 struct weston_presentation_feedback *feedback)
373{
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200374 wp_presentation_feedback_send_discarded(feedback->resource);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400375 wl_resource_destroy(feedback->resource);
376}
377
378static void
379weston_presentation_feedback_discard_list(struct wl_list *list)
380{
381 struct weston_presentation_feedback *feedback, *tmp;
382
383 wl_list_for_each_safe(feedback, tmp, list, link)
384 weston_presentation_feedback_discard(feedback);
385}
386
387static void
388weston_presentation_feedback_present(
389 struct weston_presentation_feedback *feedback,
390 struct weston_output *output,
391 uint32_t refresh_nsec,
392 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200393 uint64_t seq,
394 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400395{
396 struct wl_client *client = wl_resource_get_client(feedback->resource);
Pekka Paalanen1b9bf592017-03-27 12:15:38 +0300397 struct weston_head *head;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400398 struct wl_resource *o;
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200399 uint32_t tv_sec_hi;
400 uint32_t tv_sec_lo;
401 uint32_t tv_nsec;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300402 bool done = false;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400403
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300404 wl_list_for_each(head, &output->head_list, output_link) {
405 wl_resource_for_each(o, &head->resource_list) {
406 if (wl_resource_get_client(o) != client)
407 continue;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400408
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +0300409 wp_presentation_feedback_send_sync_output(feedback->resource, o);
410 done = true;
411 }
412
413 /* For clone mode, send it for just one wl_output global,
414 * they are all equivalent anyway.
415 */
416 if (done)
417 break;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400418 }
419
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200420 timespec_to_proto(ts, &tv_sec_hi, &tv_sec_lo, &tv_nsec);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200421 wp_presentation_feedback_send_presented(feedback->resource,
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200422 tv_sec_hi, tv_sec_lo, tv_nsec,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200423 refresh_nsec,
424 seq >> 32, seq & 0xffffffff,
425 flags | feedback->psf_flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400426 wl_resource_destroy(feedback->resource);
427}
428
429static void
430weston_presentation_feedback_present_list(struct wl_list *list,
431 struct weston_output *output,
432 uint32_t refresh_nsec,
433 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200434 uint64_t seq,
435 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400436{
437 struct weston_presentation_feedback *feedback, *tmp;
438
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200439 assert(!(flags & WP_PRESENTATION_FEEDBACK_INVALID) ||
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200440 wl_list_empty(list));
441
Pekka Paalanen133e4392014-09-23 22:08:46 -0400442 wl_list_for_each_safe(feedback, tmp, list, link)
443 weston_presentation_feedback_present(feedback, output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200444 refresh_nsec, ts, seq,
445 flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400446}
447
Jason Ekstrand7b982072014-05-20 14:33:03 -0500448static void
449surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
450{
451 struct weston_surface_state *state =
452 container_of(listener, struct weston_surface_state,
453 buffer_destroy_listener);
454
455 state->buffer = NULL;
456}
457
458static void
459weston_surface_state_init(struct weston_surface_state *state)
460{
461 state->newly_attached = 0;
462 state->buffer = NULL;
463 state->buffer_destroy_listener.notify =
464 surface_state_handle_buffer_destroy;
465 state->sx = 0;
466 state->sy = 0;
467
Derek Foreman152254b2015-11-26 14:17:48 -0600468 pixman_region32_init(&state->damage_surface);
469 pixman_region32_init(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500470 pixman_region32_init(&state->opaque);
471 region_init_infinite(&state->input);
472
473 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400474 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500475
476 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
477 state->buffer_viewport.buffer.scale = 1;
478 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
479 state->buffer_viewport.surface.width = -1;
480 state->buffer_viewport.changed = 0;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300481
482 state->acquire_fence_fd = -1;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +0530483
484 state->desired_protection = WESTON_HDCP_DISABLE;
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +0530485 state->protection_mode = WESTON_SURFACE_PROTECTION_MODE_RELAXED;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500486}
487
488static void
489weston_surface_state_fini(struct weston_surface_state *state)
490{
491 struct weston_frame_callback *cb, *next;
492
493 wl_list_for_each_safe(cb, next,
494 &state->frame_callback_list, link)
495 wl_resource_destroy(cb->resource);
496
Pekka Paalanen133e4392014-09-23 22:08:46 -0400497 weston_presentation_feedback_discard_list(&state->feedback_list);
498
Jason Ekstrand7b982072014-05-20 14:33:03 -0500499 pixman_region32_fini(&state->input);
500 pixman_region32_fini(&state->opaque);
Derek Foreman152254b2015-11-26 14:17:48 -0600501 pixman_region32_fini(&state->damage_surface);
502 pixman_region32_fini(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500503
504 if (state->buffer)
505 wl_list_remove(&state->buffer_destroy_listener.link);
506 state->buffer = NULL;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300507
508 fd_clear(&state->acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +0300509 weston_buffer_release_reference(&state->buffer_release_ref, NULL);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500510}
511
512static void
513weston_surface_state_set_buffer(struct weston_surface_state *state,
514 struct weston_buffer *buffer)
515{
516 if (state->buffer == buffer)
517 return;
518
519 if (state->buffer)
520 wl_list_remove(&state->buffer_destroy_listener.link);
521 state->buffer = buffer;
522 if (state->buffer)
523 wl_signal_add(&state->buffer->destroy_signal,
524 &state->buffer_destroy_listener);
525}
526
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500527WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500528weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500529{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500530 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400531
Bryce Harringtonde16d892014-11-20 22:21:57 -0800532 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400533 if (surface == NULL)
534 return NULL;
535
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500536 wl_signal_init(&surface->destroy_signal);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +0800537 wl_signal_init(&surface->commit_signal);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500538
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500539 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200540 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400541
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200542 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
543 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200544 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
545 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500546
547 weston_surface_state_init(&surface->pending);
548
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400549 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500550 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300551 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400552
Jason Ekstranda7af7042013-10-12 22:38:11 -0500553 wl_list_init(&surface->views);
554
555 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400556 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500557
Pekka Paalanene67858b2013-04-25 13:57:42 +0300558 wl_list_init(&surface->subsurface_list);
559 wl_list_init(&surface->subsurface_list_pending);
560
Jason Ekstrand1e059042014-10-16 10:55:19 -0500561 weston_matrix_init(&surface->buffer_to_surface_matrix);
562 weston_matrix_init(&surface->surface_to_buffer_matrix);
563
Jonas Ådahld3414f22016-07-22 17:56:31 +0800564 wl_list_init(&surface->pointer_constraints);
565
Alexandros Frantzisacff29b2018-10-19 12:14:11 +0300566 surface->acquire_fence_fd = -1;
567
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +0530568 surface->desired_protection = WESTON_HDCP_DISABLE;
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +0530569 surface->current_protection = WESTON_HDCP_DISABLE;
570 surface->protection_mode = WESTON_SURFACE_PROTECTION_MODE_RELAXED;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +0530571
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400572 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500573}
574
Alex Wu8811bf92012-02-28 18:07:54 +0800575WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500576weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200577 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500578{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100579 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Philipp Zabel195dade2018-09-03 19:44:59 +0200580 surface->is_opaque = !(alpha < 1.0);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500581}
582
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400583WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500584weston_view_to_global_float(struct weston_view *view,
585 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200586{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500587 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200588 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
589
Jason Ekstranda7af7042013-10-12 22:38:11 -0500590 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200591
592 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200593 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700594 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200595 v.f[3]);
596 *x = 0;
597 *y = 0;
598 return;
599 }
600
601 *x = v.f[0] / v.f[3];
602 *y = v.f[1] / v.f[3];
603 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500604 *x = sx + view->geometry.x;
605 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200606 }
607}
608
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500609WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200610weston_transformed_coord(int width, int height,
611 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200612 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200613 float sx, float sy, float *bx, float *by)
614{
615 switch (transform) {
616 case WL_OUTPUT_TRANSFORM_NORMAL:
617 default:
618 *bx = sx;
619 *by = sy;
620 break;
621 case WL_OUTPUT_TRANSFORM_FLIPPED:
622 *bx = width - sx;
623 *by = sy;
624 break;
625 case WL_OUTPUT_TRANSFORM_90:
626 *bx = height - sy;
627 *by = sx;
628 break;
629 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
630 *bx = height - sy;
631 *by = width - sx;
632 break;
633 case WL_OUTPUT_TRANSFORM_180:
634 *bx = width - sx;
635 *by = height - sy;
636 break;
637 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
638 *bx = sx;
639 *by = height - sy;
640 break;
641 case WL_OUTPUT_TRANSFORM_270:
642 *bx = sy;
643 *by = width - sx;
644 break;
645 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
646 *bx = sy;
647 *by = sx;
648 break;
649 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200650
651 *bx *= scale;
652 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200653}
654
655WL_EXPORT pixman_box32_t
656weston_transformed_rect(int width, int height,
657 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200658 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200659 pixman_box32_t rect)
660{
661 float x1, x2, y1, y2;
662
663 pixman_box32_t ret;
664
Alexander Larsson4ea95522013-05-22 14:41:37 +0200665 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200666 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200667 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200668 rect.x2, rect.y2, &x2, &y2);
669
670 if (x1 <= x2) {
671 ret.x1 = x1;
672 ret.x2 = x2;
673 } else {
674 ret.x1 = x2;
675 ret.x2 = x1;
676 }
677
678 if (y1 <= y2) {
679 ret.y1 = y1;
680 ret.y2 = y2;
681 } else {
682 ret.y1 = y2;
683 ret.y2 = y1;
684 }
685
686 return ret;
687}
688
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600689/** Transform a region by a matrix, restricted to axis-aligned transformations
690 *
691 * Warning: This function does not work for projective, affine, or matrices
692 * that encode arbitrary rotations. Only 90-degree step rotations are
693 * supported.
694 */
695WL_EXPORT void
696weston_matrix_transform_region(pixman_region32_t *dest,
697 struct weston_matrix *matrix,
698 pixman_region32_t *src)
699{
700 pixman_box32_t *src_rects, *dest_rects;
701 int nrects, i;
702
703 src_rects = pixman_region32_rectangles(src, &nrects);
704 dest_rects = malloc(nrects * sizeof(*dest_rects));
705 if (!dest_rects)
706 return;
707
708 for (i = 0; i < nrects; i++) {
709 struct weston_vector vec1 = {{
710 src_rects[i].x1, src_rects[i].y1, 0, 1
711 }};
712 weston_matrix_transform(matrix, &vec1);
713 vec1.f[0] /= vec1.f[3];
714 vec1.f[1] /= vec1.f[3];
715
716 struct weston_vector vec2 = {{
717 src_rects[i].x2, src_rects[i].y2, 0, 1
718 }};
719 weston_matrix_transform(matrix, &vec2);
720 vec2.f[0] /= vec2.f[3];
721 vec2.f[1] /= vec2.f[3];
722
723 if (vec1.f[0] < vec2.f[0]) {
724 dest_rects[i].x1 = floor(vec1.f[0]);
725 dest_rects[i].x2 = ceil(vec2.f[0]);
726 } else {
727 dest_rects[i].x1 = floor(vec2.f[0]);
728 dest_rects[i].x2 = ceil(vec1.f[0]);
729 }
730
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600731 if (vec1.f[1] < vec2.f[1]) {
732 dest_rects[i].y1 = floor(vec1.f[1]);
733 dest_rects[i].y2 = ceil(vec2.f[1]);
734 } else {
735 dest_rects[i].y1 = floor(vec2.f[1]);
736 dest_rects[i].y2 = ceil(vec1.f[1]);
737 }
738 }
739
740 pixman_region32_clear(dest);
741 pixman_region32_init_rects(dest, dest_rects, nrects);
742 free(dest_rects);
743}
744
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200745WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500746weston_transformed_region(int width, int height,
747 enum wl_output_transform transform,
748 int32_t scale,
749 pixman_region32_t *src, pixman_region32_t *dest)
750{
751 pixman_box32_t *src_rects, *dest_rects;
752 int nrects, i;
753
754 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
755 if (src != dest)
756 pixman_region32_copy(dest, src);
757 return;
758 }
759
760 src_rects = pixman_region32_rectangles(src, &nrects);
761 dest_rects = malloc(nrects * sizeof(*dest_rects));
762 if (!dest_rects)
763 return;
764
765 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
766 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
767 } else {
768 for (i = 0; i < nrects; i++) {
769 switch (transform) {
770 default:
771 case WL_OUTPUT_TRANSFORM_NORMAL:
772 dest_rects[i].x1 = src_rects[i].x1;
773 dest_rects[i].y1 = src_rects[i].y1;
774 dest_rects[i].x2 = src_rects[i].x2;
775 dest_rects[i].y2 = src_rects[i].y2;
776 break;
777 case WL_OUTPUT_TRANSFORM_90:
778 dest_rects[i].x1 = height - src_rects[i].y2;
779 dest_rects[i].y1 = src_rects[i].x1;
780 dest_rects[i].x2 = height - src_rects[i].y1;
781 dest_rects[i].y2 = src_rects[i].x2;
782 break;
783 case WL_OUTPUT_TRANSFORM_180:
784 dest_rects[i].x1 = width - src_rects[i].x2;
785 dest_rects[i].y1 = height - src_rects[i].y2;
786 dest_rects[i].x2 = width - src_rects[i].x1;
787 dest_rects[i].y2 = height - src_rects[i].y1;
788 break;
789 case WL_OUTPUT_TRANSFORM_270:
790 dest_rects[i].x1 = src_rects[i].y1;
791 dest_rects[i].y1 = width - src_rects[i].x2;
792 dest_rects[i].x2 = src_rects[i].y2;
793 dest_rects[i].y2 = width - src_rects[i].x1;
794 break;
795 case WL_OUTPUT_TRANSFORM_FLIPPED:
796 dest_rects[i].x1 = width - src_rects[i].x2;
797 dest_rects[i].y1 = src_rects[i].y1;
798 dest_rects[i].x2 = width - src_rects[i].x1;
799 dest_rects[i].y2 = src_rects[i].y2;
800 break;
801 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
802 dest_rects[i].x1 = height - src_rects[i].y2;
803 dest_rects[i].y1 = width - src_rects[i].x2;
804 dest_rects[i].x2 = height - src_rects[i].y1;
805 dest_rects[i].y2 = width - src_rects[i].x1;
806 break;
807 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
808 dest_rects[i].x1 = src_rects[i].x1;
809 dest_rects[i].y1 = height - src_rects[i].y2;
810 dest_rects[i].x2 = src_rects[i].x2;
811 dest_rects[i].y2 = height - src_rects[i].y1;
812 break;
813 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
814 dest_rects[i].x1 = src_rects[i].y1;
815 dest_rects[i].y1 = src_rects[i].x1;
816 dest_rects[i].x2 = src_rects[i].y2;
817 dest_rects[i].y2 = src_rects[i].x2;
818 break;
819 }
820 }
821 }
822
823 if (scale != 1) {
824 for (i = 0; i < nrects; i++) {
825 dest_rects[i].x1 *= scale;
826 dest_rects[i].x2 *= scale;
827 dest_rects[i].y1 *= scale;
828 dest_rects[i].y2 *= scale;
829 }
830 }
831
832 pixman_region32_clear(dest);
833 pixman_region32_init_rects(dest, dest_rects, nrects);
834 free(dest_rects);
835}
836
Jonny Lamb74130762013-11-26 18:19:46 +0100837static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300838viewport_surface_to_buffer(struct weston_surface *surface,
839 float sx, float sy, float *bx, float *by)
Jonny Lamb74130762013-11-26 18:19:46 +0100840{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200841 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200842 double src_width, src_height;
843 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200844
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200845 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
846 if (vp->surface.width == -1) {
847 *bx = sx;
848 *by = sy;
849 return;
850 }
Jonny Lamb74130762013-11-26 18:19:46 +0100851
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200852 src_x = 0.0;
853 src_y = 0.0;
854 src_width = surface->width_from_buffer;
855 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100856 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200857 src_x = wl_fixed_to_double(vp->buffer.src_x);
858 src_y = wl_fixed_to_double(vp->buffer.src_y);
859 src_width = wl_fixed_to_double(vp->buffer.src_width);
860 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100861 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200862
863 *bx = sx * src_width / surface->width + src_x;
864 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100865}
866
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500867WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200868weston_surface_to_buffer_float(struct weston_surface *surface,
869 float sx, float sy, float *bx, float *by)
870{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200871 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
872
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300873 /* first transform coordinates if the viewport is set */
874 viewport_surface_to_buffer(surface, sx, sy, bx, by);
Jonny Lamb74130762013-11-26 18:19:46 +0100875
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500876 weston_transformed_coord(surface->width_from_buffer,
877 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200878 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100879 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200880}
881
Derek Foreman99739672015-12-03 16:38:11 -0600882/** Transform a rectangle from surface coordinates to buffer coordinates
883 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300884 * \param surface The surface to fetch wp_viewport and buffer transformation
Derek Foreman99739672015-12-03 16:38:11 -0600885 * from.
886 * \param rect The rectangle to transform.
887 * \return The transformed rectangle.
888 *
889 * Viewport and buffer transformations can only do translation, scaling,
890 * and rotations in 90-degree steps. Therefore the only loss in the
891 * conversion is coordinate rounding.
892 *
893 * However, some coordinate rounding takes place as an intermediate
894 * step before the buffer scale factor is applied, so the rectangle
895 * boundary may not be exactly as expected.
896 *
897 * This is OK for damage tracking since a little extra coverage is
898 * not a problem.
899 */
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200900WL_EXPORT pixman_box32_t
901weston_surface_to_buffer_rect(struct weston_surface *surface,
902 pixman_box32_t rect)
903{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200904 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100905 float xf, yf;
906
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300907 /* first transform box coordinates if the viewport is set */
908 viewport_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100909 rect.x1 = floorf(xf);
910 rect.y1 = floorf(yf);
911
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300912 viewport_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
Derek Foremane2e15ac2015-12-01 13:00:43 -0600913 rect.x2 = ceilf(xf);
914 rect.y2 = ceilf(yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100915
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500916 return weston_transformed_rect(surface->width_from_buffer,
917 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200918 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200919 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200920}
921
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200922/** Transform a region from surface coordinates to buffer coordinates
923 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300924 * \param surface The surface to fetch wp_viewport and buffer transformation
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200925 * from.
Marius Vlada2dace22019-06-12 16:05:44 +0300926 * \param[in] surface_region The region in surface coordinates.
927 * \param[out] buffer_region The region converted to buffer coordinates.
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200928 *
929 * Buffer_region must be init'd, but will be completely overwritten.
930 *
931 * Viewport and buffer transformations can only do translation, scaling,
932 * and rotations in 90-degree steps. Therefore the only loss in the
Derek Foreman99739672015-12-03 16:38:11 -0600933 * conversion is from the coordinate rounding that takes place in
934 * \ref weston_surface_to_buffer_rect.
Marius Vlada2dace22019-06-12 16:05:44 +0300935 *
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200936 */
937WL_EXPORT void
938weston_surface_to_buffer_region(struct weston_surface *surface,
939 pixman_region32_t *surface_region,
940 pixman_region32_t *buffer_region)
941{
942 pixman_box32_t *src_rects, *dest_rects;
943 int nrects, i;
944
945 src_rects = pixman_region32_rectangles(surface_region, &nrects);
946 dest_rects = malloc(nrects * sizeof(*dest_rects));
947 if (!dest_rects)
948 return;
949
950 for (i = 0; i < nrects; i++) {
951 dest_rects[i] = weston_surface_to_buffer_rect(surface,
952 src_rects[i]);
953 }
954
955 pixman_region32_fini(buffer_region);
956 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
957 free(dest_rects);
958}
959
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200960WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500961weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400962 struct weston_plane *plane)
963{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500964 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400965 return;
966
Jason Ekstranda7af7042013-10-12 22:38:11 -0500967 weston_view_damage_below(view);
968 view->plane = plane;
969 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400970}
971
Pekka Paalanen51723d52015-02-17 13:10:01 +0200972/** Inflict damage on the plane where the view is visible.
973 *
974 * \param view The view that causes the damage.
975 *
976 * If the view is currently on a plane (including the primary plane),
977 * take the view's boundingbox, subtract all the opaque views that cover it,
978 * and add the remaining region as damage to the plane. This corresponds
979 * to the damage inflicted to the plane if this view disappeared.
980 *
981 * A repaint is scheduled for this view.
982 *
983 * The region of all opaque views covering this view is stored in
984 * weston_view::clip and updated by view_accumulate_damage() during
985 * weston_output_repaint(). Specifically, that region matches the
986 * scenegraph as it was last painted.
987 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400988WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500989weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200990{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500991 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200992
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500993 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +0200994 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500995 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800996 if (view->plane)
997 pixman_region32_union(&view->plane->damage,
998 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500999 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -08001000 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001001}
1002
Pekka Paalanen01e00682017-03-24 16:21:06 +02001003/** Send wl_surface.enter/leave events
1004 *
1005 * \param surface The surface.
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03001006 * \param head A head of the entered/left output.
Pekka Paalanen01e00682017-03-24 16:21:06 +02001007 * \param enter True if entered.
Marius Vlada2dace22019-06-12 16:05:44 +03001008 * \param leave True if left.
Pekka Paalanen01e00682017-03-24 16:21:06 +02001009 *
1010 * Send the enter/leave events for all protocol objects bound to the given
1011 * output by the client owning the surface.
1012 */
1013static void
1014weston_surface_send_enter_leave(struct weston_surface *surface,
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03001015 struct weston_head *head,
Pekka Paalanen01e00682017-03-24 16:21:06 +02001016 bool enter,
1017 bool leave)
1018{
1019 struct wl_resource *wloutput;
1020 struct wl_client *client;
1021
1022 assert(enter != leave);
1023
1024 client = wl_resource_get_client(surface->resource);
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03001025 wl_resource_for_each(wloutput, &head->resource_list) {
Pekka Paalanen01e00682017-03-24 16:21:06 +02001026 if (wl_resource_get_client(wloutput) != client)
1027 continue;
1028
1029 if (enter)
1030 wl_surface_send_enter(surface->resource, wloutput);
1031 if (leave)
1032 wl_surface_send_leave(surface->resource, wloutput);
1033 }
1034}
1035
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301036static void
1037weston_surface_compute_protection(struct protected_surface *psurface)
1038{
1039 enum weston_hdcp_protection min_protection;
1040 bool min_protection_valid = false;
1041 struct weston_surface *surface = psurface->surface;
1042 struct weston_output *output;
1043
1044 wl_list_for_each(output, &surface->compositor->output_list, link)
1045 if (surface->output_mask & (1u << output->id)) {
1046 if (!min_protection_valid) {
1047 min_protection = output->current_protection;
1048 min_protection_valid = true;
1049 }
1050 if (output->current_protection < min_protection)
1051 min_protection = output->current_protection;
1052 }
1053 if (!min_protection_valid)
1054 min_protection = WESTON_HDCP_DISABLE;
1055
1056 surface->current_protection = min_protection;
1057 weston_protected_surface_send_event(psurface,
1058 surface->current_protection);
1059}
1060
1061static void
1062notify_surface_protection_change(void *data)
1063{
1064 struct weston_compositor *compositor = data;
1065 struct content_protection *cp;
1066 struct protected_surface *psurface;
1067
1068 cp = compositor->content_protection;
1069 cp->surface_protection_update = NULL;
1070
1071 /* Notify the clients, whose surfaces are changed */
1072 wl_list_for_each(psurface, &cp->protected_list, link)
1073 if (psurface && psurface->surface)
1074 weston_surface_compute_protection(psurface);
1075}
1076
Bryce Harrington3f650b82015-12-23 11:01:58 -08001077/**
1078 * \param es The surface
1079 * \param mask The new set of outputs for the surface
1080 *
1081 * Sets the surface's set of outputs to the ones specified by
1082 * the new output mask provided. Identifies the outputs that
1083 * have changed, the posts enter and leave events for these
1084 * outputs as appropriate.
1085 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001086static void
1087weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
1088{
1089 uint32_t different = es->output_mask ^ mask;
1090 uint32_t entered = mask & different;
1091 uint32_t left = es->output_mask & different;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001092 uint32_t output_bit;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001093 struct weston_output *output;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001094 struct weston_head *head;
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301095 struct content_protection *cp;
1096 struct wl_event_loop *loop;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001097
1098 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001099 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001100 return;
1101 if (different == 0)
1102 return;
1103
1104 wl_list_for_each(output, &es->compositor->output_list, link) {
Pekka Paalanen01e00682017-03-24 16:21:06 +02001105 output_bit = 1u << output->id;
1106 if (!(output_bit & different))
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001107 continue;
Pekka Paalanen01e00682017-03-24 16:21:06 +02001108
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03001109 wl_list_for_each(head, &output->head_list, output_link) {
1110 weston_surface_send_enter_leave(es, head,
1111 output_bit & entered,
1112 output_bit & left);
1113 }
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001114 }
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05301115 /*
1116 * Change in surfaces' output mask might trigger a change in its
1117 * protection.
1118 */
1119 loop = wl_display_get_event_loop(es->compositor->wl_display);
1120 cp = es->compositor->content_protection;
1121 if (!cp || cp->surface_protection_update)
1122 return;
1123 cp->surface_protection_update = wl_event_loop_add_idle(loop,
1124 notify_surface_protection_change,
1125 es->compositor);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001126}
1127
Semi Malinene7a52fb2018-04-26 11:08:10 +02001128static void
1129notify_view_output_destroy(struct wl_listener *listener, void *data)
1130{
1131 struct weston_view *view =
1132 container_of(listener,
1133 struct weston_view, output_destroy_listener);
1134
1135 view->output = NULL;
1136 view->output_destroy_listener.notify = NULL;
1137}
1138
1139/** Set the primary output of the view
1140 *
1141 * \param view The view whose primary output to set
1142 * \param output The new primary output for the view
1143 *
1144 * Set \a output to be the primary output of the \a view.
1145 *
1146 * Notice that the assignment may be temporary; the primary output could be
1147 * automatically changed. Hence, one cannot rely on the value persisting.
1148 *
1149 * Passing NULL as /a output will set the primary output to NULL.
1150 */
1151WL_EXPORT void
1152weston_view_set_output(struct weston_view *view, struct weston_output *output)
1153{
1154 if (view->output_destroy_listener.notify) {
1155 wl_list_remove(&view->output_destroy_listener.link);
1156 view->output_destroy_listener.notify = NULL;
1157 }
1158 view->output = output;
1159 if (output) {
1160 view->output_destroy_listener.notify =
1161 notify_view_output_destroy;
1162 wl_signal_add(&output->destroy_signal,
1163 &view->output_destroy_listener);
1164 }
1165}
1166
Bryce Harrington3f650b82015-12-23 11:01:58 -08001167/** Recalculate which output(s) the surface has views displayed on
1168 *
1169 * \param es The surface to remap to outputs
1170 *
1171 * Finds the output that is showing the largest amount of one
1172 * of the surface's various views. This output becomes the
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001173 * surface's primary output for vsync and frame callback purposes.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001174 *
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001175 * Also notes all outputs of all of the surface's views
Bryce Harrington3f650b82015-12-23 11:01:58 -08001176 * in the output_mask for the surface.
1177 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001178static void
1179weston_surface_assign_output(struct weston_surface *es)
1180{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001181 struct weston_output *new_output;
1182 struct weston_view *view;
1183 pixman_region32_t region;
1184 uint32_t max, area, mask;
1185 pixman_box32_t *e;
1186
1187 new_output = NULL;
1188 max = 0;
1189 mask = 0;
1190 pixman_region32_init(&region);
1191 wl_list_for_each(view, &es->views, surface_link) {
1192 if (!view->output)
1193 continue;
1194
1195 pixman_region32_intersect(&region, &view->transform.boundingbox,
1196 &view->output->region);
1197
1198 e = pixman_region32_extents(&region);
1199 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1200
1201 mask |= view->output_mask;
1202
1203 if (area >= max) {
1204 new_output = view->output;
1205 max = area;
1206 }
1207 }
1208 pixman_region32_fini(&region);
1209
1210 es->output = new_output;
1211 weston_surface_update_output_mask(es, mask);
1212}
1213
Bryce Harrington3f650b82015-12-23 11:01:58 -08001214/** Recalculate which output(s) the view is displayed on
1215 *
1216 * \param ev The view to remap to outputs
1217 *
1218 * Identifies the set of outputs that the view is visible on,
1219 * noting them into the output_mask. The output that the view
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001220 * is most visible on is set as the view's primary output.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001221 *
1222 * Also does the same for the view's surface. See
1223 * weston_surface_assign_output().
1224 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001225static void
1226weston_view_assign_output(struct weston_view *ev)
1227{
1228 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001229 struct weston_output *output, *new_output;
1230 pixman_region32_t region;
1231 uint32_t max, area, mask;
1232 pixman_box32_t *e;
1233
1234 new_output = NULL;
1235 max = 0;
1236 mask = 0;
1237 pixman_region32_init(&region);
1238 wl_list_for_each(output, &ec->output_list, link) {
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03001239 if (output->destroying)
1240 continue;
1241
Jason Ekstranda7af7042013-10-12 22:38:11 -05001242 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001243 &output->region);
1244
1245 e = pixman_region32_extents(&region);
1246 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1247
1248 if (area > 0)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001249 mask |= 1u << output->id;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001250
1251 if (area >= max) {
1252 new_output = output;
1253 max = area;
1254 }
1255 }
1256 pixman_region32_fini(&region);
1257
Semi Malinene7a52fb2018-04-26 11:08:10 +02001258 weston_view_set_output(ev, new_output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001259 ev->output_mask = mask;
1260
1261 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001262}
1263
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001264static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001265weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1266 int from_x, int from_y, int *to_x, int *to_y)
1267{
1268 float x, y;
1269
1270 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1271 weston_view_from_global_float(to, x, y, &x, &y);
1272
1273 *to_x = round(x);
1274 *to_y = round(y);
1275}
1276
1277static void
1278weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1279{
1280 pixman_box32_t *a;
1281 pixman_box32_t b;
1282
1283 a = pixman_region32_extents(&from->geometry.scissor);
1284
1285 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1286 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1287
1288 pixman_region32_fini(&to->geometry.scissor);
1289 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1290}
1291
1292static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001293view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001294 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001295{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001296 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1297 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001298 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001299 { inbox->x1, inbox->y1 },
1300 { inbox->x1, inbox->y2 },
1301 { inbox->x2, inbox->y1 },
1302 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001303 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001304 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001305 int i;
1306
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001307 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001308 /* avoid rounding empty bbox to 1x1 */
1309 pixman_region32_init(bbox);
1310 return;
1311 }
1312
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001313 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001314 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001315 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001316 if (x < min_x)
1317 min_x = x;
1318 if (x > max_x)
1319 max_x = x;
1320 if (y < min_y)
1321 min_y = y;
1322 if (y > max_y)
1323 max_y = y;
1324 }
1325
Pekka Paalanen219b9822012-02-08 15:38:37 +02001326 int_x = floorf(min_x);
1327 int_y = floorf(min_y);
1328 pixman_region32_init_rect(bbox, int_x, int_y,
1329 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001330}
1331
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001332static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001333weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001334{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001335 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001336
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001337 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001338 view->geometry.x = roundf(view->geometry.x);
1339 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001340
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001341 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001342 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1343 view->transform.position.matrix.d[12] = view->geometry.x;
1344 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001345
Jason Ekstranda7af7042013-10-12 22:38:11 -05001346 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001347
Jason Ekstranda7af7042013-10-12 22:38:11 -05001348 view->transform.inverse = view->transform.position.matrix;
1349 view->transform.inverse.d[12] = -view->geometry.x;
1350 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001351
Jason Ekstranda7af7042013-10-12 22:38:11 -05001352 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001353 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001354 view->surface->width,
1355 view->surface->height);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001356 if (view->geometry.scissor_enabled)
1357 pixman_region32_intersect(&view->transform.boundingbox,
1358 &view->transform.boundingbox,
1359 &view->geometry.scissor);
1360
1361 pixman_region32_translate(&view->transform.boundingbox,
1362 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001363
Jason Ekstranda7af7042013-10-12 22:38:11 -05001364 if (view->alpha == 1.0) {
1365 pixman_region32_copy(&view->transform.opaque,
1366 &view->surface->opaque);
1367 pixman_region32_translate(&view->transform.opaque,
1368 view->geometry.x,
1369 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001370 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001371}
1372
1373static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001374weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001375{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001376 struct weston_view *parent = view->geometry.parent;
1377 struct weston_matrix *matrix = &view->transform.matrix;
1378 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001379 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001380 pixman_region32_t surfregion;
1381 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001382
Jason Ekstranda7af7042013-10-12 22:38:11 -05001383 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001384
1385 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001386 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1387 view->transform.position.matrix.d[12] = view->geometry.x;
1388 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001389
1390 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001391 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001392 weston_matrix_multiply(matrix, &tform->matrix);
1393
Pekka Paalanen483243f2013-03-08 14:56:50 +02001394 if (parent)
1395 weston_matrix_multiply(matrix, &parent->transform.matrix);
1396
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001397 if (weston_matrix_invert(inverse, matrix) < 0) {
1398 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001399 weston_log("error: weston_view %p"
1400 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001401 return -1;
1402 }
1403
Giulio Camuffo148c1992016-09-04 18:50:46 +03001404 if (view->alpha == 1.0 &&
1405 matrix->type == WESTON_MATRIX_TRANSFORM_TRANSLATE) {
1406 pixman_region32_copy(&view->transform.opaque,
1407 &view->surface->opaque);
1408 pixman_region32_translate(&view->transform.opaque,
1409 matrix->d[12],
1410 matrix->d[13]);
1411 }
1412
Pekka Paalanen380adf52015-02-16 14:39:11 +02001413 pixman_region32_init_rect(&surfregion, 0, 0,
1414 view->surface->width, view->surface->height);
1415 if (view->geometry.scissor_enabled)
1416 pixman_region32_intersect(&surfregion, &surfregion,
1417 &view->geometry.scissor);
1418 surfbox = pixman_region32_extents(&surfregion);
1419
1420 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1421 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001422
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001423 return 0;
1424}
1425
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001426static struct weston_layer *
1427get_view_layer(struct weston_view *view)
1428{
1429 if (view->parent_view)
1430 return get_view_layer(view->parent_view);
1431 return view->layer_link.layer;
1432}
1433
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001434WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001435weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001436{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001437 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001438 struct weston_layer *layer;
1439 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001440
Jason Ekstranda7af7042013-10-12 22:38:11 -05001441 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001442 return;
1443
Pekka Paalanen483243f2013-03-08 14:56:50 +02001444 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001445 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001446
Jason Ekstranda7af7042013-10-12 22:38:11 -05001447 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001448
Jason Ekstranda7af7042013-10-12 22:38:11 -05001449 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001450
Jason Ekstranda7af7042013-10-12 22:38:11 -05001451 pixman_region32_fini(&view->transform.boundingbox);
1452 pixman_region32_fini(&view->transform.opaque);
1453 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001454
Pekka Paalanencd403622012-01-25 13:37:39 +02001455 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001456 if (view->geometry.transformation_list.next ==
1457 &view->transform.position.link &&
1458 view->geometry.transformation_list.prev ==
1459 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001460 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001461 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001462 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001463 if (weston_view_update_transform_enable(view) < 0)
1464 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001465 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001466
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001467 layer = get_view_layer(view);
1468 if (layer) {
1469 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001470 pixman_region32_intersect(&view->transform.boundingbox,
1471 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001472 pixman_region32_intersect(&view->transform.opaque,
1473 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001474 pixman_region32_fini(&mask);
1475 }
1476
Pekka Paalanen380adf52015-02-16 14:39:11 +02001477 if (parent) {
1478 if (parent->geometry.scissor_enabled) {
1479 view->geometry.scissor_enabled = true;
1480 weston_view_transfer_scissor(parent, view);
1481 } else {
1482 view->geometry.scissor_enabled = false;
1483 }
1484 }
1485
Jason Ekstranda7af7042013-10-12 22:38:11 -05001486 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001487
Jason Ekstranda7af7042013-10-12 22:38:11 -05001488 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001489
Jason Ekstranda7af7042013-10-12 22:38:11 -05001490 wl_signal_emit(&view->surface->compositor->transform_signal,
1491 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001492}
1493
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001494WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001495weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001496{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001497 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001498
1499 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001500 * The invariant: if view->geometry.dirty, then all views
1501 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001502 * Corollary: if not parent->geometry.dirty, then all ancestors
1503 * are not dirty.
1504 */
1505
Jason Ekstranda7af7042013-10-12 22:38:11 -05001506 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001507 return;
1508
Jason Ekstranda7af7042013-10-12 22:38:11 -05001509 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001510
Jason Ekstranda7af7042013-10-12 22:38:11 -05001511 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001512 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001513 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001514}
1515
1516WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001517weston_view_to_global_fixed(struct weston_view *view,
1518 wl_fixed_t vx, wl_fixed_t vy,
1519 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001520{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001521 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001522
Jason Ekstranda7af7042013-10-12 22:38:11 -05001523 weston_view_to_global_float(view,
1524 wl_fixed_to_double(vx),
1525 wl_fixed_to_double(vy),
1526 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001527 *x = wl_fixed_from_double(xf);
1528 *y = wl_fixed_from_double(yf);
1529}
1530
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001531WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001532weston_view_from_global_float(struct weston_view *view,
1533 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001534{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001535 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001536 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1537
Jason Ekstranda7af7042013-10-12 22:38:11 -05001538 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001539
1540 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001541 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001542 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001543 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001544 *vx = 0;
1545 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001546 return;
1547 }
1548
Jason Ekstranda7af7042013-10-12 22:38:11 -05001549 *vx = v.f[0] / v.f[3];
1550 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001551 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001552 *vx = x - view->geometry.x;
1553 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001554 }
1555}
1556
1557WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001558weston_view_from_global_fixed(struct weston_view *view,
1559 wl_fixed_t x, wl_fixed_t y,
1560 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001561{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001562 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001563
Jason Ekstranda7af7042013-10-12 22:38:11 -05001564 weston_view_from_global_float(view,
1565 wl_fixed_to_double(x),
1566 wl_fixed_to_double(y),
1567 &vxf, &vyf);
1568 *vx = wl_fixed_from_double(vxf);
1569 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001570}
1571
1572WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001573weston_view_from_global(struct weston_view *view,
1574 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001575{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001576 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001577
Jason Ekstranda7af7042013-10-12 22:38:11 -05001578 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1579 *vx = floorf(vxf);
1580 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001581}
1582
Bryce Harrington3f650b82015-12-23 11:01:58 -08001583/**
1584 * \param surface The surface to be repainted
1585 *
1586 * Marks the output(s) that the surface is shown on as needing to be
1587 * repainted. See weston_output_schedule_repaint().
1588 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001589WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001590weston_surface_schedule_repaint(struct weston_surface *surface)
1591{
1592 struct weston_output *output;
1593
1594 wl_list_for_each(output, &surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001595 if (surface->output_mask & (1u << output->id))
Kristian Høgsberg98238702012-08-03 16:29:12 -04001596 weston_output_schedule_repaint(output);
1597}
1598
Bryce Harrington3f650b82015-12-23 11:01:58 -08001599/**
1600 * \param view The view to be repainted
1601 *
1602 * Marks the output(s) that the view is shown on as needing to be
1603 * repainted. See weston_output_schedule_repaint().
1604 */
Kristian Høgsberg98238702012-08-03 16:29:12 -04001605WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001606weston_view_schedule_repaint(struct weston_view *view)
1607{
1608 struct weston_output *output;
1609
1610 wl_list_for_each(output, &view->surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001611 if (view->output_mask & (1u << output->id))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001612 weston_output_schedule_repaint(output);
1613}
1614
Pekka Paalanene508ce62015-02-19 13:59:55 +02001615/**
1616 * XXX: This function does it the wrong way.
1617 * surface->damage is the damage from the client, and causes
1618 * surface_flush_damage() to copy pixels. No window management action can
1619 * cause damage to the client-provided content, warranting re-upload!
1620 *
1621 * Instead of surface->damage, this function should record the damage
1622 * with all the views for this surface to avoid extraneous texture
1623 * uploads.
1624 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001625WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001626weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001627{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001628 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001629 0, 0, surface->width,
1630 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001631
Kristian Høgsberg98238702012-08-03 16:29:12 -04001632 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001633}
1634
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001635WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001636weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001637{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001638 if (view->geometry.x == x && view->geometry.y == y)
1639 return;
1640
Jason Ekstranda7af7042013-10-12 22:38:11 -05001641 view->geometry.x = x;
1642 view->geometry.y = y;
1643 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001644}
1645
Pekka Paalanen483243f2013-03-08 14:56:50 +02001646static void
1647transform_parent_handle_parent_destroy(struct wl_listener *listener,
1648 void *data)
1649{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001650 struct weston_view *view =
1651 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001652 geometry.parent_destroy_listener);
1653
Jason Ekstranda7af7042013-10-12 22:38:11 -05001654 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001655}
1656
1657WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001658weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001659 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001660{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001661 if (view->geometry.parent) {
1662 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1663 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001664
1665 if (!parent)
1666 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001667 }
1668
Jason Ekstranda7af7042013-10-12 22:38:11 -05001669 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001670
Jason Ekstranda7af7042013-10-12 22:38:11 -05001671 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001672 transform_parent_handle_parent_destroy;
1673 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001674 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001675 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001676 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001677 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001678 }
1679
Jason Ekstranda7af7042013-10-12 22:38:11 -05001680 weston_view_geometry_dirty(view);
1681}
1682
Pekka Paalanen380adf52015-02-16 14:39:11 +02001683/** Set a clip mask rectangle on a view
1684 *
1685 * \param view The view to set the clip mask on.
1686 * \param x Top-left corner X coordinate of the clip rectangle.
1687 * \param y Top-left corner Y coordinate of the clip rectangle.
1688 * \param width Width of the clip rectangle, non-negative.
1689 * \param height Height of the clip rectangle, non-negative.
1690 *
1691 * A shell may set a clip mask rectangle on a view. Everything outside
1692 * the rectangle is cut away for input and output purposes: it is
1693 * not drawn and cannot be hit by hit-test based input like pointer
1694 * motion or touch-downs. Everything inside the rectangle will behave
1695 * normally. Clients are unaware of clipping.
1696 *
Yong Bakos4c72e292016-04-28 11:59:10 -05001697 * The rectangle is set in surface-local coordinates. Setting a clip
Pekka Paalanen380adf52015-02-16 14:39:11 +02001698 * mask rectangle does not affect the view position, the view is positioned
1699 * as it would be without a clip. The clip also does not change
1700 * weston_surface::width,height.
1701 *
1702 * The clip mask rectangle is part of transformation inheritance
1703 * (weston_view_set_transform_parent()). A clip set in the root of the
1704 * transformation inheritance tree will affect all views in the tree.
1705 * A clip can be set only on the root view. Attempting to set a clip
1706 * on view that has a transformation parent will fail. Assigning a parent
1707 * to a view that has a clip set will cause the clip to be forgotten.
1708 *
1709 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1710 * on the additional transformations in the child views. These transformations
1711 * may not rotate the coordinate axes, i.e., only translation and scaling
1712 * are allowed. Violating this restriction causes the clipping to malfunction.
1713 * Furthermore, using scaling may cause rounding errors in child clipping.
1714 *
1715 * The clip mask rectangle is not automatically adjusted based on
1716 * wl_surface.attach dx and dy arguments.
1717 *
1718 * A clip mask rectangle can be set only if the compositor capability
1719 * WESTON_CAP_VIEW_CLIP_MASK is present.
1720 *
1721 * This function sets the clip mask rectangle and schedules a repaint for
1722 * the view.
1723 */
1724WL_EXPORT void
1725weston_view_set_mask(struct weston_view *view,
1726 int x, int y, int width, int height)
1727{
1728 struct weston_compositor *compositor = view->surface->compositor;
1729
1730 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1731 weston_log("%s not allowed without capability!\n", __func__);
1732 return;
1733 }
1734
1735 if (view->geometry.parent) {
1736 weston_log("view %p has a parent, clip forbidden!\n", view);
1737 return;
1738 }
1739
1740 if (width < 0 || height < 0) {
1741 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1742 x, y, width, height);
1743 return;
1744 }
1745
1746 pixman_region32_fini(&view->geometry.scissor);
1747 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1748 view->geometry.scissor_enabled = true;
1749 weston_view_geometry_dirty(view);
1750 weston_view_schedule_repaint(view);
1751}
1752
1753/** Remove the clip mask from a view
1754 *
1755 * \param view The view to remove the clip mask from.
1756 *
1757 * Removed the clip mask rectangle and schedules a repaint.
1758 *
1759 * \sa weston_view_set_mask
1760 */
1761WL_EXPORT void
1762weston_view_set_mask_infinite(struct weston_view *view)
1763{
1764 view->geometry.scissor_enabled = false;
1765 weston_view_geometry_dirty(view);
1766 weston_view_schedule_repaint(view);
1767}
1768
Armin Krezović0da12b82016-06-30 06:04:33 +02001769/* Check if view should be displayed
1770 *
1771 * The indicator is set manually when assigning
1772 * a view to a surface.
1773 *
1774 * This needs reworking. See the thread starting at:
1775 *
1776 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1777 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001778WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001779weston_view_is_mapped(struct weston_view *view)
1780{
Armin Krezović0da12b82016-06-30 06:04:33 +02001781 return view->is_mapped;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001782}
1783
Philipp Zabel70decd52018-09-03 20:11:15 +02001784/* Check if view is opaque in specified region
1785 *
1786 * \param view The view to check for opacity.
1787 * \param region The region to check for opacity, in view coordinates.
1788 *
1789 * Returns true if the view is opaque in the specified region, because view
1790 * alpha is 1.0 and either the opaque region set by the client contains the
1791 * specified region, or the buffer pixel format or solid color is opaque.
1792 */
1793WL_EXPORT bool
1794weston_view_is_opaque(struct weston_view *ev, pixman_region32_t *region)
1795{
1796 pixman_region32_t r;
1797 bool ret = false;
1798
1799 if (ev->alpha < 1.0)
1800 return false;
1801
1802 if (ev->surface->is_opaque)
1803 return true;
1804
1805 if (ev->transform.dirty) {
1806 weston_log("%s: transform dirty", __func__);
1807 return false;
1808 }
1809
1810 pixman_region32_init(&r);
1811 pixman_region32_subtract(&r, region, &ev->transform.opaque);
1812
1813 if (!pixman_region32_not_empty(&r))
1814 ret = true;
1815
1816 pixman_region32_fini(&r);
1817
1818 return ret;
1819}
1820
Armin Krezović0da12b82016-06-30 06:04:33 +02001821/* Check if a surface has a view assigned to it
1822 *
1823 * The indicator is set manually when mapping
1824 * a surface and creating a view for it.
1825 *
1826 * This needs to go. See the thread starting at:
1827 *
1828 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1829 *
1830 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001831WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001832weston_surface_is_mapped(struct weston_surface *surface)
1833{
Armin Krezović0da12b82016-06-30 06:04:33 +02001834 return surface->is_mapped;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001835}
1836
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001837static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001838surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001839{
1840 struct weston_view *view;
1841
1842 if (surface->width == width && surface->height == height)
1843 return;
1844
1845 surface->width = width;
1846 surface->height = height;
1847
1848 wl_list_for_each(view, &surface->views, surface_link)
1849 weston_view_geometry_dirty(view);
1850}
1851
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001852WL_EXPORT void
1853weston_surface_set_size(struct weston_surface *surface,
1854 int32_t width, int32_t height)
1855{
1856 assert(!surface->resource);
1857 surface_set_size(surface, width, height);
1858}
1859
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001860static int
1861fixed_round_up_to_int(wl_fixed_t f)
1862{
1863 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1864}
1865
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001866static void
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001867convert_size_by_transform_scale(int32_t *width_out, int32_t *height_out,
1868 int32_t width, int32_t height,
1869 uint32_t transform,
1870 int32_t scale)
1871{
1872 assert(scale > 0);
1873
1874 switch (transform) {
1875 case WL_OUTPUT_TRANSFORM_NORMAL:
1876 case WL_OUTPUT_TRANSFORM_180:
1877 case WL_OUTPUT_TRANSFORM_FLIPPED:
1878 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1879 *width_out = width / scale;
1880 *height_out = height / scale;
1881 break;
1882 case WL_OUTPUT_TRANSFORM_90:
1883 case WL_OUTPUT_TRANSFORM_270:
1884 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1885 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1886 *width_out = height / scale;
1887 *height_out = width / scale;
1888 break;
1889 default:
1890 assert(0 && "invalid transform");
1891 }
1892}
1893
1894static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001895weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001896{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001897 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001898
1899 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001900 surface->width_from_buffer = 0;
1901 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001902 return;
1903 }
1904
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001905 convert_size_by_transform_scale(&surface->width_from_buffer,
1906 &surface->height_from_buffer,
1907 surface->buffer_ref.buffer->width,
1908 surface->buffer_ref.buffer->height,
1909 vp->buffer.transform,
1910 vp->buffer.scale);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001911}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001912
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001913static void
1914weston_surface_update_size(struct weston_surface *surface)
1915{
1916 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1917 int32_t width, height;
1918
1919 width = surface->width_from_buffer;
1920 height = surface->height_from_buffer;
1921
1922 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001923 surface_set_size(surface,
1924 vp->surface.width, vp->surface.height);
1925 return;
1926 }
1927
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001928 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001929 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1930 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1931
1932 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001933 return;
1934 }
1935
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001936 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001937}
1938
Marius Vlad9fdda7f2019-06-11 16:08:55 +03001939/** weston_compositor_get_time
1940 * \ingroup compositor
1941 */
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02001942WL_EXPORT void
1943weston_compositor_get_time(struct timespec *time)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001944{
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02001945 clock_gettime(CLOCK_REALTIME, time);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001946}
1947
Marius Vlad9fdda7f2019-06-11 16:08:55 +03001948/** weston_compositor_pick_view
1949 * \ingroup compositor
1950 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001951WL_EXPORT struct weston_view *
1952weston_compositor_pick_view(struct weston_compositor *compositor,
1953 wl_fixed_t x, wl_fixed_t y,
1954 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001955{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001956 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001957 wl_fixed_t view_x, view_y;
1958 int view_ix, view_iy;
1959 int ix = wl_fixed_to_int(x);
1960 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001961
Jason Ekstranda7af7042013-10-12 22:38:11 -05001962 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001963 if (!pixman_region32_contains_point(
1964 &view->transform.boundingbox, ix, iy, NULL))
1965 continue;
1966
1967 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
1968 view_ix = wl_fixed_to_int(view_x);
1969 view_iy = wl_fixed_to_int(view_y);
1970
1971 if (!pixman_region32_contains_point(&view->surface->input,
1972 view_ix, view_iy, NULL))
1973 continue;
1974
Pekka Paalanen380adf52015-02-16 14:39:11 +02001975 if (view->geometry.scissor_enabled &&
1976 !pixman_region32_contains_point(&view->geometry.scissor,
1977 view_ix, view_iy, NULL))
1978 continue;
1979
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001980 *vx = view_x;
1981 *vy = view_y;
1982 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001983 }
1984
Derek Foremanf9318d12015-05-11 15:40:11 -05001985 *vx = wl_fixed_from_int(-1000000);
1986 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001987 return NULL;
1988}
1989
1990static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001991weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001992{
Daniel Stone37816df2012-05-16 18:45:18 +01001993 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001994
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001995 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001996 return;
1997
Daniel Stone37816df2012-05-16 18:45:18 +01001998 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001999 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002000}
2001
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002002WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002003weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05002004{
Daniel Stone4dab5db2012-05-30 16:31:53 +01002005 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002006
Jason Ekstranda7af7042013-10-12 22:38:11 -05002007 if (!weston_view_is_mapped(view))
2008 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002009
Jason Ekstranda7af7042013-10-12 22:38:11 -05002010 weston_view_damage_below(view);
Semi Malinene7a52fb2018-04-26 11:08:10 +02002011 weston_view_set_output(view, NULL);
Xiong Zhang97116532013-10-23 13:58:31 +08002012 view->plane = NULL;
Armin Krezovićf8486c32016-06-30 06:04:28 +02002013 view->is_mapped = false;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002014 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002015 wl_list_remove(&view->link);
2016 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002017 view->output_mask = 0;
2018 weston_surface_assign_output(view->surface);
2019
2020 if (weston_surface_is_mapped(view->surface))
2021 return;
2022
2023 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002024 struct weston_touch *touch = weston_seat_get_touch(seat);
2025 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2026 struct weston_keyboard *keyboard =
2027 weston_seat_get_keyboard(seat);
2028
2029 if (keyboard && keyboard->focus == view->surface)
2030 weston_keyboard_set_focus(keyboard, NULL);
2031 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05002032 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05002033 if (touch && touch->focus == view)
2034 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01002035 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002036}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05002037
Jason Ekstranda7af7042013-10-12 22:38:11 -05002038WL_EXPORT void
2039weston_surface_unmap(struct weston_surface *surface)
2040{
2041 struct weston_view *view;
2042
Armin Krezovićf8486c32016-06-30 06:04:28 +02002043 surface->is_mapped = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002044 wl_list_for_each(view, &surface->views, surface_link)
2045 weston_view_unmap(view);
2046 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05002047}
2048
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002049static void
2050weston_surface_reset_pending_buffer(struct weston_surface *surface)
2051{
Jason Ekstrand7b982072014-05-20 14:33:03 -05002052 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002053 surface->pending.sx = 0;
2054 surface->pending.sy = 0;
2055 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002056 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002057}
2058
Jason Ekstranda7af7042013-10-12 22:38:11 -05002059WL_EXPORT void
2060weston_view_destroy(struct weston_view *view)
2061{
2062 wl_signal_emit(&view->destroy_signal, view);
2063
2064 assert(wl_list_empty(&view->geometry.child_list));
2065
2066 if (weston_view_is_mapped(view)) {
2067 weston_view_unmap(view);
2068 weston_compositor_build_view_list(view->surface->compositor);
2069 }
2070
2071 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002072 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002073
2074 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02002075 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002076 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002077 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002078
2079 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen944fae82018-05-22 13:15:58 +03002080 weston_view_set_output(view, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002081
Jason Ekstranda7af7042013-10-12 22:38:11 -05002082 wl_list_remove(&view->surface_link);
2083
2084 free(view);
2085}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002086
2087WL_EXPORT void
2088weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05002089{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002090 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002091 struct weston_view *ev, *nv;
Jonas Ådahld3414f22016-07-22 17:56:31 +08002092 struct weston_pointer_constraint *constraint, *next_constraint;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04002093
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02002094 if (--surface->ref_count > 0)
2095 return;
2096
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03002097 assert(surface->resource == NULL);
2098
Pekka Paalanenca790762015-04-17 14:23:38 +03002099 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02002100
Pekka Paalanene67858b2013-04-25 13:57:42 +03002101 assert(wl_list_empty(&surface->subsurface_list_pending));
2102 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02002103
Jason Ekstranda7af7042013-10-12 22:38:11 -05002104 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
2105 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002106
Jason Ekstrand7b982072014-05-20 14:33:03 -05002107 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002108
Pekka Paalanende685b82012-12-04 15:58:12 +02002109 weston_buffer_reference(&surface->buffer_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002110 weston_buffer_release_reference(&surface->buffer_release_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04002111
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02002112 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002113 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002114 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02002115
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002116 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002117 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04002118
Pekka Paalanen133e4392014-09-23 22:08:46 -04002119 weston_presentation_feedback_discard_list(&surface->feedback_list);
2120
Jonas Ådahld3414f22016-07-22 17:56:31 +08002121 wl_list_for_each_safe(constraint, next_constraint,
2122 &surface->pointer_constraints,
2123 link)
2124 weston_pointer_constraint_destroy(constraint);
2125
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03002126 fd_clear(&surface->acquire_fence_fd);
2127
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04002128 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05002129}
2130
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002131static void
2132destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08002133{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002134 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08002135
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03002136 assert(surface);
2137
Giulio Camuffo0d379742013-11-15 22:06:15 +01002138 /* Set the resource to NULL, since we don't want to leave a
2139 * dangling pointer if the surface was refcounted and survives
2140 * the weston_surface_destroy() call. */
2141 surface->resource = NULL;
Pekka Paalanen4826f872016-04-22 14:14:38 +03002142
2143 if (surface->viewport_resource)
2144 wl_resource_set_user_data(surface->viewport_resource, NULL);
2145
Alexandros Frantzis27d7c392018-10-19 12:14:11 +03002146 if (surface->synchronization_resource) {
2147 wl_resource_set_user_data(surface->synchronization_resource,
2148 NULL);
2149 }
2150
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002151 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08002152}
2153
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002154static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002155weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
2156{
2157 struct weston_buffer *buffer =
2158 container_of(listener, struct weston_buffer, destroy_listener);
2159
2160 wl_signal_emit(&buffer->destroy_signal, buffer);
2161 free(buffer);
2162}
2163
Giulio Camuffoe058cd12013-12-12 14:14:29 +01002164WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002165weston_buffer_from_resource(struct wl_resource *resource)
2166{
2167 struct weston_buffer *buffer;
2168 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002169
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002170 listener = wl_resource_get_destroy_listener(resource,
2171 weston_buffer_destroy_handler);
2172
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002173 if (listener)
2174 return container_of(listener, struct weston_buffer,
2175 destroy_listener);
2176
2177 buffer = zalloc(sizeof *buffer);
2178 if (buffer == NULL)
2179 return NULL;
2180
2181 buffer->resource = resource;
2182 wl_signal_init(&buffer->destroy_signal);
2183 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04002184 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002185 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08002186
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002187 return buffer;
2188}
2189
2190static void
Pekka Paalanende685b82012-12-04 15:58:12 +02002191weston_buffer_reference_handle_destroy(struct wl_listener *listener,
2192 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002193{
Pekka Paalanende685b82012-12-04 15:58:12 +02002194 struct weston_buffer_reference *ref =
2195 container_of(listener, struct weston_buffer_reference,
2196 destroy_listener);
2197
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002198 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02002199 ref->buffer = NULL;
2200}
2201
2202WL_EXPORT void
2203weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002204 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002205{
2206 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05002207 ref->buffer->busy_count--;
2208 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002209 assert(wl_resource_get_client(ref->buffer->resource));
Matt Hoosier3052bc72017-09-26 08:09:40 -05002210 wl_buffer_send_release(ref->buffer->resource);
Kristian Høgsberg20347802013-03-04 12:07:46 -05002211 }
Pekka Paalanende685b82012-12-04 15:58:12 +02002212 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002213 }
2214
Pekka Paalanende685b82012-12-04 15:58:12 +02002215 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04002216 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002217 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02002218 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02002219 }
2220
Pekka Paalanende685b82012-12-04 15:58:12 +02002221 ref->buffer = buffer;
2222 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
2223}
2224
2225static void
Alexandros Frantzis67629672018-10-19 12:14:11 +03002226weston_buffer_release_reference_handle_destroy(struct wl_listener *listener,
2227 void *data)
2228{
2229 struct weston_buffer_release_reference *ref =
2230 container_of(listener, struct weston_buffer_release_reference,
2231 destroy_listener);
2232
2233 assert((struct wl_resource *)data == ref->buffer_release->resource);
2234 ref->buffer_release = NULL;
2235}
2236
2237static void
2238weston_buffer_release_destroy(struct weston_buffer_release *buffer_release)
2239{
2240 struct wl_resource *resource = buffer_release->resource;
2241 int release_fence_fd = buffer_release->fence_fd;
2242
2243 if (release_fence_fd >= 0) {
2244 zwp_linux_buffer_release_v1_send_fenced_release(
2245 resource, release_fence_fd);
2246 } else {
2247 zwp_linux_buffer_release_v1_send_immediate_release(
2248 resource);
2249 }
2250
2251 wl_resource_destroy(resource);
2252}
2253
2254WL_EXPORT void
2255weston_buffer_release_reference(struct weston_buffer_release_reference *ref,
2256 struct weston_buffer_release *buffer_release)
2257{
2258 if (buffer_release == ref->buffer_release)
2259 return;
2260
2261 if (ref->buffer_release) {
2262 ref->buffer_release->ref_count--;
2263 wl_list_remove(&ref->destroy_listener.link);
2264 if (ref->buffer_release->ref_count == 0)
2265 weston_buffer_release_destroy(ref->buffer_release);
2266 }
2267
2268 if (buffer_release) {
2269 buffer_release->ref_count++;
2270 wl_resource_add_destroy_listener(buffer_release->resource,
2271 &ref->destroy_listener);
2272 }
2273
2274 ref->buffer_release = buffer_release;
2275 ref->destroy_listener.notify =
2276 weston_buffer_release_reference_handle_destroy;
2277}
2278
2279WL_EXPORT void
2280weston_buffer_release_move(struct weston_buffer_release_reference *dest,
2281 struct weston_buffer_release_reference *src)
2282{
2283 weston_buffer_release_reference(dest, src->buffer_release);
2284 weston_buffer_release_reference(src, NULL);
2285}
2286
2287static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002288weston_surface_attach(struct weston_surface *surface,
2289 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02002290{
2291 weston_buffer_reference(&surface->buffer_ref, buffer);
2292
Pekka Paalanena6421c42012-12-04 15:58:10 +02002293 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002294 if (weston_surface_is_mapped(surface))
2295 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002296 }
2297
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002298 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02002299
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002300 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002301 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002302}
2303
Marius Vlad9fdda7f2019-06-11 16:08:55 +03002304/** weston_compositor_damage_all
2305 * \ingroup compositor
2306 */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002307WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002308weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002309{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002310 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002311
2312 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002313 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002314}
2315
Marius Vlad55d87362019-06-11 01:15:35 +03002316/**
2317 * \ingroup output
2318 */
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05002319WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002320weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002321{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002322 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002323
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002324 pixman_region32_union(&compositor->primary_plane.damage,
2325 &compositor->primary_plane.damage,
2326 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002327 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002328}
2329
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04002330static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002331surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002332{
Pekka Paalanende685b82012-12-04 15:58:12 +02002333 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002334 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002335 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002336
Pekka Paalanenb5026542014-11-12 15:09:24 +02002337 if (weston_timeline_enabled_ &&
2338 pixman_region32_not_empty(&surface->damage))
2339 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
2340 TLP_OUTPUT(surface->output), TLP_END);
2341
Jason Ekstrandef540082014-06-26 10:37:36 -07002342 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002343}
2344
2345static void
2346view_accumulate_damage(struct weston_view *view,
2347 pixman_region32_t *opaque)
2348{
2349 pixman_region32_t damage;
2350
2351 pixman_region32_init(&damage);
2352 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002353 pixman_box32_t *extents;
2354
Jason Ekstranda7af7042013-10-12 22:38:11 -05002355 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002356 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002357 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002358 pixman_region32_copy(&damage, &view->surface->damage);
2359 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002360 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002361 }
2362
Pekka Paalanen380adf52015-02-16 14:39:11 +02002363 pixman_region32_intersect(&damage, &damage,
2364 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002365 pixman_region32_subtract(&damage, &damage, opaque);
2366 pixman_region32_union(&view->plane->damage,
2367 &view->plane->damage, &damage);
2368 pixman_region32_fini(&damage);
2369 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002370 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002371}
2372
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002373static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002374compositor_accumulate_damage(struct weston_compositor *ec)
2375{
2376 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002377 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002378 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002379
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002380 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002381
2382 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002383 pixman_region32_copy(&plane->clip, &clip);
2384
2385 pixman_region32_init(&opaque);
2386
Jason Ekstranda7af7042013-10-12 22:38:11 -05002387 wl_list_for_each(ev, &ec->view_list, link) {
2388 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002389 continue;
2390
Jason Ekstranda7af7042013-10-12 22:38:11 -05002391 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002392 }
2393
2394 pixman_region32_union(&clip, &clip, &opaque);
2395 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002396 }
2397
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002398 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002399
Jason Ekstranda7af7042013-10-12 22:38:11 -05002400 wl_list_for_each(ev, &ec->view_list, link)
Derek Foreman060cf112015-11-18 16:32:26 -06002401 ev->surface->touched = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002402
2403 wl_list_for_each(ev, &ec->view_list, link) {
2404 if (ev->surface->touched)
2405 continue;
Derek Foreman060cf112015-11-18 16:32:26 -06002406 ev->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002407
2408 surface_flush_damage(ev->surface);
2409
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002410 /* Both the renderer and the backend have seen the buffer
2411 * by now. If renderer needs the buffer, it has its own
2412 * reference set. If the backend wants to keep the buffer
2413 * around for migrating the surface into a non-primary plane
2414 * later, keep_buffer is true. Otherwise, drop the core
2415 * reference now, and allow early buffer release. This enables
2416 * clients to use single-buffering.
2417 */
Alexandros Frantzis67629672018-10-19 12:14:11 +03002418 if (!ev->surface->keep_buffer) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002419 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Alexandros Frantzis67629672018-10-19 12:14:11 +03002420 weston_buffer_release_reference(
2421 &ev->surface->buffer_release_ref, NULL);
2422 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002423 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002424}
2425
2426static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002427surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002428{
2429 struct weston_subsurface *sub;
2430
Pekka Paalanene67858b2013-04-25 13:57:42 +03002431 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002432 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002433 continue;
2434
Jason Ekstranda7af7042013-10-12 22:38:11 -05002435 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2436 wl_list_init(&sub->surface->views);
2437
2438 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002439 }
2440}
2441
2442static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002443surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002444{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002445 struct weston_subsurface *sub;
2446 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002447
Jason Ekstranda7af7042013-10-12 22:38:11 -05002448 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2449 if (sub->surface == surface)
2450 continue;
2451
George Kiagiadakised04d382014-06-13 18:10:26 +02002452 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2453 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002454 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002455 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002456
2457 surface_free_unused_subsurface_views(sub->surface);
2458 }
2459}
2460
2461static void
2462view_list_add_subsurface_view(struct weston_compositor *compositor,
2463 struct weston_subsurface *sub,
2464 struct weston_view *parent)
2465{
2466 struct weston_subsurface *child;
2467 struct weston_view *view = NULL, *iv;
2468
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002469 if (!weston_surface_is_mapped(sub->surface))
2470 return;
2471
Jason Ekstranda7af7042013-10-12 22:38:11 -05002472 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2473 if (iv->geometry.parent == parent) {
2474 view = iv;
2475 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002476 }
2477 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002478
2479 if (view) {
2480 /* Put it back in the surface's list of views */
2481 wl_list_remove(&view->surface_link);
2482 wl_list_insert(&sub->surface->views, &view->surface_link);
2483 } else {
2484 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002485 weston_view_set_position(view,
2486 sub->position.x,
2487 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002488 weston_view_set_transform_parent(view, parent);
2489 }
2490
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002491 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002492 weston_view_update_transform(view);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002493 view->is_mapped = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002494
Pekka Paalanenb188e912013-11-19 14:03:35 +02002495 if (wl_list_empty(&sub->surface->subsurface_list)) {
2496 wl_list_insert(compositor->view_list.prev, &view->link);
2497 return;
2498 }
2499
2500 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2501 if (child->surface == sub->surface)
2502 wl_list_insert(compositor->view_list.prev, &view->link);
2503 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002504 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002505 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002506}
2507
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002508/* This recursively adds the sub-surfaces for a view, relying on the
2509 * sub-surface order. Thus, if a client restacks the sub-surfaces, that
2510 * change first happens to the sub-surface list, and then automatically
2511 * propagates here. See weston_surface_damage_subsurfaces() for how the
2512 * sub-surfaces receive damage when the client changes the state.
2513 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002514static void
2515view_list_add(struct weston_compositor *compositor,
2516 struct weston_view *view)
2517{
2518 struct weston_subsurface *sub;
2519
2520 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002521
Pekka Paalanenb188e912013-11-19 14:03:35 +02002522 if (wl_list_empty(&view->surface->subsurface_list)) {
2523 wl_list_insert(compositor->view_list.prev, &view->link);
2524 return;
2525 }
2526
2527 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2528 if (sub->surface == view->surface)
2529 wl_list_insert(compositor->view_list.prev, &view->link);
2530 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002531 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002532 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002533}
2534
2535static void
2536weston_compositor_build_view_list(struct weston_compositor *compositor)
2537{
2538 struct weston_view *view;
2539 struct weston_layer *layer;
2540
2541 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002542 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002543 surface_stash_subsurface_views(view->surface);
2544
2545 wl_list_init(&compositor->view_list);
2546 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002547 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002548 view_list_add(compositor, view);
2549 }
2550 }
2551
2552 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002553 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002554 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002555}
2556
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002557static void
2558weston_output_take_feedback_list(struct weston_output *output,
2559 struct weston_surface *surface)
2560{
2561 struct weston_view *view;
2562 struct weston_presentation_feedback *feedback;
2563 uint32_t flags = 0xffffffff;
2564
2565 if (wl_list_empty(&surface->feedback_list))
2566 return;
2567
2568 /* All views must have the flag for the flag to survive. */
2569 wl_list_for_each(view, &surface->views, surface_link) {
2570 /* ignore views that are not on this output at all */
2571 if (view->output_mask & (1u << output->id))
2572 flags &= view->psf_flags;
2573 }
2574
2575 wl_list_for_each(feedback, &surface->feedback_list, link)
2576 feedback->psf_flags = flags;
2577
2578 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2579 wl_list_init(&surface->feedback_list);
2580}
2581
David Herrmann1edf44c2013-10-22 17:11:26 +02002582static int
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002583weston_output_repaint(struct weston_output *output, void *repaint_data)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002584{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002585 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002586 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002587 struct weston_animation *animation, *next;
2588 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002589 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002590 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002591 int r;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002592 uint32_t frame_time_msec;
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05302593 enum weston_hdcp_protection highest_requested = WESTON_HDCP_DISABLE;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002594
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002595 if (output->destroying)
2596 return 0;
2597
Pekka Paalanenb5026542014-11-12 15:09:24 +02002598 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2599
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002600 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002601 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002602
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05302603 /* Find the highest protection desired for an output */
2604 wl_list_for_each(ev, &ec->view_list, link) {
2605 if (ev->surface->output_mask & (1u << output->id)) {
2606 /*
2607 * The desired_protection of the output should be the
2608 * maximum of the desired_protection of the surfaces,
2609 * that are displayed on that output, to avoid
2610 * reducing the protection for existing surfaces.
2611 */
2612 if (ev->surface->desired_protection > highest_requested)
2613 highest_requested =
2614 ev->surface->desired_protection;
2615 }
2616 }
2617
2618 output->desired_protection = highest_requested;
2619
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002620 if (output->assign_planes && !output->disable_planes) {
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002621 output->assign_planes(output, repaint_data);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002622 } else {
2623 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002624 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002625 ev->psf_flags = 0;
2626 }
2627 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002628
Pekka Paalanene67858b2013-04-25 13:57:42 +03002629 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002630 wl_list_for_each(ev, &ec->view_list, link) {
2631 /* Note: This operation is safe to do multiple times on the
2632 * same surface.
2633 */
2634 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002635 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002636 &ev->surface->frame_callback_list);
2637 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002638
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002639 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002640 }
2641 }
2642
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002643 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002644
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002645 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002646 pixman_region32_intersect(&output_damage,
2647 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002648 pixman_region32_subtract(&output_damage,
2649 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002650
Scott Moreauccbf29d2012-02-22 14:21:41 -07002651 if (output->dirty)
2652 weston_output_update_matrix(output);
2653
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002654 r = output->repaint(output, &output_damage, repaint_data);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002655
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002656 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002657
Daniel Stone09a97e22017-03-01 11:34:06 +00002658 output->repaint_needed = false;
Daniel Stone05df8c12017-03-03 16:59:42 +00002659 if (r == 0)
2660 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002661
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002662 weston_compositor_repick(ec);
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002663
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002664 frame_time_msec = timespec_to_msec(&output->frame_time);
2665
Jonas Ådahldb773762012-06-13 00:01:21 +02002666 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002667 wl_callback_send_done(cb->resource, frame_time_msec);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002668 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002669 }
2670
Scott Moreaud64cf212012-06-08 19:40:54 -06002671 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002672 animation->frame_counter++;
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002673 animation->frame(animation, output, &output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002674 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002675
Pekka Paalanenb5026542014-11-12 15:09:24 +02002676 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2677
David Herrmann1edf44c2013-10-22 17:11:26 +02002678 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002679}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002680
Pekka Paalanen82919792014-05-21 13:51:49 +03002681static void
2682weston_output_schedule_repaint_reset(struct weston_output *output)
2683{
Daniel Stone05df8c12017-03-03 16:59:42 +00002684 output->repaint_status = REPAINT_NOT_SCHEDULED;
Pekka Paalanen82919792014-05-21 13:51:49 +03002685 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
Pekka Paalanen82919792014-05-21 13:51:49 +03002686}
2687
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002688static int
2689weston_output_maybe_repaint(struct weston_output *output, struct timespec *now,
2690 void *repaint_data)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002691{
Pekka Paalanen0513a952014-05-21 16:17:27 +03002692 struct weston_compositor *compositor = output->compositor;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002693 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002694 int64_t msec_to_repaint;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002695
Daniel Stone6847b852017-03-01 11:34:08 +00002696 /* We're not ready yet; come back to make a decision later. */
2697 if (output->repaint_status != REPAINT_SCHEDULED)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002698 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002699
2700 msec_to_repaint = timespec_sub_to_msec(&output->next_repaint, now);
2701 if (msec_to_repaint > 1)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002702 return ret;
Daniel Stone05df8c12017-03-03 16:59:42 +00002703
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002704 /* If we're sleeping, drop the repaint machinery entirely; we will
2705 * explicitly repaint all outputs when we come back. */
2706 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2707 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
2708 goto err;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002709
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002710 /* We don't actually need to repaint this output; drop it from
2711 * repaint until something causes damage. */
2712 if (!output->repaint_needed)
2713 goto err;
2714
2715 /* If repaint fails, we aren't going to get weston_output_finish_frame
2716 * to trigger a new repaint, so drop it from repaint and hope
Daniel Stone6847b852017-03-01 11:34:08 +00002717 * something schedules a successful repaint later. As repainting may
2718 * take some time, re-read our clock as a courtesy to the next
2719 * output. */
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002720 ret = weston_output_repaint(output, repaint_data);
Daniel Stone6847b852017-03-01 11:34:08 +00002721 weston_compositor_read_presentation_clock(compositor, now);
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002722 if (ret != 0)
2723 goto err;
2724
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09002725 output->repainted = true;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002726 return ret;
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002727
2728err:
Pekka Paalanen0513a952014-05-21 16:17:27 +03002729 weston_output_schedule_repaint_reset(output);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002730 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002731}
2732
2733static void
2734output_repaint_timer_arm(struct weston_compositor *compositor)
2735{
2736 struct weston_output *output;
2737 bool any_should_repaint = false;
2738 struct timespec now;
Sergi Granellb4c08862017-03-18 13:01:15 +01002739 int64_t msec_to_next = INT64_MAX;
Daniel Stone6847b852017-03-01 11:34:08 +00002740
2741 weston_compositor_read_presentation_clock(compositor, &now);
2742
2743 wl_list_for_each(output, &compositor->output_list, link) {
2744 int64_t msec_to_this;
2745
2746 if (output->repaint_status != REPAINT_SCHEDULED)
2747 continue;
2748
2749 msec_to_this = timespec_sub_to_msec(&output->next_repaint,
2750 &now);
2751 if (!any_should_repaint || msec_to_this < msec_to_next)
2752 msec_to_next = msec_to_this;
2753
2754 any_should_repaint = true;
2755 }
2756
2757 if (!any_should_repaint)
2758 return;
2759
2760 /* Even if we should repaint immediately, add the minimum 1 ms delay.
2761 * This is a workaround to allow coalescing multiple output repaints
2762 * particularly from weston_output_finish_frame()
2763 * into the same call, which would not happen if we called
2764 * output_repaint_timer_handler() directly.
2765 */
2766 if (msec_to_next < 1)
2767 msec_to_next = 1;
2768
2769 wl_event_source_timer_update(compositor->repaint_timer, msec_to_next);
2770}
2771
2772static int
2773output_repaint_timer_handler(void *data)
2774{
2775 struct weston_compositor *compositor = data;
2776 struct weston_output *output;
2777 struct timespec now;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002778 void *repaint_data = NULL;
Emre Ucane479ed82018-03-20 15:29:40 +01002779 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002780
2781 weston_compositor_read_presentation_clock(compositor, &now);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002782
2783 if (compositor->backend->repaint_begin)
2784 repaint_data = compositor->backend->repaint_begin(compositor);
2785
2786 wl_list_for_each(output, &compositor->output_list, link) {
2787 ret = weston_output_maybe_repaint(output, &now, repaint_data);
2788 if (ret)
2789 break;
2790 }
2791
2792 if (ret == 0) {
Tomohito Esaki09bfcd62018-06-05 10:37:05 +09002793 if (compositor->backend->repaint_flush)
Antonio Borneoc90fccc2019-06-30 15:51:10 +02002794 ret = compositor->backend->repaint_flush(compositor,
2795 repaint_data);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002796 } else {
Antonio Borneoc90fccc2019-06-30 15:51:10 +02002797 if (compositor->backend->repaint_cancel)
2798 compositor->backend->repaint_cancel(compositor,
2799 repaint_data);
2800 }
2801
2802 if (ret != 0) {
Tomohito Esaki7f4d9ff2018-06-05 10:37:06 +09002803 wl_list_for_each(output, &compositor->output_list, link) {
2804 if (output->repainted)
2805 weston_output_schedule_repaint_reset(output);
2806 }
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002807 }
Daniel Stone6847b852017-03-01 11:34:08 +00002808
Tomohito Esakiddaf95c2018-07-10 11:47:15 +09002809 wl_list_for_each(output, &compositor->output_list, link)
2810 output->repainted = false;
2811
Daniel Stone6847b852017-03-01 11:34:08 +00002812 output_repaint_timer_arm(compositor);
2813
Pekka Paalanen0513a952014-05-21 16:17:27 +03002814 return 0;
2815}
2816
Marius Vlad55d87362019-06-11 01:15:35 +03002817/**
2818 * \ingroup output
2819 */
Kristian Høgsbergef044142011-06-21 15:02:12 -04002820WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002821weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002822 const struct timespec *stamp,
2823 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002824{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002825 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002826 int32_t refresh_nsec;
2827 struct timespec now;
Daniel Stone6847b852017-03-01 11:34:08 +00002828 int64_t msec_rel;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002829
Pekka Paalanenb5026542014-11-12 15:09:24 +02002830
Daniel Stone05df8c12017-03-03 16:59:42 +00002831 assert(output->repaint_status == REPAINT_AWAITING_COMPLETION);
Daniel Stone3615ce12017-03-01 11:34:05 +00002832 assert(stamp || (presented_flags & WP_PRESENTATION_FEEDBACK_INVALID));
2833
Daniel Stone6847b852017-03-01 11:34:08 +00002834 weston_compositor_read_presentation_clock(compositor, &now);
2835
Daniel Stone3615ce12017-03-01 11:34:05 +00002836 /* If we haven't been supplied any timestamp at all, we don't have a
2837 * timebase to work against, so any delay just wastes time. Push a
2838 * repaint as soon as possible so we can get on with it. */
Daniel Stone6847b852017-03-01 11:34:08 +00002839 if (!stamp) {
2840 output->next_repaint = now;
Daniel Stone3615ce12017-03-01 11:34:05 +00002841 goto out;
Daniel Stone6847b852017-03-01 11:34:08 +00002842 }
Daniel Stone3615ce12017-03-01 11:34:05 +00002843
Marius Vladdf9278a2018-03-06 18:56:23 +02002844 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2845 TLP_VBLANK(stamp), TLP_END);
2846
Pekka Paalanend7894d02015-07-03 15:08:53 +03002847 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002848 weston_presentation_feedback_present_list(&output->feedback_list,
2849 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002850 output->msc,
2851 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002852
Alexandros Frantzise6ac2af2017-11-16 18:20:53 +02002853 output->frame_time = *stamp;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002854
Daniel Stone6847b852017-03-01 11:34:08 +00002855 timespec_add_nsec(&output->next_repaint, stamp, refresh_nsec);
2856 timespec_add_msec(&output->next_repaint, &output->next_repaint,
2857 -compositor->repaint_msec);
2858 msec_rel = timespec_sub_to_msec(&output->next_repaint, &now);
Daniel Stone84aff5c2017-03-01 11:34:04 +00002859
2860 if (msec_rel < -1000 || msec_rel > 1000) {
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002861 static bool warned;
2862
2863 if (!warned)
2864 weston_log("Warning: computed repaint delay is "
Daniel Stone6847b852017-03-01 11:34:08 +00002865 "insane: %lld msec\n", (long long) msec_rel);
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002866 warned = true;
2867
Daniel Stone6847b852017-03-01 11:34:08 +00002868 output->next_repaint = now;
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002869 }
2870
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002871 /* Called from restart_repaint_loop and restart happens already after
2872 * the deadline given by repaint_msec? In that case we delay until
2873 * the deadline of the next frame, to give clients a more predictable
2874 * timing of the repaint cycle to lock on. */
Daniel Stoneeca5cca2017-02-28 21:53:51 +00002875 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID &&
2876 msec_rel < 0) {
2877 while (timespec_sub_to_nsec(&output->next_repaint, &now) < 0) {
2878 timespec_add_nsec(&output->next_repaint,
2879 &output->next_repaint,
2880 refresh_nsec);
2881 }
2882 }
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002883
Daniel Stone3615ce12017-03-01 11:34:05 +00002884out:
Daniel Stone05df8c12017-03-03 16:59:42 +00002885 output->repaint_status = REPAINT_SCHEDULED;
Daniel Stone6847b852017-03-01 11:34:08 +00002886 output_repaint_timer_arm(compositor);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002887}
2888
2889static void
2890idle_repaint(void *data)
2891{
2892 struct weston_output *output = data;
Antonio Borneoc90fccc2019-06-30 15:51:10 +02002893 int ret;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002894
Daniel Stone05df8c12017-03-03 16:59:42 +00002895 assert(output->repaint_status == REPAINT_BEGIN_FROM_IDLE);
2896 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03002897 output->idle_repaint_source = NULL;
Antonio Borneoc90fccc2019-06-30 15:51:10 +02002898 ret = output->start_repaint_loop(output);
2899 if (ret != 0)
2900 weston_output_schedule_repaint_reset(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002901}
2902
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002903WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002904weston_layer_entry_insert(struct weston_layer_entry *list,
2905 struct weston_layer_entry *entry)
2906{
2907 wl_list_insert(&list->link, &entry->link);
2908 entry->layer = list->layer;
2909}
2910
2911WL_EXPORT void
2912weston_layer_entry_remove(struct weston_layer_entry *entry)
2913{
2914 wl_list_remove(&entry->link);
2915 wl_list_init(&entry->link);
2916 entry->layer = NULL;
2917}
2918
Quentin Glidic82681572016-12-17 13:40:51 +01002919
2920/** Initialize the weston_layer struct.
2921 *
2922 * \param compositor The compositor instance
2923 * \param layer The layer to initialize
2924 */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002925WL_EXPORT void
Quentin Glidic82681572016-12-17 13:40:51 +01002926weston_layer_init(struct weston_layer *layer,
2927 struct weston_compositor *compositor)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002928{
Quentin Glidic82681572016-12-17 13:40:51 +01002929 layer->compositor = compositor;
2930 wl_list_init(&layer->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002931 wl_list_init(&layer->view_list.link);
2932 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002933 weston_layer_set_mask_infinite(layer);
Quentin Glidic82681572016-12-17 13:40:51 +01002934}
2935
2936/** Sets the position of the layer in the layer list. The layer will be placed
2937 * below any layer with the same position value, if any.
2938 * This function is safe to call if the layer is already on the list, but the
2939 * layer may be moved below other layers at the same position, if any.
2940 *
2941 * \param layer The layer to modify
2942 * \param position The position the layer will be placed at
2943 */
2944WL_EXPORT void
2945weston_layer_set_position(struct weston_layer *layer,
2946 enum weston_layer_position position)
2947{
2948 struct weston_layer *below;
2949
2950 wl_list_remove(&layer->link);
2951
2952 /* layer_list is ordered from top to bottom, the last layer being the
2953 * background with the smallest position value */
2954
2955 layer->position = position;
2956 wl_list_for_each_reverse(below, &layer->compositor->layer_list, link) {
2957 if (below->position >= layer->position) {
2958 wl_list_insert(&below->link, &layer->link);
2959 return;
2960 }
2961 }
2962 wl_list_insert(&layer->compositor->layer_list, &layer->link);
2963}
2964
2965/** Hide a layer by taking it off the layer list.
2966 * This function is safe to call if the layer is not on the list.
2967 *
2968 * \param layer The layer to hide
2969 */
2970WL_EXPORT void
2971weston_layer_unset_position(struct weston_layer *layer)
2972{
2973 wl_list_remove(&layer->link);
2974 wl_list_init(&layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002975}
2976
2977WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002978weston_layer_set_mask(struct weston_layer *layer,
2979 int x, int y, int width, int height)
2980{
2981 struct weston_view *view;
2982
2983 layer->mask.x1 = x;
2984 layer->mask.x2 = x + width;
2985 layer->mask.y1 = y;
2986 layer->mask.y2 = y + height;
2987
2988 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2989 weston_view_geometry_dirty(view);
2990 }
2991}
2992
2993WL_EXPORT void
2994weston_layer_set_mask_infinite(struct weston_layer *layer)
2995{
2996 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2997 UINT32_MAX, UINT32_MAX);
2998}
2999
Daniel Stone3b775632018-07-20 08:38:25 +01003000WL_EXPORT bool
3001weston_layer_mask_is_infinite(struct weston_layer *layer)
3002{
3003 return layer->mask.x1 == INT32_MIN &&
3004 layer->mask.y1 == INT32_MIN &&
3005 layer->mask.x2 == INT32_MIN + UINT32_MAX &&
3006 layer->mask.y2 == INT32_MIN + UINT32_MAX;
3007}
3008
Marius Vlad55d87362019-06-11 01:15:35 +03003009/**
3010 * \ingroup output
3011 */
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03003012WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003013weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003014{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003015 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04003016 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01003017
Bryce Harrington08976ac2016-08-30 12:05:16 -07003018 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
3019 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003020 return;
3021
Pekka Paalanenb5026542014-11-12 15:09:24 +02003022 if (!output->repaint_needed)
3023 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
3024
Kristian Høgsbergef044142011-06-21 15:02:12 -04003025 loop = wl_display_get_event_loop(compositor->wl_display);
Daniel Stone09a97e22017-03-01 11:34:06 +00003026 output->repaint_needed = true;
Daniel Stone05df8c12017-03-03 16:59:42 +00003027
3028 /* If we already have a repaint scheduled for our idle handler,
3029 * no need to set it again. If the repaint has been called but
3030 * not finished, then weston_output_finish_frame() will notice
3031 * that a repaint is needed and schedule one. */
3032 if (output->repaint_status != REPAINT_NOT_SCHEDULED)
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003033 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01003034
Daniel Stone05df8c12017-03-03 16:59:42 +00003035 output->repaint_status = REPAINT_BEGIN_FROM_IDLE;
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03003036 assert(!output->idle_repaint_source);
3037 output->idle_repaint_source = wl_event_loop_add_idle(loop, idle_repaint,
3038 output);
Pekka Paalanenb5026542014-11-12 15:09:24 +02003039 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003040}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05003041
Marius Vlad9fdda7f2019-06-11 16:08:55 +03003042/** weston_compositor_schedule_repaint
3043 * \ingroup compositor
3044 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003045WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04003046weston_compositor_schedule_repaint(struct weston_compositor *compositor)
3047{
3048 struct weston_output *output;
3049
3050 wl_list_for_each(output, &compositor->output_list, link)
3051 weston_output_schedule_repaint(output);
3052}
3053
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003054static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003055surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003056{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003057 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003058}
3059
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003060static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003061surface_attach(struct wl_client *client,
3062 struct wl_resource *resource,
3063 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
3064{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003065 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05003066 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003067
Kristian Høgsbergab19f932013-08-20 11:30:54 -07003068 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05003069 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07003070 if (buffer == NULL) {
3071 wl_client_post_no_memory(client);
3072 return;
3073 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07003074 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04003075
Pekka Paalanende685b82012-12-04 15:58:12 +02003076 /* Attach, attach, without commit in between does not send
3077 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003078 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03003079
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003080 surface->pending.sx = sx;
3081 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01003082 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003083}
3084
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05003085static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003086surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003087 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003088 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05003089{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003090 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04003091
Derek Foreman57e92ed2015-11-17 14:11:35 -06003092 if (width <= 0 || height <= 0)
3093 return;
3094
Derek Foreman152254b2015-11-26 14:17:48 -06003095 pixman_region32_union_rect(&surface->pending.damage_surface,
3096 &surface->pending.damage_surface,
3097 x, y, width, height);
3098}
3099
3100static void
3101surface_damage_buffer(struct wl_client *client,
3102 struct wl_resource *resource,
3103 int32_t x, int32_t y, int32_t width, int32_t height)
3104{
3105 struct weston_surface *surface = wl_resource_get_user_data(resource);
3106
3107 if (width <= 0 || height <= 0)
3108 return;
3109
3110 pixman_region32_union_rect(&surface->pending.damage_buffer,
3111 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04003112 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003113}
3114
Kristian Høgsberg33418202011-08-16 23:01:28 -04003115static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003116destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04003117{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05003118 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003119
3120 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02003121 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003122}
3123
3124static void
3125surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003126 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04003127{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003128 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003129 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003130
3131 cb = malloc(sizeof *cb);
3132 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003133 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003134 return;
3135 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03003136
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003137 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
3138 callback);
3139 if (cb->resource == NULL) {
3140 free(cb);
3141 wl_resource_post_no_memory(resource);
3142 return;
3143 }
3144
Jason Ekstranda85118c2013-06-27 20:17:02 -05003145 wl_resource_set_implementation(cb->resource, NULL, cb,
3146 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003147
Pekka Paalanenbc106382012-10-10 12:49:31 +03003148 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04003149}
3150
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003151static void
3152surface_set_opaque_region(struct wl_client *client,
3153 struct wl_resource *resource,
3154 struct wl_resource *region_resource)
3155{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003156 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003157 struct weston_region *region;
3158
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003159 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003160 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003161 pixman_region32_copy(&surface->pending.opaque,
3162 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003163 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07003164 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003165 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003166}
3167
3168static void
3169surface_set_input_region(struct wl_client *client,
3170 struct wl_resource *resource,
3171 struct wl_resource *region_resource)
3172{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003173 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003174 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003175
3176 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003177 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003178 pixman_region32_copy(&surface->pending.input,
3179 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003180 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003181 pixman_region32_fini(&surface->pending.input);
3182 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003183 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003184}
3185
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003186/* Cause damage to this sub-surface and all its children.
3187 *
3188 * This is useful when there are state changes that need an implicit
3189 * damage, e.g. a z-order change.
3190 */
3191static void
3192weston_surface_damage_subsurfaces(struct weston_subsurface *sub)
3193{
3194 struct weston_subsurface *child;
3195
3196 weston_surface_damage(sub->surface);
3197 sub->reordered = false;
3198
3199 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link)
3200 if (child != sub)
3201 weston_surface_damage_subsurfaces(child);
3202}
3203
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003204static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003205weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003206{
Pekka Paalanene67858b2013-04-25 13:57:42 +03003207 struct weston_subsurface *sub;
3208
3209 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
3210 parent_link_pending) {
3211 wl_list_remove(&sub->parent_link);
3212 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003213
3214 if (sub->reordered)
3215 weston_surface_damage_subsurfaces(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003216 }
3217}
3218
3219static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03003220weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05003221 struct weston_matrix *matrix)
3222{
Pekka Paalanen04baea52016-04-26 15:50:58 +03003223 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05003224 double src_width, src_height, dest_width, dest_height;
3225
3226 weston_matrix_init(matrix);
3227
3228 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
3229 src_width = surface->width_from_buffer;
3230 src_height = surface->height_from_buffer;
3231 } else {
3232 src_width = wl_fixed_to_double(vp->buffer.src_width);
3233 src_height = wl_fixed_to_double(vp->buffer.src_height);
3234 }
3235
3236 if (vp->surface.width == -1) {
3237 dest_width = src_width;
3238 dest_height = src_height;
3239 } else {
3240 dest_width = vp->surface.width;
3241 dest_height = vp->surface.height;
3242 }
3243
3244 if (src_width != dest_width || src_height != dest_height)
3245 weston_matrix_scale(matrix,
3246 src_width / dest_width,
3247 src_height / dest_height, 1);
3248
3249 if (vp->buffer.src_width != wl_fixed_from_int(-1))
3250 weston_matrix_translate(matrix,
3251 wl_fixed_to_double(vp->buffer.src_x),
3252 wl_fixed_to_double(vp->buffer.src_y),
3253 0);
3254
3255 switch (vp->buffer.transform) {
3256 case WL_OUTPUT_TRANSFORM_FLIPPED:
3257 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3258 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3259 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3260 weston_matrix_scale(matrix, -1, 1, 1);
3261 weston_matrix_translate(matrix,
3262 surface->width_from_buffer, 0, 0);
3263 break;
3264 }
3265
3266 switch (vp->buffer.transform) {
3267 default:
3268 case WL_OUTPUT_TRANSFORM_NORMAL:
3269 case WL_OUTPUT_TRANSFORM_FLIPPED:
3270 break;
3271 case WL_OUTPUT_TRANSFORM_90:
3272 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3273 weston_matrix_rotate_xy(matrix, 0, 1);
3274 weston_matrix_translate(matrix,
3275 surface->height_from_buffer, 0, 0);
3276 break;
3277 case WL_OUTPUT_TRANSFORM_180:
3278 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3279 weston_matrix_rotate_xy(matrix, -1, 0);
3280 weston_matrix_translate(matrix,
3281 surface->width_from_buffer,
3282 surface->height_from_buffer, 0);
3283 break;
3284 case WL_OUTPUT_TRANSFORM_270:
3285 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3286 weston_matrix_rotate_xy(matrix, 0, -1);
3287 weston_matrix_translate(matrix,
3288 0, surface->width_from_buffer, 0);
3289 break;
3290 }
3291
3292 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
3293}
3294
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003295/**
3296 * Compute a + b > c while being safe to overflows.
3297 */
3298static bool
3299fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
3300{
3301 return (int64_t)a + (int64_t)b > (int64_t)c;
3302}
3303
3304static bool
3305weston_surface_is_pending_viewport_source_valid(
3306 const struct weston_surface *surface)
3307{
3308 const struct weston_surface_state *pend = &surface->pending;
3309 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
3310 int width_from_buffer = 0;
3311 int height_from_buffer = 0;
3312 wl_fixed_t w;
3313 wl_fixed_t h;
3314
3315 /* If viewport source rect is not set, it is always ok. */
3316 if (vp->buffer.src_width == wl_fixed_from_int(-1))
3317 return true;
3318
3319 if (pend->newly_attached) {
3320 if (pend->buffer) {
3321 convert_size_by_transform_scale(&width_from_buffer,
3322 &height_from_buffer,
3323 pend->buffer->width,
3324 pend->buffer->height,
3325 vp->buffer.transform,
3326 vp->buffer.scale);
3327 } else {
3328 /* No buffer: viewport is irrelevant. */
3329 return true;
3330 }
3331 } else {
3332 width_from_buffer = surface->width_from_buffer;
3333 height_from_buffer = surface->height_from_buffer;
3334 }
3335
3336 assert((width_from_buffer == 0) == (height_from_buffer == 0));
3337 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
3338
3339 /* No buffer: viewport is irrelevant. */
3340 if (width_from_buffer == 0 || height_from_buffer == 0)
3341 return true;
3342
3343 /* overflow checks for wl_fixed_from_int() */
3344 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
3345 return false;
3346 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
3347 return false;
3348
3349 w = wl_fixed_from_int(width_from_buffer);
3350 h = wl_fixed_from_int(height_from_buffer);
3351
3352 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
3353 return false;
3354 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
3355 return false;
3356
3357 return true;
3358}
3359
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003360static bool
3361fixed_is_integer(wl_fixed_t v)
3362{
3363 return (v & 0xff) == 0;
3364}
3365
3366static bool
3367weston_surface_is_pending_viewport_dst_size_int(
3368 const struct weston_surface *surface)
3369{
3370 const struct weston_buffer_viewport *vp =
3371 &surface->pending.buffer_viewport;
3372
3373 if (vp->surface.width != -1) {
3374 assert(vp->surface.width > 0 && vp->surface.height > 0);
3375 return true;
3376 }
3377
3378 return fixed_is_integer(vp->buffer.src_width) &&
3379 fixed_is_integer(vp->buffer.src_height);
3380}
3381
Derek Foreman152254b2015-11-26 14:17:48 -06003382/* Translate pending damage in buffer co-ordinates to surface
3383 * co-ordinates and union it with a pixman_region32_t.
3384 * This should only be called after the buffer is attached.
3385 */
3386static void
3387apply_damage_buffer(pixman_region32_t *dest,
3388 struct weston_surface *surface,
3389 struct weston_surface_state *state)
3390{
3391 struct weston_buffer *buffer = surface->buffer_ref.buffer;
3392
3393 /* wl_surface.damage_buffer needs to be clipped to the buffer,
3394 * translated into surface co-ordinates and unioned with
3395 * any other surface damage.
3396 * None of this makes sense if there is no buffer though.
3397 */
3398 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
3399 pixman_region32_t buffer_damage;
3400
3401 pixman_region32_intersect_rect(&state->damage_buffer,
3402 &state->damage_buffer,
3403 0, 0, buffer->width,
3404 buffer->height);
3405 pixman_region32_init(&buffer_damage);
3406 weston_matrix_transform_region(&buffer_damage,
3407 &surface->buffer_to_surface_matrix,
3408 &state->damage_buffer);
3409 pixman_region32_union(dest, dest, &buffer_damage);
3410 pixman_region32_fini(&buffer_damage);
3411 }
3412 /* We should clear this on commit even if there was no buffer */
3413 pixman_region32_clear(&state->damage_buffer);
3414}
3415
Jason Ekstrand1e059042014-10-16 10:55:19 -05003416static void
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303417weston_surface_set_desired_protection(struct weston_surface *surface,
3418 enum weston_hdcp_protection protection)
3419{
3420 if (surface->desired_protection == protection)
3421 return;
3422 surface->desired_protection = protection;
3423 weston_surface_damage(surface);
3424}
3425
3426static void
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303427weston_surface_set_protection_mode(struct weston_surface *surface,
3428 enum weston_surface_protection_mode p_mode)
3429{
3430 struct content_protection *cp = surface->compositor->content_protection;
3431 struct protected_surface *psurface;
3432
3433 surface->protection_mode = p_mode;
3434 wl_list_for_each(psurface, &cp->protected_list, link) {
3435 if (!psurface || psurface->surface != surface)
3436 continue;
3437 weston_protected_surface_send_event(psurface,
3438 surface->current_protection);
3439 }
3440}
3441
3442static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05003443weston_surface_commit_state(struct weston_surface *surface,
3444 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003445{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003446 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003447 pixman_region32_t opaque;
3448
Alexander Larsson4ea95522013-05-22 14:41:37 +02003449 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02003450 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03003451 /* wp_viewport.set_source */
3452 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003453 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003454
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003455 /* wl_surface.attach */
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003456 if (state->newly_attached) {
3457 /* zwp_surface_synchronization_v1.set_acquire_fence */
3458 fd_move(&surface->acquire_fence_fd,
3459 &state->acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003460 /* zwp_surface_synchronization_v1.get_release */
3461 weston_buffer_release_move(&surface->buffer_release_ref,
3462 &state->buffer_release_ref);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003463 weston_surface_attach(surface, state->buffer);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003464 }
Jason Ekstrand7b982072014-05-20 14:33:03 -05003465 weston_surface_state_set_buffer(state, NULL);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003466 assert(state->acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003467 assert(state->buffer_release_ref.buffer_release == NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01003468
Jason Ekstrand1e059042014-10-16 10:55:19 -05003469 weston_surface_build_buffer_matrix(surface,
3470 &surface->surface_to_buffer_matrix);
3471 weston_matrix_invert(&surface->buffer_to_surface_matrix,
3472 &surface->surface_to_buffer_matrix);
3473
Jason Ekstrand7b982072014-05-20 14:33:03 -05003474 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003475 weston_surface_update_size(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003476 if (surface->committed)
3477 surface->committed(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003478 }
Giulio Camuffo184df502013-02-21 11:29:21 +01003479
Jason Ekstrand7b982072014-05-20 14:33:03 -05003480 state->sx = 0;
3481 state->sy = 0;
3482 state->newly_attached = 0;
3483 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03003484
Derek Foreman152254b2015-11-26 14:17:48 -06003485 /* wl_surface.damage and wl_surface.damage_buffer */
Pekka Paalanenb5026542014-11-12 15:09:24 +02003486 if (weston_timeline_enabled_ &&
Derek Foreman152254b2015-11-26 14:17:48 -06003487 (pixman_region32_not_empty(&state->damage_surface) ||
3488 pixman_region32_not_empty(&state->damage_buffer)))
Pekka Paalanenb5026542014-11-12 15:09:24 +02003489 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06003490
Pekka Paalanen8e159182012-10-10 12:49:25 +03003491 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06003492 &state->damage_surface);
3493
3494 apply_damage_buffer(&surface->damage, surface, state);
3495
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05003496 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07003497 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06003498 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003499
3500 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003501 pixman_region32_init(&opaque);
3502 pixman_region32_intersect_rect(&opaque, &state->opaque,
3503 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003504
3505 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
3506 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003507 wl_list_for_each(view, &surface->views, surface_link)
3508 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003509 }
3510
3511 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003512
3513 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003514 pixman_region32_intersect_rect(&surface->input, &state->input,
3515 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003516
Pekka Paalanenbc106382012-10-10 12:49:31 +03003517 /* wl_surface.frame */
3518 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05003519 &state->frame_callback_list);
3520 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003521
3522 /* XXX:
3523 * What should happen with a feedback request, if there
3524 * is no wl_buffer attached for this commit?
3525 */
3526
3527 /* presentation.feedback */
3528 wl_list_insert_list(&surface->feedback_list,
3529 &state->feedback_list);
3530 wl_list_init(&state->feedback_list);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003531
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303532 /* weston_protected_surface.enforced/relaxed */
3533 if (surface->protection_mode != state->protection_mode)
3534 weston_surface_set_protection_mode(surface,
3535 state->protection_mode);
3536
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303537 /* weston_protected_surface.set_type */
3538 weston_surface_set_desired_protection(surface, state->desired_protection);
3539
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003540 wl_signal_emit(&surface->commit_signal, surface);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003541}
3542
3543static void
3544weston_surface_commit(struct weston_surface *surface)
3545{
3546 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003547
Pekka Paalanene67858b2013-04-25 13:57:42 +03003548 weston_surface_commit_subsurface_order(surface);
3549
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003550 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003551}
3552
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003553static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003554weston_subsurface_commit(struct weston_subsurface *sub);
3555
3556static void
3557weston_subsurface_parent_commit(struct weston_subsurface *sub,
3558 int parent_is_synchronized);
3559
3560static void
3561surface_commit(struct wl_client *client, struct wl_resource *resource)
3562{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003563 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003564 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
3565
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003566 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
3567 assert(surface->viewport_resource);
3568
3569 wl_resource_post_error(surface->viewport_resource,
3570 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
3571 "wl_surface@%d has viewport source outside buffer",
3572 wl_resource_get_id(resource));
3573 return;
3574 }
3575
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003576 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
3577 assert(surface->viewport_resource);
3578
3579 wl_resource_post_error(surface->viewport_resource,
3580 WP_VIEWPORT_ERROR_BAD_SIZE,
3581 "wl_surface@%d viewport dst size not integer",
3582 wl_resource_get_id(resource));
3583 return;
3584 }
3585
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003586 if (surface->pending.acquire_fence_fd >= 0) {
3587 assert(surface->synchronization_resource);
3588
3589 if (!surface->pending.buffer) {
3590 fd_clear(&surface->pending.acquire_fence_fd);
3591 wl_resource_post_error(surface->synchronization_resource,
3592 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3593 "wl_surface@%"PRIu32" no buffer for synchronization",
3594 wl_resource_get_id(resource));
3595 return;
3596 }
3597
3598 /* We support fences for both wp_linux_dmabuf and opaque EGL
3599 * buffers, as mandated by minor version 2 of the
3600 * zwp_linux_explicit_synchronization_v1 protocol. Since
3601 * renderers that support fences currently only support these
3602 * two buffer types plus SHM buffers, we can just check for the
3603 * SHM buffer case here.
3604 */
3605 if (wl_shm_buffer_get(surface->pending.buffer->resource)) {
3606 fd_clear(&surface->pending.acquire_fence_fd);
3607 wl_resource_post_error(surface->synchronization_resource,
3608 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_UNSUPPORTED_BUFFER,
3609 "wl_surface@%"PRIu32" unsupported buffer for synchronization",
3610 wl_resource_get_id(resource));
3611 return;
3612 }
3613 }
3614
Alexandros Frantzis67629672018-10-19 12:14:11 +03003615 if (surface->pending.buffer_release_ref.buffer_release &&
3616 !surface->pending.buffer) {
3617 assert(surface->synchronization_resource);
3618
3619 wl_resource_post_error(surface->synchronization_resource,
3620 ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER,
3621 "wl_surface@%"PRIu32" no buffer for synchronization",
3622 wl_resource_get_id(resource));
3623 return;
3624 }
3625
Pekka Paalanene67858b2013-04-25 13:57:42 +03003626 if (sub) {
3627 weston_subsurface_commit(sub);
3628 return;
3629 }
3630
3631 weston_surface_commit(surface);
3632
3633 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
3634 if (sub->surface != surface)
3635 weston_subsurface_parent_commit(sub, 0);
3636 }
3637}
3638
3639static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003640surface_set_buffer_transform(struct wl_client *client,
3641 struct wl_resource *resource, int transform)
3642{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003643 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003644
Jonny Lamba55f1392014-05-30 12:07:15 +02003645 /* if wl_output.transform grows more members this will need to be updated. */
3646 if (transform < 0 ||
3647 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
3648 wl_resource_post_error(resource,
3649 WL_SURFACE_ERROR_INVALID_TRANSFORM,
3650 "buffer transform must be a valid transform "
3651 "('%d' specified)", transform);
3652 return;
3653 }
3654
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003655 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003656 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003657}
3658
Alexander Larsson4ea95522013-05-22 14:41:37 +02003659static void
3660surface_set_buffer_scale(struct wl_client *client,
3661 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003662 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003663{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003664 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003665
Jonny Lamba55f1392014-05-30 12:07:15 +02003666 if (scale < 1) {
3667 wl_resource_post_error(resource,
3668 WL_SURFACE_ERROR_INVALID_SCALE,
3669 "buffer scale must be at least one "
3670 "('%d' specified)", scale);
3671 return;
3672 }
3673
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003674 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003675 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003676}
3677
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003678static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003679 surface_destroy,
3680 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04003681 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003682 surface_frame,
3683 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003684 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003685 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02003686 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06003687 surface_set_buffer_scale,
3688 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003689};
3690
3691static void
3692compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003693 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003694{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04003695 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003696 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003697
Kristian Høgsberg18c93002012-01-27 11:58:31 -05003698 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003699 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003700 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003701 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003702 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003703
Jason Ekstranda85118c2013-06-27 20:17:02 -05003704 surface->resource =
3705 wl_resource_create(client, &wl_surface_interface,
3706 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003707 if (surface->resource == NULL) {
3708 weston_surface_destroy(surface);
3709 wl_resource_post_no_memory(resource);
3710 return;
3711 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003712 wl_resource_set_implementation(surface->resource, &surface_interface,
3713 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003714
3715 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003716}
3717
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003718static void
3719destroy_region(struct wl_resource *resource)
3720{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003721 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003722
3723 pixman_region32_fini(&region->region);
3724 free(region);
3725}
3726
3727static void
3728region_destroy(struct wl_client *client, struct wl_resource *resource)
3729{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003730 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003731}
3732
3733static void
3734region_add(struct wl_client *client, struct wl_resource *resource,
3735 int32_t x, int32_t y, int32_t width, int32_t height)
3736{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003737 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003738
3739 pixman_region32_union_rect(&region->region, &region->region,
3740 x, y, width, height);
3741}
3742
3743static void
3744region_subtract(struct wl_client *client, struct wl_resource *resource,
3745 int32_t x, int32_t y, int32_t width, int32_t height)
3746{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003747 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003748 pixman_region32_t rect;
3749
3750 pixman_region32_init_rect(&rect, x, y, width, height);
3751 pixman_region32_subtract(&region->region, &region->region, &rect);
3752 pixman_region32_fini(&rect);
3753}
3754
3755static const struct wl_region_interface region_interface = {
3756 region_destroy,
3757 region_add,
3758 region_subtract
3759};
3760
3761static void
3762compositor_create_region(struct wl_client *client,
3763 struct wl_resource *resource, uint32_t id)
3764{
3765 struct weston_region *region;
3766
3767 region = malloc(sizeof *region);
3768 if (region == NULL) {
3769 wl_resource_post_no_memory(resource);
3770 return;
3771 }
3772
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003773 pixman_region32_init(&region->region);
3774
Jason Ekstranda85118c2013-06-27 20:17:02 -05003775 region->resource =
3776 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003777 if (region->resource == NULL) {
3778 free(region);
3779 wl_resource_post_no_memory(resource);
3780 return;
3781 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003782 wl_resource_set_implementation(region->resource, &region_interface,
3783 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003784}
3785
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003786static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003787 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003788 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003789};
3790
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003791static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003792weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3793{
3794 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003795
Jason Ekstrand7b982072014-05-20 14:33:03 -05003796 weston_surface_commit_state(surface, &sub->cached);
3797 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003798
3799 weston_surface_commit_subsurface_order(surface);
3800
3801 weston_surface_schedule_repaint(surface);
3802
Jason Ekstrand7b982072014-05-20 14:33:03 -05003803 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003804}
3805
3806static void
3807weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3808{
3809 struct weston_surface *surface = sub->surface;
3810
3811 /*
3812 * If this commit would cause the surface to move by the
3813 * attach(dx, dy) parameters, the old damage region must be
3814 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05003815 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003816 */
Derek Foreman152254b2015-11-26 14:17:48 -06003817 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003818 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06003819 pixman_region32_union(&sub->cached.damage_surface,
3820 &sub->cached.damage_surface,
3821 &surface->pending.damage_surface);
3822 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003823
3824 if (surface->pending.newly_attached) {
3825 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003826 weston_surface_state_set_buffer(&sub->cached,
3827 surface->pending.buffer);
3828 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003829 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003830 weston_presentation_feedback_discard_list(
3831 &sub->cached.feedback_list);
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003832 /* zwp_surface_synchronization_v1.set_acquire_fence */
3833 fd_move(&sub->cached.acquire_fence_fd,
3834 &surface->pending.acquire_fence_fd);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003835 /* zwp_surface_synchronization_v1.get_release */
3836 weston_buffer_release_move(&sub->cached.buffer_release_ref,
3837 &surface->pending.buffer_release_ref);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003838 }
Ankit Nautiyal4b6e73d2019-03-26 13:37:12 +05303839 sub->cached.desired_protection = surface->pending.desired_protection;
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05303840 sub->cached.protection_mode = surface->pending.protection_mode;
Alexandros Frantzisacff29b2018-10-19 12:14:11 +03003841 assert(surface->pending.acquire_fence_fd == -1);
Alexandros Frantzis67629672018-10-19 12:14:11 +03003842 assert(surface->pending.buffer_release_ref.buffer_release == NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003843 sub->cached.sx += surface->pending.sx;
3844 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003845
Derek Foreman152254b2015-11-26 14:17:48 -06003846 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
3847
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003848 sub->cached.buffer_viewport.changed |=
3849 surface->pending.buffer_viewport.changed;
3850 sub->cached.buffer_viewport.buffer =
3851 surface->pending.buffer_viewport.buffer;
3852 sub->cached.buffer_viewport.surface =
3853 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003854
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003855 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003856
3857 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3858
3859 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3860
3861 wl_list_insert_list(&sub->cached.frame_callback_list,
3862 &surface->pending.frame_callback_list);
3863 wl_list_init(&surface->pending.frame_callback_list);
3864
Pekka Paalanen133e4392014-09-23 22:08:46 -04003865 wl_list_insert_list(&sub->cached.feedback_list,
3866 &surface->pending.feedback_list);
3867 wl_list_init(&surface->pending.feedback_list);
3868
Jason Ekstrand7b982072014-05-20 14:33:03 -05003869 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003870}
3871
Derek Foreman280e7dd2014-10-03 13:13:42 -05003872static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003873weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3874{
3875 while (sub) {
3876 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003877 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003878
3879 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003880 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003881
3882 sub = weston_surface_to_subsurface(sub->parent);
3883 }
3884
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003885 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003886}
3887
3888static void
3889weston_subsurface_commit(struct weston_subsurface *sub)
3890{
3891 struct weston_surface *surface = sub->surface;
3892 struct weston_subsurface *tmp;
3893
3894 /* Recursive check for effectively synchronized. */
3895 if (weston_subsurface_is_synchronized(sub)) {
3896 weston_subsurface_commit_to_cache(sub);
3897 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003898 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003899 /* flush accumulated state from cache */
3900 weston_subsurface_commit_to_cache(sub);
3901 weston_subsurface_commit_from_cache(sub);
3902 } else {
3903 weston_surface_commit(surface);
3904 }
3905
3906 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3907 if (tmp->surface != surface)
3908 weston_subsurface_parent_commit(tmp, 0);
3909 }
3910 }
3911}
3912
3913static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003914weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003915{
3916 struct weston_surface *surface = sub->surface;
3917 struct weston_subsurface *tmp;
3918
Pekka Paalanene67858b2013-04-25 13:57:42 +03003919 /* From now on, commit_from_cache the whole sub-tree, regardless of
3920 * the synchronized mode of each child. This sub-surface or some
3921 * of its ancestors were synchronized, so we are synchronized
3922 * all the way down.
3923 */
3924
Jason Ekstrand7b982072014-05-20 14:33:03 -05003925 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003926 weston_subsurface_commit_from_cache(sub);
3927
3928 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3929 if (tmp->surface != surface)
3930 weston_subsurface_parent_commit(tmp, 1);
3931 }
3932}
3933
3934static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003935weston_subsurface_parent_commit(struct weston_subsurface *sub,
3936 int parent_is_synchronized)
3937{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003938 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003939 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003940 wl_list_for_each(view, &sub->surface->views, surface_link)
3941 weston_view_set_position(view,
3942 sub->position.x,
3943 sub->position.y);
3944
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003945 sub->position.set = 0;
3946 }
3947
3948 if (parent_is_synchronized || sub->synchronized)
3949 weston_subsurface_synchronized_commit(sub);
3950}
3951
Pekka Paalanen8274d902014-08-06 19:36:51 +03003952static int
3953subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3954{
3955 return snprintf(buf, len, "sub-surface");
3956}
3957
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003958static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003959subsurface_committed(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003960{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003961 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003962
Jason Ekstranda7af7042013-10-12 22:38:11 -05003963 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003964 weston_view_set_position(view,
3965 view->geometry.x + dx,
3966 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003967
3968 /* No need to check parent mappedness, because if parent is not
3969 * mapped, parent is not in a visible layer, so this sub-surface
3970 * will not be drawn either.
3971 */
Armin Krezovićf8486c32016-06-30 06:04:28 +02003972
Pekka Paalanene67858b2013-04-25 13:57:42 +03003973 if (!weston_surface_is_mapped(surface)) {
Armin Krezovićf8486c32016-06-30 06:04:28 +02003974 surface->is_mapped = true;
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003975
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003976 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003977 * because that would call it also for the parent surface,
3978 * which might not be mapped yet. That would lead to
3979 * inconsistent state, where the window could never be
3980 * mapped.
3981 *
Armin Krezovićf8486c32016-06-30 06:04:28 +02003982 * Instead just force the is_mapped flag on, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003983 * weston_surface_is_mapped() return true, so that when the
3984 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003985 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003986 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03003987 }
3988}
3989
3990static struct weston_subsurface *
3991weston_surface_to_subsurface(struct weston_surface *surface)
3992{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003993 if (surface->committed == subsurface_committed)
3994 return surface->committed_private;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003995
3996 return NULL;
3997}
3998
Pekka Paalanen01388e22013-04-25 13:57:44 +03003999WL_EXPORT struct weston_surface *
4000weston_surface_get_main_surface(struct weston_surface *surface)
4001{
4002 struct weston_subsurface *sub;
4003
4004 while (surface && (sub = weston_surface_to_subsurface(surface)))
4005 surface = sub->parent;
4006
4007 return surface;
4008}
4009
Pekka Paalanen50b67472014-10-01 15:02:41 +03004010WL_EXPORT int
4011weston_surface_set_role(struct weston_surface *surface,
4012 const char *role_name,
4013 struct wl_resource *error_resource,
4014 uint32_t error_code)
4015{
4016 assert(role_name);
4017
4018 if (surface->role_name == NULL ||
4019 surface->role_name == role_name ||
4020 strcmp(surface->role_name, role_name) == 0) {
4021 surface->role_name = role_name;
4022
4023 return 0;
4024 }
4025
4026 wl_resource_post_error(error_resource, error_code,
4027 "Cannot assign role %s to wl_surface@%d,"
4028 " already has role %s\n",
4029 role_name,
4030 wl_resource_get_id(surface->resource),
4031 surface->role_name);
4032 return -1;
4033}
4034
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02004035WL_EXPORT const char *
4036weston_surface_get_role(struct weston_surface *surface)
4037{
4038 return surface->role_name;
4039}
4040
Pekka Paalanen8274d902014-08-06 19:36:51 +03004041WL_EXPORT void
4042weston_surface_set_label_func(struct weston_surface *surface,
4043 int (*desc)(struct weston_surface *,
4044 char *, size_t))
4045{
4046 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02004047 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004048}
4049
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004050/** Get the size of surface contents
4051 *
4052 * \param surface The surface to query.
4053 * \param width Returns the width of raw contents.
4054 * \param height Returns the height of raw contents.
4055 *
4056 * Retrieves the raw surface content size in pixels for the given surface.
4057 * This is the whole content size in buffer pixels. If the surface
4058 * has no content or the renderer does not implement this feature,
4059 * zeroes are returned.
4060 *
4061 * This function is used to determine the buffer size needed for
4062 * a weston_surface_copy_content() call.
4063 */
4064WL_EXPORT void
4065weston_surface_get_content_size(struct weston_surface *surface,
4066 int *width, int *height)
4067{
4068 struct weston_renderer *rer = surface->compositor->renderer;
4069
4070 if (!rer->surface_get_content_size) {
4071 *width = 0;
4072 *height = 0;
4073 return;
4074 }
4075
4076 rer->surface_get_content_size(surface, width, height);
4077}
4078
Quentin Glidic248dd102016-08-12 10:41:34 +02004079/** Get the bounding box of a surface and its subsurfaces
4080 *
4081 * \param surface The surface to query.
4082 * \return The bounding box relative to the surface origin.
4083 *
4084 */
4085WL_EXPORT struct weston_geometry
4086weston_surface_get_bounding_box(struct weston_surface *surface)
4087{
4088 pixman_region32_t region;
4089 pixman_box32_t *box;
4090 struct weston_subsurface *subsurface;
4091
4092 pixman_region32_init_rect(&region,
4093 0, 0,
4094 surface->width, surface->height);
4095
4096 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link)
4097 pixman_region32_union_rect(&region, &region,
4098 subsurface->position.x,
4099 subsurface->position.y,
4100 subsurface->surface->width,
4101 subsurface->surface->height);
4102
4103 box = pixman_region32_extents(&region);
4104 struct weston_geometry geometry = {
4105 .x = box->x1,
4106 .y = box->y1,
4107 .width = box->x2 - box->x1,
4108 .height = box->y2 - box->y1,
4109 };
4110
4111 pixman_region32_fini(&region);
4112
4113 return geometry;
4114}
4115
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004116/** Copy surface contents to system memory.
4117 *
4118 * \param surface The surface to copy from.
4119 * \param target Pointer to the target memory buffer.
4120 * \param size Size of the target buffer in bytes.
4121 * \param src_x X location on contents to copy from.
4122 * \param src_y Y location on contents to copy from.
4123 * \param width Width in pixels of the area to copy.
4124 * \param height Height in pixels of the area to copy.
4125 * \return 0 for success, -1 for failure.
4126 *
4127 * Surface contents are maintained by the renderer. They can be in a
4128 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
4129 * else.
4130 *
4131 * Surface contents are copied into memory pointed to by target,
4132 * which has size bytes of space available. The target memory
4133 * may be larger than needed, but being smaller returns an error.
4134 * The extra bytes in target may or may not be written; their content is
4135 * unspecified. Size must be large enough to hold the image.
4136 *
4137 * The image in the target memory will be arranged in rows from
4138 * top to bottom, and pixels on a row from left to right. The pixel
4139 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
4140 * width * 4.
4141 *
4142 * Parameters src_x and src_y define the upper-left corner in buffer
4143 * coordinates (pixels) to copy from. Parameters width and height
4144 * define the size of the area to copy in pixels.
4145 *
4146 * The rectangle defined by src_x, src_y, width, height must fit in
4147 * the surface contents. Otherwise an error is returned.
4148 *
Changwoo Chof97d2502017-08-05 00:30:47 +09004149 * Use weston_surface_get_content_size to determine the content size; the
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004150 * needed target buffer size and rectangle limits.
4151 *
4152 * CURRENT IMPLEMENTATION RESTRICTIONS:
4153 * - the machine must be little-endian due to Pixman formats.
4154 *
4155 * NOTE: Pixman formats are premultiplied.
4156 */
4157WL_EXPORT int
4158weston_surface_copy_content(struct weston_surface *surface,
4159 void *target, size_t size,
4160 int src_x, int src_y,
4161 int width, int height)
4162{
4163 struct weston_renderer *rer = surface->compositor->renderer;
4164 int cw, ch;
4165 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
4166
4167 if (!rer->surface_copy_content)
4168 return -1;
4169
4170 weston_surface_get_content_size(surface, &cw, &ch);
4171
4172 if (src_x < 0 || src_y < 0)
4173 return -1;
4174
4175 if (width <= 0 || height <= 0)
4176 return -1;
4177
4178 if (src_x + width > cw || src_y + height > ch)
4179 return -1;
4180
4181 if (width * bytespp * height > size)
4182 return -1;
4183
4184 return rer->surface_copy_content(surface, target, size,
4185 src_x, src_y, width, height);
4186}
4187
Pekka Paalanene67858b2013-04-25 13:57:42 +03004188static void
4189subsurface_set_position(struct wl_client *client,
4190 struct wl_resource *resource, int32_t x, int32_t y)
4191{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004192 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004193
4194 if (!sub)
4195 return;
4196
4197 sub->position.x = x;
4198 sub->position.y = y;
4199 sub->position.set = 1;
4200}
4201
4202static struct weston_subsurface *
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004203subsurface_find_sibling(struct weston_subsurface *sub,
4204 struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004205{
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004206 struct weston_surface *parent = sub->parent;
4207 struct weston_subsurface *sibling;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004208
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004209 wl_list_for_each(sibling, &parent->subsurface_list, parent_link) {
4210 if (sibling->surface == surface && sibling != sub)
4211 return sibling;
4212 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004213
4214 return NULL;
4215}
4216
4217static struct weston_subsurface *
4218subsurface_sibling_check(struct weston_subsurface *sub,
4219 struct weston_surface *surface,
4220 const char *request)
4221{
4222 struct weston_subsurface *sibling;
4223
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004224 sibling = subsurface_find_sibling(sub, surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004225 if (!sibling) {
4226 wl_resource_post_error(sub->resource,
4227 WL_SUBSURFACE_ERROR_BAD_SURFACE,
4228 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004229 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004230 return NULL;
4231 }
4232
Arnaud Vracb8c16c92016-06-08 18:37:57 +02004233 assert(sibling->parent == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004234
4235 return sibling;
4236}
4237
4238static void
4239subsurface_place_above(struct wl_client *client,
4240 struct wl_resource *resource,
4241 struct wl_resource *sibling_resource)
4242{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004243 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004244 struct weston_surface *surface =
4245 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004246 struct weston_subsurface *sibling;
4247
4248 if (!sub)
4249 return;
4250
4251 sibling = subsurface_sibling_check(sub, surface, "place_above");
4252 if (!sibling)
4253 return;
4254
4255 wl_list_remove(&sub->parent_link_pending);
4256 wl_list_insert(sibling->parent_link_pending.prev,
4257 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004258
4259 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004260}
4261
4262static void
4263subsurface_place_below(struct wl_client *client,
4264 struct wl_resource *resource,
4265 struct wl_resource *sibling_resource)
4266{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004267 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004268 struct weston_surface *surface =
4269 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004270 struct weston_subsurface *sibling;
4271
4272 if (!sub)
4273 return;
4274
4275 sibling = subsurface_sibling_check(sub, surface, "place_below");
4276 if (!sibling)
4277 return;
4278
4279 wl_list_remove(&sub->parent_link_pending);
4280 wl_list_insert(&sibling->parent_link_pending,
4281 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01004282
4283 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004284}
4285
4286static void
4287subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
4288{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004289 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004290
4291 if (sub)
4292 sub->synchronized = 1;
4293}
4294
4295static void
4296subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
4297{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004298 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004299
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004300 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03004301 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03004302
4303 /* If sub became effectively desynchronized, flush. */
4304 if (!weston_subsurface_is_synchronized(sub))
4305 weston_subsurface_synchronized_commit(sub);
4306 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03004307}
4308
4309static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03004310weston_subsurface_unlink_parent(struct weston_subsurface *sub)
4311{
4312 wl_list_remove(&sub->parent_link);
4313 wl_list_remove(&sub->parent_link_pending);
4314 wl_list_remove(&sub->parent_destroy_listener.link);
4315 sub->parent = NULL;
4316}
4317
4318static void
4319weston_subsurface_destroy(struct weston_subsurface *sub);
4320
4321static void
4322subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
4323{
4324 struct weston_subsurface *sub =
4325 container_of(listener, struct weston_subsurface,
4326 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004327 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004328
4329 /* The protocol object (wl_resource) is left inert. */
4330 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004331 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004332
4333 weston_subsurface_destroy(sub);
4334}
4335
4336static void
4337subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
4338{
4339 struct weston_subsurface *sub =
4340 container_of(listener, struct weston_subsurface,
4341 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03004342 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004343 assert(sub->surface != sub->parent);
4344
4345 if (weston_surface_is_mapped(sub->surface))
4346 weston_surface_unmap(sub->surface);
4347
4348 weston_subsurface_unlink_parent(sub);
4349}
4350
4351static void
4352subsurface_resource_destroy(struct wl_resource *resource)
4353{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004354 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004355
4356 if (sub)
4357 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004358}
4359
4360static void
4361subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
4362{
4363 wl_resource_destroy(resource);
4364}
4365
4366static void
4367weston_subsurface_link_parent(struct weston_subsurface *sub,
4368 struct weston_surface *parent)
4369{
4370 sub->parent = parent;
4371 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004372 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004373 &sub->parent_destroy_listener);
4374
4375 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4376 wl_list_insert(&parent->subsurface_list_pending,
4377 &sub->parent_link_pending);
4378}
4379
4380static void
4381weston_subsurface_link_surface(struct weston_subsurface *sub,
4382 struct weston_surface *surface)
4383{
4384 sub->surface = surface;
4385 sub->surface_destroy_listener.notify =
4386 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004387 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03004388 &sub->surface_destroy_listener);
4389}
4390
4391static void
4392weston_subsurface_destroy(struct weston_subsurface *sub)
4393{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004394 struct weston_view *view, *next;
4395
Pekka Paalanene67858b2013-04-25 13:57:42 +03004396 assert(sub->surface);
4397
4398 if (sub->resource) {
4399 assert(weston_surface_to_subsurface(sub->surface) == sub);
4400 assert(sub->parent_destroy_listener.notify ==
4401 subsurface_handle_parent_destroy);
4402
George Kiagiadakised04d382014-06-13 18:10:26 +02004403 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
4404 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004405 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02004406 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05004407
Pekka Paalanene67858b2013-04-25 13:57:42 +03004408 if (sub->parent)
4409 weston_subsurface_unlink_parent(sub);
4410
Jason Ekstrand7b982072014-05-20 14:33:03 -05004411 weston_surface_state_fini(&sub->cached);
4412 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004413
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004414 sub->surface->committed = NULL;
4415 sub->surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004416 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004417 } else {
4418 /* the dummy weston_subsurface for the parent itself */
4419 assert(sub->parent_destroy_listener.notify == NULL);
4420 wl_list_remove(&sub->parent_link);
4421 wl_list_remove(&sub->parent_link_pending);
4422 }
4423
4424 wl_list_remove(&sub->surface_destroy_listener.link);
4425 free(sub);
4426}
4427
4428static const struct wl_subsurface_interface subsurface_implementation = {
4429 subsurface_destroy,
4430 subsurface_set_position,
4431 subsurface_place_above,
4432 subsurface_place_below,
4433 subsurface_set_sync,
4434 subsurface_set_desync
4435};
4436
4437static struct weston_subsurface *
4438weston_subsurface_create(uint32_t id, struct weston_surface *surface,
4439 struct weston_surface *parent)
4440{
4441 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004442 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004443
Bryce Harringtonde16d892014-11-20 22:21:57 -08004444 sub = zalloc(sizeof *sub);
4445 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004446 return NULL;
4447
Jason Ekstranda7af7042013-10-12 22:38:11 -05004448 wl_list_init(&sub->unused_views);
4449
Jason Ekstranda85118c2013-06-27 20:17:02 -05004450 sub->resource =
4451 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004452 if (!sub->resource) {
4453 free(sub);
4454 return NULL;
4455 }
4456
Jason Ekstranda85118c2013-06-27 20:17:02 -05004457 wl_resource_set_implementation(sub->resource,
4458 &subsurface_implementation,
4459 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004460 weston_subsurface_link_surface(sub, surface);
4461 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05004462 weston_surface_state_init(&sub->cached);
4463 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004464 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004465
4466 return sub;
4467}
4468
4469/* Create a dummy subsurface for having the parent itself in its
4470 * sub-surface lists. Makes stacking order manipulation easy.
4471 */
4472static struct weston_subsurface *
4473weston_subsurface_create_for_parent(struct weston_surface *parent)
4474{
4475 struct weston_subsurface *sub;
4476
Bryce Harringtonde16d892014-11-20 22:21:57 -08004477 sub = zalloc(sizeof *sub);
4478 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004479 return NULL;
4480
4481 weston_subsurface_link_surface(sub, parent);
4482 sub->parent = parent;
4483 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4484 wl_list_insert(&parent->subsurface_list_pending,
4485 &sub->parent_link_pending);
4486
4487 return sub;
4488}
4489
4490static void
4491subcompositor_get_subsurface(struct wl_client *client,
4492 struct wl_resource *resource,
4493 uint32_t id,
4494 struct wl_resource *surface_resource,
4495 struct wl_resource *parent_resource)
4496{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004497 struct weston_surface *surface =
4498 wl_resource_get_user_data(surface_resource);
4499 struct weston_surface *parent =
4500 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004501 struct weston_subsurface *sub;
4502 static const char where[] = "get_subsurface: wl_subsurface@";
4503
4504 if (surface == parent) {
4505 wl_resource_post_error(resource,
4506 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4507 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004508 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004509 return;
4510 }
4511
4512 if (weston_surface_to_subsurface(surface)) {
4513 wl_resource_post_error(resource,
4514 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4515 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004516 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004517 return;
4518 }
4519
Pekka Paalanen50b67472014-10-01 15:02:41 +03004520 if (weston_surface_set_role(surface, "wl_subsurface", resource,
4521 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004522 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004523
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004524 if (weston_surface_get_main_surface(parent) == surface) {
4525 wl_resource_post_error(resource,
4526 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4527 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004528 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004529 return;
4530 }
4531
Pekka Paalanene67858b2013-04-25 13:57:42 +03004532 /* make sure the parent is in its own list */
4533 if (wl_list_empty(&parent->subsurface_list)) {
4534 if (!weston_subsurface_create_for_parent(parent)) {
4535 wl_resource_post_no_memory(resource);
4536 return;
4537 }
4538 }
4539
4540 sub = weston_subsurface_create(id, surface, parent);
4541 if (!sub) {
4542 wl_resource_post_no_memory(resource);
4543 return;
4544 }
4545
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004546 surface->committed = subsurface_committed;
4547 surface->committed_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004548 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004549}
4550
4551static void
4552subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
4553{
4554 wl_resource_destroy(resource);
4555}
4556
4557static const struct wl_subcompositor_interface subcompositor_interface = {
4558 subcompositor_destroy,
4559 subcompositor_get_subsurface
4560};
4561
4562static void
4563bind_subcompositor(struct wl_client *client,
4564 void *data, uint32_t version, uint32_t id)
4565{
4566 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004567 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004568
Jason Ekstranda85118c2013-06-27 20:17:02 -05004569 resource =
4570 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004571 if (resource == NULL) {
4572 wl_client_post_no_memory(client);
4573 return;
4574 }
4575 wl_resource_set_implementation(resource, &subcompositor_interface,
4576 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004577}
4578
Bryce Harrington0795ece2016-08-30 12:04:26 -07004579/** Set a DPMS mode on all of the compositor's outputs
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004580 *
4581 * \param compositor The compositor instance
4582 * \param state The DPMS state the outputs will be set to
4583 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004584static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004585weston_compositor_dpms(struct weston_compositor *compositor,
4586 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004587{
4588 struct weston_output *output;
4589
Bryce Harrington08976ac2016-08-30 12:05:16 -07004590 wl_list_for_each(output, &compositor->output_list, link)
4591 if (output->set_dpms)
4592 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004593}
4594
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004595/** Restores the compositor to active status
4596 *
4597 * \param compositor The compositor instance
4598 *
4599 * If the compositor was in a sleeping mode, all outputs are powered
4600 * back on via DPMS. Otherwise if the compositor was inactive
4601 * (idle/locked, offscreen, or sleeping) then the compositor's wake
4602 * signal will fire.
4603 *
4604 * Restarts the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004605 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004606 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004607WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004608weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004609{
Neil Roberts8b62e202013-09-30 13:14:47 +01004610 uint32_t old_state = compositor->state;
4611
4612 /* The state needs to be changed before emitting the wake
4613 * signal because that may try to schedule a repaint which
4614 * will not work if the compositor is still sleeping */
4615 compositor->state = WESTON_COMPOSITOR_ACTIVE;
4616
4617 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004618 case WESTON_COMPOSITOR_SLEEPING:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004619 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004620 case WESTON_COMPOSITOR_OFFSCREEN:
Daniel Stone893b9362016-11-08 15:47:09 +00004621 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004622 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004623 /* fall through */
4624 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004625 wl_event_source_timer_update(compositor->idle_source,
4626 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004627 }
4628}
4629
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004630/** Turns off rendering and frame events for the compositor.
4631 *
4632 * \param compositor The compositor instance
4633 *
4634 * This is used for example to prevent further rendering while the
4635 * compositor is shutting down.
4636 *
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004637 * Stops the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004638 *
4639 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004640 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004641WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004642weston_compositor_offscreen(struct weston_compositor *compositor)
4643{
4644 switch (compositor->state) {
4645 case WESTON_COMPOSITOR_OFFSCREEN:
4646 return;
4647 case WESTON_COMPOSITOR_SLEEPING:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004648 default:
4649 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4650 wl_event_source_timer_update(compositor->idle_source, 0);
4651 }
4652}
4653
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004654/** Powers down all attached output devices
4655 *
4656 * \param compositor The compositor instance
4657 *
4658 * Causes rendering to the outputs to cease, and no frame events to be
4659 * sent. Only powers down the outputs if the compositor is not already
4660 * in sleep mode.
4661 *
4662 * Stops the idle timer.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004663 *
4664 * \ingroup compositor
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004665 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004666WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004667weston_compositor_sleep(struct weston_compositor *compositor)
4668{
4669 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
4670 return;
4671
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004672 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004673 compositor->state = WESTON_COMPOSITOR_SLEEPING;
4674 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
4675}
4676
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004677/** Sets compositor to idle mode
4678 *
4679 * \param data The compositor instance
4680 *
4681 * This is called when the idle timer fires. Once the compositor is in
4682 * idle mode it requires a wake action (e.g. via
4683 * weston_compositor_wake()) to restore it. The compositor's
4684 * idle_signal will be triggered when the idle event occurs.
4685 *
4686 * Idleness can be inhibited by setting the compositor's idle_inhibit
4687 * property.
4688 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004689static int
4690idle_handler(void *data)
4691{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004692 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004693
4694 if (compositor->idle_inhibit)
4695 return 1;
4696
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004697 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004698 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004699
4700 return 1;
4701}
4702
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004703WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08004704weston_plane_init(struct weston_plane *plane,
4705 struct weston_compositor *ec,
4706 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004707{
4708 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004709 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004710 plane->x = x;
4711 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08004712 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004713
4714 /* Init the link so that the call to wl_list_remove() when releasing
4715 * the plane without ever stacking doesn't lead to a crash */
4716 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004717}
4718
4719WL_EXPORT void
4720weston_plane_release(struct weston_plane *plane)
4721{
Xiong Zhang97116532013-10-23 13:58:31 +08004722 struct weston_view *view;
4723
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004724 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004725 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004726
Xiong Zhang97116532013-10-23 13:58:31 +08004727 wl_list_for_each(view, &plane->compositor->view_list, link) {
4728 if (view->plane == plane)
4729 view->plane = NULL;
4730 }
4731
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004732 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004733}
4734
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004735/** weston_compositor_stack_plane
4736 * \ingroup compositor
4737 */
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004738WL_EXPORT void
4739weston_compositor_stack_plane(struct weston_compositor *ec,
4740 struct weston_plane *plane,
4741 struct weston_plane *above)
4742{
4743 if (above)
4744 wl_list_insert(above->link.prev, &plane->link);
4745 else
4746 wl_list_insert(&ec->plane_list, &plane->link);
4747}
4748
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004749static void
4750output_release(struct wl_client *client, struct wl_resource *resource)
4751{
4752 wl_resource_destroy(resource);
4753}
4754
4755static const struct wl_output_interface output_interface = {
4756 output_release,
4757};
4758
4759
Casey Dahlin9074db52012-04-19 22:50:09 -04004760static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004761{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004762 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004763}
4764
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004765static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004766bind_output(struct wl_client *client,
4767 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05004768{
Pekka Paalanen05347622017-03-27 12:24:34 +03004769 struct weston_head *head = data;
4770 struct weston_output *output = head->output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004771 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004772 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004773
Jason Ekstranda85118c2013-06-27 20:17:02 -05004774 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004775 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004776 if (resource == NULL) {
4777 wl_client_post_no_memory(client);
4778 return;
4779 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004780
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03004781 wl_list_insert(&head->resource_list, wl_resource_get_link(resource));
Pekka Paalanen055c1132017-03-27 16:31:25 +03004782 wl_resource_set_implementation(resource, &output_interface, head,
Pekka Paalanen05347622017-03-27 12:24:34 +03004783 unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04004784
Pekka Paalanen05347622017-03-27 12:24:34 +03004785 assert(output);
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004786 wl_output_send_geometry(resource,
4787 output->x,
4788 output->y,
Pekka Paalanen01f60212017-03-24 15:39:24 +02004789 head->mm_width,
4790 head->mm_height,
4791 head->subpixel,
4792 head->make, head->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04004793 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004794 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004795 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02004796 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004797
4798 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004799 wl_output_send_mode(resource,
4800 mode->flags,
4801 mode->width,
4802 mode->height,
4803 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004804 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02004805
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004806 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004807 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004808}
4809
Pekka Paalanendcac3512017-12-08 14:13:34 +02004810static void
4811weston_head_add_global(struct weston_head *head)
4812{
4813 head->global = wl_global_create(head->compositor->wl_display,
4814 &wl_output_interface, 3,
4815 head, bind_output);
4816}
4817
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02004818/** Remove the global wl_output protocol object
4819 *
4820 * \param head The head whose global to remove.
4821 *
4822 * Also orphans the wl_resources for this head (wl_output).
4823 */
4824static void
4825weston_head_remove_global(struct weston_head *head)
4826{
4827 struct wl_resource *resource, *tmp;
4828
4829 if (head->global)
4830 wl_global_destroy(head->global);
4831 head->global = NULL;
4832
4833 wl_resource_for_each_safe(resource, tmp, &head->resource_list) {
4834 unbind_resource(resource);
4835 wl_resource_set_destructor(resource, NULL);
4836 wl_resource_set_user_data(resource, NULL);
4837 }
Roman Gilge97391c2019-03-29 13:01:06 +01004838
4839 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
4840 /* It's sufficient to unset the destructor, then the list elements
4841 * won't be accessed.
4842 */
4843 wl_resource_set_destructor(resource, NULL);
4844 }
4845 wl_list_init(&head->xdg_output_resource_list);
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02004846}
4847
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004848/** Get the backing object of wl_output
4849 *
4850 * \param resource A wl_output protocol object.
4851 * \return The backing object (user data) of a wl_resource representing a
4852 * wl_output protocol object.
Marius Vlad78984ee2019-06-11 00:05:08 +03004853 *
4854 * \ingroup head
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004855 */
Pekka Paalanen055c1132017-03-27 16:31:25 +03004856WL_EXPORT struct weston_head *
4857weston_head_from_resource(struct wl_resource *resource)
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03004858{
4859 assert(wl_resource_instance_of(resource, &wl_output_interface,
4860 &output_interface));
4861
4862 return wl_resource_get_user_data(resource);
4863}
4864
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004865/** Initialize a pre-allocated weston_head
4866 *
4867 * \param head The head to initialize.
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004868 * \param name The head name, e.g. the connector name or equivalent.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004869 *
4870 * The head will be safe to attach, detach and release.
4871 *
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004872 * The name is used in logs, and can be used by compositors as a configuration
4873 * identifier.
4874 *
Marius Vlad78984ee2019-06-11 00:05:08 +03004875 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004876 * \internal
4877 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004878WL_EXPORT void
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004879weston_head_init(struct weston_head *head, const char *name)
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004880{
4881 /* Add some (in)sane defaults which can be used
4882 * for checking if an output was properly configured
4883 */
4884 memset(head, 0, sizeof *head);
4885
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004886 wl_list_init(&head->compositor_link);
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03004887 wl_signal_init(&head->destroy_signal);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004888 wl_list_init(&head->output_link);
4889 wl_list_init(&head->resource_list);
Roman Gilge97391c2019-03-29 13:01:06 +01004890 wl_list_init(&head->xdg_output_resource_list);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03004891 head->name = strdup(name);
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05304892 head->current_protection = WESTON_HDCP_DISABLE;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03004893}
4894
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004895/** Send output heads changed signal
4896 *
4897 * \param output The output that changed.
4898 *
4899 * Notify that the enabled output gained and/or lost heads, or that the
4900 * associated heads may have changed their connection status. This does not
4901 * include cases where the output becomes enabled or disabled. The registered
4902 * callbacks are called after the change has successfully happened.
4903 *
4904 * If connection status change causes the compositor to attach or detach a head
4905 * to an enabled output, the registered callbacks may be called multiple times.
Marius Vlad55d87362019-06-11 01:15:35 +03004906 *
4907 * \ingroup output
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004908 */
4909static void
4910weston_output_emit_heads_changed(struct weston_output *output)
4911{
4912 wl_signal_emit(&output->compositor->output_heads_changed_signal,
4913 output);
4914}
4915
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004916/** Idle task for emitting heads_changed_signal */
4917static void
4918weston_compositor_call_heads_changed(void *data)
4919{
4920 struct weston_compositor *compositor = data;
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004921 struct weston_head *head;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004922
4923 compositor->heads_changed_source = NULL;
4924
4925 wl_signal_emit(&compositor->heads_changed_signal, compositor);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02004926
4927 wl_list_for_each(head, &compositor->head_list, compositor_link) {
4928 if (head->output && head->output->enabled)
4929 weston_output_emit_heads_changed(head->output);
4930 }
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004931}
4932
4933/** Schedule a call on idle to heads_changed callback
4934 *
4935 * \param compositor The Compositor.
4936 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004937 * \ingroup compositor
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004938 * \internal
4939 */
4940static void
4941weston_compositor_schedule_heads_changed(struct weston_compositor *compositor)
4942{
4943 struct wl_event_loop *loop;
4944
4945 if (compositor->heads_changed_source)
4946 return;
4947
4948 loop = wl_display_get_event_loop(compositor->wl_display);
4949 compositor->heads_changed_source = wl_event_loop_add_idle(loop,
4950 weston_compositor_call_heads_changed, compositor);
4951}
4952
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004953/** Register a new head
4954 *
4955 * \param compositor The compositor.
4956 * \param head The head to register, must not be already registered.
4957 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004958 * This signals the core that a new head has become available, leading to
4959 * heads_changed hook being called later.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004960 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004961 * \ingroup compositor
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004962 * \internal
4963 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03004964WL_EXPORT void
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004965weston_compositor_add_head(struct weston_compositor *compositor,
4966 struct weston_head *head)
4967{
4968 assert(wl_list_empty(&head->compositor_link));
4969 assert(head->name);
4970
4971 wl_list_insert(compositor->head_list.prev, &head->compositor_link);
4972 head->compositor = compositor;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004973 weston_compositor_schedule_heads_changed(compositor);
4974}
4975
4976/** Adds a listener to be called when heads change
4977 *
4978 * \param compositor The compositor.
4979 * \param listener The listener to add.
4980 *
Marius Vlada2dace22019-06-12 16:05:44 +03004981 * The listener notify function argument is weston_compositor.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004982 *
4983 * The listener function will be called after heads are added or their
4984 * connection status has changed. Several changes may be accumulated into a
4985 * single call. The user is expected to iterate over the existing heads and
4986 * check their statuses to find out what changed.
4987 *
4988 * \sa weston_compositor_iterate_heads, weston_head_is_connected,
4989 * weston_head_is_enabled
Marius Vlad9fdda7f2019-06-11 16:08:55 +03004990 * \ingroup compositor
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03004991 */
4992WL_EXPORT void
4993weston_compositor_add_heads_changed_listener(struct weston_compositor *compositor,
4994 struct wl_listener *listener)
4995{
4996 wl_signal_add(&compositor->heads_changed_signal, listener);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03004997}
4998
4999/** Iterate over available heads
5000 *
5001 * \param compositor The compositor.
Marius Vlada2dace22019-06-12 16:05:44 +03005002 * \param iter The iterator, or NULL for start.
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005003 * \return The next available head in the list.
5004 *
5005 * Returns all available heads, regardless of being connected or enabled.
5006 *
5007 * You can iterate over all heads as follows:
5008 * \code
5009 * struct weston_head *head = NULL;
5010 *
5011 * while ((head = weston_compositor_iterate_heads(compositor, head))) {
5012 * ...
5013 * }
5014 * \endcode
5015 *
5016 * If you cause \c iter to be removed from the list, you cannot use it to
5017 * continue iterating. Removing any other item is safe.
5018 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005019 * \ingroup compositor
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005020 */
5021WL_EXPORT struct weston_head *
5022weston_compositor_iterate_heads(struct weston_compositor *compositor,
5023 struct weston_head *iter)
5024{
5025 struct wl_list *list = &compositor->head_list;
5026 struct wl_list *node;
5027
5028 assert(compositor);
5029 assert(!iter || iter->compositor == compositor);
5030
5031 if (iter)
5032 node = iter->compositor_link.next;
5033 else
5034 node = list->next;
5035
5036 assert(node);
5037 assert(!iter || node != &iter->compositor_link);
5038
5039 if (node == list)
5040 return NULL;
5041
5042 return container_of(node, struct weston_head, compositor_link);
5043}
5044
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005045/** Iterate over attached heads
5046 *
5047 * \param output The output whose heads to iterate.
Marius Vlada2dace22019-06-12 16:05:44 +03005048 * \param iter The iterator, or NULL for start.
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005049 * \return The next attached head in the list.
5050 *
5051 * Returns all heads currently attached to the output.
5052 *
5053 * You can iterate over all heads as follows:
5054 * \code
5055 * struct weston_head *head = NULL;
5056 *
5057 * while ((head = weston_output_iterate_heads(output, head))) {
5058 * ...
5059 * }
5060 * \endcode
5061 *
5062 * If you cause \c iter to be removed from the list, you cannot use it to
5063 * continue iterating. Removing any other item is safe.
5064 *
Marius Vlad55d87362019-06-11 01:15:35 +03005065 * \ingroup ouput
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005066 */
5067WL_EXPORT struct weston_head *
5068weston_output_iterate_heads(struct weston_output *output,
5069 struct weston_head *iter)
5070{
5071 struct wl_list *list = &output->head_list;
5072 struct wl_list *node;
5073
5074 assert(output);
5075 assert(!iter || iter->output == output);
5076
5077 if (iter)
5078 node = iter->output_link.next;
5079 else
5080 node = list->next;
5081
5082 assert(node);
5083 assert(!iter || node != &iter->output_link);
5084
5085 if (node == list)
5086 return NULL;
5087
5088 return container_of(node, struct weston_head, output_link);
5089}
5090
Pekka Paalanendcac3512017-12-08 14:13:34 +02005091/** Attach a head to an output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005092 *
5093 * \param output The output to attach to.
5094 * \param head The head that is not yet attached.
5095 * \return 0 on success, -1 on failure.
5096 *
5097 * Attaches the given head to the output. All heads of an output are clones
5098 * and share the resolution and timings.
5099 *
5100 * Cloning heads this way uses less resources than creating an output for
5101 * each head, but is not always possible due to environment, driver and hardware
5102 * limitations.
5103 *
5104 * On failure, the head remains unattached. Success of this function does not
5105 * guarantee the output configuration is actually valid. The final checks are
Pekka Paalanendcac3512017-12-08 14:13:34 +02005106 * made on weston_output_enable() unless the output was already enabled.
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005107 *
Marius Vlad55d87362019-06-11 01:15:35 +03005108 * \ingroup output
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005109 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005110WL_EXPORT int
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005111weston_output_attach_head(struct weston_output *output,
5112 struct weston_head *head)
5113{
Pekka Paalanendcac3512017-12-08 14:13:34 +02005114 char *head_names;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005115
5116 if (!wl_list_empty(&head->output_link))
5117 return -1;
5118
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005119 if (output->attach_head) {
5120 if (output->attach_head(output, head) < 0)
5121 return -1;
5122 } else if (!wl_list_empty(&output->head_list)) {
5123 /* No support for clones in the legacy path. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005124 return -1;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005125 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005126
5127 head->output = output;
5128 wl_list_insert(output->head_list.prev, &head->output_link);
5129
Pekka Paalanendcac3512017-12-08 14:13:34 +02005130 if (output->enabled) {
5131 weston_head_add_global(head);
5132
5133 head_names = weston_output_create_heads_string(output);
5134 weston_log("Output '%s' updated to have head(s) %s\n",
5135 output->name, head_names);
5136 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005137
5138 weston_output_emit_heads_changed(output);
Pekka Paalanendcac3512017-12-08 14:13:34 +02005139 }
5140
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005141 return 0;
5142}
5143
5144/** Detach a head from its output
5145 *
5146 * \param head The head to detach.
5147 *
5148 * It is safe to detach a non-attached head.
5149 *
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005150 * If the head is attached to an enabled output and the output will be left
5151 * with no heads, the output will be disabled.
5152 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005153 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005154 * \sa weston_output_disable
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005155 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005156WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005157weston_head_detach(struct weston_head *head)
5158{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005159 struct weston_output *output = head->output;
Pekka Paalanena0106992017-12-08 16:11:17 +02005160 char *head_names;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005161
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005162 wl_list_remove(&head->output_link);
5163 wl_list_init(&head->output_link);
5164 head->output = NULL;
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005165
5166 if (!output)
5167 return;
5168
5169 if (output->detach_head)
5170 output->detach_head(output, head);
5171
5172 if (output->enabled) {
5173 weston_head_remove_global(head);
5174
Pekka Paalanena0106992017-12-08 16:11:17 +02005175 if (wl_list_empty(&output->head_list)) {
5176 weston_log("Output '%s' no heads left, disabling.\n",
5177 output->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005178 weston_output_disable(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02005179 } else {
5180 head_names = weston_output_create_heads_string(output);
5181 weston_log("Output '%s' updated to have head(s) %s\n",
5182 output->name, head_names);
5183 free(head_names);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02005184
5185 weston_output_emit_heads_changed(output);
Pekka Paalanena0106992017-12-08 16:11:17 +02005186 }
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005187 }
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005188}
5189
5190/** Destroy a head
5191 *
5192 * \param head The head to be released.
5193 *
5194 * Destroys the head. The caller is responsible for freeing the memory pointed
5195 * to by \c head.
5196 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005197 * \ingroup head
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005198 * \internal
5199 */
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005200WL_EXPORT void
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005201weston_head_release(struct weston_head *head)
5202{
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005203 wl_signal_emit(&head->destroy_signal, head);
5204
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005205 weston_head_detach(head);
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005206
5207 free(head->make);
5208 free(head->model);
5209 free(head->serial_number);
Pekka Paalanen9b02e472017-08-14 14:43:13 +03005210 free(head->name);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03005211
5212 wl_list_remove(&head->compositor_link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005213}
5214
Pekka Paalanene19970f2017-08-28 14:11:02 +03005215static void
5216weston_head_set_device_changed(struct weston_head *head)
5217{
5218 head->device_changed = true;
5219
5220 if (head->compositor)
5221 weston_compositor_schedule_heads_changed(head->compositor);
5222}
5223
5224/** String equal comparison with NULLs being equal */
5225static bool
5226str_null_eq(const char *a, const char *b)
5227{
5228 if (!a && !b)
5229 return true;
5230
5231 if (!!a != !!b)
5232 return false;
5233
5234 return strcmp(a, b) == 0;
5235}
5236
Pekka Paalanen01f60212017-03-24 15:39:24 +02005237/** Store monitor make, model and serial number
5238 *
5239 * \param head The head to modify.
5240 * \param make The monitor make. If EDID is available, the PNP ID. Otherwise
5241 * any string, or NULL for none.
5242 * \param model The monitor model or name, or a made-up string, or NULL for
5243 * none.
5244 * \param serialno The monitor serial number, a made-up string, or NULL for
5245 * none.
5246 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005247 * This may set the device_changed flag.
5248 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005249 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005250 * \internal
5251 */
5252WL_EXPORT void
5253weston_head_set_monitor_strings(struct weston_head *head,
5254 const char *make,
5255 const char *model,
5256 const char *serialno)
5257{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005258 if (str_null_eq(head->make, make) &&
5259 str_null_eq(head->model, model) &&
5260 str_null_eq(head->serial_number, serialno))
5261 return;
5262
Pekka Paalanen06f99ef2017-04-04 16:26:23 +03005263 free(head->make);
5264 free(head->model);
5265 free(head->serial_number);
5266
5267 head->make = make ? strdup(make) : NULL;
5268 head->model = model ? strdup(model) : NULL;
5269 head->serial_number = serialno ? strdup(serialno) : NULL;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005270
5271 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005272}
5273
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005274/** Store display non-desktop status
5275 *
5276 * \param head The head to modify.
5277 * \param non_desktop Whether the head connects to a non-desktop display.
5278 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005279 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005280 * \internal
5281 */
5282WL_EXPORT void
5283weston_head_set_non_desktop(struct weston_head *head, bool non_desktop)
5284{
5285 if (head->non_desktop == non_desktop)
5286 return;
5287
5288 head->non_desktop = non_desktop;
5289
5290 weston_head_set_device_changed(head);
5291}
5292
Pekka Paalanen01f60212017-03-24 15:39:24 +02005293/** Store physical image size
5294 *
5295 * \param head The head to modify.
5296 * \param mm_width Image area width in millimeters.
5297 * \param mm_height Image area height in millimeters.
5298 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005299 * This may set the device_changed flag.
5300 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005301 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005302 * \internal
5303 */
5304WL_EXPORT void
5305weston_head_set_physical_size(struct weston_head *head,
5306 int32_t mm_width, int32_t mm_height)
5307{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005308 if (head->mm_width == mm_width &&
5309 head->mm_height == mm_height)
5310 return;
5311
Pekka Paalanen01f60212017-03-24 15:39:24 +02005312 head->mm_width = mm_width;
5313 head->mm_height = mm_height;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005314
5315 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005316}
5317
5318/** Store monitor sub-pixel layout
5319 *
5320 * \param head The head to modify.
5321 * \param sp Sub-pixel layout. The possible values are:
5322 * - WL_OUTPUT_SUBPIXEL_UNKNOWN,
5323 * - WL_OUTPUT_SUBPIXEL_NONE,
5324 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB,
5325 * - WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR,
5326 * - WL_OUTPUT_SUBPIXEL_VERTICAL_RGB,
5327 * - WL_OUTPUT_SUBPIXEL_VERTICAL_BGR
5328 *
Pekka Paalanene19970f2017-08-28 14:11:02 +03005329 * This may set the device_changed flag.
5330 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005331 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005332 * \internal
5333 */
5334WL_EXPORT void
5335weston_head_set_subpixel(struct weston_head *head,
5336 enum wl_output_subpixel sp)
5337{
Pekka Paalanene19970f2017-08-28 14:11:02 +03005338 if (head->subpixel == sp)
5339 return;
5340
Pekka Paalanen01f60212017-03-24 15:39:24 +02005341 head->subpixel = sp;
Pekka Paalanene19970f2017-08-28 14:11:02 +03005342
5343 weston_head_set_device_changed(head);
Pekka Paalanen01f60212017-03-24 15:39:24 +02005344}
5345
5346/** Mark the monitor as internal
5347 *
5348 * This is used for embedded screens, like laptop panels.
5349 *
5350 * \param head The head to mark as internal.
5351 *
5352 * By default a head is external. The type is often inferred from the physical
5353 * connector type.
5354 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005355 * \ingroup head
Pekka Paalanen01f60212017-03-24 15:39:24 +02005356 * \internal
5357 */
5358WL_EXPORT void
5359weston_head_set_internal(struct weston_head *head)
5360{
5361 head->connection_internal = true;
5362}
Pekka Paalanen9ffb2502017-03-27 15:14:32 +03005363
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005364/** Store connector status
5365 *
5366 * \param head The head to modify.
5367 * \param connected Whether the head is connected.
5368 *
5369 * Connectors are created as disconnected. This function can be used to
5370 * set the connector status.
5371 *
5372 * The status should be set to true when a physical connector is connected to
5373 * a video sink device like a monitor and to false when the connector is
5374 * disconnected. For nested backends, the connection status should reflect the
5375 * connection to the parent display server.
5376 *
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005377 * When the connection status changes, it schedules a call to the heads_changed
Pekka Paalanene19970f2017-08-28 14:11:02 +03005378 * hook and sets the device_changed flag.
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005379 *
5380 * \sa weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005381 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005382 * \internal
5383 */
5384WL_EXPORT void
5385weston_head_set_connection_status(struct weston_head *head, bool connected)
5386{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005387 if (head->connected == connected)
5388 return;
5389
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005390 head->connected = connected;
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03005391
Pekka Paalanene19970f2017-08-28 14:11:02 +03005392 weston_head_set_device_changed(head);
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005393}
5394
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305395static void
5396weston_output_compute_protection(struct weston_output *output)
5397{
5398 struct weston_head *head;
5399 enum weston_hdcp_protection op_protection;
5400 bool op_protection_valid = false;
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305401 struct weston_compositor *wc = output->compositor;
5402 struct content_protection *cp = wc->content_protection;
5403
5404 if (!cp)
5405 return;
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305406
5407 wl_list_for_each(head, &output->head_list, output_link) {
5408 if (!op_protection_valid) {
5409 op_protection = head->current_protection;
5410 op_protection_valid = true;
5411 }
5412 if (head->current_protection < op_protection)
5413 op_protection = head->current_protection;
5414 }
5415
5416 if (!op_protection_valid)
5417 op_protection = WESTON_HDCP_DISABLE;
5418
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305419 if (output->current_protection != op_protection) {
5420 struct wl_event_loop *loop;
5421
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305422 output->current_protection = op_protection;
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +05305423 weston_output_damage(output);
5424 if (cp->surface_protection_update)
5425 return;
5426 loop = wl_display_get_event_loop(wc->wl_display);
5427 cp->surface_protection_update = wl_event_loop_add_idle(loop,
5428 notify_surface_protection_change,
5429 wc);
5430 }
Ankit Nautiyal4f64ff82019-04-03 18:44:35 +05305431}
5432
5433WL_EXPORT void
5434weston_head_set_content_protection_status(struct weston_head *head,
5435 enum weston_hdcp_protection status)
5436{
5437 head->current_protection = status;
5438 if (head->output)
5439 weston_output_compute_protection(head->output);
5440}
5441
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005442/** Is the head currently connected?
5443 *
5444 * \param head The head to query.
5445 * \return Connection status.
5446 *
5447 * Returns true if the head is physically connected to a monitor, or in
5448 * case of a nested backend returns true when there is a connection to the
5449 * parent display server.
5450 *
5451 * This is independent from the head being enabled.
5452 *
5453 * \sa weston_head_is_enabled
Marius Vlad78984ee2019-06-11 00:05:08 +03005454 * \ingroup head
Pekka Paalanen7fe858b2017-08-14 15:45:14 +03005455 */
5456WL_EXPORT bool
5457weston_head_is_connected(struct weston_head *head)
5458{
5459 return head->connected;
5460}
5461
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005462/** Is the head currently enabled?
5463 *
5464 * \param head The head to query.
5465 * \return Video status.
5466 *
5467 * Returns true if the head is currently transmitting a video stream.
5468 *
5469 * This is independent of the head being connected.
5470 *
5471 * \sa weston_head_is_connected
Marius Vlad78984ee2019-06-11 00:05:08 +03005472 * \ingroup head
Pekka Paalanen8e552fd2018-02-15 15:18:20 +02005473 */
5474WL_EXPORT bool
5475weston_head_is_enabled(struct weston_head *head)
5476{
5477 if (!head->output)
5478 return false;
5479
5480 return head->output->enabled;
5481}
5482
Pekka Paalanene19970f2017-08-28 14:11:02 +03005483/** Has the device information changed?
5484 *
5485 * \param head The head to query.
5486 * \return True if the device information has changed since last reset.
5487 *
5488 * The information about the connected display device, e.g. a monitor, may
5489 * change without being disconnected in between. Changing information
5490 * causes a call to the heads_changed hook.
5491 *
5492 * The information includes make, model, serial number, physical size,
5493 * and sub-pixel type. The connection status is also included.
5494 *
5495 * \sa weston_head_reset_device_changed, weston_compositor_set_heads_changed_cb
Marius Vlad78984ee2019-06-11 00:05:08 +03005496 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03005497 */
5498WL_EXPORT bool
5499weston_head_is_device_changed(struct weston_head *head)
5500{
5501 return head->device_changed;
5502}
5503
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005504/** Does the head represent a non-desktop display?
5505 *
5506 * \param head The head to query.
5507 * \return True if the device is a non-desktop display.
5508 *
5509 * Non-desktop heads are not attached to outputs by default.
5510 * This stops weston from extending the desktop onto head mounted displays.
5511 *
Marius Vlad78984ee2019-06-11 00:05:08 +03005512 * \ingroup head
Philipp Zabelc18ffd32018-08-30 17:38:03 +02005513 */
5514WL_EXPORT bool
5515weston_head_is_non_desktop(struct weston_head *head)
5516{
5517 return head->non_desktop;
5518}
5519
Pekka Paalanene19970f2017-08-28 14:11:02 +03005520/** Acknowledge device information change
5521 *
5522 * \param head The head to acknowledge.
5523 *
5524 * Clears the device changed flag on this head. When a compositor has processed
5525 * device information, it should call this to be able to notice further
5526 * changes.
5527 *
5528 * \sa weston_head_is_device_changed
Marius Vlad78984ee2019-06-11 00:05:08 +03005529 * \ingroup head
Pekka Paalanene19970f2017-08-28 14:11:02 +03005530 */
5531WL_EXPORT void
5532weston_head_reset_device_changed(struct weston_head *head)
5533{
5534 head->device_changed = false;
5535}
5536
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005537/** Get the name of a head
5538 *
5539 * \param head The head to query.
5540 * \return The head's name, not NULL.
5541 *
5542 * The name depends on the backend. The DRM backend uses connector names,
5543 * other backends may use hardcoded names or user-given names.
Marius Vlad78984ee2019-06-11 00:05:08 +03005544 *
5545 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005546 */
5547WL_EXPORT const char *
5548weston_head_get_name(struct weston_head *head)
5549{
5550 return head->name;
5551}
5552
5553/** Get the output the head is attached to
5554 *
5555 * \param head The head to query.
5556 * \return The output the head is attached to, or NULL if detached.
Marius Vlad78984ee2019-06-11 00:05:08 +03005557 * \ingroup head
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03005558 */
5559WL_EXPORT struct weston_output *
5560weston_head_get_output(struct weston_head *head)
5561{
5562 return head->output;
5563}
5564
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005565/** Add destroy callback for a head
5566 *
5567 * \param head The head to watch for.
5568 * \param listener The listener to add. The \c notify member must be set.
5569 *
5570 * Heads may get destroyed for various reasons by the backends. If a head is
5571 * attached to an output, the compositor should listen for head destruction
5572 * and reconfigure or destroy the output if necessary.
5573 *
5574 * The destroy callbacks will be called on weston_head destruction before any
5575 * automatic detaching from an associated weston_output and before any
5576 * weston_head information is lost.
5577 *
5578 * The \c data argument to the notify callback is the weston_head being
5579 * destroyed.
Marius Vlad78984ee2019-06-11 00:05:08 +03005580 *
5581 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005582 */
5583WL_EXPORT void
5584weston_head_add_destroy_listener(struct weston_head *head,
5585 struct wl_listener *listener)
5586{
5587 wl_signal_add(&head->destroy_signal, listener);
5588}
5589
5590/** Look up destroy listener for a head
5591 *
5592 * \param head The head to query.
5593 * \param notify The notify function used used for the added destroy listener.
5594 * \return The listener, or NULL if not found.
5595 *
5596 * This looks up the previously added destroy listener struct based on the
5597 * notify function it has. The listener can be used to access user data
5598 * through \c container_of().
5599 *
5600 * \sa wl_signal_get()
Marius Vlad78984ee2019-06-11 00:05:08 +03005601 * \ingroup head
Pekka Paalanen2e1bedb2017-10-10 11:21:58 +03005602 */
5603WL_EXPORT struct wl_listener *
5604weston_head_get_destroy_listener(struct weston_head *head,
5605 wl_notify_func_t notify)
5606{
5607 return wl_signal_get(&head->destroy_signal, notify);
5608}
5609
David Fort0de859e2016-05-27 23:22:57 +02005610/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005611static void
David Fort0de859e2016-05-27 23:22:57 +02005612weston_compositor_reflow_outputs(struct weston_compositor *compositor,
5613 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005614{
5615 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02005616 bool start_resizing = false;
5617
5618 if (!delta_width)
5619 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005620
5621 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02005622 if (output == resized_output) {
5623 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005624 continue;
5625 }
5626
David Fort0de859e2016-05-27 23:22:57 +02005627 if (start_resizing) {
5628 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02005629 output->dirty = 1;
5630 }
5631 }
5632}
5633
Pekka Paalanend72bad22017-03-29 17:01:41 +03005634static void
Scott Moreauccbf29d2012-02-22 14:21:41 -07005635weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005636{
Scott Moreau850ca422012-05-21 15:21:25 -06005637 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05005638
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005639 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005640 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06005641
Scott Moreauccbf29d2012-02-22 14:21:41 -07005642 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06005643 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005644 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005645 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005646 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01005647 weston_matrix_scale(&output->matrix, magnification,
5648 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005649 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005650
Jason Ekstrandfb23df72014-10-16 10:55:21 -05005651 switch (output->transform) {
5652 case WL_OUTPUT_TRANSFORM_FLIPPED:
5653 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5654 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5655 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5656 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5657 weston_matrix_scale(&output->matrix, -1, 1, 1);
5658 break;
5659 }
5660
5661 switch (output->transform) {
5662 default:
5663 case WL_OUTPUT_TRANSFORM_NORMAL:
5664 case WL_OUTPUT_TRANSFORM_FLIPPED:
5665 break;
5666 case WL_OUTPUT_TRANSFORM_90:
5667 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5668 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
5669 weston_matrix_rotate_xy(&output->matrix, 0, 1);
5670 break;
5671 case WL_OUTPUT_TRANSFORM_180:
5672 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
5673 weston_matrix_translate(&output->matrix,
5674 -output->width, -output->height, 0);
5675 weston_matrix_rotate_xy(&output->matrix, -1, 0);
5676 break;
5677 case WL_OUTPUT_TRANSFORM_270:
5678 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5679 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
5680 weston_matrix_rotate_xy(&output->matrix, 0, -1);
5681 break;
5682 }
5683
5684 if (output->current_scale != 1)
5685 weston_matrix_scale(&output->matrix,
5686 output->current_scale,
5687 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01005688
Scott Moreauccbf29d2012-02-22 14:21:41 -07005689 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05005690
5691 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005692}
5693
Scott Moreau1bad5db2012-08-18 01:04:05 -06005694static void
Alexander Larsson0b135062013-05-28 16:23:36 +02005695weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06005696{
5697 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005698 output->native_scale = scale;
5699 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06005700
Pekka Paalanen59987fa2016-04-26 15:50:59 +03005701 convert_size_by_transform_scale(&output->width, &output->height,
5702 output->current_mode->width,
5703 output->current_mode->height,
5704 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02005705}
5706
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005707static void
5708weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07005709{
5710 output->x = x;
5711 output->y = y;
5712
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005713 pixman_region32_fini(&output->previous_damage);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02005714 pixman_region32_init(&output->previous_damage);
Pekka Paalanen4b582c72017-03-30 16:04:58 +03005715
5716 pixman_region32_fini(&output->region);
Scott Moreauccbf29d2012-02-22 14:21:41 -07005717 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06005718 output->width,
5719 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01005720}
5721
Marius Vlad55d87362019-06-11 01:15:35 +03005722/**
5723 * \ingroup output
5724 */
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005725WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005726weston_output_move(struct weston_output *output, int x, int y)
5727{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005728 struct weston_head *head;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005729 struct wl_resource *resource;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005730 int ver;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005731
5732 output->move_x = x - output->x;
5733 output->move_y = y - output->y;
5734
5735 if (output->move_x == 0 && output->move_y == 0)
5736 return;
5737
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005738 weston_output_init_geometry(output, x, y);
5739
5740 output->dirty = 1;
5741
5742 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005743 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005744
5745 /* Notify clients of the change for output position. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005746 wl_list_for_each(head, &output->head_list, output_link) {
5747 wl_resource_for_each(resource, &head->resource_list) {
5748 wl_output_send_geometry(resource,
5749 output->x,
5750 output->y,
5751 head->mm_width,
5752 head->mm_height,
5753 head->subpixel,
5754 head->make,
5755 head->model,
5756 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08005757
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005758 ver = wl_resource_get_version(resource);
5759 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
5760 wl_output_send_done(resource);
5761 }
Roman Gilge97391c2019-03-29 13:01:06 +01005762
5763 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
5764 zxdg_output_v1_send_logical_position(resource,
5765 output->x,
5766 output->y);
5767 zxdg_output_v1_send_done(resource);
5768 }
Quanxian Wangb2c86362014-03-14 09:16:25 +08005769 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02005770}
5771
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005772/** Signal that a pending output is taken into use.
5773 *
5774 * Removes the output from the pending list and adds it to the compositor's
5775 * list of enabled outputs. The output created signal is emitted.
Giulio Camuffob1147152015-05-06 21:41:57 +03005776 *
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005777 * The output gets an internal ID assigned, and the wl_output global is
5778 * created.
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005779 *
Giulio Camuffob1147152015-05-06 21:41:57 +03005780 * \param compositor The compositor instance.
5781 * \param output The output to be added.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005782 *
5783 * \internal
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005784 * \ingroup compositor
Giulio Camuffob1147152015-05-06 21:41:57 +03005785 */
Pekka Paalanenf9681b52017-03-29 16:58:48 +03005786static void
Giulio Camuffob1147152015-05-06 21:41:57 +03005787weston_compositor_add_output(struct weston_compositor *compositor,
5788 struct weston_output *output)
5789{
Armin Krezoviće5403842016-08-05 15:28:29 +02005790 struct weston_view *view, *next;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005791 struct weston_head *head;
Armin Krezoviće5403842016-08-05 15:28:29 +02005792
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005793 assert(!output->enabled);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005794
5795 /* Verify we haven't reached the limit of 32 available output IDs */
5796 assert(ffs(~compositor->output_id_pool) > 0);
5797
5798 /* Invert the output id pool and look for the lowest numbered
5799 * switch (the least significant bit). Take that bit's position
5800 * as our ID, and mark it used in the compositor's output_id_pool.
5801 */
5802 output->id = ffs(~compositor->output_id_pool) - 1;
5803 compositor->output_id_pool |= 1u << output->id;
5804
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005805 wl_list_remove(&output->link);
Giulio Camuffob1147152015-05-06 21:41:57 +03005806 wl_list_insert(compositor->output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005807 output->enabled = true;
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005808
Pekka Paalanendcac3512017-12-08 14:13:34 +02005809 wl_list_for_each(head, &output->head_list, output_link)
5810 weston_head_add_global(head);
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005811
Giulio Camuffob1147152015-05-06 21:41:57 +03005812 wl_signal_emit(&compositor->output_created_signal, output);
Armin Krezoviće5403842016-08-05 15:28:29 +02005813
5814 wl_list_for_each_safe(view, next, &compositor->view_list, link)
5815 weston_view_geometry_dirty(view);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005816}
5817
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005818/** Transform device coordinates into global coordinates
5819 *
Marius Vlada2dace22019-06-12 16:05:44 +03005820 * \param output the weston_output object
5821 * \param[in] device_x X coordinate in device units.
5822 * \param[in] device_y Y coordinate in device units.
5823 * \param[out] x X coordinate in the global space.
5824 * \param[out] y Y coordinate in the global space.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005825 *
Marius Vlada2dace22019-06-12 16:05:44 +03005826 * Transforms coordinates from the device coordinate space (physical pixel
5827 * units) to the global coordinate space (logical pixel units). This takes
5828 * into account output transform and scale.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005829 *
Marius Vlad55d87362019-06-11 01:15:35 +03005830 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005831 * \internal
5832 */
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005833WL_EXPORT void
5834weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005835 double device_x, double device_y,
5836 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005837{
Derek Foreman0f679412014-10-02 13:41:17 -05005838 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005839 device_x,
5840 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05005841 0.0,
5842 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005843
Derek Foreman67a18b92015-03-24 11:36:14 -05005844 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005845
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02005846 *x = p.f[0] / p.f[3];
5847 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07005848}
5849
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005850/** Removes output from compositor's list of enabled outputs
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005851 *
5852 * \param output The weston_output object that is being removed.
5853 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005854 * The following happens:
5855 *
5856 * - The output assignments of all views in the current scenegraph are
5857 * recomputed.
5858 *
5859 * - Presentation feedback is discarded.
5860 *
5861 * - Compositor is notified that outputs were changed and
5862 * applies the necessary changes to re-layout outputs.
5863 *
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005864 * - The output is put back in the pending outputs list.
5865 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03005866 * - Signal is emitted to notify all users of the weston_output
5867 * object that the output is being destroyed.
5868 *
5869 * - wl_output protocol objects referencing this weston_output
Pekka Paalanen2210ad02017-03-30 15:48:06 +03005870 * are made inert, and the wl_output global is removed.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005871 *
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005872 * - The output's internal ID is released.
5873 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03005874 * \ingroup compositor
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005875 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005876 */
5877static void
5878weston_compositor_remove_output(struct weston_output *output)
5879{
Pekka Paalanenbccda712017-03-29 16:16:04 +03005880 struct weston_compositor *compositor = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005881 struct weston_view *view;
Pekka Paalanen1b9bf592017-03-27 12:15:38 +03005882 struct weston_head *head;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005883
5884 assert(output->destroying);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005885 assert(output->enabled);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005886
Pekka Paalanenbccda712017-03-29 16:16:04 +03005887 wl_list_for_each(view, &compositor->view_list, link) {
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005888 if (view->output_mask & (1u << output->id))
5889 weston_view_assign_output(view);
5890 }
5891
5892 weston_presentation_feedback_discard_list(&output->feedback_list);
5893
Pekka Paalanen9711fd92018-06-21 14:26:18 +03005894 weston_compositor_reflow_outputs(compositor, output, -output->width);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005895
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005896 wl_list_remove(&output->link);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03005897 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Pekka Paalanen7f340ff2017-03-30 14:56:22 +03005898 output->enabled = false;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005899
Pekka Paalanenbccda712017-03-29 16:16:04 +03005900 wl_signal_emit(&compositor->output_destroyed_signal, output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005901 wl_signal_emit(&output->destroy_signal, output);
5902
Pekka Paalanend9dcc6d2017-12-04 15:28:13 +02005903 wl_list_for_each(head, &output->head_list, output_link)
5904 weston_head_remove_global(head);
Pekka Paalanen3d2d4972017-03-30 15:19:45 +03005905
5906 compositor->output_id_pool &= ~(1u << output->id);
5907 output->id = 0xffffffff; /* invalid */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005908}
5909
5910/** Sets the output scale for a given output.
5911 *
5912 * \param output The weston_output object that the scale is set for.
5913 * \param scale Scale factor for the given output.
5914 *
5915 * It only supports setting scale for an output that
5916 * is not enabled and it can only be ran once.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005917 *
Marius Vlad55d87362019-06-11 01:15:35 +03005918 * \ingroup ouput
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005919 */
5920WL_EXPORT void
5921weston_output_set_scale(struct weston_output *output,
5922 int32_t scale)
5923{
5924 /* We can only set scale on a disabled output */
5925 assert(!output->enabled);
5926
5927 /* We only want to set scale once */
5928 assert(!output->scale);
5929
5930 output->scale = scale;
5931}
5932
5933/** Sets the output transform for a given output.
5934 *
5935 * \param output The weston_output object that the transform is set for.
5936 * \param transform Transform value for the given output.
5937 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005938 * Refer to wl_output::transform section located at
5939 * https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_output
5940 * for list of values that can be passed to this function.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03005941 *
Marius Vlad55d87362019-06-11 01:15:35 +03005942 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005943 */
5944WL_EXPORT void
5945weston_output_set_transform(struct weston_output *output,
5946 uint32_t transform)
5947{
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005948 struct weston_pointer_motion_event ev;
5949 struct wl_resource *resource;
5950 struct weston_seat *seat;
5951 pixman_region32_t old_region;
5952 int mid_x, mid_y;
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005953 struct weston_head *head;
5954 int ver;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005955
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005956 if (!output->enabled && output->transform == UINT32_MAX) {
5957 output->transform = transform;
5958 return;
5959 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005960
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005961 weston_output_transform_scale_init(output, transform, output->scale);
5962
5963 pixman_region32_init(&old_region);
5964 pixman_region32_copy(&old_region, &output->region);
5965
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005966 weston_output_init_geometry(output, output->x, output->y);
5967
5968 output->dirty = 1;
5969
5970 /* Notify clients of the change for output transform. */
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005971 wl_list_for_each(head, &output->head_list, output_link) {
5972 wl_resource_for_each(resource, &head->resource_list) {
5973 wl_output_send_geometry(resource,
5974 output->x,
5975 output->y,
5976 head->mm_width,
5977 head->mm_height,
5978 head->subpixel,
5979 head->make,
5980 head->model,
5981 output->transform);
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005982
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03005983 ver = wl_resource_get_version(resource);
5984 if (ver >= WL_OUTPUT_DONE_SINCE_VERSION)
5985 wl_output_send_done(resource);
5986 }
Roman Gilge97391c2019-03-29 13:01:06 +01005987 wl_resource_for_each(resource, &head->xdg_output_resource_list) {
5988 zxdg_output_v1_send_logical_position(resource,
5989 output->x,
5990 output->y);
5991 zxdg_output_v1_send_logical_size(resource,
5992 output->width,
5993 output->height);
5994 zxdg_output_v1_send_done(resource);
5995 }
Ilia Bozhinov8564a0d2017-06-25 12:21:39 +00005996 }
5997
5998 /* we must ensure that pointers are inside output, otherwise they disappear */
5999 mid_x = output->x + output->width / 2;
6000 mid_y = output->y + output->height / 2;
6001
6002 ev.mask = WESTON_POINTER_MOTION_ABS;
6003 ev.x = wl_fixed_to_double(wl_fixed_from_int(mid_x));
6004 ev.y = wl_fixed_to_double(wl_fixed_from_int(mid_y));
6005
6006 wl_list_for_each(seat, &output->compositor->seat_list, link) {
6007 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
6008
6009 if (pointer && pixman_region32_contains_point(&old_region,
6010 wl_fixed_to_int(pointer->x),
6011 wl_fixed_to_int(pointer->y),
6012 NULL))
6013 weston_pointer_move(pointer, &ev);
6014 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006015}
6016
6017/** Initializes a weston_output object with enough data so
6018 ** an output can be configured.
6019 *
6020 * \param output The weston_output object to initialize
6021 * \param compositor The compositor instance.
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006022 * \param name Name for the output (the string is copied).
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006023 *
6024 * Sets initial values for fields that are expected to be
6025 * configured either by compositors or backends.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006026 *
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006027 * The name is used in logs, and can be used by compositors as a configuration
6028 * identifier.
6029 *
Marius Vlad55d87362019-06-11 01:15:35 +03006030 * \ingroup output
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006031 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006032 */
6033WL_EXPORT void
Armin Krezović40087402016-09-30 14:11:12 +02006034weston_output_init(struct weston_output *output,
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006035 struct weston_compositor *compositor,
6036 const char *name)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006037{
6038 output->compositor = compositor;
6039 output->destroying = 0;
Pekka Paalanen26ac2e12017-04-03 13:18:13 +03006040 output->name = strdup(name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006041 wl_list_init(&output->link);
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006042 wl_signal_init(&output->user_destroy_signal);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006043 output->enabled = false;
Ankit Nautiyal2690a772019-03-25 17:57:59 +05306044 output->desired_protection = WESTON_HDCP_DISABLE;
Ankit Nautiyal2844f8e2019-04-03 10:14:59 +05306045 output->allow_protection = true;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006046
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006047 wl_list_init(&output->head_list);
6048
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006049 /* Add some (in)sane defaults which can be used
6050 * for checking if an output was properly configured
6051 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006052 output->scale = 0;
6053 /* Can't use -1 on uint32_t and 0 is valid enum value */
6054 output->transform = UINT32_MAX;
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006055
6056 pixman_region32_init(&output->previous_damage);
6057 pixman_region32_init(&output->region);
Pekka Paalanen42704142017-09-06 16:47:52 +03006058 wl_list_init(&output->mode_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006059}
6060
6061/** Adds weston_output object to pending output list.
6062 *
6063 * \param output The weston_output object to add
6064 * \param compositor The compositor instance.
6065 *
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03006066 * The opposite of this operation is built into weston_output_release().
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006067 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006068 * \ingroup compositor
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006069 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006070 */
6071WL_EXPORT void
6072weston_compositor_add_pending_output(struct weston_output *output,
6073 struct weston_compositor *compositor)
6074{
Pekka Paalanene952a012017-03-29 17:14:00 +03006075 assert(output->disable);
6076 assert(output->enable);
6077
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006078 wl_list_remove(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006079 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006080}
6081
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006082/** Create a string with the attached heads' names.
6083 *
6084 * The string must be free()'d.
Marius Vlad55d87362019-06-11 01:15:35 +03006085 *
6086 * \ingroup output
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006087 */
6088static char *
6089weston_output_create_heads_string(struct weston_output *output)
6090{
6091 FILE *fp;
6092 char *str = NULL;
6093 size_t size = 0;
6094 struct weston_head *head;
6095 const char *sep = "";
6096
6097 fp = open_memstream(&str, &size);
6098 if (!fp)
6099 return NULL;
6100
6101 wl_list_for_each(head, &output->head_list, output_link) {
6102 fprintf(fp, "%s%s", sep, head->name);
6103 sep = ", ";
6104 }
6105 fclose(fp);
6106
6107 return str;
6108}
6109
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006110/** Constructs a weston_output object that can be used by the compositor.
6111 *
Pekka Paalanencc201e42017-03-30 15:11:25 +03006112 * \param output The weston_output object that needs to be enabled. Must not
Pekka Paalanenddce54d2017-08-23 16:00:21 +03006113 * be enabled already. Must have at least one head attached.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006114 *
6115 * Output coordinates are calculated and each new output is by default
6116 * assigned to the right of previous one.
6117 *
6118 * Sets up the transformation, zoom, and geometry of the output using
6119 * the properties that need to be configured by the compositor.
6120 *
6121 * Establishes a repaint timer for the output with the relevant display
6122 * object's event loop. See output_repaint_timer_handler().
6123 *
6124 * The output is assigned an ID. Weston can support up to 32 distinct
6125 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
6126 * is referred to and used to find the first available ID number, and
6127 * then this ID is marked as used in output_id_pool.
6128 *
6129 * The output is also assigned a Wayland global with the wl_output
6130 * external interface.
6131 *
6132 * Backend specific function is called to set up the output output.
6133 *
6134 * Output is added to the compositor's output list
6135 *
6136 * If the backend specific function fails, the weston_output object
6137 * is returned to a state it was before calling this function and
6138 * is added to the compositor's pending_output_list in case it needs
6139 * to be reconfigured or just so it can be destroyed at shutdown.
6140 *
6141 * 0 is returned on success, -1 on failure.
Marius Vlad55d87362019-06-11 01:15:35 +03006142 *
6143 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006144 */
6145WL_EXPORT int
6146weston_output_enable(struct weston_output *output)
6147{
Armin Krezović782f5df2016-09-30 14:11:11 +02006148 struct weston_compositor *c = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006149 struct weston_output *iterator;
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03006150 struct weston_head *head;
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006151 char *head_names;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006152 int x = 0, y = 0;
6153
Pekka Paalanencc201e42017-03-30 15:11:25 +03006154 if (output->enabled) {
6155 weston_log("Error: attempt to enable an enabled output '%s'\n",
6156 output->name);
6157 return -1;
6158 }
6159
Pekka Paalanenddce54d2017-08-23 16:00:21 +03006160 if (wl_list_empty(&output->head_list)) {
6161 weston_log("Error: cannot enable output '%s' without heads.\n",
6162 output->name);
6163 return -1;
6164 }
6165
Pekka Paalanen586e1ac2017-09-14 16:17:59 +03006166 if (wl_list_empty(&output->mode_list) || !output->current_mode) {
6167 weston_log("Error: no video mode for output '%s'.\n",
6168 output->name);
6169 return -1;
6170 }
6171
Pekka Paalanenec25b0a2017-08-24 16:08:49 +03006172 wl_list_for_each(head, &output->head_list, output_link) {
6173 assert(head->make);
6174 assert(head->model);
6175 }
6176
Armin Krezović782f5df2016-09-30 14:11:11 +02006177 iterator = container_of(c->output_list.prev,
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006178 struct weston_output, link);
6179
Armin Krezović782f5df2016-09-30 14:11:11 +02006180 if (!wl_list_empty(&c->output_list))
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006181 x = iterator->x + iterator->width;
6182
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006183 /* Make sure the scale is set up */
6184 assert(output->scale);
6185
6186 /* Make sure we have a transform set */
6187 assert(output->transform != UINT32_MAX);
6188
Armin Krezović782f5df2016-09-30 14:11:11 +02006189 output->x = x;
6190 output->y = y;
6191 output->dirty = 1;
6192 output->original_scale = output->scale;
6193
6194 weston_output_transform_scale_init(output, output->transform, output->scale);
6195 weston_output_init_zoom(output);
6196
6197 weston_output_init_geometry(output, x, y);
6198 weston_output_damage(output);
6199
6200 wl_signal_init(&output->frame_signal);
6201 wl_signal_init(&output->destroy_signal);
6202 wl_list_init(&output->animation_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02006203 wl_list_init(&output->feedback_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02006204
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006205 /* Enable the output (set up the crtc or create a
6206 * window representing the output, set up the
6207 * renderer, etc)
6208 */
6209 if (output->enable(output) < 0) {
6210 weston_log("Enabling output \"%s\" failed.\n", output->name);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006211 return -1;
6212 }
6213
6214 weston_compositor_add_output(output->compositor, output);
6215
Pekka Paalanen3e8f2012017-11-02 14:03:11 +02006216 head_names = weston_output_create_heads_string(output);
6217 weston_log("Output '%s' enabled with head(s) %s\n",
6218 output->name, head_names);
6219 free(head_names);
6220
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006221 return 0;
6222}
6223
6224/** Converts a weston_output object to a pending output state, so it
6225 ** can be configured again or destroyed.
6226 *
6227 * \param output The weston_output object that needs to be disabled.
6228 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006229 * Calls a backend specific function to disable an output, in case
6230 * such function exists.
6231 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03006232 * The backend specific disable function may choose to postpone the disabling
6233 * by returning a negative value, in which case this function returns early.
6234 * In that case the backend will guarantee the output will be disabled soon
6235 * by the backend calling this function again. One must not attempt to re-enable
6236 * the output until that happens.
6237 *
6238 * Otherwise, if the output is being used by the compositor, it is removed
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006239 * from weston's output_list (see weston_compositor_remove_output())
6240 * and is returned to a state it was before weston_output_enable()
6241 * was ran (see weston_output_enable_undo()).
6242 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03006243 * See weston_output_init() for more information on the
6244 * state output is returned to.
Pekka Paalanencc201e42017-03-30 15:11:25 +03006245 *
6246 * If the output has never been enabled yet, this function can still be
6247 * called to ensure that the output is actually turned off rather than left
6248 * in the state it was discovered in.
Marius Vlad55d87362019-06-11 01:15:35 +03006249 *
6250 * \ingroup output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006251 */
6252WL_EXPORT void
6253weston_output_disable(struct weston_output *output)
6254{
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006255 /* Should we rename this? */
6256 output->destroying = 1;
6257
Pekka Paalanenc65df642017-03-29 15:45:46 +03006258 /* Disable is called unconditionally also for not-enabled outputs,
6259 * because at compositor start-up, if there is an output that is
6260 * already on but the compositor wants to turn it off, we have to
6261 * forward the turn-off to the backend so it knows to do it.
6262 * The backend cannot initially turn off everything, because it
6263 * would cause unnecessary mode-sets for all outputs the compositor
6264 * wants to be on.
6265 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006266 if (output->disable(output) < 0)
6267 return;
6268
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006269 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006270 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006271
6272 output->destroying = 0;
6273}
6274
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006275/** Forces a synchronous call to heads_changed hook
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006276 *
6277 * \param compositor The compositor instance
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006278 *
6279 * If there are new or changed heads, calls the heads_changed hook and
6280 * returns after the hook returns.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006281 *
6282 * \ingroup compositor
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006283 */
6284WL_EXPORT void
Pekka Paalanen8a8dcac2017-08-17 17:29:36 +03006285weston_compositor_flush_heads_changed(struct weston_compositor *compositor)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006286{
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03006287 if (compositor->heads_changed_source) {
6288 wl_event_source_remove(compositor->heads_changed_source);
6289 weston_compositor_call_heads_changed(compositor);
6290 }
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006291}
6292
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006293/** Add destroy callback for an output
6294 *
6295 * \param output The output to watch.
6296 * \param listener The listener to add. The \c notify member must be set.
6297 *
6298 * The listener callback will be called when user destroys an output. This
6299 * may be delayed by a backend in some cases. The main purpose of the
6300 * listener is to allow hooking up custom data to the output. The custom data
6301 * can be fetched via weston_output_get_destroy_listener() followed by
6302 * container_of().
6303 *
6304 * The \c data argument to the notify callback is the weston_output being
6305 * destroyed.
6306 *
6307 * @note This is for the final destruction of an output, not when it gets
6308 * disabled. If you want to keep track of enabled outputs, this is not it.
Marius Vlad55d87362019-06-11 01:15:35 +03006309 *
6310 * \ingroup ouput
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006311 */
6312WL_EXPORT void
6313weston_output_add_destroy_listener(struct weston_output *output,
6314 struct wl_listener *listener)
6315{
6316 wl_signal_add(&output->user_destroy_signal, listener);
6317}
6318
6319/** Look up destroy listener for an output
6320 *
6321 * \param output The output to query.
6322 * \param notify The notify function used used for the added destroy listener.
6323 * \return The listener, or NULL if not found.
6324 *
6325 * This looks up the previously added destroy listener struct based on the
6326 * notify function it has. The listener can be used to access user data
6327 * through \c container_of().
6328 *
6329 * \sa wl_signal_get() weston_output_add_destroy_listener()
Marius Vlad55d87362019-06-11 01:15:35 +03006330 * \ingroup output
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006331 */
6332WL_EXPORT struct wl_listener *
6333weston_output_get_destroy_listener(struct weston_output *output,
6334 wl_notify_func_t notify)
6335{
6336 return wl_signal_get(&output->user_destroy_signal, notify);
6337}
6338
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006339/** Uninitialize an output
6340 *
6341 * Removes the output from the list of enabled outputs if necessary, but
6342 * does not call the backend's output disable function. The output will no
6343 * longer be in the list of pending outputs either.
6344 *
6345 * All fields of weston_output become uninitialized, i.e. should not be used
6346 * anymore. The caller can free the memory after this.
6347 *
Marius Vlad55d87362019-06-11 01:15:35 +03006348 * \ingroup ouput
Pekka Paalanenee16ea92017-03-29 16:53:50 +03006349 * \internal
6350 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006351WL_EXPORT void
Pekka Paalanenae6d35d2017-08-16 12:07:14 +03006352weston_output_release(struct weston_output *output)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006353{
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006354 struct weston_head *head, *tmp;
6355
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006356 output->destroying = 1;
6357
Pekka Paalanen37b7c6e2017-11-07 10:15:01 +02006358 wl_signal_emit(&output->user_destroy_signal, output);
6359
Pekka Paalanendcbcfc72017-10-26 14:33:59 +03006360 if (output->idle_repaint_source)
6361 wl_event_source_remove(output->idle_repaint_source);
6362
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006363 if (output->enabled)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006364 weston_compositor_remove_output(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006365
Pekka Paalanen4b582c72017-03-30 16:04:58 +03006366 pixman_region32_fini(&output->region);
6367 pixman_region32_fini(&output->previous_damage);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03006368 wl_list_remove(&output->link);
Pekka Paalanen7cdbabe2017-03-28 16:27:25 +03006369
6370 wl_list_for_each_safe(head, tmp, &output->head_list, output_link)
6371 weston_head_detach(head);
6372
Armin Krezovića01ab6d2016-09-30 14:11:02 +02006373 free(output->name);
6374}
6375
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006376/** Find an output by its given name
6377 *
6378 * \param compositor The compositor to search in.
6379 * \param name The output name to search for.
6380 * \return An existing output with the given name, or NULL if not found.
6381 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006382 * \ingroup compositor
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006383 */
6384WL_EXPORT struct weston_output *
6385weston_compositor_find_output_by_name(struct weston_compositor *compositor,
6386 const char *name)
6387{
6388 struct weston_output *output;
6389
6390 wl_list_for_each(output, &compositor->output_list, link)
6391 if (strcmp(output->name, name) == 0)
6392 return output;
6393
6394 wl_list_for_each(output, &compositor->pending_output_list, link)
6395 if (strcmp(output->name, name) == 0)
6396 return output;
6397
6398 return NULL;
6399}
6400
6401/** Create a named output
6402 *
6403 * \param compositor The compositor.
6404 * \param name The name for the output.
6405 * \return A new \c weston_output, or NULL on failure.
6406 *
6407 * This creates a new weston_output that starts with no heads attached.
6408 *
6409 * An output must be configured and it must have at least one head before
6410 * it can be enabled.
6411 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006412 * \ingroup compositor
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006413 */
6414WL_EXPORT struct weston_output *
6415weston_compositor_create_output(struct weston_compositor *compositor,
6416 const char *name)
6417{
6418 assert(compositor->backend->create_output);
6419
6420 if (weston_compositor_find_output_by_name(compositor, name)) {
6421 weston_log("Warning: attempted to create an output with a "
6422 "duplicate name '%s'.\n", name);
6423 return NULL;
6424 }
6425
6426 return compositor->backend->create_output(compositor, name);
6427}
6428
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006429/** Create an output for an unused head
6430 *
6431 * \param compositor The compositor.
6432 * \param head The head to attach to the output.
6433 * \return A new \c weston_output, or NULL on failure.
6434 *
6435 * This creates a new weston_output that starts with the given head attached.
6436 * The output inherits the name of the head. The head must not be already
6437 * attached to another output.
6438 *
6439 * An output must be configured before it can be enabled.
6440 *
Marius Vlad9fdda7f2019-06-11 16:08:55 +03006441 * \ingroup compositor
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006442 */
6443WL_EXPORT struct weston_output *
6444weston_compositor_create_output_with_head(struct weston_compositor *compositor,
6445 struct weston_head *head)
6446{
6447 struct weston_output *output;
6448
Pekka Paalanen1ae9d082017-11-02 14:11:53 +02006449 output = weston_compositor_create_output(compositor, head->name);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006450 if (!output)
6451 return NULL;
6452
6453 if (weston_output_attach_head(output, head) < 0) {
Pekka Paalanen42c0e142017-10-27 12:07:49 +03006454 weston_output_destroy(output);
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006455 return NULL;
6456 }
6457
6458 return output;
6459}
6460
6461/** Destroy an output
6462 *
6463 * \param output The output to destroy.
6464 *
6465 * The heads attached to the given output are detached and become unused again.
6466 *
6467 * It is not necessary to explicitly destroy all outputs at compositor exit.
6468 * weston_compositor_destroy() will automatically destroy any remaining
6469 * outputs.
6470 *
Marius Vlad55d87362019-06-11 01:15:35 +03006471 * \ingroup ouput
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006472 */
6473WL_EXPORT void
6474weston_output_destroy(struct weston_output *output)
6475{
Pekka Paalanen992a8cb2017-08-16 10:39:17 +03006476 output->destroy(output);
6477}
6478
Pekka Paalanencf0a4762017-04-04 16:36:07 +03006479/** When you need a head...
6480 *
6481 * This function is a hack, used until all code has been converted to become
6482 * multi-head aware.
6483 *
6484 * \param output The weston_output whose head to get.
6485 * \return The first head in the output's list.
Marius Vlad55d87362019-06-11 01:15:35 +03006486 *
6487 * \ingroup ouput
Pekka Paalanencf0a4762017-04-04 16:36:07 +03006488 */
6489WL_EXPORT struct weston_head *
6490weston_output_get_first_head(struct weston_output *output)
6491{
6492 if (wl_list_empty(&output->head_list))
6493 return NULL;
6494
6495 return container_of(output->head_list.next,
6496 struct weston_head, output_link);
6497}
6498
Ankit Nautiyal2844f8e2019-04-03 10:14:59 +05306499/** Allow/Disallow content-protection support for an output
6500 *
6501 * This function sets the allow_protection member for an output. Setting of
6502 * this field will allow the compositor to attempt content-protection for this
6503 * output, for a backend that supports the content-protection protocol.
6504 *
6505 * \param output The weston_output for whom the content-protection is to be
6506 * allowed.
6507 * \param allow_protection The bool value which is to be set.
6508 */
6509WL_EXPORT void
6510weston_output_allow_protection(struct weston_output *output,
6511 bool allow_protection)
6512{
6513 output->allow_protection = allow_protection;
6514}
6515
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01006516static void
Roman Gilge97391c2019-03-29 13:01:06 +01006517xdg_output_unlist(struct wl_resource *resource)
6518{
6519 wl_list_remove(wl_resource_get_link(resource));
6520}
6521
6522static void
6523xdg_output_destroy(struct wl_client *client, struct wl_resource *resource)
6524{
6525 wl_resource_destroy(resource);
6526}
6527
6528static const struct zxdg_output_v1_interface xdg_output_interface = {
6529 xdg_output_destroy
6530};
6531
6532static void
6533xdg_output_manager_destroy(struct wl_client *client,
6534 struct wl_resource *resource)
6535{
6536 wl_resource_destroy(resource);
6537}
6538
6539static void
6540xdg_output_manager_get_xdg_output(struct wl_client *client,
6541 struct wl_resource *manager,
6542 uint32_t id,
6543 struct wl_resource *output_resource)
6544{
6545 int version = wl_resource_get_version(manager);
6546 struct weston_head *head = wl_resource_get_user_data(output_resource);
6547 struct weston_output *output = head->output;
6548 struct wl_resource *resource;
6549
6550 resource = wl_resource_create(client, &zxdg_output_v1_interface,
6551 version, id);
6552 if (resource == NULL) {
6553 wl_client_post_no_memory(client);
6554 return;
6555 }
6556
6557 wl_list_insert(&head->xdg_output_resource_list,
6558 wl_resource_get_link(resource));
6559
6560 wl_resource_set_implementation(resource, &xdg_output_interface,
6561 NULL, xdg_output_unlist);
6562
6563 zxdg_output_v1_send_logical_position(resource, output->x, output->y);
6564 zxdg_output_v1_send_logical_size(resource,
6565 output->width,
6566 output->height);
6567 if (version >= ZXDG_OUTPUT_V1_NAME_SINCE_VERSION)
6568 zxdg_output_v1_send_name(resource, head->name);
6569
6570 zxdg_output_v1_send_done(resource);
6571}
6572
6573static const struct zxdg_output_manager_v1_interface xdg_output_manager_interface = {
6574 xdg_output_manager_destroy,
6575 xdg_output_manager_get_xdg_output
6576};
6577
6578static void
6579bind_xdg_output_manager(struct wl_client *client,
6580 void *data, uint32_t version, uint32_t id)
6581{
6582 struct wl_resource *resource;
6583
6584 resource = wl_resource_create(client, &zxdg_output_manager_v1_interface,
6585 version, id);
6586 if (resource == NULL) {
6587 wl_client_post_no_memory(client);
6588 return;
6589 }
6590
6591 wl_resource_set_implementation(resource, &xdg_output_manager_interface,
6592 NULL, NULL);
6593}
6594
6595static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006596destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006597{
Jonny Lamb74130762013-11-26 18:19:46 +01006598 struct weston_surface *surface =
6599 wl_resource_get_user_data(resource);
6600
Pekka Paalanen4826f872016-04-22 14:14:38 +03006601 if (!surface)
6602 return;
6603
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006604 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02006605 surface->pending.buffer_viewport.buffer.src_width =
6606 wl_fixed_from_int(-1);
6607 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006608 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006609}
6610
6611static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006612viewport_destroy(struct wl_client *client,
6613 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006614{
6615 wl_resource_destroy(resource);
6616}
6617
6618static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006619viewport_set_source(struct wl_client *client,
6620 struct wl_resource *resource,
6621 wl_fixed_t src_x,
6622 wl_fixed_t src_y,
6623 wl_fixed_t src_width,
6624 wl_fixed_t src_height)
6625{
6626 struct weston_surface *surface =
6627 wl_resource_get_user_data(resource);
6628
Pekka Paalanen4826f872016-04-22 14:14:38 +03006629 if (!surface) {
6630 wl_resource_post_error(resource,
6631 WP_VIEWPORT_ERROR_NO_SURFACE,
6632 "wl_surface for this viewport is no longer exists");
6633 return;
6634 }
6635
6636 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03006637 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006638
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006639 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03006640 src_height == wl_fixed_from_int(-1) &&
6641 src_x == wl_fixed_from_int(-1) &&
6642 src_y == wl_fixed_from_int(-1)) {
6643 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006644 surface->pending.buffer_viewport.buffer.src_width =
6645 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006646 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006647 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006648 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006649
Pekka Paalanen201769a2016-04-26 14:42:11 +03006650 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006651 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006652 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03006653 "wl_surface@%d viewport source "
6654 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
6655 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006656 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03006657 wl_fixed_to_double(src_height),
6658 wl_fixed_to_double(src_x),
6659 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006660 return;
6661 }
6662
6663 surface->pending.buffer_viewport.buffer.src_x = src_x;
6664 surface->pending.buffer_viewport.buffer.src_y = src_y;
6665 surface->pending.buffer_viewport.buffer.src_width = src_width;
6666 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006667 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006668}
6669
6670static void
6671viewport_set_destination(struct wl_client *client,
6672 struct wl_resource *resource,
6673 int32_t dst_width,
6674 int32_t dst_height)
6675{
6676 struct weston_surface *surface =
6677 wl_resource_get_user_data(resource);
6678
Pekka Paalanen4826f872016-04-22 14:14:38 +03006679 if (!surface) {
6680 wl_resource_post_error(resource,
6681 WP_VIEWPORT_ERROR_NO_SURFACE,
6682 "wl_surface for this viewport no longer exists");
6683 return;
6684 }
6685
6686 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006687
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006688 if (dst_width == -1 && dst_height == -1) {
6689 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006690 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006691 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006692 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006693 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006694
6695 if (dst_width <= 0 || dst_height <= 0) {
6696 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006697 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03006698 "destination size must be positive (%dx%d)",
6699 dst_width, dst_height);
6700 return;
6701 }
6702
6703 surface->pending.buffer_viewport.surface.width = dst_width;
6704 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02006705 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006706}
6707
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006708static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006709 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006710 viewport_set_source,
6711 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01006712};
6713
6714static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006715viewporter_destroy(struct wl_client *client,
6716 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006717{
6718 wl_resource_destroy(resource);
6719}
6720
6721static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006722viewporter_get_viewport(struct wl_client *client,
6723 struct wl_resource *viewporter,
6724 uint32_t id,
6725 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006726{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006727 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006728 struct weston_surface *surface =
6729 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006730 struct wl_resource *resource;
6731
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006732 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006733 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006734 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006735 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01006736 return;
6737 }
6738
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006739 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02006740 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006741 if (resource == NULL) {
6742 wl_client_post_no_memory(client);
6743 return;
6744 }
6745
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006746 wl_resource_set_implementation(resource, &viewport_interface,
6747 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01006748
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02006749 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01006750}
6751
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006752static const struct wp_viewporter_interface viewporter_interface = {
6753 viewporter_destroy,
6754 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01006755};
6756
6757static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006758bind_viewporter(struct wl_client *client,
6759 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01006760{
6761 struct wl_resource *resource;
6762
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03006763 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006764 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01006765 if (resource == NULL) {
6766 wl_client_post_no_memory(client);
6767 return;
6768 }
6769
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03006770 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01006771 NULL, NULL);
6772}
6773
6774static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04006775destroy_presentation_feedback(struct wl_resource *feedback_resource)
6776{
6777 struct weston_presentation_feedback *feedback;
6778
6779 feedback = wl_resource_get_user_data(feedback_resource);
6780
6781 wl_list_remove(&feedback->link);
6782 free(feedback);
6783}
6784
6785static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006786presentation_destroy(struct wl_client *client, struct wl_resource *resource)
6787{
6788 wl_resource_destroy(resource);
6789}
6790
6791static void
6792presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04006793 struct wl_resource *presentation_resource,
6794 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006795 uint32_t callback)
6796{
Pekka Paalanen133e4392014-09-23 22:08:46 -04006797 struct weston_surface *surface;
6798 struct weston_presentation_feedback *feedback;
6799
6800 surface = wl_resource_get_user_data(surface_resource);
6801
Bryce Harringtonde16d892014-11-20 22:21:57 -08006802 feedback = zalloc(sizeof *feedback);
6803 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04006804 goto err_calloc;
6805
6806 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006807 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04006808 1, callback);
6809 if (!feedback->resource)
6810 goto err_create;
6811
6812 wl_resource_set_implementation(feedback->resource, NULL, feedback,
6813 destroy_presentation_feedback);
6814
6815 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
6816
6817 return;
6818
6819err_create:
6820 free(feedback);
6821
6822err_calloc:
6823 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006824}
6825
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006826static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006827 presentation_destroy,
6828 presentation_feedback
6829};
6830
6831static void
6832bind_presentation(struct wl_client *client,
6833 void *data, uint32_t version, uint32_t id)
6834{
6835 struct weston_compositor *compositor = data;
6836 struct wl_resource *resource;
6837
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006838 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006839 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006840 if (resource == NULL) {
6841 wl_client_post_no_memory(client);
6842 return;
6843 }
6844
6845 wl_resource_set_implementation(resource, &presentation_implementation,
6846 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02006847 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04006848}
6849
6850static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05006851compositor_bind(struct wl_client *client,
6852 void *data, uint32_t version, uint32_t id)
6853{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05006854 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05006855 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05006856
Jason Ekstranda85118c2013-06-27 20:17:02 -05006857 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06006858 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07006859 if (resource == NULL) {
6860 wl_client_post_no_memory(client);
6861 return;
6862 }
6863
6864 wl_resource_set_implementation(resource, &compositor_interface,
6865 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05006866}
6867
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006868WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006869weston_environment_get_fd(const char *env)
6870{
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07006871 char *e;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006872 int fd, flags;
6873
6874 e = getenv(env);
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07006875 if (!e || !safe_strtoint(e, &fd))
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02006876 return -1;
6877
6878 flags = fcntl(fd, F_GETFD);
6879 if (flags == -1)
6880 return -1;
6881
6882 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
6883 unsetenv(env);
6884
6885 return fd;
6886}
6887
Pekka Paalanenb5026542014-11-12 15:09:24 +02006888static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02006889timeline_key_binding_handler(struct weston_keyboard *keyboard,
6890 const struct timespec *time, uint32_t key,
6891 void *data)
Pekka Paalanenb5026542014-11-12 15:09:24 +02006892{
6893 struct weston_compositor *compositor = data;
6894
6895 if (weston_timeline_enabled_)
6896 weston_timeline_close();
6897 else
6898 weston_timeline_open(compositor);
6899}
6900
Daniel Stonece62cb32018-07-20 09:46:24 +01006901static const char *
6902output_repaint_status_text(struct weston_output *output)
6903{
6904 switch (output->repaint_status) {
6905 case REPAINT_NOT_SCHEDULED:
6906 return "no repaint";
6907 case REPAINT_BEGIN_FROM_IDLE:
6908 return "start_repaint_loop scheduled";
6909 case REPAINT_SCHEDULED:
6910 return "repaint scheduled";
6911 case REPAINT_AWAITING_COMPLETION:
6912 return "awaiting completion";
6913 }
6914
6915 assert(!"output_repaint_status_text missing enum");
6916 return NULL;
6917}
6918
6919static void
6920debug_scene_view_print_buffer(FILE *fp, struct weston_view *view)
6921{
6922 struct weston_buffer *buffer = view->surface->buffer_ref.buffer;
6923 struct wl_shm_buffer *shm;
6924 struct linux_dmabuf_buffer *dmabuf;
Marius Vlad00a6e012018-11-20 17:52:31 +02006925 const struct pixel_format_info *pixel_info = NULL;
Daniel Stonece62cb32018-07-20 09:46:24 +01006926
6927 if (!buffer) {
6928 fprintf(fp, "\t\t[buffer not available]\n");
6929 return;
6930 }
6931
6932 shm = wl_shm_buffer_get(buffer->resource);
6933 if (shm) {
Marius Vlad00a6e012018-11-20 17:52:31 +02006934 uint32_t _format = wl_shm_buffer_get_format(shm);
6935 pixel_info = pixel_format_get_info_shm(_format);
Daniel Stonece62cb32018-07-20 09:46:24 +01006936 fprintf(fp, "\t\tSHM buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02006937 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
6938 (unsigned long) _format,
6939 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Daniel Stonece62cb32018-07-20 09:46:24 +01006940 return;
6941 }
6942
6943 dmabuf = linux_dmabuf_buffer_get(buffer->resource);
6944 if (dmabuf) {
Marius Vlad00a6e012018-11-20 17:52:31 +02006945 pixel_info = pixel_format_get_info(dmabuf->attributes.format);
Daniel Stonece62cb32018-07-20 09:46:24 +01006946 fprintf(fp, "\t\tdmabuf buffer\n");
Marius Vlad00a6e012018-11-20 17:52:31 +02006947 fprintf(fp, "\t\t\tformat: 0x%lx %s\n",
6948 (unsigned long) dmabuf->attributes.format,
6949 pixel_info ? pixel_info->drm_format_name : "UNKNOWN");
Daniel Stonece62cb32018-07-20 09:46:24 +01006950 fprintf(fp, "\t\t\tmodifier: 0x%llx\n",
6951 (unsigned long long) dmabuf->attributes.modifier[0]);
6952 return;
6953 }
6954
Marius Vlad253ba9a2019-03-17 18:22:21 +02006955 fprintf(fp, "\t\tEGL buffer\n");
Daniel Stonece62cb32018-07-20 09:46:24 +01006956}
6957
6958static void
6959debug_scene_view_print(FILE *fp, struct weston_view *view, int view_idx)
6960{
6961 struct weston_compositor *ec = view->surface->compositor;
6962 struct weston_output *output;
6963 char desc[512];
6964 pixman_box32_t *box;
6965 uint32_t surface_id = 0;
6966 pid_t pid = 0;
6967
6968 if (view->surface->resource) {
6969 struct wl_resource *resource = view->surface->resource;
6970 wl_client_get_credentials(wl_resource_get_client(resource),
6971 &pid, NULL, NULL);
6972 surface_id = wl_resource_get_id(view->surface->resource);
6973 }
6974
6975 if (!view->surface->get_label ||
6976 view->surface->get_label(view->surface, desc, sizeof(desc)) < 0) {
6977 strcpy(desc, "[no description available]");
6978 }
6979 fprintf(fp, "\tView %d (role %s, PID %d, surface ID %u, %s, %p):\n",
6980 view_idx, view->surface->role_name, pid, surface_id,
6981 desc, view);
6982
6983 box = pixman_region32_extents(&view->transform.boundingbox);
6984 fprintf(fp, "\t\tposition: (%d, %d) -> (%d, %d)\n",
6985 box->x1, box->y1, box->x2, box->y2);
6986 box = pixman_region32_extents(&view->transform.opaque);
6987
6988 if (pixman_region32_equal(&view->transform.opaque,
6989 &view->transform.boundingbox)) {
6990 fprintf(fp, "\t\t[fully opaque]\n");
6991 } else if (!pixman_region32_not_empty(&view->transform.opaque)) {
6992 fprintf(fp, "\t\t[not opaque]\n");
6993 } else {
6994 fprintf(fp, "\t\t[opaque: (%d, %d) -> (%d, %d)]\n",
6995 box->x1, box->y1, box->x2, box->y2);
6996 }
6997
6998 if (view->alpha < 1.0)
6999 fprintf(fp, "\t\talpha: %f\n", view->alpha);
7000
7001 if (view->output_mask != 0) {
7002 bool first_output = true;
7003 fprintf(fp, "\t\toutputs: ");
7004 wl_list_for_each(output, &ec->output_list, link) {
7005 if (!(view->output_mask & (1 << output->id)))
7006 continue;
7007 fprintf(fp, "%s%d (%s)%s",
7008 (first_output) ? "" : ", ",
7009 output->id, output->name,
7010 (view->output == output) ? " (primary)" : "");
7011 first_output = false;
7012 }
7013 } else {
7014 fprintf(fp, "\t\t[no outputs]");
7015 }
7016
7017 fprintf(fp, "\n");
7018
7019 debug_scene_view_print_buffer(fp, view);
7020}
7021
Marius Vlad433f4e72019-02-17 22:14:23 +02007022static void
7023debug_scene_view_print_tree(struct weston_view *view,
Marius Vlada6acfa82019-03-17 18:10:09 +02007024 FILE *fp, int *view_idx)
Marius Vlad433f4e72019-02-17 22:14:23 +02007025{
7026 struct weston_subsurface *sub;
7027 struct weston_view *ev;
7028
7029 /*
7030 * print the view first, then we recursively go on printing
7031 * sub-surfaces. We bail out once no more sub-surfaces are available.
7032 */
Marius Vlada6acfa82019-03-17 18:10:09 +02007033 debug_scene_view_print(fp, view, *view_idx);
Marius Vlad433f4e72019-02-17 22:14:23 +02007034
7035 /* no more sub-surfaces */
7036 if (wl_list_empty(&view->surface->subsurface_list))
7037 return;
7038
7039 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
7040 wl_list_for_each(ev, &sub->surface->views, surface_link) {
7041 /* do not print again the parent view */
7042 if (view == ev)
7043 continue;
Marius Vlada6acfa82019-03-17 18:10:09 +02007044
7045 (*view_idx)++;
Marius Vlad433f4e72019-02-17 22:14:23 +02007046 debug_scene_view_print_tree(ev, fp, view_idx);
7047 }
7048 }
7049}
7050
Daniel Stonece62cb32018-07-20 09:46:24 +01007051/**
7052 * Output information on how libweston is currently composing the scene
7053 * graph.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007054 *
7055 * \ingroup compositor
Daniel Stonece62cb32018-07-20 09:46:24 +01007056 */
7057WL_EXPORT char *
7058weston_compositor_print_scene_graph(struct weston_compositor *ec)
7059{
7060 struct weston_output *output;
7061 struct weston_layer *layer;
7062 struct timespec now;
7063 int layer_idx = 0;
7064 FILE *fp;
7065 char *ret;
7066 size_t len;
7067 int err;
7068
7069 fp = open_memstream(&ret, &len);
7070 assert(fp);
7071
7072 weston_compositor_read_presentation_clock(ec, &now);
7073 fprintf(fp, "Weston scene graph at %ld.%09ld:\n\n",
7074 now.tv_sec, now.tv_nsec);
7075
7076 wl_list_for_each(output, &ec->output_list, link) {
7077 struct weston_head *head;
7078 int head_idx = 0;
7079
7080 fprintf(fp, "Output %d (%s):\n", output->id, output->name);
7081 assert(output->enabled);
7082
7083 fprintf(fp, "\tposition: (%d, %d) -> (%d, %d)\n",
7084 output->x, output->y,
7085 output->x + output->width,
7086 output->y + output->height);
7087 fprintf(fp, "\tmode: %dx%d@%.3fHz\n",
7088 output->current_mode->width,
7089 output->current_mode->height,
7090 output->current_mode->refresh / 1000.0);
7091 fprintf(fp, "\tscale: %d\n", output->scale);
7092
7093 fprintf(fp, "\trepaint status: %s\n",
7094 output_repaint_status_text(output));
7095 if (output->repaint_status == REPAINT_SCHEDULED)
7096 fprintf(fp, "\tnext repaint: %ld.%09ld\n",
7097 output->next_repaint.tv_sec,
7098 output->next_repaint.tv_nsec);
7099
7100 wl_list_for_each(head, &output->head_list, output_link) {
7101 fprintf(fp, "\tHead %d (%s): %sconnected\n",
7102 head_idx++, head->name,
7103 (head->connected) ? "" : "not ");
7104 }
7105 }
7106
7107 fprintf(fp, "\n");
7108
7109 wl_list_for_each(layer, &ec->layer_list, link) {
7110 struct weston_view *view;
7111 int view_idx = 0;
7112
7113 fprintf(fp, "Layer %d (pos 0x%lx):\n", layer_idx++,
7114 (unsigned long) layer->position);
7115
7116 if (!weston_layer_mask_is_infinite(layer)) {
7117 fprintf(fp, "\t[mask: (%d, %d) -> (%d,%d)]\n\n",
7118 layer->mask.x1, layer->mask.y1,
7119 layer->mask.x2, layer->mask.y2);
7120 }
7121
Marius Vlada6acfa82019-03-17 18:10:09 +02007122 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
7123 debug_scene_view_print_tree(view, fp, &view_idx);
7124 view_idx++;
7125 }
Daniel Stonece62cb32018-07-20 09:46:24 +01007126
7127 if (wl_list_empty(&layer->view_list.link))
7128 fprintf(fp, "\t[no views]\n");
7129
7130 fprintf(fp, "\n");
7131 }
7132
7133 err = fclose(fp);
7134 assert(err == 0);
7135
7136 return ret;
7137}
7138
7139/**
7140 * Called when the 'scene-graph' debug scope is bound by a client. This
7141 * one-shot weston-debug scope prints the current scene graph when bound,
7142 * and then terminates the stream.
7143 */
7144static void
7145debug_scene_graph_cb(struct weston_debug_stream *stream, void *data)
7146{
7147 struct weston_compositor *ec = data;
7148 char *str = weston_compositor_print_scene_graph(ec);
7149
7150 weston_debug_stream_printf(stream, "%s", str);
7151 free(str);
7152 weston_debug_stream_complete(stream);
7153}
7154
Giulio Camuffo459137b2014-10-11 23:56:24 +03007155/** Create the compositor.
7156 *
7157 * This functions creates and initializes a compositor instance.
7158 *
7159 * \param display The Wayland display to be used.
7160 * \param user_data A pointer to an object that can later be retrieved
Marius Vlada2dace22019-06-12 16:05:44 +03007161 * \param log_ctx A pointer to weston_debug_compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007162 * using the \ref weston_compositor_get_user_data function.
7163 * \return The compositor instance on success or NULL on failure.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007164 *
7165 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007166 */
7167WL_EXPORT struct weston_compositor *
Marius Vlad880b4852019-04-07 17:07:58 +03007168weston_compositor_create(struct wl_display *display,
Marius Vlad3d7d9782019-04-17 12:35:38 +03007169 struct weston_log_context *log_ctx,
Marius Vlad880b4852019-04-07 17:07:58 +03007170 void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04007171{
Giulio Camuffo459137b2014-10-11 23:56:24 +03007172 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05007173 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07007174
Giulio Camuffo459137b2014-10-11 23:56:24 +03007175 ec = zalloc(sizeof *ec);
7176 if (!ec)
7177 return NULL;
7178
7179 ec->wl_display = display;
7180 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04007181 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07007182 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04007183 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03007184 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03007185 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02007186 wl_signal_init(&ec->idle_signal);
7187 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02007188 wl_signal_init(&ec->show_input_panel_signal);
7189 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02007190 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01007191 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01007192 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02007193 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02007194 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02007195 wl_signal_init(&ec->output_resized_signal);
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03007196 wl_signal_init(&ec->heads_changed_signal);
Pekka Paalanen8dc6db82018-03-20 13:29:40 +02007197 wl_signal_init(&ec->output_heads_changed_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07007198 wl_signal_init(&ec->session_signal);
Robert Beckettc569bdc2019-07-10 16:40:12 +01007199 ec->session_active = true;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04007200
Casey Dahlin58ba1372012-04-19 22:50:08 -04007201 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03007202 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04007203
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02007204 ec->activate_serial = 1;
7205
Louis-Francis Ratté-Bouliannec4689ff2017-11-28 20:42:47 -05007206 ec->touch_mode = WESTON_TOUCH_MODE_NORMAL;
7207
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05307208 ec->content_protection = NULL;
7209
Derek Foreman152254b2015-11-26 14:17:48 -06007210 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04007211 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007212 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05007213
Giulio Camuffo954f1832014-10-11 18:27:30 +03007214 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04007215 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007216 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03007217
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03007218 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03007219 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007220 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01007221
Roman Gilge97391c2019-03-29 13:01:06 +01007222 if (!wl_global_create(ec->wl_display, &zxdg_output_manager_v1_interface, 2,
7223 ec, bind_xdg_output_manager))
7224 goto fail;
7225
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02007226 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007227 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03007228 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04007229
Marius Vlad3d7d9782019-04-17 12:35:38 +03007230 if (weston_log_ctx_compositor_setup(ec, log_ctx) < 0)
Pekka Paalanena5630ea2017-10-12 13:13:42 +02007231 goto fail;
7232
Jonas Ådahl30d61d82014-10-22 21:21:17 +02007233 if (weston_input_init(ec) != 0)
7234 goto fail;
7235
Jason Ekstranda7af7042013-10-12 22:38:11 -05007236 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02007237 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007238 wl_list_init(&ec->layer_list);
7239 wl_list_init(&ec->seat_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007240 wl_list_init(&ec->pending_output_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007241 wl_list_init(&ec->output_list);
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03007242 wl_list_init(&ec->head_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007243 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01007244 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007245 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01007246 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007247 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02007248 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01007249
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007250 wl_list_init(&ec->plugin_api_list);
7251
Xiong Zhang97116532013-10-23 13:58:31 +08007252 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02007253 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04007254
Giulio Camuffo459137b2014-10-11 23:56:24 +03007255 wl_data_device_manager_init(ec->wl_display);
7256
7257 wl_display_init_shm(ec->wl_display);
7258
7259 loop = wl_display_get_event_loop(ec->wl_display);
7260 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Daniel Stone6847b852017-03-01 11:34:08 +00007261 ec->repaint_timer =
7262 wl_event_loop_add_timer(loop, output_repaint_timer_handler,
7263 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007264
Quentin Glidic82681572016-12-17 13:40:51 +01007265 weston_layer_init(&ec->fade_layer, ec);
7266 weston_layer_init(&ec->cursor_layer, ec);
7267
7268 weston_layer_set_position(&ec->fade_layer, WESTON_LAYER_POSITION_FADE);
7269 weston_layer_set_position(&ec->cursor_layer,
7270 WESTON_LAYER_POSITION_CURSOR);
Giulio Camuffo459137b2014-10-11 23:56:24 +03007271
7272 weston_compositor_add_debug_binding(ec, KEY_T,
7273 timeline_key_binding_handler, ec);
7274
Daniel Stonece62cb32018-07-20 09:46:24 +01007275 ec->debug_scene =
Marius Vlad7e4db952019-04-17 13:47:06 +03007276 weston_compositor_add_log_scope(ec->weston_log_ctx, "scene-graph",
7277 "Scene graph details\n",
7278 debug_scene_graph_cb,
7279 ec);
Daniel Stonece62cb32018-07-20 09:46:24 +01007280
Giulio Camuffo459137b2014-10-11 23:56:24 +03007281 return ec;
7282
7283fail:
7284 free(ec);
7285 return NULL;
7286}
7287
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007288/** weston_compositor_shutdown
7289 * \ingroup compositor
7290 */
Benjamin Franzkeb8263022011-08-30 11:32:47 +02007291WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007292weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07007293{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05007294 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07007295
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007296 wl_event_source_remove(ec->idle_source);
7297
Matt Roper361d2ad2011-08-29 13:52:23 -07007298 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02007299 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07007300 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02007301
Armin Krezovića01ab6d2016-09-30 14:11:02 +02007302 /* Destroy all pending outputs associated with this compositor */
7303 wl_list_for_each_safe(output, next, &ec->pending_output_list, link)
7304 output->destroy(output);
7305
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02007306 if (ec->renderer)
7307 ec->renderer->destroy(ec);
7308
Daniel Stone325fc2d2012-05-30 16:31:58 +01007309 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09007310 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01007311 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01007312 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01007313 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02007314 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02007315
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04007316 weston_plane_release(&ec->primary_plane);
Matt Roper361d2ad2011-08-29 13:52:23 -07007317}
7318
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007319/** weston_compositor_exit_with_code
7320 * \ingroup compositor
7321 */
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05007322WL_EXPORT void
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007323weston_compositor_exit_with_code(struct weston_compositor *compositor,
Frederic Plourdec336f062014-10-29 14:44:33 -04007324 int exit_code)
7325{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02007326 if (compositor->exit_code == EXIT_SUCCESS)
7327 compositor->exit_code = exit_code;
7328
Giulio Camuffo459137b2014-10-11 23:56:24 +03007329 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04007330}
7331
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007332/** weston_compositor_set_default_pointer_grab
7333 * \ingroup compositor
7334 */
Frederic Plourdec336f062014-10-29 14:44:33 -04007335WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007336weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
7337 const struct weston_pointer_grab_interface *interface)
7338{
7339 struct weston_seat *seat;
7340
7341 ec->default_pointer_grab = interface;
7342 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05007343 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
7344
7345 if (pointer)
7346 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007347 }
7348}
7349
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007350/** weston_compositor_set_presentation_clock
7351 * \ingroup compositor
7352 */
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007353WL_EXPORT int
7354weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
7355 clockid_t clk_id)
7356{
7357 struct timespec ts;
7358
7359 if (clock_gettime(clk_id, &ts) < 0)
7360 return -1;
7361
7362 compositor->presentation_clock = clk_id;
7363
7364 return 0;
7365}
7366
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007367/** For choosing the software clock, when the display hardware or API
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007368 * does not expose a compatible presentation timestamp.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007369 *
7370 * \ingroup compositor
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04007371 */
7372WL_EXPORT int
7373weston_compositor_set_presentation_clock_software(
7374 struct weston_compositor *compositor)
7375{
7376 /* In order of preference */
7377 static const clockid_t clocks[] = {
7378 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
7379 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
7380 CLOCK_MONOTONIC, /* no jumps, may crawl */
7381 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
7382 CLOCK_REALTIME /* may jump and crawl */
7383 };
7384 unsigned i;
7385
7386 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
7387 if (weston_compositor_set_presentation_clock(compositor,
7388 clocks[i]) == 0)
7389 return 0;
7390
7391 weston_log("Error: no suitable presentation clock available.\n");
7392
7393 return -1;
7394}
7395
Pekka Paalanen662f3842015-03-18 12:17:26 +02007396/** Read the current time from the Presentation clock
7397 *
7398 * \param compositor
Marius Vlada2dace22019-06-12 16:05:44 +03007399 * \param[out] ts The current time.
Pekka Paalanen662f3842015-03-18 12:17:26 +02007400 *
7401 * \note Reading the current time in user space is always imprecise to some
7402 * degree.
7403 *
7404 * This function is never meant to fail. If reading the clock does fail,
7405 * an error message is logged and a zero time is returned. Callers are not
7406 * supposed to detect or react to failures.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007407 *
7408 * \ingroup compositor
Pekka Paalanen662f3842015-03-18 12:17:26 +02007409 */
7410WL_EXPORT void
7411weston_compositor_read_presentation_clock(
7412 const struct weston_compositor *compositor,
7413 struct timespec *ts)
7414{
7415 static bool warned;
7416 int ret;
7417
7418 ret = clock_gettime(compositor->presentation_clock, ts);
7419 if (ret < 0) {
7420 ts->tv_sec = 0;
7421 ts->tv_nsec = 0;
7422
7423 if (!warned)
7424 weston_log("Error: failure to read "
Antonio Borneo39578632019-04-26 23:57:31 +02007425 "the presentation clock %#x: '%s' (%d)\n",
7426 compositor->presentation_clock,
7427 strerror(errno), errno);
Pekka Paalanen662f3842015-03-18 12:17:26 +02007428 warned = true;
7429 }
7430}
7431
Pekka Paalanen230f3b12014-09-29 14:18:40 -04007432/** Import dmabuf buffer into current renderer
7433 *
7434 * \param compositor
7435 * \param buffer the dmabuf buffer to import
7436 * \return true on usable buffers, false otherwise
7437 *
7438 * This function tests that the linux_dmabuf_buffer is usable
7439 * for the current renderer. Returns false on unusable buffers. Usually
7440 * usability is tested by importing the dmabufs for composition.
7441 *
7442 * This hook is also used for detecting if the renderer supports
7443 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
7444 * supported.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007445 *
7446 * \ingroup compositor
7447 */
Pekka Paalanen230f3b12014-09-29 14:18:40 -04007448WL_EXPORT bool
7449weston_compositor_import_dmabuf(struct weston_compositor *compositor,
7450 struct linux_dmabuf_buffer *buffer)
7451{
7452 struct weston_renderer *renderer;
7453
7454 renderer = compositor->renderer;
7455
7456 if (renderer->import_dmabuf == NULL)
7457 return false;
7458
7459 return renderer->import_dmabuf(compositor, buffer);
7460}
7461
Giulio Camuffocdb4d292013-11-14 23:42:53 +01007462WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05007463weston_version(int *major, int *minor, int *micro)
7464{
7465 *major = WESTON_VERSION_MAJOR;
7466 *minor = WESTON_VERSION_MINOR;
7467 *micro = WESTON_VERSION_MICRO;
7468}
7469
Daniel Stonee03c1112016-11-24 20:45:45 +00007470/**
7471 * Attempts to find a module path from the module map specified in the
7472 * environment. If found, writes the full path into the path variable.
7473 *
7474 * The module map is a string in environment variable WESTON_MODULE_MAP, where
7475 * each entry is of the form "name=path" and entries are separated by
7476 * semicolons. Whitespace is significant.
7477 *
7478 * \param name The name to search for.
7479 * \param path Where the path is written to if found.
7480 * \param path_len Allocated bytes at \c path .
7481 * \returns The length of the string written to path on success, or 0 if the
7482 * module was not specified in the environment map or path_len was too small.
7483 */
7484WL_EXPORT size_t
7485weston_module_path_from_env(const char *name, char *path, size_t path_len)
7486{
7487 const char *mapping = getenv("WESTON_MODULE_MAP");
7488 const char *end;
7489 const int name_len = strlen(name);
7490
7491 if (!mapping)
7492 return 0;
7493
7494 end = mapping + strlen(mapping);
7495 while (mapping < end && *mapping) {
7496 const char *filename, *next;
7497
7498 /* early out: impossibly short string */
7499 if (end - mapping < name_len + 1)
7500 return 0;
7501
7502 filename = &mapping[name_len + 1];
7503 next = strchrnul(mapping, ';');
7504
7505 if (strncmp(mapping, name, name_len) == 0 &&
7506 mapping[name_len] == '=') {
7507 size_t file_len = next - filename; /* no trailing NUL */
7508 if (file_len >= path_len)
7509 return 0;
7510 strncpy(path, filename, file_len);
7511 path[file_len] = '\0';
7512 return file_len;
7513 }
7514
7515 mapping = next + 1;
7516 }
7517
7518 return 0;
7519}
7520
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03007521WL_EXPORT void *
7522weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007523{
7524 char path[PATH_MAX];
7525 void *module, *init;
Daniel Stonebeb97e52016-11-28 12:13:54 +00007526 size_t len;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007527
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08007528 if (name == NULL)
7529 return NULL;
7530
Derek Foreman3f86e502015-06-08 11:46:54 -05007531 if (name[0] != '/') {
Daniel Stonee03c1112016-11-24 20:45:45 +00007532 len = weston_module_path_from_env(name, path, sizeof path);
7533 if (len == 0)
Daniel Stonebeb97e52016-11-28 12:13:54 +00007534 len = snprintf(path, sizeof path, "%s/%s",
7535 LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05007536 } else {
Daniel Stonebeb97e52016-11-28 12:13:54 +00007537 len = snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05007538 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007539
Daniel Stonebeb97e52016-11-28 12:13:54 +00007540 /* snprintf returns the length of the string it would've written,
7541 * _excluding_ the NUL byte. So even being equal to the size of
7542 * our buffer is an error here. */
7543 if (len >= sizeof path)
7544 return NULL;
7545
Kristian Høgsberga6813d22012-09-12 12:21:01 -04007546 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
7547 if (module) {
7548 weston_log("Module '%s' already loaded\n", path);
7549 dlclose(module);
7550 return NULL;
7551 }
7552
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03007553 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04007554 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007555 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03007556 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007557 return NULL;
7558 }
7559
7560 init = dlsym(module, entrypoint);
7561 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03007562 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00007563 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04007564 return NULL;
7565 }
7566
7567 return init;
7568}
7569
Giulio Camuffo459137b2014-10-11 23:56:24 +03007570
7571/** Destroys the compositor.
7572 *
7573 * This function cleans up the compositor state and destroys it.
7574 *
7575 * \param compositor The compositor to be destroyed.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007576 *
7577 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007578 */
7579WL_EXPORT void
7580weston_compositor_destroy(struct weston_compositor *compositor)
7581{
7582 /* prevent further rendering while shutting down */
7583 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
7584
7585 wl_signal_emit(&compositor->destroy_signal, compositor);
7586
7587 weston_compositor_xkb_destroy(compositor);
7588
Giulio Camuffo2d24e642015-10-03 16:25:15 +03007589 if (compositor->backend)
7590 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007591
Pekka Paalanen1adcbac2017-08-14 16:05:35 +03007592 /* The backend is responsible for destroying the heads. */
7593 assert(wl_list_empty(&compositor->head_list));
7594
Pekka Paalanen827b5d22016-06-29 11:54:26 +02007595 weston_plugin_api_destroy_list(compositor);
7596
Pekka Paalanen37e6c9e2017-08-15 13:00:02 +03007597 if (compositor->heads_changed_source)
7598 wl_event_source_remove(compositor->heads_changed_source);
7599
Marius Vlad7e4db952019-04-17 13:47:06 +03007600 weston_compositor_log_scope_destroy(compositor->debug_scene);
Daniel Stonece62cb32018-07-20 09:46:24 +01007601 compositor->debug_scene = NULL;
Marius Vlad3d7d9782019-04-17 12:35:38 +03007602 weston_log_ctx_compositor_destroy(compositor);
Pekka Paalanena5630ea2017-10-12 13:13:42 +02007603
Giulio Camuffo459137b2014-10-11 23:56:24 +03007604 free(compositor);
7605}
7606
7607/** Instruct the compositor to exit.
7608 *
7609 * This functions does not directly destroy the compositor object, it merely
7610 * command it to start the tear down process. It is not guaranteed that the
7611 * tear down will happen immediately.
7612 *
7613 * \param compositor The compositor to tear down.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007614 *
7615 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007616 */
7617WL_EXPORT void
7618weston_compositor_exit(struct weston_compositor *compositor)
7619{
7620 compositor->exit(compositor);
7621}
7622
7623/** Return the user data stored in the compositor.
7624 *
7625 * This function returns the user data pointer set with user_data parameter
7626 * to the \ref weston_compositor_create function.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007627 *
7628 * \ingroup compositor
Giulio Camuffo459137b2014-10-11 23:56:24 +03007629 */
7630WL_EXPORT void *
7631weston_compositor_get_user_data(struct weston_compositor *compositor)
7632{
7633 return compositor->user_data;
7634}
Pekka Paalanendd186732016-06-03 14:49:54 +03007635
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007636static const char * const backend_map[] = {
7637 [WESTON_BACKEND_DRM] = "drm-backend.so",
7638 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
7639 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
7640 [WESTON_BACKEND_RDP] = "rdp-backend.so",
7641 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
7642 [WESTON_BACKEND_X11] = "x11-backend.so",
7643};
7644
Pekka Paalanendd186732016-06-03 14:49:54 +03007645/** Load a backend into a weston_compositor
7646 *
7647 * A backend must be loaded to make a weston_compositor work. A backend
7648 * provides input and output capabilities, and determines the renderer to use.
7649 *
7650 * \param compositor A compositor that has not had a backend loaded yet.
7651 * \param backend Name of the backend file.
7652 * \param config_base A pointer to a backend-specific configuration
7653 * structure's 'base' member.
7654 *
7655 * \return 0 on success, or -1 on error.
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007656 *
7657 * \ingroup compositor
Pekka Paalanendd186732016-06-03 14:49:54 +03007658 */
7659WL_EXPORT int
7660weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007661 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03007662 struct weston_backend_config *config_base)
7663{
7664 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03007665 struct weston_backend_config *config_base);
7666
Pekka Paalanend7e35112017-08-29 17:04:12 +03007667 if (compositor->backend) {
7668 weston_log("Error: attempt to load a backend when one is already loaded\n");
7669 return -1;
7670 }
7671
Quentin Glidic887c0182016-07-10 11:00:53 +02007672 if (backend >= ARRAY_LENGTH(backend_map))
Pekka Paalanen50dbf382016-06-03 15:23:46 +03007673 return -1;
7674
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01007675 backend_init = weston_load_module(backend_map[backend], "weston_backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03007676 if (!backend_init)
7677 return -1;
7678
Pekka Paalanend7e35112017-08-29 17:04:12 +03007679 if (backend_init(compositor, config_base) < 0) {
7680 compositor->backend = NULL;
7681 return -1;
7682 }
7683
7684 return 0;
Pekka Paalanendd186732016-06-03 14:49:54 +03007685}
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007686
Marius Vlad9fdda7f2019-06-11 16:08:55 +03007687/** weston_compositor_load_xwayland
7688 * \ingroup compositor
7689 */
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007690WL_EXPORT int
7691weston_compositor_load_xwayland(struct weston_compositor *compositor)
7692{
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007693 int (*module_init)(struct weston_compositor *ec);
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007694
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007695 module_init = weston_load_module("xwayland.so", "weston_module_init");
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007696 if (!module_init)
7697 return -1;
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01007698 if (module_init(compositor) < 0)
Giulio Camuffo9c764df2016-06-29 11:54:27 +02007699 return -1;
7700 return 0;
7701}
Vasilis Liaskovitis486b4632018-10-10 16:14:55 +02007702
7703/** Resolve an internal compositor error by disconnecting the client.
7704 *
7705 * This function is used in cases when the wl_buffer turns out
7706 * unusable and there is no fallback path.
7707 *
7708 * It is possible the fault is caused by a compositor bug, the underlying
7709 * graphics stack bug or normal behaviour, or perhaps a client mistake.
7710 * In any case, the options are to either composite garbage or nothing,
7711 * or disconnect the client. This is a helper function for the latter.
7712 *
7713 * The error is sent as an INVALID_OBJECT error on the client's wl_display.
7714 *
7715 * \param buffer The weston buffer that is unusable.
7716 * \param msg A custom error message attached to the protocol error.
7717 */
7718WL_EXPORT void
7719weston_buffer_send_server_error(struct weston_buffer *buffer,
7720 const char *msg)
7721{
7722 struct wl_client *client;
7723 struct wl_resource *display_resource;
7724 uint32_t id;
7725
7726 assert(buffer->resource);
7727 id = wl_resource_get_id(buffer->resource);
7728 client = wl_resource_get_client(buffer->resource);
7729 display_resource = wl_client_get_object(client, 1);
7730
7731 assert(display_resource);
7732 wl_resource_post_error(display_resource,
7733 WL_DISPLAY_ERROR_INVALID_OBJECT,
7734 "server error with "
7735 "wl_buffer@%u: %s", id, msg);
7736}